diff --git a/docs/console_libraries.md b/docs/console_libraries.md index b7d1ca2d9..cce2facef 100644 --- a/docs/console_libraries.md +++ b/docs/console_libraries.md @@ -88,9 +88,11 @@ descriptor-heap start handles returned in RAX). The other 19 function exports, i out because its return was not measured (**this repo**, `registry.cpp`). The COM surface behind the device — queue, command list, fence, `PresentX` — is vtable work, not -an import problem; it is tracked in [d3d12_vulkan.md](d3d12_vulkan.md) and -[d3d12_command_objects.md](d3d12_command_objects.md). The presentation core -(`runtime/shims/xbox_present.h`) waits on the unmeasured Xbox device/queue slots. +an import problem; it is tracked in [d3d12_vulkan.md](d3d12_vulkan.md), +[d3d12_command_objects.md](d3d12_command_objects.md) and +[d3d12_xs_console_abi.md](d3d12_xs_console_abi.md). The presentation core +(`runtime/shims/xbox_present.h`) is wired to the measured `PresentX` queue slots (30, or 32 on the +newer layout) in `d3d12_xs_device.cpp`; the plane and parameter struct layouts are still provisional. ### `xgameruntime.dll` diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 5882f9478..896c125a8 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1892,7 +1892,7 @@ if(TARGET Vulkan::Headers) # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan). if(TARGET gta6rt) - target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com) + target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com user32) # user32: the PresentX window target_compile_definitions(gta6rt PRIVATE LEONIDA_VULKAN_DEVICE) endif() # bvh_builder_test can put the package's XboxBvhBuilder on this device too (LEONIDA_BVH_DEVICE=vulkan). diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index feef51bc0..28a3ce2cd 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -19,6 +19,12 @@ #include "d3d12_xs_device.h" #include "d3d12_trace.h" +#ifdef LEONIDA_VULKAN_DEVICE +// PresentX wiring: the queue's console slots land on the Xbox presentation core, which needs the Vulkan layer's +// WindowPresenter. Other builds of this file (mock and COM tests) keep the refused-slot behaviour. +#include "xbox_present.h" +#endif + #include #include @@ -45,8 +51,14 @@ struct DeviceProxy { void** real_vtable; unsigned slot_count; // number of valid entries in real_vtable std::atomic refs; + // The PresentX state (window, presenter, XboxPresentation) the first queue PresentX builds on this device, + // under LEONIDA_VULKAN_DEVICE; always null elsewhere. Opaque: PresentState is defined with the queue proxy. + void* present_state = nullptr; }; +// Freed in proxy_release; defined with the queue proxy where PresentState is complete. +void destroy_present_state(void* present_state); + constexpr unsigned kHeapSlots = 11; // IUnknown + ID3D12Object + DeviceChild + Pageable + GetDesc + 2 handles struct HeapProxy { @@ -152,6 +164,7 @@ unsigned long proxy_release(DeviceProxy* self) { note_slot(2); const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1; if (refs == 0) { + destroy_present_state(self->present_state); ID3D12Device* real = self->real; delete self; if (real) real->Release(); @@ -425,6 +438,12 @@ struct ChildProxy { void** real_vtable; unsigned slot_count; // public slots the real object implements; the rest of the table is refused std::atomic refs; + // Queues only: the console runtime layout this title was built against (docs/d3d12_xs_console_abi.md §4), + // detected by a fence reaching slot 16/17. Unused on fences and allocators. + static constexpr unsigned char kQueueLayoutUnknown = 0; + static constexpr unsigned char kQueueLayoutOlder = 1; // PC order; PresentX 30, SignalX 23 + static constexpr unsigned char kQueueLayoutNewer = 2; // +2 shift; PresentX 32, SignalX 25 + unsigned char queue_layout = kQueueLayoutUnknown; }; template @@ -718,6 +737,90 @@ LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) { return EXCEPTION_CONTINUE_SEARCH; // diagnostics only: the fault goes on to the title's handlers or the crash } +// ---- PresentX on the queue (LEONIDA_VULKAN_DEVICE) --------------------------------------------------------- +// +// The measured console queue slots (docs/d3d12_xs_console_abi.md §4): the older runtime keeps the PC order — +// Signal 14, Wait 15, frequency 16, calibration 17, GetDesc 18 — and presents at slot 30, with SignalX at 23. A +// newer game OS inserts two methods before Signal: Signal 16, Wait 17, frequency 18, calibration 19, GetDesc 20, +// SignalX 25, PresentX 32. The layout is detected once, by a fence object reaching slot 16 or 17: on the older +// layout those are the frequency and calibration getters, which take no object argument. +// +// Every other console-only queue method answers S_OK (sibling layer: title frame pacing flows through them, and a +// refusal wedges the present loop), still recorded by note_child_slot. Slot 148's AllocateUploadMemory is a list +// method, not a queue method, so it is not part of this table. + +#ifdef LEONIDA_VULKAN_DEVICE + +struct PresentState { + ~PresentState() { + if (window) DestroyWindow(window); // the presentation and its window die with the device proxy + } + HWND window = nullptr; + std::unique_ptr xbox; + bool tried = false; // creation is attempted once; a null xbox means this device cannot present +}; + +void destroy_present_state(void* present_state) { + delete static_cast(present_state); +} + +LRESULT CALLBACK present_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { + return DefWindowProcW(window, message, wparam, lparam); +} + +// Lazily builds the window, presenter and XboxPresentation a console PresentX needs, once per device. Returns the +// state even when `xbox` stays null (no presentation support): the failure is logged once, not retried. +PresentState* present_state_for(DeviceProxy* device) { + if (auto* state = static_cast(device->present_state)) return state; + auto* state = new (std::nothrow) PresentState; + if (!state) return nullptr; + device->present_state = state; + state->tried = true; + WNDCLASSW window_class{}; + window_class.lpfnWndProc = present_window_proc; + window_class.hInstance = GetModuleHandleW(nullptr); + window_class.lpszClassName = L"LeonidaPresentX"; + if (!RegisterClassW(&window_class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) { + std::fprintf(stderr, "[rt] d3d12_xs: PresentX window class failed (error %lu)\n", GetLastError()); + return state; + } + state->window = CreateWindowExW(0, window_class.lpszClassName, L"Leonida", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, + window_class.hInstance, nullptr); + if (!state->window) { + std::fprintf(stderr, "[rt] d3d12_xs: PresentX window failed (error %lu)\n", GetLastError()); + return state; + } + auto presenter = rt::d3d12::create_window_presenter(device->real, state->window); + if (!presenter) { + std::fprintf(stderr, "[rt] d3d12_xs: no window presenter on this device; PresentX is a no-op\n"); + return state; + } + state->xbox = std::make_unique(std::move(presenter)); + std::fprintf(stderr, "[rt] d3d12_xs: PresentX ready (window %p)\n", static_cast(state->window)); + return state; +} + +std::uint64_t queue_present_x(ChildProxy* self, std::uint64_t plane_count, std::uint64_t planes, + std::uint64_t params) { + PresentState* state = self->device ? present_state_for(self->device) : nullptr; + if (!state || !state->xbox) return hr_to_u64(S_OK); // a title still runs with nothing presented + return hr_to_u64(state->xbox->PresentX( + static_cast(plane_count), + reinterpret_cast(planes), + reinterpret_cast(params))); +} + +#else // !LEONIDA_VULKAN_DEVICE: this translation unit stands alone (mock and COM tests) + +void destroy_present_state(void*) {} + +std::uint64_t queue_present_x(ChildProxy*, std::uint64_t, std::uint64_t, std::uint64_t) { + return hr_to_u64(S_OK); +} + +#endif + template std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3, std::uint64_t a4, std::uint64_t a5, std::uint64_t a6, std::uint64_t a7, @@ -737,6 +840,52 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ reinterpret_cast(a2))); } else { note_child_slot(Slot); + if constexpr (K == ChildKind::queue) { + // Layout detection (the comment at the PresentX section): a fence reaching slot 16 or 17 is the newer + // layout's Signal/Wait; anything else there is the older layout's frequency/calibration getter. A + // fence at slot 14 or 15 is the older Signal/Wait; a non-fence argument there is one of the newer + // layout's two inserted console methods. + if (Slot == 16 || Slot == 17) { + if (IUnknown* fence = proxy_real_object(reinterpret_cast(a1))) { + self->queue_layout = ChildProxy::kQueueLayoutNewer; + using Fn = HRESULT(WINAPI*)(IUnknown*, IUnknown*, UINT64); + return hr_to_u64(reinterpret_cast(self->real_vtable[kQueueSignal + (Slot - 16)])( + self->real, fence, a2)); + } + if (self->queue_layout == ChildProxy::kQueueLayoutUnknown) + self->queue_layout = ChildProxy::kQueueLayoutOlder; + } else if ((Slot == 14 || Slot == 15) && !proxy_real_object(reinterpret_cast(a1)) && + self->queue_layout == ChildProxy::kQueueLayoutNewer) { + return hr_to_u64(S_OK); // the newer layout's two inserted console methods, unmeasured + } + const bool newer = self->queue_layout == ChildProxy::kQueueLayoutNewer; + if (newer && Slot >= 18 && Slot <= 20) { + // Newer layout: frequency 18, calibration 19, GetDesc 20 are the PC slots 16-18 shifted. + d3d12_trace::record(child_trace_name(Slot)); + using ForwardFn = 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); + auto fn = reinterpret_cast(self->real_vtable[Slot - 2]); + return fn(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); + } + if (Slot == (newer ? 25u : 23u)) { // SignalX: console Signal; the extra X word is dropped + if (IUnknown* fence = proxy_real_object(reinterpret_cast(a1))) { + static std::atomic logged{false}; + if (!logged.exchange(true, std::memory_order_relaxed)) + std::fprintf(stderr, "[rt] d3d12_xs: SignalX (queue slot %u) forwarded as Signal\n", Slot); + using Fn = HRESULT(WINAPI*)(IUnknown*, IUnknown*, UINT64); + return hr_to_u64(reinterpret_cast(self->real_vtable[kQueueSignal])(self->real, fence, a2)); + } + return hr_to_u64(S_OK); + } + if (Slot == (newer ? 32u : 30u)) return queue_present_x(self, a1, a2, a3); // PresentX + if (Slot >= self->slot_count || (newer && (Slot == 14 || Slot == 15))) { + d3d12_trace::record(child_trace_name(Slot), "console-only"); + return hr_to_u64(S_OK); // unmodelled console queue methods: frame pacing flows through them + } + } if (Slot >= self->slot_count) { bool expected = false; if (ChildStats::beyond_warned[Slot].compare_exchange_strong(expected, true, diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index f115b6655..d438f7668 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -648,7 +648,7 @@ int check_allocators() { return 0; } -// ---- 7. The refused tail and the guard region ---- +// ---- 7. The console tail and the guard region ---- int check_tail(bool want_guarded) { Mock* mock = nullptr; @@ -662,8 +662,10 @@ int check_tail(bool want_guarded) { // Signal (14) still reaches the real queue, with the real fence. EXPECT(hr_of(raw(queue, 14, word(fence), 5)) == S_OK && queue_mock->calls[14] == 1 && queue_mock->args[14][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[14][1] == 5); + // Console-only queue methods (the tail past the PC chain, SignalX, PresentX) report S_OK instead of + // refusing: frame pacing flows through them. Nothing is forwarded — the real queue never sees them. for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) - EXPECT(hr_of(raw(queue, slot)) == E_NOTIMPL && queue_mock->calls[slot] == 0); + EXPECT(hr_of(raw(queue, slot)) == S_OK && queue_mock->calls[slot] == 0); for (unsigned slot = 12; slot < xs::fence_proxy_table_size(); ++slot) EXPECT(hr_of(raw(fence, slot)) == E_NOTIMPL); EXPECT(xs::queue_table_overruns() == 0 && xs::fence_table_overruns() == 0); EXPECT(release(queue) == 0 && release(fence) == 0 && release(device) == 0 && mock->refs == 0); @@ -743,8 +745,9 @@ int run_fault_mode(const char* mode) { g_armed = false; if (failed) return 1; EXPECT(g_injected.load() > 0); // the proxy's own call was the one refused - std::printf("%s: %s refused %d time(s): the tables %s guarded, the proxies forward and refuse their tail as " - "before\n", m.flag, m.function, g_injected.load(), m.guarded ? "stay" : "are not"); + std::printf("%s: %s refused %d time(s): the tables %s guarded, the proxies forward and answer their " + "console tail as before\n", m.flag, m.function, g_injected.load(), + m.guarded ? "stay" : "are not"); return 0; } return 2;