@gasm-compiler/core 0.2.1 → 0.3.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 +52 -0
- package/dist/browser-executor.js +1 -1
- package/dist/browser.d.ts +84 -3
- package/dist/browser.js +10 -1
- package/dist/{chunk-G6YRZSKB.js → chunk-IZGS3OS2.js} +1 -1
- package/dist/chunk-PS4NV6ZB.js +5 -0
- package/dist/chunk-VSCQDCQR.js +9 -0
- package/dist/compiler.js +1 -1
- package/dist/{parser-C7xAx4nx.d.ts → error_codes-D6RsiZ33.d.ts} +78 -1
- package/dist/gasm_core_rs.wasm +0 -0
- package/dist/mod.d.ts +6 -3
- package/dist/mod.js +1 -1
- package/package.json +10 -5
- package/dist/chunk-3GAG34KX.js +0 -5
- package/dist/chunk-OJJM3SJ5.js +0 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as l}from"./chunk-STDXBN5E.js";var U=globalThis.navigator?.gpu,P=class{constructor(e={}){this.config=e}device=null;queue=null;initialized=!1;pipelineCache=new Map;gpuBufferCache=new Map;stagingBuffer=null;stagingBufferSize=0;_animBindGroup=null;_animInputBuffer=null;_animInputSize=0;_animPipeline=null;_animStagingSize=0;_animStagingBuffer=null;_animResultBuffer=null;_animResultType="i32";_animCopyU8Src=null;_animCopyU8Dst=null;async prepareAnimation(e,i,s,c,a,d,r="main"){await this.ensureInitialized();let p=this.pipelineCache.get(e);if(!p){let v=this.device.createShaderModule({code:e,label:"anim"}),w=await v.getCompilationInfo?.();if(w){let _=w.messages.filter(A=>A.type==="error");if(_.length>0){let A=_.map(t=>`${t.message} at line ${t.lineNum}:${t.linePos}`).join("; ");throw new l(`WGSL compilation failed: ${A}`,"compilation")}}let x=this.device.createBindGroupLayout({entries:[{binding:0,visibility:4,buffer:{type:"storage"}}]}),C=this.device.createComputePipeline({layout:this.device.createPipelineLayout({bindGroupLayouts:[x]}),compute:{module:v,entryPoint:r}});p={shaderModule:v,pipeline:C,bindGroupLayout:x},this.pipelineCache.set(e,p)}this._animPipeline=p.pipeline;let m=i.byteLength;this._animInputBuffer=this.device.createBuffer({label:"anim_memory",size:m,usage:140}),this._animInputSize=m,this.queue.writeBuffer(this._animInputBuffer,0,i.buffer,i.byteOffset,m),this._animBindGroup=this.device.createBindGroup({layout:p.bindGroupLayout,entries:[{binding:0,resource:{buffer:this._animInputBuffer}}]});let n=Math.max(4,Math.ceil(c/4)*4);this._animStagingBuffer=this.device.createBuffer({label:"anim_staging",size:n,usage:9}),this._animStagingSize=n;let h=n/4;a==="f32"?this._animResultBuffer=new Float32Array(h):a==="i32"?this._animResultBuffer=new Int32Array(h):this._animResultBuffer=new Uint32Array(h),this._animResultType=a,this._animCopyU8Dst=new Uint8Array(this._animResultBuffer.buffer,0,n),this._animWorkgroupCount=d}_animWorkgroupCount=[1,1,1];async executeFrame(e){this.queue.writeBuffer(this._animInputBuffer,0,e.buffer,e.byteOffset,this._animInputSize);let i=this.device.createCommandEncoder(),s=i.beginComputePass();s.setPipeline(this._animPipeline),s.setBindGroup(0,this._animBindGroup),s.dispatchWorkgroups(this._animWorkgroupCount[0],this._animWorkgroupCount[1],this._animWorkgroupCount[2]),s.end(),i.copyBufferToBuffer(this._animInputBuffer,0,this._animStagingBuffer,0,this._animStagingSize),this.queue.submit([i.finish()]),await this._animStagingBuffer.mapAsync(1);let c=this._animStagingBuffer.getMappedRange();return this._animCopyU8Dst.set(new Uint8Array(c,0,this._animStagingSize)),this._animStagingBuffer.unmap(),this._animResultBuffer}async ensureInitialized(){if(!this.initialized)try{let e=await U?.requestAdapter?.();if(!e)throw new l("No GPU adapter available. WebGPU may not be supported in this browser.","compilation","navigator.gpu.requestAdapter() returned null");if(this.device=await e.requestDevice?.(),!this.device)throw new l("Failed to request GPU device","compilation","adapter.requestDevice() returned null");this.queue=this.device.queue,this.initialized=!0}catch(e){throw e instanceof l?e:new l(`GPU initialization failed: ${e instanceof Error?e.message:String(e)}`,"compilation",String(e))}}resultBufferCache=new Map;getOrCreateBuffer(e,i,s){let c=`${e}:${i.byteLength}:${s}`,a=this.gpuBufferCache.get(c);return a||(a=this.device.createBuffer({label:e,size:i.byteLength,usage:s}),this.gpuBufferCache.set(c,a)),this.queue.writeBuffer(a,0,i.buffer,i.byteOffset,i.byteLength),a}async readBuffer(e,i,s,c){let a=Math.max(4,Math.ceil(i/4)*4);(!this.stagingBuffer||this.stagingBufferSize<a)&&(this.stagingBuffer?.destroy(),this.stagingBuffer=this.device.createBuffer({label:"staging",size:a,usage:9}),this.stagingBufferSize=a);let d=this.stagingBuffer,r=this.device.createCommandEncoder();r.copyBufferToBuffer(e,0,d,0,Math.min(i,a)),this.queue.submit([r.finish()]);try{await d.mapAsync(1)}catch(h){throw new l(`Failed to map staging buffer: ${h instanceof Error?h.message:String(h)}`,"dispatch",String(h))}let p=d.getMappedRange(),m=a/4,n=this.resultBufferCache.get(c);return(!n||n.length<m)&&(s==="f32"?n=new Float32Array(m):s==="i32"?n=new Int32Array(m):n=new Uint32Array(m),this.resultBufferCache.set(c,n)),s==="f32"?n.set(new Float32Array(p)):s==="i32"?n.set(new Int32Array(p)):n.set(new Uint32Array(p)),d.unmap(),{data:n,type:s}}async execute(e,i,s,c={}){let a=c.workgroupCount??[1,1,1],d=c.outputBuffers??Object.fromEntries(s.map(r=>[r,{size:4,type:"f32"}]));if(await this.ensureInitialized(),!this.device||!this.queue)throw new l("Device initialization failed","compilation");try{let r=this.pipelineCache.get(e);if(!r){let t;try{let o=c.shaderLabel||"Gasm Compiler Compute Shader";t=this.device.createShaderModule({code:e,label:o});let f=await t.getCompilationInfo?.();if(f&&f.messages.length>0){let B=f.messages.filter(z=>z.type==="error");if(B.length>0){let z=B.map(M=>`${M.message} at line ${M.lineNum}:${M.linePos}`).join("; ");throw new l(`WGSL compilation failed: ${z}`,"compilation")}}}catch(o){throw o instanceof l?o:new l(`Failed to create shader module: ${o instanceof Error?o.message:String(o)}`,"compilation",String(o))}let y=Object.keys(i).length,u=Object.keys(d).filter(o=>!i[o]).length,g=y+u,b=Array.from({length:g},(o,f)=>{let B=f===0&&i.memory!==void 0,z=f>=y;return{binding:f,visibility:4,buffer:{type:!B&&!z?"read-only-storage":"storage"}}}),S=this.device.createBindGroupLayout({entries:b}),I=this.device.createComputePipeline({layout:this.device.createPipelineLayout({bindGroupLayouts:[S]}),compute:{module:t,entryPoint:c.entryPoint??"main"}});r={shaderModule:t,pipeline:I,bindGroupLayout:S},this.pipelineCache.set(e,r)}let{pipeline:p,bindGroupLayout:m}=r,n=[],h=0,v=new Map;for(let[t,{data:y}]of Object.entries(i)){let u=this.getOrCreateBuffer(`input_${t}`,y,140);v.set(t,u),n.push({binding:h++,resource:{buffer:u}})}let w=new Map;for(let[t,y]of Object.entries(d)){if(v.has(t)){let g=v.get(t),b=i[t];w.set(t,{buffer:g,spec:{type:b.type,size:b.data.byteLength}});continue}let u=this.getOrCreateBuffer(`output_${t}`,new Uint32Array(Math.ceil((y.size??4)/4)),
|
|
1
|
+
import{a as l}from"./chunk-STDXBN5E.js";var U=globalThis.navigator?.gpu,P=class{constructor(e={}){this.config=e}device=null;queue=null;initialized=!1;pipelineCache=new Map;gpuBufferCache=new Map;stagingBuffer=null;stagingBufferSize=0;_animBindGroup=null;_animInputBuffer=null;_animInputSize=0;_animPipeline=null;_animStagingSize=0;_animStagingBuffer=null;_animResultBuffer=null;_animResultType="i32";_animCopyU8Src=null;_animCopyU8Dst=null;async prepareAnimation(e,i,s,c,a,d,r="main"){await this.ensureInitialized();let p=this.pipelineCache.get(e);if(!p){let v=this.device.createShaderModule({code:e,label:"anim"}),w=await v.getCompilationInfo?.();if(w){let _=w.messages.filter(A=>A.type==="error");if(_.length>0){let A=_.map(t=>`${t.message} at line ${t.lineNum}:${t.linePos}`).join("; ");throw new l(`WGSL compilation failed: ${A}`,"compilation")}}let x=this.device.createBindGroupLayout({entries:[{binding:0,visibility:4,buffer:{type:"storage"}}]}),C=this.device.createComputePipeline({layout:this.device.createPipelineLayout({bindGroupLayouts:[x]}),compute:{module:v,entryPoint:r}});p={shaderModule:v,pipeline:C,bindGroupLayout:x},this.pipelineCache.set(e,p)}this._animPipeline=p.pipeline;let m=i.byteLength;this._animInputBuffer=this.device.createBuffer({label:"anim_memory",size:m,usage:140}),this._animInputSize=m,this.queue.writeBuffer(this._animInputBuffer,0,i.buffer,i.byteOffset,m),this._animBindGroup=this.device.createBindGroup({layout:p.bindGroupLayout,entries:[{binding:0,resource:{buffer:this._animInputBuffer}}]});let n=Math.max(4,Math.ceil(c/4)*4);this._animStagingBuffer=this.device.createBuffer({label:"anim_staging",size:n,usage:9}),this._animStagingSize=n;let h=n/4;a==="f32"?this._animResultBuffer=new Float32Array(h):a==="i32"?this._animResultBuffer=new Int32Array(h):this._animResultBuffer=new Uint32Array(h),this._animResultType=a,this._animCopyU8Dst=new Uint8Array(this._animResultBuffer.buffer,0,n),this._animWorkgroupCount=d}_animWorkgroupCount=[1,1,1];async executeFrame(e){this.queue.writeBuffer(this._animInputBuffer,0,e.buffer,e.byteOffset,this._animInputSize);let i=this.device.createCommandEncoder(),s=i.beginComputePass();s.setPipeline(this._animPipeline),s.setBindGroup(0,this._animBindGroup),s.dispatchWorkgroups(this._animWorkgroupCount[0],this._animWorkgroupCount[1],this._animWorkgroupCount[2]),s.end(),i.copyBufferToBuffer(this._animInputBuffer,0,this._animStagingBuffer,0,this._animStagingSize),this.queue.submit([i.finish()]),await this._animStagingBuffer.mapAsync(1);let c=this._animStagingBuffer.getMappedRange();return this._animCopyU8Dst.set(new Uint8Array(c,0,this._animStagingSize)),this._animStagingBuffer.unmap(),this._animResultBuffer}async ensureInitialized(){if(!this.initialized)try{let e=await U?.requestAdapter?.();if(!e)throw new l("No GPU adapter available. WebGPU may not be supported in this browser.","compilation","navigator.gpu.requestAdapter() returned null");if(this.device=await e.requestDevice?.(),!this.device)throw new l("Failed to request GPU device","compilation","adapter.requestDevice() returned null");this.queue=this.device.queue,this.initialized=!0}catch(e){throw e instanceof l?e:new l(`GPU initialization failed: ${e instanceof Error?e.message:String(e)}`,"compilation",String(e))}}resultBufferCache=new Map;getOrCreateBuffer(e,i,s){let c=`${e}:${i.byteLength}:${s}`,a=this.gpuBufferCache.get(c);return a||(a=this.device.createBuffer({label:e,size:i.byteLength,usage:s}),this.gpuBufferCache.set(c,a)),this.queue.writeBuffer(a,0,i.buffer,i.byteOffset,i.byteLength),a}async readBuffer(e,i,s,c){let a=Math.max(4,Math.ceil(i/4)*4);(!this.stagingBuffer||this.stagingBufferSize<a)&&(this.stagingBuffer?.destroy(),this.stagingBuffer=this.device.createBuffer({label:"staging",size:a,usage:9}),this.stagingBufferSize=a);let d=this.stagingBuffer,r=this.device.createCommandEncoder();r.copyBufferToBuffer(e,0,d,0,Math.min(i,a)),this.queue.submit([r.finish()]);try{await d.mapAsync(1)}catch(h){throw new l(`Failed to map staging buffer: ${h instanceof Error?h.message:String(h)}`,"dispatch",String(h))}let p=d.getMappedRange(),m=a/4,n=this.resultBufferCache.get(c);return(!n||n.length<m)&&(s==="f32"?n=new Float32Array(m):s==="i32"?n=new Int32Array(m):n=new Uint32Array(m),this.resultBufferCache.set(c,n)),s==="f32"?n.set(new Float32Array(p)):s==="i32"?n.set(new Int32Array(p)):n.set(new Uint32Array(p)),d.unmap(),{data:n,type:s}}async execute(e,i,s,c={}){let a=c.workgroupCount??[1,1,1],d=c.outputBuffers??Object.fromEntries(s.map(r=>[r,{size:4,type:"f32"}]));if(await this.ensureInitialized(),!this.device||!this.queue)throw new l("Device initialization failed","compilation");try{let r=this.pipelineCache.get(e);if(!r){let t;try{let o=c.shaderLabel||"Gasm Compiler Compute Shader";t=this.device.createShaderModule({code:e,label:o});let f=await t.getCompilationInfo?.();if(f&&f.messages.length>0){let B=f.messages.filter(z=>z.type==="error");if(B.length>0){let z=B.map(M=>`${M.message} at line ${M.lineNum}:${M.linePos}`).join("; ");throw new l(`WGSL compilation failed: ${z}`,"compilation")}}}catch(o){throw o instanceof l?o:new l(`Failed to create shader module: ${o instanceof Error?o.message:String(o)}`,"compilation",String(o))}let y=Object.keys(i).length,u=Object.keys(d).filter(o=>!i[o]).length,g=y+u,b=Array.from({length:g},(o,f)=>{let B=f===0&&i.memory!==void 0,z=f>=y;return{binding:f,visibility:4,buffer:{type:!B&&!z?"read-only-storage":"storage"}}}),S=this.device.createBindGroupLayout({entries:b}),I=this.device.createComputePipeline({layout:this.device.createPipelineLayout({bindGroupLayouts:[S]}),compute:{module:t,entryPoint:c.entryPoint??"main"}});r={shaderModule:t,pipeline:I,bindGroupLayout:S},this.pipelineCache.set(e,r)}let{pipeline:p,bindGroupLayout:m}=r,n=[],h=0,v=new Map;for(let[t,{data:y}]of Object.entries(i)){let u=this.getOrCreateBuffer(`input_${t}`,y,140);v.set(t,u),n.push({binding:h++,resource:{buffer:u}})}let w=new Map;for(let[t,y]of Object.entries(d)){if(v.has(t)){let g=v.get(t),b=i[t];w.set(t,{buffer:g,spec:{type:b.type,size:b.data.byteLength}});continue}let u=this.getOrCreateBuffer(`output_${t}`,new Uint32Array(Math.ceil((y.size??4)/4)),140);w.set(t,{buffer:u,spec:y}),n.push({binding:h++,resource:{buffer:u}})}let x=this.device.createBindGroup({layout:m,entries:n}),C=this.device.createCommandEncoder(),_=C.beginComputePass();_.setPipeline(p),_.setBindGroup(0,x),_.dispatchWorkgroups(...a),_.end();for(let[,{buffer:t,spec:y}]of w.entries()){let u=y.size??4,g=Math.max(4,Math.ceil(u/4)*4);(!this.stagingBuffer||this.stagingBufferSize<g)&&(this.stagingBuffer?.destroy(),this.stagingBuffer=this.device.createBuffer({label:"staging",size:g,usage:9}),this.stagingBufferSize=g),C.copyBufferToBuffer(t,0,this.stagingBuffer,0,Math.min(u,g))}this.queue.submit([C.finish()]);let A={};for(let[t,{buffer:y,spec:u}]of w.entries()){let g=u.size??4,b=Math.max(4,Math.ceil(g/4)*4),S=`${t}:${g}:${u.type}`;try{await this.stagingBuffer.mapAsync(1)}catch(B){throw new l(`Failed to map staging buffer: ${B instanceof Error?B.message:String(B)}`,"dispatch",String(B))}let I=this.stagingBuffer.getMappedRange(),o=b/4,f=this.resultBufferCache.get(S);(!f||f.length<o)&&(u.type==="f32"?f=new Float32Array(o):u.type==="i32"?f=new Int32Array(o):f=new Uint32Array(o),this.resultBufferCache.set(S,f)),new Uint8Array(f.buffer,0,b).set(new Uint8Array(I,0,b)),this.stagingBuffer.unmap(),A[t]={data:f,type:u.type}}return A}catch(r){throw r instanceof l?r:new l(`Execution failed: ${r instanceof Error?r.message:String(r)}`,"dispatch",String(r))}}async destroy(){for(let e of this.gpuBufferCache.values())e.destroy();this.resultBufferCache.clear(),this.gpuBufferCache.clear(),this.pipelineCache.clear(),this.stagingBuffer?.destroy(),this.stagingBuffer=null,this.stagingBufferSize=0,this.device?.destroy?.(),this.device=null,this.queue=null,this.initialized=!1}};export{P as a};
|