@phil65/demoreel 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -12
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/args.js +26 -0
- package/dist/cli.js +53 -3
- package/dist/compose.d.ts +4 -0
- package/dist/compose.js +4 -0
- package/dist/gif.d.ts +16 -0
- package/dist/gif.js +19 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.js +441 -0
- package/dist/openscreen/project.js +3 -3
- package/dist/project.d.ts +51 -1
- package/dist/project.js +89 -1
- package/dist/record.d.ts +6 -1
- package/dist/record.js +2 -1
- package/dist/renderer/assets/CanvasPool-PBu3bHFZ.js +1 -0
- package/dist/renderer/assets/CanvasRenderer-HmF-4gyW.js +1 -0
- package/dist/renderer/assets/RenderTargetSystem-Dk1U4wDJ.js +184 -0
- package/dist/renderer/assets/WebGLRenderer-C0v9HBel.js +149 -0
- package/dist/renderer/assets/WebGPURenderer-Ce0-U9Q1.js +95 -0
- package/dist/renderer/assets/{browserAll-aHn2isHo.js → browserAll-DhvWQ7nM.js} +2 -2
- package/dist/renderer/assets/canvasUtils-KRroregW.js +1 -0
- package/dist/renderer/assets/{getTexelRangeRects-yD6MQ0xy.js → getTexelRangeRects-D3pwCyRt.js} +3 -3
- package/dist/renderer/assets/index-CE-lli1m.js +556 -0
- package/dist/renderer/assets/init-DfmWKarH.js +83 -0
- package/dist/renderer/assets/webworkerAll-B41hok0-.js +1 -0
- package/dist/renderer/index.html +1 -6
- package/dist/scene.d.ts +24 -4
- package/dist/scene.js +74 -5
- package/dist/sheet.d.ts +6 -0
- package/dist/sheet.js +10 -0
- package/dist/types.d.ts +4 -0
- package/package.json +7 -1
- package/skills/demoreel/SKILL.md +161 -0
- package/dist/renderer/assets/CanvasPool-CHSHS1-v.js +0 -23
- package/dist/renderer/assets/RenderTargetSystem-EAUH9iQk.js +0 -404
- package/dist/renderer/assets/canvasUtils-n5g2f3_X.js +0 -1
- package/dist/renderer/assets/getTextureBatchBindGroup-Bwvgbco6.js +0 -1
- package/dist/renderer/assets/index-BwP6rH7I.js +0 -556
- package/dist/renderer/assets/init-C2Za5NQh.js +0 -83
- package/dist/renderer/assets/webworkerAll-JSSmbaR2.js +0 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import{A as e,C as t,Ct as n,Dt as r,Et as i,F as a,H as o,K as s,L as c,N as l,O as u,P as d,S as f,Tt as p,V as m,_ as ee,b as te,bt as ne,c as h,ct as g,d as re,dt as _,gt as v,h as ie,p as y,pt as b,q as x,v as S,w as C,xt as w,z as T}from"./index-CE-lli1m.js";import{a as ae,c as oe,d as se,l as ce,n as le,o as E,p as D,r as ue,s as de,t as fe}from"./getTexelRangeRects-D3pwCyRt.js";import{n as pe,r as me,t as he}from"./RenderTargetSystem-Dk1U4wDJ.js";function ge(){let{userAgent:e}=x.get().getNavigator();return/^((?!chrome|android).)*safari/i.test(e)}var O=class{constructor(){this._tempState=d.for2d(),this._didUploadHash={}}init(e){e.renderer.runners.contextChange.add(this)}contextChange(){this._didUploadHash={}}start(e,t,n){let r=e.renderer,i=this._didUploadHash[n.uid];r.shader.bind(n,i),i||(this._didUploadHash[n.uid]=!0),r.shader.updateUniformGroup(r.globalUniforms.uniformGroup),r.geometry.bind(t,n.glProgram)}execute(e,t){let n=e.renderer;this._tempState.blendMode=t.blendMode,n.state.set(this._tempState);let r=t.textures.textures;for(let e=0;e<t.textures.count;e++)n.texture.bind(r[e],e);n.texture.unbindIntegerTextures(t.textures.count),n.geometry.draw(t.topology,t.size,t.start)}};O.extension={type:[i.WebGLPipesAdaptor],name:`batch`};var k=(e=>(e[e.ELEMENT_ARRAY_BUFFER=34963]=`ELEMENT_ARRAY_BUFFER`,e[e.ARRAY_BUFFER=34962]=`ARRAY_BUFFER`,e[e.UNIFORM_BUFFER=35345]=`UNIFORM_BUFFER`,e))(k||{}),_e=class{constructor(e,t){this.buffer=e||null,this.updateID=-1,this.byteLength=-1,this.type=t}destroy(){this.buffer=null,this.updateID=-1,this.byteLength=-1,this.type=-1}},A=class{constructor(e){this._boundBuffers=[],this._boundOffsets=[],this._boundSizes=[],this._renderer=e,this._managedBuffers=new h({renderer:e,type:`resource`,onUnload:this.onBufferUnload.bind(this),name:`glBuffer`})}destroy(){this._managedBuffers.destroy(),this._renderer=null,this._gl=null,this._boundBuffers.length=0}contextChange(){this._gl=this._renderer.gl,this.destroyAll(!0),this._boundBuffers.length=0}getGlBuffer(e){return e._gcLastUsed=this._renderer.gc.now,e._gpuData[this._renderer.uid]||this.createGLBuffer(e)}bind(e){let{_gl:t}=this,n=this.getGlBuffer(e);t.bindBuffer(n.type,n.buffer)}bindBufferBase(e,t){(this._boundBuffers[t]!==e||this._boundSizes[t]!==-1)&&(this._boundBuffers[t]=e,this._boundOffsets[t]=0,this._boundSizes[t]=-1,this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,t,e.buffer))}bindBufferRange(e,t=0,n=0,r){r??=e.byteLength-n,(this._boundBuffers[t]!==e||this._boundOffsets[t]!==n||this._boundSizes[t]!==r)&&(this._boundBuffers[t]=e,this._boundOffsets[t]=n,this._boundSizes[t]=r,this._gl.bindBufferRange(this._gl.UNIFORM_BUFFER,t,e.buffer,n,r))}updateBuffer(e){let t=this.getGlBuffer(e);return this._upload(e,t,t.type),t}_upload(e,t,n){let{_gl:r}=this;if(e._updateID===t.updateID)return;t.updateID=e._updateID,r.bindBuffer(n,t.buffer);let i=e.data,a=e.descriptor.usage&f.STATIC?r.STATIC_DRAW:r.DYNAMIC_DRAW;if(i){if(t.byteLength>=i.byteLength){let t=e._updateOffset/i.BYTES_PER_ELEMENT,a=(e._updateSize||i.byteLength)/i.BYTES_PER_ELEMENT;this._renderer.context.webGLVersion===2?r.bufferSubData(n,e._updateOffset,i,t,a):r.bufferSubData(n,e._updateOffset,a===i.length?i:i.subarray(t,t+a))}else t.byteLength=i.byteLength,r.bufferData(n,i,a)}else t.byteLength=e.descriptor.size,r.bufferData(n,t.byteLength,a)}destroyAll(e=!1){this._managedBuffers.removeAll(e)}onBufferUnload(e,t=!1){e.off(`update`,this.updateBuffer,this),e.off(`update`,this._updateIndexBuffer,this);let n=e._gpuData[this._renderer.uid];n&&(t||this._gl.deleteBuffer(n.buffer))}createGLBuffer(e){let{_gl:t}=this,n=k.ARRAY_BUFFER;e.descriptor.usage&f.INDEX?n=k.ELEMENT_ARRAY_BUFFER:e.descriptor.usage&f.UNIFORM&&(n=k.UNIFORM_BUFFER);let r=new _e(t.createBuffer(),n);return e._gpuData[this._renderer.uid]=r,this._managedBuffers.add(e)&&(n===k.ELEMENT_ARRAY_BUFFER?e.on(`update`,this._updateIndexBuffer,this):e.on(`update`,this.updateBuffer,this)),r}_updateIndexBuffer(e){let t=this.getGlBuffer(e);t.byteLength<0||(this._renderer.context.webGLVersion===2?this._upload(e,t,this._gl.COPY_WRITE_BUFFER):(this._renderer.geometry._activeVao&&this._renderer.geometry.resetState(),this._upload(e,t,t.type)))}resetState(){this._boundBuffers.length=0}};A.extension={type:[i.WebGLSystem],name:`buffer`};var j=class e{constructor(e){this.supports={uint32Indices:!0,uniformBufferObject:!0,vertexArrayObject:!0,srgbTextures:!0,nonPowOf2wrapping:!0,msaa:!0,nonPowOf2mipmaps:!0},this._renderer=e,this.extensions=Object.create(null),this.handleContextLost=this.handleContextLost.bind(this),this.handleContextRestored=this.handleContextRestored.bind(this)}get isLost(){return!this.gl||this.gl.isContextLost()}contextChange(e){this.gl=e,this._renderer.gl=e}init(t){t={...e.defaultOptions,...t};let n=this.multiView=t.multiView;if(t.context&&n&&(g(`Renderer created with both a context and multiview enabled. Disabling multiView as both cannot work together.`),n=!1),this.canvas=n?x.get().createCanvas(this._renderer.canvas.width,this._renderer.canvas.height):this._renderer.view.canvas,t.context)this.initFromContext(t.context);else{let e=this._renderer.background.alpha<1,n=t.premultipliedAlpha??!0,r=t.antialias&&!this._renderer.backBuffer.useBackBuffer;this.createContext(t.preferWebGLVersion,{alpha:e,premultipliedAlpha:n,antialias:r,stencil:!0,preserveDrawingBuffer:t.preserveDrawingBuffer,powerPreference:t.powerPreference??`default`})}}ensureCanvasSize(e){if(!this.multiView){e!==this.canvas&&g(`multiView is disabled, but targetCanvas is not the main canvas`);return}let{canvas:t}=this;(t.width<e.width||t.height<e.height)&&(t.width=Math.max(e.width,e.width),t.height=Math.max(e.height,e.height))}initFromContext(e){this.gl=e,this.webGLVersion=e instanceof x.get().getWebGLRenderingContext()?1:2,this.getExtensions(),this.validateContext(e),this._renderer.runners.contextChange.emit(e);let t=this._renderer.view.canvas;t.addEventListener(`webglcontextlost`,this.handleContextLost,!1),t.addEventListener(`webglcontextrestored`,this.handleContextRestored,!1)}createContext(e,t){let n,r=this.canvas;if(e===2&&(n=r.getContext(`webgl2`,t)),!n&&(n=r.getContext(`webgl`,t),!n))throw Error(`This browser does not support WebGL. Try using the canvas renderer`);this.gl=n,this.initFromContext(this.gl)}getExtensions(){let{gl:e}=this,t={anisotropicFiltering:e.getExtension(`EXT_texture_filter_anisotropic`),floatTextureLinear:e.getExtension(`OES_texture_float_linear`),s3tc:e.getExtension(`WEBGL_compressed_texture_s3tc`),s3tc_sRGB:e.getExtension(`WEBGL_compressed_texture_s3tc_srgb`),etc:e.getExtension(`WEBGL_compressed_texture_etc`),etc1:e.getExtension(`WEBGL_compressed_texture_etc1`),pvrtc:e.getExtension(`WEBGL_compressed_texture_pvrtc`)||e.getExtension(`WEBKIT_WEBGL_compressed_texture_pvrtc`),atc:e.getExtension(`WEBGL_compressed_texture_atc`),astc:e.getExtension(`WEBGL_compressed_texture_astc`),bptc:e.getExtension(`EXT_texture_compression_bptc`),rgtc:e.getExtension(`EXT_texture_compression_rgtc`),loseContext:e.getExtension(`WEBGL_lose_context`)};if(this.webGLVersion===1)this.extensions={...t,drawBuffers:e.getExtension(`WEBGL_draw_buffers`),depthTexture:e.getExtension(`WEBGL_depth_texture`),vertexArrayObject:e.getExtension(`OES_vertex_array_object`)||e.getExtension(`MOZ_OES_vertex_array_object`)||e.getExtension(`WEBKIT_OES_vertex_array_object`),uint32ElementIndex:e.getExtension(`OES_element_index_uint`),floatTexture:e.getExtension(`OES_texture_float`),floatTextureLinear:e.getExtension(`OES_texture_float_linear`),textureHalfFloat:e.getExtension(`OES_texture_half_float`),textureHalfFloatLinear:e.getExtension(`OES_texture_half_float_linear`),vertexAttribDivisorANGLE:e.getExtension(`ANGLE_instanced_arrays`),srgb:e.getExtension(`EXT_sRGB`)};else{this.extensions={...t,colorBufferFloat:e.getExtension(`EXT_color_buffer_float`)};let n=e.getExtension(`WEBGL_provoking_vertex`);n&&n.provokingVertexWEBGL(n.FIRST_VERTEX_CONVENTION_WEBGL)}}handleContextLost(e){e.preventDefault(),this._contextLossForced&&(this._contextLossForced=!1,setTimeout(()=>{this.gl.isContextLost()&&this.extensions.loseContext?.restoreContext()},0))}handleContextRestored(){this.getExtensions(),this._renderer.runners.contextChange.emit(this.gl)}destroy(){let e=this._renderer.view.canvas;this._renderer=null,e.removeEventListener(`webglcontextlost`,this.handleContextLost),e.removeEventListener(`webglcontextrestored`,this.handleContextRestored),this.gl.useProgram(null),this.extensions.loseContext?.loseContext()}forceContextLoss(){this.extensions.loseContext?.loseContext(),this._contextLossForced=!0}validateContext(e){let t=e.getContextAttributes();t&&!t.stencil&&g(`Provided WebGL context does not have a stencil buffer, masks may not render correctly`);let n=this.supports,r=this.webGLVersion===2,i=this.extensions;n.uint32Indices=r||!!i.uint32ElementIndex,n.uniformBufferObject=r,n.vertexArrayObject=r||!!i.vertexArrayObject,n.srgbTextures=r||!!i.srgb,n.nonPowOf2wrapping=r,n.nonPowOf2mipmaps=r,n.msaa=r,n.uint32Indices||g(`Provided WebGL context does not support 32 index buffer, large scenes may not render correctly`)}};j.extension={type:[i.WebGLSystem],name:`context`},j.defaultOptions={context:null,premultipliedAlpha:!0,preserveDrawingBuffer:!1,powerPreference:void 0,preferWebGLVersion:2,multiView:!1};var ve=j,M=(e=>(e[e.RGBA=6408]=`RGBA`,e[e.RGB=6407]=`RGB`,e[e.RG=33319]=`RG`,e[e.RED=6403]=`RED`,e[e.RGBA_INTEGER=36249]=`RGBA_INTEGER`,e[e.RGB_INTEGER=36248]=`RGB_INTEGER`,e[e.RG_INTEGER=33320]=`RG_INTEGER`,e[e.RED_INTEGER=36244]=`RED_INTEGER`,e[e.ALPHA=6406]=`ALPHA`,e[e.LUMINANCE=6409]=`LUMINANCE`,e[e.LUMINANCE_ALPHA=6410]=`LUMINANCE_ALPHA`,e[e.DEPTH_COMPONENT=6402]=`DEPTH_COMPONENT`,e[e.DEPTH_STENCIL=34041]=`DEPTH_STENCIL`,e))(M||{}),N=(e=>(e[e.TEXTURE_2D=3553]=`TEXTURE_2D`,e[e.TEXTURE_CUBE_MAP=34067]=`TEXTURE_CUBE_MAP`,e[e.TEXTURE_2D_ARRAY=35866]=`TEXTURE_2D_ARRAY`,e[e.TEXTURE_3D=32879]=`TEXTURE_3D`,e[e.TEXTURE_CUBE_MAP_POSITIVE_X=34069]=`TEXTURE_CUBE_MAP_POSITIVE_X`,e[e.TEXTURE_CUBE_MAP_NEGATIVE_X=34070]=`TEXTURE_CUBE_MAP_NEGATIVE_X`,e[e.TEXTURE_CUBE_MAP_POSITIVE_Y=34071]=`TEXTURE_CUBE_MAP_POSITIVE_Y`,e[e.TEXTURE_CUBE_MAP_NEGATIVE_Y=34072]=`TEXTURE_CUBE_MAP_NEGATIVE_Y`,e[e.TEXTURE_CUBE_MAP_POSITIVE_Z=34073]=`TEXTURE_CUBE_MAP_POSITIVE_Z`,e[e.TEXTURE_CUBE_MAP_NEGATIVE_Z=34074]=`TEXTURE_CUBE_MAP_NEGATIVE_Z`,e))(N||{}),P=(e=>(e[e.UNSIGNED_BYTE=5121]=`UNSIGNED_BYTE`,e[e.UNSIGNED_SHORT=5123]=`UNSIGNED_SHORT`,e[e.UNSIGNED_SHORT_5_6_5=33635]=`UNSIGNED_SHORT_5_6_5`,e[e.UNSIGNED_SHORT_4_4_4_4=32819]=`UNSIGNED_SHORT_4_4_4_4`,e[e.UNSIGNED_SHORT_5_5_5_1=32820]=`UNSIGNED_SHORT_5_5_5_1`,e[e.UNSIGNED_INT=5125]=`UNSIGNED_INT`,e[e.UNSIGNED_INT_10F_11F_11F_REV=35899]=`UNSIGNED_INT_10F_11F_11F_REV`,e[e.UNSIGNED_INT_2_10_10_10_REV=33640]=`UNSIGNED_INT_2_10_10_10_REV`,e[e.UNSIGNED_INT_24_8=34042]=`UNSIGNED_INT_24_8`,e[e.UNSIGNED_INT_5_9_9_9_REV=35902]=`UNSIGNED_INT_5_9_9_9_REV`,e[e.BYTE=5120]=`BYTE`,e[e.SHORT=5122]=`SHORT`,e[e.INT=5124]=`INT`,e[e.FLOAT=5126]=`FLOAT`,e[e.FLOAT_32_UNSIGNED_INT_24_8_REV=36269]=`FLOAT_32_UNSIGNED_INT_24_8_REV`,e[e.HALF_FLOAT=36193]=`HALF_FLOAT`,e))(P||{}),F={uint8x2:P.UNSIGNED_BYTE,uint8x4:P.UNSIGNED_BYTE,sint8x2:P.BYTE,sint8x4:P.BYTE,unorm8x2:P.UNSIGNED_BYTE,unorm8x4:P.UNSIGNED_BYTE,snorm8x2:P.BYTE,snorm8x4:P.BYTE,uint16x2:P.UNSIGNED_SHORT,uint16x4:P.UNSIGNED_SHORT,sint16x2:P.SHORT,sint16x4:P.SHORT,unorm16x2:P.UNSIGNED_SHORT,unorm16x4:P.UNSIGNED_SHORT,snorm16x2:P.SHORT,snorm16x4:P.SHORT,float16x2:P.HALF_FLOAT,float16x4:P.HALF_FLOAT,float32:P.FLOAT,float32x2:P.FLOAT,float32x3:P.FLOAT,float32x4:P.FLOAT,uint32:P.UNSIGNED_INT,uint32x2:P.UNSIGNED_INT,uint32x3:P.UNSIGNED_INT,uint32x4:P.UNSIGNED_INT,sint32:P.INT,sint32x2:P.INT,sint32x3:P.INT,sint32x4:P.INT};function ye(e){return F[e]??F.float32}var be={"point-list":0,"line-list":1,"line-strip":3,"triangle-list":4,"triangle-strip":5},xe=class{constructor(){this.vaoCache=Object.create(null)}destroy(){this.vaoCache=Object.create(null)}},I=class{constructor(e){this._renderer=e,this._activeGeometry=null,this._activeVao=null,this.hasVao=!0,this.hasInstance=!0,this._managedGeometries=new h({renderer:e,type:`resource`,onUnload:this.onGeometryUnload.bind(this),name:`glGeometry`})}contextChange(){let e=this.gl=this._renderer.gl;if(!this._renderer.context.supports.vertexArrayObject)throw Error(`[PixiJS] Vertex Array Objects are not supported on this device`);this.destroyAll(!0);let t=this._renderer.context.extensions.vertexArrayObject;t&&(e.createVertexArray=()=>t.createVertexArrayOES(),e.bindVertexArray=e=>t.bindVertexArrayOES(e),e.deleteVertexArray=e=>t.deleteVertexArrayOES(e));let n=this._renderer.context.extensions.vertexAttribDivisorANGLE;n&&(e.drawArraysInstanced=(e,t,r,i)=>{n.drawArraysInstancedANGLE(e,t,r,i)},e.drawElementsInstanced=(e,t,r,i,a)=>{n.drawElementsInstancedANGLE(e,t,r,i,a)},e.vertexAttribDivisor=(e,t)=>n.vertexAttribDivisorANGLE(e,t)),this._activeGeometry=null,this._activeVao=null}bind(e,t){let n=this.gl;this._activeGeometry=e;let r=this.getVao(e,t);this._activeVao!==r&&(this._activeVao=r,n.bindVertexArray(r)),this.updateBuffers()}resetState(){this.unbind()}updateBuffers(){let e=this._activeGeometry,t=this._renderer.buffer;for(let n=0;n<e.buffers.length;n++){let r=e.buffers[n];t.updateBuffer(r)}e._gcLastUsed=this._renderer.gc.now}checkCompatibility(e,t){let n=e.attributes,r=t._attributeData;for(let e in r)if(!n[e])throw Error(`shader and geometry incompatible, geometry missing the "${e}" attribute`)}getSignature(e,t){let n=e.attributes,r=t._attributeData,i=[`g`,e.uid];for(let e in n)r[e]&&i.push(e,r[e].location);return i.join(`-`)}getVao(e,t){return e._gpuData[this._renderer.uid]?.vaoCache[t._key]||this.initGeometryVao(e,t)}initGeometryVao(e,t,n=!0){let r=this._renderer.gl,i=this._renderer.buffer;this._renderer.shader._getProgramData(t),this.checkCompatibility(e,t);let a=this.getSignature(e,t),o=e._gpuData[this._renderer.uid];o||(o=new xe,e._gpuData[this._renderer.uid]=o,this._managedGeometries.add(e));let s=o.vaoCache,c=s[a];if(c)return s[t._key]=c,c;ce(e,t._attributeData);let l=e.buffers;c=r.createVertexArray(),r.bindVertexArray(c);for(let e=0;e<l.length;e++){let t=l[e];i.bind(t)}return this.activateVao(e,t),s[t._key]=c,s[a]=c,r.bindVertexArray(null),c}onGeometryUnload(e,t=!1){let n=e._gpuData[this._renderer.uid];if(!n)return;let r=n.vaoCache;if(!t)for(let e in r)this._activeVao===r[e]&&this.resetState(),this.gl.deleteVertexArray(r[e])}destroyAll(e=!1){this._managedGeometries.removeAll(e)}activateVao(e,t){let n=this._renderer.gl,r=this._renderer.buffer,i=e.attributes;e.indexBuffer&&r.bind(e.indexBuffer);let a=null;for(let e in i){let o=i[e],s=o.buffer,c=r.getGlBuffer(s),l=t._attributeData[e];if(l){a!==c&&(r.bind(s),a=c);let e=l.location;n.enableVertexAttribArray(e);let t=m(o.format),i=ye(o.format);if(l.format?.substring(1,4)===`int`?n.vertexAttribIPointer(e,t.size,i,o.stride,o.offset):n.vertexAttribPointer(e,t.size,i,t.normalised,o.stride,o.offset),o.instance){if(this.hasInstance){let t=o.divisor??1;n.vertexAttribDivisor(e,t)}else throw Error(`geometry error, GPU Instancing is not supported on this device`)}}}}draw(e,t,n,r){let{gl:i}=this._renderer,a=this._activeGeometry,o=be[e||a.topology];if(r??=a.instanceCount,a.indexBuffer){let e=a.indexBuffer.data.BYTES_PER_ELEMENT,s=e===2?i.UNSIGNED_SHORT:i.UNSIGNED_INT,c=t||a.indexCount||a.indexBuffer.data.length,l=(n||0)*e;r===1?i.drawElements(o,c,s,l):i.drawElementsInstanced(o,c,s,l,r)}else r===1?i.drawArrays(o,n||0,t||a.vertexCount):i.drawArraysInstanced(o,n||0,t||a.vertexCount,r);return this}unbind(){this.gl.bindVertexArray(null),this._activeVao=null,this._activeGeometry=null}destroy(){this._managedGeometries.destroy(),this._renderer=null,this.gl=null,this._activeVao=null,this._activeGeometry=null}};I.extension={type:[i.WebGLSystem],name:`geometry`};var Se=new te({attributes:{aPosition:[-1,-1,3,-1,-1,3]}}),L=class e{constructor(e){this.useBackBuffer=!1,this._useBackBufferThisRender=!1,this._renderer=e}init(t={}){let{useBackBuffer:n,antialias:r}={...e.defaultOptions,...t};this.useBackBuffer=n,this._antialias=r,this._renderer.context.supports.msaa||(g(`antialiasing, is not supported on when using the back buffer`),this._antialias=!1),this._state=d.for2d();let i=new o({vertex:`
|
|
2
|
+
attribute vec2 aPosition;
|
|
3
|
+
out vec2 vUv;
|
|
4
|
+
|
|
5
|
+
void main() {
|
|
6
|
+
gl_Position = vec4(aPosition, 0.0, 1.0);
|
|
7
|
+
|
|
8
|
+
vUv = (aPosition + 1.0) / 2.0;
|
|
9
|
+
|
|
10
|
+
// flip dem UVs
|
|
11
|
+
vUv.y = 1.0 - vUv.y;
|
|
12
|
+
}`,fragment:`
|
|
13
|
+
in vec2 vUv;
|
|
14
|
+
out vec4 finalColor;
|
|
15
|
+
|
|
16
|
+
uniform sampler2D uTexture;
|
|
17
|
+
|
|
18
|
+
void main() {
|
|
19
|
+
finalColor = texture(uTexture, vUv);
|
|
20
|
+
}`,name:`big-triangle`});this._bigTriangleShader=new a({glProgram:i,resources:{uTexture:_.WHITE.source}})}renderStart(e){let t=this._renderer.renderTarget.getRenderTarget(e.target);if(this._useBackBufferThisRender=this.useBackBuffer&&!!t.isRoot,this._useBackBufferThisRender){let t=this._renderer.renderTarget.getRenderTarget(e.target);this._targetTexture=t.colorTexture,e.target=this._getBackBufferTexture(t.colorTexture)}}renderEnd(){this._presentBackBuffer()}_presentBackBuffer(){let e=this._renderer;e.renderTarget.finishRenderPass(),this._useBackBufferThisRender&&(e.renderTarget.bind({target:this._targetTexture,clear:!1}),this._bigTriangleShader.resources.uTexture=this._backBufferTexture.source,e.encoder.draw({geometry:Se,shader:this._bigTriangleShader,state:this._state}))}_getBackBufferTexture(e){return this._backBufferTexture=this._backBufferTexture||new _({source:new b({width:e.width,height:e.height,resolution:e._resolution,antialias:this._antialias})}),this._backBufferTexture.source.resize(e.width,e.height,e._resolution),this._backBufferTexture}destroy(){this._backBufferTexture&&=(this._backBufferTexture.destroy(),null)}};L.extension={type:[i.WebGLSystem],name:`backBuffer`,priority:1},L.defaultOptions={useBackBuffer:!1};var Ce=L,R=class{constructor(e){this._colorMaskCache=15,this._renderer=e}setMask(e){this._colorMaskCache!==e&&(this._colorMaskCache=e,this._renderer.gl.colorMask(!!(e&8),!!(e&4),!!(e&2),!!(e&1)))}};R.extension={type:[i.WebGLSystem],name:`colorMask`};var z=class{constructor(e){this.commandFinished=Promise.resolve(),this._renderer=e}setGeometry(e,t){this._renderer.geometry.bind(e,t.glProgram)}finishRenderPass(){}draw(e){let t=this._renderer,{geometry:n,shader:r,state:i,skipSync:a,topology:o,size:s,start:c,instanceCount:l}=e;t.shader.bind(r,a),t.geometry.bind(n,t.shader._activeProgram),i&&t.state.set(i),t.geometry.draw(o,s,c,l??n.instanceCount)}destroy(){this._renderer=null}};z.extension={type:[i.WebGLSystem],name:`encoder`};var B=class{constructor(e){this._renderer=e}contextChange(){let e=this._renderer.gl;this.maxTextures=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.maxBatchableTextures=t(this.maxTextures,e);let n=this._renderer.context.webGLVersion===2;this.maxUniformBindings=n?e.getParameter(e.MAX_UNIFORM_BUFFER_BINDINGS):0,this.minUniformBufferOffsetAlignment=n?e.getParameter(e.UNIFORM_BUFFER_OFFSET_ALIGNMENT):0}destroy(){}};B.extension={type:[i.WebGLSystem],name:`limits`};var we=class{constructor(){this.width=-1,this.height=-1,this.msaa=!1,this._attachedMipLevel=0,this._attachedLayer=0,this._drawBuffersMask=-1,this._attachedDepthStencilRenderBuffer=null,this.stencilMode=C.DISABLED,this.stencilReference=0,this.maskStackIndex=0}},V=class{constructor(e){this._stencilCache={enabled:!1,stencilReference:0,stencilMode:C.NONE},this._renderer=e,e.renderTarget.onRenderTargetChange.add(this)}contextChange(e){this._gl=e,this._comparisonFuncMapping={always:e.ALWAYS,never:e.NEVER,equal:e.EQUAL,"not-equal":e.NOTEQUAL,less:e.LESS,"less-equal":e.LEQUAL,greater:e.GREATER,"greater-equal":e.GEQUAL},this._stencilOpsMapping={keep:e.KEEP,zero:e.ZERO,replace:e.REPLACE,invert:e.INVERT,"increment-clamp":e.INCR,"decrement-clamp":e.DECR,"increment-wrap":e.INCR_WRAP,"decrement-wrap":e.DECR_WRAP},this._activeGpuRenderTarget=null,this.resetState()}onRenderTargetChange(e){if(this._activeRenderTarget===e)return;this._activeRenderTarget=e;let t=this._renderer.renderTarget.getGpuRenderTarget(e);this._activeGpuRenderTarget=t,this.setStencilMode(t.stencilMode,t.stencilReference)}resetState(){this._stencilCache.enabled=!1,this._stencilCache.stencilMode=C.NONE,this._stencilCache.stencilReference=0}setStencilMode(e,t){let n=this._activeGpuRenderTarget??=this._renderer.renderTarget.getGpuRenderTarget(this._activeRenderTarget),r=this._gl,i=oe[e],a=this._stencilCache;n.stencilMode=e,n.stencilReference=t,e===C.DISABLED?this._stencilCache.enabled&&(this._stencilCache.enabled=!1,r.disable(r.STENCIL_TEST)):(this._stencilCache.enabled||(this._stencilCache.enabled=!0,r.enable(r.STENCIL_TEST)),(e!==a.stencilMode||a.stencilReference!==t)&&(a.stencilMode=e,a.stencilReference=t,r.stencilFunc(this._comparisonFuncMapping[i.stencilBack.compare],t,255),r.stencilOp(r.KEEP,r.KEEP,this._stencilOpsMapping[i.stencilBack.passOp])))}destroy(){this._renderer.renderTarget.onRenderTargetChange.remove(this),this._renderer=null,this._gl=null,this._activeRenderTarget=null,this._activeGpuRenderTarget=null}};V.extension={type:[i.WebGLSystem],name:`stencil`};var H={f32:4,i32:4,"vec2<f32>":8,"vec3<f32>":12,"vec4<f32>":16,"vec2<i32>":8,"vec3<i32>":12,"vec4<i32>":16,u32:4,"vec2<u32>":8,"vec3<u32>":12,"vec4<u32>":16,"mat2x2<f32>":32,"mat3x3<f32>":48,"mat4x4<f32>":64};function Te(e){let t=e.map(e=>({data:e,offset:0,size:0})),n=0,r=0;for(let e=0;e<t.length;e++){let i=t[e];if(n=H[i.data.type],!n)throw Error(`Unknown type ${i.data.type}`);i.data.size>1&&(n=Math.max(n,16)*i.data.size);let a=n===12?16:n;i.size=n;let o=r%16;o>0&&16-o<a?r+=(16-o)%16:r+=(n-o%n)%n,i.offset=r,r+=n}return r=Math.ceil(r/16)*16,{uboElements:t,size:r}}function Ee(e,t){let n=Math.max(H[e.data.type]/16,1),r=e.data.value.length/e.data.size,i=(4-r%4)%4,a=e.data.type.indexOf(`i32`)>=0?`dataInt32`:`data`;return`
|
|
21
|
+
v = uv.${e.data.name};
|
|
22
|
+
offset += ${t};
|
|
23
|
+
|
|
24
|
+
arrayOffset = offset;
|
|
25
|
+
|
|
26
|
+
t = 0;
|
|
27
|
+
|
|
28
|
+
for(var i=0; i < ${e.data.size*n}; i++)
|
|
29
|
+
{
|
|
30
|
+
for(var j = 0; j < ${r}; j++)
|
|
31
|
+
{
|
|
32
|
+
${a}[arrayOffset++] = v[t++];
|
|
33
|
+
}
|
|
34
|
+
${i===0?``:`arrayOffset += ${i};`}
|
|
35
|
+
}
|
|
36
|
+
`}function De(e){return ae(e,ue,Ee)}var U=class extends de{constructor(){super({createUboElements:Te,generateUboSync:De})}};U.extension={type:[i.WebGLSystem],name:`ubo`};var Oe=class{constructor(){this._clearColorCache=[0,0,0,0],this._viewPortCache=new w,this._boundFramebuffer=void 0,this._canvasMultisampled=!1,this._clearUint=new Uint32Array(4),this._clearInt=new Int32Array(4)}init(e,t){this._renderer=e,this._renderTargetSystem=t,e.runners.contextChange.add(this)}contextChange(){this._clearColorCache=[0,0,0,0],this._viewPortCache=new w,this._boundFramebuffer=void 0;let e=this._renderer.gl;this._canvasMultisampled=!!e.getContextAttributes()?.antialias,this._drawBuffersCache=[]}copyToTexture(e,t,n,r,i){let a=this._renderTargetSystem,o=this._renderer,s=a.getGpuRenderTarget(e),c=o.gl;return this.finishRenderPass(e),c.bindFramebuffer(c.FRAMEBUFFER,s.resolveTargetFramebuffer),this._boundFramebuffer=s.resolveTargetFramebuffer,o.texture.bind(t,0),c.copyTexSubImage2D(c.TEXTURE_2D,0,i.x,i.y,n.x,n.y,r.width,r.height),t}copyDepthTexture(e,t,n,r,i){let a=this._renderTargetSystem,o=this._renderer.gl;this.finishRenderPass(e);let s=a.getRenderTarget(t),c=a.getGpuRenderTarget(e),l=a.getGpuRenderTarget(s),u=c.framebuffer;(c.msaa||e.isRoot&&this._canvasMultisampled)&&(n.x!==i.x||n.y!==i.y)&&(c.resolveTargetFramebuffer??=o.createFramebuffer(),o.bindFramebuffer(o.FRAMEBUFFER,c.resolveTargetFramebuffer),this._attachDepthStencilTexture(e,0,0),o.bindFramebuffer(o.READ_FRAMEBUFFER,c.framebuffer),o.blitFramebuffer(n.x,n.y,n.x+r.width,n.y+r.height,n.x,n.y,n.x+r.width,n.y+r.height,o.DEPTH_BUFFER_BIT,o.NEAREST),u=c.resolveTargetFramebuffer),o.bindFramebuffer(o.READ_FRAMEBUFFER,u),o.bindFramebuffer(o.DRAW_FRAMEBUFFER,l.framebuffer),this._boundFramebuffer=void 0,o.blitFramebuffer(n.x,n.y,n.x+r.width,n.y+r.height,i.x,i.y,i.x+r.width,i.y+r.height,o.DEPTH_BUFFER_BIT,o.NEAREST)}startRenderPass(e,t=!0,n,r,i=0,a=0){let o=this._renderTargetSystem.getGpuRenderTarget(e);if(a!==0&&this._renderer.context.webGLVersion<2)throw Error(`[RenderTargetSystem] Rendering to array layers requires WebGL2.`);if(i>0){if(o.msaa)throw Error(`[RenderTargetSystem] Rendering to mip levels is not supported with MSAA render targets.`);if(this._renderer.context.webGLVersion<2)throw Error(`[RenderTargetSystem] Rendering to mip levels requires WebGL2.`)}e.colorAttachments.forEach(e=>{this._renderer.texture.unbind(e.texture)});let s=this._renderer.gl;this.bindFramebuffer(o.framebuffer),!e.isRoot&&e.colorAttachments.length>0&&(o._attachedMipLevel!==i||o._attachedLayer!==a)&&(e.colorAttachments.forEach((e,t)=>{let n=e.texture,r=this._renderer.texture.getGlSource(n);if(r.target===s.TEXTURE_2D){if(a!==0)throw Error(`[RenderTargetSystem] layer must be 0 when rendering to 2D textures in WebGL.`);s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0+t,s.TEXTURE_2D,r.texture,i)}else if(r.target===s.TEXTURE_2D_ARRAY||r.target===s.TEXTURE_3D)s.framebufferTextureLayer(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0+t,r.texture,i,a);else if(r.target===s.TEXTURE_CUBE_MAP){if(a<0||a>5)throw Error(`[RenderTargetSystem] Cube map layer must be between 0 and 5.`);s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0+t,s.TEXTURE_CUBE_MAP_POSITIVE_X+a,r.texture,i)}else throw Error(`[RenderTargetSystem] Unsupported texture target for render-to-layer in WebGL.`)}),o._attachedMipLevel=i,o._attachedLayer=a),o.framebuffer&&(e.depthStencilAttachment?o.msaa?this._attachMsaaDepthStencil(e,o):this._attachDepthStencilTexture(e,i,a):!o.depthStencilRenderBuffer&&(e.stencil||e.depth)&&this._initStencil(o));let c=e.colorAttachments.length;c>1&&o._drawBuffersMask===-1&&this._setDrawBuffers(e,(1<<c)-1);let l=r.y;e.isRoot&&(l=e.pixelHeight-r.height-r.y);let u=this._viewPortCache;(u.x!==r.x||u.y!==l||u.width!==r.width||u.height!==r.height)&&(u.x=r.x,u.y=l,u.width=r.width,u.height=r.height,s.viewport(r.x,l,r.width,r.height)),this.clear(e,t,n);let d=this._renderer.shader._activeProgram;c>1&&d&&this.setFragmentOutputs(d)}setFragmentOutputs(e){let t=this._renderTargetSystem.renderTarget,n=t?.colorAttachments.length??0;if(n<2||this._renderer.context.webGLVersion===1)return;let r=(1<<n)-1;this._setDrawBuffers(t,(e.fragmentOutputMask??r)&r)}_setDrawBuffers(e,t){if(e.colorAttachments.length<2)return;let n=this._renderTargetSystem.getGpuRenderTarget(e);if(n.framebuffer!==this._boundFramebuffer||n._drawBuffersMask===t)return;n._drawBuffersMask=t;let r=this._getDrawBuffers(t);if(this._renderer.context.webGLVersion===1){let e=this._renderer.context.extensions.drawBuffers;e?e.drawBuffersWEBGL(r):g(`[RenderTexture] This WebGL1 context does not support rendering to multiple targets`)}else this._renderer.gl.drawBuffers(r)}_getDrawBuffers(e){let t=this._drawBuffersCache[e];if(!t){let n=this._renderer.gl;t=this._drawBuffersCache[e]=[];for(let r=0;e>>r>0;r++)t[r]=e&1<<r?n.COLOR_ATTACHMENT0+r:n.NONE;t.length===0&&(t[0]=n.NONE)}return t}finishRenderPass(e){let t=this._renderTargetSystem.getGpuRenderTarget(e);if(!t.msaa||e.colorAttachments.length===0)return;let n=this._renderer.gl;n.bindFramebuffer(n.FRAMEBUFFER,t.resolveTargetFramebuffer),n.bindFramebuffer(n.READ_FRAMEBUFFER,t.framebuffer);let r=e.colorAttachments.length;if(r===1)n.blitFramebuffer(0,0,t.width,t.height,0,0,t.width,t.height,n.COLOR_BUFFER_BIT,n.NEAREST);else{for(let e=0;e<r;e++)n.readBuffer(n.COLOR_ATTACHMENT0+e),n.drawBuffers(this._getDrawBuffers(1<<e)),n.blitFramebuffer(0,0,t.width,t.height,0,0,t.width,t.height,n.COLOR_BUFFER_BIT,n.NEAREST);n.readBuffer(n.COLOR_ATTACHMENT0),n.drawBuffers(this._getDrawBuffers(1))}n.bindFramebuffer(n.FRAMEBUFFER,t.framebuffer),this._boundFramebuffer=t.framebuffer}initGpuRenderTarget(e){let t=this._renderer.gl,n=new we;n._attachedMipLevel=0,n._attachedLayer=0;let r=e.colorTexture;return r instanceof s?(this._renderer.context.ensureCanvasSize(r.resource),n.framebuffer=null,n):(n.width=e.pixelWidth,n.height=e.pixelHeight,e.colorAttachments.length===0?this._initDepth(e,n):this._initColor(e,n),e.depthStencilAttachment&&!n.msaa&&this._attachDepthStencilTexture(e,0,0),t.bindFramebuffer(t.FRAMEBUFFER,null),this._boundFramebuffer=null,n)}destroyGpuRenderTarget(e){let t=this._renderer.gl;e.framebuffer&&=(t.deleteFramebuffer(e.framebuffer),null),e.resolveTargetFramebuffer&&=(t.deleteFramebuffer(e.resolveTargetFramebuffer),null),e.depthStencilRenderBuffer&&=(t.deleteRenderbuffer(e.depthStencilRenderBuffer),null)}clear(t,n,r,i,a=0,o=0){if(o!==0)throw Error(`[RenderTargetSystem] Clearing array layers is not supported in WebGL renderer.`);typeof n==`boolean`&&(n=n?e.ALL:e.NONE);let s=t.colorAttachments,c=0;if(n&e.COLOR)for(let e=0;e<s.length;e++)s[e].loadOp!==`load`&&(c|=1<<e);if(c||(n&=~e.COLOR),!n)return;let l=this._renderer.gl,u=!!(n&e.DEPTH)&&!this._renderer.state.depthMaskEnabled;if(u&&l.depthMask(!0),n&e.COLOR){let i=this._renderTargetSystem.getGpuRenderTarget(t);if(this._renderer.context.webGLVersion===2&&(s.length>1||v(s[0].texture.format))&&i.framebuffer===this._boundFramebuffer){let a=i._drawBuffersMask;this._setDrawBuffers(t,(1<<s.length)-1),this._clearEachAttachment(t,c,r),a!==-1&&this._setDrawBuffers(t,a),n&=~e.COLOR}else{let e=s[0].clearValue??r??this._renderTargetSystem.defaultClearColor,t=this._clearColorCache;(t[0]!==e[0]||t[1]!==e[1]||t[2]!==e[2]||t[3]!==e[3])&&(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],l.clearColor(e[0],e[1],e[2],e[3]))}}n&&l.clear(n),u&&l.depthMask(!1)}_clearEachAttachment(e,t,n){let r=this._renderer.gl,i=e.colorAttachments,a=n??this._renderTargetSystem.defaultClearColor;for(let e=0;e<i.length;e++){if(!(t&1<<e))continue;let n=i[e].texture.format,o=i[e].clearValue??a;n.endsWith(`uint`)?(this._clearUint.set(o),r.clearBufferuiv(r.COLOR,e,this._clearUint)):n.endsWith(`sint`)?(this._clearInt.set(o),r.clearBufferiv(r.COLOR,e,this._clearInt)):r.clearBufferfv(r.COLOR,e,o)}}resizeGpuRenderTarget(e){if(e.isRoot)return;let t=this._renderTargetSystem.getGpuRenderTarget(e);t.width=e.pixelWidth,t.height=e.pixelHeight,e.colorAttachments.length>0&&this._resizeColor(e,t),t.depthStencilRenderBuffer&&this._resizeStencil(t),this._boundFramebuffer=void 0}_initColor(e,t){let n=this._renderer,r=n.gl,i=r.createFramebuffer();t.resolveTargetFramebuffer=i,r.bindFramebuffer(r.FRAMEBUFFER,i),e.colorAttachments.forEach((e,i)=>{let a=e.texture;a.antialias&&(n.context.supports.msaa?t.msaa=!0:g(`[RenderTexture] Antialiasing on textures is not supported in WebGL1`)),n.texture.bindSource(a,0);let o=n.texture.getGlSource(a),s=o.texture;if(o.target===r.TEXTURE_2D)r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0+i,r.TEXTURE_2D,s,0);else if(o.target===r.TEXTURE_2D_ARRAY||o.target===r.TEXTURE_3D)r.framebufferTextureLayer(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0+i,s,0,0);else if(o.target===r.TEXTURE_CUBE_MAP)r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0+i,r.TEXTURE_CUBE_MAP_POSITIVE_X,s,0);else throw Error(`[RenderTargetSystem] Unsupported texture target for framebuffer attachment.`)}),t.framebuffer=t.msaa?r.createFramebuffer():i,this._resizeColor(e,t)}_initDepth(e,t){let n=this._renderer;if(n.context.webGLVersion<2)throw Error(`[RenderTargetSystem] Depth-only render targets require WebGL2.`);let r=n.gl,i=r.createFramebuffer();t.resolveTargetFramebuffer=i,t.framebuffer=i,r.bindFramebuffer(r.FRAMEBUFFER,i),r.drawBuffers([r.NONE]),r.readBuffer(r.NONE)}_resizeColor(e,t){let n=e.colorAttachments[0].texture;if(t._attachedMipLevel=0,t._attachedLayer=0,e.colorAttachments.forEach((e,t)=>{t!==0&&e.texture.resize(n.width,n.height,n._resolution)}),t.msaa){let n=this._renderer,r=n.gl;r.bindFramebuffer(r.FRAMEBUFFER,t.framebuffer),e.colorAttachments.forEach((e,t)=>{r.framebufferRenderbuffer(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0+t,r.RENDERBUFFER,n.texture.getMsaaRenderBuffer(e.texture))})}}_attachMsaaDepthStencil(e,t){let n=this._renderer.gl,r=this._renderer.texture,i=e.depthStencilAttachment.texture,a=t.depthStencilRenderBuffer;if(a){let e=r.getGlSource(i);!e.msaaRenderBuffer&&e.internalFormat===n.DEPTH24_STENCIL8?(e.msaaRenderBuffer=a,e.msaaWidth=t.width,e.msaaHeight=t.height,t._attachedDepthStencilRenderBuffer=a):n.deleteRenderbuffer(a),t.depthStencilRenderBuffer=null}let o=r.getMsaaRenderBuffer(i);t._attachedDepthStencilRenderBuffer!==o&&(t._attachedDepthStencilRenderBuffer=o,n.framebufferRenderbuffer(n.FRAMEBUFFER,this._getDepthStencilAttachmentPoint(e),n.RENDERBUFFER,o))}_attachDepthStencilTexture(e,t,n){let r=this._renderer,i=r.gl,a=e.depthStencilAttachment.texture,o=r.texture.getGlSource(a),s=o.texture,c=this._getDepthStencilAttachmentPoint(e);o.target===i.TEXTURE_2D?i.framebufferTexture2D(i.FRAMEBUFFER,c,i.TEXTURE_2D,s,t):o.target===i.TEXTURE_2D_ARRAY||o.target===i.TEXTURE_3D?i.framebufferTextureLayer(i.FRAMEBUFFER,c,s,t,n):o.target===i.TEXTURE_CUBE_MAP&&i.framebufferTexture2D(i.FRAMEBUFFER,c,i.TEXTURE_CUBE_MAP_POSITIVE_X+n,s,t)}_getDepthStencilAttachmentPoint(e){let t=this._renderer.gl,n=e.depthStencilAttachment.texture.format;return n===`depth24plus-stencil8`||n===`depth32float-stencil8`?t.DEPTH_STENCIL_ATTACHMENT:n===`stencil8`?t.STENCIL_ATTACHMENT:t.DEPTH_ATTACHMENT}_initStencil(e){if(e.framebuffer===null)return;let t=this._renderer.gl,n=t.createRenderbuffer();e.depthStencilRenderBuffer=n,t.bindRenderbuffer(t.RENDERBUFFER,n),t.framebufferRenderbuffer(t.FRAMEBUFFER,t.DEPTH_STENCIL_ATTACHMENT,t.RENDERBUFFER,n),this._resizeStencil(e)}_resizeStencil(e){let t=this._renderer.gl;t.bindRenderbuffer(t.RENDERBUFFER,e.depthStencilRenderBuffer),e.msaa?t.renderbufferStorageMultisample(t.RENDERBUFFER,4,t.DEPTH24_STENCIL8,e.width,e.height):t.renderbufferStorage(t.RENDERBUFFER,this._renderer.context.webGLVersion===2?t.DEPTH24_STENCIL8:t.DEPTH_STENCIL,e.width,e.height)}prerender(e){if(e.colorAttachments.length===0)return;let t=e.colorAttachments[0].texture.resource;this._renderer.context.multiView&&s.test(t)&&this._renderer.context.ensureCanvasSize(t)}postrender(e){if(!this._renderer.context.multiView||e.colorAttachments.length===0)return;let t=e.colorAttachments[0].texture;if(s.test(t.resource)){let e=this._renderer.context.canvas,n=t;n.context2D.drawImage(e,0,n.pixelHeight-e.height)}}resetState(){this._boundFramebuffer=void 0,this._viewPortCache=new w,this._clearColorCache=[0,0,0,0]}bindFramebuffer(e){this._boundFramebuffer!==e&&(this._boundFramebuffer=e,this._renderer.gl.bindFramebuffer(this._renderer.gl.FRAMEBUFFER,e))}},ke=class extends he{constructor(e){super(e),this.adaptor=new Oe,this.adaptor.init(e,this)}resetState(){this.adaptor.resetState()}};ke.extension={type:[i.WebGLSystem],name:`renderTarget`};var Ae=class extends p{constructor(e,t){super(),this._resourceType=`textureView`,this._resourceId=ne(`resource`),this.source=e,this.viewDescriptor=t,this._onChange=this._onChange.bind(this),this._onDestroy=this._onDestroy.bind(this),this.source.on(`change`,this._onChange),this.source.on(`destroy`,this._onDestroy)}_onChange(){this.emit(`change`,this)}_onDestroy(){this.destroy()}get destroyed(){return this.source.destroyed}destroy(){this.source&&(this.source.off(`change`,this._onChange),this.source.off(`destroy`,this._onDestroy)),this.emit(`destroy`,this),this.removeAllListeners()}};function je(e){let t={};for(let n in e)t[e[n]]=n;return t}function Me(e,t){let n=[],r=[`
|
|
37
|
+
var g = s.groups;
|
|
38
|
+
var sS = r.shader;
|
|
39
|
+
var p = s.glProgram;
|
|
40
|
+
var ugS = r.uniformGroup;
|
|
41
|
+
var resources;
|
|
42
|
+
`],i=!1,a=0,o=t._getProgramData(e.glProgram);for(let s in e.groups){let c=e.groups[s],l=c.layout?je(c.layout):e._uniformBindMap[s];n.push(`
|
|
43
|
+
resources = g[${s}].resources;
|
|
44
|
+
`);for(let s in c.resources){let u=c.resources[s];if(u instanceof T&&!u.ubo)n.push(`
|
|
45
|
+
ugS.updateUniformGroup(resources[${s}], p, sD);
|
|
46
|
+
`);else if(u instanceof T||u instanceof le){let t=l[Number(s)];e.glProgram._uniformBlockData[t]&&n.push(`
|
|
47
|
+
sS.bindUniformBlock(
|
|
48
|
+
resources[${s}],
|
|
49
|
+
'${t}'
|
|
50
|
+
);
|
|
51
|
+
`)}else if(u instanceof b||u instanceof Ae){let e=l[Number(s)],c=o.uniformData[e];c&&(i||(i=!0,r.push(`
|
|
52
|
+
var tS = r.texture;
|
|
53
|
+
`)),t._gl.uniform1i(c.location,a),n.push(`
|
|
54
|
+
tS.bind(resources[${s}], ${a});
|
|
55
|
+
`),a++)}}}let s=[...r,...n].join(`
|
|
56
|
+
`);return Function(`r`,`s`,`sD`,s)}var Ne=class{constructor(e,t){this.program=e,this.uniformData=t,this.uniformGroups={},this.uniformDirtyGroups={}}destroy(){this.uniformData=null,this.uniformGroups=null,this.uniformDirtyGroups=null,this.program=null}};function W(e,t,n){let r=e.createShader(t);return e.shaderSource(r,n),e.compileShader(r),r}function G(e){let t=Array(e);for(let e=0;e<t.length;e++)t[e]=!1;return t}function K(e,t){switch(e){case`float`:return 0;case`vec2`:return new Float32Array(2*t);case`vec3`:return new Float32Array(3*t);case`vec4`:return new Float32Array(4*t);case`int`:case`uint`:case`sampler2D`:case`sampler2DArray`:case`sampler3D`:return 0;case`ivec2`:return new Int32Array(2*t);case`ivec3`:return new Int32Array(3*t);case`ivec4`:return new Int32Array(4*t);case`uvec2`:return new Uint32Array(2*t);case`uvec3`:return new Uint32Array(3*t);case`uvec4`:return new Uint32Array(4*t);case`bool`:return!1;case`bvec2`:return G(2*t);case`bvec3`:return G(3*t);case`bvec4`:return G(4*t);case`mat2`:return new Float32Array([1,0,0,1]);case`mat3`:return new Float32Array([1,0,0,0,1,0,0,0,1]);case`mat4`:return new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])}return null}var q=null,J={FLOAT:`float`,FLOAT_VEC2:`vec2`,FLOAT_VEC3:`vec3`,FLOAT_VEC4:`vec4`,INT:`int`,INT_VEC2:`ivec2`,INT_VEC3:`ivec3`,INT_VEC4:`ivec4`,UNSIGNED_INT:`uint`,UNSIGNED_INT_VEC2:`uvec2`,UNSIGNED_INT_VEC3:`uvec3`,UNSIGNED_INT_VEC4:`uvec4`,BOOL:`bool`,BOOL_VEC2:`bvec2`,BOOL_VEC3:`bvec3`,BOOL_VEC4:`bvec4`,FLOAT_MAT2:`mat2`,FLOAT_MAT3:`mat3`,FLOAT_MAT4:`mat4`,SAMPLER_2D:`sampler2D`,INT_SAMPLER_2D:`sampler2D`,UNSIGNED_INT_SAMPLER_2D:`sampler2D`,SAMPLER_2D_SHADOW:`sampler2DShadow`,SAMPLER_CUBE:`samplerCube`,INT_SAMPLER_CUBE:`samplerCube`,UNSIGNED_INT_SAMPLER_CUBE:`samplerCube`,SAMPLER_CUBE_SHADOW:`samplerCubeShadow`,SAMPLER_2D_ARRAY:`sampler2DArray`,INT_SAMPLER_2D_ARRAY:`sampler2DArray`,UNSIGNED_INT_SAMPLER_2D_ARRAY:`sampler2DArray`,SAMPLER_2D_ARRAY_SHADOW:`sampler2DArrayShadow`,SAMPLER_3D:`sampler3D`,INT_SAMPLER_3D:`sampler3D`,UNSIGNED_INT_SAMPLER_3D:`sampler3D`},Pe={float:`float32`,vec2:`float32x2`,vec3:`float32x3`,vec4:`float32x4`,int:`sint32`,ivec2:`sint32x2`,ivec3:`sint32x3`,ivec4:`sint32x4`,uint:`uint32`,uvec2:`uint32x2`,uvec3:`uint32x3`,uvec4:`uint32x4`,bool:`uint32`,bvec2:`uint32x2`,bvec3:`uint32x3`,bvec4:`uint32x4`};function Fe(e,t){if(!q){let t=Object.keys(J);q={};for(let n=0;n<t.length;++n){let r=t[n];q[e[r]]=J[r]}}return q[t]}function Ie(e,t){return Pe[Fe(e,t)]||`float32`}function Le(e,t,n=!1){let r={},i=t.getProgramParameter(e,t.ACTIVE_ATTRIBUTES);for(let n=0;n<i;n++){let i=t.getActiveAttrib(e,n);if(i.name.startsWith(`gl_`))continue;let a=Ie(t,i.type);r[i.name]={location:0,format:a,stride:m(a).stride,offset:0,instance:!1,start:0}}let a=Object.keys(r);if(n){a.sort((e,t)=>e>t?1:-1);for(let n=0;n<a.length;n++)r[a[n]].location=n,t.bindAttribLocation(e,n,a[n]);t.linkProgram(e)}else for(let n=0;n<a.length;n++)r[a[n]].location=t.getAttribLocation(e,a[n]);return r}function Re(e,t,n=0){if(!t.ACTIVE_UNIFORM_BLOCKS)return{};let r={},i=t.getProgramParameter(e,t.ACTIVE_UNIFORM_BLOCKS);for(let a=0;a<i;a++){let i=t.getActiveUniformBlockName(e,a),o=t.getUniformBlockIndex(e,i),s=t.getActiveUniformBlockParameter(e,a,t.UNIFORM_BLOCK_DATA_SIZE);r[i]={name:i,index:o,binding:o+n,size:s}}return r}function ze(e,t){let n={},r=t.getProgramParameter(e,t.ACTIVE_UNIFORMS);for(let i=0;i<r;i++){let r=t.getActiveUniform(e,i),a=r.name.replace(/\[.*?\]$/,``),o=!!r.name.match(/\[.*?\]$/),s=Fe(t,r.type);n[a]={name:a,index:i,type:s,size:r.size,isArray:o,value:K(s,r.size)}}return n}function Be(e,t){let n=e.getShaderSource(t);if(n===null){console.error(`PixiJS Error: Could not retrieve shader source (WebGL context may be lost).`);return}let r=n.split(`
|
|
57
|
+
`).map((e,t)=>`${t}: ${e}`),i=e.getShaderInfoLog(t)??``,a=i.split(`
|
|
58
|
+
`),o={},s=a.map(e=>parseFloat(e.replace(/^ERROR\: 0\:([\d]+)\:.*$/,`$1`))).filter(e=>e&&!o[e]?(o[e]=!0,!0):!1),c=[``];s.forEach(e=>{r[e-1]=`%c${r[e-1]}%c`,c.push(`background: #FF0000; color:#FFFFFF; font-size: 10px`,`font-size: 10px`)}),c[0]=r.join(`
|
|
59
|
+
`),console.error(i),console.groupCollapsed(`click to view full shader code`),console.warn(...c),console.groupEnd()}function Ve(e,t,n,r){e.getProgramParameter(t,e.LINK_STATUS)||(e.getShaderParameter(n,e.COMPILE_STATUS)||Be(e,n),e.getShaderParameter(r,e.COMPILE_STATUS)||Be(e,r),console.error(`PixiJS Error: Could not initialize shader.`),e.getProgramInfoLog(t)!==``&&console.warn(`PixiJS Warning: gl.getProgramInfoLog()`,e.getProgramInfoLog(t)))}function He(e,t){let n=W(e,e.VERTEX_SHADER,t.vertex),r=W(e,e.FRAGMENT_SHADER,t.fragment),i=e.createProgram();e.attachShader(i,n),e.attachShader(i,r);let a=t.transformFeedbackVaryings;a&&(typeof e.transformFeedbackVaryings==`function`?e.transformFeedbackVaryings(i,a.names,a.bufferMode===`separate`?e.SEPARATE_ATTRIBS:e.INTERLEAVED_ATTRIBS):g(`TransformFeedback is not supported but TransformFeedbackVaryings are given.`)),e.linkProgram(i),e.getProgramParameter(i,e.LINK_STATUS)||Ve(e,i,n,r),t._attributeData=Le(i,e,!/^[ \t]*#[ \t]*version[ \t]+300[ \t]+es[ \t]*$/m.test(t.vertex)),t._uniformData=ze(i,e),t._uniformBlockData=Re(i,e,+!!a);for(let n in t._uniformBlockData){let{index:r,binding:a}=t._uniformBlockData[n];e.uniformBlockBinding(i,r,a)}e.deleteShader(n),e.deleteShader(r);let o={};for(let n in t._uniformData){let r=t._uniformData[n];o[n]={location:e.getUniformLocation(i,n),value:K(r.type,r.size)}}return new Ne(i,o)}var Y={textureCount:0},Ue=class{constructor(e){this._activeProgram=null,this._programDataHash=Object.create(null),this._shaderSyncFunctions=Object.create(null),this._renderer=e}contextChange(e){this._gl=e,this._programDataHash=Object.create(null),this._shaderSyncFunctions=Object.create(null),this._activeProgram=null}bind(e,t){if(this._setProgram(e.glProgram),t)return;Y.textureCount=0;let n=this._shaderSyncFunctions[e.glProgram._key];n||=this._shaderSyncFunctions[e.glProgram._key]=this._generateShaderSync(e,this),n(this._renderer,e,Y)}updateUniformGroup(e){this._renderer.uniformGroup.updateUniformGroup(e,this._activeProgram,Y)}bindUniformBlock(e,t){let n=this._renderer.buffer,{binding:r}=this._activeProgram._uniformBlockData[t],i=e._bufferResource;i||this._renderer.ubo.updateUniformGroup(e);let a=n.updateBuffer(e.buffer);if(!i){n.bindBufferBase(a,r);return}let{offset:o,size:s}=e;if(o===0&&(s===void 0||s===a.byteLength)){n.bindBufferBase(a,r);return}let c=this._renderer.limits.minUniformBufferOffsetAlignment;o%c!==0&&g(`[GlShaderSystem] uniform block "${t}" has a BufferResource offset of ${o}, which is not a multiple of renderer.limits.minUniformBufferOffsetAlignment (${c}). WebGL will not bind it.`),n.bindBufferRange(a,r,o,s)}_setProgram(e){if(this._activeProgram===e)return;this._activeProgram=e;let t=this._getProgramData(e);this._gl.useProgram(t.program),this._renderer.renderTarget.adaptor.setFragmentOutputs(e)}_getProgramData(e){return this._programDataHash[e._key]||this._createProgramData(e)}_createProgramData(e){let t=e._key;return this._programDataHash[t]=He(this._gl,e),this._programDataHash[t]}destroy(){for(let e of Object.keys(this._programDataHash))this._programDataHash[e].destroy();this._programDataHash=null,this._shaderSyncFunctions=null,this._activeProgram=null,this._renderer=null,this._gl=null}_generateShaderSync(e,t){return Me(e,t)}resetState(){this._activeProgram=null}};Ue.extension={type:[i.WebGLSystem],name:`shader`};var We={f32:`if (cv !== v) {
|
|
60
|
+
cu.value = v;
|
|
61
|
+
gl.uniform1f(location, v);
|
|
62
|
+
}`,"vec2<f32>":`if (cv[0] !== v[0] || cv[1] !== v[1]) {
|
|
63
|
+
cv[0] = v[0];
|
|
64
|
+
cv[1] = v[1];
|
|
65
|
+
gl.uniform2f(location, v[0], v[1]);
|
|
66
|
+
}`,"vec3<f32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) {
|
|
67
|
+
cv[0] = v[0];
|
|
68
|
+
cv[1] = v[1];
|
|
69
|
+
cv[2] = v[2];
|
|
70
|
+
gl.uniform3f(location, v[0], v[1], v[2]);
|
|
71
|
+
}`,"vec4<f32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) {
|
|
72
|
+
cv[0] = v[0];
|
|
73
|
+
cv[1] = v[1];
|
|
74
|
+
cv[2] = v[2];
|
|
75
|
+
cv[3] = v[3];
|
|
76
|
+
gl.uniform4f(location, v[0], v[1], v[2], v[3]);
|
|
77
|
+
}`,i32:`if (cv !== v) {
|
|
78
|
+
cu.value = v;
|
|
79
|
+
gl.uniform1i(location, v);
|
|
80
|
+
}`,"vec2<i32>":`if (cv[0] !== v[0] || cv[1] !== v[1]) {
|
|
81
|
+
cv[0] = v[0];
|
|
82
|
+
cv[1] = v[1];
|
|
83
|
+
gl.uniform2i(location, v[0], v[1]);
|
|
84
|
+
}`,"vec3<i32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) {
|
|
85
|
+
cv[0] = v[0];
|
|
86
|
+
cv[1] = v[1];
|
|
87
|
+
cv[2] = v[2];
|
|
88
|
+
gl.uniform3i(location, v[0], v[1], v[2]);
|
|
89
|
+
}`,"vec4<i32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) {
|
|
90
|
+
cv[0] = v[0];
|
|
91
|
+
cv[1] = v[1];
|
|
92
|
+
cv[2] = v[2];
|
|
93
|
+
cv[3] = v[3];
|
|
94
|
+
gl.uniform4i(location, v[0], v[1], v[2], v[3]);
|
|
95
|
+
}`,u32:`if (cv !== v) {
|
|
96
|
+
cu.value = v;
|
|
97
|
+
gl.uniform1ui(location, v);
|
|
98
|
+
}`,"vec2<u32>":`if (cv[0] !== v[0] || cv[1] !== v[1]) {
|
|
99
|
+
cv[0] = v[0];
|
|
100
|
+
cv[1] = v[1];
|
|
101
|
+
gl.uniform2ui(location, v[0], v[1]);
|
|
102
|
+
}`,"vec3<u32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) {
|
|
103
|
+
cv[0] = v[0];
|
|
104
|
+
cv[1] = v[1];
|
|
105
|
+
cv[2] = v[2];
|
|
106
|
+
gl.uniform3ui(location, v[0], v[1], v[2]);
|
|
107
|
+
}`,"vec4<u32>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) {
|
|
108
|
+
cv[0] = v[0];
|
|
109
|
+
cv[1] = v[1];
|
|
110
|
+
cv[2] = v[2];
|
|
111
|
+
cv[3] = v[3];
|
|
112
|
+
gl.uniform4ui(location, v[0], v[1], v[2], v[3]);
|
|
113
|
+
}`,bool:`if (cv !== v) {
|
|
114
|
+
cu.value = v;
|
|
115
|
+
gl.uniform1i(location, v);
|
|
116
|
+
}`,"vec2<bool>":`if (cv[0] !== v[0] || cv[1] !== v[1]) {
|
|
117
|
+
cv[0] = v[0];
|
|
118
|
+
cv[1] = v[1];
|
|
119
|
+
gl.uniform2i(location, v[0], v[1]);
|
|
120
|
+
}`,"vec3<bool>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) {
|
|
121
|
+
cv[0] = v[0];
|
|
122
|
+
cv[1] = v[1];
|
|
123
|
+
cv[2] = v[2];
|
|
124
|
+
gl.uniform3i(location, v[0], v[1], v[2]);
|
|
125
|
+
}`,"vec4<bool>":`if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) {
|
|
126
|
+
cv[0] = v[0];
|
|
127
|
+
cv[1] = v[1];
|
|
128
|
+
cv[2] = v[2];
|
|
129
|
+
cv[3] = v[3];
|
|
130
|
+
gl.uniform4i(location, v[0], v[1], v[2], v[3]);
|
|
131
|
+
}`,"mat2x2<f32>":`gl.uniformMatrix2fv(location, false, v);`,"mat3x3<f32>":`gl.uniformMatrix3fv(location, false, v);`,"mat4x4<f32>":`gl.uniformMatrix4fv(location, false, v);`},Ge={f32:`gl.uniform1fv(location, v);`,"vec2<f32>":`gl.uniform2fv(location, v);`,"vec3<f32>":`gl.uniform3fv(location, v);`,"vec4<f32>":`gl.uniform4fv(location, v);`,"mat2x2<f32>":`gl.uniformMatrix2fv(location, false, v);`,"mat3x3<f32>":`gl.uniformMatrix3fv(location, false, v);`,"mat4x4<f32>":`gl.uniformMatrix4fv(location, false, v);`,i32:`gl.uniform1iv(location, v);`,"vec2<i32>":`gl.uniform2iv(location, v);`,"vec3<i32>":`gl.uniform3iv(location, v);`,"vec4<i32>":`gl.uniform4iv(location, v);`,u32:`gl.uniform1iv(location, v);`,"vec2<u32>":`gl.uniform2iv(location, v);`,"vec3<u32>":`gl.uniform3iv(location, v);`,"vec4<u32>":`gl.uniform4iv(location, v);`,bool:`gl.uniform1iv(location, v);`,"vec2<bool>":`gl.uniform2iv(location, v);`,"vec3<bool>":`gl.uniform3iv(location, v);`,"vec4<bool>":`gl.uniform4iv(location, v);`};function Ke(e,t){let n=[`
|
|
132
|
+
var v = null;
|
|
133
|
+
var cv = null;
|
|
134
|
+
var cu = null;
|
|
135
|
+
var t = 0;
|
|
136
|
+
var gl = renderer.gl;
|
|
137
|
+
var name = null;
|
|
138
|
+
`];for(let r in e.uniforms){if(!t[r]){e.uniforms[r]instanceof T?e.uniforms[r].ubo?n.push(`
|
|
139
|
+
renderer.shader.bindUniformBlock(uv.${r}, "${r}");
|
|
140
|
+
`):n.push(`
|
|
141
|
+
renderer.shader.updateUniformGroup(uv.${r});
|
|
142
|
+
`):e.uniforms[r]instanceof le&&n.push(`
|
|
143
|
+
renderer.shader.bindBufferResource(uv.${r}, "${r}");
|
|
144
|
+
`);continue}let i=e.uniformStructures[r],a=!1;for(let e=0;e<E.length;e++){let t=E[e];if(i.type===t.type&&t.test(i)){n.push(`name = "${r}";`,E[e].uniform),a=!0;break}}if(!a){let e=(i.size===1?We:Ge)[i.type].replace(`location`,`ud["${r}"].location`);n.push(`
|
|
145
|
+
cu = ud["${r}"];
|
|
146
|
+
cv = cu.value;
|
|
147
|
+
v = uv["${r}"];
|
|
148
|
+
${e};`)}}return Function(`ud`,`uv`,`renderer`,`syncData`,n.join(`
|
|
149
|
+
`))}var qe=class{constructor(e){this._cache={},this._uniformGroupSyncHash={},this._renderer=e,this.gl=null,this._cache={}}contextChange(e){this.gl=e}updateUniformGroup(e,t,n){let r=this._renderer.shader._getProgramData(t);e.isStatic&&e._dirtyId===r.uniformDirtyGroups[e.uid]||(r.uniformDirtyGroups[e.uid]=e._dirtyId,this._getUniformSyncFunction(e,t)(r.uniformData,e.uniforms,this._renderer,n))}_getUniformSyncFunction(e,t){return this._uniformGroupSyncHash[e._signature]?.[t._key]||this._createUniformSyncFunction(e,t)}_createUniformSyncFunction(e,t){let n=this._uniformGroupSyncHash[e._signature]||(this._uniformGroupSyncHash[e._signature]={}),r=this._getSignature(e,t._uniformData,`u`);return this._cache[r]||(this._cache[r]=this._generateUniformsSync(e,t._uniformData)),n[t._key]=this._cache[r],n[t._key]}_generateUniformsSync(e,t){return Ke(e,t)}_getSignature(e,t,n){let r=e.uniforms,i=[`${n}-`];for(let e in r)i.push(e),t[e]&&i.push(t[e].type);return i.join(`-`)}destroy(){this._renderer=null,this._cache=null}};qe.extension={type:[i.WebGLSystem],name:`uniformGroup`};function Je(e){let t={};if(t.normal=[e.ONE,e.ONE_MINUS_SRC_ALPHA],t.add=[e.ONE,e.ONE],t.multiply=[e.DST_COLOR,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA],t.screen=[e.ONE,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA],t.none=[0,0],t[`normal-npm`]=[e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA],t[`add-npm`]=[e.SRC_ALPHA,e.ONE,e.ONE,e.ONE],t[`screen-npm`]=[e.SRC_ALPHA,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA],t.erase=[e.ZERO,e.ONE_MINUS_SRC_ALPHA],!(e instanceof x.get().getWebGLRenderingContext()))t.min=[e.ONE,e.ONE,e.ONE,e.ONE,e.MIN,e.MIN],t.max=[e.ONE,e.ONE,e.ONE,e.ONE,e.MAX,e.MAX];else{let n=e.getExtension(`EXT_blend_minmax`);n&&(t.min=[e.ONE,e.ONE,e.ONE,e.ONE,n.MIN_EXT,n.MIN_EXT],t.max=[e.ONE,e.ONE,e.ONE,e.ONE,n.MAX_EXT,n.MAX_EXT])}return t}var{BLEND:Ye,OFFSET:Xe,CULLING:Ze,DEPTH_TEST:Qe,WINDING:$e,DEPTH_MASK:et}=l,tt=class e{constructor(e){this._invertFrontFace=!1,this._renderer=e,this.gl=null,this.stateId=0,this.polygonOffset=0,this.blendMode=`none`,this._blendEq=!1,this.map=[],this.map[Ye]=this.setBlend,this.map[Xe]=this.setOffset,this.map[Ze]=this.setCullFace,this.map[Qe]=this.setDepthTest,this.map[$e]=this.setFrontFace,this.map[et]=this.setDepthMask,this.checks=[],this.defaultState=d.for2d(),e.renderTarget.onRenderTargetChange.add(this)}onRenderTargetChange(e){this._invertFrontFace=this._renderer.renderTarget.isFrontFaceInverted(e,e.flipY),this._cullFace?this.setFrontFace(this._frontFace):this._frontFaceDirty=!0}contextChange(e){this.gl=e,this.blendModesMap=Je(e),this.resetState()}set(e){if(e||=this.defaultState,this.stateId!==e.data){let t=this.stateId^e.data,n=0;for(;t;)t&1&&this.map[n].call(this,!!(e.data&1<<n)),t>>=1,n++;this.stateId=e.data}for(let t=0;t<this.checks.length;t++)this.checks[t](this,e)}forceState(e){e||=this.defaultState;for(let t=0;t<this.map.length;t++)this.map[t].call(this,!!(e.data&1<<t));for(let t=0;t<this.checks.length;t++)this.checks[t](this,e);this.stateId=e.data}setBlend(t){this._updateCheck(e._checkBlendMode,t),this.gl[t?`enable`:`disable`](this.gl.BLEND)}setOffset(t){this._updateCheck(e._checkPolygonOffset,t),this.gl[t?`enable`:`disable`](this.gl.POLYGON_OFFSET_FILL)}setDepthTest(e){this.gl[e?`enable`:`disable`](this.gl.DEPTH_TEST)}setDepthMask(e){this.gl.depthMask(e)}get depthMaskEnabled(){return!!(this.stateId&1<<et)}setCullFace(e){this._cullFace=e,this.gl[e?`enable`:`disable`](this.gl.CULL_FACE),this._cullFace&&this._frontFaceDirty&&this.setFrontFace(this._frontFace)}setFrontFace(e){this._frontFace=e,this._frontFaceDirty=!1;let t=this._invertFrontFace?!e:e;this._glFrontFace!==t&&(this._glFrontFace=t,this.gl.frontFace(this.gl[t?`CW`:`CCW`]))}setBlendMode(e){if(this.blendModesMap[e]||(e=`normal`),e===this.blendMode)return;this.blendMode=e;let t=this.blendModesMap[e],n=this.gl;t.length===2?n.blendFunc(t[0],t[1]):n.blendFuncSeparate(t[0],t[1],t[2],t[3]),t.length===6?(this._blendEq=!0,n.blendEquationSeparate(t[4],t[5])):this._blendEq&&(this._blendEq=!1,n.blendEquationSeparate(n.FUNC_ADD,n.FUNC_ADD))}setPolygonOffset(e,t){this.gl.polygonOffset(e,t)}resetState(){this._glFrontFace=!1,this._frontFace=!1,this._cullFace=!1,this._frontFaceDirty=!1,this._invertFrontFace=!1,this.gl.frontFace(this.gl.CCW),this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!1),this.forceState(this.defaultState),this._blendEq=!0,this.blendMode=``,this.setBlendMode(`normal`)}_updateCheck(e,t){let n=this.checks.indexOf(e);t&&n===-1?this.checks.push(e):!t&&n!==-1&&this.checks.splice(n,1)}static _checkBlendMode(e,t){e.setBlendMode(t.blendMode)}static _checkPolygonOffset(e,t){e.setPolygonOffset(1,t.polygonOffset)}destroy(){this.gl=null,this.checks.length=0,this._renderer=null}};tt.extension={type:[i.WebGLSystem],name:`state`};var nt=tt,rt=class{constructor(e){this.target=N.TEXTURE_2D,this._layerInitMask=0,this.msaaRenderBuffer=null,this.msaaWidth=-1,this.msaaHeight=-1,this.texture=e,this.width=-1,this.height=-1,this.type=P.UNSIGNED_BYTE,this.internalFormat=M.RGBA,this.format=M.RGBA,this.samplerType=0}destroy(){}},it=[new w,new w,new w],at={id:`buffer`,upload(e,t,n,r,i,a=!1){let o=i||t.target,s=e.resource;if(o===N.TEXTURE_3D||o===N.TEXTURE_2D_ARRAY){a||t.width!==e.width||t.height!==e.height?n.texImage3D(o,0,t.internalFormat,e.width,e.height,e.depthOrArrayLayers,0,t.format,t.type,s):n.texSubImage3D(o,0,0,0,0,e.width,e.height,e.depthOrArrayLayers,t.format,t.type,s),t.width=e.width,t.height=e.height;return}let c=e.width*e.height,l=a||t.width!==e.width||t.height!==e.height,u=e._updateStart>0||e._updateEnd<c;if(l)n.texImage2D(o,0,t.internalFormat,e.width,e.height,0,t.format,t.type,s);else if(u){let r=s.length/c,i=fe(e._updateStart,e._updateEnd,e.width,e.height,it);for(let a=0;a<i;a++){let i=it[a],c=(i.y*e.width+i.x)*r;n.texSubImage2D(o,0,i.x,i.y,i.width,i.height,t.format,t.type,s.subarray(c,c+i.width*i.height*r))}}else n.texSubImage2D(o,0,0,0,e.width,e.height,t.format,t.type,s);t.width=e.width,t.height=e.height}},ot={"bc1-rgba-unorm":!0,"bc1-rgba-unorm-srgb":!0,"bc2-rgba-unorm":!0,"bc2-rgba-unorm-srgb":!0,"bc3-rgba-unorm":!0,"bc3-rgba-unorm-srgb":!0,"bc4-r-unorm":!0,"bc4-r-snorm":!0,"bc5-rg-unorm":!0,"bc5-rg-snorm":!0,"bc6h-rgb-ufloat":!0,"bc6h-rgb-float":!0,"bc7-rgba-unorm":!0,"bc7-rgba-unorm-srgb":!0,"etc2-rgb8unorm":!0,"etc2-rgb8unorm-srgb":!0,"etc2-rgb8a1unorm":!0,"etc2-rgb8a1unorm-srgb":!0,"etc2-rgba8unorm":!0,"etc2-rgba8unorm-srgb":!0,"eac-r11unorm":!0,"eac-r11snorm":!0,"eac-rg11unorm":!0,"eac-rg11snorm":!0,"astc-4x4-unorm":!0,"astc-4x4-unorm-srgb":!0,"astc-5x4-unorm":!0,"astc-5x4-unorm-srgb":!0,"astc-5x5-unorm":!0,"astc-5x5-unorm-srgb":!0,"astc-6x5-unorm":!0,"astc-6x5-unorm-srgb":!0,"astc-6x6-unorm":!0,"astc-6x6-unorm-srgb":!0,"astc-8x5-unorm":!0,"astc-8x5-unorm-srgb":!0,"astc-8x6-unorm":!0,"astc-8x6-unorm-srgb":!0,"astc-8x8-unorm":!0,"astc-8x8-unorm-srgb":!0,"astc-10x5-unorm":!0,"astc-10x5-unorm-srgb":!0,"astc-10x6-unorm":!0,"astc-10x6-unorm-srgb":!0,"astc-10x8-unorm":!0,"astc-10x8-unorm-srgb":!0,"astc-10x10-unorm":!0,"astc-10x10-unorm-srgb":!0,"astc-12x10-unorm":!0,"astc-12x10-unorm-srgb":!0,"astc-12x12-unorm":!0,"astc-12x12-unorm-srgb":!0},st={id:`compressed`,upload(e,t,n,r,i,a){let o=i??t.target;n.pixelStorei(n.UNPACK_ALIGNMENT,4);let s=e.pixelWidth,c=e.pixelHeight,l=!!ot[e.format];for(let r=0;r<e.resource.length;r++){let i=e.resource[r];l?n.compressedTexImage2D(o,r,t.internalFormat,s,c,0,i):n.texImage2D(o,r,t.internalFormat,s,c,0,t.format,t.type,i),s=Math.max(s>>1,1),c=Math.max(c>>1,1)}}},ct=[`right`,`left`,`top`,`bottom`,`front`,`back`];function lt(e){return{id:`cube`,upload(t,n,r,i){let a=t.faces;for(let t=0;t<ct.length;t++){let o=a[ct[t]];o.resource&&((e[o.uploadMethodId]||e.image).upload(o,n,r,i,N.TEXTURE_CUBE_MAP_POSITIVE_X+t,!(n._layerInitMask&1<<t)),n._layerInitMask|=1<<t)}n.width=t.pixelWidth,n.height=t.pixelHeight}}}var ut={id:`image`,upload(e,t,n,r,i,a=!1){let o=i||t.target,s=e.pixelWidth,c=e.pixelHeight,l=e.resourceWidth,u=e.resourceHeight,d=r===2,f=a||t.width!==s||t.height!==c,p=l>=s&&u>=c,m=e.resource;(d?dt:ft)(n,o,t,s,c,l,u,m,f,p),t.width=s,t.height=c}};function dt(e,t,n,r,i,a,o,s,c,l){l?c?e.texImage2D(t,0,n.internalFormat,r,i,0,n.format,n.type,s):e.texSubImage2D(t,0,0,0,n.format,n.type,s):(c&&e.texImage2D(t,0,n.internalFormat,r,i,0,n.format,n.type,null),e.texSubImage2D(t,0,0,0,a,o,n.format,n.type,s))}function ft(e,t,n,r,i,a,o,s,c,l){l?c?e.texImage2D(t,0,n.internalFormat,n.format,n.type,s):e.texSubImage2D(t,0,0,0,n.format,n.type,s):(c&&e.texImage2D(t,0,n.internalFormat,r,i,0,n.format,n.type,null),e.texSubImage2D(t,0,0,0,n.format,n.type,s))}var pt=ge(),mt={id:`video`,upload(e,t,n,r,i,a=pt){if(e.isValid)ut.upload(e,t,n,r,i,a);else{let e=i??t.target;n.texImage2D(e,0,t.internalFormat,1,1,0,t.format,t.type,null)}}},X={linear:9729,nearest:9728},ht={linear:{linear:9987,nearest:9985},nearest:{linear:9986,nearest:9984}},Z={"clamp-to-edge":33071,repeat:10497,"mirror-repeat":33648},gt={never:512,less:513,equal:514,"less-equal":515,greater:516,"not-equal":517,"greater-equal":518,always:519};function _t(e,t,n,r,i,a,o,s){let c=a;if(!s||e.addressModeU!==`repeat`||e.addressModeV!==`repeat`||e.addressModeW!==`repeat`){let n=Z[o?`clamp-to-edge`:e.addressModeU],r=Z[o?`clamp-to-edge`:e.addressModeV],a=Z[o?`clamp-to-edge`:e.addressModeW];t[i](c,t.TEXTURE_WRAP_S,n),t[i](c,t.TEXTURE_WRAP_T,r),t.TEXTURE_WRAP_R&&t[i](c,t.TEXTURE_WRAP_R,a)}if(s&&e.magFilter===`linear`||t[i](c,t.TEXTURE_MAG_FILTER,X[e.magFilter]),n){if(!s||e.mipmapFilter!==`linear`){let n=ht[e.minFilter][e.mipmapFilter];t[i](c,t.TEXTURE_MIN_FILTER,n)}}else t[i](c,t.TEXTURE_MIN_FILTER,X[e.minFilter]);if(r&&e.maxAnisotropy>1){let n=Math.min(e.maxAnisotropy,t.getParameter(r.MAX_TEXTURE_MAX_ANISOTROPY_EXT));t[i](c,r.TEXTURE_MAX_ANISOTROPY_EXT,n)}e.compare?(t[i](c,t.TEXTURE_COMPARE_FUNC,gt[e.compare]),t[i](c,t.TEXTURE_COMPARE_MODE,t.COMPARE_REF_TO_TEXTURE)):!s&&t.TEXTURE_COMPARE_MODE&&t[i](c,t.TEXTURE_COMPARE_MODE,t.NONE)}function vt(e){return{r8unorm:e.RED,r8snorm:e.RED,r8uint:e.RED_INTEGER,r8sint:e.RED_INTEGER,r16uint:e.RED_INTEGER,r16sint:e.RED_INTEGER,r16float:e.RED,rg8unorm:e.RG,rg8snorm:e.RG,rg8uint:e.RG_INTEGER,rg8sint:e.RG_INTEGER,r32uint:e.RED_INTEGER,r32sint:e.RED_INTEGER,r32float:e.RED,rg16uint:e.RG_INTEGER,rg16sint:e.RG_INTEGER,rg16float:e.RG,rgba8unorm:e.RGBA,"rgba8unorm-srgb":e.RGBA,rgba8snorm:e.RGBA,rgba8uint:e.RGBA_INTEGER,rgba8sint:e.RGBA_INTEGER,bgra8unorm:e.RGBA,"bgra8unorm-srgb":e.RGBA,rgb9e5ufloat:e.RGB,rgb10a2unorm:e.RGBA,rg11b10ufloat:e.RGB,rg32uint:e.RG_INTEGER,rg32sint:e.RG_INTEGER,rg32float:e.RG,rgba16uint:e.RGBA_INTEGER,rgba16sint:e.RGBA_INTEGER,rgba16float:e.RGBA,rgba32uint:e.RGBA_INTEGER,rgba32sint:e.RGBA_INTEGER,rgba32float:e.RGBA,stencil8:e.STENCIL_INDEX8,depth16unorm:e.DEPTH_COMPONENT,depth24plus:e.DEPTH_COMPONENT,"depth24plus-stencil8":e.DEPTH_STENCIL,depth32float:e.DEPTH_COMPONENT,"depth32float-stencil8":e.DEPTH_STENCIL}}function yt(e,t){let n={},r=e.RGBA;return e instanceof x.get().getWebGLRenderingContext()?t.srgb&&(n={"rgba8unorm-srgb":t.srgb.SRGB8_ALPHA8_EXT,"bgra8unorm-srgb":t.srgb.SRGB8_ALPHA8_EXT}):(n={"rgba8unorm-srgb":e.SRGB8_ALPHA8,"bgra8unorm-srgb":e.SRGB8_ALPHA8},r=e.RGBA8),{r8unorm:e.R8,r8snorm:e.R8_SNORM,r8uint:e.R8UI,r8sint:e.R8I,r16uint:e.R16UI,r16sint:e.R16I,r16float:e.R16F,rg8unorm:e.RG8,rg8snorm:e.RG8_SNORM,rg8uint:e.RG8UI,rg8sint:e.RG8I,r32uint:e.R32UI,r32sint:e.R32I,r32float:e.R32F,rg16uint:e.RG16UI,rg16sint:e.RG16I,rg16float:e.RG16F,rgba8unorm:e.RGBA,...n,rgba8snorm:e.RGBA8_SNORM,rgba8uint:e.RGBA8UI,rgba8sint:e.RGBA8I,bgra8unorm:r,rgb9e5ufloat:e.RGB9_E5,rgb10a2unorm:e.RGB10_A2,rg11b10ufloat:e.R11F_G11F_B10F,rg32uint:e.RG32UI,rg32sint:e.RG32I,rg32float:e.RG32F,rgba16uint:e.RGBA16UI,rgba16sint:e.RGBA16I,rgba16float:e.RGBA16F,rgba32uint:e.RGBA32UI,rgba32sint:e.RGBA32I,rgba32float:e.RGBA32F,stencil8:e.STENCIL_INDEX8,depth16unorm:e.DEPTH_COMPONENT16,depth24plus:e.DEPTH_COMPONENT24,"depth24plus-stencil8":e.DEPTH24_STENCIL8,depth32float:e.DEPTH_COMPONENT32F,"depth32float-stencil8":e.DEPTH32F_STENCIL8,...t.s3tc?{"bc1-rgba-unorm":t.s3tc.COMPRESSED_RGBA_S3TC_DXT1_EXT,"bc2-rgba-unorm":t.s3tc.COMPRESSED_RGBA_S3TC_DXT3_EXT,"bc3-rgba-unorm":t.s3tc.COMPRESSED_RGBA_S3TC_DXT5_EXT}:{},...t.s3tc_sRGB?{"bc1-rgba-unorm-srgb":t.s3tc_sRGB.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,"bc2-rgba-unorm-srgb":t.s3tc_sRGB.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,"bc3-rgba-unorm-srgb":t.s3tc_sRGB.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT}:{},...t.rgtc?{"bc4-r-unorm":t.rgtc.COMPRESSED_RED_RGTC1_EXT,"bc4-r-snorm":t.rgtc.COMPRESSED_SIGNED_RED_RGTC1_EXT,"bc5-rg-unorm":t.rgtc.COMPRESSED_RED_GREEN_RGTC2_EXT,"bc5-rg-snorm":t.rgtc.COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT}:{},...t.bptc?{"bc6h-rgb-float":t.bptc.COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT,"bc6h-rgb-ufloat":t.bptc.COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT,"bc7-rgba-unorm":t.bptc.COMPRESSED_RGBA_BPTC_UNORM_EXT,"bc7-rgba-unorm-srgb":t.bptc.COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT}:{},...t.etc?{"etc2-rgb8unorm":t.etc.COMPRESSED_RGB8_ETC2,"etc2-rgb8unorm-srgb":t.etc.COMPRESSED_SRGB8_ETC2,"etc2-rgb8a1unorm":t.etc.COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,"etc2-rgb8a1unorm-srgb":t.etc.COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,"etc2-rgba8unorm":t.etc.COMPRESSED_RGBA8_ETC2_EAC,"etc2-rgba8unorm-srgb":t.etc.COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,"eac-r11unorm":t.etc.COMPRESSED_R11_EAC,"eac-rg11unorm":t.etc.COMPRESSED_SIGNED_RG11_EAC}:{},...t.astc?{"astc-4x4-unorm":t.astc.COMPRESSED_RGBA_ASTC_4x4_KHR,"astc-4x4-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,"astc-5x4-unorm":t.astc.COMPRESSED_RGBA_ASTC_5x4_KHR,"astc-5x4-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,"astc-5x5-unorm":t.astc.COMPRESSED_RGBA_ASTC_5x5_KHR,"astc-5x5-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,"astc-6x5-unorm":t.astc.COMPRESSED_RGBA_ASTC_6x5_KHR,"astc-6x5-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,"astc-6x6-unorm":t.astc.COMPRESSED_RGBA_ASTC_6x6_KHR,"astc-6x6-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,"astc-8x5-unorm":t.astc.COMPRESSED_RGBA_ASTC_8x5_KHR,"astc-8x5-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,"astc-8x6-unorm":t.astc.COMPRESSED_RGBA_ASTC_8x6_KHR,"astc-8x6-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,"astc-8x8-unorm":t.astc.COMPRESSED_RGBA_ASTC_8x8_KHR,"astc-8x8-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,"astc-10x5-unorm":t.astc.COMPRESSED_RGBA_ASTC_10x5_KHR,"astc-10x5-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,"astc-10x6-unorm":t.astc.COMPRESSED_RGBA_ASTC_10x6_KHR,"astc-10x6-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,"astc-10x8-unorm":t.astc.COMPRESSED_RGBA_ASTC_10x8_KHR,"astc-10x8-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,"astc-10x10-unorm":t.astc.COMPRESSED_RGBA_ASTC_10x10_KHR,"astc-10x10-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,"astc-12x10-unorm":t.astc.COMPRESSED_RGBA_ASTC_12x10_KHR,"astc-12x10-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,"astc-12x12-unorm":t.astc.COMPRESSED_RGBA_ASTC_12x12_KHR,"astc-12x12-unorm-srgb":t.astc.COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR}:{}}}function bt(e){return{r8unorm:e.UNSIGNED_BYTE,r8snorm:e.BYTE,r8uint:e.UNSIGNED_BYTE,r8sint:e.BYTE,r16uint:e.UNSIGNED_SHORT,r16sint:e.SHORT,r16float:e.HALF_FLOAT,rg8unorm:e.UNSIGNED_BYTE,rg8snorm:e.BYTE,rg8uint:e.UNSIGNED_BYTE,rg8sint:e.BYTE,r32uint:e.UNSIGNED_INT,r32sint:e.INT,r32float:e.FLOAT,rg16uint:e.UNSIGNED_SHORT,rg16sint:e.SHORT,rg16float:e.HALF_FLOAT,rgba8unorm:e.UNSIGNED_BYTE,"rgba8unorm-srgb":e.UNSIGNED_BYTE,rgba8snorm:e.BYTE,rgba8uint:e.UNSIGNED_BYTE,rgba8sint:e.BYTE,bgra8unorm:e.UNSIGNED_BYTE,"bgra8unorm-srgb":e.UNSIGNED_BYTE,rgb9e5ufloat:e.UNSIGNED_INT_5_9_9_9_REV,rgb10a2unorm:e.UNSIGNED_INT_2_10_10_10_REV,rg11b10ufloat:e.UNSIGNED_INT_10F_11F_11F_REV,rg32uint:e.UNSIGNED_INT,rg32sint:e.INT,rg32float:e.FLOAT,rgba16uint:e.UNSIGNED_SHORT,rgba16sint:e.SHORT,rgba16float:e.HALF_FLOAT,rgba32uint:e.UNSIGNED_INT,rgba32sint:e.INT,rgba32float:e.FLOAT,stencil8:e.UNSIGNED_BYTE,depth16unorm:e.UNSIGNED_SHORT,depth24plus:e.UNSIGNED_INT,"depth24plus-stencil8":e.UNSIGNED_INT_24_8,depth32float:e.FLOAT,"depth32float-stencil8":e.FLOAT_32_UNSIGNED_INT_24_8_REV}}function xt(e){return{"2d":e.TEXTURE_2D,cube:e.TEXTURE_CUBE_MAP,"1d":null,"3d":e?.TEXTURE_3D||null,"2d-array":e?.TEXTURE_2D_ARRAY||null,"cube-array":e?.TEXTURE_CUBE_MAP_ARRAY||null}}var St=4,Q=class e{constructor(t){this._glSamplers=Object.create(null),this._boundTextures=[],this._integerUnits=0,this._activeTextureLocation=-1,this._boundSamplers=Object.create(null),this._premultiplyAlpha=!1,this._useSeparateSamplers=!1,this._renderer=t,this._managedTextures=new h({renderer:t,type:`resource`,onUnload:this.onSourceUnload.bind(this),name:`glTexture`});let n={image:ut,buffer:at,video:mt,compressed:st,...e.uploadExtensions};this._uploads={...n,cube:lt(n)}}get managedTextures(){return Object.values(this._managedTextures.items)}contextChange(e){this._gl=e,this._mapFormatToInternalFormat||(this._mapFormatToInternalFormat=yt(e,this._renderer.context.extensions),this._mapFormatToType=bt(e),this._mapFormatToFormat=vt(e),this._mapViewDimensionToGlTarget=xt(e)),this._managedTextures.removeAll(!0),this._glSamplers=Object.create(null),this._boundSamplers=Object.create(null),this._premultiplyAlpha=!1;for(let e=0;e<16;e++)this.bind(_.EMPTY,e)}initSource(e){this.bind(e)}bind(e,t=0){let n=e.source;e?(this.bindSource(n,t),this._useSeparateSamplers&&this._bindSampler(n.style,t)):(this.bindSource(null,t),this._useSeparateSamplers&&this._bindSampler(null,t))}bindSource(e,t=0){let n=this._gl;if(e._gcLastUsed=this._renderer.gc.now,this._boundTextures[t]!==e){this._setBoundTexture(t,e),this._activateLocation(t),e||=_.EMPTY.source;let r=this.getGlSource(e);n.bindTexture(r.target,r.texture)}}unbindIntegerTextures(e){if(e>=32)return;let t=this._integerUnits>>>e<<e;for(;t;){let e=31-Math.clz32(t);t&=~(1<<e),this.bind(_.EMPTY,e)}}_setBoundTexture(e,t){this._boundTextures[e]=t,e<0||e>31||t&&t.viewDimension===`2d`&&(v(t.format)?this._integerUnits|=1<<e:this._integerUnits&=~(1<<e))}_bindSampler(e,t=0){let n=this._gl;if(!e){this._boundSamplers[t]=null,n.bindSampler(t,null);return}let r=this._getGlSampler(e);this._boundSamplers[t]!==r&&(this._boundSamplers[t]=r,n.bindSampler(t,r))}unbind(e){let t=e.source,n=this._boundTextures,r=this._gl;for(let e=0;e<n.length;e++)if(n[e]===t){this._activateLocation(e);let n=this.getGlSource(t);r.bindTexture(n.target,null),this._setBoundTexture(e,null)}}_activateLocation(e){this._activeTextureLocation!==e&&(this._activeTextureLocation=e,this._gl.activeTexture(this._gl.TEXTURE0+e))}_initSource(e){let t=new rt(this._gl.createTexture());if(t.type=this._mapFormatToType[e.format],t.internalFormat=this._mapFormatToInternalFormat[e.format],t.format=this._mapFormatToFormat[e.format],t.target=this._mapViewDimensionToGlTarget[e.viewDimension],t.target===null)throw Error(`Unsupported view dimension: ${e.viewDimension} with this webgl version: ${this._renderer.context.webGLVersion}`);if(e.autoGenerateMipmaps&&(this._renderer.context.supports.nonPowOf2mipmaps||e.isPowerOfTwo)){let t=Math.max(e.width,e.height,e.depth);e.mipLevelCount=Math.floor(Math.log2(t))+1}return e._gpuData[this._renderer.uid]=t,this._managedTextures.add(e)&&(e.on(`update`,this.onSourceUpdate,this),e.on(`resize`,this.onSourceUpdate,this),e.on(`styleChange`,this.onStyleChange,this),e.on(`updateMipmaps`,this.onUpdateMipmaps,this)),this.onSourceUpdate(e),this.updateStyle(e,!1),t}onStyleChange(e){this.updateStyle(e,!1)}updateStyle(e,t){let n=this._gl,r=this.getGlSource(e);n.bindTexture(r.target,r.texture),this._setBoundTexture(this._activeTextureLocation,e),_t(e.style,n,e.mipLevelCount>1,this._renderer.context.extensions.anisotropicFiltering,`texParameteri`,r.target,!this._renderer.context.supports.nonPowOf2wrapping&&!e.isPowerOfTwo,t)}onSourceUnload(e,t=!1){let n=e._gpuData[this._renderer.uid];n&&(t||(this.unbind(e),this._gl.deleteTexture(n.texture),n.msaaRenderBuffer&&this._gl.deleteRenderbuffer(n.msaaRenderBuffer)),e.off(`update`,this.onSourceUpdate,this),e.off(`resize`,this.onSourceUpdate,this),e.off(`styleChange`,this.onStyleChange,this),e.off(`updateMipmaps`,this.onUpdateMipmaps,this))}onSourceUpdate(e){let t=this._gl,n=this.getGlSource(e);t.bindTexture(n.target,n.texture),this._setBoundTexture(this._activeTextureLocation,e);let r=e.alphaMode===`premultiply-alpha-on-upload`&&!v(e.format)&&n.target!==t.TEXTURE_3D&&n.target!==t.TEXTURE_2D_ARRAY;if(this._premultiplyAlpha!==r&&(this._premultiplyAlpha=r,t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,r)),this._uploads[e.uploadMethodId])this._uploads[e.uploadMethodId].upload(e,n,t,this._renderer.context.webGLVersion);else if(n.target===t.TEXTURE_2D)this._initEmptyTexture2D(n,e);else if(n.target===t.TEXTURE_2D_ARRAY||n.target===t.TEXTURE_3D)this._initEmptyTexture3D(n,e);else if(n.target===t.TEXTURE_CUBE_MAP)this._initEmptyTextureCube(n,e);else throw Error(`[GlTextureSystem] Unsupported texture target for empty allocation.`);this._applyMipRange(n,e),e.autoGenerateMipmaps&&e.mipLevelCount>1&&this.onUpdateMipmaps(e,!1)}onUpdateMipmaps(e,t=!0){t&&this.bindSource(e,0);let n=this.getGlSource(e);this._gl.generateMipmap(n.target)}_initEmptyTexture2D(e,t){let n=this._gl;n.texImage2D(n.TEXTURE_2D,0,e.internalFormat,t.pixelWidth,t.pixelHeight,0,e.format,e.type,null);let r=Math.max(t.pixelWidth>>1,1),i=Math.max(t.pixelHeight>>1,1);for(let a=1;a<t.mipLevelCount;a++)n.texImage2D(n.TEXTURE_2D,a,e.internalFormat,r,i,0,e.format,e.type,null),r=Math.max(r>>1,1),i=Math.max(i>>1,1)}_initEmptyTexture3D(e,t){let n=this._gl,r=e.target===n.TEXTURE_3D,i=t.pixelWidth,a=t.pixelHeight,o=t.depthOrArrayLayers;for(let s=0;s<t.mipLevelCount;s++)n.texImage3D(e.target,s,e.internalFormat,i,a,o,0,e.format,e.type,null),i=Math.max(i>>1,1),a=Math.max(a>>1,1),r&&(o=Math.max(o>>1,1))}_initEmptyTextureCube(e,t){let n=this._gl;for(let r=0;r<6;r++)n.texImage2D(n.TEXTURE_CUBE_MAP_POSITIVE_X+r,0,e.internalFormat,t.pixelWidth,t.pixelHeight,0,e.format,e.type,null);let r=Math.max(t.pixelWidth>>1,1),i=Math.max(t.pixelHeight>>1,1);for(let a=1;a<t.mipLevelCount;a++){for(let t=0;t<6;t++)n.texImage2D(n.TEXTURE_CUBE_MAP_POSITIVE_X+t,a,e.internalFormat,r,i,0,e.format,e.type,null);r=Math.max(r>>1,1),i=Math.max(i>>1,1)}}_applyMipRange(e,t){if(this._renderer.context.webGLVersion!==2||t.mipLevelCount<=1)return;let n=this._gl,r=Math.max((t.mipLevelCount|0)-1,0);n.texParameteri(e.target,n.TEXTURE_BASE_LEVEL,0),n.texParameteri(e.target,n.TEXTURE_MAX_LEVEL,r)}_initSampler(e){let t=this._gl,n=this._gl.createSampler();return this._glSamplers[e._resourceId]=n,_t(e,t,this._boundTextures[this._activeTextureLocation].mipLevelCount>1,this._renderer.context.extensions.anisotropicFiltering,`samplerParameteri`,n,!1,!0),this._glSamplers[e._resourceId]}_getGlSampler(e){return this._glSamplers[e._resourceId]||this._initSampler(e)}getGlSource(e){return e._gcLastUsed=this._renderer.gc.now,e._gpuData[this._renderer.uid]||this._initSource(e)}getMsaaRenderBuffer(e){let t=this._gl,n=this.getGlSource(e);return n.msaaRenderBuffer??=t.createRenderbuffer(),(n.msaaWidth!==e.pixelWidth||n.msaaHeight!==e.pixelHeight)&&(n.msaaWidth=e.pixelWidth,n.msaaHeight=e.pixelHeight,t.bindRenderbuffer(t.RENDERBUFFER,n.msaaRenderBuffer),t.renderbufferStorageMultisample(t.RENDERBUFFER,4,n.internalFormat,e.pixelWidth,e.pixelHeight)),n.msaaRenderBuffer}generateCanvas(e){let{pixels:t,width:n,height:r}=this.getPixels(e),i=x.get().createCanvas();i.width=n,i.height=r;let a=i.getContext(`2d`);if(a){let e=a.createImageData(n,r);e.data.set(t),a.putImageData(e,0,0)}return i}getPixels(e){let t=e.source.resolution,n=e.frame,r=Math.max(Math.round(n.width*t),1),i=Math.max(Math.round(n.height*t),1),a=new Uint8Array(St*r*i),o=this._renderer,s=o.renderTarget.getRenderTarget(e),c=o.renderTarget.getGpuRenderTarget(s),l=o.gl;return o.renderTarget.adaptor.bindFramebuffer(c.resolveTargetFramebuffer),l.readPixels(Math.round(n.x*t),Math.round(n.y*t),r,i,l.RGBA,l.UNSIGNED_BYTE,a),{pixels:new Uint8ClampedArray(a.buffer),width:r,height:i}}destroy(){this._managedTextures.destroy(),this._glSamplers=null,this._boundTextures=null,this._boundSamplers=null,this._mapFormatToInternalFormat=null,this._mapFormatToType=null,this._mapFormatToFormat=null,this._uploads=null,this._renderer=null}resetState(){let e=this._renderer.limits.maxTextures;this._activeTextureLocation=-1,this._boundTextures.fill(null),this._integerUnits=e>=32?-1:(1<<e)-1,this._boundSamplers=Object.create(null);let t=this._gl;this._premultiplyAlpha=!1,t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,this._premultiplyAlpha)}};Q.extension={type:[i.WebGLSystem],name:`texture`},Q.uploadExtensions=Object.create(null);var Ct=Q;r.handleByMap(i.TextureUploaderWebGL,Ct.uploadExtensions);var wt=class{_getShader(e){return this.shader}execute(e,t){let n=t.context,r=e.renderer,i=this._getRenderData(r,n),{batcher:a,instructions:o}=i,s=n.customShader||this._getShader(i);s.groups[0]=r.globalUniforms.bindGroup,r.state.set(e.state),r.shader.bind(s),r.geometry.bind(a.geometry,s.glProgram);let c=o.instructions;for(let e=0;e<o.instructionSize;e++){let t=c[e];if(t.size){for(let e=0;e<t.textures.count;e++)r.texture.bind(t.textures.textures[e],e);r.texture.unbindIntegerTextures(t.textures.count),r.geometry.draw(t.topology,t.size,t.start)}}}destroy(){this.shader.destroy(!0),this.shader=null}},Tt=class extends wt{contextChange(e){let t=new T({uColor:{value:new Float32Array([1,1,1,1]),type:`vec4<f32>`},uTransformMatrix:{value:new n,type:`mat3x3<f32>`},uRound:{value:0,type:`f32`}}),r=e.limits.maxBatchableTextures,i=S({name:`graphics`,bits:[ee,ie(r),D,y]});this.shader=new a({glProgram:i,resources:{localUniforms:t,batchSamplers:re(r)}})}_getRenderData(e,t){return e.graphicsContext.getContextRenderData(t)}};Tt.extension={type:[i.WebGLPipesAdaptor],name:`graphics`};var Et=class{init(){let e=S({name:`mesh`,bits:[D,se,y]});this._shader=new a({glProgram:e,resources:{uTexture:_.EMPTY.source,textureUniforms:{uTextureMatrix:{type:`mat3x3<f32>`,value:new n}}}})}execute(e,t){let n=e.renderer,r=t._shader;if(!r){r=this._shader;let e=t.texture,n=e.source;r.resources.uTexture=n,r.resources.uSampler=n.style,r.resources.textureUniforms.uniforms.uTextureMatrix=e.textureMatrix.mapCoord}else if(!r.glProgram){g(`Mesh shader has no glProgram`,t.shader);return}r.groups[100]=n.globalUniforms.bindGroup,r.groups[101]=e.localUniformsBindGroup,n.encoder.draw({geometry:t._geometry,shader:r,state:t.state})}destroy(){this._shader.destroy(!0),this._shader=null}};Et.extension={type:[i.WebGLPipesAdaptor],name:`mesh`};var Dt=[...me,U,Ce,ve,B,A,Ct,ke,I,qe,Ue,z,nt,V,R],Ot=[...pe],kt=[O,Et,Tt],At=[],jt=[],Mt=[],$=[];r.handleByNamedList(i.WebGLSystem,At),r.handleByNamedList(i.WebGLPipes,jt),r.handleByNamedList(i.WebGLPipesAdaptor,Mt),r.handleByNamedList(i.WebGLLoader,$),r.add(...Dt,...Ot,...kt);var Nt=class extends u{constructor(){let e={name:`webgl`,type:c.WEBGL,systems:At,renderPipes:jt,renderPipeAdaptors:Mt,loaders:$};super(e)}};export{Nt as WebGLRenderer};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import{A as e,Ct as t,Dt as n,Et as r,F as i,I as a,K as o,L as s,O as c,P as l,R as u,S as d,T as f,U as p,c as m,ct as h,dt as g,f as _,g as v,gt as y,l as b,m as x,pt as S,q as C,w,x as ee,xt as T,y as E,z as D}from"./index-CE-lli1m.js";import{a as te,c as ne,f as re,i as ie,l as O,m as ae,n as oe,s as se,t as ce,u as le}from"./getTexelRangeRects-D3pwCyRt.js";import{n as ue,r as de,t as fe}from"./RenderTargetSystem-Dk1U4wDJ.js";import{t as k}from"./CanvasPool-PBu3bHFZ.js";var A=l.for2d(),j=class{start(e,t,n){let r=e.renderer,i=r.encoder,a=n.gpuProgram;this._shader=n,this._geometry=t,i.setGeometry(t,a),A.blendMode=`normal`,r.pipeline.getPipeline(t,a,A,void 0,n._overrides);let o=r.globalUniforms.bindGroup;i.resetBindGroup(1),i.setBindGroup(0,o,a)}execute(e,t){let n=this._shader.gpuProgram,r=e.renderer,i=r.encoder;if(!t.bindGroup){let e=t.textures;t.bindGroup=b(e.textures,e.count,r.limits.maxBatchableTextures)}A.blendMode=t.blendMode;let a=r.bindGroup.getBindGroup(t.bindGroup,n,1),o=r.pipeline.getPipeline(this._geometry,n,A,t.topology,this._shader._overrides);t.bindGroup._touch(r.gc.now),i.setPipeline(o),i.renderPassEncoder.setBindGroup(1,a),i.renderPassEncoder.drawIndexed(t.size,1,t.start)}};j.extension={type:[r.WebGPUPipesAdaptor],name:`batch`};var pe=0,me=class{constructor(e,t,n,r,i,a){this.autoGarbageCollect=!0,this._gpuData=null,this.gpuBindGroup=e,this._gcLastUsed=t,this.keyLow=n,this.keyHigh=r,this.layoutKey=i,this.cacheId=a}unload(){this.gpuBindGroup=null}},M=class{constructor(e){this._hash=Object.create(null),this._cacheId=0,this._renderer=e,e.gc.addResourceHash(this,`_hash`,`resource`),e.gc.addCollection(this,`_hash`,`hash`)}contextChange(e){this._gpu=e,this._cacheId=++pe,this._hash=Object.create(null)}getBindGroup(e,t,n){let r=t._layoutKey<<4|n,i=e._gpuEntry;if(i!==null&&e._gpuEntryLayoutKey===r&&i.gpuBindGroup!==null&&i.cacheId===this._cacheId)return i._gcLastUsed=this._renderer.gc.now,i.gpuBindGroup;let a=e._keyLow,o=e._keyHigh,s=(a^Math.imul(r+1,0x9e3779b1))&0x3fffffff;return i=this._hash[s],i&&i.keyLow===a&&i.keyHigh===o&&i.layoutKey===r?i._gcLastUsed=this._renderer.gc.now:(i=new me(this._createBindGroup(e,t,n),this._renderer.gc.now,a,o,r,this._cacheId),this._hash[s]=i),e._gpuEntry=i,e._gpuEntryLayoutKey=r,i.gpuBindGroup}_createBindGroup(e,t,n){let r=this._gpu.device,i=t.layout[n],a=e.layout,o=[],s=this._renderer;for(let t in i){let n=e.resources[a?a[t]:i[t]];if(!n||n.destroyed)throw Error(`[BindGroup] no usable resource for the shader's '${t}' binding: it was never set, was destroyed while a shader still uses it, or is missing from the bind group's layout.`);let r;if(n._resourceType===`uniformGroup`){let e=n;s.ubo.updateUniformGroup(e);let t=e.buffer;r={buffer:s.buffer.getGPUBuffer(t),offset:0,size:t.descriptor.size}}else if(n._resourceType===`buffer`){let e=n;r={buffer:s.buffer.getGPUBuffer(e),offset:0,size:e.descriptor.size}}else if(n._resourceType===`bufferResource`){let e=n;r={buffer:s.buffer.getGPUBuffer(e.buffer),offset:e.offset,size:e.size}}else if(n._resourceType===`textureSampler`){let e=n;r=s.texture.getGpuSampler(e)}else if(n._resourceType===`textureSource`){let e=n;r=s.texture.getTextureView(e)}else if(n._resourceType===`textureView`){let e=n;r=s.texture.getTextureView(e.source,e.viewDescriptor)}o.push({binding:i[t],resource:r})}let c=s.shader.getProgramData(t).bindGroups[n];return r.createBindGroup({layout:c,entries:o})}destroy(){this._hash=null,this._renderer=null}};M.extension={type:[r.WebGPUSystem],name:`bindGroup`};var he=class{constructor(e){this.gpuBuffer=e}destroy(){this.gpuBuffer.destroy(),this.gpuBuffer=null}},N=class{constructor(e){this._renderer=e,this._managedBuffers=new m({renderer:e,type:`resource`,onUnload:this.onBufferUnload.bind(this),name:`gpuBuffer`})}contextChange(e){this._gpu=e,this.destroyAll()}getGPUBuffer(e){return e._gcLastUsed=this._renderer.gc.now,e._gpuData[this._renderer.uid]?.gpuBuffer||this.createGPUBuffer(e)}updateBuffer(e){let t=this.getGPUBuffer(e),n=e.data;return e._updateID&&n&&(e._updateID=0,this._gpu.device.queue.writeBuffer(t,e._updateOffset,n.buffer,n.byteOffset+e._updateOffset,(e._updateSize||n.byteLength)+3&-4)),t}destroyAll(){this._managedBuffers.removeAll()}onBufferUnload(e){e.off(`update`,this.updateBuffer,this),e.off(`change`,this.onBufferChange,this)}createGPUBuffer(e){let t=this._gpu.device.createBuffer(e.descriptor);return e._updateID=0,e.data&&(f(e.data.buffer,t.getMappedRange(),e.data.byteOffset,e.data.byteLength),t.unmap()),e._gpuData[this._renderer.uid]=new he(t),this._managedBuffers.add(e)&&(e.on(`update`,this.updateBuffer,this),e.on(`change`,this.onBufferChange,this)),t}onBufferChange(e){this._managedBuffers.remove(e),e._updateID=0,this.createGPUBuffer(e)}destroy(){this._managedBuffers.destroy(),this._renderer=null,this._gpu=null}};N.extension={type:[r.WebGPUSystem],name:`buffer`};var ge=class{constructor({minUniformOffsetAlignment:e}){this._minUniformOffsetAlignment=256,this.byteIndex=0,this._minUniformOffsetAlignment=e,this.data=new Float32Array(65535)}clear(){this.byteIndex=0}addEmptyGroup(e){if(e>this._minUniformOffsetAlignment/4)throw Error(`UniformBufferBatch: array is too large: ${e*4}`);let t=this.byteIndex,n=t+e*4;if(n=Math.ceil(n/this._minUniformOffsetAlignment)*this._minUniformOffsetAlignment,n>this.data.length*4)throw Error(`UniformBufferBatch: ubo batch got too big`);return this.byteIndex=n,t}addGroup(e){let t=this.addEmptyGroup(e.length);for(let n=0;n<e.length;n++)this.data[t/4+n]=e[n];return t}destroy(){this.data=null}},P=class{constructor(e){this._colorMaskCache=15,this._renderer=e}setMask(e){this._colorMaskCache!==e&&(this._colorMaskCache=e,this._renderer.pipeline.setColorMask(e))}destroy(){this._renderer=null,this._colorMaskCache=null}};P.extension={type:[r.WebGPUSystem],name:`colorMask`};function _e(e){let{userAgent:t}=C.get().getNavigator();if(!/AppleWebKit\//.test(t)||/Chrome\/|Chromium\//.test(t))return!1;if(!e)return!0;let n=/Version\/([\d.]+)/.exec(t)?.[1];return!n||!(e.from&&F(n,e.from)<0||e.before&&F(n,e.before)>=0)}function F(e,t){let n=e.split(`.`),r=t.split(`.`);for(let e=0;e<Math.max(n.length,r.length);e++){let t=(parseInt(n[e],10)||0)-(parseInt(r[e],10)||0);if(t)return t}return 0}var ve=new Set([`apple`,`arm`,`qualcomm`,`img-tec`]),I=class{constructor(e){this._renderer=e}async init(e){return this._initPromise||=(this._options=e,(e.gpu?Promise.resolve(e.gpu):this._createDeviceAndAdaptor(e)).then(e=>this._setGpu(e))),this._initPromise}_setGpu(e){this.gpu=e,this.extensions={transientAttachment:typeof GPUTextureUsage.TRANSIENT_ATTACHMENT==`number`&&!_e({from:`27.1`,before:`28`}),tileBased:ve.has(e.adapter?.info?.vendor)},this._options.gpu||e.device.lost.then(()=>this._restoreDevice()).catch(e=>h(`WebGPU device was lost and could not be restored`,e)),this._renderer.runners.contextChange.emit(this.gpu)}async _restoreDevice(){if(!this._renderer)return;let e=await this._createDeviceAndAdaptor(this._options);this._renderer?this._setGpu(e):e.device.destroy()}contextChange(e){this._renderer.gpu=e}async _createDeviceAndAdaptor(e){let t=await C.get().getNavigator().gpu.requestAdapter({powerPreference:e.powerPreference,forceFallbackAdapter:e.forceFallbackAdapter});if(!t)throw Error(`WebGPU not supported. No GPU adapter was returned by navigator.gpu.requestAdapter().`);let n=[`texture-compression-bc`,`texture-compression-astc`,`texture-compression-etc2`,`indirect-first-instance`,`bgra8unorm-storage`,`texture-formats-tier1`].filter(e=>t.features.has(e));return{adapter:t,device:await t.requestDevice({requiredFeatures:n,requiredLimits:{maxSampledTexturesPerShaderStage:t.limits.maxSampledTexturesPerShaderStage,maxSamplersPerShaderStage:t.limits.maxSamplersPerShaderStage,minUniformBufferOffsetAlignment:t.limits.minUniformBufferOffsetAlignment}})}}destroy(){this._options?.gpu||this.gpu?.device.destroy(),this.gpu=null,this.extensions=null,this._renderer=null}};I.extension={type:[r.WebGPUSystem],name:`device`},I.defaultOptions={powerPreference:void 0,forceFallbackAdapter:!1};var ye=class{constructor(e,t,n,r){this.gpuBundle=e,this.stateKey=t,this.device=n,this.label=r}},L=class{constructor(e){this._boundBindGroup=Object.create(null),this._boundVertexBuffer=Object.create(null),this._renderer=e;for(let e=0;e<16;e++)this._boundBindGroup[e]={bindGroup:null,program:null,entry:null}}renderStart(){this.commandFinished=new Promise(e=>{this._resolveCommandFinished=e}),this.commandEncoder=this._renderer.gpu.device.createCommandEncoder()}beginRenderPass(e){return this.endRenderPass(),this._clearCache(),this._passEncoder=this.commandEncoder.beginRenderPass(e.descriptor),this.renderPassEncoder=this._passEncoder,this._passEncoder}endRenderPass(){this._passEncoder&&this._passEncoder.end(),this.renderPassEncoder=null,this._passEncoder=null}beginBundle(e){if(this._passEncoder!==this.renderPassEncoder)throw Error(`Cannot begin a new render bundle while one is already being recorded.`);this._clearCache();let t=this._renderer.pipeline,n=t.getBundleDescriptor();n.label=e,this._bundleStateKey=t.bundleStateKey,this._bundleLabel=e,this.renderPassEncoder=this._gpu.device.createRenderBundleEncoder(n)}endBundle(){let e=this.renderPassEncoder;if(!e||!(`finish`in e))throw Error(`endBundle called without an active render bundle.`);let t=e.finish({label:this._bundleLabel});return this.renderPassEncoder=this._passEncoder,this._clearCache(),new ye(t,this._bundleStateKey,this._gpu.device,this._bundleLabel)}isBundleValid(e){return e?.stateKey===this._renderer.pipeline.bundleStateKey&&e.device===this._gpu.device}executeBundle(e){let t=[];if(Array.isArray(e))for(let n=0;n<e.length;n++)t[n]=this._getGpuBundle(e[n]);else t[0]=this._getGpuBundle(e);this._clearCache(),this._passEncoder.executeBundles(t)}_getGpuBundle(e){return this.isBundleValid(e)||h(`Render bundle ${e?.label??`(unlabeled)`} was recorded against a different render target state or on a lost device. Re-record it \u2014 replaying it will either be rejected by WebGPU or draw with the wrong winding. Check encoder.isBundleValid(bundle) before executing.`),e.gpuBundle}setViewport(e){this._passEncoder.setViewport(e.x,e.y,e.width,e.height,0,1)}setStencilReference(e){this._passEncoder.setStencilReference(e)}setPipelineFromGeometryProgramAndState(e,t,n,r,i){let a=this._renderer.pipeline.getPipeline(e,t,n,r,i);this.setPipeline(a)}setPipeline(e){this._boundPipeline!==e&&(this._boundPipeline=e,this.renderPassEncoder.setPipeline(e))}_setVertexBuffer(e,t){this._boundVertexBuffer[e]!==t&&(this._boundVertexBuffer[e]=t,this.renderPassEncoder.setVertexBuffer(e,this._renderer.buffer.updateBuffer(t)))}_setIndexBuffer(e){if(this._boundIndexBuffer===e)return;this._boundIndexBuffer=e;let t=e.data.BYTES_PER_ELEMENT===2?`uint16`:`uint32`;this.renderPassEncoder.setIndexBuffer(this._renderer.buffer.updateBuffer(e),t)}resetBindGroup(e){let t=this._boundBindGroup[e];t.bindGroup=null,t.program=null,t.entry=null}setBindGroup(e,t,n){let r=this._boundBindGroup[e];if(r.bindGroup===t&&r.program===n&&r.entry===t._gpuEntry)return;r.bindGroup=t,r.program=n,t._touch(this._renderer.gc.now);let i=this._renderer.bindGroup.getBindGroup(t,n,e);r.entry=t._gpuEntry,this.renderPassEncoder.setBindGroup(e,i)}setGeometry(e,t){let n=this._renderer.pipeline.getBufferNamesToBind(e,t);for(let t=0;t<n.length;t++)this._setVertexBuffer(t,e.attributes[n[t]].buffer);e.indexBuffer&&this._setIndexBuffer(e.indexBuffer)}_setShaderBindGroups(e,t){let n=e.gpuProgram,r=n.layout;for(let i=0;i<r.length;i++){let a=e.groups[i];r[i]&&a&&(t||this._syncBindGroup(a),this.setBindGroup(i,a,n))}}_syncBindGroup(e){let t=e.resources,n=e._resourceKeys;for(let e=0;e<n.length;e++){let r=t[n[e]];r&&r.isUniformGroup&&this._renderer.ubo.updateUniformGroup(r)}}draw(e){let{geometry:t,shader:n,state:r,topology:i,size:a,start:o,baseVertex:s,instanceCount:c,skipSync:l,firstInstance:u}=e;this.setPipelineFromGeometryProgramAndState(t,n.gpuProgram,r,i,n._overrides),this.setGeometry(t,n.gpuProgram),this._setShaderBindGroups(n,l),t.indexBuffer?this.renderPassEncoder.drawIndexed(a||t.indexCount||t.indexBuffer.data.length,c??t.instanceCount,o||0,s||0,u||0):this.renderPassEncoder.draw(a||t.vertexCount,c??t.instanceCount,o||0,u||0)}drawIndirect(e){let{geometry:t,shader:n,state:r,topology:i,skipSync:a,indirectBuffer:o,indirectOffset:s}=e;this.setPipelineFromGeometryProgramAndState(t,n.gpuProgram,r,i,n._overrides),this.setGeometry(t,n.gpuProgram),this._setShaderBindGroups(n,a),t.indexBuffer?this.renderPassEncoder.drawIndexedIndirect(o,s):this.renderPassEncoder.drawIndirect(o,s)}finishRenderPass(){this._passEncoder&&=(this._passEncoder.end(),this.renderPassEncoder=null,null)}postrender(){this.finishRenderPass(),this._gpu.device.queue.submit([this.commandEncoder.finish()]),this._resolveCommandFinished(),this.commandEncoder=null}_clearCache(){for(let e=0;e<16;e++){let t=this._boundBindGroup[e];t.bindGroup=null,t.program=null,t.entry=null,this._boundVertexBuffer[e]=null}this._boundIndexBuffer=null,this._boundPipeline=null}destroy(){this._renderer=null,this._gpu=null,this._boundBindGroup=null,this._boundVertexBuffer=null,this._boundIndexBuffer=null,this._boundPipeline=null,this.renderPassEncoder=null,this._passEncoder=null}contextChange(e){this._gpu=e}};L.extension={type:[r.WebGPUSystem],name:`encoder`,priority:1};var R=class{constructor(e){this.supportsOverrideConstants=!1,this._renderer=e}contextChange(){let e=this._renderer.device.gpu.device;this.maxTextures=Math.min(e.limits.maxSampledTexturesPerShaderStage,e.limits.maxSamplersPerShaderStage),this.maxBatchableTextures=this.maxTextures,this.minUniformBufferOffsetAlignment=e.limits.minUniformBufferOffsetAlignment,this._detectOverrideConstantsSupport(e)}_detectOverrideConstantsSupport(e){e.pushErrorScope(`validation`);let t=e.createShaderModule({code:`override TEST_VALUE: f32 = 0.0;
|
|
2
|
+
@compute @workgroup_size(1) fn main() {}`});e.createComputePipeline({layout:`auto`,compute:{module:t,entryPoint:`main`,constants:{TEST_VALUE:1}}}),e.popErrorScope().then(e=>{this.supportsOverrideConstants=!e})}destroy(){}};R.extension={type:[r.WebGPUSystem],name:`limits`};var z=class{constructor(e){this._renderer=e,e.renderTarget.onRenderTargetChange.add(this)}contextChange(){this._activeGpuRenderTarget=null}onRenderTargetChange(e){let t=this._renderer.renderTarget.getGpuRenderTarget(e);this._activeRenderTarget=e,this._activeGpuRenderTarget=t,this.setStencilMode(t.stencilMode,t.stencilReference)}setStencilMode(e,t){let n=this._activeGpuRenderTarget??=this._renderer.renderTarget.getGpuRenderTarget(this._activeRenderTarget);n.stencilMode=e,n.stencilReference=t;let r=this._renderer;r.pipeline.setStencilMode(e),r.encoder.setStencilReference(t)}destroy(){this._renderer.renderTarget.onRenderTargetChange.remove(this),this._renderer=null,this._activeRenderTarget=null,this._activeGpuRenderTarget=null}};z.extension={type:[r.WebGPUSystem],name:`stencil`};var B={i32:{align:4,size:4},u32:{align:4,size:4},f32:{align:4,size:4},f16:{align:2,size:2},"vec2<i32>":{align:8,size:8},"vec2<u32>":{align:8,size:8},"vec2<f32>":{align:8,size:8},"vec2<f16>":{align:4,size:4},"vec3<i32>":{align:16,size:12},"vec3<u32>":{align:16,size:12},"vec3<f32>":{align:16,size:12},"vec3<f16>":{align:8,size:6},"vec4<i32>":{align:16,size:16},"vec4<u32>":{align:16,size:16},"vec4<f32>":{align:16,size:16},"vec4<f16>":{align:8,size:8},"mat2x2<f32>":{align:8,size:16},"mat2x2<f16>":{align:4,size:8},"mat3x2<f32>":{align:8,size:24},"mat3x2<f16>":{align:4,size:12},"mat4x2<f32>":{align:8,size:32},"mat4x2<f16>":{align:4,size:16},"mat2x3<f32>":{align:16,size:32},"mat2x3<f16>":{align:8,size:16},"mat3x3<f32>":{align:16,size:48},"mat3x3<f16>":{align:8,size:24},"mat4x3<f32>":{align:16,size:64},"mat4x3<f16>":{align:8,size:32},"mat2x4<f32>":{align:16,size:32},"mat2x4<f16>":{align:8,size:16},"mat3x4<f32>":{align:16,size:48},"mat3x4<f16>":{align:8,size:24},"mat4x4<f32>":{align:16,size:64},"mat4x4<f16>":{align:8,size:32}};function be(e){let t=e.map(e=>({data:e,offset:0,size:0})),n=0;for(let e=0;e<t.length;e++){let r=t[e],i=B[r.data.type].size,a=B[r.data.type].align;if(!B[r.data.type])throw Error(`[Pixi.js] WebGPU UniformBuffer: Unknown type ${r.data.type}`);r.data.size>1&&(i=Math.max(i,a)*r.data.size),n=Math.ceil(n/a)*a,r.size=i,r.offset=n,n+=i}return n=Math.ceil(n/16)*16,{uboElements:t,size:n}}function xe(e,t){let{size:n,align:r}=B[e.data.type],i=(Math.max(n,r)-n)/4,a=e.data.type.indexOf(`i32`)>=0?`dataInt32`:`data`;return`
|
|
3
|
+
v = uv.${e.data.name};
|
|
4
|
+
${t===0?``:`offset += ${t};`}
|
|
5
|
+
|
|
6
|
+
arrayOffset = offset;
|
|
7
|
+
|
|
8
|
+
t = 0;
|
|
9
|
+
|
|
10
|
+
for(var i=0; i < ${e.data.size}; i++)
|
|
11
|
+
{
|
|
12
|
+
for(var j = 0; j < ${n/4}; j++)
|
|
13
|
+
{
|
|
14
|
+
${a}[arrayOffset++] = v[t++];
|
|
15
|
+
}
|
|
16
|
+
${i===0?``:`arrayOffset += ${i};`}
|
|
17
|
+
}
|
|
18
|
+
`}function Se(e){return te(e,ie,xe)}var V=class extends se{constructor(){super({createUboElements:be,generateUboSync:Se})}};V.extension={type:[r.WebGPUSystem],name:`ubo`};var H=128,U=class{constructor(e){this._bindGroupHash=Object.create(null),this._buffers=[],this._bindGroups=[],this._bufferResources=[],this._renderer=e,this._batchBuffer=new ge({minUniformOffsetAlignment:H});let t=256/H;for(let e=0;e<t;e++)this._buffers.push(new ee({data:this._batchBuffer.data,usage:d.UNIFORM|d.COPY_DST}))}renderEnd(){this._uploadBindGroups(),this._resetBindGroups()}_resetBindGroups(){this._bindGroupHash=Object.create(null),this._batchBuffer.clear()}getUniformBindGroup(e,t){if(!t&&this._bindGroupHash[e.uid])return this._bindGroupHash[e.uid];this._renderer.ubo.ensureUniformGroup(e);let n=e.buffer.data,r=this._batchBuffer.addEmptyGroup(n.length);return this._renderer.ubo.syncUniformGroup(e,this._batchBuffer.data,r/4),this._bindGroupHash[e.uid]=this._getBindGroup(r/H),this._bindGroupHash[e.uid]}getUboResource(e){this._renderer.ubo.updateUniformGroup(e);let t=e.buffer.data,n=this._batchBuffer.addGroup(t);return this._getBufferResource(n/H)}getArrayBindGroup(e){let t=this._batchBuffer.addGroup(e);return this._getBindGroup(t/H)}getArrayBufferResource(e){let t=this._batchBuffer.addGroup(e)/H;return this._getBufferResource(t)}_getBufferResource(e){if(!this._bufferResources[e]){let t=this._buffers[e%2];this._bufferResources[e]=new oe({buffer:t,offset:(e/2|0)*256,size:H})}return this._bufferResources[e]}_getBindGroup(e){if(!this._bindGroups[e]){let t=new u({0:this._getBufferResource(e)});this._bindGroups[e]=t}return this._bindGroups[e]}_uploadBindGroups(){let e=this._renderer.buffer,t=this._buffers[0];t.update(this._batchBuffer.byteIndex),e.updateBuffer(t);let n=this._batchBuffer.data,r=this._batchBuffer.byteIndex-H;if(!(r<=0))for(let t=1;t<this._buffers.length;t++)this._renderer.gpu.device.queue.writeBuffer(e.getGPUBuffer(this._buffers[t]),0,n.buffer,n.byteOffset+H,r)}destroy(){for(let e=0;e<this._bindGroups.length;e++)this._bindGroups[e]?.destroy();this._bindGroups=null,this._bindGroupHash=null;for(let e=0;e<this._buffers.length;e++)this._buffers[e].destroy();this._buffers=null;for(let e=0;e<this._bufferResources.length;e++)this._bufferResources[e].destroy();this._bufferResources=null,this._batchBuffer.destroy(),this._renderer=null}};U.extension={type:[r.WebGPUPipes],name:`uniformBatch`};var W={"point-list":0,"line-list":1,"line-strip":2,"triangle-list":3,"triangle-strip":4},Ce=new a({}),we={"depth24plus-stencil8":{depth:!0,stencil:!0,index:1},depth24plus:{depth:!0,stencil:!1,index:2},depth32float:{depth:!0,stencil:!1,index:3},"depth32float-stencil8":{depth:!0,stencil:!0,index:4},depth16unorm:{depth:!0,stencil:!1,index:5},stencil8:{depth:!1,stencil:!0,index:6}},Te={depth:!1,stencil:!1,index:0};function G(e,t){for(let[n,r]of Object.entries(t)){let t=RegExp(`override\\s+${n}\\s*:\\s*(\\w+)\\s*(?:=[^;]*)?;`);e=e.replace(t,(e,t)=>{let i;return i=t===`u32`?`${Math.trunc(r)}u`:t===`i32`?`${Math.trunc(r)}`:Number.isInteger(r)?`${r}.0`:`${r}`,`const ${n}: ${t} = ${i};`})}return e}function K(e,t,n,r,i,a){return e*0x200000000000+t*536870912+a*16384+(n<<8)+(r<<3)+i}var q=Object.create(null),Ee=0;function J(e){let t=q[e];return t===void 0&&(t=q[e]=Ee++),t}function De(e,t,n,r,i,a,o){return i<<13|o<<12|r<<9|n<<5|e<<2|a<<1|t}function Oe(e,t,n,r,i,a){return a<<7|i<<6|r<<5|n<<4|t<<1|e}var ke=class{constructor(e){this._moduleCache=Object.create(null),this._bufferLayoutsCache=Object.create(null),this._bindingNamesCache=Object.create(null),this._pipeCache=new Map,this._pipeStateCaches=Object.create(null),this._colorMask=15,this._multisampleCount=1,this._colorFormats=[`bgra8unorm`],this._colorFormatId=J(`bgra8unorm`),this._depthStencilFormat=`depth24plus-stencil8`,this._depthStencilFormatData=Te,this._depthReadOnly=!1,this._stencilReadOnly=!1,this._invertFrontFace=!1,this._renderer=e}contextChange(e){this._gpu=e,this._moduleCache=Object.create(null),this._pipeStateCaches=Object.create(null),this.setStencilMode(w.DISABLED),this._updatePipeHash()}setMultisampleCount(e){this._multisampleCount!==e&&(this._multisampleCount=e,this._updatePipeHash())}setRenderTarget(e){let t=e.colorAttachments[0]?.texture;this._multisampleCount=t?.source.antialias?4:1,this._setColorFormats(e),this._depthStencilFormat=e.depthStencilAttachment?.texture.format,this._depthStencilFormatData=we[this._depthStencilFormat]||Te,this._depthReadOnly=e.depthStencilAttachment?.depthReadOnly??!1,this._stencilReadOnly=e.depthStencilAttachment?.stencilReadOnly??this._depthReadOnly,this._invertFrontFace=this._renderer.renderTarget.isFrontFaceInverted(e,e.flipY),this._updatePipeHash()}setColorMask(e){this._colorMask!==e&&(this._colorMask=e,this._updatePipeHash())}setStencilMode(e){this._stencilMode!==e&&(this._stencilMode=e,this._stencilState=ne[e],this._updatePipeHash())}get bundleStateKey(){let e=this._depthStencilFormatData;return Oe(this._multisampleCount===1?0:1,e.index,e.depth&&this._depthReadOnly?1:0,e.stencil&&this._stencilReadOnly?1:0,+!!this._invertFrontFace,this._colorFormatId)}getBundleDescriptor(){let e={colorFormats:this._colorFormats.slice(),sampleCount:this._multisampleCount},t=this._depthStencilFormatData;return(t.depth||t.stencil)&&(e.depthStencilFormat=this._depthStencilFormat,t.depth&&this._depthReadOnly&&(e.depthReadOnly=!0),t.stencil&&this._stencilReadOnly&&(e.stencilReadOnly=!0)),e}setPipeline(e,t,n,r){let i=this.getPipeline(e,t,n);r.setPipeline(i)}getPipelineKey(e,t,n,r,i){return e._layoutKey||(O(e,t.attributeData),this._generateBufferKey(e)),K(e._layoutKey,t._layoutKey,n.data,n._blendModeId,W[r],i.id)}getPipeline(e,t,n,r,i){e._layoutKey||(O(e,t.attributeData),this._generateBufferKey(e)),r||=e.topology,i||=Ce;let a=K(e._layoutKey,t._layoutKey,n.data,n._blendModeId,W[r],i.id),o=this._pipeCache.get(a);return o||(o=this._createPipeline(e,t,n,r,i),this._pipeCache.set(a,o)),o}_createPipeline(e,t,n,r,i){let a=this._gpu.device,o=this._createVertexBufferLayouts(e,t),s=this._renderer.state.getColorTargets(n,this._colorFormats),c=this._stencilMode===w.RENDERING_MASK_ADD?0:this._colorMask;this._applyFragmentOutputs(s,t,c);let l=this._renderer.shader.getProgramData(t).pipeline,u=Object.keys(i.data).length>0,d=t.vertex.source,f=t.fragment.source,p;u&&(this._renderer.limits.supportsOverrideConstants?p=i.data:(d=G(d,i.data),f=G(f,i.data)));let m={vertex:{module:this._getModule(d),entryPoint:t.vertex.entryPoint,constants:p,buffers:o},fragment:{module:this._getModule(f),entryPoint:t.fragment.entryPoint,targets:s,constants:p},primitive:{topology:r,cullMode:n.culling?`back`:`none`,frontFace:n.clockwiseFrontFace===this._invertFrontFace?`ccw`:`cw`},layout:l,multisample:{count:this._multisampleCount},label:t.name?`PIXI Pipeline (${t.name})`:`PIXI Pipeline`};if(this._depthStencilFormatData.depth||this._depthStencilFormatData.stencil){let e=this._depthStencilFormatData;m.depthStencil={...this._stencilState,format:this._depthStencilFormat,depthWriteEnabled:e.depth?n.depthMask&&!this._depthReadOnly:!1,depthCompare:e.depth&&n.depthTest?`less`:`always`}}return a.createRenderPipeline(m)}_getModule(e){return this._moduleCache[e]||this._createModule(e)}_createModule(e){let t=this._gpu.device;return this._moduleCache[e]=t.createShaderModule({code:e}),this._moduleCache[e]}_generateBufferKey(e){let t=[],n=0,r=Object.keys(e.attributes).sort();for(let i=0;i<r.length;i++){let a=e.attributes[r[i]];t[n++]=a.offset,t[n++]=a.format,t[n++]=a.stride,t[n++]=a.instance}let i=t.join(`|`);return e._layoutKey=p(i,`geometry`),e._layoutKey}_generateAttributeLocationsKey(e){let t=[],n=0,r=Object.keys(e.attributeData).sort();for(let i=0;i<r.length;i++){let a=e.attributeData[r[i]];t[n++]=a.location}let i=t.join(`|`);return e._attributeLocationsKey=p(i,`programAttributes`),e._attributeLocationsKey}getBufferNamesToBind(e,t){e._layoutKey||(O(e,t.attributeData),this._generateBufferKey(e)),t._attributeLocationsKey||this._generateAttributeLocationsKey(t);let n=e._layoutKey<<16|t._attributeLocationsKey;if(this._bindingNamesCache[n])return this._bindingNamesCache[n];let r=this._createVertexBufferLayouts(e,t),i=[],a=t.attributeData;for(let e=0;e<r.length;e++){let t=Object.values(r[e].attributes)[0].shaderLocation;for(let n in a)if(a[n].location===t){i[e]=n;break}}return this._bindingNamesCache[n]=i,i}_createVertexBufferLayouts(e,t){t._attributeLocationsKey||this._generateAttributeLocationsKey(t);let n=e._layoutKey<<16|t._attributeLocationsKey;if(this._bufferLayoutsCache[n])return this._bufferLayoutsCache[n];let r=[];return e.buffers.forEach(n=>{let i={arrayStride:0,stepMode:`vertex`,attributes:[]},a=i.attributes;for(let r in t.attributeData){let o=e.attributes[r];(o.divisor??1)!==1&&h(`Attribute ${r} has an invalid divisor value of '${o.divisor}'. WebGPU only supports a divisor value of 1`),o.buffer===n&&(i.arrayStride=o.stride,i.stepMode=o.instance?`instance`:`vertex`,a.push({shaderLocation:t.attributeData[r].location,offset:o.offset,format:o.format}))}a.length&&r.push(i)}),this._bufferLayoutsCache[n]=r,r}_setColorFormats(e){let t=e.colorAttachments,n=this._colorFormats;n.length=t.length;for(let e=0;e<t.length;e++)n[e]=t[e].texture.format;this._colorFormatId=J(n.join(`|`))}_applyFragmentOutputs(e,t,n){let r=t.fragmentOutputMask??-1;for(let t=0;t<e.length;t++){let i=!!(r&1<<t);e[t].writeMask=i?n:0,i||(e[t].blend=void 0)}}_updatePipeHash(){var e;let t=De(this._stencilMode,this._multisampleCount===1?0:1,this._colorMask,this._depthStencilFormatData.index,this._colorFormatId,+!!this._depthReadOnly,+!!this._invertFrontFace);this._pipeCache=(e=this._pipeStateCaches)[t]??(e[t]=new Map)}destroy(){this._bufferLayoutsCache=null,this._pipeCache=null,this._gpu=null,this._renderer=null,this._bindingNamesCache=null,this._pipeStateCaches=null,this._moduleCache=null}};ke.extension={type:[r.WebGPUSystem],name:`pipeline`};var Ae=class{constructor(e){this._variants=Object.create(null),this._bindGroups=new WeakMap,this._depthStencilAttachment={view:null,depthLoadOp:`load`,depthStoreOp:`store`},this._passDescriptor={label:`depth-copy`,colorAttachments:[],depthStencilAttachment:this._depthStencilAttachment},this._renderer=e,this._device=e.gpu.device}copy(e,t,n,r,i,a){var o,s;let c=this._renderer.texture.getGpuSource(t),l=c.sampleCount,u=(o=this._variants)[l]??(o[l]=this._createVariant(l)),d=this._bindGroups.get(c);d||(d=this._device.createBindGroup({layout:u.layout,entries:[{binding:0,resource:c.createView({aspect:`depth-only`})}]}),this._bindGroups.set(c,d));let f=n.format,p=this._depthStencilAttachment,m=f.includes(`stencil`);p.view=this._renderer.texture.getTextureView(n),p.stencilLoadOp=m?`load`:void 0,p.stencilStoreOp=m?`store`:void 0;let h=e.beginRenderPass(this._passDescriptor);p.view=null;let g=(r.x-a.x+32768|r.y-a.y+32768<<16)>>>0;h.setViewport(a.x,a.y,i.width,i.height,0,1),h.setPipeline((s=u.pipelines)[f]??(s[f]=this._createPipeline(f,u))),h.setBindGroup(0,d),h.draw(3,1,0,g),h.end()}_createVariant(e){let t=e>1,n=this._device.createBindGroupLayout({entries:[{binding:0,visibility:GPUShaderStage.FRAGMENT,texture:{sampleType:`depth`,multisampled:t}}]}),r=this._device.createShaderModule({label:`depth-copy`,code:`
|
|
19
|
+
@group(0) @binding(0) var source: ${t?`texture_depth_multisampled_2d`:`texture_depth_2d`};
|
|
20
|
+
|
|
21
|
+
struct Varyings {
|
|
22
|
+
@builtin(position) position: vec4<f32>,
|
|
23
|
+
@location(0) @interpolate(flat) offset: vec2<i32>,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
@vertex
|
|
27
|
+
fn vertexMain(@builtin(vertex_index) i: u32, @builtin(instance_index) packed: u32) -> Varyings {
|
|
28
|
+
let pos = array<vec2<f32>, 3>(vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0));
|
|
29
|
+
let offset = vec2<i32>(i32(packed & 0xffffu), i32(packed >> 16u)) - vec2<i32>(32768);
|
|
30
|
+
|
|
31
|
+
return Varyings(vec4<f32>(pos[i], 0.0, 1.0), offset);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@fragment
|
|
35
|
+
fn fragmentMain(varyings: Varyings) -> @builtin(frag_depth) f32 {
|
|
36
|
+
return textureLoad(source, vec2<i32>(varyings.position.xy) + varyings.offset, 0);
|
|
37
|
+
}
|
|
38
|
+
`});return{layout:n,pipelineLayout:this._device.createPipelineLayout({bindGroupLayouts:[n]}),module:r,pipelines:Object.create(null)}}_createPipeline(e,t){return this._device.createRenderPipeline({label:`depth-copy`,layout:t.pipelineLayout,vertex:{module:t.module,entryPoint:`vertexMain`},fragment:{module:t.module,entryPoint:`fragmentMain`,targets:[]},depthStencil:{format:e,depthWriteEnabled:!0,depthCompare:`always`}})}},je=class{constructor(e){this._pipelines=Object.create(null),this._modules=Object.create(null),this._bindGroups=new WeakMap,this._renderer=e,this._device=e.gpu.device,this._layout=this._device.createBindGroupLayout({entries:[{binding:0,visibility:GPUShaderStage.FRAGMENT,texture:{sampleType:`unfilterable-float`}}]}),this._pipelineLayout=this._device.createPipelineLayout({bindGroupLayouts:[this._layout]})}copy(e,t,n,r){var i;let a=this._renderer.renderTarget.getGpuRenderTarget(t),o=t.colorAttachments[n].texture;(i=a.msaaBackTextures)[n]??(i[n]=new S({label:`msaa-back-texture`,width:o.width,height:o.height,resolution:o._resolution,format:o.format,autoGenerateMipmaps:!1,autoGarbageCollect:!0}));let s=this._renderer.texture.getGpuSource(a.msaaBackTextures[n]);e.copyTextureToTexture({texture:r},{texture:s},{width:Math.min(r.width,s.width),height:Math.min(r.height,s.height)})}draw(e,t,n){var r;let i=this._renderer.renderTarget.getGpuRenderTarget(t),a=this._renderer.texture.getGpuSource(i.msaaBackTextures[n]),o=t.depthStencilAttachment?.texture.format??``;for(let e of t.colorAttachments)o+=`|${e.texture.format}`;let s=(r=this._pipelines)[o]??(r[o]=[]),c=this._bindGroups.get(a);c||(c=this._device.createBindGroup({layout:this._layout,entries:[{binding:0,resource:a.createView()}]}),this._bindGroups.set(a,c)),e.setPipeline(s[n]??(s[n]=this._createPipeline(t,n))),e.setBindGroup(0,c),e.draw(3)}_getModule(e){var t;return(t=this._modules)[e]??(t[e]=this._device.createShaderModule({label:`msaa-restore`,code:`
|
|
39
|
+
@group(0) @binding(0) var resolved: texture_2d<f32>;
|
|
40
|
+
|
|
41
|
+
@vertex
|
|
42
|
+
fn vertexMain(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
|
|
43
|
+
let pos = array<vec2<f32>, 3>(vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0));
|
|
44
|
+
|
|
45
|
+
return vec4<f32>(pos[i], 0.0, 1.0);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@fragment
|
|
49
|
+
fn fragmentMain(@builtin(position) position: vec4<f32>) -> @location(${e}) vec4<f32> {
|
|
50
|
+
return textureLoad(resolved, vec2<i32>(position.xy), 0);
|
|
51
|
+
}
|
|
52
|
+
`})),this._modules[e]}_createPipeline(e,t){let n=e.depthStencilAttachment?.texture.format,r;n&&(r={format:n},n.includes(`depth`)&&(r.depthWriteEnabled=!1,r.depthCompare=`always`));let i=this._getModule(t);return this._device.createRenderPipeline({label:`msaa-restore`,layout:this._pipelineLayout,vertex:{module:i,entryPoint:`vertexMain`},fragment:{module:i,entryPoint:`fragmentMain`,targets:e.colorAttachments.map((e,n)=>({format:e.texture.format,writeMask:n===t?GPUColorWrite.ALL:0}))},depthStencil:r,multisample:{count:4}})}},Me=class{constructor(){this.contexts=[],this.canvasTextures=[],this.canvasMsaaTextures=[],this.transient=!1,this.msaaSamples=1,this.msaaBackTextures=[],this.stencilMode=w.DISABLED,this.stencilReference=0,this.maskStackIndex=0}},Ne=class{constructor(e,t){let{tileBased:n,transientAttachment:r}=e.device.extensions;this.transient=n||t.transient;let i=GPUTextureUsage.RENDER_ATTACHMENT;this.transient&&r&&(i|=GPUTextureUsage.TRANSIENT_ATTACHMENT),this.texture=e.gpu.device.createTexture({label:t.label,size:{width:t.pixelWidth,height:t.pixelHeight,depthOrArrayLayers:t.depthOrArrayLayers},format:t.format,sampleCount:4,mipLevelCount:1,usage:i}),this.view=this.texture.createView()}destroy(){this.texture.destroy(),this.texture=null,this.view=null}},Pe={bgra8unorm:!0,rgba8unorm:!0,rgba16float:!0};function Fe(e){if(Pe[e])return e;let t=navigator.gpu.getPreferredCanvasFormat();return h(`[WebGPU] CanvasSource format '${e}' is not a valid GPUCanvasContext format. Falling back to '${t}'. Allowed formats are: bgra8unorm, rgba8unorm, rgba16float.`),t}var Ie=class{constructor(){this._activePass=null}init(e,t){this._renderer=e,this._renderTargetSystem=t,e.runners.contextChange.add(this),e.runners.destroy.add(this)}contextChange(){this._msaaRestore=null,this._depthCopy=null}destroy(){this._msaaRestore=null,this._depthCopy=null}copyToTexture(e,t,n,r,i){let a=this._renderer;this.finishRenderPass();let o=this._getGpuColorTexture(e),s=a.texture.getGpuSource(t.source);return a.encoder.commandEncoder.copyTextureToTexture({texture:o,origin:n},{texture:s,origin:i},r),t}copyDepthTexture(e,t,n,r,i){let a=this._renderer;this.finishRenderPass();let o=e.depthStencilAttachment.texture,s=t.source,c=a.texture.getGpuSource(o),l=a.texture.getGpuSource(s),u=c.sampleCount>1||n.x!==0||n.y!==0||i.x!==0||i.y!==0||r.width!==c.width||r.height!==c.height||r.width!==l.width||r.height!==l.height;if(u){if(o.transient){h(`[RenderTargetSystem] copyDepthTexture: the source target is transient, so its multisampled depth is discarded at the end of each pass and there is nothing to copy`);return}if(!o.format.includes(`depth`)||!s.format.includes(`depth`)){h(`[RenderTargetSystem] copyDepthTexture: copying from an antialiased target or a region copies depth only, so both textures need a depth aspect (got '${o.format}' to '${s.format}')`);return}}let d=a.encoder.commandEncoder===null,f=d?a.gpu.device.createCommandEncoder():a.encoder.commandEncoder;u?(this._depthCopy??=new Ae(a),this._depthCopy.copy(f,o,s,n,r,i)):f.copyTextureToTexture({texture:c},{texture:l},r),d&&a.gpu.device.queue.submit([f.finish()])}startRenderPass(t,n=!0,r,i,a=0,o=0){let s=this._renderTargetSystem.getGpuRenderTarget(t);if(o!==0&&s.msaa)throw Error(`[RenderTargetSystem] Rendering to array layers is not supported with MSAA render targets.`);if(a>0&&s.msaa)throw Error(`[RenderTargetSystem] Rendering to mip levels is not supported with MSAA render targets.`);let c=n;typeof n==`boolean`&&(c=n?e.ALL:e.NONE),(t.stencil||t.depth)&&!t.depthStencilAttachment&&t.ensureDepthStencilTexture();let l=!!t.depthStencilAttachment,u=this._activePass,d=u!==null&&u.renderTarget===t&&u.mipLevel===a&&u.layer===o&&u.depthStencil===l&&this._renderer.encoder.renderPassEncoder!==null&&c===e.NONE;if(this._renderer.pipeline.setRenderTarget(t),d){this._renderer.encoder.setViewport(i);return}let{descriptor:f,restore:p}=this._buildDescriptor(t,c,r,a,o);s.descriptor=f;let m=this._renderer.encoder;p?(m.endRenderPass(),this._beginRestoredPass(t,p,m.commandEncoder,()=>m.beginRenderPass(s))):m.beginRenderPass(s),m.setViewport(i),this._activePass={renderTarget:t,mipLevel:a,layer:o,depthStencil:l}}finishRenderPass(){this._renderer.encoder.endRenderPass(),this._activePass=null}_beginRestoredPass(e,t,n,r){let i=this._msaaRestore??=new je(this._renderer);for(let r of t)i.copy(n,e,r,this._getGpuColorTexture(e,r));let a=r();for(let n of t)i.draw(a,e,n);return a}_getGpuColorTexture(e,t=0){if(e.colorAttachments.length===0)throw Error(`[GpuRenderTargetAdaptor] cannot get gpu color texture from a depth-only render target`);let n=e.colorAttachments[t].texture;return n instanceof o&&n._gpuContext?n._gpuContext.getCurrentTexture():this._renderer.texture.getGpuSource(n)}getDescriptor(e,t,n,r=0,i=0){return this._buildDescriptor(e,t,n,r,i).descriptor}_buildDescriptor(t,n,r,i=0,a=0){let s=null;typeof n==`boolean`&&(n=n?e.ALL:e.NONE);let c=this._renderTargetSystem,l=c.getGpuRenderTarget(t),u=t.colorAttachments.map((t,u)=>{var d;let f=t.texture,p=f instanceof o?f._gpuContext:null,m,h,g=!1;if(p){if(a!==0)throw Error(`[RenderTargetSystem] Rendering to array layers is not supported for canvas targets.`);let e=p.getCurrentTexture();g=l.canvasTextures[u]!==e,l.canvasTextures[u]=e,m=e.createView(t.viewDescriptor)}else m=this._renderer.texture.getTextureRenderTargetView(t.texture,i,a,t.viewDescriptor);let _=null;f.antialias&&(_=p?(d=l.canvasMsaaTextures)[u]??(d[u]=new Ne(this._renderer,f)):this._renderer.texture.getMsaaTexture(f),h=m,m=_.view);let v=(n&e.COLOR)!==0&&t.loadOp!==`load`?`clear`:`load`,y=!!_?.transient,b=y&&v!==`clear`,x=b&&!g;b&&(v=`clear`,x&&(s??=[]).push(u));let S={view:m,resolveTarget:h,storeOp:y?`discard`:t.storeOp??`store`,loadOp:v};f.dimension===`3d`&&(S.depthSlice=a),b?S.clearValue=[0,0,0,0]:v===`clear`&&(S.clearValue=t.clearValue??r??c.defaultClearColor);for(let e in t)e!==`texture`&&e!==`viewDescriptor`&&e!==`clearValue`&&e!==`loadOp`&&e!==`storeOp`&&(S[e]=t[e]);return S}),d;if(t.depthStencilAttachment){this._prepareDepthStencil(t,l);let r=t.depthStencilAttachment,o=r.texture.format.includes(`stencil`),s=r.texture.format.includes(`depth`),c=l.transient?`discard`:`store`,u=l.transient?`clear`:`load`;d={view:this._renderer.texture.getTextureRenderTargetView(r.texture,i,a,r.viewDescriptor)};let f=r.depthReadOnly??!1,p=r.stencilReadOnly??f;o&&!p?(d.stencilLoadOp=n&e.STENCIL?`clear`:r.stencilLoadOp??u,d.stencilStoreOp=r.stencilStoreOp??c,d.stencilLoadOp===`clear`&&(d.stencilClearValue=r.stencilClearValue??0)):o&&p&&(d.stencilReadOnly=!0),s&&!f?(d.depthLoadOp=n&e.DEPTH?`clear`:r.depthLoadOp??u,d.depthStoreOp=r.depthStoreOp??c,d.depthLoadOp===`clear`&&(d.depthClearValue=r.depthClearValue??1)):s&&f&&(d.depthReadOnly=!0);for(let e in r)e!==`texture`&&e!==`viewDescriptor`&&e!==`stencilLoadOp`&&e!==`stencilStoreOp`&&e!==`stencilClearValue`&&e!==`stencilReadOnly`&&e!==`depthLoadOp`&&e!==`depthStoreOp`&&e!==`depthClearValue`&&e!==`depthReadOnly`&&(d[e]=r[e])}return{descriptor:{colorAttachments:u,depthStencilAttachment:d,label:t.label},restore:s}}clear(e,t=!0,n,r,i=0,a=0){if(!t)return;let{gpu:o,encoder:s}=this._renderer,c=o.device;if(s.commandEncoder===null){let o=c.createCommandEncoder(),{descriptor:s,restore:l}=this._buildDescriptor(e,t,n,i,a),u=()=>o.beginRenderPass(s),d=l?this._beginRestoredPass(e,l,o,u):u();d.setViewport(r.x,r.y,r.width,r.height,0,1),d.end();let f=o.finish();c.queue.submit([f])}else this.startRenderPass(e,t,n,r,i,a)}initGpuRenderTarget(e){e.isRoot=!0;let t=new Me;return t.transient=!!e.colorTexture?.antialias&&!!e.colorTexture.transient,e.colorAttachments.forEach((e,n)=>{let r=e.texture;if(r instanceof o){let e=r._gpuContext??=r.resource.getContext(`webgpu`),i=r.transparent?`premultiplied`:`opaque`,a=Fe(r.format);try{e.configure({device:this._renderer.gpu.device,usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST|GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.COPY_SRC,format:a,alphaMode:i,...a===`rgba16float`?{toneMapping:{mode:`extended`}}:{}})}catch(e){console.error(e)}t.contexts[n]=e}t.msaa||=r.source.antialias}),t.msaa&&(t.msaaSamples=4),t}_prepareDepthStencil(e,t){let n=e.depthStencilAttachment?.texture;n&&t.msaa&&(n.sampleCount=4,n.transient=t.transient)}destroyGpuRenderTarget(e){e.contexts.forEach(e=>{e.unconfigure()}),e.canvasMsaaTextures.forEach(e=>{e.destroy()}),e.msaaBackTextures.forEach(e=>{e?.destroy()}),e.canvasMsaaTextures.length=0,e.msaaBackTextures.length=0,e.contexts.length=0}ensureDepthStencilTexture(e){this._prepareDepthStencil(e,this._renderTargetSystem.getGpuRenderTarget(e))}resizeGpuRenderTarget(e){let t=this._renderTargetSystem.getGpuRenderTarget(e);t.width=e.width,t.height=e.height,t.msaa&&e.colorAttachments.forEach((e,n)=>{let r=e.texture;r instanceof o&&r.antialias&&(t.canvasMsaaTextures[n]?.destroy(),t.canvasMsaaTextures[n]=null),t.msaaBackTextures[n]?.resize(r.width,r.height,r._resolution)})}},Le=class extends fe{constructor(e){super(e),this.adaptor=new Ie,this.adaptor.init(e,this)}};Le.extension={type:[r.WebGPUSystem],name:`renderTarget`};var Re=class{constructor(){this._gpuProgramData=Object.create(null)}contextChange(e){this._gpu=e,this._gpuProgramData=Object.create(null)}getProgramData(e){return this._gpuProgramData[e._layoutKey]||this._createGPUProgramData(e)}_createGPUProgramData(e){let t=this._gpu.device,n=e.gpuLayout.map(e=>t.createBindGroupLayout({entries:e})),r={bindGroupLayouts:n};return this._gpuProgramData[e._layoutKey]={bindGroups:n,pipeline:t.createPipelineLayout(r)},this._gpuProgramData[e._layoutKey]}destroy(){this._gpu=null,this._gpuProgramData=null}};Re.extension={type:[r.WebGPUSystem],name:`shader`};var Y={};Y.normal={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`}},Y.add={alpha:{srcFactor:`one`,dstFactor:`one`,operation:`add`},color:{srcFactor:`one`,dstFactor:`one`,operation:`add`}},Y.multiply={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`dst`,dstFactor:`one-minus-src-alpha`,operation:`add`}},Y.screen={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`one`,dstFactor:`one-minus-src`,operation:`add`}},Y.overlay={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`one`,dstFactor:`one-minus-src`,operation:`add`}},Y.none={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`zero`,dstFactor:`zero`,operation:`add`}},Y[`normal-npm`]={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`src-alpha`,dstFactor:`one-minus-src-alpha`,operation:`add`}},Y[`add-npm`]={alpha:{srcFactor:`one`,dstFactor:`one`,operation:`add`},color:{srcFactor:`src-alpha`,dstFactor:`one`,operation:`add`}},Y[`screen-npm`]={alpha:{srcFactor:`one`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`src-alpha`,dstFactor:`one-minus-src`,operation:`add`}},Y.erase={alpha:{srcFactor:`zero`,dstFactor:`one-minus-src-alpha`,operation:`add`},color:{srcFactor:`zero`,dstFactor:`one-minus-src-alpha`,operation:`add`}},Y.min={alpha:{srcFactor:`one`,dstFactor:`one`,operation:`min`},color:{srcFactor:`one`,dstFactor:`one`,operation:`min`}},Y.max={alpha:{srcFactor:`one`,dstFactor:`one`,operation:`max`},color:{srcFactor:`one`,dstFactor:`one`,operation:`max`}};var ze=class{constructor(){this.defaultState=new l,this.defaultState.blend=!0}contextChange(e){this.gpu=e}getColorTargets(e,t){let n=e.blend?Y[e.blendMode]||Y.normal:void 0,r=[];for(let e=0;e<t.length;e++){let i=t[e];r[e]={format:i,writeMask:0,blend:y(i)?void 0:n}}return r}destroy(){this.gpu=null}};ze.extension={type:[r.WebGPUSystem],name:`state`};var Be=[new T,new T,new T],Ve={type:`image`,upload(e,t,n,r=0){let i=e.resource,a=e.pixelWidth|0,o=e.pixelHeight|0,s=e.depthOrArrayLayers,c=i.byteLength/(a*o*s);if(s>1){n.device.queue.writeTexture({texture:t,origin:{x:0,y:0,z:r}},i,{offset:0,rowsPerImage:o,bytesPerRow:a*c},{width:a,height:o,depthOrArrayLayers:s});return}let l=ce(e._updateStart,e._updateEnd,a,o,Be);for(let e=0;e<l;e++){let o=Be[e];n.device.queue.writeTexture({texture:t,origin:{x:o.x,y:o.y,z:r}},i,{offset:(o.y*a+o.x)*c,rowsPerImage:o.height,bytesPerRow:a*c},{width:o.width,height:o.height,depthOrArrayLayers:1})}}},He={"bc1-rgba-unorm":{blockBytes:8,blockWidth:4,blockHeight:4},"bc2-rgba-unorm":{blockBytes:16,blockWidth:4,blockHeight:4},"bc3-rgba-unorm":{blockBytes:16,blockWidth:4,blockHeight:4},"bc7-rgba-unorm":{blockBytes:16,blockWidth:4,blockHeight:4},"etc1-rgb-unorm":{blockBytes:8,blockWidth:4,blockHeight:4},"etc2-rgba8unorm":{blockBytes:16,blockWidth:4,blockHeight:4},"astc-4x4-unorm":{blockBytes:16,blockWidth:4,blockHeight:4}},Ue={blockBytes:4,blockWidth:1,blockHeight:1},We={type:`compressed`,upload(e,t,n,r=0){let i=e.pixelWidth,a=e.pixelHeight,o=He[e.format]||Ue;for(let s=0;s<e.resource.length;s++){let c=e.resource[s],l=Math.ceil(i/o.blockWidth)*o.blockBytes;n.device.queue.writeTexture({texture:t,mipLevel:s,origin:{x:0,y:0,z:r}},c,{offset:0,bytesPerRow:l},{width:Math.ceil(i/o.blockWidth)*o.blockWidth,height:Math.ceil(a/o.blockHeight)*o.blockHeight,depthOrArrayLayers:1}),i=Math.max(i>>1,1),a=Math.max(a>>1,1)}}},Ge=[`right`,`left`,`top`,`bottom`,`front`,`back`];function Ke(e){return{type:`cube`,upload(t,n,r){let i=t.faces;for(let t=0;t<Ge.length;t++){let a=i[Ge[t]];(e[a.uploadMethodId]||e.image).upload(a,n,r,t)}}}}var qe={type:`image`,upload(e,t,n,r=0){let i=e.resource;if(!i)return;if(globalThis.HTMLImageElement&&i instanceof HTMLImageElement){let t=C.get().createCanvas(i.width,i.height);t.getContext(`2d`).drawImage(i,0,0,i.width,i.height),e.resource=t,h(`ImageSource: Image element passed, converting to canvas and replacing resource.`)}let a=Math.min(t.width,e.resourceWidth||e.pixelWidth),o=Math.min(t.height,e.resourceHeight||e.pixelHeight),s=e.alphaMode===`premultiply-alpha-on-upload`;n.device.queue.copyExternalImageToTexture({source:i},{texture:t,origin:{x:0,y:0,z:r},premultipliedAlpha:s},{width:a,height:o})}},Je={type:`video`,upload(e,t,n,r){qe.upload(e,t,n,r)}},Ye=[`rgba8unorm`,`rgba16float`],X=4;function Xe(e,t){if(!t)throw Error(`[Gpu3dMipmapGenerator] generating mipmaps for a 3D texture needs storage: true.`);if(!Ye.includes(e))throw Error(`[Gpu3dMipmapGenerator] format '${e}' can't be downsampled. Use ${Ye.join(`, `)}.`)}var Ze=class{constructor(e){this._pipelines={},this._device=e,this._sampler=e.createSampler({magFilter:`linear`,minFilter:`linear`})}_getMipmapPipeline(e){var t;return(t=this._pipelines)[e]??(t[e]=this._device.createComputePipeline({layout:`auto`,compute:{module:this._device.createShaderModule({code:`
|
|
53
|
+
@group(0) @binding(0) var srcSampler : sampler;
|
|
54
|
+
@group(0) @binding(1) var src : texture_3d<f32>;
|
|
55
|
+
@group(0) @binding(2) var dst : texture_storage_3d<${e}, write>;
|
|
56
|
+
|
|
57
|
+
@compute @workgroup_size(${X}, ${X}, ${X})
|
|
58
|
+
fn main(@builtin(global_invocation_id) gid : vec3<u32>) {
|
|
59
|
+
let dstSize = textureDimensions(dst);
|
|
60
|
+
|
|
61
|
+
if (gid.x >= dstSize.x || gid.y >= dstSize.y || gid.z >= dstSize.z) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// centre of the 2x2x2 footprint in the previous mip. When a dimension is
|
|
66
|
+
// odd its last texel, row or slice is never sampled, as in the 2D generator
|
|
67
|
+
let uv = (vec3<f32>(gid) * 2.0 + 1.0) / vec3<f32>(textureDimensions(src));
|
|
68
|
+
|
|
69
|
+
textureStore(dst, gid, textureSampleLevel(src, srcSampler, uv, 0.0));
|
|
70
|
+
}
|
|
71
|
+
`}),entryPoint:`main`}})),this._pipelines[e]}generateMipmap(e){if(e.dimension!==`3d`)throw Error(`[Gpu3dMipmapGenerator] generateMipmap expects a 3D texture.`);if(e.mipLevelCount<2)return;Xe(e.format,(e.usage&GPUTextureUsage.STORAGE_BINDING)!==0);let t=this._getMipmapPipeline(e.format),n=t.getBindGroupLayout(0),r=this._device.createCommandEncoder({label:`Gpu3dMipmapGenerator`}),i=r.beginComputePass();i.setPipeline(t);let a=e.width,o=e.height,s=e.depthOrArrayLayers,c=e.createView({dimension:`3d`,baseMipLevel:0,mipLevelCount:1});for(let t=1;t<e.mipLevelCount;t++){a=Math.max(1,a>>1),o=Math.max(1,o>>1),s=Math.max(1,s>>1);let r=e.createView({dimension:`3d`,baseMipLevel:t,mipLevelCount:1});i.setBindGroup(0,this._device.createBindGroup({layout:n,entries:[{binding:0,resource:this._sampler},{binding:1,resource:c},{binding:2,resource:r}]})),i.dispatchWorkgroups(Math.ceil(a/X),Math.ceil(o/X),Math.ceil(s/X)),c=r}i.end(),this._device.queue.submit([r.finish()])}},Qe=class{constructor(e){this._pipelines={},this._device=e,this._sampler=e.createSampler({minFilter:`linear`})}_getMipmapPipeline(e){let t=this._pipelines[e];return t||(this._mipmapShaderModule||=this._device.createShaderModule({code:`
|
|
72
|
+
var<private> pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
|
|
73
|
+
vec2<f32>(-1.0, -1.0), vec2<f32>(-1.0, 3.0), vec2<f32>(3.0, -1.0));
|
|
74
|
+
|
|
75
|
+
struct VertexOutput {
|
|
76
|
+
@builtin(position) position : vec4<f32>,
|
|
77
|
+
@location(0) texCoord : vec2<f32>,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
@vertex
|
|
81
|
+
fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> VertexOutput {
|
|
82
|
+
var output : VertexOutput;
|
|
83
|
+
output.texCoord = pos[vertexIndex] * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5);
|
|
84
|
+
output.position = vec4<f32>(pos[vertexIndex], 0.0, 1.0);
|
|
85
|
+
return output;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@group(0) @binding(0) var imgSampler : sampler;
|
|
89
|
+
@group(0) @binding(1) var img : texture_2d<f32>;
|
|
90
|
+
|
|
91
|
+
@fragment
|
|
92
|
+
fn fragmentMain(@location(0) texCoord : vec2<f32>) -> @location(0) vec4<f32> {
|
|
93
|
+
return textureSample(img, imgSampler, texCoord);
|
|
94
|
+
}
|
|
95
|
+
`}),t=this._device.createRenderPipeline({layout:`auto`,vertex:{module:this._mipmapShaderModule,entryPoint:`vertexMain`},fragment:{module:this._mipmapShaderModule,entryPoint:`fragmentMain`,targets:[{format:e}]}}),this._pipelines[e]=t),t}generateMipmap(e){if(e.dimension===`3d`)throw Error(`[GpuMipmapGenerator] a 3D texture uses Gpu3dMipmapGenerator.`);if(e.dimension===`1d`)throw Error(`Generating mipmaps for 1d textures is currently unsupported!`);let t=this._getMipmapPipeline(e.format),n=e,r=e.depthOrArrayLayers||1,i=e.usage&GPUTextureUsage.RENDER_ATTACHMENT;if(!i){let t={size:{width:Math.ceil(e.width/2),height:Math.ceil(e.height/2),depthOrArrayLayers:r},format:e.format,usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_SRC|GPUTextureUsage.RENDER_ATTACHMENT,mipLevelCount:e.mipLevelCount-1};n=this._device.createTexture(t)}let a=this._device.createCommandEncoder({}),o=t.getBindGroupLayout(0);for(let s=0;s<r;++s){let r=e.createView({baseMipLevel:0,mipLevelCount:1,dimension:`2d`,baseArrayLayer:s,arrayLayerCount:1}),c=+!!i;for(let i=1;i<e.mipLevelCount;++i){let e=n.createView({baseMipLevel:c++,mipLevelCount:1,dimension:`2d`,baseArrayLayer:s,arrayLayerCount:1}),i=a.beginRenderPass({colorAttachments:[{view:e,storeOp:`store`,loadOp:`clear`,clearValue:{r:0,g:0,b:0,a:0}}]}),l=this._device.createBindGroup({layout:o,entries:[{binding:0,resource:this._sampler},{binding:1,resource:r}]});i.setPipeline(t),i.setBindGroup(0,l),i.draw(3,1,0,0),i.end(),r=e}}if(!i){let t={width:Math.ceil(e.width/2),height:Math.ceil(e.height/2),depthOrArrayLayers:r};for(let r=1;r<e.mipLevelCount;++r)a.copyTextureToTexture({texture:n,mipLevel:r-1},{texture:e,mipLevel:r},t),t.width=Math.ceil(t.width/2),t.height=Math.ceil(t.height/2)}return this._device.queue.submit([a.finish()]),i||n.destroy(),e}},$e=class{constructor(e){this.textureView=null,this.textureViews=Object.create(null),this.msaa=null,this.gpuTexture=e}destroy(){this.gpuTexture.destroy(),this.msaa?.destroy(),this.textureView=null,this.textureViews=null,this.gpuTexture=null,this.msaa=null}},et=/^(rgba8|bgra8|bc[1237]-rgba-|etc2-rgb8|etc2-rgb8a1|etc2-rgba8|astc-\d+x\d+-)unorm$/;function tt(e){return et.test(e)?[`${e}-srgb`]:void 0}function nt(e){return`${e.format||``}.${e.dimension||``}.${e.aspect||``}.${e.baseMipLevel||0}.${e.mipLevelCount||``}.${e.baseArrayLayer||0}.${e.arrayLayerCount||``}`}var Z=class e{constructor(t){this._gpuSamplers=Object.create(null),this._bindGroupHash=Object.create(null),this._renderer=t,t.gc.addCollection(this,`_bindGroupHash`,`hash`),this._managedTextures=new m({renderer:t,type:`resource`,onUnload:this.onSourceUnload.bind(this),name:`gpuTextureSource`});let n={image:qe,buffer:Ve,video:Je,compressed:We,...e.uploadExtensions};this._uploads={...n,cube:Ke(n)}}get managedTextures(){return Object.values(this._managedTextures.items)}contextChange(e){this._gpu=e,this._managedTextures.removeAll(),this._gpuSamplers=Object.create(null),this._mipmapGenerator=null,this._mipmap3dGenerator=null}initSource(e){return e._gpuData[this._renderer.uid]?.gpuTexture||this._initSource(e)}_initSource(e){if(e.autoGenerateMipmaps){e.dimension===`3d`&&Xe(e.format,e.storage);let t=Math.max(e.pixelWidth,e.pixelHeight,e.depth);e.mipLevelCount=Math.floor(Math.log2(t))+1}let t,n;e.sampleCount>1?(t=GPUTextureUsage.RENDER_ATTACHMENT,e.transient?this._renderer.device.extensions.transientAttachment&&(t|=GPUTextureUsage.TRANSIENT_ATTACHMENT):e.format.includes(`depth`)&&(t|=GPUTextureUsage.TEXTURE_BINDING)):(t=GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST,e.uploadMethodId!==`compressed`&&(t|=GPUTextureUsage.RENDER_ATTACHMENT,t|=GPUTextureUsage.COPY_SRC),e.storage&&(t|=GPUTextureUsage.STORAGE_BINDING),n=tt(e.format));let r=He[e.format]||{blockBytes:4,blockWidth:1,blockHeight:1},i=Math.ceil(e.pixelWidth/r.blockWidth)*r.blockWidth,a=Math.ceil(e.pixelHeight/r.blockHeight)*r.blockHeight,o={label:e.label,size:{width:i,height:a,depthOrArrayLayers:e.depthOrArrayLayers},format:e.format,viewFormats:n,sampleCount:e.sampleCount,mipLevelCount:e.mipLevelCount,dimension:e.dimension,usage:t},s=this._gpu.device.createTexture(o);return e._gpuData[this._renderer.uid]=new $e(s),this._managedTextures.add(e)&&(e.on(`update`,this.onSourceUpdate,this),e.on(`resize`,this.onSourceResize,this),e.on(`updateMipmaps`,this.onUpdateMipmaps,this)),this.onSourceUpdate(e),s}onSourceUpdate(e){let t=this.getGpuSource(e);t&&(this._uploads[e.uploadMethodId]&&this._uploads[e.uploadMethodId].upload(e,t,this._gpu),e.autoGenerateMipmaps&&e.mipLevelCount>1&&this.onUpdateMipmaps(e))}onUpdateMipmaps(e){let t=this.getGpuSource(e);e.dimension===`3d`?(this._mipmap3dGenerator??=new Ze(this._gpu.device),this._mipmap3dGenerator.generateMipmap(t)):(this._mipmapGenerator??=new Qe(this._gpu.device),this._mipmapGenerator.generateMipmap(t))}onSourceUnload(e){e.off(`update`,this.onSourceUpdate,this),e.off(`resize`,this.onSourceResize,this),e.off(`updateMipmaps`,this.onUpdateMipmaps,this)}onSourceResize(e){e._gcLastUsed=this._renderer.gc.now;let t=e._gpuData[this._renderer.uid],n=t?.gpuTexture;n?(n.width!==e.pixelWidth||n.height!==e.pixelHeight)&&(t.destroy(),this._bindGroupHash[e.uid]=null,e._gpuData[this._renderer.uid]=null,this.initSource(e)):this.initSource(e)}_initSampler(e){return this._gpuSamplers[e._resourceId]=this._gpu.device.createSampler(e),this._gpuSamplers[e._resourceId]}getGpuSampler(e){return this._gpuSamplers[e._resourceId]||this._initSampler(e)}getGpuSource(e){return e._gcLastUsed=this._renderer.gc.now,e._gpuData[this._renderer.uid]?.gpuTexture||this.initSource(e)}getMsaaTexture(e){this.getGpuSource(e);let t=e._gpuData[this._renderer.uid];return t.msaa??=new Ne(this._renderer,e),t.msaa}getTextureBindGroup(e){return this._bindGroupHash[e.uid]||this._createTextureBindGroup(e)}_createTextureBindGroup(e){let t=e.source;return this._bindGroupHash[e.uid]=new u({0:t,1:t.style,2:new D({uTextureMatrix:{type:`mat3x3<f32>`,value:e.textureMatrix.mapCoord}})}),this._bindGroupHash[e.uid]}getTextureView(e,t){var n;let r=e.source;r._gcLastUsed=this._renderer.gc.now;let i=r._gpuData[this._renderer.uid];i||=(this.initSource(r),r._gpuData[this._renderer.uid]);let a=t?nt(t):0;return(n=i.textureViews)[a]||(n[a]=i.gpuTexture.createView({dimension:r.viewDimension,...t})),i.textureViews[a]}getTextureRenderTargetView(e,t=0,n=0,r){var i;let a=e.source;a._gcLastUsed=this._renderer.gc.now;let o=a._gpuData[this._renderer.uid];o||=(this.initSource(a),a._gpuData[this._renderer.uid]);let s=a.dimension===`3d`;s&&(n=0);let c=n*(a.mipLevelCount||1)+t+1;return r&&(c=`${c}.${nt(r)}`),(i=o.textureViews)[c]||(i[c]=o.gpuTexture.createView({dimension:s?`3d`:`2d`,baseMipLevel:t,mipLevelCount:1,baseArrayLayer:n,arrayLayerCount:1,...r})),o.textureViews[c]}generateCanvas(e){let t=this._renderer,n=t.gpu.device.createCommandEncoder(),r=C.get().createCanvas();r.width=e.source.pixelWidth,r.height=e.source.pixelHeight;let i=r.getContext(`webgpu`),a=e.source.format.replace(`-srgb`,``),o=Pe[a]?a:C.get().getNavigator().gpu.getPreferredCanvasFormat();return i.configure({device:t.gpu.device,usage:GPUTextureUsage.COPY_DST|GPUTextureUsage.COPY_SRC,format:o,alphaMode:`premultiplied`}),n.copyTextureToTexture({texture:t.texture.getGpuSource(e.source),origin:{x:0,y:0}},{texture:i.getCurrentTexture()},{width:r.width,height:r.height}),t.gpu.device.queue.submit([n.finish()]),r}getPixels(e){let t=this.generateCanvas(e),n=k.getOptimalCanvasAndContext(t.width,t.height),r=n.context;r.drawImage(t,0,0);let{width:i,height:a}=t,o=r.getImageData(0,0,i,a),s=new Uint8ClampedArray(o.data.buffer);return k.returnCanvasAndContext(n),{pixels:s,width:i,height:a}}destroy(){this._managedTextures.destroy();for(let e of Object.keys(this._bindGroupHash)){let t=Number(e);this._bindGroupHash[t]?.destroy()}this._renderer=null,this._gpu=null,this._mipmapGenerator=null,this._mipmap3dGenerator=null,this._gpuSamplers=null,this._bindGroupHash=null}};Z.extension={type:[r.WebGPUSystem],name:`texture`},Z.uploadExtensions=Object.create(null);var Q=Z;n.handleByMap(r.TextureUploaderWebGPU,Q.uploadExtensions);var rt=class{constructor(){this._maxTextures=0}_getShader(e){return this.shader}execute(e,t){let n=t.context,r=e.renderer,i=this._getRenderData(r,n),{batcher:a,instructions:o}=i,s=n.customShader||this._getShader(i),c=r.encoder;c.setGeometry(a.geometry,s.gpuProgram);let l=r.globalUniforms.bindGroup;c.setBindGroup(0,l,s.gpuProgram);let u=r.renderPipes.uniformBatch.getUniformBindGroup(s.resources.localUniforms,!0);c.setBindGroup(2,u,s.gpuProgram);let d=s.gpuProgram.layout;for(let e=3;e<d.length;e++){let t=s.groups[e];if(d[e]&&t){for(let e in t.resources){let n=t.resources[e];n?.isUniformGroup&&r.ubo.updateUniformGroup(n)}c.setBindGroup(e,t,s.gpuProgram)}}let f=o.instructions,p=null;for(let t=0;t<o.instructionSize;t++){let n=f[t];if(n.topology!==p&&(p=n.topology,c.setPipelineFromGeometryProgramAndState(a.geometry,s.gpuProgram,e.state,n.topology,s._overrides)),s.groups[1]=n.bindGroup,!n.gpuBindGroup){let e=n.textures;n.bindGroup=b(e.textures,e.count,this._maxTextures),n.gpuBindGroup=r.bindGroup.getBindGroup(n.bindGroup,s.gpuProgram,1)}c.setBindGroup(1,n.bindGroup,s.gpuProgram),c.renderPassEncoder.drawIndexed(n.size,1,n.start)}}destroy(){this.shader.destroy(),this.shader=null}},it=class extends rt{contextChange(e){let n=new D({uTransformMatrix:{value:new t,type:`mat3x3<f32>`},uColor:{value:new Float32Array([1,1,1,1]),type:`vec4<f32>`},uRound:{value:0,type:`f32`}});this._maxTextures=e.limits.maxBatchableTextures;let r=E({name:`graphics`,bits:[v,x(this._maxTextures),ae,_]});this.shader=new i({gpuProgram:r,resources:{localUniforms:n}})}_getRenderData(e,t){return e.graphicsContext.getContextRenderData(t)}};it.extension={type:[r.WebGPUPipesAdaptor],name:`graphics`};var at=class{init(){let e=E({name:`mesh`,bits:[re,le,_]});this._shader=new i({gpuProgram:e,resources:{uTexture:g.EMPTY._source,uSampler:g.EMPTY._source.style,textureUniforms:{uTextureMatrix:{type:`mat3x3<f32>`,value:new t}}}})}execute(e,t){let n=e.renderer,r=t._shader;if(!r)r=this._shader,r.groups[2]=n.texture.getTextureBindGroup(t.texture);else if(!r.gpuProgram){h(`Mesh shader has no gpuProgram`,t.shader);return}let i=r.gpuProgram;if(i.autoAssignGlobalUniforms&&(r.groups[0]=n.globalUniforms.bindGroup),i.autoAssignLocalUniforms){let t=e.localUniforms;r.groups[1]=n.renderPipes.uniformBatch.getUniformBindGroup(t,!0)}n.encoder.draw({geometry:t._geometry,shader:r,state:t.state})}destroy(){this._shader.destroy(),this._shader=null}};at.extension={type:[r.WebGPUPipesAdaptor],name:`mesh`};var ot=[...de,V,L,I,R,N,Q,Le,Re,ze,ke,P,z,M],st=[...ue,U],ct=[j,at,it],lt=[],ut=[],dt=[],$=[];n.handleByNamedList(r.WebGPUSystem,lt),n.handleByNamedList(r.WebGPUPipes,ut),n.handleByNamedList(r.WebGPUPipesAdaptor,dt),n.handleByNamedList(r.WebGPULoader,$),n.add(...ot,...st,...ct);var ft=class extends c{constructor(){let e={name:`webgpu`,type:s.WEBGPU,systems:lt,renderPipes:ut,renderPipeAdaptors:dt,loaders:$};super(e)}};export{ft as WebGPURenderer};
|