Avoid if statements in DirectX 10 shaders?

Posted by PolGraphic on Game Development See other posts from Game Development or by PolGraphic
Published on 2012-12-11T22:37:01Z Indexed on 2012/12/11 23:15 UTC
Read the original article Hit count: 372

Filed under:
|
|
|
|

I have heard that if statements should be avoid in shaders, because both parts of the statements will be execute, and than the wrong will be dropped (which harms the performance).

It's still a problem in DirectX 10? Somebody told me, that in it only the right branch will be execute.

For the illustration I have the code:

float y1 = 5; float y2 = 6; float b1 = 2; float b2 = 3;

if(x>0.5){
    x = 10 * y1 + b1;
}else{
    x = 10 * y2 + b2;
}

Is there an other way to make it faster?

If so, how do it?

Both branches looks similar, the only difference is the values of "constants" (y1, y2, b1, b2 are the same for all pixels in Pixel Shader).

© Game Development or respective owner

Related posts about c++

Related posts about directx