@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.89 kB
JavaScript
import{StringIds as t}from"../../../core/string-ids.js";const e=new t;class s{constructor(){this.format=void 0,this.multisampledBuffer=void 0}destroy(){var t;null==(t=this.multisampledBuffer)||t.destroy(),this.multisampledBuffer=null}}class r{constructor(t){this.initialized=!1,this.key=void 0,this.colorAttachments=[],this.depthFormat=void 0,this.hasStencil=void 0,this.depthTexture=null,this.depthTextureInternal=!1,this.assignedColorTexture=null,this.renderPassDescriptor={},this.renderTarget=t,t._colorBuffers&&t._colorBuffers.forEach(((t,e)=>{this.setColorAttachment(e,void 0,t.impl.format)})),this.updateKey()}destroy(t){var e;(this.initialized=!1,this.depthTextureInternal)&&(null==(e=this.depthTexture)||e.destroy(),this.depthTexture=null);this.assignedColorTexture=null,this.colorAttachments.forEach((t=>{t.destroy()})),this.colorAttachments.length=0}updateKey(){const t=this.renderTarget;let s=`${t.samples}:${t.depth?this.depthFormat:"nodepth"}`;this.colorAttachments.forEach((t=>{s+=`:${t.format}`})),this.key=e.get(s)}setDepthFormat(t){this.depthFormat=t,this.hasStencil="depth24plus-stencil8"===t}assignColorTexture(t){this.assignedColorTexture=t;const e=t.createView(),s=this.renderPassDescriptor.colorAttachments[0];this.renderTarget.samples>1?s.resolveTarget=e:s.view=e,this.setColorAttachment(0,void 0,t.format),this.updateKey()}setColorAttachment(t,e,r){this.colorAttachments[t]||(this.colorAttachments[t]=new s),e&&(this.colorAttachments[t].multisampledBuffer=e),r&&(this.colorAttachments[t].format=r)}init(t,e){var s,r;const i=t.wgpu;this.initDepthStencil(i,e),this.renderPassDescriptor.colorAttachments=[];const o=null!=(s=null==(r=e._colorBuffers)?void 0:r.length)?s:1;for(let t=0;t<o;++t){var l;const s=this.initColor(i,e,t),r=0===t&&(null==(l=this.colorAttachments[0])?void 0:l.format);(s.view||r)&&this.renderPassDescriptor.colorAttachments.push(s)}this.initialized=!0}initDepthStencil(t,e){const{samples:s,width:r,height:i,depth:o,depthBuffer:l}=e;if(o||l){if(l)this.depthTexture=l.impl.gpuTexture,this.setDepthFormat(l.impl.format);else{this.setDepthFormat("depth24plus-stencil8");const e={size:[r,i,1],dimension:"2d",sampleCount:s,format:this.depthFormat,usage:GPUTextureUsage.RENDER_ATTACHMENT};e.usage|=s>1?GPUTextureUsage.TEXTURE_BINDING:GPUTextureUsage.COPY_SRC,this.depthTexture=t.createTexture(e),this.depthTextureInternal=!0}this.renderPassDescriptor.depthStencilAttachment={view:this.depthTexture.createView()}}}initColor(t,e,s){const r={},{samples:i,width:o,height:l}=e,a=e.getColorBuffer(s);let h=null;if(a){const t=1;h=a.cubemap?a.impl.createView({dimension:"2d",baseArrayLayer:e.face,arrayLayerCount:1,mipLevelCount:t}):a.impl.createView({mipLevelCount:t})}if(i>1){var n,c;const e={size:[o,l,1],dimension:"2d",sampleCount:i,format:null!=(n=null==(c=this.colorAttachments[s])?void 0:c.format)?n:a.impl.format,usage:GPUTextureUsage.RENDER_ATTACHMENT},d=t.createTexture(e);this.setColorAttachment(s,d,e.format),r.view=d.createView(),r.resolveTarget=h}else r.view=h;return r}setupForRenderPass(t){var e,s;const r=null!=(e=null==(s=this.renderPassDescriptor.colorAttachments)?void 0:s.length)?e:0;for(let e=0;e<r;++e){const s=this.renderPassDescriptor.colorAttachments[e],r=t.colorArrayOps[e];s.clearValue=r.clearValue,s.loadOp=r.clear?"clear":"load",s.storeOp=r.store?"store":"discard"}const i=this.renderPassDescriptor.depthStencilAttachment;i&&(i.depthClearValue=t.depthStencilOps.clearDepthValue,i.depthLoadOp=t.depthStencilOps.clearDepth?"clear":"load",i.depthStoreOp=t.depthStencilOps.storeDepth?"store":"discard",i.depthReadOnly=!1,this.hasStencil&&(i.stencilClearValue=t.depthStencilOps.clearStencilValue,i.stencilLoadOp=t.depthStencilOps.clearStencil?"clear":"load",i.stencilStoreOp=t.depthStencilOps.storeStencil?"store":"discard",i.stencilReadOnly=!1))}loseContext(){this.initialized=!1}resolve(t,e,s,r){}}export{r as WebgpuRenderTarget};