• Advertisement
Sign in to follow this  

changing code on its roots

This topic is 1680 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 have this code

 

float Code(float a,float b, int c)

{

    float r=0;

 

      if( c==0)

      {

           r=a*b;

      }

     else if( c==1)

     {

           r=a/b;

     }

     else if( c==2)

     {

          r=2*a;

     }

 

     return r;

}

 

 

 

Lets say i don't have 3 different "if" but a thousand different "if", and "c" as it is in the code just selects one "if" 

 

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code

 

Or is there anything related to this that solves the problem in a different way?

Edited by lomateron

Share this post


Link to post
Share on other sites
Advertisement

A switch statement? It'll create a jump table to efficiently map code to execute to the value of c.

switch(c)
{
  case 0: r = a * b; break;
  case 1: r = a / b; break;
  case 2: r = 2 * a; break;
  // ...
}

Or you can directly return if you have nothing to after the conditional.

 

EDIT: damn, missed the HLSL tag, I don't think it has switch case support. In this case, does the value of c stay the same for every shader invocation? If so, you might be able to have the compiler optimize it down. Otherwise, if it changes dynamically, I'm afraid you're going to have to do the comparisons in any case.. are you looking for syntactic sugar or an actual performance improvement? GPU's aren't that good at control flow so any "clever" hacks to minimize the number of comparisons might yield even worse performance. If there's any way you can weave "c" directly into the formula that would be ideal of course, but it doesn't seem possible here.

Edited by Bacterius

Share this post


Link to post
Share on other sites

HLSL supports switches and in my case the "if"s will translate the same way a switch will do to assembly code

don't derail my real question

 

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

Share this post


Link to post
Share on other sites

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

 

Maybe shader permuation with compile defines?

#ifdef SOMETHING
r = a*b;
#else ifdef SOMETHINGELSE
r = a/b;
#else
r = 2*a;
#endif

Pardon me if I got the actual defines wrong. You can now employ a system, which is called "shader permumations", which will generate for each shader a set of possible compile-define combinations (in that case, a maximum of three). I don't know if this is too complicated/sofisticated for you, you might can up with an easier system based on that, but thats AFAIK the only way to fully eliminate the if-statements.

Edited by Juliean

Share this post


Link to post
Share on other sites

You can do it with somewhat less code modification like this, but you'll still need to recompile modified source for each shader:

#define c 42
 
float Code(float a,float b)
{
    // original logic stays here
}

Share this post


Link to post
Share on other sites

Dynamic linking can definitely be used to implement this, although it's a little wacky to use and will often generate sub-optimal code. Personally I would just do this by pre-compiling several permutations of the shader, with the value of c defined in a a preprocessor macro (similar to what Adam_42 suggests). Doing it this way allows the compiler to completely optimize away the if statement, and also any additional operations performed with the value of c. You can specify the macro definition when compiling the shader using the "pDefines" parameter of D3DCompile, and then just compile the shaders in a for loop.

Share this post


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

  • Advertisement