From 31719cc56f9d0d7755ba161b697bdafec002d2d5 Mon Sep 17 00:00:00 2001 From: rasptum <348594691+rasptum@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:23:33 -0500 Subject: [PATCH 1/5] startup: keep the title's heap-indexing root flags for shader translation on Windows 10 The executable's root signatures set CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED and SAMPLER_HEAP_DIRECTLY_INDEXED (0xC00). The in-box Windows 10 D3D12 runtime predates shader model 6.6 and answers E_INVALIDARG. The executable does not check the result, so its pipeline object stays null and a startup run stops at about 75 s on a read through it (#1160). With X2W_D3D12_PERMISSIVE=1, d3d12_xs_SerializeVersionedRootSignature now repeats a refused serialization without those two bits and remembers, by blob content, which bits it removed (exported as x2w_stripped_root_flags). The recording device restores them in the copy of the blob it keeps for shader translation. The title's shaders take the heap pointers in the user SGPRs those flags add; without them gfx2hlsl lays the registers out one short ("s_load through s10 holding None") and the UI glyph pipelines are never translated. The desktop root signature itself still lacks the bits, so a shader that really indexes the heaps still needs shader model 6.6. Test: d3d12_xs_shim_heap_indexing. - Switch off: the shim answers E_INVALIDARG like the desktop runtime (negative control). - Switch on: the retried blob is byte-identical to the desktop serialization without the bits, alone and with the console bits 0x03000000, and the caller's descriptor is unchanged. - x2w_stripped_root_flags reports 0xC00 for that blob and 0 for a blob with other content and for null. - Unobserved invalid bits next to 0xC00 stay refused. - A runtime that accepts the flags makes the retry unreachable: the test then checks pass-through and returns 77 (skip). Mutants: no retry, and retry without recording, each fail it. Measured on Windows 10 19045, RTX 3060: the startup run continues past the 75 s stop, and the four glyph pipelines translate once 0x800 is restored. Fixes #1160 (the Windows 10 cause). Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 4 +- docs/startup-runtime.md | 2 +- src/d3d12_xs_shim.cpp | 86 ++++++++++++++++++++++++-- src/d3d12_xs_shim.def | 1 + src/startup_stubs/d3d12_capture.inl | 21 ++++++- tests/d3d12_xs_shim_check.cpp | 96 +++++++++++++++++++++++++++++ 6 files changed, 203 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8031c71f6d..00a41d6c07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -712,9 +712,11 @@ add_dependencies(d3d12_xs_shim_check pso_compute_headers) target_link_libraries(d3d12_xs_shim_check PRIVATE d3d12_xs_shim dxgi) target_include_directories(d3d12_xs_shim_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") target_compile_features(d3d12_xs_shim_check PRIVATE cxx_std_17) -foreach(scenario IN ITEMS exports serialize deserialize iids warp_root) +foreach(scenario IN ITEMS exports serialize deserialize iids warp_root heap_indexing) add_test(NAME d3d12_xs_shim_${scenario} COMMAND d3d12_xs_shim_check ${scenario}) endforeach() +# 77: the desktop runtime accepts the heap-indexing flags (shader model 6.6), so the retry is unreachable. +set_tests_properties(d3d12_xs_shim_heap_indexing PROPERTIES SKIP_RETURN_CODE 77) # D3D12XboxCreateDevice device-slot inventory and source-built creation fixture (#XCL-08). set(d3d12x_pso_header "${CMAKE_CURRENT_BINARY_DIR}/d3d12x_pso_cs.h") add_custom_command(OUTPUT "${d3d12x_pso_header}" diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 79e6fb2448..a4c1a7767d 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -97,7 +97,7 @@ ctest --preset release --no-tests=error -R '^startup_launch$' -V |---|---| | `X2W_XMEM_PERMISSIVE=1` | accepts the reservation, mapping and allocation shapes a startup run uses | | `X2W_XMEM_GPU_MIRROR=1` | pairs a no-access graphics reservation with the read-write reservation of the same size made just before it, and maps every physical page into both (same section) | -| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking | +| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets them back (`d3d12_xs_shim_heap_indexing`) | | `X2W_D3D12_RENDER=1` | draws through translated pipelines from `X2W_SHADER_CACHE` | | `X2W_SHADER_CACHE=` | translation cache: the runtime writes `req_.{a,b,root}` for untranslated pipelines; `tools/startup/gfx2hlsl.py --pending ` translates them | | `X2W_DRAW_NULL_IDENTITY=1` | null descriptors read as identity values instead of zero | diff --git a/src/d3d12_xs_shim.cpp b/src/d3d12_xs_shim.cpp index 4511343ef1..360c50cc61 100644 --- a/src/d3d12_xs_shim.cpp +++ b/src/d3d12_xs_shim.cpp @@ -39,6 +39,84 @@ struct VmState { bool device_created = false; }; VmState vm_state; + +// Desktop root-signature flags for direct heap indexing (CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED and +// SAMPLER_HEAP_DIRECTLY_INDEXED). A desktop runtime older than shader model 6.6 does not know them +// and answers E_INVALIDARG; the in-box Windows 10 runtime is one. With X2W_D3D12_PERMISSIVE=1 the +// serialization is then repeated without them, so that a caller that sets them on every root +// signature still gets a blob. The result no longer declares direct heap indexing. +constexpr std::uint32_t heap_indexing_flags = 0x00000C00u; + +// Blobs serialized without heap-indexing bits, by content hash, so that the recording device can give +// the bits back to its copy for shader translation (x2w_stripped_root_flags). +struct StrippedBlob { std::uint64_t hash; std::uint32_t bits; }; +SRWLOCK stripped_lock = SRWLOCK_INIT; +StrippedBlob stripped_blobs[4096]; +LONG stripped_count; + +std::uint64_t blob_hash(const void* data, std::size_t bytes) +{ + std::uint64_t h = 1469598103934665603ull; + for (std::size_t i = 0; i < bytes; ++i) h = (h ^ static_cast(data)[i]) * 1099511628211ull; + return h ^ bytes; +} + +bool permissive_switch() +{ + const DWORD saved = GetLastError(); + char value[4] = {}; + const bool on = GetEnvironmentVariableA("X2W_D3D12_PERMISSIVE", value, sizeof(value)) > 0 && value[0] == '1'; + SetLastError(saved); + return on; +} + +HRESULT serialize_for_runtime(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, ID3DBlob** blob, ID3DBlob** error_blob) +{ + const HRESULT hr = D3D12SerializeVersionedRootSignature(desc, blob, error_blob); + if (hr != E_INVALIDARG || !desc || !blob) return hr; + if (desc->Version != D3D_ROOT_SIGNATURE_VERSION_1_0 && desc->Version != D3D_ROOT_SIGNATURE_VERSION_1_1) return hr; + // Flags sits at the same offset in both versions (see the static_asserts above). + const auto flags = static_cast(desc->Desc_1_1.Flags); + if (!(flags & heap_indexing_flags) || !permissive_switch()) return hr; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC copy = *desc; + copy.Desc_1_1.Flags = static_cast(flags & ~heap_indexing_flags); + ID3DBlob* retry_error = nullptr; + const HRESULT retry = D3D12SerializeVersionedRootSignature(©, blob, &retry_error); + if (FAILED(retry)) { + if (retry_error) retry_error->Release(); + return hr; // something else is wrong: keep the first answer and its error blob + } + if (retry_error) retry_error->Release(); + if (*blob) { + const std::uint64_t hash = blob_hash((*blob)->GetBufferPointer(), (*blob)->GetBufferSize()); + AcquireSRWLockExclusive(&stripped_lock); + if (stripped_count < static_cast(sizeof(stripped_blobs) / sizeof(stripped_blobs[0]))) + stripped_blobs[stripped_count++] = {hash, flags & heap_indexing_flags}; + ReleaseSRWLockExclusive(&stripped_lock); + } + if (error_blob && *error_blob) { + (*error_blob)->Release(); + *error_blob = nullptr; + } + static LONG logged; + if (InterlockedIncrement(&logged) == 1) + std::fprintf(stderr, "x2w-d3d12 root signature flags 0x%x: this desktop runtime refuses the heap-indexing bits 0x%x; " + "serialized without them (X2W_D3D12_PERMISSIVE)\n", flags, flags & heap_indexing_flags); + return retry; +} +} + +// The heap-indexing bits serialize_for_runtime removed from this blob, 0 if it removed none. +extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t bytes) +{ + if (!blob || !bytes) return 0; + const std::uint64_t hash = blob_hash(blob, bytes); + std::uint32_t bits = 0; + AcquireSRWLockShared(&stripped_lock); + for (LONG i = 0; i < stripped_count; ++i) + if (stripped_blobs[i].hash == hash) bits = stripped_blobs[i].bits; + ReleaseSRWLockShared(&stripped_lock); + return bits; } extern "C" HRESULT WINAPI d3d12_xs_SerializeVersionedRootSignature( @@ -47,24 +125,24 @@ extern "C" HRESULT WINAPI d3d12_xs_SerializeVersionedRootSignature( ID3DBlob** error_blob) { if (!root_signature) { - return D3D12SerializeVersionedRootSignature(root_signature, blob, error_blob); + return serialize_for_runtime(root_signature, blob, error_blob); } if (root_signature->Version == D3D_ROOT_SIGNATURE_VERSION_1_1) { const auto flags = static_cast(root_signature->Desc_1_1.Flags); if (flags & observed_console_flags) { D3D12_VERSIONED_ROOT_SIGNATURE_DESC copy = *root_signature; copy.Desc_1_1.Flags = static_cast(flags & ~observed_console_flags); - return D3D12SerializeVersionedRootSignature(©, blob, error_blob); + return serialize_for_runtime(©, blob, error_blob); } } else if (root_signature->Version == D3D_ROOT_SIGNATURE_VERSION_1_0) { const auto flags = static_cast(root_signature->Desc_1_0.Flags); if (flags & observed_console_flags) { D3D12_VERSIONED_ROOT_SIGNATURE_DESC copy = *root_signature; copy.Desc_1_0.Flags = static_cast(flags & ~observed_console_flags); - return D3D12SerializeVersionedRootSignature(©, blob, error_blob); + return serialize_for_runtime(©, blob, error_blob); } } - return D3D12SerializeVersionedRootSignature(root_signature, blob, error_blob); + return serialize_for_runtime(root_signature, blob, error_blob); } extern "C" HRESULT WINAPI d3d12_xs_CreateRootSignatureDeserializer( diff --git a/src/d3d12_xs_shim.def b/src/d3d12_xs_shim.def index b71c8c30c0..37e149e4a9 100644 --- a/src/d3d12_xs_shim.def +++ b/src/d3d12_xs_shim.def @@ -4,6 +4,7 @@ EXPORTS D3D12CreateRootSignatureDeserializer=d3d12_xs_CreateRootSignatureDeserializer D3D12XboxCreateDevice=d3d12_xs_XboxCreateDevice D3D12ConfigureVirtualMemoryX=d3d12_xs_ConfigureVirtualMemoryX + x2w_stripped_root_flags IID_ID3D12CommandAllocator DATA IID_ID3D12CommandList DATA IID_ID3D12CommandQueue DATA diff --git a/src/startup_stubs/d3d12_capture.inl b/src/startup_stubs/d3d12_capture.inl index c2d4e35b8c..e00155ddf8 100644 --- a/src/startup_stubs/d3d12_capture.inl +++ b/src/startup_stubs/d3d12_capture.inl @@ -111,11 +111,30 @@ const PipelineInfo* pipeline_info(const Wrapper* w) ReleaseSRWLockShared(¬e_lock); return info; } +extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t bytes); void note_root_blob(Wrapper* w, const void* blob, std::size_t bytes) { if (!blob || !bytes || bytes > (1u << 20)) return; + std::vector copy(static_cast(blob), static_cast(blob) + bytes); + // A blob the d3d12_xs serializer produced without the title's heap-indexing flags (see + // serialize_for_runtime in src/d3d12_xs_shim.cpp): the copy kept for shader translation gets them + // back, because the title's shaders take the heap pointers in the registers those flags add. + if (const std::uint32_t stripped = x2w_stripped_root_flags(blob, bytes); stripped && bytes >= 32 && !std::memcmp(copy.data(), "DXBC", 4)) { + std::uint32_t parts = 0; + std::memcpy(&parts, copy.data() + 28, 4); + for (std::uint32_t i = 0; i < parts && 32 + 4 * (i + 1) <= bytes; ++i) { + std::uint32_t at = 0; + std::memcpy(&at, copy.data() + 32 + 4 * i, 4); + if (std::size_t{at} + 8 + 24 <= bytes && !std::memcmp(copy.data() + at, "RTS0", 4)) { + std::uint32_t flags = 0; + std::memcpy(&flags, copy.data() + at + 8 + 20, 4); + flags |= stripped; + std::memcpy(copy.data() + at + 8 + 20, &flags, 4); + } + } + } AcquireSRWLockExclusive(¬e_lock); - root_blobs()[w].assign(static_cast(blob), static_cast(blob) + bytes); + root_blobs()[w] = std::move(copy); ReleaseSRWLockExclusive(¬e_lock); } void note_resource(Wrapper* w, const D3D12_RESOURCE_DESC* console, u64 bytes) diff --git a/tests/d3d12_xs_shim_check.cpp b/tests/d3d12_xs_shim_check.cpp index 8bc36c85d0..2dcbd05000 100644 --- a/tests/d3d12_xs_shim_check.cpp +++ b/tests/d3d12_xs_shim_check.cpp @@ -196,6 +196,98 @@ bool serialize_checks() return true; } +// Heap-indexing flags (CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED). A desktop +// runtime older than shader model 6.6 refuses them with E_INVALIDARG; with X2W_D3D12_PERMISSIVE=1 +// d3d12_xs.dll serializes again without them and records which bits it removed for that blob. +// Returns 0 (pass), 1 (fail) or 77 when the desktop runtime accepts the flags (retry unreachable). +int heap_indexing_checks() +{ + constexpr std::uint32_t heap_indexing = 0x00000C00u; + DesktopD3D12 desktop; + if (!desktop.load()) return 1; + using Stripped = std::uint32_t(__cdecl*)(const void*, std::size_t); + const auto stripped_bits = reinterpret_cast( + GetProcAddress(GetModuleHandleW(L"d3d12_xs.dll"), "x2w_stripped_root_flags")); + if (!stripped_bits) { + std::fprintf(stderr, "x2w_stripped_root_flags not exported\n"); + return 1; + } + + constexpr auto desktop_flag = D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS; + D3D12_ROOT_PARAMETER1 param = make_constants_param1(); + D3D12_VERSIONED_ROOT_SIGNATURE_DESC base{}; + base.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; + base.Desc_1_1.NumParameters = 1; + base.Desc_1_1.pParameters = ¶m; + base.Desc_1_1.Flags = desktop_flag; + ComPtr baseline; + if (FAILED(desktop.serialize(&base, &baseline, nullptr)) || !baseline) return 1; + + D3D12_VERSIONED_ROOT_SIGNATURE_DESC indexed = base; + const auto indexed_flags = static_cast(static_cast(desktop_flag) | heap_indexing); + indexed.Desc_1_1.Flags = indexed_flags; + ComPtr desktop_indexed; + const HRESULT desktop_hr = desktop.serialize(&indexed, &desktop_indexed, nullptr); + SetEnvironmentVariableW(L"X2W_D3D12_PERMISSIVE", nullptr); + + if (SUCCEEDED(desktop_hr)) { + // A runtime with shader model 6.6 keeps the flags; the shim must pass them through unchanged. + ComPtr shim_indexed; + const HRESULT shim_hr = D3D12SerializeVersionedRootSignature(&indexed, &shim_indexed, nullptr); + const bool same = SUCCEEDED(shim_hr) && same_blob(shim_indexed.Get(), desktop_indexed.Get()) && + stripped_bits(shim_indexed->GetBufferPointer(), shim_indexed->GetBufferSize()) == 0; + std::printf("desktop accepts heap-indexing flags (hr=0x%08lx); pass-through %s; retry not reachable: skip\n", + static_cast(desktop_hr), same ? "identical" : "DIFFERENT"); + return same ? 77 : 1; + } + + // Negative control: without the switch the shim answers like the desktop runtime. + ComPtr refused; + const HRESULT off_hr = D3D12SerializeVersionedRootSignature(&indexed, &refused, nullptr); + if (desktop_hr != E_INVALIDARG || off_hr != E_INVALIDARG || refused) { + std::fprintf(stderr, "switch off: desktop=0x%08lx shim=0x%08lx\n", static_cast(desktop_hr), + static_cast(off_hr)); + return 1; + } + + SetEnvironmentVariableW(L"X2W_D3D12_PERMISSIVE", L"1"); + int failures = 0; + // The retry: the blob equals the desktop serialization without the two bits, the caller's + // descriptor is untouched, and the removed bits are recorded for that blob (and only for it). + for (const std::uint32_t extra : {0u, 0x03000000u}) { + D3D12_VERSIONED_ROOT_SIGNATURE_DESC tagged = indexed; + const auto tagged_flags = static_cast(static_cast(indexed_flags) | extra); + tagged.Desc_1_1.Flags = tagged_flags; + ComPtr retried; + const HRESULT hr = D3D12SerializeVersionedRootSignature(&tagged, &retried, nullptr); + const std::uint32_t recorded = retried ? stripped_bits(retried->GetBufferPointer(), retried->GetBufferSize()) : ~0u; + const bool ok = SUCCEEDED(hr) && same_blob(retried.Get(), baseline.Get()) && tagged.Desc_1_1.Flags == tagged_flags && + recorded == heap_indexing; + std::printf("retry extra=0x%08x: hr=0x%08lx same_as_desktop_without_bits=%d recorded=0x%x\n", extra, + static_cast(hr), retried && same_blob(retried.Get(), baseline.Get()) ? 1 : 0, recorded); + if (!ok) ++failures; + } + // Bits are recorded by blob content (a caller may copy the blob before CreateRootSignature), so a + // blob with other content records nothing, nor does a null blob. + D3D12_VERSIONED_ROOT_SIGNATURE_DESC other = base; + other.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE; + ComPtr other_blob; + if (FAILED(D3D12SerializeVersionedRootSignature(&other, &other_blob, nullptr)) || !other_blob || + stripped_bits(other_blob->GetBufferPointer(), other_blob->GetBufferSize()) != 0 || stripped_bits(nullptr, 0) != 0) { + std::fprintf(stderr, "stripped bits recorded for a blob without a retry\n"); + ++failures; + } + // The retry does not hide another fault: unobserved invalid bits next to 0xC00 stay refused. + D3D12_VERSIONED_ROOT_SIGNATURE_DESC bad = indexed; + bad.Desc_1_1.Flags = static_cast(static_cast(indexed_flags) | 0x04000000u); + ComPtr bad_blob; + const HRESULT bad_hr = D3D12SerializeVersionedRootSignature(&bad, &bad_blob, nullptr); + std::printf("invalid bits with heap-indexing: hr=0x%08lx\n", static_cast(bad_hr)); + if (bad_hr != E_INVALIDARG || bad_blob) ++failures; + SetEnvironmentVariableW(L"X2W_D3D12_PERMISSIVE", nullptr); + return failures ? 1 : 0; +} + bool deserialize_checks() { constexpr auto desktop_flag = D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS; @@ -391,6 +483,10 @@ int main(int argc, char** argv) passed = iid_checks(); } else if (scenario == "warp_root"sv) { passed = warp_root_checks(); + } else if (scenario == "heap_indexing"sv) { + const int result = heap_indexing_checks(); + std::printf("Desktop d3d12_xs.dll heap_indexing: %s\n", result == 0 ? "PASS" : result == 77 ? "SKIP" : "FAIL"); + return result; } else { return 2; } From f92d39c06b36f60951d274695ed559bfb9419d3d Mon Sep 17 00:00:00 2001 From: rasptum <348594691+rasptum@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:41:11 -0500 Subject: [PATCH 2/5] startup: tie stripped heap-indexing root flags to the blob the retry returned Review of #1277: the bytes of a blob serialized without the heap-indexing bits do not identify the input. The legal baseline without them, an input with only 0x400, one with only 0x800 and one with both serialize to the same bytes, so the content-keyed lookup gave a baseline blob the bits of another call and could not tell the two single-flag inputs apart. Design: of the two options in the review, this keeps an explicitly identifiable representation, because the recording device only ever sees (pointer, size) at CreateRootSignature and cannot be handed the original descriptor. After a retry, d3d12_xs_SerializeVersionedRootSignature returns its own ID3DBlob around the desktop blob. That object registers its buffer with the removed bits while it lives and unregisters it in its destructor, before releasing the desktop blob, so the address cannot be reused while a record for it exists. x2w_stripped_root_flags answers only for that exact buffer and size. A blob from any other call, or a copy of the bytes in other memory, reports 0: the translation copy then keeps the desktop flags instead of guessing. Nothing changes with X2W_D3D12_PERMISSIVE unset or on a runtime that accepts the flags (no retry, no wrapper). The recording device declares x2w_stripped_root_flags with a linker fallback that returns 0, so offscreen_check, which compiles d3d12_xs_device.cpp without the shim, links again on current main. A fixture export, x2w_d3d12_root_translation_blob, returns the copy the recording device keeps for translation (the .root request and the translation key). Test: d3d12_xs_shim_heap_indexing gains a provenance control. With the switch on, the baseline, 0x400-only, 0x800-only and 0xC00 inputs are serialized and held alive together, in both call orders (baseline first, both first). Each blob is byte-identical to the others, goes through the recording device's CreateRootSignature (slot 16) on WARP, and the RTS0 flags of the translation copy, parsed by the test, must be the base flags plus exactly that call's bits; the lookup must agree. A copy of the 0xC00 blob in test memory must give the base flags only. Existing controls are unchanged: switch off refuses, retried bytes equal the desktop serialization, unrelated blob and null record nothing, unobserved invalid bits stay refused, and a runtime with shader model 6.6 skips (77). Mutant: the previous content-keyed shim fails the provenance control. Merged origin/main (d90dd76b) first; it merged without conflicts. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 2 +- src/d3d12_xs_shim.cpp | 79 +++++++++++++++------- src/startup_stubs/d3d12_capture.inl | 7 +- src/startup_stubs/d3d12_xs_device.cpp | 20 ++++++ tests/d3d12_xs_shim_check.cpp | 94 ++++++++++++++++++++++++++- 5 files changed, 173 insertions(+), 29 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 8c1db2eac4..bb9144efa9 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -268,7 +268,7 @@ ctest --preset dev --no-tests=error -R '^startup_local_log(_guard_drop_control)? |---|---| | `X2W_XMEM_PERMISSIVE=1` | accepts the reservation, mapping and allocation shapes a startup run uses | | `X2W_XMEM_GPU_MIRROR=1` | pairs a no-access graphics reservation with the read-write reservation of the same size made just before it, and maps every physical page into both (same section) | -| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets them back (`d3d12_xs_shim_heap_indexing`) | +| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets that call's bits back when `CreateRootSignature` receives the returned blob's own buffer (a copy of the bytes keeps the desktop flags; `d3d12_xs_shim_heap_indexing`) | | `X2W_D3D12_RENDER=1` | draws through translated pipelines from `X2W_SHADER_CACHE` | | `X2W_SHADER_CACHE=` | translation cache: the runtime writes source requests `req_.{a,b,root}` and, when needed, RTV variants `req_.rtv`. Run `tools/startup/gfx2hlsl.py --pending ` while the process is running. Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. | | `X2W_DRAW_NULL_IDENTITY=1` | null descriptors read as identity values instead of zero | diff --git a/src/d3d12_xs_shim.cpp b/src/d3d12_xs_shim.cpp index 360c50cc61..523064b654 100644 --- a/src/d3d12_xs_shim.cpp +++ b/src/d3d12_xs_shim.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "d3d12x_device_slots.h" @@ -47,19 +48,59 @@ VmState vm_state; // signature still gets a blob. The result no longer declares direct heap indexing. constexpr std::uint32_t heap_indexing_flags = 0x00000C00u; -// Blobs serialized without heap-indexing bits, by content hash, so that the recording device can give -// the bits back to its copy for shader translation (x2w_stripped_root_flags). -struct StrippedBlob { std::uint64_t hash; std::uint32_t bits; }; +// The serialized bytes do not say which input produced them: the legal baseline and inputs with only +// 0x400, only 0x800 or both serialize to the same blob once the bits are removed. The removed bits are +// therefore tied to the blob object a retry returns. That object registers its buffer while it lives +// and unregisters it before the buffer can be freed and reused, so x2w_stripped_root_flags answers +// for that exact buffer only. A copy of the bytes, or a blob from any other call, reports 0. +struct StrippedBlob { const void* data; std::size_t bytes; std::uint32_t bits; }; SRWLOCK stripped_lock = SRWLOCK_INIT; -StrippedBlob stripped_blobs[4096]; -LONG stripped_count; +std::vector stripped_blobs; -std::uint64_t blob_hash(const void* data, std::size_t bytes) -{ - std::uint64_t h = 1469598103934665603ull; - for (std::size_t i = 0; i < bytes; ++i) h = (h ^ static_cast(data)[i]) * 1099511628211ull; - return h ^ bytes; -} +class RetriedBlob final : public ID3DBlob { +public: + RetriedBlob(ID3DBlob* inner, std::uint32_t bits) : inner_(inner) + { + AcquireSRWLockExclusive(&stripped_lock); + stripped_blobs.push_back({inner_->GetBufferPointer(), inner_->GetBufferSize(), bits}); + ReleaseSRWLockExclusive(&stripped_lock); + } + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** out) override + { + if (!out) return E_POINTER; + *out = nullptr; + if (iid != __uuidof(IUnknown) && iid != __uuidof(ID3D10Blob)) return E_NOINTERFACE; + *out = static_cast(this); + AddRef(); + return S_OK; + } + ULONG STDMETHODCALLTYPE AddRef() override { return static_cast(InterlockedIncrement(&refs_)); } + ULONG STDMETHODCALLTYPE Release() override + { + const LONG left = InterlockedDecrement(&refs_); + if (!left) delete this; + return static_cast(left); + } + LPVOID STDMETHODCALLTYPE GetBufferPointer() override { return inner_->GetBufferPointer(); } + SIZE_T STDMETHODCALLTYPE GetBufferSize() override { return inner_->GetBufferSize(); } + +private: + ~RetriedBlob() + { + const void* data = inner_->GetBufferPointer(); + AcquireSRWLockExclusive(&stripped_lock); + for (std::size_t i = 0; i < stripped_blobs.size(); ++i) + if (stripped_blobs[i].data == data) { + stripped_blobs[i] = stripped_blobs.back(); + stripped_blobs.pop_back(); + break; + } + ReleaseSRWLockExclusive(&stripped_lock); + inner_->Release(); // only now may the buffer be reused + } + ID3DBlob* inner_; + LONG refs_ = 1; +}; bool permissive_switch() { @@ -87,13 +128,7 @@ HRESULT serialize_for_runtime(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, I return hr; // something else is wrong: keep the first answer and its error blob } if (retry_error) retry_error->Release(); - if (*blob) { - const std::uint64_t hash = blob_hash((*blob)->GetBufferPointer(), (*blob)->GetBufferSize()); - AcquireSRWLockExclusive(&stripped_lock); - if (stripped_count < static_cast(sizeof(stripped_blobs) / sizeof(stripped_blobs[0]))) - stripped_blobs[stripped_count++] = {hash, flags & heap_indexing_flags}; - ReleaseSRWLockExclusive(&stripped_lock); - } + if (*blob) *blob = new RetriedBlob(*blob, flags & heap_indexing_flags); if (error_blob && *error_blob) { (*error_blob)->Release(); *error_blob = nullptr; @@ -106,15 +141,15 @@ HRESULT serialize_for_runtime(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, I } } -// The heap-indexing bits serialize_for_runtime removed from this blob, 0 if it removed none. +// The heap-indexing bits serialize_for_runtime removed for the live blob whose buffer is exactly +// (blob, bytes), 0 for any other pointer or size. extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t bytes) { if (!blob || !bytes) return 0; - const std::uint64_t hash = blob_hash(blob, bytes); std::uint32_t bits = 0; AcquireSRWLockShared(&stripped_lock); - for (LONG i = 0; i < stripped_count; ++i) - if (stripped_blobs[i].hash == hash) bits = stripped_blobs[i].bits; + for (const StrippedBlob& entry : stripped_blobs) + if (entry.data == blob && entry.bytes == bytes) bits = entry.bits; ReleaseSRWLockShared(&stripped_lock); return bits; } diff --git a/src/startup_stubs/d3d12_capture.inl b/src/startup_stubs/d3d12_capture.inl index c9abee2779..0e2878a699 100644 --- a/src/startup_stubs/d3d12_capture.inl +++ b/src/startup_stubs/d3d12_capture.inl @@ -130,14 +130,13 @@ const PipelineInfo* pipeline_info(const Wrapper* w) ReleaseSRWLockShared(¬e_lock); return info; } -extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t bytes); void note_root_blob(Wrapper* w, const void* blob, std::size_t bytes) { if (!blob || !bytes || bytes > (1u << 20)) return; std::vector copy(static_cast(blob), static_cast(blob) + bytes); - // A blob the d3d12_xs serializer produced without the title's heap-indexing flags (see - // serialize_for_runtime in src/d3d12_xs_shim.cpp): the copy kept for shader translation gets them - // back, because the title's shaders take the heap pointers in the registers those flags add. + // The blob the d3d12_xs serializer returned after removing the title's heap-indexing flags (see + // serialize_for_runtime in src/d3d12_xs_shim.cpp): the copy kept for shader translation gets that + // call's bits back, because the title's shaders take the heap pointers in the registers they add. if (const std::uint32_t stripped = x2w_stripped_root_flags(blob, bytes); stripped && bytes >= 32 && !std::memcmp(copy.data(), "DXBC", 4)) { std::uint32_t parts = 0; std::memcpy(&parts, copy.data() + 28, 4); diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index b08ed9089a..1e0fcc888d 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -55,6 +55,12 @@ #include #include +// The heap-indexing bits src/d3d12_xs_shim.cpp removed for the blob it returned at exactly this +// buffer. A build that compiles this file without the shim (offscreen_check) links the fallback, 0. +extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t bytes); +extern "C" std::uint32_t x2w_no_stripped_root_flags(const void*, std::size_t) { return 0; } +#pragma comment(linker, "/alternatename:x2w_stripped_root_flags=x2w_no_stripped_root_flags") + namespace { using u64 = std::uint64_t; @@ -1475,6 +1481,20 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v return S_OK; } +// Fixtures: the copy of a root signature's blob kept for shader translation (the .root request and the +// translation key). Returns its size and copies up to capacity bytes to out; 0 for anything else. +extern "C" __declspec(dllexport) std::size_t x2w_d3d12_root_translation_blob(void* root, void* out, std::size_t capacity) +{ + Wrapper* w = as_wrapper(root); + if (!w || w->own().kind != root_signature_kind) return 0; + AcquireSRWLockShared(¬e_lock); + const auto found = root_blobs().find(w); + const std::size_t bytes = found == root_blobs().end() ? 0 : found->second.size(); + if (bytes && out) std::memcpy(out, found->second.data(), bytes < capacity ? bytes : capacity); + ReleaseSRWLockShared(¬e_lock); + return bytes; +} + ID3DBlob* compile_translation(const void* container, const std::vector*, char* why, std::size_t why_size) { const auto* digest = static_cast(container) + 4; diff --git a/tests/d3d12_xs_shim_check.cpp b/tests/d3d12_xs_shim_check.cpp index 2dcbd05000..31e3117003 100644 --- a/tests/d3d12_xs_shim_check.cpp +++ b/tests/d3d12_xs_shim_check.cpp @@ -8,12 +8,14 @@ #include #include #include +#include #include "pso_compute_embedded.h" #include "pso_compute_stripped.h" using Microsoft::WRL::ComPtr; using namespace std::literals; +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); namespace { constexpr std::uint32_t observed_console_flags = 0x03000000u; @@ -196,6 +198,94 @@ bool serialize_checks() return true; } +// The RTS0 flags word of a serialized root signature, ~0u if the container has none. +std::uint32_t rts0_flags(const unsigned char* data, std::size_t bytes) +{ + std::uint32_t parts = 0; + if (bytes < 32 || std::memcmp(data, "DXBC", 4) != 0) return ~0u; + std::memcpy(&parts, data + 28, 4); + for (std::uint32_t i = 0; i < parts && 32 + 4 * (i + 1) <= bytes; ++i) { + std::uint32_t at = 0, flags = 0; + std::memcpy(&at, data + 32 + 4 * i, 4); + if (std::size_t{at} + 8 + 24 > bytes || std::memcmp(data + at, "RTS0", 4) != 0) continue; + std::memcpy(&flags, data + at + 8 + 20, 4); + return flags; + } + return ~0u; +} + +// Provenance (switch on, retry reachable). The baseline without heap-indexing bits and inputs with +// only 0x400, only 0x800 and both all serialize to the same bytes, so the bytes cannot say which call +// produced a blob. All four blobs are alive at once, in both call orders; each goes through the +// recording device's CreateRootSignature, and the copy that device keeps for shader translation must +// carry exactly its own call's bits. A copy of the bytes in other memory gets none. +bool provenance_checks(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& base, std::uint32_t(__cdecl* stripped_bits)(const void*, std::size_t)) +{ + using TranslationBlob = std::size_t(__cdecl*)(void*, void*, std::size_t); + const auto translation_blob = reinterpret_cast( + GetProcAddress(GetModuleHandleW(L"d3d12_xs.dll"), "x2w_d3d12_root_translation_blob")); + ComPtr factory; + ComPtr warp; + std::array parameters{}; + void* device = nullptr; + if (!translation_blob || FAILED(CreateDXGIFactory2(0, IID_PPV_ARGS(&factory))) || + FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp))) || + FAILED(D3D12XboxCreateDevice(warp.Get(), parameters.data(), __uuidof(ID3D12Device), &device)) || !device) { + std::fprintf(stderr, "provenance: no recording device (translation export %p)\n", reinterpret_cast(translation_blob)); + return false; + } + // Device slot 16: CreateRootSignature(node, blob, size, riid, ppv). + const auto create_root = [device](const void* blob, std::size_t size, void** root) { + using Slot = HRESULT(WINAPI*)(void*, UINT, const void*, SIZE_T, const GUID*, void**); + return reinterpret_cast((*static_cast(device))[16])(device, 0, blob, size, &__uuidof(ID3D12RootSignature), root); + }; + // The flags of the copy the recording device kept for translation, ~0u if it kept none. + const auto translation_flags = [translation_blob](void* root) { + std::array copy{}; + const std::size_t bytes = translation_blob(root, copy.data(), copy.size()); + return bytes && bytes <= copy.size() ? rts0_flags(copy.data(), bytes) : ~0u; + }; + + const auto base_flags = static_cast(base.Desc_1_1.Flags); + bool ok = true; + for (const std::array& order : + {std::array{0u, 0x400u, 0x800u, 0xC00u}, std::array{0xC00u, 0x800u, 0x400u, 0u}}) { + std::array, 4> blobs; + for (std::size_t i = 0; i < order.size(); ++i) { + D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc = base; + desc.Desc_1_1.Flags = static_cast(base_flags | order[i]); + if (FAILED(D3D12SerializeVersionedRootSignature(&desc, &blobs[i], nullptr)) || !blobs[i]) { + std::fprintf(stderr, "provenance: serialization of 0x%x failed\n", order[i]); + return false; + } + } + for (std::size_t i = 0; i < order.size(); ++i) { + ComPtr root; + const HRESULT hr = create_root(blobs[i]->GetBufferPointer(), blobs[i]->GetBufferSize(), reinterpret_cast(root.GetAddressOf())); + const std::uint32_t lookup = stripped_bits(blobs[i]->GetBufferPointer(), blobs[i]->GetBufferSize()); + const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get()) : ~0u; + const bool same_bytes = same_blob(blobs[i].Get(), blobs[0].Get()); + const bool good = SUCCEEDED(hr) && same_bytes && lookup == order[i] && used == (base_flags | order[i]); + std::printf("provenance order=%s input=0x%03x: create=0x%08lx same_bytes=%d lookup=0x%03x translation_flags=0x%08x %s\n", + order[0] == 0 ? "baseline-first" : "both-first", order[i], static_cast(hr), same_bytes ? 1 : 0, + lookup, used, good ? "ok" : "WRONG"); + ok = ok && good; + } + // The same bytes copied elsewhere carry no provenance: the translation copy keeps the + // desktop flags rather than guess which call they came from. + std::vector copied(static_cast(blobs[3]->GetBufferPointer()), + static_cast(blobs[3]->GetBufferPointer()) + blobs[3]->GetBufferSize()); + ComPtr root; + const HRESULT hr = create_root(copied.data(), copied.size(), reinterpret_cast(root.GetAddressOf())); + const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get()) : ~0u; + const bool good = SUCCEEDED(hr) && stripped_bits(copied.data(), copied.size()) == 0 && used == base_flags; + std::printf("provenance copy of 0x%03x: translation_flags=0x%08x %s\n", order[3], used, good ? "ok" : "WRONG"); + ok = ok && good; + } + static_cast(device)->Release(); + return ok; +} + // Heap-indexing flags (CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED). A desktop // runtime older than shader model 6.6 refuses them with E_INVALIDARG; with X2W_D3D12_PERMISSIVE=1 // d3d12_xs.dll serializes again without them and records which bits it removed for that blob. @@ -267,8 +357,7 @@ int heap_indexing_checks() static_cast(hr), retried && same_blob(retried.Get(), baseline.Get()) ? 1 : 0, recorded); if (!ok) ++failures; } - // Bits are recorded by blob content (a caller may copy the blob before CreateRootSignature), so a - // blob with other content records nothing, nor does a null blob. + // A blob with other content records nothing, nor does a null blob. D3D12_VERSIONED_ROOT_SIGNATURE_DESC other = base; other.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE; ComPtr other_blob; @@ -277,6 +366,7 @@ int heap_indexing_checks() std::fprintf(stderr, "stripped bits recorded for a blob without a retry\n"); ++failures; } + if (!provenance_checks(base, stripped_bits)) ++failures; // The retry does not hide another fault: unobserved invalid bits next to 0xC00 stay refused. D3D12_VERSIONED_ROOT_SIGNATURE_DESC bad = indexed; bad.Desc_1_1.Flags = static_cast(static_cast(indexed_flags) | 0x04000000u); From f799a9e34eca8ccd903945a4f966c6fa4174ab52 Mon Sep 17 00:00:00 2001 From: rasptum <348594691+rasptum@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:21:24 -0500 Subject: [PATCH 3/5] startup: pin stripped root flags against the deferred heap-indexed roots After merging main with #1732, device slot 16 records each root signature's heap-indexing flags for command-list slot 34, which defers a flagged root instead of setting it on the desktop list. It reads those flags from the blob passed to CreateRootSignature through the desktop deserializer. note_root_blob restores the bits only in its separate translation copy. So after a Windows 10 retry, slot 34 sees 0 and sets the root directly, while translation (the .root request, its key, and the manifest that create_translated_root consumes) keeps the restored bits. That is the right split on a runtime that refuses 0xC00: the desktop root signature really has no indexed heaps, so the debug-layer rejection #1732 avoids cannot happen, and real draws under it are not heap-indexed. No code change was needed to reconcile the two. Test: d3d12_xs_shim_heap_indexing's provenance control now also checks, for baseline, 0x400-only, 0x800-only and 0xC00 in both orders and for a byte copy, that the flags slot 34 acts on are 0. The fixture export x2w_d3d12_root_translation_blob reports them alongside the translation copy. Mutant: OR-ing the restored bits into slot 34's flags fails 6 of the 8 provenance cases (all flagged inputs). Review nits (tedin7, c31afac4): - RetriedBlob::wrap allocates with nothrow and registers inside try/catch; if either fails the caller gets the desktop blob unwrapped (no bits), so nothing leaks and no exception crosses the export. - x2w_stripped_root_flags logs once when a blob misses the pointer lookup but has the bytes of a live retried blob: either a copy (bits not restored) or a call without the bits; the bytes cannot say which, so the answer stays 0. Co-Authored-By: Claude Opus 5.5 --- src/d3d12_xs_shim.cpp | 32 ++++++++++++++++++++++++--- src/startup_stubs/d3d12_xs_device.cpp | 6 +++-- tests/d3d12_xs_shim_check.cpp | 31 +++++++++++++++++--------- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/d3d12_xs_shim.cpp b/src/d3d12_xs_shim.cpp index 523064b654..54d18c5e11 100644 --- a/src/d3d12_xs_shim.cpp +++ b/src/d3d12_xs_shim.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "d3d12x_device_slots.h" @@ -59,11 +60,24 @@ std::vector stripped_blobs; class RetriedBlob final : public ID3DBlob { public: - RetriedBlob(ID3DBlob* inner, std::uint32_t bits) : inner_(inner) + // The blob to hand out for inner: a registered wrapper, or inner itself (no bits recorded) when the + // wrapper or its record cannot be allocated. + static ID3DBlob* wrap(ID3DBlob* inner, std::uint32_t bits) noexcept { + RetriedBlob* wrapper = new (std::nothrow) RetriedBlob(inner); + if (!wrapper) return inner; + bool registered = true; AcquireSRWLockExclusive(&stripped_lock); - stripped_blobs.push_back({inner_->GetBufferPointer(), inner_->GetBufferSize(), bits}); + try { + stripped_blobs.push_back({inner->GetBufferPointer(), inner->GetBufferSize(), bits}); + } catch (...) { + registered = false; + } ReleaseSRWLockExclusive(&stripped_lock); + if (registered) return wrapper; + wrapper->inner_ = nullptr; // inner stays with the caller + delete wrapper; + return inner; } HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** out) override { @@ -85,8 +99,10 @@ class RetriedBlob final : public ID3DBlob { SIZE_T STDMETHODCALLTYPE GetBufferSize() override { return inner_->GetBufferSize(); } private: + explicit RetriedBlob(ID3DBlob* inner) : inner_(inner) {} ~RetriedBlob() { + if (!inner_) return; const void* data = inner_->GetBufferPointer(); AcquireSRWLockExclusive(&stripped_lock); for (std::size_t i = 0; i < stripped_blobs.size(); ++i) @@ -128,7 +144,7 @@ HRESULT serialize_for_runtime(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, I return hr; // something else is wrong: keep the first answer and its error blob } if (retry_error) retry_error->Release(); - if (*blob) *blob = new RetriedBlob(*blob, flags & heap_indexing_flags); + if (*blob) *blob = RetriedBlob::wrap(*blob, flags & heap_indexing_flags); if (error_blob && *error_blob) { (*error_blob)->Release(); *error_blob = nullptr; @@ -147,10 +163,20 @@ extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t b { if (!blob || !bytes) return 0; std::uint32_t bits = 0; + bool same_bytes_elsewhere = false; AcquireSRWLockShared(&stripped_lock); for (const StrippedBlob& entry : stripped_blobs) if (entry.data == blob && entry.bytes == bytes) bits = entry.bits; + // Live entries' buffers stay valid while the lock is held (the destructor unregisters first). + if (!bits) + for (const StrippedBlob& entry : stripped_blobs) + if (entry.bytes == bytes && std::memcmp(entry.data, blob, bytes) == 0) same_bytes_elsewhere = true; ReleaseSRWLockShared(&stripped_lock); + // The bytes alone cannot say which it is, so the answer stays 0; the line says why bits may be missing. + static LONG logged; + if (same_bytes_elsewhere && InterlockedIncrement(&logged) == 1) + std::fprintf(stderr, "x2w-d3d12 root signature blob %p has the bytes of a retried blob at another address: a copy " + "(its heap-indexing bits are not restored for translation) or a call without them\n", blob); return bits; } diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 8603e198b6..00775f4664 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1504,11 +1504,13 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v } // Fixtures: the copy of a root signature's blob kept for shader translation (the .root request and the -// translation key). Returns its size and copies up to capacity bytes to out; 0 for anything else. -extern "C" __declspec(dllexport) std::size_t x2w_d3d12_root_translation_blob(void* root, void* out, std::size_t capacity) +// translation key). Returns its size and copies up to capacity bytes to out; 0 for anything else. The +// heap-indexing flags slot 34 acts on (root_heap_flags, read from the desktop blob) go to heap_flags. +extern "C" __declspec(dllexport) std::size_t x2w_d3d12_root_translation_blob(void* root, void* out, std::size_t capacity, UINT* heap_flags) { Wrapper* w = as_wrapper(root); if (!w || w->own().kind != root_signature_kind) return 0; + if (heap_flags) *heap_flags = root_heap_flags(w); AcquireSRWLockShared(¬e_lock); const auto found = root_blobs().find(w); const std::size_t bytes = found == root_blobs().end() ? 0 : found->second.size(); diff --git a/tests/d3d12_xs_shim_check.cpp b/tests/d3d12_xs_shim_check.cpp index 31e3117003..4f4069a5b6 100644 --- a/tests/d3d12_xs_shim_check.cpp +++ b/tests/d3d12_xs_shim_check.cpp @@ -219,9 +219,12 @@ std::uint32_t rts0_flags(const unsigned char* data, std::size_t bytes) // produced a blob. All four blobs are alive at once, in both call orders; each goes through the // recording device's CreateRootSignature, and the copy that device keeps for shader translation must // carry exactly its own call's bits. A copy of the bytes in other memory gets none. +// The flags command-list slot 34 acts on (src/startup_stubs/heap_indexed_root.h) come from the desktop +// blob, which has no heap-indexing bits after a retry: the desktop root signature does not index heaps, +// so slot 34 must set it directly (recorded flags 0), whatever the translation copy carries. bool provenance_checks(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& base, std::uint32_t(__cdecl* stripped_bits)(const void*, std::size_t)) { - using TranslationBlob = std::size_t(__cdecl*)(void*, void*, std::size_t); + using TranslationBlob = std::size_t(__cdecl*)(void*, void*, std::size_t, UINT*); const auto translation_blob = reinterpret_cast( GetProcAddress(GetModuleHandleW(L"d3d12_xs.dll"), "x2w_d3d12_root_translation_blob")); ComPtr factory; @@ -239,10 +242,12 @@ bool provenance_checks(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& base, std::uin using Slot = HRESULT(WINAPI*)(void*, UINT, const void*, SIZE_T, const GUID*, void**); return reinterpret_cast((*static_cast(device))[16])(device, 0, blob, size, &__uuidof(ID3D12RootSignature), root); }; - // The flags of the copy the recording device kept for translation, ~0u if it kept none. - const auto translation_flags = [translation_blob](void* root) { + // The flags of the copy the recording device kept for translation, ~0u if it kept none; desktop gets + // the heap-indexing flags slot 34 acts on. + const auto translation_flags = [translation_blob](void* root, UINT* desktop) { std::array copy{}; - const std::size_t bytes = translation_blob(root, copy.data(), copy.size()); + *desktop = ~0u; + const std::size_t bytes = translation_blob(root, copy.data(), copy.size(), desktop); return bytes && bytes <= copy.size() ? rts0_flags(copy.data(), bytes) : ~0u; }; @@ -263,12 +268,14 @@ bool provenance_checks(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& base, std::uin ComPtr root; const HRESULT hr = create_root(blobs[i]->GetBufferPointer(), blobs[i]->GetBufferSize(), reinterpret_cast(root.GetAddressOf())); const std::uint32_t lookup = stripped_bits(blobs[i]->GetBufferPointer(), blobs[i]->GetBufferSize()); - const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get()) : ~0u; + UINT desktop = ~0u; + const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get(), &desktop) : ~0u; const bool same_bytes = same_blob(blobs[i].Get(), blobs[0].Get()); - const bool good = SUCCEEDED(hr) && same_bytes && lookup == order[i] && used == (base_flags | order[i]); - std::printf("provenance order=%s input=0x%03x: create=0x%08lx same_bytes=%d lookup=0x%03x translation_flags=0x%08x %s\n", + const bool good = SUCCEEDED(hr) && same_bytes && lookup == order[i] && used == (base_flags | order[i]) && desktop == 0; + std::printf("provenance order=%s input=0x%03x: create=0x%08lx same_bytes=%d lookup=0x%03x translation_flags=0x%08x " + "slot34_heap_flags=0x%x %s\n", order[0] == 0 ? "baseline-first" : "both-first", order[i], static_cast(hr), same_bytes ? 1 : 0, - lookup, used, good ? "ok" : "WRONG"); + lookup, used, desktop, good ? "ok" : "WRONG"); ok = ok && good; } // The same bytes copied elsewhere carry no provenance: the translation copy keeps the @@ -277,9 +284,11 @@ bool provenance_checks(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC& base, std::uin static_cast(blobs[3]->GetBufferPointer()) + blobs[3]->GetBufferSize()); ComPtr root; const HRESULT hr = create_root(copied.data(), copied.size(), reinterpret_cast(root.GetAddressOf())); - const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get()) : ~0u; - const bool good = SUCCEEDED(hr) && stripped_bits(copied.data(), copied.size()) == 0 && used == base_flags; - std::printf("provenance copy of 0x%03x: translation_flags=0x%08x %s\n", order[3], used, good ? "ok" : "WRONG"); + UINT desktop = ~0u; + const std::uint32_t used = SUCCEEDED(hr) && root ? translation_flags(root.Get(), &desktop) : ~0u; + const bool good = SUCCEEDED(hr) && stripped_bits(copied.data(), copied.size()) == 0 && used == base_flags && desktop == 0; + std::printf("provenance copy of 0x%03x: translation_flags=0x%08x slot34_heap_flags=0x%x %s\n", order[3], used, desktop, + good ? "ok" : "WRONG"); ok = ok && good; } static_cast(device)->Release(); From 9398fce14e6ed7a468e2436539c0fd276b2b9259 Mon Sep 17 00:00:00 2001 From: rasptum <348594691+rasptum@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:07:33 -0500 Subject: [PATCH 4/5] startup: stop the copied-blob scan once its line is logged; say when the retry runs x2w_stripped_root_flags compared every registered blob's bytes on a miss only to print a once-only line; the comparison now stops after a match and is skipped once the line has been logged. docs/startup-runtime.md says the retry runs only on runtimes that refuse the heap-indexing flags and that d3d12_xs_shim_heap_indexing reports 77 elsewhere. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 2 +- src/d3d12_xs_shim.cpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 9ccc87e095..f391cb4da4 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -597,7 +597,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr | `X2W_XMEM_PERMISSIVE=1` | accepts the reservation, mapping and allocation shapes a startup run uses | | `X2W_XMEM_STARTUP_BELOW_40=1` (`run.ps1 -StartupBelow40`) | startup compatibility configuration ([#1758](https://github.com/R0Y00/Xbox2Windows/issues/1758)): the large-page startup blocks with attribute `0x283` (null address, type `0x20003000`, protection `0x2004` or `0x2404`) are placed so that the whole block, last byte included, lies below 2^40. A block that does not fit there returns NULL with the Windows error and logs `x2w-xmem startup block below 2^40 refused:`; nothing is truncated or placed higher. Other shapes and explicit addresses keep their placement. See [Startup blocks below 2^40](experiments.md#startup-blocks-below-240) | | `X2W_XMEM_GPU_MIRROR=1` | pairs a no-access graphics reservation with the read-write reservation of the same size made just before it, and maps every physical page into both (same section) | -| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets that call's bits back when `CreateRootSignature` receives the returned blob's own buffer (a copy of the bytes keeps the desktop flags; `d3d12_xs_shim_heap_indexing`) | +| `X2W_D3D12_PERMISSIVE=1` | wraps the device: recording command lists, console present, view tracking; on a runtime without shader model 6.6 (in-box Windows 10), root signatures with the heap-indexing flags (`0xC00`) are serialized again without them, and the copy kept for shader translation gets that call's bits back when `CreateRootSignature` receives the returned blob's own buffer (a copy of the bytes keeps the desktop flags; `d3d12_xs_shim_heap_indexing`). The retry runs only on runtimes that refuse the flags (before shader model 6.6); where the runtime accepts them, `d3d12_xs_shim_heap_indexing` reports a capability skip (77) | | `X2W_D3D12_RENDER=1` | draws through translated pipelines from `X2W_SHADER_CACHE` and forwards [single-record indirect Dispatch](experiments.md#gpu-written-indirect-dispatch-arguments), [DrawIndexed](experiments.md#gpu-written-indirect-indexed-draw-arguments), [Draw](experiments.md#gpu-written-indirect-draw-arguments), and [DispatchMesh](experiments.md#gpu-written-indirect-mesh-dispatch-arguments) for supported desktop pipelines and tracked placed buffers | | `X2W_SHADER_CACHE=` | translation cache: the runtime writes source requests `req_.{a,b,root}` and, when needed, RTV variants `req_.rtv`. Run `tools/startup/gfx2hlsl.py --pending ` while the process is running. Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. The translator compiles `.vs.cso` and `.ps.cso` with `fxc.exe`: `X2W_FXC`, else the Windows SDK 10.0.26100.0 copy, else the newest `Windows Kits\10\bin\\x64`, else `PATH`. When none exists it writes `.fail` naming `X2W_FXC` and no manifest; `X2W_FXC=` (empty) skips compilation and publishes the manifest alone. | | `X2W_DRAW_NULL_IDENTITY=1` | null descriptors read as identity values instead of zero | diff --git a/src/d3d12_xs_shim.cpp b/src/d3d12_xs_shim.cpp index 54d18c5e11..9702fef314 100644 --- a/src/d3d12_xs_shim.cpp +++ b/src/d3d12_xs_shim.cpp @@ -164,17 +164,21 @@ extern "C" std::uint32_t x2w_stripped_root_flags(const void* blob, std::size_t b if (!blob || !bytes) return 0; std::uint32_t bits = 0; bool same_bytes_elsewhere = false; + // The byte comparison below only feeds a once-only line; it stops once that line is logged. + static LONG logged; AcquireSRWLockShared(&stripped_lock); for (const StrippedBlob& entry : stripped_blobs) if (entry.data == blob && entry.bytes == bytes) bits = entry.bits; // Live entries' buffers stay valid while the lock is held (the destructor unregisters first). - if (!bits) + if (!bits && !InterlockedCompareExchange(&logged, 0, 0)) for (const StrippedBlob& entry : stripped_blobs) - if (entry.bytes == bytes && std::memcmp(entry.data, blob, bytes) == 0) same_bytes_elsewhere = true; + if (entry.bytes == bytes && std::memcmp(entry.data, blob, bytes) == 0) { + same_bytes_elsewhere = true; + break; + } ReleaseSRWLockShared(&stripped_lock); // The bytes alone cannot say which it is, so the answer stays 0; the line says why bits may be missing. - static LONG logged; - if (same_bytes_elsewhere && InterlockedIncrement(&logged) == 1) + if (same_bytes_elsewhere && InterlockedExchange(&logged, 1) == 0) std::fprintf(stderr, "x2w-d3d12 root signature blob %p has the bytes of a retried blob at another address: a copy " "(its heap-indexing bits are not restored for translation) or a call without them\n", blob); return bits; From 9deb0453f0de9ce476a2f19ed835764d8281a166 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Wed, 25 Nov 2026 13:19:32 +0800 Subject: [PATCH 5/5] startup: fail the root signature retry if provenance cannot be kept --- src/d3d12_xs_shim.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/d3d12_xs_shim.cpp b/src/d3d12_xs_shim.cpp index 9702fef314..c026600337 100644 --- a/src/d3d12_xs_shim.cpp +++ b/src/d3d12_xs_shim.cpp @@ -60,12 +60,11 @@ std::vector stripped_blobs; class RetriedBlob final : public ID3DBlob { public: - // The blob to hand out for inner: a registered wrapper, or inner itself (no bits recorded) when the - // wrapper or its record cannot be allocated. + // Take ownership only after the wrapper and its provenance record have both been allocated. static ID3DBlob* wrap(ID3DBlob* inner, std::uint32_t bits) noexcept { RetriedBlob* wrapper = new (std::nothrow) RetriedBlob(inner); - if (!wrapper) return inner; + if (!wrapper) return nullptr; bool registered = true; AcquireSRWLockExclusive(&stripped_lock); try { @@ -77,7 +76,7 @@ class RetriedBlob final : public ID3DBlob { if (registered) return wrapper; wrapper->inner_ = nullptr; // inner stays with the caller delete wrapper; - return inner; + return nullptr; } HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** out) override { @@ -132,23 +131,29 @@ HRESULT serialize_for_runtime(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc, I const HRESULT hr = D3D12SerializeVersionedRootSignature(desc, blob, error_blob); if (hr != E_INVALIDARG || !desc || !blob) return hr; if (desc->Version != D3D_ROOT_SIGNATURE_VERSION_1_0 && desc->Version != D3D_ROOT_SIGNATURE_VERSION_1_1) return hr; - // Flags sits at the same offset in both versions (see the static_asserts above). - const auto flags = static_cast(desc->Desc_1_1.Flags); + const bool version_1_0 = desc->Version == D3D_ROOT_SIGNATURE_VERSION_1_0; + const auto flags = static_cast(version_1_0 ? desc->Desc_1_0.Flags : desc->Desc_1_1.Flags); if (!(flags & heap_indexing_flags) || !permissive_switch()) return hr; D3D12_VERSIONED_ROOT_SIGNATURE_DESC copy = *desc; - copy.Desc_1_1.Flags = static_cast(flags & ~heap_indexing_flags); + auto& retry_flags = version_1_0 ? copy.Desc_1_0.Flags : copy.Desc_1_1.Flags; + retry_flags = static_cast(flags & ~heap_indexing_flags); + ID3DBlob* retry_blob = nullptr; ID3DBlob* retry_error = nullptr; - const HRESULT retry = D3D12SerializeVersionedRootSignature(©, blob, &retry_error); - if (FAILED(retry)) { - if (retry_error) retry_error->Release(); + const HRESULT retry = D3D12SerializeVersionedRootSignature(©, &retry_blob, &retry_error); + if (retry_error) retry_error->Release(); + if (FAILED(retry) || !retry_blob) { + if (retry_blob) retry_blob->Release(); return hr; // something else is wrong: keep the first answer and its error blob } - if (retry_error) retry_error->Release(); - if (*blob) *blob = RetriedBlob::wrap(*blob, flags & heap_indexing_flags); + *blob = RetriedBlob::wrap(retry_blob, flags & heap_indexing_flags); if (error_blob && *error_blob) { (*error_blob)->Release(); *error_blob = nullptr; } + if (!*blob) { + retry_blob->Release(); + return E_OUTOFMEMORY; + } static LONG logged; if (InterlockedIncrement(&logged) == 1) std::fprintf(stderr, "x2w-d3d12 root signature flags 0x%x: this desktop runtime refuses the heap-indexing bits 0x%x; "