/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{releaseMaybe as t}from"../../../../core/maybe.js";import{copy as e}from"../../../../core/libs/gl-matrix-2/math/vec4.js";import{create as i,ZEROS as s}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{ViewportSize as r}from"./BindParameters.js";import{ColorAttachment1 as o}from"../../../webgl/enums.js";import{ensureAttachmentMaxSize as h}from"../../../webgl/FramebufferObject.js";class l{constructor(t,e){this._fbos=t,this._bind=e,this._size=new r(0,0),this._clearColor=i()}dispose(){this._color=t(this._color),this.releaseDepth()}initialize(t,i,s){const r=this._color;return this._color=null,this.releaseDepth(),this._size.width=t,this._size.height=i,h(this._size,this._fbos.rctx.parameters.maxTextureSize),e(this._clearColor,s),r}releaseDepth(){this._color?.detachDepth(),this._depth=t(this._depth)}update(t){return this.bind(),this._color=t(this.color),this._color}bind(){const{rctx:t}=this._fbos,e=null==this._color;this.color.attachDepth(this.depth),t.bindFramebuffer(this.color.fbo),e&&(t.setClearStencil(0),t.setClearColor(this._clearColor[0],this._clearColor[1],this._clearColor[2],this._clearColor[3]),t.clear(17664),this.color.getTexture(o)&&t.clearBuffer(1,s))}ensureEmissionAttachment(){this.color.getTexture(o)||(this.color.acquireColor(o,this._bind.useFloatBlend.value?8:5,"emissive"),this._fbos.rctx.bindFramebuffer(this.color.fbo),this._fbos.rctx.resetDrawBuffers(this._bind.output),this._fbos.rctx.clearBuffer(1,s))}get size(){return this._size}get color(){return this._ensureColor()}get depth(){return this._depth??=this._fbos.acquireDepth(14,this._size.width,this._size.height,"main depth"),this._depth}_ensureColor(){return this._color??=this._bind.hasOpaqueEmission?this._fbos.acquire(this._size.width,this._size.height,"main").acquireColor(o,this._bind.useFloatBlend.value?8:5,"emissive"):this._fbos.acquire(this._size.width,this._size.height,"main"),this._color}}export{l as MainFramebuffer};
