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