Skip to content

Commit 96db402

Browse files
fix: #289 update documentation for existing feature
1 parent 20a9ed5 commit 96db402

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ NOTE: documentation is slightly out of date for the upcoming release of v2. We
8888
* [Offscreen Canvas](#offscreen-canvas)
8989
* [Cleanup](#cleanup)
9090
* [Flattened typed array support](#flattened-typed-array-support)
91+
* [Precompiled and Lighter Weight Kernels](#precompiled-and-lighter-weight-kernels)
9192
* [Supported Math functions](#supported-math-functions)
9293
* [Typescript Typings](#typescript-typings)
9394
* [Full API reference](#full-api-reference)
@@ -760,6 +761,49 @@ kernel(
760761

761762
Note: `input(value, size)` is a simple pointer for `new Input(value, size)`
762763

764+
## Precompiled and Lighter Weight Kernels
765+
766+
### using JSON
767+
GPU.js packs a lot of functionality into a single file, such as a complete javascript parse, which may not be needed in some cases.
768+
To aid in keeping your kernels lightweight, the `kernel.toJSON()` method was added.
769+
This allows you to reuse a previously built kernel, without the need to re-parse the javascript.
770+
Here is an example:
771+
772+
```js
773+
const gpu = new GPU();
774+
const kernel = gpu.createKernel(function() {
775+
return [1,2,3,4];
776+
}, { output: [1] });
777+
console.log(kernel()); // [Float32Array([1,2,3,4])];
778+
const json = kernel.toJSON();
779+
const newKernelFromJson = gpu.createKernel(json);
780+
console.log(newKernelFromJSON()); // [Float32Array([1,2,3,4])];
781+
```
782+
783+
NOTE: There is lighter weight, pre-built, version of GPU.js to assist with serializing from to and from json in the bin folder of the project, which include:
784+
* [bin/gpu-browser-core.js](bin/gpu-browser-core.js)
785+
* [bin/gpu-browser-core.min.js](bin/gpu-browser-core.min.js)
786+
787+
### using `kernel.toString(args...)`
788+
GPU.js supports seeing exactly how it is interacting with the graphics processor by means of the `kernel.toString(...)` method.
789+
This method, when called, creates a kernel that executes _exactly the instruction set given to the GPU_ as a function that sets up a kernel.
790+
Here is an example:
791+
792+
```js
793+
const gpu = new GPU();
794+
const kernel = gpu.createKernel(function(a) {
795+
let sum = 0;
796+
for (let i = 0; i < 6; i++) {
797+
sum += a[this.thread.x][i];
798+
}
799+
return sum;
800+
}, { output: [6] });
801+
kernel(input(a, [6, 6]));
802+
const kernelString = kernel.toString(input(a, [6, 6]));
803+
const newKernel = new Function('return ' + kernelString)()(context);
804+
newKernel(input(a, [6, 6]));
805+
```
806+
763807
## Supported Math functions
764808

765809
Since the code running in the kernel is actually compiled to GLSL code, not all functions from the JavaScript Math module are supported.

0 commit comments

Comments
 (0)