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 {
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({
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))}`)
}