diff --git a/docs/experiments/native-global-srv-heap-replay.md b/docs/experiments/native-global-srv-heap-replay.md index e8618bb26f..3577e05e36 100644 --- a/docs/experiments/native-global-srv-heap-replay.md +++ b/docs/experiments/native-global-srv-heap-replay.md @@ -31,12 +31,13 @@ Issue #2908, branch `cpp-migration`. Builds on the heap read in the direct C++ D select need slot + dword (after the slot lines; typed buffers of 32-bit channels only, #3023) ``` - `compile_dxil ... gfx1013-container-cs` reads an optional `req_.views` next to the `.cdx` (single file and `--requests`). It decodes the file, refuses a selection whose slot holds another view type than `need`, refuses a `need` - other than `raw_buffer`, translates with the `raw_buffer` contract and prints `global_srv select= need=`. Every - refusal happens before the `.cso` is published. Without the file a heap-reading shader is refused as before. The + other than `raw_buffer` or a one-channel 32-bit `typed_buffer` (#3023), translates with the matching contract and + prints `global_srv select= need=`. Every refusal happens before the `.cso` is published. Without the file a heap-reading shader is refused as before. The `.views` file is not part of the `.cdx`/`.root` content key: it describes one captured dispatch. ## Meaning of "native recording" and "runtime path" here @@ -132,10 +133,31 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha the window binding as the same raw view). `native_direct_container` compiles both formats through a compute table window and refuses the window without the byte-count product. Against `837728dd` the heap check fails at the `twindow_loop` shape (text "non-contiguous resource descriptor"). +- Typed heap loads (#3023): under the new `HeapViewContract::typed_dword` a one-channel typed load + (`buffer_load_format_x`, an `idxen` element index) through the GPU-selected heap descriptor reads + `ResourceDescriptorHeap[slot]` as a typed buffer of one U32 component (binding kind `heap_typed_srv`, cache identity + `/heap-view/typed_dword`). `.views` gains a `dword ` line for each typed buffer slot of 32-bit + channels; `compile_dxil` and the fixture derive the contract from `need typed_buffer` when every captured typed + buffer slot is a one-channel 32-bit buffer (`x2w_startup::global_srv_contract`). `record` takes + `--need typed_buffer`. `native_global_srv_heap_replay_hardware` passes with 122 assertions: the typed load is + recorded for slot 3 (`R32_UINT`), compiled, compiled again from the frontend cache, and replayed (output equals the + slot 3 data); replaying it against slot 0 reports a mismatch; selections 0 and 2 are refused before the dispatch; + ten compile refusals cover a two-channel load, a raw load and a `tbuffer` load under the typed contract, the typed + load under the raw contract, and metadata without the `dword` line, with a channel count of 5, with a `dword` line for + a raw slot, a duplicate or an early `dword` line, or with another selection. On the local capture (counts only) all 906 typed heap loads behind the raw contract have this + one-channel `idxen` shape; with `typed_dword` forced by an uncommitted switch, 895 of those programs then stop at a + raw heap read of another slot: they read the heap both ways, which one captured selection does not cover. ## Limits -- Only raw buffer heap reads are translated; other view classes are refused with a named diagnostic. +- Only raw buffer heap reads (`raw_buffer`) and one-channel 32-bit typed buffer heap reads (`typed_dword`) are + translated, one class per program; other view classes are refused with a named diagnostic. +- Under `typed_dword` the translator sees no descriptor values. The caller guarantees that every selectable slot is an + `R32_UINT`, `R32_SINT` or `R32_FLOAT` buffer view bound as `R32_UINT` over the same element range, that the guest + descriptor has stride 4, a 32-bit data format, identity `dst_sel_x`, swizzle and `add_tid` disabled, and that its + `num_records` is the view's element count (reads past it return 0). `.views` records the resource size, not the + view's first element or element count, so `global_srv_contract` checks necessary conditions only. Only `R32_UINT` + slots are exercised; `R32_SINT` and `R32_FLOAT` are accepted but untested. No runtime binds a typed heap slot yet. - The selected slot's index is per-lane VGPR data in general; reads stay inside the raw buffer view, which returns 0 out of bounds. No static index bound is checked. - Replay regenerates the slot contents deterministically from the slot index; `.views` captures type, stride and size, @@ -156,8 +178,8 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha `max(stride, 1) * num_records` bytes (`indirect_table_buffer`, `src/startup_stubs/d3d12_draw.inl`), the guest window's byte count, or of the title's own view size for a view token. No fixture runs this manifest path for a window. -- Typed loads (`buffer_load_format_*`) through a heap descriptor and scalar loads through the rewritten descriptor stay - refused. The startup runtime +- Multi-channel typed heap loads, typed loads under the raw contract and scalar loads through the rewritten descriptor + stay refused. The startup runtime does not pass a contract yet, so none of this reaches a title run (#3023). - The captured `select` must be the slot index the dispatch addresses; producing it from a title capture is outside this fixture. diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index e633a50b20..8b8dcf0802 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -127,7 +127,7 @@ NativeComputeShader decode(std::string_view bytes) { unsigned n = in.count(1000000); shader.bindings.reserve(n); for (unsigned j = 0; j < n; ++j) { - auto kind = static_cast(in.count(static_cast(ComputeViewKind::heap_buffer_srv))); + auto kind = static_cast(in.count(static_cast(ComputeViewKind::heap_typed_srv))); ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)}; b.scalar_table = static_cast(in.count(unsigned(ComputeViewBinding::ScalarTable::srv))); if (!b.array_count || b.array_position >= b.array_count || @@ -317,7 +317,9 @@ NativeComputeShader prepare_direct_compute(const std::filesystem::path &input, u identity += container ? "/container/cs_6_6" : "/slot67/cs_6_6"; word(identity, container ? 0 : entry); if (heap_view != HeapViewContract::none) - identity += heap_view == HeapViewContract::raw_buffer ? "/heap-view/raw_buffer" : "/heap-view/other"; + identity += heap_view == HeapViewContract::raw_buffer ? "/heap-view/raw_buffer" + : heap_view == HeapViewContract::typed_dword ? "/heap-view/typed_dword" + : "/heap-view/other"; if (container) { packed = read_compute_container(input); identity += shader_digest(packed.bytes) + shader_digest(packed.root); diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 03dc53cec8..221522290e 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -35,11 +35,14 @@ enum class ComputeViewKind { indexed_atomic_uav, d16_sample_srv, byte_window_srv, - heap_buffer_srv // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value. + heap_buffer_srv, // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value. + heap_typed_srv // A one-channel 32-bit typed buffer read the same way (HeapViewContract::typed_dword). }; inline constexpr unsigned compute_resource_heap = ~0u; // A binding of the whole descriptor heap: it has no table register and no root parameter. -inline bool compute_heap_binding(ComputeViewKind kind) { return kind == ComputeViewKind::heap_buffer_srv; } +inline bool compute_heap_binding(ComputeViewKind kind) { + return kind == ComputeViewKind::heap_buffer_srv || kind == ComputeViewKind::heap_typed_srv; +} inline bool compute_buffer_uav(ComputeViewKind kind) { return kind == ComputeViewKind::raw_buffer_uav || kind == ComputeViewKind::typed_buffer_uav || kind == ComputeViewKind::half_buffer_uav || kind == ComputeViewKind::indexed_atomic_uav; @@ -117,7 +120,13 @@ struct NativeComputeShader { // every slot the program can select is a raw view whose size is the guest range num_records * max(stride, 1); the // translator does not check that, nor the swizzle bits (word 1 bits 31:30) or the other word 3 fields, and a program // with two heap loads from different selectors is outside what one captured selection covers. -enum class HeapViewContract : unsigned { none, raw_buffer, other }; +// typed_dword: only typed loads (buffer_load_format_x, an idxen element index or none, no offsets) read the heap; raw +// loads and byte windows are refused. The caller guarantees that every slot the program can select is a typed buffer SRV +// of R32_UINT, R32_SINT or R32_FLOAT (default mapping, no flags, no structure stride), bound as R32_UINT over the same +// resource and element range, and that the guest descriptor it stands for has stride 4, a 32-bit UINT/SINT/FLOAT data +// format, identity dst_sel_x, swizzle and add_tid disabled, and the out-of-bounds rule "index >= num_records reads 0", +// with num_records equal to the view's element count. The translator checks none of these values. +enum class HeapViewContract : unsigned { none, raw_buffer, typed_dword, other }; NativeComputeShader translate_compute(const ComputeRequest &request, unsigned entry_byte_offset, bool direct_dxil = false); NativeComputeShader translate_compute_container(ComputeContainer container, diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index e845bff371..d823ab0ca3 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -271,7 +271,7 @@ struct DirectCompute { } } } - // Refuses a GPU-selected heap read that the contract does not cover. raw_buffer is the only class that is translated. + // Refuses a GPU-selected heap read that the contract does not cover. raw_buffer and typed_dword are translated. void refuse_heap_contract(unsigned pc) const { if (heap_view == HeapViewContract::none) refuse(pc, "direct resource-heap load requires the selected GPU slot and its view type"); @@ -283,11 +283,18 @@ struct DirectCompute { // dispatch before the shader runs. The binding names the heap (no table register); the pipeline indexes it directly. // A byte window is the byte-address rewrite of that descriptor (word 0 copied, word 1 without stride, word 2 the // byte count, word 3 with the raw out-of-bounds mode; see byte_descriptor_field): read as the same raw slot view. + // Under typed_dword the slot is a one-channel 32-bit typed buffer instead: a typed load of one channel (an element + // index), read as R32_UINT; raw reads and byte windows are refused. string heap_resource(const Inst &i, const State &s, unsigned base, ComputeViewKind kind, unsigned bytes) { const auto &symbol = s.at(base); refuse_heap_contract(i.pc); + const bool typed = heap_view == HeapViewContract::typed_dword; + if (typed && kind != ComputeViewKind::typed_buffer_srv) + refuse(i.pc, "a GPU-selected heap slot is read as a typed dword buffer SRV only"); + if (typed && (i.op & 3)) + refuse(i.pc, "typed heap load reads one channel only"); const bool window = kind == ComputeViewKind::byte_window_srv; - if (kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv && !window) + if (!typed && kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv && !window) refuse(i.pc, "a GPU-selected heap slot is read as a raw buffer SRV only"); if (symbol.offset % 32 || s[base].type != Symbol::Type::indexed_descriptor) refuse(i.pc, "unaligned heap descriptor"); @@ -310,13 +317,20 @@ struct DirectCompute { refuse(i.pc, "non-contiguous resource descriptor"); } auto &bindings = analysis.result.bindings; - auto it = std::find_if(bindings.begin(), bindings.end(), [](const auto &b) { - return b.kind == ComputeViewKind::heap_buffer_srv; + const auto heap_kind = typed ? ComputeViewKind::heap_typed_srv : ComputeViewKind::heap_buffer_srv; + auto it = std::find_if(bindings.begin(), bindings.end(), [heap_kind](const auto &b) { + return b.kind == heap_kind; }); if (it == bindings.end()) - bindings.push_back({ComputeViewKind::heap_buffer_srv, compute_resource_heap, 0, 0, bytes}); + bindings.push_back({heap_kind, compute_resource_heap, 0, 0, bytes}); else it->required_bytes = (std::max)(it->required_bytes, bytes); + if (typed) { + // A typed buffer of one U32 component (TypedBuffer, props: component type 5 + count * 256); the load reads + // element 0 of its ResRet only. No raw-buffer flag. + ir.features |= 0x40000000; + return ir.heap_handle(ir.binary("add", read(base), to_string(symbol.offset / 32)), 10, 5 + 1 * 256); + } // 16: raw buffers. 0x40000000: the module reads ResourceDescriptorHeap (DXC sets both for the HLSL reference, // see tests/native_heap_handle_probe_check.py); the validator refuses a module whose flags do not match. ir.features |= 16 | 0x40000000; diff --git a/src/startup_stubs/native_global_srv.h b/src/startup_stubs/native_global_srv.h index 0ea0f62d9a..10104b1996 100644 --- a/src/startup_stubs/native_global_srv.h +++ b/src/startup_stubs/native_global_srv.h @@ -12,9 +12,10 @@ // The view type of a global (descriptor heap) SRV slot that a dispatch can select from GPU data. // // A shader that picks its slot at run time can only be recorded and replayed if the type of every slot it may pick is -// known: the capture keeps {slot, type, stride} for each slot of the heap, and the dispatch is refused, before any -// shader runs, when the selected slot holds another type than the one the shader reads. No heap-indexed data read is -// translated here; this header only classifies, serializes and checks. +// known: the capture keeps {slot, type, stride, bytes} for each slot of the heap (and the 32-bit channel count of a +// typed buffer slot), and the dispatch is refused, before any shader runs, when the selected slot holds another type +// than the one the shader reads. No heap-indexed data read is translated here; this header only classifies, +// serializes and checks, and names the heap view contract a request allows (global_srv_contract). // // A .views file describes the one dispatch it was captured from. It is not part of the .cdx/.root content key of the // request it sits next to. @@ -43,6 +44,7 @@ struct GlobalSrvView { GlobalSrvType type = GlobalSrvType::none; unsigned stride = 0; // Bytes per element for a buffer view (the value native_view_stride gives), 0 for a texture. std::uint64_t bytes = 0; // Size of the buffer in bytes, 0 for a texture. Set by the caller from the resource description. + unsigned dword_channels = 0; // Typed buffer of 32-bit channels: their count (typed_dword_channels), else 0. }; // Classifies the description a title passed for an SRV (slot 18 of the device interface). False: not a view this @@ -62,6 +64,7 @@ inline bool classify_global_srv_view(unsigned heap_slot, const void *bytes, std: view.type = desc.Buffer.StructureByteStride ? GlobalSrvType::structured_buffer : (desc.Buffer.Flags & D3D12_BUFFER_SRV_FLAG_RAW) ? GlobalSrvType::raw_buffer : GlobalSrvType::typed_buffer; + if (view.type == GlobalSrvType::typed_buffer) view.dword_channels = typed_dword_channels(desc); return true; } @@ -78,6 +81,10 @@ inline std::string encode_global_srv_request(const GlobalSrvRequest &request) { out << "x2w-global-srv-views 2\nselect " << request.selected << "\nneed " << global_srv_type_name(request.needed) << '\n'; for (const auto &view : request.views) out << "slot " << view.slot << ' ' << global_srv_type_name(view.type) << ' ' << view.stride << ' ' << view.bytes << '\n'; + // After the slot lines: the 32-bit channel count of each typed buffer slot that has one. + for (const auto &view : request.views) + if (view.type == GlobalSrvType::typed_buffer && view.dword_channels) + out << "dword " << view.slot << ' ' << view.dword_channels << '\n'; return out.str(); } @@ -121,6 +128,19 @@ inline bool decode_global_srv_request(const std::string &text, GlobalSrvRequest return false; } request.views.push_back(view); + } else if (key == "dword") { + // Names an earlier typed buffer slot line, once, with 1-4 channels. + unsigned slot = 0, channels = 0; + GlobalSrvView *named = nullptr; + if (fields >> slot >> channels) + for (auto &view : request.views) + if (view.slot == slot) named = &view; + if (!named || named->type != GlobalSrvType::typed_buffer || named->dword_channels || channels < 1 || + channels > 4) { + error = "global SRV view metadata: unreadable dword line"; + return false; + } + named->dword_channels = channels; } else { error = "global SRV view metadata: unknown line '" + key + "'"; return false; @@ -145,4 +165,30 @@ inline std::string refuse_global_srv_selection(const std::vector } return "global SRV slot " + std::to_string(selected) + " has no captured view type"; } + +// The class of heap reads a request's metadata lets the translator emit (x2w::HeapViewContract::none, raw_buffer, +// typed_dword). Call after refuse_global_srv_selection accepted the selection. A typed request needs one-channel 32-bit +// typed buffers: the checks are necessary conditions on the captured resources only; the view's element range and the +// guest descriptor fields stay the caller's guarantee. +enum class GlobalSrvContract { none, raw_buffer, typed_dword }; + +inline GlobalSrvContract global_srv_contract(const GlobalSrvRequest &request, std::string &error) { + const auto refuse = [&error](std::string text) { + error = std::move(text) + "; only raw and one-channel 32-bit typed heap reads are translated"; + return GlobalSrvContract::none; + }; + if (request.needed == GlobalSrvType::raw_buffer) return GlobalSrvContract::raw_buffer; + if (request.needed != GlobalSrvType::typed_buffer) + return refuse(std::string("GPU-selected heap slot view type ") + global_srv_type_name(request.needed) + + " is not raw_buffer or typed_buffer"); + // Every captured typed buffer slot, the selected one included, must be a one-channel 32-bit buffer. + for (const auto &view : request.views) { + if (view.type != GlobalSrvType::typed_buffer) continue; + if (view.dword_channels != 1 || view.stride != 4 || !view.bytes || view.bytes % 4) + return refuse("GPU-selected heap slot " + std::to_string(view.slot) + + " is not a one-channel 32-bit typed buffer" + + (view.dword_channels ? "" : " (no 32-bit channel metadata)")); + } + return GlobalSrvContract::typed_dword; +} } // namespace x2w_startup diff --git a/tests/native_global_srv_heap_replay.cpp b/tests/native_global_srv_heap_replay.cpp index c0d823b1aa..72d6ebdd21 100644 --- a/tests/native_global_srv_heap_replay.cpp +++ b/tests/native_global_srv_heap_replay.cpp @@ -328,23 +328,42 @@ void check_pipeline_shape(const x2w::NativeComputePipeline &pipeline) { throw std::runtime_error("native pipeline has an unexpected binding layout"); } +// The pipeline contract a request's metadata allows; empty error when it is translated. +x2w::HeapViewContract heap_contract(const x2w_startup::GlobalSrvRequest &request, std::string &error) { + const auto contract = x2w_startup::global_srv_contract(request, error); + return contract == x2w_startup::GlobalSrvContract::typed_dword ? x2w::HeapViewContract::typed_dword + : x2w::HeapViewContract::raw_buffer; +} + int record(int argc, wchar_t **argv) { - if (argc != 8 && argc != 9) return 2; + if (argc < 8 || argc > 11) return 2; const std::wstring expect = argv[7]; - const bool guard = !(argc == 9 && std::wstring_view(argv[8]) == L"--no-guard"); - if ((argc == 9 && guard) || (expect != L"ok" && expect != L"refused")) return 2; + bool guard = true, need_given = false; + GlobalSrvType needed = GlobalSrvType::raw_buffer; + for (int a = 8; a < argc; ++a) { + const std::wstring_view flag = argv[a]; + if (flag == L"--no-guard" && guard) { + guard = false; + } else if (flag == L"--need" && a + 1 < argc && !need_given) { + need_given = true; + std::string name; // a view type name: ASCII, anything else names no type + for (const wchar_t c : std::wstring_view(argv[++a])) name += c < 0x80 ? static_cast(c) : '?'; + needed = x2w_startup::global_srv_type_from_name(name); + if (needed == GlobalSrvType::none) return 2; + } else { + return 2; + } + } + if (expect != L"ok" && expect != L"refused") return 2; const unsigned selector = static_cast(std::stoul(argv[6])); const fs::path cdx = argv[4]; Rig rig; if (int skip = rig.open(argv[5])) return skip; try { x2w::ShaderDxilCompiler compiler(argv[2]); - auto pipeline = x2w::create_native_compute_pipeline(rig.device.Get(), compiler, cdx, 0, argv[3], true, - x2w::HeapViewContract::raw_buffer); - check_pipeline_shape(pipeline); rig.create_io(); x2w_startup::GlobalSrvRequest request; - request.needed = GlobalSrvType::raw_buffer; + request.needed = needed; request.views.push_back(rig.add_slot(0, GlobalSrvType::raw_buffer, slot_words * 4)); request.views.push_back(rig.add_slot(1, GlobalSrvType::raw_buffer, slot_words * 4)); request.views.push_back(rig.add_slot(2, GlobalSrvType::texture, 0)); @@ -356,14 +375,25 @@ int record(int argc, wchar_t **argv) { auto views_path = cdx; views_path.replace_extension(L".views"); std::ofstream(views_path, std::ios::binary) << x2w_startup::encode_global_srv_request(request); - std::printf("captured select=%u need=raw_buffer views=%s\n", request.selected, views_path.string().c_str()); + std::printf("captured select=%u need=%s views=%s\n", request.selected, + x2w_startup::global_srv_type_name(request.needed), views_path.string().c_str()); + // The contract depends on the captured views, not on the selection: the shader is translated for every record, + // a refused selection included. + std::string error; + const auto contract = heap_contract(request, error); + if (!error.empty()) { + std::printf("refused: %s\n", error.c_str()); + return refused_path(rig, expect == L"ok"); + } + auto pipeline = x2w::create_native_compute_pipeline(rig.device.Get(), compiler, cdx, 0, argv[3], true, contract); + check_pipeline_shape(pipeline); const auto diagnostic = x2w_startup::refuse_global_srv_selection(request.views, request.selected, request.needed); if (!diagnostic.empty() && guard) { std::printf("refused select=%u: %s\n", request.selected, diagnostic.c_str()); return refused_path(rig, expect == L"ok"); } - if (request.selected >= slot_count || request.views[request.selected].type != GlobalSrvType::raw_buffer) - throw std::runtime_error("--no-guard only dispatches a raw buffer slot"); + if (request.selected >= slot_count || request.views[request.selected].type != request.needed) + throw std::runtime_error("--no-guard only dispatches a slot of the needed view type"); rig.dispatch(pipeline, nullptr, rig.slots[request.selected].Get()); if (expect == L"refused") { // Discrimination control: without the guard the shader ran, so the output must have changed. @@ -401,16 +431,14 @@ int replay(int argc, wchar_t **argv) { } if (error.empty()) error = x2w_startup::refuse_global_srv_selection(request.views, request.selected, request.needed); - if (error.empty() && request.needed != GlobalSrvType::raw_buffer) - error = std::string("GPU-selected heap slot view type ") + x2w_startup::global_srv_type_name(request.needed) + - " is not raw_buffer; only raw buffer heap reads are translated"; + auto contract = x2w::HeapViewContract::raw_buffer; + if (error.empty()) contract = heap_contract(request, error); if (!error.empty()) { std::printf("refused: %s\n", error.c_str()); return refused_path(rig, expect == L"ok"); } x2w::ShaderDxilCompiler compiler(argv[2]); - auto pipeline = x2w::create_native_compute_pipeline(rig.device.Get(), compiler, cdx, 0, argv[3], true, - x2w::HeapViewContract::raw_buffer); + auto pipeline = x2w::create_native_compute_pipeline(rig.device.Get(), compiler, cdx, 0, argv[3], true, contract); check_pipeline_shape(pipeline); std::ifstream shader(cso, std::ios::binary); if (!shader) throw std::runtime_error("missing compiled shader " + cso.string()); @@ -446,6 +474,6 @@ int wmain(int argc, wchar_t **argv) { return 4; } std::fputs("usage: native_global_srv_heap_replay root OUT.root | record SDK CACHE REQ.cdx ADAPTER SELECTOR ok|refused " - "[--no-guard] | replay SDK CACHE REQ.cdx COMPILED.cso VIEWS ADAPTER ok|refused REFERENCE_SLOT\n", stderr); + "[--no-guard] [--need raw_buffer|typed_buffer] | replay SDK CACHE REQ.cdx COMPILED.cso VIEWS ADAPTER ok|refused REFERENCE_SLOT\n", stderr); return 2; } diff --git a/tests/native_global_srv_heap_replay_check.py b/tests/native_global_srv_heap_replay_check.py index ce4fee7f64..048a12e3bd 100644 --- a/tests/native_global_srv_heap_replay_check.py +++ b/tests/native_global_srv_heap_replay_check.py @@ -356,4 +356,89 @@ def window_request(name, program): require(code == 0 and "global SRV view metadata file missing: " + str(missing) in out and "sentinels intact" in out, "replay with a missing .views path refuses with the file diagnostic, u0 sentinels intact") + # 10. Typed heap load (HeapViewContract::typed_dword): buffer_load_format_x v2, v0, s[20:23], 0 idxen reads element + # `lane` of the selected slot, a one-channel 32-bit typed buffer (slot 3, R32_UINT), so u0 equals the slot 3 data. + TYPED_RAW = "global SRV slot {n} has view type {t}, the shader needs typed_buffer" + TYPED_ONLY = "a GPU-selected heap slot is read as a typed dword buffer SRV only" + + def typed_program(load_words): + program = list(words_list) + program[load:load + 2] = load_words + return program + + TYPED = typed_program((0xe0002000, 0x80050200)) # buffer_load_format_x v2, v0, s[20:23], 0 idxen + + def typed_dir(name, program=TYPED): + directory = base / name + directory.mkdir() + cdx_bytes = compute_two_inputs(words(*program)) + typed_stem = "req_%016x" % key_of(root, cdx_bytes) + (directory / (typed_stem + ".cdx")).write_bytes(cdx_bytes) + (directory / (typed_stem + ".root")).write_bytes(root) + return directory, typed_stem + + def typed_compile(directory, typed_stem, out_name): + out_dir = base / out_name + code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir]) + return code, out, err, out_dir / (typed_stem[len("req_"):] + ".cs.cso") + + typed_selection = {} + for n in (3, 0, 2): + directory, typed_stem = typed_dir(f"typed{n}") + typed_selection[n] = (directory, typed_stem) + expect = "ok" if n == 3 else "refused" + code, out, err = run([fixture, "record", sdk, cache, directory / (typed_stem + ".cdx"), adapter, n, expect, + "--need", "typed_buffer"]) + if n == 3: + require(code == 0 and "output matches slot 3 data" in out, + "record typed select 3: u0 equals the slot 3 data read by the typed heap load") + text = (directory / (typed_stem + ".views")).read_text() + require(text.startswith("x2w-global-srv-views 2\nselect 3\nneed typed_buffer\n") and "dword 3 1\n" in text, + "record typed select 3 captured need typed_buffer and the slot 3 channel count") + else: + type_name = "raw_buffer" if n == 0 else "texture" + require(code == 0 and TYPED_RAW.format(n=n, t=type_name) in out and "no dispatch submitted" in out and + "sentinels intact" in out, f"record typed select {n}: refused before the dispatch") + directory, typed_stem = typed_selection[3] + typed_views = (directory / (typed_stem + ".views")).read_text() + code, out, err, typed_cso = typed_compile(directory, typed_stem, "typed3_out") + require(code == 0 and "requests=1 compiled=1 refused=0" in out and "global_srv select=3 need=typed_buffer" in out and + typed_cso.exists(), "compile_dxil typed select 3: compiled and published a .cso") + code, out, err, again = typed_compile(directory, typed_stem, "typed3_again") + require(code == 0 and "frontend_cache_hit=1" in out and again.read_bytes() == typed_cso.read_bytes(), + "compile_dxil typed select 3 again: frontend cache hit (the typed heap binding decodes), same .cso") + views_path = directory / (typed_stem + ".views") + code, out, err = run([fixture, "replay", sdk, cache, directory / (typed_stem + ".cdx"), typed_cso, views_path, + adapter, "ok", 3]) + require(code == 0 and "output matches slot 3 data" in out, "replay typed select 3: output equals slot 3 data") + code, out, err = run([fixture, "replay", sdk, cache, directory / (typed_stem + ".cdx"), typed_cso, views_path, + adapter, "ok", 0]) + require(code == 1 and "mismatch word 0" in out, "replay typed select 3 compared with slot 0 data reports a mismatch") + + # Refused shapes and metadata, compile_dxil, no .cso. + typed_negatives = { + # buffer_load_format_xy v[2:3], v0, s[20:23], 0 idxen + "typed_xy": (typed_program((0xe0042000, 0x80050200)), typed_views, "typed heap load reads one channel only"), + "typed_raw_program": (list(words_list), typed_views, TYPED_ONLY), + # tbuffer_load_format_x v2, v1, s[20:23], 0 format:[BUF_FMT_32_UINT] offen through the heap descriptor + "typed_tbuffer": (typed_program((0xe8a01000, 0x80050201)), typed_views, TYPED_ONLY), + "typed_raw_views": (TYPED, good, "a GPU-selected heap slot is read as a raw buffer SRV only"), + "typed_no_dword": (TYPED, typed_views.replace("dword 3 1\n", ""), + "GPU-selected heap slot 3 is not a one-channel 32-bit typed buffer (no 32-bit channel metadata)"), + "typed_select_0": (TYPED, typed_views.replace("select 3", "select 0"), TYPED_RAW.format(n=0, t="raw_buffer")), + "typed_dword_5": (TYPED, typed_views.replace("dword 3 1", "dword 3 5"), + "global SRV view metadata: unreadable dword line"), + "typed_dword_raw_slot": (TYPED, typed_views.replace("dword 3 1", "dword 0 1"), + "global SRV view metadata: unreadable dword line"), + "typed_dword_twice": (TYPED, typed_views + "dword 3 1\n", "global SRV view metadata: unreadable dword line"), + "typed_dword_first": (TYPED, typed_views.replace("dword 3 1\n", "").replace("need typed_buffer\n", + "need typed_buffer\ndword 3 1\n"), "global SRV view metadata: unreadable dword line"), + } + for name, (program, views_text, expected) in typed_negatives.items(): + directory, typed_stem = typed_dir("neg_" + name, program) + (directory / (typed_stem + ".views")).write_text(views_text, newline="\n") + code, out, err, cso = typed_compile(directory, typed_stem, "neg_" + name + "_out") + require(code == 1 and expected in err, f"compile_dxil '{name}': refused with '{expected}'") + require(not any(cso.parent.glob("*.cso")), f"compile_dxil '{name}': no .cso") + print("native_global_srv_heap_replay: all checks passed") diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 7fb8f8da63..c607a39183 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -72,11 +72,10 @@ static CapturedViews read_captured_views(const fs::path& request_path) { const auto& request = views.request; auto refusal = x2w_startup::refuse_global_srv_selection(request.views, request.selected, request.needed); if (!refusal.empty()) throw std::runtime_error(refusal); - if (request.needed != x2w_startup::GlobalSrvType::raw_buffer) - throw std::runtime_error(std::string("GPU-selected heap slot view type ") + - x2w_startup::global_srv_type_name(request.needed) + - " is not raw_buffer; only raw buffer heap reads are translated"); - views.contract = x2w::HeapViewContract::raw_buffer; + const auto contract = x2w_startup::global_srv_contract(request, error); + if (contract == x2w_startup::GlobalSrvContract::none) throw std::runtime_error(error); + views.contract = contract == x2w_startup::GlobalSrvContract::typed_dword ? x2w::HeapViewContract::typed_dword + : x2w::HeapViewContract::raw_buffer; return views; } @@ -133,7 +132,7 @@ static std::optional compile_one(x2w::ShaderDxilCompiler& compiler, } if(views.present){ const bool heap_read=std::any_of(program.bindings.begin(),program.bindings.end(),[](const auto& b){ - return b.kind==x2w::ComputeViewKind::heap_buffer_srv || b.kind==x2w::ComputeViewKind::heap_stride_srv;}); + return x2w::compute_heap_binding(b.kind) || b.kind==x2w::ComputeViewKind::heap_stride_srv;}); if(heap_read){ auto refusal=x2w_startup::refuse_global_srv_selection(views.request.views,views.request.selected,views.request.needed); if(!refusal.empty())throw std::runtime_error(refusal);