Update BlitterBatch

This commit is contained in:
Felipe Alfonso 2017-01-19 14:53:20 -03:00
parent 65c1e062ea
commit 9965ab674d
8 changed files with 94 additions and 83 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = { var CHECKSUM = {
build: 'a3087720-dd97-11e6-a4f9-9d0768ce91d7' build: 'bc213d90-de6f-11e6-bd32-974da4fb460f'
}; };
module.exports = CHECKSUM; module.exports = CHECKSUM;

View file

@ -7,6 +7,7 @@
var CONST = require('../../const'); var CONST = require('../../const');
var CreateEmptyTexture = require('./utils/CreateEmptyTexture'); var CreateEmptyTexture = require('./utils/CreateEmptyTexture');
var BlitterBatch = require('./batches/blitter/BlitterBatch');
var WebGLRenderer = function (game) var WebGLRenderer = function (game)
{ {
@ -44,6 +45,9 @@ var WebGLRenderer = function (game)
this.gl = null; this.gl = null;
this.init(); this.init();
this.blitterBatch = new BlitterBatch(game, this.gl, this);
this.batch = null;
this.currentTexture2D = null;
}; };
WebGLRenderer.prototype.constructor = WebGLRenderer; WebGLRenderer.prototype.constructor = WebGLRenderer;
@ -127,6 +131,12 @@ WebGLRenderer.prototype = {
]; ];
}, },
setTexture2D: function(texture2D)
{
this.currentTexture = texture2D;
this.batch.dirty = true;
},
resize: function (width, height) resize: function (width, height)
{ {
var res = this.game.config.resolution; var res = this.game.config.resolution;
@ -177,38 +187,44 @@ WebGLRenderer.prototype = {
var gl = this.gl; var gl = this.gl;
/*
// This is the old render loop - add what you need here to replace it, // This is the old render loop - add what you need here to replace it,
// but please allow each State to render to its own Quad FBO // but please allow each State to render to its own Quad FBO
var fbo = state.sys.fbo; //var fbo = state.sys.fbo;
fbo.activate(); //fbo.activate();
// clear is needed for the FBO, otherwise corruption ... // clear is needed for the FBO, otherwise corruption ...
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
this.setBlendMode(CONST.blendModes.NORMAL); //this.setBlendMode(CONST.blendModes.NORMAL);
this.batch.start(); //this.batch.start();
// Could move to the State Systems or MainLoop // Could move to the State Systems or MainLoop
this.game.state.renderChildren(this, state, interpolationPercentage); for (var c = 0; c < state.sys.children.list.length; c++)
{
var child = state.sys.children.list[c];
this.batch.stop(); child.renderWebGL(this, child, interpolationPercentage);
}
this.batch.flush();
//this.batch.stop();
// Call state.render here, so we can do some extra shizzle on the top // Call state.render here, so we can do some extra shizzle on the top
// Maybe pass in the FBO texture too? // Maybe pass in the FBO texture too?
fbo.render(null); //fbo.render(null);
// Unbind the fbo texture and replace it with an empty texture. // Unbind the fbo texture and replace it with an empty texture.
// If we forget this we corrupt the main context texture! // If we forget this we corrupt the main context texture!
// or get `RENDER WARNING: there is no texture bound to the unit 0` spam in the console // or get `RENDER WARNING: there is no texture bound to the unit 0` spam in the console
gl.bindTexture(gl.TEXTURE_2D, this.emptyTexture); //gl.bindTexture(gl.TEXTURE_2D, this.emptyTexture);
*/
// console.log('%c render end ', 'color: #ffffff; background: #ff0000;'); // console.log('%c render end ', 'color: #ffffff; background: #ff0000;');
@ -218,8 +234,8 @@ WebGLRenderer.prototype = {
destroy: function () destroy: function ()
{ {
this.gl = null; this.gl = null;
} },
createFBO: function () {}
}; };
module.exports = WebGLRenderer; module.exports = WebGLRenderer;

View file

@ -12,7 +12,7 @@ var VertexArray = require('../../utils/VertexArray');
var PHASER_CONST = require('../../../../const'); var PHASER_CONST = require('../../../../const');
var CONST = require('./const'); var CONST = require('./const');
var ParticleRenderer = function (game) var BlitterBatch = function (game, gl, manager)
{ {
this.game = game; this.game = game;
this.type = PHASER_CONST.WEBGL; this.type = PHASER_CONST.WEBGL;
@ -22,7 +22,7 @@ var ParticleRenderer = function (game)
this.width = game.config.width * game.config.resolution; this.width = game.config.width * game.config.resolution;
this.height = game.config.height * game.config.resolution; this.height = game.config.height * game.config.resolution;
this.glContext = null; this.glContext = gl;
this.maxParticles = null; this.maxParticles = null;
@ -57,78 +57,73 @@ var ParticleRenderer = function (game)
} }
}; };
this.init(); this.manager = manager;
this.dirty = false;
this.init(this.glContext);
}; };
ParticleRenderer.prototype.constructor = ParticleRenderer; BlitterBatch.prototype.constructor = BlitterBatch;
ParticleRenderer.prototype = { BlitterBatch.prototype = {
init: function () init: function (gl)
{ {
if (this.glContext === null)
{
var gl = this.view.getContext('webgl', this.config.WebGLContextOptions) || this.view.getContext('experimental-webgl', this.config.WebGLContextOptions);
var vertexDataBuffer = new VertexBuffer(CONST.VERTEX_SIZE * CONST.PARTICLE_VERTEX_COUNT * CONST.MAX_PARTICLES); var vertexDataBuffer = new VertexBuffer(CONST.VERTEX_SIZE * CONST.PARTICLE_VERTEX_COUNT * CONST.MAX_PARTICLES);
var indexDataBuffer = new IndexBuffer(CONST.INDEX_SIZE * CONST.PARTICLE_INDEX_COUNT * CONST.MAX_PARTICLES); var indexDataBuffer = new IndexBuffer(CONST.INDEX_SIZE * CONST.PARTICLE_INDEX_COUNT * CONST.MAX_PARTICLES);
var vertShader = CreateShader(gl, CONST.VERTEX_SHADER_SOURCE, gl.VERTEX_SHADER); var vertShader = CreateShader(gl, CONST.VERTEX_SHADER_SOURCE, gl.VERTEX_SHADER);
var fragShader = CreateShader(gl, CONST.FRAGMENT_SHADER_SOURCE, gl.FRAGMENT_SHADER); var fragShader = CreateShader(gl, CONST.FRAGMENT_SHADER_SOURCE, gl.FRAGMENT_SHADER);
var program = CreateProgram(gl, vertShader, fragShader); var program = CreateProgram(gl, vertShader, fragShader);
var indexBufferObject = CreateBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW, null, indexDataBuffer.getByteCapacity()); var indexBufferObject = CreateBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW, null, indexDataBuffer.getByteCapacity());
var attribArray = [ var attribArray = [
CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0), CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0),
CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8) CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8)
]; ];
var vertexArray = new VertexArray(CreateBuffer(gl, gl.ARRAY_BUFFER, gl.STREAM_DRAW, null, vertexDataBuffer.getByteCapacity()), attribArray); var vertexArray = new VertexArray(CreateBuffer(gl, gl.ARRAY_BUFFER, gl.STREAM_DRAW, null, vertexDataBuffer.getByteCapacity()), attribArray);
var viewMatrixLocation = gl.getUniformLocation(program, 'u_view_matrix'); var viewMatrixLocation = gl.getUniformLocation(program, 'u_view_matrix');
this.vertexDataBuffer = vertexDataBuffer; this.vertexDataBuffer = vertexDataBuffer;
this.indexDataBuffer = indexDataBuffer; this.indexDataBuffer = indexDataBuffer;
this.vertShader = vertShader; this.vertShader = vertShader;
this.fragShader = fragShader; this.fragShader = fragShader;
this.program = program; this.program = program;
this.indexBufferObject = indexBufferObject; this.indexBufferObject = indexBufferObject;
this.vertexArray = vertexArray; this.vertexArray = vertexArray;
this.glContext = gl;
this.viewMatrixLocation = viewMatrixLocation; this.viewMatrixLocation = viewMatrixLocation;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferObject); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferObject);
var indexBuffer = indexDataBuffer.wordView; var indexBuffer = indexDataBuffer.wordView;
var max = CONST.MAX_PARTICLES * CONST.PARTICLE_INDEX_COUNT; var max = CONST.MAX_PARTICLES * CONST.PARTICLE_INDEX_COUNT;
// Populate the index buffer only once // Populate the index buffer only once
for (var indexA = 0, indexB = 0; indexA < max; indexA += CONST.PARTICLE_INDEX_COUNT, indexB += CONST.PARTICLE_VERTEX_COUNT) for (var indexA = 0, indexB = 0; indexA < max; indexA += CONST.PARTICLE_INDEX_COUNT, indexB += CONST.PARTICLE_VERTEX_COUNT)
{ {
indexBuffer[indexA + 0] = indexB + 0; indexBuffer[indexA + 0] = indexB + 0;
indexBuffer[indexA + 1] = indexB + 1; indexBuffer[indexA + 1] = indexB + 1;
indexBuffer[indexA + 2] = indexB + 2; indexBuffer[indexA + 2] = indexB + 2;
indexBuffer[indexA + 3] = indexB + 0; indexBuffer[indexA + 3] = indexB + 0;
indexBuffer[indexA + 4] = indexB + 2; indexBuffer[indexA + 4] = indexB + 2;
indexBuffer[indexA + 5] = indexB + 3; indexBuffer[indexA + 5] = indexB + 3;
}
this.bind();
this.resize(this.width, this.height);
this.unbind();
}
else
{
console.error('ParticleRenderer already initialized');
} }
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, indexDataBuffer.getUsedBufferAsWord());
this.bind();
this.resize(this.width, this.height);
this.unbind();
}, },
isFull: function () isFull: function ()
@ -138,9 +133,19 @@ ParticleRenderer.prototype = {
add: function (x, y, width, height, umin, vmin, umax, vmax) add: function (x, y, width, height, umin, vmin, umax, vmax)
{ {
// The user must check if the buffers are full before flushing var manager = this.manager;
// this is to give freedom of when should the renderer flush. if (manager.batch !== this || this.dirty)
{
if (manager.batch)
manager.batch.flush();
this.bind();
this.setTexture2D(manager.currentTexture2D, true);
manager.batch = this;
}
// The user must check if the buffers are full before flushing
// this is to give freedom of when should the renderer flush. var vertexDataBuffer = this.vertexDataBuffer;
var vertexDataBuffer = this.vertexDataBuffer; var vertexDataBuffer = this.vertexDataBuffer;
var vertexBuffer = vertexDataBuffer.floatView; var vertexBuffer = vertexDataBuffer.floatView;
var vertexOffset = vertexDataBuffer.allocate(CONST.PARTICLE_VERTEX_COMPONENT_COUNT * CONST.PARTICLE_VERTEX_COUNT); var vertexOffset = vertexDataBuffer.allocate(CONST.PARTICLE_VERTEX_COMPONENT_COUNT * CONST.PARTICLE_VERTEX_COUNT);
@ -168,11 +173,11 @@ ParticleRenderer.prototype = {
this.elementCount += CONST.PARTICLE_INDEX_COUNT; this.elementCount += CONST.PARTICLE_INDEX_COUNT;
}, },
setTexture2D: function (texture2D) setTexture2D: function (texture2D, force)
{ {
var gl = this.glContext; var gl = this.glContext;
if (this.currentTexture2D !== texture2D) if (this.currentTexture2D !== texture2D || force)
{ {
this.flush(); this.flush();
@ -183,16 +188,6 @@ ParticleRenderer.prototype = {
} }
}, },
render: function ()
{
// Stops it breaking
},
createFBO: function ()
{
// Stops it breaking
},
bind: function () bind: function ()
{ {
var gl = this.glContext; var gl = this.glContext;
@ -282,4 +277,4 @@ ParticleRenderer.prototype = {
}; };
module.exports = ParticleRenderer; module.exports = BlitterBatch;

View file

@ -0,0 +1,4 @@
var SpriteBatch = function ()
{};
module.exports = SpriteBatch;

View file

@ -1,4 +0,0 @@
var SpriteRenderer = function ()
{};
module.exports = SpriteRenderer;