@awayjs/stage
Version:
Stage for AwayJS
96 lines (95 loc) • 3.51 kB
JavaScript
var VaoContextWebGL = /** @class */ (function () {
function VaoContextWebGL(_context) {
this._context = _context;
/* internal */ this._isRequireUnbound = false;
var gl = _context._gl;
if (!VaoContextWebGL.isSupported(gl)) {
throw '[VaoContextWebGL] VAO not supported!';
}
if (window.WebGL2RenderingContext && gl instanceof window.WebGL2RenderingContext) {
this._createVertexArray = gl.createVertexArray.bind(gl);
this._deleteVertexArray = gl.deleteVertexArray.bind(gl);
this._bindVertexArray = gl.bindVertexArray.bind(gl);
}
else {
var ext = gl.getExtension('OES_vertex_array_object');
this._createVertexArray = ext.createVertexArrayOES.bind(ext);
this._deleteVertexArray = ext.deleteVertexArrayOES.bind(ext);
this._bindVertexArray = ext.bindVertexArrayOES.bind(ext);
}
}
VaoContextWebGL.isSupported = function (gl) {
if (window.WebGL2RenderingContext && gl instanceof window.WebGL2RenderingContext) {
return true;
}
if (gl.getSupportedExtensions().indexOf('OES_vertex_array_object') > -1) {
return true;
}
return false;
};
VaoContextWebGL.prototype.createVertexArray = function () {
this._context.stats.counter.vao++;
return this._createVertexArray();
};
VaoContextWebGL.prototype.unbindVertexArrays = function () {
this._bindVertexArray(null);
this._lastBoundedVao = null;
this._isRequireUnbound = false;
};
VaoContextWebGL.prototype.bindVertexArray = function (v) {
this._context.stateChangeCallback && this._context.stateChangeCallback('bindVertexArray');
// not unbound vao, to reduce bound flips
if (v === this._lastBoundedVao) {
return;
}
if (!v) {
// mark that we require a unbound VAO as fast as possible
this._isRequireUnbound = true;
return;
}
this._isRequireUnbound = false;
this._lastBoundedVao = v;
this._bindVertexArray(v._vao);
};
VaoContextWebGL.prototype.deleteVertexArray = function (v) {
if (this._lastBoundedVao === v) {
this._bindVertexArray(null);
}
this._deleteVertexArray(v._vao);
this._lastBoundedVao = null;
this._context.stats.counter.vao--;
};
return VaoContextWebGL;
}());
export { VaoContextWebGL };
var VaoWebGL = /** @class */ (function () {
function VaoWebGL(_context) {
this._context = _context;
this._vao = _context._vaoContext.createVertexArray();
}
VaoWebGL.prototype.attachIndexBuffer = function (buffer) {
if (this._indexBuffer) {
return;
}
this.bind();
this._context.bindIndexBuffer(buffer);
this._indexBuffer = buffer;
};
VaoWebGL.prototype.bind = function () {
this._context._vaoContext.bindVertexArray(this);
};
VaoWebGL.prototype.unbind = function (force) {
if (force === void 0) { force = false; }
if (force) {
this._context._vaoContext.unbindVertexArrays();
return;
}
this._context._vaoContext.bindVertexArray(null);
};
VaoWebGL.prototype.dispose = function () {
this._context._vaoContext.deleteVertexArray(this);
this._vao = null;
};
return VaoWebGL;
}());
export { VaoWebGL };