2018-01-09 01:34:45 +00:00
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
|
2018-01-09 21:00:56 +00:00
|
|
|
var Pipeline = new Class({
|
2018-01-09 01:34:45 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-01-09 21:00:56 +00:00
|
|
|
function Pipeline(config)
|
2018-01-09 01:34:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
this.name = config.name;
|
|
|
|
this.game = config.game;
|
|
|
|
this.view = config.game.canvas;
|
|
|
|
this.resolution = config.game.config.resolution;
|
|
|
|
this.width = config.game.config.width * this.resolution;
|
|
|
|
this.height = config.game.config.height * this.resolution;
|
|
|
|
this.glContext = config.gl;
|
|
|
|
this.vertexCount = 0;
|
|
|
|
this.vertexCapacity = config.vertexCapacity;
|
|
|
|
this.manager = config.manager;
|
|
|
|
this.resources = config.manager.resourceManager;
|
|
|
|
this.vertexData = new ArrayBuffer(config.vertexCapacity * config.vertexSize);
|
|
|
|
this.vertexBuffer = null;
|
|
|
|
this.program = null;
|
|
|
|
this.vertexLayout = config.vertexLayout;
|
|
|
|
this.vertexSize = config.vertexSize;
|
|
|
|
this.currentRenderTarget = null;
|
|
|
|
this.currentProgram = null;
|
2018-01-09 21:00:56 +00:00
|
|
|
this.topology = config.topology;
|
|
|
|
|
|
|
|
// Initialize Shaders and Buffers
|
2018-01-09 01:34:45 +00:00
|
|
|
{
|
2018-01-09 21:00:56 +00:00
|
|
|
var gl = this.glContext;
|
|
|
|
var resources = this.resources;
|
|
|
|
var vertexSize = this.vertexSize;
|
|
|
|
var vertexLayout = this.vertexLayout;
|
|
|
|
var program = resources.createShader(this.name, config.shader);
|
|
|
|
var vertexBuffer = resources.createBuffer(gl.ARRAY_BUFFER, this.vertexCapacity, gl.STREAM_DRAW);
|
|
|
|
|
|
|
|
for (var key in vertexLayout)
|
|
|
|
{
|
|
|
|
var element = vertexLayout[key];
|
|
|
|
|
|
|
|
vertexBuffer.addAttribute(
|
|
|
|
program.getAttribLocation(key),
|
|
|
|
element.size,
|
|
|
|
element.type,
|
|
|
|
element.normalize,
|
|
|
|
vertexSize,
|
|
|
|
element.offset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.vertexBuffer = vertexBuffer;
|
|
|
|
this.program = program;
|
2018-01-09 01:34:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-09 21:00:56 +00:00
|
|
|
beginDraw: function (renderTarget, program)
|
2018-01-09 01:34:45 +00:00
|
|
|
{
|
|
|
|
if (this.currentRenderTarget !== null ||
|
|
|
|
this.currentProgram !== null)
|
|
|
|
{
|
2018-01-09 21:00:56 +00:00
|
|
|
this.draw();
|
|
|
|
this.endDraw();
|
2018-01-09 01:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.currentRenderTarget = (renderTarget || null);
|
|
|
|
this.currentProgram = (program || this.program);
|
2018-01-09 21:00:56 +00:00
|
|
|
this.currentProgram.bind();
|
|
|
|
this.vertexBuffer.bind();
|
|
|
|
|
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.currentRenderTarget.framebufferObject);
|
2018-01-09 01:34:45 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-09 21:00:56 +00:00
|
|
|
draw: function ()
|
2018-01-09 01:34:45 +00:00
|
|
|
{
|
|
|
|
var gl = this.glContext;
|
|
|
|
var vertexCount = this.vertexCount;
|
|
|
|
var vertexBuffer = this.vertexBuffer;
|
|
|
|
var vertexData = this.vertexData;
|
|
|
|
var topology = this.topology;
|
|
|
|
|
|
|
|
if (vertexCount === 0) return;
|
|
|
|
|
|
|
|
vertexBuffer.updateResource(vertexData, 0);
|
|
|
|
gl.drawArrays(topology, 0, vertexCount);
|
2018-01-09 21:00:56 +00:00
|
|
|
|
|
|
|
this.vertexCount = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
endDraw: function ()
|
|
|
|
{
|
|
|
|
var renderTarget = this.currentRenderTarget;
|
|
|
|
var program = this.currentProgram;
|
|
|
|
|
2018-01-09 01:34:45 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
|
|
|
2018-01-09 21:00:56 +00:00
|
|
|
this.currentRenderTarget = null;
|
|
|
|
this.program = null;
|
|
|
|
this.vertexCount = 0;
|
|
|
|
|
2018-01-09 01:34:45 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
var resources = this.resources;
|
|
|
|
|
|
|
|
resources.deleteShader(this.program);
|
|
|
|
resources.deleteBuffer(this.vertexBuffer);
|
|
|
|
|
|
|
|
this.program = null;
|
|
|
|
this.vertexBuffer = null;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = BaseRenderer;
|