Skip to content

Commit d71d7d5

Browse files
committed
Fixed a couple of bugs on Graphics renderer and added should Flush to all webgl renderers
1 parent 0d72f09 commit d71d7d5

8 files changed

Lines changed: 75 additions & 42 deletions

File tree

v3/src/gameobjects/renderpass/RenderPass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ var RenderPass = new Class({
107107

108108
if (gl)
109109
{
110+
this.renderer.setRenderer(this.renderer.spriteBatch, null, null);
110111
this.renderer.spriteBatch.addSprite(gameObject, camera);
111112
this.renderer.spriteBatch.flush(this.passShader, this.passRenderTarget.framebufferObject);
112-
this.renderer.setRenderer(null, null, null);
113113
}
114114
},
115115

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var WebGLRenderer = function (game)
4949
this.blendModes = [];
5050
this.gl = null;
5151
this.extensions = null;
52-
this.currentRendereres = [];
52+
this.rendererArray = [];
5353
this.blitterBatch = null;
5454
this.aaQuadBatch = null;
5555
this.spriteBatch = null;
@@ -92,7 +92,6 @@ WebGLRenderer.prototype = {
9292
gl.enable(gl.BLEND);
9393
gl.clearColor(color.redGL, color.greenGL, color.blueGL, color.alphaGL);
9494

95-
this.resize(this.width, this.height);
9695

9796
// Map Blend Modes
9897

@@ -116,7 +115,9 @@ WebGLRenderer.prototype = {
116115
this.shapeBatch = this.addRenderer(new ShapeBatch(this.game, gl, this));
117116
this.effectRenderer = this.addRenderer(new EffectRenderer(this.game, gl, this));
118117
this.tileBatch = this.addRenderer(new TileBatch(this.game, gl, this));
118+
this.currentRenderer = this.spriteBatch;
119119
this.setBlendMode(0);
120+
this.resize(this.width, this.height);
120121
},
121122

122123
createTexture: function (source, width, height)
@@ -170,14 +171,12 @@ WebGLRenderer.prototype = {
170171
{
171172
if (this.currentTexture !== texture)
172173
{
173-
if (this.currentRenderer)
174-
{
175-
this.currentRenderer.flush();
176-
}
177-
178174
var gl = this.gl;
179175

176+
this.currentRenderer.flush();
177+
180178
gl.activeTexture(gl.TEXTURE0);
179+
181180
if (texture !== null)
182181
{
183182
gl.bindTexture(gl.TEXTURE_2D, texture.texture);
@@ -196,13 +195,9 @@ WebGLRenderer.prototype = {
196195
this.setTexture(texture);
197196
this.setRenderTarget(renderTarget);
198197

199-
if (this.currentRenderer !== renderer)
198+
if (this.currentRenderer !== renderer || this.currentRenderer.shouldFlush())
200199
{
201-
if (this.currentRenderer)
202-
{
203-
this.currentRenderer.flush();
204-
}
205-
200+
this.currentRenderer.flush();
206201
this.currentRenderer = renderer;
207202
}
208203
},
@@ -213,10 +208,7 @@ WebGLRenderer.prototype = {
213208

214209
if (this.currentRenderTarget !== renderTarget)
215210
{
216-
if (this.currentRenderer)
217-
{
218-
this.currentRenderer.flush();
219-
}
211+
this.currentRenderer.flush();
220212

221213
if (renderTarget !== null)
222214
{
@@ -239,30 +231,27 @@ WebGLRenderer.prototype = {
239231

240232
resize: function (width, height)
241233
{
242-
var res = this.game.config.resolution;
234+
var resolution = this.game.config.resolution;
243235

244-
this.width = width * res;
245-
this.height = height * res;
236+
this.width = width * resolution;
237+
this.height = height * resolution;
246238

247239
this.view.width = this.width;
248240
this.view.height = this.height;
249241

250242
if (this.autoResize)
251243
{
252-
this.view.style.width = (this.width / res) + 'px';
253-
this.view.style.height = (this.height / res) + 'px';
244+
this.view.style.width = (this.width / resolution) + 'px';
245+
this.view.style.height = (this.height / resolution) + 'px';
254246
}
255247

256248
this.gl.viewport(0, 0, this.width, this.height);
257-
for (var i = 0, l = this.currentRendereres.length; i < l; ++i)
258-
{
259-
this.currentRendereres[i].bind();
260-
this.currentRendereres[i].resize(width, height, resolution);
261-
}
262-
if (this.currentRenderer)
249+
for (var i = 0, l = this.rendererArray.length; i < l; ++i)
263250
{
264-
this.currentRenderer.bind();
251+
this.rendererArray[i].bind();
252+
this.rendererArray[i].resize(width, height, resolution);
265253
}
254+
this.currentRenderer.bind();
266255
},
267256

268257
// Call at the start of the render loop
@@ -342,15 +331,14 @@ WebGLRenderer.prototype = {
342331
// drawing child
343332
child.renderWebGL(this, child, interpolationPercentage, camera);
344333
renderer = this.currentRenderer;
345-
if (renderer && renderer.isFull())
334+
if (renderer.isFull() || renderer.shouldFlush())
346335
{
347336
renderer.flush();
348337
}
349338
}
350-
if (this.currentRenderer)
351-
{
352-
this.currentRenderer.flush();
353-
}
339+
340+
this.currentRenderer.flush();
341+
354342
if (camera._fadeAlpha > 0 || camera._flashAlpha > 0)
355343
{
356344
this.setRenderTarget(null);
@@ -384,10 +372,7 @@ WebGLRenderer.prototype = {
384372
// Called at the end of the render loop (tidy things up, etc)
385373
postRender: function ()
386374
{
387-
if (this.currentRenderer)
388-
{
389-
this.currentRenderer.flush();
390-
}
375+
this.currentRenderer.flush();
391376

392377
if (this.snapshotCallback)
393378
{
@@ -440,10 +425,10 @@ WebGLRenderer.prototype = {
440425

441426
addRenderer: function (rendererInstance)
442427
{
443-
var index = this.currentRendereres.indexOf(rendererInstance);
428+
var index = this.rendererArray.indexOf(rendererInstance);
444429
if (index < 0)
445430
{
446-
this.currentRendereres.push(rendererInstance);
431+
this.rendererArray.push(rendererInstance);
447432
return rendererInstance;
448433
}
449434
return null;

v3/src/renderer/webgl/renderers/blitterbatch/BlitterBatch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ BlitterBatch.prototype = {
9090
this.resize(this.width, this.height, this.game.config.resolution);
9191
},
9292

93+
shouldFlush: function ()
94+
{
95+
return false;
96+
},
97+
9398
isFull: function ()
9499
{
95100
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());

v3/src/renderer/webgl/renderers/effectrenderer/EffectRenderer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ EffectRenderer.prototype = {
9090

9191
this.resize(this.width, this.height, this.game.config.resolution);
9292
},
93+
94+
shouldFlush: function ()
95+
{
96+
return false;
97+
},
9398

9499
isFull: function ()
95100
{

v3/src/renderer/webgl/renderers/quadbatch/QuadBatch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ QuadBatch.prototype = {
8787
this.resize(this.width, this.height, this.game.config.resolution);
8888
},
8989

90+
shouldFlush: function ()
91+
{
92+
return false;
93+
},
94+
9095
isFull: function ()
9196
{
9297
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());

v3/src/renderer/webgl/renderers/shapebatch/ShapeBatch.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ShapeBatch.prototype = {
7676
this.resize(this.width, this.height, this.game.config.resolution);
7777
},
7878

79+
shouldFlush: function ()
80+
{
81+
return false;
82+
},
83+
7984
isFull: function ()
8085
{
8186
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());
@@ -573,6 +578,7 @@ ShapeBatch.prototype = {
573578
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
574579
tempTriangle, lineWidth, lineColor, lineAlpha,
575580
a, b, c, d, e, f,
581+
false,
576582
currentMatrix
577583
);
578584
}

v3/src/renderer/webgl/renderers/spritebatch/SpriteBatch.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var SpriteBatch = function (game, gl, manager)
4343

4444
this.manager = manager;
4545
this.dirty = false;
46+
this.drawIndexed = true;
47+
this.vertexCount = 0;
4648

4749
this.init(this.glContext);
4850
};
@@ -91,6 +93,11 @@ SpriteBatch.prototype = {
9193
this.resize(this.width, this.height, this.game.config.resolution);
9294
},
9395

96+
shouldFlush: function ()
97+
{
98+
return false;
99+
},
100+
94101
isFull: function ()
95102
{
96103
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());
@@ -128,9 +135,18 @@ SpriteBatch.prototype = {
128135

129136
this.bind(shader);
130137
this.vertexBufferObject.updateResource(vertexDataBuffer.getUsedBufferAsFloat(), 0);
131-
gl.drawElements(gl.TRIANGLES, this.elementCount, gl.UNSIGNED_SHORT, 0);
138+
if (this.drawIndexed)
139+
{
140+
gl.drawElements(gl.TRIANGLES, this.elementCount, gl.UNSIGNED_SHORT, 0);
141+
}
142+
else
143+
{
144+
gl.drawArrays(gl.TRIANGLES, 0, this.vertexCount);
145+
}
146+
132147
vertexDataBuffer.clear();
133148
this.elementCount = 0;
149+
this.vertexCount = 0;
134150

135151
if (renderTarget)
136152
{
@@ -172,6 +188,12 @@ SpriteBatch.prototype = {
172188
this.vertexBufferObject = null;
173189
},
174190

191+
192+
addMesh: function (gameObject, camera)
193+
{
194+
195+
},
196+
175197
addSpriteTexture: function (gameObject, camera, texture, textureWidth, textureHeight)
176198
{
177199
var tempMatrix = this.tempMatrix;

v3/src/renderer/webgl/renderers/tilebatch/TileBatch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ TileBatch.prototype = {
9191
this.resize(this.width, this.height, this.game.config.resolution);
9292
},
9393

94+
shouldFlush: function ()
95+
{
96+
return false;
97+
},
98+
9499
isFull: function ()
95100
{
96101
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());

0 commit comments

Comments
 (0)