phaser/src/renderer/webgl/WebGLPipeline.js

179 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-01-10 20:03:01 +00:00
var Class = require('../../utils/Class');
2018-01-17 21:25:43 +00:00
var WebGLPipeline = new Class({
2018-01-10 20:03:01 +00:00
initialize:
2018-01-17 21:25:43 +00:00
function WebGLPipeline(config)
2018-01-10 20:03:01 +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.gl = 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.topology = config.topology;
// Initialize Shaders and Buffers
{
var gl = this.gl;
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.vertexData.byteLength, 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;
}
},
shouldFlush: function ()
{
return this.vertexCount >= this.vertexCapacity;
},
resize: function (width, height, resolution)
{
2018-01-17 21:25:43 +00:00
this.width = width * resolution;
this.height = height * resolution;
2018-01-10 20:03:01 +00:00
return this;
},
2018-01-17 21:25:43 +00:00
bind: function (overrideProgram)
2018-01-10 20:03:01 +00:00
{
2018-01-17 21:25:43 +00:00
// Check if we're using a custom program or
// the default one from the pipeline.
if (!overrideProgram) this.program.bind();
else overrideProgram.bind();
2018-01-10 20:03:01 +00:00
this.vertexBuffer.bind();
return this;
},
flush: function ()
{
var gl = this.gl;
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);
this.vertexCount = 0;
2018-01-17 21:25:43 +00:00
return this;
},
destroy: function ()
{
var resources = this.resources;
resources.deleteShader(this.program);
resources.deleteBuffer(this.vertexBuffer);
this.program = null;
this.vertexBuffer = null;
2018-01-10 20:03:01 +00:00
return this;
},
2018-01-17 21:25:43 +00:00
setFloat1: function (name, x)
2018-01-10 20:03:01 +00:00
{
2018-01-17 21:25:43 +00:00
this.gl.uniform1f(this.gl.getUniformLocation(this.program.program, name), x);
return this;
},
2018-01-10 20:03:01 +00:00
2018-01-17 21:25:43 +00:00
setFloat2: function (name, x, y)
{
this.gl.uniform2f(this.gl.getUniformLocation(this.program.program, name), x, y);
return this;
},
2018-01-10 20:03:01 +00:00
2018-01-17 21:25:43 +00:00
setFloat3: function (name, x, y, z)
{
this.gl.uniform3f(this.gl.getUniformLocation(this.program.program, name), x, y, z);
return this;
},
2018-01-10 20:03:01 +00:00
2018-01-17 21:25:43 +00:00
setFloat4: function (name, x, y, z, w)
{
this.gl.uniform4f(this.gl.getUniformLocation(this.program.program, name), x, y, z, w);
return this;
},
2018-01-10 20:03:01 +00:00
2018-01-17 21:25:43 +00:00
setInt1: function (name, x)
{
this.gl.uniform1i(this.gl.getUniformLocation(this.program.program, name), x);
2018-01-10 20:03:01 +00:00
return this;
},
2018-01-17 21:25:43 +00:00
setInt2: function (name, x, y)
2018-01-10 20:03:01 +00:00
{
2018-01-17 21:25:43 +00:00
this.gl.uniform2i(this.gl.getUniformLocation(this.program.program, name), x, y);
return this;
},
2018-01-17 21:25:43 +00:00
setInt3: function (name, x, y, z)
{
this.gl.uniform3i(this.gl.getUniformLocation(this.program.program, name), x, y, z);
return this;
},
2018-01-17 21:25:43 +00:00
setInt4: function (name, x, y, z, w)
{
this.gl.uniform4i(this.gl.getUniformLocation(this.program.program, name), x, y, z, w);
return this;
},
2018-01-17 21:25:43 +00:00
setMatrix2: function (name, transpose, matrix)
{
this.gl.uniformMatrix2fv(this.gl.getUniformLocation(this.program.program, name), transpose, matrix);
return this;
},
setMatrix3: function (name, transpose, matrix)
{
this.gl.uniformMatrix2fv(this.gl.getUniformLocation(this.program.program, name), transpose, matrix);
return this;
},
setMatrix4: function (name, transpose, matrix)
{
this.gl.uniformMatrix2fv(this.gl.getUniformLocation(this.program.program, name), transpose, matrix);
2018-01-10 20:03:01 +00:00
return this;
}
});
2018-01-17 21:25:43 +00:00
module.exports = WebGLPipeline;