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

Author Topic: RenderTexture heavy CPU overhead  (Read 1269 times)

0 Members and 1 Guest are viewing this topic.

winfab

  • Newbie
  • *
  • Posts: 2
    • View Profile
RenderTexture heavy CPU overhead
« on: March 10, 2014, 07:59:19 pm »
Hello,

I tried adding light effects to my game using this tutorial https://github.com/mattdesl/lwjgl-basics/wiki/2D-Pixel-Perfect-Shadows. Which works fine, but just one light source with the size 600x600 caused the framerate to drop from 2000 to 700. Which I thought was fine because it uses a rather big fragment shader. So I tried to optimize it by using a smaller image size and then scaling it up. Then I noticed that no matter how much I scale down the image the framerate stays at 700. Even if the light size is just 1x1 the framerate stays the same. The framerate also only gets lower when I increase the size of the light until the GPU load reaches almost 100%. On my notebook (with HD4000) if I scale down the light to 1x1 the framerate is almost the same as if the light wasn't there all (stays around 500).

It seems I get massiv CPU overhead on my PC just because of calling clear() and draw() on my RenderTexture(s). On my notebook I don't get that at all. So I'm suspecting my CPU is doing work my GPU should be doing. But since I have little to no knowledge about openGL I'm kinda lost here.

I could reproduce my problem with a (rather) minimal example where I'm just drawing to a RenderTexture and display it. Again if I change the size of the RenderTexture  from 1x1 to 400x400 the framerate stays the same on my PC while on my Notebook it drops a lot.

I'm sorry it's bit to big to call it minimal :D
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include <deque>
using namespace sf;
using namespace std;
int main()
{
    RenderWindow window(sf::VideoMode(800, 600), "SFML window", sf::Style::Titlebar | sf::Style::Close);

    RenderTexture renderTexture;
    renderTexture.create(1,1);
    //renderTexture.create(400,400);

    bool drawRenderTexture = true;

    deque<float> fpsBuffer;
    for (int i = 0; i < 1000; i++)
        fpsBuffer.push_back(0.0f);


    CircleShape cs(150);
    cs.setFillColor(Color::Blue);
    cs.setOrigin(150,150);
    cs.setPosition(Vector2f(200,200));

    Font font;
    font.loadFromFile("..\\resources\\font\\arial.ttf");

    Text fpsText;
    fpsText.setFont(font);
    fpsText.setColor(Color::Red);

    Clock clk;
    while (window.isOpen()) {

        float frametime = clk.restart().asSeconds();
        fpsBuffer.push_front(1.0f/frametime);
        fpsBuffer.pop_back();

        float avgFT = 0;
        for (auto &ft : fpsBuffer) {
            avgFT += ft;
        }
        avgFT /= fpsBuffer.size();

        fpsText.setString(((ostringstream&)(ostringstream() << static_cast<int>(avgFT))).str ());

        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event)) {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::T)
                drawRenderTexture ^= 1;
        }

        window.clear();

        if (drawRenderTexture) {

            renderTexture.clear();
            renderTexture.draw(cs);
            renderTexture.display();

            Sprite s(renderTexture.getTexture());
            s.setOrigin(200,200);
            s.setPosition(300,300);
            window.draw(s);
        }
        window.draw(fpsText);
        window.display();
    }
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: RenderTexture heavy CPU overhead
« Reply #1 on: March 10, 2014, 08:09:35 pm »
A drop from 2000 to 700 is nothing to really worry about. Keep in mind that the framerate is not linear - it doesn't go done as fast in the lower numbers as it does in the higher numbers. Usually it's quite better to look at the frametime, i.e. the time between two frames.

I've quickly tested it and get around 900 fps for both scenarios.

Is your graphics card driver up-to-date?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

winfab

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: RenderTexture heavy CPU overhead
« Reply #2 on: March 10, 2014, 08:49:20 pm »
A drop from 2000 to 700 is nothing to really worry about. Keep in mind that the framerate is not linear - it doesn't go done as fast in the lower numbers as it does in the higher numbers. Usually it's quite better to look at the frametime, i.e. the time between two frames.
I get that and it doesn't really bother me expect I would expect the framerate to go up if I use a texture size that is 400 times smaller since dropped that much before.

I've quickly tested it and get around 900 fps for both scenarios.
On my notebook I get almost 1000 fps difference, while on my PC it stays the same. Don't you think thats weird?

Is your graphics card driver up-to-date?
Yes