From 15d5063c4df5e7825ccea017d66f5e73aeb989d9 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 18:01:44 +0100 Subject: [PATCH 1/2] startup: bind a default sampler heap before a sampler-heap-indexed root signature --- CMakeLists.txt | 10 ++ docs/experiments.md | 9 + docs/startup-runtime.md | 8 + src/startup_stubs/d3d12_draw.inl | 28 +++ src/startup_stubs/d3d12_xs_device.cpp | 24 ++- .../translated_sampler_bindings.h | 34 ++++ tests/startup_root_sampler_heap.cpp | 169 ++++++++++++++++++ 7 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 tests/startup_root_sampler_heap.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cbab513a4d..16e2de7923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -582,6 +582,16 @@ foreach(backend IN ITEMS warp hardware) ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1;X2W_TEST_SUBMIT_FENCE_FAIL=1") endforeach() +# A sampler-heap-indexed title root signature gets a sampler heap before it is set (debug layer ID 1419). +add_executable(startup_root_sampler_heap tests/startup_root_sampler_heap.cpp) +target_compile_features(startup_root_sampler_heap PRIVATE cxx_std_17) +target_link_libraries(startup_root_sampler_heap PRIVATE d3d12_xs_shim d3d12 dxgi) +foreach(backend IN ITEMS warp hardware) + add_test(NAME startup_root_sampler_heap_${backend} COMMAND startup_root_sampler_heap ${backend}) + set_tests_properties(startup_root_sampler_heap_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1") +endforeach() + add_executable(startup_nv12_draw tests/startup_nv12_draw.cpp tests/desktop_gpu.cpp) target_compile_features(startup_nv12_draw PRIVATE cxx_std_17) target_include_directories(startup_nv12_draw PRIVATE src) diff --git a/docs/experiments.md b/docs/experiments.md index 2ea9c885c2..c8d95bdd29 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10652,3 +10652,12 @@ Command-list slot 42 records the address of a root parameter declared as a CBV, cmake --build --preset dev --target startup_stream_consumer ctest --preset dev --no-tests=error -R "^startup_root_cbv_(warp|hardware)$" -V ``` + +## Default sampler heap for sampler-heap-indexed root signatures + +A title root signature flagged `D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED` is set by command-list slot 34 with a one-entry default sampler heap bound first when the list has none, so the debug layer no longer reports message ID 1419 for it. The fixture (`tests/startup_root_sampler_heap.cpp`) is synthetic only: no title data, a root signature with no parameters, and a plain desktop list as the negative control that must produce the message. It checks the flagged root signature through the stand-in (0 messages), an unflagged one (0 messages) and the device state. The translated-draw and pipeline-creation occurrences of the message are not covered. Details are in [startup-runtime.md](startup-runtime.md#sampler-heap-indexed-root-signatures). + +```powershell +cmake --build --preset dev --target startup_root_sampler_heap +ctest --preset dev --no-tests=error -R "^startup_root_sampler_heap_(warp|hardware)$" -V +``` diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index bc40ff0710..80b94e13f5 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -148,6 +148,14 @@ by reading a render log. The device prints the counts by cause value, so checks without a device that each cause lands in the slot printed under its label, and that the header's names follow the same order. The control swaps the buffer and token slots and must fail. +## Sampler-heap-indexed root signatures + +The desktop command list never receives the heaps a title sets with list slot 32: the stand-in only records the title's sampler source, and a translated draw binds heaps of its own. A title root signature created with `D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED` (`0x800`) was therefore set by slot 34 on a list with no sampler heap, which the debug layer reports as message ID 1419. A recent log set counted 394 of these (348 from the title's own root signatures, 43 on translated draws, 2 at translated pipeline creation). + +The stand-in records the root signature flags in `CreateRootSignature`. When slot 34 sets a flagged root signature on a list that has no sampler heap bound, it binds one shared one-entry shader-visible sampler heap (the border sampler) first, keeping the view heap a translated draw already bound, and logs one line per run start (at most four): `sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first`. A sampler heap already bound on the list is never replaced, and a translated draw rebinds its own heaps as before. + +Open scope: the translated root signature is created with no flags, so the 43 translated-draw and 2 pipeline-creation messages are not explained by this change and are not claimed fixed. No Windows run of this change has been made yet; `startup_root_sampler_heap_*` counts the ID 1419 messages through a debug layer message callback and skips (77) when the debug layer cannot report the control case. + ## Root constant buffer views Command-list slot 42 (desktop `SetGraphicsRootConstantBufferView` plus four) records the title address for a root diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index be28272f3c..5ba0865563 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1096,6 +1096,34 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI return drawn; } +// One shared 1-entry sampler heap for lists that set a sampler-heap-indexed root signature with no sampler +// heap bound. It lives as long as the startup render device: submitted lists may still reference it. +SRWLOCK default_sampler_lock = SRWLOCK_INIT; +x2w_startup::ComPtr default_sampler_heap; +Wrapper* default_sampler_heap_device; + +// Called before the real SetGraphicsRootSignature of a root signature that indexes the sampler heap. A +// sampler heap already bound on this list (the title's snapshot, or a translated draw's) is kept. +void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list) +{ + Pass& pass = pass_of(self); + if (pass.bound_sampler_heap || !the_device) return; + AcquireSRWLockExclusive(&default_sampler_lock); + if (!default_sampler_heap || default_sampler_heap_device != the_device) { + default_sampler_heap.Reset(); + if (SUCCEEDED(x2w_startup::create_default_sampler_heap(device_of(the_device), default_sampler_heap.GetAddressOf()))) + default_sampler_heap_device = the_device; + } + ID3D12DescriptorHeap* fallback = default_sampler_heap.Get(); + ReleaseSRWLockExclusive(&default_sampler_lock); + ID3D12DescriptorHeap* views = pass.heap_set ? draw_heap : nullptr; + if (!x2w_startup::bind_default_sampler_heap(list, views, pass.bound_sampler_heap, fallback)) return; + pass.bound_sampler_heap = fallback; + static LONG reports; + if (first_calls(reports, 4)) + log("x2w-d3d12 sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first"); +} + // List slot 32, SetDescriptorHeaps. Track the title's shader-visible sampler // source while translated draws use their own physical descriptor heaps. void __stdcall list_set_descriptor_heaps(Wrapper* self, UINT count, void* const* heaps) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index ba8d2aabbe..46d7c470d8 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -979,6 +979,7 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec // Everything else on a list is still dropped. void release_targets(Wrapper* self); void discard_draw_sampler_heaps(Wrapper* self); +void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list); void use_as_target(Wrapper* self, ID3D12Resource* resource); bool is_container(const void* p); bool shader_dump_dir(wchar_t* out, DWORD size); @@ -1415,6 +1416,20 @@ std::unordered_map>& root_infos() static std::unordered_map> map; return map; } +std::unordered_map& root_flag_map() +{ + static std::unordered_map map; + return map; +} +// The D3D12_ROOT_SIGNATURE_FLAGS a title root signature was created with (0 when unknown). +UINT root_flags_of(const Wrapper* root) +{ + AcquireSRWLockShared(&root_lock); + const auto found = root_flag_map().find(root); + const UINT flags = found == root_flag_map().end() ? 0 : found->second; + ReleaseSRWLockShared(&root_lock); + return flags; +} const std::vector* root_info(const Wrapper* root) { AcquireSRWLockShared(&root_lock); @@ -1442,10 +1457,12 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v Wrapper* w = wrap(root, root_signature_kind); note_root_blob(w, blob, size); std::vector parameters; + UINT flags = 0; ID3D12VersionedRootSignatureDeserializer* deserializer = nullptr; if (SUCCEEDED(D3D12CreateVersionedRootSignatureDeserializer(blob, size, IID_PPV_ARGS(&deserializer)))) { const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc = nullptr; if (SUCCEEDED(deserializer->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1, &desc)) && desc) { + flags = desc->Desc_1_1.Flags; for (UINT i = 0; i < desc->Desc_1_1.NumParameters; ++i) { const auto& parameter = desc->Desc_1_1.pParameters[i]; RootParameter info{parameter.ParameterType, 0, 0}; @@ -1470,6 +1487,7 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v } AcquireSRWLockExclusive(&root_lock); root_infos()[w] = std::move(parameters); + root_flag_map()[w] = flags; ReleaseSRWLockExclusive(&root_lock); *out = w; return S_OK; @@ -1717,8 +1735,12 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) std::fill_n(pass.root_address, 16, 0); pass.root = params; pass.srv_bound = 0; - if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real) + if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real) { + // The desktop list never receives the title's heaps, so a sampler-heap-indexed root signature + // would be set with no sampler heap bound (debug layer ID 1419): give the list a default one first. + if (x2w_startup::root_indexes_sampler_heap(root_flags_of(w))) ensure_sampler_heap_for_root(self, list); list->SetGraphicsRootSignature(static_cast(w->own().real)); + } } DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target) { diff --git a/src/startup_stubs/translated_sampler_bindings.h b/src/startup_stubs/translated_sampler_bindings.h index 1375b4562a..1b7135b89c 100644 --- a/src/startup_stubs/translated_sampler_bindings.h +++ b/src/startup_stubs/translated_sampler_bindings.h @@ -225,4 +225,38 @@ inline void bind_translated_heaps(ID3D12GraphicsCommandList* list, ID3D12Descrip list->SetDescriptorHeaps(samplers ? 2 : 1, heaps); } +// D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED, spelled out so older headers build. +constexpr UINT root_flag_sampler_heap_indexed = 0x800; + +inline bool root_indexes_sampler_heap(UINT root_flags) +{ + return (root_flags & root_flag_sampler_heap_indexed) != 0; +} + +// One shader-visible sampler (the border sampler) for a list that sets a sampler-heap-indexed root +// signature while no sampler heap is bound: the debug layer rejects that call (message ID 1419). +inline HRESULT create_default_sampler_heap(ID3D12Device* device, ID3D12DescriptorHeap** out) +{ + const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + const HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(out)); + if (SUCCEEDED(hr)) { + const auto sampler = border_sampler(); + device->CreateSampler(&sampler, (*out)->GetCPUDescriptorHandleForHeapStart()); + } + return hr; +} + +// Binds fallback as the list's sampler heap, keeping the view heap already bound (null: none). +// A sampler heap that is already bound is never replaced: returns false and records nothing. +inline bool bind_default_sampler_heap(ID3D12GraphicsCommandList* list, ID3D12DescriptorHeap* views, + ID3D12DescriptorHeap* bound_samplers, ID3D12DescriptorHeap* fallback) +{ + if (bound_samplers || !fallback) return false; + ID3D12DescriptorHeap* heaps[]{views, fallback}; + if (views) list->SetDescriptorHeaps(2, heaps); + else list->SetDescriptorHeaps(1, heaps + 1); + return true; +} + } // namespace x2w_startup diff --git a/tests/startup_root_sampler_heap.cpp b/tests/startup_root_sampler_heap.cpp new file mode 100644 index 0000000000..c704c0fc1c --- /dev/null +++ b/tests/startup_root_sampler_heap.cpp @@ -0,0 +1,169 @@ +// A sampler-heap-indexed root signature set on a console list (debug layer message ID 1419). +// +// startup_root_sampler_heap (X2W_D3D12_PERMISSIVE=1 X2W_D3D12_RENDER=1 X2W_D3D12_DEBUG=1) +// +// The desktop list never receives the title's descriptor heaps, so a title root signature created with +// D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED used to be set with no sampler heap bound. Every +// debug layer message is counted through a message callback, because the stand-in drains the stored ones: +// +// control the flagged root signature set on a plain desktop list, no heap >= 1 message with ID 1419 +// flagged the same root signature through the stand-in's list 0 messages with ID 1419 +// plain a root signature without the flag through the stand-in's list 0 messages with ID 1419 +// +// The list is closed, submitted and waited for each time, and the device must not be removed. +// +// Exit codes: 0 pass, 77 no physical adapter or no debug layer, other values name the failing step. +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); + +namespace { +using Word = std::uint64_t; +constexpr int sampler_heap_message = 1419; +std::atomic messages_1419{0}, messages_other{0}; + +template Word arg(T* value) { return reinterpret_cast(value); } +Word invoke(void* object, unsigned index, Word a = 0, Word b = 0, Word c = 0, Word d = 0, Word e = 0) +{ + using Slot = Word (WINAPI*)(void*, Word, Word, Word, Word, Word); + return reinterpret_cast((*static_cast(object))[index])(object, a, b, c, d, e); +} +bool ok(Word hr) { return SUCCEEDED(static_cast(hr)); } + +void WINAPI on_message(D3D12_MESSAGE_CATEGORY, D3D12_MESSAGE_SEVERITY severity, D3D12_MESSAGE_ID id, LPCSTR text, void*) +{ + if (severity > D3D12_MESSAGE_SEVERITY_ERROR) return; + if (static_cast(id) == sampler_heap_message) ++messages_1419; + else ++messages_other; + std::printf("debug layer id %d: %s\n", static_cast(id), text); +} + +// A root signature with no parameters and the given D3D12_ROOT_SIGNATURE_FLAGS. +ComPtr root_blob(UINT flags) +{ + D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{}; + desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; + desc.Desc_1_1.Flags = static_cast(flags); + ComPtr blob; + return SUCCEEDED(D3D12SerializeVersionedRootSignature(&desc, &blob, nullptr)) ? blob : nullptr; +} + +void wait_for(ID3D12Device* desktop, ID3D12CommandQueue* queue) +{ + ComPtr fence; + if (FAILED(desktop->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)))) return; + queue->Signal(fence.Get(), 1); + while (fence->GetCompletedValue() < 1) Sleep(1); +} +} // namespace + +int main(int argc, char** argv) +{ + if (argc != 2) return 2; + ComPtr factory; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 3; + ComPtr adapter; + if (std::strcmp(argv[1], "hardware") == 0) { + for (UINT n = 0;; ++n) { + ComPtr candidate; + const HRESULT hr = factory->EnumAdapterByGpuPreference(n, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&candidate)); + if (hr == DXGI_ERROR_NOT_FOUND) return 77; + if (FAILED(hr)) return 3; + DXGI_ADAPTER_DESC1 desc{}; + if (FAILED(candidate->GetDesc1(&desc))) return 3; + if (!(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) && + SUCCEEDED(D3D12CreateDevice(candidate.Get(), D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), nullptr))) { + adapter = candidate; + break; + } + } + } else if (std::strcmp(argv[1], "warp") != 0 || FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)))) { + return 3; + } + + std::array parameters{}; + void* device = nullptr; + if (FAILED(D3D12XboxCreateDevice(adapter.Get(), parameters.data(), __uuidof(ID3D12Device), &device))) return 4; + ComPtr desktop; + if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&desktop)))) return 4; + ComPtr queue1; + DWORD cookie = 0; + if (FAILED(desktop.As(&queue1)) || FAILED(queue1->RegisterMessageCallback(on_message, D3D12_MESSAGE_CALLBACK_FLAG_NONE, nullptr, &cookie))) { + std::printf("no debug layer message callback: skipped\n"); + return 77; + } + + const auto flagged = root_blob(0x800), plain = root_blob(0); + if (!flagged || !plain) { + std::printf("this runtime cannot serialize the sampler heap flag: skipped\n"); + return 77; + } + D3D12_COMMAND_QUEUE_DESC queue_desc{}; + ComPtr desktop_queue; + ComPtr desktop_allocator; + ComPtr desktop_list; + ComPtr desktop_root; + if (FAILED(desktop->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&desktop_queue))) || + FAILED(desktop->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&desktop_allocator))) || + FAILED(desktop->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, desktop_allocator.Get(), nullptr, IID_PPV_ARGS(&desktop_list))) || + FAILED(desktop->CreateRootSignature(0, flagged->GetBufferPointer(), flagged->GetBufferSize(), IID_PPV_ARGS(&desktop_root)))) + return 5; + + // Control: the debug layer reports the flagged root signature set with no sampler heap bound. + desktop_list->SetGraphicsRootSignature(desktop_root.Get()); + desktop_list->Close(); + ID3D12CommandList* lists[] = {desktop_list.Get()}; + desktop_queue->ExecuteCommandLists(1, lists); + wait_for(desktop.Get(), desktop_queue.Get()); + std::printf("control: %d messages with ID %d\n", messages_1419.load(), sampler_heap_message); + if (!messages_1419) { + std::printf("the debug layer does not report the sampler heap message here: skipped\n"); + return 77; + } + const int control = messages_1419.exchange(0); + messages_other = 0; + + // The same two root signatures through the stand-in's list. + void *console_queue = nullptr, *allocator = nullptr, *list = nullptr, *flagged_root = nullptr, *plain_root = nullptr; + const unsigned list_desc[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; + if (!ok(invoke(device, 8, arg(&queue_desc), arg(&__uuidof(ID3D12CommandQueue)), arg(&console_queue))) || + !ok(invoke(device, 9, D3D12_COMMAND_LIST_TYPE_DIRECT, arg(&__uuidof(ID3D12CommandAllocator)), arg(&allocator))) || + !ok(invoke(device, 73, arg(list_desc), arg(allocator), 0, arg(&__uuidof(ID3D12GraphicsCommandList)), arg(&list))) || + !ok(invoke(device, 16, 0, arg(flagged->GetBufferPointer()), flagged->GetBufferSize(), arg(&__uuidof(ID3D12RootSignature)), arg(&flagged_root))) || + !ok(invoke(device, 16, 0, arg(plain->GetBufferPointer()), plain->GetBufferSize(), arg(&__uuidof(ID3D12RootSignature)), arg(&plain_root)))) + return 6; + const auto record_and_submit = [&](void* root) { + invoke(list, 34, arg(root)); // SetGraphicsRootSignature + if (!ok(invoke(list, 9))) return false; + void* submit[] = {list}; + invoke(console_queue, 10, 1, arg(submit)); + wait_for(desktop.Get(), desktop_queue.Get()); + Sleep(200); + if (!ok(invoke(allocator, 8)) || !ok(invoke(list, 139, arg(allocator)))) return false; + return true; + }; + if (!record_and_submit(flagged_root)) return 10; + const int flagged_messages = messages_1419.exchange(0); + std::printf("flagged root signature: %d messages with ID %d\n", flagged_messages, sampler_heap_message); + if (!record_and_submit(plain_root)) return 11; + const int plain_messages = messages_1419.exchange(0); + std::printf("plain root signature: %d messages with ID %d\n", plain_messages, sampler_heap_message); + + const HRESULT removed = desktop->GetDeviceRemovedReason(); + std::printf("control %d, device removed reason 0x%08lx, other debug layer errors %d\n", control, + static_cast(removed), messages_other.load()); + queue1->UnregisterMessageCallback(cookie); + if (flagged_messages) return 20; + if (plain_messages) return 21; + return removed == S_OK && messages_other == 0 ? 0 : 70; +} From 5dbd2711ba5af3f592ce83e4f4456a61fc0c5e06 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 18:10:28 +0100 Subject: [PATCH 2/2] startup: scope default sampler heap per device and reset pass state --- docs/startup-runtime.md | 2 +- src/startup_stubs/d3d12_draw.inl | 30 ++++++++++++++------------- src/startup_stubs/d3d12_xs_device.cpp | 6 ++++++ tests/startup_root_sampler_heap.cpp | 16 +++++++++++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 80b94e13f5..89bd2b9d61 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -154,7 +154,7 @@ The desktop command list never receives the heaps a title sets with list slot 32 The stand-in records the root signature flags in `CreateRootSignature`. When slot 34 sets a flagged root signature on a list that has no sampler heap bound, it binds one shared one-entry shader-visible sampler heap (the border sampler) first, keeping the view heap a translated draw already bound, and logs one line per run start (at most four): `sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first`. A sampler heap already bound on the list is never replaced, and a translated draw rebinds its own heaps as before. -Open scope: the translated root signature is created with no flags, so the 43 translated-draw and 2 pipeline-creation messages are not explained by this change and are not claimed fixed. No Windows run of this change has been made yet; `startup_root_sampler_heap_*` counts the ID 1419 messages through a debug layer message callback and skips (77) when the debug layer cannot report the control case. +Open scope: the translated root signature is created with no flags, so the 43 translated-draw and 2 pipeline-creation messages are not explained by this change and are not claimed fixed. `startup_root_sampler_heap_*` counts the ID 1419 messages through a debug layer message callback and skips (77) when the debug layer cannot report the control case. ## Root constant buffer views diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 5ba0865563..36c68bd8bf 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1096,29 +1096,31 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI return drawn; } -// One shared 1-entry sampler heap for lists that set a sampler-heap-indexed root signature with no sampler -// heap bound. It lives as long as the startup render device: submitted lists may still reference it. +// One shared 1-entry sampler heap per device for lists that set a sampler-heap-indexed root signature with no +// sampler heap bound. Each heap lives for the lifetime of the process so submitted lists may still reference it. SRWLOCK default_sampler_lock = SRWLOCK_INIT; -x2w_startup::ComPtr default_sampler_heap; -Wrapper* default_sampler_heap_device; +std::unordered_map>& default_sampler_heaps() +{ + static std::unordered_map> map; + return map; +} // Called before the real SetGraphicsRootSignature of a root signature that indexes the sampler heap. A // sampler heap already bound on this list (the title's snapshot, or a translated draw's) is kept. void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list) { Pass& pass = pass_of(self); - if (pass.bound_sampler_heap || !the_device) return; + Wrapper* device = self->own().list_device; + if (pass.bound_sampler_heap || !device || !device_of(device)) return; + x2w_startup::ComPtr fallback; AcquireSRWLockExclusive(&default_sampler_lock); - if (!default_sampler_heap || default_sampler_heap_device != the_device) { - default_sampler_heap.Reset(); - if (SUCCEEDED(x2w_startup::create_default_sampler_heap(device_of(the_device), default_sampler_heap.GetAddressOf()))) - default_sampler_heap_device = the_device; - } - ID3D12DescriptorHeap* fallback = default_sampler_heap.Get(); + auto& slot = default_sampler_heaps()[device]; + if (!slot) x2w_startup::create_default_sampler_heap(device_of(device), slot.GetAddressOf()); + fallback = slot; ReleaseSRWLockExclusive(&default_sampler_lock); - ID3D12DescriptorHeap* views = pass.heap_set ? draw_heap : nullptr; - if (!x2w_startup::bind_default_sampler_heap(list, views, pass.bound_sampler_heap, fallback)) return; - pass.bound_sampler_heap = fallback; + ID3D12DescriptorHeap* views = pass.heap_set && device == draw_heap_device ? draw_heap : nullptr; + if (!x2w_startup::bind_default_sampler_heap(list, views, pass.bound_sampler_heap, fallback.Get())) return; + pass.bound_sampler_heap = fallback.Get(); static LONG reports; if (first_calls(reports, 4)) log("x2w-d3d12 sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first"); diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 46d7c470d8..bc69d82e21 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -978,6 +978,7 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec // when the desktop GPU finishes, and the presented surface is read back and shown in the window. // Everything else on a list is still dropped. void release_targets(Wrapper* self); +void reset_pass(Wrapper* self); void discard_draw_sampler_heaps(Wrapper* self); void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list); void use_as_target(Wrapper* self, ID3D12Resource* resource); @@ -1198,6 +1199,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator) ReleaseSRWLockExclusive(&allocator_lock); } if (SUCCEEDED(hr)) { + reset_pass(self); self->own().list_ready = false; discard_draw_sampler_heaps(self); if (draw_dump_frame()) draw_dumps().discard(static_cast(self->own().real)); @@ -1707,6 +1709,10 @@ void release_targets(Wrapper* self) transition(static_cast(self->own().real), known.first, known.second, D3D12_RESOURCE_STATE_COMMON); pass = Pass{}; } +void reset_pass(Wrapper* self) +{ + pass_of(self) = Pass{}; +} // List slot 20, IASetPrimitiveTopology. 0x11 is a console-only rectangle list (no desktop draw yet). void __stdcall list_topology(Wrapper* self, UINT topology) diff --git a/tests/startup_root_sampler_heap.cpp b/tests/startup_root_sampler_heap.cpp index c704c0fc1c..55084425d1 100644 --- a/tests/startup_root_sampler_heap.cpp +++ b/tests/startup_root_sampler_heap.cpp @@ -134,21 +134,27 @@ int main(int argc, char** argv) messages_other = 0; // The same two root signatures through the stand-in's list. - void *console_queue = nullptr, *allocator = nullptr, *list = nullptr, *flagged_root = nullptr, *plain_root = nullptr; + void *console_queue = nullptr, *console_fence = nullptr, *allocator = nullptr, *list = nullptr, *flagged_root = nullptr, *plain_root = nullptr; const unsigned list_desc[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; if (!ok(invoke(device, 8, arg(&queue_desc), arg(&__uuidof(ID3D12CommandQueue)), arg(&console_queue))) || + !ok(invoke(device, 36, 0, 0, arg(&__uuidof(ID3D12Fence)), arg(&console_fence))) || !ok(invoke(device, 9, D3D12_COMMAND_LIST_TYPE_DIRECT, arg(&__uuidof(ID3D12CommandAllocator)), arg(&allocator))) || !ok(invoke(device, 73, arg(list_desc), arg(allocator), 0, arg(&__uuidof(ID3D12GraphicsCommandList)), arg(&list))) || !ok(invoke(device, 16, 0, arg(flagged->GetBufferPointer()), flagged->GetBufferSize(), arg(&__uuidof(ID3D12RootSignature)), arg(&flagged_root))) || !ok(invoke(device, 16, 0, arg(plain->GetBufferPointer()), plain->GetBufferSize(), arg(&__uuidof(ID3D12RootSignature)), arg(&plain_root)))) return 6; + Word fence_value = 0; const auto record_and_submit = [&](void* root) { invoke(list, 34, arg(root)); // SetGraphicsRootSignature if (!ok(invoke(list, 9))) return false; void* submit[] = {list}; invoke(console_queue, 10, 1, arg(submit)); - wait_for(desktop.Get(), desktop_queue.Get()); - Sleep(200); + if (!ok(invoke(console_queue, 14, arg(console_fence), ++fence_value))) return false; + const ULONGLONG deadline = GetTickCount64() + 10000; + while (invoke(console_fence, 8) < fence_value) { + if (GetTickCount64() > deadline) return false; + Sleep(1); + } if (!ok(invoke(allocator, 8)) || !ok(invoke(list, 139, arg(allocator)))) return false; return true; }; @@ -158,6 +164,9 @@ int main(int argc, char** argv) if (!record_and_submit(plain_root)) return 11; const int plain_messages = messages_1419.exchange(0); std::printf("plain root signature: %d messages with ID %d\n", plain_messages, sampler_heap_message); + if (!record_and_submit(flagged_root)) return 12; + const int second_flagged_messages = messages_1419.exchange(0); + std::printf("flagged root signature after reset: %d messages with ID %d\n", second_flagged_messages, sampler_heap_message); const HRESULT removed = desktop->GetDeviceRemovedReason(); std::printf("control %d, device removed reason 0x%08lx, other debug layer errors %d\n", control, @@ -165,5 +174,6 @@ int main(int argc, char** argv) queue1->UnregisterMessageCallback(cookie); if (flagged_messages) return 20; if (plain_messages) return 21; + if (second_flagged_messages) return 22; return removed == S_OK && messages_other == 0 ? 0 : 70; }