Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing large array into uniform in WebGL

Tags:

arrays

webgl

Wondering if it's possible to pass a large array into a WebGL shader, like this:

// array here
uniform vec4[huge] mydynamicarray;

void main() {
  // iterate through the array here to perform processing on it,
  // then write value to gl_Position
  gl_Position = ...;
}

Then it would be populated like this:

gl.uniform4fv(myarrayloc, myarray)

I have seen many examples of how to pass in values like this, such as:

gl.uniform4fv(offsetLoc, [1, 0, 0, 0])

But I have not seen if it's possible to pass in a very large, dynamically sized array.

The reason to do this would be you could process 2 arrays:

  1. One that is the vector array running in parallel in WebGL.
  2. One that is the uniform array that you can iterate through for each vector.
like image 834
Lance Avatar asked Apr 25 '18 23:04

Lance


1 Answers

Most WebGL implementations have a limit of 1024 or less uniform vectors

In other words huge can't be greater than 1024 vec4s or whatever the limit of your particular GPU is. Also note that based on uniform packing rules specified in the spec that also means the largest float uniform array is also 1024 or whatever the limit of your particular GPU is.

You can declare arrays

uniform vec4 foo[3];

And set their values with

gl.uniform4fv(fooLoc, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);

Textures are how you pass in large amounts of random access data into WebGL. This answer might be relevant.

like image 102
gman Avatar answered Sep 24 '22 21:09

gman