diff --git a/docs/experiments/native-sampler-heap.md b/docs/experiments/native-sampler-heap.md index 59dab0b62b..420bb043c0 100644 --- a/docs/experiments/native-sampler-heap.md +++ b/docs/experiments/native-sampler-heap.md @@ -20,9 +20,11 @@ requests stop first at such a load (117 `dwordx4`, 13 `dwordx8`). The change rem ## What changed -- `compute_sampler_heap` (`~0u - 1`) is a second sentinel next to `compute_resource_heap`. Under the existing condition - (root flags `0xc00`, exactly two user SGPRs left after the root parameters) the SGPR after the resource heap is now the table - `{table, compute_sampler_heap}` instead of `unbound`. Entry `n` of the sampler heap is title byte offset `16 * n`. +- `compute_sampler_heap` (`~0u - 1`) is a second sentinel next to `compute_resource_heap`. For root-based compute layouts, + `0x400` alone binds the resource heap at `next`, `0x800` alone binds the sampler heap there, and `0xc00` binds both with + the resource heap first. The heaps bind only when `next + heap_count == user`, where `next` is the first SGPR after the + root parameters, `heap_count` is the number of enabled heaps, and `user` is the user SGPR count. Entry `n` of the sampler + heap is title byte offset `16 * n`. - Direct path, load from the sampler heap (`gfx1013_direct_compute.inl`), refused in this order: a scalar buffer load ("scalar buffer load from the sampler heap is not translated"); an SGPR offset ("sampler heap load with an SGPR offset (a GPU-selected sampler) is not translated"); any width but `dwordx4` / `dwordx8` ("sampler heap load must read one or two @@ -67,6 +69,12 @@ builds its heap with `dynamic_sampler()` and the index parity itself. `tests/native_sampler_heap.cpp`, `tests/native_sampler_heap_check.py`, `tests/cmake/native_sampler_heap.cmake`; tests `native_sampler_heap_hardware` and `native_sampler_heap_warp` (`SKIP_RETURN_CODE 77`: WARP has no 32-lane waves). +Run the sampler checks and the resource-heap stride check with the dev preset. + +```powershell +ctest --preset dev --no-tests=error -R '^native_(sampler_heap_(hardware|warp)|wave_request_hardware)$' -V +``` + The script writes source-built compute requests (`.c67`, as `tests/native_compute_raw_root_check.py`) with the root `RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED), DescriptorTable(SRV(t0), UAV(u0))`, group 32x1x1 and seven user SGPRs (s4 table, s5 resource heap, s6 sampler heap). The instruction words were assembled once with LLVM 15 @@ -114,7 +122,7 @@ skips: the four WARP tests that already skipped and the new one). - Stage 1 only: no runtime binding of a mirrored sampler heap for natively translated compute dispatch. - GPU-selected sampler indices (SGPR offset), comparison, gather and gradient sampling through the heap, and containers are - refused. Roots with only the `0x800` flag (10 requests in the capture) are not covered. + refused. - The fixture samples with point filters and compares exact texels; decoys are checked to change the result (and to run: not the output sentinel), bilinear decoys only that. - Not exercised by a test: the highest accepted entry 2046 in a sample (the edge controls only load it), and `bind()`'s diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 423da7765a..11b2b8d706 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -1831,11 +1831,18 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi } else refuse(entry, "root-address parameter"); } - // In this compute ABI the two directly indexed heaps follow the user root parameters. - // Require both flags and exactly two remaining user SGPRs, as in the matched native layouts. - if (!stage && (UINT(layout->Desc_1_1.Flags) & 0xc00u) == 0xc00u && next + 2 == user) { - initial[next] = {Symbol::Type::table, compute_resource_heap}; - initial[next + 1] = {Symbol::Type::table, compute_sampler_heap}; + // The enabled direct heaps follow the root parameters, resource heap first. + // Bind them only when they account for every remaining user SGPR. + const auto flags = layout->Desc_1_1.Flags; + const bool resource_heap = (flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) != 0; + const bool sampler_heap = (flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) != 0; + const unsigned heap_count = unsigned(resource_heap) + unsigned(sampler_heap); + if (!stage && heap_count && next + heap_count == user) { + unsigned slot = next; + if (resource_heap) + initial[slot++] = {Symbol::Type::table, compute_resource_heap}; + if (sampler_heap) + initial[slot] = {Symbol::Type::table, compute_sampler_heap}; } // The legacy HLSL profile models wave32. Direct DXIL supports both guest wave sizes. A wave64 request whose // group has at most 32 threads stays on the wave32 model, as before the wave size was read: its one guest wave diff --git a/tests/native_sampler_heap_check.py b/tests/native_sampler_heap_check.py index 2d20e2aa8f..c135d17a89 100644 --- a/tests/native_sampler_heap_check.py +++ b/tests/native_sampler_heap_check.py @@ -30,6 +30,7 @@ ROOT_RW = "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED), DescriptorTable(SRV(t0), UAV(u0))" ROOT_VIEWS_ONLY = "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED), DescriptorTable(SRV(t0), UAV(u0))" +ROOT_SAMPLERS_ONLY = "RootFlags(SAMPLER_HEAP_DIRECTLY_INDEXED), DescriptorTable(SRV(t0), UAV(u0))" REFUSALS = { "buffer": "scalar buffer load from the sampler heap is not translated", @@ -239,7 +240,6 @@ def run(command, expect_skip=False): with tempfile.TemporaryDirectory(prefix="x2w_sampler_heap_") as scratch: base = pathlib.Path(scratch) # Sampler heap content, texture and expectations are in the fixture; the requests are written here. - sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1, 0, 7 << 1) # group 32x1x1, 7 user SGPRs roots = {} def root(text): @@ -254,20 +254,28 @@ def root(text): counter = 0 - def request(name, root_text=ROOT_RW): + def request(name, root_text=ROOT_RW, *, user=None): nonlocal counter counter += 1 folder = base / f"req_{counter}_{name}" folder.mkdir() - return write_request(folder, PROGRAMS[name], root(root_text), sa) + words = PROGRAMS[name] + if root_text == ROOT_SAMPLERS_ONLY: + assert name == "sample" + words = words.copy() + words[2] = 0xbe8a0305 # s_mov_b32 s10, s5 + if user is None: + user = 6 if root_text == ROOT_SAMPLERS_ONLY else 7 + sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1, 0, user << 1) + return write_request(folder, words, root(root_text), sa) def compile_request(marker, profile="gfx1013-direct-cs@0", cache=None): output = marker.with_suffix(".cso") code, out, err = run([compile_dxil, sdk, cache or marker.parent / "cache", profile, marker, output]) return code, out, err, output - def refused(name, key, root_text=ROOT_RW, profile="gfx1013-direct-cs@0"): - marker = request(name, root_text) + def refused(name, key, root_text=ROOT_RW, profile="gfx1013-direct-cs@0", *, user=None): + marker = request(name, root_text, user=user) code, out, err, output = compile_request(marker, profile) require(code != 0 and REFUSALS[key] in err and not output.exists(), f"{name}: refused with \"{REFUSALS[key]}\" and no .cso") @@ -278,6 +286,9 @@ def accepted(name, message): require(code == 0 and output.exists() and "sampler_heap span" not in out, message) return out + # Four reserved SGPRs and one table leave no user SGPR for the sampler heap. + refused("sample", "table", root_text=ROOT_SAMPLERS_ONLY, user=5) + # 1. Fixture runs. A fresh frontend cache per run: the fixture asserts a miss and then a hit. expected_lines = { "sample": ["entry 67 (result 0) equals the CPU model in all 32 lanes", @@ -293,17 +304,19 @@ def accepted(name, message): *[f"decoy {n} in place of entry 67 changes result 1" for n in range(3)], "1088 bytes}"], } - for mode, lines in expected_lines.items(): - marker = request(mode) - cache = base / f"cache_{mode}" + cases = [(mode, mode, ROOT_RW) for mode in expected_lines] + cases.append(("sampler_only", "sample", ROOT_SAMPLERS_ONLY)) + for name, mode, root_text in cases: + marker = request(mode, root_text) + cache = base / f"cache_{name}" cache.mkdir() code, out, err = run([fixture, sdk, cache, marker, adapter, mode], expect_skip=True) - require(code == 0, f"fixture mode {mode} exits 0") - for line in lines: - require(line in out, f"{mode}: {line}") + require(code == 0, f"fixture case {name} exits 0") + for line in expected_lines[mode]: + require(line in out, f"{name}: {line}") require("pass: prepare_direct_compute misses on a fresh cache and hits on the second call" in out, - f"{mode}: frontend cache round trip (miss, then hit)") - require("pass: pipeline has sampler_heap_indexed" in out, f"{mode}: pipeline asks for the sampler heap flag only") + f"{name}: frontend cache round trip (miss, then hit)") + require("pass: pipeline has sampler_heap_indexed" in out, f"{name}: pipeline asks for the sampler heap flag only") code, out, err = run([fixture, sdk, base / "cache_unbound", request("sample"), adapter, "unbound"], expect_skip=True) require(code == 0 and "bind without a sampler heap throws" in out, "bind without a sampler heap throws") diff --git a/tests/native_wave_request_check.py b/tests/native_wave_request_check.py index cdab046316..6a3a57f69d 100644 --- a/tests/native_wave_request_check.py +++ b/tests/native_wave_request_check.py @@ -132,30 +132,45 @@ def scalar(op, dst, a, b): assert result.returncode and "needs offen" in result.stderr and not (work / "indexed.cso").exists() -def global_stride_fixtures(gpu, sdk, work, root, source, sa): - source.write_text('#define SIG "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED),' - 'DescriptorTable(SRV(t0,numDescriptors=16)),DescriptorTable(UAV(u0,numDescriptors=16)),' - 'RootConstants(num32BitConstants=4,b0)"') - subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", "-Fo", root, source], check=True) - # GPU input s[40:47] feeds s43. Select a heap slot, then extract descriptor word 1 bits 16..29. - # The table index is deliberately unbounded at compile time, and is overwritten before the extract. - for selector_offset in (0, 4): +def global_stride_fixtures(cli_args, gpu, sdk, work, root, source, sa): + def program(selector_offset, group_x): + # GPU input s[40:47] feeds s43. The unbounded index is overwritten before the stride extract. + group_y = group_x + 1 words = [0xbe940304, 0xbe950380, 0xbe960305, 0xbe970380, 0xf4080000 | (24 << 6) | 10, 0xfa000040, 0xf40c0000 | (56 << 6) | 11, 0xfa0000e0, - 0x80000000 | (0x1e << 23) | (20 << 16) | (133 << 8) | 12, + 0x80000000 | (0x1e << 23) | (20 << 16) | (133 << 8) | group_x, 0xf42c0000 | (40 << 6) | 12, (20 << 25) | selector_offset, 0xbe9e030a, 0xbe9f0380, 0x80000000 | (0x1e << 23) | (43 << 16) | (133 << 8) | 43, 0xf4080000 | (48 << 6) | 15, 43 << 25, 0xbeab0380, 0x93800000 | (28 << 16) | (255 << 8) | 49, 0x000e0010, - 0xd7460008, 12 | (133 << 9) | (256 << 18), 0x7e12020d] + 0xd7460008, group_x | (133 << 9) | (256 << 18), 0x7e120200 | group_y] words += [0x7e000000 | (v << 17) | (6 << 9) | 28 for v in range(2, 6)] words += [0xf0200f08, (14 << 16) | (2 << 8) | 8, 0xbf810000] - marker = write_request(work, words, root.read_bytes(), sa) - result = subprocess.run([gpu, sdk, marker, work / "cache", "global-stride"], capture_output=True, text=True) - print("global-stride", selector_offset, result.stdout.strip(), flush=True) - assert result.returncode == int(bool(selector_offset)), (result.stdout, result.stderr) - assert f"differing_components={8192 if selector_offset else 0} " in result.stdout + return words + + views = "CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED" + sa_views = sa[:-4] + struct.pack("