Skip to content

Commit 9965ab6

Browse files
committed
Update BlitterBatch
1 parent 65c1e06 commit 9965ab6

8 files changed

Lines changed: 92 additions & 81 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'a3087720-dd97-11e6-a4f9-9d0768ce91d7'
2+
build: 'bc213d90-de6f-11e6-bd32-974da4fb460f'
33
};
44
module.exports = CHECKSUM;

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
var CONST = require('../../const');
99
var CreateEmptyTexture = require('./utils/CreateEmptyTexture');
10+
var BlitterBatch = require('./batches/blitter/BlitterBatch');
1011

1112
var WebGLRenderer = function (game)
1213
{
@@ -44,6 +45,9 @@ var WebGLRenderer = function (game)
4445
this.gl = null;
4546

4647
this.init();
48+
this.blitterBatch = new BlitterBatch(game, this.gl, this);
49+
this.batch = null;
50+
this.currentTexture2D = null;
4751
};
4852

4953
WebGLRenderer.prototype.constructor = WebGLRenderer;
@@ -127,6 +131,12 @@ WebGLRenderer.prototype = {
127131
];
128132
},
129133

134+
setTexture2D: function(texture2D)
135+
{
136+
this.currentTexture = texture2D;
137+
this.batch.dirty = true;
138+
},
139+
130140
resize: function (width, height)
131141
{
132142
var res = this.game.config.resolution;
@@ -177,38 +187,44 @@ WebGLRenderer.prototype = {
177187

178188
var gl = this.gl;
179189

180-
/*
190+
181191

182192
// This is the old render loop - add what you need here to replace it,
183193
// but please allow each State to render to its own Quad FBO
184194

185-
var fbo = state.sys.fbo;
195+
//var fbo = state.sys.fbo;
186196

187-
fbo.activate();
197+
//fbo.activate();
188198

189199
// clear is needed for the FBO, otherwise corruption ...
190200
gl.clear(gl.COLOR_BUFFER_BIT);
191201

192-
this.setBlendMode(CONST.blendModes.NORMAL);
202+
//this.setBlendMode(CONST.blendModes.NORMAL);
193203

194-
this.batch.start();
204+
//this.batch.start();
195205

196206
// Could move to the State Systems or MainLoop
197-
this.game.state.renderChildren(this, state, interpolationPercentage);
207+
for (var c = 0; c < state.sys.children.list.length; c++)
208+
{
209+
var child = state.sys.children.list[c];
198210

199-
this.batch.stop();
211+
child.renderWebGL(this, child, interpolationPercentage);
212+
}
213+
this.batch.flush();
214+
215+
//this.batch.stop();
200216

201217
// Call state.render here, so we can do some extra shizzle on the top
202218
// Maybe pass in the FBO texture too?
203219

204-
fbo.render(null);
220+
//fbo.render(null);
205221

206222
// Unbind the fbo texture and replace it with an empty texture.
207223
// If we forget this we corrupt the main context texture!
208224
// or get `RENDER WARNING: there is no texture bound to the unit 0` spam in the console
209-
gl.bindTexture(gl.TEXTURE_2D, this.emptyTexture);
225+
//gl.bindTexture(gl.TEXTURE_2D, this.emptyTexture);
210226

211-
*/
227+
212228

213229
// console.log('%c render end ', 'color: #ffffff; background: #ff0000;');
214230

@@ -218,8 +234,8 @@ WebGLRenderer.prototype = {
218234
destroy: function ()
219235
{
220236
this.gl = null;
221-
}
222-
237+
},
238+
createFBO: function () {}
223239
};
224240

225241
module.exports = WebGLRenderer;

v3/src/renderer/webgl/renderers/particle/ParticleRenderer.js renamed to v3/src/renderer/webgl/batches/blitter/BlitterBatch.js

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var VertexArray = require('../../utils/VertexArray');
1212
var PHASER_CONST = require('../../../../const');
1313
var CONST = require('./const');
1414

15-
var ParticleRenderer = function (game)
15+
var BlitterBatch = function (game, gl, manager)
1616
{
1717
this.game = game;
1818
this.type = PHASER_CONST.WEBGL;
@@ -22,7 +22,7 @@ var ParticleRenderer = function (game)
2222
this.width = game.config.width * game.config.resolution;
2323
this.height = game.config.height * game.config.resolution;
2424

25-
this.glContext = null;
25+
this.glContext = gl;
2626

2727
this.maxParticles = null;
2828

@@ -57,78 +57,73 @@ var ParticleRenderer = function (game)
5757
}
5858
};
5959

