diff --git a/bin/gpu-browser-core.js b/bin/gpu-browser-core.js index 321f5656..4caf79c7 100644 --- a/bin/gpu-browser-core.js +++ b/bin/gpu-browser-core.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:13 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:46 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -3466,9 +3466,10 @@ class GLKernel extends Kernel { const XResultsMax = xMax * 4; for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * XResultsMax; let i = 0; for (let x = 0; x < XResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -3483,9 +3484,10 @@ class GLKernel extends Kernel { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; for (let x = 0; x < xResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -3507,13 +3509,14 @@ class GLKernel extends Kernel { render2DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -3522,15 +3525,16 @@ class GLKernel extends Kernel { render3DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -3552,13 +3556,14 @@ class GLKernel extends Kernel { render2DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResult = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResult; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } @@ -3567,15 +3572,16 @@ class GLKernel extends Kernel { render3DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xResultsMap = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xResultsMap; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } diff --git a/bin/gpu-browser-core.min.js b/bin/gpu-browser-core.min.js index b6a240ce..5ee9ff7a 100644 --- a/bin/gpu-browser-core.min.js +++ b/bin/gpu-browser-core.min.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:16 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:48 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -18,7 +18,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:13 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:46 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -3479,9 +3479,10 @@ class GLKernel extends Kernel { const XResultsMax = xMax * 4; for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * XResultsMax; let i = 0; for (let x = 0; x < XResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -3496,9 +3497,10 @@ class GLKernel extends Kernel { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; for (let x = 0; x < xResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -3520,13 +3522,14 @@ class GLKernel extends Kernel { render2DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -3535,15 +3538,16 @@ class GLKernel extends Kernel { render3DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -3565,13 +3569,14 @@ class GLKernel extends Kernel { render2DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResult = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResult; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } @@ -3580,15 +3585,16 @@ class GLKernel extends Kernel { render3DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xResultsMap = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xResultsMap; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } diff --git a/bin/gpu-browser.js b/bin/gpu-browser.js index 7c93f786..e6597e82 100644 --- a/bin/gpu-browser.js +++ b/bin/gpu-browser.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:14 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:46 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -8230,9 +8230,10 @@ class GLKernel extends Kernel { const XResultsMax = xMax * 4; for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * XResultsMax; let i = 0; for (let x = 0; x < XResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -8247,9 +8248,10 @@ class GLKernel extends Kernel { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; for (let x = 0; x < xResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -8271,13 +8273,14 @@ class GLKernel extends Kernel { render2DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -8286,15 +8289,16 @@ class GLKernel extends Kernel { render3DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -8316,13 +8320,14 @@ class GLKernel extends Kernel { render2DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResult = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResult; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } @@ -8331,15 +8336,16 @@ class GLKernel extends Kernel { render3DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xResultsMap = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xResultsMap; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } diff --git a/bin/gpu-browser.min.js b/bin/gpu-browser.min.js index be38fcf4..8eda1be2 100644 --- a/bin/gpu-browser.min.js +++ b/bin/gpu-browser.min.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:16 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:48 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -18,7 +18,7 @@ * GPU Accelerated JavaScript * * @version 2.0.0-rc.13 - * @date Mon Apr 29 2019 08:45:14 GMT-0400 (Eastern Daylight Time) + * @date Mon Apr 29 2019 20:46:46 GMT+0200 (CEST) * * @license MIT * The MIT License @@ -8243,9 +8243,10 @@ class GLKernel extends Kernel { const XResultsMax = xMax * 4; for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * XResultsMax; let i = 0; for (let x = 0; x < XResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -8260,9 +8261,10 @@ class GLKernel extends Kernel { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; for (let x = 0; x < xResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -8284,13 +8286,14 @@ class GLKernel extends Kernel { render2DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -8299,15 +8302,16 @@ class GLKernel extends Kernel { render3DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -8329,13 +8333,14 @@ class GLKernel extends Kernel { render2DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResult = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResult; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } @@ -8344,15 +8349,16 @@ class GLKernel extends Kernel { render3DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xResultsMap = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xResultsMap; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } diff --git a/src/backend/gl-kernel.js b/src/backend/gl-kernel.js index 5b9f06ac..fc334a6c 100644 --- a/src/backend/gl-kernel.js +++ b/src/backend/gl-kernel.js @@ -752,9 +752,10 @@ class GLKernel extends Kernel { const XResultsMax = xMax * 4; for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * XResultsMax; let i = 0; for (let x = 0; x < XResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -769,9 +770,10 @@ class GLKernel extends Kernel { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; for (let x = 0; x < xResultsMax; x += 4) { - xResults[i++] = pixels.subarray(x, x + 2); + xResults[i++] = pixels.subarray(x + offset, x + offset + 2); } yResults[y] = xResults; } @@ -793,13 +795,14 @@ class GLKernel extends Kernel { render2DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -808,15 +811,16 @@ class GLKernel extends Kernel { render3DArray3() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xMaxResults = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xMaxResults; x += 4) { - xResults[i++] = pixels.subarray(x, x + 3); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 3); } yResults[y] = xResults; } @@ -838,13 +842,14 @@ class GLKernel extends Kernel { render2DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax] = this.output; - const xMaxResult = xMax * 4; + const xResultsMax = xMax * 4; const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = y * xResultsMax; let i = 0; - for (let x = 0; x < xMaxResult; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } @@ -853,15 +858,16 @@ class GLKernel extends Kernel { render3DArray4() { const pixels = this.readFloatPixelsToFloat32Array(); const [xMax, yMax, zMax] = this.output; - const xResultsMap = xMax * 4; + const xResultsMax = xMax * 4; const zResults = new Array(zMax); for (let z = 0; z < zMax; z++) { const yResults = new Array(yMax); for (let y = 0; y < yMax; y++) { const xResults = new Array(xMax); + const offset = (z * xResultsMax * yMax) + (y * xResultsMax); let i = 0; - for (let x = 0; x < xResultsMap; x += 4) { - xResults[i++] = pixels.subarray(x, x + 4); + for (let x = 0; x < xResultsMax; x += 4) { + xResults[i++] = pixels.subarray(x + offset, x + offset + 4); } yResults[y] = xResults; } diff --git a/test/features/return-arrays.js b/test/features/return-arrays.js index 63e55c31..1f08500e 100644 --- a/test/features/return-arrays.js +++ b/test/features/return-arrays.js @@ -40,10 +40,15 @@ test('return Array(2) from kernel cpu', () => { function returnArray2D2FromKernel(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { - return [1, 2]; - }, { output: [2, 2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(result.map(matrix => matrix.map(row => Array.from(row))), [[[1, 2], [1, 2]],[[1, 2], [1, 2]]]); + return [this.thread.x, this.thread.y]; + }, { output : [3, 7], precision: 'single' }); + const res = kernel(); + for (let y = 0; y < 7; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[y][x][0], x); + assert.equal(res[y][x][1], y); + } + } gpu.destroy(); } @@ -73,11 +78,18 @@ test('return Array2D(2) from kernel cpu', () => { function returnArray3D2FromKernel(mode) { const gpu = new GPU({ mode }); - const kernel = gpu.createKernel(function() { - return [1, 2]; - }, { output: [2, 2, 2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row)))), [[[[1, 2], [1, 2]],[[1, 2], [1, 2]]],[[[1, 2], [1, 2]],[[1, 2], [1, 2]]]]); +const kernel = gpu.createKernel(function() { + return [this.thread.y, this.thread.z]; + }, { output : [3, 5, 7], precision: 'single' }); + const res = kernel(); + for (let z = 0; z < 7; ++z) { + for (let y = 0; y < 5; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[z][y][x][0], y); + assert.equal(res[z][y][x][1], z); + } + } + } gpu.destroy(); } @@ -143,10 +155,16 @@ test('return Array(3) from kernel cpu', () => { function returnArray2D3FromKernel(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { - return [1, 2, 3]; - }, { output: [2,2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(Array.from(result.map(matrix => matrix.map(row => Array.from(row)))), [[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]]); + return [this.thread.x, this.thread.y, this.thread.x * this.thread.y]; + }, { output : [3, 7] ,precision: 'single' }); + const res = kernel(); + for (let y = 0; y < 7; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[y][x][0], x); + assert.equal(res[y][x][1], y); + assert.equal(res[y][x][2], x * y); + } + } gpu.destroy(); } @@ -177,10 +195,18 @@ test('return Array2D(3) from kernel cpu', () => { function returnArray3D3FromKernel(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { - return [1, 2, 3]; - }, { output: [2,2,2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(Array.from(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row))))), [[[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]],[[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]]]); + return [this.thread.x, this.thread.y, this.thread.z]; + }, { output : [3, 5, 7] ,precision: 'single' }); + const res = kernel(); + for (let z = 0; z < 7; ++z) { + for (let y = 0; y < 5; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[z][y][x][0], x); + assert.equal(res[z][y][x][1], y); + assert.equal(res[z][y][x][2], z); + } + } + } gpu.destroy(); } @@ -245,10 +271,17 @@ test('return Array(4) from kernel cpu', () => { function returnArray2D4FromKernel(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { - return [1, 2, 3, 4]; - }, { output: [2,2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(result.map(matrix => matrix.map(row => Array.from(row))), [[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]]); + return [this.thread.x, this.thread.y, this.thread.x * this.thread.y, this.thread.x - this.thread.y]; + }, { output : [3, 7], precision: 'single' }); + const res = kernel(); + for (let y = 0; y < 3; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[y][x][0], x); + assert.equal(res[y][x][1], y); + assert.equal(res[y][x][2], x * y); + assert.equal(res[y][x][3], x - y); + } + } gpu.destroy(); } @@ -279,10 +312,19 @@ test('return Array2D(4) from kernel cpu', () => { function returnArray3D4FromKernel(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { - return [1, 2, 3, 4]; - }, { output: [2,2,2], precision: 'single' }); - const result = kernel(); - assert.deepEqual(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row)))), [[[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]],[[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]]]); + return [this.thread.x, this.thread.y, this.thread.z, this.thread.x * this.thread.y * this.thread.z]; + }, { output : [3, 5, 7], precision: 'single' }); + const res = kernel(); + for (let z = 0; z < 7; ++z) { + for (let y = 0; y < 5; ++y) { + for (let x = 0; x < 3; ++x) { + assert.equal(res[z][y][x][0], x); + assert.equal(res[z][y][x][1], y); + assert.equal(res[z][y][x][2], z); + assert.equal(res[z][y][x][3], x * y * z); + } + } + } gpu.destroy(); }