Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should layout quantifier (location) differ between uniform/in/out?

I am now setting up layout quantifier (location) for my GLSL shaders. And this question hits me whether those quantifier ID need differ with each other.

Does it have to be:

layout (location = 0) uniform vec3 v1;
layout (location = 1) in vec3 v2;
layout (location = 2) uniform vec3 v3;
layout (location = 3) in vec3 v4;

Or it can be (as the location can be specified as uniforms or inputs):

layout (location = 0) uniform vec3 v1;
layout (location = 0) in vec3 v2;
layout (location = 1) uniform vec3 v3;
layout (location = 1) in vec3 v4;

Thanks.

like image 638
Haotian Liu Avatar asked Jul 23 '17 05:07

Haotian Liu


People also ask

Which of the following variable qualifiers specify a variable that Cannot be changed after initialization?

Constant qualifier This means that the variable's value cannot be changed after it is initialized.

What is GLSL uniform?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.

What is Std140?

Std140 is a standardized memory layout for GLSL shader interface blocks (e.g. uniform blocks). An interface block is a group op typed GLSL variables. For details on the layout rules for std140, please refer to the section 2.12. 6.4 “Standard Uniform Block Layout” of the OpenGL ES 3.0 Specification.

What is GLSL layout?

A number of OpenGL Shading Language variables and definitions can have layout qualifiers associated with them. Layout qualifiers affect where the storage for a variable comes from, as well as other user-facing properties of a particular definition.


1 Answers

While for vertex shader attributes the layout location is the attribute index, the layout location for uniform variables is the uniform location. These are different things.

If you do not set explicit layout locations and read the locations after linking the shader program, you can see that they can be both in the same range. This can be done by glGetAttribLocation and glGetUniformLocation

Both of your variants are correct and possible. Attribute locations must be unique and uniform locations must be unique. But they don't have to be unambiguous, beyond the location index type.

For more detailed information on layout qualifier, I recommend the OGL and GLSL documentation of the Khronos Group: Layout Qualifier (GLSL)

Respectively see OpenGL 4.6 API Core Profile Specification - 7.3.1 Program Interfaces.

Each entry in the active resource list for an interface is assigned a unique unsigned integer index in the range zero to N − 1, where N is the number of entries in the active resource list.

While the interface type for uniform variables is UNIFORM, the type for attributes is PROGRAM_INPUT. The location of the different program resources can be get with the instruction glGetProgramResourceLocation by its program interface type and name.

like image 141
Rabbid76 Avatar answered Oct 07 '22 21:10

Rabbid76