]> zoso.dev Git - libnemo.git/commitdiff
Make pixel array static so it can be allocated once and reused.
authorChris Duncan <chris@zoso.dev>
Tue, 17 Dec 2024 14:00:18 +0000 (06:00 -0800)
committerChris Duncan <chris@zoso.dev>
Tue, 17 Dec 2024 14:00:18 +0000 (06:00 -0800)
src/lib/workers/pow.ts

index 4c4204fc1a145cedcf9bc7e24012971a5fb6912c..8f05539582ed819d8b491c287baccbbed3afb4f7 100644 (file)
@@ -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<string>) => 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)