vkd3d-shader/hlsl: Flatten conditional branches containing stores.

For an if block

    if (cond)
    {
        <then_block>
    }
    else
    {
        <else_block>
    }

We flatten it by first replacing any store instruction `v[[k]] = x`
in the then_block with the following:

    1: load(v[[k]])
    2: cond ? x : @1
    3: v[[k]] = @2

Similarly, we replace any store instruction `v[[k]] = x` in the
else_block with the following:

    1: load(v[[k]])
    2: cond ? @1 : x
    3: v[[k]] = @2

Then we can concatenate <then_block> and <else_block> together and
get rid of the if block.
This commit is contained in:
Shaun Ren
2025-10-16 23:30:46 -04:00
committed by Henri Verbeet
parent 200e66ba4f
commit 4d5a1528ab
Notes: Henri Verbeet 2025-10-30 19:59:51 +01:00
Approved-by: Francisco Casas (@fcasas)
Approved-by: Elizabeth Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1732
10 changed files with 374 additions and 141 deletions

View File

@@ -32,7 +32,7 @@ float4 main() : sv_target
draw quad
probe (0, 0) rgba (0.2, 0.1, 0.8, 0.5);
[pixel shader todo(sm<4)]
[pixel shader]
uniform float f;
float func(out float o)
@@ -79,19 +79,19 @@ float4 main() : sv_target
[test]
uniform 0 float 0.1
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.2, 0.6, 0.3) 1
draw quad
probe (0, 0) f32(0.3, 0.2, 0.6, 0.3) 1
uniform 0 float 0.4
todo(sm<4) draw quad
probe (0, 0) rgba (0.6, 0.5, 0.6, 0.3) 1
draw quad
probe (0, 0) f32(0.6, 0.5, 0.6, 0.3) 1
uniform 0 float 0.6
todo(sm<4) draw quad
probe (0, 0) rgba (0.6, 0.5, 0.4, 0.5) 1
draw quad
probe (0, 0) f32(0.6, 0.5, 0.4, 0.5) 1
uniform 0 float 0.8
todo(sm<4) draw quad
probe (0, 0) rgba (0.8, 0.7, 0.4, 0.5) 1
draw quad
probe (0, 0) f32(0.8, 0.7, 0.4, 0.5) 1
[pixel shader todo(sm<4)]
[pixel shader]
uniform float f;
float func(out float o)
@@ -134,14 +134,14 @@ float4 main() : sv_target
[test]
uniform 0 float 0.1
todo(sm<4 | msl & sm>=6) draw quad
probe (0, 0) rgba (0.2, 0.1, 0.2, 0.1) 1
todo(msl & sm>=6) draw quad
probe (0, 0) f32(0.2, 0.1, 0.2, 0.1) 1
uniform 0 float 0.5
todo(sm<4 | msl & sm>=6) draw quad
probe (0, 0) rgba (0.5, 0.4, 1.0, 0.9) 1
todo(msl & sm>=6) draw quad
probe (0, 0) f32(0.5, 0.4, 1.0, 0.9) 1
uniform 0 float 0.9
todo(sm<4 | msl & sm>=6) draw quad
probe (0, 0) rgba (1.0, 0.9, 1.0, 0.6) 1
todo(msl & sm>=6) draw quad
probe (0, 0) f32(1.0, 0.9, 1.0, 0.6) 1
[pixel shader]
float func(out float o)
@@ -184,7 +184,7 @@ float4 main() : sv_target
draw quad
probe (0, 0) rgba (0.4, 0.3, 0.3, 0.9) 1
[pixel shader todo(sm<4)]
[pixel shader]
uniform float f;
float func(out float o)
@@ -235,26 +235,26 @@ float4 main() : sv_target
[test]
uniform 0 float 0.0
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.2, 0.3, 0.3) 1
draw quad
probe (0, 0) f32(0.3, 0.2, 0.3, 0.3) 1
uniform 0 float 0.1
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.3, 0.3, 0.3) 1
draw quad
probe (0, 0) f32(0.3, 0.3, 0.3, 0.3) 1
uniform 0 float 0.3
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.5, 0.3, 0.3) 1
draw quad
probe (0, 0) f32(0.3, 0.5, 0.3, 0.3) 1
uniform 0 float 0.7
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.9, 0.7, 0.6) 1
draw quad
probe (0, 0) f32(0.3, 0.9, 0.7, 0.6) 1
uniform 0 float 0.9
todo(sm<4) draw quad
probe (0, 0) rgba (0.4, 0.1, 0.7, 0.6) 1
draw quad
probe (0, 0) f32(0.4, 0.1, 0.7, 0.6) 1
[pixel shader todo(sm<4)]
[pixel shader]
uniform float4 f[3];
@@ -291,24 +291,24 @@ float4 main() : sv_target
uniform 0 float4 0.3 0.0 0.0 0.0
uniform 4 float4 0.0 0.0 0.0 0.0
uniform 8 float4 0.1 0.0 0.0 0.0
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.2, 0.6, 0.6) 1
draw quad
probe (0, 0) f32(0.3, 0.2, 0.6, 0.6) 1
uniform 4 float4 0.35 0.0 0.0 0.0
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.3, 0.6, 0.6) 1
draw quad
probe (0, 0) f32(0.3, 0.3, 0.6, 0.6) 1
uniform 8 float4 0.5 0.0 0.0 0.0
todo(sm<4) draw quad
draw quad
% Apparently fixed by Mesa commit 9785fa460c41b9498c24a82b98069655a91224c5,
% which only adds optimization; so it might just be hiding the bug rather than
% solving it; see also https://gitlab.freedesktop.org/mesa/mesa/-/issues/13092
bug(mesa<25.1 & llvm>=16) probe (0, 0) f32(0.3, 0.5, 0.6, 0.6) 1
uniform 0 float4 1.0 0.0 0.0 0.0
todo(sm<4) draw quad
probe (0, 0) rgba (0.3, 0.5, 0.6, 0.6) 1
draw quad
probe (0, 0) f32(0.3, 0.5, 0.6, 0.6) 1
uniform 4 float4 2.0 0.0 0.0 0.0
todo(sm<4) draw quad
probe (0, 0) rgba (0.4, 0.1, 0.6, 0.6) 1
draw quad
probe (0, 0) f32(0.4, 0.1, 0.6, 0.6) 1