molstar
Version:
A comprehensive macromolecular library.
122 lines (121 loc) • 5.95 kB
JavaScript
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { QuadSchema, QuadValues } from '../../mol-gl/compute/util';
import { createComputeRenderable } from '../../mol-gl/renderable';
import { TextureSpec, UniformSpec } from '../../mol-gl/renderable/schema';
import { ShaderCode } from '../../mol-gl/shader-code';
import { createComputeRenderItem } from '../../mol-gl/webgl/render-item';
import { ValueCell } from '../../mol-util';
import { quad_vert } from '../../mol-gl/shader/quad.vert';
import { evaluateWboit_frag } from '../../mol-gl/shader/evaluate-wboit.frag';
import { Vec2 } from '../../mol-math/linear-algebra';
import { isDebugMode, isTimingMode } from '../../mol-util/debug';
import { isWebGL2 } from '../../mol-gl/webgl/compat';
var EvaluateWboitSchema = __assign(__assign({}, QuadSchema), { tWboitA: TextureSpec('texture', 'rgba', 'float', 'nearest'), tWboitB: TextureSpec('texture', 'rgba', 'float', 'nearest'), uTexSize: UniformSpec('v2') });
var EvaluateWboitShaderCode = ShaderCode('evaluate-wboit', quad_vert, evaluateWboit_frag);
function getEvaluateWboitRenderable(ctx, wboitATexture, wboitBTexture) {
var values = __assign(__assign({}, QuadValues), { tWboitA: ValueCell.create(wboitATexture), tWboitB: ValueCell.create(wboitBTexture), uTexSize: ValueCell.create(Vec2.create(wboitATexture.getWidth(), wboitATexture.getHeight())) });
var schema = __assign({}, EvaluateWboitSchema);
var renderItem = createComputeRenderItem(ctx, 'triangles', EvaluateWboitShaderCode, schema, values);
return createComputeRenderable(renderItem, values);
}
//
var WboitPass = /** @class */ (function () {
function WboitPass(webgl, width, height) {
this.webgl = webgl;
this._supported = false;
if (!WboitPass.isSupported(webgl))
return;
var resources = webgl.resources, gl = webgl.gl;
this.textureA = resources.texture('image-float32', 'rgba', 'float', 'nearest');
this.textureA.define(width, height);
this.textureB = resources.texture('image-float32', 'rgba', 'float', 'nearest');
this.textureB.define(width, height);
this.depthRenderbuffer = isWebGL2(gl)
? resources.renderbuffer('depth32f', 'depth', width, height)
: resources.renderbuffer('depth16', 'depth', width, height);
this.renderable = getEvaluateWboitRenderable(webgl, this.textureA, this.textureB);
this.framebuffer = resources.framebuffer();
this._supported = true;
this._init();
}
Object.defineProperty(WboitPass.prototype, "supported", {
get: function () {
return this._supported;
},
enumerable: false,
configurable: true
});
WboitPass.prototype.bind = function () {
var _a = this.webgl, state = _a.state, gl = _a.gl;
this.framebuffer.bind();
state.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
state.disable(gl.DEPTH_TEST);
state.blendFuncSeparate(gl.ONE, gl.ONE, gl.ZERO, gl.ONE_MINUS_SRC_ALPHA);
state.enable(gl.BLEND);
};
WboitPass.prototype.render = function () {
if (isTimingMode)
this.webgl.timer.mark('WboitPass.render');
var _a = this.webgl, state = _a.state, gl = _a.gl;
state.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
state.enable(gl.BLEND);
this.renderable.update();
this.renderable.render();
if (isTimingMode)
this.webgl.timer.markEnd('WboitPass.render');
};
WboitPass.prototype.setSize = function (width, height) {
var _a = this.renderable.values.uTexSize.ref.value, w = _a[0], h = _a[1];
if (width !== w || height !== h) {
this.textureA.define(width, height);
this.textureB.define(width, height);
this.depthRenderbuffer.setSize(width, height);
ValueCell.update(this.renderable.values.uTexSize, Vec2.set(this.renderable.values.uTexSize.ref.value, width, height));
}
};
WboitPass.prototype.reset = function () {
if (this._supported)
this._init();
};
WboitPass.prototype._init = function () {
var drawBuffers = this.webgl.extensions.drawBuffers;
this.framebuffer.bind();
drawBuffers.drawBuffers([
drawBuffers.COLOR_ATTACHMENT0,
drawBuffers.COLOR_ATTACHMENT1,
]);
this.textureA.attachFramebuffer(this.framebuffer, 'color0');
this.textureB.attachFramebuffer(this.framebuffer, 'color1');
this.depthRenderbuffer.attachFramebuffer(this.framebuffer);
};
WboitPass.isSupported = function (webgl) {
var _a = webgl.extensions, drawBuffers = _a.drawBuffers, textureFloat = _a.textureFloat, colorBufferFloat = _a.colorBufferFloat, depthTexture = _a.depthTexture;
if (!textureFloat || !colorBufferFloat || !depthTexture || !drawBuffers) {
if (isDebugMode) {
var missing = [];
if (!textureFloat)
missing.push('textureFloat');
if (!colorBufferFloat)
missing.push('colorBufferFloat');
if (!depthTexture)
missing.push('depthTexture');
if (!drawBuffers)
missing.push('drawBuffers');
console.log("Missing \"".concat(missing.join('", "'), "\" extensions required for \"wboit\""));
}
return false;
}
else {
return true;
}
};
return WboitPass;
}());
export { WboitPass };