This has been asked before for samplers, so I thought I’d toss in my two cents.
Consider the following:
uniform samplerBuffer data;
layout(std140) uniform transform
{
mat4 modelview;
mat4 projection;
};
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
layout(location = 2) in vec4 instance_offset;
layout(location = 0) out vec4 fs_color;
void main()
{
...
Setting sampler image unit indices and block buffer locations through the API requires knowledge of the symbols within the calling application, an implementation-defined linkage descriptor that has to match the variable names, a mapping of the declaration order to unit indices and buffer locations, or an augmented-variable-name convention (e.g. “samplerBuffer data_iu0”). These requirements obviously increase client code complexity and the overall difficulty in using GLSL.
Since shader interfaces (formerly attributes and varyings) provide a means of explicitly setting their stream indices (locations) within the shader source itself, I can’t imagine a reason for not providing this functionality for other types like samplers and uniform blocks.
To accomplish this, I’m implementing a preprocessor that parses (and cleans) layout qualifiers with an extended syntax, records the identifier names associated with the declarations, and sets their values when the program is linked.
The extended syntax would be used as follows:
layout(location = 0) uniform samplerBuffer data;
layout(location = 0, std140) uniform transform
{
mat4 modelview;
mat4 projection;
};
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
layout(location = 2) in vec4 instance_offset;
layout(location = 0) out vec4 fs_color;
void main()
{
...
The sampler declaration could be made more intuitive:
layout(image_unit = 0) uniform samplerBuffer data;
Or, since samplers are required to be declared uniform anyway, the following won’t conflict with the layout qualifier’s action on uniforms (though it really shouldn’t in the previous case):
layout(image_unit = 0) samplerBuffer data;
These extensions, as far as I can tell, would not interfere with the language as it is (aside from dropping “uniform” from sampler declarations), and changing the sampler image unit bindings or block location bindings with the API would override the declaration (as it does already with attributes and fragment outputs).
Since the GLSL compiler links uniform blocks by name, the “location” layout qualifier id must be present on all declarations of the block in all shaders linked, and must have the same value. The same rule applies to samplers.
Personally, I think it should be possible to eliminate the required use of string literals by the client just to set up a shader for what could otherwise be a fixed shader interface (e.g. color textures bound to unit 1, so shaders are designed accordingly).