]> zoso.dev Git - nano-pow.git/commitdiff
Declare bind group once and reuse.
authorChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 03:20:30 +0000 (20:20 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 03:20:30 +0000 (20:20 -0700)
src/lib/gpu/index.ts

index d4c1ba90201b927d466f22108047c7de8f16bec8..4cd24fd31338dca591f468cf53a1a29180de8756 100644 (file)
@@ -23,6 +23,7 @@ export class NanoPowGpu {
        static #uboView: DataView
        static #resultView: DataView
        static #bindGroupLayout: GPUBindGroupLayout
+       static #bindGroup: GPUBindGroup
        static #searchPipeline: GPUComputePipeline
        static #validatePipeline: GPUComputePipeline
 
@@ -92,6 +93,24 @@ export class NanoPowGpu {
                                module: shaderModule
                        }
                })
+               // Bind UBO read and GPU write buffers
+               this.#bindGroup = this.#device.createBindGroup({
+                       layout: this.#bindGroupLayout,
+                       entries: [
+                               {
+                                       binding: 0,
+                                       resource: {
+                                               buffer: this.#uboBuffer
+                                       },
+                               },
+                               {
+                                       binding: 1,
+                                       resource: {
+                                               buffer: this.#gpuBuffer
+                                       },
+                               },
+                       ],
+               })
                // Create pipeline to connect compute shader to binding layout
                this.#validatePipeline = this.#device.createComputePipeline({
                        layout: this.#device.createPipelineLayout({
@@ -170,32 +189,13 @@ export class NanoPowGpu {
                this.#device.queue.writeBuffer(this.#gpuBuffer, 0, this.#bufferReset)
                this.#device.queue.writeBuffer(this.#cpuBuffer, 0, this.#bufferReset)
 
-               // Bind UBO read and GPU write buffers
-               const bindGroup = this.#device.createBindGroup({
-                       layout: this.#bindGroupLayout,
-                       entries: [
-                               {
-                                       binding: 0,
-                                       resource: {
-                                               buffer: this.#uboBuffer
-                                       },
-                               },
-                               {
-                                       binding: 1,
-                                       resource: {
-                                               buffer: this.#gpuBuffer
-                                       },
-                               },
-                       ],
-               })
-
                // Create command encoder to issue commands to GPU and initiate computation
                const commandEncoder = this.#device.createCommandEncoder()
                const passEncoder = commandEncoder.beginComputePass()
 
                // Issue commands and end compute pass structure
                passEncoder.setPipeline(pipeline)
-               passEncoder.setBindGroup(0, bindGroup)
+               passEncoder.setBindGroup(0, this.#bindGroup)
                passEncoder.dispatchWorkgroups(passes, passes)
                passEncoder.end()