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

Author Topic: Shader performance  (Read 2703 times)

0 Members and 1 Guest are viewing this topic.

ic5y

  • Newbie
  • *
  • Posts: 4
    • View Profile
Shader performance
« on: November 30, 2013, 06:03:38 pm »
Hello!

I have an interesting problem, maybe it's just my old video card. I'm trying to create a custom font renderer (using sf::Sprite with shaders). The problem is that if I draw 3700 sprite without shaders, it runs at 300-400 FPS, but if I turn on the very basic shaders the FPS drops to 9. The vertex shader does nothing, but on the fragment, I just do some simple calculation.

uniform sampler2D texture;

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
    if(pixel == vec4(1.0, 0.0, 1.0, 1.0))
    {
        pixel = vec4(vec3(0.0, 0.0, 0.66666), 1.0);
    }
    else
    {
        pixel = vec4(vec3(1.0, 1.0, 0.3), 1.0);
    }
    gl_FragColor = pixel;
}

Any idea? I thought about the lot of texture swap, but then it won't run with 300 - 400 fps without shaders.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Shader performance
« Reply #1 on: November 30, 2013, 06:16:08 pm »
Try to avoid the conditional jump (if-else). What possible colors can the texture have, and how should it be manipulated depending on them?

Minor issues are the useless operations: why packing a vec3 inside a vec4, and why the intermediate assignment to pixel instead of gl_FragColor?

Furthermore, test how the code behaves with vertex arrays instead of sprites.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ic5y

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shader performance
« Reply #2 on: November 30, 2013, 06:24:46 pm »
I packing the vec3 into a vec4, because I had some uniforms to modify the color of the fonts. But I commented them out, and deleted from the source. On the other hand, I'm checking if the texture is transparent(255,0,255 color), and I set that pixel to the background color (now vec3(0.0, 0.0, 0.66666)), then set the other pixels color to the other color.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Shader performance
« Reply #3 on: November 30, 2013, 09:39:15 pm »
On the other hand, I'm checking if the texture is transparent(255,0,255 color), and I set that pixel to the background color (now vec3(0.0, 0.0, 0.66666)), then set the other pixels color to the other color.
But to check whether the pixel is transparent, it suffices to look at the alpha channel. Assuming the alpha component is either 0 or 1, you can use multiplication instead of a branch:
const vec4 result1 = vec4(...);
const vec4 result2 = vec4(...);

vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
gl_FragColor = pixel.a * result1 + (1-pixel.a) * result2;

Just to show the principle, I don't know how well drivers can optimize here. You can reduce the number of needed operations even further:
const vec4 result1 = vec4(...);
const vec4 result2 = vec4(...);
const vec4 diff = result1 - result2; // or hardcode this directly

vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
gl_FragColor = result2 + pixel.a * diff;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ic5y

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shader performance
« Reply #4 on: December 02, 2013, 12:58:45 am »
But could it be a shader problem? On the other hand, I haven't tried yet, what happens if I use vbos, because I just couldn't drop GLEW into the project. ><

EDIT: I modified the shader not to use branching, only got +1 FPS.
« Last Edit: December 02, 2013, 02:32:41 am by ic5y »

upseen

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Shader performance
« Reply #5 on: December 07, 2013, 10:24:32 am »
By any chance, could it be that you're loading the shader from file every single frame? Also, what kind of graphics card do you have? Does it run the shaders' example smoothly?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Shader performance
« Reply #6 on: December 07, 2013, 03:06:47 pm »
Should we guess further? If not, come up with a complete and minimal example, to make sure there are no mistakes like the one mentioned by upseen.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ic5y

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Shader performance
« Reply #7 on: December 08, 2013, 06:11:18 pm »
http://pastebin.com/9trE982C <- here is my code

The code of the shader:
uniform sampler2D texture;

const vec4 res1 = vec4(1.0, 1.0, 0.3, 1.0);
const vec4 res2 = vec4(0.0, 0.0, 0.66666, 1.0);

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
    gl_FragColor = pixel.a * res1 + (1.0 - pixel.a) * res2;
}
 

The shader examples are running smooth, maybe the old and slow VGA is the problem.