]> zoso.dev Git - libnemo.git/commitdiff
Add blockhash to UBO.
authorChris Duncan <chris@zoso.dev>
Fri, 27 Dec 2024 06:36:26 +0000 (22:36 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 27 Dec 2024 06:36:26 +0000 (22:36 -0800)
src/lib/workers/powgl.ts

index fbd70c45aa950b6dce8bbdb0f6aa22ea2d0a4804..dac9de197d7b4a5dfb7042aae6773fe893bded2c 100644 (file)
@@ -62,11 +62,11 @@ precision highp int;
 in vec2 uv_pos;
 out vec4 fragColor;
 
-// Precalculated block hash components
-uniform uint blockHash[8];
+// blockhash - array of precalculated block hash components
 // threshold - 0xfffffff8 for send/change blocks, 0xfffffe00 for all else
 // workload - Defines canvas size
 layout(std140) uniform UBO {
+       uint blockhash[8];
        uint threshold;
        float workload;
 };
@@ -195,7 +195,7 @@ void main() {
 
        // Block hash
        for (i=0;i<8;i++) {
-               m[i+2] = blockHash[i];
+               m[i+2] = blockhash[i];
        }
 
        // twelve rounds of mixing
@@ -238,7 +238,6 @@ void main() {
        static #gl: WebGL2RenderingContext | null
        static #program: WebGLProgram | null
        static #workLocation: WebGLUniformLocation | null
-       static #blockHashLocation: WebGLUniformLocation | null
        static #vertexShader: WebGLShader | null
        static #fragmentShader: WebGLShader | null
        static #positionBuffer: WebGLBuffer | null
@@ -304,16 +303,14 @@ void main() {
 
                this.#uboBuffer = this.#gl.createBuffer()
                this.#gl.bindBuffer(this.#gl.UNIFORM_BUFFER, this.#uboBuffer)
-               this.#gl.bufferData(this.#gl.UNIFORM_BUFFER, 16, this.#gl.DYNAMIC_DRAW)
+               this.#gl.bufferData(this.#gl.UNIFORM_BUFFER, 144, this.#gl.DYNAMIC_DRAW)
                this.#gl.bindBuffer(this.#gl.UNIFORM_BUFFER, null)
                this.#gl.bindBufferBase(this.#gl.UNIFORM_BUFFER, 0, this.#uboBuffer)
                this.#gl.uniformBlockBinding(this.#program, this.#gl.getUniformBlockIndex(this.#program, 'UBO'), 0)
 
                this.#pixels = new Uint8Array(this.#gl.drawingBufferWidth * this.#gl.drawingBufferHeight * 4)
                this.#query = this.#gl.createQuery()
-
                this.#workLocation = this.#gl.getUniformLocation(this.#program, 'work')
-               this.#blockHashLocation = this.#gl.getUniformLocation(this.#program, "blockHash")
        }
 
        static #calculate (hashHex: string, callback: (nonce: string | PromiseLike<string>) => any, threshold: number): void {
@@ -322,19 +319,14 @@ void main() {
                if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`)
                if (this.#gl == null) throw new Error('WebGL 2 is required')
 
-               // Convert big-endian hash hex to buffer for use by shader
-               const view = new DataView(new ArrayBuffer(32))
-               for (let i = 0; i < 64; i += 2) {
-                       const byte = hashHex.slice(i, i + 2)
-                       view.setUint8(i / 2, parseInt(byte, 16))
-               }
-               const hashBytes = new Uint32Array(view.buffer)
-               Pow.#gl.uniform1uiv(Pow.#blockHashLocation, hashBytes)
-
                // Set up uniform buffer object
-               const uboView = new DataView(new ArrayBuffer(16))
-               uboView.setUint32(0, threshold, true)
-               uboView.setFloat32(4, Pow.#WORKLOAD - 1, true)
+               const uboView = new DataView(new ArrayBuffer(144))
+               for (let i = 0; i < 64; i += 8) {
+                       const uint32 = hashHex.slice(i, i + 8)
+                       uboView.setUint32(i * 2, parseInt(uint32, 16))
+               }
+               uboView.setUint32(128, threshold, true)
+               uboView.setFloat32(132, Pow.#WORKLOAD - 1, true)
                Pow.#gl.bindBuffer(Pow.#gl.UNIFORM_BUFFER, Pow.#uboBuffer)
                Pow.#gl.bufferSubData(Pow.#gl.UNIFORM_BUFFER, 0, uboView)
                Pow.#gl.bindBuffer(Pow.#gl.UNIFORM_BUFFER, null)