/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../../chunks/_rollupPluginBabelHelpers","../../../../../core/Logger","../../../../../core/maybe","../enums","../VertexStream","./WGLGeometryBrushMarker","../techniques/utils","../../../../webgl/context-util","../../../../webgl/enums","../../../../webgl/FramebufferObject","../../../../webgl/Texture"],(function(e,t,a,r,i,s,u,n,o,l,d){"use strict";const c=t.getLogger("esri.views.2d.engine.webgl.brushes.WGLBrushHeatmap"),m={vsPath:"heatmap/heatmapResolve",fsPath:"heatmap/heatmapResolve",attributes:new Map([["a_position",0]])};let h=function(t){function s(){return t.apply(this,arguments)||this}e._inheritsLoose(s,t);var h=s.prototype;return h.supportsSymbology=function(e){return e===r.WGLSymbologyType.HEATMAP},h.dispose=function(){t.prototype.dispose.call(this),this._accumulateOutputTexture=a.disposeMaybe(this._accumulateOutputTexture),this._accumulateFramebuffer=a.disposeMaybe(this._accumulateFramebuffer),this._resolveGradientTexture=a.disposeMaybe(this._resolveGradientTexture),this._tileQuad=a.disposeMaybe(this._tileQuad)},h.drawGeometry=function(e,a,r,i){const{defines:s}=this._loadQualityProfile(e.context);t.prototype.drawGeometry.call(this,e,a,r,i?[...i,...s]:s)},h._drawMarkers=function(e,t,a,r,i,s,u){const{context:n}=e,l=n.getBoundFramebufferObject(),d=n.getViewport();this._prepareAccumulatePass(e,t,a,u),n.drawElements(r,i,o.DataType.UNSIGNED_INT,s),u||this._drawResolvePass(e,t,l,d)},h._prepareAccumulatePass=function(e,t,a,r){const{context:i,rendererInfo:s,state:n,displayLevel:l}=e,{rendererSchema:d}=s;u.assertRendererSchema(d,"heatmap");const{referenceScale:c,radius:m,isFieldActive:h}=d;this._loadResources(i),this._updateResources(d);const p=m*(0!==c?c/n.scale:1)*2**(t.key.level-l);a.setUniform1f("u_radius",p),r||(i.bindFramebuffer(this._accumulateFramebuffer),a.setUniform1f("u_isFieldActive",h),i.setViewport(0,0,this._accumulateFramebuffer.width,this._accumulateFramebuffer.height),i.setBlendingEnabled(!0),i.setBlendFunction(o.BlendFactor.ONE,o.BlendFactor.ONE),i.setClearColor(0,0,0,0),i.clear(o.ClearBufferBit.COLOR_BUFFER_BIT))},h._drawResolvePass=function(e,t,a,r){const{context:i,painter:s,rendererInfo:n}=e,{rendererSchema:l}=n;u.assertRendererSchema(l,"heatmap");const{radius:d,minDensity:c,densityRange:h}=l,{defines:p}=this._loadQualityProfile(i),f=s.materialManager.getProgram(m,p);i.useProgram(f),this._setSharedUniforms(f,e,t),i.bindFramebuffer(a),i.setViewport(0,0,r.width,r.height),i.setBlendFunction(o.BlendFactor.ONE,o.BlendFactor.ONE_MINUS_SRC_ALPHA),i.bindTexture(this._accumulateOutputTexture,8),i.bindTexture(this._resolveGradientTexture,9),f.setUniform1i("u_texture",8),f.setUniform1i("u_gradient",9),f.setUniform2f("u_densityMinAndInvRange",c,1/h),f.setUniform1f("u_densityNormalization",3/(d*d*Math.PI)),this._tileQuad.draw()},h._loadResources=function(e){const t=e.type===n.ContextType.WEBGL2,{dataType:a,samplingMode:r,shadingRate:s}=this._loadQualityProfile(e);null!=this._accumulateOutputTexture||(this._accumulateOutputTexture=new d.Texture(e,{target:o.TextureType.TEXTURE_2D,pixelFormat:t?o.PixelFormat.RED:o.PixelFormat.RGBA,internalFormat:t?a===o.PixelType.FLOAT?o.SizedPixelFormat.R32F:o.SizedPixelFormat.R16F:o.PixelFormat.RGBA,dataType:a,samplingMode:r,wrapMode:o.TextureWrapMode.CLAMP_TO_EDGE,width:Math.round(512*s),height:Math.round(512*s)})),null!=this._accumulateFramebuffer||(this._accumulateFramebuffer=new l.FramebufferObject(e,{},this._accumulateOutputTexture)),null!=this._resolveGradientTexture||(this._resolveGradientTexture=new d.Texture(e,{target:o.TextureType.TEXTURE_2D,pixelFormat:o.PixelFormat.RGBA,dataType:o.PixelType.UNSIGNED_BYTE,samplingMode:o.TextureSamplingMode.LINEAR,wrapMode:o.TextureWrapMode.CLAMP_TO_EDGE})),null!=this._tileQuad||(this._tileQuad=new i(e,[0,0,1,0,0,1,1,1]))},h._updateResources=function(e){const{gradientHash:t,gradient:a}=e;this._prevGradientHash!==t&&(this._resolveGradientTexture.resize(a.length/4,1),this._resolveGradientTexture.setData(a),this._prevGradientHash=t)},h._loadQualityProfile=function(e){if(a.isNone(this._qualityProfile)){const{textureFloat:t,colorBufferFloat:a}=e.capabilities,{textureFloat:r,textureFloatLinear:i,textureHalfFloat:s,textureHalfFloatLinear:u,HALF_FLOAT:n}=t,{textureFloat:l,textureHalfFloat:d,floatBlend:m}=a;let h,p;const f=r&&l&&m,_=s&&d;f&&i?(h=o.PixelType.FLOAT,p=o.TextureSamplingMode.LINEAR):_&&u?(h=n,p=o.TextureSamplingMode.LINEAR):f?(h=o.PixelType.FLOAT,p=o.TextureSamplingMode.NEAREST):_?(h=n,p=o.TextureSamplingMode.NEAREST):c.error("Missing webgl extensions for heatmap!"),p===o.TextureSamplingMode.NEAREST&&c.warnOnce("Missing linear filtering webgl extension(s). Heatmap quality may be reduced."),this._qualityProfile={dataType:h,samplingMode:p,shadingRate:.5,defines:h===n?["heatmapPrecisionHalfFloat"]:[]}}return this._qualityProfile},s}(s);return h}));
