@derschmale/tiny-helix 0.1.0 → 0.2.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.
@@ -1,2 +1,2 @@
1
- var t,e,r,s,i,n,a,o,h,c,u,l;(function(t){t.sRGB="srgb",t.DisplayP3="display-p3"})(t||(t={})),function(t){t.Nearest="nearest",t.Linear="linear"}(e||(e={})),function(t){t.ClampToEdge="clamp-to-edge",t.Repeat="repeat",t.MirrorRepeat="mirror-repeat"}(r||(r={})),function(t){t.None="none",t.Front="front",t.Back="back"}(s||(s={})),function(t){t.R8Unorm="r8unorm",t.R8Snorm="r8snorm",t.R8Uint="r8uint",t.R8Sint="r8sint",t.R16Unorm="r16unorm",t.R16Snorm="r16snorm",t.R16Uint="r16uint",t.R16Sint="r16sint",t.R16Float="r16float",t.RG8Unorm="rg8unorm",t.RG8Snorm="rg8snorm",t.RG8Uint="rg8uint",t.RG8Sint="rg8sint",t.R32Uint="r32uint",t.R32Sint="r32sint",t.R32Float="r32float",t.RG16Unorm="rg16unorm",t.RG16Snorm="rg16snorm",t.RG16Uint="rg16uint",t.RG16Sint="rg16sint",t.RG16Float="rg16float",t.Rgba8Unorm="rgba8unorm",t.Rgba8UnormSrgb="rgba8unorm-srgb",t.Rgba8Snorm="rgba8snorm",t.Rgba8Uint="rgba8uint",t.Rgba8Sint="rgba8sint",t.Bgra8Unorm="bgra8unorm",t.Bgra8UnormSrgb="bgra8unorm-srgb",t.Rgb9e5Ufloat="rgb9e5ufloat",t.Rgb10a2Uint="rgb10a2uint",t.Rgb10a2Unorm="rgb10a2unorm",t.Rg11b10Ufloat="rg11b10ufloat",t.RG32Uint="rg32uint",t.RG32Sint="rg32sint",t.RG32Float="rg32float",t.Rgba16Unorm="rgba16unorm",t.Rgba16Snorm="rgba16snorm",t.Rgba16Uint="rgba16uint",t.Rgba16Sint="rgba16sint",t.Rgba16Float="rgba16float",t.Rgba32Uint="rgba32uint",t.Rgba32Sint="rgba32sint",t.Rgba32Float="rgba32float",t.Stencil8="stencil8",t.Depth16Unorm="depth16unorm",t.Depth24Plus="depth24plus",t.Depth24PlusStencil8="depth24plus-stencil8",t.Depth32Float="depth32float",t.Depth32FloatStencil8="depth32float-stencil8",t.Bc1RgbaUnorm="bc1-rgba-unorm",t.Bc1RgbaUnormSrgb="bc1-rgba-unorm-srgb",t.Bc2RgbaUnorm="bc2-rgba-unorm",t.Bc2RgbaUnormSrgb="bc2-rgba-unorm-srgb",t.Bc3RgbaUnorm="bc3-rgba-unorm",t.Bc3RgbaUnormSrgb="bc3-rgba-unorm-srgb",t.Bc4RUnorm="bc4-r-unorm",t.Bc4RSnorm="bc4-r-snorm",t.Bc5RgUnorm="bc5-rg-unorm",t.Bc5RgSnorm="bc5-rg-snorm",t.Bc6hRgbUfloat="bc6h-rgb-ufloat",t.Bc6hRgbFloat="bc6h-rgb-float",t.Bc7RgbaUnorm="bc7-rgba-unorm",t.Bc7RgbaUnormSrgb="bc7-rgba-unorm-srgb",t.Etc2Rgb8Unorm="etc2-rgb8unorm",t.Etc2Rgb8UnormSrgb="etc2-rgb8unorm-srgb",t.Etc2Rgb8A1Unorm="etc2-rgb8a1unorm",t.Etc2Rgb8A1UnormSrgb="etc2-rgb8a1unorm-srgb",t.Etc2Rgba8Unorm="etc2-rgba8unorm",t.Etc2Rgba8UnormSrgb="etc2-rgba8unorm-srgb",t.EacR11Unorm="eac-r11unorm",t.EacR11Snorm="eac-r11snorm",t.EacRg11Unorm="eac-rg11unorm",t.EacRg11Snorm="eac-rg11snorm",t.Astc4x4Unorm="astc-4x4-unorm",t.Astc4x4UnormSrgb="astc-4x4-unorm-srgb",t.Astc5x4Unorm="astc-5x4-unorm",t.Astc5x4UnormSrgb="astc-5x4-unorm-srgb",t.Astc5x5Unorm="astc-5x5-unorm",t.Astc5x5UnormSrgb="astc-5x5-unorm-srgb",t.Astc6x5Unorm="astc-6x5-unorm",t.Astc6x5UnormSrgb="astc-6x5-unorm-srgb",t.Astc6x6Unorm="astc-6x6-unorm",t.Astc6x6UnormSrgb="astc-6x6-unorm-srgb",t.Astc8x5Unorm="astc-8x5-unorm",t.Astc8x5UnormSrgb="astc-8x5-unorm-srgb",t.Astc8x6Unorm="astc-8x6-unorm",t.Astc8x6UnormSrgb="astc-8x6-unorm-srgb",t.Astc8x8Unorm="astc-8x8-unorm",t.Astc8x8UnormSrgb="astc-8x8-unorm-srgb",t.Astc10x5Unorm="astc-10x5-unorm",t.Astc10x5UnormSrgb="astc-10x5-unorm-srgb",t.Astc10x6Unorm="astc-10x6-unorm",t.Astc10x6UnormSrgb="astc-10x6-unorm-srgb",t.Astc10x8Unorm="astc-10x8-unorm",t.Astc10x8UnormSrgb="astc-10x8-unorm-srgb",t.Astc10x10Unorm="astc-10x10-unorm",t.Astc10x10UnormSrgb="astc-10x10-unorm-srgb",t.Astc12x10Unorm="astc-12x10-unorm",t.Astc12x10UnormSrgb="astc-12x10-unorm-srgb",t.Astc12x12Unorm="astc-12x12-unorm",t.Astc12x12UnormSrgb="astc-12x12-unorm-srgb"}(i||(i={})),function(t){t.Zero="zero",t.One="one",t.SrcColor="src",t.OneMinusSrcColor="one-minus-src",t.SrcAlpha="src-alpha",t.OneMinusSrcAlpha="one-minus-src-alpha",t.DstColor="dst",t.OneMinusDstColor="one-minus-dst",t.DstAlpha="dst-alpha",t.OneMinusDstAlpha="one-minus-dst-alpha",t.SrcAlphaSaturated="src-alpha-saturated",t.Constant="constant",t.OneMinusConstant="one-minus-constant",t.Src1Color="src1",t.OneMinusSrc1Color="one-minus-src1",t.Src1Alpha="src1-alpha",t.OneMinusSrc1Alpha="one-minus-src1-alpha"}(n||(n={})),function(t){t.Never="never",t.Less="less",t.Equal="equal",t.LessEqual="less-equal",t.Greater="greater",t.NotEqual="not-equal",t.GreaterEqual="greater-equal",t.Always="always"}(a||(a={})),function(t){t.ReadWrite="read-write",t.Read="read-only",t.Write="write-only"}(o||(o={}));class _{constructor(){this._adapter=null,this._device=null,this._context=null,this._format=i.Rgba8UnormSrgb,this._canvas=null,this._colorSpace=t.sRGB}get adapter(){return this._adapter}get device(){return this._device}get context(){return this._context}get format(){return this._format}get colorSpace(){return this._colorSpace}get canvas(){return this._canvas}static isSupported(){return"undefined"!=typeof navigator&&"gpu"in navigator}async initialize(e={}){if(!_.isSupported())throw new Error("WebGPU is not supported in this browser");if(this._adapter=await navigator.gpu.requestAdapter({powerPreference:e.powerPreference??"high-performance"}),!this._adapter)throw new Error("Failed to get WebGPU adapter");if(this._device=await this._adapter.requestDevice({requiredFeatures:e.requiredFeatures,requiredLimits:e.requiredLimits}),!this._device)throw new Error("Failed to get WebGPU device");if(this._device.lost.then(t=>{console.error("WebGPU device lost:",t.message),"destroyed"!==t.reason&&this.initialize(e).catch(t=>{console.error("Failed to reinitialize WebGPU context:",t)})}),e.canvas){if(this._canvas=e.canvas,this._context=this._canvas.getContext("webgpu"),!this._context)throw new Error("Failed to get WebGPU context from canvas");const r=window.matchMedia("(color-gamut: p3)").matches;this._colorSpace=e.colorSpace??t.sRGB,this._colorSpace!==t.DisplayP3||r||(this._colorSpace=t.sRGB,console.warn("DisplayP3 not supported, falling back to sRGB")),this._format=navigator.gpu.getPreferredCanvasFormat(),this._context.configure({device:this._device,format:this._format,colorSpace:this._colorSpace,alphaMode:"premultiplied"})}else console.warn("No canvas provided!")}getCurrentTexture(){return this._context.getCurrentTexture()}destroy(){this._device&&(this._device.destroy(),this._device=null),this._adapter=null,this._context=null,this._canvas=null}}function m(t,e){if(void 0!==t)return e(t)}class g{constructor(t){this._numVertices=0,this._numIndices=0,this._inner=t}setPipeline(t){return this._renderPipeline!=t&&(this._inner.setPipeline(t._inner),this._renderPipeline=t),this}setMesh(t){for(let e=0;e<t.numStreams;++e){const r=t.getVertexBuffer(e);this._inner.setVertexBuffer(e,r._inner)}this._numVertices=t.numVertices;const e=t.indexBuffer;return e?(this._inner.setIndexBuffer(e._inner,t.indexFormat),this._numIndices=t.numIndices):this._numIndices=0,this}setBindGroup(t,e){return this._inner.setBindGroup(t,e._inner),this}draw(){return this._numIndices?this._inner.drawIndexed(this._numIndices,1,0,0,0):this._inner.draw(this._numVertices,1,0,0),this}end(){this._inner.end()}}class d{constructor(t,e,r,s){this._colorTargets=[],this._clearColors=[],this._encoder=t,this._defaultTarget=r,this._defaultDepthTarget=s,this._globalBindGroups=e}withLabel(t){return this._label=t,this}withColorTarget(t){return this._colorTargets.push(t),this}withDepthStencilTarget(t){return this._depthTarget=t,this}withClearColor(t,e,r,s){let i;return i=void 0===t?[0,0,0,1]:"number"==typeof t?[t,e??0,r??0,s??1]:[t[0],t[1],t[2],t[3]??1],0===this._colorTargets.length?this._clearColors[0]=i:this._clearColors[this._colorTargets.length-1]=i,this}withClearStencil(t){return this._clearStencil=t,this}withClearDepth(t){return this._clearDepth=t,this}build(){let t,e,r,s,n;this._colorTargets.length||this._depthTarget?(t=this._colorTargets,e=this._depthTarget):(t=[this._defaultTarget],e=this._defaultDepthTarget),e?.format!==i.Depth32FloatStencil8&&e?.format!==i.Depth24PlusStencil8||(r=null!=this._clearStencil?"clear":"load",s=m(this._clearStencil,()=>"store"),n=this._clearStencil);const a={colorAttachments:t.map((t,e)=>({view:t._inner,loadOp:this._clearColors[e]?"clear":"load",storeOp:"store",clearValue:this._clearColors[e]})),depthStencilAttachment:m(e,t=>({view:t._inner,depthClearValue:this._clearDepth,depthLoadOp:null!=this._clearDepth?"clear":"load",depthStoreOp:m(this._clearDepth,()=>"store"),stencilClearValue:n,stencilLoadOp:r,stencilStoreOp:s})),label:this._label},o=new g(this._encoder.beginRenderPass(a));return this._globalBindGroups.forEach((t,e)=>o.setBindGroup(e,t)),o}}class b{constructor(t){this._inner=t}setPipeline(t){return this._inner.setPipeline(t._inner),this}dispatch(t,e,r){return this._inner.dispatchWorkgroups(t,e,r),this}dispatchIndirect(t,e){return this._inner.dispatchWorkgroupsIndirect(t._inner,e),this}setBindGroup(t,e){return this._inner.setBindGroup(t,e._inner),this}end(){this._inner.end()}}class p{constructor(t,e){this._encoder=t,this._globalBindGroups=e}withLabel(t){return this._label=t,this}build(){const t={label:this._label},e=new b(this._encoder.beginComputePass(t));for(let t=0;t<this._globalBindGroups.length;++t)e.setBindGroup(t,this._globalBindGroups[t]);return e}}class f{constructor(t,e,r,s,i){this._commandBuffer=null;const n=r.device;this._queue=n.queue,this._backbufferTarget=t,this._depthStencilTarget=s,this._globalBindGroups=e,this._encoder=n.createCommandEncoder({label:i})}clearBuffer(t,e,r){return this._encoder.clearBuffer(t._inner,e,r),this}createRenderPass(){return new d(this._encoder,this._globalBindGroups,this._backbufferTarget,this._depthStencilTarget)}createComputePass(){return new p(this._encoder,this._globalBindGroups)}finish(){this._commandBuffer||(this._commandBuffer=this._encoder.finish())}submit(){this.finish(),this._queue.submit([this._commandBuffer])}}class x{static from_webgpu(t,e){return new x(t,e)}constructor(t,e){this._inner=t,this._format=e}createView(){return new w(this)}get format(){return this._format}_getBufferResource(){return this._inner.createView()}}class U{constructor(t){this._size=[1,1,1],this._data=void 0,this._format=i.Rgba8UnormSrgb,this._usage=GPUTextureUsage.TEXTURE_BINDING,this._ctx=t}withSize(t,e,r=1){return this._size=[t,e,r],this}withData(t){return this._data=t,this}withFormat(t){return this._format=t,this}withImage(t){return this._data=t,this._size=[t.width,t.height,1],this}withUsage(t){return this._usage|=t,this}build(){this._data&&(this._usage|=GPUTextureUsage.COPY_DST);const t=this._ctx.device.createTexture({format:this._format,size:this._size,usage:this._usage});if(this._data instanceof ImageBitmap)this._ctx.device.queue.copyExternalImageToTexture({source:this._data},{texture:t},[this._size[0],this._size[1],1]);else if(this._data){const[e,r,s]=this._size,n=function(t){if(function(t){switch(t){case i.Bc1RgbaUnorm:case i.Bc1RgbaUnormSrgb:case i.Bc2RgbaUnorm:case i.Bc2RgbaUnormSrgb:case i.Bc3RgbaUnorm:case i.Bc3RgbaUnormSrgb:case i.Bc4RUnorm:case i.Bc4RSnorm:case i.Bc5RgUnorm:case i.Bc5RgSnorm:case i.Bc6hRgbUfloat:case i.Bc6hRgbFloat:case i.Bc7RgbaUnorm:case i.Bc7RgbaUnormSrgb:return!0;default:return!1}}(t)||function(t){switch(t){case i.Etc2Rgb8Unorm:case i.Etc2Rgb8UnormSrgb:case i.Etc2Rgb8A1Unorm:case i.Etc2Rgb8A1UnormSrgb:case i.Etc2Rgba8Unorm:case i.Etc2Rgba8UnormSrgb:case i.EacR11Unorm:case i.EacR11Snorm:case i.EacRg11Unorm:case i.EacRg11Snorm:return!0;default:return!1}}(t))return 4;switch(t){case i.Astc4x4Unorm:case i.Astc4x4UnormSrgb:return 4;case i.Astc5x4Unorm:case i.Astc5x4UnormSrgb:case i.Astc5x5Unorm:case i.Astc5x5UnormSrgb:return 5;case i.Astc6x5Unorm:case i.Astc6x5UnormSrgb:case i.Astc6x6Unorm:case i.Astc6x6UnormSrgb:return 6;case i.Astc8x5Unorm:case i.Astc8x5UnormSrgb:case i.Astc8x6Unorm:case i.Astc8x6UnormSrgb:case i.Astc8x8Unorm:case i.Astc8x8UnormSrgb:return 8;case i.Astc10x5Unorm:case i.Astc10x5UnormSrgb:case i.Astc10x6Unorm:case i.Astc10x6UnormSrgb:case i.Astc10x8Unorm:case i.Astc10x8UnormSrgb:case i.Astc10x10Unorm:case i.Astc10x10UnormSrgb:return 10;case i.Astc12x10Unorm:case i.Astc12x10UnormSrgb:case i.Astc12x12Unorm:case i.Astc12x12UnormSrgb:return 12;default:return 1}}(this._format),a=Math.ceil(e/n)*function(t){switch(t){case i.R8Unorm:case i.R8Snorm:case i.R8Uint:case i.R8Sint:case i.Stencil8:return 1;case i.R16Uint:case i.R16Sint:case i.R16Unorm:case i.R16Snorm:case i.R16Float:case i.RG8Unorm:case i.RG8Snorm:case i.RG8Uint:case i.RG8Sint:case i.Depth16Unorm:return 2;case i.R32Uint:case i.R32Sint:case i.R32Float:case i.RG16Uint:case i.RG16Sint:case i.RG16Unorm:case i.RG16Snorm:case i.RG16Float:case i.Rgba8Unorm:case i.Rgba8UnormSrgb:case i.Rgba8Snorm:case i.Rgba8Uint:case i.Rgba8Sint:case i.Bgra8Unorm:case i.Bgra8UnormSrgb:case i.Rgb9e5Ufloat:case i.Rgb10a2Uint:case i.Rgb10a2Unorm:case i.Depth24Plus:case i.Depth24PlusStencil8:case i.Depth32Float:return 4;case i.Rg11b10Ufloat:case i.RG32Uint:case i.RG32Sint:case i.RG32Float:case i.Rgba16Uint:case i.Rgba16Sint:case i.Rgba16Unorm:case i.Rgba16Snorm:case i.Rgba16Float:case i.Depth32FloatStencil8:case i.Bc1RgbaUnorm:case i.Bc1RgbaUnormSrgb:case i.Bc4RUnorm:case i.Bc4RSnorm:case i.Etc2Rgb8Unorm:case i.Etc2Rgb8UnormSrgb:case i.Etc2Rgb8A1Unorm:case i.Etc2Rgb8A1UnormSrgb:case i.EacR11Unorm:case i.EacR11Snorm:return 8;case i.Rgba32Uint:case i.Rgba32Sint:case i.Rgba32Float:case i.Bc2RgbaUnorm:case i.Bc2RgbaUnormSrgb:case i.Bc3RgbaUnorm:case i.Bc3RgbaUnormSrgb:case i.Bc5RgUnorm:case i.Bc5RgSnorm:case i.Bc6hRgbUfloat:case i.Bc6hRgbFloat:case i.Bc7RgbaUnorm:case i.Bc7RgbaUnormSrgb:case i.Etc2Rgba8Unorm:case i.Etc2Rgba8UnormSrgb:case i.EacRg11Unorm:case i.EacRg11Snorm:case i.Astc4x4Unorm:case i.Astc4x4UnormSrgb:case i.Astc5x4Unorm:case i.Astc5x4UnormSrgb:case i.Astc5x5Unorm:case i.Astc5x5UnormSrgb:case i.Astc6x5Unorm:case i.Astc6x5UnormSrgb:case i.Astc6x6Unorm:case i.Astc6x6UnormSrgb:case i.Astc8x5Unorm:case i.Astc8x5UnormSrgb:case i.Astc8x6Unorm:case i.Astc8x6UnormSrgb:case i.Astc8x8Unorm:case i.Astc8x8UnormSrgb:case i.Astc10x5Unorm:case i.Astc10x5UnormSrgb:case i.Astc10x6Unorm:case i.Astc10x6UnormSrgb:case i.Astc10x8Unorm:case i.Astc10x8UnormSrgb:case i.Astc10x10Unorm:case i.Astc10x10UnormSrgb:case i.Astc12x10Unorm:case i.Astc12x10UnormSrgb:case i.Astc12x12Unorm:case i.Astc12x12UnormSrgb:case i.Bc2RgbaUnorm:return 16;default:throw new Error(`Unsupported texture format: ${t}`)}}(this._format);this._ctx.device.queue.writeTexture({texture:t},this._data,{bytesPerRow:a},{width:e,height:r,depthOrArrayLayers:s})}return new x(t,this._format)}}class S{constructor(t){this._inner=t}}class w{constructor(t){this._desc={},this._texture=t}withSingleMip(t){return this._desc.baseMipLevel=t,this._desc.mipLevelCount=1,this}withMipRange(t,e){return this._desc.baseMipLevel=t,this._desc.mipLevelCount=e-t,this}withSingleLayer(t){return this._desc.baseArrayLayer=t,this._desc.arrayLayerCount=1,this}withLayerRange(t,e){return this._desc.baseArrayLayer=t,this._desc.arrayLayerCount=e,this}build(){return new S(this._texture._inner.createView(this._desc))}}class R{constructor(t,e){this._inner=t,this._format=e}get format(){return this._format}}class A{constructor(t){this._baseMipLevel=0,this._baseArrayLayer=0,this._texture=t}withMipLevel(t){return this._baseMipLevel=t,this}withArrayLayer(t){return this._baseArrayLayer=t,this}build(){const t=this._texture._inner,e={format:t.format,dimension:t.dimension,aspect:"all",baseMipLevel:this._baseMipLevel,mipLevelCount:1,baseArrayLayer:this._baseArrayLayer,arrayLayerCount:1};return new R(this._texture._inner.createView(e),this._texture.format)}}!function(t){t[t.MapRead=GPUBufferUsage.MAP_READ]="MapRead",t[t.MapWrite=GPUBufferUsage.MAP_WRITE]="MapWrite",t[t.CopySrc=GPUBufferUsage.COPY_SRC]="CopySrc",t[t.CopyDst=GPUBufferUsage.COPY_DST]="CopyDst",t[t.Index=GPUBufferUsage.INDEX]="Index",t[t.Vertex=GPUBufferUsage.VERTEX]="Vertex",t[t.Uniform=GPUBufferUsage.UNIFORM]="Uniform",t[t.Storage=GPUBufferUsage.STORAGE]="Storage",t[t.Indirect=GPUBufferUsage.INDIRECT]="Indirect",t[t.QueryResolve=GPUBufferUsage.QUERY_RESOLVE]="QueryResolve"}(h||(h={})),function(t){t[t.CopySrc=GPUTextureUsage.COPY_SRC]="CopySrc",t[t.CopyDst=GPUTextureUsage.COPY_DST]="CopyDst",t[t.TextureBinding=GPUTextureUsage.TEXTURE_BINDING]="TextureBinding",t[t.StorageBinding=GPUTextureUsage.STORAGE_BINDING]="StorageBinding",t[t.RenderAttachment=GPUTextureUsage.RENDER_ATTACHMENT]="RenderAttachment"}(c||(c={}));class y{constructor(t){this._size=0,this._keepData=!1,this._usage=0,this._ctx=t}withUsage(t){return this._usage|=t,this}withData(t,e=!1){return this._data=t,this._size=t.byteLength,this._keepData=e,this._usage|=h.CopyDst,this}withSize(t){return this._size=t,this}build(){const t=m(this._data,t=>function(t){const e=4*Math.ceil(t.byteLength/4);if(e===t.byteLength)return t;const r=t instanceof ArrayBuffer?new ArrayBuffer(e):new SharedArrayBuffer(e),s=new Uint8Array(t);return new Uint8Array(r).set(s),r}(t)),e=this._ctx.device.createBuffer({size:t?t.byteLength:this._size,usage:this._usage});return t&&this._ctx.device.queue.writeBuffer(e,0,t),new B(e,this._keepData?this._data:void 0)}}class B{constructor(t,e){this._inner=t,this.data=e}get size(){return this._inner.size}_uploadData(t){this.data?t.device.queue.writeBuffer(this._inner,0,this.data):console.warn("Buffer has no data to upload.")}_getBufferResource(){return{buffer:this._inner}}}function F(t){const e=new Float32Array(1);e[0]=t;const r=new Uint32Array(e.buffer)[0],s=r>>16&32768,i=(r>>23&255)-127;return i<=-15?s:i>16?31744|s:s|i+15<<10|(8388607&r)>>13}function v(t){switch(t){case u.Float16:return 2;case u.Float32:return 4;case u.Boolean:return 1;default:throw new Error("Unknown BaseType.")}}!function(t){t[t.Float16=0]="Float16",t[t.Float32=1]="Float32",t[t.Uint=2]="Uint",t[t.Sint=3]="Sint",t[t.Boolean=4]="Boolean"}(u||(u={})),function(t){t[t.Scalar=0]="Scalar",t[t.Vec=1]="Vec",t[t.Matrix=2]="Matrix",t[t.Array=3]="Array"}(l||(l={}));class T{constructor(t,e,r,s){if(this.numRows=1,this.numCols=1,e===l.Scalar&&1!==r&&1!==s)throw new Error("Scalar member must have numRows = 1.");this.baseType=t,this.containerType=e,this.numRows=r,this.numCols=s}get size(){const t=this.numCols>1&&3===this.numRows?4:this.numRows;return v(this.baseType)*t*this.numCols}get alignment(){const t=3===this.numRows?4:this.numRows;return v(this.baseType)*t}}class G{constructor(){this._size=0,this._members=new Map}withSize(t){if(0!==this._size)throw new Error("Size already set.");return this._size=t,this}pushScalar(t,e){return this._pushField(t,e,l.Scalar)}pushVec2(t,e){return this._pushField(t,e,l.Vec,2)}pushVec3(t,e){return this._pushField(t,e,l.Vec,3)}pushVec4(t,e){return this._pushField(t,e,l.Vec,4)}pushMatrix(t,e,r,s){if(!Number.isInteger(e)||!Number.isInteger(r))throw new Error("Matrix dimensions must be integers");if(e<2||e>4||r<2||r>4)throw new Error("Matrix dimensions must be between 2 and 4 (inclusive)");return this._pushField(t,s,l.Matrix,e,r)}build(){return this._size=16*Math.ceil(this._size/16),new E(this._size,this._members)}_pushField(t,e,r,s=1,i=1){const n=new T(t,r,s,i),a=n.alignment,o=Math.ceil(this._size/a)*a;return this._members.set(e,{offset:o,type:n}),this._size=o+n.size,this}}class E{constructor(t,e){this.size=t,this._members=e}_createDataViews(t){const e=new Map;for(const[r,s]of this._members){let i;const n=s.type.size;let a=4;switch(s.type.baseType){case u.Float16:a=2,i=Uint16Array;break;case u.Float32:i=Float32Array;break;case u.Uint:case u.Boolean:i=Uint32Array;break;case u.Sint:i=Int32Array}e.set(r,[s,new i(t,s.offset,n/a)])}return e}_getMemberOrThrow(t){const e=this._members.get(t);if(!e)throw new Error(`Member ${t} not found in layout.`);return e}}class M{constructor(t,e){this._dataViews=new Map;const r=new ArrayBuffer(t.size);this._buffer=new y(e).withUsage(GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST).withData(r,!0).build(),this._data=this._buffer.data,this._dataViews=t._createDataViews(this._data),this._ctx=e}setScalar(t,e){const[r,s]=this._dataViews.get(t);return r.type.baseType===u.Float16?s[0]=F(e):s[0]=e,this}setVec(t,e){const[r,s]=this._dataViews.get(t);if(r.type.baseType==u.Float16)for(let t=0;t<e.length;++t)s[t]=F(e[t]);else for(let t=0;t<e.length;++t)s[t]=e[t];return this}setMatrix(t,e){const[r,s]=this._dataViews.get(t),i=r.type.numCols,n=r.type.numRows,a=3===n;let o=0;if(r.type.baseType===u.Float16)for(let t=0;t<i;++t){for(let t=0;t<n;++t)s[o]=F(e[o]),++o;a&&++o}else for(let t=0;t<i;++t){for(let t=0;t<n;++t)s[o]=e[o],++o;a&&++o}return this}upload(){return this._buffer._uploadData(this._ctx),this}_getBufferResource(){return{buffer:this._buffer._inner}}}class C{constructor(t){this._inner=t}}class L{constructor(t,e){this._entries=[],this._ctx=t,this._layout=e}withLabel(t){return this._label=t,this}withBuffer(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._getBufferResource()},this}withTexture(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._inner},this}withSampler(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._inner},this}build(){const t=this._ctx.device.createBindGroup({label:this._label,entries:this._entries,layout:this._layout._inner});return new C(t)}}class P{constructor(t,e,r,s){this._inner=t,this._indices=r,this._uboLayouts=s,this._ctx=e}getUniformBufferLayout(t){return this._uboLayouts.get(t)}createUniformBuffer(t){return m(this._uboLayouts.get(t),t=>new M(t,this._ctx))}_getBindingIndex(t){const e=this._indices.get(t);if(void 0===e)throw`No field name found for ${t} in bind group layout.`;return e}}const D=class{constructor(t){this._entries=[],this._indices=new Map,this._uboLayouts=new Map,this._ctx=t}withLabel(t){return this._label=t,this}withUniformBuffer(t,e,r,s){return this._entries[t]={binding:t,visibility:s??GPUShaderStage.VERTEX|GPUShaderStage.FRAGMENT|GPUShaderStage.COMPUTE,buffer:{type:"uniform"}},this._indices.set(e,t),this._uboLayouts.set(e,r),this}withStorageBuffer(t,e,r,s){let i="storage";return r===o.Read&&(i="read-only-storage"),this._entries[t]={binding:t,visibility:s??GPUShaderStage.FRAGMENT|GPUShaderStage.COMPUTE,buffer:{type:i,hasDynamicOffset:!1,minBindingSize:4}},this._indices.set(e,t),this}withStorageTexture(t,e,r,s,i){return this._entries[t]={binding:t,visibility:i??GPUShaderStage.FRAGMENT|GPUShaderStage.COMPUTE,storageTexture:{access:s,format:r}},this._indices.set(e,t),this}withTexture(t,e,r){return this._entries[t]={binding:t,visibility:r??GPUShaderStage.FRAGMENT|GPUShaderStage.COMPUTE,texture:{sampleType:"float"}},this._indices.set(e,t),this}withSampler(t,e,r){return this._entries[t]={binding:t,visibility:r??GPUShaderStage.FRAGMENT|GPUShaderStage.COMPUTE,sampler:{type:"filtering"}},this._indices.set(e,t),this}build(){const t=this._ctx.device.createBindGroupLayout({label:this._label,entries:this._entries});return new P(t,this._ctx,this._indices,this._uboLayouts)}};class O{constructor(t,e,r,s){this._inner=t,this._ctx=e,this._vertexAttributes=r,this._bindGroupLayouts=s}getVertexAttributeLocation(t){return this._vertexAttributes.get(t)??void 0}hasVertexAttribute(t){return this._vertexAttributes.has(t)}createBindGroup(t){return new L(this._ctx,this._bindGroupLayouts[t])}}class V{constructor(t){this._vertexAttributes=new Map,this._includes=new Map,this._ctx=t}withLabel(t){return this._label=t,this}withCode(t){return this._code=t,this}withInclude(t,e){return this._includes.set(t,e),this}withVertexAttribute(t,e){return this._vertexAttributes.set(t,e),this}withBindGroup(t,e){if(this._bindGroupLayouts=this._bindGroupLayouts??[],e instanceof P)this._bindGroupLayouts[t]=e;else{const r=new D(this._ctx);e(r),this._bindGroupLayouts[t]=r.build()}return this}build(){if(!this._code)throw new Error("Shader code not specified. Use withCode() to set the shader source.");let t=this._code;for(const[e,r]of this._includes){const s=e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),i=new RegExp(`#\\s*include\\s*<\\s*${s}\\s*>`,"g");t=t.replace(i,r)}const e=this._ctx.device.createShaderModule({label:this._label,code:t});return new O(e,this._ctx,this._vertexAttributes,this._bindGroupLayouts)}}class I{constructor(t,e){this._inner=t,this._shader=e}getVertexAttributeLocation(t){return this._shader.getVertexAttributeLocation(t)}}class z{constructor(t,e){this._colorTargets=[],this._vertexEntry=void 0,this._fragmentEntry=void 0,this._label=void 0,this._overrideConstants={},this._cullMode=s.Back,this._mesh=void 0,this._ctx=t,this._defaultColorState=[{format:t.format,blend:void 0,writeMask:GPUColorWrite.ALL}],this._defaultDepthState=m(e,t=>({format:t,depthWriteEnabled:!0,depthCompare:"less"}))}withLabel(t){return this._label=t,this}withCullMode(t){return this._cullMode=t,this}withMesh(t){return this._mesh=t,this}withShader(t){return this._shader=t,this}withVertexShader(t){return this._vertexEntry=t,this}withFragmentShader(t){return this._fragmentEntry=t,this}withColorTarget(t){return this._colorTargets.push({format:t,blend:void 0,writeMask:GPUColorWrite.ALL}),this}withDepthTarget(t){return this._depthState={format:t,...this._defaultDepthState},this}withDepthCompare(t){const e=this._depthState??this._defaultDepthState;return e?e.depthCompare=t:console.warn("No depth target set. Setting depth compare function has no effect."),this}withDepthWrite(t){const e=this._depthState??this._defaultDepthState;return e?e.depthWriteEnabled=t:console.warn("No depth target set. Setting depth write enabled has no effect."),this}withOverrideConstant(t,e){return this._overrideConstants[t]=e,this}withBlendMode(t){return this.lastColorTarget.blend=t._inner,this}build(){if(!this._shader)throw new Error("Shader not specified. Use withShader() to set the shader to use.");const t=this._shader;if(void 0===this._vertexEntry)throw new Error("Shader does not contain a vertex entry point. Use ShaderBuilder.withVertexShader() to set one.");let e,r;this._colorTargets.length||this._depthState?(e=this._colorTargets,r=this._depthState):(e=this._defaultColorState,r=this._defaultDepthState);const s=m(this._mesh,t=>({topology:t.topology,frontFace:t.frontFace,cullMode:this._cullMode})),i=m(this._mesh,e=>{const r=[];for(let s=0;s<e.numStreams;++s){const i=e.getStreamAttributes(s).filter(e=>t.hasVertexAttribute(e.name)).map(e=>({shaderLocation:t.getVertexAttributeLocation(e.name),offset:e.offset,format:e.format}));r.push({arrayStride:e.getStreamStride(s),stepMode:"vertex",attributes:i})}return r}),n=m(t._bindGroupLayouts,t=>this._ctx.device.createPipelineLayout({bindGroupLayouts:t.map(t=>t._inner),label:this._label}))??"auto",a={label:this._label,layout:n,primitive:s,depthStencil:r,vertex:{buffers:i,module:t._inner,entryPoint:this._vertexEntry,constants:this._overrideConstants},fragment:m(this._fragmentEntry,r=>({module:t._inner,entryPoint:r,targets:e,constants:this._overrideConstants}))},o=this._ctx.device.createRenderPipeline(a);return new I(o,t)}get lastColorTarget(){return this._colorTargets.length?this._colorTargets[this._colorTargets.length-1]:this._defaultColorState[0]}}var k,N,W,q;!function(t){t.Uint16="uint16",t.Uint32="uint32"}(k||(k={}));class Y{constructor(t,e,r=!1){const s=t.BYTES_PER_ELEMENT;this.format=2===s?k.Uint16:k.Uint32,this.count=t.length,this.buffer=new y(e).withData(t.buffer,r).withUsage(h.CopyDst|h.Index).build()}}!function(t){t.TriangleList="triangle-list",t.TriangleStrip="triangle-strip",t.LineList="line-list",t.LineStrip="line-strip",t.PointList="point-list"}(N||(N={})),function(t){t.Clockwise="cw",t.CounterClockwise="ccw"}(W||(W={})),function(t){t.Uint8="uint8",t.Uint8x2="uint8x2",t.Uint8x4="uint8x4",t.Sint8="sint8",t.Sint8x2="sint8x2",t.Sint8x4="sint8x4",t.Unorm8="unorm8",t.Unorm8x2="unorm8x2",t.Unorm8x4="unorm8x4",t.Snorm8="snorm8",t.Snorm8x2="snorm8x2",t.Snorm8x4="snorm8x4",t.Uint16="uint16",t.Uint16x2="uint16x2",t.Uint16x4="uint16x4",t.Sint16="sint16",t.Sint16x2="sint16x2",t.Sint16x4="sint16x4",t.Unorm16="unorm16",t.Unorm16x2="unorm16x2",t.Unorm16x4="unorm16x4",t.Snorm16="snorm16",t.Snorm16x2="snorm16x2",t.Snorm16x4="snorm16x4",t.Float16="float16",t.Float16x2="float16x2",t.Float16x4="float16x4",t.Float32="float32",t.Float32x2="float32x2",t.Float32x3="float32x3",t.Float32x4="float32x4",t.Uint32="uint32",t.Uint32x2="uint32x2",t.Uint32x3="uint32x3",t.Uint32x4="uint32x4",t.Sint32="sint32",t.Sint32x2="sint32x2",t.Sint32x3="sint32x3",t.Sint32x4="sint32x4",t.Unorm10_10_10_2="unorm10-10-10-2",t.Unorm8x4_bgra="unorm8x4-bgra"}(q||(q={}));class ${constructor(t,e,r,s){this._streams=[],this._topology=t,this._frontFace=e,this._streams=r,this._indexBuffer=s,this._numVertices=this._streams[0].numVertices,this._streams.forEach(t=>console.assert(t.numVertices===this._numVertices,"All vertex streams must have the same number of vertices."))}get topology(){return this._topology}get frontFace(){return this._frontFace}get numStreams(){return this._streams.length}get numVertices(){return this._numVertices}getVertexBuffer(t){return this._streams[t].buffer}getStreamStride(t){return this._streams[t].stride}getStreamAttributes(t){return this._streams[t].attributes}get indexBuffer(){return this._indexBuffer?.buffer}get indexFormat(){return this._indexBuffer?.format??k.Uint16}get numIndices(){return this._indexBuffer?.count??0}}class H{constructor(){this.stride=0,this.attributes=[]}pushAttribute(t,e){this.attributes.push({name:t,format:e,offset:this.stride}),this.stride+=function(t){switch(t){case q.Uint8:case q.Sint8:case q.Unorm8:case q.Snorm8:return 1;case q.Uint8x2:case q.Sint8x2:case q.Unorm8x2:case q.Snorm8x2:case q.Uint16:case q.Sint16:case q.Unorm16:case q.Snorm16:case q.Float16:return 2;case q.Uint8x4:case q.Sint8x4:case q.Unorm8x4:case q.Snorm8x4:case q.Uint16x2:case q.Sint16x2:case q.Unorm16x2:case q.Snorm16x2:case q.Float16x2:case q.Float32:case q.Uint32:case q.Sint32:case q.Unorm10_10_10_2:case q.Unorm8x4_bgra:return 4;case q.Uint16x4:case q.Sint16x4:case q.Unorm16x4:case q.Snorm16x4:case q.Float16x4:case q.Float32x2:case q.Uint32x2:case q.Sint32x2:return 8;case q.Float32x3:case q.Uint32x3:case q.Sint32x3:return 12;case q.Float32x4:case q.Uint32x4:case q.Sint32x4:return 16}}(e)}get numVertices(){return this.buffer.size/this.stride}}class X{constructor(t,e){this._stream=t,this._ctx=e}pushAttribute(t,e){return this._stream.pushAttribute(t,e),this}withData(t,e=!1){this._stream.buffer=new y(this._ctx).withData(t,e).withUsage(h.Vertex|h.CopyDst).build()}}class Q{constructor(t){this._topology=N.TriangleList,this._frontFace=W.CounterClockwise,this._streams=[],this._ctx=t}pushStream(t){const e=new H;return t(new X(e,this._ctx)),console.assert(void 0!==e.buffer,"Vertex stream must have a buffer. Did you forget to call withData()?"),this._streams.push(e),this}withFrontFace(t){return this._frontFace=t,this}withTopology(t){return this._topology=t,this}withIndexData(t,e=!1){return this._indexBuffer=m(t,t=>new Y(t,this._ctx,e)),this}build(){return console.assert(this._streams.length>0,"Mesh must have at least one vertex stream."),new $(this._topology,this._frontFace,this._streams,this._indexBuffer)}}class Z{constructor(t){this._inner=t}}class K{constructor(t){this._ctx=t}withAddressMode(t,e,r){return this._addressModeU=t,this._addressModeV=e??t,this._addressModeW=r??t,this}withFiltering(t,e,r){return this._minFilter=t,this._magFilter=e??t,this._mipmapFilter=r??t,this}withTrilinearFiltering(){return this.withFiltering(e.Linear),this}withNearestFiltering(){return this.withFiltering(e.Nearest),this}withAnisotropicFiltering(t){return this.withFiltering(e.Linear),this._maxAnisotropy=t,this}withMinMipLevel(t){return this._minMipLevel=t,this}withMaxMipLevel(t){return this._maxMipLevel=t,this}withCompareFunction(t){return this._compare=t,this}build(){return new Z(this._ctx.device.createSampler({addressModeU:this._addressModeU,addressModeV:this._addressModeV,addressModeW:this._addressModeW,magFilter:this._magFilter,minFilter:this._minFilter,mipmapFilter:this._mipmapFilter,lodMinClamp:this._minMipLevel,lodMaxClamp:this._maxMipLevel,compare:this._compare,maxAnisotropy:this._maxAnisotropy}))}}class j{constructor(t){this._inner=t}}class J{constructor(t){this._ctx=t}withLabel(t){return this._label=t,this}withShader(t,e){return this._shader=t,this._entry=e,this}build(){const t=this._shader,e=m(t._bindGroupLayouts,t=>this._ctx.device.createPipelineLayout({bindGroupLayouts:t.map(t=>t._inner),label:this._label}))??"auto",r={label:this._label,compute:{module:t._inner,entryPoint:this._entry},layout:e};return new j(this._ctx.device.createComputePipeline(r))}}class tt{constructor(t){this._options={},this._shaderIncludes=new Map,this._globalBindBufferLayouts=[],this._globalBindBuffers=[],this._context=new _,this._canvas=t}async initialize(t={}){t.canvas=this._canvas,this._options=t,await this._context.initialize(t),this._createDepthStencil()}resize(t,e){this._canvas.width=t,this._canvas.height=e,this._createDepthStencil()}addShaderInclude(t,e){return this._shaderIncludes.set(t,e),this}get depthStencilFormat(){return this._options.depthStencilFormat}get backbuffer(){if(!this._backbuffer)throw new Error("Backbuffer not initialized. Did you forget to call startFrame()?");return this._backbuffer}get backbufferTarget(){if(!this._backbufferTarget)throw new Error("Backbuffer not initialized. Did you forget to call startFrame()?");return this._backbufferTarget}get colorSpace(){return this._context.colorSpace}get backbufferFormat(){return this._context.format}get backbufferWidth(){return this._canvas.width}get backbufferHeight(){return this._canvas.height}startFrame(){this._backbuffer=x.from_webgpu(this._context.getCurrentTexture(),this._context.format),this._backbufferTarget=this.createRenderTarget(this._backbuffer).build()}createRenderTarget(t){return new A(t)}createShader(){let t=new V(this._context);return this._shaderIncludes.forEach((e,r)=>t=t.withInclude(r,e)),this._globalBindBufferLayouts.forEach((e,r)=>t.withBindGroup(r,e)),t}createBindGroupLayout(){return new D(this._context)}createBindGroup(t){return new L(this._context,t)}createMesh(){return new Q(this._context)}createRenderPipeline(){return new z(this._context,this._options.depthStencilFormat)}createComputePipeline(){return new J(this._context)}createSampler(){return new K(this._context)}createTexture(){return new U(this._context)}createCommandEncoder(t){return new f(this.backbufferTarget,this._globalBindBuffers,this._context,this._depthStencilTarget,t)}createUniformBufferLayout(){return new G}createUniformBuffer(t){return new M(t,this._context)}createBuffer(){return new y(this._context)}setGlobalBindGroup(t,e,r){return this._globalBindBufferLayouts[t]=e,this._globalBindBuffers[t]=r,this}destroy(){this._context.destroy()}depthStencilTexture(){return this._depthStencil}depthStencilTarget(){return this._depthStencilTarget}_createDepthStencil(){this._depthStencil=m(this._options.depthStencilFormat,t=>this.createTexture().withFormat(t).withSize(this._canvas.width,this._canvas.height).withUsage(GPUTextureUsage.RENDER_ATTACHMENT).build()),this._depthStencilTarget=m(this._depthStencil,t=>this.createRenderTarget(t).build())}}class et{get srcFactor(){return this._inner.color.srcFactor}set srcFactor(t){this._inner.color.srcFactor=t}get dstFactor(){return this._inner.color.dstFactor}set dstFactor(t){this._inner.color.dstFactor=t}get alphaSrcFactor(){return this._inner.alpha.srcFactor}set alphaSrcFactor(t){this._inner.alpha.srcFactor=t}get alphaDstFactor(){return this._inner.alpha.dstFactor}set alphaDstFactor(t){this._inner.alpha.dstFactor=t}constructor(t,e,r,s){this._inner={color:{srcFactor:n.SrcAlpha,dstFactor:n.OneMinusSrcAlpha},alpha:{srcFactor:n.One,dstFactor:n.OneMinusSrcAlpha}},this.srcFactor=t,this.dstFactor=e,this.alphaSrcFactor=r,this.alphaDstFactor=s}}et.ALPHA=new et(n.SrcAlpha,n.OneMinusSrcAlpha,n.One,n.OneMinusSrcAlpha),et.ADDITIVE=new et(n.SrcAlpha,n.One,n.One,n.One),et.MULTIPLY=new et(n.DstColor,n.Zero,n.Zero,n.One),et.SCREEN=new et(n.One,n.OneMinusSrcColor,n.One,n.OneMinusSrcAlpha),et.OVERLAY=new et(n.SrcAlpha,n.OneMinusSrcAlpha,n.One,n.OneMinusSrcAlpha),et.DARKEN=new et(n.One,n.OneMinusSrcColor,n.One,n.OneMinusSrcAlpha),et.LIGHTEN=new et(n.OneMinusSrcColor,n.One,n.One,n.OneMinusSrcAlpha);class rt{constructor(t){this._offset=0,this._buffer=new ArrayBuffer(t),this._view=new DataView(this._buffer)}pushUint8(t){return this._view.setUint8(this._offset,t),this._offset+=1,this}pushUint8x2(t,e){return this.pushUint8(t),this.pushUint8(e),this}pushUint8x4(t,e,r,s){return this.pushUint8(t),this.pushUint8(e),this.pushUint8(r),this.pushUint8(s),this}pushSint8(t){return this._view.setInt8(this._offset,t),this._offset+=1,this}pushSint8x2(t,e){return this.pushSint8(t),this.pushSint8(e),this}pushSint8x4(t,e,r,s){return this.pushSint8(t),this.pushSint8(e),this.pushSint8(r),this.pushSint8(s),this}pushUint16(t){return this._view.setUint16(this._offset,t,!0),this._offset+=2,this}pushUint16x2(t,e){return this.pushUint16(t),this.pushUint16(e),this}pushUint16x4(t,e,r,s){return this.pushUint16(t),this.pushUint16(e),this.pushUint16(r),this.pushUint16(s),this}pushSint16(t){return this._view.setInt16(this._offset,t,!0),this._offset+=2,this}pushSint16x2(t,e){return this.pushSint16(t),this.pushSint16(e),this}pushSint16x4(t,e,r,s){return this.pushSint16(t),this.pushSint16(e),this.pushSint16(r),this.pushSint16(s),this}pushFloat16(t){const e=F(t);return this._view.setUint16(this._offset,e,!0),this._offset+=2,this}pushFloat16x2(t,e){return this.pushFloat16(t),this.pushFloat16(e),this}pushFloat16x4(t,e,r,s){return this.pushFloat16(t),this.pushFloat16(e),this.pushFloat16(r),this.pushFloat16(s),this}pushFloat32(t){return this._view.setFloat32(this._offset,t,!0),this._offset+=4,this}pushFloat32x2(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this}pushFloat32x3(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this.pushFloat32(t[2]),this}pushFloat32x4(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this.pushFloat32(t[2]),this.pushFloat32(t[3]),this}pushUint32(t){return this._view.setUint32(this._offset,t,!0),this._offset+=4,this}pushUint32x2(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this}pushUint32x3(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this.pushUint32(t[2]),this}pushUint32x4(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this.pushUint32(t[2]),this.pushUint32(t[3]),this}pushSint32(t){return this._view.setInt32(this._offset,t,!0),this._offset+=4,this}pushSint32x2(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this}pushSint32x3(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this.pushSint32(t[2]),this}pushSint32x4(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this.pushSint32(t[2]),this.pushSint32(t[3]),this}get arrayBuffer(){return this._buffer}}const st=tt;export{r as AddressMode,u as BaseType,C as BindGroup,L as BindGroupBuilder,P as BindGroupLayout,n as BlendFactor,et as BlendMode,B as Buffer,y as BufferBuilder,rt as BufferDataWriter,h as BufferUsage,t as ColorSpace,f as CommandEncoder,a as CompareFunction,b as ComputePass,p as ComputePassBuilder,j as ComputePipeline,J as ComputePipelineBuilder,s as CullMode,e as FilterMode,W as FrontFace,k as IndexFormat,$ as Mesh,Q as MeshBuilder,N as MeshTopology,g as RenderPass,d as RenderPassBuilder,I as RenderPipeline,z as RenderPipelineBuilder,R as RenderTarget,A as RenderTargetBuilder,Z as Sampler,K as SamplerBuilder,O as Shader,V as ShaderBuilder,o as StorageAccess,X as StreamBuilder,x as Texture,U as TextureBuilder,i as TextureFormat,c as TextureUsage,S as TextureView,w as TextureViewBuilder,tt as TinyHelix,M as UniformBuffer,E as UniformBufferLayout,G as UniformBufferLayoutBuilder,q as VertexFormat,_ as WebGPUContext,st as default};
1
+ var t,e,r,s,i,n,a,o,h,c,u,l,_,d,p,m,g,b,f;(function(t){t.sRGB="srgb",t.DisplayP3="display-p3"})(t||(t={})),function(t){t.Nearest="nearest",t.Linear="linear"}(e||(e={})),function(t){t.ClampToEdge="clamp-to-edge",t.Repeat="repeat",t.MirrorRepeat="mirror-repeat"}(r||(r={})),function(t){t.None="none",t.Front="front",t.Back="back"}(s||(s={})),function(t){t.R8Unorm="r8unorm",t.R8Snorm="r8snorm",t.R8Uint="r8uint",t.R8Sint="r8sint",t.R16Unorm="r16unorm",t.R16Snorm="r16snorm",t.R16Uint="r16uint",t.R16Sint="r16sint",t.R16Float="r16float",t.RG8Unorm="rg8unorm",t.RG8Snorm="rg8snorm",t.RG8Uint="rg8uint",t.RG8Sint="rg8sint",t.R32Uint="r32uint",t.R32Sint="r32sint",t.R32Float="r32float",t.RG16Unorm="rg16unorm",t.RG16Snorm="rg16snorm",t.RG16Uint="rg16uint",t.RG16Sint="rg16sint",t.RG16Float="rg16float",t.Rgba8Unorm="rgba8unorm",t.Rgba8UnormSrgb="rgba8unorm-srgb",t.Rgba8Snorm="rgba8snorm",t.Rgba8Uint="rgba8uint",t.Rgba8Sint="rgba8sint",t.Bgra8Unorm="bgra8unorm",t.Bgra8UnormSrgb="bgra8unorm-srgb",t.Rgb9e5Ufloat="rgb9e5ufloat",t.Rgb10a2Uint="rgb10a2uint",t.Rgb10a2Unorm="rgb10a2unorm",t.Rg11b10Ufloat="rg11b10ufloat",t.RG32Uint="rg32uint",t.RG32Sint="rg32sint",t.RG32Float="rg32float",t.Rgba16Unorm="rgba16unorm",t.Rgba16Snorm="rgba16snorm",t.Rgba16Uint="rgba16uint",t.Rgba16Sint="rgba16sint",t.Rgba16Float="rgba16float",t.Rgba32Uint="rgba32uint",t.Rgba32Sint="rgba32sint",t.Rgba32Float="rgba32float",t.Stencil8="stencil8",t.Depth16Unorm="depth16unorm",t.Depth24Plus="depth24plus",t.Depth24PlusStencil8="depth24plus-stencil8",t.Depth32Float="depth32float",t.Depth32FloatStencil8="depth32float-stencil8",t.Bc1RgbaUnorm="bc1-rgba-unorm",t.Bc1RgbaUnormSrgb="bc1-rgba-unorm-srgb",t.Bc2RgbaUnorm="bc2-rgba-unorm",t.Bc2RgbaUnormSrgb="bc2-rgba-unorm-srgb",t.Bc3RgbaUnorm="bc3-rgba-unorm",t.Bc3RgbaUnormSrgb="bc3-rgba-unorm-srgb",t.Bc4RUnorm="bc4-r-unorm",t.Bc4RSnorm="bc4-r-snorm",t.Bc5RgUnorm="bc5-rg-unorm",t.Bc5RgSnorm="bc5-rg-snorm",t.Bc6hRgbUfloat="bc6h-rgb-ufloat",t.Bc6hRgbFloat="bc6h-rgb-float",t.Bc7RgbaUnorm="bc7-rgba-unorm",t.Bc7RgbaUnormSrgb="bc7-rgba-unorm-srgb",t.Etc2Rgb8Unorm="etc2-rgb8unorm",t.Etc2Rgb8UnormSrgb="etc2-rgb8unorm-srgb",t.Etc2Rgb8A1Unorm="etc2-rgb8a1unorm",t.Etc2Rgb8A1UnormSrgb="etc2-rgb8a1unorm-srgb",t.Etc2Rgba8Unorm="etc2-rgba8unorm",t.Etc2Rgba8UnormSrgb="etc2-rgba8unorm-srgb",t.EacR11Unorm="eac-r11unorm",t.EacR11Snorm="eac-r11snorm",t.EacRg11Unorm="eac-rg11unorm",t.EacRg11Snorm="eac-rg11snorm",t.Astc4x4Unorm="astc-4x4-unorm",t.Astc4x4UnormSrgb="astc-4x4-unorm-srgb",t.Astc5x4Unorm="astc-5x4-unorm",t.Astc5x4UnormSrgb="astc-5x4-unorm-srgb",t.Astc5x5Unorm="astc-5x5-unorm",t.Astc5x5UnormSrgb="astc-5x5-unorm-srgb",t.Astc6x5Unorm="astc-6x5-unorm",t.Astc6x5UnormSrgb="astc-6x5-unorm-srgb",t.Astc6x6Unorm="astc-6x6-unorm",t.Astc6x6UnormSrgb="astc-6x6-unorm-srgb",t.Astc8x5Unorm="astc-8x5-unorm",t.Astc8x5UnormSrgb="astc-8x5-unorm-srgb",t.Astc8x6Unorm="astc-8x6-unorm",t.Astc8x6UnormSrgb="astc-8x6-unorm-srgb",t.Astc8x8Unorm="astc-8x8-unorm",t.Astc8x8UnormSrgb="astc-8x8-unorm-srgb",t.Astc10x5Unorm="astc-10x5-unorm",t.Astc10x5UnormSrgb="astc-10x5-unorm-srgb",t.Astc10x6Unorm="astc-10x6-unorm",t.Astc10x6UnormSrgb="astc-10x6-unorm-srgb",t.Astc10x8Unorm="astc-10x8-unorm",t.Astc10x8UnormSrgb="astc-10x8-unorm-srgb",t.Astc10x10Unorm="astc-10x10-unorm",t.Astc10x10UnormSrgb="astc-10x10-unorm-srgb",t.Astc12x10Unorm="astc-12x10-unorm",t.Astc12x10UnormSrgb="astc-12x10-unorm-srgb",t.Astc12x12Unorm="astc-12x12-unorm",t.Astc12x12UnormSrgb="astc-12x12-unorm-srgb"}(i||(i={})),function(t){t.Zero="zero",t.One="one",t.SrcColor="src",t.OneMinusSrcColor="one-minus-src",t.SrcAlpha="src-alpha",t.OneMinusSrcAlpha="one-minus-src-alpha",t.DstColor="dst",t.OneMinusDstColor="one-minus-dst",t.DstAlpha="dst-alpha",t.OneMinusDstAlpha="one-minus-dst-alpha",t.SrcAlphaSaturated="src-alpha-saturated",t.Constant="constant",t.OneMinusConstant="one-minus-constant",t.Src1Color="src1",t.OneMinusSrc1Color="one-minus-src1",t.Src1Alpha="src1-alpha",t.OneMinusSrc1Alpha="one-minus-src1-alpha"}(n||(n={})),function(t){t.Never="never",t.Less="less",t.Equal="equal",t.LessEqual="less-equal",t.Greater="greater",t.NotEqual="not-equal",t.GreaterEqual="greater-equal",t.Always="always"}(a||(a={})),function(t){t.ReadWrite="read-write",t.Read="read-only",t.Write="write-only"}(o||(o={})),function(t){t.Float="float",t.Sint="sint",t.Uint="uint",t.UnfilterableFloat="unfilterable-float",t.Depth="depth"}(h||(h={})),function(t){t.Filtering="filtering",t.NonFiltering="non-filtering",t.Comparison="comparison"}(c||(c={})),function(t){t[t.Red=GPUColorWrite.RED]="Red",t[t.Green=GPUColorWrite.GREEN]="Green",t[t.Blue=GPUColorWrite.BLUE]="Blue"}(u||(u={})),function(t){t.D1="1d",t.D2="2d",t.D3="3d"}(l||(l={})),function(t){t.D1="1d",t.D2="2d",t.D2Array="2d-array",t.Cube="cube",t.CubeArray="cube-array",t.D3="3d"}(_||(_={})),function(t){t[t.Vertex=GPUShaderStage.VERTEX]="Vertex",t[t.Fragment=GPUShaderStage.FRAGMENT]="Fragment",t[t.Compute=GPUShaderStage.COMPUTE]="Compute"}(d||(d={})),function(t){t.Store="store",t.Discard="discard"}(p||(p={}));class x{constructor(){this._adapter=null,this._device=null,this._context=null,this._format=i.Rgba8UnormSrgb,this._canvas=null,this._colorSpace=t.sRGB,this._shaderF16Supported=!1,this._floatFilteringSupported=!1}get adapter(){return this._adapter}get device(){return this._device}get context(){return this._context}get format(){return this._format}get colorSpace(){return this._colorSpace}get canvas(){return this._canvas}static isSupported(){return"undefined"!=typeof navigator&&"gpu"in navigator}async initialize(e={}){if(!x.isSupported())throw new Error("WebGPU is not supported in this browser");if(this._adapter=await navigator.gpu.requestAdapter({powerPreference:e.powerPreference??"high-performance"}),!this._adapter)throw new Error("Failed to get WebGPU adapter");this._shaderF16Supported=this._adapter.features.has("shader-f16"),this._floatFilteringSupported=this._adapter.features.has("float32-filterable");const r=e.requiredFeatures?.includes("shader-f16");if(this._shaderF16Supported)r||(e.requiredFeatures=e.requiredFeatures??[],e.requiredFeatures?.push("shader-f16"));else{if(r)throw new Error("shader-f16 feature requested but not available");console.warn("WebGPU adapter does not support shader-f16 feature. Performance may be suboptimal for certain workloads.")}if(this._floatFilteringSupported&&!e.requiredFeatures?.includes("float32-filterable")&&(e.requiredFeatures=e.requiredFeatures??[],e.requiredFeatures?.push("float32-filterable")),this._device=await this._adapter.requestDevice({requiredFeatures:e.requiredFeatures,requiredLimits:e.requiredLimits}),!this._device)throw new Error("Failed to get WebGPU device");if(this._device.lost.then(t=>{console.error("WebGPU device lost:",t.message,"\nReason:",t.reason),"destroyed"!==t.reason&&this.initialize(e).catch(t=>{console.error("Failed to reinitialize WebGPU context:",t)})}),e.canvas){if(this._canvas=e.canvas,this._context=this._canvas.getContext("webgpu"),!this._context)throw new Error("Failed to get WebGPU context from canvas");const r=window.matchMedia("(color-gamut: p3)").matches;this._colorSpace=e.colorSpace??t.sRGB,this._colorSpace!==t.DisplayP3||r||(this._colorSpace=t.sRGB,console.warn("DisplayP3 not supported, falling back to sRGB")),this._format=navigator.gpu.getPreferredCanvasFormat(),this._context.configure({device:this._device,format:this._format,colorSpace:this._colorSpace,alphaMode:"premultiplied"})}else console.warn("No canvas provided!")}get shaderF16Supported(){return this._shaderF16Supported}get floatFilteringSupported(){return this._floatFilteringSupported}getCurrentTexture(){return this._context.getCurrentTexture()}destroy(){this._device&&(this._device.destroy(),this._device=null),this._adapter=null,this._context=null,this._canvas=null}}function S(t,e){if(void 0!==t)return e(t)}class U{constructor(t){this._numVertices=0,this._numIndices=0,this._inner=t}setPipeline(t){return this._renderPipeline!=t&&(this._inner.setPipeline(t._inner),this._renderPipeline=t),this}setMesh(t){for(let e=0;e<t.numStreams;++e){const r=t.getVertexBuffer(e);this._inner.setVertexBuffer(e,r._inner)}this._numVertices=t.numVertices;const e=t.indexBuffer;return e?(this._inner.setIndexBuffer(e._inner,t.indexFormat),this._numIndices=t.numIndices):this._numIndices=0,this}setBindGroup(t,e){return this._inner.setBindGroup(t,e._inner),this}draw(t=1){return this._numIndices?this._inner.drawIndexed(this._numIndices,t,0,0,0):this._inner.draw(this._numVertices,t,0,0),this}drawIndirect(t,e=0){return this._numIndices?this._inner.drawIndexedIndirect(t._inner,e):this._inner.drawIndirect(t._inner,e),this}end(){this._inner.end()}}class w{constructor(t,e,r,s){this._colorTargets=[],this._clearColors=[],this._storeOps=[],this._encoder=t,this._defaultTarget=r,this._defaultDepthTarget=s,this._globalBindGroups=e}withLabel(t){return this._label=t,this}withColorTarget(t){return this._colorTargets.push(t),this}withDepthStencilTarget(t){return this._depthTarget=t,this}withClearColor(t,e,r,s){let i;return i=void 0===t?[0,0,0,1]:"number"==typeof t?[t,e??0,r??0,s??1]:[t[0],t[1],t[2],t[3]??1],0===this._colorTargets.length?this._clearColors[0]=i:this._clearColors[this._colorTargets.length-1]=i,this}withStoreOp(t){return 0===this._colorTargets.length?this._storeOps[0]=t:this._storeOps[this._colorTargets.length-1]=t,this}withClearStencil(t){return this._clearStencil=t,this}withClearDepth(t){return this._clearDepth=t,this}build(){let t,e,r,s,n;this._colorTargets.length||this._depthTarget?(t=this._colorTargets,e=this._depthTarget):(t=[this._defaultTarget],e=this._defaultDepthTarget),e?.format!==i.Depth32FloatStencil8&&e?.format!==i.Depth24PlusStencil8||(r=null!=this._clearStencil?"clear":"load",s=S(this._clearStencil,()=>"store"),n=this._clearStencil);const a={colorAttachments:t.map((t,e)=>({view:t._inner,loadOp:this._clearColors[e]?"clear":"load",storeOp:this._storeOps[e]??"store",clearValue:this._clearColors[e]})),depthStencilAttachment:S(e,t=>({view:t._inner,depthClearValue:this._clearDepth,depthLoadOp:null!=this._clearDepth?"clear":"load",depthStoreOp:S(this._clearDepth,()=>"store"),stencilClearValue:n,stencilLoadOp:r,stencilStoreOp:s})),label:this._label},o=new U(this._encoder.beginRenderPass(a));return this._globalBindGroups.forEach((t,e)=>o.setBindGroup(e,t)),o}}class R{constructor(t){this._inner=t}setPipeline(t){return this._inner.setPipeline(t._inner),this}dispatch(t,e,r){return this._inner.dispatchWorkgroups(t,e,r),this}dispatchIndirect(t,e){return this._inner.dispatchWorkgroupsIndirect(t._inner,e),this}setBindGroup(t,e){return this._inner.setBindGroup(t,e._inner),this}end(){this._inner.end()}}class y{constructor(t,e){this._encoder=t,this._globalBindGroups=e}withLabel(t){return this._label=t,this}build(){const t={label:this._label},e=new R(this._encoder.beginComputePass(t));for(let t=0;t<this._globalBindGroups.length;++t)e.setBindGroup(t,this._globalBindGroups[t]);return e}}class v{constructor(t,e,r,s,i){this._commandBuffer=null;const n=r.device;this._queue=n.queue,this._backbufferTarget=t,this._depthStencilTarget=s,this._globalBindGroups=e,this._encoder=n.createCommandEncoder({label:i})}clearBuffer(t,e,r){return this._encoder.clearBuffer(t._inner,e,r),this}createRenderPass(){return new w(this._encoder,this._globalBindGroups,this._backbufferTarget,this._depthStencilTarget)}createComputePass(){return new y(this._encoder,this._globalBindGroups)}finish(){this._commandBuffer||(this._commandBuffer=this._encoder.finish())}submit(){this.finish(),this._queue.submit([this._commandBuffer])}}!function(t){t[t.MapRead=GPUBufferUsage.MAP_READ]="MapRead",t[t.MapWrite=GPUBufferUsage.MAP_WRITE]="MapWrite",t[t.CopySrc=GPUBufferUsage.COPY_SRC]="CopySrc",t[t.CopyDst=GPUBufferUsage.COPY_DST]="CopyDst",t[t.Index=GPUBufferUsage.INDEX]="Index",t[t.Vertex=GPUBufferUsage.VERTEX]="Vertex",t[t.Uniform=GPUBufferUsage.UNIFORM]="Uniform",t[t.Storage=GPUBufferUsage.STORAGE]="Storage",t[t.Indirect=GPUBufferUsage.INDIRECT]="Indirect",t[t.QueryResolve=GPUBufferUsage.QUERY_RESOLVE]="QueryResolve"}(m||(m={})),function(t){t[t.CopySrc=GPUTextureUsage.COPY_SRC]="CopySrc",t[t.CopyDst=GPUTextureUsage.COPY_DST]="CopyDst",t[t.TextureBinding=GPUTextureUsage.TEXTURE_BINDING]="TextureBinding",t[t.StorageBinding=GPUTextureUsage.STORAGE_BINDING]="StorageBinding",t[t.RenderAttachment=GPUTextureUsage.RENDER_ATTACHMENT]="RenderAttachment"}(g||(g={}));class F{constructor(t){this._size=0,this._keepData=!1,this._usage=0,this._dataOffset=0,this._ctx=t}withUsage(t){return this._usage|=t,this}withData(t,e=!1,r=0,s=t.byteLength){return this._data=t,this._size=s??t.byteLength-r,this._keepData=e,this._dataOffset=r,this._usage|=m.CopyDst,this}withSize(t){return this._size=t,this}build(){const t=S(this._data,t=>function(t,e,r,s){const i=4*Math.ceil(s/4);if(0===r&&s===t.byteLength&&i===s)return t;const n=t instanceof ArrayBuffer?new ArrayBuffer(i):new SharedArrayBuffer(i),a=new Uint8Array(t,r,s);return new Uint8Array(n).set(a),n}(t,0,this._dataOffset,this._size));this._keepData&&t&&(this._data=t),this._size=t?.byteLength??this._size;const e=this._ctx.device.createBuffer({size:this._size,usage:this._usage});return t&&this._ctx.device.queue.writeBuffer(e,0,t),new A(e,this._keepData?this._data:void 0)}}class A{constructor(t,e){this._inner=t,this.data=e}get size(){return this._inner.size}_uploadData(t){this.data?t.device.queue.writeBuffer(this._inner,0,this.data):console.warn("Buffer has no data to upload.")}_getBufferResource(){return{buffer:this._inner}}}class B{constructor(t,r,s){this._bindgroups=[],this._views=[],this._passDescs=[],this._ctx=t;const i=t.device.createSampler({label:"MipRenderer Bilinear Sampler",magFilter:e.Linear,minFilter:e.Linear,mipmapFilter:e.Nearest});this._bindgroupLayout=t.device.createBindGroupLayout({label:"MipRenderer BindGroup Layout",entries:[{binding:0,visibility:GPUShaderStage.FRAGMENT,texture:{sampleType:"float",viewDimension:s.dimension(),multisampled:!1}},{binding:1,visibility:GPUShaderStage.FRAGMENT,sampler:{type:"filtering"}}]}),this._pipeline=t.device.createRenderPipeline({label:"MipRenderer Pipeline",layout:t.device.createPipelineLayout({label:"MipRenderer Pipeline Layout",bindGroupLayouts:[this._bindgroupLayout]}),vertex:{module:r,entryPoint:"vs_main"},fragment:{module:r,entryPoint:"fs_main",targets:[{format:s.format}]},primitive:{topology:"triangle-list",stripIndexFormat:void 0,frontFace:"ccw",cullMode:"none"}});for(let e=0;e<s.mipLevelCount;++e)this._views.push(s.createView().withSingleMip(e).withUsage(g.RenderAttachment).withUsage(g.TextureBinding).build()._inner),0!==e&&(this._bindgroups.push(t.device.createBindGroup({label:`MipRenderer BindGroup Mip ${e}`,layout:this._bindgroupLayout,entries:[{binding:0,resource:this._views[e-1]},{binding:1,resource:i}]})),this._passDescs.push({label:`MipRenderer Render Pass mip ${e}`,colorAttachments:[{view:this._views[e],loadOp:"clear",storeOp:"store",clearValue:{r:0,g:0,b:0,a:0}}]}))}render(){const t=this._ctx.device.createCommandEncoder({label:"MipRenderer Command Encoder"});this._passDescs.forEach((e,r)=>{const s=t.beginRenderPass(e);s.setPipeline(this._pipeline),s.setBindGroup(0,this._bindgroups[r]),s.draw(3,1,0,0),s.end()}),this._ctx.device.queue.submit([t.finish()])}}class C{static from_webgpu(t,e,r,s){return new C(t,e,r,s)}constructor(t,e,r,s){this._inner=t,this._format=e,this._ctx=r,this._mipper=S(s,t=>new B(r,t,this))}createView(){return new L(this)}get format(){return this._format}get mipLevelCount(){return this._inner.mipLevelCount}get width(){return this._inner.width}get height(){return this._inner.height}get depthOrArrayLayers(){return this._inner.depthOrArrayLayers}_getBufferResource(){return this._inner.createView()}uploadImage(t,e=0){this._ctx.device.queue.copyExternalImageToTexture({source:t},{texture:this._inner,mipLevel:e},[t.width,t.height,1])}uploadData(t,e=0){const r=Math.max(this._inner.width>>e,1),s=Math.max(this._inner.height>>e,1),i=Math.max(this._inner.depthOrArrayLayers>>e,1),n=T.getBlockWidth(this._format),a=Math.ceil(r/n)*T.bytesPerBlock(this._format);this._ctx.device.queue.writeTexture({texture:this._inner,mipLevel:e},t,{bytesPerRow:a,rowsPerImage:s},{width:r,height:s,depthOrArrayLayers:i})}generateMipmaps(){if(this._inner.dimension!==l.D2)throw new Error("generateMipmaps currently only supports 2D textures.");this._mipper.render()}dimension(){switch(this._inner.dimension){case"1d":return l.D1;case"2d":return l.D2;case"3d":return l.D3;default:throw new Error(`Unknown texture dimension: ${this._inner.dimension}`)}}}class G{constructor(t,e){this._size=[1,1,1],this._format=i.Rgba8UnormSrgb,this._usage=GPUTextureUsage.TEXTURE_BINDING,this._dimension=l.D2,this._mipLevelCount=1,this._generateMips=!1,this._mipShader=e,this._ctx=t}withSize(t,e,r=1){return this._size=[t,e,r],r>1&&(this._dimension=l.D3),this}withDimension(t){return this._dimension=t,this}withMipLevels(t){return this._mipLevelCount=t??-1,this}withFormat(t){return this._format=t,this}withData(t,e=0){return this._data=this._data??[],this._data[e]=t,this._mipLevelCount=Math.max(this._mipLevelCount,e+1),this}withImage(t,e=0){return this._data=this._data??[],this._data[e]=t,0===e&&(this._size=[t.width,t.height,1]),this._mipLevelCount=Math.max(this._mipLevelCount,e+1),this}withGeneratedMipmaps(){return this._mipLevelCount=-1,this._generateMips=!0,this}withUsage(t){return this._usage|=t,this}build(){if(this._data&&(this._usage|=GPUTextureUsage.COPY_DST),this._generateMips&&(this._usage|=GPUTextureUsage.RENDER_ATTACHMENT),-1==this._mipLevelCount){const t=Math.max(this._size[0],this._size[1],this._size[2]);this._mipLevelCount=Math.floor(Math.log2(t))+1}const t=this._ctx.device.createTexture({format:this._format,size:this._size,usage:this._usage,dimension:this._dimension,mipLevelCount:this._mipLevelCount}),e=new C(t,this._format,this._ctx,this._generateMips?this._mipShader:void 0);if(this._data){const t=this._generateMips?1:this._mipLevelCount;for(let r=0;r<t;r++){const t=this._data[r];if(!t)throw new Error(`TextureBuilder: Missing data for mip level ${r}`);t instanceof ImageBitmap?e.uploadImage(t,r):e.uploadData(t,r)}this._generateMips&&e.generateMipmaps()}return e}}class E{constructor(t){this._inner=t}}class L{constructor(t){this._desc={},this._texture=t}withUsage(t){return void 0===this._desc.usage&&(this._desc.usage=0),this._desc.usage|=t,this}withSingleMip(t){return this._desc.baseMipLevel=t,this._desc.mipLevelCount=1,this}withMipRange(t,e){return this._desc.baseMipLevel=t,this._desc.mipLevelCount=e-t,this}withSingleLayer(t){return this._desc.baseArrayLayer=t,this._desc.arrayLayerCount=1,this}withLayerRange(t,e){return this._desc.baseArrayLayer=t,this._desc.arrayLayerCount=e,this}withDimension(t){return this._desc.dimension=t,this}build(){return new E(this._texture._inner.createView(this._desc))}}class T{static isBc(t){switch(t){case i.Bc1RgbaUnorm:case i.Bc1RgbaUnormSrgb:case i.Bc2RgbaUnorm:case i.Bc2RgbaUnormSrgb:case i.Bc3RgbaUnorm:case i.Bc3RgbaUnormSrgb:case i.Bc4RUnorm:case i.Bc4RSnorm:case i.Bc5RgUnorm:case i.Bc5RgSnorm:case i.Bc6hRgbUfloat:case i.Bc6hRgbFloat:case i.Bc7RgbaUnorm:case i.Bc7RgbaUnormSrgb:return!0;default:return!1}}static isEtc(t){switch(t){case i.Etc2Rgb8Unorm:case i.Etc2Rgb8UnormSrgb:case i.Etc2Rgb8A1Unorm:case i.Etc2Rgb8A1UnormSrgb:case i.Etc2Rgba8Unorm:case i.Etc2Rgba8UnormSrgb:case i.EacR11Unorm:case i.EacR11Snorm:case i.EacRg11Unorm:case i.EacRg11Snorm:return!0;default:return!1}}static getBlockWidth(t){if(T.isBc(t)||T.isEtc(t))return 4;switch(t){case i.Astc4x4Unorm:case i.Astc4x4UnormSrgb:return 4;case i.Astc5x4Unorm:case i.Astc5x4UnormSrgb:case i.Astc5x5Unorm:case i.Astc5x5UnormSrgb:return 5;case i.Astc6x5Unorm:case i.Astc6x5UnormSrgb:case i.Astc6x6Unorm:case i.Astc6x6UnormSrgb:return 6;case i.Astc8x5Unorm:case i.Astc8x5UnormSrgb:case i.Astc8x6Unorm:case i.Astc8x6UnormSrgb:case i.Astc8x8Unorm:case i.Astc8x8UnormSrgb:return 8;case i.Astc10x5Unorm:case i.Astc10x5UnormSrgb:case i.Astc10x6Unorm:case i.Astc10x6UnormSrgb:case i.Astc10x8Unorm:case i.Astc10x8UnormSrgb:case i.Astc10x10Unorm:case i.Astc10x10UnormSrgb:return 10;case i.Astc12x10Unorm:case i.Astc12x10UnormSrgb:case i.Astc12x12Unorm:case i.Astc12x12UnormSrgb:return 12;default:return 1}}static bytesPerBlock(t){switch(t){case i.R8Unorm:case i.R8Snorm:case i.R8Uint:case i.R8Sint:case i.Stencil8:return 1;case i.R16Uint:case i.R16Sint:case i.R16Unorm:case i.R16Snorm:case i.R16Float:case i.RG8Unorm:case i.RG8Snorm:case i.RG8Uint:case i.RG8Sint:case i.Depth16Unorm:return 2;case i.R32Uint:case i.R32Sint:case i.R32Float:case i.RG16Uint:case i.RG16Sint:case i.RG16Unorm:case i.RG16Snorm:case i.RG16Float:case i.Rgba8Unorm:case i.Rgba8UnormSrgb:case i.Rgba8Snorm:case i.Rgba8Uint:case i.Rgba8Sint:case i.Bgra8Unorm:case i.Bgra8UnormSrgb:case i.Rgb9e5Ufloat:case i.Rgb10a2Uint:case i.Rgb10a2Unorm:case i.Depth24Plus:case i.Depth24PlusStencil8:case i.Depth32Float:return 4;case i.Rg11b10Ufloat:case i.RG32Uint:case i.RG32Sint:case i.RG32Float:case i.Rgba16Uint:case i.Rgba16Sint:case i.Rgba16Unorm:case i.Rgba16Snorm:case i.Rgba16Float:case i.Depth32FloatStencil8:case i.Bc1RgbaUnorm:case i.Bc1RgbaUnormSrgb:case i.Bc4RUnorm:case i.Bc4RSnorm:case i.Etc2Rgb8Unorm:case i.Etc2Rgb8UnormSrgb:case i.Etc2Rgb8A1Unorm:case i.Etc2Rgb8A1UnormSrgb:case i.EacR11Unorm:case i.EacR11Snorm:return 8;case i.Rgba32Uint:case i.Rgba32Sint:case i.Rgba32Float:case i.Bc2RgbaUnorm:case i.Bc2RgbaUnormSrgb:case i.Bc3RgbaUnorm:case i.Bc3RgbaUnormSrgb:case i.Bc5RgUnorm:case i.Bc5RgSnorm:case i.Bc6hRgbUfloat:case i.Bc6hRgbFloat:case i.Bc7RgbaUnorm:case i.Bc7RgbaUnormSrgb:case i.Etc2Rgba8Unorm:case i.Etc2Rgba8UnormSrgb:case i.EacRg11Unorm:case i.EacRg11Snorm:case i.Astc4x4Unorm:case i.Astc4x4UnormSrgb:case i.Astc5x4Unorm:case i.Astc5x4UnormSrgb:case i.Astc5x5Unorm:case i.Astc5x5UnormSrgb:case i.Astc6x5Unorm:case i.Astc6x5UnormSrgb:case i.Astc6x6Unorm:case i.Astc6x6UnormSrgb:case i.Astc8x5Unorm:case i.Astc8x5UnormSrgb:case i.Astc8x6Unorm:case i.Astc8x6UnormSrgb:case i.Astc8x8Unorm:case i.Astc8x8UnormSrgb:case i.Astc10x5Unorm:case i.Astc10x5UnormSrgb:case i.Astc10x6Unorm:case i.Astc10x6UnormSrgb:case i.Astc10x8Unorm:case i.Astc10x8UnormSrgb:case i.Astc10x10Unorm:case i.Astc10x10UnormSrgb:case i.Astc12x10Unorm:case i.Astc12x10UnormSrgb:case i.Astc12x12Unorm:case i.Astc12x12UnormSrgb:case i.Bc2RgbaUnorm:return 16;default:throw new Error(`Unsupported texture format: ${t}`)}}}class M{constructor(t,e){this._inner=t,this._format=e}get format(){return this._format}}class D{constructor(t){this._baseMipLevel=0,this._baseArrayLayer=0,this._texture=t}withMipLevel(t){return this._baseMipLevel=t,this}withArrayLayer(t){return this._baseArrayLayer=t,this}build(){const t=this._texture._inner,e=this._texture._inner.createView({format:t.format,dimension:t.dimension,aspect:"all",baseMipLevel:this._baseMipLevel,mipLevelCount:1,baseArrayLayer:this._baseArrayLayer,arrayLayerCount:1,usage:GPUTextureUsage.RENDER_ATTACHMENT});return new M(e,this._texture.format)}}function P(t){const e=new Float32Array(1);e[0]=t;const r=new Uint32Array(e.buffer)[0],s=r>>16&32768,i=(r>>23&255)-127;return i<=-15?s:i>16?31744|s:s|i+15<<10|(8388607&r)>>13}function O(t){switch(t){case b.Float16:return 2;case b.Float32:case b.Uint:case b.Sint:return 4;case b.Boolean:return 1;default:throw new Error("Unknown BaseType.")}}!function(t){t[t.Float16=0]="Float16",t[t.Float32=1]="Float32",t[t.Uint=2]="Uint",t[t.Sint=3]="Sint",t[t.Boolean=4]="Boolean"}(b||(b={})),function(t){t[t.Scalar=0]="Scalar",t[t.Vec=1]="Vec",t[t.Matrix=2]="Matrix",t[t.Array=3]="Array"}(f||(f={}));class V{constructor(t,e,r,s){if(this.numRows=1,this.numCols=1,e===f.Scalar&&1!==r&&1!==s)throw new Error("Scalar member must have numRows = 1.");this.baseType=t,this.containerType=e,this.numRows=r,this.numCols=s}get size(){const t=this.numCols>1&&3===this.numRows?4:this.numRows;return O(this.baseType)*t*this.numCols}get alignment(){const t=3===this.numRows?4:this.numRows;return O(this.baseType)*t}}class I{constructor(){this._size=0,this._members=new Map}withSize(t){if(0!==this._size)throw new Error("Size already set.");return this._size=t,this}pushScalar(t,e){return this._pushField(t,e,f.Scalar)}pushVec2(t,e){return this._pushField(t,e,f.Vec,2)}pushVec3(t,e){return this._pushField(t,e,f.Vec,3)}pushVec4(t,e){return this._pushField(t,e,f.Vec,4)}pushMatrix(t,e,r,s){if(!Number.isInteger(e)||!Number.isInteger(r))throw new Error("Matrix dimensions must be integers");if(e<2||e>4||r<2||r>4)throw new Error("Matrix dimensions must be between 2 and 4 (inclusive)");return this._pushField(t,s,f.Matrix,e,r)}build(){return this._size=16*Math.ceil(this._size/16),new k(this._size,this._members)}_pushField(t,e,r,s=1,i=1){const n=new V(t,r,s,i),a=n.alignment,o=Math.ceil(this._size/a)*a;return this._members.set(e,{offset:o,type:n}),this._size=o+n.size,this}}class k{constructor(t,e){this.size=t,this._members=e}_createDataViews(t){const e=new Map;for(const[r,s]of this._members){let i;const n=s.type.size;let a=4;switch(s.type.baseType){case b.Float16:a=2,i=Uint16Array;break;case b.Float32:i=Float32Array;break;case b.Uint:case b.Boolean:i=Uint32Array;break;case b.Sint:i=Int32Array}e.set(r,[s,new i(t,s.offset,n/a)])}return e}_getMemberOrThrow(t){const e=this._members.get(t);if(!e)throw new Error(`Member ${t} not found in layout.`);return e}}class z{constructor(t,e){this._dataViews=new Map;const r=new ArrayBuffer(t.size);this._buffer=new F(e).withUsage(GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST).withData(r,!0).build(),this._data=this._buffer.data,this._dataViews=t._createDataViews(this._data),this._ctx=e}setScalar(t,e){const[r,s]=this._dataViews.get(t);return r.type.baseType===b.Float16?s[0]=P(e):s[0]=e,this}setVec(t,e){const[r,s]=this._dataViews.get(t);for(let t=0;t<s.length;++t)s[t]=0;if(r.type.baseType==b.Float16)for(let t=0;t<e.length;++t)s[t]=P(e[t]);else for(let t=0;t<e.length;++t)s[t]=e[t];return this}setMatrix(t,e){const[r,s]=this._dataViews.get(t),i=r.type.numCols,n=r.type.numRows,a=3===n;for(let t=0;t<s.length;++t)s[t]=0;let o=0;if(r.type.baseType===b.Float16)for(let t=0;t<i;++t){for(let t=0;t<n;++t)s[o]=P(e[o]),++o;a&&++o}else for(let t=0;t<i;++t){for(let t=0;t<n;++t)s[o]=e[o],++o;a&&++o}return this}upload(){return this._buffer._uploadData(this._ctx),this}_getBufferResource(){return{buffer:this._buffer._inner}}}class N{constructor(t){this._inner=t}}class W{constructor(t,e){this._entries=[],this._ctx=t,this._layout=e}withLabel(t){return this._label=t,this}withBuffer(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._getBufferResource()},this}withTexture(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._inner},this}withSampler(t,e){const r=this._layout._getBindingIndex(t);return this._entries[r]={binding:r,resource:e._inner},this}build(){const t=this._ctx.device.createBindGroup({label:this._label,entries:this._entries,layout:this._layout._inner});return new N(t)}}class q{constructor(t,e,r,s){this._inner=t,this._indices=r,this._uboLayouts=s,this._ctx=e}getUniformBufferLayout(t){return this._uboLayouts.get(t)}createUniformBuffer(t){return S(this._uboLayouts.get(t),t=>new z(t,this._ctx))}_getBindingIndex(t){const e=this._indices.get(t);if(void 0===e)throw`No field name found for ${t} in bind group layout.`;return e}}const H=class{constructor(t){this._entries=[],this._indices=new Map,this._uboLayouts=new Map,this._ctx=t}withLabel(t){return this._label=t,this}withUniformBuffer(t,e,r,s){return this._entries[t]={binding:t,visibility:s??d.Vertex|d.Fragment|d.Compute,buffer:{type:"uniform"}},this._indices.set(e,t),this._uboLayouts.set(e,r),this}withStorageBuffer(t,e,r,s=4,i){let n="storage";return r===o.Read&&(n="read-only-storage"),this._entries[t]={binding:t,visibility:i??d.Fragment|d.Compute,buffer:{type:n,hasDynamicOffset:!1,minBindingSize:s}},this._indices.set(e,t),this}withStorageTexture(t,e,r,s,i,n){return this._entries[t]={binding:t,visibility:i??d.Fragment|d.Compute,storageTexture:{access:s,format:r,viewDimension:n??"2d"}},this._indices.set(e,t),this}withTexture(t,e,r,s,i=h.Float){return this._entries[t]={binding:t,visibility:r??d.Fragment|d.Compute,texture:{sampleType:i,viewDimension:s??"2d"}},this._indices.set(e,t),this}withSampler(t,e,r=c.Filtering,s){return this._entries[t]={binding:t,visibility:s??d.Fragment|d.Compute,sampler:{type:r}},this._indices.set(e,t),this}build(){const t=this._ctx.device.createBindGroupLayout({label:this._label,entries:this._entries});return new q(t,this._ctx,this._indices,this._uboLayouts)}};class Y{constructor(t,e,r,s){this._inner=t,this._ctx=e,this._vertexAttributes=r,this._bindGroupLayouts=s}getCompilationInfo(){return this._inner.getCompilationInfo()}getVertexAttributeLocation(t){return this._vertexAttributes.get(t)??void 0}hasVertexAttribute(t){return this._vertexAttributes.has(t)}createBindGroup(t){return new W(this._ctx,this._bindGroupLayouts[t])}}class ${constructor(t){this._vertexAttributes=new Map,this._includes=new Map,this._ctx=t}withLabel(t){return this._label=t,this}withCode(t){return this._code=t,this}withInclude(t,e){return this._includes.set(t,e),this}withVertexAttribute(t,e){return this._vertexAttributes.set(e,t),this}withBindGroup(t,e){if(this._bindGroupLayouts=this._bindGroupLayouts??[],e instanceof q)this._bindGroupLayouts[t]=e;else{const r=new H(this._ctx);e(r),this._bindGroupLayouts[t]=r.build()}return this}build(){if(!this._code)throw new Error("Shader code not specified. Use withCode() to set the shader source.");let t=(this._ctx.shaderF16Supported?"enable f16;\n alias half = f16;\n alias vec2h = vec2<f16>;\n alias vec3h = vec3<f16>;\n alias vec4h = vec4<f16>;\n ":"alias half = f32;\n alias vec2h = vec2<f32>; \n alias vec3h = vec3<f32>;\n alias vec4h = vec4<f32>;\n ")+"\n\n"+this._code;for(const[e,r]of this._includes){const s=e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),i=new RegExp(`#\\s*include\\s*<\\s*${s}\\s*>`,"g");t=t.replace(i,r)}const e=this._ctx.device.createShaderModule({label:this._label,code:t});return new Y(e,this._ctx,this._vertexAttributes,this._bindGroupLayouts)}}class X{constructor(t,e){this._inner=t,this._shader=e}getVertexAttributeLocation(t){return this._shader.getVertexAttributeLocation(t)}}class Q{constructor(t,e){this._colorTargets=[],this._vertexEntry=void 0,this._fragmentEntry=void 0,this._label=void 0,this._overrideConstants={},this._cullMode=s.Back,this._mesh=void 0,this._ctx=t,this._defaultColorState=[{format:t.format,blend:void 0,writeMask:GPUColorWrite.ALL}],this._defaultDepthState=S(e,t=>({format:t,depthWriteEnabled:!0,depthCompare:"less"}))}withLabel(t){return this._label=t,this}withCullMode(t){return this._cullMode=t,this}withMesh(t){return this._mesh=t,this}withShader(t){return this._shader=t,this}withVertexShader(t){return this._vertexEntry=t,this}withFragmentShader(t){return this._fragmentEntry=t,this}withColorTarget(t){return this._colorTargets.push({format:t,blend:void 0,writeMask:GPUColorWrite.ALL}),this}withDepthTarget(t){return this._depthState={format:t,...this._defaultDepthState},this}withDepthCompare(t){const e=this._depthState??this._defaultDepthState;return e?e.depthCompare=t:console.warn("No depth target set. Setting depth compare function has no effect."),this}withDepthBias(t,e,r){const s=this._depthState??this._defaultDepthState;return s?(s.depthBias=t,s.depthBiasSlopeScale=e,s.depthBiasClamp=r):console.warn("No depth target set. Setting depth compare function has no effect."),this}withDepthWrite(t){const e=this._depthState??this._defaultDepthState;return e?e.depthWriteEnabled=t:console.warn("No depth target set. Setting depth write enabled has no effect."),this}withOverrideConstant(t,e,r){return r&GPUShaderStage.VERTEX&&(this._overrideConstants[d.Vertex]=this._overrideConstants[d.Vertex]||{},this._overrideConstants[d.Vertex][t]=e),r&GPUShaderStage.FRAGMENT&&(this._overrideConstants[d.Fragment]=this._overrideConstants[d.Fragment]||{},this._overrideConstants[d.Fragment][t]=e),this}withBlendMode(t){return this.lastColorTarget.blend=t._inner,this}withColorWrite(t,e,r,s){return e??(e=t),r??(r=e),s??(s=r),this.lastColorTarget.writeMask=(t?GPUColorWrite.RED:0)|(e?GPUColorWrite.GREEN:0)|(r?GPUColorWrite.BLUE:0)|(s?GPUColorWrite.ALPHA:0),this}build(){if(!this._shader)throw new Error("Shader not specified. Use withShader() to set the shader to use.");const t=this._shader;if(void 0===this._vertexEntry)throw new Error("Shader does not contain a vertex entry point. Use ShaderBuilder.withVertexShader() to set one.");let e,r;this._colorTargets.length||this._depthState?(e=this._colorTargets,r=this._depthState):(e=this._defaultColorState,r=this._defaultDepthState);const s=S(this._mesh,t=>({topology:t.topology,frontFace:t.frontFace,cullMode:this._cullMode})),i=S(this._mesh,e=>{const r=[];for(let s=0;s<e.numStreams;++s){const i=e.getStreamAttributes(s).filter(e=>t.hasVertexAttribute(e.name)).map(e=>({shaderLocation:t.getVertexAttributeLocation(e.name),offset:e.offset,format:e.format}));r.push({arrayStride:e.getStreamStride(s),stepMode:"vertex",attributes:i})}return r}),n=S(t._bindGroupLayouts,t=>this._ctx.device.createPipelineLayout({bindGroupLayouts:t.map(t=>t._inner),label:this._label}))??"auto",a={label:this._label,layout:n,primitive:s,depthStencil:r,vertex:{buffers:i,module:t._inner,entryPoint:this._vertexEntry,constants:this._overrideConstants[d.Vertex]},fragment:S(this._fragmentEntry,r=>({module:t._inner,entryPoint:r,targets:e,constants:this._overrideConstants[d.Fragment]}))},o=this._ctx.device.createRenderPipeline(a);return new X(o,t)}get lastColorTarget(){return this._colorTargets.length?this._colorTargets[this._colorTargets.length-1]:this._defaultColorState[0]}}var Z,K,j,J;!function(t){t.Uint16="uint16",t.Uint32="uint32"}(Z||(Z={}));class tt{constructor(t,e,r=!1){const s=t.BYTES_PER_ELEMENT;this.format=2===s?Z.Uint16:Z.Uint32,this.count=t.length,this.buffer=new F(e).withData(t.buffer,r,t.byteOffset,t.byteLength).withUsage(m.CopyDst|m.Index).build()}}!function(t){t.TriangleList="triangle-list",t.TriangleStrip="triangle-strip",t.LineList="line-list",t.LineStrip="line-strip",t.PointList="point-list"}(K||(K={})),function(t){t.Clockwise="cw",t.CounterClockwise="ccw"}(j||(j={})),function(t){t.Uint8="uint8",t.Uint8x2="uint8x2",t.Uint8x4="uint8x4",t.Sint8="sint8",t.Sint8x2="sint8x2",t.Sint8x4="sint8x4",t.Unorm8="unorm8",t.Unorm8x2="unorm8x2",t.Unorm8x4="unorm8x4",t.Snorm8="snorm8",t.Snorm8x2="snorm8x2",t.Snorm8x4="snorm8x4",t.Uint16="uint16",t.Uint16x2="uint16x2",t.Uint16x4="uint16x4",t.Sint16="sint16",t.Sint16x2="sint16x2",t.Sint16x4="sint16x4",t.Unorm16="unorm16",t.Unorm16x2="unorm16x2",t.Unorm16x4="unorm16x4",t.Snorm16="snorm16",t.Snorm16x2="snorm16x2",t.Snorm16x4="snorm16x4",t.Float16="float16",t.Float16x2="float16x2",t.Float16x4="float16x4",t.Float32="float32",t.Float32x2="float32x2",t.Float32x3="float32x3",t.Float32x4="float32x4",t.Uint32="uint32",t.Uint32x2="uint32x2",t.Uint32x3="uint32x3",t.Uint32x4="uint32x4",t.Sint32="sint32",t.Sint32x2="sint32x2",t.Sint32x3="sint32x3",t.Sint32x4="sint32x4",t.Unorm10_10_10_2="unorm10-10-10-2",t.Unorm8x4_bgra="unorm8x4-bgra"}(J||(J={}));class et{constructor(t,e,r,s){this._streams=[],this._topology=t,this._frontFace=e,this._streams=r,this._indexBuffer=s,this._numVertices=this._streams[0].numVertices,this._streams.forEach(t=>console.assert(t.numVertices===this._numVertices,"All vertex streams must have the same number of vertices."))}get topology(){return this._topology}get frontFace(){return this._frontFace}get numStreams(){return this._streams.length}get numVertices(){return this._numVertices}getVertexBuffer(t){return this._streams[t].buffer}getStreamStride(t){return this._streams[t].stride}getStreamAttributes(t){return this._streams[t].attributes}get indexBuffer(){return this._indexBuffer?.buffer}get indexFormat(){return this._indexBuffer?.format??Z.Uint16}get numIndices(){return this._indexBuffer?.count??0}}class rt{constructor(){this.stride=0,this.attributes=[]}pushAttribute(t,e){this.attributes.push({name:t,format:e,offset:this.stride}),this.stride+=function(t){switch(t){case J.Uint8:case J.Sint8:case J.Unorm8:case J.Snorm8:return 1;case J.Uint8x2:case J.Sint8x2:case J.Unorm8x2:case J.Snorm8x2:case J.Uint16:case J.Sint16:case J.Unorm16:case J.Snorm16:case J.Float16:return 2;case J.Uint8x4:case J.Sint8x4:case J.Unorm8x4:case J.Snorm8x4:case J.Uint16x2:case J.Sint16x2:case J.Unorm16x2:case J.Snorm16x2:case J.Float16x2:case J.Float32:case J.Uint32:case J.Sint32:case J.Unorm10_10_10_2:case J.Unorm8x4_bgra:return 4;case J.Uint16x4:case J.Sint16x4:case J.Unorm16x4:case J.Snorm16x4:case J.Float16x4:case J.Float32x2:case J.Uint32x2:case J.Sint32x2:return 8;case J.Float32x3:case J.Uint32x3:case J.Sint32x3:return 12;case J.Float32x4:case J.Uint32x4:case J.Sint32x4:return 16}}(e)}get numVertices(){return this.buffer.size/this.stride}}class st{constructor(t,e){this._stream=t,this._ctx=e}pushAttribute(t,e){return this._stream.pushAttribute(t,e),this}withData(t,e=0,r,s=!1){r=r??t.byteLength-e,this._stream.buffer=new F(this._ctx).withData(t,s,e,r).withUsage(m.Vertex|m.CopyDst).build()}}class it{constructor(t){this._topology=K.TriangleList,this._frontFace=j.CounterClockwise,this._streams=[],this._ctx=t}pushStream(t){const e=new rt;return t(new st(e,this._ctx)),console.assert(void 0!==e.buffer,"Vertex stream must have a buffer. Did you forget to call withData()?"),this._streams.push(e),this}withFrontFace(t){return this._frontFace=t,this}withTopology(t){return this._topology=t,this}withIndexData(t,e=!1){return this._indexBuffer=S(t,t=>new tt(t,this._ctx,e)),this}build(){return console.assert(this._streams.length>0,"Mesh must have at least one vertex stream."),new et(this._topology,this._frontFace,this._streams,this._indexBuffer)}}class nt{constructor(t){this._inner=t}}class at{constructor(t){this._desc={},this._ctx=t}withLabel(t){return this._desc.label=t,this}withAddressMode(t,e,r){return this._desc.addressModeU=t,this._desc.addressModeV=e??t,this._desc.addressModeW=r??t,this}withFiltering(t,e,r){return this._desc.minFilter=t,this._desc.magFilter=e??t,this._desc.mipmapFilter=r??t,this}withTrilinearFiltering(){return this.withFiltering(e.Linear),this}withNearestFiltering(){return this.withFiltering(e.Nearest),this}withAnisotropicFiltering(t){return this.withFiltering(e.Linear),this._desc.maxAnisotropy=t,this}withMinMipLevel(t){return this._desc.lodMinClamp=t,this}withMaxMipLevel(t){return this._desc.lodMaxClamp=t,this}withCompareFunction(t){return this._desc.compare=t,this}build(){return new nt(this._ctx.device.createSampler(this._desc))}}class ot{constructor(t){this._inner=t}}class ht{constructor(t){this._overrideConstants={},this._ctx=t}withLabel(t){return this._label=t,this}withOverrideConstant(t,e){return this._overrideConstants[t]=e,this}withShader(t,e){return this._shader=t,this._entry=e,this}build(){const t=this._shader,e=S(t._bindGroupLayouts,t=>this._ctx.device.createPipelineLayout({bindGroupLayouts:t.map(t=>t._inner),label:this._label}))??"auto",r={label:this._label,compute:{module:t._inner,entryPoint:this._entry,constants:this._overrideConstants},layout:e};return new ot(this._ctx.device.createComputePipeline(r))}}class ct{constructor(t){this._parent=null,this._options={},this._shaderIncludes=new Map,this._globalBindGroupLayouts=[],this._globalBindGroups=[],t instanceof ct?(this._context=t._context,this._canvas=t._canvas,this._mipShader=t._mipShader,this._depthStencil=t._depthStencil,this._depthStencilTarget=t._depthStencilTarget):(this._context=new x,this._canvas=t)}get shaderF16Supported(){return this._context.shaderF16Supported}get floatFilteringSupported(){return this._context.floatFilteringSupported}get adapter(){return this._context.adapter}get device(){return this._context.device}get context(){return this._context.context}copyIncludesFrom(t){return t._shaderIncludes.forEach((t,e)=>this._shaderIncludes.set(e,t)),this}copyGlobalBindGroupsFrom(t){if(this._context!=t._context)throw new Error("Cannot copy global bind groups from another TinyHelix instance with a different WebGPU context.");if(this._globalBindGroups.length>0||this._globalBindGroupLayouts.length>0)throw new Error("Cannot copy global bind groups from another TinyHelix instance if this instance already has global bind groups set.");return this._globalBindGroupLayouts=[...t._globalBindGroupLayouts],this._globalBindGroups=[...t._globalBindGroups],this}async initialize(t={}){t.canvas=this._canvas,this._options=t,await this._context.initialize(t),this._createDepthStencil(),this._mipShader=this._context.device.createShaderModule({code:"@group(0) @binding(0) var source_tex: texture_2d<f32>;\n@group(0) @binding(1) var bilinear_sampler: sampler;\n\nstruct VertexOutput {\n @builtin(position) position: vec4f,\n @location(0) uv: vec2f,\n}\n\n@vertex\nfn vs_main(@builtin(vertex_index) i: u32) -> VertexOutput {\n var output: VertexOutput;\n\n // Full-screen triangle\n let x = f32((i << 1u) & 2u);\n let y = f32(i & 2u);\n\n output.position = vec4f(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);\n output.uv = vec2f(x, y);\n\n return output;\n}\n\n@fragment\nfn fs_main(input: VertexOutput) -> @location(0) vec4f {\n return textureSample(source_tex, bilinear_sampler, input.uv);\n}\n\n",label:"MipRenderer Shader Module"})}resize(t,e){this._canvas.width=t,this._canvas.height=e,this._createDepthStencil()}addShaderInclude(t,e){return this._shaderIncludes.set(t,e),this}get depthStencilFormat(){return this._options.depthStencilFormat}get backbuffer(){if(!this._backbuffer)throw new Error("Backbuffer not initialized. Did you forget to call startFrame()?");return this._backbuffer}get backbufferTarget(){if(!this._backbufferTarget)throw new Error("Backbuffer not initialized. Did you forget to call startFrame()?");return this._backbufferTarget}get colorSpace(){return this._context.colorSpace}get backbufferFormat(){return this._context.format}get backbufferWidth(){return this._canvas.width}get backbufferHeight(){return this._canvas.height}startFrame(){if(this._parent)return this._backbuffer=this._parent.backbuffer,void(this._backbufferTarget=this._parent.backbufferTarget);this._backbuffer=C.from_webgpu(this._context.getCurrentTexture(),this._context.format,this._context),this._backbufferTarget=this.createRenderTarget(this._backbuffer).withMipLevel(0).build()}createRenderTarget(t){return new D(t)}createShader(){let t=new $(this._context);return this._shaderIncludes.forEach((e,r)=>t=t.withInclude(r,e)),this._globalBindGroupLayouts.forEach((e,r)=>t.withBindGroup(r,e)),t}createBindGroupLayout(){return new H(this._context)}createBindGroup(t){return new W(this._context,t)}createMesh(){return new it(this._context)}createRenderPipeline(){return new Q(this._context,this._options.depthStencilFormat)}createComputePipeline(){return new ht(this._context)}createSampler(){return new at(this._context)}createTexture(){return new G(this._context,this._mipShader)}createCommandEncoder(t){return new v(this.backbufferTarget,this._globalBindGroups,this._context,this._depthStencilTarget,t)}createUniformBufferLayout(){return new I}createUniformBuffer(t){return new z(t,this._context)}createBuffer(){return new F(this._context)}setGlobalBindGroup(t,e,r){return this._globalBindGroupLayouts[t]=e,this._globalBindGroups[t]=r,this}destroy(){this._context.destroy()}depthStencilTexture(){return this._depthStencil}depthStencilTarget(){return this._depthStencilTarget}_createDepthStencil(){this._depthStencil=S(this._options.depthStencilFormat,t=>this.createTexture().withFormat(t).withSize(this._canvas.width,this._canvas.height).withUsage(GPUTextureUsage.RENDER_ATTACHMENT).build()),this._depthStencilTarget=S(this._depthStencil,t=>this.createRenderTarget(t).build())}}class ut{get srcFactor(){return this._inner.color.srcFactor}set srcFactor(t){this._inner.color.srcFactor=t}get dstFactor(){return this._inner.color.dstFactor}set dstFactor(t){this._inner.color.dstFactor=t}get alphaSrcFactor(){return this._inner.alpha.srcFactor}set alphaSrcFactor(t){this._inner.alpha.srcFactor=t}get alphaDstFactor(){return this._inner.alpha.dstFactor}set alphaDstFactor(t){this._inner.alpha.dstFactor=t}constructor(t,e,r,s){this._inner={color:{srcFactor:n.SrcAlpha,dstFactor:n.OneMinusSrcAlpha},alpha:{srcFactor:n.One,dstFactor:n.OneMinusSrcAlpha}},this.srcFactor=t,this.dstFactor=e,this.alphaSrcFactor=r,this.alphaDstFactor=s}}ut.ALPHA=new ut(n.SrcAlpha,n.OneMinusSrcAlpha,n.One,n.OneMinusSrcAlpha),ut.ADDITIVE=new ut(n.SrcAlpha,n.One,n.One,n.One),ut.MULTIPLY=new ut(n.DstColor,n.Zero,n.Zero,n.One),ut.SCREEN=new ut(n.One,n.OneMinusSrcColor,n.One,n.OneMinusSrcAlpha),ut.OVERLAY=new ut(n.SrcAlpha,n.OneMinusSrcAlpha,n.One,n.OneMinusSrcAlpha),ut.DARKEN=new ut(n.One,n.OneMinusSrcColor,n.One,n.OneMinusSrcAlpha),ut.LIGHTEN=new ut(n.OneMinusSrcColor,n.One,n.One,n.OneMinusSrcAlpha);class lt{constructor(t){this._offset=0,this._buffer=new ArrayBuffer(t),this._view=new DataView(this._buffer)}pushUint8(t){return this._view.setUint8(this._offset,t),this._offset+=1,this}pushUint8x2(t,e){return this.pushUint8(t),this.pushUint8(e),this}pushUint8x4(t,e,r,s){return this.pushUint8(t),this.pushUint8(e),this.pushUint8(r),this.pushUint8(s),this}pushSint8(t){return this._view.setInt8(this._offset,t),this._offset+=1,this}pushSint8x2(t,e){return this.pushSint8(t),this.pushSint8(e),this}pushSint8x4(t,e,r,s){return this.pushSint8(t),this.pushSint8(e),this.pushSint8(r),this.pushSint8(s),this}pushUint16(t){return this._view.setUint16(this._offset,t,!0),this._offset+=2,this}pushUint16x2(t,e){return this.pushUint16(t),this.pushUint16(e),this}pushUint16x4(t,e,r,s){return this.pushUint16(t),this.pushUint16(e),this.pushUint16(r),this.pushUint16(s),this}pushSint16(t){return this._view.setInt16(this._offset,t,!0),this._offset+=2,this}pushSint16x2(t,e){return this.pushSint16(t),this.pushSint16(e),this}pushSint16x4(t,e,r,s){return this.pushSint16(t),this.pushSint16(e),this.pushSint16(r),this.pushSint16(s),this}pushFloat16(t){const e=P(t);return this._view.setUint16(this._offset,e,!0),this._offset+=2,this}pushFloat16x2(t,e){return this.pushFloat16(t),this.pushFloat16(e),this}pushFloat16x4(t,e,r,s){return this.pushFloat16(t),this.pushFloat16(e),this.pushFloat16(r),this.pushFloat16(s),this}pushFloat32(t){return this._view.setFloat32(this._offset,t,!0),this._offset+=4,this}pushFloat32x2(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this}pushFloat32x3(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this.pushFloat32(t[2]),this}pushFloat32x4(t){return this.pushFloat32(t[0]),this.pushFloat32(t[1]),this.pushFloat32(t[2]),this.pushFloat32(t[3]),this}pushUint32(t){return this._view.setUint32(this._offset,t,!0),this._offset+=4,this}pushUint32x2(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this}pushUint32x3(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this.pushUint32(t[2]),this}pushUint32x4(t){return this.pushUint32(t[0]),this.pushUint32(t[1]),this.pushUint32(t[2]),this.pushUint32(t[3]),this}pushSint32(t){return this._view.setInt32(this._offset,t,!0),this._offset+=4,this}pushSint32x2(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this}pushSint32x3(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this.pushSint32(t[2]),this}pushSint32x4(t){return this.pushSint32(t[0]),this.pushSint32(t[1]),this.pushSint32(t[2]),this.pushSint32(t[3]),this}get arrayBuffer(){return this._buffer}}const _t=ct;export{r as AddressMode,b as BaseType,N as BindGroup,W as BindGroupBuilder,q as BindGroupLayout,n as BlendFactor,ut as BlendMode,A as Buffer,F as BufferBuilder,lt as BufferDataWriter,m as BufferUsage,u as ColorChannel,t as ColorSpace,v as CommandEncoder,a as CompareFunction,R as ComputePass,y as ComputePassBuilder,ot as ComputePipeline,ht as ComputePipelineBuilder,s as CullMode,e as FilterMode,j as FrontFace,Z as IndexFormat,et as Mesh,it as MeshBuilder,K as MeshTopology,U as RenderPass,w as RenderPassBuilder,X as RenderPipeline,Q as RenderPipelineBuilder,M as RenderTarget,D as RenderTargetBuilder,nt as Sampler,at as SamplerBuilder,c as SamplerType,Y as Shader,$ as ShaderBuilder,d as ShaderStage,o as StorageAccess,p as StoreOp,st as StreamBuilder,C as Texture,G as TextureBuilder,l as TextureDimension,i as TextureFormat,h as TextureSampleType,g as TextureUsage,T as TextureUtils,E as TextureView,L as TextureViewBuilder,_ as TextureViewDimension,ct as TinyHelix,z as UniformBuffer,k as UniformBufferLayout,I as UniformBufferLayoutBuilder,J as VertexFormat,x as WebGPUContext,_t as default};
2
2
  //# sourceMappingURL=tiny-helix.esm.js.map