Felix Jentsch
Felix Jentsch

Reputation: 45

Dynamic array of uniforms (GLSL OpenGL ES 2.0)

In a shader (using OpenGL ES 2.0) I want to have an array with a dynamic size.

I can declare an array with fixed size:

uniform vec2 vertexPositions[4];

But now I want to set the size dynamic to the number of points, which I will pass.

I thought about making a string replacement in the shader source before compiling it, but than I have to compile it everytime I draw a different element. That seems to be CPU-intensive.

Upvotes: 1

Views: 1986

Answers (1)

Orin Tresnjak
Orin Tresnjak

Reputation: 123

The typical approach would be to size the uniform array to the maximum number of elements you expect to use, and then only update the subset of it that you're actually using. You can then pass in the effective size of the array as a separate uniform.

uniform vec2 arr[MAX_SIZE];
uniform int arr_size;

Upvotes: 1

Related Questions