SFML community forums

Help => Window => Topic started by: Hedgeberry on June 29, 2022, 07:53:33 pm

Title: RenderTarget slower RenderWindow
Post by: Hedgeberry on June 29, 2022, 07:53:33 pm
We have a hierarchy of classes that store a reference where they are rendered. This can be either RenderTexture or RenderWindow (passed during initialization), so we store a reference to RenderTarget, after we changed the reference type from RenderWindow to RenderTarget (but the reference still passes the same window) our program has halved the FPS we were counting with sf::Clock. Is the problem somewhere in us or in SFML and the libraries on which it depends?
Title: Re: RenderTarget slower RenderWindow
Post by: eXpl0it3r on June 29, 2022, 11:55:32 pm
Continuation from this thread (https://en.sfml-dev.org/forums/index.php?topic=28600.0).

My guess is, that you're measuring the FPS count wrong.
For example, since you still want to see the result, you're probably also rendering the render texture to the window, thus if you're not paying attention you'll count the rendering twice and it can "half" your "fps".
Title: Re: RenderTarget slower RenderWindow
Post by: Hedgeberry on July 06, 2022, 07:04:59 pm
The timer is trivially reset at the beginning of each frame, so it takes everything into account. We don't re-render the texture, because we still render directly to the RenderWindow, it's just that now the reference to the RenderWindow has the type RenderTarget& instead of RenderWindow& as it was before, we haven't changed anything else. Thanks for the help, but apparently you don't know what this problem is.
Title: Re: RenderTarget slower RenderWindow
Post by: eXpl0it3r on July 06, 2022, 07:19:34 pm
If you can create a minimal, complete and compliable example that reproduces the issue, then we can take a look. Otherwise, all I can do is guess and from experience the problem is then often found somewhere in the user code.

You can also try to build in release mode and see if some of the debug vtable code path are very unoptimized, but kind of doubtful.
Title: Re: RenderTarget slower RenderWindow
Post by: Hedgeberry on July 10, 2022, 07:38:30 pm
I tried to write code that reproduces the problem, but it does not reproduce it, apparently the problem is still somewhere, thank you
#include <SFML/Graphics.hpp>

float calculateMediumFPS(std::vector<float>& lastFPS) {
        float sum = 0.0f;
        for(const auto &fps: lastFPS) {
                sum += fps;
        }
        return sum / static_cast<float>(lastFPS.size());
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
       
        sf::RenderTarget* renderTarget = &window; //When the pointer type is sf::RenderWindow, it works faster
       
        sf::Clock clock;
        std::vector<float> lastFPS(60, 0.0f);
       
        while (window.isOpen())
        {
                lastFPS[0] = 1.f / clock.restart().asSeconds();
                std::rotate(lastFPS.begin(), lastFPS.begin() + 1, lastFPS.end());
                float mediumFPS = calculateMediumFPS(lastFPS);
                window.setTitle(std::to_string(static_cast<int>(mediumFPS)));
                clock.restart(); //to count only the game cycle, not taking into account the cost of the FPS meter
               
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
               
                window.clear();
                renderTarget->draw(shape);
                window.display();
        }
       
        return 0;
}