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 9373 times)

0 Members and 1 Guest are viewing this topic.

Azaral

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

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Shader is running really slow. Not sure what it is.
« Reply #16 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
« Last Edit: February 17, 2014, 06:51:53 am by Azaral »

dixondean25

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

Azaral

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

dixondean25

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