/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{FeatureTechnique as e}from"../FeatureTechnique.js";import{isHittest as t,getSelectionDefines as r,getFeatureTextures as s,getFeatureUniforms as i}from"../featureTechniqueUtils.js";import{HeatmapResources as n}from"./HeatmapResources.js";import{HeatmapAccumulateShader as o}from"../shaders/HeatmapAccumulateShader.js";import{HeatmapResolveShader as u}from"../shaders/HeatmapResolveShader.js";class a extends e{constructor(){super(...arguments),this.type=19,this.drawPhase=73,this.shaders={accumulate:new o,resolve:new u},this._isBound=!1,this._resources=new Map}shutdown(e){super.shutdown(e),this._resources.get(e)?.destroy(),this._resources.delete(e),this._prevFBO=null,this._unbind()}render(e,n){const{context:o,painter:u,state:a}=e,l=n.instance.getInput(),{isFieldActive:d}=l.uniforms,p=this._getOrCreateResourcesRecord(o),m=p.loadQualityProfile(o);t(e)||this._bind(e,p,l),u.setShader({shader:this.shaders.accumulate,uniforms:{...i(e,n.target),kernelControls:{radius:f(l,a),isFieldActive:d?1:0}},textures:s(e),defines:{...r(e),...m.defines},optionalAttributes:{},useComputeBuffer:t(e)});const _=t(e)?h:c;u.setPipelineState(_),u.submitDraw(e,n)}getStencilReference(e){return d(e)}renderResolvePass(e,r){if(t(e))return;const{context:s,painter:i}=e,n=this._resources.get(s);if(null==this._prevFBO||null==this._prevViewport||!n?.initialized)return;const{defines:o}=n.loadQualityProfile(s),{minDensity:u,maxDensity:a,radius:l}=r.getInput().uniforms,d=8,c=9,h=n.accumulateFramebuffer,f=n.resolveGradientTexture,m={shader:this.shaders.resolve,uniforms:{accumulatedDensity:{minAndInvRange:[u,1/(a-u)],normalization:3/(l*l*Math.PI)}},textures:{densityTexture:{unit:d,texture:h.colorTexture},gradientTexture:{unit:c,texture:f}},defines:o,optionalAttributes:{},useComputeBuffer:!1};s.bindFramebuffer(this._prevFBO),s.setViewport(0,0,this._prevViewport.width,this._prevViewport.height),s.bindTexture(h.colorTexture,d),s.bindTexture(f,c),i.setPipelineState(p),i.submitDrawMesh(s,m,i.quadMesh),this._unbind()}_getOrCreateResourcesRecord(e){let t=this._resources.get(e);return null==t&&(t=new n,this._resources.set(e,t)),t}_unbind(){this._prevFBO=null,this._prevViewport=null,this._isBound=!1}_bind(e,t,r){const{context:s,state:i,pixelRatio:n}=e,o=s.boundFramebuffer;if(this._isBound||null==o)return;const u=s.getViewport();this._prevFBO=o,this._prevViewport=u;const{gradient:a,gradientHash:d}=r.uniforms;t.ensureResolveGradientTexture(s,d,a);const{width:c,height:h}=u,p=l(f(r,i),n),m=c*p,_=h*p,w=t.ensureAccumulateFBO(s,m,_);s.blitFramebuffer(o,w,1024),s.bindFramebuffer(w),s.setViewport(0,0,w.width,w.height),s.setColorMask(!0,!0,!0,!0),s.setClearColor(0,0,0,0),s.clear(16384),this._isBound=!0}}function l(e,t){const r=t>1.5?.25:.5;return e<1/(2*r)?1:r}function d(e){return e.key.level+1}const c={color:{write:[!0,!0,!0,!0],blendMode:"additive"},depth:!1,stencil:{write:!1,test:{compare:518,mask:255,op:{fail:7680,zFail:7680,zPass:7681}}}},h={...c,stencil:!1},p={color:{write:[!0,!0,!0,!0],blendMode:"composite"},depth:!1,stencil:!1};function f(e,t){const{referenceScale:r,radius:s}=e.uniforms;return s*(0!==r?r/t.scale:1)}export{a as HeatmapTechnique};
