From 34d109602e57899ea998264b48578974c85ab4bc Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 17 Dec 2024 06:00:18 -0800 Subject: [PATCH] Make pixel array static so it can be allocated once and reused. --- src/lib/workers/pow.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 4c4204f..8f05539 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -222,6 +222,7 @@ void main() { static #fragmentShader: WebGLShader | null static #positionBuffer: WebGLBuffer | null static #uvBuffer: WebGLBuffer | null + static #pixels: Uint8Array // Vertex Positions, 2 triangles static #positions = new Float32Array([ -1, -1, 0, -1, 1, 0, 1, 1, 0, @@ -282,6 +283,7 @@ void main() { this.#blockHashLocation = this.#gl.getUniformLocation(this.#program, "blockHash") this.#thresholdLocation = this.#gl.getUniformLocation(this.#program, "threshold") this.#workloadLocation = this.#gl.getUniformLocation(this.#program, "workload") + this.#pixels = new Uint8Array(this.#gl.drawingBufferWidth * this.#gl.drawingBufferHeight * 4) } static #calculate (hashHex: string, callback: (nonce: string | PromiseLike) => any, threshold: number): void { @@ -318,21 +320,20 @@ void main() { Pow.#gl.clear(Pow.#gl.COLOR_BUFFER_BIT) Pow.#gl.drawArrays(Pow.#gl.TRIANGLES, 0, 6) - const pixels = new Uint8Array(Pow.#gl.drawingBufferWidth * Pow.#gl.drawingBufferHeight * 4) - Pow.#gl.readPixels(0, 0, Pow.#gl.drawingBufferWidth, Pow.#gl.drawingBufferHeight, Pow.#gl.RGBA, Pow.#gl.UNSIGNED_BYTE, pixels) + Pow.#gl.readPixels(0, 0, Pow.#gl.drawingBufferWidth, Pow.#gl.drawingBufferHeight, Pow.#gl.RGBA, Pow.#gl.UNSIGNED_BYTE, Pow.#pixels) // Check the pixels for any success - for (let i = 0; i < pixels.length; i += 4) { - if (pixels[i] !== 0) { + for (let i = 0; i < Pow.#pixels.length; i += 4) { + if (Pow.#pixels[i] !== 0) { performance.mark('end') frameTimes.push(performance.measure('draw', 'start', 'end').duration) 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(work1) + this.#hexify([ - pixels[i + 2], - pixels[i + 3], - work0[2] ^ (pixels[i] - 1), - work0[3] ^ (pixels[i + 1] - 1) + Pow.#pixels[i + 2], + Pow.#pixels[i + 3], + work0[2] ^ (Pow.#pixels[i] - 1), + work0[3] ^ (Pow.#pixels[i + 1] - 1) ]) // Return the work value with the custom bits typeof callback === 'function' && callback(hex) -- 2.34.1