diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3ae29163e5..dff747f310 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3506,6 +3506,7 @@ to be recovered from call sites (or the NDA GDK headers, if a developer has acce | DCC / texture compatibility | Public release note for `D3D12XBOX_RESOURCE_FLAG_FORCE_TEXTURE_COMPATIBILITY` | Let Vulkan choose compression; fail if the game aliases/reads raw compressed data. | | PresentX / present planes / frame events | Public GDK samples (MIT/zlib), §4.5 | Likely the only present path of a GDKX title. Presenter + vblank-timed frame events; 1-2 planes composed into the Vulkan swapchain (placement, filter, alpha overlay, HDR10 conversions). Call shapes known; slots/layouts **Open**. | | Device slots 48/73/78/79/80 | Public HLE `Freminet`/`ScarlettX` (third-party, not Microsoft documentation) | `d3d12_xs_device.cpp` routes `CreateCommandQueueX` (48), `CreateCommandListX` (73), `SetFrameIntervalX` (78), `ScheduleFrameEventX` (79) and `WaitFrameEventX` (80) before it indexes the PC vtable. Queue and list creation use the existing create paths. The frame-event calls are a per-device token counter: `WaitFrameEventX` returns at once with a fresh non-zero token, because there is no vblank source before a presenter exists. The five indices can be moved with `LEONIDA_XS_QUEUE_X_SLOT`, `LEONIDA_XS_LIST_X_SLOT`, `LEONIDA_XS_FRAME_INTERVAL_SLOT`, `LEONIDA_XS_SCHEDULE_EVENT_SLOT` and `LEONIDA_XS_WAIT_EVENT_SLOT` (3..127; a value that names a slot the proxy already handles keeps that slot's handler); a trace that disagrees wins. | +| Device slot 81, queue `PresentX`/`SuspendX`/`ResumeX` | XCL (GPL-2.0-only, third-party): `GetFrameStatisticsX` at device slot 81, `PresentX` at queue slot 30 (older runtime layout) or 32 (newer); `SuspendX`/`ResumeX` slots unmeasured | `set_presentation_handler` forwards 78-81 and the queue routes to a presenter (`attach_xbox_presentation`; `gta6rt` creates no `XboxPresentation` yet). Without one, slot 81 answers `S_FALSE` with a zero count (no statistics yet) and the queue slots stay refused. Moved with `LEONIDA_XS_FRAME_STATISTICS_SLOT` (3..127) and `LEONIDA_XS_PRESENT_X_SLOT` (default 30); `SuspendX` and `ResumeX` are routed only when `LEONIDA_XS_SUSPEND_X_SLOT` / `LEONIDA_XS_RESUME_X_SLOT` are set (queue slots 19..127; two routes on one slot are refused with a log). | | VRS | Public `D3D12_SHADING_RATE` / `D3D12_AXIS_SHADING_RATE` | `VK_KHR_fragment_shading_rate`; `RSSetShadingRate`/image-based rate map to `vkCmdSetFragmentShadingRateKHR`. | | Mesh shader tier | Public `D3D12_MESH_SHADER_TIER` | `VK_EXT_mesh_shader`; the 9,282 translated mesh shaders make this a hard requirement for any content that uses them. | | Wave MMA | Public `D3D12_WAVE_MMA_TIER` | Not used: no shader sets `WAVE_MMA` in SFI0 and none contains `v_mfma`/`v_wmma` (all 26,503, `tools/feature_scan.py`). Answer the tier query truthfully; no cooperative-matrix mapping is needed. | diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 7f7040fdd9..327d5eaa57 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -76,8 +76,25 @@ struct DeviceProxy { std::atomic frame_latency{1}; std::atomic frame_event_scheduled{false}; std::atomic frame_next_token{1}; + // Optional presentation wiring attached via set_presentation_handler (guarded by presentation_lock). + SRWLOCK presentation_lock = SRWLOCK_INIT; + PresentationHandler presentation{}; }; +// Runs f(handler) with the presentation lock held shared, so a detach cannot destroy the presentation mid-call. f +// returns false, without calling the presentation, when it has no function for the slot; the result says whether +// f handled the call. +// ponytail: a presenter callback must not call set_presentation_handler (exclusive inside shared deadlocks), and a +// detach waits for a WaitFrameEventX in flight up to its timeout. +template +bool call_presentation(DeviceProxy* self, F&& f) { + if (!self) return false; + AcquireSRWLockShared(&self->presentation_lock); + const bool handled = f(static_cast(self->presentation)); + ReleaseSRWLockShared(&self->presentation_lock); + return handled; +} + constexpr unsigned kHeapSlots = 11; // IUnknown + ID3D12Object + DeviceChild + Pageable + GetDesc + 2 handles struct HeapProxy { @@ -181,24 +198,27 @@ struct XboxDeviceSlotRoutes { unsigned frame_interval = 78; // SetFrameIntervalX(output, interval_us, latency, flags) unsigned schedule_event = 79; // ScheduleFrameEventX(event, offset_us, reserved, flags) unsigned wait_event = 80; // WaitFrameEventX(event, timeout_ms, reserved, flags, &token) + unsigned frame_statistics = 81; // GetFrameStatisticsX(token, type_mask, count, out) unsigned queue_x = 48; // CreateCommandQueueX(desc, riid, out) unsigned list_x = 73; // CreateCommandListX(desc, allocator, initial_state, riid, out) }; -unsigned env_device_slot(const char* name, unsigned fallback) { +// Reads a slot number in lo..hi from `name`: an unset variable gives `fallback`; a bad value is logged (`what` names +// the kind of slot) and ignored. +unsigned env_slot(const char* name, unsigned fallback, unsigned lo, unsigned hi, const char* what) { char value[32] = {}; const DWORD length = GetEnvironmentVariableA(name, value, sizeof(value)); if (length == 0) return fallback; if (length >= sizeof(value)) { - std::fprintf(stderr, "[rt] d3d12_xs: %s is too long for a device slot; the default is kept\n", name); + std::fprintf(stderr, "[rt] d3d12_xs: %s is too long for a %s; the default is kept\n", name, what); return fallback; } errno = 0; char* end = nullptr; const unsigned long slot = std::strtoul(value, &end, 0); - if (errno != 0 || end == value || *end != '\0' || slot < 3 || slot > 127) { - std::fprintf(stderr, "[rt] d3d12_xs: %s=%s is not a device slot in 3..127; the default is kept\n", name, - value); + if (errno != 0 || end == value || *end != '\0' || slot < lo || slot > hi) { + std::fprintf(stderr, "[rt] d3d12_xs: %s=%s is not a %s in %u..%u; the default is kept\n", name, value, what, + lo, hi); return fallback; } return static_cast(slot); @@ -207,23 +227,29 @@ unsigned env_device_slot(const char* name, unsigned fallback) { const XboxDeviceSlotRoutes& xbox_device_slot_routes() { static const XboxDeviceSlotRoutes routes = [] { XboxDeviceSlotRoutes r; - const unsigned frame_interval = env_device_slot("LEONIDA_XS_FRAME_INTERVAL_SLOT", r.frame_interval); - const unsigned schedule_event = env_device_slot("LEONIDA_XS_SCHEDULE_EVENT_SLOT", r.schedule_event); - const unsigned wait_event = env_device_slot("LEONIDA_XS_WAIT_EVENT_SLOT", r.wait_event); - const unsigned queue_x = env_device_slot("LEONIDA_XS_QUEUE_X_SLOT", r.queue_x); - const unsigned list_x = env_device_slot("LEONIDA_XS_LIST_X_SLOT", r.list_x); + const unsigned frame_interval = env_slot("LEONIDA_XS_FRAME_INTERVAL_SLOT", r.frame_interval, 3, 127, + "device slot"); + const unsigned schedule_event = env_slot("LEONIDA_XS_SCHEDULE_EVENT_SLOT", r.schedule_event, 3, 127, + "device slot"); + const unsigned wait_event = env_slot("LEONIDA_XS_WAIT_EVENT_SLOT", r.wait_event, 3, 127, "device slot"); + const unsigned frame_statistics = env_slot("LEONIDA_XS_FRAME_STATISTICS_SLOT", r.frame_statistics, 3, 127, + "device slot"); + const unsigned queue_x = env_slot("LEONIDA_XS_QUEUE_X_SLOT", r.queue_x, 3, 127, "device slot"); + const unsigned list_x = env_slot("LEONIDA_XS_LIST_X_SLOT", r.list_x, 3, 127, "device slot"); const bool moved = frame_interval != r.frame_interval || schedule_event != r.schedule_event || - wait_event != r.wait_event || queue_x != r.queue_x || list_x != r.list_x; + wait_event != r.wait_event || frame_statistics != r.frame_statistics || + queue_x != r.queue_x || list_x != r.list_x; r.frame_interval = frame_interval; r.schedule_event = schedule_event; r.wait_event = wait_event; + r.frame_statistics = frame_statistics; r.queue_x = queue_x; r.list_x = list_x; if (moved) std::fprintf(stderr, "[rt] d3d12_xs: Xbox device slots: SetFrameIntervalX=%u ScheduleFrameEventX=%u " - "WaitFrameEventX=%u CreateCommandQueueX=%u CreateCommandListX=%u\n", - r.frame_interval, r.schedule_event, r.wait_event, r.queue_x, r.list_x); + "WaitFrameEventX=%u GetFrameStatisticsX=%u CreateCommandQueueX=%u CreateCommandListX=%u\n", + r.frame_interval, r.schedule_event, r.wait_event, r.frame_statistics, r.queue_x, r.list_x); return r; }(); return routes; @@ -1194,6 +1220,40 @@ 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 is at queue slot 30 in an older runtime layout and 32 in a newer one (public corroboration, XCL; see +// docs/d3d12_vulkan.md). 30 is the default and LEONIDA_XS_PRESENT_X_SLOT sets the other. SuspendX and ResumeX have no +// default: 0 means not routed, so they answer only when their variable is set. +struct XboxQueueSlotRoutes { + unsigned present_x = 30; // PresentX(plane_count, planes, params) + unsigned suspend_x = 0; // SuspendX(flags) + unsigned resume_x = 0; // ResumeX() +}; + +const XboxQueueSlotRoutes& xbox_queue_slot_routes() { + static const XboxQueueSlotRoutes routes = [] { + XboxQueueSlotRoutes r; + r.present_x = env_slot("LEONIDA_XS_PRESENT_X_SLOT", r.present_x, kQueuePublicSlots, kQueueSlots - 1, + "queue slot"); + r.suspend_x = env_slot("LEONIDA_XS_SUSPEND_X_SLOT", r.suspend_x, kQueuePublicSlots, kQueueSlots - 1, + "queue slot"); + r.resume_x = env_slot("LEONIDA_XS_RESUME_X_SLOT", r.resume_x, kQueuePublicSlots, kQueueSlots - 1, + "queue slot"); + // Two routes on one slot cannot both answer: PresentX keeps it, then SuspendX; the later route is unrouted. + if (r.suspend_x && r.suspend_x == r.present_x) { + std::fprintf(stderr, "[rt] d3d12_xs: SuspendX slot %u is PresentX's slot; SuspendX is not routed\n", + r.suspend_x); + r.suspend_x = 0; + } + if (r.resume_x && (r.resume_x == r.present_x || r.resume_x == r.suspend_x)) { + std::fprintf(stderr, "[rt] d3d12_xs: ResumeX slot %u is already routed; ResumeX is not routed\n", + r.resume_x); + r.resume_x = 0; + } + return r; + }(); + return routes; +} + 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, @@ -1214,6 +1274,27 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ } else { note_child_slot(Slot); if (Slot >= self->slot_count) { + if constexpr (K == ChildKind::queue) { + const XboxQueueSlotRoutes& qroutes = xbox_queue_slot_routes(); + HRESULT hr = S_OK; + if (call_presentation(self->device, [&](const PresentationHandler& p) { + if (Slot == qroutes.present_x && p.present_x) { + hr = p.present_x(p.context, static_cast(a1), reinterpret_cast(a2), + reinterpret_cast(a3)); + return true; + } + if (Slot == qroutes.suspend_x && p.suspend_x) { + hr = p.suspend_x(p.context, static_cast(a1)); + return true; + } + if (Slot == qroutes.resume_x && p.resume_x) { + hr = p.resume_x(p.context); + return true; + } + return false; + })) + return hr_to_u64(hr); + } bool expected = false; if (ChildStats::beyond_warned[Slot].compare_exchange_strong(expected, true, std::memory_order_relaxed)) { @@ -1380,8 +1461,14 @@ HRESULT proxy_create_command_queue(DeviceProxy* self, const D3D12_COMMAND_QUEUE_ // 60 Hz), and latency is the allowed frames in flight. S_OK even for 0: a boot must not abort here, and nothing paces. HRESULT proxy_set_frame_interval_x(DeviceProxy* self, std::uint64_t output, std::uint64_t interval_us, std::uint64_t latency, std::uint64_t flags) { - (void)output; - (void)flags; + HRESULT hr = S_OK; + if (call_presentation(self, [&](const PresentationHandler& p) { + if (!p.set_frame_interval) return false; + hr = p.set_frame_interval(p.context, reinterpret_cast(output), static_cast(interval_us), + static_cast(latency), static_cast(flags)); + return true; + })) + return hr; self->frame_interval_us.store(static_cast(interval_us), std::memory_order_relaxed); self->frame_latency.store(static_cast(latency ? latency : 1), std::memory_order_relaxed); return S_OK; @@ -1390,22 +1477,31 @@ HRESULT proxy_set_frame_interval_x(DeviceProxy* self, std::uint64_t output, std: // Xbox device slot 79 (public HLE): ScheduleFrameEventX(event, offset_us, pEvent, flags); our pacer has only the origin. HRESULT proxy_schedule_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t offset_us, std::uint64_t flags) { - (void)event; - (void)offset_us; - (void)flags; + HRESULT hr = S_OK; + if (call_presentation(self, [&](const PresentationHandler& p) { + if (!p.schedule_frame_event) return false; + hr = p.schedule_frame_event(p.context, static_cast(event), static_cast(offset_us), + static_cast(flags)); + return true; + })) + return hr; self->frame_event_scheduled.store(true, std::memory_order_relaxed); return S_OK; } -// Xbox device slot 80 (public HLE): WaitFrameEventX(event, timeout_ms, pEvent, flags, &token). It returns at once with -// a fresh non-zero token (the first-frame choice): there is no vblank source behind the device before a presenter, and -// a zero wait cannot stall the title. A null token pointer is E_POINTER, as a _Out_ parameter. +// Xbox device slot 80 (public HLE): WaitFrameEventX(event, timeout_ms, pEvent, flags, &token). When a presenter is +// attached, forwards directly to it; otherwise returns at once with a fresh non-zero token so boot tests cannot stall. HRESULT proxy_wait_frame_event_x(DeviceProxy* self, std::uint64_t event, std::uint64_t timeout_ms, std::uint64_t flags, std::uint64_t token_word) { - (void)event; - (void)timeout_ms; - (void)flags; auto* token = reinterpret_cast(token_word); + HRESULT hr = S_OK; + if (call_presentation(self, [&](const PresentationHandler& p) { + if (!p.wait_frame_event) return false; + hr = p.wait_frame_event(p.context, static_cast(event), static_cast(timeout_ms), + static_cast(flags), token); + return true; + })) + return hr; if (!token) return E_POINTER; std::uint64_t value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed); if (value == 0) value = self->frame_next_token.fetch_add(1, std::memory_order_relaxed); // skip the null token @@ -2038,6 +2134,22 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 log_xbox_route_once(Slot, "WaitFrameEventX", a1, a2, a3, a4, a5); return hr_to_u64(proxy_wait_frame_event_x(self, a1, a2, a4, a5)); } + if (Slot == routes.frame_statistics) { + HRESULT hr = S_OK; + if (call_presentation(self, [&](const PresentationHandler& p) { + if (!p.get_frame_statistics) return false; + log_xbox_route_once(Slot, "GetFrameStatisticsX", a1, a2, a3, a4, a5); + hr = p.get_frame_statistics(p.context, a1, static_cast(a2), reinterpret_cast(a3), + reinterpret_cast(a4)); + return true; + })) + return hr_to_u64(hr); + // No presenter: no statistics yet (S_FALSE with a zero count, the "not available yet" answer in + // docs/d3d12_vulkan.md), and never the PC slot 81 (ID3D12Device13::OpenExistingHeapFromAddress1). + log_xbox_route_once(Slot, "GetFrameStatisticsX", a1, a2, a3, a4, a5); + if (auto* count = reinterpret_cast(a3)) *count = 0; + return hr_to_u64(S_FALSE); + } if (Slot == routes.queue_x) { log_xbox_route_once(Slot, "CreateCommandQueueX", a1, a2, a3, a4, a5); return hr_to_u64(proxy_create_command_queue_xbox(self, reinterpret_cast(a1), @@ -2246,6 +2358,15 @@ void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) { g_console_descriptor_heap.store(handler, std::memory_order_release); } +bool set_presentation_handler(void* device, const PresentationHandler* handler) { + auto* proxy = static_cast(device_proxy_primary(device)); + if (!proxy) return false; + AcquireSRWLockExclusive(&proxy->presentation_lock); + proxy->presentation = handler ? *handler : PresentationHandler{}; + ReleaseSRWLockExclusive(&proxy->presentation_lock); + return true; +} + ID3D12Device* device_proxy_real_device(const void* device) { if (!is_device_proxy(device)) return nullptr; return static_cast(device)->real; @@ -2258,7 +2379,7 @@ bool device_slot_overridden(unsigned slot) { return true; const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes(); return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event || - slot == routes.queue_x || slot == routes.list_x; + slot == routes.frame_statistics || slot == routes.queue_x || slot == routes.list_x; } bool is_queue_proxy(const void* queue) { return is_child_proxy(queue); } diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 7ee8809397..b7f020bebe 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -132,6 +132,24 @@ using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12 REFIID riid, void** out); void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler); +// Presentation wiring: connects device frame-event slots (78/79/80/81: SetFrameIntervalX, ScheduleFrameEventX, +// WaitFrameEventX, GetFrameStatisticsX; 81 is LEONIDA_XS_FRAME_STATISTICS_SLOT) and queue presentation slots (PresentX +// at 30, or LEONIDA_XS_PRESENT_X_SLOT; SuspendX and ResumeX are routed only when LEONIDA_XS_SUSPEND_X_SLOT / +// LEONIDA_XS_RESUME_X_SLOT are set) to an external presenter such as rt::d3d12::XboxPresentation without coupling +// d3d12_xs_device to Vulkan headers. Passing nullptr detaches the handler and restores the default mock/fallback +// behavior. +struct PresentationHandler { + void* context = nullptr; + HRESULT (*set_frame_interval)(void* context, IUnknown* output, UINT interval_us, UINT latency, UINT flags) = nullptr; + HRESULT (*schedule_frame_event)(void* context, UINT event, UINT offset_us, UINT flags) = nullptr; + HRESULT (*wait_frame_event)(void* context, UINT event, DWORD timeout_ms, UINT flags, std::uint64_t* token) = nullptr; + HRESULT (*get_frame_statistics)(void* context, std::uint64_t token, UINT type_mask, UINT* count, void* out) = nullptr; + HRESULT (*present_x)(void* context, UINT plane_count, const void* planes, const void* params) = nullptr; + HRESULT (*suspend_x)(void* context, UINT flags) = nullptr; + HRESULT (*resume_x)(void* context) = nullptr; +}; +bool set_presentation_handler(void* device, const PresentationHandler* handler); + // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue // proxies), 9 (allocator proxies), 14, 26 (the console's struct-return form), // 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and diff --git a/runtime/shims/xbox_present.cpp b/runtime/shims/xbox_present.cpp index dd5f810cf7..024b821701 100644 --- a/runtime/shims/xbox_present.cpp +++ b/runtime/shims/xbox_present.cpp @@ -1,6 +1,7 @@ #include "xbox_present.h" #include "d3d12_trace.h" +#include "d3d12_xs_device.h" #include "../src/vulkan_present.h" @@ -314,4 +315,39 @@ HRESULT XboxPresentation::ResumeX() { return S_OK; } +bool attach_xbox_presentation(void* device_proxy, XboxPresentation* presentation) { + if (!presentation) return rt::d3d12_xs::set_presentation_handler(device_proxy, nullptr); + rt::d3d12_xs::PresentationHandler handler{}; + handler.context = presentation; + handler.set_frame_interval = [](void* ctx, IUnknown* output, UINT interval_us, UINT latency, UINT flags) { + return static_cast(ctx)->SetFrameIntervalX(output, interval_us, latency, flags); + }; + handler.schedule_frame_event = [](void* ctx, UINT event, UINT offset_us, UINT flags) { + return static_cast(ctx)->ScheduleFrameEventX(event, offset_us, flags); + }; + handler.wait_frame_event = [](void* ctx, UINT event, DWORD timeout_ms, UINT flags, std::uint64_t* token) { + return static_cast(ctx)->WaitFrameEventX(event, timeout_ms, flags, token); + }; + handler.get_frame_statistics = [](void* ctx, std::uint64_t token, UINT type_mask, UINT* count, void* out) { + return static_cast(ctx)->GetFrameStatisticsX( + token, type_mask, count, static_cast(out)); + }; + handler.present_x = [](void* ctx, UINT plane_count, const void* planes, const void* params) { + return static_cast(ctx)->PresentX( + plane_count, static_cast(planes), + static_cast(params)); + }; + handler.suspend_x = [](void* ctx, UINT flags) { + return static_cast(ctx)->SuspendX(flags); + }; + handler.resume_x = [](void* ctx) { + return static_cast(ctx)->ResumeX(); + }; + return rt::d3d12_xs::set_presentation_handler(device_proxy, &handler); +} + +bool detach_xbox_presentation(void* device_proxy) { + return rt::d3d12_xs::set_presentation_handler(device_proxy, nullptr); +} + } // namespace rt::d3d12 diff --git a/runtime/shims/xbox_present.h b/runtime/shims/xbox_present.h index 60a317beec..ea23f7b86d 100644 --- a/runtime/shims/xbox_present.h +++ b/runtime/shims/xbox_present.h @@ -199,5 +199,15 @@ class XboxPresentation { }; inline constexpr UINT xbox_frame_event_origin = leonida::frame_event_origin; +inline constexpr UINT xbox_frame_event_vblank = leonida::frame_event_vblank; +inline constexpr UINT xbox_frame_event_flip = leonida::frame_event_flip; +inline constexpr UINT xbox_frame_wait_token_on_timeout = leonida::frame_wait_flag_token_on_timeout; + +// Seam for the presenter, not yet called by the runtime: gta6rt creates no XboxPresentation, and only +// d3d12_boot_movie_test and xbox_present_test construct one. Wires it into a d3d12_xs device proxy's frame-event +// slots (78–81) and queue PresentX (slot 30, or LEONIDA_XS_PRESENT_X_SLOT); SuspendX and ResumeX are routed only +// when LEONIDA_XS_SUSPEND_X_SLOT / LEONIDA_XS_RESUME_X_SLOT are set. Passing nullptr detaches the presentation. +bool attach_xbox_presentation(void* device_proxy, XboxPresentation* presentation); +bool detach_xbox_presentation(void* device_proxy); } // namespace rt::d3d12 diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index fa9c20df1d..38f7860c1a 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h @@ -102,8 +102,14 @@ class ManualFrameClock final : public FrameClock { std::function wake_; }; -// Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. +// Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. Frame event +// kinds (0: origin, 1: vblank, 2: flip) and wait flags. inline constexpr std::uint32_t frame_event_origin = 0; +inline constexpr std::uint32_t frame_event_vblank = 1; +inline constexpr std::uint32_t frame_event_flip = 2; +inline constexpr std::uint32_t frame_event_count = 3; +// Issue a consecutive frame token even when WaitFrameEventX(origin, ...) times out (returns S_FALSE). +inline constexpr std::uint32_t frame_wait_flag_token_on_timeout = 2; // Statistics types (bit mask). Interval and render entries are never produced here. inline constexpr std::uint32_t stat_frame_interval = 0x01; inline constexpr std::uint32_t stat_frame_event = 0x02; @@ -177,38 +183,42 @@ class FramePacer { latency_ = std::max(latency, 1u); epoch_us_ = clock_.now_us(); interval_set_ = true; - offset_us_ = std::min(offset_us_, period_us - 1); - last_origin_ = 0; // origin 0 is the epoch itself, which has passed + for (std::uint32_t k = 0; k < frame_event_count; ++k) { + event_offset_us_[k] = std::min(event_offset_us_[k], period_us - 1); + last_event_tick_[k] = 0; // tick 0 is the epoch itself, which has passed + } cv_.notify_all(); return pacer_hr::ok; } - // ScheduleFrameEventX(event, offset, ..., flags). Only the origin event exists here. + // ScheduleFrameEventX(event, offset, ..., flags). Supports origin (0), vblank (1), and flip (2). std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) { (void)flags; - if (event != frame_event_origin) return pacer_hr::invalid_arg; + if (event >= frame_event_count) return pacer_hr::invalid_arg; std::lock_guard guard(mutex_); if (suspended_ || !interval_set_) return pacer_hr::invalid_state; if (offset_us >= period_us_) return pacer_hr::invalid_arg; - offset_us_ = offset_us; - armed_ = true; - // The first origin is the next one that has not come yet: origin n is due at epoch + n * period + offset. + event_offset_us_[event] = offset_us; + event_armed_[event] = true; + // The first event is the next one that has not come yet: tick n is due at epoch + n * period + offset. const std::uint64_t now = clock_.now_us(); - last_origin_ = now >= epoch_us_ + offset_us_ ? std::int64_t((now - epoch_us_ - offset_us_) / period_us_) : 0; + last_event_tick_[event] = + now >= epoch_us_ + offset_us ? std::int64_t((now - epoch_us_ - offset_us) / period_us_) : 0; cv_.notify_all(); return pacer_hr::ok; } - // WaitFrameEventX(event, timeout, ..., flags, &token). Blocks until the next origin, and while `latency` frames - // are in flight. pacer_hr::timeout when `timeout_ms` passes first (infinite_timeout_ms: never); the token is left - // alone then. invalid_state when nothing is registered, the pacer is suspended, or one of those happens while waiting. + // WaitFrameEventX(event, timeout, ..., flags, &token). Origin waits block until the next origin tick and while + // `latency` frames are in flight, issuing a consecutive token on success (or on timeout with + // frame_wait_flag_token_on_timeout, returning S_FALSE). Vblank/flip waits pace independently per event kind and + // leave `*token` untouched. std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags, std::uint64_t* token) { - (void)flags; - if (event != frame_event_origin || !token) return pacer_hr::invalid_arg; + if (event >= frame_event_count) return pacer_hr::invalid_arg; + if (event == frame_event_origin && !token) return pacer_hr::invalid_arg; std::unique_lock lock(mutex_); ++waiters_; - const std::int32_t result = wait_locked(lock, timeout_ms, token); + const std::int32_t result = wait_locked(lock, event, timeout_ms, flags, token); if (--waiters_ == 0) cv_.notify_all(); return result; } @@ -302,7 +312,7 @@ class FramePacer { const std::uint64_t now = clock_.now_us(); retire_locked(now); last_presented_ = std::max(last_presented_, token); - if (!interval_set_ || !armed_ || uncapped_) { // not paced or uncapped: shows at once + if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once for (Record& r : records_) if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } return; @@ -330,7 +340,7 @@ class FramePacer { // the origin event not armed or the cap off it is not paced: it shows at once and is not counted. void note_untokened_present() { std::lock_guard guard(mutex_); - if (!interval_set_ || !armed_ || uncapped_) return; + if (!interval_set_ || !any_armed_locked() || uncapped_) return; const std::uint64_t now = clock_.now_us(); retire_locked(now); const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; @@ -344,7 +354,8 @@ class FramePacer { void suspend() { std::lock_guard guard(mutex_); suspended_ = true; - interval_set_ = armed_ = false; + interval_set_ = false; + for (bool& a : event_armed_) a = false; pending_.clear(); cv_.notify_all(); } @@ -352,7 +363,8 @@ class FramePacer { void resume() { std::lock_guard guard(mutex_); suspended_ = false; - interval_set_ = armed_ = false; + interval_set_ = false; + for (bool& a : event_armed_) a = false; cv_.notify_all(); } @@ -366,7 +378,7 @@ class FramePacer { // Frames that were flipped mid-scan because of ImmediateThresholdPercent. std::uint64_t immediate_presents() const { std::lock_guard guard(mutex_); return immediate_presents_; } std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; } - bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && armed_; } + bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && any_armed_locked(); } // The fps cap off (LEONIDA_PRESENT_UNCAP; see the top comment): waits never block and presents count as shown // at once. Registration and suspension keep meaning what they did; the flag survives suspend/resume. @@ -399,45 +411,67 @@ class FramePacer { } } - std::int32_t wait_locked(std::unique_lock& lock, std::uint32_t timeout_ms, std::uint64_t* token) { + bool any_armed_locked() const { + return event_armed_[0] || event_armed_[1] || event_armed_[2]; + } + + std::uint64_t issue_token_locked(std::uint64_t origin_us, std::uint64_t due_us) { + const std::uint64_t tok = ++issued_; + Record record; + record.token = tok; + record.origin_us = origin_us; + record.due_us = due_us; + records_.push_back(record); + if (records_.size() > 64) records_.pop_front(); + return tok; + } + + std::int32_t wait_locked(std::unique_lock& lock, std::uint32_t event, std::uint32_t timeout_ms, + std::uint32_t flags, std::uint64_t* token) { + const bool is_origin = (event == frame_event_origin); + const bool token_on_timeout = (flags & frame_wait_flag_token_on_timeout) != 0; const std::uint64_t start = clock_.now_us(); const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline : start + std::uint64_t(timeout_ms) * 1000; for (;;) { - if (shutdown_ || suspended_ || !interval_set_ || !armed_) return pacer_hr::invalid_state; + if (shutdown_ || suspended_ || !interval_set_) return pacer_hr::invalid_state; + if (is_origin ? !event_armed_[frame_event_origin] + : (!event_armed_[event] && !event_armed_[frame_event_origin])) { + return pacer_hr::invalid_state; + } const std::uint64_t now = clock_.now_us(); retire_locked(now); - if (uncapped_) { // no vblank grid and no latency throttle: a token at once, due now - *token = ++issued_; - Record record; - record.token = *token; - record.origin_us = now; - record.due_us = now; - records_.push_back(record); - if (records_.size() > 64) records_.pop_front(); + if (uncapped_) { // no vblank grid and no latency throttle + if (is_origin) *token = issue_token_locked(now, now); return pacer_hr::ok; } - const std::uint64_t first = epoch_us_ + offset_us_; // origin 0 - const std::int64_t next = last_origin_ + 1; + const std::uint32_t offset = event_offset_us_[event]; + const std::uint64_t first = epoch_us_ + offset; // tick 0 + const std::int64_t next = last_event_tick_[event] + 1; const std::uint64_t next_us = first + std::uint64_t(next) * period_us_; std::uint64_t wake = next_us; if (now >= next_us) { - if (pending_.size() < latency_) { + if (!is_origin || pending_.size() < latency_) { const std::int64_t newest = std::int64_t((now - first) / period_us_); - const std::int64_t origin = std::max(next, newest); - last_origin_ = origin; - *token = ++issued_; - Record record; - record.token = *token; - record.origin_us = first + std::uint64_t(origin) * period_us_; - record.due_us = record.origin_us - offset_us_ + period_us_; - records_.push_back(record); - if (records_.size() > 64) records_.pop_front(); + const std::int64_t tick = std::max(next, newest); + last_event_tick_[event] = tick; + if (is_origin) { + const std::uint64_t origin_us = first + std::uint64_t(tick) * period_us_; + const std::uint64_t due_us = origin_us - offset + period_us_; + *token = issue_token_locked(origin_us, due_us); + } return pacer_hr::ok; } wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown } - if (give_up != no_deadline && now >= give_up) return pacer_hr::timeout; + if (give_up != no_deadline && now >= give_up) { + if (is_origin && token_on_timeout) { + // Frame started early on timeout: issue a consecutive token without consuming the tick. + *token = issue_token_locked(now, next_us - offset + period_us_); + return pacer_hr::false_; + } + return token_on_timeout ? pacer_hr::false_ : pacer_hr::timeout; + } clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up)); } } @@ -450,13 +484,13 @@ class FramePacer { bool suspended_ = false; bool interval_set_ = false; - bool armed_ = false; + bool event_armed_[frame_event_count] = {false, false, false}; bool uncapped_ = false; std::uint32_t period_us_ = 0; std::uint32_t latency_ = 1; - std::uint32_t offset_us_ = 0; + std::uint32_t event_offset_us_[frame_event_count] = {0, 0, 0}; std::uint64_t epoch_us_ = 0; - std::int64_t last_origin_ = 0; // origin 0 is the epoch itself, which has passed + std::int64_t last_event_tick_[frame_event_count] = {0, 0, 0}; // tick 0 is the epoch itself, which has passed std::uint64_t issued_ = 0; std::uint64_t last_presented_ = 0; std::uint64_t immediate_presents_ = 0; diff --git a/runtime/test/frame_pacer_test.cpp b/runtime/test/frame_pacer_test.cpp index 8d089acdaf..b14148f87e 100644 --- a/runtime/test/frame_pacer_test.cpp +++ b/runtime/test/frame_pacer_test.cpp @@ -586,6 +586,47 @@ int main() { EXPECT(ms.count() >= 45 && ms.count() < 1000); } + // TOKEN_ON_TIMEOUT (flag 2) and multi-event pacing (origin poll + blocking flip/vblank wait). + { + ManualFrameClock clock; + FramePacer pacer(clock); + EXPECT(pacer.set_frame_interval(period, 3, 0) == pacer_hr::ok); + EXPECT(pacer.schedule_frame_event(frame_event_origin, 0, 0) == pacer_hr::ok); + EXPECT(pacer.schedule_frame_event(frame_event_vblank, 0, 0) == pacer_hr::ok); + EXPECT(pacer.schedule_frame_event(frame_event_flip, 0, 0) == pacer_hr::ok); + + // Origin poll with timeout 0 and TOKEN_ON_TIMEOUT issues a consecutive token and returns S_FALSE. + std::uint64_t tok1 = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, frame_wait_flag_token_on_timeout, &tok1) == + pacer_hr::false_); + EXPECT(tok1 == 1); + EXPECT(pacer.check_present_token(tok1) == pacer_hr::ok); + + // A blocking flip wait paces to the next tick and preserves the caller's token unchanged. + auto flip_wait = std::async(std::launch::async, [&] { + std::uint64_t preserved = tok1; + const std::int32_t hr = pacer.wait_frame_event(frame_event_flip, infinite_timeout_ms, 0, &preserved); + return Result{hr, preserved}; + }); + EXPECT(blocked(flip_wait)); + clock.advance_us(period); + const Result fr = flip_wait.get(); + EXPECT(fr.hr == pacer_hr::ok && fr.token == tok1); + + // Second flip wait blocks for the following tick; vblank wait on tick 1 succeeds immediately (independent). + EXPECT(pacer.wait_frame_event(frame_event_vblank, 0, 0, nullptr) == pacer_hr::ok); + auto flip_wait2 = std::async(std::launch::async, [&] { + return Result{pacer.wait_frame_event(frame_event_flip, infinite_timeout_ms, 0, nullptr), 0}; + }); + EXPECT(blocked(flip_wait2)); + clock.advance_us(period); + EXPECT(flip_wait2.get().hr == pacer_hr::ok); + + // Origin wait at tick 2 advances consecutively after the early-started token 1. + std::uint64_t tok2 = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &tok2) == pacer_hr::ok && tok2 == 2); + } + std::printf("frame_pacer_test: all cases pass\n"); return 0; } diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 224030247c..ca301cd849 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -981,17 +981,27 @@ int check_xbox_slots() { // ---- 8. Environment overrides for the Xbox device slots (one process each, read once) ---- +struct EnvPresenter { + int stats = 0, present = 0, suspend = 0, resume = 0; +}; + int run_env_slots(bool valid) { const char* frame_interval = valid ? "96" : "2"; const char* schedule_event = valid ? "97" : "2"; const char* wait_event = valid ? "98" : "2"; const char* queue_x = valid ? "99" : "2"; const char* list_x = valid ? "100" : "2"; + const char* frame_statistics = valid ? "101" : "2"; + const char* suspend_x = valid ? "33" : "2"; // a queue slot beyond the PC table, as SuspendX would be + const char* resume_x = valid ? "30" : "2"; // equal to PresentX's default: ResumeX is unrouted (logged) EXPECT(SetEnvironmentVariableA("LEONIDA_XS_FRAME_INTERVAL_SLOT", frame_interval) != FALSE); EXPECT(SetEnvironmentVariableA("LEONIDA_XS_SCHEDULE_EVENT_SLOT", schedule_event) != FALSE); EXPECT(SetEnvironmentVariableA("LEONIDA_XS_WAIT_EVENT_SLOT", wait_event) != FALSE); + EXPECT(SetEnvironmentVariableA("LEONIDA_XS_FRAME_STATISTICS_SLOT", frame_statistics) != FALSE); EXPECT(SetEnvironmentVariableA("LEONIDA_XS_QUEUE_X_SLOT", queue_x) != FALSE); EXPECT(SetEnvironmentVariableA("LEONIDA_XS_LIST_X_SLOT", list_x) != FALSE); + EXPECT(SetEnvironmentVariableA("LEONIDA_XS_SUSPEND_X_SLOT", suspend_x) != FALSE); + EXPECT(SetEnvironmentVariableA("LEONIDA_XS_RESUME_X_SLOT", resume_x) != FALSE); Mock* mock = nullptr; void* device = make_device(mock, 11, true); @@ -1018,6 +1028,36 @@ int run_env_slots(bool valid) { EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK && mock->calls[78] == 1); EXPECT(release(list) == 0); EXPECT(release(allocator) == 0); + + // The presenter answers frame statistics at 101 only (81 is not routed, so the mock refuses it); PresentX + // stays at 30, SuspendX answers at 33, and ResumeX, asked for 30, is unrouted, so 32 is refused. + EnvPresenter env{}; + xs::PresentationHandler h{}; + h.context = &env; + h.get_frame_statistics = [](void* ctx, std::uint64_t, UINT, UINT*, void*) -> HRESULT { + ++static_cast(ctx)->stats; + return S_FALSE; + }; + h.present_x = [](void* ctx, UINT, const void*, const void*) -> HRESULT { + ++static_cast(ctx)->present; + return S_OK; + }; + h.suspend_x = [](void* ctx, UINT) -> HRESULT { + ++static_cast(ctx)->suspend; + return S_OK; + }; + h.resume_x = [](void* ctx) -> HRESULT { + ++static_cast(ctx)->resume; + return S_OK; + }; + EXPECT(xs::set_presentation_handler(device, &h)); + UINT count = 0; + EXPECT(hr_of(raw(device, 101, 77, 0x1a, word(&count), 0)) == S_FALSE && env.stats == 1); + EXPECT(hr_of(raw(device, 81, 77, 0x1a, word(&count), 0)) == E_NOTIMPL && env.stats == 1); + EXPECT(hr_of(raw(queue, 30, 1, 0, 0)) == S_OK && env.present == 1); + EXPECT(hr_of(raw(queue, 33)) == S_OK && env.suspend == 1); + EXPECT(hr_of(raw(queue, 32)) == E_NOTIMPL && env.resume == 0); + EXPECT(xs::set_presentation_handler(device, nullptr)); EXPECT(release(queue) == 0); } else { // Values outside 3..127 are ignored: the five defaults still answer, and a PC slot that is not a default is @@ -1025,6 +1065,7 @@ int run_env_slots(bool valid) { EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK && mock->calls[78] == 0); EXPECT(hr_of(raw(device, 79, 0, 0, 0, 0)) == S_OK && mock->calls[79] == 0); EXPECT(hr_of(raw(device, 80, 0, 0, 0, 0, word(&token))) == S_OK && token && mock->calls[80] == 0); + EXPECT(hr_of(raw(device, 81, 0, 0, 0, 0)) == S_FALSE); // the default GetFrameStatisticsX slot, unattached EXPECT(hr_of(raw(device, 71)) == S_OK && mock->calls[71] == 1); // not an Xbox slot: forwarded } EXPECT(release(device) == 0 && mock->refs == 0); @@ -1534,6 +1575,99 @@ int check_console_command_list() { return 0; } +// ---- 11. Presentation handler wiring (device slots 78–81 + queue PresentX/SuspendX/ResumeX) ---- + +struct MockPresenter { + int interval_calls = 0; + UINT last_interval = 0; + int schedule_calls = 0; + UINT last_event = 0; + int wait_calls = 0; + UINT last_flags = 0; + int stats_calls = 0; + int present_calls = 0; + UINT last_planes = 0; + int suspend_calls = 0; + int resume_calls = 0; +}; + +int check_presentation_wiring() { + Mock* mock = nullptr; + void* device = make_device(mock, 11, true); + EXPECT(device != nullptr); + void* queue = make_queue(device); + EXPECT(queue != nullptr); + + MockPresenter p{}; + xs::PresentationHandler h{}; + h.context = &p; + h.set_frame_interval = [](void* ctx, IUnknown*, UINT us, UINT, UINT) -> HRESULT { + auto* m = static_cast(ctx); + ++m->interval_calls; + m->last_interval = us; + return S_OK; + }; + h.schedule_frame_event = [](void* ctx, UINT ev, UINT, UINT) -> HRESULT { + auto* m = static_cast(ctx); + ++m->schedule_calls; + m->last_event = ev; + return S_OK; + }; + h.wait_frame_event = [](void* ctx, UINT, DWORD, UINT flags, std::uint64_t* tok) -> HRESULT { + auto* m = static_cast(ctx); + ++m->wait_calls; + m->last_flags = flags; + if (tok) *tok = 77; + return S_FALSE; + }; + h.get_frame_statistics = [](void* ctx, std::uint64_t, UINT, UINT* count, void*) -> HRESULT { + auto* m = static_cast(ctx); + ++m->stats_calls; + if (count) *count = 0; + return S_FALSE; + }; + h.present_x = [](void* ctx, UINT plane_count, const void*, const void*) -> HRESULT { + auto* m = static_cast(ctx); + ++m->present_calls; + m->last_planes = plane_count; + return S_OK; + }; + h.suspend_x = [](void* ctx, UINT) -> HRESULT { + ++static_cast(ctx)->suspend_calls; + return S_OK; + }; + h.resume_x = [](void* ctx) -> HRESULT { + ++static_cast(ctx)->resume_calls; + return S_OK; + }; + + EXPECT(xs::set_presentation_handler(device, &h)); + EXPECT(hr_of(raw(device, 78, 0, 33333, 2, 0)) == S_OK && p.interval_calls == 1 && p.last_interval == 33333); + EXPECT(hr_of(raw(device, 79, 2, 0, 0, 0)) == S_OK && p.schedule_calls == 1 && p.last_event == 2); + std::uint64_t tok = 0; + EXPECT(hr_of(raw(device, 80, 0, 0, 0, 2, word(&tok))) == S_FALSE && tok == 77 && p.wait_calls == 1 && + p.last_flags == 2); + UINT count = 3; + EXPECT(hr_of(raw(device, 81, 77, 0x1a, word(&count), 0)) == S_FALSE && count == 0 && p.stats_calls == 1); + + // Queue slot 30 routes to PresentX while attached. SuspendX and ResumeX have no default route, so 31/32 are refused + // and never reach the presenter (the env mode, run_env_slots, routes them). + EXPECT(hr_of(raw(queue, 30, 1, 0x1000, 0x2000)) == S_OK && p.present_calls == 1 && p.last_planes == 1); + EXPECT(hr_of(raw(queue, 31, 0)) == E_NOTIMPL && p.suspend_calls == 0); + EXPECT(hr_of(raw(queue, 32)) == E_NOTIMPL && p.resume_calls == 0); + + // Detaching restores default E_NOTIMPL refusal on beyond-PC queue slots. + EXPECT(xs::set_presentation_handler(device, nullptr)); + UINT none = 7; // unattached GetFrameStatisticsX: no statistics yet, never the PC slot 81 + EXPECT(hr_of(raw(device, 81, 77, 0x1a, word(&none), 0)) == S_FALSE && none == 0 && p.stats_calls == 1); + EXPECT(hr_of(raw(queue, 30, 1, 0, 0)) == E_NOTIMPL && p.present_calls == 1); + + EXPECT(release(queue) == 0); + EXPECT(release(device) == 0 && mock->refs == 0); + std::printf("%s: presentation handler wiring (slots 78-81 and queue PresentX 30) passed\n", g_label); + return 0; +} + int main(int argc, char** argv) { no_dialogs(); std::setvbuf(stdout, nullptr, _IONBF, 0); @@ -1546,7 +1680,8 @@ int main(int argc, char** argv) { if (check_tail(true) || check_levels() || check_slot48() || check_fence1() || check_create_failures() || check_open_shared() || check_alloc_failures() || check_allocators() || check_xbox_slots() || check_console_command_list() || check_custom_heap_properties() || - check_resource_allocation_info() || check_slot70() || check_committed_resource3_x()) + check_resource_allocation_info() || check_slot70() || check_committed_resource3_x() || + check_presentation_wiring()) return 1; std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); return 0;