Skip to content

Commit 254b067

Browse files
feat: Handle ++ in function-tracer.js
This is in favor of using for loop variable position tracking, which is removed. feat: Removal of no longer needed `warnVarUsage` feat: If ternary returns void, use if statement in webgl fix: Update documentation and clarify variable declarations Officially support private functions - WOOHOO!
1 parent cd0b417 commit 254b067

26 files changed

+660
-709
lines changed

README.md

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ NOTE: documentation is slightly out of date for the upcoming release of v2. We
7878
* [Installation](#installation)
7979
* [`GPU` Settings](#gpu-settings)
8080
* [`gpu.createKernel` Settings](#gpucreatekernel-settings)
81+
* [Declaring variables/functions within kernels](#declaring-variablesfunctions-within-kernels)
8182
* [Creating and Running Functions](#creating-and-running-functions)
8283
* [Debugging](#debugging)
8384
* [Accepting Input](#accepting-input)
@@ -237,7 +238,6 @@ Settings are an object used to create a `kernel` or `kernelMap`. Example: `gpu.
237238
* ~~`immutable` or `kernel.setImmutable(boolean)`: boolean, default = `false`~~ Deprecated
238239
* `strictIntegers` or `kernel.setStrictIntegers(boolean)`: boolean, default = `false` - allows undefined argumentTypes and function return values to use strict integer declarations.
239240
* `useLegacyEncoder` or `kernel.setUseLegacyEncoder(boolean)`: boolean, default `false` - more info [here](https://github.com/gpujs/gpu.js/wiki/Encoder-details).
240-
* `warnVarUsage` or `kernel.setWarnVarUsage(boolean)`: turn off var usage warnings, they can be irritating, and in transpiled environments, there is nothing we can do about it.
241241
* `tactic` or `kernel.setTactic('speed' | 'balanced' | 'precision')` **New in V2!**: Set the kernel's tactic for compilation. Allows for compilation to better fit how GPU.js is being used (internally uses `lowp` for 'speed', `mediump` for 'balanced', and `highp` for 'precision'). Default is lowest resolution supported for output.
242242

243243

@@ -292,70 +292,93 @@ kernel();
292292
// Result: Float32Array[0, 1, 2, 3, ... 99]
293293
```
294294

295-
### Declaring variables
295+
### Declaring variables/functions within kernels
296296

297297
GPU.js makes variable declaration inside kernel functions easy. Variable types supported are:
298-
Numbers
299-
Array(2)
300-
Array(3)
301-
Array(4)
298+
* `Number` (Integer or Number), example: `let value = 1` or `let value = 1.1`
299+
* `Boolean`, example: `let value = true`
300+
* `Array(2)`, example: `let value = [1, 1]`
301+
* `Array(3)`, example: `let value = [1, 1, 1]`
302+
* `Array(4)`, example: `let value = [1, 1, 1, 1]`
303+
* `private Function`, example: `function myFunction(value) { return value + 1; }`
304+
305+
`Number` kernel example:
306+
```js
307+
const kernel = gpu.createKernel(function() {
308+
const i = 1;
309+
const j = 0.89;
310+
return i + j;
311+
}).setOutput([100]);
312+
```
302313

303-
Numbers example:
314+
`Boolean` kernel example:
304315
```js
305-
const kernel = gpu.createKernel(function() {
306-
const i = 1;
307-
const j = 0.89;
308-
return i + j;
309-
}).setOutput([100]);
316+
const kernel = gpu.createKernel(function() {
317+
const i = true;
318+
if (i) return 1;
319+
return 0;
320+
}).setOutput([100]);
310321
```
311322

312-
Array(2) examples:
323+
`Array(2)` kernel examples:
313324
Using declaration
314325
```js
315-
const kernel = gpu.createKernel(function() {
316-
const array2 = [0.08, 2];
317-
return array2;
318-
}).setOutput([100]);
326+
const kernel = gpu.createKernel(function() {
327+
const array2 = [0.08, 2];
328+
return array2;
329+
}).setOutput([100]);
319330
```
320331

321332
Directly returned
322333
```js
323-
const kernel = gpu.createKernel(function() {
324-
return [0.08, 2];
325-
}).setOutput([100]);
334+
const kernel = gpu.createKernel(function() {
335+
return [0.08, 2];
336+
}).setOutput([100]);
326337
```
327338

328-
Array(3) example:
339+
`Array(3)` kernel example:
329340
Using declaration
330341
```js
331-
const kernel = gpu.createKernel(function() {
332-
const array2 = [0.08, 2, 0.1];
333-
return array2;
334-
}).setOutput([100]);
342+
const kernel = gpu.createKernel(function() {
343+
const array2 = [0.08, 2, 0.1];
344+
return array2;
345+
}).setOutput([100]);
335346
```
336347

337348
Directly returned
338349
```js
339-
const kernel = gpu.createKernel(function() {
340-
return [0.08, 2, 0.1];
341-
}).setOutput([100]);
350+
const kernel = gpu.createKernel(function() {
351+
return [0.08, 2, 0.1];
352+
}).setOutput([100]);
342353
```
343354

344-
Array(4) example:
355+
`Array(4)` kernel example:
345356
Using declaration
346357
```js
347-
const kernel = gpu.createKernel(function() {
348-
const array2 = [0.08, 2, 0.1, 3];
349-
return array2;
350-
}).setOutput([100]);
358+
const kernel = gpu.createKernel(function() {
359+
const array2 = [0.08, 2, 0.1, 3];
360+
return array2;
361+
}).setOutput([100]);
351362
```
352363

353364
Directly returned
354365
```js
355-
const kernel = gpu.createKernel(function() {
356-
return [0.08, 2, 0.1, 3];
357-
}).setOutput([100]);
366+
const kernel = gpu.createKernel(function() {
367+
return [0.08, 2, 0.1, 3];
368+
}).setOutput([100]);
358369
```
370+
371+
`private Function` kernel example:
372+
```js
373+
const kernel = gpu.createKernel(function() {
374+
function myPrivateFunction() {
375+
return [0.08, 2, 0.1, 3];
376+
}
377+
378+
return myPrivateFunction(); // <-- type inherited here
379+
}).setOutput([100]);
380+
```
381+
359382
## Debugging
360383
Debugging can be done in a variety of ways, and there are different levels of debugging.
361384
* Debugging kernels with breakpoints can be done with `new GPU({ mode: 'dev' })`
@@ -1087,17 +1110,6 @@ Transpilation doesn't do the best job of keeping code beautiful. To aid in this
10871110
Here is a list of a few things that GPU.js does to fix transpilation:
10881111

10891112
* When a transpiler such as [Babel](https://babeljs.io/) changes `myCall()` to `(0, _myCall.myCall)`, it is gracefully handled.
1090-
* Using `var` will have a lot of warnings by default, this can be irritating because sometimes there is nothing we can do about this in transpiled environment.
1091-
To aid in the irritation, there is an option to alleviate the irritation.
1092-
When `const` and `let` are converted to `var`, and you'r prefer not to see it, use the following:
1093-
```js
1094-
const kernel = gpu.createKernel(myKernelFunction)
1095-
.setWarnVarUsage(false);
1096-
```
1097-
or:
1098-
```js
1099-
const kernel = gpu.createKernel(myKernelFunction, { output: [1], warnVarUsage: false });
1100-
```
11011113

11021114
## Full API Reference
11031115

0 commit comments

Comments
 (0)