c++ - 为什么 glGetSubroutineIndex 为不同的函数返回相同的值?

标签 c++ opengl glsl subroutine

我有一个片段着色器应该操作 2 种颜色 - 红色和蓝色。

#version 460 core

layout (location = 0) out vec4 outColor;

subroutine vec4 colorRedBlue();

subroutine (colorRedBlue) 
vec4 redColor() 
{
    return vec4(1.0, 0.0, 0.0, 1.0);
} 

subroutine (colorRedBlue) 
vec4 blueColor() 
{ 
    return vec4(0.0, 0.0, 1.0, 1.0);
}

subroutine uniform colorRedBlue myRedBlueSelection;

void main()
{
    outColor = myRedBlueSelection();
}

当我计算 glGetSubroutineIndex(shaderProgram, GL_FRAGMENT_SHADER, "redColor");glGetSubroutineIndex(shaderProgram, GL_FRAGMENT_SHADER, "blueColor");

它们是相同的数字:4294967295

为什么它们是相同的数字?我做错了什么?

最佳答案

如果您想检索有关子程序索引的信息,您必须使用 glGetActiveSubroutineUniformiv :

GLint no_of_subroutines;
glGetActiveSubroutineUniformiv(
    shaderProgram, GL_FRAGMENT_SHADER, 
    0, // Index of the subroutine uniform, but NOT the location of the uniform variable 
    GL_NUM_COMPATIBLE_SUBROUTINES, &no_of_subroutines);

std::vector<GLint> sub_routines( no_of_subroutines ); 
glGetActiveSubroutineUniformiv(
    shaderProgram, GL_FRAGMENT_SHADER, 
    0, // Index of the subroutine uniform, but NOT the location of the uniform variable 
    GL_COMPATIBLE_SUBROUTINES, sub_routines.data());

通过glGetActiveSubroutineName可以得到子程序索引对应的子程序名。 .

但我建议使用布局限定符在着色器代码中指定子例程索引 - 参见 Shader Subroutine - In-shader specification :

subroutine vec4 colorRedBlue();

layout(index = 1) subroutine (colorRedBlue) 
vec4 redColor() 
{
    return vec4(1.0, 0.0, 0.0, 1.0);
} 

layout(index = 2) subroutine (colorRedBlue) 
vec4 blueColor() 
{ 
    return vec4(0.0, 0.0, 1.0, 1.0);
}  

layout(location = 0) subroutine uniform colorRedBlue myRedBlueSelection;

请注意,一个着色器阶段的所有子例程统一体的所有子例程必须由 glUniformSubroutinesuiv 立即设置。 .在你的情况下,这不是什么大事,因为只有 1 个子程序制服。此操作适用于当前着色器程序 (glUsePrgram)。
您必须传递给 glUniformSubroutinesuiv 的索引数量必须与 GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS 的数量相匹配,但不能与 GL_ACTIVE_SUBROUTINE_UNIFORM 的数量相匹配 - 请参阅 glGetProgramStageiv :

GLint nLocationCount = 0;
glGetProgramStageiv( 
    program, GL_FRAGMENT_SHADER, GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS, &nLocationCount );

std::vector<GLuint> sub_routines( nLocationCount, 0 );

Glint redBlueSelection_location = 0;
sub_routines[redBlueSelection_location] = 1; // index of 'redColor` or `blueColor`

glUniformSubroutinesuiv(
    GL_FRAGMENT_SHADER, (GLsizei)sub_routines.size(), sub_routines.data() );

关于c++ - 为什么 glGetSubroutineIndex 为不同的函数返回相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52899233/

相关文章:

c++ - 将程序 A 的输出重定向到程序 B 的输入,反之亦然

c++ - 使用 QGLShaderProgram 时出现 GLSL 编译错误

c++ - std::list< std::unique_ptr<T>>:传递它

opengl - 如何更改一个顶点而不是所有顶点的颜色?

c++ - 为什么我的链接数据类型复制构造函数不工作?

c++ - glShaderSource() 上的段错误

c++ - 如何在使用 triangle_strip 创建的 OpenGL 中对圆柱体进行纹理处理?

c++ - Visual Studio 2015 中 OpenGL 4 实现中的纹理单元数 (GL_TEXTUREi)

opengl - 几何着色器产生线之间的间隙

Three.js/GLSL - 将像素坐标转换为世界坐标