]> zoso.dev Git - libnemo.git/commitdiff
Draw depends on a specific work value, so move them into the function call to avoid...
authorChris Duncan <chris@zoso.dev>
Sun, 15 Dec 2024 19:25:58 +0000 (11:25 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 15 Dec 2024 19:25:58 +0000 (11:25 -0800)
src/lib/workers/pow.ts

index 72fa88cafbb47e3e1d6981b742f534f9245d7d73..fc1080bd6241581c93f884736d8962cb1beecfef 100644 (file)
@@ -203,8 +203,6 @@ void main() {
 
        /** Used to set canvas size. Must be a multiple of 256. */
        static #WORKLOAD: number = 256 * Math.max(1, Math.floor(navigator.hardwareConcurrency / 2))
-       static #work0 = new Uint8Array(4)
-       static #work1 = new Uint8Array(4)
 
        static #hexify (arr: number[] | Uint8Array): string {
                let out = ''
@@ -307,6 +305,8 @@ void main() {
                        view.setUint8(i / 2, parseInt(byte, 16))
                }
                const hashBytes = new Uint32Array(view.buffer)
+               const work0 = new Uint8Array(4)
+               const work1 = new Uint8Array(4)
 
                // Draw output until success or progressCallback says to stop
                let n = 0
@@ -315,14 +315,14 @@ void main() {
                        n++
                        if (Pow.#gl == null) throw new Error('WebGL 2 is required')
                        performance.mark('start')
-                       crypto.getRandomValues(Pow.#work0)
-                       crypto.getRandomValues(Pow.#work1)
+                       crypto.getRandomValues(work0)
+                       crypto.getRandomValues(work1)
 
-                       Pow.#gl.uniform4uiv(Pow.#work0Location, this.#work0)
-                       Pow.#gl.uniform4uiv(Pow.#work1Location, this.#work1)
+                       Pow.#gl.uniform4uiv(Pow.#work0Location, work0)
+                       Pow.#gl.uniform4uiv(Pow.#work1Location, work1)
                        Pow.#gl.uniform1uiv(Pow.#blockHashLocation, hashBytes)
                        Pow.#gl.uniform1ui(Pow.#thresholdLocation, threshold)
-                       Pow.#gl.uniform1f(Pow.#workloadLocation, this.#WORKLOAD - 1)
+                       Pow.#gl.uniform1f(Pow.#workloadLocation, Pow.#WORKLOAD - 1)
 
                        Pow.#gl.clear(Pow.#gl.COLOR_BUFFER_BIT)
                        Pow.#gl.drawArrays(Pow.#gl.TRIANGLES, 0, 6)
@@ -336,11 +336,11 @@ void main() {
                                        performance.clearMarks()
                                        console.log(`average frame time: ${(frameTimes.reduce((a, b) => a + b)) / frameTimes.length} ms`)
                                        console.log(`frames calculated: ${n}`)
-                                       const hex = this.#hexify(this.#work1) + this.#hexify([
+                                       const hex = this.#hexify(work1) + this.#hexify([
                                                pixels[i + 2],
                                                pixels[i + 3],
-                                               this.#work0[2] ^ (pixels[i] - 1),
-                                               this.#work0[3] ^ (pixels[i + 1] - 1)
+                                               work0[2] ^ (pixels[i] - 1),
+                                               work0[3] ^ (pixels[i + 1] - 1)
                                        ])
                                        // Return the work value with the custom bits
                                        typeof callback === 'function' && callback(hex)