Skip to content
Open
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
10 changes: 9 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,21 @@ the game's present entry point (`PresentX`; the DXGI factory/swapchain and the p
152b9cde, `src/amd/vulkan/radv_physical_device.c`, `requiredSubgroupSizeStages`; older Mesa reported compute only).
NVIDIA lists every stage but only size 32, so a native wave64 shader cannot get 64 there. Vertex and geometry
stages are in neither list.
- **Wave32 vertex shaders** (`wave32_halves` in the index): the AMD Windows driver runs the vertex stage
64-wide (measured on an RX 7600, 26.11.2: 62 to 64 vertices per subgroup, sizes 32..64, VERTEX not requirable),
so a wave32 VS shared one subgroup with a second wave's worth of vertices: lanes 32..63 reported lane 32,
`v_readfirstlane`, ballots, votes and `s_bcnt1` covered 64 vertices, `ds_bpermute` read the other half and a
write to `exec_lo` did not reach lanes 32..63. The translator now keeps each wave in its own 32 subgroup lanes
(docs/design.md, "Wave32 vertex shaders on wider subgroups"), and the
decision is `halves` there: nothing is required and nothing is logged. A device that can run the stage below
32 lanes still gets `unavailable` (or `device_default` when its default is 32).
- **A size the device cannot give** is logged once per stage with the first shader's name (and again at 10, 100,
1000 shaders of that stage): `[rt] d3d12: <name> (<stage>) is a 64-wide shader but this device cannot require that
subgroup size ...`. For compute and mesh the fix is the build, `--wave64-emulate` (`make_game.py
--wave64-emulate auto` picks it for this GPU); a wave64 vertex, geometry or pixel shader has no emulation yet
(the ~10,000 wave64 pixel shaders are the open case on NVIDIA, SHADER-10).
- **Test:** `subgroup_size_test` (CPU) checks the decision against AMD-like, NVIDIA-like and old-RADV
properties and the resulting create-info chain; `d3d12_subgroup_test` (GPU) runs `cs_w64x_readlane` natively
properties (and a halves VS on each, and on a device with sizes below 32) and the resulting create-info chain; `d3d12_subgroup_test` (GPU) runs `cs_w64x_readlane` natively
and with `--wave64-emulate`, and `cs_wqm_w32`, through D3D12 PSOs and compares the read-back words.

Known gaps: set 0 in pipelines (the device keeps the bindless set, but no pipeline declares or binds it yet) and
Expand Down
3 changes: 2 additions & 1 deletion docs/design.md

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion make_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,8 @@ def fspx_key_clash(spirv: str):
# The translator's fallback flags make_game.py can forward, and the argparse attribute each one reads.
FALLBACK_FLAGS = (("--no-float-controls", "no_float_controls"), ("--bary-fallback", "bary_fallback"),
("--texel-offset-fallback", "texel_offset_fallback"), ("--mesh-compute", "mesh_compute"),
("--f16-interp-fallback", "f16_interp_fallback"), ("--pc-tail-ubo", "pc_tail_ubo"))
("--f16-interp-fallback", "f16_interp_fallback"), ("--pc-tail-ubo", "pc_tail_ubo"),
("--no-wave32-halves", "no_wave32_halves"))


def fallback_flags(a) -> tuple[str, ...]:
Expand Down Expand Up @@ -1232,6 +1233,18 @@ def runtime_gpu_per_vertex():
return None


VK_DRIVER_ID_MOLTENVK = 14


def wave32_halves_fallback(info: dict) -> str | None:
"""Reason to use the old VS lowering on the selected GPU, if known."""
if info.get("driver_id") == VK_DRIVER_ID_MOLTENVK:
return "MoltenVK vertex functions cannot read the subgroup lane"
if info.get("vertex_ballot") is False:
return "the device does not support subgroup ballots in vertex shaders"
return None


