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

Author Topic: SFML-Like entities and Vertex Arrays  (Read 2054 times)

0 Members and 1 Guest are viewing this topic.

larryturtis

  • Newbie
  • *
  • Posts: 3
    • View Profile
SFML-Like entities and Vertex Arrays
« on: November 06, 2016, 09:13:46 pm »
I've been looking at the SFML C++ library and am interested in using it to build some 2D graphics apps. I don't want to learn OpenGL if I can avoid it; I'm okay working at the level of abstraction SFML provides.

In reading the documentation, I've determined that there are performance gains to be had in putting most of my objects into vertexArrays. I don't need or care about textures at this point, simple colors and geometry are fine.

Now, let's say I have around 50,000 triangles and I want to display them in 5 layers of 10,000 triangles each. I can either try to manage them in a single SFML entity or I can create 5 SFML entities and manage each as its own layer. The latter approach seems easier in terms of maintainability, but from a performance standpoint, does it matter? Will I get significant performance gains if I use a single entity?

What about if I up the number of layers from 5 to 500? Does that change anything?

Thanks for your help!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML-Like entities and Vertex Arrays
« Reply #1 on: November 06, 2016, 10:15:30 pm »
5 layers causes 5 separate draw calls. Draw calls are (usually) the (most) expensive part so reducing the amount is often the focus in optimising drawing efficiency.

That said, 5 is very few and there should be no significant difference in the speed of drawing 50,000 triangles with one call and drawing 10,000 triangles 5 times.

However, draw calls add up and increasing that layer to 500 (as you suggested) would result in a number of draw calls that can severely "lag" some systems. For so many layers, you can combine them into fewer calls and draw the later layer's triangles towards the end of the call to be draw over the top.

It's simpler and easier to draw the layers separated but too many layers can cause unacceptable frame drops so if many layers are needed, combine them when drawing.

Are you really considering draw 500 layers of 10,000 triangles? 5 million triangles is a lot, especially for 2D work since a full HD display is only displaying just over 2 million pixels.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

larryturtis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML-Like entities and Vertex Arrays
« Reply #2 on: November 06, 2016, 11:17:32 pm »
Thank you. No, those numbers are purely theoretical. It sounds you are suggesting there is a way to have multiple Drawable objects and then combine them into a single draw call... is there any documentation or sample code I could look into?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML-Like entities and Vertex Arrays
« Reply #3 on: November 06, 2016, 11:56:44 pm »
By combine, I mean combining multiple triangles into a single draw call and therefore multiple layers of triangles into a single draw call of triangles.
This would require use of vertex arrays; other drawables cannot be combined automatically.

So, for example, if you have 5 vertex arrays (layers of triangles), you can combine them into a single vertex array of triangles (the order of the triangles would matter to be drawn in the correct order).

One thing to note is that each draw call can only use a single texture so you may have to plan your textures for this scenario.

Are there any particular drawables of which you are interested in having lots?
"Sprite batching" is a common term for combining sprites into a single draw call; this involves creating a vertex array that reproduces each of the sprites. There are a few examples of sprite batching on the SFML forum and the SFML wiki. Here is one (in the Wiki/Sources/Drawables section): https://github.com/SFML/SFML/wiki/Source%3A-High-Performance-Sprite-Container
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

larryturtis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML-Like entities and Vertex Arrays
« Reply #4 on: November 08, 2016, 10:37:45 pm »
Thanks, this is great. I will take a look at Sprite batching.