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

Author Topic: sf::Text performance  (Read 3947 times)

0 Members and 2 Guests are viewing this topic.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
sf::Text performance
« on: September 29, 2015, 08:32:34 pm »
I have some issues with render text performance.
So, could someone suggest me the best approaches to improve performance for text rendering?

I need to render object names which should fit to the object size. And each object has it's own size which may be varied in very different range. The total object count and it's sizes is changed in realtime and sometimes it changes very quickly (it may be significantly diferrent on two followed frames). Usually I have 200-500 objects on the screen  whhich should be rendered with name.

So my current best approach is to initialize single Text object, assign the font (because the font and color is always the same). And then render it in a loop with calculating CharacterSize from object size.
I tried to render text within separate loop to avoid context switch frome shape to the text. It was recommended in another topic. But as a result I got a lower performsnce. So, it's not a good solution.
May be there is another way to implement such functionality? Something like render text into texture on text change, using text scaling or something like that?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Text performance
« Reply #1 on: September 29, 2015, 08:54:03 pm »
If all the possible object names are known up-front you could render them all up-front and just pick which ones to show where each frame. Or render them on demand but keep them around (cache them) for use later - again, just draw the needed subset of pre-rendered names every frame. Doesn't sound like there's a need to do all the work each frame since much of it will be repeated from previous frame and just repositioning and drawing an existing object is a lot lighter than creating/positioning/drawing it each frame...

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sf::Text performance
« Reply #2 on: September 30, 2015, 02:02:09 am »
I tried to cache it,  but it causes Out of memory error.  I need to render them all, because they all are visible to the user (objects which is not in camera area is not passed to the renderer)...

I have a lot of objects in the game (for about 10000-20000), and it already limit its count for renderer by passing only objects which is in visible area. But the visible area may contains up to 1000-2000 objects simultaneously. Usually most of them is simple and does not need for the text rendering, but some objects (for about 10-20%) needs to render 2 text strings (name and info).

So, I cannot cache texts for every objects, because it will eat all available memory and will crash the game...
« Last Edit: September 30, 2015, 04:21:26 am by mkalex777 »

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sf::Text performance
« Reply #3 on: September 30, 2015, 05:30:05 pm »
I found the reason of system freezes and low text rendering performance :)

First, I catch the case when it happens. It happens when a large text appears or disappears on the screen.
I tried some tests and found that the issue appears, because I'm using Text.CharacterSize to change the size of text.
I changed it to use Text.Scale instead of Text.CharacterSize. And now it works very smooth and fast!
No more fps drops! Just cool!  ;D

I think it's probably because when I used Text.CharacterSize it cause a large amount of writes into video memory.

UPDATE: I don't believe! I replaced Text usage with constant CharacterSize and now fps grow up 10x times! All works very smoothly even with 1000-2000 game objects on the screen!  ;D
« Last Edit: September 30, 2015, 06:02:11 pm by mkalex777 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11037
    • View Profile
    • development blog
    • Email
sf::Text performance
« Reply #4 on: September 30, 2015, 06:00:16 pm »
AFAIK if you change the character size it reloads the font glyphs with the right size. With scale you just use the loaded glyphs and resize them, which will result in less crispy text rendering. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sf::Text performance
« Reply #5 on: September 30, 2015, 06:05:08 pm »
AFAIK if you change the character size it reloads the font glyphs with the right size. With scale you just use the loaded glyphs and resize them, which will result in less crispy text rendering. ;)

I just set maximum CharacterSize=200 which is possible in the game, as result I have crystal clear texts even with AA=16x
The performance grows up very significantly! so I even can disable optimizations such as interpolation of circles with squares when it's physical size is too small...
« Last Edit: September 30, 2015, 06:07:33 pm by mkalex777 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Text performance
« Reply #6 on: September 30, 2015, 11:48:45 pm »
Glyphs are loaded lazily but each size is in its' own Texture so there is a lot of Texture switching when various sizes are mixed.
Back to C++ gamedev with SFML in May 2023

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sf::Text performance
« Reply #7 on: October 01, 2015, 01:39:44 am »
Glyphs are loaded lazily but each size is in its' own Texture so there is a lot of Texture switching when various sizes are mixed.

It very interest, because it freezes entire system, even mouse cursor stops to respond...

 

anything