Why is conversion from an untyped array to Uint8ClampedArray so slow?

215 Views Asked by At

I've got a rendering pipeline where I'm trying out gpu.js as my shader mechanism. From what I can tell though, while gpu.js can take a typed array buffer as input, there's no way to output to a typed array. So to render the shaded result, I need to convert this buffer (potentially 1080 x 1920 x 4 = 8,294,400 length array buffer) to a typed array.

Doing so, like this:

outputBufferRaw = pixelateMatrix(frameBuffer); // shading = ~30ms (kinda slow)
outputBuffer = new Uint8ClampedArray(outputBufferRaw); // conversion = ~100ms (very slow)

takes ~100ms, far far too slow for a real time rendering pipeline. I suspect that normal arrays are just slow to work with and I need to handle this in a different way that never outputs an untyped array anywhere in the rendering pipeline, that's fair enough, but my question is: Why? Why does is take so long to convert a normal array to a typed array? Why are normal arrays so slow to work with?

1

There are 1 best solutions below

0
On

gpu.js now outputs typed (Float32) arrays or arrays of Float32Array for multidimensional. The slowest part will be gl.readPixels, best to use textures to keep the values in GPU RAM as much as possible. Also creating a buffer this size to read back into is not insignificant.