• Advertisement
Sign in to follow this  

Shaders & FPS

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I just got introduced to Shading and I'm surprised of how slow my first shader is. The shader pair is dead simple (read below) and when I turn it on in my application, the FPS drops from > 80fps to around 20fps. vertex shader: varying float varHeight; void main(void) { varHeight = gl_Vertex.y; gl_Position = ftransform(); } fragment shader: uniform vec3 colorHeight; varying float varHeight; void main(void) { if (varHeight > colorHeight[0]) //green gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); else if (varHeight > colorHeight[1]) //amber gl_FragColor = vec4(1.0, 0.645, 0.0, 1.0); else if (varHeight > colorHeight[2]) //red gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); else //blue gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); } my only idea to explain that is maybe the fact that I turn shader on/off in the main render function like: void render() { if (useShader){ glUseProgram(program); //do the drawings glUseProgram(0); } else //do the drawings //do some other drawing in fixed mode } is my assumption right or am I missing something?

Share this post


Link to post
Share on other sites
Advertisement
it's probably all those conditionals.

Try rewriting it to use Clamp instead - more math, but easier to predict.

Share this post


Link to post
Share on other sites
hmm.. sorry don't know what Clamp is. Apart from the verb and the manga making team and that's what precisely google is giving me. :)
Do you have a link or doc?

Share this post


Link to post
Share on other sites
I'm fairly sure clamp is a built in function in GLSL, if not it should be.

You actually want to use a step function, not clamp (sorry). They are closely related though. I've forgotten the details, but essentially you use multiplication by 1 or 0 to fake the 'if'.

I'm fairly sure someone cleverer than me will come along and explain it, but if not I'll look it up when I get back from work and reply then.

if you know anyone with this book, read the first chapter.

Share this post


Link to post
Share on other sites
Switching shaders, and even worse, switching between fixed function and shaders, is really slow. I would try just rendering all geometry with your one shader, to see how fast it "can" go. It doesn't matter that it looks wrong -- you're just benchmarking.

Also: what graphics card are you using? Make sure you're using a card that actually supports shaders in hardware... That would mean a GeForce 5200 and up, or a Radeon 9500 and up. Ideally, a Radeon X series, or a GeForce 6 series or better, as they were iffy in the beginning (GeForces more so than Radeons).

Share this post


Link to post
Share on other sites
phantom: my first lines states I'm new to the stuff so I can only agree with you. As I haven't run into the word "clamp" in my reading I couldn't have guessed. :/ Now I do: pages 129, 133-134 of the orange book. :)

hplus0603:
omilla@d5201:~/SVN/GFX/branches/omilla/playground/terrain> glxinfo | grep renderer
OpenGL renderer string: GeForce FX 5200/AGP/SSE2



Share this post


Link to post
Share on other sites
alright, I changed my fragment to thhis:

uniform vec3 colorHeight;
varying float varHeight;

vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);
vec4 green = vec4(0.0, 1.0, 0.0, 1.0);
vec4 amber = vec4(1.0, 0.645, 0.0, 1.0);
vec4 red = vec4(1.0, 0.0, 0.0, 1.0);

void main(void)
{
gl_FragColor = red * step(colorHeight[2],varHeight) +
amber * step(colorHeight[1],varHeight) +
green * step(colorHeight[0],varHeight) +
blue * (1.0 - step(colorHeight[0],varHeight));
}

Now I fall to 33fps instead of 21. :)

Note that another way of doing the intended behaviour uses clipping and so 4 renderings with 4 different clips makes me drop to about 44fps. Maybe I can't get better than that (or I guess I could with VBOs but that's another story). :)

Share this post


Link to post
Share on other sites
w00t, been able to improve performance up to the clipping version with:

uniform vec3 colorHeight;
varying float varHeight;

void main(void)
{
gl_FragColor = vec4(step(colorHeight[1],varHeight) + step(colorHeight[2],varHeight),
step(colorHeight[0],varHeight) + 0.645 * step(colorHeight[1],varHeight),
1.0 - step(colorHeight[0],varHeight),
1.0);
}

man, shaders are SO sensitive, if I add a simple global variable declaration even without using it, I loose FPS! 0_o

Share this post


Link to post
Share on other sites
Quote:
Original post by mrbastard
it's probably all those conditionals.

Try rewriting it to use Clamp instead - more math, but easier to predict.


The conditionals were probably not the problem.
Depending on your video card, driver version, and shader compiler the resulting assembly they were most likely converted to "step" or something similar anyway.

Most hardware does not support actual branching so you are just doing the compilers work for it. Branching support is limited because of the way the GPU executes multiple calls to the shader in parallel.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement