upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/xbox_present.cpp
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 01:55:18 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 05:28:06 +0100
commitb7a0232523b510f4e6e7233a6b8d503f0134d431 (patch)
tree380a277ef81c0ad62645431a6b41b8c7c9103244 /runtime/shims/xbox_present.cpp
parent4d62f48a5cd7f0133cccb83f55a763b587e64019 (diff)
runtime: XboxPresentation (PresentX 0/1/2 planes, frame events, Suspend/ResumeX) and xbox_present_test
Provisional struct layouts and constants of ours; real slots unmeasured (TODO table in the header). The test reads the swapchain image back on a Vulkan device and runs the frame events on a fake clock, then 30 frames in real time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xbox_present.cpp')
-rw-r--r--runtime/shims/xbox_present.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/runtime/shims/xbox_present.cpp b/runtime/shims/xbox_present.cpp
new file mode 100644
index 000000000..35d14eaa7
--- /dev/null
+++ b/runtime/shims/xbox_present.cpp
@@ -0,0 +1,112 @@
1#include "xbox_present.h"
2
3#include <cstdio>
4
5namespace rt::d3d12 {
6
7namespace {
8
9using leonida::FramePacer;
10namespace hr = leonida::pacer_hr;
11
12HRESULT from_pacer(std::int32_t value) { return HRESULT(value); }
13
14// The fixed text of a log line is printed once per object.
15void log_once(bool& done, const char* text) {
16 if (done) return;
17 done = true;
18 std::fprintf(stderr, "[rt] present: %s\n", text);
19}
20
21} // namespace
22
23XboxPresentation::XboxPresentation(std::unique_ptr<WindowPresenter> presenter, leonida::FrameClock* clock)
24 : presenter_(std::move(presenter)),
25 own_clock_(clock ? nullptr : std::make_unique<leonida::SteadyFrameClock>()),
26 pacer_(clock ? *clock : *own_clock_) {}
27
28XboxPresentation::~XboxPresentation() = default;
29
30HRESULT XboxPresentation::SetFrameIntervalX(IUnknown*, UINT interval_period_us, UINT latency, UINT flags) {
31 return from_pacer(pacer_.set_frame_interval(interval_period_us, latency, flags));
32}
33
34HRESULT XboxPresentation::ScheduleFrameEventX(UINT event, UINT offset_us, UINT flags) {
35 return from_pacer(pacer_.schedule_frame_event(event, offset_us, flags));
36}
37
38HRESULT XboxPresentation::WaitFrameEventX(UINT event, DWORD timeout_ms, UINT flags, std::uint64_t* token) {
39 return from_pacer(pacer_.wait_frame_event(event, timeout_ms, flags, token));
40}
41
42HRESULT XboxPresentation::GetFrameStatisticsX(std::uint64_t token, UINT type_mask, UINT* count,
43 leonida::FrameStatistic* out) {
44 return from_pacer(pacer_.get_frame_statistics(token, type_mask, count, out));
45}
46
47HRESULT XboxPresentation::PresentX(UINT plane_count, const XboxPresentPlaneParameters* planes,
48 const XboxPresentParameters* params) {
49 std::lock_guard serialise(present_mutex_);
50 if (plane_count > 2 || (plane_count && !planes)) return E_INVALIDARG;
51 if (pacer_.suspended()) return from_pacer(hr::invalid_state);
52 if (params && params->ImmediateThresholdPercent != 0.0f)
53 log_once(logged_threshold_, "ImmediateThresholdPercent is not implemented: a late frame waits for the next vblank");
54
55 // Unpaced (no interval registered): do not hold the queue for vblanks.
56 const UINT sync_interval = pacer_.registered() ? 1 : 0;
57
58 if (plane_count == 0) {
59 const HRESULT result = presenter_->present_blank(sync_interval);
60 if (SUCCEEDED(result)) {
61 pacer_.note_untokened_present();
62 ++presents_;
63 }
64 return result;
65 }
66
67 // All planes carry the token of one frame.
68 const std::uint64_t token = planes[0].Token;
69 for (UINT i = 0; i < plane_count; ++i) {
70 const XboxPresentPlaneParameters& plane = planes[i];
71 if (plane.Token != token || plane.ResourceCount == 0 || !plane.ppResources || !plane.ppResources[0])
72 return E_INVALIDARG;
73 }
74 if (const std::int32_t checked = pacer_.check_present_token(token); checked != hr::ok) return from_pacer(checked);
75
76 const XboxPresentPlaneParameters& plane = planes[0];
77 ID3D12Resource* resource = plane.ppResources[0];
78 const D3D12_RESOURCE_DESC desc = resource->GetDesc();
79 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.SampleDesc.Count != 1) return E_INVALIDARG;
80 D3D12_HEAP_PROPERTIES heap{};
81 D3D12_HEAP_FLAGS heap_flags = D3D12_HEAP_FLAG_NONE;
82 if (SUCCEEDED(resource->GetHeapProperties(&heap, &heap_flags)) && !(heap_flags & D3D12_HEAP_FLAG_ALLOW_DISPLAY))
83 log_once(logged_display_, "a present plane's resource was created without D3D12_HEAP_FLAG_ALLOW_DISPLAY; presenting it anyway");
84
85 const D3D12_RECT* rect = plane.pSrcViewRects;
86 if (rect && (rect->left < 0 || rect->top < 0 || rect->left >= rect->right || rect->top >= rect->bottom ||
87 UINT64(rect->right) > desc.Width || UINT(rect->bottom) > desc.Height))
88 return E_INVALIDARG;
89
90 if (plane_count == 2)
91 log_once(logged_planes_, "PresentX with 2 planes: only plane 0 is shown; HDR/GameDVR plane composition is not implemented");
92
93 const HRESULT result = presenter_->present(resource, sync_interval, rect);
94 if (FAILED(result)) return result;
95 pacer_.commit_present_token(token, plane_count);
96 ++presents_;
97 return S_OK;
98}
99
100HRESULT XboxPresentation::SuspendX(UINT) {
101 std::lock_guard serialise(present_mutex_);
102 pacer_.suspend();
103 return S_OK;
104}
105
106HRESULT XboxPresentation::ResumeX() {
107 std::lock_guard serialise(present_mutex_);
108 pacer_.resume();
109 return S_OK;
110}
111
112} // namespace rt::d3d12