Jump to content
  • Advertisement
Sign in to follow this  
GamerSg

OpenGL Conditional usage in GLSL

This topic is 4872 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

I know that the use of conditionals(which includes the use of for loops where i is a uniform) is considered bad, atleast for non SM3.0 hardware. But everywhere i see(including the Orange book and tutorials), the way the standard OpenGL fixed functionality is implemented in GLSL, there is heavy usage of if's. So does anyone have any efficient source code(GLSL) implementing the fixed functionality pipeline without conditional usage? The way i see it, determining if the vertex/fragment is facing the light, finding out if there is any specular, etc... requires the use of if statements. But im sure there is some way around it.

Share this post


Link to post
Share on other sites
Advertisement
It'd probably depend on what you're doing, but you can often 'disable' certain sections by deliberatly setting the values to 0 (eg. an all black specular colour has no effect). Similarly for front/back light facing based off of a dot product.

I seem to remember nVidia had a complete vertex and fragment shader which did all of the fixed-function pipeline, that might be a good starting point.

Share this post


Link to post
Share on other sites
If statements aren't as bad as you might think (they work fine on ancient hardware, like my GF2). What you can't do on older hardware is repeat some segment of code some varying number of times.


//Things you *can* do on older hardware:
if (...){
...
}

float i;
for (i = 0;i < 5;i++){
...
}
/*
The above for loop would become this:
... //first iteration
... //second...
... //third...
... //fourth...
... //last iteration
*/



//Things you can't do:
float i;
for (i = inData.pos.x;i < 100;i += 5){
...
}

Share this post


Link to post
Share on other sites
I have no idea if GLSL works on a GF2, but ARB vertex programs do, and I use Cg to compile my programs in to ARB vertex program code. Speaking of which, it was the Cg documentation that said what you can and can't use (at least I'm pretty sure that's what it was, since I didn't like reading the ARB vertex program specifications). ;)

Share this post


Link to post
Share on other sites
Vertex programs are emulated on GF2 cards, and performed entirely on the CPU, so obviously branches are a no-brainer then. Fragment shaders are right out on a GF2 (and GLSL fragment shaders aren't avalible on anything less than a GFFX).

Branches on cards which actually support shaders are not going to be fast. In particular lots of cards will just evaluate all of the instructions anyway (because the different pixel pipelines are all advancing together in lockstep).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!