Skip to content

Commit de98b01

Browse files
feat: Finish HTMLCanvas input from PR #566
fix: #567 fix: #569 fix: #568 fix: #565 fix: #564
1 parent 13f70f4 commit de98b01

38 files changed

+1687
-290
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ Settings are an object used to create a `kernel` or `kernelMap`. Example: `gpu.
230230
* `optimizeFloatMemory` or `kernel.setOptimizeFloatMemory(boolean)` **New in V2!**: boolean - causes a float32 texture to use all 4 channels rather than 1, using less memory, but consuming more GPU.
231231
* `precision` or `kernel.setPrecision('unsigned' | 'single')` **New in V2!**: 'single' or 'unsigned' - if 'single' output texture uses float32 for each colour channel rather than 8
232232
* `fixIntegerDivisionAccuracy` or `kernel.setFixIntegerDivisionAccuracy(boolean)` : boolean - some cards have accuracy issues dividing by factors of three and some other primes (most apple kit?). Default on for affected cards, disable if accuracy not required.
233-
* `functions` or `kernel.setFunctions(object)`: array, array of functions to be used inside kernel. If undefined, inherits from `GPU` instance.
234-
* `nativeFunctions` or `kernel.setNativeFunctions(object)`: object, defined as: `{ name: string, source: string, settings: object }`. This is generally set via using GPU.addNativeFunction()
233+
* `functions` or `kernel.setFunctions(array)`: array, array of functions to be used inside kernel. If undefined, inherits from `GPU` instance. Can also be an array of `{ source: function, argumentTypes: object, returnType: string }`.
234+
* `nativeFunctions` or `kernel.setNativeFunctions(array)`: object, defined as: `{ name: string, source: string, settings: object }`. This is generally set via using GPU.addNativeFunction()
235235
* VERY IMPORTANT! - Use this to add special native functions to your environment when you need specific functionality is needed.
236236
* `injectedNative` or `kernel.setInjectedNative(string)` **New in V2!**: string, defined as: `{ functionName: functionSource }`. This is for injecting native code before translated kernel functions.
237237
* `subKernels` or `kernel.setSubKernels(array)`: array, generally inherited from `GPU` instance.
@@ -667,7 +667,8 @@ megaKernel(a, b, c);
667667
This gives you the flexibility of using parts of a single transformation without the performance penalty, resulting in much much _MUCH_ faster operation.
668668

669669
## Adding custom functions
670-
use `gpu.addFunction(function() {}, settings)` for adding custom functions. Example:
670+
### To `GPU` instance
671+
use `gpu.addFunction(function() {}, settings)` for adding custom functions to all kernels. Example:
671672

672673

673674
```js
@@ -683,6 +684,23 @@ const kernel = gpu.createKernel(function(a, b) {
683684
}).setOutput([20]);
684685
```
685686

687+
### To `Kernel` instance
688+
use `kernel.addFunction(function() {}, settings)` for adding custom functions to all kernels. Example:
689+
690+
691+
```js
692+
kernel.addFunction(function mySuperFunction(a, b) {
693+
return a - b;
694+
});
695+
function anotherFunction(value) {
696+
return value + 1;
697+
}
698+
kernel.addFunction(anotherFunction);
699+
const kernel = gpu.createKernel(function(a, b) {
700+
return anotherFunction(mySuperFunction(a[this.thread.x], b[this.thread.x]));
701+
}).setOutput([20]);
702+
```
703+
686704
### Adding strongly typed functions
687705

688706
To manually strongly type a function you may use settings.
@@ -691,13 +709,23 @@ Settings take an optional hash values:
691709
* `returnType`: optional, defaults to inference from `FunctionBuilder`, the value you'd like to return from the function.
692710
* `argumentTypes`: optional, defaults to inference from `FunctionBuilder` for each param, a hash of param names with values of the return types.
693711

694-
Example:
712+
Example on `GPU` instance:
695713
```js
696714
gpu.addFunction(function mySuperFunction(a, b) {
697715
return [a - b[1], b[0] - a];
698716
}, { argumentTypes: { a: 'Number', b: 'Array(2)'}, returnType: 'Array(2)' });
699717
```
700718

719+
Example on `Kernel` instance:
720+
```js
721+
kernel.addFunction(function mySuperFunction(a, b) {
722+
return [a - b[1], b[0] - a];
723+
}, { argumentTypes: { a: 'Number', b: 'Array(2)'}, returnType: 'Array(2)' });
724+
```
725+
726+
NOTE: GPU.js infers types if they are not defined and is generally able to detect the types you need, however
727+
'Array(2)', 'Array(3)', and 'Array(4)' are exceptions, at least on the kernel level. Also, it is nice to have power
728+
over the automatic type inference system.
701729

702730
## Adding custom functions directly to kernel
703731
```js
@@ -732,6 +760,7 @@ Types that can be used with GPU.js are as follows:
732760
* 'Array3D(2)' **New in V2!**
733761
* 'Array3D(3)' **New in V2!**
734762
* 'Array3D(4)' **New in V2!**
763+
* 'HTMLCanvas' **New in V2.6**
735764
* 'HTMLImage'
736765
* 'HTMLImageArray'
737766
* 'HTMLVideo' **New in V2!**

0 commit comments

Comments
 (0)