SFML community forums

Help => Graphics => Topic started by: dixondean25 on February 17, 2014, 02:10:57 am

Title: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 02:10:57 am
here is the entire code...

#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
sf::Shader testShader;
sf::RectangleShape testShape;
sf::RenderTexture renderTexture;
sf::Texture texture;
sf::Sprite sprite;
sf::Clock time1;
float lastTime;
int main(){
   sf::RenderWindow Window;
   Window.create(sf::VideoMode(1280, 720, 32), "Test");
   //Window.setFramerateLimit(60);
   sf::Event event;
   testShape.setSize(sf::Vector2f(500,500));
   testShader.loadFromFile("wave2.vert",sf::Shader::Fragment);
   renderTexture.create(500,500);
   while (Window.isOpen())
    {
      while (Window.pollEvent(event))
      {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
               Window.close();
      }
         /** When the user left-mouse-click, add a box into the world */
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
      {
          Window.close();
      }
      Window.clear(sf::Color::Black);
      testShader.setParameter("resolution",100,100);
      testShader.setParameter("time",time1.getElapsedTime().asSeconds());
      renderTexture.clear();
      renderTexture.draw(testShape,&testShader);
      renderTexture.display();
      texture = renderTexture.getTexture();
      sprite.setTexture(texture);
      Window.draw(sprite);
      Window.display();
      float currentTime = time1.getElapsedTime().asSeconds();
        float fps = 1.f / (currentTime - lastTime);
        lastTime = currentTime;
      cout<<"FPS: "<<fps<<"\n";
   }
   return 0;
}

does this look wrong to anybody? the fps I'm getting is about 7 without a framerate limit. If this looks right I guess it could be the shader itself. I implented the one shown here:
http://glsl.heroku.com/e#8067.3
i just wanted to see if i could get a shader working at all, but it is really slow. Does anybody have answers?
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Hapax on February 17, 2014, 02:57:39 am
I run this code without any alterations and it was saying that I was getting around 300 FPS  :o
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 03:37:53 am
I run this code without any alterations and it was saying that I was getting around 300 FPS  :o

then it is probably the shader that i'm using?
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Hapax on February 17, 2014, 03:41:31 am
then it is probably the shader that i'm using?
It looks like something that's not in that code, yes ;)
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 03:57:43 am
it works fine in the link in my original post. why can't it run fine on sfml?
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 04:02:48 am
this is how i want it to look, but take the whole screen up, and run fast.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Hapax on February 17, 2014, 04:12:42 am
I'm going to have to bail here. I know pretty much nothing of shaders  :P

Have you tried to run it on a small window? Then, if it's ok, increasing the size to see where it slows down. Full-sized windows can be a bit slow, I've found.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 04:27:01 am
I'm going to have to bail here. I know pretty much nothing of shaders  :P

Have you tried to run it on a small window? Then, if it's ok, increasing the size to see where it slows down. Full-sized windows can be a bit slow, I've found.

lol i ran it in a 50x50 window and getting same results.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: eigenbom on February 17, 2014, 04:43:01 am
A few things to try:
- set your render texture size to 512x512 (or any power of two)
- the demo you linked to is running at half resolution afaict (so will be quicker)
- cout << "ddd" will slow each frame down, instead try doing a cout every 100 frames or so..
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 05:32:53 am
A few things to try:
- set your render texture size to 512x512 (or any power of two)
- the demo you linked to is running at half resolution afaict (so will be quicker)
- cout << "ddd" will slow each frame down, instead try doing a cout every 100 frames or so..

Alright changing the resolution to 512x512 was still slow about 6 fps, but 256x256 was at 25 fps, which looks fine. Thank you!
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 05:46:27 am
I ran it unaltered and was getting 90-110 fps.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 05:51:16 am
I changed the part for outputting the fps to once a second vs every frame and it runs 109-111 fps now.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 05:53:01 am
I changed it to take up the entire 1280x720 resolution and it now runs at around 23 fps.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 06:19:27 am
I changed it to take up the entire 1280x720 resolution and it now runs at around 23 fps.

do you mind showing the code please? I want to see exactly what you put
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 06:41:53 am
I changed it to take up the entire 1280x720 resolution and it now runs at around 23 fps.

do you mind showing the code please? I want to see exactly what you put

testShape.setSize(sf::Vector2f(1280,720));
testShader.loadFromFile("wave2.txt",sf::Shader::Fragment);
renderTexture.create(1280,720);
 

Instead of
 testShape.setSize(sf::Vector2f(500,500));
   testShader.loadFromFile("wave2.vert",sf::Shader::Fragment);
   renderTexture.create(500,500);
 

I had to change it from a .vert file to a .txt file because I couldn't quite figure out how to make a .vert file, though I don't know if this matters or not.

I also changed
 testShader.setParameter("resolution",100,100);
 
to
 testShader.setParameter("resolution",1280,720);
 


I also ran it with a blank shader file and it did not change the FPS. It was still running at ~20fps.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 06:44:55 am
I think I found the problem in your code. I just made a change and it runs at max resoltuion at over 175fps. Will follow up with a post in a few minutes showing the problem I found.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 06:50:11 am
The problem section is here
texture = renderTexture.getTexture();
sprite.setTexture(texture);
 

You are copying a texture and then setting this in the sprite and then drawing the sprite.

I instead did this outside of the while loop
sprite.setTexture(renderTexture.getTexture());
 

Then this section
Window.clear(sf::Color::Black);
testShader.setParameter("resolution",100,100);
testShader.setParameter("time",time1.getElapsedTime().asSeconds());
renderTexture.clear();
renderTexture.draw(testShape,&testShader);
renderTexture.display();
texture = renderTexture.getTexture();
sprite.setTexture(texture);
Window.draw(sprite);
Window.display();
 
became this
Window.clear(sf::Color::Black);
testShader.setParameter("resolution",1280,720);
testShader.setParameter("time",time1.getElapsedTime().asSeconds());

renderTexture.clear();
renderTexture.draw(testShape,&testShader);
renderTexture.display();

Window.draw(sprite);
Window.display();
 

The main problem was you were copying a texture, which is a heavy weight operation, and in this case completely unnecessary. The sprite just needs it's texture set once as the sprite contains a pointer to its texture. Copying the texture was unnecessary since you can just set the sprite's texture to be the texture inside the render texture and then any changes will automatically be taken into account when you draw the sprite since the sprite points to the texture inside the render texture.

With these changes it runs silky smooth at ~180 fps on my machine, which is ~5 years old. The graphics card is an nvidia 8800 GTX
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 06:58:29 am
That was the problem, I'm getting great fps now. I will admit this was my first time using sf::RenderTexture, and I didn't know you only need to call it once. Thank you so much Azaral you're friggin' awesome.
Title: Re: Shader is running really slow. Not sure what it is.
Post by: Azaral on February 17, 2014, 06:59:33 am
I try to be and occasionally I succeed.

Also, I tested it and you do not need the sf::RectangleShape here
 renderTexture.draw(testShape,&testShader);
 
I did this instead and it works near as I can tell
renderTexture.draw(sprite,&testShader);
 
Title: Re: Shader is running really slow. Not sure what it is.
Post by: dixondean25 on February 17, 2014, 07:44:07 am
I try to be and occasionally I succeed.

Also, I tested it and you do not need the sf::RectangleShape here
 renderTexture.draw(testShape,&testShader);
 
I did this instead and it works near as I can tell
renderTexture.draw(sprite,&testShader);
 

yes this also works. thank you again!