c++ - glGetUniformLocation 为第一个以外的采样器返回 -1?

标签 c++ graphics opengl-es glsl opengl-es-2.0

我一直在用 OpenGL 开发一些应用程序。 在第一遍中,我将一些值写入我的 FBO,它附加了 3 种颜色纹理。在第二遍中,我将这 3 个纹理附加为着色器中的采样器,并对颜色进行一些计算。以下是第二遍的着色器代码。

const char* final_fragment_source[] = {
    "uniform sampler2D rt1;\n"
    "uniform sampler2D rt2;\n"
    "uniform sampler2D rt3;\n"
    "out vec4 color;\n"
    "void main(){\n"
    "vec3 bg = vec3(0.0, 0.0, 0.0);\n"
    "vec4 RGB1 = texture2D(rt1,vec2(gl_FragCoord.xy));\n"
    "vec4 RGB2 = texture2D(rt2,vec2(gl_FragCoord.xy));\n"
    "vec4 RGB3 = texture2D(rt3,vec2(gl_FragCoord.xy));\n"
    "vec3 tempcolor = RGB1.rgb -  bg * RGB1.a + bg * RGB3.a + bg * RGB2.rgb * RGB3.a + RGB3.rgb * RGB2.a * 0.0f + bg;\n"
    "color = vec4(tempcolor,0.25);\n"
    "} \n"
};

问题是当我为 rt2 和 rt3 调用 glGetUniformLocation() 时,我得到 -1。我得到了 rt1 的正确位置。

这个我试过了

-- 我知道如果您不使用您在片段着色器中声明的任何统一变量,那么驱动程序可能会优化并为该变量返回 -1。很明显,我在计算最终颜色时使用了所有变量。

--此着色器代码中没有编译时或链接错误。

以下是我出错的代码

glUseProgram(fp_render_prog);
err = glGetError();

rt1 = glGetUniformLocation(fp_render_prog, "rt1");
err = glGetError();

rt2 = glGetUniformLocation(fp_render_prog, "rt2");
err = glGetError();

rt3 = glGetUniformLocation(fp_render_prog, "rt3");
err = glGetError();

MVPLocation = glGetUniformLocation(render_prog, "MVP");
err = glGetError();``

--我已经尝试使用 glGetError() 并且没有收到任何错误。

提前感谢您的帮助。

最佳答案

glGetUniformLocation(fp_render_prog, "rt2")glGetUniformLocation(fp_render_prog, "rt3") 返回 -1,因为 rt2rt3 未激活。

请注意,glGetUniformLocation 的第二个参数必须是事件统一变量的名称:

rt2rt3在设置RGB2RGB3时使用:

vec4 RGB2 = texture2D(rt2,vec2(gl_FragCoord.xy));
vec4 RGB3 = texture2D(rt3,vec2(gl_FragCoord.xy));

RGB2RGB3 乘以 0.0:

vec3 bg = vec3(0.0, 0.0, 0.0);
vec3 tempcolor = 
    RGB1.rgb - 
    bg * RGB1.a +
    bg * RGB3.a +
    bg * RGB2.rgb * RGB3.a +
    RGB3.rgb * RGB2.a * 0.0f +
    bg;

编译器可能会优化:

vec3 tempcolor = 
    RGB1.rgb - 
    vec3(0.0) * RGB1.a +
    vec3(0.0) * RGB3.a +
    vec3(0.0) * RGB2.rgb * RGB3.a +
    RGB3.rgb * RGB2.a * 0.0f +
    vec3(0.0);

在 final 中与:

vec3 tempcolor = RGB1.rgb;

这导致 rt2rt3 没有在可执行代码中使用,统一变量 rt2rt3 不活跃


请参阅 [OpenGL ES 2 规范 - 2.10.4 着色器变量 - p. 35] ( https://www.khronos.org/registry/OpenGL/specs/es/2.0/es_full_spec_2.0.pdf ):

A uniform is considered active if it is determined by the compiler and linker that the uniform will actually be accessed when the executable code is executed. In cases where the compiler and linker cannot make a conclusive determination, the uniform will be considered active.

.....

To find the location of an active uniform variable within a program object, use the command

int GetUniformLocation( uint program, const char *name );


参见 OpenGL ES 2.0 Online Manual Pages. - glGetActiveUniform :

A uniform variable (either built-in or user-defined) is considered active if it is determined during the link operation that it may be accessed during program execution.

关于c++ - glGetUniformLocation 为第一个以外的采样器返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871575/

相关文章:

graphics - OpenCV 中多边形的相交点

graphics - 如何将由 4 个点标识的任意平面投影到二维平面上?

Android 和 glOrthof

android - 将宽度和高度增加 2 倍后宽度缩小 openGL C++

c++ - SFML - 将对象移向坐标

c# - 使用Kinect [Algo]检测跳跃手势

c++ - 在 C++ 中,是否可以调和基于堆栈的内存管理和多态性?

c++ - 将图像数据复制到 cv::mat 的 vector

graphics - 为类似 Photoshop 的图像编辑器高效地合成/渲染多个图层

opengl-es - glGetShaderInfoLog 返回空字符串,但着色器程序未正确链接