]> zoso.dev Git - libnemo.git/commitdiff
Move uniform transfers outside of draw loop where possible.
authorChris Duncan <chris@zoso.dev>
Wed, 18 Dec 2024 23:50:30 +0000 (15:50 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 18 Dec 2024 23:50:30 +0000 (15:50 -0800)
src/lib/workers/pow.ts

index fee4e81d5b0a0d01d6fc0f72b41c7f2c95bda03a..c9e3bab9e539c27e28b5d04aa525d7625c651aca 100644 (file)
@@ -16,12 +16,6 @@ export class Pow {
                })
        }
 
-       /**
-       * nano-webgl-pow
-       * Nano Currency Proof of Work Value generation using WebGL2
-       * Author:  numtel <ben@latenightsketches.com>
-       * License: MIT
-       */
        // Vertex Shader
        static #vsSource = `#version 300 es
 #pragma vscode_glsllint_stage: vert
@@ -294,6 +288,7 @@ void main() {
        }
 
        static #calculate (hashHex: string, callback: (nonce: string | PromiseLike<string>) => any, threshold: number): void {
+               if (Pow.#gl == null) throw new Error('WebGL 2 is required')
                if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`)
                if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`)
                if (this.#gl == null) throw new Error('WebGL 2 is required')
@@ -305,6 +300,9 @@ void main() {
                        view.setUint8(i / 2, parseInt(byte, 16))
                }
                const hashBytes = new Uint32Array(view.buffer)
+               Pow.#gl.uniform1uiv(Pow.#blockHashLocation, hashBytes)
+               Pow.#gl.uniform1ui(Pow.#thresholdLocation, threshold)
+               Pow.#gl.uniform1f(Pow.#workloadLocation, Pow.#WORKLOAD - 1)
                const work0 = new Uint8Array(4)
                const work1 = new Uint8Array(4)
 
@@ -322,9 +320,6 @@ void main() {
 
                        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, Pow.#WORKLOAD - 1)
 
                        Pow.#gl.beginQuery(Pow.#gl.ANY_SAMPLES_PASSED_CONSERVATIVE, Pow.#query)
                        Pow.#gl.drawArrays(Pow.#gl.TRIANGLES, 0, 6)