Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send const data to shaders?

I want to send const int variable from CPU side to shader so I could initialize the array in the shader conveniently.

But if sending with usual glUniform1ui(programm, N) shader compiler says that N must be const.

#version 450 core
uniform const int N;
int myArray[N];

void main() {
//...
}

Is this possible ? If yes what are the workarounds ?

p.s. I know that this is not related to the definition of uniform variables which clarifies that uniforms are immutable per shaders executing

like image 363
ampawd Avatar asked Dec 09 '25 05:12

ampawd


1 Answers

A constant is as the name says constant and cannot be changed from outside via one of the glUniform methods and is NOT a uniform.

If you want to change the constant value, you've to recompile the hole shader and changing the shaders text before.

Sample ( Pseudocode )

GLchar** shaderSources = new GLChar*[3];
shaderSources[0] = "#version 450 core\n";
shaderSources[1] = "#define ARRAY_LENGTH 5\n";
shaderSources[2] = myShaderCode;
glShaderSource(shader, 2, shaderSources, LENGTH_OF_EACH(shaderSources, 2));

Shader:

int myArray[ARRAY_LENGTH];
void main()
{
    for (int i = 0; i < ARRAY_LENGTH; i++) myArray[i] ....;
}

A different approach is using textures ( or SSBO ) instead of arrays and get values of the texture without interpolation between the values.

like image 66
Felix K. Avatar answered Dec 10 '25 20:12

Felix K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!