UNPKG

@awayjs/stage

Version:
144 lines (143 loc) 5.98 kB
var RenderTargetPool = /** @class */ (function () { function RenderTargetPool() { } RenderTargetPool.store = function (target) { var key = "".concat(target.width, "_").concat(target.height, "_").concat(target.isMsaaTarget); if (!this._pool[key]) { this._pool[key] = []; } this._pool[key].push(target); return true; }; RenderTargetPool.get = function (context, width, height, colorOnly) { if (colorOnly === void 0) { colorOnly = false; } var key = "".concat(width, "_").concat(height, "_").concat(false); if (this._pool[key] && this._pool[key].length) { return this._pool[key].pop(); } return new RenderTargetWebGL(context, width, height, colorOnly); }; RenderTargetPool._pool = {}; return RenderTargetPool; }()); export { RenderTargetPool }; var RenderTargetWebGL = /** @class */ (function () { function RenderTargetWebGL(_context, width, height, _colorOnly) { if (_colorOnly === void 0) { _colorOnly = false; } this._context = _context; this.width = width; this.height = height; this._colorOnly = _colorOnly; this.isMsaaTarget = false; this.memoryUsage = 0; this.init(); } Object.defineProperty(RenderTargetWebGL.prototype, "drawBuffer", { get: function () { return this._framebuffer; }, enumerable: false, configurable: true }); Object.defineProperty(RenderTargetWebGL.prototype, "readBuffer", { get: function () { return this._framebuffer; }, enumerable: false, configurable: true }); Object.defineProperty(RenderTargetWebGL.prototype, "isValid", { get: function () { return this._framebuffer && this._linkedTexture; }, enumerable: false, configurable: true }); RenderTargetWebGL.prototype.init = function () { var gl = this._context._gl; this._framebuffer = gl.createFramebuffer(); this._depthStencil = this._colorOnly ? null : gl.createRenderbuffer(); this._context.stats.counter.renderTarget++; // we not have RB, not require init it if (!this._depthStencil) { return; } this.memoryUsage = this.width * this.height * 8; var prev = this._context._texContext.bindRenderTarget(this, false); gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthStencil); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencil); gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, this.width, this.height); gl.bindRenderbuffer(gl.RENDERBUFFER, null); this._context._texContext.bindRenderTarget(prev, true); this._context.stats.memory.renderTarget += this.memoryUsage; }; RenderTargetWebGL.prototype.linkTexture = function (texture) { if (this._linkedTexture === texture) { return; } var gl = this._context._gl; var tex = this._context._texContext; var link = texture === null || texture === void 0 ? void 0 : texture.glTexture; var prevTex = tex.bindTexture(texture, true); if (texture) { if (texture.height !== this.height || texture.width !== this.width) { throw ('Texture size and render target is different' + "expected ".concat(this.width, "x").concat(this.height, ", actual ").concat(texture.width, "x").concat(texture.height)); } // we should fill texture, because otherwithe a FB will be incomplete if (!texture._isFilled) { texture.uploadFromArray(null, 0, false); } } var prevTarget = tex.bindRenderTarget(this, true); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, link, 0); tex.bindRenderTarget(prevTarget, true); tex.bindTexture(prevTex, true); this._linkedTexture = texture; }; RenderTargetWebGL.prototype.present = function (target, sourceRect, targetPoint, _intoThis) { if (_intoThis === void 0) { _intoThis = false; } if (!target) { return; } if (target.isMsaaTarget) { throw 'Invalid opration, try copy noMSAA to MSAA'; } if (!target.isValid) { throw 'Target RT MUST be valid and have texture link'; } if (!this.isValid) { throw 'Source RT MUST be valid and have texture link'; } var gl = this._context._gl; var tex = this._context._texContext; var x = sourceRect.x, y = sourceRect.y, width = sourceRect.width, height = sourceRect.height; var tx = (targetPoint === null || targetPoint === void 0 ? void 0 : targetPoint.x) || 0; var ty = (targetPoint === null || targetPoint === void 0 ? void 0 : targetPoint.y) || 0; var prevTarget = tex.bindRenderTarget(this); var prevTex = tex.bindTexture(target._linkedTexture); gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, tx, ty, x, y, width, height); tex.bindTexture(prevTex); tex.bindRenderTarget(prevTarget); }; RenderTargetWebGL.prototype.dispose = function () { /*if (RenderTargetPool.store(this)) { return; }*/ this.unload(); }; RenderTargetWebGL.prototype.unload = function () { var gl = this._context._gl; var fb = this._framebuffer; var rb = this._depthStencil; gl.deleteFramebuffer(fb); gl.deleteRenderbuffer(rb); this._linkedTexture = null; this._framebuffer = null; this._depthStencil = null; this._context.stats.counter.renderTarget--; this._context.stats.memory.renderTarget -= this.memoryUsage; }; return RenderTargetWebGL; }()); export { RenderTargetWebGL };