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

Author Topic: Shader is running really slow. Not sure what it is.  (Read 9390 times)

0 Members and 1 Guest are viewing this topic.

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Shader is running really slow. Not sure what it is.
« 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?
« Last Edit: February 17, 2014, 02:13:25 am by dixondean25 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Shader is running really slow. Not sure what it is.
« Reply #1 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #2 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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Shader is running really slow. Not sure what it is.
« Reply #3 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 ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #4 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?

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #5 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Shader is running really slow. Not sure what it is.
« Reply #6 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #7 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.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #8 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..

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #9 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!

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #10 on: February 17, 2014, 05:46:27 am »
I ran it unaltered and was getting 90-110 fps.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #11 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.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #12 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.

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Shader is running really slow. Not sure what it is.
« Reply #13 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

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #14 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.