"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.PolylineEdgeGeometry = exports.SilhouetteEdgeGeometry = exports.EdgeGeometry = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const RenderMemory_1 = require("../RenderMemory");
const AttributeMap_1 = require("./AttributeMap");
const CachedGeometry_1 = require("./CachedGeometry");
const GL_1 = require("./GL");
const AttributeBuffers_1 = require("./AttributeBuffers");
const System_1 = require("./System");
const MeshGeometry_1 = require("./MeshGeometry");
/** @internal */
class EdgeGeometry extends MeshGeometry_1.MeshGeometry {
    get lutBuffers() { return this.buffers; }
    get asSurface() { return undefined; }
    get asEdge() { return this; }
    get asSilhouette() { return undefined; }
    static create(mesh, edges) {
        const indexBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(edges.indices.data);
        const endPointBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(edges.endPointAndQuadIndices);
        return undefined !== indexBuffer && undefined !== endPointBuffer ? new EdgeGeometry(indexBuffer, endPointBuffer, edges.indices.length, mesh) : undefined;
    }
    get isDisposed() {
        return this.buffers.isDisposed
            && this._indices.isDisposed
            && this._endPointAndQuadIndices.isDisposed;
    }
    dispose() {
        (0, core_bentley_1.dispose)(this.buffers);
        (0, core_bentley_1.dispose)(this._indices);
        (0, core_bentley_1.dispose)(this._endPointAndQuadIndices);
    }
    collectStatistics(stats) {
        stats.addVisibleEdges(this._indices.bytesUsed + this._endPointAndQuadIndices.bytesUsed);
    }
    _draw(numInstances, instanceBuffersContainer) {
        const bufs = instanceBuffersContainer !== undefined ? instanceBuffersContainer : this.buffers;
        bufs.bind();
        System_1.System.instance.drawArrays(GL_1.GL.PrimitiveType.Triangles, 0, this._numIndices, numInstances);
        bufs.unbind();
    }
    _wantWoWReversal(_target) { return true; }
    _getLineCode(params) { return this.computeEdgeLineCode(params); }
    get techniqueId() { return 4 /* TechniqueId.Edge */; }
    getPass(target) { return this.computeEdgePass(target); }
    get renderOrder() { return this.isPlanar ? 14 /* RenderOrder.PlanarEdge */ : 6 /* RenderOrder.Edge */; }
    getColor(target) { return this.computeEdgeColor(target); }
    get endPointAndQuadIndices() { return this._endPointAndQuadIndices; }
    wantMonochrome(target) {
        return target.currentViewFlags.renderMode === core_common_1.RenderMode.Wireframe;
    }
    constructor(indices, endPointAndQuadsIndices, numIndices, mesh) {
        super(mesh, numIndices);
        this.buffers = AttributeBuffers_1.BuffersContainer.create();
        const attrPos = AttributeMap_1.AttributeMap.findAttribute("a_pos", 4 /* TechniqueId.Edge */, false);
        const attrEndPointAndQuadIndices = AttributeMap_1.AttributeMap.findAttribute("a_endPointAndQuadIndices", 4 /* TechniqueId.Edge */, false);
        (0, core_bentley_1.assert)(attrPos !== undefined);
        (0, core_bentley_1.assert)(attrEndPointAndQuadIndices !== undefined);
        this.buffers.addBuffer(indices, [AttributeBuffers_1.BufferParameters.create(attrPos.location, 3, GL_1.GL.DataType.UnsignedByte, false, 0, 0, false)]);
        this.buffers.addBuffer(endPointAndQuadsIndices, [AttributeBuffers_1.BufferParameters.create(attrEndPointAndQuadIndices.location, 4, GL_1.GL.DataType.UnsignedByte, false, 0, 0, false)]);
        this._indices = indices;
        this._endPointAndQuadIndices = endPointAndQuadsIndices;
    }
}
exports.EdgeGeometry = EdgeGeometry;
/** @internal */
class SilhouetteEdgeGeometry extends EdgeGeometry {
    get asSilhouette() { return this; }
    static createSilhouettes(mesh, params) {
        const indexBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(params.indices.data);
        const endPointBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(params.endPointAndQuadIndices);
        const normalsBuffer = AttributeBuffers_1.BufferHandle.createArrayBuffer(params.normalPairs);
        return undefined !== indexBuffer && undefined !== endPointBuffer && undefined !== normalsBuffer ? new SilhouetteEdgeGeometry(indexBuffer, endPointBuffer, normalsBuffer, params.indices.length, mesh) : undefined;
    }
    get isDisposed() { return super.isDisposed && this._normalPairs.isDisposed; }
    dispose() {
        super.dispose();
        (0, core_bentley_1.dispose)(this._normalPairs);
    }
    collectStatistics(stats) {
        stats.addSilhouetteEdges(this._indices.bytesUsed + this._endPointAndQuadIndices.bytesUsed + this._normalPairs.bytesUsed);
    }
    get techniqueId() { return 5 /* TechniqueId.SilhouetteEdge */; }
    get renderOrder() { return this.isPlanar ? 15 /* RenderOrder.PlanarSilhouette */ : 7 /* RenderOrder.Silhouette */; }
    get normalPairs() { return this._normalPairs; }
    constructor(indices, endPointAndQuadsIndices, normalPairs, numIndices, mesh) {
        super(indices, endPointAndQuadsIndices, numIndices, mesh);
        const attrNormals = AttributeMap_1.AttributeMap.findAttribute("a_normals", 5 /* TechniqueId.SilhouetteEdge */, false);
        (0, core_bentley_1.assert)(attrNormals !== undefined);
        this.buffers.addBuffer(normalPairs, [AttributeBuffers_1.BufferParameters.create(attrNormals.location, 4, GL_1.GL.DataType.UnsignedByte, false, 0, 0, false)]);
        this._normalPairs = normalPairs;
    }
}
exports.SilhouetteEdgeGeometry = SilhouetteEdgeGeometry;
/** @internal */
class PolylineEdgeGeometry extends MeshGeometry_1.MeshGeometry {
    get lutBuffers() { return this._buffers.buffers; }
    static create(mesh, polyline) {
        const buffers = CachedGeometry_1.PolylineBuffers.create(polyline);
        return undefined !== buffers ? new PolylineEdgeGeometry(polyline.indices.length, buffers, mesh) : undefined;
    }
    get isDisposed() { return this._buffers.isDisposed; }
    dispose() {
        (0, core_bentley_1.dispose)(this._buffers);
    }
    collectStatistics(stats) {
        this._buffers.collectStatistics(stats, RenderMemory_1.RenderMemory.BufferType.PolylineEdges);
    }
    _wantWoWReversal(_target) { return true; }
    _getLineWeight(params) { return this.computeEdgeWeight(params); }
    _getLineCode(params) { return this.computeEdgeLineCode(params); }
    getColor(target) { return this.computeEdgeColor(target); }
    get techniqueId() { return 1 /* TechniqueId.Polyline */; }
    getPass(target) { return this.computeEdgePass(target); }
    get renderOrder() { return this.isPlanar ? 14 /* RenderOrder.PlanarEdge */ : 6 /* RenderOrder.Edge */; }
    get polylineBuffers() { return this._buffers; }
    wantMonochrome(target) {
        return target.currentViewFlags.renderMode === core_common_1.RenderMode.Wireframe;
    }
    _draw(numInstances, instanceBuffersContainer) {
        const gl = System_1.System.instance;
        const bufs = instanceBuffersContainer !== undefined ? instanceBuffersContainer : this._buffers.buffers;
        bufs.bind();
        gl.drawArrays(GL_1.GL.PrimitiveType.Triangles, 0, this._numIndices, numInstances);
        bufs.unbind();
    }
    constructor(numIndices, buffers, mesh) {
        super(mesh, numIndices);
        this._buffers = buffers;
    }
}
exports.PolylineEdgeGeometry = PolylineEdgeGeometry;
//# sourceMappingURL=EdgeGeometry.js.map