@@ -88,6 +88,7 @@ NOTE: documentation is slightly out of date for the upcoming release of v2. We
88
88
* [ Offscreen Canvas] ( #offscreen-canvas )
89
89
* [ Cleanup] ( #cleanup )
90
90
* [ Flattened typed array support] ( #flattened-typed-array-support )
91
+ * [ Precompiled and Lighter Weight Kernels] ( #precompiled-and-lighter-weight-kernels )
91
92
* [ Supported Math functions] ( #supported-math-functions )
92
93
* [ Typescript Typings] ( #typescript-typings )
93
94
* [ Full API reference] ( #full-api-reference )
@@ -760,6 +761,49 @@ kernel(
760
761
761
762
Note: ` input(value, size) ` is a simple pointer for ` new Input(value, size) `
762
763
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
+
763
807
## Supported Math functions
764
808
765
809
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