"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderBufferMultiSample = exports.RenderBuffer = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const GL_1 = require("./GL");
const System_1 = require("./System");
function computeBytesUsed(width, height, format, numSamples) {
    const bytesPerPixel = (GL_1.GL.RenderBuffer.Format.DepthComponent16 === format ? 2 : 4);
    return width * height * bytesPerPixel * numSamples;
}
/** @internal */
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_1.GL.RenderBuffer.Format.DepthComponent16) {
        const gl = System_1.System.instance.context;
        const glBuffer = gl.createRenderbuffer();
        if (null === glBuffer) {
            return undefined;
        }
        (0, core_bentley_1.assert)(0 < width && 0 < height);
        RenderBuffer.bindBuffer(glBuffer);
        gl.renderbufferStorage(GL_1.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_1.System.instance.context.deleteRenderbuffer((0, core_bentley_1.expectDefined)(this._glBuffer));
            this._glBuffer = undefined;
            this._bytesUsed = 0;
        }
    }
    bind() {
        (0, core_bentley_1.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_1.System.instance.context.bindRenderbuffer(GL_1.GL.RenderBuffer.TARGET, glBuffer); }
    static unbind() { this.bindBuffer(null); }
}
exports.RenderBuffer = RenderBuffer;
/**
 * A RenderBuffer for doing antialiasing (multisampling).
 * @internal
 */
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_1.System.instance.context;
        const glBuffer = gl.createRenderbuffer();
        if (null === glBuffer)
            return undefined;
        (0, core_bentley_1.assert)(0 < width && 0 < height);
        RenderBufferMultiSample.bindBuffer(glBuffer);
        gl.renderbufferStorageMultisample(GL_1.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_1.System.instance.context.deleteRenderbuffer((0, core_bentley_1.expectDefined)(this._glBuffer));
            this._glBuffer = undefined;
        }
    }
    bind() {
        (0, core_bentley_1.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_1.System.instance.context.bindRenderbuffer(GL_1.GL.RenderBuffer.TARGET, glBuffer); }
    static unbind() { this.bindBuffer(null); }
}
exports.RenderBufferMultiSample = RenderBufferMultiSample;
//# sourceMappingURL=RenderBuffer.js.map