Skip to content

Commit 402e6d2

Browse files
committed
Testing making the pipeline responsible for setting the buffer to cut down on potential points of failure
1 parent c6489cf commit 402e6d2

4 files changed

Lines changed: 58 additions & 101 deletions

File tree

CHANGELOG-v3.50.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ All of the internal functions, such as `batchQuad` and `batchSprite` have been u
215215
* `WebGLRenderer.isBooted` is a new boolean property that lets you know if the rendere has fully finished booting.
216216
* The `WebGLRenderer` now extends the Event Emitter, allowing you to listen to renderer specific events.
217217
* `WebGLRenderer.defaultCamera` has been removed as it's not used anywhere internally any longer.
218+
* The `WebGLRenderer.setVertexBuffer` method has been removed along with the `WebGLRenderer.currentVertexBuffer` property. This is now set directly by the WebGL Pipeline, as needed.
219+
* The `WebGLRenderer.setIndexBuffer` method has been removed along with the `WebGLRenderer.currentIndexBuffer` property. This is now set directly by the WebGL Pipeline, as needed.
218220

219221
### WebGL and Canvas Renderer Events
220222

src/renderer/webgl/PipelineManager.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ var PipelineManager = new Class({
721721

722722
/**
723723
* Checks to see if the given pipeline is already the active pipeline, both within this
724-
* Pipeline Manager, and also has the same vertex buffer and shader set within the Renderer.
724+
* Pipeline Manager and also has the same shader set in the Renderer.
725725
*
726726
* @method Phaser.Renderer.WebGL.PipelineManager#isCurrent
727727
* @since 3.50.0
@@ -741,11 +741,7 @@ var PipelineManager = new Class({
741741
currentShader = current.currentShader;
742742
}
743743

744-
return !(
745-
current !== pipeline ||
746-
current.vertexBuffer !== renderer.currentVertexBuffer ||
747-
currentShader.program !== renderer.currentProgram
748-
);
744+
return !(current !== pipeline || currentShader.program !== renderer.currentProgram);
749745
},
750746

751747
/**
@@ -996,14 +992,14 @@ var PipelineManager = new Class({
996992
gl.viewport(0, 0, renderer.width, renderer.height);
997993

998994
renderer.currentProgram = null;
999-
renderer.currentVertexBuffer = null;
1000-
renderer.currentIndexBuffer = null;
1001995

1002996
renderer.setBlendMode(0, true);
1003997

1004998
this.current = pipeline;
1005999

10061000
pipeline.rebind();
1001+
1002+
renderer.resetTextures();
10071003
},
10081004

10091005
/**
@@ -1024,12 +1020,18 @@ var PipelineManager = new Class({
10241020

10251021
this.flush();
10261022

1027-
this.previous = this.current;
1028-
this.current = null;
1023+
if (this.current)
1024+
{
1025+
this.current.unbind();
1026+
this.previous = this.current;
1027+
this.current = null;
1028+
}
1029+
else
1030+
{
1031+
this.previous = null;
1032+
}
10291033

10301034
renderer.currentProgram = null;
1031-
renderer.currentVertexBuffer = null;
1032-
renderer.currentIndexBuffer = null;
10331035

10341036
renderer.setBlendMode(0, true);
10351037
},

src/renderer/webgl/WebGLPipeline.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,13 @@ var WebGLPipeline = new Class({
443443

444444
// Set-up shaders
445445

446-
this.renderer.setVertexBuffer(this.vertexBuffer);
446+
this.setVertexBuffer();
447447

448-
for (i = 0; i < shaders.length; i++)
448+
for (i = shaders.length - 1; i >= 0; i--)
449449
{
450450
shaders[i].rebind();
451451
}
452452

453-
this.currentShader.bind();
454-
455453
this.hasBooted = true;
456454

457455
renderer.on(Events.RESIZE, this.resize, this);
@@ -511,7 +509,12 @@ var WebGLPipeline = new Class({
511509

512510
renderer.resetTextures();
513511

514-
renderer.setVertexBuffer(this.vertexBuffer);
512+
var wasBound = this.setVertexBuffer();
513+
514+
if (wasBound && !setAttributes)
515+
{
516+
setAttributes = true;
517+
}
515518

516519
shader.bind(setAttributes, false);
517520

@@ -743,7 +746,7 @@ var WebGLPipeline = new Class({
743746
{
744747
if (currentShader === undefined) { currentShader = this.currentShader; }
745748

746-
var wasBound = this.renderer.setVertexBuffer(this.vertexBuffer);
749+
var wasBound = this.setVertexBuffer();
747750

748751
currentShader.bind(wasBound);
749752

@@ -766,22 +769,49 @@ var WebGLPipeline = new Class({
766769
*/
767770
rebind: function ()
768771
{
769-
this.renderer.setVertexBuffer(this.vertexBuffer);
772+
var wasBound = this.setVertexBuffer();
770773

771774
var shaders = this.shaders;
772775

773-
for (var i = 0; i < shaders.length; i++)
776+
// Loop in reverse, so the first shader in the array is left as being bound
777+
for (var i = shaders.length - 1; i >= 0; i--)
774778
{
775-
shaders[i].rebind();
779+
this.currentShader = shaders[i].bind(wasBound);
776780
}
777781

778-
this.currentShader = shaders[0];
782+
this.onActive(this.currentShader);
779783

780784
this.onRebind();
781785

782786
return this;
783787
},
784788

789+
/**
790+
* Binds the vertex buffer to be the active ARRAY_BUFFER on the WebGL context.
791+
*
792+
* It first checks to see if it's already set as the active buffer and only
793+
* binds itself if not.
794+
*
795+
* @method Phaser.Renderer.WebGL.WebGLPipeline#setVertexBuffer
796+
* @since 3.50.0
797+
*
798+
* @return {boolean} `true` if the vertex buffer was bound, or `false` if it was already bound.
799+
*/
800+
setVertexBuffer: function ()
801+
{
802+
var gl = this.gl;
803+
var buffer = this.vertexBuffer;
804+
805+
if (gl.getParameter(gl.ARRAY_BUFFER_BINDING) !== buffer)
806+
{
807+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
808+
809+
return true;
810+
}
811+
812+
return false;
813+
},
814+
785815
/**
786816
* This method is called as a result of the `WebGLPipeline.batchQuad` method, right before a quad
787817
* belonging to a Game Object is about to be added to the batch. When this is called, the
@@ -890,6 +920,8 @@ var WebGLPipeline = new Class({
890920

891921
if (this.active)
892922
{
923+
this.setVertexBuffer();
924+
893925
if (vertexCount === this.vertexCapacity)
894926
{
895927
gl.bufferData(gl.ARRAY_BUFFER, this.vertexData, gl.DYNAMIC_DRAW);

src/renderer/webgl/WebGLRenderer.js

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,6 @@ var WebGLRenderer = new Class({
294294
*/
295295
this.currentProgram = null;
296296

297-
/**
298-
* Current WebGLBuffer (Vertex buffer) in use
299-
*
300-
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentVertexBuffer
301-
* @type {WebGLBuffer}
302-
* @default null
303-
* @since 3.0.0
304-
*/
305-
this.currentVertexBuffer = null;
306-
307-
/**
308-
* Current WebGLBuffer (Index buffer) in use
309-
*
310-
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentIndexBuffer
311-
* @type {WebGLBuffer}
312-
* @default null
313-
* @since 3.0.0
314-
*/
315-
this.currentIndexBuffer = null;
316-
317297
/**
318298
* Current blend mode in use
319299
*
@@ -1687,65 +1667,6 @@ var WebGLRenderer = new Class({
16871667
return this;
16881668
},
16891669

1690-
/**
1691-
* Binds a vertex buffer.
1692-
*
1693-
* If there was a different vertex buffer already bound it will force a pipeline flush first.
1694-
*
1695-
* If the same buffer given to this method is already set as the current buffer, no change
1696-
* will take place and this method will return `false`.
1697-
*
1698-
* @method Phaser.Renderer.WebGL.WebGLRenderer#setVertexBuffer
1699-
* @since 3.0.0
1700-
*
1701-
* @param {WebGLBuffer} vertexBuffer - The buffer that needs to be bound.
1702-
*
1703-
* @return {boolean} `true` if the given buffer was bound, otherwise `false`.
1704-
*/
1705-
setVertexBuffer: function (vertexBuffer)
1706-
{
1707-
if (vertexBuffer && vertexBuffer !== this.currentVertexBuffer)
1708-
{
1709-
var gl = this.gl;
1710-
1711-
this.flush();
1712-
1713-
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
1714-
1715-
this.currentVertexBuffer = vertexBuffer;
1716-
1717-
return true;
1718-
}
1719-
1720-
return false;
1721-
},
1722-
1723-
/**
1724-
* Binds an index buffer. If there is an index buffer already bound it'll force a pipeline flush.
1725-
*
1726-
* @method Phaser.Renderer.WebGL.WebGLRenderer#setIndexBuffer
1727-
* @since 3.0.0
1728-
*
1729-
* @param {WebGLBuffer} indexBuffer - The buffer the needs to be bound.
1730-
*
1731-
* @return {this} This WebGLRenderer instance.
1732-
*/
1733-
setIndexBuffer: function (indexBuffer)
1734-
{
1735-
var gl = this.gl;
1736-
1737-
if (indexBuffer && indexBuffer !== this.currentIndexBuffer)
1738-
{
1739-
this.flush();
1740-
1741-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
1742-
1743-
this.currentIndexBuffer = indexBuffer;
1744-
}
1745-
1746-
return this;
1747-
},
1748-
17491670
/**
17501671
* Creates a texture from an image source. If the source is not valid it creates an empty texture.
17511672
*

0 commit comments

Comments
 (0)