Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable size array GLSL

Tags:

java

glsl

lwjgl

I am working on creating shaders for an OpenGL/Java engine that I am building. I have searched for a while, but I cannot find a way to have an array of variable size. I know that I can create a statically sized one like:

uniform vec3 variable[4];

But how would I go about creating an array of size X based on what I load to the shader from the CPU, if this is even possible.

Thanks in advance!

like image 916
Nick Clark Avatar asked May 19 '16 23:05

Nick Clark


2 Answers

GLSL does not allow varying array sizes. It does however, as of OpenGL 4, support Shader Storage Buffer Objects which can be varying sizes.

More information on SSBOs: https://www.opengl.org/wiki/Shader_Storage_Buffer_Object

Thinking about it now, a hack way of doing it as well could be to encode your data into a texture and then pass that into the shader. For example, each of the 4 rgba components could be 1 byte of the data you wanted to pass. For data larger than a byte, you would break it out into bytes.

like image 75
CConard96 Avatar answered Oct 17 '22 02:10

CConard96


You can't.

Either do as CConard96 said, or if you can't use SSBOs then just declare an hardcoded maximum value and set a lower value that you need.

For example like Nicol Bolas says here:

#define MAX_NUM_TOTAL_LIGHTS 100
struct Light {
  vec3 position;
  float padding;
}
layout (std140) uniform Lights {
  Light light[MAX_NUM_TOTAL_LIGHTS];
  int numLights;
}
like image 37
elect Avatar answered Oct 17 '22 02:10

elect