60-
this.init();
60+
this.manager = manager;
61+
this.dirty = false;
62+
63+
this.init(this.glContext);
6164
};
6265

63-
ParticleRenderer.prototype.constructor = ParticleRenderer;
66+
BlitterBatch.prototype.constructor = BlitterBatch;
6467

65-
ParticleRenderer.prototype = {
68+
BlitterBatch.prototype = {
6669

67-
init: function ()
70+
init: function (gl)
6871
{
69-
if (this.glContext === null)
70-
{
71-
var gl = this.view.getContext('webgl', this.config.WebGLContextOptions) || this.view.getContext('experimental-webgl', this.config.WebGLContextOptions);
7272

73-
var vertexDataBuffer = new VertexBuffer(CONST.VERTEX_SIZE * CONST.PARTICLE_VERTEX_COUNT * CONST.MAX_PARTICLES);
73+
var vertexDataBuffer = new VertexBuffer(CONST.VERTEX_SIZE * CONST.PARTICLE_VERTEX_COUNT * CONST.MAX_PARTICLES);
7474

75-
var indexDataBuffer = new IndexBuffer(CONST.INDEX_SIZE * CONST.PARTICLE_INDEX_COUNT * CONST.MAX_PARTICLES);
75+
var indexDataBuffer = new IndexBuffer(CONST.INDEX_SIZE * CONST.PARTICLE_INDEX_COUNT * CONST.MAX_PARTICLES);
7676

77-
var vertShader = CreateShader(gl, CONST.VERTEX_SHADER_SOURCE, gl.VERTEX_SHADER);
78-
var fragShader = CreateShader(gl, CONST.FRAGMENT_SHADER_SOURCE, gl.FRAGMENT_SHADER);
79-
var program = CreateProgram(gl, vertShader, fragShader);
77+
var vertShader = CreateShader(gl, CONST.VERTEX_SHADER_SOURCE, gl.VERTEX_SHADER);
78+
var fragShader = CreateShader(gl, CONST.FRAGMENT_SHADER_SOURCE, gl.FRAGMENT_SHADER);
79+
var program = CreateProgram(gl, vertShader, fragShader);
8080

81-
var indexBufferObject = CreateBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW, null, indexDataBuffer.getByteCapacity());
81+
var indexBufferObject = CreateBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW, null, indexDataBuffer.getByteCapacity());
8282

83-
var attribArray = [
84-
CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0),
85-
CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8)
86-
];
83+
var attribArray = [
84+
CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0),
85+
CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8)
86+
];
8787

88-
var vertexArray = new VertexArray(CreateBuffer(gl, gl.ARRAY_BUFFER, gl.STREAM_DRAW, null, vertexDataBuffer.getByteCapacity()), attribArray);
88+
var vertexArray = new VertexArray(CreateBuffer(gl, gl.ARRAY_BUFFER, gl.STREAM_DRAW, null, vertexDataBuffer.getByteCapacity()), attribArray);
8989

90-
var viewMatrixLocation = gl.getUniformLocation(program, 'u_view_matrix');
90+
var viewMatrixLocation = gl.getUniformLocation(program, 'u_view_matrix');
9191

92-
this.vertexDataBuffer = vertexDataBuffer;
93-
this.indexDataBuffer = indexDataBuffer;
92+
this.vertexDataBuffer = vertexDataBuffer;
93+
this.indexDataBuffer = indexDataBuffer;
9494

95-
this.vertShader = vertShader;
96-
this.fragShader = fragShader;
97-
this.program = program;
95+
this.vertShader = vertShader;
96+
this.fragShader = fragShader;
97+
this.program = program;
9898

99-
this.indexBufferObject = indexBufferObject;
100-
this.vertexArray = vertexArray;
99+
this.indexBufferObject = indexBufferObject;
100+
this.vertexArray = vertexArray;
101101

102-
this.glContext = gl;
103102

104-
this.viewMatrixLocation = viewMatrixLocation;
103+
this.viewMatrixLocation = viewMatrixLocation;
105104

106-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferObject);
105+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferObject);
107106

108-
var indexBuffer = indexDataBuffer.wordView;
109-
var max = CONST.MAX_PARTICLES * CONST.PARTICLE_INDEX_COUNT;
107+
var indexBuffer = indexDataBuffer.wordView;
108+
var max = CONST.MAX_PARTICLES * CONST.PARTICLE_INDEX_COUNT;
110109

