@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 4.82 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import e from"../../../../core/Error.js";import{ColorFormat as t,DepthFormat as r}from"../../webgl/formats.js";import{ManagedColorAttachment as a}from"../../webgl/ManagedColorAttachment.js";import{ManagedDepthAttachment as o}from"../../webgl/ManagedDepthAttachment.js";import c from"../../webgl/ManagedFBO.js";import{AttachmentType as i}from"../../webgl/ManagedFBObject.js";import{FBOPool as h}from"./FBOPool.js";import{PixelFormat as n,SizedPixelFormat as s,TextureWrapMode as m,PixelType as _,TextureSamplingMode as d,RenderbufferFormat as l,ColorAttachment as u}from"../../../webgl/enums.js";import{FramebufferObject as p}from"../../../webgl/FramebufferObject.js";import{Renderbuffer as f}from"../../../webgl/Renderbuffer.js";import{RenderbufferDescriptor as w}from"../../../webgl/RenderbufferDescriptor.js";import{Texture as E}from"../../../webgl/Texture.js";import{TextureDescriptor as C}from"../../../webgl/TextureDescriptor.js";class T{constructor(e){this.rctx=e,this._acquired=new Set,this._cache=new h(e.newCache,"FBOCache"),this._depthCache=new h(e.newCache,"DepthAttachmentCache"),this._colorCache=new h(e.newCache,"ColorAttachmentCache")}destroy(){this._cache.destroy(),this._depthCache.destroy(),this._colorCache.destroy()}clean(){this._cache.clean(),this._colorCache.clean(),this._depthCache.clean()}frameStart(){this._cache.frame(),this._colorCache.frame(),this._depthCache.frame(),this.debugCallback?.()}frameEnd(){const e=this.debugCallback;e&&this._acquired.forEach((t=>t.type===i.FBO&&e(t.name,t.fbo,t.numberOfColorAttachments)))}get usedMemory(){return Array.from(this._acquired.values()).reduce(((e,t)=>e+("getTexture"in t?t.getTexture()?.usedMemory??0:t.cachedMemory)),this._cache.usedMemory+this._colorCache.usedMemory+this._depthCache.usedMemory)}set interactive(e){this._cache.interactive=this._colorCache.interactive=this._depthCache.interactive=e}acquire(a,o,i,h=t.RGBA){const n=A(h,a,o);let s=this._cache.pop(n);if(s){s.retain(),s.setName(i);const t=this.rctx.getBoundFramebufferObject();if(this.rctx.bindFramebuffer(s.fbo),this.rctx.setDrawBuffers([u.COLOR_ATTACHMENT0]),!s.fbo)throw new e("attempt to use a not existing framebuffer");this.rctx.unbindTexture(s.fbo.colorTexture),this.rctx.bindFramebuffer(t)}else s=new c(n,i,new p(this.rctx,{...G[h],width:a,height:o}),(e=>{e??=r.DEPTH_STENCIL_TEXTURE;const t=this._acquireDepth(e,s.fbo.width,s.fbo.height,`${s.name} depth`);return s.attachDepth(t),t.release(),s}),((e,r,c)=>{r??=t.RGBA;const i=this._acquireColor(r,a,o,c??`${s.name} color ${e}`);return this.rctx.unbindTexture(i.attachment),s.attachColor(i,e),i.release(),s}),(()=>{this.debugCallback?.(s.name,s.fbo,s.numberOfColorAttachments),this._acquired.delete(s),s.detachAll(),this._cache.put(s)}));return this._trackHandle(s)}acquireDepth(e,t,r,a){return this._acquireDepth(e,t,r,a)}_acquireDepth(e,t,a,c){const i=A(e,t,a),h=this._depthCache.pop(i);if(h)return h.retain(),h.name=c,this._trackHandle(h);const n=e===r.DEPTH_STENCIL_TEXTURE?new o(i,new E(this.rctx,{...P[e],width:t,height:a}),(()=>{this._acquired.delete(n),this._depthCache.put(n)})):new o(i,new f(this.rctx,{...P[e],width:t,height:a}),(()=>{this._acquired.delete(n),this._depthCache.put(n)}));return n.name=c,this._trackHandle(n)}_acquireColor(e,t,r,o){const c=A(e,t,r),i=this._colorCache.pop(c);if(i)return i.retain(),i.name=o,this._trackHandle(i);const h=new a(c,new E(this.rctx,{...G[e],width:t,height:r}),(()=>{this._acquired.delete(h),this._colorCache.put(h)}));return h.name=o,this._trackHandle(h)}_trackHandle(e){return this._acquired.add(e),e}}const b=new c("default","default",null,(()=>b),(()=>b),(()=>{}));function A(e,t,r){return`${e}x${t}x${r}`}b.release=()=>!1;const M=new C;M.pixelFormat=n.RED,M.internalFormat=s.R8,M.wrapMode=m.CLAMP_TO_EDGE;const g=new C;g.pixelFormat=n.RG,g.internalFormat=s.RG8,g.wrapMode=m.CLAMP_TO_EDGE;const R=new C;R.internalFormat=s.RGBA4,R.dataType=_.UNSIGNED_SHORT_4_4_4_4,R.wrapMode=m.CLAMP_TO_EDGE;const F=new C;F.wrapMode=m.CLAMP_TO_EDGE;const x=new C;x.wrapMode=m.CLAMP_TO_EDGE,x.samplingMode=d.LINEAR_MIPMAP_LINEAR,x.hasMipmap=!0,x.maxAnisotropy=8;const D=new C;D.pixelFormat=n.RED,D.dataType=_.HALF_FLOAT,D.internalFormat=s.R16F,D.samplingMode=d.NEAREST;const O=new C;O.dataType=_.HALF_FLOAT,O.internalFormat=s.RGBA16F,O.samplingMode=d.NEAREST;const G={[t.RED]:M,[t.RG]:g,[t.RGBA4]:R,[t.RGBA]:F,[t.RGBA_MIPMAP]:x,[t.R16F]:D,[t.RGBA16F]:O},N=new C;N.pixelFormat=n.DEPTH_STENCIL,N.dataType=_.UNSIGNED_INT_24_8,N.samplingMode=d.NEAREST,N.wrapMode=m.CLAMP_TO_EDGE,N.internalFormat=n.DEPTH24_STENCIL8;const P={[r.DEPTH_STENCIL_TEXTURE]:N,[r.DEPTH16_BUFFER]:new w(l.DEPTH_COMPONENT16,4)};export{T as FBOCache,b as defaultWebGLFBO};