From: Chris Duncan Date: Thu, 12 Dec 2024 21:25:18 +0000 (-0800) Subject: Condense introductory code of calculate function. Remove logging of readPixels since... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=577cec0ed5a82ce8a87c6eafd913d67f932e0579;p=libnemo.git Condense introductory code of calculate function. Remove logging of readPixels since we know it is the longest operation. Clarify callback execution by separating its argument. --- diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index ff48dd9..b71370e 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -48,13 +48,8 @@ export class Pow { if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16) if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`) let reverseHex = '' - for (let i = hashHex.length; i > 0; i -= 2) { - reverseHex += hashHex.slice(i - 2, i) - } - - if (this.gl == null) - throw new Error('webgl2_required') - + for (let i = hashHex.length; i > 0; i -= 2) reverseHex += hashHex.slice(i - 2, i) + if (this.gl == null) throw new Error('webgl2_required') this.gl.clearColor(0, 0, 0, 1) // Vertext Shader @@ -306,22 +301,20 @@ export class Pow { this.gl.drawArrays(this.gl.TRIANGLES, 0, 6) const pixels = new Uint8Array(this.gl.drawingBufferWidth * this.gl.drawingBufferHeight * 4) - console.log(`start readPixels: ${performance.now()}`) this.gl.readPixels(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight, this.gl.RGBA, this.gl.UNSIGNED_BYTE, pixels) - console.log(`end readPixels: ${performance.now()}`) // Check the pixels for any success for (let i = pixels.length - 4; i >= 0; i -= 4) { if (pixels[i] !== 0) { console.log(`frame time: ${performance.now() - start}`) + const hex = this.hexify(this.work1) + this.hexify([ + pixels[i + 2], + pixels[i + 3], + this.work0[2] ^ (pixels[i] - 1), + this.work0[3] ^ (pixels[i + 1] - 1) + ]) // Return the work value with the custom bits - typeof callback === 'function' && - callback(this.hexify(this.work1) + this.hexify([ - pixels[i + 2], - pixels[i + 3], - this.work0[2] ^ (pixels[i] - 1), - this.work0[3] ^ (pixels[i + 1] - 1) - ])) + typeof callback === 'function' && callback(hex) return } }