Tuesday, April 18, 2017

opengl - Textures "don't work" when I don't specify any texture parameters. Is this a driver bug or intended behavior?


Whenever I try to use textures, I have to at least specify the sample filtering parameters (GL_TEXTURE_MAG_FILTER, GL_TEXTURE_MIN_FILTER) for textures to work at all. If I don't, sampling the textures in a shader will usually just return a value of 0 for all texels in the texture, even though glReadPixels return the correct values, and rendering to the textures actually works. Debugging tools like Nsight and gDebugger confirm this.


I tested this on two NVIDIA GPUs (GTX 970, GT 555m) with the latest driver versions (347.09), and the behavior is the same.


However, I can't find any reference to this in docs like this or this



I assumed that not specifying these parameters just made them take on their default values. So is this actually intended standard behavior (probably meaning "undefined behavior") or can I assume this is a driver bug?



Answer



By default, OpenGL assumes that your textures will have mipmaps attached. If you haven't attached any mipmaps to a texture, then OpenGL assumes that you didn't finish setting it up. OpenGL will therefore deem the texture to be "incomplete" and will refuse to render from it.


In order to make your texture "complete" in OpenGL's eyes (and therefore usable for rendering), you need to do one of the following:



  • Set GL_TEXTURE_MIN_FILTER to something which won't try to use mipmap data. (GL_NEAREST or GL_LINEAR)

  • or set GL_TEXTURE_MAX_LEVEL to 0, thus explicitly declaring that your texture has no mipmaps, so that OpenGL won't be bothered by their nonexistence.

  • or create mipmaps for your texture.

  • or tell OpenGL to automatically create the mipmaps for you, using glGenerateMipmap() or the GL_GENERATE_MIPMAP texture parameter.



You can read more discussion of this issue in opengl.org's Common Mistakes document. This issue is number six in their list; "Creating a complete texture".


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...