/** * The GPU device, requested the same way in the page and the CLI. * * The precompute holds its whole kernel bank in one storage buffer, which * at fine grids outgrows WebGPU's 128 MiB default binding size, so the * request asks for the adapter's actual maximum buffer and binding sizes. * That is a capacity request: if the adapter cannot give more, the request * still succeeds and a too-large bake fails with a clear buffer error. */ let heldAdapter: GPUAdapter | null = null; export async function requestDevice(): Promise { if (!navigator.gpu) { throw new Error( 'this environment has no WebGPU. In the browser: Chrome and Edge 113+, ' + 'Safari 26+; on Linux it may need enabling explicitly. In node: the ' + 'optional `webgpu` package (Google Dawn).', ); } const adapter = await navigator.gpu.requestAdapter({ powerPreference: 'high-performance' }); if (!adapter) throw new Error('WebGPU found no adapter on this machine.'); heldAdapter = adapter; return adapter.requestDevice({ requiredLimits: { maxBufferSize: adapter.limits.maxBufferSize, maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize, }, }); } export const currentAdapter = (): GPUAdapter | null => heldAdapter;