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;
};
// Block hash
for (i=0;i<8;i++) {
- m[i+2] = blockHash[i];
+ m[i+2] = blockhash[i];
}
// twelve rounds of mixing
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
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 {
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)