"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.createCopyColorProgram = createCopyColorProgram;
const RenderFlags_1 = require("../RenderFlags");
const Texture_1 = require("../Texture");
const Fragment_1 = require("./Fragment");
const ViewportQuad_1 = require("./ViewportQuad");
const computeColor = "return TEXTURE(u_color, v_texCoord);";
// Transparent background color will not have premultiplied alpha - multiply it when copying.
// Set all other pixels opaque.
const computeColorNoAlpha = `
  vec4 color = TEXTURE(u_color, v_texCoord);
  if (color == u_bgColor)
    return vec4(color.rgb * color.a, color.a);
  else
    return vec4(color.rgb, 1.0);
`;
/** @internal */
function createCopyColorProgram(context, copyAlpha = true) {
    const builder = (0, ViewportQuad_1.createViewportQuadBuilder)(true);
    const frag = builder.frag;
    frag.set(1 /* FragmentShaderComponent.ComputeBaseColor */, copyAlpha ? computeColor : computeColorNoAlpha);
    frag.set(18 /* FragmentShaderComponent.AssignFragData */, Fragment_1.assignFragColor);
    frag.addUniform("u_color", 8 /* VariableType.Sampler2D */, (prog) => {
        prog.addGraphicUniform("u_color", (uniform, params) => {
            const geom = params.geometry;
            Texture_1.Texture2DHandle.bindSampler(uniform, geom.texture, RenderFlags_1.TextureUnit.Zero);
        });
    });
    if (!copyAlpha) {
        frag.addUniform("u_bgColor", 5 /* VariableType.Vec4 */, (prog) => {
            prog.addProgramUniform("u_bgColor", (uniform, params) => {
                params.target.uniforms.style.bindBackgroundRgba(uniform);
            });
        });
    }
    const flagString = (copyAlpha ? "-CopyAlpha" : "-NoAlpha");
    builder.vert.headerComment = `//!V! CopyColor${flagString}`;
    builder.frag.headerComment = `//!F! CopyColor${flagString}`;
    return builder.buildProgram(context);
}
//# sourceMappingURL=CopyColor.js.map