From: Chris Duncan Date: Sun, 20 Apr 2025 22:20:17 +0000 (-0700) Subject: Compile and cache shader prior to actual compute dispatch. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=60f626df7bd24a8dcb82fd2b7aea3f27cfdb6e81;p=nano-pow.git Compile and cache shader prior to actual compute dispatch. --- diff --git a/src/lib/gpu/index.ts b/src/lib/gpu/index.ts index 945b9bb..af6f545 100644 --- a/src/lib/gpu/index.ts +++ b/src/lib/gpu/index.ts @@ -38,7 +38,7 @@ export class NanoPowGpu { if (!(device instanceof GPUDevice)) throw new Error('WebGPU device failed to load.') device.lost?.then(this.reset) this.#device = device - this.setup() + await this.setup() } catch (err) { throw new Error('WebGPU initialization failed.', { cause: err }) } finally { @@ -47,7 +47,7 @@ export class NanoPowGpu { this.#isInitialized = true } - static setup (): void { + static async setup (): Promise { if (this.#device == null) throw new Error(`WebGPU device failed to load.`) // Create buffers for writing GPU calculations and reading from Javascript this.#gpuBuffer = this.#device.createBuffer({ @@ -101,6 +101,11 @@ export class NanoPowGpu { module: shaderModule } }) + // Compile and cache shader prior to actual dispatch + const cmd = this.#device.createCommandEncoder() + cmd.beginComputePass().end() + this.#device.queue.submit([cmd.finish()]) + await this.#device.queue.onSubmittedWorkDone() console.log(`NanoPow WebGPU initialized. Recommended effort: ${Math.max(1, Math.floor(navigator.hardwareConcurrency / 2))}`) }