@awayjs/stage
Version:
Stage for AwayJS
91 lines (90 loc) • 2.98 kB
JavaScript
var PixelBufferWebGL = /** @class */ (function () {
function PixelBufferWebGL(_contex, size) {
if (size === void 0) { size = 0; }
this._contex = _contex;
this._alreadyInit = false;
this._size = 0;
this._gl = _contex._gl;
this._buffer = this._gl.createBuffer();
this._size = size;
_contex.stats.counter.pbo++;
}
PixelBufferWebGL.unboundAll = function () {
// if there are bounded, we should unbound it
if (this._lastBounded) {
this._lastBounded.unbind();
}
};
PixelBufferWebGL.isSuported = function (gl) {
return (window.WebGL2RenderingContext && gl instanceof window.WebGL2RenderingContext);
};
Object.defineProperty(PixelBufferWebGL.prototype, "alive", {
get: function () {
return !!this._buffer;
},
enumerable: false,
configurable: true
});
/**
* sync
*/
PixelBufferWebGL.prototype.read = function (buffer) {
var size = buffer.length;
if (size <= 0 || size !== this._size) {
throw 'Buffer length MUST be a same size as buffer on read state!';
}
var gl = this._gl;
if (PixelBufferWebGL._lastBounded !== this) {
gl.bindBuffer(gl.PIXEL_PACK_BUFFER, this._buffer);
PixelBufferWebGL._lastBounded = this;
}
gl.getBufferSubData(gl.PIXEL_PACK_BUFFER, 0, buffer);
//this.unbind();
};
/**
* bind
*/
PixelBufferWebGL.prototype.bind = function (size) {
if (size === void 0) { size = 0; }
if (this._size === 0 && size === 0) {
throw 'Buffer require size for bounding';
}
var gl = this._gl;
if (PixelBufferWebGL._lastBounded !== this)
gl.bindBuffer(gl.PIXEL_PACK_BUFFER, this._buffer);
if (!this._alreadyInit || this._size !== size && size > 0) {
this._contex.stats.memory.pbo -= this._size;
if (size > 0) {
this._size = size;
}
gl.bufferData(gl.PIXEL_PACK_BUFFER, this._size, gl.DYNAMIC_READ);
this._alreadyInit = true;
this._contex.stats.memory.pbo += this._size;
}
PixelBufferWebGL._lastBounded = this;
};
/**
* unbind
*/
PixelBufferWebGL.prototype.unbind = function () {
if (PixelBufferWebGL._lastBounded !== this) {
return;
}
this._gl.bindBuffer(this._gl.PIXEL_PACK_BUFFER, null);
PixelBufferWebGL._lastBounded = null;
};
/**
* destroy
*/
PixelBufferWebGL.prototype.dispose = function () {
this.unbind();
this._gl.deleteBuffer(this._buffer);
this._buffer = null;
this._size = 0;
this._gl = null;
this._contex.stats.counter.pbo--;
this._contex.stats.memory.pbo += this._size;
};
return PixelBufferWebGL;
}());
export { PixelBufferWebGL };