diff options
| author | DevilNine <102010568+DevilNine@users.noreply.github.com> | 2026-11-23 14:46:12 -0300 |
|---|---|---|
| committer | Mumogg <104529023+Mumogg@users.noreply.github.com> | 2026-11-24 03:52:41 -0600 |
| commit | bbae96de2a46e472c5e8627829d17c648b06366e (patch) | |
| tree | 7cd02c19ef7009dced9ea8f9337dba25a7c37f0a /runtime | |
| parent | 1bd72a87ada80475d2a51f9eda359906d9962a3f (diff) | |
d3d12_xs: handle ID3D12CommandAllocator slot 9 (SetAllocatorCallbackX) on allocator proxy
(cherry picked from commit e29e9899809347a730fd57a14532a22fe0ee7231)
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/d3d12_xs_device.cpp | 34 | ||||
| -rw-r--r-- | runtime/shims/d3d12_xs_device.h | 12 | ||||
| -rw-r--r-- | runtime/test/allocator_proxy_test.cpp | 31 | ||||
| -rw-r--r-- | runtime/test/xs_proxy_mock_test.cpp | 16 |
4 files changed, 80 insertions, 13 deletions
diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index f293ea049..dd84c7b68 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp | |||
| @@ -816,6 +816,9 @@ struct ChildProxy { | |||
| 816 | void** real_vtable; | 816 | void** real_vtable; |
| 817 | unsigned slot_count; // public slots the real object implements; the rest of the table is refused | 817 | unsigned slot_count; // public slots the real object implements; the rest of the table is refused |
| 818 | std::atomic<unsigned long> refs; | 818 | std::atomic<unsigned long> refs; |
| 819 | // Allocators only: callback and context recorded by console slot 9 (SetAllocatorCallbackX). | ||
| 820 | std::atomic<std::uintptr_t> allocator_callback{0}; | ||
| 821 | std::atomic<std::uintptr_t> allocator_callback_context{0}; | ||
| 819 | }; | 822 | }; |
| 820 | 823 | ||
| 821 | template <ChildKind K> | 824 | template <ChildKind K> |
| @@ -853,6 +856,7 @@ const char* child_public_name(unsigned slot) { | |||
| 853 | if (slot < kFence1PublicSlots) return fence[slot - 8]; | 856 | if (slot < kFence1PublicSlots) return fence[slot - 8]; |
| 854 | } else { | 857 | } else { |
| 855 | if (slot == 8) return "Reset"; | 858 | if (slot == 8) return "Reset"; |
| 859 | if (slot == 9) return "SetAllocatorCallbackX"; | ||
| 856 | } | 860 | } |
| 857 | return nullptr; | 861 | return nullptr; |
| 858 | } | 862 | } |
| @@ -1354,6 +1358,24 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ | |||
| 1354 | d3d12_trace::record(child_trace_name<K>(Slot)); | 1358 | d3d12_trace::record(child_trace_name<K>(Slot)); |
| 1355 | return queue_execute_command_lists(self, a1, a2); | 1359 | return queue_execute_command_lists(self, a1, a2); |
| 1356 | } | 1360 | } |
| 1361 | if constexpr (K == ChildKind::allocator && Slot == 9) { | ||
| 1362 | // Console-only ID3D12CommandAllocator::SetAllocatorCallbackX(callback, context, ...), observed past the | ||
| 1363 | // 9-slot PC chain on the retail executable (call shape: function pointer, context, plus trailing words). | ||
| 1364 | // The PC/Vulkan layer manages command-buffer backing directly without invoking a title allocator callback, | ||
| 1365 | // so record the callback and context on the proxy and return S_OK while leaving slots 10+ refused. | ||
| 1366 | self->allocator_callback.store(static_cast<std::uintptr_t>(a1), std::memory_order_relaxed); | ||
| 1367 | self->allocator_callback_context.store(static_cast<std::uintptr_t>(a2), std::memory_order_relaxed); | ||
| 1368 | static std::atomic<bool> logged{false}; | ||
| 1369 | if (!logged.exchange(true, std::memory_order_relaxed)) { | ||
| 1370 | std::fprintf(stderr, | ||
| 1371 | "[rt] d3d12_xs: allocator slot 9 (SetAllocatorCallbackX) recorded " | ||
| 1372 | "(callback=0x%llx context=0x%llx a3=0x%llx a4=0x%llx)\n", | ||
| 1373 | static_cast<unsigned long long>(a1), static_cast<unsigned long long>(a2), | ||
| 1374 | static_cast<unsigned long long>(a3), static_cast<unsigned long long>(a4)); | ||
| 1375 | } | ||
| 1376 | d3d12_trace::record(child_trace_name<K>(Slot)); | ||
| 1377 | return hr_to_u64(S_OK); | ||
| 1378 | } | ||
| 1357 | if (Slot >= self->slot_count) { | 1379 | if (Slot >= self->slot_count) { |
| 1358 | bool expected = false; | 1380 | bool expected = false; |
| 1359 | if (ChildStats<K>::beyond_warned[Slot].compare_exchange_strong(expected, true, | 1381 | if (ChildStats<K>::beyond_warned[Slot].compare_exchange_strong(expected, true, |
| @@ -2948,6 +2970,18 @@ unsigned allocator_proxy_slot_count(const void* allocator) { | |||
| 2948 | return is_allocator_proxy(allocator) ? static_cast<const ChildProxy*>(allocator)->slot_count : 0; | 2970 | return is_allocator_proxy(allocator) ? static_cast<const ChildProxy*>(allocator)->slot_count : 0; |
| 2949 | } | 2971 | } |
| 2950 | 2972 | ||
| 2973 | std::uintptr_t allocator_proxy_callback(const void* allocator) { | ||
| 2974 | return is_allocator_proxy(allocator) | ||
| 2975 | ? static_cast<const ChildProxy*>(allocator)->allocator_callback.load(std::memory_order_relaxed) | ||
| 2976 | : 0; | ||
| 2977 | } | ||
| 2978 | |||
| 2979 | std::uintptr_t allocator_proxy_callback_context(const void* allocator) { | ||
| 2980 | return is_allocator_proxy(allocator) | ||
| 2981 | ? static_cast<const ChildProxy*>(allocator)->allocator_callback_context.load(std::memory_order_relaxed) | ||
| 2982 | : 0; | ||
| 2983 | } | ||
| 2984 | |||
| 2951 | unsigned long command_object_proxy_ref_count(const void* object) { | 2985 | unsigned long command_object_proxy_ref_count(const void* object) { |
| 2952 | if (!is_queue_proxy(object) && !is_fence_proxy(object) && !is_allocator_proxy(object)) return 0; | 2986 | if (!is_queue_proxy(object) && !is_fence_proxy(object) && !is_allocator_proxy(object)) return 0; |
| 2953 | return static_cast<const ChildProxy*>(object)->refs.load(std::memory_order_relaxed); | 2987 | return static_cast<const ChildProxy*>(object)->refs.load(std::memory_order_relaxed); |
diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 9f7282025..652b6047c 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h | |||
| @@ -196,10 +196,11 @@ std::uint64_t fence_table_overruns(); | |||
| 196 | // or the fault logger failed). | 196 | // or the fault logger failed). |
| 197 | bool command_object_tables_guarded(); | 197 | bool command_object_tables_guarded(); |
| 198 | 198 | ||
| 199 | // Command allocator proxies (device slot 9): the public chain (9 slots: the base chain and Reset) is forwarded, a | 199 | // Command allocator proxies (device slot 9): the public chain (9 slots: the base chain and Reset) is forwarded, |
| 200 | // slot past it up to the end of the table (64 slots) is logged and refused with E_NOTIMPL, and a read past the | 200 | // slot 9 (console SetAllocatorCallbackX(callback, context, ...)) records the callback and context on the proxy and |
| 201 | // table faults in a guard region, as for queues and fences. The console allocator's extra methods are not | 201 | // returns S_OK without invoking the callback on the PC/Vulkan path, slots 10..63 are logged and refused with |
| 202 | // measured and not guessed. Slot 9 wraps only an allocator that answers kLeonidaCommandAllocatorTag, Leonida's | 202 | // E_NOTIMPL, and a read past the table faults in a guard region, as for queues and fences. Slot 9 wraps only an |
| 203 | // allocator that answers kLeonidaCommandAllocatorTag, Leonida's | ||
| 203 | // own Vulkan-layer allocator: its command lists resolve the proxy (CreateCommandList, Reset). A PC runtime | 204 | // own Vulkan-layer allocator: its command lists resolve the proxy (CreateCommandList, Reset). A PC runtime |
| 204 | // allocator goes back unchanged, because the PC runtime's lists would take a proxy for their own allocator; a | 205 | // allocator goes back unchanged, because the PC runtime's lists would take a proxy for their own allocator; a |
| 205 | // command-list proxy that unwraps at Reset is what lets the host backend wrap allocators too. | 206 | // command-list proxy that unwraps at Reset is what lets the host backend wrap allocators too. |
| @@ -211,6 +212,9 @@ bool is_allocator_proxy(const void* allocator); | |||
| 211 | ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator); | 212 | ID3D12CommandAllocator* allocator_proxy_real_allocator(const void* allocator); |
| 212 | unsigned allocator_proxy_slot_count(const void* allocator); | 213 | unsigned allocator_proxy_slot_count(const void* allocator); |
| 213 | unsigned allocator_proxy_table_size(); | 214 | unsigned allocator_proxy_table_size(); |
| 215 | // Callback and context recorded by console allocator slot 9 (SetAllocatorCallbackX), or 0 if unset / not a proxy. | ||
| 216 | std::uintptr_t allocator_proxy_callback(const void* allocator); | ||
| 217 | std::uintptr_t allocator_proxy_callback_context(const void* allocator); | ||
| 214 | std::uint64_t allocator_slot_hits(unsigned slot); | 218 | std::uint64_t allocator_slot_hits(unsigned slot); |
| 215 | bool allocator_slot_first_call_seen(unsigned slot); | 219 | bool allocator_slot_first_call_seen(unsigned slot); |
| 216 | std::uint64_t allocator_table_overruns(); | 220 | std::uint64_t allocator_table_overruns(); |
diff --git a/runtime/test/allocator_proxy_test.cpp b/runtime/test/allocator_proxy_test.cpp index 5f89366f5..b48b224b7 100644 --- a/runtime/test/allocator_proxy_test.cpp +++ b/runtime/test/allocator_proxy_test.cpp | |||
| @@ -58,11 +58,13 @@ using SlotFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std:: | |||
| 58 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, | 58 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, |
| 59 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t); | 59 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t); |
| 60 | 60 | ||
| 61 | // A call through slot `slot` with all words zero, the way a title calls a console method the PC interface lacks. | 61 | // A call through slot `slot` with up to four explicit words (rest zero), the way a title calls a console method the PC |
| 62 | HRESULT call_slot(void* object, unsigned slot) { | 62 | // interface lacks. |
| 63 | HRESULT call_slot(void* object, unsigned slot, std::uint64_t a1 = 0, std::uint64_t a2 = 0, std::uint64_t a3 = 0, | ||
| 64 | std::uint64_t a4 = 0) { | ||
| 63 | void** vtable = *reinterpret_cast<void***>(object); | 65 | void** vtable = *reinterpret_cast<void***>(object); |
| 64 | return static_cast<HRESULT>(static_cast<std::uint32_t>( | 66 | return static_cast<HRESULT>(static_cast<std::uint32_t>( |
| 65 | reinterpret_cast<SlotFn>(vtable[slot])(object, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))); | 67 | reinterpret_cast<SlotFn>(vtable[slot])(object, a1, a2, a3, a4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))); |
| 66 | } | 68 | } |
| 67 | 69 | ||
| 68 | ULONG_PTR g_fault_address = 0; | 70 | ULONG_PTR g_fault_address = 0; |
| @@ -182,10 +184,24 @@ int check_proxy(const Backend& b, ID3D12CommandAllocator* allocator) { | |||
| 182 | back->Release(); | 184 | back->Release(); |
| 183 | EXPECT(SUCCEEDED(allocator->SetPrivateDataInterface(key, nullptr))); | 185 | EXPECT(SUCCEEDED(allocator->SetPrivateDataInterface(key, nullptr))); |
| 184 | 186 | ||
| 185 | // The refused tail: every slot past the public chain, up to the table end, is E_NOTIMPL and counted. | 187 | // Console slot 9 (SetAllocatorCallbackX): records callback/context on the proxy and returns S_OK. |
| 186 | const unsigned table = xs::allocator_proxy_table_size(); | 188 | const unsigned table = xs::allocator_proxy_table_size(); |
| 187 | EXPECT(table == 64); | 189 | EXPECT(table == 64); |
| 188 | for (unsigned slot = 9; slot < table; ++slot) { | 190 | { |
| 191 | EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0); | ||
| 192 | constexpr std::uintptr_t fake_cb = 0x11223344u; | ||
| 193 | constexpr std::uintptr_t fake_ctx = 0x55667788u; | ||
| 194 | const std::uint64_t before = xs::allocator_slot_hits(9); | ||
| 195 | EXPECT(call_slot(allocator, 9, fake_cb, fake_ctx, 0, 0x99aabbccu) == S_OK); | ||
| 196 | EXPECT(xs::allocator_slot_hits(9) == before + 1 && xs::allocator_slot_first_call_seen(9)); | ||
| 197 | EXPECT(xs::allocator_proxy_callback(allocator) == fake_cb && | ||
| 198 | xs::allocator_proxy_callback_context(allocator) == fake_ctx); | ||
| 199 | EXPECT(call_slot(allocator, 9, 0, 0) == S_OK); | ||
| 200 | EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0); | ||
| 201 | } | ||
| 202 | |||
| 203 | // The refused tail: every slot past slot 9, up to the table end, is E_NOTIMPL and counted. | ||
| 204 | for (unsigned slot = 10; slot < table; ++slot) { | ||
| 189 | const std::uint64_t before = xs::allocator_slot_hits(slot); | 205 | const std::uint64_t before = xs::allocator_slot_hits(slot); |
| 190 | EXPECT(call_slot(allocator, slot) == E_NOTIMPL); | 206 | EXPECT(call_slot(allocator, slot) == E_NOTIMPL); |
| 191 | EXPECT(xs::allocator_slot_hits(slot) == before + 1 && xs::allocator_slot_first_call_seen(slot)); | 207 | EXPECT(xs::allocator_slot_hits(slot) == before + 1 && xs::allocator_slot_first_call_seen(slot)); |
| @@ -201,8 +217,9 @@ int check_proxy(const Backend& b, ID3D12CommandAllocator* allocator) { | |||
| 201 | EXPECT(g_fault_address == reinterpret_cast<ULONG_PTR>(&vtable[slot])); | 217 | EXPECT(g_fault_address == reinterpret_cast<ULONG_PTR>(&vtable[slot])); |
| 202 | EXPECT(xs::allocator_table_overruns() == before + 1); | 218 | EXPECT(xs::allocator_table_overruns() == before + 1); |
| 203 | } | 219 | } |
| 204 | std::printf("%s: allocator proxy: 9 public slots forwarded, slots 9-%u refused, the guard region names a read " | 220 | std::printf("%s: allocator proxy: 9 public slots forwarded, slot 9 SetAllocatorCallbackX handled, slots 10-%u " |
| 205 | "past the table; QueryInterface/GetDevice/private data keep the proxy identity\n", label, table - 1); | 221 | "refused, the guard region names a read past the table; QueryInterface/GetDevice/private data keep " |
| 222 | "the proxy identity\n", label, table - 1); | ||
| 206 | return 0; | 223 | return 0; |
| 207 | } | 224 | } |
| 208 | 225 | ||
diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 6745c24dc..83f0240a9 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp | |||
| @@ -935,9 +935,21 @@ int check_allocators() { | |||
| 935 | EXPECT(xs::is_allocator_proxy(allocator) && word(xs::allocator_proxy_real_allocator(allocator)) == word(real)); | 935 | EXPECT(xs::is_allocator_proxy(allocator) && word(xs::allocator_proxy_real_allocator(allocator)) == word(real)); |
| 936 | EXPECT(mock->args[9][0] == D3D12_COMMAND_LIST_TYPE_DIRECT && real->refs == 1); | 936 | EXPECT(mock->args[9][0] == D3D12_COMMAND_LIST_TYPE_DIRECT && real->refs == 1); |
| 937 | EXPECT(xs::allocator_proxy_slot_count(allocator) == 9 && xs::device_proxy_ref_count(device) == device_refs + 1); | 937 | EXPECT(xs::allocator_proxy_slot_count(allocator) == 9 && xs::device_proxy_ref_count(device) == device_refs + 1); |
| 938 | // Reset (slot 8) reaches the real allocator; every slot past it up to the table end is refused. | 938 | // Reset (slot 8) reaches the real allocator; slot 9 (SetAllocatorCallbackX) records callback/context and |
| 939 | // succeeds without touching the real vtable; every slot past 9 up to the table end is refused. | ||
| 939 | EXPECT(hr_of(raw(allocator, 8)) == S_OK && real->calls[8] == 1); | 940 | EXPECT(hr_of(raw(allocator, 8)) == S_OK && real->calls[8] == 1); |
| 940 | for (unsigned slot = 9; slot < xs::allocator_proxy_table_size(); ++slot) | 941 | EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0); |
| 942 | EXPECT(xs::allocator_proxy_callback(nullptr) == 0 && xs::allocator_proxy_callback_context(device) == 0); | ||
| 943 | constexpr std::uintptr_t fake_cb = 0x12345678u; | ||
| 944 | constexpr std::uintptr_t fake_ctx = 0x9abcdef0u; | ||
| 945 | const std::uint64_t slot9_hits_before = xs::allocator_slot_hits(9); | ||
| 946 | EXPECT(hr_of(raw(allocator, 9, fake_cb, fake_ctx, 0, 0xfeedfaceu)) == S_OK && real->calls[9] == 0); | ||
| 947 | EXPECT(xs::allocator_slot_hits(9) == slot9_hits_before + 1 && xs::allocator_slot_first_call_seen(9)); | ||
| 948 | EXPECT(xs::allocator_proxy_callback(allocator) == fake_cb && | ||
| 949 | xs::allocator_proxy_callback_context(allocator) == fake_ctx); | ||
| 950 | EXPECT(hr_of(raw(allocator, 9, 0, 0)) == S_OK && real->calls[9] == 0); | ||
| 951 | EXPECT(xs::allocator_proxy_callback(allocator) == 0 && xs::allocator_proxy_callback_context(allocator) == 0); | ||
| 952 | for (unsigned slot = 10; slot < xs::allocator_proxy_table_size(); ++slot) | ||
| 941 | EXPECT(hr_of(raw(allocator, slot)) == E_NOTIMPL && real->calls[slot] == 0); | 953 | EXPECT(hr_of(raw(allocator, slot)) == E_NOTIMPL && real->calls[slot] == 0); |
| 942 | // The tag and the real object stay hidden: QueryInterface answers only the modelled interfaces. | 954 | // The tag and the real object stay hidden: QueryInterface answers only the modelled interfaces. |
| 943 | void* found = reinterpret_cast<void*>(1); | 955 | void* found = reinterpret_cast<void*>(1); |