/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{tileSize as e,textureBindingRenderer1 as t,textureBindingRenderer0 as i}from"../../../definitions.js";import{FeatureTechnique as r}from"../FeatureTechnique.js";import{isHighlight as s,isHittest as o,getSelectionDefines as n,getFeatureTextures as a,getFeatureUniforms as d,getFeaturePipelineState as l,getViewUniforms as u}from"../featureTechniqueUtils.js";import{DotDensityPointShader as c}from"./DotDensityPointShader.js";import{DotDensityPolygonShader as p}from"./DotDensityPolygonShader.js";import{DotDensityResources as h}from"./DotDensityResources.js";import{FillShader as f}from"../shaders/FillShader.js";class m extends r{constructor(){super(...arguments),this.type=12,this.shaders={polygon:new p,point:new c,fill:new f},this._resources=new Map}render(e,t){s(e)||o(e)?this._renderPolygons(e,t):this._renderDotDensity(e,t)}_renderPolygons(e,t){const{painter:i}=e;i.setShader({shader:this.shaders.fill,uniforms:{...d(e,t.target),visualVariableColor:null,visualVariableOpacity:null},textures:a(e),defines:{...n(e)},optionalAttributes:{zoomRange:!1},useComputeBuffer:o(e)}),i.setPipelineState(l(e)),i.submitDraw(e,t)}_renderDotDensity(r,s){const{context:o,painter:c,requiredLevel:p}=r,h=s.instance.getInput().uniforms,f=this._getOrCreateResourcesRecord(o),m=f.getDotDensityTextures(o,e,h.seed),w=1/2**(p-s.target.key.level),g=e,x=g*window.devicePixelRatio*g*window.devicePixelRatio,D=1/w*(1/w),b=h.dotScale?r.state.scale/h.dotScale:1,y=h.dotValue*b*D;c.setShader({shader:this.shaders.polygon,uniforms:{...d(r,s.target),instance:{isActive:h.isActive,colors:h.colors,dotValue:Math.max(1,y)},draw:{tileZoomFactor:w,pixelRatio:window.devicePixelRatio,tileDotsOverArea:x/(e*window.devicePixelRatio*e*window.devicePixelRatio)}},textures:{...a(r),drawTextures:{dotTexture0:{unit:i,texture:m[0]},dotTexture1:{unit:t,texture:m[1]}}},defines:{...n(r),blending:h.blending},optionalAttributes:{},useComputeBuffer:!1});const R=o.getViewport();o.setViewport(0,0,e,e);const v=o.boundFramebuffer,P=f.getFBO(o);o.bindFramebuffer(P),o.setClearColor(0,0,0,0),o.clear(16384),c.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"composite"},depth:!1,stencil:!1}),c.updatePipelineState(o),c.submitDraw(r,s),o.bindFramebuffer(v),o.setViewport(R.x,R.y,R.width,R.height);const S=f.getFBO(o).colorTexture,_={shader:this.shaders.point,uniforms:{view:u(r,s.target),instance:{dotSize:h.dotSize},draw:{tileZoomFactor:1,pixelRatio:window.devicePixelRatio}},textures:{drawLocations:{unit:i,texture:S}},defines:{...n(r)},optionalAttributes:{},useComputeBuffer:!1};c.setPipelineState(l(r)),c.submitDrawMesh(o,_,f.getDotDensityMesh(o),{stencilRef:s.getStencilReference()})}shutdown(e){super.shutdown(e),this._resources.get(e)?.destroy(),this._resources.delete(e)}_getOrCreateResourcesRecord(e){let t=this._resources.get(e);return null==t&&(t=new h,this._resources.set(e,t)),t}}export{m as DotDensityTechnique};
