From 7698e4e1674376d0d89ceb930b5014d2b721d5fb Mon Sep 17 00:00:00 2001 From: c0pperlane <84517100+c0pperlane@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:54:11 +0100 Subject: [PATCH] present: a LEONIDA_CAPTURE file is written by the present it captures (RT-CAPTURE-LAST-FRAME) With LEONIDA_CAPTURE set, a run that presented N frames left N-1 files. A frame due a file only marked its fence pending (vulkan_present.h, pending_capture_), and the file was written by the next present, a swapchain recreation or ~VulkanPresenter. gta6rt ends a run with the title's device alive (main.cpp: stop_list_values, stop_pipeline_compiles, then ExitProcess), and the presenter belongs to the device proxy's PresentState, which only proxy_release frees: no destructor runs, so the last frame due was lost. submit_now() now finishes the pending capture once the frame's state is recorded: it waits for that frame's fence (its blit and copy) and writes the file before returning. Nothing runs at exit, so no Vulkan call is made after the device or loader goes and nothing runs under the loader lock; only frames due a file wait, as the present dump already does for its sampled frames. xbox_present.h: the comment on attach_xbox_presentation said gta6rt creates no XboxPresentation. The device proxy builds one on the title's first queue PresentX (present_state_for); the comment now says so, and that nothing calls the attach seam yet. d3d12_present_test: after each of the first four presents (EVERY=1, LIMIT=4) the files on disk must equal the presents so far, with the presenter and device alive. Fails without the change ("the file of frame 1 was not on disk when its present returned") on an RTX 5080 Laptop GPU and a Radeon 610M; passes with it. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/xbox_present.h | 11 +++++++---- runtime/src/vulkan_present.h | 4 ++++ runtime/test/d3d12_present_test.cpp | 27 +++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/runtime/shims/xbox_present.h b/runtime/shims/xbox_present.h index 86381f2d62..107b5f6411 100644 --- a/runtime/shims/xbox_present.h +++ b/runtime/shims/xbox_present.h @@ -207,10 +207,13 @@ 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. +// Seam for a presentation the caller owns, which nothing calls yet. gta6rt does not need it: its device proxy builds +// an XboxPresentation of its own on the title's first queue PresentX (present_state_for in d3d12_xs_device.cpp) and +// keeps it until the proxy goes; PresentX reaches that one while nothing is attached here, and slots 78–81 only +// with LEONIDA_FRAME_PACING=1. Attaching wires `presentation` into a d3d12_xs device proxy's frame-event slots +// (78–81) and queue PresentX (slot 30, 32 in the newer queue layout, or LEONIDA_XS_PRESENT_X_SLOT) ahead of that; +// 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); diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index d494233d3a..3bc41765d7 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h @@ -683,6 +683,10 @@ class VulkanPresenter { captured_extent_ = extent_; captured_format_ = format_; } + // LEONIDA_CAPTURE: a frame due a file has it written here, once its copy is done, not by the next present. A + // run ends with the device and this presenter alive (gta6rt's ExitProcess runs no destructor), so a file left + // for later lost the last frame. Only frames due a file wait, as the present dump above does for its frames. + if (const VkResult r = finish_pending_capture(); r != VK_SUCCESS) return stop(r); if (result == VK_ERROR_SURFACE_LOST_KHR) { // The surface was lost during the present (display disconnected, a driver reset of the window's // plane): vkd3d keeps rendering to nothing, and the next present recreates surface and swapchain. diff --git a/runtime/test/d3d12_present_test.cpp b/runtime/test/d3d12_present_test.cpp index b94e958522..9ff65bf738 100644 --- a/runtime/test/d3d12_present_test.cpp +++ b/runtime/test/d3d12_present_test.cpp @@ -470,9 +470,28 @@ int main() { return SUCCEEDED(queue->Signal(fence.p, ++signalled)); }; + // The capture files on disk so far (LEONIDA_CAPTURE_LIMIT is 4, so at most frames 1 to 4). + const auto capture_files = [&]() -> std::uint64_t { + std::uint64_t files = 0; + for (int i = 1; i <= 4; ++i) { + wchar_t path[MAX_PATH + 32]{}; + swprintf_s(path, L"%s\\frame_%05d.ppm", capture_dir, i); + if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES) ++files; + } + return files; + }; + // Every present is counted unless the surface reported it out of date, which the driver may do around a - // resize; the double counts those. - for (int i = 0; i < 60; ++i) EXPECT(frame(1)); + // resize; the double counts those. Each present due a capture file has written it by the time it returns, + // with the presenter and the device still alive: a run ends in that state (gta6rt's ExitProcess destroys + // neither), so a file left for the next present or the presenter's destructor is lost, and N presents + // gave N-1 files. Checked with the other capture checks below, after the files are removed. + std::uint64_t capture_late = 0; // the first present whose file was not on disk when it returned + for (int i = 0; i < 60; ++i) { + EXPECT(frame(1)); + const std::uint64_t shown = presenter->presented(); + if (capture_dir[0] && !capture_late && shown <= 4 && capture_files() != shown) capture_late = shown; + } EXPECT(presenter->presented() == 60 - driver::out_of_date); const VkExtent2D first = presenter->extent(); EXPECT(first.width == width && first.height == height); @@ -685,10 +704,14 @@ int main() { std::fabs(b - (red ? 0 : 230)) <= 3.0) ++matching; } std::printf("capture: %d correct images, %d index rows, %d matching frame colours\n", exact_files, lines, matching); + if (capture_late) + std::printf("capture: the file of frame %llu was not on disk when its present returned\n", + static_cast(capture_late)); index.close(); DeleteFileA((std::string(narrow) + "\\index.txt").c_str()); RemoveDirectoryA(narrow); EXPECT(exact_files == 4 && lines == 4 && matching == 4); + EXPECT(!capture_late); } presenter.reset(); // before the device's queue and resources go