Skip to main content
gamedev.net
🔒 Locked

Is switching textures a performance hit?

Started by Funkymunky 4 replies 5.5k views
Original Post
Funkymunky
Funkymunky
Currently, I have a texture array that I'm using to represent multiple levels of detail. I bind it once (I'm using OpenGL), then render something for every level of the array using a shader that takes that level as input.

I'm now thinking of switching to a method that would require me to bind a different texture for every level, rather than just binding the array once at the beginning. Essentially, i'd be managing the array manually myself. Is there a performance hit for every time I switch which texture is assigned to my shader for input, even though all the textures are already loaded in video memory at that point?
karwosts
karwosts
Yes, there is some performance cost to switching textures. I can't say whether or not it will be better or worse than whatever alternate solution you have, but it is not free.

Also, you don't get to control which textures are loaded in video memory. The OpenGL driver is free to move things in or out of video memory as it sees fit at any time. You can upload your data into a texture, but you don't know or control whether or not it goes into video memory or system memory, or whether it will stay there or not.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Funkymunky
Funkymunky
okay. but, if it is there, then it's a fairly trivial performance hit, right? (it's not gonna copy any data around, just change where the sampler points to in the shader)
Danny02
Danny02
In my experience switching a texture is one of the most expensive state changes of opengl.
I would probably would always sort want I render first by textures used.

as karwosts said it can indeed copy larg amounts of data around, in the normal case textures are the resources which take the most of the video memory. So when u activate a texture the driver might need to copy some data from VRAM to RAM and the texture u wanna use from RAM to VRAM.
Erik Rufelt
Erik Rufelt
Unless you change texture like a thousand times per frame I wouldn't worry too much about it. Definitely prefer sorting items by texture for optimal performance though.
Hodgman
Hodgman
On the CPU side, any state change is a performance hit in that you have to call a function that causes the driver to insert something into the GPU's command buffer.

On the GPU side, switching textures means that the contents of the texture cache are no longer useful, and the next thing to draw will experience cache-misses until the cache warms up again. However, if you're using a texture array and switching which 'slice' you're fetching from all the time, this will have the same effect on the cache anyway.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.