Explicit uniform location has no effect - opengl

In a GLSL vertex shader I have:
#version 330
layout (location=0) uniform mat4 wm_;
layout (location=1) uniform mat4 vm_;
...more code...
I get no compile errors when the shader is compiled. Later, I am assert()ing on glGetUniformLocation for wm_ and vm_ returning the locations specified in the shader source. The asserions fire, as glGetUniformLocation returns different values (it do not return -1, that is, the uniforms don't get optimized away, they get assigned what appear to be valid but different locations.)
Can anyone explain this behavior?

Explicit uniform locations are supported since GLSL 4.30 or by extension GL_ARB_explicit_uniform_location.
The compiler here is too tolerant and allows syntax that is not supported in GLSL 3.30
This should be considered as a driver bug.

Related

Referring to uniforms without glGetUniformLocation

in my shaders I like to use a syntax like this:
layout (location = 0) in vec3 aPos;
So that I can just use the index 0 in glVertexAttribPointer and the such, saving the effort for the glGetAttribLocation call. I'd like to do the same with uniform values, but if I do
layout (location = 2) uniform float offset;
My vertex shader fails to compile. Is there any way to achieve the same behavior and not use glGetUniformLocation?
OpenGL 4.3 or the ARB_explicit_uniform_location extension allows you to specify uniform locations within the shader using that syntax. So your shader #version needs to be 430 or you need to activate the extension in your shader to be able to use this syntax.

Correspondance between texture units and sampler uniforms in opengl

The correspondence between sampler uniforms and texture units used by glActiveTexture apparently can't be queried with opengl, and I can't find good documentation on how to find which texture unit is mapped to which sampler uniform. Here is what I have been able to find:
If there is only sampler uniform in a program, then it is mapped to gl_TEXTURE0
If there are multiple sampler uniforms in a single program stage, then they are mapped in the order they are declared in the shader.
If the vertex and fragment shaders have disjoint sets of sampler uniforms, then the samplers in the vertex shader come first and are followed by the samplers in the fragment shader.
This behavior appears to be defined by the specification.
So for example if the vertex shader defines:
uniform sampler2D color;
And the fragment shader defines:
uniform sampler2D tex;
uniform sampler2D norm;
Then color gets mapped to gl_TEXTURE0, tex gets mapped to gl_TEXTURE1, and norm gets mapped to gl_TEXTURE2. But if instead the vertex shader defines:
uniform sampler2D norm;
Then it is not clear how the different textures get mapped. This is additionally complicated by the possibility of having layout qualifiers or separate shader stages.
I can't seem to find documentation on this anywhere. Everything I know about it either comes from my own experimentation or answers on Stackoverflow or the OpenGL forum. Does anyone know of a comprehensive set of rules for how this works in all possible cases, or a way to query the texture unit that a sampler corresponds to?
Here is what I have been able to find:
If there is only sampler uniform in a program, then it is mapped to gl_TEXTURE0
If there are multiple sampler uniforms in a single program stage, then they are mapped in the order they are declared in the shader.
If the vertex and fragment shaders have disjoint sets of sampler uniforms, then the samplers in the vertex shader come first and are followed by the samplers in the fragment shader.
This behavior appears to be defined by the specification.
None of this is true. OK, the first one is true, but only by accident.
All uniform values which are not initialized in the shader are initialized to the value 0. The spec makes this quite clear:
Any uniform sampler or image variable declared without a binding qualifier is initially bound to unit zero.
A sampler uniform's value is the integer index of the texture unit it represents. So a value of 0 corresponds to GL_TEXTURE0. All uninitialized sampler uniforms should have a value of 0.
If the behavior you describe is happening, then that implementation is in violation of the OpenGL specification.
Unless you use the layout(binding = ) syntax to assign a uniform's texture unit, you must manually in your OpenGL code assign each sampler uniform a value for its texture unit. This is done by setting its uniform value, just like any other integer uniform: you call glUniform1i with the location corresponding to that uniform. So if you want to associate it with texture image unit index 4, you call glUniform1i(..., 4), where ... is the uniform location for that uniform.
You have to set the index of the texture unit to sampler uniform (similar as setting the value of a uniform variable of type int). e.g. value 1 for GL_TEXTURE1.
See OpenGL 4.6 API Compatibility Profile Specification; 7.10 Samplers; page 154:
Samplers are special uniforms used in the OpenGL Shading Language to identify
the texture object used for each texture lookup. The value of a sampler indicates the texture image unit being accessed. Setting a sampler’s value to i selects texture image unit number i.
e.g.
layout (location = 11) uniform sampler2D color;
layout (location = 12) uniform sampler2D tex;
layout (location = 13) uniform sampler2D norm;
glUniform1i(11, 0); // 0: GL_TEXTURE0
glUniform1i(12, 1); // 1: GL_TEXTURE1
glUniform1i(13, 2); // 2: GL_TEXTURE2
Since GLSL version 4.2 this can be done in the fragment shader by specifying binding points - See OpenGL Shading Language 4.20 Specification - 4.4.4 Opaque-Uniform Layout Qualifiers; page 60:
#version 420
layout (binding = 0) uniform sampler2D color;
layout (binding = 1) uniform sampler2D tex;
layout (binding = 2) uniform sampler2D norm;

Compiling vertex shader program fails

I'm doing my first steps with OpenGL and stumbled upon a problem in my vertex shader program:
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 inputTransform;
void main()
{
gl_Position = inputTransform * vec4(aPos, 1.0);
}
compiles and works well, but when I change the first line to
#version 130 core
because I'm bound to use OpenGL 3.0 at max, it first complains about the "location" statement. When I remove it, the remaining error message for the line
layout in vec3 aPos;
is
ERROR: 0:2: 'in' : syntax error syntax error
What's wrong here - how do I have to declare input variables in this version of the language?
Input layout qualifiers have been added in GLSL version 1.50 wich corresponds to OpeneGL version 3.2
See OpenGL Shading Language 1.50 Specification; - 4.3.8.1 Input Layout Qualifiers; page 37.
An input layout qualifier consists of the keyword layout followed by the layout-qualifier-id-list.
It is not sufficient to remove the layout-qualifier-id-list alone, you have to remove the keyword layout too:
in vec3 aPos;
The syntax error is caused by the fact, that it is not a proper syntax, that layout is directly followed by in.
Either you have to ask for the attribute index by glGetAttribLocation after the program is linked, or you have to specify the attribute location by glBindAttribLocation before the shader program is linked.

Difference between GLSL shader variable types?

When seeing some OpenGL examples, some use the following types of variables when declaring them at the top of the shader:
in
out
and some use:
attribute
varying
uniform
What is the difference? Are they mutually exclusive?
attribute and varying were removed from GLSL 1.40 and above (desktop GL version 3.1) in core OpenGL. OpenGL ES 2 still uses them, but were removed from ES 3.0 code.
You can still use the old constructs in compatibility profiles, but attribute only maps to vertex shader inputs. varying maps to both VS outputs and FS inputs.
uniform has not changed; it still means what it always has: values set by the outside world which are fixed during a rendering operation.
In modern OpenGL, you have a series of shaders hooked up to a pipeline. A simple pipeline will have a vertex shader and a fragment shader.
For each shader in the pipeline, the in is the input to that stage, and the out is the output to that stage. The out from one stage will get matched with the in from the next stage.
A uniform can be used in any shader and will stay constant for the entire draw call.
If you want an analogy, think of it as a factory. The in and out are conveyor belts going in and out of machines. The uniform are knobs that you turn on a machine to change how it works.
Example
Vertex shader:
// Input from the vertex array
in vec3 VertPos;
in vec2 VertUV;
// Output to fragment shader
out vec2 TexCoord;
// Transformation matrix
uniform mat4 ModelViewProjectionMatrix;
Fragment shader:
// Input from vertex shader
in vec2 TexCoord;
// Output pixel data
out vec4 Color;
// Texture to use
uniform sampler2D Texture;
Older OpenGL
In older versions of OpenGL (2.1 / GLSL 1.20), other keywords were used instead of in and out:
attribute was used for the inputs to the vertex shader.
varying was used for the vertex shader outputs and fragment shader inputs.
Fragment shader outputs were implicitly declared, you would use gl_FragColor instead of specifying your own.

OpenGL uniform block works in vertex shader only if defined in fragment shader. Separable shader programs

I have a UBO defined in my vertex shader as follows:
layout (std140) uniform matricesBuffer
{
mat4 MVPMatrix;
mat4 modelMatrix;
};
I spent ages figuring out why the matrix I needed wasn't getting through. Then I added the same uniform block to the fragment shader, and it magically worked. So my question is, I know there are specifications about interface matching between shader programs/stages when using separable shaders in a pipeline, but from what I've read these only describe the in and out variables, but this uniform block, or UBO it is, should be accessible from just the vertex shader, as I linked the UBO to that binding point. I'm confused about this, any info would be appreciated.
Edit: I've been trying some things, putting just:
layout (std140) uniform anyName
{
mat4 worldMatrix;
};
in the fragment shader makes it work. And if I change "anyName" to certain names it either works or doesn't. For example if it starts with the letter R it doesn't work, then most other letters it works. So confused.