How to create a buffer from an array of arrays in JavaScript and use it in a compute shader?
For example: const arr = [new Float32Array([1, 3, 5]), new Float32Array([2, 4, 6]), ...]
How to create a buffer for arr and declare it in the compute shader?
The intention is to create both an input and an output in the same format.
For WGSL you declare an array of arrays like this
For example
Is an array of 2 arrays of 3 vec4fs
Back in JavaScript, how you read/write those is up to you,
As one large array
Or arrays of typed arrays,
Or arrays of arrays of arrays
Of those, the first, just one
Float32Array
would arguably be the most common since making lots of views has a cost.As for uploading, you could either make typed arrays that view the same buffer (like above) and then upload the entire buffer. Or you can upload one smaller typed array at a time
For example
In the example above the array is declared as
Leaving off the length of the outer array means the size of the outer array is decided by the size of the buffer you bind at runtime.
You might find this article about memory layout and this wsgl offset computer helpful