From ca96748fddde8b326093f799e009eebe430512f5 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Thu, 26 Nov 2026 23:39:28 +0000 Subject: [PATCH 1/2] startup: bind translated compute outputs named by a buffer UAV view token A translated Dispatch refused every output whose table entry holds the token of a buffer view the title created (device slot 19), with "an output is not an aliased placed buffer range with unordered access". Such an output is now bound in that view's desktop resource over the view's own range: raw, structured, or typed (sized by its format), as input tokens already are. Still refused, one reason each: a token that is not a buffer UAV of a desktop resource with unordered access; a view with no element size; a range that is empty, unaligned, over 16 MiB or outside its resource; an entry value read from the output's hardware descriptor words, which a token does not have; and an output buffer that is also an input. Outputs named by an address are unchanged. startup_compute_dispatch gains dispatch-output-token and dispatch-output-token-typed: the output descriptor is replaced by the token of a raw, or R32_UINT, view of the output buffer, and all 320 words must match. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sir6Mr49SZWax95VApRPvz --- src/startup_stubs/d3d12_compute_dispatch.inl | 52 ++++++++++++++++++-- tests/startup_compute_dispatch_check.py | 13 +++-- tests/startup_stream_consumer.cpp | 10 +++- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 7115c28500..0a5fd21552 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -1,7 +1,8 @@ // Translated compute programs at Dispatch (list slot 14). A device slot 67 pipeline is a placeholder that keeps // the key of its published request; gfx2hlsl.py --pending answers the request with .txt (`stage cs`) and // .cs.cso. Bound from the compute tables recorded by slot 35: raw buffers (`srv buf table ` -// inputs, `uav table ` outputs) named by hardware buffer descriptors, and 2D textures +// inputs, `uav table ` outputs) named by hardware buffer descriptors or, for outputs, by the token of +// a raw, structured or typed buffer view the title created (device slot 19), and 2D textures // (`srv tex2d table ` inputs, `uav tex2d/tex2da float table channels 4` outputs) named // by the token of a view the title created (device slots 18 and 19). `smp` lines become static samplers. Entry // values come from the compute root constants of slots 37 and 39, or from the descriptor a buffer input or output is @@ -340,12 +341,57 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& std::memcpy(views[i].words, words, sizeof(views[i].words)); continue; } + if (words[0] == view_token_magic) { + // An output named by the token of a raw, structured or typed buffer UAV the title created (device slot 19) is + // bound in that view's desktop resource over the view's own range, as an input token is (slot 18). + ViewRecord record{}; + bool known = false; + AcquireSRWLockShared(¬e_lock); + if (words[1] < view_records().size()) { + record = view_records()[words[1]]; + known = true; + } + ReleaseSRWLockShared(¬e_lock); + const Wrapper* const owner = known ? as_wrapper(reinterpret_cast(record.resource)) : nullptr; + auto* const buffer = owner && owner->own().kind == resource_kind ? static_cast(owner->own().real) : nullptr; + D3D12_UNORDERED_ACCESS_VIEW_DESC desc{}; + if (known && record.desc_bytes >= sizeof(desc)) std::memcpy(&desc, record.desc, sizeof(desc)); + if (desc.Format) desc.Format = desktop_format(desc.Format); + const bool raw = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_RAW && desc.Format == DXGI_FORMAT_R32_TYPELESS && + !desc.Buffer.StructureByteStride; + const bool structured = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_NONE && desc.Format == DXGI_FORMAT_UNKNOWN && + desc.Buffer.StructureByteStride; + const bool typed = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_NONE && !desc.Buffer.StructureByteStride; + const UINT element = raw ? 4 : structured ? desc.Buffer.StructureByteStride : + typed ? x2w_startup::typed_buffer_format_bytes(desc.Format) : 0; + const D3D12_RESOURCE_DESC resource = buffer ? buffer->GetDesc() : D3D12_RESOURCE_DESC{}; + const u64 offset = element && desc.Buffer.FirstElement <= UINT64_MAX / element ? desc.Buffer.FirstElement * element : ~u64{}; + const u64 bytes = u64{desc.Buffer.NumElements} * element; + if (!known || record.slot != 19 || !buffer || record.desc_bytes < sizeof(desc) || + desc.ViewDimension != D3D12_UAV_DIMENSION_BUFFER || resource.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || + !(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) + reason = "an output view token is not a buffer UAV of a desktop resource"; + else if (!element) reason = "an output buffer view has no element size"; + else if (!bytes || bytes % 4 || bytes > (16u << 20) || offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT || + offset > resource.Width || bytes > resource.Width - offset) + reason = "an output buffer view range is empty, unaligned or outside its resource"; + // The entry values an output descriptor supplies are words of a hardware descriptor, which a token has not. + for (const ComputeTranslation::Slot& ud : tr->uds) + if (!reason && ud.source >= ComputeTranslation::output_word3 && ud.a >= 0 && + inputs + static_cast(ud.a) == i) + reason = "an entry value reads the descriptor of an output named by a view token"; + for (std::size_t input = 0; input < inputs && !reason; ++input) + if (views[input].resource == buffer) reason = "an output buffer is also an input"; + views[i] = {buffer, offset, bytes, true}; + std::memcpy(views[i].words, words, sizeof(views[i].words)); + continue; + } // An output is written by the GPU: only a placed buffer that aliases the title's memory shows the result - // to the title. shortcut: view tokens and copied placements are refused, add them with their first user. + // to the title. shortcut: copied placements are refused, add them with their first user. const u64 address = words[0] | (u64{words[1] & 0xffff} << 32); const UINT stride = (words[1] >> 16) & 0x3fff; const u64 bytes = u64{stride ? stride : 1} * words[2]; - const auto placed = words[0] == view_token_magic || !bytes || bytes % 4 || bytes > (16u << 20) + const auto placed = !bytes || bytes % 4 || bytes > (16u << 20) ? PlacedVertexView{} : placed_buffer_range(self->own().list_device, address, bytes); const ResourceInfo* info = placed.alias ? resource_info(placed.owner) : nullptr; const u64 offset = placed.alias ? address - reinterpret_cast(placed.owner->own().title_memory) : 0; diff --git a/tests/startup_compute_dispatch_check.py b/tests/startup_compute_dispatch_check.py index bc1ee05770..e9247005bb 100644 --- a/tests/startup_compute_dispatch_check.py +++ b/tests/startup_compute_dispatch_check.py @@ -18,7 +18,9 @@ 6. five more refusals, each with its one logged reason and an untouched output: dispatch-missing-word (no slot 37 call), dispatch-root-changed (another compute root and back after the constants), dispatch-reset (list closed and reset after the constants), dispatch-wrong-root (recorded under a root the pipeline was not created with) - and dispatch-same-buffer (the output buffer is also the input). + and dispatch-same-buffer (the output buffer is also the input); +7. dispatch-output-token and dispatch-output-token-typed (the output named by the token of a raw, or R32_UINT, buffer + view of the output buffer): all 320 words match and nothing is refused. Exit 77 when the adapter is not available. """ import os @@ -129,8 +131,13 @@ def main(consumer, dxc, adapter): assert got == untouched, f'{mode}: a refused Dispatch wrote the output' refused = lines(result, 'refused: ') assert len(refused) == 1 and refused[0].endswith('refused: ' + reason), f'{mode}: {refused}\n{result.stderr[-3000:]}' - print(f'startup_compute_dispatch: PASS on {adapter} (all {C.WORDS} words match after translation and with the ' - 'same root set again; untouched before translation, without a Dispatch and for 6 refusals)') + for mode in ('dispatch-output-token', 'dispatch-output-token-typed'): + got, result = dispatch(directory, consumer, env, mode) + wrong = [i for i in range(C.WORDS) if got[i] != expected[i]] + assert not wrong and not lines(result, 'refused'), f'{mode}: {len(wrong)} words differ\n{result.stderr[-3000:]}' + print(f'startup_compute_dispatch: PASS on {adapter} (all {C.WORDS} words match after translation, with the ' + 'same root set again and with the output named by a raw or typed view token; untouched before ' + 'translation, without a Dispatch and for 6 refusals)') if __name__ == '__main__': diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index eddf880ff0..d3cecced0f 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -205,7 +205,9 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // Controls on the recorded state: dispatch-missing-word leaves out the slot 37 call; dispatch-root-changed sets // another compute root and the first one again after the constants, dispatch-same-root sets the same root again; // dispatch-reset closes and resets the list after the constants; dispatch-wrong-root records under the other -// root; dispatch-same-buffer names the output buffer as the input too. +// root; dispatch-same-buffer names the output buffer as the input too. dispatch-output-token writes, where the output +// descriptor was, the token of a raw unordered access view of the output buffer (device slot 19); +// dispatch-output-token-typed makes that view R32_UINT. // Input descriptors the dispatch refuses, one reason each: dispatch-input-empty has zero records (a nonzero base, // word 3 zero), dispatch-input-past ends 16 bytes past the input buffer, dispatch-input-unplaced names memory between // the two buffers, dispatch-input-copied a third buffer placed there at an offset the desktop cannot alias, and @@ -343,6 +345,12 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin std::memcpy(reinterpret_cast(cpu + sizeof(descriptors[0])), descriptors[1], sizeof(descriptors[1])); std::memcpy(unplaced + 4, buffers[0], words * sizeof(UINT)); } + if (mode == "dispatch-output-token" || mode == "dispatch-output-token-typed") { + const bool typed = mode == "dispatch-output-token-typed"; + D3D12_UNORDERED_ACCESS_VIEW_DESC view{typed ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R32_TYPELESS, D3D12_UAV_DIMENSION_BUFFER}; + view.Buffer = {0, words, 0, 0, typed ? D3D12_BUFFER_UAV_FLAG_NONE : D3D12_BUFFER_UAV_FLAG_RAW}; + invoke(device, 19, word(placed[1]), 0, word(&view), cpu + sizeof(descriptors[0])); + } if (mode != "dispatch-missing-word") invoke(list, 37, 0, constants[0], 0); invoke(list, 40, 0, 2, word(graphics), 0); invoke(list, 39, 0, 1, word(&constants[1]), 1); From 383ffaeb0a65dd37b873f83a4de3ce91a76b65c4 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Fri, 27 Nov 2026 18:36:22 +0000 Subject: [PATCH 2/2] test: cover 8-byte elements in compute output view tokens dispatch-output-token-structured (stride 8) and dispatch-output-token-typed8 (R16G16B16A16_FLOAT) describe the output's 1280 bytes as 160 8-byte elements. The raw and R32_UINT cases both have 4-byte elements, so they could not tell a derived element size from a fixed 4; these two can. Suggested by the review on this PR. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sir6Mr49SZWax95VApRPvz --- tests/startup_compute_dispatch_check.py | 9 ++++++--- tests/startup_stream_consumer.cpp | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/startup_compute_dispatch_check.py b/tests/startup_compute_dispatch_check.py index 28f92ed2aa..08259995a9 100644 --- a/tests/startup_compute_dispatch_check.py +++ b/tests/startup_compute_dispatch_check.py @@ -22,8 +22,10 @@ 7. slot 35 selects the correct table at descriptor index 2 instead of the swapped pair at the heap start; setting the same heap again preserves the table, but changing the compute root or view heap after binding must refuse at the table lookup, not an earlier check. -8. dispatch-output-token and dispatch-output-token-typed (the output named by the token of a raw, or R32_UINT, buffer - view of the output buffer): all 320 words match and nothing is refused. +8. the output named by the token of a buffer view of the output buffer, all 320 words matching and nothing refused: + dispatch-output-token (raw), dispatch-output-token-typed (R32_UINT), and dispatch-output-token-structured + (stride 8) and dispatch-output-token-typed8 (R16G16B16A16_FLOAT), whose 8-byte elements catch an element size + taken as 4. Exit 77 only if the initial probe cannot use the adapter or required heaps. """ import os @@ -158,7 +160,8 @@ def main(consumer, dxc, adapter): print(f'{mode}: exact table refusal, all {C.WORDS} sentinel words unchanged') errors = [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] assert not errors, errors[0] - for mode in ('dispatch-output-token', 'dispatch-output-token-typed'): + for mode in ('dispatch-output-token', 'dispatch-output-token-typed', 'dispatch-output-token-structured', + 'dispatch-output-token-typed8'): got, result = dispatch(directory, consumer, env, mode) wrong = sum(a != b for a, b in zip(got, expected)) assert not wrong, f'{mode}: {wrong} words differ from the CPU reference\n{result.stderr[-3000:]}' diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 08bbe91d3a..6f1cebd535 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -209,7 +209,9 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // dispatch-reset closes and resets the list after the constants; dispatch-wrong-root records under the other // root; dispatch-same-buffer names the output buffer as the input too. dispatch-output-token writes, where the output // descriptor was, the token of a raw unordered access view of the output buffer (device slot 19); -// dispatch-output-token-typed makes that view R32_UINT. +// dispatch-output-token-typed makes that view R32_UINT. dispatch-output-token-structured (stride 8) and +// dispatch-output-token-typed8 (R16G16B16A16_FLOAT) describe the same 1280 bytes as 160 8-byte elements, so an +// element size taken as 4 binds only the first half of the output. // Input descriptors the dispatch refuses, one reason each: dispatch-input-empty has zero records (a nonzero base, // word 3 zero), dispatch-input-past ends 16 bytes past the input buffer, dispatch-input-unplaced names memory between // the two buffers, dispatch-input-copied a third buffer placed there at an offset the desktop cannot alias, and @@ -418,10 +420,19 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin if (FAILED(static_cast(invoke(list, 9))) || FAILED(static_cast(invoke(list, 139, word(allocator))))) return 74; bind(); } - if (mode == "dispatch-output-token" || mode == "dispatch-output-token-typed") { - const bool typed = mode == "dispatch-output-token-typed"; - D3D12_UNORDERED_ACCESS_VIEW_DESC view{typed ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R32_TYPELESS, D3D12_UAV_DIMENSION_BUFFER}; - view.Buffer = {0, words, 0, 0, typed ? D3D12_BUFFER_UAV_FLAG_NONE : D3D12_BUFFER_UAV_FLAG_RAW}; + if (mode.rfind("dispatch-output-token", 0) == 0) { + D3D12_UNORDERED_ACCESS_VIEW_DESC view{DXGI_FORMAT_R32_TYPELESS, D3D12_UAV_DIMENSION_BUFFER}; + view.Buffer = {0, words, 0, 0, D3D12_BUFFER_UAV_FLAG_RAW}; + if (mode == "dispatch-output-token-typed") { + view.Format = DXGI_FORMAT_R32_UINT; + view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE; + } else if (mode == "dispatch-output-token-structured") { + view.Format = DXGI_FORMAT_UNKNOWN; + view.Buffer = {0, words / 2, 8, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + } else if (mode == "dispatch-output-token-typed8") { + view.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; + view.Buffer = {0, words / 2, 0, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + } invoke(device, 19, word(placed[1]), 0, word(&view), cpu + sizeof(descriptors[0])); } if (mode != "dispatch-missing-word") invoke(list, 37, 0, constants[0], 0);