Search Unity

[Compute Shader] Branching

Discussion in 'Shaders' started by Gizmoi, May 4, 2017.

  1. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I know that branching in shaders is generally bad, but Aras mentioned in this thread that as long as the branching is 'static' the GPU can branch effectively.
    So my understanding of static branching is that if the condition is constant for the entire 'Draw' / Dispatch of a shader, it is considered static. With that in mind, can anybody confirm that this psuedo code would conform to static branching and thus not incur a performance hit?

    Code (CSharp):
    1. void shader_kernel(uint3 id : SV_DispatchThreadID)
    2. {
    3.     float cumulative_complex_calculation = 0;
    4.     for (uint i = 0; i < len; ++i)
    5.     {
    6.         ShaderObject t = ObjectBuffer[i];
    7.  
    8.         if (t.Ignore == false)
    9.         {
    10.             // complex calculation
    11.             cumulative_complex_calculation++;
    12.         }
    13.     }
    14.  
    15.     Output[id] = value_from_complex_calculation;
    16. }
    This code will run id.x by id.y times (over a texture) and the ObjectBuffer does not change during a single dispatch.
     
  2. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    Nobody?

    @Aras , I know you're not on graphics anymore, but could you weigh in?