GLSL branching behaviour

前端 未结 2 1838
说谎
说谎 2021-01-01 23:30

I have a rather simple fragment shader with a branch and I\'m a bit unsure how it is handled by the GLSL compiler and how it would affect performance.

unifor         


        
2条回答
  •  执念已碎
    2021-01-02 00:18

    Here you have it:

    il_ps_2_0
    dcl_input_generic_interp(linear) v1
    dcl_resource_id(0)_type(2d)_fmtx(float)_fmty(float)_fmtz(float)_fmtw(float)
    eq r2.xy__, c1.xyyy, c0.xyyy
    imul r5.x___, r2.x, r2.y
    mov r1.x___, r5.x
    if_logicalnz r1.x
        sample_resource(0)_sampler(0) r6, v1.xyyy
        mov r7, r6
    else
        sample_resource(0)_sampler(0) r8, v1.xyyy
        mov r7, r8
    endif
    mov r9, r7
    mov oC0, r9
    endmain
    

    To rephrase a bit what Kos said, what matters is to know if the guard condition can be known before execution. This is the case here since c1 and c0 registers are constant (constant registers start with letter 'c') and so is r1.x register value.

    That means this value is the same for all invocated fragment shaders, therefore no thread divergence can happen.

    Btw, I'm using AMD GPU ShaderAnalyser for transforming GLSL into the IL. You can also generate native GPU assembly code for a specific generation (ranging from HD29xx to HD58xx).This is really a good tool!

提交回复
热议问题