2

I've been messing around with HLSL and Direct3D12 and encountered something strange. Here's a minimal reproducible example:

float4 main() : SV_TARGET
{
    uint indices[3];
    indices[0] = 0;
    uint l = 0;

    [loop] while (true) {
        if (indices[l] == 1)
            return float4(1.0f, 0.0f, 0.0f, 1.0f);
        indices[l + 1] = 1;
        if (++l == 2)
            break;
    }
    return float4(0.0f, 1.0f, 0.0f, 1.0f);
}

The code is completely pointless, but at least in my eyes, there shouldn't be a problem with it, and it should return a red color. However, compiling and running this (with HLSL version 5_0, with and without optimizations) does strangely enough neither return red nor green; it doesn't seem to return anything at all, as even though I have all blending disabled, nothing is being drawn.

Changing seemingly meaningless things, like removing the [loop] tag or changing the size of indices to 2, gets rid of the strange behavior.

Does anyone know what's going on here? Are HLSL loops simply broken?

BubLblckZ
  • 434
  • 4
  • 14
  • 1
    Have you checked that `float4 main() : SV_TARGET { return float4(0.0f, 1.0f, 0.0f, 1.0f); }` actually works? – Tom Huntington Jul 04 '22 at 09:18
  • 1
    @TomHuntington Yep, it does. As mentioned, doing seemingly meaningless things like removing the [loop] tag gets rid of the strange behavior and returns a red color. As far as I know it shouldn't have anything to do with the CPU side. – BubLblckZ Jul 04 '22 at 09:40
  • do you use fxc or dxc? – mrvux Jul 19 '22 at 14:57
  • @mrvux Shader model 5_0, so fxc. – BubLblckZ Jul 21 '22 at 16:36
  • That's weird, I tried to copy your code and had the expected result. Can you post disassembly? Also which gpu are you using? – mrvux Jul 24 '22 at 11:30

0 Answers0