From af236e196bdcbaf90c7cba9ce0b3668521d0afc8 Mon Sep 17 00:00:00 2001 From: SWAYAM Date: Sun, 22 Nov 2026 18:57:50 +0530 Subject: d3d12_xs: route slot 67 compute pipeline from native code to CreateComputePipelineState (RT-99) Xbox device slot 67 takes (desc, riid, out) with IID_ID3D12PipelineState and a console-only descriptor with the root signature at +0x00 and code blob pointer/size at +0x28/+0x30. Previously it logged and returned E_NOTIMPL. Route it to CreateComputePipelineState on the PC device (slot 11) like slot 62, mapping the console descriptor to D3D12_COMPUTE_PIPELINE_STATE_DESC. On the Vulkan device this resolves through the translator via fssm_program and ShaderLibrary. - Implement proxy_create_compute_pipeline_from_native_code in d3d12_xs_device.cpp - Update device_proxy_test to verify slot 67 thunk, compute PSO creation over WARP, and argument checks - Update d3d12_compute_test to verify slot 67 compute PSO creation on the Vulkan device - Update documentation in d3d12_command_objects.md and d3d12_vulkan.md Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_command_objects.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'docs/d3d12_command_objects.md') diff --git a/docs/d3d12_command_objects.md b/docs/d3d12_command_objects.md index a55c3d964..497431d8f 100644 --- a/docs/d3d12_command_objects.md +++ b/docs/d3d12_command_objects.md @@ -371,9 +371,10 @@ vtable pointer of a public object. - **Slot 67** (call at `0x56d9`) is **not** the PC `CreateProtectedResourceSession1`: it takes `(desc, riid, out)` with `IID_ID3D12PipelineState`. The DLL's own messages name it `DeserializeComputePipelineStateX`. Its desc is the root signature at `+0x00`, then pointer and size pairs for the shader's `FSPX` entries: kind 0 at `+0x08`, kind 1 - at `+0x18`, a copy of the kind-6 program at `+0x28`, and kind 7 at `+0x38` ([fspx.md](fspx.md)). The proxy used to - forward it to the PC method at the same index; it now refuses it (`E_NOTIMPL`). When it fails, the DLL takes - slot 62; its message for that failure calls the fallback "NewBE". + at `+0x18`, a copy of the kind-6 program at `+0x28`, and kind 7 at `+0x38` ([fspx.md](fspx.md)). The proxy maps the + console desc to `D3D12_COMPUTE_PIPELINE_STATE_DESC` and routes it to `CreateComputePipelineState` (PC slot 11). On a + PC device that call fails (`E_FAIL`) because the code is not a PC shader, so the DLL falls back to slot 62; its message + for that failure calls the fallback "NewBE". - **Slot 62** (call at `0x5825`) takes the measured `(D3D12_COMPUTE_PIPELINE_STATE_DESC*, 0, 0, riid, out)`. The PC runtime refuses the DLL's code (`E_FAIL`): it is not a PC shader. The DLL's pipeline function then returns `0x8000000d` (`0x4b85`), but the dispatch goes on, records the null pipeline and faults (`0x65ff`), which is where a -- cgit v1.2.3 From 2123a02506f16cab63c676dbb029edd0bbe859a2 Mon Sep 17 00:00:00 2001 From: AimLogin <4jt8mn8wjn@privaterelay.appleid.com> Date: Sun, 22 Nov 2026 15:11:53 +0100 Subject: Runtime: Xbox-ABI proxy for the command allocator (device slot 9) Device slot 9 (CreateCommandAllocator) now returns an allocator proxy, the fourth command object after the queue and fence (BUILD-231) and the list (D3D-136): the public chain (9 slots, Reset at 8) is forwarded, a slot past it up to the end of a 64-slot table is logged and refused with E_NOTIMPL, and a read past the table faults in its own guard region. The console allocator's extra methods are not measured and not guessed. A list takes the title's allocator in CreateCommandList and Reset, so a proxy is only safe where the list side resolves it. Slot 9 therefore wraps an allocator only when it answers kLeonidaCommandAllocatorTag (the Vulkan layer's CommandAllocator, whose from() resolves the proxy for both calls); a PC runtime allocator is handed out unchanged, logged once, until a command-list proxy unwraps it at Reset. Allocator proxies are unwrapped in the residency calls like heap, queue and fence proxies. Device slot 12 is not touched. Tests: allocator_proxy_test (WARP, a hardware adapter, the Vulkan device: forwarding, refused tail, guard region, identity, D3D12's allocator rules, a list created and Reset on the proxy with GPU copies through the queue and fence proxies read back), mock cases for allocators with and without the tag and their failure paths, and device_proxy_test's slot-9 expectations. No game files. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_command_objects.md | 20 ++ runtime/CMakeLists.txt | 8 + runtime/shims/d3d12_com.cpp | 12 +- runtime/shims/d3d12_xs_device.cpp | 118 +++++++++-- runtime/shims/d3d12_xs_device.h | 29 ++- runtime/test/allocator_proxy_test.cpp | 382 ++++++++++++++++++++++++++++++++++ runtime/test/device_proxy_test.cpp | 4 +- runtime/test/xs_proxy_mock_test.cpp | 93 ++++++++- 8 files changed, 640 insertions(+), 26 deletions(-) create mode 100644 runtime/test/allocator_proxy_test.cpp (limited to 'docs/d3d12_command_objects.md') diff --git a/docs/d3d12_command_objects.md b/docs/d3d12_command_objects.md index a55c3d964..9d2044272 100644 --- a/docs/d3d12_command_objects.md +++ b/docs/d3d12_command_objects.md @@ -563,6 +563,26 @@ Queues and fences are wrapped the same way. and a heap proxy given to a list's `SetDescriptorHeaps` reaches the real list unchanged. The command-list proxy of §3.5a adds the unwrap at both places. +Command allocators get the same kind of proxy: device slot 9 (`CreateCommandAllocator`) returns one. + +- **What is forwarded.** The public chain, 9 slots: the base chain and `Reset` at 8. A slot past it, up to the + end of a 64-slot table, is logged and refused, and a read past the table faults in its own guard region. + The console allocator's extra methods are not measured. +- **Where a list takes it.** A list takes an allocator from the title in `CreateCommandList` and `Reset`, so + the proxy is safe only where the list side resolves it. Slot 9 therefore wraps an allocator only when it + answers `kLeonidaCommandAllocatorTag` (`d3d12_xs_device.h`). That is the Vulkan layer's own allocator, whose + `CommandAllocator::from()` resolves the proxy for both list calls. +- **On the host runtime.** A PC runtime allocator comes back unwrapped, logged once, because the PC runtime's + lists would take a proxy for their own allocator. A command-list proxy that unwraps at `Reset` (§3.5a) is + what lets the host backend wrap allocators too. +- **Residency.** An allocator is a pageable, so `MakeResident`/`Evict`/`SetResidencyPriority`/ + `EnqueueMakeResident` unwrap allocator proxies like heap, queue and fence proxies. + +`allocator_proxy_test` checks this on the Vulkan device and on the host runtime, and the mock test checks it +over allocators with and without the tag. On the Vulkan device it checks forwarding, the refused tail, the guard +region, identity, and D3D12's allocator rules through the proxy, and runs a list created and `Reset` on the +proxy whose copies go through the queue and fence proxies and are read back. + Which of this is measured: the slot numbers and signatures are the SDK's (public). That the console keeps the public queue and fence slots, and device slots 8/32/36/75, is an assumption the package cannot test, as above. The proxies make a wrong assumption visible in the log; they do not prove it right. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 6ea698f38..326c1ad46 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -928,6 +928,14 @@ if(TARGET Vulkan::Headers) target_compile_options(queue_proxy_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME queue_proxy COMMAND queue_proxy_test) set_tests_properties(queue_proxy PROPERTIES SKIP_RETURN_CODE 77) + # Xbox-ABI command allocator proxies (device slot 9) on the Vulkan device, and the unwrapped PC allocator on the + # host runtime: forwarding, refused tail, guard region, lists and GPU copies on the proxy. 77: no device at all. + add_executable(allocator_proxy_test test/allocator_proxy_test.cpp) + target_link_libraries(allocator_proxy_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) + target_compile_definitions(allocator_proxy_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(allocator_proxy_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME allocator_proxy COMMAND allocator_proxy_test) + set_tests_properties(allocator_proxy PROPERTIES SKIP_RETURN_CODE 77) # The countdown behind those event-backed waits, with no GPU: the full unsigned count, no wrap past zero. add_executable(d3d12_fence_waiter_test test/d3d12_fence_waiter_test.cpp) target_compile_definitions(d3d12_fence_waiter_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 6fc3e2c51..65bac3943 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -113,8 +113,9 @@ class CommandAllocator final : public Child { public: - // {7d0e6b51-34a2-4f0c-9a61-0c5b2e8f4a10} - static constexpr GUID kTag{0x7d0e6b51, 0x34a2, 0x4f0c, {0x9a, 0x61, 0x0c, 0x5b, 0x2e, 0x8f, 0x4a, 0x10}}; + // {7d0e6b51-34a2-4f0c-9a61-0c5b2e8f4a10}, public in d3d12_xs_device.h: the device proxy's slot 9 wraps an allocator + // in an Xbox-ABI allocator proxy only when it answers this tag, because from() below resolves such a proxy. + static constexpr GUID kTag = d3d12_xs::kLeonidaCommandAllocatorTag; explicit CommandAllocator(D3D12_COMMAND_LIST_TYPE type) : type_(type) {} // Lists own their recorded commands, so there is nothing to free. As on the PC runtime (D3D-51, measured by // d3d12_vulkan_device_test): E_FAIL while a list is recording into this allocator, and S_OK while its lists are @@ -124,7 +125,12 @@ public: // The one list that is recording into this allocator, or null. D3D12 allows one at a time. const void* recording() const { return recording_; } void set_recording(const void* list) { recording_ = list; } - static CommandAllocator* from(IUnknown* object) { return Child::from(object); } + // An allocator proxy (the title's allocator from device slot 9) stands for the allocator behind it, so every + // list method that takes an allocator (CreateCommandList, Reset) resolves it here. + static CommandAllocator* from(IUnknown* object) { + if (ID3D12CommandAllocator* real = d3d12_xs::allocator_proxy_real_allocator(object)) object = real; + return Child::from(object); + } private: D3D12_COMMAND_LIST_TYPE type_; diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 57cd292c7..e87444f53 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -388,18 +388,29 @@ HRESULT create_heap_proxy(ID3D12DescriptorHeap* real, DeviceProxy* device, REFII // // QueryInterface answers only the interfaces the proxy models (the base chain and its own interface, Fence1 when // the real fence has it), like the device and heap proxies, so the COM identity holds and no real object leaks to -// the title. Leonida's own code reaches the real object through queue_proxy_real_queue / fence_proxy_real_fence. +// the title. Leonida's own code reaches the real object through queue_proxy_real_queue / fence_proxy_real_fence / +// allocator_proxy_real_allocator. +// +// Command allocators (device slot 9) are the third kind: their public chain is 9 slots (Reset at 8). Slot 9 wraps +// only an allocator that answers kLeonidaCommandAllocatorTag, because a list takes the title's allocator in +// CreateCommandList and Reset and only the Vulkan layer's lists resolve a proxy there (CommandAllocator::from); +// a PC runtime allocator is handed out unchanged until a command-list proxy unwraps it at Reset. -enum class ChildKind { queue, fence }; +enum class ChildKind { queue, fence, allocator }; constexpr unsigned kQueueSlots = 128; // the table: 19 public slots plus a refused tail constexpr unsigned kFenceSlots = 64; // the table: 12 public slots plus a refused tail constexpr unsigned kQueuePublicSlots = 19; // ID3D12CommandQueue constexpr unsigned kFencePublicSlots = 11; // ID3D12Fence constexpr unsigned kFence1PublicSlots = 12; // ID3D12Fence1 +constexpr unsigned kAllocatorSlots = 64; // the table: 9 public slots plus a refused tail +constexpr unsigned kAllocatorPublicSlots = 9; // ID3D12CommandAllocator: the base chain and Reset (slot 8) constexpr SIZE_T kTableRegion = 64 * 1024; // one allocation granule: the table page plus 15 never-committed pages -static_assert(kQueueSlots > kQueuePublicSlots && kFenceSlots > kFence1PublicSlots, "the tables need a tail"); -static_assert(kQueueSlots * sizeof(void*) <= 4096 && kFenceSlots * sizeof(void*) <= 4096, +static_assert(kQueueSlots > kQueuePublicSlots && kFenceSlots > kFence1PublicSlots && + kAllocatorSlots > kAllocatorPublicSlots, + "the tables need a tail"); +static_assert(kQueueSlots * sizeof(void*) <= 4096 && kFenceSlots * sizeof(void*) <= 4096 && + kAllocatorSlots * sizeof(void*) <= 4096, "a table must fit in the first page of its reservation"); constexpr unsigned kQueueSignal = 14, kQueueWait = 15; // (ID3D12Fence*, UINT64) @@ -415,7 +426,9 @@ struct ChildProxy { template struct ChildStats { - static constexpr unsigned kSlots = K == ChildKind::queue ? kQueueSlots : kFenceSlots; + static constexpr unsigned kSlots = K == ChildKind::queue ? kQueueSlots + : K == ChildKind::fence ? kFenceSlots + : kAllocatorSlots; static inline std::atomic hits[kSlots]; static inline std::atomic first[kSlots]; static inline std::atomic beyond_warned[kSlots]; @@ -426,7 +439,7 @@ struct ChildStats { template constexpr const char* child_kind_name() { - return K == ChildKind::queue ? "queue" : "fence"; + return K == ChildKind::queue ? "queue" : K == ChildKind::fence ? "fence" : "allocator"; } template @@ -441,8 +454,10 @@ const char* child_public_name(unsigned slot) { if (slot < 8) return object[slot]; if constexpr (K == ChildKind::queue) { if (slot < kQueuePublicSlots) return queue[slot - 8]; - } else { + } else if constexpr (K == ChildKind::fence) { if (slot < kFence1PublicSlots) return fence[slot - 8]; + } else { + if (slot == 8) return "Reset"; } return nullptr; } @@ -454,7 +469,9 @@ template const char* child_trace_name(unsigned slot) { static const auto names = [] { static char storage[ChildStats::kSlots][64]; - const char* iface = K == ChildKind::queue ? "XboxID3D12CommandQueue" : "XboxID3D12Fence"; + const char* iface = K == ChildKind::queue ? "XboxID3D12CommandQueue" + : K == ChildKind::fence ? "XboxID3D12Fence" + : "XboxID3D12CommandAllocator"; for (unsigned s = 0; s < ChildStats::kSlots; ++s) { const char* method = child_public_name(s); if (method) @@ -528,8 +545,10 @@ bool is_child_iid(const IID& iid) { return true; if constexpr (K == ChildKind::queue) { return IsEqualIID(iid, __uuidof(ID3D12CommandQueue)); - } else { + } else if constexpr (K == ChildKind::fence) { return IsEqualIID(iid, __uuidof(ID3D12Fence)) || IsEqualIID(iid, __uuidof(ID3D12Fence1)); + } else { + return IsEqualIID(iid, __uuidof(ID3D12CommandAllocator)); } } @@ -608,7 +627,8 @@ IUnknown* proxy_real_object(const void* object) { void* const vtable = *reinterpret_cast(object); if (vtable == reinterpret_cast(get_heap_vtable())) return static_cast(object)->real; if (vtable == reinterpret_cast(get_child_vtable()) || - vtable == reinterpret_cast(get_child_vtable())) + vtable == reinterpret_cast(get_child_vtable()) || + vtable == reinterpret_cast(get_child_vtable())) return static_cast(object)->real; return nullptr; } @@ -685,6 +705,7 @@ LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) { const auto address = static_cast(record->ExceptionInformation[1]); report_table_overrun(address); report_table_overrun(address); + report_table_overrun(address); } return EXCEPTION_CONTINUE_SEARCH; // diagnostics only: the fault goes on to the title's handlers or the crash } @@ -803,8 +824,10 @@ HRESULT wrap_child(IUnknown* created, DeviceProxy* device, const IID* riid, void proxy->real_vtable = *reinterpret_cast(created); if constexpr (K == ChildKind::queue) { proxy->slot_count = kQueuePublicSlots; - } else { + } else if constexpr (K == ChildKind::fence) { proxy->slot_count = supports_iid(created, __uuidof(ID3D12Fence1)) ? kFence1PublicSlots : kFencePublicSlots; + } else { + proxy->slot_count = kAllocatorPublicSlots; } proxy->refs.store(1, std::memory_order_relaxed); proxy_add_ref(device); // the object keeps its Xbox-ABI device alive for GetDevice @@ -894,6 +917,43 @@ HRESULT proxy_create_fence(DeviceProxy* self, std::uint64_t initial_value, std:: return wrap_child(static_cast(created), self, riid, out); } +// True when `allocator` is one whose consumers resolve allocator proxies: Leonida's own Vulkan-layer allocator, +// whose lists (CreateCommandList, Reset) find the real allocator through allocator_proxy_real_allocator. A PC +// runtime allocator does not answer the tag, and its lists would take a proxy for their own allocator. +bool resolves_allocator_proxies(IUnknown* allocator) { + void* found = nullptr; + if (FAILED(allocator->QueryInterface(kLeonidaCommandAllocatorTag, &found)) || !found) return false; + static_cast(found)->Release(); // the implementation pointer starts with its IUnknown chain + return true; +} + +// Device slot 9: CreateCommandAllocator(type, riid, out). An allocator proxy where the list side resolves it (see +// resolves_allocator_proxies); any other allocator goes back unchanged, logged once, so the host runtime's lists +// never see a proxy. Null output and null IID as slot 8. +HRESULT proxy_create_command_allocator(DeviceProxy* self, std::uint64_t type, const IID* riid, void** out) { + note_slot(9); + if (out) *out = nullptr; // as slot 8 + if (!riid) return E_POINTER; + const auto list_type = static_cast(static_cast(type)); +#pragma warning(suppress : 6387) // the caller's null output, on purpose (see slot 8) + if (!out) return self->real->CreateCommandAllocator(list_type, *riid, nullptr); + void* created = nullptr; + const HRESULT hr = self->real->CreateCommandAllocator(list_type, __uuidof(ID3D12CommandAllocator), &created); + if (FAILED(hr)) return hr; + if (!created) return E_FAIL; + auto* allocator = static_cast(created); + if (!resolves_allocator_proxies(allocator)) { + static std::atomic logged{false}; + if (!logged.exchange(true, std::memory_order_relaxed)) + std::fprintf(stderr, "[rt] d3d12_xs: allocators of this device are not wrapped: its command lists " + "cannot resolve an allocator proxy\n"); + const HRESULT asked = allocator->QueryInterface(*riid, out); + allocator->Release(); + return asked; + } + return wrap_child(allocator, self, riid, out); +} + // Device slot 32: OpenSharedHandle(handle, riid, out). The object is opened as asked; when it is a fence (whatever // interface was asked for, IUnknown included) the title gets a fence proxy, anything else (heaps, resources) is // returned unchanged. @@ -1024,6 +1084,9 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 } else if constexpr (Slot == 8) { return hr_to_u64(proxy_create_command_queue(self, reinterpret_cast(a1), reinterpret_cast(a2), reinterpret_cast(a3))); + } else if constexpr (Slot == 9) { + return hr_to_u64(proxy_create_command_allocator(self, a1, reinterpret_cast(a2), + reinterpret_cast(a3))); } else if constexpr (Slot == 36) { return hr_to_u64(proxy_create_fence(self, a1, a2, reinterpret_cast(a3), reinterpret_cast(a4))); @@ -1186,13 +1249,19 @@ ID3D12Device* device_proxy_real_device(const void* device) { } bool device_slot_overridden(unsigned slot) { - return slot <= 2 || slot == 8 || slot == 14 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || + return slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 || slot == 75 || slot == 76; } bool is_queue_proxy(const void* queue) { return is_child_proxy(queue); } bool is_fence_proxy(const void* fence) { return is_child_proxy(fence); } +bool is_allocator_proxy(const void* allocator) { return is_child_proxy(allocator); } + +ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator) { + if (!is_allocator_proxy(allocator)) return nullptr; + return reinterpret_cast(static_cast(allocator)->real); +} ID3D12CommandQueue* queue_proxy_real_queue(const void* queue) { if (!is_queue_proxy(queue)) return nullptr; @@ -1214,9 +1283,13 @@ unsigned fence_proxy_slot_count(const void* fence) { unsigned queue_proxy_table_size() { return kQueueSlots; } unsigned fence_proxy_table_size() { return kFenceSlots; } +unsigned allocator_proxy_table_size() { return kAllocatorSlots; } +unsigned allocator_proxy_slot_count(const void* allocator) { + return is_allocator_proxy(allocator) ? static_cast(allocator)->slot_count : 0; +} unsigned long command_object_proxy_ref_count(const void* object) { - if (!is_queue_proxy(object) && !is_fence_proxy(object)) return 0; + if (!is_queue_proxy(object) && !is_fence_proxy(object) && !is_allocator_proxy(object)) return 0; return static_cast(object)->refs.load(std::memory_order_relaxed); } @@ -1251,8 +1324,24 @@ std::uint64_t fence_table_overruns() { bool command_object_tables_guarded() { get_child_vtable(); get_child_vtable(); + get_child_vtable(); return ChildStats::table_end.load(std::memory_order_acquire) && - ChildStats::table_end.load(std::memory_order_acquire); + ChildStats::table_end.load(std::memory_order_acquire) && + ChildStats::table_end.load(std::memory_order_acquire); +} + +std::uint64_t allocator_slot_hits(unsigned slot) { + using S = ChildStats; + return slot < S::kSlots ? S::hits[slot].load(std::memory_order_relaxed) : 0; +} + +bool allocator_slot_first_call_seen(unsigned slot) { + using S = ChildStats; + return slot < S::kSlots && S::first[slot].load(std::memory_order_relaxed); +} + +std::uint64_t allocator_table_overruns() { + return ChildStats::overruns.load(std::memory_order_relaxed); } bool heap_slot_overridden(unsigned slot) { @@ -1292,6 +1381,7 @@ void reset_device_proxy_stats() { } reset_child_stats(); reset_child_stats(); + reset_child_stats(); } } // namespace rt::d3d12_xs diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 510560516..b61b24802 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -57,8 +57,9 @@ unsigned device_proxy_slot_count(const void* device); ID3D12Device* device_proxy_real_device(const void* device); // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue -// proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 62, 67 and 76. -// Slots 9 and 10 are intentionally false; the handle conversion is on the heap proxy. +// proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 62, 67 +// and 76. Slot 10 is intentionally false: the measured 9/10 handle conversion is on the heap proxy, and device +// slot 9 is CreateCommandAllocator, which the proxy handles for the allocator proxy. bool device_slot_overridden(unsigned slot); // Queue and fence proxies created by device slots 8/75 and 36. The real_* accessors return the wrapped object @@ -72,7 +73,7 @@ unsigned queue_proxy_slot_count(const void* queue); unsigned fence_proxy_slot_count(const void* fence); unsigned queue_proxy_table_size(); unsigned fence_proxy_table_size(); -// Live references on a queue or fence proxy (0 for anything else). Test helper. +// Live references on a queue, fence or allocator proxy (0 for anything else). Test helper. unsigned long command_object_proxy_ref_count(const void* object); // Per-slot call counters and first-call flags, shared by all queue proxies and all fence proxies. std::uint64_t queue_slot_hits(unsigned slot); @@ -82,8 +83,28 @@ bool fence_slot_first_call_seen(unsigned slot); // Vtable reads past a queue or fence proxy table that faulted in its guard region (counted by the fault logger). std::uint64_t queue_table_overruns(); std::uint64_t fence_table_overruns(); -// True when both tables sit in front of their guard region (false only if the reservation failed). +// True when the queue, fence and allocator tables sit in front of their guard regions (false only if a reservation +// or the fault logger failed). bool command_object_tables_guarded(); + +// Command allocator proxies (device slot 9): the public chain (9 slots: the base chain and Reset) is forwarded, a +// slot past it up to the end of the table (64 slots) is logged and refused with E_NOTIMPL, and a read past the +// table faults in a guard region, as for queues and fences. The console allocator's extra methods are not +// measured and not guessed. Slot 9 wraps only an allocator that answers kLeonidaCommandAllocatorTag, Leonida's +// own Vulkan-layer allocator: its command lists resolve the proxy (CreateCommandList, Reset). A PC runtime +// allocator goes back unchanged, because the PC runtime's lists would take a proxy for their own allocator; a +// command-list proxy that unwraps at Reset is what lets the host backend wrap allocators too. +// {7d0e6b51-34a2-4f0c-9a61-0c5b2e8f4a10}: the Vulkan layer's CommandAllocator answers it with its implementation. +inline constexpr GUID kLeonidaCommandAllocatorTag{ + 0x7d0e6b51, 0x34a2, 0x4f0c, {0x9a, 0x61, 0x0c, 0x5b, 0x2e, 0x8f, 0x4a, 0x10}}; +bool is_allocator_proxy(const void* allocator); +// The wrapped allocator (no reference added), or null when `allocator` is not an allocator proxy. +ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator); +unsigned allocator_proxy_slot_count(const void* allocator); +unsigned allocator_proxy_table_size(); +std::uint64_t allocator_slot_hits(unsigned slot); +bool allocator_slot_first_call_seen(unsigned slot); +std::uint64_t allocator_table_overruns(); // Heap slots 0-2, 7 (GetDevice -> device proxy) and 9/10 (handle conversion). bool heap_slot_overridden(unsigned slot); diff --git a/runtime/test/allocator_proxy_test.cpp b/runtime/test/allocator_proxy_test.cpp new file mode 100644 index 000000000..733e37cac --- /dev/null +++ b/runtime/test/allocator_proxy_test.cpp @@ -0,0 +1,382 @@ +// Xbox-ABI command allocator proxies (d3d12_xs_device.h, device slot 9) behind the device proxy, on the Vulkan-backed +// device and on the host PC runtime (WARP, and the first hardware adapter when there is one): +// - Vulkan device: slot 9 wraps the allocator; the public chain (9 slots) is forwarded with the real allocator's +// answers, every slot past it up to the end of the table is refused (E_NOTIMPL), and a read past the table faults +// in the guard region and is counted; +// - QueryInterface identity, GetDevice -> the device proxy, references and release orders; +// - a command list created on the proxy and Reset on it (the Vulkan layer resolves the proxy), recording a buffer +// copy that runs through the queue and fence proxies and is read back, twice, with the allocator Reset between; +// and D3D12's allocator rules through the proxy (Reset while a list records: E_FAIL; a second list on a busy +// allocator: E_INVALIDARG); +// - MakeResident/Evict with an allocator proxy give the real allocator's HRESULT; +// - host runtime: slot 9 hands out the PC allocator unwrapped (its lists could not resolve a proxy), and the same +// copy works. +// LEONIDA_VK_VALIDATION=1 runs the Vulkan device under VK_LAYER_KHRONOS_validation with synchronization validation; +// any validation error fails the test. Exits 77 when neither a host D3D12 device nor a Vulkan device can be made. +// No shaders, game data or Xbox DLLs. +#include "vk_test_device.h" +#include "../shims/d3d12_com.h" +#include "../shims/d3d12_xs_device.h" + +#include +#include + +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "%s line %d: %s\n", label, __LINE__, #condition); return 1; } \ +} while (0) + +namespace { + +namespace xs = rt::d3d12_xs; + +template +struct Ref { + T* p = nullptr; + Ref() = default; + Ref(const Ref&) = delete; + Ref& operator=(const Ref&) = delete; + ~Ref() { reset(); } + void reset() { + if (p) p->Release(); + p = nullptr; + } + T* operator->() const { return p; } +}; + +struct Backend { + const char* label; + ID3D12Device* device; // the device proxy + ID3D12Device* real; // the device behind it (PC runtime or Vulkan layer) + bool vulkan; +}; + +using SlotFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, + std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, + std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t); + +// A call through slot `slot` with all words zero, the way a title calls a console method the PC interface lacks. +HRESULT call_slot(void* object, unsigned slot) { + void** vtable = *reinterpret_cast(object); + return static_cast(static_cast( + reinterpret_cast(vtable[slot])(object, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))); +} + +ULONG_PTR g_fault_address = 0; + +int record_fault(EXCEPTION_POINTERS* info) { + g_fault_address = info->ExceptionRecord->NumberParameters >= 2 ? info->ExceptionRecord->ExceptionInformation[1] : 0; + return EXCEPTION_EXECUTE_HANDLER; +} + +// The read in a function of its own: clang-cl's __try only catches a fault raised inside a call. +__declspec(noinline) void* read_entry(void* const* vtable, unsigned slot) { + return *static_cast(&vtable[slot]); +} + +// Reads vtable[slot] the way a call through it does. The exception code, or 0 when the read worked. +DWORD read_slot_guarded(void* const* vtable, unsigned slot) { + g_fault_address = 0; + __try { + void* volatile entry = read_entry(vtable, slot); + (void)entry; + return 0; + } __except (record_fault(GetExceptionInformation())) { + return GetExceptionCode(); + } +} + +int wait_for(const char* label, ID3D12Fence* fence, UINT64 value) { + HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(event != nullptr); + const bool set = SUCCEEDED(fence->SetEventOnCompletion(value, event)) && WaitForSingleObject(event, 5000) == 0; + CloseHandle(event); + EXPECT(set && fence->GetCompletedValue() >= value); + return 0; +} + +int make_buffer(const char* label, ID3D12Device* device, D3D12_HEAP_TYPE heap_type, D3D12_RESOURCE_STATES state, + Ref& out) { + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = heap_type; + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + desc.Width = 256; + desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1; + desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, + IID_PPV_ARGS(&out.p)))); + return 0; +} + +// One recording on `list` (open, recording into the allocator): 256 bytes of `byte` copied from an upload buffer to a +// readback buffer, executed through the queue proxy, finished through the fence proxy at `value`, then compared. +int copy_round(const char* label, ID3D12Device* device, ID3D12CommandQueue* queue, ID3D12Fence* fence, + ID3D12GraphicsCommandList* list, std::uint8_t byte, UINT64 value) { + Ref upload, readback; + if (make_buffer(label, device, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, upload)) return 1; + if (make_buffer(label, device, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST, readback)) return 1; + void* mapped = nullptr; + EXPECT(SUCCEEDED(upload->Map(0, nullptr, &mapped)) && mapped); + std::memset(mapped, byte, 256); + upload->Unmap(0, nullptr); + list->CopyBufferRegion(readback.p, 0, upload.p, 0, 256); + EXPECT(SUCCEEDED(list->Close())); + ID3D12CommandList* lists[] = {list}; + queue->ExecuteCommandLists(1, lists); + EXPECT(SUCCEEDED(queue->Signal(fence, value))); + if (wait_for(label, fence, value)) return 1; + const D3D12_RANGE read{0, 256}; + EXPECT(SUCCEEDED(readback->Map(0, &read, &mapped)) && mapped); + const auto* bytes = static_cast(mapped); + int wrong = 0; + for (int i = 0; i < 256; ++i) wrong += bytes[i] != byte; + const D3D12_RANGE none{0, 0}; + readback->Unmap(0, &none); + EXPECT(wrong == 0); + return 0; +} + +// ---- 1. The proxy itself: slots, identity, guard (Vulkan device) ---- + +int check_proxy(const Backend& b, ID3D12CommandAllocator* allocator) { + const char* label = b.label; + ID3D12CommandAllocator* real = xs::allocator_proxy_real_allocator(allocator); + EXPECT(real && static_cast(real) != static_cast(allocator)); + EXPECT(xs::allocator_proxy_slot_count(allocator) == 9); + EXPECT(xs::command_object_proxy_ref_count(allocator) == 1); + + // QueryInterface: the base chain and the allocator itself answer with the proxy; anything else is refused. + const IID modelled[] = {__uuidof(IUnknown), __uuidof(ID3D12Object), __uuidof(ID3D12DeviceChild), + __uuidof(ID3D12Pageable), __uuidof(ID3D12CommandAllocator)}; + for (const IID& iid : modelled) { + void* same = nullptr; + EXPECT(SUCCEEDED(allocator->QueryInterface(iid, &same)) && same == allocator); + static_cast(same)->Release(); + } + void* other = reinterpret_cast(1); + EXPECT(allocator->QueryInterface(__uuidof(ID3D12Fence), &other) == E_NOINTERFACE && !other); + other = reinterpret_cast(1); // the Vulkan layer's private tag: not modelled, the real object stays hidden + EXPECT(allocator->QueryInterface(xs::kLeonidaCommandAllocatorTag, &other) == E_NOINTERFACE && !other); + EXPECT(xs::command_object_proxy_ref_count(allocator) == 1); + + ID3D12Device* owner = nullptr; + EXPECT(SUCCEEDED(allocator->GetDevice(IID_PPV_ARGS(&owner))) && owner == b.device); + owner->Release(); + // Private data and SetName go to the real allocator; the proxy pointer round-trips as an interface. + EXPECT(SUCCEEDED(allocator->SetName(L"leonida allocator proxy"))); + constexpr GUID key{0x2a6b8c41, 0x77d2, 0x4e19, {0x9c, 0x3a, 0x51, 0xe0, 0x8f, 0x24, 0x6d, 0x0b}}; + EXPECT(SUCCEEDED(allocator->SetPrivateDataInterface(key, allocator))); + IUnknown* back = nullptr; + UINT size = sizeof(back); + EXPECT(SUCCEEDED(allocator->GetPrivateData(key, &size, &back)) && back == allocator); + back->Release(); + EXPECT(SUCCEEDED(allocator->SetPrivateDataInterface(key, nullptr))); + + // The refused tail: every slot past the public chain, up to the table end, is E_NOTIMPL and counted. + const unsigned table = xs::allocator_proxy_table_size(); + EXPECT(table == 64); + for (unsigned slot = 9; slot < table; ++slot) { + const std::uint64_t before = xs::allocator_slot_hits(slot); + EXPECT(call_slot(allocator, slot) == E_NOTIMPL); + EXPECT(xs::allocator_slot_hits(slot) == before + 1 && xs::allocator_slot_first_call_seen(slot)); + } + + // Past the table: the vtable read itself faults in the guard region, at the slot's address, and is counted. + EXPECT(xs::command_object_tables_guarded()); + void* const* vtable = *reinterpret_cast(allocator); + EXPECT(read_slot_guarded(vtable, table - 1) == 0); + for (unsigned slot : {table, table + 512, table + 7000}) { + const std::uint64_t before = xs::allocator_table_overruns(); + EXPECT(read_slot_guarded(vtable, slot) == EXCEPTION_ACCESS_VIOLATION); + EXPECT(g_fault_address == reinterpret_cast(&vtable[slot])); + EXPECT(xs::allocator_table_overruns() == before + 1); + } + std::printf("%s: allocator proxy: 9 public slots forwarded, slots 9-%u refused, the guard region names a read " + "past the table; QueryInterface/GetDevice/private data keep the proxy identity\n", label, table - 1); + return 0; +} + +// ---- 2. Lists on the allocator, GPU work, D3D12's allocator rules, residency ---- + +int check_backend(const Backend& b) { + const char* label = b.label; + const unsigned long device_refs = xs::device_proxy_ref_count(b.device); + { + Ref allocator; + EXPECT(SUCCEEDED(b.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, + IID_PPV_ARGS(&allocator.p)))); + // Vulkan layer: wrapped. Host runtime: its lists could not resolve a proxy, so the PC allocator comes back. + EXPECT(xs::is_allocator_proxy(allocator.p) == b.vulkan); + if (b.vulkan && check_proxy(b, allocator.p)) return 1; + // As IUnknown, and without an output (the device's own answer), as slot 8 does for queues. + Ref as_unknown; + EXPECT(SUCCEEDED(b.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(IUnknown), + reinterpret_cast(&as_unknown.p)))); + EXPECT(xs::is_allocator_proxy(as_unknown.p) == b.vulkan); +#pragma warning(suppress : 6387) // the null output on purpose + EXPECT(b.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), + nullptr) == + b.real->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), + nullptr)); + Ref not_an_allocator; // asked for an interface the allocator does not have + not_an_allocator.p = reinterpret_cast(1); + const HRESULT wrong_iid = b.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, + IID_PPV_ARGS(¬_an_allocator.p)); + const bool cleared = !not_an_allocator.p; + not_an_allocator.p = nullptr; + EXPECT(FAILED(wrong_iid) && cleared); + + D3D12_COMMAND_QUEUE_DESC queue_desc{}; + Ref queue; + Ref fence; + EXPECT(SUCCEEDED(b.device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); + EXPECT(SUCCEEDED(b.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + EXPECT(xs::is_queue_proxy(queue.p) && xs::is_fence_proxy(fence.p)); + + // A list on the allocator: the Vulkan layer resolves the proxy in CreateCommandList. + Ref list; + EXPECT(SUCCEEDED(b.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + // D3D12's rules, through the proxy: a recording list holds the allocator. + const std::uint64_t resets_before = xs::allocator_slot_hits(8); + EXPECT(allocator->Reset() == E_FAIL); + Ref second; + EXPECT(b.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&second.p)) == E_INVALIDARG && !second.p); + if (copy_round(label, b.device, queue.p, fence.p, list.p, 0x5a, 1)) return 1; + + // Done on the GPU: the allocator resets through the proxy, and the list resets onto it (Reset resolves it). + EXPECT(SUCCEEDED(allocator->Reset())); + EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr))); + if (copy_round(label, b.device, queue.p, fence.p, list.p, 0xa5, 2)) return 1; + // The two allocator Reset calls above went through slot 8 (list->Reset is the list's method, not this one). + if (b.vulkan) EXPECT(xs::allocator_slot_hits(8) == resets_before + 2); + + // Residency: the real runtime gets the real allocator (an allocator is a pageable). + ID3D12Pageable* pageable = allocator.p; + ID3D12CommandAllocator* real_allocator = + b.vulkan ? xs::allocator_proxy_real_allocator(allocator.p) : allocator.p; + ID3D12Pageable* real_pageable = real_allocator; + EXPECT(b.device->MakeResident(1, &pageable) == b.real->MakeResident(1, &real_pageable)); + EXPECT(b.device->Evict(1, &pageable) == b.real->Evict(1, &real_pageable)); + + // Release orders: the allocator first while its (closed) list still exists, then the list. + if (b.vulkan) EXPECT(xs::command_object_proxy_ref_count(allocator.p) == 1); + allocator.reset(); + list.reset(); + } + EXPECT(xs::device_proxy_ref_count(b.device) == device_refs); // nothing the proxies made kept the device + std::printf("%s: CreateCommandAllocator %s; a list created and Reset on it, two GPU copies through the queue and " + "fence proxies read back exactly; allocator rules and residency as on the real allocator\n", label, + b.vulkan ? "returns an allocator proxy" : "returns the PC allocator unwrapped"); + return 0; +} + +int run_host(IDXGIAdapter1* adapter, const char* label) { + ID3D12Device* real = nullptr; + if (FAILED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&real)))) { + std::printf("%s: no D3D12 device, skipped\n", label); + return 77; + } + real->AddRef(); // the backend's own reference; the proxy takes the other + Ref device; + if (FAILED(xs::create_device_proxy(real, __uuidof(ID3D12Device), reinterpret_cast(&device.p)))) { + real->Release(); + real->Release(); + std::fprintf(stderr, "%s: device proxy creation failed\n", label); + return 1; + } + const int result = check_backend(Backend{label, device.p, real, false}); + device.reset(); + if (result) return 1; + if (real->Release() != 0) { + std::fprintf(stderr, "%s: the PC device outlives its proxies\n", label); + return 1; + } + return 0; +} + +} // namespace + +// A failure ends the process with a message on stderr, never in a dialog a test run would wait on. +static void no_dialogs() { + _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); + SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); +} + +int main() { + no_dialogs(); + std::setvbuf(stdout, nullptr, _IONBF, 0); + const char* label = "allocator_proxy_test"; + int ran = 0; + + // Host PC runtime: WARP, then the first hardware adapter. Under Wine the host d3d12.dll is Wine's own, not the + // PC runtime the proxy is written against, so only Vulkan runs there. + const bool wine = GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "wine_get_version") != nullptr; + Ref factory; + if (wine) std::printf("host D3D12: under Wine, skipped\n"); + if (!wine && SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) { + Ref warp; + if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p)))) { + const int r = run_host(warp.p, "host WARP"); + if (r == 1) return 1; + ran += r == 0; + } + for (UINT i = 0;; ++i) { + Ref adapter; + if (factory->EnumAdapters1(i, &adapter.p) == DXGI_ERROR_NOT_FOUND) break; + DXGI_ADAPTER_DESC1 desc{}; + if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) continue; + char name[128] = {}; + WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, name, sizeof(name) - 1, nullptr, nullptr); + char host_label[160]; + std::snprintf(host_label, sizeof(host_label), "host %s", name); + const int r = run_host(adapter.p, host_label); + if (r == 1) return 1; + ran += r == 0; + break; + } + } + + // The Vulkan-backed device, the way D3D12XboxCreateDevice hands it out with LEONIDA_D3D12=vulkan. + vktest::Device d; + if (d.create()) { + label = "Vulkan"; + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = d.instance_proc; + info.instance = d.instance; + info.physical = d.physical; + info.device = d.device; + info.queue = d.queue; + info.queue_family = d.family; + ID3D12Device* inner = nullptr; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); + inner->AddRef(); // the backend's own reference; the proxy takes the other + ID3D12Device* device = nullptr; + EXPECT(SUCCEEDED(xs::create_device_proxy(inner, __uuidof(ID3D12Device), reinterpret_cast(&device)))); + rt::d3d12::set_device_outer(inner, device); + const int result = check_backend(Backend{label, device, inner, true}); + device->Release(); + if (result) return 1; + EXPECT(inner->Release() == 0); // nothing the proxies made kept the layer's device alive + ++ran; + if (d.validation_errors()) { + std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); + return 1; + } + } else { + std::printf("Vulkan: no device, skipped\n"); + } + + if (!ran) { + std::printf("SKIP allocator_proxy_test: neither a host D3D12 device nor a Vulkan device\n"); + return 77; + } + std::printf("allocator_proxy_test: %d backend(s) passed\n", ran); + return 0; +} diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp index e414f155b..64df3e21d 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp @@ -151,7 +151,7 @@ int main() { unsigned slots = rt::d3d12_xs::device_proxy_slot_count(device.Get()); std::printf("proxy forwards %u PC vtable slots (ID3D12Device..Device14 max is 83)\n", slots); check(slots >= 44, "proxy forwards at least the base ID3D12Device vtable"); - check(!rt::d3d12_xs::device_slot_overridden(9), "device slot 9 is forwarded (PC CreateCommandAllocator)"); + check(rt::d3d12_xs::device_slot_overridden(9), "device slot 9 is CreateCommandAllocator (allocator proxies)"); check(!rt::d3d12_xs::device_slot_overridden(10), "device slot 10 is forwarded (PC CreateGraphicsPipelineState)"); check(rt::d3d12_xs::device_slot_overridden(14), "device slot 14 is the descriptor-heap override"); check(rt::d3d12_xs::device_slot_overridden(62), "device slot 62 is the compute-PSO override"); @@ -278,6 +278,8 @@ int main() { ComPtr allocator; hr = device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(allocator.GetAddressOf())); check(SUCCEEDED(hr) && allocator != nullptr, "device slot 9 forwards CreateCommandAllocator"); + // A PC runtime allocator is not wrapped: the PC runtime's lists could not resolve an allocator proxy. + check(!rt::d3d12_xs::is_allocator_proxy(allocator.Get()), "slot 9 hands out the PC allocator unwrapped"); check(rt::d3d12_xs::device_slot_hits(9) == 1, "slot 9 hit counter is 1"); // Slot 76: buffer resource with the measured Xbox layout word 0x101; PC committed resources take ROW_MAJOR. diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 429261e98..f115b6655 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -70,7 +70,7 @@ namespace xs = rt::d3d12_xs; // ---- Mocks ---- -enum class Kind { device, queue, fence, resource }; +enum class Kind { device, queue, fence, resource, allocator }; constexpr unsigned kMockSlots = 128; constexpr std::uint64_t kFenceHandle = 0x1000, kResourceHandle = 0x2000; // the handles the mock device opens @@ -84,6 +84,7 @@ struct Mock { long refs; int device_level; // device: highest ID3D12DeviceN it answers (0 = ID3D12Device only) bool fence1; // fence: answers ID3D12Fence1; device: the fences it makes do + bool allocator_tag; // allocator: answers kLeonidaCommandAllocatorTag; device: the allocators it makes do bool create_null; // device: creation returns create_result with no object HRESULT create_result; // device: what creation returns (S_OK makes the object) Mock* last_child; // device: the object its last creation or open made @@ -126,6 +127,9 @@ bool mock_has(const Mock* m, REFIID iid) { case Kind::fence: return IsEqualIID(iid, __uuidof(ID3D12Fence)) || (m->fence1 && IsEqualIID(iid, __uuidof(ID3D12Fence1))); case Kind::resource: return IsEqualIID(iid, __uuidof(ID3D12Resource)); + case Kind::allocator: + return IsEqualIID(iid, __uuidof(ID3D12CommandAllocator)) || + (m->allocator_tag && IsEqualIID(iid, xs::kLeonidaCommandAllocatorTag)); default: return false; } } @@ -148,6 +152,7 @@ HRESULT mock_create(Mock* device, Kind kind, std::uint64_t riid, std::uint64_t o if (device->create_null || FAILED(device->create_result)) return device->create_result; Mock* child = new_mock(kind); child->fence1 = device->fence1; + child->allocator_tag = device->allocator_tag; device->last_child = child; return mock_query(child, reinterpret_cast(riid), out); } @@ -163,6 +168,7 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { if (m->kind == Kind::device) { switch (slot) { case 8: return static_cast(mock_create(m, Kind::queue, a2, a3)); // CreateCommandQueue + case 9: return static_cast(mock_create(m, Kind::allocator, a2, a3)); // CreateCmdAllocator case 75: return static_cast(mock_create(m, Kind::queue, a3, a4)); // CreateCommandQueue1 case 36: return static_cast(mock_create(m, Kind::fence, a3, a4)); // CreateFence case 32: // OpenSharedHandle @@ -248,10 +254,11 @@ const IID kDevice1Iid = __uuidof(ID3D12Device1); const D3D12_COMMAND_QUEUE_DESC kQueueDesc{}; // A device proxy over a new mock device. The proxy takes the mock's one reference. -void* make_device(Mock*& mock, int level, bool fence1) { +void* make_device(Mock*& mock, int level, bool fence1, bool allocator_tag = true) { mock = new_mock(Kind::device); mock->device_level = level; mock->fence1 = fence1; + mock->allocator_tag = allocator_tag; mock->refs = 1; void* device = nullptr; if (FAILED(xs::create_device_proxy(reinterpret_cast(mock), __uuidof(ID3D12Device), &device))) @@ -563,7 +570,85 @@ int check_alloc_failures() { return 0; } -// ---- 6. The refused tail and the guard region ---- +// ---- 6. Command allocators (device slot 9) ---- + +int check_allocators() { + const IID allocator_iid = __uuidof(ID3D12CommandAllocator); + // An allocator that answers Leonida's tag (its lists resolve a proxy) comes back wrapped. + { + Mock* mock = nullptr; + void* device = make_device(mock, 9, true, true); + EXPECT(device); + const unsigned long device_refs = xs::device_proxy_ref_count(device); + void* allocator = nullptr; + EXPECT(hr_of(raw(device, 9, D3D12_COMMAND_LIST_TYPE_DIRECT, word(&allocator_iid), word(&allocator))) == S_OK); + Mock* const real = mock->last_child; + EXPECT(xs::is_allocator_proxy(allocator) && word(xs::allocator_proxy_real_allocator(allocator)) == word(real)); + EXPECT(mock->args[9][0] == D3D12_COMMAND_LIST_TYPE_DIRECT && real->refs == 1); + EXPECT(xs::allocator_proxy_slot_count(allocator) == 9 && xs::device_proxy_ref_count(device) == device_refs + 1); + // Reset (slot 8) reaches the real allocator; every slot past it up to the table end is refused. + EXPECT(hr_of(raw(allocator, 8)) == S_OK && real->calls[8] == 1); + for (unsigned slot = 9; slot < xs::allocator_proxy_table_size(); ++slot) + EXPECT(hr_of(raw(allocator, slot)) == E_NOTIMPL && real->calls[slot] == 0); + // The tag and the real object stay hidden: QueryInterface answers only the modelled interfaces. + void* found = reinterpret_cast(1); + EXPECT(hr_of(raw(allocator, 0, word(&xs::kLeonidaCommandAllocatorTag), word(&found))) == E_NOINTERFACE && + !found); + EXPECT(hr_of(raw(allocator, 0, word(&allocator_iid), word(&found))) == S_OK && found == allocator); + EXPECT(release(found) == 1); + // An allocator is a pageable: MakeResident gives the real runtime the real allocator. + const std::uint64_t objects[1] = {word(allocator)}; + EXPECT(hr_of(raw(device, 34, 1, word(objects))) == S_OK && mock->array[34][0] == word(real)); + EXPECT(release(allocator) == 0 && real->refs == 0); + EXPECT(xs::device_proxy_ref_count(device) == device_refs); + // Null IID, null output, a failing or empty creation, an IID the allocator lacks. + void* out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 9, 0, 0, word(&out))) == E_POINTER && !out); + EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), 0)) == S_FALSE); // the device's own answer + mock->create_result = E_OUTOFMEMORY; + out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), word(&out))) == E_OUTOFMEMORY && !out); + mock->create_result = S_OK; + mock->create_null = true; + out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), word(&out))) == E_FAIL && !out); + mock->create_null = false; + const IID fence_iid = __uuidof(ID3D12Fence); + out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 9, 0, word(&fence_iid), word(&out))) == E_NOINTERFACE && !out); + EXPECT(mock->last_child->refs == 0); + // A refused allocation for the proxy: E_OUTOFMEMORY, and the real allocator is released. + g_fail_news = 1; + out = reinterpret_cast(1); + const HRESULT starved = hr_of(raw(device, 9, 0, word(&allocator_iid), word(&out))); + g_fail_news = 0; + EXPECT(starved == E_OUTOFMEMORY && !out && mock->last_child->refs == 0); + EXPECT(xs::device_proxy_ref_count(device) == device_refs); + EXPECT(release(device) == 0 && mock->refs == 0); + } + // An allocator without the tag (a PC runtime allocator, whose lists could not resolve a proxy) comes back as it + // is, as the interface asked for. + { + Mock* mock = nullptr; + void* device = make_device(mock, 9, true, false); + EXPECT(device); + void* allocator = nullptr; + EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), word(&allocator))) == S_OK); + EXPECT(allocator == mock->last_child && !xs::is_allocator_proxy(allocator) && mock->last_child->refs == 1); + Mock* const real = mock->last_child; + EXPECT(release(allocator) == 0 && real->refs == 0); + const IID fence_iid = __uuidof(ID3D12Fence); + void* out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 9, 0, word(&fence_iid), word(&out))) == E_NOINTERFACE && !out); + EXPECT(mock->last_child->refs == 0); + EXPECT(release(device) == 0 && mock->refs == 0); + } + std::printf("%s: allocators answering Leonida's tag are wrapped (Reset forwarded, tail refused, residency " + "unwrapped, references balanced); others come back unwrapped; failures leave no reference\n", g_label); + return 0; +} + +// ---- 7. The refused tail and the guard region ---- int check_tail(bool want_guarded) { Mock* mock = nullptr; @@ -686,7 +771,7 @@ int main(int argc, char** argv) { if (argc == 2) return run_fault_mode(argv[1]); if (argc != 1) return 2; if (check_tail(true) || check_levels() || check_fence1() || check_create_failures() || check_open_shared() || - check_alloc_failures()) + check_alloc_failures() || check_allocators()) return 1; std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); return 0; -- cgit v1.2.3