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

Author Topic: [SOLVED]Maximum number of quads in a sf::VertexArray ?  (Read 187 times)

0 Members and 1 Guest are viewing this topic.

tsterin

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
[SOLVED]Maximum number of quads in a sf::VertexArray ?
« on: June 19, 2020, 07:07:37 pm »
Hello,

I am working on a Cellular Automota simulation project (Game Of Life type).

I allow the user to zoom in and out, and when they zoom out, more cells of the simulation are computed.

I was drawing all cells individually and I, very quickly, encountered performance issues as I was calling draw so many times.

I would like to use sf::VertexArray to display my cells (each cell is a quad). However, I wondered if it is not dangerous that the number of cells I use can be unbounded (since the user can zoom out).

Put it in an other way, is there a maximal size above which it is not safe/performant to use sf::VertexArray ? Will SFML or OpenGL automatically split my sf::VertexArray if it is too big for the buffer of my GPU ?

Would you recommend I fix a maximal limit in number of quads in a sf::VertexArray and potentially use several of them ? If yes, what would you recommend that maximal size to be ?

Thank very much in advance,
« Last Edit: June 22, 2020, 10:33:36 am by tsterin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9573
    • View Profile
    • development blog
    • Email
Re: Maximum number of quads in a sf::VertexArray ?
« Reply #1 on: June 21, 2020, 02:28:15 pm »
Everything is built with certain limits. Sometimes it's a fixed limit, sometimes a physical one and other times it really depends on a mix of hardware and software.
I don't know if OpenGL sets a limit (maybe limited by an overflowing integer number), but your hardware/driver setup will slowly reach a frametime of 1s or more, so you have 1fps or less.
Instead of thinking of how to make sure to render everything, it would be probably better to think about, what you don't need to render once you're zoomed out. At one point a line is smaller than a pixel line anyways and then you have to decide how to merge these lines. Plus you also have to deal with some limits, then at some point you may not be GPU limited by rather CPU limited. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6226
  • Thor Developer
    • View Profile
    • Bromeon
Re: Maximum number of quads in a sf::VertexArray ?
« Reply #2 on: June 21, 2020, 02:59:20 pm »
To extend eXpl0it3r's answer:

Quote
Put it in an other way, is there a maximal size above which it is not safe/performant to use sf::VertexArray ? Will SFML or OpenGL automatically split my sf::VertexArray if it is too big for the buffer of my GPU ?
No, SFML/OpenGL will not split the vertex array.

Apart from VRAM, you also are limited by RAM -- since sf::VertexArray is backed by an std::vector, you need enough contiguous memory to store all the cells. Even if modern machines come with lots of RAM, you need a big enough chunk of memory at once.

What I would probably do is hierarchical rendering. Split the world into regions, where one region is e.g. 100x100 cells. Render each region separately, and only if it's in view.

There are futher optimizations you can do:
  • Obviously, only update sf::VertexArray if the cells in that region change
  • Do not recreate/assign the vertex array, instead use clear() + append() or resize() + operator[]
  • Depending on your graphics, it may be enough to render the active cells (often a sparse subset of all cells)
  • At some zoom level, you may end up with so many tiny cells that the quad representation is inefficient, and you can e.g. render dots instead, or use a shader
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

tsterin

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Maximum number of quads in a sf::VertexArray ?
« Reply #3 on: June 21, 2020, 09:25:48 pm »
Thank you very much both for your help, that's very very helpful.

@Nexus: in your example you choose 100x100 cells because 10^4 quads is what the average gpu can handle easily at once?

Thanks again,

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6226
  • Thor Developer
    • View Profile
    • Bromeon
Re: Maximum number of quads in a sf::VertexArray ?
« Reply #4 on: June 21, 2020, 09:43:17 pm »
@Nexus: in your example you choose 100x100 cells because 10^4 quads is what the average gpu can handle easily at once?
No, it was just an example with arbitrary numbers. I don't know how many quads can be handled.

You may need to experiment to find what works best. With smaller regions, you are on the safer side, but it costs you some extra drawcalls.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

tsterin

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Maximum number of quads in a sf::VertexArray ?
« Reply #5 on: June 22, 2020, 10:33:06 am »
Ok, sounds great thank you!