3
votes

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.

2
possible duplicate of Binding a zero texture in OpenGLBahbar
Yes it is. Thanks for that. Answered my question!Robinson

2 Answers

2
votes

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.

1
votes

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