UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 4.36 kB
import{nullifyNonNullableForDispose as e,isSome as t}from"../../../../core/maybe.js";import{generateUID as r}from"../../../../core/uid.js";import{PixelType as i,PixelFormat as s,TextureType as h,TextureSamplingMode as o,TextureWrapMode as d,RenderbufferFormat as a,TargetType as p,DepthStencilTargetType as u}from"../../../webgl/enums.js";import{FramebufferObject as c}from"../../../webgl/FramebufferObject.js";import{Renderbuffer as T}from"../../../webgl/Renderbuffer.js";import{Texture as f}from"../../../webgl/Texture.js";import{getGpuMemoryUsage as l}from"../../../webgl/Util.js";const _={dataType:i.UNSIGNED_BYTE,internalFormat:s.RGBA},g={};class n{constructor(e){this._rctx=e,this._activeTargets=new Set,this._depthTextures=new Map,this._depthBuffers=new Map,this._colorTextures=new Map,this._framebuffers=new Map,this.depthTextureSupported=e.capabilities.depthTexture}dispose(){this._depthBuffers.forEach((e=>e.dispose())),this._depthBuffers.clear(),this._depthTextures.forEach((e=>e.dispose())),this._depthTextures.clear(),this._colorTextures.forEach((e=>e.dispose())),this._colorTextures.clear(),this._framebuffers.forEach((e=>e.dispose())),this._framebuffers.clear(),this._activeTargets.clear()}disposeTargetResource(e){const t=e.id;this._activeTargets.has(t)&&(this._activeTargets.delete(t),this._disposeWithFramebuffers(this._depthTextures,t),this._disposeWithFramebuffers(this._depthBuffers,t),this._disposeWithFramebuffers(this._colorTextures,t))}_disposeWithFramebuffers(e,t){const r=e.get(t);r&&(this._framebuffers.forEach(((e,t)=>{e.colorAttachment!==r&&e.depthStencilAttachment!==r||(e.detachAll(),e.dispose(),this._framebuffers.delete(t))})),r.dispose(),e.delete(t))}getDepthTexture(t,r){if(!this.depthTextureSupported)return null;let a=this._depthTextures.get(t.id);return!a||a.descriptor.width===r.width&&a.descriptor.height===r.height||(a.dispose(),a=e()),a||(a=new f(this._rctx,{target:h.TEXTURE_2D,pixelFormat:s.DEPTH_STENCIL,dataType:i.UNSIGNED_INT_24_8,samplingMode:o.NEAREST,wrapMode:d.CLAMP_TO_EDGE,width:r.width,height:r.height}),this._depthTextures.set(t.id,a),this._activeTargets.add(t.id)),a}getAllocatedDepthTexture(e){return this._depthTextures.get(e.id)}getDepthBuffer(e,t){if(this.depthTextureSupported)return null;let r=this._depthBuffers.get(e.id);return r?r.descriptor.width===t.width&&r.descriptor.height===t.height||r.resize(t.width,t.height):(r=new T(this._rctx,{internalFormat:a.DEPTH_STENCIL,...t}),this._depthBuffers.set(e.id,r),this._activeTargets.add(e.id)),r}getColorTexture(t,r){let i=this._colorTextures.get(t.id);return i&&(i.descriptor.width===r.width&&i.descriptor.height===r.height||(i.dispose(),i=e())),i||(i=new f(this._rctx,{target:h.TEXTURE_2D,pixelFormat:s.RGBA,internalFormat:t.internalFormat,dataType:t.dataType,samplingMode:null!=t.samplingMode?t.samplingMode:o.LINEAR,wrapMode:d.CLAMP_TO_EDGE,width:r.width,height:r.height}),this._colorTextures.set(t.id,i),this._activeTargets.add(t.id)),i}getAllocatedColorTexture(e){return this._colorTextures.get(e.id)}registerDepthTarget(e={}){return{id:r(),...g,...e}}registerColorTarget(e={}){return{id:r(),..._,...e}}getFramebuffer(e,r,i){const s=this._getKey(r,i);let h=this._framebuffers.get(s);const o=this.getColorTexture(r,e);if(this.depthTextureSupported){const r=i?this.getDepthTexture(i,e):void 0;if(!h)return h=t(i)?new c(this._rctx,{colorTarget:p.TEXTURE,depthStencilTarget:u.DEPTH_STENCIL_TEXTURE},o,r):new c(this._rctx,{colorTarget:p.TEXTURE,depthStencilTarget:u.NONE},o),this._framebuffers.set(s,h),h;return(h.width!==e.width||h.height!==e.height||h.colorTexture!==o||h.depthStencilTexture!==r)&&(h.detachAll(),h.resize(e.width,e.height),h.attachColorTexture(o),h.attachDepthStencilTexture(r)),h}const d=i?this.getDepthBuffer(i,e):void 0;if(!h)return h=new c(this._rctx,{colorTarget:p.TEXTURE,depthStencilTarget:i?u.DEPTH_STENCIL_RENDER_BUFFER:u.NONE},o,d),this._framebuffers.set(s,h),h;return(h.width!==e.width||h.height!==e.height||h.colorTexture!==o)&&(h.detachAll(),h.resize(e.width,e.height),h.attachColorTexture(o),h.attachDepthStencilBuffer(d)),h}_getKey(e,t){return`${e.id}_${t?t.id:"X"}_${e.name}${t?"_"+t.name:""}`}get gpuMemoryUsage(){let e=0;const t=new Set,r=r=>{t.has(r)||(t.add(r),e+=l(r))};return this._depthTextures.forEach(r),this._colorTextures.forEach(r),this._depthBuffers.forEach(r),e}}export{n as RenderTargetHelper};