// If-ElseIf(condition,()=>{// true}).ElseIf(other,()=>{// other true}).Else(()=>{// false});// Select (ternary)constresult=select(condition,trueVal,falseVal);// LoopLoop(10,({i})=>{// i = 0 to 9});// Loop controlBreak();Continue();Discard();// Fragment only
Custom Functions
// Basic functionconstmyFn=Fn(([a,b])=>{returna.add(b);});// With defaultsconstmyFn=Fn(([a=1.0,b=2.0])=>{returna.add(b);});// UsagemyFn(x,y);myFn();// uses defaults
// Listen for device lossrenderer.backend.device.lost.then((info)=>{if(info.reason==='unknown'){// Unexpected loss - recoverrenderer.dispose();initWebGPU();// Reinitialize}});// Simulate loss for testingrenderer.backend.device.destroy();
Loss Reason
Meaning
'destroyed'
Intentional via destroy()
'unknown'
Unexpected (driver crash, timeout, etc.)
Recovery tips:
Always get fresh adapter before new device
Save/restore application state (not transient data)
Use Chrome about:gpucrash to test real GPU crashes
Compute Shader Built-ins
Node
Description
instanceIndex
Current instance/invocation index
vertexIndex
Current vertex index
drawIndex
Current draw call index
globalId
Global invocation position (uvec3)
localId
Local workgroup position (uvec3)
workgroupId
Workgroup index (uvec3)
numWorkgroups
Number of workgroups dispatched (uvec3)
subgroupSize
Size of the subgroup
Device Limits
WebGPU devices use default minimums unless you request higher limits. This is critical for large buffers.
// Three.js: pass requiredLimits to the rendererconstrenderer=newTHREE.WebGPURenderer({requiredLimits:{maxBufferSize:1024*1024*1024,// 1 GiBmaxStorageBufferBindingSize:1024*1024*512,// 512 MiB},});awaitrenderer.init();
Limit
Default
Common Need
maxBufferSize
256 MiB
Large vertex/storage buffers
maxStorageBufferBindingSize
128 MiB
Large compute buffers
maxStorageBuffersPerShaderStage
8
Many storage buffers
Check before requesting:
constadapter=awaitnavigator.gpu?.requestAdapter();if(adapter.limits.maxBufferSize>=desiredSize){// Safe to request}