GPU Accelerated JavaScript
The GPU.js library class which manages the GPU context for the creating kernels
GPU
Creates an instance of GPU.
| Name | Type | Description | |
|---|---|---|---|
| settings |
IGPUSettings
|
|
Optional |
Void
| Name | Type | Description | |
|---|---|---|---|
| source |
Function
String
object
|
|
|
| settings |
IGPUKernelSettings
|
|
Optional |
IKernelRunShortcut
callable function to run
| Name | Type | Description | |
|---|---|---|---|
| reasons |
Array.<IReason>
|
||
| args |
IArguments
|
||
| _kernel |
Kernel
|
Create a super kernel which executes sub kernels and saves their output to be used with the next sub kernel. This can be useful if we want to save the output on one kernel, and then use it as an input to another kernel. Machine Learning
| Name | Type | Description | |
|---|---|---|---|
| subKernels |
Object
Array
|
|
|
| rootKernel |
Function
|
|
const megaKernel = gpu.createKernelMap({
addResult: function add(a, b) {
return a[this.thread.x] + b[this.thread.x];
},
multiplyResult: function multiply(a, b) {
return a[this.thread.x] * b[this.thread.x];
},
}, function(a, b, c) {
return multiply(add(a, b), c);
});
megaKernel(a, b, c);
Note: You can also define subKernels as an array of functions.
> [add, multiply]
Function
callable kernel function
Combine different kernels into one super Kernel, useful to perform multiple operations inside one kernel without the penalty of data transfer between cpu and gpu.
The number of kernel functions sent to this method can be variable. You can send in one, two, etc.
| Name | Type | Description | |
|---|---|---|---|
| subKernels |
Function
|
|
|
| rootKernel |
Function
|
|
combineKernels(add, multiply, function(a,b,c){
return add(multiply(a,b), c)
})
Function
Callable kernel function
| Name | Type | Description | |
|---|---|---|---|
| source |
Function
String
|
|
|
| settings |
IFunctionSettings
|
Optional |
GPU
returns itself
| Name | Type | Description | |
|---|---|---|---|
| name |
String
|
|
|
| source |
String
|
|
|
| settings |
object
|
Optional |
GPU
returns itself
Inject a string just before translated kernel functions
| Name | Type | Description | |
|---|---|---|---|
| source |
String
|
GPU
A kernel may also be given as source text. This is the only form available where the engine does not retain function source — React Native's Hermes, for instance, returns "function name(a0, a1) { [bytecode] }" from Function.prototype.toString().
Void
Makes kernels easier for mortals (including me)
| Name | Type | Description | |
|---|---|---|---|
| kernel |
function()
String
'LE' or 'BE' depending on system architecture Credit: https://gist.github.com/TooTallNate/4750953
| Name | Type | Description | |
|---|---|---|---|
| funcObj |
Function
|
|
Boolean
TRUE if the object is a JS function
| Name | Type | Description | |
|---|---|---|---|
| fn |
String
|
|
Boolean
TRUE if the string passes basic validation
| Name | Type | Description | |
|---|---|---|---|
| funcStr |
String
|
|
String
Function name string (if found)
| Name | Type | Description | |
|---|---|---|---|
| fn |
String
|
|
Array.<String>
Array representing all the parameter names
| Name | Type | Description | |
|---|---|---|---|
| obj |
Object
|
|
Object
Array
Cloned object
| Name | Type | Description | |
|---|---|---|---|
| array |
Object
|
|
Boolean
true if is array or Array-like object
| Name | Type | Description | |
|---|---|---|---|
| value |
|
||
| strictIntegers |
boolean
|
Optional |
String
Argument type Array/Number/Float/Texture/Unknown
| Name | Type | Description | |
|---|---|---|---|
| length |
Number
|
TextureDimensions
A texture takes up four
| Name | Type | Description | |
|---|---|---|---|
| dimensions |
OutputDimensions
|
||
| bitRatio |
Number
|
TextureDimensions
| Name | Type | Description | |
|---|---|---|---|
| dimensions | |||
| bitRatio |
TextureDimensions
| Name | Type | Description | |
|---|---|---|---|
| x |
Array
String
Texture
Input
|
|
|
| pad |
Boolean
|
|
Optional |
OutputDimensions
Puts a nested 2d array into a one-dimensional target array
| Name | Type | Description | |
|---|---|---|---|
| array |
Array
|
||
| target |
Float32Array
Float64Array
|
Void
Puts a nested 3d array into a one-dimensional target array
| Name | Type | Description | |
|---|---|---|---|
| array |
Array
|
||
| target |
Float32Array
Float64Array
|
Void
Puts a nested 4d array into a one-dimensional target array
| Name | Type | Description | |
|---|---|---|---|
| array |
Array
|
||
| target |
Float32Array
Float64Array
|
Void
Puts a nested 1d, 2d, or 3d array into a one-dimensional target array
| Name | Type | Description | |
|---|---|---|---|
| array |
Float32Array
Uint16Array
Uint8Array
|
||
| target |
Float32Array
|
Void
| Name | Type | Description | |
|---|---|---|---|
| array |
Array.<Number>
|
|
|
| part |
Number
|
|
Array.<Number>
An array of smaller chunks
| Name | Type | Description | |
|---|---|---|---|
| lines |
Array
|
|
String
Single combined String, separated by \n
| Name | Type | Description | |
|---|---|---|---|
| source |
String
|
||
| settings |
Object
|
String
A visual debug utility
| Name | Type | Description | |
|---|---|---|---|
| gpu |
GPU
|
||
| rgba | |||
| width | |||
| height |
Array.<Object>
| Name | Type | Description | |
|---|---|---|---|
| source |
string
object
|
||
| settings |
IFunctionSettings
|
Optional |
Void
| Name | Type | Description | |
|---|---|---|---|
| name |
String
|
boolean
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
String
the function namespace call, unrolled
| Name | Type | Description | |
|---|---|---|---|
| inParser |
Object
|
|
Optional |
Object
The function AST Object, note that result is cached under this.ast;
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
String
Type of the parameter
Generally used to lookup the value type returned from a member expressions
| Name | Type | Description | |
|---|---|---|---|
| type |
String
|
String
Recursively looks up type for ast expression until it's found
| Name | Type | Description | |
|---|---|---|---|
| ast |
String
| Name | Type | Description | |
|---|---|---|---|
| ast | |||
| dependencies | |||
| isNotSafe |
Array
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed string array
| Name | Type | Description | |
|---|---|---|---|
| error |
string
|
|
|
| ast |
Object
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
||
| retArr |
Array.<String>
|
Array.<String>
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| esNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| eNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| brNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| crNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| iVarDecNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| uNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| uNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| logNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
IFunctionNodeMemberExpressionDetails
| Name | Type | Description | |
|---|---|---|---|
| kernel |
Kernel
|
||
| FunctionNode |
FunctionNode
|
||
| extraNodeOptions |
object
|
Optional |
FunctionBuilder
| Name | Type | Description | |
|---|---|---|---|
| settings |
IFunctionBuilderSettings
|
Optional |
Void
| Name | Type | Description | |
|---|---|---|---|
| functionNode |
FunctionNode
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| functionName |
String
|
|
|
| retList |
Array.<String>
|
|
Optional |
Array.<String>
Returning list of function names that is traced. Including itself.
https://github.com/gpujs/gpu.js/issues/207 if dependent function is already in the list, because a function depends on it, and because it has already been traced, we know that we must move the dependent function to the end of the the retList.
Void
https://github.com/gpujs/gpu.js/issues/207 if dependent function is already in the list, because a function depends on it, and because it has already been traced, we know that we must move the dependent function to the end of the the retList.
Void
| Name | Type | Description | |
|---|---|---|---|
| functionName |
String
|
|
String
The full string, of all the various functions. Trace optimized if functionName given
| Name | Type | Description | |
|---|---|---|---|
| functionName |
String
|
|
Optional |
Array
The full string, of all the various functions. Trace optimized if functionName given
| Name | Type | Description | |
|---|---|---|---|
| functionList |
Array.<String>
|
|
String
The string, of all the various functions. Trace optimized if functionName given
| Name | Type | Description | |
|---|---|---|---|
| functionList |
Array.<String>
|
|
Array
Prototypes of all functions converted
| Name | Type | Description | |
|---|---|---|---|
| functionName |
String
|
|
String
settings - The string, of all the various functions. Trace optimized if functionName given
| Name | Type | Description | |
|---|---|---|---|
| functionName |
String
|
FunctionNode
| Name | Type | Description | |
|---|---|---|---|
| functionName |
string
|
||
| argumentName |
string
|
number
| Name | Type | Description | |
|---|---|---|---|
| functionName |
string
|
||
| argumentName |
string
|
||
| calleeFunctionName |
string
|
||
| argumentIndex |
number
|
number
Recursively scans AST for declarations and functions, and add them to their respective context
| Name | Type | Description | |
|---|---|---|---|
| ast |
Void
| Name | Type | Description | |
|---|---|---|---|
| value |
KernelVariable
|
||
| settings |
IKernelValueSettings
|
Void
| Name | Type | Description | |
|---|---|---|---|
| source |
string
IKernelJSON
|
||
| settings |
Void
Maximum loops when using argument values to prevent infinity
Void
Make GPU use single precision or unsigned. Acceptable values: 'single' or 'unsigned'
Void
Seed for Math.random() so kernel runs are reproducible; null seeds from Math.random()
Void
| Name | Type | Description | |
|---|---|---|---|
| settings |
IDirectKernelSettings
IJSONSettings
|
Void
Float32Array
Array.<Float32Array>
Array.<Array.<Float32Array>>
Result The final output of the program, as float, and as Textures for reuse.
| Name | Type | Description | |
|---|---|---|---|
| settings |
IDirectKernelSettings
|
{string[]};
| Name | Type | Description | |
|---|---|---|---|
| source |
KernelFunction
string
IGPUFunction
|
||
| settings |
IFunctionSettings
|
Optional |
Kernel
| Name | Type | Description | |
|---|---|---|---|
| name |
string
|
||
| source |
string
|
||
| settings |
IGPUFunctionSettings
|
Optional |
Void
| Name | Type | Description | |
|---|---|---|---|
| args |
IArguments
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| output |
Array
Object
|
Array.<number>
| Name | Type | Description | |
|---|---|---|---|
| output |
Array
Object
|
|
this
| Name | Type | Description | |
|---|---|---|---|
| flag |
Boolean
|
|
this
| Name | Type | Description | |
|---|---|---|---|
| max |
number
|
|
this
| Name | Type | Description | |
|---|---|---|---|
| constantTypes |
IKernelValueTypes
|
this
| Name | Type | Description | |
|---|---|---|---|
| functions |
Array.<IFunction>
Array.<KernelFunction>
|
this
| Name | Type | Description | |
|---|---|---|---|
| nativeFunctions |
Array.<IGPUNativeFunction>
|
this
| Name | Type | Description | |
|---|---|---|---|
| injectedNative |
String
|
this
Set writing to texture on/off
| Name | Type | Description | |
|---|---|---|---|
| flag |
this
Set precision to 'unsigned' or 'single'
| Name | Type | Description | |
|---|---|---|---|
| flag |
String
|
'unsigned' or 'single' |
this
Set a seed for Math.random(), so kernel runs are reproducible
| Name | Type | Description | |
|---|---|---|---|
| seed |
Number
|
this
| Name | Type | Description | |
|---|---|---|---|
| context |
WebGLRenderingContext
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| argumentTypes |
IKernelValueTypes
Array.<GPUVariableType>
|
this
| Name | Type | Description | |
|---|---|---|---|
| subKernel |
ISubKernel
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| removeCanvasReferences |
Boolean
|
remove any associated canvas references |
Optional |
Void
bit storage ratio of source to target 'buffer', i.e. if 8bit array -> 32bit tex = 4
| Name | Type | Description | |
|---|---|---|---|
| value |
number
| Name | Type | Description | |
|---|---|---|---|
| flip |
Boolean
|
Optional |
Uint8ClampedArray
| Name | Type | Description | |
|---|---|---|---|
| kernel |
Kernel
|
||
| args |
IArguments
|
GPUVariableType[]
| Name | Type | Description | |
|---|---|---|---|
| kernel |
Kernel
|
||
| argumentTypes |
Array.<GPUVariableType>
|
Void
| Name | Type | Description | |
|---|---|---|---|
| source |
String
Function
|
||
| settings |
IFunctionSettings
|
Optional |
IGPUFunction
mulberry32, a fast counter-based PRNG with good distribution
| Name | Type | Description | |
|---|---|---|---|
| seed |
Number
|
Function
| Name | Type | Description | |
|---|---|---|---|
| gl |
WebGLRenderingContext
|
||
| options |
IGLWiretapOptions
|
Optional |
GLWiretapProxy
| Name | Type | Description | |
|---|---|---|---|
| extension | |||
| options |
IGLExtensionWiretapOptions
|
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| idtNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| forNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed webgl string
| Name | Type | Description | |
|---|---|---|---|
| whileNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed javascript string
| Name | Type | Description | |
|---|---|---|---|
| doWhileNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed webgl string
| Name | Type | Description | |
|---|---|---|---|
| assNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| bNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| varDecNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ifNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| tNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| mNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| arrNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| Kernel |
GLKernel
|
||
| args |
Array.<KernelVariable>
|
||
| originKernel |
Kernel
|
||
| setupContextString |
string
|
Optional | |
| destroyContextString |
string
|
Optional |
string
| Name | Type | Description | |
|---|---|---|---|
| argument |
KernelVariable
|
||
| kernelValues |
Array.<KernelValue>
|
||
| values |
Array.<KernelVariable>
|
||
| context | |||
| uploadedValues |
Array.<KernelVariable>
|
string
| Name | Type | Description | |
|---|---|---|---|
| fix |
Boolean
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| flag |
String
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| flag |
Boolean
|
|
Void
A highly readable very forgiving micro-parser for a glsl function that gets argument types
| Name | Type | Description | |
|---|---|---|---|
| source |
String
|
[object Object]
Picks a render strategy for the now finally parsed kernel
| Name | Type | Description | |
|---|---|---|---|
| args |
KernelOutput
| Name | Type | Description | |
|---|---|---|---|
| flip |
Boolean
|
Optional |
Uint8ClampedArray
| Name | Type | Description | |
|---|---|---|---|
| kernelValue |
WebGLKernelValue
|
||
| arg |
GLTexture
|
Void
| Name | Type | Description | |
|---|---|---|---|
| idtNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
|
|
String
result
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
||
| retArr |
Array
|
Array.<String>
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
||
| retArr |
Array
|
Array.<String>
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
||
| retArr |
Array
|
Array.<String>
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
||
| retArr |
Array
|
Array.<String>
| Name | Type | Description | |
|---|---|---|---|
| idtNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| forNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed webgl string
| Name | Type | Description | |
|---|---|---|---|
| whileNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed webgl string
| Name | Type | Description | |
|---|---|---|---|
| doWhileNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the parsed webgl string
| Name | Type | Description | |
|---|---|---|---|
| assNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| bNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| varDecNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ifNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| tNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| mNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| ast |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| arrNode |
Object
|
|
|
| retArr |
Array
|
|
Array
the append retArr
| Name | Type | Description | |
|---|---|---|---|
| textureCache |
Array.<WebGLTexture>
|
|
|
| programUniformLocationCache |
Object.<string, WebGLUniformLocation>
|
|
|
| framebuffer |
WebGLFramebuffer
|
|
|
| buffer |
WebGLBuffer
|
|
|
| program |
WebGLProgram
|
|
|
| functionBuilder |
FunctionBuilder
|
|
|
| pipeline |
Boolean
|
|
|
| endianness |
string
|
|
|
| argumentTypes |
Array.<string>
|
|
|
| compiledFragmentShader |
string
|
|
|
| compiledVertexShader |
string
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| type | |||
| dynamic | |||
| precision | |||
| value |
KernelValue
| Name | Type | Description | |
|---|---|---|---|
| source |
String
IKernelJSON
|
||
| settings |
IDirectKernelSettings
|
Void
| Name | Type | Description | |
|---|---|---|---|
| settings |
IDirectKernelSettings
|
Array.<string>
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
|
|
Object
An object containing the Shader Artifacts(CONSTANTS, HEADER, KERNEL, etc.)
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
|
|
Object
An object containing the Shader Artifacts(CONSTANTS, HEADER, KERNEL, etc.)
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
|
|
String
result
| Name | Type | Description | |
|---|---|---|---|
| src |
String
|
|
|
| map |
Object
|
|
Void
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
|
|
string
Fragment Shader string
| Name | Type | Description | |
|---|---|---|---|
| args |
Array
IArguments
|
|
string
Vertical Shader string
| Name | Type | Description | |
|---|---|---|---|
| width |
number
|
||
| height |
number
|
Void
bit storage ratio of source to target 'buffer', i.e. if 8bit array -> 32bit tex = 4
| Name | Type | Description | |
|---|---|---|---|
| value |
number
| Name | Type | Description | |
|---|---|---|---|
| value |
KernelVariable
|
||
| settings |
IWebGLKernelValueSettings
|
Void
Used for when we want a string output of our kernel, so we can still input values to the kernel
Void
| Name | Type | Description | |
|---|---|---|---|
| inputTexture |
GLTextureMemoryOptimized
|
Void