4

Does the OpenGL standard mandate what the result of a texture2d operation should be given a uniform sampler2D that the program hasn't bound to a texture unit?

For example in the pixel shader:

layout(binding=0) uniform sampler2D Map_Diffuse;
...
texture2D(Map_Diffuse, attrib_Fragment_Texture)

Where in the program:

::glActiveTexture(GL_TEXTURE0); 
::glBindTexture(GL_TEXTURE_2D, 0);

For context I'm wondering whether I can use the same shader for textured and non-textured entities, where (hopefully) I only need to make sure nothing is bound to GL_TEXTURE_2D for texture2d() to return 0, 0, 0, 1. Otherwise I'll need one shader for each permutation.

concept3d
  • 2,248
  • 12
  • 21
Robinson
  • 9,666
  • 16
  • 71
  • 115

2 Answers2

4

The way I read the spec, it's guaranteed to return black. The following quotes are copied from the 3.3 spec.

In section 2.11.7 "Shader Execution" under "Vertex Shaders", on page 81:

Using a sampler in a vertex or geometry shader will return (R,G,B,A) = (0,0,0,1) if the sampler’s associated texture is not complete, as defined in section 3.8.14.

and equivalent in section 3.9.2 "Shader Execution" under "Fragment Shaders", on page 188:

Using a sampler in a fragment shader will return (R,G,B,A) = (0,0,0,1) if the sampler’s associated texture is not complete, as defined in section 3.8.14.

In section 3.8.14 "Texture Completeness", it says:

A texture is said to be complete if all the image arrays and texture parameters required to utilize the texture for texture application are consistently defined.

Now, it doesn't explicitly say anything about texture objects that don't even exist. But since texture names that don't reference a texture object (which includes 0) certainly don't have "all the image arrays and texture parameters consistently defined", I would argue that they fall under "not complete" in the definitions above.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
2

Does the OpenGL standard mandate what the result of a texture2d operation should be given a uniform sampler2D that the program hasn't bound to a texture unit?

When a texture sampler is unbound to a texture object, it is by default bound to texture object 0/null, this is like null in C/C++. When accessing object null, from my experience you get zero values For example:

 vec2 Data = texture(unboundSampler, textureCoords); 

Data will often be zoroes, but my assumption is this implementation dependent, some drivers may crash.

For context I'm wondering whether I can use the same shader for textured and non-textured entities

In my engine I solved this by creating a default white texture that is 4 white pixels. Generated by code when the engine is initialized. When I want to use a shader that has a texture sampler and the corresponding material doesn't have a texture I assign the default white texture. This way I can reuse the same shader. If you care so much about performance you might want to use a shader without textures for non textured objects.

The standard doesn't talk about the implementation but it encourages you to think about the zero object as non-functional object. https://www.opengl.org/wiki/OpenGL_Object#Object_zero

concept3d
  • 2,248
  • 12
  • 21
  • That's a great idea, though often I prefer things to crash horribly if they try to do something they aren't supposed to do. I hate it when drivers and libraries are robust to bad input. They don't do the programmer any favours. Do you know what I mean? I'd still like to know if the standard says anything about it. – Robinson Feb 09 '15 at 15:16
  • @Robinson Edited the answer to clarify. – concept3d Feb 09 '15 at 17:19