/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../core/maybe","../views/3d/webgl-engine/core/shaderLibrary/output/BlendOptions","../views/3d/webgl-engine/core/shaderLibrary/terrain/BackgroundGrid.glsl","../views/3d/webgl-engine/core/shaderLibrary/terrain/TileComposite.glsl","../views/3d/webgl-engine/core/shaderLibrary/util/BlendModes.glsl","../views/3d/webgl-engine/core/shaderModules/Float3PassUniform","../views/3d/webgl-engine/core/shaderModules/FloatPassUniform","../views/3d/webgl-engine/core/shaderModules/interfaces","../views/3d/webgl-engine/core/shaderModules/ShaderBuilder","../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform"],(function(e,o,r,a,l,s,t,i,d,n,u){"use strict";var g,c;function p(g){const c=new n.ShaderBuilder,p=g.output===e.BlendLayersOutput.GridOnly,y=g.output===e.BlendLayersOutput.GridComposite,b=y||p,m=g.output===e.BlendLayersOutput.ColorOnly,O=g.output===e.BlendLayersOutput.ColorComposite,f=O||m,C=y||O;if(c.include(l.TileComposite),f&&c.fragment.uniforms.add(new t.Float3PassUniform("backgroundColor",(e=>e.backgroundColor))),g.baseOpacityMode!==e.BaseOpacityMode.One&&c.fragment.uniforms.add(new i.FloatPassUniform("baseOpacity",(e=>e.baseOpacity))),b&&(c.extensions.add("GL_OES_standard_derivatives"),c.fragment.include(a.BackgroundGrid)),p||m)return c.fragment.code.add(d.glsl`
    void main() {
      gl_FragColor = ${m?d.glsl`vec4(backgroundColor, 1.0)`:d.glsl`gridColor(uv)`};
    }
  `),c;const B=g.blendMode!==r.LayerBlendMode.Normal,v=g.baseOpacityMode===e.BaseOpacityMode.OnBaseLayer,L=g.baseOpacityMode===e.BaseOpacityMode.OnBackground||g.baseOpacityMode===e.BaseOpacityMode.OnBaseLayer;return c.fragment.uniforms.add(new u.Texture2DPassUniform("tex",(e=>e.texture))),c.fragment.uniforms.add(new i.FloatPassUniform("opacity",(e=>e.opacity))),(B||v)&&(c.fragment.uniforms.add(new u.Texture2DPassUniform("fboColor",(e=>e.fboTexture))),c.fragment.uniforms.add(new i.FloatPassUniform("tileSize",(e=>o.isSome(e.fboTexture)?e.fboTexture.descriptor.width:1)))),c.fragment.include(s.BlendModes,g),c.fragment.code.add(d.glsl`
    void main() {
      ${C||v?d.glsl`
      vec4 bgColor = ${v?d.glsl`texture2D(fboColor, gl_FragCoord.xy / tileSize)`:O?d.glsl`vec4(backgroundColor, 1.0)`:d.glsl`gridColor(uv)`};
      ${L?d.glsl`bgColor *= baseOpacity;`:""}`:""}
      vec4 colorLayer = texture2D(tex, uv);
      ${B?d.glsl`
          vec4 fboTex = ${C?d.glsl`bgColor;`:d.glsl`texture2D(fboColor, gl_FragCoord.xy / tileSize) ${v?" * baseOpacity":""};`}
          vec3 Cb = fboTex.a == 0.0 ? fboTex.rgb : vec3(fboTex.rgb * fboTex.a);
          gl_FragColor = applyBlendMode(colorLayer.rgb, colorLayer.a * opacity, Cb, fboTex.a);`:d.glsl`
          vec4 pmColorLayer = vec4(colorLayer.xyz, 1.0);
          float composeAlpha = colorLayer.a * opacity;
          gl_FragColor = ${C||v?g.premultipliedSource?d.glsl`bgColor * (1.0 - composeAlpha) + colorLayer * opacity;`:d.glsl`mix(bgColor, pmColorLayer, composeAlpha);`:d.glsl`pmColorLayer * composeAlpha;`}`}
    }
  `),c}e.BlendLayersOutput=void 0,(g=e.BlendLayersOutput||(e.BlendLayersOutput={}))[g.Composite=0]="Composite",g[g.ColorOnly=1]="ColorOnly",g[g.GridOnly=2]="GridOnly",g[g.ColorComposite=3]="ColorComposite",g[g.GridComposite=4]="GridComposite",g[g.COUNT=5]="COUNT",e.BaseOpacityMode=void 0,(c=e.BaseOpacityMode||(e.BaseOpacityMode={}))[c.One=0]="One",c[c.OnBackground=1]="OnBackground",c[c.OnBaseLayer=2]="OnBaseLayer",c[c.COUNT=3]="COUNT";const y=Object.freeze(Object.defineProperty({__proto__:null,get BlendLayersOutput(){return e.BlendLayersOutput},get BaseOpacityMode(){return e.BaseOpacityMode},build:p},Symbol.toStringTag,{value:"Module"}));e.BlendLayers=y,e.build=p}));
