1

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?

4
  • it suggests then its skipping to the last line...... Commented Sep 14, 2023 at 19:16
  • I checked disassembly of your code, it seems you hit a compiler bug in that case (fxc tries to automatically reduce your loop since it understands you run it only once anyway, but seems the generated code does not make it return, so the write to blue is not taken away. Commented Sep 15, 2023 at 11:34
  • 1
    Actually you did not hit a bug, but in Unity you need to do a different setup to add a default to you Iterations variable (so this is likely ignored and you actually go in the loop 0 times), which means you produce blue Commented Sep 18, 2023 at 12:10
  • Yes thanks fpr your help, apparently I have ot initialize the variables in my c# script, otherwise they are all just initialized with 0 Commented Sep 18, 2023 at 13:36

1 Answer 1

0

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);
    }     
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.