Skip to content

Commit ce6fb28

Browse files
committed
merge upstream changes
2 parents cfd8c52 + 47bae6b commit ce6fb28

29 files changed

+1538
-533
lines changed

README.md

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ NOTE: documentation is slightly out of date for the upcoming release of v2. We
7474
* [`GPU` Settings](#gpu-settings)
7575
* [`gpu.createKernel` Settings](#gpu-createkernel-settings)
7676
* [Creating and Running Functions](#creating-and-running-functions)
77+
* [Debugging](#debugging)
7778
* [Accepting Input](#accepting-input)
7879
* [Graphical Output](#graphical-output)
7980
* [Combining Kernels](#combining-kernels)
8081
* [Create Kernel Map](#create-kernel-map)
8182
* [Adding Custom Functions](#adding-custom-functions)
8283
* [Adding Custom Functions Directly to Kernel](#adding-custom-functions-directly-to-kernel)
84+
* [Types](#types)
8385
* [Loops](#loops)
8486
* [Pipelining](#pipelining)
8587
* [Offscreen Canvas](#offscreen-canvas)
@@ -292,6 +294,68 @@ Directly returned
292294
return [0.08, 2, 0.1, 3];
293295
}).setOutput([100]);
294296
```
297+
## Debugging
298+
Debugging can be done in a variety of ways, and there are different levels of debugging.
299+
* Debugging kernels with breakpoints can be done with `new GPU({ mode: 'dev' })`
300+
* This puts `GPU.js` into development mode. Here you can insert breakpoints, and be somewhat liberal in how your kernel is developed.
301+
* This mode _does not_ actually "compile" (parse, and eval) a kernel, it simply iterates on your code.
302+
* You can break a lot of rules here, because your kernel's function still has context of the state it came from.
303+
* Example:
304+
```js
305+
const gpu = new GPU({ mode: 'dev' });
306+
const kernel = gpu.createKernel(function(arg1, time) {
307+
// put a breakpoint on the next line, and watch it get hit
308+
const v = arg1[this.thread.y][this.thread.x * time];
309+
return v;
310+
}, { output: [100, 100] });
311+
```
312+
* Debugging actual kernels on CPU with `debugger`:
313+
* This will cause "breakpoint" like behaviour, but in an actual CPU kernel. You'll peer into the compiled kernel here, for a CPU.
314+
* Example:
315+
```js
316+
const gpu = new GPU({ mode: 'cpu' });
317+
const kernel = gpu.createKernel(function(arg1, time) {
318+
debugger; // <--NOTICE THIS, IMPORTANT!
319+
const v = arg1[this.thread.y][this.thread.x * time];
320+
return v;
321+
}, { output: [100, 100] });
322+
```
323+
* Debugging an actual GPU kernel:
324+
* There are no breakpoints available on the GPU, period. By providing the same level of abstraction and logic, the above methods should give you enough insight to debug, but sometimes we just need to see what is on the GPU.
325+
* Be VERY specific and deliberate, and use the kernel to your advantage, rather than just getting frustrated or giving up.
326+
* Example:
327+
```js
328+
const gpu = new GPU({ mode: 'cpu' });
329+
const kernel = gpu.createKernel(function(arg1, time) {
330+
const x = this.thread.x * time;
331+
return x; // <--NOTICE THIS, IMPORTANT!
332+
const v = arg1[this.thread.y][x];
333+
return v;
334+
}, { output: [100, 100] });
335+
```
336+
In this example, we return early the value of x, to see exactly what it is. The rest of the logic is ignored, but now you can see the value that is calculated from `x`, and debug it.
337+
This is an overly simplified problem.
338+
* Sometimes you need to solve graphical problems, that can be done similarly.
339+
* Example:
340+
```js
341+
const gpu = new GPU({ mode: 'cpu' });
342+
const kernel = gpu.createKernel(function(arg1, time) {
343+
const x = this.thread.x * time;
344+
if (x < 4 || x > 2) {
345+
// RED
346+
this.color(1, 0, 0); // <--NOTICE THIS, IMPORTANT!
347+
return;
348+
}
349+
if (x > 6 && x < 12) {
350+
// GREEN
351+
this.color(0, 1, 0); // <--NOTICE THIS, IMPORTANT!
352+
return;
353+
}
354+
const v = arg1[this.thread.y][x];
355+
return v;
356+
}, { output: [100, 100], graphical: true });
357+
```
358+
Here we are making the canvas red or green depending on the value of `x`.
295359
296360
## Accepting Input
297361
### Supported Input Types
@@ -514,25 +578,11 @@ const kernel = gpu.createKernel(function(a, b) {
514578

515579
### Adding strongly typed functions
516580

517-
To strongly type a function you may use settings. Settings take an optional hash values:
518-
`returnType`: optional, defaults to inference from `FunctionNode`, the value you'd like to return from the function. By setting this value, it makes the build step of the kernel less resource intensive.
519-
`argumentTypes`: optional, defaults to inference from `FunctionNode` for each param, a hash of param names with values of the return types. By setting this value, it makes the build step of the kernel less resource intensive.
520-
521-
Types: that may be used for `returnType` or for each property of `argumentTypes`:
522-
* 'Array'
523-
* 'Array(2)'
524-
* 'Array(3)'
525-
* 'Array(4)'
526-
* 'HTMLImage'
527-
* 'HTMLImageArray'
528-
* 'Number'
529-
* 'Float'
530-
* 'Integer'
531-
* 'NumberTexture'
532-
* 'ArrayTexture(1)'
533-
* 'ArrayTexture(2)'
534-
* 'ArrayTexture(3)'
535-
* 'ArrayTexture(4)'
581+
To manually strongly type a function you may use settings.
582+
By setting this value, it makes the build step of the kernel less resource intensive.
583+
Settings take an optional hash values:
584+
* `returnType`: optional, defaults to inference from `FunctionBuilder`, the value you'd like to return from the function.
585+
* `argumentTypes`: optional, defaults to inference from `FunctionBuilder` for each param, a hash of param names with values of the return types.
536586

537587
Example:
538588
```js
@@ -555,6 +605,44 @@ const kernel = gpu.createKernel(function(a, b) {
555605

556606
```
557607

608+
609+
## Types
610+
GPU.js does type inference when types are not defined, so even if you code weak type, you are typing strongly typed.
611+
This is needed because c++, which glsl is a subset of, is, of course, strongly typed.
612+
Types that can be used with GPU.js are as follows:
613+
614+
### Argument Types
615+
Types: that may be used for `returnType` or for each property of `argumentTypes`:
616+
* 'Array'
617+
* 'Array(2)'
618+
* 'Array(3)'
619+
* 'Array(4)'
620+
* 'HTMLImage'
621+
* 'HTMLImageArray'
622+
* 'Number'
623+
* 'Float'
624+
* 'Integer'
625+
* 'Boolean' **New in V2!**
626+
627+
### Return Types
628+
Types: that may be used for `returnType` or for each property of `argumentTypes`:
629+
* 'Array(2)'
630+
* 'Array(3)'
631+
* 'Array(4)'
632+
* 'HTMLImage'
633+
* 'HTMLImageArray'
634+
* 'Number'
635+
* 'Float'
636+
* 'Integer'
637+
638+
### Internal Types
639+
Types generally used in the `Texture` class, for #pipelining or for advanced usage.
640+
* 'NumberTexture'
641+
* 'ArrayTexture(1)' **New in V2!**
642+
* 'ArrayTexture(2)' **New in V2!**
643+
* 'ArrayTexture(3)' **New in V2!**
644+
* 'ArrayTexture(4)' **New in V2!**
645+
558646
## Loops
559647
* Any loops defined inside the kernel must have a maximum iteration count defined by the loopMaxIterations setting.
560648
* Other than defining the iterations by a constant or fixed value as shown [Dynamic sized via constants](dynamic-sized-via-constants), you can also simply pass the number of iterations as a variable to the kernel

0 commit comments

Comments
 (0)