def runtime_gpu_max_push_constants():
"""max_push_constants_size as tools/vk_subgroup_info.py answers for the GPU the runtime would use, or None."""
v = runtime_gpu_info().get("max_push_constants_size")
Expand Down Expand Up @@ -1839,6 +1852,9 @@ def main() -> int:
ap.add_argument("--mesh-compute", action="store_true",
help="translate mesh shaders to compute shaders writing an output buffer, for devices "
"without VK_EXT_mesh_shader")
ap.add_argument("--no-wave32-halves", action="store_true",
help="translate wave32 vertex shaders as one wave per subgroup instead of one per 32 subgroup "
"lanes; selected automatically on MoltenVK and devices without vertex subgroup ballots")
ap.add_argument("--pc-tail-ubo", action="store_true",
help="keep s0..s31 in 128 push-constant bytes and move heap/code base to a set-1 uniform "
"block; on by itself when the runtime's GPU reports maxPushConstantsSize below what "
Expand Down Expand Up @@ -1915,6 +1931,11 @@ def main() -> int:
log("shaders: the GPU the runtime would use can't take PerVertexKHR inputs (MoltenVK, or no "
"fragmentShaderBarycentric): translating with --f16-interp-fallback")
a.f16_interp_fallback = True
if "shaders" in stages and not a.no_wave32_halves:
reason = wave32_halves_fallback(runtime_gpu_info())
if reason:
log(f"shaders: {reason}; translating with --no-wave32-halves")
a.no_wave32_halves = True
if "shaders" in stages and not a.pc_tail_ubo:
need_pc = 152 if a.mesh_compute else 144
max_pc = runtime_gpu_max_push_constants()
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,10 @@ struct ShaderEntry {
// subgroup size. Empty for a native translation.
std::string wave64_exchange;
bool emulated() const { return !wave64_exchange.empty(); }
// A wave32 vertex shader translated with one wave per 32 subgroup lanes (rdna2spirv.py Gen.halves): exact on any
// subgroup size that is a multiple of 32, so a 64-wide vertex stage (the AMD Windows driver) runs it as two
// wave32s. False for one translated with --no-wave32-halves or before the field existed.
bool wave32_halves = false;
std::vector<SignatureElement> input_signature; // vertex shaders: what the input layout must feed
std::vector<std::uint32_t> inputs; // pixel shaders: input locations declared
std::vector<std::uint32_t> outputs; // pixel/vertex shaders: output locations written
Expand Down Expand Up @@ -788,6 +792,8 @@ class ShaderLibrary {
e.subgroup_size = u32(g);
if (const Json* x = meta.find("wave64_exchange"); x && x->kind == Json::Kind::string)
e.wave64_exchange = x->string;
if (const Json* h = meta.find("wave32_halves"); h && h->kind == Json::Kind::boolean)
e.wave32_halves = h->boolean;
if (const Json* s = meta.find("input_signature"); s && s->kind == Json::Kind::array) {
for (const Json& item : s->array) {
SignatureElement element;
Expand Down
9 changes: 9 additions & 0 deletions runtime/src/subgroup_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ enum class SubgroupOutcome {
device_fixed, // the device has one size and it is the wanted one: nothing to ask for
device_default, // cannot be required, but the default size is the wanted one: works, not guaranteed stable
unavailable, // the wanted size cannot be had: the shader runs at another size and may be inexact
halves, // wave32 with `wave32_halves`: the size cannot be had, but every size the device runs is a
// multiple of 32 and the shader keeps each wave in its own 32 lanes
unknown_stage, // no Vulkan stage for this entry
unknown_device, // the device sizes were not queried (all zero): nothing is asked for
};
Expand Down Expand Up @@ -103,6 +105,10 @@ inline bool subgroup_workgroup_fits(const std::array<std::uint32_t, 3>& local_si
// - A device whose one and only subgroup size (min == max) is the wanted one needs nothing (`device_fixed`).
// - Where the size cannot be required (stage not listed, feature off) but the device default is the wanted size,
// the shader works as it is (`device_default`): nothing guarantees it, a driver may change its default.
// - A wave32 entry with `wave32_halves` (a vertex shader) partitions a wide subgroup into 32-lane waves, so where neither
// holds but the device's smallest size (minSubgroupSize, else the default) is at least 32 the outcome is
// `halves`: no width to request. The tested RX 7600 Windows driver (vertex stage 64-wide, VERTEX not
// requirable) is that case.
// Otherwise the outcome is `unavailable`.
inline SubgroupRequest decide_subgroup_size(const ShaderEntry& entry, const SubgroupSizeCaps& caps) {
SubgroupRequest r;
Expand Down Expand Up @@ -136,6 +142,9 @@ inline SubgroupRequest decide_subgroup_size(const ShaderEntry& entry, const Subg
r.flags |= VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT;
} else if (caps.default_size == r.wanted) {
r.outcome = SubgroupOutcome::device_default;
} else if (entry.wave32_halves && !entry.wave64 && r.wanted == 32 &&
(caps.min_size ? caps.min_size : caps.default_size) >= 32) {
r.outcome = SubgroupOutcome::halves;
} else {
r.outcome = SubgroupOutcome::unavailable;
}
Expand Down
12 changes: 12 additions & 0 deletions runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from pathlib import Path

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE) # this script is run with python -I too
import w32_vs_halves # noqa: E402 (the wave32 VS fixtures' invariant checks)
W32_VS_FIXTURES = w32_vs_halves.FIXTURES # vs_w32_*: judged against wave32 invariants, not fixed words
EXE = ".exe" if os.name == "nt" else ""
UNSUPPORTED = 77 # a test tool exits with this when the device lacks a feature

Expand Down Expand Up @@ -1310,6 +1313,13 @@ def interp_f16_high_problem(px):
return "" if err < 2e-3 else f"f16 halves off by {err:.3g}"


def w32_vs_halves_test(tools, spirv, name):
"""A vs_w32_* fixture: a wave32 VS on a vertex subgroup that may be wider than 32 lanes, drawn with 960 vertices
and checked against what every wave32 satisfies, whatever packing the driver chose (w32_vs_halves.py)."""
problem = w32_vs_halves.check(tools, spirv, name)
return Skip(problem) if problem.startswith("SKIP") else problem


def base_ids_test(tools, spirv):
"""vs_base_ids with ps_flat, drawn with firstVertex 16 and firstInstance 7: the VS input VGPRs are zero-based on
the hardware, so every pixel must still see vertex 0 of instance 0 (0x101, 0x102), not 0x7111."""
Expand Down Expand Up @@ -1560,6 +1570,8 @@ def main():
for name in TEXTURE_CONSTANTS:
results.append((name, texture_constant_test(tools, spirv, name)))
results.append(("vs_base_ids", base_ids_test(tools, spirv)))
for name in W32_VS_FIXTURES:
results.append((name, w32_vs_halves_test(tools, spirv, name)))
results.append(("ps_tex", bindless_test(tools, spirv)))
results.append(("ps_ssharp", sampler_test(tools, spirv)))
results.append(("ps_imgatomic", bindless_test(tools, spirv, "ps_imgatomic")))
Expand Down
32 changes: 32 additions & 0 deletions runtime/test/shaders/vs_w32_ballot.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; Wave32 vertex shader on a 64-wide vertex subgroup (w32_vs_halves.py): a ballot read as a whole. VCC = the lanes
; whose vertex has v % 4 == 1 (v0 = vertex index). Record A at heap 0x1000 + 16 * v, record B at 0x5000 + 16 * v:
; A = (lane, rank, key, v) lane = v_mbcnt_lo of -1, rank = v_mbcnt_lo of VCC (selected lanes below this one,
; the slot a stream compaction would give), key = v_readfirstlane of v0
; B = (count, first, sel, 0) count = s_bcnt1 of vcc_lo, first = s_ff1 of vcc_lo (the lowest selected lane, -1 if
; none), sel = v % 4 == 1
; Checked per wave (the vertices with one key): count and first agree with the members' sel and lanes, and each
; member's rank is the number of selected members on lower lanes.
; Run: as vs_w32_lane.asm.
; mattr: +wavefrontsize32,-wavefrontsize64
v_mbcnt_lo_u32_b32 v6, -1, 0
v_and_b32_e32 v4, 3, v0
v_cmp_eq_u32_e32 vcc_lo, 1, v4
v_mbcnt_lo_u32_b32 v7, vcc_lo, 0
v_readfirstlane_b32 s5, v0
v_mov_b32_e32 v8, s5
v_mov_b32_e32 v9, v0
v_lshlrev_b32_e32 v12, 4, v0
v_add_nc_u32_e32 v13, 0x1000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
s_bcnt1_i32_b32 s6, vcc_lo
s_ff1_i32_b32 s7, vcc_lo
v_mov_b32_e32 v6, s6
v_mov_b32_e32 v7, s7
v_cndmask_b32_e64 v8, 0, 1, vcc_lo
v_mov_b32_e32 v9, 0
v_add_nc_u32_e32 v13, 0x5000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
v_mov_b32_e32 v5, 0
exp pos0 v5, v5, v5, v5 done
exp param0 v5, v5, v5, v5
s_endpgm
24 changes: 24 additions & 0 deletions runtime/test/shaders/vs_w32_lane.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Wave32 vertex shader on a 64-wide vertex subgroup (w32_vs_halves.py): the wave's shape. Vertex v (v0 = vertex
; index) stores (lane, count, key, v) to its record at heap 0x1000 + 16 * v through the V# in s[0:3]:
; lane = v_mbcnt_lo_u32_b32 of -1: this vertex's lane in its wave, 0..31 in a wave32
; count = s_bcnt1_i32_b32 of exec_lo: the wave's active lanes, at most 32
; key = v_readfirstlane_b32 of v0: the vertex in the wave's first active lane, which names the wave
; Which vertices share a wave is the driver's choice, so the checks are what every wave32 must satisfy: the vertices
; with one key are one wave, there are `count` of them, at most 32, each on its own lane, and the first of them (the
; lowest lane) is the key's own vertex.
; Run: VKH_VERTICES=960 VKH_SGPRS=0,0,65536,0x30000000 VKH_HEAP=0=0 VKH_HEAP_DUMP=heap.raw vk_harness
; vs_w32_lane.spv <any fragment shader> out.raw (the triangles have zero area, so nothing is drawn).
; mattr: +wavefrontsize32,-wavefrontsize64
v_mbcnt_lo_u32_b32 v6, -1, 0
s_bcnt1_i32_b32 s4, exec_lo
v_mov_b32_e32 v7, s4
v_readfirstlane_b32 s5, v0
v_mov_b32_e32 v8, s5
v_mov_b32_e32 v9, v0
v_lshlrev_b32_e32 v12, 4, v0
v_add_nc_u32_e32 v12, 0x1000, v12
buffer_store_dwordx4 v[6:9], v12, s[0:3], 0 offen
v_mov_b32_e32 v5, 0
exp pos0 v5, v5, v5, v5 done
exp param0 v5, v5, v5, v5
s_endpgm
36 changes: 36 additions & 0 deletions runtime/test/shaders/vs_w32_shuffle.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; Wave32 vertex shader on a 64-wide vertex subgroup (w32_vs_halves.py): reads from other lanes of the wave.
; Record A at heap 0x1000 + 16 * v, record B at 0x5000 + 16 * v (v0 = vertex index):
; A = (lane, readlane5, key, v) readlane5 = v_readlane_b32 of v0 at lane 5, key = v_readfirstlane of v0
; B = (xor1, next, readlane31, 0) xor1 = ds_bpermute_b32 of v0 from lane ^ 1, next = from (lane + 1) & 31 (each 0
; when that lane is not in the wave), readlane31 = v_readlane_b32 of v0 at lane 31
; Checked per wave (the vertices with one key): each value is the v of the member on that lane.
; Run: as vs_w32_lane.asm.
; mattr: +wavefrontsize32,-wavefrontsize64
v_mbcnt_lo_u32_b32 v6, -1, 0
v_readlane_b32 s6, v0, 5
v_mov_b32_e32 v7, s6
v_readfirstlane_b32 s5, v0
v_mov_b32_e32 v8, s5
v_mov_b32_e32 v9, v0
v_lshlrev_b32_e32 v12, 4, v0
v_add_nc_u32_e32 v13, 0x1000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
v_xor_b32_e32 v14, 1, v6
v_lshlrev_b32_e32 v14, 2, v14
v_add_nc_u32_e32 v15, 1, v6
v_and_b32_e32 v15, 31, v15
v_lshlrev_b32_e32 v15, 2, v15
ds_bpermute_b32 v16, v14, v0
ds_bpermute_b32 v17, v15, v0
s_waitcnt lgkmcnt(0)
v_readlane_b32 s7, v0, 31
v_mov_b32_e32 v6, v16
v_mov_b32_e32 v7, v17
v_mov_b32_e32 v8, s7
v_mov_b32_e32 v9, 0
v_add_nc_u32_e32 v13, 0x5000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
v_mov_b32_e32 v5, 0
exp pos0 v5, v5, v5, v5 done
exp param0 v5, v5, v5, v5
s_endpgm
34 changes: 34 additions & 0 deletions runtime/test/shaders/vs_w32_vote.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; Wave32 vertex shader on a 64-wide vertex subgroup (w32_vs_halves.py): a wave-wide branch and a write to EXEC.
; (1) s_cbranch_vccz over a scalar write: only the wave that holds vertex 37 sets s5 = 7, every other wave keeps 0
; (a wave-level culling decision has this shape).
; (2) exec_lo = 0xffff: lanes 0..15 of each wave write 1, lanes 16..31 keep 0, then EXEC is restored.
; Vertex v (v0 = vertex index) stores (lane, s5, key, v) to heap 0x1000 + 16 * v and (low16, 0, 0, 0) to 0x5000 + 16 * v,
; key = v_readfirstlane of v0. Checked per wave: s5 == 7 exactly in the wave that has vertex 37, low16 == lane < 16.
; Run: as vs_w32_lane.asm.
; mattr: +wavefrontsize32,-wavefrontsize64
v_mbcnt_lo_u32_b32 v6, -1, 0
s_mov_b32 s5, 0
v_cmp_eq_u32_e32 vcc_lo, 37, v0
s_cbranch_vccz 1
s_mov_b32 s5, 7
v_mov_b32_e32 v7, s5
v_readfirstlane_b32 s6, v0
v_mov_b32_e32 v8, s6
v_mov_b32_e32 v9, v0
v_lshlrev_b32_e32 v12, 4, v0
v_add_nc_u32_e32 v13, 0x1000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
s_mov_b32 s10, exec_lo
v_mov_b32_e32 v6, 0
s_mov_b32 exec_lo, 0xffff
v_mov_b32_e32 v6, 1
s_mov_b32 exec_lo, s10
v_mov_b32_e32 v7, 0
v_mov_b32_e32 v8, 0
v_mov_b32_e32 v9, 0
v_add_nc_u32_e32 v13, 0x5000, v12
buffer_store_dwordx4 v[6:9], v13, s[0:3], 0 offen
v_mov_b32_e32 v5, 0
exp pos0 v5, v5, v5, v5 done
exp param0 v5, v5, v5, v5
s_endpgm
Loading