/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{disposeMaybe as e}from"../../../../../../../core/maybe.js";import{simplePipelineState as t}from"../../utils.js";import{Technique as s}from"../Technique.js";import{BlitShader as r}from"../shaders/BlitShader.js";import{RadialBlurShader as i,GaussianBlurShader as u}from"../shaders/BlurShader.js";import{DisposableFramebufferObject as a}from"../../../../../../webgl/FramebufferObject.js";import{TextureDescriptor as n}from"../../../../../../webgl/TextureDescriptor.js";const o=[1,0],l=[0,1];class d extends s{constructor(){super(...arguments),this.type=7,this._blurFBO=null,this._size=[0,0],this.shaders={gaussianBlur:new u,radialBlur:new i,blit:new r}}shutdown(){super.shutdown(),this._blurFBO=e(this._blurFBO)}render(e,t){const{type:s,radius:r}=t.effect,{layerFBO:i}=t;0!==r&&(this._createOrResizeResources(e),"blur"===s?this._gaussianBlur(e,i,t.effect.radius):this._radialBlur(e,i))}_gaussianBlur(e,s,r){const{context:i,state:u,painter:a,pixelRatio:n}=e,{size:d}=u,h=[Math.round(n*d[0]),Math.round(n*d[1])],c=this._blurFBO,f={...t,color:{write:[!0,!0,!0,!0],blendMode:"none"}};a.setPipelineState(f),i.bindFramebuffer(c),a.submitDrawMesh(i,{shader:this.shaders.gaussianBlur,uniforms:{gaussianBlurConfig:{texSize:h,direction:o,sigma:r}},textures:{texture:{texture:s.colorTexture,unit:4}},defines:{kernelRadius:Math.ceil(r)},optionalAttributes:null,useComputeBuffer:!1},a.quadMesh),i.bindFramebuffer(s);const b={color:{write:[!0,!0,!0,!0],blendMode:"none"},depth:{write:!1,test:!1},stencil:{write:{mask:0},test:!1}};a.setPipelineState(b),a.submitDrawMesh(i,{shader:this.shaders.gaussianBlur,uniforms:{gaussianBlurConfig:{texSize:h,direction:l,sigma:r}},textures:{texture:{texture:c?.colorTexture,unit:5}},defines:{kernelRadius:Math.ceil(r)},optionalAttributes:null,useComputeBuffer:!1},a.quadMesh),a.setPipelineState({...t,stencil:{test:{mask:255,compare:514,op:{fail:7680,zFail:7680,zPass:7680}},write:!1},color:{write:[!0,!0,!0,!0],blendMode:"additive"}})}_radialBlur(e,s){const{context:r,painter:i}=e,u=this._blurFBO;r.bindFramebuffer(u);const a={...t,color:{write:[!0,!0,!0,!0],blendMode:"none"}};i.setPipelineState(a),i.submitDrawMeshUntyped(r,{shader:this.shaders.radialBlur,uniforms:{},textures:{texture:{texture:s.colorTexture,unit:4}},defines:null,optionalAttributes:null,useComputeBuffer:!1},i.quadMesh),r.bindFramebuffer(s),i.setPipelineState({...t,color:{write:[!0,!0,!0,!0],blendMode:"custom",blendParameters:{srcRGB:1,dstRGB:771,srcAlpha:1,dstAlpha:771}}}),i.submitDrawMeshUntyped(r,{shader:this.shaders.blit,uniforms:{},textures:{blitTexture:{texture:u?.colorTexture,unit:5}},defines:null,optionalAttributes:null,useComputeBuffer:!1},i.quadMesh)}_createOrResizeResources(e){const{context:t,state:s,pixelRatio:r}=e,{size:i}=s,u=Math.round(r*i[0]),o=Math.round(r*i[1]);if(!this._blurFBO||this._size[0]!==u||this._size[1]!==o)if(this._size[0]=u,this._size[1]=o,this._blurFBO)this._blurFBO.resize(u,o);else{const e=new n(u,o);e.internalFormat=6408,e.wrapMode=33071,this._blurFBO=new a(t,e)}}}export{d as BlurTechnique};
