/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import{clamp as r}from"../../../../../core/mathUtils.js";import{disposeMaybe as t}from"../../../../../core/maybe.js";import{watch as s}from"../../../../../core/reactiveUtils.js";import{Milliseconds as i}from"../../../../../core/time.js";import{property as a,subclass as o}from"../../../../../core/accessorSupport/decorators.js";import{set as n}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{InternalRenderCategory as m}from"../../../webgl.js";import p from"../../../webgl/RenderNode.js";import{distanceFadeEnd as h,distanceFadeStart as c}from"../../core/shaderLibrary/shading/ScreenSpaceConstants.js";import{g as u}from"../../../../../chunks/SSAO.glsl.js";import{SSAOBlurTechnique as l}from"./SSAOBlurTechnique.js";import{noiseData as d}from"./SSAONoiseData.js";import{SSAOPassParameters as f,BlurDrawParameters as b}from"./SSAOParameters.js";import{SSAOTechnique as w}from"./SSAOTechnique.js";import{DepthStencilAttachment as T}from"../../../../webgl/enums.js";import _ from"../../../../webgl/Texture.js";import{TextureDescriptor as P}from"../../../../webgl/TextureDescriptor.js";const g=2;let S=class extends p{constructor(e){super(e),this.consumes={required:["normals"]},this.produces=m.AMBIENT_ILLUMINATION,this._enableTime=i(0),this._passParameters=new f,this._drawParameters=new b}initialize(){const e=Uint8Array.from(atob(d),e=>e.charCodeAt(0)),r=new P(32);r.wrapMode=33071,r.pixelFormat=6407,r.wrapMode=10497,r.hasMipmap=!0,this._passParameters.noiseTexture=new _(this.renderingContext,r,e),this.addHandles(s(()=>this.view.stage.renderer.hasAmbientIllumination,()=>this._enableTime=i(0)))}destroy(){this._passParameters.noiseTexture=t(this._passParameters.noiseTexture)}render(e){const t=e.find(({name:e})=>"normals"===e),s=t?.getTexture(),a=t?.getTexture(T);if(!s||!a)return;const o=this.techniques.getCompiled(w),p=this.techniques.getCompiled(l);if(!o||!p)return this._enableTime=i(performance.now()),void this.requestRender(1);0===this._enableTime&&(this._enableTime=i(performance.now()));const d=this.renderingContext,f=this.view.qualitySettings.fadeDuration,b=this.bindParameters,_=b.camera,P=_.relativeElevation,S=r((h-P)/(h-c),0,1),j=f>0?Math.min(f,performance.now()-this._enableTime)/f:1,q=j*S;this._passParameters.normalTexture=s,this._passParameters.depthTexture=a,this._passParameters.projScale=1/_.computeScreenPixelSizeAtDist(1),this._passParameters.intensity=4*x/u(_)**6*q;const A=_.fullViewport[2],v=_.fullViewport[3],C=this.fboCache.acquire(A,v,"ssao input",2);d.bindFramebuffer(C.fbo),d.setViewport(0,0,A,v),d.bindTechnique(o,b,this._passParameters,this._drawParameters),d.screen.draw();const M=Math.round(A/g),I=Math.round(v/g),y=this.fboCache.acquire(M,I,"ssao blur",0);d.bindFramebuffer(y.fbo),this._drawParameters.colorTexture=C.getTexture(),n(this._drawParameters.blurSize,0,g/v),d.bindTechnique(p,b,this._passParameters,this._drawParameters),d.setViewport(0,0,M,I),d.screen.draw(),C.release();const N=this.fboCache.acquire(M,I,m.AMBIENT_ILLUMINATION,0);return d.bindFramebuffer(N.fbo),d.setViewport(0,0,A,v),d.setClearColor(1,1,1,0),d.clear(16384),this._drawParameters.colorTexture=y.getTexture(),n(this._drawParameters.blurSize,g/A,0),d.bindTechnique(p,b,this._passParameters,this._drawParameters),d.setViewport(0,0,M,I),d.screen.draw(),d.setViewport4fv(_.fullViewport),y.release(),j<1&&this.requestRender(2),N}};e([a()],S.prototype,"consumes",void 0),e([a()],S.prototype,"produces",void 0),S=e([o("esri.views.3d.webgl-engine.effects.ssao.SSAO")],S);const x=.5;export{S as SSAO,g as blurSizePixels};
