/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{ZEROS as o}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{BackgroundGrid as r}from"./BackgroundGrid.glsl.js";import{BlendModes as e}from"../util/BlendModes.glsl.js";import{Float3PassUniform as a}from"../../shaderModules/Float3PassUniform.js";import{FloatPassUniform as c}from"../../shaderModules/FloatPassUniform.js";import{glsl as l,If as s}from"../../shaderModules/glsl.js";import{Texture2DPassUniform as t}from"../../shaderModules/Texture2DPassUniform.js";import{NoParameters as d}from"../../../../../webgl/NoParameters.js";class i extends d{constructor(){super(...arguments),this.baseOpacity=1,this.backgroundColor=o,this.fboTexture=null}}function b(o,d){const{output:i,blendMode:b,applyBaseOpacity:g,premultipliedAlphaSource:n}=d,u=o.fragment;g&&u.uniforms.add(new c("baseOpacity",o=>o.baseOpacity));const p=0!==b,m=!p&&!n&&(1===i&&!g||4===i);u.include(e,d);let y="";switch(i){case 4:case 0:y=l`vec4(0.0)`;break;case 2:u.uniforms.add(new a("backgroundColor",o=>o.backgroundColor)),y=l`vec4(backgroundColor, 1.0)`;break;case 3:u.include(r),y=l`vec4(gridColor(uv), 1.0)`;break;case 1:u.uniforms.add(new t("fboColor",o=>o.fboTexture)),y=l`texelFetch(fboColor, ivec2(gl_FragCoord.xy), 0)`}u.code.add(l`
    vec4 getBackground(vec2 uv) {
      return ${s(g,l`baseOpacity *`)} ${y};
    }`),p?u.code.add(l`vec4 blendLayers(vec2 bgUV, vec4 colorLayer, float opacity) {
vec3 cl = colorLayer.a == 0.0 ? colorLayer.rgb : colorLayer.rgb / colorLayer.a;
vec4 bgColor = getBackground(bgUV);
vec3 cb = bgColor.a == 0.0 ? bgColor.rgb : bgColor.rgb / bgColor.a;
return applyBlendMode(clamp(cl, vec3(0.0), vec3(1.0)), colorLayer.a * opacity, cb, bgColor.a);
}`):u.code.add(l`
      vec4 blendLayers(vec2 bgUV, vec4 colorLayer, float opacity) {
        float composeAlpha = colorLayer.a * opacity;
        ${s(m,l`return colorLayer * opacity;`,l`
            vec4 bgColor = getBackground(bgUV);
            return bgColor * (1.0 - composeAlpha) + colorLayer * opacity;
          `)}
      }`)}export{b as TileBackground,i as TileBackgroundPassParameters};
