phaser/src/renderer/webgl/WebGLPipeline.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-01-10 20:03:01 +00:00
var Class = require('../../utils/Class');
2018-01-22 22:51:15 +00:00
var Utils = require('./Utils');
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;
2018-01-20 04:05:56 +00:00
this.renderer = config.renderer;
this.vertexData = (config.vertices ? config.vertices : new ArrayBuffer(config.vertexCapacity * config.vertexSize));
this.vertexBuffer = this.renderer.createVertexBuffer((config.vertices ? config.vertices : this.vertexData.byteLength), this.gl.STREAM_DRAW);
2018-01-22 22:51:15 +00:00
this.program = this.renderer.createProgram(config.vertShader, config.fragShader);
2018-01-20 04:05:56 +00:00
this.attributes = config.attributes;
2018-01-10 20:03:01 +00:00
this.vertexSize = config.vertexSize;
this.topology = config.topology;
2018-01-22 21:21:47 +00:00
this.bytes = new Uint8Array(this.vertexData);
2018-01-22 22:51:15 +00:00
// This will store the amount of components of 32 bit length
2018-01-23 20:32:20 +00:00
this.vertexComponentCount = Utils.getComponentCount(config.attributes);
2018-01-10 20:03:01 +00:00
},
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-30 03:38:31 +00:00
bind: function ()
2018-01-10 20:03:01 +00:00
{
2018-01-20 04:05:56 +00:00
var gl = this.gl;
var vertexBuffer = this.vertexBuffer;
var attributes = this.attributes;
2018-01-30 03:38:31 +00:00
var program = this.program;
2018-01-20 04:05:56 +00:00
var renderer = this.renderer;
var vertexSize = this.vertexSize;
2018-01-10 20:03:01 +00:00
2018-01-20 04:05:56 +00:00
renderer.setProgram(program);
renderer.setVertexBuffer(vertexBuffer);
for (var index = 0; index < attributes.length; ++index)
{
var element = attributes[index];
var location = gl.getAttribLocation(program, element.name);
if (location >= 0)
{
gl.enableVertexAttribArray(location);
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexSize, element.offset);
}
else
{
gl.disableVertexAttribArray(location);
}
}
2018-01-10 20:03:01 +00:00
return this;
},
2018-01-25 05:26:14 +00:00
onBind: function ()
{
// This is for updating uniform data it's called on each bind attempt.
return this;
},
2018-01-26 23:17:11 +00:00
onPreRender: function ()
{
// called once every frame
return this;
},
2018-01-29 21:46:48 +00:00
onRender: function (scene, camera)
{
// called for each camera
return this;
},
2018-01-26 23:17:11 +00:00
onPostRender: function ()
{
// called once every frame
return this;
},
2018-01-10 20:03:01 +00:00
flush: function ()
{
var gl = this.gl;
var vertexCount = this.vertexCount;
var vertexBuffer = this.vertexBuffer;
var vertexData = this.vertexData;
var topology = this.topology;
2018-01-22 21:21:47 +00:00
var vertexSize = this.vertexSize;
2018-01-10 20:03:01 +00:00
if (vertexCount === 0) return;
2018-01-22 21:21:47 +00:00
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.bytes.subarray(0, vertexCount * vertexSize));
2018-01-10 20:03:01 +00:00
gl.drawArrays(topology, 0, vertexCount);
this.vertexCount = 0;
2018-01-17 21:25:43 +00:00
return this;
},
destroy: function ()
{
2018-01-20 04:05:56 +00:00
var gl = this.gl;
2018-01-17 21:25:43 +00:00
2018-01-20 04:05:56 +00:00
gl.deleteShader(this.program);
gl.deleteBuffer(this.vertexBuffer);
2018-01-17 21:25:43 +00:00
this.program = null;
this.vertexBuffer = null;
2018-01-10 20:03:01 +00:00
return this;
}
});
2018-01-17 21:25:43 +00:00
module.exports = WebGLPipeline;