Reputation: 1862
When sampling a texture in GLSL like this:
vec4 color = texture(mySampler, myCoords);
If there is no texture bound to mySampler, color seems to be always (0, 0, 0, 1).
Is this the standard behavior? Or may it be undefined on some implementations? I couldn't find anything in the specs.
Upvotes: 17
Views: 4797
Reputation: 106
Nathan is correct (but I am too new/reputation too low to upvote).
I have /certainly/ seen that on some OpenGL ES platforms, using an unbound texture will render garbage on the screen (random contents of graphics memory). You can't trust all driver vendors to follow the spec.
Never, ever, reference un-bound OpenGL objects or state.
Upvotes: 5
Reputation: 5470
There's really no such thing as being "unbound" -- you're simply bound to the initial texture object which is known as texture 0. According to the OpenGL wiki (http://www.opengl.org/wiki/OpenGL_Objects),
You are strongly encouraged to think of texture 0 as a non-existent texture.
Ultimately, the value you're going to get in the shader depends on what happens to be in Texture Object 0 at the time. I can't really find anything except one forum post that actually says what this should be:
The texture 0 represents an actual texture object, the default texture.
All textures start out as 1x1 white textures.
I certainly wouldn't trust that to be consistent across all GPU drivers, yours might have it initialized to all zero, or it might be considered an incomplete texture, in which case the OpenGL spec (2.0 and later at least) says:
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).
... So really the wisest course of action seems to be "don't do that." Make sure you have a valid (non-zero id) texture bound if you're going to run a sampler.
Upvotes: 11