PDA

View Full Version : How can I share a uniform between programs?



Kerndog73
02-10-2017, 11:42 PM
Some models need to be rendered with different shader programs but all shader programs have some common uniforms. For example, a phong fragment shader needs all of the lights in the scene but so does the oren nayer shader and the blinn shader. So currently I upload the same data (the lights) for each program. I'd like to upload the lighting data once per frame (or less) and have all the programs share that same data but I don't know how to do that.

I'm using OpenGL 4.1

Thanks

john_connor
02-11-2017, 02:08 AM
depending on how much data it is they share, you can use a uniform block (backed by an uniform buffer object)

lets say you use uniform block: index = 3 for all the data used by both programs

GLuint uniformbuffer = 0;
glGenBuffers(1, &uniformbuffer);
glBindBufferBase(GL_UNIFORM_BUFFER, 3, uniformbuffer);
glBufferData(GL_UNIFORM_BUFFER, ..., NULL, GL_STREAM_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);


now you have a "uniformbuffer" bound at uniform block: index = 3, containing nothing
to access that buffers content in your shaders:

layout (binding = 3, std140) uniform MY_SHARED_DATA {
// here your variables
};


what is confusing is that those variables declared in "MY_SHARED_DATA" are not necessary continuous layed down in the memory of "uniformbuffer"
https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)#Memory_layout

Kerndog73
02-11-2017, 03:31 PM
I've never heard of a uniform buffer. I'll have to do some googling but it looks like it will work in my situation. Thank you for your response.

OceanJeff40
02-11-2017, 03:57 PM
Save and read from a file.
Jeff