1

Supposing I need this function in a shader:

if (y < 1) return x/y;
else
{
    if (x < 0.5) return x;
    else if (x < y -0.5) return 0.5;
    else return x - y + 1;
}

This can be achieved without if branches, like:

return 
(x/y) +
max(0,y-1)/(y-1) *
(
    2 * (max(y - 0.5, x)-(y - 1)) * min(0.5, x)
    -(x/y)
);

Is this the second function really worth the trade?
Regarding performance. That seems a lot computation.
But they say if branching is no-go for fixed pipeline GPUs.

I would not ask if the second function would not involve computations that many.

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • @nicol-bolas Is it a tradeoff in this specific case? – Geri Borbás Feb 03 '17 at 05:47
  • Well, your "specific case" lacks specifics. And even if you did provide those specifics, it's unlikely that anyone could answer that without compiling it and seeing what the performance is like. – Nicol Bolas Feb 03 '17 at 06:02
  • Well I did it actually, tried with 3 different case (the middle was a hybrid), but did not experienced any (!!!) difference in the fill rate. Other may had similar experiences in the past, and could warn me something like: Hey dude, it is not that big deal! Your `if` statements would be just fine. – Geri Borbás Feb 03 '17 at 06:44
  • Inside a CG Shader on an iPad 3. – Geri Borbás Feb 03 '17 at 06:45

0 Answers0