1

Given that gpus benefit from being massively parallel, I have come to conclude that using code branches is an unreliable way of gaining performance in a shader because, in the worst case scenario, all the blocks will still be executed even though only the correct result will be used. Please correct me if my premise is incorrect, however.

I'm currently looking to gain some performance in relation to the sin function. Whether or not I can actually gain performance depends on what the original function actually does. If it's complicated and does a lot of work behind the scenes, then having a crude approximation (even with branching) may still be faster if the entire custom function adds up to less calculations than sin(). But if sin() can only be implemented with several lines of code, than I fear my efforts may be for naught. Again, please correct me if my premise is incorrect.

In other words, I'm asking if I may gain some performance using this shader function (it's using the syntax of the Unity Engine shading language). Note that high accuracy isn't needed - it's being used to make a simple sine wave effect for glowing stuff.

inline fixed CustomSine(fixed val) {
    //it is assumed that the input value is never negative

    fixed i = val * 57.2958;
    fixed ibak;
    fixed l;

    ibak = i;
    i /= 360;
    i = floor (i);
    i *= 360;
    i = ibak - i;

    if (i <= 90) {
        l = i / 90; 
    } else {
        if (i <= 180) {
            i -= 90;
            i = 90 - i;
            l = i / 90; 
        } else {
            if (i <= 270) {
                i -= 180;
                l = i / -90; 
            } else {
                i -= 270;
                i = 90 - i;
                l = i / -90; 
            }
        }
    }

    return l;
}
Neuron
  • 5,141
  • 5
  • 38
  • 59

0 Answers0