gpujs / gpu.js

GPU Accelerated JavaScript
https://gpu.rocks
MIT License
15.08k stars 650 forks source link

How to create Float32Array inside GPU Kernel #703

Open zlelik opened 3 years ago

zlelik commented 3 years ago

What is wrong?

I am trying to create an array inside GPU kernel for internal calculation like this

var testArray1 = new Float32Array(16);
var testArray2 = new Array(16);

and I am getting this error.

gpu-browser.min.js:14 Uncaught Error: unhandled type "NewExpression"
    at FunctionTracer.scan (gpu-browser.min.js:14)
    at FunctionTracer.scan (gpu-browser.min.js:14)
    at FunctionTracer.scan (gpu-browser.min.js:14)
    at gpu-browser.min.js:14
    at FunctionTracer.useFunctionContext (gpu-browser.min.js:14)
    at FunctionTracer.scan (gpu-browser.min.js:14)
    at FunctionTracer.scan (gpu-browser.min.js:14)
    at gpu-browser.min.js:14
    at FunctionTracer.newContext (gpu-browser.min.js:14)
    at FunctionTracer.scan (gpu-browser.min.js:14)

Where does it happen?

Inside GPU kerneI code.

How do we replicate the issue?

add this code below to the GPU kernel.

var testArray1 = new Float32Array(16);

How important is this (1-5)?

4

Expected behavior (i.e. solution)

Array should be created without any errors.

Other Comments

I can create this array outside GPU Kernel and pass it to GPU kernel, but because it is for internal calculation I would prefer to create it inside.

benrayfield commented 3 years ago

GPU doesnt use javascript vars, Float32Arrays, etc. Those are whats copied into GPU, then it does alot of work, then copied from GPU to CPU as those. It probably would have no effect on GPU efficiency to do that, other than floats are much faster than doubles in GPU.