/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../chunks/_rollupPluginBabelHelpers","../../../../../core/maybe","../../vectorTiles/style/StyleDefinition","../definitions","../enums","../number","./WGLBrush","../../../../webgl/enums"],(function(e,t,i,n,a,r,o,l,s){"use strict";const u=1/65536;let f=function(e){function l(){var t;return(t=e.apply(this,arguments)||this)._fillProgramOptions={id:!1,pattern:!1},t._outlineProgramOptions={id:!1},t}t._inheritsLoose(l,e);var f=l.prototype;return f.dispose=function(){},f.drawMany=function(e,t){const{displayLevel:i,drawPhase:n,renderPass:a,spriteMosaic:l,styleLayerUID:s}=e;let u=!1;for(const r of t)if(r.layerData.has(s)){const e=r.layerData.get(s);if(e.fillIndexCount>0||e.outlineIndexCount>0){u=!0;break}}if(!u)return;const f=e.styleLayer,c=f.getPaintProperty("fill-pattern"),d=void 0!==c,T=d&&c.isDataDriven;let p;if(d&&!T){const e=c.getValue(i);p=l.getMosaicItemPosition(e,!0)}const _=!d&&f.getPaintValue("fill-antialias",i);let m=!0,y=1;if(!d){const e=f.getPaintProperty("fill-color"),t=f.getPaintProperty("fill-opacity");if(!(null!=e&&e.isDataDriven||null!=t&&t.isDataDriven)){const e=f.getPaintValue("fill-color",i);y=f.getPaintValue("fill-opacity",i)*e[3],y>=1&&(m=!1)}}if(m&&"opaque"===a)return;let E;n===r.WGLDrawPhase.HITTEST&&(E=o.u32to4Xu8(s+1));const I=f.getPaintValue("fill-translate",i),P=f.getPaintValue("fill-translate-anchor",i);(m||"translucent"!==a)&&this._drawFill(e,s,f,t,I,P,d,p,T,E);const g=!f.hasDataDrivenOutlineColor&&f.outlineUsesFillColor&&y<1;_&&"opaque"!==a&&!g&&this._drawOutline(e,s,f,t,I,P,E)},f._drawFill=function(e,t,o,l,f,c,d,T,p,_){if(d&&!p&&i.isNone(T))return;const{context:m,displayLevel:y,state:E,drawPhase:I,painter:P,pixelRatio:g,spriteMosaic:v,requestRender:S}=e,U=o.fillMaterial,M=P.vectorTilesMaterialManager,N=g>a.VTL_HIGH_RES_CUTOFF?2:1,D=I===r.WGLDrawPhase.HITTEST,h=this._fillProgramOptions;h.id=D,h.pattern=d;const L=M.getMaterialProgram(m,U,h);if(i.isSome(S)&&!D&&!L.isCompiled)return void S();if(m.useProgram(L),i.isSome(T)){const{page:e}=T,t=v.getPageSize(e);i.isSome(t)&&(v.bind(m,s.TextureSamplingMode.LINEAR,e,a.VTL_TEXTURE_BINDING_UNIT_SPRITES),L.setUniform2fv("u_mosaicSize",t),L.setUniform1i("u_texture",a.VTL_TEXTURE_BINDING_UNIT_SPRITES))}L.setUniformMatrix3fv("u_displayMat3",c===n.TranslateAnchor.VIEWPORT?E.displayMat3:E.displayViewMat3),L.setUniform2fv("u_fillTranslation",f),L.setUniform1f("u_depth",o.z+u),D&&L.setUniform4fv("u_id",_);let R=-1;for(const n of l){if(!n.layerData.has(t))continue;n.key.level!==R&&(R=n.key.level,U.setDataUniforms(L,y,o,R,v));const e=n.layerData.get(t);if(!e.fillIndexCount)continue;e.prepareForRendering(m);const r=e.fillVertexArrayObject;if(!i.isNone(r)){if(m.bindVAO(r),L.setUniformMatrix3fv("u_dvsMat3",n.transforms.dvs),m.setStencilFunction(s.CompareFunction.EQUAL,n.stencilRef,255),d){const e=Math.max(2**(Math.round(y)-n.key.level),1),t=n.rangeX/(N*n.width*e);L.setUniform1f("u_patternFactor",t)}if(p){const t=e.patternMap;if(!t)continue;for(const[e,n]of t){const t=v.getPageSize(e);i.isSome(t)&&(v.bind(m,s.TextureSamplingMode.LINEAR,e,a.VTL_TEXTURE_BINDING_UNIT_SPRITES),L.setUniform2fv("u_mosaicSize",t),L.setUniform1i("u_texture",a.VTL_TEXTURE_BINDING_UNIT_SPRITES),m.drawElements(s.PrimitiveType.TRIANGLES,n[1],s.DataType.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*n[0]))}}else m.drawElements(s.PrimitiveType.TRIANGLES,e.fillIndexCount,s.DataType.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*e.fillIndexStart);n.triangleCount+=e.fillIndexCount/3}}},f._drawOutline=function(e,t,a,o,l,f,c){const{context:d,displayLevel:T,state:p,drawPhase:_,painter:m,pixelRatio:y,spriteMosaic:E,requestRender:I}=e,P=a.outlineMaterial,g=m.vectorTilesMaterialManager,v=.75/y,S=_===r.WGLDrawPhase.HITTEST,U=this._outlineProgramOptions;U.id=S;const M=g.getMaterialProgram(d,P,U);if(i.isSome(I)&&!S&&!M.isCompiled)return void I();d.useProgram(M),M.setUniformMatrix3fv("u_displayMat3",f===n.TranslateAnchor.VIEWPORT?p.displayMat3:p.displayViewMat3),M.setUniform2fv("u_fillTranslation",l),M.setUniform1f("u_depth",a.z+u),M.setUniform1f("u_outline_width",v),S&&M.setUniform4fv("u_id",c);let N=-1;for(const n of o){if(!n.layerData.has(t))continue;n.key.level!==N&&(N=n.key.level,P.setDataUniforms(M,T,a,N,E));const e=n.layerData.get(t);if(e.prepareForRendering(d),!e.outlineIndexCount)continue;const r=e.outlineVertexArrayObject;i.isNone(r)||(d.bindVAO(r),M.setUniformMatrix3fv("u_dvsMat3",n.transforms.dvs),d.setStencilFunction(s.CompareFunction.EQUAL,n.stencilRef,255),d.drawElements(s.PrimitiveType.TRIANGLES,e.outlineIndexCount,s.DataType.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*e.outlineIndexStart),n.triangleCount+=e.outlineIndexCount/3)}},l}(l);e.WGLBrushVTLFill=f,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
