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
#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();
}
}