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

Author Topic: Draw million sprites efficiently  (Read 6817 times)

0 Members and 1 Guest are viewing this topic.

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Draw million sprites efficiently
« on: October 28, 2013, 01:03:59 am »
Hello, I want do draw big isometric maps efficiently with SFML.
The maximum map size I intend at the moment is 1024*1024. But the perfomance is already bad at 512*512. It takes around 0,67s to render a frame.
The maps are randomly created and there are 20 textures for the ground each 44*27px. Every texture is in the memory once, no copies. Sprites out of view are not rendered, but i want to zoom out and see the whole map.
There has to be a faster way than calling draw() per tile.

One Tile:


Screenshot from a 512*512 map:

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Draw million sprites efficiently
« Reply #1 on: October 28, 2013, 01:05:03 am »
There is.  VertexArrays.

If I'm interpreting your post right you'll have to combine those 20 textures into a single file/texture, but after that it's just a question of how to generate the list of vertices.

And since this use case is related to zooming out, there's a chance that using some OpenGL calls to generate mipmaps might help, but focus on vertex arrays first.
« Last Edit: October 28, 2013, 01:09:13 am by Ixrec »

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Draw million sprites efficiently
« Reply #2 on: October 28, 2013, 05:12:43 pm »
Thank you for your answer.
I'm not sure how to use vertex arrays, but i'm sure i'll figure it out.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Draw million sprites efficiently
« Reply #3 on: October 28, 2013, 06:42:08 pm »
http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php

Edit: Just realized Ixrec had the same link in his reply. :P
DSFML - SFML for the D Programming Language.

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Draw million sprites efficiently
« Reply #4 on: October 28, 2013, 10:26:52 pm »
After switching to vertex arrays a frame takes 0,017s (60FPS) to draw at 512*512 maps.  At 1024*1024 each frame takes 0,04 (25FPS). This is a good improvement. I'm using quads in single array.
Some more Questions:
  • Is there a way to improve that even more?
  • Is it possible not to draw tiles out sight?
  • Maybe by breaking down the single array into smaller ones?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw million sprites efficiently
« Reply #5 on: October 28, 2013, 10:33:38 pm »
Yes, yes and yes.
Laurent Gomila - SFML developer

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Draw million sprites efficiently
« Reply #6 on: October 29, 2013, 06:22:26 pm »
What would you do?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw million sprites efficiently
« Reply #7 on: October 29, 2013, 07:09:53 pm »
You have to ask precise questions, we're not going to post a complete code or algorithm for you ;)
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Draw million sprites efficiently
« Reply #8 on: October 29, 2013, 07:54:45 pm »
Yes, yes and yes.

I was under the impression that the purpose of a vertex array was to use as few draw calls as possible, but you said that breaking the larger vertex array into smaller ones would help to increase performance. Can you elaborate on that for me please?
DSFML - SFML for the D Programming Language.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw million sprites efficiently
« Reply #9 on: October 29, 2013, 08:00:05 pm »
1 or 10 draw calls won't make a noticeable difference, however drawing 1,000,000 or 10,000 quads will. So sometimes it may be a good strategy to have a few vertex arrays rather than a big one, in order to be able to ignore parts that are not visible.
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Draw million sprites efficiently
« Reply #10 on: October 29, 2013, 09:02:42 pm »
In this case, yeah I can see how that would improve performance. I guess I mistook it for a generalization.
DSFML - SFML for the D Programming Language.

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Draw million sprites efficiently
« Reply #11 on: October 29, 2013, 09:29:34 pm »
You have to ask precise questions, we're not going to post a complete code or algorithm for you ;)

How would you draw 1024² tiles in order to get good performance?
As long as there are good tutorials I don't need complete code and SFML has good tutorials  ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Draw million sprites efficiently
« Reply #12 on: October 29, 2013, 09:32:10 pm »
Why would you want to do that? With 1024 tiles in width and height, each tile would have a size of 1-2 pixels.

Only draw the tiles you really need.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Verteo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Draw million sprites efficiently
« Reply #13 on: October 29, 2013, 09:48:20 pm »
Because I want to zoom out and see the whole map not just parts. While zooming in I won't draw tiles out of view.
Maybe implementing a LOD system would be an option. I'm open for suggestions  ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Draw million sprites efficiently
« Reply #14 on: October 29, 2013, 10:02:13 pm »
Yes, you could render different zoom levels to a render texture, and then draw a sprite using this texture.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything