在GLSL中对纹理进行采样时,如下所示:
vec4 color = texture(mySampler, myCoords);
Run Code Online (Sandbox Code Playgroud)
如果没有绑定到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.
Run Code Online (Sandbox Code Playgroud)
最终,您将在着色器中获得的值取决于当时纹理对象0中发生的情况.我真的找不到任何东西,除了一个论坛帖子实际上说这应该是什么:
The texture 0 represents an actual texture object, the default texture.
All textures start out as 1x1 white textures.
Run Code Online (Sandbox Code Playgroud)
我当然不相信在所有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).
Run Code Online (Sandbox Code Playgroud)
......所以,最明智的做法似乎是"不要这样做".如果要运行采样器,请确保有一个有效的(非零id)纹理绑定.
小智 5
内森是正确的(但我太新/声誉太低而无法投票)。
我/当然/看到在某些 OpenGL ES 平台上,使用未绑定的纹理会在屏幕上渲染垃圾(图形内存的随机内容)。您不能相信所有驱动程序供应商都遵循规范。
永远不要引用未绑定的 OpenGL 对象或状态。