phaser/src/renderer/webgl/WebGLPipeline.js

109 lines
3.1 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;
2018-01-20 04:05:56 +00:00
this.renderer = config.renderer;
2018-01-10 20:03:01 +00:00
this.vertexData = new ArrayBuffer(config.vertexCapacity * config.vertexSize);
2018-01-22 21:21:47 +00:00
this.vertexBuffer = this.renderer.createVertexBuffer(this.vertexData.byteLength, this.gl.STREAM_DRAW);
this.program = this.renderer.createProgram(config.shader.vert, config.shader.frag);
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-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;
},
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;