/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{FeatureTechnique as e}from"../FeatureTechnique.js";import{isHittest as t,getSelectionDefines as r,getFeatureUniforms as s}from"../featureTechniqueUtils.js";import{HeatmapResources as i}from"./HeatmapResources.js";import{HeatmapAccumulateShader as n}from"../shaders/HeatmapAccumulateShader.js";import{HeatmapResolveShader as o}from"../shaders/HeatmapResolveShader.js";class u extends e{constructor(){super(...arguments),this.type=18,this.drawPhase=73,this.shaders={accumulate:new n,resolve:new o},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,i){const{context:n,painter:o,state:u}=e,a=i.instance.getInput(),{isFieldActive:l}=a.uniforms,h=this._getOrCreateResourcesRecord(n),f=h.loadQualityProfile(n);t(e)||this._bind(e,h,a),o.setShader({shader:this.shaders.accumulate,uniforms:{...s(e,i.target),kernelControls:{radius:p(a,u),isFieldActive:l?1:0}},defines:{...r(e),...f.defines},optionalAttributes:{},useComputeBuffer:t(e)});const m=t(e)?c:d;o.setPipelineState(m),o.submitDraw(e,i)}getStencilReference(e){return l(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,p=n.accumulateFramebuffer,f=n.resolveGradientTexture,m={shader:this.shaders.resolve,uniforms:{accumulatedDensity:{texture:{unit:d,texture:p.colorTexture},minAndInvRange:[u,1/(a-u)],normalization:3/(l*l*Math.PI)},gradient:{texture:{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(p.colorTexture,d),s.bindTexture(f,c),i.setPipelineState(h),i.submitDrawMesh(s,m,i.quadMesh),this._unbind()}_getOrCreateResourcesRecord(e){let t=this._resources.get(e);return null==t&&(t=new i,this._resources.set(e,t)),t}_unbind(){this._prevFBO=null,this._prevViewport=null,this._isBound=!1}_bind(e,t,r){if(this._isBound)return;const{context:s,state:i,pixelRatio:n}=e,o=s.getBoundFramebufferObject(),u=s.getViewport();this._prevFBO=o,this._prevViewport=u;const{gradient:l,gradientHash:d}=r.uniforms;t.ensureResolveGradientTexture(s,d,l);const{width:c,height:h}=u,f=a(p(r,i),n),m=c*f,_=h*f,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 a(e,t){const r=t>1.5?.25:.5;return e<1/(2*r)?1:r}function l(e){return e.key.level+1}const d={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}}}},c={...d,stencil:!1},h={color:{write:[!0,!0,!0,!0],blendMode:"composite"},depth:!1,stencil:!1};function p(e,t){const{referenceScale:r,radius:s}=e.uniforms;return s*(0!==r?r/t.scale:1)}export{u as HeatmapTechnique};
