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