From d3631cb1f7d5c1b27de76709fe4f5cd44402830a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 26 Dec 2024 22:36:26 -0800 Subject: [PATCH] Add blockhash to UBO. --- src/lib/workers/powgl.ts | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/lib/workers/powgl.ts b/src/lib/workers/powgl.ts index fbd70c4..dac9de1 100644 --- a/src/lib/workers/powgl.ts +++ b/src/lib/workers/powgl.ts @@ -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) => 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) -- 2.34.1