Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Question about rendering performance.  (Read 3131 times)

0 Members and 1 Guest are viewing this topic.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Question about rendering performance.
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Question about rendering performance.
« Reply #1 on: January 14, 2012, 09:50:29 am »
Using a single texture is always faster, because changing the current texture is a costly operation for the OpenGL driver.
Laurent Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Question about rendering performance.
« Reply #2 on: January 14, 2012, 12:03:27 pm »
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.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Question about rendering performance.
« Reply #3 on: January 14, 2012, 12:03:30 pm »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Question about rendering performance.
« Reply #4 on: January 14, 2012, 03:36:13 pm »
Quote
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.

Quote
If that is the case I don't see how it makes a difference in that scenario.

It doesn't.

Quote
would I achieve better performance by drawing objects that use the same texture in order then?

Yes.

Quote
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 Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Question about rendering performance.
« Reply #5 on: January 15, 2012, 04:29:58 am »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Question about rendering performance.
« Reply #6 on: January 15, 2012, 09:49:49 am »
Quote
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):
Code: [Select]
// 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.
Laurent Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Question about rendering performance.
« Reply #7 on: January 15, 2012, 08:16:45 pm »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Question about rendering performance.
« Reply #8 on: January 15, 2012, 10:02:21 pm »
Quote
So what I said before is true

Absolutely, I already agreed above ;)
Laurent Gomila - SFML developer