Skip to content

Commit

Permalink
Merge pull request #389 from gpujs/deep-type-detection
Browse files Browse the repository at this point in the history
Deep type detection
  • Loading branch information
robertleeplummerjr authored Oct 31, 2018
2 parents 43bb07e + d765de4 commit d051256
Show file tree
Hide file tree
Showing 37 changed files with 1,121 additions and 663 deletions.
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,72 @@ const myFunc = gpu.createKernel(function() {
myFunc();
// Result: [0, 1, 2, 3, ... 99]
```

### Declaring variables

GPU.js makes variable declaration inside kernel functions easy. Variable types supported are:
Numbers
Array(2)
Array(3)
Array(4)

Numbers example:
```js
const myFunc = gpu.createKernel(function() {
const i = 1;
const j = 0.89;
return i + j;
}).setOutput([100]);
```

Array(2) examples:
Using declaration
```js
const myFunc = gpu.createKernel(function() {
const array2 = [0.08, 2];
return array2;
}).setOutput([100]);
```

Directly returned
```js
const myFunc = gpu.createKernel(function() {
return [0.08, 2];
}).setOutput([100]);
```

Array(3) example:
Using declaration
```js
const myFunc = gpu.createKernel(function() {
const array2 = [0.08, 2, 0.1];
return array2;
}).setOutput([100]);
```

Directly returned
```js
const myFunc = gpu.createKernel(function() {
return [0.08, 2, 0.1];
}).setOutput([100]);
```

Array(4) example:
Using declaration
```js
const myFunc = gpu.createKernel(function() {
const array2 = [0.08, 2, 0.1, 3];
return array2;
}).setOutput([100]);
```

Directly returned
```js
const myFunc = gpu.createKernel(function() {
return [0.08, 2, 0.1, 3];
}).setOutput([100]);
```

## Accepting Input
### Supported Input Types
* Numbers
Expand Down Expand Up @@ -346,7 +412,9 @@ megaKernel(a, b, c);
This gives you the flexibility of using parts of a single transformation without the performance penalty, resulting in much much _MUCH_ faster operation.

## Adding custom functions
Do you have a custom function you'd like to use on the gpu? Although limited, you can:
use `gpu.addFunction(function() {}, options)` for adding custom functions. Example:


```js
gpu.addFunction(function mySuperFunction(a, b) {
return a - b;
Expand All @@ -360,6 +428,31 @@ const kernel = gpu.createKernel(function(a, b) {
}).setOutput([20]);
```

### Adding strongly typed functions

To strongly type a function you may use options. Options take an optional hash values:
`returnType`: optional, defaults to float, the value you'd like to return from the function
`paramTypes`: optional, defaults to float for each param, a hash of param names with values of the return types

Types: that may be used for `returnType` or for each property of `paramTypes`:
'Array'
'Array(2)'
'Array(3)'
'Array(4)'
'HTMLImage'
'HTMLImageArray'
'Number'
'NumberTexture'
'ArrayTexture(4)'

Example:
```js
gpu.addFunction(function mySuperFunction(a, b) {
return [a - b[1], b[0] - a];
}, { paramTypes: { a: 'Integer', b: 'Array(2)'}, returnType: 'Array(2)' });
```


## Adding custom functions directly to kernel
```js
function mySuperFunction(a, b) {
Expand Down
4 changes: 2 additions & 2 deletions bin/gpu-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 1.9.1
* @date Wed Oct 24 2018 15:14:26 GMT-0400 (EDT)
* @version 1.10.0
* @date Tue Oct 30 2018 07:33:44 GMT-0600 (MDT)
*
* @license MIT
* The MIT License
Expand Down
4 changes: 2 additions & 2 deletions bin/gpu-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d051256

Please sign in to comment.