wesl-debug
Version:
Utilities for testing WESL/WGSL shaders in Node.js environments.
30 lines (26 loc) • 840 B
text/typescript
let sharedGpu: GPU | undefined;
let sharedDevice: GPUDevice | undefined;
/** get or create shared GPU device for testing */
export async function getGPUDevice(): Promise<GPUDevice> {
if (!sharedDevice) {
const gpu = await setupWebGPU();
const adapter = await gpu.requestAdapter();
if (!adapter) throw new Error("Failed to get GPU adapter");
sharedDevice = await adapter.requestDevice();
}
return sharedDevice;
}
/** destroy globally shared GPU test device */
export function destroySharedDevice(): void {
sharedDevice?.destroy();
sharedDevice = undefined;
}
/** initialize WebGPU for testing */
async function setupWebGPU(): Promise<GPU> {
if (!sharedGpu) {
const webgpu = await import("webgpu");
Object.assign(globalThis, webgpu.globals);
sharedGpu = webgpu.create([]);
}
return sharedGpu;
}