无纹理绑定的 OpenGL 采样



在 GLSL 中像这样采样纹理时:

vec4 color = texture(mySampler, myCoords);
如果没有绑定到

mySampler 的纹理,则颜色似乎始终为 (0, 0, 0, 1)。

这是标准行为吗?或者在某些实现上可能未定义?我在规格中找不到任何东西。

实际上没有"未绑定"这样的东西 - 你只是绑定到初始纹理对象,称为纹理 0。 根据OpenGL维基(http://www.opengl.org/wiki/OpenGL_Objects),

You are strongly encouraged to think of texture 0 as a non-existent texture.

最终,您将在着色器中获得的值取决于当时纹理对象 0 中碰巧出现的情况。 除了一个论坛帖子之外,我真的找不到任何东西,该帖子实际上说明了这应该是什么:

The texture 0 represents an actual texture object, the default texture.
All textures start out as 1x1 white textures.

我当然不会相信在所有 GPU 驱动程序中保持一致,您的驱动程序可能会将其初始化为全部零,或者它可能被认为是不完整的纹理,在这种情况下,OpenGL 规范(至少 2.0 及更高版本)说:

If a fragment shader uses a sampler whose associated texture object is not
complete, the texture image unit will return (R, G, B, A) = (0, 0, 0, 1).

。因此,最明智的做法似乎是"不要那样做"。 如果要运行采样器,请确保绑定了有效的(非零 id)纹理。

内森是正确的(但我太新/声誉太低而无法投票)。

我已经/肯定/看到在某些OpenGL ES平台上,使用未绑定的纹理会在屏幕上渲染垃圾(图形内存的随机内容)。 不能信任所有驱动程序供应商都遵循规范。

永远不要引用未绑定的 OpenGL 对象或状态。

最新更新