upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:07:14 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:07:14 -0500
commit2be1e08271de77edcab38785c177714a0600a25b (patch)
treed5dfb55e97bbe6d153f0cce54a0a288cb048a051 /runtime
parente688f7df1cd9d8d79a48e5a5feaacc8ea5c08879 (diff)
parent0438ee44fdc03b65765e2ef3285e05326691afbd (diff)
Merge PR #9549
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/xbox_present.cpp9
-rw-r--r--runtime/shims/xbox_present.h4
-rw-r--r--runtime/test/xbox_present_test.cpp75
3 files changed, 86 insertions, 2 deletions
diff --git a/runtime/shims/xbox_present.cpp b/runtime/shims/xbox_present.cpp
index f9ed6191a..dd5f810cf 100644
--- a/runtime/shims/xbox_present.cpp
+++ b/runtime/shims/xbox_present.cpp
@@ -1,5 +1,7 @@
1#include "xbox_present.h" 1#include "xbox_present.h"
2 2
3#include "d3d12_trace.h"
4
3#include "../src/vulkan_present.h" 5#include "../src/vulkan_present.h"
4 6
5#include <cerrno> 7#include <cerrno>
@@ -119,6 +121,13 @@ HRESULT XboxPresentation::GetFrameStatisticsX(std::uint64_t token, UINT type_mas
119 121
120HRESULT XboxPresentation::PresentX(UINT plane_count, const XboxPresentPlaneParameters* planes, 122HRESULT XboxPresentation::PresentX(UINT plane_count, const XboxPresentPlaneParameters* planes,
121 const XboxPresentParameters* params) { 123 const XboxPresentParameters* params) {
124 // One trace line per present, whatever path the call takes out of present_x: the queue proxy's PresentX slot is
125 // past the PC chain, so nothing else records it, and a frame loop's presents could not be counted from a trace.
126 return d3d12_trace::result("XboxPresentation::PresentX", present_x(plane_count, planes, params));
127}
128
129HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlaneParameters* planes,
130 const XboxPresentParameters* params) {
122 std::lock_guard serialise(present_mutex_); 131 std::lock_guard serialise(present_mutex_);
123 if (plane_count > 2 || (plane_count && !planes)) return E_INVALIDARG; 132 if (plane_count > 2 || (plane_count && !planes)) return E_INVALIDARG;
124 if (pacer_.suspended()) return from_pacer(hr::invalid_state); 133 if (pacer_.suspended()) return from_pacer(hr::invalid_state);
diff --git a/runtime/shims/xbox_present.h b/runtime/shims/xbox_present.h
index a478df0dd..60a317bee 100644
--- a/runtime/shims/xbox_present.h
+++ b/runtime/shims/xbox_present.h
@@ -146,6 +146,8 @@ public:
146 // for a present that was waiting for a swapchain image when SuspendX came. 146 // for a present that was waiting for a swapchain image when SuspendX came.
147 // `params` may be null (threshold 0). ImmediateThresholdPercent: see XboxPresentParameters; a frame that was shown 147 // `params` may be null (threshold 0). ImmediateThresholdPercent: see XboxPresentParameters; a frame that was shown
148 // at once has its display statistic's percent_scanned set and is counted in immediate_presents(). 148 // at once has its display statistic's percent_scanned set and is counted in immediate_presents().
149 // With --trace-d3d12 every call writes one `[d3d12-trace] ... XboxPresentation::PresentX <ok|failed>`
150 // line (shims/d3d12_trace.h), so a run's presents can be counted and a refused one is visible.
149 HRESULT PresentX(UINT plane_count, const XboxPresentPlaneParameters* planes, const XboxPresentParameters* params); 151 HRESULT PresentX(UINT plane_count, const XboxPresentPlaneParameters* planes, const XboxPresentParameters* params);
150 // SuspendX drops the interval, the origin event and the frames in flight, and refuses presents. ResumeX allows 152 // SuspendX drops the interval, the origin event and the frames in flight, and refuses presents. ResumeX allows
151 // presents again; SetFrameIntervalX and ScheduleFrameEventX must be called again before the next wait. 153 // presents again; SetFrameIntervalX and ScheduleFrameEventX must be called again before the next wait.
@@ -171,6 +173,8 @@ public:
171 std::uint64_t immediate_presents() const { return pacer_.immediate_presents(); } 173 std::uint64_t immediate_presents() const { return pacer_.immediate_presents(); }
172 174
173private: 175private:
176 // PresentX without its `[d3d12-trace]` line.
177 HRESULT present_x(UINT plane_count, const XboxPresentPlaneParameters* planes, const XboxPresentParameters* params);
174 float effective_sdr_white_nits(); // override, else the cached Windows setting while HDR10, else 203 178 float effective_sdr_white_nits(); // override, else the cached Windows setting while HDR10, else 203
175 void display_white_worker(); // refreshes display_white_ off the present path 179 void display_white_worker(); // refreshes display_white_ off the present path
176 std::unique_ptr<WindowPresenter> presenter_; 180 std::unique_ptr<WindowPresenter> presenter_;
diff --git a/runtime/test/xbox_present_test.cpp b/runtime/test/xbox_present_test.cpp
index e15183798..91dd13b90 100644
--- a/runtime/test/xbox_present_test.cpp
+++ b/runtime/test/xbox_present_test.cpp
@@ -14,6 +14,7 @@
14#include "xbox_present_readback_size.h" 14#include "xbox_present_readback_size.h"
15#include "../shims/d3d12_com.h" 15#include "../shims/d3d12_com.h"
16#include "../shims/d3d12_xs_device.h" 16#include "../shims/d3d12_xs_device.h"
17#include "../shims/d3d12_trace.h"
17#include "../shims/xbox_present.h" 18#include "../shims/xbox_present.h"
18 19
19#include <algorithm> 20#include <algorithm>
@@ -24,6 +25,11 @@
24#include <cstdint> 25#include <cstdint>
25#include <cstdlib> 26#include <cstdlib>
26#include <array> 27#include <array>
28#include <filesystem>
29#include <fstream>
30#include <io.h>
31#include <iterator>
32#include <string>
27#include <vector> 33#include <vector>
28 34
29#define EXPECT(condition) do { \ 35#define EXPECT(condition) do { \
@@ -34,6 +40,41 @@ namespace {
34 40
35using XboxHeapHandleFn = std::uint64_t(WINAPI*)(void* heap); // heap slot 9: 8-byte handle in RAX 41using XboxHeapHandleFn = std::uint64_t(WINAPI*)(void* heap); // heap slot 9: 8-byte handle in RAX
36 42
43// Sends stderr (where the D3D12 trace goes) to a file while it lives, with the trace on or off. `redirected` is false
44// when the file could not be opened (the capture then holds nothing, which must not read as "no trace line").
45struct TraceCapture {
46 std::filesystem::path path;
47 int saved = -1;
48 bool redirected = false;
49 TraceCapture(const std::filesystem::path& file, bool trace_on) : path(file) {
50 std::fflush(stderr);
51 std::FILE* out = nullptr;
52 if (_wfopen_s(&out, path.c_str(), L"wb") != 0) out = nullptr; // wide: a TEMP outside the ANSI code page
53 saved = _dup(2);
54 if (out && saved >= 0) redirected = _dup2(_fileno(out), 2) == 0;
55 if (out) std::fclose(out);
56 rt::d3d12_trace::set_enabled(trace_on);
57 }
58 std::string finish() {
59 rt::d3d12_trace::set_enabled(false);
60 std::fflush(stderr);
61 if (saved >= 0) {
62 _dup2(saved, 2);
63 _close(saved);
64 saved = -1;
65 }
66 std::ifstream in(path, std::ios::binary);
67 return {std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
68 }
69 ~TraceCapture() { finish(); }
70};
71
72int count_of(const std::string& text, const char* needle) {
73 int count = 0;
74 for (std::size_t at = text.find(needle); at != std::string::npos; at = text.find(needle, at + 1)) ++count;
75 return count;
76}
77
37template<class T> 78template<class T>
38struct Ref { 79struct Ref {
39 T* p = nullptr; 80 T* p = nullptr;
@@ -440,8 +481,38 @@ int main() {
440 EXPECT(region_is(frame, 0, 0, width, height, {51, 153, 230})); 481 EXPECT(region_is(frame, 0, 0, width, height, {51, 153, 230}));
441 std::printf("1 plane, RGBA8 solid: ok (swapchain format %d)\n", int(frame.format)); 482 std::printf("1 plane, RGBA8 solid: ok (swapchain format %d)\n", int(frame.format));
442 483
443 // 0 planes: a black frame, and no token is needed. 484 // 0 planes: a black frame, and no token is needed. With the D3D12 trace on, each PresentX writes one line
444 EXPECT(xbox.PresentX(0, nullptr, nullptr) == S_OK); 485 // with its outcome, a refused call included; with it off, none.
486 {
487 std::error_code ignored;
488 const std::filesystem::path trace_file =
489 std::filesystem::temp_directory_path(ignored) / "leonida_xbox_present_trace.txt";
490 std::string trace, untraced;
491 HRESULT blank = E_FAIL, refused = S_OK, refused_untraced = S_OK;
492 bool captured = false, captured_untraced = false;
493 {
494 TraceCapture capture(trace_file, true);
495 captured = capture.redirected;
496 blank = xbox.PresentX(0, nullptr, nullptr);
497 refused = xbox.PresentX(3, &plane, nullptr); // more than two planes: refused before anything is shown
498 trace = capture.finish();
499 }
500 {
501 TraceCapture capture(trace_file, false);
502 captured_untraced = capture.redirected;
503 refused_untraced = xbox.PresentX(3, &plane, nullptr);
504 untraced = capture.finish();
505 }
506 std::filesystem::remove(trace_file, ignored);
507 EXPECT(captured && captured_untraced);
508 EXPECT(blank == S_OK);
509 EXPECT(refused == E_INVALIDARG);
510 EXPECT(refused_untraced == E_INVALIDARG);
511 EXPECT(count_of(untraced, "[d3d12-trace]") == 0);
512 EXPECT(count_of(trace, " XboxPresentation::PresentX ok\n") == 1);
513 EXPECT(count_of(trace, " XboxPresentation::PresentX failed\n") == 1);
514 EXPECT(count_of(trace, "[d3d12-trace] #") == 2);
515 }
445 EXPECT(grab(frame)); 516 EXPECT(grab(frame));
446 EXPECT(region_is(frame, 0, 0, width, height, {0, 0, 0}, 0)); 517 EXPECT(region_is(frame, 0, 0, width, height, {0, 0, 0}, 0));
447 // (The blank frame is a frame in flight like any other: with latency 1 the next wait needs it shown first.) 518 // (The blank frame is a frame in flight like any other: with latency 1 the next wait needs it shown first.)