unity-game-enginehlslcompute-shader

Unity compute shader produces unexpected texture


I just started working with computer shaders in Unity Engine and right now I have only this method inside my compute shader:

int Iterations = 1000;

[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    [unroll(1000)] for (int i = 0; i < Iterations; i++)
    {
        Result[id.xy] = float4(1, 0, 0, 1.0);
        return;
    }
    Result[id.xy] = float4(0, 0, 1, 1.0);
}

I expected the output texture to be completely red but it is blue. However, as far as my understanding goes, that would mean that the for loop is not executed or that the return statement does not work. But both things don't seem to make sense to me, so can someone explain to me what is going on here?


Solution

  • Latest Hlsl compiler does not expose variables anymore (this is some leftover from old versions like fx framework).

    Unity also requires to expose your variables (you can set a default in there which should be taken, and eventually modified in a c# script).

    So it has to be set externally. Non initialized resources in DirectX (eg : not bound resources) generally produce a 0, so yes your loop is not executed at all.

    Please note that the compiler also completely removes the loop in your case (no matter the unroll value), since it detects that it will be ran at most once.

    so the bytecode generated by your shader looks like this equivalent :

    int Iterations = 1000;
    
    [numthreads(8,8,1)]
    void CSMain (uint3 id : SV_DispatchThreadID)
    {
        if (Iterations > 0)
        {
            Result[id.xy] = float4(1, 0, 0, 1.0);
        }
        else
        {
            Result[id.xy] = float4(0, 0, 1, 1.0);
        }     
    }