Very strange problem with Texture Units

Started by
10 comments, last by CirdanValen 7 years, 1 month ago

You're over-writing your texture bindings; this is a side-effect of OpenGL's bind-to-modify behaviour and happens because in non-DSA OpenGL state used for drawing is the same as state used for creating/modifying objects.

At line 180 you have the following:

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D,5)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D,1)
glActiveTexture(GL_TEXTURE2)
glBindTexture(GL_TEXTURE_2D,3)
glActiveTexture(GL_TEXTURE3)

glBindTexture(GL_TEXTURE_2D,6)

Then at line 249 you have the following:

glBindTexture(GL_TEXTURE_2D,1)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1904,1041,0,GL_RGBA,GL_UNSIGNED_BYTE,0000000000000000)
glBindTexture(GL_TEXTURE_2D,2)
glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,1904,1041,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,0000000000000000)
glBindTexture(GL_TEXTURE_2D,0)
glBindTexture(GL_TEXTURE_2D,3)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1904,1041,0,GL_RGBA,GL_UNSIGNED_BYTE,0000000000000000)
glBindTexture(GL_TEXTURE_2D,4)
glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,1904,1041,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,0000000000000000)

glBindTexture(GL_TEXTURE_2D,0)

These calls will over-write the binding for GL_TEXTURE3 and when you come to draw texture '0' (which is actually a valid texture object in OpenGL) will be bound to it.

If you hadn't been un-binding (which IMO is a bad practice) you would have probably caught this sooner because you would have clearly seen the wrong texture being used.

To resolve, either re-bind '6' to GL_TEXTURE3 before drawing; or reserve a texture unit for updates and make that active before doing any updates; or use direct state access. I personally recommend a combination of direct state access (assuming your target market will stand it) and bind-before-use, getting rid of all unbinding.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Advertisement

Oh wow. I can't believe I missed that, tho I'm still surprised this problem occurred in this code base and not my previous one. I normally don't unbind anything, since the binds get overwritten anyway, I'll just have to be more careful about texture units.

Still, what was happening is when the window is resized...I recreate the frame buffers (which are what those glTexImage2D calls are after binding to the texture units). So after the initializing the framebuffers for the first time, they immediately get recreated again because Windows sends a resized event when the app first starts up. I'll admit, this should be changed.

I guess I didn't realize that calling glBindTexture while a different texture unit was active, would change the texture bound to that unit. It makes sense in retrospect. I'll figure out a way to prevent this from happening in the future.

Thanks!

This topic is closed to new replies.

Advertisement