Skip to content

Commit bacff65

Browse files
committed
Refactoring texture batch creation
1 parent 5683713 commit bacff65

1 file changed

Lines changed: 65 additions & 49 deletions

File tree

src/renderer/webgl/pipelines/TextureTintPipeline.js

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ var TextureTintPipeline = new Class({
278278

279279
this.mvpUpdate();
280280

281-
if (this.batches.length === 0)
282-
{
283-
this.pushBatch();
284-
}
285-
286281
return this;
287282
},
288283

@@ -308,56 +303,54 @@ var TextureTintPipeline = new Class({
308303
},
309304

310305
/**
311-
* Assigns a texture to the current batch. If a texture is already set it creates
312-
* a new batch object.
306+
* Assigns a texture to the current batch. If a different texture is already set it creates a new batch object.
313307
*
314308
* @method Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline#setTexture2D
315309
* @since 3.1.0
316310
*
317-
* @param {WebGLTexture} texture - WebGLTexture that will be assigned to the current batch.
318-
* @param {integer} textureUnit - Texture unit to which the texture needs to be bound.
311+
* @param {WebGLTexture} [texture] - WebGLTexture that will be assigned to the current batch. If not given uses blankTexture.
312+
* @param {integer} [unit=0] - Texture unit to which the texture needs to be bound.
319313
*
320314
* @return {Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline} This pipeline instance.
321315
*/
322316
setTexture2D: function (texture, unit)
323317
{
324-
if (!texture)
325-
{
326-
texture = this.renderer.blankTexture.glTexture;
327-
unit = 0;
328-
}
318+
if (texture === undefined) { texture = this.renderer.blankTexture.glTexture; }
319+
if (unit === undefined) { unit = 0; }
329320

330-
var batches = this.batches;
331-
332-
if (batches.length === 0)
321+
if (this.requireTextureBatch(texture, unit))
333322
{
334-
this.pushBatch();
323+
this.pushBatch(texture, unit);
335324
}
336325

337-
var batch = batches[batches.length - 1];
326+
return this;
327+
},
338328

339-
if (unit > 0)
340-
{
341-
if (batch.textures[unit - 1] &&
342-
batch.textures[unit - 1] !== texture)
343-
{
344-
this.pushBatch();
345-
}
329+
/**
330+
* Checks if the current batch has the same texture and texture unit, or if we need to create a new batch.
331+
*
332+
* @method Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline#requireTextureBatch
333+
* @since 3.16.0
334+
*
335+
* @param {WebGLTexture} texture - WebGLTexture that will be assigned to the current batch. If not given uses blankTexture.
336+
* @param {integer} unit - Texture unit to which the texture needs to be bound.
337+
*
338+
* @return {boolean} `true` if the pipeline needs to create a new batch, otherwise `false`.
339+
*/
340+
requireTextureBatch: function (texture, unit)
341+
{
342+
var batches = this.batches;
343+
var batchLength = batches.length;
346344

347-
batches[batches.length - 1].textures[unit - 1] = texture;
348-
}
349-
else
345+
if (batchLength > 0)
350346
{
351-
if (batch.texture !== null &&
352-
batch.texture !== texture)
353-
{
354-
this.pushBatch();
355-
}
347+
// If Texture Unit specified, we get the texture from the textures array, otherwise we use the texture property
348+
var currentTexture = (unit > 0) ? batches[batchLength - 1].textures[unit - 1] : batches[batchLength - 1].texture;
356349

357-
batches[batches.length - 1].texture = texture;
350+
return !(currentTexture === texture);
358351
}
359352

360-
return this;
353+
return true;
361354
},
362355

363356
/**
@@ -368,16 +361,32 @@ var TextureTintPipeline = new Class({
368361
*
369362
* @method Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline#pushBatch
370363
* @since 3.1.0
364+
*
365+
* @param {WebGLTexture} texture - Optional WebGLTexture that will be assigned to the created batch.
366+
* @param {integer} unit - Texture unit to which the texture needs to be bound.
371367
*/
372-
pushBatch: function ()
368+
pushBatch: function (texture, unit)
373369
{
374-
var batch = {
375-
first: this.vertexCount,
376-
texture: null,
377-
textures: []
378-
};
370+
if (unit === 0)
371+
{
372+
this.batches.push({
373+
first: this.vertexCount,
374+
texture: texture,
375+
textures: []
376+
});
377+
}
378+
else
379+
{
380+
var textures = [];
381+
382+
textures[unit - 1] = texture;
379383

380-
this.batches.push(batch);
384+
this.batches.push({
385+
first: this.vertexCount,
386+
texture: null,
387+
textures: textures
388+
});
389+
}
381390
},
382391

383392
/**
@@ -420,11 +429,14 @@ var TextureTintPipeline = new Class({
420429

421430
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.bytes.subarray(0, vertexCount * vertexSize));
422431

432+
// Process the TEXTURE BATCHES
433+
423434
for (var index = 0; index < batchCount - 1; index++)
424435
{
425436
batch = batches[index];
426437
batchNext = batches[index + 1];
427438

439+
// Multi-texture check (for non-zero texture units)
428440
if (batch.textures.length > 0)
429441
{
430442
for (textureIndex = 0; textureIndex < batch.textures.length; ++textureIndex)
@@ -433,7 +445,7 @@ var TextureTintPipeline = new Class({
433445

434446
if (nTexture)
435447
{
436-
renderer.setTexture2D(nTexture, 1 + textureIndex);
448+
renderer.setTexture2D(nTexture, 1 + textureIndex, false);
437449
}
438450
}
439451

@@ -442,19 +454,22 @@ var TextureTintPipeline = new Class({
442454

443455
batchVertexCount = batchNext.first - batch.first;
444456

457+
// Bail out if texture property is null (i.e. if a texture unit > 0)
445458
if (batch.texture === null || batchVertexCount <= 0)
446459
{
447460
continue;
448461
}
449462

450-
renderer.setTexture2D(batch.texture, 0);
463+
renderer.setTexture2D(batch.texture, 0, false);
451464

452465
gl.drawArrays(topology, batch.first, batchVertexCount);
453466
}
454467

455468
// Left over data
456469
batch = batches[batchCount - 1];
457470

471+
// Multi-texture check (for non-zero texture units)
472+
458473
if (batch.textures.length > 0)
459474
{
460475
for (textureIndex = 0; textureIndex < batch.textures.length; ++textureIndex)
@@ -463,7 +478,7 @@ var TextureTintPipeline = new Class({
463478

464479
if (nTexture)
465480
{
466-
renderer.setTexture2D(nTexture, 1 + textureIndex);
481+
renderer.setTexture2D(nTexture, 1 + textureIndex, false);
467482
}
468483
}
469484

@@ -474,7 +489,7 @@ var TextureTintPipeline = new Class({
474489

475490
if (batch.texture && batchVertexCount > 0)
476491
{
477-
renderer.setTexture2D(batch.texture, 0);
492+
renderer.setTexture2D(batch.texture, 0, false);
478493

479494
gl.drawArrays(topology, batch.first, batchVertexCount);
480495
}
@@ -483,8 +498,6 @@ var TextureTintPipeline = new Class({
483498

484499
batches.length = 0;
485500

486-
this.pushBatch();
487-
488501
this.flushLocked = false;
489502

490503
return this;
@@ -502,6 +515,7 @@ var TextureTintPipeline = new Class({
502515
*/
503516
batchSprite: function (sprite, camera, parentTransformMatrix)
504517
{
518+
// Will cause a flush if there are batchSize entries already
505519
this.renderer.setPipeline(this);
506520

507521
var camMatrix = this._tempMatrix1;
@@ -619,10 +633,12 @@ var TextureTintPipeline = new Class({
619633
ty3 = Math.round(ty3);
620634
}
621635

636+
// Adds texture to batch (if not present)
622637
this.setTexture2D(texture, 0);
623638

624639
var tintEffect = (sprite._isTinted && sprite.tintFill);
625640

641+
// Flushes batch if full, which can take the texture batch with it
626642
this.batchQuad(tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect);
627643
},
628644

0 commit comments

Comments
 (0)