Skip to content

Commit 26e14d9

Browse files
Merge pull request #305 from gpujs/297-array-images
Closes #297 array of images
2 parents 419be53 + b0ccde2 commit 26e14d9

27 files changed

+630
-149
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ myFunc();
170170
* 2d Array
171171
* 3d Array
172172
* HTML Image
173+
* Array of HTML Images
173174
To define an argument, simply add it to the kernel function like regular JavaScript.
174175

175176
### Input Examples
@@ -193,6 +194,34 @@ myFunc([1, 2, 3]);
193194
// Result: [1, 2, 3, 1, ... 1 ]
194195
```
195196

197+
An HTML Image:
198+
199+
```js
200+
const myFunc = gpu.createKernel(function(image) {
201+
const pixel = image[this.thread.y][this.thread.x];
202+
this.color(pixel[0], pixel[1], pixel[2], pixel[3]);
203+
})
204+
.setGraphical(true)
205+
.setOutput([100]);
206+
207+
myFunc([1, 2, 3]);
208+
// Result: colorful image
209+
```
210+
211+
An Array of HTML Images:
212+
213+
```js
214+
const myFunc = gpu.createKernel(function(image) {
215+
const pixel = image[this.thread.z][this.thread.y][this.thread.x];
216+
this.color(pixel[0], pixel[1], pixel[2], pixel[3]);
217+
})
218+
.setGraphical(true)
219+
.setOutput([100]);
220+
221+
myFunc([1, 2, 3]);
222+
// Result: colorful image
223+
```
224+
196225
## Graphical Output
197226

198227
Sometimes, you want to produce a `canvas` image instead of doing numeric computations. To achieve this, set the `graphical` flag to `true` and the output dimensions to `[width, height]`. The thread identifiers will now refer to the `x` and `y` coordinate of the pixel you are producing. Inside your kernel function, use `this.color(r,g,b)` or `this.color(r,g,b,a)` to specify the color of the pixel.

bin/gpu-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 1.3.0
8-
* @date Mon Jun 04 2018 20:06:27 GMT-0400 (EDT)
7+
* @version 1.4.0
8+
* @date Tue Jun 05 2018 20:28:34 GMT-0400 (EDT)
99
*
1010
* @license MIT
1111
* The MIT License

bin/gpu-core.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/gpu.js

Lines changed: 131 additions & 25 deletions
Large diffs are not rendered by default.

bin/gpu.min.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backend/cpu/kernel.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,13 @@ module.exports = function (_KernelBase) {
336336
value: function _processInputs() {
337337
var result = [];
338338
for (var i = 0; i < this.paramTypes.length; i++) {
339-
if (this.paramTypes[i] === 'HTMLImage') {
340-
result.push(' user_' + this.paramNames[i] + ' = this._imageTo2DArray(user_' + this.paramNames[i] + ')');
339+
switch (this.paramTypes[i]) {
340+
case 'HTMLImage':
341+
result.push(' user_' + this.paramNames[i] + ' = this._imageTo2DArray(user_' + this.paramNames[i] + ')');
342+
break;
343+
case 'HTMLImageArray':
344+
result.push(' user_' + this.paramNames[i] + ' = this._imageTo3DArray(user_' + this.paramNames[i] + ')');
345+
break;
341346
}
342347
}
343348
return result.join(';\n');
@@ -347,15 +352,24 @@ module.exports = function (_KernelBase) {
347352
value: function _imageTo2DArray(image) {
348353
this._canvasCtx.drawImage(image, 0, 0, image.width, image.height);
349354
var pixelsData = this._canvasCtx.getImageData(0, 0, image.width, image.height).data;
350-
var result = new Array(image.height);
355+
var imageArray = new Array(image.height);
351356
var index = 0;
352357
for (var y = 0; y < image.height; y++) {
353-
result[y] = new Array(image.width);
358+
imageArray[y] = new Array(image.width);
354359
for (var x = 0; x < image.width; x++) {
355-
result[y][x] = [pixelsData[index++] / 255, pixelsData[index++] / 255, pixelsData[index++] / 255, pixelsData[index++] / 255];
360+
imageArray[y][x] = [pixelsData[index++] / 255, pixelsData[index++] / 255, pixelsData[index++] / 255, pixelsData[index++] / 255];
356361
}
357362
}
358-
return result;
363+
return imageArray;
364+
}
365+
}, {
366+
key: '_imageTo3DArray',
367+
value: function _imageTo3DArray(images) {
368+
var imagesArray = new Array(images.length);
369+
for (var i = 0; i < images.length; i++) {
370+
imagesArray[i] = this._imageTo2DArray(images[i]);
371+
}
372+
return imagesArray;
359373
}
360374
}], [{
361375
key: 'compileKernel',

dist/backend/web-gl/function-node.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ module.exports = function (_FunctionNodeBase) {
718718
}
719719
var retDeclaration = [];
720720
this.astGeneric(declaration, retDeclaration, funcParam);
721-
if (retDeclaration[2] === 'getImage(') {
721+
if (retDeclaration[2] === 'getImage2D(' || retDeclaration[2] === 'getImage3D(') {
722722
if (i === 0) {
723723
retArr.push('vec4 ');
724724
}
@@ -993,9 +993,28 @@ module.exports = function (_FunctionNodeBase) {
993993
retArr.push(mNode.property.raw);
994994
retArr.push(']');
995995
break;
996+
case 'HTMLImageArray':
997+
// Get from image
998+
retArr.push('getImage3D(');
999+
this.astGeneric(mNode.object, retArr, funcParam);
1000+
retArr.push(', vec2(');
1001+
this.astGeneric(mNode.object, retArr, funcParam);
1002+
retArr.push('Size[0],');
1003+
this.astGeneric(mNode.object, retArr, funcParam);
1004+
retArr.push('Size[1]), vec3(');
1005+
this.astGeneric(mNode.object, retArr, funcParam);
1006+
retArr.push('Dim[0],');
1007+
this.astGeneric(mNode.object, retArr, funcParam);
1008+
retArr.push('Dim[1],');
1009+
this.astGeneric(mNode.object, retArr, funcParam);
1010+
retArr.push('Dim[2]');
1011+
retArr.push('), ');
1012+
this.astGeneric(mNode.property, retArr, funcParam);
1013+
retArr.push(')');
1014+
break;
9961015
case 'HTMLImage':
9971016
// Get from image
998-
retArr.push('getImage(');
1017+
retArr.push('getImage2D(');
9991018
this.astGeneric(mNode.object, retArr, funcParam);
10001019
retArr.push(', vec2(');
10011020
this.astGeneric(mNode.object, retArr, funcParam);

dist/backend/web-gl/kernel.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ module.exports = function (_KernelBase) {
10411041
result.push('uniform highp sampler2D user_' + paramName, 'uniform highp vec2 user_' + paramName + 'Size', 'uniform highp vec3 user_' + paramName + 'Dim');
10421042
} else if (paramType === 'Number') {
10431043
result.push('uniform highp float user_' + paramName);
1044+
} else {
1045+
throw new Error('Param type ' + paramType + ' not supported in WebGL, only WebGL2');
10441046
}
10451047
}
10461048
}

dist/backend/web-gl/shader-frag.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backend/web-gl2/function-builder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
77
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
88

99
var FunctionBuilderBase = require('../function-builder-base');
10-
var WebGLFunctionNode = require('./function-node');
10+
var WebGL2FunctionNode = require('./function-node');
1111

1212
/**
1313
* @class WebGLFunctionBuilder
@@ -25,7 +25,7 @@ module.exports = function (_FunctionBuilderBase) {
2525

2626
var _this = _possibleConstructorReturn(this, (WebGL2FunctionBuilder.__proto__ || Object.getPrototypeOf(WebGL2FunctionBuilder)).call(this));
2727

28-
_this.Node = WebGLFunctionNode;
28+
_this.Node = WebGL2FunctionNode;
2929
return _this;
3030
}
3131

0 commit comments

Comments
 (0)