SFML community forums
General => General discussions => Topic started by: vidjogamer on January 14, 2012, 05:03:25 am
-
Hello, I am just curious what the performance cost would be if each frame of my animated sprite were a separate png vs setting the sub rect of a large sprite sheet each frame.
-
Using a single texture is always faster, because changing the current texture is a costly operation for the OpenGL driver.
-
Laurent, given that a bunch of animations would be printed at every frame, is there a difference between changing to the large texture for each animation or changing for the small frame texture?
Since there is a lot of animations, the texture will already be changed all the time.
-
So if I have an animated character in my game, but there is only one instance of this character. He gets drawn every frame between the background and foreground tiles. Would it not affect the game? At first thought it would seem the system switches to the background texture, then to the current character texture (regardless of sprite sheet or individual textures it has to change just once), then once more to the foreground texture. For a total of 3 changes per frame. If that is the case I don't see how it makes a difference in that scenario.
So now assume we have a game with many different objects, would I achieve better performance by drawing objects that use the same texture in order then? Or does SFML do this under the hood?
-
Laurent, given that a bunch of animations would be printed at every frame, is there a difference between changing to the large texture for each animation or changing for the small frame texture?
Since there is a lot of animations, the texture will already be changed all the time.
What's important is how many times you change the current texture in one frame.
If that is the case I don't see how it makes a difference in that scenario.
It doesn't.
would I achieve better performance by drawing objects that use the same texture in order then?
Yes.
Or does SFML do this under the hood?
No.
And don't forget to test potential optimizations before applying them. Different use cases will react differently to optimizations.
-
Laurent, is this bad performance from the loading of different images on the go?
If all the animation images are loaded right on the beginning, wouldn't it be the same?
If not, I'm not getting the point.
-
Laurent, is this bad performance from the loading of different images on the go?
No. The bad performance is from changing the active texture. OpenGL is a state machine, it can only have one active value for a given state. So if you want to use a different texture than the one which has been used previously, you need to tell OpenGL to change it.
Internally it's like this (simplified):
// sprite 1
glBindTexture(texture1); // this call is expensive
glDraw(....);
// sprite 2
glBindTexture(texture2); // this call is expensive
glDraw(....);
...
If you use the same texture for several sprites, the implementation will need to call glBindTexture less often, and performances will be improved.
-
So what I said before is true: if each sprite uses a different texture anyway, it doesn't matter if each uses one big texture or one texture per frame, since the texture will already be changed at each sprite drawing.
The performance improvement would only surface if we use the same texture for everything on the game, what I think is not advisable.
-
So what I said before is true
Absolutely, I already agreed above ;)