111110
// Populate the index buffer only once
112-
for (var indexA = 0, indexB = 0; indexA < max; indexA += CONST.PARTICLE_INDEX_COUNT, indexB += CONST.PARTICLE_VERTEX_COUNT)
111+
for (var indexA = 0, indexB = 0; indexA < max; indexA += CONST.PARTICLE_INDEX_COUNT, indexB += CONST.PARTICLE_VERTEX_COUNT)
113112
{
114-
indexBuffer[indexA + 0] = indexB + 0;
115-
indexBuffer[indexA + 1] = indexB + 1;
116-
indexBuffer[indexA + 2] = indexB + 2;
117-
indexBuffer[indexA + 3] = indexB + 0;
118-
indexBuffer[indexA + 4] = indexB + 2;
119-
indexBuffer[indexA + 5] = indexB + 3;
120-
}
113+
indexBuffer[indexA + 0] = indexB + 0;
114+
indexBuffer[indexA + 1] = indexB + 1;
115+
indexBuffer[indexA + 2] = indexB + 2;
116+
indexBuffer[indexA + 3] = indexB + 0;
117+
indexBuffer[indexA + 4] = indexB + 2;
118+
indexBuffer[indexA + 5] = indexB + 3;
119+
}
120+
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, indexDataBuffer.getUsedBufferAsWord());
121121

122-
this.bind();
122+
this.bind();
123123

124-
this.resize(this.width, this.height);
124+
this.resize(this.width, this.height);
125125

126-
this.unbind();
127-
}
128-
else
129-
{
130-
console.error('ParticleRenderer already initialized');
131-
}
126+
this.unbind();
132127
},
133128

134129
isFull: function ()
@@ -138,9 +133,19 @@ ParticleRenderer.prototype = {
138133

139134
add: function (x, y, width, height, umin, vmin, umax, vmax)
140135
{
141-
// The user must check if the buffers are full before flushing
142-
// this is to give freedom of when should the renderer flush.
136+
var manager = this.manager;
137+
if (manager.batch !== this || this.dirty)
138+
{
139+
if (manager.batch)
140+
manager.batch.flush();
141+
142+
this.bind();
143+
this.setTexture2D(manager.currentTexture2D, true);
144+
manager.batch = this;
145+
}
143146

147+
// The user must check if the buffers are full before flushing
148+
// this is to give freedom of when should the renderer flush. var vertexDataBuffer = this.vertexDataBuffer;
144149
var vertexDataBuffer = this.vertexDataBuffer;
145150
var vertexBuffer = vertexDataBuffer.floatView;
146151
var vertexOffset = vertexDataBuffer.allocate(CONST.PARTICLE_VERTEX_COMPONENT_COUNT * CONST.PARTICLE_VERTEX_COUNT);
@@ -168,11 +173,11 @@ ParticleRenderer.prototype = {
168173
this.elementCount += CONST.PARTICLE_INDEX_COUNT;
169174
},
170175

171-
setTexture2D: function (texture2D)
176+
setTexture2D: function (texture2D, force)
172177
{
173178
var gl = this.glContext;
174179

175-
if (this.currentTexture2D !== texture2D)
180+
if (this.currentTexture2D !== texture2D || force)
176181
{
177182
this.flush();
178183

@@ -183,16 +188,6 @@ ParticleRenderer.prototype = {
183188
}
184189
},
185190

186-
render: function ()
187-
{
188-
// Stops it breaking
189-
},
190-
191-
createFBO: function ()
192-
{
193-
// Stops it breaking
194-
},
195-
196191
bind: function ()
197192
{
198193
var gl = this.glContext;
@@ -282,4 +277,4 @@ ParticleRenderer.prototype = {
282277

283278
};
284279

285-
module.exports = ParticleRenderer;
280+
module.exports = BlitterBatch;

v3/src/renderer/webgl/renderers/particle/FragmentShader.js renamed to v3/src/renderer/webgl/batches/blitter/FragmentShader.js

File renamed without changes.

v3/src/renderer/webgl/renderers/particle/VertexShader.js renamed to v3/src/renderer/webgl/batches/blitter/VertexShader.js

File renamed without changes.
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var SpriteBatch = function ()
2+
{};
3+
4+
module.exports = SpriteBatch;

v3/src/renderer/webgl/renderers/sprite/SpriteRenderer.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)