@awayjs/stage
Version:
Stage for AwayJS
74 lines (73 loc) • 2.67 kB
JavaScript
import { PixelBufferWebGL } from './PixelBufferWebGL';
var FenceContextWebGL = /** @class */ (function () {
function FenceContextWebGL(_context) {
this._context = _context;
this.pool = [];
this._tasks = [];
this._gl = _context._gl;
}
/**
* Async read from PBO.
* @see http://www.songho.ca/opengl/gl_pbo.html
*/
FenceContextWebGL.prototype.readPixels = function (x, y, width, height) {
var _this = this;
var pbo = this.pool.pop() || new PixelBufferWebGL(this._context);
// only for RGBA
pbo.bind(width * height * 4);
var gl = this._gl;
// read to PBO, this is asynce instruction
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, 0);
var fence = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0);
if (!gl.isSync(fence)) {
console.warn('[FenceContextWebGL] Fence return invalid state, closig task immediate:', fence);
return Promise.resolve(pbo);
}
// return a promise that was used for requesting a data
var p = new Promise(function (task) {
_this._tasks.push({ task: task, pbo: pbo, fence: fence });
});
return p;
};
FenceContextWebGL.prototype.tick = function () {
if (this._tasks.length === 0)
return;
var gl = this._gl;
var tasks = this._tasks;
this._tasks = [];
for (var i = 0; i < tasks.length; i++) {
var t = tasks[i];
var closeTask = false;
if (!gl.isSync(t.fence)) {
console.warn('[FenceContextWebGL] Task has invalid fence state, closig task immediate:', t.fence);
closeTask = true;
}
if (closeTask || gl.getSyncParameter(t.fence, gl.SYNC_STATUS) === gl.SIGNALED) {
t.task(t.pbo);
gl.deleteSync(t.fence);
tasks[i] = undefined;
}
else {
this._tasks.push(t);
}
}
};
FenceContextWebGL.prototype.release = function (pbo) {
// push a PBO back to pool;
if (pbo.alive) {
this.pool.push(pbo);
}
};
FenceContextWebGL.prototype.unboundAll = function () {
PixelBufferWebGL.unboundAll();
};
FenceContextWebGL.prototype.dispose = function () {
this._tasks = null;
this.pool.forEach(function (e) { return e.dispose(); });
this.pool = [];
this._gl = null;
};
FenceContextWebGL.isSupported = PixelBufferWebGL.isSuported;
return FenceContextWebGL;
}());
export { FenceContextWebGL };