@itwin/core-frontend
Version: 
iTwin.js frontend components
111 lines • 4.28 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module WebGL
 */
import { assert } from "@itwin/core-bentley";
import { GL } from "./GL";
import { System } from "./System";
function computeBytesUsed(width, height, format, numSamples) {
    const bytesPerPixel = (GL.RenderBuffer.Format.DepthComponent16 === format ? 2 : 4);
    return width * height * bytesPerPixel * numSamples;
}
/** @internal */
export class RenderBuffer {
    _glBuffer;
    _bytesUsed = 0;
    _width;
    _height;
    get bytesUsed() { return this._bytesUsed; }
    get width() { return this._width; }
    get height() { return this._height; }
    getHandle() { return this._glBuffer; }
    static create(width, height, format = GL.RenderBuffer.Format.DepthComponent16) {
        const gl = System.instance.context;
        const glBuffer = gl.createRenderbuffer();
        if (null === glBuffer) {
            return undefined;
        }
        assert(0 < width && 0 < height);
        RenderBuffer.bindBuffer(glBuffer);
        gl.renderbufferStorage(GL.RenderBuffer.TARGET, format, width, height);
        RenderBuffer.unbind();
        return new RenderBuffer(glBuffer, width, height, computeBytesUsed(width, height, format, 1));
    }
    get isDisposed() { return this._glBuffer === undefined || this._glBuffer === null; }
    [Symbol.dispose]() {
        if (!this.isDisposed) {
            System.instance.context.deleteRenderbuffer(this._glBuffer);
            this._glBuffer = undefined;
            this._bytesUsed = 0;
        }
    }
    bind() {
        assert(undefined !== this._glBuffer);
        if (undefined !== this._glBuffer) {
            RenderBuffer.bindBuffer(this._glBuffer);
        }
    }
    constructor(glBuffer, width, height, bytesUsed) {
        this._glBuffer = glBuffer;
        this._bytesUsed = bytesUsed;
        this._width = width;
        this._height = height;
    }
    static bindBuffer(glBuffer) { System.instance.context.bindRenderbuffer(GL.RenderBuffer.TARGET, glBuffer); }
    static unbind() { this.bindBuffer(null); }
}
/**
 * A RenderBuffer for doing antialiasing (multisampling).
 * @internal
 */
export class RenderBufferMultiSample {
    _glBuffer;
    _bytesUsed = 0;
    _width;
    _height;
    _isDirty = false;
    get bytesUsed() { return this._bytesUsed; }
    get width() { return this._width; }
    get height() { return this._height; }
    get isDirty() { return this._isDirty; }
    markBufferDirty(dirty) {
        this._isDirty = dirty;
    }
    getHandle() { return this._glBuffer; }
    static create(width, height, format, numSamples) {
        const gl = System.instance.context;
        const glBuffer = gl.createRenderbuffer();
        if (null === glBuffer)
            return undefined;
        assert(0 < width && 0 < height);
        RenderBufferMultiSample.bindBuffer(glBuffer);
        gl.renderbufferStorageMultisample(GL.RenderBuffer.TARGET, numSamples, format, width, height);
        RenderBufferMultiSample.unbind();
        return new RenderBufferMultiSample(glBuffer, width, height, computeBytesUsed(width, height, format, numSamples));
    }
    get isDisposed() { return this._glBuffer === undefined || this._glBuffer === null; }
    [Symbol.dispose]() {
        if (!this.isDisposed) {
            System.instance.context.deleteRenderbuffer(this._glBuffer);
            this._glBuffer = undefined;
        }
    }
    bind() {
        assert(undefined !== this._glBuffer);
        if (undefined !== this._glBuffer) {
            RenderBufferMultiSample.bindBuffer(this._glBuffer);
        }
    }
    constructor(glBuffer, width, height, bytesUsed) {
        this._glBuffer = glBuffer;
        this._bytesUsed = bytesUsed;
        this._width = width;
        this._height = height;
    }
    static bindBuffer(glBuffer) { System.instance.context.bindRenderbuffer(GL.RenderBuffer.TARGET, glBuffer); }
    static unbind() { this.bindBuffer(null); }
}
//# sourceMappingURL=RenderBuffer.js.map