"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.ShadowUniforms = void 0;
const core_geometry_1 = require("@itwin/core-geometry");
const core_common_1 = require("@itwin/core-common");
const FloatRGBA_1 = require("./FloatRGBA");
const Matrix_1 = require("./Matrix");
const Sync_1 = require("./Sync");
/** Maintains state of uniforms used for applying shadows.
 * @internal
 */
class ShadowUniforms {
    constructor(target) {
        this.syncKey = 0;
        this._enabled = false;
        this._projectionMatrix = core_geometry_1.Matrix4d.createIdentity();
        this._color = core_common_1.RgbColor.fromJSON(undefined);
        this._bias = 0;
        // GPU state
        this._projection32 = new Matrix_1.Matrix4();
        this._colorAndBias = new FloatRGBA_1.FloatRgba();
        // Working variables
        this._scratchModel = core_geometry_1.Matrix4d.createIdentity();
        this._scratchModelProjection = core_geometry_1.Matrix4d.createIdentity();
        this._target = target;
    }
    update() {
        const map = this._target.solarShadowMap;
        if (this._enabled !== map.isEnabled) {
            (0, Sync_1.desync)(this);
            this._enabled = map.isEnabled;
        }
        if (!map.isEnabled)
            return;
        const settings = map.settings;
        if (this._bias !== settings.bias) {
            (0, Sync_1.desync)(this);
            this._bias = this._colorAndBias.alpha = settings.bias;
        }
        if (!this._color.equals(settings.color)) {
            (0, Sync_1.desync)(this);
            this._color = settings.color;
            this._colorAndBias.setTbgr(core_common_1.ColorDef.computeTbgrFromComponents(settings.color.r, settings.color.g, settings.color.b));
            this._colorAndBias.alpha = this._bias;
        }
        // NB: The projection matrix must be computed later when it is bound because it uses the model matrix.
        const proj = map.projectionMatrix;
        if (!proj.isExactEqual(this._projectionMatrix)) {
            (0, Sync_1.desync)(this);
            proj.clone(this._projectionMatrix);
        }
    }
    computeProjection() {
        const branch = this._target.uniforms.branch;
        if ((0, Sync_1.sync)(branch, this))
            return;
        // NB: We could decouple from the other uniforms so they don't get invalidated when frustum changes but meh.
        (0, Sync_1.desync)(this);
        const proj = this._target.solarShadowMap.projectionMatrix;
        const model = core_geometry_1.Matrix4d.createTransform(this._target.currentTransform, this._scratchModel);
        const modelProj = proj.multiplyMatrixMatrix(model, this._scratchModelProjection);
        this._projection32.initFromMatrix4d(modelProj);
    }
    bindColorAndBias(uniform) {
        if (!(0, Sync_1.sync)(this, uniform))
            this._colorAndBias.bind(uniform);
    }
    bindProjectionMatrix(uniform) {
        this.computeProjection();
        if (!(0, Sync_1.sync)(this, uniform))
            uniform.setMatrix4(this._projection32);
    }
}
exports.ShadowUniforms = ShadowUniforms;
//# sourceMappingURL=ShadowUniforms.js.map