GPU

var gpu = new GPU();

Initialises the GPU.js library class which manages the WebGL context for the created functions.


var kernelObj = gpu.createKernel(kernelFunc, [options]);

Parameters

kernelFunc
The kernel function to compute a single element in the output array. The function body must use the gpu.js specific subset of JavaScript or the compilation will fail.
options (optional)
The options object for the kernel function. The following options are available:
Option Default Description
dimensions An array containing the desired dimensions of the output.
constants An object containing key-value pairs to declare as constants to be used in the kernel. These constants will be available as this.constants.<name>. These constants can also be used to declare the limits of a fixed-sized loop.
debug false Set flag to true to print compile information in the JavaScript console.
graphical false Set flag to true to produce an image in the Canvas object rather than returning numbers
hardcodeConstants false Set flag to true to hardcode input array size and dimension constants rather than copying to the WebGL uniform buffer. May or may not improve performance at the penalty of additional compilation on input of different size.
outputToTexture false Set flag to true to output to a gpu.js Texture Object that can be used as input to kernels. Eliminates the round-trip penalty of sending the output of a kernel into the input of another kernel. Note: Texture objects are only supported on GPU mode.
loopMaxIterations 100 WebGL requires all loops to be of fixed length. If you cannot statically determine the length of your for loops, you can generate shader code that makes your loops fixed length for you. However, you must specify the upper bound of your for loops. This upper bound cannot be too high, otherwise the shader will be killed by your browser for taking too long.
mode auto "auto", "gpu" or "cpu". "auto" will fallback to "cpu" if "gpu" fails. Otherwise an error is thrown upon failure.

Return value

A new kernel function object.

gpu.addFunction(func);

Parameters

func
The helper function to make available for use inside the kernel function. This function must be named and can only accept numbers as input and must return a number. The function body must use the gpu.js specific subset of JavaScript or the compilation will fail.

var gl = gpu.getGl();

Return value

Get the WebGL Context.

var canvas = gpu.getCanvas([mode]);

Parameters

mode (optional)
The mode of the canvas to retrieve. Either "cpu" or "gpu" (Default is "gpu").

Return value

Gets the canvas for the mode type.

var arr = gpu.textureToArray(textureObj);

Parameters

textureObj
The gpu.js Texture Object to convert into an array. Note: not to be confused with WebGL Textures. Texture objects are WebGL Textures with additional metadata information.

Return value

The values in the Texture Object as a JavaScript Array.

Kernel Function Object

var out = kernelObj([args]);

Parameters

[args] (should match up to the paramaters used during kernel creation)
Multiple arguments for a number, 1D, 2D 3D array, or a gpu.js Texture Object to be forwarded to the kernel. The arguments given must match up with the paramters used during kernel creation.

Return value

The output array or a gpu.js Texture object produced by executing the kernel function.

kernelObj.dimensions(dim);
Same as providing options to createKernel. This method is chainable.
kernelObj.debug(flag);
Same as providing options to createKernel. This method is chainable.
kernelObj.graphical(flag);
Same as providing options to createKernel. This method is chainable.
kernelObj.hardcodeConstants(flag);
Same as providing options to createKernel. This method is chainable.
kernelObj.outputToTexture(flag);
Same as providing options to createKernel. This method is chainable.
kernelObj.loopMaxIterations(max);
Same as providing options to loopMaxIterations. This method is chainable.
kernelObj.mode(mode);
Same as providing options to createKernel. This method is chainable.
var canvas = kernelObj.getCanvas();

Return value

Gets the canvas for the kernel.

Texture Object

var arr = textureObj.toArray();

Return value

The values in the Texture Object as a JavaScript Array.