SirRatty
SirRatty

Reputation: 2406

OpenGL - glBindTexture - premature optimisation?

I've seen sample code (most notably in iOS GL projects) where the current GL texture name is cached, and a comparison done before calling glBindTexture. The goal is to avoid unnecessary calls to glBindTexture.

e.g.

if (textureName != cachedTextureName) {
    glBindTexture(GL_TEXTURE_2D, textureName);
    cachedTextureName = textureName;
}

This approach requires that ALL calls to glBindTexture be handled similarly, preferably through a wrapper. But the inclusion of third-party code makes this problematic, as well as being Yet Another Thing to Remember.

Q. Is this a useful optimisation? Or is the OpenGL implementation smart enough to disregard calls to glBlindTexture where the texture name has not changed?

Thanks.

Upvotes: 4

Views: 883

Answers (1)

Ron Warholic
Ron Warholic

Reputation: 10074

This isn't called out in the spec as far as I've seen. In practice is appears many implementations actually do some processing if you rebind the same texture as you'll see a relative performance drop if you don't test the current texture. I'd recommend using the test if possible just to ensure that implementation details don't have an adverse effect on your program.

Generally speaking it is quite useful to abstract over the OpenGL state machine with your own state handling so you can query state such as the bound texture or the active matrices without calling glGet which will almost always be slower than your custom handling. This also allows you to prohibit invalid behavior and generally makes your program easier to reason about.

Upvotes: 3

Related Questions