英文:
Layout specifiers for uniforms
问题
在顶点着色器中,我们可以指定两个输出 uniform 变量。
layout(location = 0) out float v_transparency;
layout(location = 1) out vec2 v_texcoord;
在片元着色器中,我们可以这样接收它们。
layout(location = 0) in float v_transparency;
layout(location = 1) in vec2 v_texcoord;
不指定布局也可以完成相同的操作,那么指定布局是否有任何优势呢?
英文:
In the vertex shader we can specify two OUT uniforms.
layout(location = 0) out float v_transparency;
layout(location = 1) out vec2 v_texcoord;
In the Fragment shader we can receive them like this.
layout(location = 0) in float v_transparency;
layout(location = 1) in vec2 v_texcoord;
The same can also be done without specifying the layout , Is there any advantage in specifying the layouts ?
答案1
得分: 1
如果将着色器对象链接成包含多个着色器阶段的程序,那么这两个程序之间的接口必须完全匹配。也就是说,在一个阶段的每个输入必须在下一个阶段中有对应的输出,反之亦然。否则,将会发生链接错误。
然而,如果您使用分开的程序,那么这并不是必需的。着色器阶段的输出可以不包含在下一个着色器阶段的输入中。没有由输出填充的输入具有未指定的值(因此,这仅在您不从中读取时才有用);未被输入接收的输出将被忽略。
但是,这个规则仅适用于使用 layout(location = #)
限定符声明的接口变量。对于通过名称匹配的变量(以及输入/输出接口块),每个输入必须对应一个输出,反之亦然。
此外,对于带有位置限定符的接口变量,类型不必完全匹配。如果输出和输入类型是相同基本类型的不同向量大小(或标量)(例如 vec2
和 vec3
,uvec4
和 uint
,但不包括 uvec3
和 ivec2
),只要输入的成分少于输出,匹配仍然有效。因此,输出可以是 vec4
,而输入可以是 vec2
;输入将只获取前两个值。
请注意,这也是分开的程序的特性,不仅仅是使用位置限定符的情况。
英文:
If you link shader objects together into programs containing multiple shader stages, then the interfaces between the two programs must exactly match. That is, for every input in one stage, there must be a corresponding output in the next, and vice-versa. Otherwise, a linker error occurs.
If you use separate programs however, this is not necessary. It is possible for a shader stage output to not be listed among the next shader stage's inputs. Inputs not filled by an output have unspecified values (and therefore this is only useful if you don't read from it); outputs not received by an input are ignored.
However, this rule only applies to interface variables declared with layout(location = #)
qualifiers. For variables that match via name (as well as input/output interface blocks), every input must go to an output and vice-versa.
Also, for interface variables with location qualifiers, the types do not have to exactly match. If the output and input types are different vector sizes (or scalar) of the same basic type (vec2
and vec3
, uvec4
and uint
, but not uvec3
and ivec2
), so long as the input has fewer components than the output, the match will still work. So the output can be a vec4
while the input is a vec2
; the input will only get the first 2 values.
Note that this is also a function of separate programs, not just the use of location qualifiers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论