vkd3d/tests/hlsl/switch.shader_test
Giovanni Mascellani 51f13391e6 vkd3d-shader/ir: Introduce a simple control flow graph structurizer.
The structurizer is implemented along the lines of what is usually called
the "structured program theorem": the control flow is completely
virtualized by mean of an additional TEMP register which stores the
block index which is currently running. The whole program is then
converted to a huge switch construction enclosed in a loop, executing
at each iteration the appropriate block and updating the register
depending on block jump instruction.

The algorithm's generality is also its major weakness: it accepts any
input program, even if its CFG is not reducible, but the output
program lacks any useful convergence information. It satisfies the
letter of the SPIR-V requirements, but it is expected that it will
be very inefficient to run on a GPU (unless a downstream compiler is
able to devirtualize the control flow and do a proper convergence
analysis pass). The algorithm is however very simple, and good enough
to at least pass tests, enabling further development. A better
alternative is expected to be upstreamed incrementally.

Side note: the structured program theorem is often called the
Böhm-Jacopini theorem; Böhm and Jacopini did indeed prove a variation
of it, but their algorithm is different from what is commontly attributed
to them and implemented here, so I opted for not using their name.
2024-02-06 23:07:07 +01:00

588 lines
8.2 KiB
Plaintext

[require]
shader model >= 4.0
[pixel shader]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
return 3.0;
case 1:
return 4.0;
default:
return 5.0;
}
}
[test]
uniform 0 uint4 3 0 0 0
draw quad
probe all rgba (5.0, 5.0, 5.0, 5.0)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (4.0, 4.0, 4.0, 4.0)
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (3.0, 3.0, 3.0, 3.0)
% just a default case
[pixel shader]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
default:
return 5.0;
}
}
[test]
uniform 0 uint4 3 0 0 0
draw quad
probe all rgba (5.0, 5.0, 5.0, 5.0)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (5.0, 5.0, 5.0, 5.0)
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (5.0, 5.0, 5.0, 5.0)
% completely empty
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
}
}
% falling through is only supported for empty case statements
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 0:
case 1:
c.x += 0.1f;
break;
}
return c;
}
[test]
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (1.1, 2.0, 3.0, 4.0)
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (1.1, 2.0, 3.0, 4.0)
% case value evaluation
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 1+1:
c += 0.1f;
break;
case 0:
c += 0.2f;
break;
}
return c;
}
[test]
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (1.1, 2.1, 3.1, 4.1)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
% floats are accepted
[pixel shader fail(sm>=6)]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 2.1f:
c += 0.1f;
break;
case 0.9f:
c += 0.2f;
break;
}
return c;
}
[test]
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (1.1, 2.1, 3.1, 4.1)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
[pixel shader fail(sm>=6)]
float4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 2.1f:
c += 0.1f;
break;
case 0.9f:
c += 0.2f;
break;
}
return c;
}
[test]
uniform 0 float4 2.0 0.0 0.0 0.0
draw quad
probe all rgba (1.1, 2.1, 3.1, 4.1)
uniform 0 float4 1.0 0.0 0.0 0.0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
uint a = 1;
switch (v.x)
{
case 1+a:
c += 0.1f;
break;
case 0:
c += 0.2f;
break;
}
return c;
}
% duplicate cases
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 2:
c += 0.1f;
break;
case 1+1:
c += 0.2f;
break;
}
return c;
}
% multiple default cases
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
default:
case 2:
c += 0.1f;
break;
case 1:
c += 0.2f;
break;
default:
break;
}
return c;
}
% unterminated cases
[pixel shader fail(sm<6)]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 0:
c += 0.1f;
case 1:
c += 0.2f;
break;
}
return c;
}
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
return 3.0;
case 1:
return 4.0;
case 2:
}
return 0.0;
}
[pixel shader fail]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
return 3.0;
case 1:
return 4.0;
default:
}
return 0.0;
}
[pixel shader fail(sm<6)]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
return 3.0;
case 1:
return 4.0;
default:
discard;
}
return 0.0;
}
[pixel shader fail(sm<6)]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
return 3.0;
case 1:
return 4.0;
case 2:
discard;
}
return 0.0;
}
[pixel shader fail(sm<6)]
uint4 v;
float4 main() : sv_target
{
switch (v.x)
{
case 0:
discard;
case 1:
return 4.0;
}
return 0.0;
}
% more complicated breaks
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
switch (v.x)
{
case 2:
c += 0.1f;
if (true) break;
c = 9.0f;
case 1:
if (false) break;
c += 0.2f;
break;
default:
case 0:
break;
}
return c;
}
[test]
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (1.1, 2.1, 3.1, 4.1)
uniform 0 uint4 1 0 0 0
draw quad
probe all rgba (1.2, 2.2, 3.2, 4.2)
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
% switch breaks within a loop
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0f, 2.0f, 3.0f, 4.0f};
for (int i = 0; i < 4; ++i)
{
switch (v.x)
{
case 2:
c += 1.0f;
break;
case 1:
c -= 1.0f;
break;
default:
case 0:
break;
}
}
return c;
}
[test]
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (5.0, 6.0, 7.0, 8.0)
% default case placement
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0f, 2.0f, 3.0f, 4.0f};
switch (v.x)
{
case 2:
c += 1.0f;
break;
case 1:
c -= 1.0f;
break;
case 3:
default:
case 0:
c += 3.0f;
break;
}
return c;
}
[test]
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (4.0, 5.0, 6.0, 7.0)
uniform 0 uint4 2 0 0 0
draw quad
probe all rgba (2.0, 3.0, 4.0, 5.0)
uniform 0 uint4 3 0 0 0
draw quad
probe all rgba (4.0, 5.0, 6.0, 7.0)
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0f, 2.0f, 3.0f, 4.0f};
switch (v.x)
{
case 2:
c += 1.0f;
break;
case 1:
c -= 1.0f;
break;
case 3:
default:
break;
case 0:
c += 3.0f;
break;
}
return c;
}
[test]
uniform 0 uint4 3 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
uniform 0 uint4 0 0 0 0
draw quad
probe all rgba (4.0, 5.0, 6.0, 7.0)
uniform 0 uint4 5 0 0 0
draw quad
probe all rgba (1.0, 2.0, 3.0, 4.0)
% 'continue' is not supported in switches
[pixel shader fail(sm<6)]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
uint i, j;
for (i = 0; i < v.z; i++)
{
switch (v.x)
{
case 0:
c += 0.1f;
continue;
break;
case 1:
c += 0.2f;
break;
}
}
return c;
}
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
uint i, j;
for (i = 0; i < v.z; i++)
{
switch (v.x)
{
case 0:
for (j = 0; j < v.z; j++)
{
c += 1.0f;
if (v.w)
continue;
}
break;
case 1:
c += 2.0f;
break;
}
}
return c;
}
[test]
uniform 0 uint4 0 0 3 1
draw quad
probe all rgba (10.0, 11.0, 12.0, 13.0)
uniform 0 uint4 1 0 3 1
draw quad
probe all rgba (7.0, 8.0, 9.0, 10.0)
% return from a switch nested in a loop
[pixel shader]
uint4 v;
float4 main() : sv_target
{
float4 c = {1.0, 2.0, 3.0, 4.0};
uint i, j;
for (i = 0; i < v.z; i++)
{
switch (v.x)
{
case 0:
c += 1.0f;
break;
case 1:
c += 2.0f;
return c;
}
c += 100.0f;
}
return c;
}
[test]
uniform 0 uint4 0 0 3 1
draw quad
probe all rgba (304.0, 305.0, 306.0, 307.0)
uniform 0 uint4 1 0 3 1
draw quad
probe all rgba (3.0, 4.0, 5.0, 6.0)