Skip to content

Instantly share code, notes, and snippets.

@samme
Created September 5, 2017 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samme/c4d0928c94a345976ee022884b2bce58 to your computer and use it in GitHub Desktop.
Save samme/c4d0928c94a345976ee022884b2bce58 to your computer and use it in GitHub Desktop.
Phaser CE issue #194
/**
* Renders the content and empties the current batch.
*
* Modified for {@link https://github.com/photonstorm/phaser-ce/issues/194 WebGL: INVALID_VALUE: vertexAttribPointer: index out of range (Ubuntu)}.
*
* @method PIXI.WebGLSpriteBatch#flush
*/
PIXI.WebGLSpriteBatch.prototype.flush = function () {
// If the batch is length 0 then return as there is nothing to draw
if (this.currentBatchSize === 0) {
return;
}
var gl = this.gl;
var shader;
if (this.dirty) {
this.dirty = false;
shader = this.defaultShader.shaders[gl.id];
// bind the main texture
gl.activeTexture(gl.TEXTURE0);
// bind the buffers
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
// this is the same for each shader?
var stride = this.vertexSize; //this.vertSize * 4;
gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0);
gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false, stride, 8);
// color attributes will be interpreted as unsigned bytes and normalized
gl.vertexAttribPointer(shader.colorAttribute, 4, gl.UNSIGNED_BYTE, true, stride, 16);
// Texture index
// Workaround for <https://github.com/photonstorm/phaser-ce/issues/194>
if (shader.aTextureIndex > -1) {
gl.vertexAttribPointer(shader.aTextureIndex, 1, gl.FLOAT, false, stride, 20);
}
}
// upload the verts to the buffer
if (this.currentBatchSize > (this.size * 0.5)) {
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices);
} else {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
var view = this.positions.subarray(0, this.currentBatchSize * this.vertexSize);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
}
var nextTexture, nextBlendMode, nextShader;
var batchSize = 0;
var start = 0;
var currentBaseTexture = null;
var currentBlendMode = this.renderSession.blendModeManager.currentBlendMode;
var currentShader = null;
var blendSwap = false;
var shaderSwap = false;
var sprite;
var textureIndex = 0;
for (var i = 0, j = this.currentBatchSize; i < j; i++) {
sprite = this.sprites[i];
if (sprite.tilingTexture) {
nextTexture = sprite.tilingTexture.baseTexture;
} else {
nextTexture = sprite.texture.baseTexture;
}
nextBlendMode = sprite.blendMode;
nextShader = sprite.shader || this.defaultShader;
blendSwap = currentBlendMode !== nextBlendMode;
shaderSwap = currentShader !== nextShader; // should I use _UIDS???
var skip = nextTexture.skipRender;
if (skip && sprite.children.length > 0) {
skip = false;
}
//
if (/*(currentBaseTexture != nextTexture && !skip) ||*/
blendSwap ||
shaderSwap) {
this.renderBatch(currentBaseTexture, batchSize, start);
start = i;
batchSize = 0;
currentBaseTexture = nextTexture;
if (blendSwap) {
currentBlendMode = nextBlendMode;
this.renderSession.blendModeManager.setBlendMode(currentBlendMode);
}
if (shaderSwap) {
currentShader = nextShader;
shader = currentShader.shaders[gl.id];
if (!shader) {
shader = new PIXI.PixiShader(gl);
shader.fragmentSrc = currentShader.fragmentSrc;
shader.uniforms = currentShader.uniforms;
shader.init();
currentShader.shaders[gl.id] = shader;
}
// set shader function???
this.renderSession.shaderManager.setShader(shader);
if (shader.dirty) {
shader.syncUniforms();
}
// both these only need to be set if they are changing..
// set the projection
var projection = this.renderSession.projection;
gl.uniform2f(shader.projectionVector, projection.x, projection.y);
// TODO - this is temporary!
var offsetVector = this.renderSession.offset;
gl.uniform2f(shader.offsetVector, offsetVector.x, offsetVector.y);
// set the pointers
}
}
batchSize++;
}
this.renderBatch(currentBaseTexture, batchSize, start);
// then reset the batch!
this.currentBatchSize = 0;
this.renderSession.flushCount++;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment