Skip to content

Commit 5059045

Browse files
committed
Cache attribute location
* `WebGLPipeline.boot` will now check all of the attributes and store the pointer location within the attribute entry. * `WebGLPipeline.bind` no longer looks-up and enables every attribute, every frame. Instead it uses the cached pointer location stored in the attribute entry, cutting down on redundant WebGL operations.
1 parent 98d6478 commit 5059045

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

src/renderer/webgl/WebGLPipeline.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ var WebGLPipeline = new Class({
163163
this.vertexBuffer = this.renderer.createVertexBuffer((config.vertices ? config.vertices : this.vertexData.byteLength), this.gl.STREAM_DRAW);
164164

165165
/**
166-
* The handle to a WebGL program
166+
* The handle to a WebGL program.
167167
*
168168
* @name Phaser.Renderer.WebGL.WebGLPipeline#program
169169
* @type {WebGLProgram}
@@ -172,7 +172,7 @@ var WebGLPipeline = new Class({
172172
this.program = this.renderer.createProgram(config.vertShader, config.fragShader);
173173

174174
/**
175-
* Array of objects that describe the vertex attributes
175+
* Array of objects that describe the vertex attributes.
176176
*
177177
* @name Phaser.Renderer.WebGL.WebGLPipeline#attributes
178178
* @type {object}
@@ -181,7 +181,7 @@ var WebGLPipeline = new Class({
181181
this.attributes = config.attributes;
182182

183183
/**
184-
* The size in bytes of the vertex
184+
* The size in bytes of the vertex.
185185
*
186186
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexSize
187187
* @type {integer}
@@ -190,7 +190,7 @@ var WebGLPipeline = new Class({
190190
this.vertexSize = config.vertexSize;
191191

192192
/**
193-
* The primitive topology which the pipeline will use to submit draw calls
193+
* The primitive topology which the pipeline will use to submit draw calls.
194194
*
195195
* @name Phaser.Renderer.WebGL.WebGLPipeline#topology
196196
* @type {integer}
@@ -199,8 +199,7 @@ var WebGLPipeline = new Class({
199199
this.topology = config.topology;
200200

201201
/**
202-
* Uint8 view to the vertex raw buffer. Used for uploading vertex buffer resources
203-
* to the GPU.
202+
* Uint8 view to the vertex raw buffer. Used for uploading vertex buffer resources to the GPU.
204203
*
205204
* @name Phaser.Renderer.WebGL.WebGLPipeline#bytes
206205
* @type {Uint8Array}
@@ -209,7 +208,7 @@ var WebGLPipeline = new Class({
209208
this.bytes = new Uint8Array(this.vertexData);
210209

211210
/**
212-
* This will store the amount of components of 32 bit length
211+
* This will store the amount of components of 32 bit length.
213212
*
214213
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexComponentCount
215214
* @type {integer}
@@ -249,6 +248,35 @@ var WebGLPipeline = new Class({
249248
*/
250249
boot: function ()
251250
{
251+
var gl = this.gl;
252+
var vertexBuffer = this.vertexBuffer;
253+
var attributes = this.attributes;
254+
var program = this.program;
255+
var renderer = this.renderer;
256+
var vertexSize = this.vertexSize;
257+
258+
renderer.setProgram(program);
259+
renderer.setVertexBuffer(vertexBuffer);
260+
261+
for (var i = 0; i < attributes.length; i++)
262+
{
263+
var element = attributes[i];
264+
var location = gl.getAttribLocation(program, element.name);
265+
266+
if (location >= 0)
267+
{
268+
gl.enableVertexAttribArray(location);
269+
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexSize, element.offset);
270+
element.enabled = true;
271+
element.location = location;
272+
}
273+
else if (location !== -1)
274+
{
275+
gl.disableVertexAttribArray(location);
276+
}
277+
}
278+
279+
return this;
252280
},
253281

254282
/**
@@ -272,7 +300,9 @@ var WebGLPipeline = new Class({
272300
size: size,
273301
type: this.renderer.glFormats[type],
274302
normalized: normalized,
275-
offset: offset
303+
offset: offset,
304+
enabled: false,
305+
location: -1
276306
});
277307

278308
this.vertexComponentCount = Utils.getComponentCount(
@@ -317,7 +347,7 @@ var WebGLPipeline = new Class({
317347
},
318348

319349
/**
320-
* Binds the pipeline resources, including programs, vertex buffers and binds attributes
350+
* Binds the pipeline resources, including the program, vertex buffer and attribute pointers.
321351
*
322352
* @method Phaser.Renderer.WebGL.WebGLPipeline#bind
323353
* @since 3.0.0
@@ -336,19 +366,18 @@ var WebGLPipeline = new Class({
336366
renderer.setProgram(program);
337367
renderer.setVertexBuffer(vertexBuffer);
338368

339-
for (var index = 0; index < attributes.length; ++index)
369+
for (var i = 0; i < attributes.length; i++)
340370
{
341-
var element = attributes[index];
342-
var location = gl.getAttribLocation(program, element.name);
371+
var element = attributes[i];
343372

344-
if (location >= 0)
373+
if (element.enabled)
345374
{
346-
gl.enableVertexAttribArray(location);
347-
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexSize, element.offset);
375+
gl.vertexAttribPointer(element.location, element.size, element.type, element.normalized, vertexSize, element.offset);
348376
}
349-
else if (location !== -1)
377+
else if (!element.enabled && element.location > -1)
350378
{
351-
gl.disableVertexAttribArray(location);
379+
gl.disableVertexAttribArray(element.location);
380+
element.location = -1;
352381
}
353382
}
354383

0 commit comments

Comments
 (0)