@awayjs/stage
Version:
Stage for AwayJS
119 lines (118 loc) • 5.32 kB
JavaScript
import { __extends } from "tslib";
import { RenderTargetWebGL } from './RenderTargetWebGL';
var RenderTargetWebGLMSAA = /** @class */ (function (_super) {
__extends(RenderTargetWebGLMSAA, _super);
function RenderTargetWebGLMSAA() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.isMsaaTarget = true;
return _this;
}
Object.defineProperty(RenderTargetWebGLMSAA.prototype, "readBuffer", {
get: function () {
return this._drawTarget.readBuffer;
},
enumerable: false,
configurable: true
});
Object.defineProperty(RenderTargetWebGLMSAA.prototype, "isValid", {
get: function () {
return this._framebuffer && this._drawTarget && this._drawTarget.isValid;
},
enumerable: false,
configurable: true
});
RenderTargetWebGLMSAA.prototype.init = function () {
var gl = this._context._gl;
var tex = this._context._texContext;
this._context.stats.counter.renderTarget++;
this._framebuffer = gl.createFramebuffer();
this._depthStencil = this._colorOnly ? null : gl.createRenderbuffer();
this._color = gl.createRenderbuffer();
var stencil = this._depthStencil;
var color = this._color;
var width = this.width;
var height = this.height;
var GL_MAX_MSAA = gl.getParameter(gl.MAX_SAMPLES);
var GL_RB = gl.RENDERBUFFER;
var GL_FB = gl.FRAMEBUFFER;
var GL_C_ATTACH = gl.COLOR_ATTACHMENT0;
var GL_DS_ATTACH = gl.DEPTH_STENCIL_ATTACHMENT;
var GL_RGBA8 = gl.RGBA8;
var GL_D24_S8 = gl.DEPTH24_STENCIL8;
var prevRT = tex.bindRenderTarget(this);
gl.bindRenderbuffer(GL_RB, color);
gl.framebufferRenderbuffer(GL_FB, GL_C_ATTACH, GL_RB, color);
gl.renderbufferStorageMultisample(GL_RB, GL_MAX_MSAA, GL_RGBA8, width, height);
this.memoryUsage = width * height * 8 * Math.sqrt(GL_MAX_MSAA);
if (stencil) {
gl.bindRenderbuffer(GL_RB, stencil);
// eslint-disable-next-line max-len
gl.framebufferRenderbuffer(GL_FB, GL_DS_ATTACH, GL_RB, stencil);
gl.renderbufferStorageMultisample(GL_RB, GL_MAX_MSAA, GL_D24_S8, width, height);
this.memoryUsage *= 2;
}
tex.bindRenderTarget(prevRT, true);
this._context.stats.memory.renderTarget += this.memoryUsage;
};
RenderTargetWebGLMSAA.prototype.linkTarget = function (target) {
if (target === this) {
throw 'Framebuffer loop, linking to itself';
}
this._drawTarget = target;
};
RenderTargetWebGLMSAA.prototype.present = function (target, sourceRect, targetPoint) {
if (target === void 0) { target = this._drawTarget; }
if (!this._framebuffer) {
return;
}
if (target === this) {
throw 'Framebuffer loop, presenting to itself';
}
if (!target)
return;
if (!target.isValid)
return;
var sx = ~~(sourceRect === null || sourceRect === void 0 ? void 0 : sourceRect.x) || 0;
var sy = ~~(sourceRect === null || sourceRect === void 0 ? void 0 : sourceRect.y) || 0;
var sw = ~~(sourceRect === null || sourceRect === void 0 ? void 0 : sourceRect.width) || target.width;
var sh = ~~(sourceRect === null || sourceRect === void 0 ? void 0 : sourceRect.height) || target.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 dest = target;
var needOffset = tx || ty;
// we can't blit MSAA to noMSAA with offset
if (needOffset && !target.isMsaaTarget) {
dest = this._drawTarget;
sx = sy = tx = ty = 0;
sw = target.width;
sh = target.height;
}
var gl = this._context._gl;
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this._framebuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, dest._framebuffer);
//so, i think that not needed, because blit should clear all values
// gl.clearBufferfv(gl.COLOR, 0, [0.0, 0.0, 0.0, 0.0]);
gl.blitFramebuffer(sx, sy, sx + sw, sy + sh, tx, ty, tx + sw, ty + sh, gl.COLOR_BUFFER_BIT, gl.NEAREST);
// target can be MSAA (unpossible, because we have single MSAA render)
// dest.present();
if (needOffset && !target.isMsaaTarget) {
// copy pixel, because we can't blit MSAA with offset
this._drawTarget.present(target, sourceRect, targetPoint);
}
};
RenderTargetWebGLMSAA.prototype.linkTexture = function (_texture) {
throw 'Texture can\'t be linked to MSAA render target';
};
RenderTargetWebGLMSAA.prototype.dispose = function () {
// MSAA not pooled!
this.unload();
};
RenderTargetWebGLMSAA.prototype.unload = function () {
this._context._gl.deleteRenderbuffer(this._color);
this._color = null;
this._drawTarget = null;
_super.prototype.unload.call(this);
};
return RenderTargetWebGLMSAA;
}(RenderTargetWebGL));
export { RenderTargetWebGLMSAA };