]> zoso.dev Git - nano-pow.git/commitdiff
Compile and cache shader prior to actual compute dispatch.
authorChris Duncan <chris@zoso.dev>
Sun, 20 Apr 2025 22:20:17 +0000 (15:20 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 20 Apr 2025 22:20:17 +0000 (15:20 -0700)
src/lib/gpu/index.ts

index 945b9bb26cd71067eea7b8325625964d381c6715..af6f54548e869a70312e89cef6304af55a281468 100644 (file)
@@ -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<void> {
                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))}`)
        }