phaser/src/renderer/webgl/WebGLPipeline.js

118 lines
3.5 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.currentProgram = this.program;
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-17 21:25:43 +00:00
bind: function (overrideProgram)
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;
var program = (!overrideProgram ? this.program : overrideProgram);
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
2018-01-22 21:21:47 +00:00
this.currentProgram = program;
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-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;