Hello all!
I've been playing around with SFML and I have to say it's a neat little library. For debugging purposes I have several texts rendered on the screen, which (as I checked) quite drastically lowered my FPS. This is not the end of the world, but I would like to ask you several questions regarding this matter and how to make text rendering faster.
SpecsSFML: 2.0 custom built (527bb287a5538f547b5b29c464d22a0ebfa0baa2)
IDE: Visual Studio 2012 Professional
OS: Windows 7 x64
GPU: ATI Radeon HD 4850 (512 GDDR3) + Catalyst 12.4
CPU: Athlon Phenom X2 3.1GHz (2 cores)
RAM: 6GB DDR2
Splitting the loopThis is basically the main part of rendering which takes the most time (82%), while
RenderWindow.clear() and
RenderWindow.display() takes the rest (~18%).
void render(sf::RenderWindow& rw, const sf::Font& font)
{
sf::ConcaveShape shape = ...;
sf::Text t;
t.setFont(font);
for ( 100 ) {
auto pos = ...;
shape.setPosition(pos);
rw.draw(shape);
#ifdef DRAW_TEXT
const auto str = ...;
t.setString(str);
t.setPosition(pos);
rw.draw(t);
#endif // DRAW_TEXT
}
}
Text: 365 fps
No text: 1850 fps (!)
So I decided to check whether splitting the loop into 2 helps.
void render(sf::RenderWindow& rw, const sf::Font& font)
{
sf::ConcaveShape shape = ...;
sf::Text t;
t.setFont(font);
for ( 100 ) {
auto pos = ...;
shape.setPosition(pos);
rw.draw(shape);
}
#ifdef DRAW_TEXT
for ( 100 ) {
auto pos = ...;
const auto str = ...;
t.setString(str);
t.setPosition(pos);
rw.draw(t);
}
#endif // DRAW_TEXT
}
Text: 640 fps
No text: 1850 fps
There is a huge (1.75x) increase in performance!
I am assuming this is due to the fact that the render state is not swapped back and forth between shape and font?ProfilingThis is what VS2012 profiler shows:
void render(sf::RenderWindow& rw, const sf::Font& font)
{
sf::ConcaveShape shape = ...;
sf::Text t;
t.setFont(font);
for ( 100 ) {
auto pos = ...;
shape.setPosition(pos);
rw.draw(shape); // 26.7%
}
#ifdef DRAW_TEXT
for ( 100 ) {
auto pos = ...;
const auto str = ...; // 13%
t.setString(str); // 34.5%
t.setPosition(pos);
rw.draw(t); // 25.3%
}
#endif // DRAW_TEXT
}
Percentage values describe how much of the whole time
for this method was taken by the given line.
OptimizationOk, so the question remains:
how can I make it faster?
- Precreation and caching of all texts (they are not that dynamic) in sf::Texts. It would take a lot of resources I assume.
- Using bitmap fonts. I am not sure how this could help as the string would still have to be "parsed" and mapped to glyphs.
I would be glad for any other suggestions and comments.