Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/design.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@ only for `check.py`, the shaders read the tail from push constants the programs
(`LEONIDA_MESH_COMPUTE=1`) isn't combined with it yet: its vertex shader still reads the output address from push
constants, so those tests skip.

## Mesh shaders with their waves in series

`LEONIDA_MESH_SERIAL_WAVES=1` for `run_gpu_tests.py`, together with `LEONIDA_W64_EMULATE=1`, translates the mesh
shaders with `--mesh-serial-waves` (`docs/design.md`, "Mesh shaders with their waves in series"): the ones that
qualify run one pass per wave in 64 invocations. The `ms_*` tests must pass with and without it;
`ms_w64x_wave_votes` is the one whose two waves take different branches and both give the triangle a vertex. `ms_w64x_wave_loops` is the one whose two waves run a loop of different length (5 and 9 iterations), each iteration reading a wave-wide count, so under emulation the waves reach different numbers of barriers.

## GPUs without mesh shaders (NVIDIA Pascal and older)

Without `VK_EXT_mesh_shader`, the `ms_*` tests are skipped; that's expected, not a failure. To test the path that
Expand Down
63 changes: 63 additions & 0 deletions runtime/test/shaders/ms_w64x_wave_loops.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
; Mesh shader (NGG, wave64, 128 threads = 2 waves): each wave runs a loop whose every iteration reads a wave-wide
; count, and the two waves leave it after a different number of iterations.
; limit of a lane = (lane & 3) + 1 in wave 0, (lane & 7) + 1 in wave 1.
; each iteration counts the lanes whose limit is above the iteration number (s_bcnt1 of a 64-lane compare), adds
; the count to a sum and goes round again while the count is not 0.
; wave 0: counts 64, 48, 32, 16, 0: 5 iterations, sum 160. wave 1: 64, 56, ..., 8, 0: 9 iterations, sum 288.
; A wave whose sum is right sets the blue channel of param0 to 0.5 in the vertices it exports, else 0.
; The triangle is ms_tri's, with its vertices from threads 0 and 1 (wave 0) and thread 64 (wave 1), as in
; ms_w64x_wave_votes: the picture is ms_tri's only when both waves ran their own number of iterations with their
; own lanes. Under --wave64-emulate every count is an exchange with a workgroup barrier inside the loop, so the
; waves reach different numbers of barriers; with --mesh-serial-waves the two loops run one after the other.
; Only wave 0 sends the mesh allocation (1 primitive, 65 vertices: indices 0, 1 and 64 are used).
; Needs a DXBC with PSV0 (128 threads, 128/64 outputs, triangles; see scripts/run_gpu_tests.py):
; VKH_MESH=1 vk_harness ms_w64x_wave_loops.spv fs_passthru.spv out.raw
; -> every pixel = ((x+0.5)/64, (y+0.5)/64, 0.5, 1), identical to ms_tri.
; mattr: +wavefrontsize64,-wavefrontsize32
v_mbcnt_lo_u32_b32 v0, -1, 0
v_mbcnt_hi_u32_b32 v0, -1, v0
v_mov_b32_e32 v11, v0
s_bfe_u32 s4, s3, 0x40018
v_lshl_add_u32 v0, s4, 6, v0
s_mov_b32 s14, 288
s_cmp_eq_u32 s4, 0
s_cselect_b32 s5, 3, 7
s_cselect_b32 s14, 160, s14
v_and_b32_e32 v13, s5, v11
v_add_nc_u32_e32 v13, 1, v13
s_mov_b32 s20, 0
s_mov_b32 s22, 0
v_mov_b32_e32 v20, s20
v_cmp_lt_u32_e64 s[6:7], v20, v13
s_bcnt1_i32_b64 s10, s[6:7]
s_add_u32 s22, s22, s10
s_add_u32 s20, s20, 1
s_cmp_lg_u32 s10, 0
s_cbranch_scc1 65528
s_cmp_eq_u32 s22, s14
s_cselect_b32 s13, 1, 0
v_cvt_f32_u32_e32 v15, s13
v_mul_f32_e32 v15, 0.5, v15
v_cmp_eq_u32_e32 vcc, 1, v0
v_cndmask_b32_e64 v1, -1.0, 0x40400000, vcc
v_cndmask_b32_e64 v3, 0, 2.0, vcc
v_cmp_eq_u32_e32 vcc, 64, v0
v_cndmask_b32_e64 v2, -1.0, 0x40400000, vcc
v_cndmask_b32_e64 v4, 0, 2.0, vcc
v_mov_b32_e32 v6, 0
v_mov_b32_e32 v7, 1.0
v_mov_b32_e32 v8, v15
s_mov_b32 m0, 0x1041
s_cmp_lg_u32 s4, 0
s_cbranch_scc1 1
s_sendmsg sendmsg(MSG_GS_ALLOC_REQ)
v_cmp_gt_u32_e64 s[16:17], 2, v0
v_cmp_eq_u32_e64 s[18:19], 64, v0
s_or_b64 exec, s[16:17], s[18:19]
exp pos0 v1, v2, v6, v7 done
exp param0 v3, v4, v8, v7
s_mov_b64 exec, -1
v_cmpx_eq_u32_e32 0, v0
v_mov_b32_e32 v9, 0x4000400
exp prim v9, off, off, off done
s_endpgm
57 changes: 57 additions & 0 deletions runtime/test/shaders/ms_w64x_wave_votes.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
; Mesh shader (NGG, wave64, 128 threads = 2 waves): the two waves take different branches, each with its own
; wave-wide reads, and each wave contributes vertices to the one triangle.
; wave 0: ballot of (lane & 7) == 5, s_bcnt1 must be 8.
; wave 1: ballot of lane < 3, s_bcnt1 must be 3, plus v_readfirstlane of the lane number, which must be 0.
; A wave whose count is right sets the blue channel of param0 to 0.5 in the vertices it exports, else 0.
; The triangle is ms_tri's, but its vertices come from threads 0 and 1 (wave 0) and thread 64 (wave 1), so the
; picture is ms_tri's only when both waves ran their own branch with their own votes: a wave that saw the other
; wave's ballot, or ran the other branch, gets a different count. Only wave 0 sends the mesh allocation
; (1 primitive, 65 vertices: indices 0, 1 and 64 are used).
; Needs a DXBC with PSV0 (128 threads, 128/64 outputs, triangles; see scripts/run_gpu_tests.py):
; VKH_MESH=1 vk_harness ms_w64x_wave_votes.spv fs_passthru.spv out.raw
; -> every pixel = ((x+0.5)/64, (y+0.5)/64, 0.5, 1), identical to ms_tri.
; mattr: +wavefrontsize64,-wavefrontsize32
v_mbcnt_lo_u32_b32 v0, -1, 0
v_mbcnt_hi_u32_b32 v0, -1, v0
v_mov_b32_e32 v11, v0
s_bfe_u32 s4, s3, 0x40018
v_lshl_add_u32 v0, s4, 6, v0
s_cmp_eq_u32 s4, 0
s_cbranch_scc0 6
v_and_b32_e32 v12, 7, v11
v_cmp_eq_u32_e64 s[6:7], 5, v12
s_bcnt1_i32_b64 s10, s[6:7]
s_mov_b32 s14, 8
s_branch 6
v_cmp_gt_u32_e64 s[6:7], 3, v11
s_bcnt1_i32_b64 s10, s[6:7]
v_readfirstlane_b32 s15, v11
s_add_u32 s10, s10, s15
s_mov_b32 s14, 3
s_cmp_eq_u32 s10, s14
s_cselect_b32 s13, 1, 0
v_cvt_f32_u32_e32 v15, s13
v_mul_f32_e32 v15, 0.5, v15
v_cmp_eq_u32_e32 vcc, 1, v0
v_cndmask_b32_e64 v1, -1.0, 0x40400000, vcc
v_cndmask_b32_e64 v3, 0, 2.0, vcc
v_cmp_eq_u32_e32 vcc, 64, v0
v_cndmask_b32_e64 v2, -1.0, 0x40400000, vcc
v_cndmask_b32_e64 v4, 0, 2.0, vcc
v_mov_b32_e32 v6, 0
v_mov_b32_e32 v7, 1.0
v_mov_b32_e32 v8, v15
s_mov_b32 m0, 0x1041
s_cmp_lg_u32 s4, 0
s_cbranch_scc1 1
s_sendmsg sendmsg(MSG_GS_ALLOC_REQ)
v_cmp_gt_u32_e64 s[16:17], 2, v0
v_cmp_eq_u32_e64 s[18:19], 64, v0
s_or_b64 exec, s[16:17], s[18:19]
exp pos0 v1, v2, v6, v7 done
exp param0 v3, v4, v8, v7
s_mov_b64 exec, -1
v_cmpx_eq_u32_e32 0, v0
v_mov_b32_e32 v9, 0x4000400
exp prim v9, off, off, off done
s_endpgm
8 changes: 6 additions & 2 deletions scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@ def run_suite(a, work: str) -> int:
])
elif name in ("ms_tri", "ms_w64x_ballot", "ms_wave_info_s3", "ms_f16_pack"):
dxbc = make_dxbc([(b"PSV0", make_ms_psv0(128, 64, 64, 2))])
elif name in ("ms_w64x_wave_votes", "ms_w64x_wave_loops"): # vertex 64 comes from the second wave: 128 output vertices
dxbc = make_dxbc([(b"PSV0", make_ms_psv0(128, 128, 64, 2))])
elif name == "ms_v5_workgroup":
dxbc = make_dxbc([(b"PSV0", make_ms_psv0(32, 3, 1, group_shared=128))])
elif name == "ms_clip":
Expand Down Expand Up @@ -945,6 +947,8 @@ def run_suite(a, work: str) -> int:
cmd.append("--texel-offset-fallback")
if MESH_COMPUTE: # mesh shaders as compute writing to a buffer, drawn by tools/mesh_compute_vs.py
cmd.append("--mesh-compute")
if os.environ.get("LEONIDA_MESH_SERIAL_WAVES") == "1": # with --wave64-emulate: one pass per wave
cmd.append("--mesh-serial-waves")
if pc_tail_ubo(): # 128 push-constant bytes; heap base and code base from a uniform block at set 1
cmd.append("--pc-tail-ubo")
if os.environ.get("LEONIDA_FMA") in ("glsl", "khr", "exact"): # GLSL Fma, OpFmaKHR or emulated (docs/design.md)
Expand Down Expand Up @@ -1632,13 +1636,13 @@ def run_suite(a, work: str) -> int:
problem = mesh_v5_test(a.build, spirv_dir, Path(raw_dir) / f"{name}.raw")
results.append((name, None if isinstance(problem, Skip) else not problem,
problem or "24/24 workgroup coordinates exact (2 x 3 x 4)"))
elif name in ("ms_tri", "ms_w64x_ballot", "ms_wave_info_s3"):
elif name in ("ms_tri", "ms_w64x_ballot", "ms_wave_info_s3", "ms_w64x_wave_votes", "ms_w64x_wave_loops"):
raw = os.path.join(raw_dir, f"{name}.raw")
env = mesh_env(spirv_dir, name, gfx_env)
rc, msg = run_readback(raw, run_cmd, [vk_harness, spv, fs_passthru, raw], env=env)
# ms_w64x_ballot needs exact wave64 (a 64-wide subgroup, or --wave64-emulate): skip it elsewhere, as
# the wave64 compute dumps are.
no64 = (name == "ms_w64x_ballot" and not w64_emulated() and
no64 = (name in ("ms_w64x_ballot", "ms_w64x_wave_votes", "ms_w64x_wave_loops") and not w64_emulated() and
not any(int(x) >= 64 for x in re.findall(r"subgroup \d+\.\.(\d+)", msg)))
if rc == UNSUPPORTED:
results.append((name, None, unsupported_skip(msg)))
Expand Down
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def mesh_psv0_chunk(threads=(128, 1, 1), max_vertices=64, max_primitives=64, top
SAMPLE_DXBC = {"gs_tri": GS_TRI_DXBC, "ms_tri": MS_TRI_DXBC, "ms_clip": MS_CLIP_DXBC, "ms_wave_info_s3": MS_TRI_DXBC,
"ms_w64x_ballot": MS_TRI_DXBC, "ms_f16_pack": MS_TRI_DXBC, # scripts/run_gpu_tests.py, ms_tri's PSV0
"gs_w64_a_ring": GS_TRI_DXBC, "gs_w64_c_lane_id": GS_TRI_DXBC, # build.py gives every gs_* gs_tri's
# vertex 64 comes from the second wave: 128 output vertices (scripts/run_gpu_tests.py)
"ms_w64x_wave_votes": make_dxbc([mesh_psv0_chunk(max_vertices=128)]),
"ms_w64x_wave_loops": make_dxbc([mesh_psv0_chunk(max_vertices=128)]),
"ms_v5_workgroup": make_dxbc([mesh_psv0_chunk(threads=(32, 1, 1), max_vertices=3,
max_primitives=1, group_shared=128)])}

Expand Down
138 changes: 138 additions & 0 deletions tests/test_mesh_serial_waves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""rdna2spirv.py --mesh-serial-waves: a mesh shader's waves one after the other in 64 invocations.
Under --wave64-emulate a 128-thread mesh shader is two waves side by side in one workgroup, and what the waves do
not do alike goes through mini-schedulers. With --mesh-serial-waves a program whose waves cannot pass each other
anything (no LDS, no store or atomic to memory) declares 64 invocations and runs one pass per wave, each pass a
single wave. The inputs are the hand-written GPU tests runtime/test/shaders/ms_*.asm with conftest's synthetic
PSV0 containers (no game data); on the GPU, scripts/run_gpu_tests.py with LEONIDA_W64_EMULATE=1 and
LEONIDA_MESH_SERIAL_WAVES=1 draws them. Here:
* a program that qualifies declares LocalSize 64, loops over the passes (and, when it has branches, has no
OpSwitch left: one wave gets the inline plan), takes the invocation's index from the built-in plus 64 * pass, keeps one copy of the export variables per pass and sets
the mesh outputs once; it passes spirv-val; the metadata records the number of passes and keeps the program's
thread count;
* the exchange takes its place in %x_buf from the invocation's own index, not from the lane it runs;
* ms_wave_info_s3, whose waves count lanes in LDS, keeps the side-by-side form, and so does every program
without --wave64-emulate, under --mesh-compute or of at most 64 threads;
* without the flag the module is unchanged.
"""
import os
import re
import sys

import pytest

from conftest import ROOT, SAMPLE_DXBC, requires_tools

sys.path.insert(0, os.path.join(ROOT, "tools"))
import rdna2spirv # noqa: E402

SERIAL = ("ms_tri", "ms_clip", "ms_f16_pack", "ms_w64x_ballot", "ms_w64x_wave_votes", "ms_w64x_wave_loops")
SIDE_BY_SIDE = ("ms_wave_info_s3", # LDS shared by the waves
"ms_v5_workgroup") # 32 threads: one partial wave


def _translate(make_shader, name, serial, emulate=True, compute=False):
with open(os.path.join(ROOT, "runtime", "test", "shaders", name + ".asm"), encoding="utf-8") as f:
shader = make_shader(f.read(), name, dxbc=SAMPLE_DXBC[name])
old = rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE
rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE = serial, emulate, compute
try:
spvasm = shader.translate()
meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spvasm)
finally:
rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE = old
return shader, spvasm, meta


@requires_tools
@pytest.mark.parametrize("name", SERIAL)
def test_serial_module(make_shader, name):
shader, spvasm, meta = _translate(make_shader, name, True)
shader.validate(spvasm)
assert "OpExecutionMode %main LocalSize 64 1 1" in spvasm
assert meta["mesh_serial_waves"] == 2 and meta["local_size"] == [128, 1, 1]
if name == "ms_w64x_wave_votes": # a program with branches: one wave gets the inline plan, no scheduler
assert "OpSwitch" not in spvasm # (a straight-line program keeps the round loop over its segments)
assert spvasm.count("OpLoopMerge %msw_exit %msw_cont None") == 1
# The built-in is only read to make the pass's index and the exchange's own place.
assert "OpDecorate %real_local_index BuiltIn LocalInvocationIndex" in spvasm
assert "%local_index = OpVariable %_ptr_Function_uint Function" in spvasm
assert re.search(r"%msw_li = OpIAdd %uint %msw_rl %msw_off\s+OpStore %local_index %msw_li", spvasm)
assert "%x_li = OpLoad %uint %real_local_index" in spvasm
assert "%x_li = OpLoad %uint %local_index" not in spvasm
# One copy of what a pass exported per pass, one OpSetMeshOutputsEXT, one write block per pass.
for w in (0, 1):
assert f"OpCopyMemory %ms_pos__w{w} %ms_pos" in spvasm
assert f"OpLoad %v4float %ms_pos__w{w}" in spvasm
assert f"%mli_w{w} = OpIAdd %uint %msw_erl" in spvasm
assert spvasm.count("OpSetMeshOutputsEXT") == 1
assert spvasm.index("%msw_exit = OpLabel") < spvasm.index("OpSetMeshOutputsEXT")


@requires_tools
@pytest.mark.parametrize("name", SERIAL)
def test_initialised_variables_are_set_again_each_pass(make_shader, name):
_, spvasm, _ = _translate(make_shader, name, True)
body = spvasm[spvasm.index("%msw_body = OpLabel"):]
variables = re.findall(r"^\s*(%\w+) = OpVariable \S+ Function (\S+)$", spvasm, re.M)
assert variables
for var, init in variables:
if var in ("%local_index", "%msw_pass") or "__w" in var:
continue # the pass counter, the index it sets, and the per-pass copies
assert re.search(rf"OpStore {re.escape(var)} {re.escape(init)}\n", body), var


@requires_tools
@pytest.mark.parametrize("name", SIDE_BY_SIDE)
def test_program_that_does_not_qualify_is_unchanged(make_shader, name):
_, plain, plain_meta = _translate(make_shader, name, False)
_, flagged, meta = _translate(make_shader, name, True)
assert flagged == plain and meta == plain_meta
assert "mesh_serial_waves" not in meta and "%msw_" not in flagged


@requires_tools
@pytest.mark.parametrize("emulate,compute", [(False, False), (True, True)], ids=["native", "mesh-compute"])
def test_flag_needs_emulation_and_the_mesh_path(make_shader, emulate, compute):
_, plain, _ = _translate(make_shader, "ms_tri", False, emulate, compute)
_, flagged, meta = _translate(make_shader, "ms_tri", True, emulate, compute)
assert flagged == plain and "mesh_serial_waves" not in meta


@requires_tools
@pytest.mark.parametrize("name", SERIAL)
def test_without_the_flag_the_waves_are_side_by_side(make_shader, name):
shader, spvasm, meta = _translate(make_shader, name, False)
shader.validate(spvasm)
assert "OpExecutionMode %main LocalSize 128 1 1" in spvasm
assert "%msw_" not in spvasm and "mesh_serial_waves" not in meta
assert "OpDecorate %local_index BuiltIn LocalInvocationIndex" in spvasm


@requires_tools
def test_a_wave_that_ends_early_does_not_skip_the_later_passes(make_shader):
# ms_w64x_wave_votes with an s_endpgm in the middle of the program, behind a branch on the wave's number (7,
# so never taken here): the end of one wave's program must end its pass, not the invocation.
name = "ms_w64x_wave_votes"
with open(os.path.join(ROOT, "runtime", "test", "shaders", name + ".asm"), encoding="utf-8") as f:
text = f.read()
at = "s_bfe_u32 s4, s3, 0x40018\n"
assert text.count(at) == 1
text = text.replace(at, at + "s_cmp_eq_u32 s4, 7\ns_cbranch_scc0 1\ns_endpgm\n")
shader = make_shader(text, name, dxbc=SAMPLE_DXBC[name])
old = rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE
rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE = True, True
try:
spvasm = shader.translate()
meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spvasm)
finally:
rdna2spirv.MESH_SERIAL_WAVES, rdna2spirv.WAVE64_EMULATE = old
shader.validate(spvasm)
assert meta["mesh_serial_waves"] == 2
assert spvasm != _translate(make_shader, name, True)[1] # the early end is in the module
assert "OpExecutionMode %main LocalSize 64 1 1" in spvasm
main = spvasm[spvasm.index("%main = OpFunction"):]
main = main[:main.index("OpFunctionEnd")]
# One return, after the loop over the passes and after the outputs are written.
assert main.count("OpReturn") == 1 and "OpKill" not in main and "OpTerminateInvocation" not in main
assert main.index("%msw_exit = OpLabel") < main.index("OpSetMeshOutputsEXT") < main.index("OpReturn")
Loading
Loading