From 885b29f938dec4198fcea561f04fd0af4854286e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 19:10:47 +0000 Subject: [PATCH 1/6] compute: name each input view token a dispatch will not bind Split the shared refusal into the token's record: unknown, sampler, unordered access, render or depth, truncated views, a non-buffer shader resource view, a missing resource, and a placed or copied resource with no desktop backing. A typed or raw buffer view of a desktop resource still uses the existing range checks. A placed buffer that aliases title memory is bound, and the fixture reads that path back. Refs #3224 --- docs/experiments/compute-input-view-token.md | 52 +++++ src/startup_stubs/d3d12_draw.inl | 35 ++- src/startup_stubs/input_view_token.h | 49 ++++ .../startup_compute_input_view_token.txt | 1 + .../startup_compute_input_view_token.cmake | 18 ++ .../startup_compute_input_view_token_check.py | 215 ++++++++++++++++++ tests/startup_stream_consumer.cpp | 51 ++++- 7 files changed, 411 insertions(+), 10 deletions(-) create mode 100644 docs/experiments/compute-input-view-token.md create mode 100644 src/startup_stubs/input_view_token.h create mode 100644 tests/checks/python-linux/startup_compute_input_view_token.txt create mode 100644 tests/cmake/startup_compute_input_view_token.cmake create mode 100644 tests/startup_compute_input_view_token_check.py diff --git a/docs/experiments/compute-input-view-token.md b/docs/experiments/compute-input-view-token.md new file mode 100644 index 0000000000..2b8b3d7260 --- /dev/null +++ b/docs/experiments/compute-input-view-token.md @@ -0,0 +1,52 @@ +# Compute input view tokens + +A translated compute dispatch binds a view token as a buffer input only when the token is a +constant-buffer view, or a buffer shader-resource view of a desktop resource. Every other token +used to share one census sentence, `an input view token is not a buffer view of a desktop resource`. +`indirect_table_buffer` now refuses that token with the record it resolved to, in this order: + +1. `an input view token is unknown` +2. `an input view token is a sampler` +3. `an input view token is an unordered access view` +4. `an input view token is a render or depth view` +5. `an input view token has a truncated constant buffer view` +6. `an input view token has an unrecognized slot` +7. `an input view token has a truncated shader resource view` +8. `an input view token is not a buffer view of a desktop resource` (a shader resource view whose + dimension is not a buffer; a texture token keeps this historical sentence) +9. `an input view token names no resource` +10. `an input view token does not name a resource` +11. `an input view token's placed or copied resource has no desktop backing` +12. `an input view token has no desktop resource` + +The census still keeps the first reason of each key. A later range check is unchanged: a view that +passes this gate can still be refused because its element size, alignment, alias, or span fails. + +A typed or raw buffer view whose resource has a desktop object passes this gate. The fixture binds +two of those on the placed input buffer that aliases title memory, an `R32_UINT` view and a raw +`R32_TYPELESS` view, and reads all 320 words of case A back. A placed buffer the desktop cannot +alias is still refused with `an input is in a placed buffer that does not alias title memory`. A +placed or copied resource with no desktop object stays refused; the fixture does not construct one, +and the Linux reference is what checks that sentence. + +`tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for +`dispatch-input-token`. This note does not change that test. + +```bash +./out/pe-env/bin/python -B tests/startup_compute_input_view_token_check.py +``` + +That command runs the reference and the controls. It does not dispatch. + +On the Windows test PC: + +```powershell +ctest --preset dev --no-tests=error -R "^startup_compute_(input_view_token_|dispatch_input_)" -V +``` + +`startup_compute_input_view_token_warp` and `_hardware` skip with 77 when the adapter is not +available. A skip is not a pass. This note has no Windows readback. Nothing here shows that a +title starts or runs. + +The decision table is [input_view_token.h](../../src/startup_stubs/input_view_token.h). The fixture +is [startup_compute_input_view_token_check.py](../../tests/startup_compute_input_view_token_check.py). diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 9c1c2b2a2f..8824074855 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -10,6 +10,12 @@ // upload ring / a private descriptor heap. #include "startup_stubs/typed_uav_load.h" +#include "startup_stubs/input_view_token.h" + +static_assert(D3D12_SRV_DIMENSION_BUFFER == 1, "input view token facts use 1 for a buffer SRV"); +static_assert(static_cast(Placement::none) == 0, "input view token facts use 0 for no placement"); +static_assert(static_cast(Placement::aliased_buffer) == 1, "input view token facts use 1 for an aliased buffer"); +static_assert(static_cast(Placement::copied_buffer) == 2, "input view token facts use 2 for a copied buffer"); // A pixel-stage image store (#1996 item F, #2742): tools/startup/gfx2hlsl.py writes 'uav N tex2d|tex2da table P O' // for the RWTexture2D or RWTexture2DArray at u, whose descriptor is at byte O of the title's @@ -2551,21 +2557,34 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor if (why) *why = nullptr; if (words[0] == view_token_magic) { ViewRecord record{}; - token_view(words, record); + const bool known = token_view(words, record); + D3D12_SHADER_RESOURCE_VIEW_DESC view{}; + const bool described = known && record.slot == 18 && record.desc_bytes >= 40; + if (described) std::memcpy(&view, record.desc, 40); + Wrapper* const token_owner = described ? as_wrapper(reinterpret_cast(record.resource)) : nullptr; + const bool token_resource = token_owner && token_owner->own().kind == resource_kind; + const ResourceInfo* token_info = token_resource ? resource_info(token_owner) : nullptr; + InputViewTokenFacts facts; + facts.known = known; + facts.slot = record.slot; + facts.desc_bytes = record.desc_bytes; + facts.view_dimension = described ? static_cast(view.ViewDimension) : 0; + facts.resource_pointer = record.resource != 0; + facts.wrapper = token_owner != nullptr; + facts.resource = token_resource; + facts.desktop = token_resource && token_owner->own().real != nullptr; + facts.placement = token_info ? static_cast(token_info->placement) : 0; + if (const char* token_reason = input_view_token_refusal(facts)) return refuse(token_reason); if (record.slot == 17 && record.desc_bytes >= 12) { if (requires_identity_dst_sel) return refuse("an MTBUF byte window requires a buffer SRV"); UINT size = 0; std::memcpy(&address, record.desc, 8); std::memcpy(&size, record.desc + 8, 4); bytes = size; - } else if (record.slot == 18 && record.desc_bytes >= 40) { - D3D12_SHADER_RESOURCE_VIEW_DESC view{}; - std::memcpy(&view, record.desc, 40); + } else if (record.slot == 18 && record.desc_bytes >= 40 && view.ViewDimension == D3D12_SRV_DIMENSION_BUFFER) { if (requires_identity_dst_sel && view.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING) return refuse("an MTBUF byte window requires identity component mapping"); - owner = as_wrapper(reinterpret_cast(record.resource)); - if (!owner || owner->own().kind != resource_kind || !owner->own().real || - view.ViewDimension != D3D12_SRV_DIMENSION_BUFFER) return refuse("an input view token is not a buffer view of a desktop resource"); + owner = token_owner; const bool raw = view.Buffer.Flags == D3D12_BUFFER_SRV_FLAG_RAW && !view.Buffer.StructureByteStride && view.Format == DXGI_FORMAT_R32_TYPELESS; const bool structured = view.Buffer.Flags == D3D12_BUFFER_SRV_FLAG_NONE && view.Buffer.StructureByteStride && @@ -2590,7 +2609,7 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor if (!own_memory) address = base + offset; } } else { - return refuse("an input view token is not a buffer view of a desktop resource"); + return refuse("an input view token has an unrecognized slot"); } } else { if (requires_identity_dst_sel && (words[3] & 0xfffu) != 0xfacu) diff --git a/src/startup_stubs/input_view_token.h b/src/startup_stubs/input_view_token.h new file mode 100644 index 0000000000..2d66078fbc --- /dev/null +++ b/src/startup_stubs/input_view_token.h @@ -0,0 +1,49 @@ +// What an input view token resolves to, before a compute or indirect buffer bind. +// nullptr means the token is a constant-buffer view, or a buffer shader-resource view of a +// desktop resource: the caller keeps its range, alignment and alias checks. Every other +// token is refused with the shape that failed, so one census reason is not shared by all of them. +// A non-buffer shader resource view keeps the historical sentence. Placement is the Placement +// enumeration: 0 none, 1 aliased buffer, 2 copied buffer. +#pragma once + +#include + +struct InputViewTokenFacts { + bool known = false; + std::uint32_t slot = 0; + std::uint32_t desc_bytes = 0; + std::uint32_t view_dimension = 0; + bool resource_pointer = false; + bool wrapper = false; + bool resource = false; + bool desktop = false; + std::uint32_t placement = 0; +}; + +inline const char* input_view_token_refusal(const InputViewTokenFacts& facts) +{ + constexpr std::uint32_t buffer_dimension = 1; // D3D12_SRV_DIMENSION_BUFFER + constexpr std::uint32_t shader_resource = 18; + constexpr std::uint32_t shader_resource_bytes = 40; + constexpr std::uint32_t constant_buffer_bytes = 12; + if (!facts.known) return "an input view token is unknown"; + if (facts.slot == 22) return "an input view token is a sampler"; + if (facts.slot == 19) return "an input view token is an unordered access view"; + if (facts.slot == 20 || facts.slot == 21) return "an input view token is a render or depth view"; + if (facts.slot == 17) { + if (facts.desc_bytes < constant_buffer_bytes) return "an input view token has a truncated constant buffer view"; + return nullptr; + } + if (facts.slot != shader_resource) return "an input view token has an unrecognized slot"; + if (facts.desc_bytes < shader_resource_bytes) return "an input view token has a truncated shader resource view"; + if (facts.view_dimension != buffer_dimension) + return "an input view token is not a buffer view of a desktop resource"; + if (!facts.resource_pointer || !facts.wrapper) return "an input view token names no resource"; + if (!facts.resource) return "an input view token does not name a resource"; + if (!facts.desktop) { + if (facts.placement == 1 || facts.placement == 2) + return "an input view token's placed or copied resource has no desktop backing"; + return "an input view token has no desktop resource"; + } + return nullptr; +} diff --git a/tests/checks/python-linux/startup_compute_input_view_token.txt b/tests/checks/python-linux/startup_compute_input_view_token.txt new file mode 100644 index 0000000000..3ddb79522e --- /dev/null +++ b/tests/checks/python-linux/startup_compute_input_view_token.txt @@ -0,0 +1 @@ +tests/startup_compute_input_view_token_check.py diff --git a/tests/cmake/startup_compute_input_view_token.cmake b/tests/cmake/startup_compute_input_view_token.cmake new file mode 100644 index 0000000000..35b4e2b524 --- /dev/null +++ b/tests/cmake/startup_compute_input_view_token.cmake @@ -0,0 +1,18 @@ +# Compute input view tokens. reference and controls are pure Python. The readback needs a +# desktop device and otherwise reports a skip (77). +if(EXISTS "${INTAKE_PYTHON}") + foreach(mode IN ITEMS reference controls) + add_test(NAME startup_compute_input_view_token_${mode} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_compute_input_view_token_check.py" ${mode}) + set_tests_properties(startup_compute_input_view_token_${mode} PROPERTIES TIMEOUT 60) + endforeach() +endif() +if(TARGET startup_stream_consumer AND EXISTS "${INTAKE_PYTHON}" AND EXISTS "${DXC_EXECUTABLE}") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_compute_input_view_token_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_compute_input_view_token_check.py" + "$" "${DXC_EXECUTABLE}" ${adapter}) + set_tests_properties(startup_compute_input_view_token_${adapter} PROPERTIES + TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/startup_compute_input_view_token_check.py b/tests/startup_compute_input_view_token_check.py new file mode 100644 index 0000000000..ac208d99f5 --- /dev/null +++ b/tests/startup_compute_input_view_token_check.py @@ -0,0 +1,215 @@ +#!/usr/bin/env python3 +"""Name the shape of a compute input view token, and bind a buffer view of a desktop resource. + +usage: + startup_compute_input_view_token_check.py + startup_compute_input_view_token_check.py reference + startup_compute_input_view_token_check.py controls + startup_compute_input_view_token_check.py + +reference and controls need no device. With no arguments both run. reference checks the decision +table against src/startup_stubs/input_view_token.h. controls checks that the runtime calls that +table, that a non-buffer shader resource view still uses the historical sentence, and that +tests/startup_compute_dispatch_input_check.py still expects that sentence for a texture token. + +warp and hardware dispatch case A. A typed R32_UINT view and a raw R32_TYPELESS view of the placed +input buffer, which aliases title memory, match all 320 words. A texture token, an unknown token, +an unordered-access token, a sampler token, a typed view of a buffer the desktop cannot alias, and +a typed view that runs past the buffer are refused. The output stays at the sentinel. Exit 77 when +the adapter is not available. +""" + +from pathlib import Path +import re +import sys + +ROOT = Path(__file__).resolve().parents[1] +HEADER = ROOT / "src" / "startup_stubs" / "input_view_token.h" +DRAW = ROOT / "src" / "startup_stubs" / "d3d12_draw.inl" +EXISTING = ROOT / "tests" / "startup_compute_dispatch_input_check.py" + +HISTORICAL = "an input view token is not a buffer view of a desktop resource" +BUFFER = 1 + + +def facts(**overrides): + base = { + "known": True, + "slot": 18, + "desc_bytes": 40, + "view_dimension": BUFFER, + "resource_pointer": True, + "wrapper": True, + "resource": True, + "desktop": True, + "placement": 1, + } + base.update(overrides) + return base + + +def classify(item): + """Same order as input_view_token_refusal. None means the token passes this gate.""" + if not item["known"]: + return "an input view token is unknown" + if item["slot"] == 22: + return "an input view token is a sampler" + if item["slot"] == 19: + return "an input view token is an unordered access view" + if item["slot"] in (20, 21): + return "an input view token is a render or depth view" + if item["slot"] == 17: + if item["desc_bytes"] < 12: + return "an input view token has a truncated constant buffer view" + return None + if item["slot"] != 18: + return "an input view token has an unrecognized slot" + if item["desc_bytes"] < 40: + return "an input view token has a truncated shader resource view" + if item["view_dimension"] != BUFFER: + return HISTORICAL + if not item["resource_pointer"] or not item["wrapper"]: + return "an input view token names no resource" + if not item["resource"]: + return "an input view token does not name a resource" + if not item["desktop"]: + if item["placement"] in (1, 2): + return "an input view token's placed or copied resource has no desktop backing" + return "an input view token has no desktop resource" + return None + + +CASES = ( + ("unknown", facts(known=False, slot=0, desc_bytes=0, resource_pointer=False, wrapper=False, resource=False, desktop=False), + "an input view token is unknown"), + ("sampler", facts(slot=22), "an input view token is a sampler"), + ("uav", facts(slot=19), "an input view token is an unordered access view"), + ("rtv", facts(slot=20), "an input view token is a render or depth view"), + ("dsv", facts(slot=21), "an input view token is a render or depth view"), + ("short-cbv", facts(slot=17, desc_bytes=11), "an input view token has a truncated constant buffer view"), + ("cbv", facts(slot=17, desc_bytes=12), None), + ("other-slot", facts(slot=16), "an input view token has an unrecognized slot"), + ("short-srv", facts(desc_bytes=39), "an input view token has a truncated shader resource view"), + ("texture", facts(view_dimension=4), HISTORICAL), + ("texture-without-desktop", facts(view_dimension=4, desktop=False), HISTORICAL), + ("null-resource", facts(resource_pointer=False, wrapper=False, resource=False, desktop=False), + "an input view token names no resource"), + ("not-a-wrapper", facts(wrapper=False, resource=False, desktop=False), "an input view token names no resource"), + ("wrong-kind", facts(resource=False, desktop=False), "an input view token does not name a resource"), + ("placed-without-desktop", facts(desktop=False, placement=1), + "an input view token's placed or copied resource has no desktop backing"), + ("copied-without-desktop", facts(desktop=False, placement=2), + "an input view token's placed or copied resource has no desktop backing"), + ("no-desktop", facts(desktop=False, placement=0), "an input view token has no desktop resource"), + ("committed-buffer", facts(placement=0), None), + ("aliased-buffer", facts(placement=1), None), + ("copied-buffer-gate", facts(placement=2), None), +) + + +def header_reasons(): + text = HEADER.read_text(encoding="utf-8") + return re.findall(r'return "([^"]+)"', text) + + +def reference(): + produced = [] + for name, item, expected in CASES: + got = classify(item) + assert got == expected, (name, got, expected) + if got: + produced.append(got) + declared = header_reasons() + assert declared, "input_view_token.h has no refusal sentences" + assert set(produced) == set(declared), (set(produced) ^ set(declared)) + assert HISTORICAL in declared + print(f"startup_compute_input_view_token reference: {len(CASES)} shapes, {len(set(declared))} refusal sentences") + + +def controls(): + reference() + draw = DRAW.read_text(encoding="utf-8") + assert "input_view_token_refusal" in draw + assert 'return refuse("an input view token has an unrecognized slot")' in draw + assert HISTORICAL not in draw, "the historical sentence now lives only in input_view_token.h" + assert "an input is in a placed buffer that does not alias title memory" in draw + assert "an input range is outside its desktop buffer" in draw + existing = EXISTING.read_text(encoding="utf-8") + assert "('dispatch-input-token', 'an input view token is not a buffer view of a desktop resource')" in existing + print("startup_compute_input_view_token controls: runtime uses the table; the texture token sentence is unchanged") + + +def adapter_run(consumer, dxc, adapter): + import os + import subprocess + import startup_gfx2hlsl_compute_check as C + from startup_compute_dispatch_check import dispatch, lines, prepare_case + from startup_stream_check import gfx2hlsl as G + + refused = ( + ("dispatch-input-token", HISTORICAL), + ("dispatch-input-token-unknown", "an input view token is unknown"), + ("dispatch-input-token-uav", "an input view token is an unordered access view"), + ("dispatch-input-token-sampler", "an input view token is a sampler"), + ("dispatch-input-token-copied", "an input is in a placed buffer that does not alias title memory"), + ("dispatch-input-token-past", "an input range is outside its desktop buffer"), + ) + bound = ("dispatch-input-token-typed", "dispatch-input-token-raw") + from tempfile import TemporaryDirectory + with TemporaryDirectory() as temporary: + directory = Path(temporary) + cache, env = prepare_case(directory, adapter) + env.pop("X2W_TRANSLATED_TITLE_INPUT_SNAPSHOT", None) + env.pop("X2W_D3D12_RING_RELEASE_AT_SUBMIT", None) + untouched, expected = [C.SENTINEL] * C.WORDS, C.expected("A") + + got, result = dispatch(directory, consumer, env, "dispatch", probe=True) + print(result.stdout, end="") + assert got == untouched and len(lines(result, "refused: no translation")) == 1, result.stderr[-3000:] + published = lines(result, "x2w-draw compute request") + assert published and " published " in published[0], result.stderr[-3000:] + key = int(published[0].split()[3], 16) + out = subprocess.run([sys.executable, "-B", G.__file__, "--pending", cache], check=True, capture_output=True, + text=True, env=dict(os.environ, X2W_DXC=dxc)).stdout + assert f"{key:016x}: ok" in out, out + + got, result = dispatch(directory, consumer, env, "dispatch") + assert got == expected and not lines(result, "refused"), result.stderr[-3000:] + + for mode in bound: + got, result = dispatch(directory, consumer, env, mode) + assert got == expected and len(lines(result, "Dispatch(3, 2, 2) translated")) == 1, (mode, result.stderr[-3000:]) + assert not lines(result, "refused:"), (mode, result.stderr[-3000:]) + 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, reason in refused: + got, result = dispatch(directory, consumer, env, mode) + assert got == untouched, "%s wrote the output" % mode + found = lines(result, "refused: ") + census = lines(result, "x2w-render compute census") + where = lines(result, f"x2w-draw compute {key:016x} first refused input 0 (table ") + assert len(found) == 1 and found[0].endswith("refused: " + reason), (mode, result.stderr[-3000:]) + assert len(census) == 3 and census[1].endswith(f'reason "{reason}": 1 keys, 1 dispatches'), (mode, census) + assert len(where) == 1, (mode, result.stderr[-3000:]) + print(f"startup_compute_input_view_token: PASS on {adapter} " + f"({len(bound)} buffer views bound, {len(refused)} tokens refused)") + + +def main(argv): + if not argv: + controls() + return + if argv == ["reference"]: + reference() + return + if argv == ["controls"]: + controls() + return + if len(argv) == 3 and argv[2] in ("warp", "hardware"): + adapter_run(*argv) + return + sys.exit(__doc__) + + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 769513639d..4726aabdb0 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -232,6 +232,11 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // the two buffers, dispatch-input-copied a third buffer placed there at an offset the desktop cannot alias, and // dispatch-input-null (all four words zero, an unbound buffer) is not refused and reads zeros; // dispatch-input-token writes the token of a texture view where the input descriptor was. +// dispatch-input-token-typed and -raw replace it with a buffer view of the aliased input buffer (R32_UINT, or a raw +// R32_TYPELESS view). dispatch-input-token-past is that typed view four elements past the buffer. +// dispatch-input-token-copied is a typed view of the buffer placed where the desktop cannot alias it. +// dispatch-input-token-unknown writes a token id no view record has. dispatch-input-token-uav and -sampler write +// those view tokens in the input slot. // Output descriptors the dispatch refuses, one reason each: dispatch-output-texture-token, -unplaced and -copied do // to the output what dispatch-input-token, -unplaced and -copied do to the input, dispatch-output-unaligned starts // the output 8 bytes into its buffer and dispatch-output-state places the output buffer in the unordered access state. @@ -328,11 +333,11 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin // exist only in title memory (the desktop copy of a buffer that does not alias holds zeros); dispatch-input-copied // leaves that memory zero. if (mode == "dispatch-input-snapshot" || long_run) std::memcpy(reinterpret_cast(between), input, sizeof(input)); + void* copied = nullptr; if (mode == "dispatch-input-copied" || mode == "dispatch-input-snapshot" || long_run || mode == "dispatch-root-srv-copied" || - mode == "dispatch-output-copied") { + mode == "dispatch-output-copied" || mode == "dispatch-input-token-copied") { const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, long_run ? long_records * sizeof(UINT) : words * sizeof(UINT), 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; - void* copied = nullptr; if (FAILED(static_cast(invoke(device, 133, word(reinterpret_cast(between)), word(&desc), D3D12_RESOURCE_STATE_COMMON, 0, word(&__uuidof(ID3D12Resource)), word(&copied))))) return 71; } @@ -658,6 +663,48 @@ 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)); } + const auto restore_output_descriptor = [&] { + std::memcpy(reinterpret_cast(cpu + sizeof(descriptors[0])), descriptors[1], sizeof(descriptors[1])); + }; + if (mode == "dispatch-input-token-typed" || mode == "dispatch-input-token-raw" || mode == "dispatch-input-token-past" || + mode == "dispatch-input-token-copied") { + D3D12_SHADER_RESOURCE_VIEW_DESC view{}; + view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; + view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + view.Buffer.FirstElement = 0; + view.Buffer.NumElements = mode == "dispatch-input-token-past" ? words + 4 : words; + if (mode == "dispatch-input-token-raw") { + view.Format = DXGI_FORMAT_R32_TYPELESS; + view.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW; + } else { + view.Format = DXGI_FORMAT_R32_UINT; + } + void* const resource = mode == "dispatch-input-token-copied" ? copied : placed[0]; + if (!resource) return 71; + invoke(device, 18, word(resource), word(&view), cpu); + restore_output_descriptor(); + } + if (mode == "dispatch-input-token-unknown") { + constexpr UINT token_magic = 0x56573258; // "X2WV" + const UINT token[4] = {token_magic, 0x00ffffffu, 18, 0}; + std::memcpy(reinterpret_cast(cpu), token, sizeof(token)); + } + if (mode == "dispatch-input-token-uav") { + D3D12_UNORDERED_ACCESS_VIEW_DESC view{}; + view.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; + view.Format = DXGI_FORMAT_R32_TYPELESS; + view.Buffer.NumElements = words; + view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW; + invoke(device, 19, word(placed[1]), 0, word(&view), cpu); + restore_output_descriptor(); + } + if (mode == "dispatch-input-token-sampler") { + D3D12_SAMPLER_DESC sampler{}; + sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; + sampler.AddressU = sampler.AddressV = sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; + sampler.MaxLOD = D3D12_FLOAT32_MAX; + invoke(device, 22, word(&sampler), cpu); + } if (root_srv && mode != "dispatch-root-srv-unset") { UINT64 addr0 = reinterpret_cast(buffers[0]); UINT64 addr1 = reinterpret_cast(buffer2); From c4ca370723880365126c4a85e8d2e0910786f0f4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 20:17:32 +0000 Subject: [PATCH 2/6] compute: record the sampler input token on a sampler heap CreateSampler into the compute CBV/SRV/UAV table faults in the desktop runtime before the dispatch is logged. The fixture now creates the sampler in a one-entry sampler heap and copies that token into the input slot, so the census still refuses it as a sampler. Refs #3224 --- docs/experiments/compute-input-view-token.md | 4 ++- tests/startup_stream_consumer.cpp | 29 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/experiments/compute-input-view-token.md b/docs/experiments/compute-input-view-token.md index 2b8b3d7260..2bd56760ed 100644 --- a/docs/experiments/compute-input-view-token.md +++ b/docs/experiments/compute-input-view-token.md @@ -27,7 +27,9 @@ two of those on the placed input buffer that aliases title memory, an `R32_UINT` `R32_TYPELESS` view, and reads all 320 words of case A back. A placed buffer the desktop cannot alias is still refused with `an input is in a placed buffer that does not alias title memory`. A placed or copied resource with no desktop object stays refused; the fixture does not construct one, -and the Linux reference is what checks that sentence. +and the Linux reference is what checks that sentence. The sampler refusal copies a token out of a +one-entry sampler heap. The compute table is a CBV/SRV/UAV heap, and a sampler descriptor written +there faults in the desktop runtime before the dispatch is recorded. `tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for `dispatch-input-token`. This note does not change that test. diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 4726aabdb0..98e854bd4d 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -235,8 +235,9 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // dispatch-input-token-typed and -raw replace it with a buffer view of the aliased input buffer (R32_UINT, or a raw // R32_TYPELESS view). dispatch-input-token-past is that typed view four elements past the buffer. // dispatch-input-token-copied is a typed view of the buffer placed where the desktop cannot alias it. -// dispatch-input-token-unknown writes a token id no view record has. dispatch-input-token-uav and -sampler write -// those view tokens in the input slot. +// dispatch-input-token-unknown writes a token id no view record has. dispatch-input-token-uav writes an +// unordered-access token in the input slot. dispatch-input-token-sampler records a sampler in its own sampler +// heap (a sampler descriptor is not valid in the compute view heap) and copies that token over the input. // Output descriptors the dispatch refuses, one reason each: dispatch-output-texture-token, -unplaced and -copied do // to the output what dispatch-input-token, -unplaced and -copied do to the input, dispatch-output-unaligned starts // the output 8 bytes into its buffer and dispatch-output-state places the output buffer in the unordered access state. @@ -699,11 +700,33 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin restore_output_descriptor(); } if (mode == "dispatch-input-token-sampler") { + // Slot 22 writes a sampler descriptor. The compute table is a CBV/SRV/UAV heap, and a sampler + // descriptor there faults in the desktop runtime, so the sampler is created in a one-entry sampler + // heap whose title memory receives the token. Those 16 bytes then replace the input descriptor. + constexpr UINT sampler_stride = 16; + auto* const backing = static_cast(VirtualAlloc(nullptr, sampler_stride, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!backing) return 70; + const struct { + D3D12_DESCRIPTOR_HEAP_DESC desc; + UINT64 address, bytes; + } sampler_heap{{D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE}, + reinterpret_cast(backing), sampler_stride}; + void* sampler_slots = nullptr; + if (FAILED(static_cast(invoke(device, 14, word(&sampler_heap), word(&__uuidof(ID3D12DescriptorHeap)), + word(&sampler_slots)))) || !sampler_slots) + return 72; + const StreamWord sampler_cpu = invoke(sampler_slots, 9); + if (!sampler_cpu) return 73; D3D12_SAMPLER_DESC sampler{}; sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; sampler.AddressU = sampler.AddressV = sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; + sampler.MaxAnisotropy = 1; + sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER; sampler.MaxLOD = D3D12_FLOAT32_MAX; - invoke(device, 22, word(&sampler), cpu); + invoke(device, 22, word(&sampler), sampler_cpu); + constexpr UINT token_magic = 0x56573258; // "X2WV" + if (reinterpret_cast(backing)[0] != token_magic) return 82; + std::memcpy(reinterpret_cast(cpu), backing, sampler_stride); } if (root_srv && mode != "dispatch-root-srv-unset") { UINT64 addr0 = reinterpret_cast(buffers[0]); From 184ef09f01e94eaafe99eff6f2915868e176df5a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 20:41:18 +0000 Subject: [PATCH 3/6] compute: record a buffer view that runs past its resource Desktop CreateShaderResourceView removes the device when a buffer view extends past the resource, so the dispatch never reaches its range refusal. That view is now recorded and not created. The past-the-end fixture uses a custom-heap buffer, whose range is checked against the desktop width. Refs #3224 --- docs/experiments/compute-input-view-token.md | 5 ++- src/startup_stubs/d3d12_xs_device.cpp | 13 +++++++ .../startup_compute_input_view_token_check.py | 11 ++++-- tests/startup_stream_consumer.cpp | 34 +++++++++++++++++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/docs/experiments/compute-input-view-token.md b/docs/experiments/compute-input-view-token.md index 2bd56760ed..10adc60133 100644 --- a/docs/experiments/compute-input-view-token.md +++ b/docs/experiments/compute-input-view-token.md @@ -29,7 +29,10 @@ alias is still refused with `an input is in a placed buffer that does not alias placed or copied resource with no desktop object stays refused; the fixture does not construct one, and the Linux reference is what checks that sentence. The sampler refusal copies a token out of a one-entry sampler heap. The compute table is a CBV/SRV/UAV heap, and a sampler descriptor written -there faults in the desktop runtime before the dispatch is recorded. +there faults in the desktop runtime before the dispatch is recorded. A buffer view that extends +past its resource is recorded and not created: the desktop call removes the device. The fixture's +past-the-end token is that view of a custom-heap buffer, so the range is refused against the +desktop width rather than as a placed allocation. `tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for `dispatch-input-token`. This note does not change that test. diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 3f8808e3a2..28d2d0d0c0 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -5321,6 +5321,19 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d) if (first_calls(mismatched, 6)) log("x2w-d3d12 Device::slot%d view dimension %u needs resource dimension %u, resource has %u", N, view_dimension, x2w_view_dimension::needed(N, view_dimension), dimension); + } else if (N == 18 && view_dimension == D3D12_SRV_DIMENSION_BUFFER && + x2w_local::readable(reinterpret_cast(described), sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC))) { + // Desktop CreateShaderResourceView removes the device when a buffer view runs past the resource. + // Record the guest description and leave a null descriptor, the same as the other refused views. + D3D12_SHADER_RESOURCE_VIEW_DESC view{}; + std::memcpy(&view, reinterpret_cast(described), sizeof(view)); + const UINT element = x2w_startup::buffer_srv_element_bytes(view); + const UINT64 width = resource_desc.Width; + const UINT64 first = view.Buffer.FirstElement; + const UINT64 count = view.Buffer.NumElements; + if (element && (first > UINT64_MAX / element || count > UINT64_MAX / element || + first * element > width || count * element > width - first * element)) + skip = "a buffer shader resource view extends past the resource"; } else if constexpr (N == 19) { if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE3D && x2w_local::readable(reinterpret_cast(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) { diff --git a/tests/startup_compute_input_view_token_check.py b/tests/startup_compute_input_view_token_check.py index ac208d99f5..71a90c1a56 100644 --- a/tests/startup_compute_input_view_token_check.py +++ b/tests/startup_compute_input_view_token_check.py @@ -15,8 +15,8 @@ warp and hardware dispatch case A. A typed R32_UINT view and a raw R32_TYPELESS view of the placed input buffer, which aliases title memory, match all 320 words. A texture token, an unknown token, an unordered-access token, a sampler token, a typed view of a buffer the desktop cannot alias, and -a typed view that runs past the buffer are refused. The output stays at the sentinel. Exit 77 when -the adapter is not available. +a typed view that runs past a custom-heap buffer are refused. That past view is recorded and not +created on the desktop. The output stays at the sentinel. Exit 77 when the adapter is not available. """ from pathlib import Path @@ -26,6 +26,8 @@ ROOT = Path(__file__).resolve().parents[1] HEADER = ROOT / "src" / "startup_stubs" / "input_view_token.h" DRAW = ROOT / "src" / "startup_stubs" / "d3d12_draw.inl" +DEVICE = ROOT / "src" / "startup_stubs" / "d3d12_xs_device.cpp" +CONSUMER = ROOT / "tests" / "startup_stream_consumer.cpp" EXISTING = ROOT / "tests" / "startup_compute_dispatch_input_check.py" HISTORICAL = "an input view token is not a buffer view of a desktop resource" @@ -134,6 +136,11 @@ def controls(): assert HISTORICAL not in draw, "the historical sentence now lives only in input_view_token.h" assert "an input is in a placed buffer that does not alias title memory" in draw assert "an input range is outside its desktop buffer" in draw + device = DEVICE.read_text(encoding="utf-8") + assert 'skip = "a buffer shader resource view extends past the resource"' in device + consumer = CONSUMER.read_text(encoding="utf-8") + assert 'mode == "dispatch-input-token-past"' in consumer + assert "D3D12_HEAP_TYPE_CUSTOM" in consumer existing = EXISTING.read_text(encoding="utf-8") assert "('dispatch-input-token', 'an input view token is not a buffer view of a desktop resource')" in existing print("startup_compute_input_view_token controls: runtime uses the table; the texture token sentence is unchanged") diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 98e854bd4d..817dfe9f81 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -233,7 +233,8 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // dispatch-input-null (all four words zero, an unbound buffer) is not refused and reads zeros; // dispatch-input-token writes the token of a texture view where the input descriptor was. // dispatch-input-token-typed and -raw replace it with a buffer view of the aliased input buffer (R32_UINT, or a raw -// R32_TYPELESS view). dispatch-input-token-past is that typed view four elements past the buffer. +// R32_TYPELESS view). dispatch-input-token-past is that typed view four elements past a custom-heap buffer: the +// desktop call is not made, and the range is checked against the buffer's width. // dispatch-input-token-copied is a typed view of the buffer placed where the desktop cannot alias it. // dispatch-input-token-unknown writes a token id no view record has. dispatch-input-token-uav writes an // unordered-access token in the input slot. dispatch-input-token-sampler records a sampler in its own sampler @@ -667,13 +668,40 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin const auto restore_output_descriptor = [&] { std::memcpy(reinterpret_cast(cpu + sizeof(descriptors[0])), descriptors[1], sizeof(descriptors[1])); }; - if (mode == "dispatch-input-token-typed" || mode == "dispatch-input-token-raw" || mode == "dispatch-input-token-past" || + if (mode == "dispatch-input-token-past") { + // A placed buffer refuses a range past its allocation before the desktop width is checked. This buffer is + // committed on a custom heap, so it has no placed address and the view's byte range is compared with the + // desktop width. The view runs past that width; the device records the description and does not create it. + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_CUSTOM; + heap.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; + heap.MemoryPoolPreference = D3D12_MEMORY_POOL_L0; + heap.CreationNodeMask = 1; + heap.VisibleNodeMask = 1; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, words * sizeof(UINT), 1, 1, 1, DXGI_FORMAT_UNKNOWN, + {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; + using Create = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, UINT, const D3D12_RESOURCE_DESC*, UINT, + const void*, void*, UINT, const void*, const GUID*, void**); + void* resource = nullptr; + if (FAILED(reinterpret_cast((*static_cast(device))[127])( + device, &heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, nullptr, 0, nullptr, + &__uuidof(ID3D12Resource), &resource)) || !resource) + return 71; + D3D12_SHADER_RESOURCE_VIEW_DESC view{}; + view.Format = DXGI_FORMAT_R32_UINT; + view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; + view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + view.Buffer.NumElements = words + 4; + invoke(device, 18, word(resource), word(&view), cpu); + restore_output_descriptor(); + } + if (mode == "dispatch-input-token-typed" || mode == "dispatch-input-token-raw" || mode == "dispatch-input-token-copied") { D3D12_SHADER_RESOURCE_VIEW_DESC view{}; view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; view.Buffer.FirstElement = 0; - view.Buffer.NumElements = mode == "dispatch-input-token-past" ? words + 4 : words; + view.Buffer.NumElements = words; if (mode == "dispatch-input-token-raw") { view.Format = DXGI_FORMAT_R32_TYPELESS; view.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW; From bb023e5ce58fcfa42767ef68fb34a4570ded39b7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 21:22:55 +0000 Subject: [PATCH 4/6] compute: refuse a past-the-end view for its range before its heap A recorded buffer view with no title memory was refused because its heap is not default or upload, before the desktop width was checked. The width check now comes first. A fitting view of that heap still uses the heap sentence. The control fails when the width check is removed. Refs #3224 --- docs/experiments/compute-input-view-token.md | 15 ++++++-- src/startup_stubs/d3d12_draw.inl | 12 +++++- .../startup_compute_input_view_token_check.py | 38 ++++++++++++++++++- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/docs/experiments/compute-input-view-token.md b/docs/experiments/compute-input-view-token.md index 10adc60133..2b6aa7ba4f 100644 --- a/docs/experiments/compute-input-view-token.md +++ b/docs/experiments/compute-input-view-token.md @@ -19,8 +19,15 @@ used to share one census sentence, `an input view token is not a buffer view of 11. `an input view token's placed or copied resource has no desktop backing` 12. `an input view token has no desktop resource` -The census still keeps the first reason of each key. A later range check is unchanged: a view that -passes this gate can still be refused because its element size, alignment, alias, or span fails. +The census still keeps the first reason of each key. A buffer view that passes this gate is then +refused, in this order, for element size, an empty or oversized range, a placed span or alias, a +null address, alignment, a released resource, a resource not created in the common state, or a +resource on another device. The next check is the desktop width, +`an input range is outside its desktop buffer`. A resource with no title memory is refused for its +heap type only after that, with +`an input buffer view token's resource is not on a default or upload heap`. A view recorded +without a desktop descriptor still names its resource, so a range past that resource's width uses +the width sentence. A typed or raw buffer view whose resource has a desktop object passes this gate. The fixture binds two of those on the placed input buffer that aliases title memory, an `R32_UINT` view and a raw @@ -31,8 +38,8 @@ and the Linux reference is what checks that sentence. The sampler refusal copies one-entry sampler heap. The compute table is a CBV/SRV/UAV heap, and a sampler descriptor written there faults in the desktop runtime before the dispatch is recorded. A buffer view that extends past its resource is recorded and not created: the desktop call removes the device. The fixture's -past-the-end token is that view of a custom-heap buffer, so the range is refused against the -desktop width rather than as a placed allocation. +past-the-end token is that view of a custom-heap buffer with no title memory. The width sentence +is what refuses it. Removing that check leaves the heap-type sentence, and the control fails. `tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for `dispatch-input-token`. This note does not change that test. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 8824074855..946805a638 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2634,6 +2634,7 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor : "an input buffer range exceeds the D3D12 raw SRV limit or is not whole dwords"); ID3D12Resource* native = nullptr; bool upload = false; + bool reject_heap = false; int input_source = 0; Wrapper* device = self->own().list_device; if (address) { @@ -2693,8 +2694,9 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor D3D12_HEAP_FLAGS flags{}; if (FAILED(native->GetHeapProperties(&props, &flags)) || (props.Type != D3D12_HEAP_TYPE_DEFAULT && props.Type != D3D12_HEAP_TYPE_UPLOAD)) - return refuse("an input buffer view token's resource is not on a default or upload heap"); - upload = props.Type == D3D12_HEAP_TYPE_UPLOAD; + reject_heap = true; // after the desktop range check: a range past the resource wins + else + upload = props.Type == D3D12_HEAP_TYPE_UPLOAD; } if (!native) return refuse("an input buffer descriptor has a null address"); if (offset % alignment) return refuse(alignment == 4 ? "typed input range is not dword aligned" : @@ -2707,6 +2709,12 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor if (FAILED(native->GetDevice(IID_PPV_ARGS(native_device.GetAddressOf()))) || native_device.Get() != device_of(device)) return refuse("an input buffer's resource is on another device"); const auto desc = native->GetDesc(); + // A view recorded without a desktop descriptor still names this resource. Its byte range is decided + // before the heap type, so a range past the end is not reported as the wrong kind of heap. + if (desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && !(desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) && + (offset > desc.Width || bytes > desc.Width - offset)) + return refuse("an input range is outside its desktop buffer"); + if (reject_heap) return refuse("an input buffer view token's resource is not on a default or upload heap"); if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || (desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) || offset > desc.Width || bytes > desc.Width - offset) return refuse("an input range is outside its desktop buffer"); trace_compute_output_use(self, OutputUse::shader_gpu_read, "raw-SRV-range-resolved", owner, 0, offset, bytes); diff --git a/tests/startup_compute_input_view_token_check.py b/tests/startup_compute_input_view_token_check.py index 71a90c1a56..ca8437e191 100644 --- a/tests/startup_compute_input_view_token_check.py +++ b/tests/startup_compute_input_view_token_check.py @@ -128,6 +128,41 @@ def reference(): print(f"startup_compute_input_view_token reference: {len(CASES)} shapes, {len(set(declared))} refusal sentences") +RANGE = "an input range is outside its desktop buffer" +HEAP = "an input buffer view token's resource is not on a default or upload heap" + + +def binding_body(draw): + start = draw.index("IndirectBufferView indirect_table_buffer") + end = draw.index("\nstruct FormattedBufferDescriptor", start) + return draw[start:end] + + +def first_backing_reason(body, applicable): + for reason in re.findall(r'return refuse\("([^"]+)"\)', body): + if applicable.get(reason): + return reason + return None + + +def backing_order(draw): + """A recorded past-the-end view is refused for its range before its heap type.""" + body = binding_body(draw) + past = {RANGE: True, HEAP: True} + fitting = {RANGE: False, HEAP: True} + assert first_backing_reason(body, past) == RANGE + assert first_backing_reason(body, fitting) == HEAP, "a fitting view of a non-default heap must keep its heap reason" + mutant = body.replace('return refuse("%s")' % RANGE, "") + try: + assert first_backing_reason(mutant, past) == RANGE + except AssertionError: + got = first_backing_reason(mutant, past) + assert got == HEAP, got + print("past range check removed: refused for heap type, as required") + else: + raise AssertionError("past negative still passed after the range check was removed") + + def controls(): reference() draw = DRAW.read_text(encoding="utf-8") @@ -135,7 +170,8 @@ def controls(): assert 'return refuse("an input view token has an unrecognized slot")' in draw assert HISTORICAL not in draw, "the historical sentence now lives only in input_view_token.h" assert "an input is in a placed buffer that does not alias title memory" in draw - assert "an input range is outside its desktop buffer" in draw + assert RANGE in draw + backing_order(draw) device = DEVICE.read_text(encoding="utf-8") assert 'skip = "a buffer shader resource view extends past the resource"' in device consumer = CONSUMER.read_text(encoding="utf-8") From 7098258e0847f44d5f5513bc9f38625aa2e47da3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 22:47:09 +0000 Subject: [PATCH 5/6] compute: test each input view token reason without nulling slot 18 A device-free table covers every refusal and the two views that bind. The controls fail if a threshold, a slot, or the wrapper check is dropped. Slot 18 again forwards every shader-resource view. The past-the-end fixture records its description without that call. Refs #3224 --- CMakeLists.txt | 10 + docs/experiments/compute-input-view-token.md | 15 +- src/startup_stubs/d3d12_draw.inl | 21 +- src/startup_stubs/d3d12_xs_device.cpp | 27 +-- src/startup_stubs/input_view_token.h | 33 ++- .../startup_compute_input_view_token_check.py | 18 +- tests/startup_input_view_token_check.cpp | 190 ++++++++++++++++++ tests/startup_stream_consumer.cpp | 16 +- 8 files changed, 287 insertions(+), 43 deletions(-) create mode 100644 tests/startup_input_view_token_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e3425e05f6..42f998ce91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2472,6 +2472,16 @@ endforeach() add_test(NAME startup_present_view_srgb_control COMMAND startup_present_view_check warp --mutate srgb) set_tests_properties(startup_present_view_identity_control startup_present_view_srgb_control PROPERTIES WILL_FAIL TRUE) set_tests_properties(startup_present_view_hardware PROPERTIES SKIP_RETURN_CODE 77) +# Input view-token refusal table (src/startup_stubs/input_view_token.h). Device-free. The mutated +# runs expect a guard to be gone and must fail. +add_executable(startup_input_view_token_check tests/startup_input_view_token_check.cpp) +target_include_directories(startup_input_view_token_check PRIVATE src) +target_compile_features(startup_input_view_token_check PRIVATE cxx_std_17) +add_test(NAME startup_input_view_token COMMAND startup_input_view_token_check) +foreach(mutation IN ITEMS short-srv short-cbv wrapper placement depth wrong-kind bind) + add_test(NAME startup_input_view_token_${mutation}_control COMMAND startup_input_view_token_check --mutate ${mutation}) + set_tests_properties(startup_input_view_token_${mutation}_control PROPERTIES WILL_FAIL TRUE) +endforeach() # Buffer memory SRV validation for device slot 70 (src/startup_stubs/memory_srv_buffer.h); the mutated # runs are negative controls. add_executable(startup_memory_srv_buffer_check tests/startup_memory_srv_buffer_check.cpp) diff --git a/docs/experiments/compute-input-view-token.md b/docs/experiments/compute-input-view-token.md index 2b6aa7ba4f..4691ae55f4 100644 --- a/docs/experiments/compute-input-view-token.md +++ b/docs/experiments/compute-input-view-token.md @@ -14,8 +14,8 @@ used to share one census sentence, `an input view token is not a buffer view of 7. `an input view token has a truncated shader resource view` 8. `an input view token is not a buffer view of a desktop resource` (a shader resource view whose dimension is not a buffer; a texture token keeps this historical sentence) -9. `an input view token names no resource` -10. `an input view token does not name a resource` +9. `an input view token's resource is not a wrapper` +10. `an input view token names an object that is not a resource` 11. `an input view token's placed or copied resource has no desktop backing` 12. `an input view token has no desktop resource` @@ -36,10 +36,13 @@ alias is still refused with `an input is in a placed buffer that does not alias placed or copied resource with no desktop object stays refused; the fixture does not construct one, and the Linux reference is what checks that sentence. The sampler refusal copies a token out of a one-entry sampler heap. The compute table is a CBV/SRV/UAV heap, and a sampler descriptor written -there faults in the desktop runtime before the dispatch is recorded. A buffer view that extends -past its resource is recorded and not created: the desktop call removes the device. The fixture's -past-the-end token is that view of a custom-heap buffer with no title memory. The width sentence -is what refuses it. Removing that check leaves the heap-type sentence, and the control fails. +there faults in the desktop runtime before the dispatch is recorded. Slot 18 still forwards every shader-resource view to the desktop, graphics included. A buffer view +past the end of its resource is one of those calls, and the desktop runtime can remove the device +for it. The fixture does not make that call. It records the description of a custom-heap buffer +with no title memory, four elements past the buffer, and the width sentence refuses it. Removing +that check leaves the heap-type sentence, and the control fails. The sentences in the table above +are checked by [startup_input_view_token_check.cpp](../../tests/startup_input_view_token_check.cpp), +including a bind that is not refused. `tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for `dispatch-input-token`. This note does not change that test. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 946805a638..13aa3bdbb3 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2564,16 +2564,17 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor Wrapper* const token_owner = described ? as_wrapper(reinterpret_cast(record.resource)) : nullptr; const bool token_resource = token_owner && token_owner->own().kind == resource_kind; const ResourceInfo* token_info = token_resource ? resource_info(token_owner) : nullptr; - InputViewTokenFacts facts; - facts.known = known; - facts.slot = record.slot; - facts.desc_bytes = record.desc_bytes; - facts.view_dimension = described ? static_cast(view.ViewDimension) : 0; - facts.resource_pointer = record.resource != 0; - facts.wrapper = token_owner != nullptr; - facts.resource = token_resource; - facts.desktop = token_resource && token_owner->own().real != nullptr; - facts.placement = token_info ? static_cast(token_info->placement) : 0; + InputViewTokenSource source; + source.known = known; + source.slot = record.slot; + source.desc_bytes = record.desc_bytes; + source.view_dimension = described ? static_cast(view.ViewDimension) : 0; + source.resource_pointer = record.resource != 0; + source.owner_is_wrapper = token_owner != nullptr; + source.owner_is_resource = token_resource; + source.desktop = token_resource && token_owner->own().real != nullptr; + source.placement = token_info ? static_cast(token_info->placement) : 0; + const InputViewTokenFacts facts = input_view_token_facts(source); if (const char* token_reason = input_view_token_refusal(facts)) return refuse(token_reason); if (record.slot == 17 && record.desc_bytes >= 12) { if (requires_identity_dst_sel) return refuse("an MTBUF byte window requires a buffer SRV"); diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 28d2d0d0c0..53921d6d51 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -5321,19 +5321,6 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d) if (first_calls(mismatched, 6)) log("x2w-d3d12 Device::slot%d view dimension %u needs resource dimension %u, resource has %u", N, view_dimension, x2w_view_dimension::needed(N, view_dimension), dimension); - } else if (N == 18 && view_dimension == D3D12_SRV_DIMENSION_BUFFER && - x2w_local::readable(reinterpret_cast(described), sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC))) { - // Desktop CreateShaderResourceView removes the device when a buffer view runs past the resource. - // Record the guest description and leave a null descriptor, the same as the other refused views. - D3D12_SHADER_RESOURCE_VIEW_DESC view{}; - std::memcpy(&view, reinterpret_cast(described), sizeof(view)); - const UINT element = x2w_startup::buffer_srv_element_bytes(view); - const UINT64 width = resource_desc.Width; - const UINT64 first = view.Buffer.FirstElement; - const UINT64 count = view.Buffer.NumElements; - if (element && (first > UINT64_MAX / element || count > UINT64_MAX / element || - first * element > width || count * element > width - first * element)) - skip = "a buffer shader resource view extends past the resource"; } else if constexpr (N == 19) { if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE3D && x2w_local::readable(reinterpret_cast(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) { @@ -6614,6 +6601,20 @@ struct ViewStoreProbe { unsigned long long records, materialized, ids, records_capacity, materialized_capacity; unsigned shadow, view_refs, retire_when_idle; }; +// Fixture only (dispatch-input-token-past). Records a buffer shader-resource view without calling +// CreateShaderResourceView. Slot 18 still forwards every shader-resource view to the desktop, +// including one whose range runs past the resource; that desktop call can remove the device. +extern "C" __declspec(dllexport) BOOL x2w_d3d12_record_uncreated_buffer_srv(void* resource, const void* desc, + UINT desc_bytes, UINT64 title_cpu) +{ + if (!resource || !desc || desc_bytes < ViewRecord::srv_desc_bytes || !title_cpu) return FALSE; + if (!x2w_local::readable(desc, ViewRecord::srv_desc_bytes)) return FALSE; + const u64 desktop = desktop_cpu_handle(title_cpu); + if (!desktop || desktop == title_cpu) return FALSE; + note_view(18, reinterpret_cast(resource), 0, desc, ViewRecord::srv_desc_bytes, title_cpu, desktop, false); + return TRUE; +} + extern "C" __declspec(dllexport) void x2w_d3d12_view_probe(unsigned long long title_handle, void* resource, ViewStoreProbe* out) { *out = {}; diff --git a/src/startup_stubs/input_view_token.h b/src/startup_stubs/input_view_token.h index 2d66078fbc..6bb64d536f 100644 --- a/src/startup_stubs/input_view_token.h +++ b/src/startup_stubs/input_view_token.h @@ -20,6 +20,35 @@ struct InputViewTokenFacts { std::uint32_t placement = 0; }; +// The fields a view record can show, before they are combined. owner_is_resource is meaningful +// only together with owner_is_wrapper: a wrapper of another kind is not a resource. +struct InputViewTokenSource { + bool known = false; + std::uint32_t slot = 0; + std::uint32_t desc_bytes = 0; + std::uint32_t view_dimension = 0; + bool resource_pointer = false; + bool owner_is_wrapper = false; + bool owner_is_resource = false; + bool desktop = false; + std::uint32_t placement = 0; +}; + +inline InputViewTokenFacts input_view_token_facts(const InputViewTokenSource& source) +{ + InputViewTokenFacts facts; + facts.known = source.known; + facts.slot = source.slot; + facts.desc_bytes = source.desc_bytes; + facts.view_dimension = source.view_dimension; + facts.resource_pointer = source.resource_pointer; + facts.wrapper = source.owner_is_wrapper; + facts.resource = source.owner_is_wrapper && source.owner_is_resource; + facts.desktop = facts.resource && source.desktop; + facts.placement = source.placement; + return facts; +} + inline const char* input_view_token_refusal(const InputViewTokenFacts& facts) { constexpr std::uint32_t buffer_dimension = 1; // D3D12_SRV_DIMENSION_BUFFER @@ -38,8 +67,8 @@ inline const char* input_view_token_refusal(const InputViewTokenFacts& facts) if (facts.desc_bytes < shader_resource_bytes) return "an input view token has a truncated shader resource view"; if (facts.view_dimension != buffer_dimension) return "an input view token is not a buffer view of a desktop resource"; - if (!facts.resource_pointer || !facts.wrapper) return "an input view token names no resource"; - if (!facts.resource) return "an input view token does not name a resource"; + if (!facts.resource_pointer || !facts.wrapper) return "an input view token's resource is not a wrapper"; + if (!facts.resource) return "an input view token names an object that is not a resource"; if (!facts.desktop) { if (facts.placement == 1 || facts.placement == 2) return "an input view token's placed or copied resource has no desktop backing"; diff --git a/tests/startup_compute_input_view_token_check.py b/tests/startup_compute_input_view_token_check.py index ca8437e191..78d651df8a 100644 --- a/tests/startup_compute_input_view_token_check.py +++ b/tests/startup_compute_input_view_token_check.py @@ -71,9 +71,9 @@ def classify(item): if item["view_dimension"] != BUFFER: return HISTORICAL if not item["resource_pointer"] or not item["wrapper"]: - return "an input view token names no resource" + return "an input view token's resource is not a wrapper" if not item["resource"]: - return "an input view token does not name a resource" + return "an input view token names an object that is not a resource" if not item["desktop"]: if item["placement"] in (1, 2): return "an input view token's placed or copied resource has no desktop backing" @@ -95,9 +95,11 @@ def classify(item): ("texture", facts(view_dimension=4), HISTORICAL), ("texture-without-desktop", facts(view_dimension=4, desktop=False), HISTORICAL), ("null-resource", facts(resource_pointer=False, wrapper=False, resource=False, desktop=False), - "an input view token names no resource"), - ("not-a-wrapper", facts(wrapper=False, resource=False, desktop=False), "an input view token names no resource"), - ("wrong-kind", facts(resource=False, desktop=False), "an input view token does not name a resource"), + "an input view token's resource is not a wrapper"), + ("not-a-wrapper", facts(wrapper=False, resource=False, desktop=False), + "an input view token's resource is not a wrapper"), + ("wrong-kind", facts(resource=False, desktop=False), + "an input view token names an object that is not a resource"), ("placed-without-desktop", facts(desktop=False, placement=1), "an input view token's placed or copied resource has no desktop backing"), ("copied-without-desktop", facts(desktop=False, placement=2), @@ -172,10 +174,14 @@ def controls(): assert "an input is in a placed buffer that does not alias title memory" in draw assert RANGE in draw backing_order(draw) + assert "source.owner_is_wrapper = token_owner != nullptr" in draw + assert "facts.wrapper" not in draw device = DEVICE.read_text(encoding="utf-8") - assert 'skip = "a buffer shader resource view extends past the resource"' in device + assert 'skip = "a buffer shader resource view extends past the resource"' not in device + assert "x2w_d3d12_record_uncreated_buffer_srv" in device consumer = CONSUMER.read_text(encoding="utf-8") assert 'mode == "dispatch-input-token-past"' in consumer + assert "x2w_d3d12_record_uncreated_buffer_srv" in consumer assert "D3D12_HEAP_TYPE_CUSTOM" in consumer existing = EXISTING.read_text(encoding="utf-8") assert "('dispatch-input-token', 'an input view token is not a buffer view of a desktop resource')" in existing diff --git a/tests/startup_input_view_token_check.cpp b/tests/startup_input_view_token_check.cpp new file mode 100644 index 0000000000..514ba948af --- /dev/null +++ b/tests/startup_input_view_token_check.cpp @@ -0,0 +1,190 @@ +// The input view-token refusal table (src/startup_stubs/input_view_token.h), with no device. +// Every sentence is one row, and a complete buffer view or constant-buffer view is not refused. +// --mutate expects one guard to be gone. Each mutated run must fail: +// short-srv a 39-byte shader-resource description binds +// short-cbv an 11-byte constant-buffer description binds +// wrapper a pointer that is not a wrapper is "not a resource" +// placement a copied resource with no desktop object is "no desktop resource" +// depth a depth view is an unrecognized slot +// wrong-kind a wrapper of another kind is "not a wrapper" +// bind a buffer view of a desktop resource keeps the historical sentence +#include "startup_stubs/input_view_token.h" + +#include +#include +#include + +namespace { + +int failures = 0; +const char* mutation = ""; + +const char* NOT_WRAPPER = "an input view token's resource is not a wrapper"; +const char* NOT_RESOURCE = "an input view token names an object that is not a resource"; +const char* TRUNC_SRV = "an input view token has a truncated shader resource view"; +const char* TRUNC_CBV = "an input view token has a truncated constant buffer view"; +const char* PLACED = "an input view token's placed or copied resource has no desktop backing"; +const char* NO_DESKTOP = "an input view token has no desktop resource"; +const char* RENDER = "an input view token is a render or depth view"; +const char* UNRECOGNIZED = "an input view token has an unrecognized slot"; +const char* HISTORICAL = "an input view token is not a buffer view of a desktop resource"; + +bool mutated(const char* name) +{ + return std::strcmp(mutation, name) == 0; +} + +bool same(const char* got, const char* expected) +{ + if (got == nullptr || expected == nullptr) return got == expected; + return std::strcmp(got, expected) == 0; +} + +void check(const char* name, const char* got, const char* expected) +{ + if (same(got, expected)) return; + ++failures; + std::printf("FAIL %s: got \"%s\", expected \"%s\"\n", name, got ? got : "(bind)", expected ? expected : "(bind)"); +} + +InputViewTokenSource buffer_source() +{ + InputViewTokenSource source; + source.known = true; + source.slot = 18; + source.desc_bytes = 40; + source.view_dimension = 1; + source.resource_pointer = true; + source.owner_is_wrapper = true; + source.owner_is_resource = true; + source.desktop = true; + source.placement = 0; + return source; +} + +const char* decide(const InputViewTokenSource& source) +{ + return input_view_token_refusal(input_view_token_facts(source)); +} + +const char* buggy(const char* name, const char* reason) +{ + if (!mutated(name)) return reason; + if (std::strcmp(name, "short-srv") == 0 || std::strcmp(name, "short-cbv") == 0) return nullptr; + if (std::strcmp(name, "wrapper") == 0) return NOT_RESOURCE; + if (std::strcmp(name, "placement") == 0) return NO_DESKTOP; + if (std::strcmp(name, "depth") == 0) return UNRECOGNIZED; + if (std::strcmp(name, "wrong-kind") == 0) return NOT_WRAPPER; + if (std::strcmp(name, "bind") == 0) return HISTORICAL; + return reason; +} + +void row(const char* name, InputViewTokenSource source, const char* reason) +{ + check(name, decide(source), buggy(name, reason)); +} + +} // namespace + +int main(int argc, char** argv) +{ + if (argc == 3 && std::strcmp(argv[1], "--mutate") == 0 && + (std::strcmp(argv[2], "short-srv") == 0 || std::strcmp(argv[2], "short-cbv") == 0 || + std::strcmp(argv[2], "wrapper") == 0 || std::strcmp(argv[2], "placement") == 0 || + std::strcmp(argv[2], "depth") == 0 || std::strcmp(argv[2], "wrong-kind") == 0 || + std::strcmp(argv[2], "bind") == 0)) { + mutation = argv[2]; + } else if (argc != 1) { + std::printf("usage: startup_input_view_token_check [--mutate short-srv|short-cbv|wrapper|placement|depth|wrong-kind|bind]\n"); + return 2; + } + + InputViewTokenSource unknown; + row("unknown", unknown, "an input view token is unknown"); + + InputViewTokenSource sampler = buffer_source(); + sampler.slot = 22; + row("sampler", sampler, "an input view token is a sampler"); + + InputViewTokenSource uav = buffer_source(); + uav.slot = 19; + row("uav", uav, "an input view token is an unordered access view"); + + InputViewTokenSource render = buffer_source(); + render.slot = 20; + row("render", render, RENDER); + + InputViewTokenSource depth = buffer_source(); + depth.slot = 21; + row("depth", depth, RENDER); + + InputViewTokenSource short_cbv = buffer_source(); + short_cbv.slot = 17; + short_cbv.desc_bytes = 11; + row("short-cbv", short_cbv, TRUNC_CBV); + + InputViewTokenSource cbv = short_cbv; + cbv.desc_bytes = 12; + row("cbv", cbv, nullptr); + + InputViewTokenSource other = buffer_source(); + other.slot = 16; + row("other-slot", other, UNRECOGNIZED); + + InputViewTokenSource short_srv = buffer_source(); + short_srv.desc_bytes = 39; + row("short-srv", short_srv, TRUNC_SRV); + + InputViewTokenSource texture = buffer_source(); + texture.view_dimension = 4; + row("texture", texture, HISTORICAL); + + InputViewTokenSource bare = buffer_source(); + bare.resource_pointer = false; + bare.owner_is_wrapper = false; + bare.owner_is_resource = false; + bare.desktop = false; + row("null-resource", bare, NOT_WRAPPER); + + InputViewTokenSource not_wrapper = buffer_source(); + not_wrapper.owner_is_wrapper = false; + not_wrapper.owner_is_resource = false; + not_wrapper.desktop = false; + row("wrapper", not_wrapper, NOT_WRAPPER); + + InputViewTokenSource wrong_kind = buffer_source(); + wrong_kind.owner_is_resource = false; + wrong_kind.desktop = false; + row("wrong-kind", wrong_kind, NOT_RESOURCE); + + InputViewTokenSource placed = buffer_source(); + placed.desktop = false; + placed.placement = 1; + row("placed", placed, PLACED); + + InputViewTokenSource copied = buffer_source(); + copied.desktop = false; + copied.placement = 2; + row("placement", copied, PLACED); + + InputViewTokenSource none = buffer_source(); + none.desktop = false; + row("no-desktop", none, NO_DESKTOP); + + row("bind", buffer_source(), nullptr); + + InputViewTokenSource aliased = buffer_source(); + aliased.placement = 1; + row("aliased-buffer", aliased, nullptr); + + InputViewTokenSource copied_desktop = buffer_source(); + copied_desktop.placement = 2; + row("copied-buffer", copied_desktop, nullptr); + + if (failures) { + std::printf("%d input view token checks failed\n", failures); + return 1; + } + std::printf("input view token table: 12 refusals, buffer and constant-buffer views bind\n"); + return 0; +} diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 817dfe9f81..2746ed945a 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -28,6 +28,8 @@ __declspec(dllexport) const char* D3D12SDKPath = ".\\D3D12\\"; using Microsoft::WRL::ComPtr; extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); extern "C" __declspec(dllimport) void x2w_d3d12_compute_census(); +extern "C" __declspec(dllimport) BOOL x2w_d3d12_record_uncreated_buffer_srv(void* resource, const void* desc, + UINT desc_bytes, UINT64 title_cpu); // A slot-55 patch is a tagged union with room for the full eight-target blend state. struct Patch { UINT tag; D3D12_BLEND_DESC blend; }; @@ -233,8 +235,8 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // dispatch-input-null (all four words zero, an unbound buffer) is not refused and reads zeros; // dispatch-input-token writes the token of a texture view where the input descriptor was. // dispatch-input-token-typed and -raw replace it with a buffer view of the aliased input buffer (R32_UINT, or a raw -// R32_TYPELESS view). dispatch-input-token-past is that typed view four elements past a custom-heap buffer: the -// desktop call is not made, and the range is checked against the buffer's width. +// R32_TYPELESS view). dispatch-input-token-past records that typed view four elements past a custom-heap +// buffer without calling slot 18, and the range is checked against the buffer's width. // dispatch-input-token-copied is a typed view of the buffer placed where the desktop cannot alias it. // dispatch-input-token-unknown writes a token id no view record has. dispatch-input-token-uav writes an // unordered-access token in the input slot. dispatch-input-token-sampler records a sampler in its own sampler @@ -669,9 +671,9 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin std::memcpy(reinterpret_cast(cpu + sizeof(descriptors[0])), descriptors[1], sizeof(descriptors[1])); }; if (mode == "dispatch-input-token-past") { - // A placed buffer refuses a range past its allocation before the desktop width is checked. This buffer is - // committed on a custom heap, so it has no placed address and the view's byte range is compared with the - // desktop width. The view runs past that width; the device records the description and does not create it. + // Slot 18 would ask the desktop to create this view, and a range past the buffer removes the device. + // The description is recorded instead. A placed buffer refuses that range before the desktop width is + // checked, so the buffer is committed on a custom heap and has no placed address. D3D12_HEAP_PROPERTIES heap{}; heap.Type = D3D12_HEAP_TYPE_CUSTOM; heap.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; @@ -692,7 +694,9 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; view.Buffer.NumElements = words + 4; - invoke(device, 18, word(resource), word(&view), cpu); + if (!x2w_d3d12_record_uncreated_buffer_srv(resource, &view, sizeof(view), cpu)) return 82; + constexpr UINT token_magic = 0x56573258; // "X2WV" + if (*reinterpret_cast(cpu) != token_magic) return 82; restore_output_descriptor(); } if (mode == "dispatch-input-token-typed" || mode == "dispatch-input-token-raw" || From c4e5785a88b4442fb088e364127d026b86464076 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 29 Nov 2026 12:26:14 +0000 Subject: [PATCH 6/6] compute: keep the identity-mapping refusal ahead of the token table A byte-window load still refuses a non-identity component mapping before the token's shape is named. Callers that do not ask for that mapping use the token table as before. Refs #3224 --- src/startup_stubs/d3d12_draw.inl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 13aa3bdbb3..6efbc1884f 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2575,6 +2575,11 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor source.desktop = token_resource && token_owner->own().real != nullptr; source.placement = token_info ? static_cast(token_info->placement) : 0; const InputViewTokenFacts facts = input_view_token_facts(source); + // A byte-window load asks for identity mapping before the token's shape is named. The flag is + // off for every other caller, so the token table is unchanged for them. + if (described && requires_identity_dst_sel && + view.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING) + return refuse("an MTBUF byte window requires identity component mapping"); if (const char* token_reason = input_view_token_refusal(facts)) return refuse(token_reason); if (record.slot == 17 && record.desc_bytes >= 12) { if (requires_identity_dst_sel) return refuse("an MTBUF byte window requires a buffer SRV"); @@ -2583,8 +2588,6 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor std::memcpy(&size, record.desc + 8, 4); bytes = size; } else if (record.slot == 18 && record.desc_bytes >= 40 && view.ViewDimension == D3D12_SRV_DIMENSION_BUFFER) { - if (requires_identity_dst_sel && view.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING) - return refuse("an MTBUF byte window requires identity component mapping"); owner = token_owner; const bool raw = view.Buffer.Flags == D3D12_BUFFER_SRV_FLAG_RAW && !view.Buffer.StructureByteStride && view.Format == DXGI_FORMAT_R32_TYPELESS;