The background of this question is that I am trying to write an automatic binding system, where for each uniform variable I want to declare in my GLSL code, I call a method declareUniform
which is counting up the location value, saving the type and name of the uniform to a list which will be saved within my shaderobject...
I already did something similar with varyings, where I save the varying information (location, type, name, shader stage from, shader stage to, isarray, arraycount) and of cause also generating glsl string which is working fine.
I am trying to bind several things to a shader, which does not work right now.
I've read, that uniform block binding indices have nothing to do with sampler binding locations here, which is totally confusing me... I also read the Explicit_uniform_location
site from khronos here.
I am trying to bind some sampler2D, image2D and other uniforms in one shader.
layout(location = 0) in vec2 texCoordIn; //value from VBO
layout(binding = 0, rgba32f) uniform image2D writeColorTexture;
layout(binding = 1) uniform sampler2D readColorTexture;
layout(location = 0) uniform float u_apature;
layout(location = 1) uniform float u_focalDistance;
...
On CPU it looks like this:
GLint readColorTextureLoc = glGetUniformLocation(glShaderHandle, "readColorTexture");
GLint writeColorTextureLoc = glGetUniformLocation(glShaderHandle, "writeColorTexture");
trying to bind the samper2D:
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glUniform1i(readColorTextureLoc, 0);
glBindTexture(GL_TEXTURE_2D, colorTexture->id());
trying to bind the image2D:
glBindImageTexture(writeColorTextureLoc, colorImage->id(), 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA32F);
when using glGetUniformLocation(glShaderHandle, "readColorTexture");
I get the value 31 which also confuses me.
This brought me to understand it to the following way: layout(location = ...) uniform
is the value which will be returned by glGetUniformLocation
.
The binding qualifier maps the uniform variable to a (I'll call it) "background list", which can be addressed by glActiveTexture
for example if it is a sampler. As far as I understood (please correct me if I am wrong!) there are several of these "background lists". One for samplers, one for block bindings, one for images... and each of them starts with 0.
Now my question: Am I right? Did I understand it correctly? If so: which "background lists" are available and how can I bind a sampler together with an Image?