opengl中纹理单元和采样器制服之间的对应关系 [英] Correspondance between texture units and sampler uniforms in opengl
问题描述
glActiveTexture
使用的采样器制服与纹理单元之间的对应关系显然无法通过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:
- 如果程序中只有统一的采样器,则将其映射到
gl_TEXTURE0
- 如果在一个程序阶段中有多个采样器制服,则将按照在着色器中声明的顺序对其进行映射.
- 如果顶点着色器和片段着色器的采样器制服集不相交,则顶点着色器中的采样器排在最前面,然后是片段着色器中的采样器.
- 此行为似乎是由规范定义的.
- 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;
片段着色器定义:
uniform sampler2D tex;
uniform sampler2D norm;
然后,color
映射到gl_TEXTURE0
,tex
映射到gl_TEXTURE1
,并且norm
映射到gl_TEXTURE2
.但是,如果顶点着色器定义为:
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.
我似乎在任何地方都找不到此文档.我所知道的一切要么来自我自己的实验,要么来自Stackoverflow或OpenGL论坛的答案.是否有人知道在所有可能的情况下如何工作的全面规则,还是一种查询采样器对应的纹理单位的方法?
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:
- 如果程序中只有统一的采样器,则将其映射到
gl_TEXTURE0
- 如果在一个程序阶段中有多个采样器制服,则将按照在着色器中声明的顺序对其进行映射.
- 如果顶点着色器和片段着色器的采样器制服集不相交,则顶点着色器中的采样器排在最前面,然后是片段着色器中的采样器.
- 此行为似乎是由规范定义的.
- 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.
所有未在着色器中初始化的统一值都将初始化为值0.规范使这一点很清楚:
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.
采样器统一的值是它表示的纹理单位的整数索引.因此,值0对应GL_TEXTURE0
.所有未初始化的采样器制服的值都应为0.
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.
如果您描述的行为正在发生,则该实现将违反OpenGL规范.
If the behavior you describe is happening, then that implementation is in violation of the OpenGL specification.
除非使用layout(binding = )
语法指定制服的纹理单位,否则必须手动在OpenGL代码中为每个采样器的制服为其纹理单位分配一个值.可以通过设置其统一值来完成此操作,就像任何其他整数统一一样:您调用glUniform1i
并指定与该统一对应的位置.因此,如果要将其与纹理图像单元索引4关联,请调用glUniform1i(..., 4)
,其中...
是该制服的制服位置.
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.
这篇关于opengl中纹理单元和采样器制服之间的对应关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!