"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.IndexedEdgeGeometry = exports.EdgeLUT = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const Texture_1 = require("./Texture");
const AttributeBuffers_1 = require("./AttributeBuffers");
const MeshGeometry_1 = require("./MeshGeometry");
const AttributeMap_1 = require("./AttributeMap");
const GL_1 = require("./GL");
const System_1 = require("./System");
/** @see [[EdgeTable]]
 * @internal
 */
class EdgeLUT {
    constructor(texture, numSegments, silhouettePadding) {
        this.texture = texture;
        this.numSegments = numSegments;
        this.silhouettePadding = silhouettePadding;
    }
    dispose() {
        (0, core_bentley_1.dispose)(this.texture);
    }
    static create(table) {
        const texture = Texture_1.TextureHandle.createForData(table.width, table.height, table.data);
        return texture ? new EdgeLUT(texture, table.numSegments, table.silhouettePadding) : undefined;
    }
    get bytesUsed() {
        return this.texture.bytesUsed;
    }
    get isDisposed() {
        return this.texture.isDisposed;
    }
}
exports.EdgeLUT = EdgeLUT;
/** @see [[IndexedEdgeParams]]
 * @internal
 */
class IndexedEdgeGeometry extends MeshGeometry_1.MeshGeometry {
    get lutBuffers() { return this._buffers; }
    get asIndexedEdge() { return this; }
    constructor(mesh, indices, numIndices, lut) {
        super(mesh, numIndices);
        this.edgeLut = lut;
        this._buffers = AttributeBuffers_1.BuffersContainer.create();
        const attrPos = AttributeMap_1.AttributeMap.findAttribute("a_pos", 6 /* TechniqueId.IndexedEdge */, false);
        (0, core_bentley_1.assert)(undefined !== attrPos);
        this._buffers.addBuffer(indices, [AttributeBuffers_1.BufferParameters.create(attrPos.location, 3, GL_1.GL.DataType.UnsignedByte, false, 0, 0, false)]);
        this._indices = indices;
    }
    dispose() {
        (0, core_bentley_1.dispose)(this._buffers);
        (0, core_bentley_1.dispose)(this._indices);
        (0, core_bentley_1.dispose)(this.edgeLut);
    }
    get isDisposed() {
        return this._buffers.isDisposed && this._indices.isDisposed && this.edgeLut.isDisposed;
    }
    static create(mesh, params) {
        const indexBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(params.indices.data);
        const lut = EdgeLUT.create(params.edges);
        return indexBuffer && lut ? new IndexedEdgeGeometry(mesh, indexBuffer, params.indices.length, lut) : undefined;
    }
    collectStatistics(stats) {
        stats.addIndexedEdges(this._indices.bytesUsed);
        stats.addEdgeTable(this.edgeLut.bytesUsed);
    }
    _draw(numInstances, instances) {
        const bufs = instances ?? this._buffers;
        bufs.bind();
        System_1.System.instance.drawArrays(GL_1.GL.PrimitiveType.Triangles, 0, this._numIndices, numInstances);
        bufs.unbind();
    }
    _wantWoWReversal() { return true; }
    _getLineCode(params) { return this.computeEdgeLineCode(params); }
    get techniqueId() { return 6 /* TechniqueId.IndexedEdge */; }
    getPass(target) { return this.computeEdgePass(target); }
    get renderOrder() { return this.isPlanar ? 14 /* RenderOrder.PlanarEdge */ : 6 /* RenderOrder.Edge */; }
    getColor(target) { return this.computeEdgeColor(target); }
    wantMonochrome(target) { return target.currentViewFlags.renderMode === core_common_1.RenderMode.Wireframe; }
}
exports.IndexedEdgeGeometry = IndexedEdgeGeometry;
//# sourceMappingURL=IndexedEdgeGeometry.js.map