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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
commit154e4ec034902deffa4b89b2cbca9a949f38baec (patch)
tree1b2ef5656de42c172291bc6009efa4c510650815 /runtime/shims/xbox_present.cpp
parenteb6a08cd359d98b5777e70d54d0ae8a75879495a (diff)
parent8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff)
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the two solve one problem in two ways the base's is the frame and #939's plugs into it. A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h, d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease, raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages), the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in. A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow #939's rule that a device which ever had a handler has no queue fallback. B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h, pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad lists as triangle indices. B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list, shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch, executable shaders, default A8 views). C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h, rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp, replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the label fence on the base's submit. D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test, main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with #939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's answering first; --first-run-ui kept without host speech in its profile. E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py, scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the base's executable-shader step with #939's cache fix; 366 registered tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xbox_present.cpp')
-rw-r--r--runtime/shims/xbox_present.cpp79
1 files changed, 66 insertions, 13 deletions
diff --git a/runtime/shims/xbox_present.cpp b/runtime/shims/xbox_present.cpp
index f048504e8..77c2c9c53 100644
--- a/runtime/shims/xbox_present.cpp
+++ b/runtime/shims/xbox_present.cpp
@@ -6,6 +6,7 @@
6 6
7#include "../src/vulkan_present.h" 7#include "../src/vulkan_present.h"
8 8
9#include <atomic>
9#include <cerrno> 10#include <cerrno>
10#include <chrono> 11#include <chrono>
11#include <cstdio> 12#include <cstdio>
@@ -69,6 +70,7 @@ XboxPresentation::XboxPresentation(std::unique_ptr<WindowPresenter> presenter, l
69} 70}
70 71
71XboxPresentation::~XboxPresentation() { 72XboxPresentation::~XboxPresentation() {
73 if (presents_.load() > 0) log_pacing_summary();
72 { 74 {
73 std::lock_guard lock(worker_mutex_); 75 std::lock_guard lock(worker_mutex_);
74 worker_stop_ = true; 76 worker_stop_ = true;
@@ -78,6 +80,26 @@ XboxPresentation::~XboxPresentation() {
78 presenter_->set_abort(nullptr); 80 presenter_->set_abort(nullptr);
79} 81}
80 82
83void XboxPresentation::log_pacing_summary() const {
84 const leonida::FramePacingSummary s = pacer_.pacing_summary();
85 std::fprintf(stderr,
86 "[rt] present: pacing summary frames=%llu samples=%llu target_us=%u mean_us=%.1f drift_us=%+.1f "
87 "p50_us=%llu p95_us=%llu p99_us=%llu latency_samples=%llu latency_p50_us=%llu "
88 "latency_p95_us=%llu latency_p99_us=%llu\n",
89 static_cast<unsigned long long>(s.frames),
90 static_cast<unsigned long long>(s.samples),
91 s.target_interval_us,
92 s.mean_us,
93 s.drift_us,
94 static_cast<unsigned long long>(s.p50_us),
95 static_cast<unsigned long long>(s.p95_us),
96 static_cast<unsigned long long>(s.p99_us),
97 static_cast<unsigned long long>(s.latency_samples),
98 static_cast<unsigned long long>(s.latency_p50_us),
99 static_cast<unsigned long long>(s.latency_p95_us),
100 static_cast<unsigned long long>(s.latency_p99_us));
101}
102
81// Windows' SDR white level costs a QueryDisplayConfig and two DisplayConfigGetDeviceInfo calls (measured in 103// Windows' SDR white level costs a QueryDisplayConfig and two DisplayConfigGetDeviceInfo calls (measured in
82// xbox_present_test, docs section 4.5): too much for the present path, which would hitch once a second and feed the pacer. 104// xbox_present_test, docs section 4.5): too much for the present path, which would hitch once a second and feed the pacer.
83// So it is read here, about once a second while an HDR10 swapchain is in use, and PresentX reads the cached value. 105// So it is read here, about once a second while an HDR10 swapchain is in use, and PresentX reads the cached value.
@@ -129,11 +151,31 @@ HRESULT XboxPresentation::PresentX(UINT plane_count, const XboxPresentPlaneParam
129 return d3d12_trace::result("XboxPresentation::PresentX", present_x(plane_count, planes, params)); 151 return d3d12_trace::result("XboxPresentation::PresentX", present_x(plane_count, planes, params));
130} 152}
131 153
154// LOCAL DIAGNOSTIC (LEONIDA_LOCAL_DIAGNOSTICS=1): which check refuses PresentX, with its HRESULT.
155static HRESULT present_diag(int line, HRESULT hr) {
156 static std::atomic<int> logged{0};
157 if (FAILED(hr) && leonida::local_diagnostics() && logged.fetch_add(1) < 40)
158 std::fprintf(stderr, "[rt] PresentX diag: refused at line %d hr 0x%08lx\n", line, static_cast<unsigned long>(hr));
159 return hr;
160}
161
132HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlaneParameters* planes, 162HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlaneParameters* planes,
133 const XboxPresentParameters* params) { 163 const XboxPresentParameters* params) {
134 std::lock_guard serialise(present_mutex_); 164 std::lock_guard serialise(present_mutex_);
135 if (plane_count > 2 || (plane_count && !planes)) return E_INVALIDARG; 165 if (leonida::local_diagnostics()) {
136 if (pacer_.suspended()) return from_pacer(hr::invalid_state); 166 static std::atomic<int> dumped{0};
167 if (dumped.fetch_add(1) < 6) {
168 std::fprintf(stderr, "[rt] PresentX diag: planes %u params %p suspended %d registered %d\n", plane_count,
169 static_cast<const void*>(params), int(pacer_.suspended()), int(pacer_.registered()));
170 for (UINT i = 0; planes && i < plane_count && i < 2; ++i)
171 std::fprintf(stderr, "[rt] PresentX diag: plane %u token 0x%llx count %u res %p res0 %p\n", i,
172 static_cast<unsigned long long>(planes[i].Token), planes[i].ResourceCount,
173 static_cast<const void*>(planes[i].ppResources),
174 planes[i].ppResources ? static_cast<const void*>(planes[i].ppResources[0]) : nullptr);
175 }
176 }
177 if (plane_count > 2 || (plane_count && !planes)) return present_diag(117, E_INVALIDARG);
178 if (pacer_.suspended()) return present_diag(118, from_pacer(hr::invalid_state));
137 // A zero, negative or NaN threshold never tears; an uncapped frame is never late for anything either. 179 // A zero, negative or NaN threshold never tears; an uncapped frame is never late for anything either.
138 const float threshold = params ? params->ImmediateThresholdPercent : 0.0f; 180 const float threshold = params ? params->ImmediateThresholdPercent : 0.0f;
139 const bool wants_tear = threshold > 0.0f && !pacer_.uncapped(); 181 const bool wants_tear = threshold > 0.0f && !pacer_.uncapped();
@@ -152,7 +194,7 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
152 pacer_.note_untokened_present(); 194 pacer_.note_untokened_present();
153 ++presents_; 195 ++presents_;
154 } 196 }
155 return result; 197 return present_diag(__LINE__, result);
156 } 198 }
157 199
158 // All planes carry the token of one frame. 200 // All planes carry the token of one frame.
@@ -160,20 +202,20 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
160 for (UINT i = 0; i < plane_count; ++i) { 202 for (UINT i = 0; i < plane_count; ++i) {
161 const XboxPresentPlaneParameters& plane = planes[i]; 203 const XboxPresentPlaneParameters& plane = planes[i];
162 if (plane.Token != token || plane.ResourceCount == 0 || !plane.ppResources || !plane.ppResources[0]) 204 if (plane.Token != token || plane.ResourceCount == 0 || !plane.ppResources || !plane.ppResources[0])
163 return E_INVALIDARG; 205 return present_diag(145, E_INVALIDARG);
164 // Both paths read ppResources[0] only: a plane with several resources would silently lose the others and could show 206 // Both paths read ppResources[0] only: a plane with several resources would silently lose the others and could show
165 // the wrong frame, so it is refused (before anything is presented) until it is modelled. What the console does 207 // the wrong frame, so it is refused (before anything is presented) until it is modelled. What the console does
166 // with ResourceCount above 1 (views? stereo?) is unmeasured. 208 // with ResourceCount above 1 (views? stereo?) is unmeasured.
167 if (plane.ResourceCount != 1) { 209 if (plane.ResourceCount != 1) {
168 log_once(logged_resource_count_, "PresentX plane with ResourceCount other than 1: not implemented, refused"); 210 log_once(logged_resource_count_, "PresentX plane with ResourceCount other than 1: not implemented, refused");
169 return DXGI_ERROR_UNSUPPORTED; 211 return present_diag(__LINE__, DXGI_ERROR_UNSUPPORTED);
170 } 212 }
171 } 213 }
172 // Only a token that was never handed out is refused. Null, stale and repeated tokens are presented anyway (what the 214 // Only a token that was never handed out is refused. Null, stale and repeated tokens are presented anyway (what the
173 // console does with them is unmeasured, and a title that does it must not freeze); each kind is logged once. 215 // console does with them is unmeasured, and a title that does it must not freeze); each kind is logged once.
174 FramePacer::TokenIssue issue{}; 216 FramePacer::TokenIssue issue{};
175 if (const std::int32_t checked = pacer_.check_present_token(token, &issue); checked != hr::ok) 217 if (const std::int32_t checked = pacer_.check_present_token(token, &issue); checked != hr::ok)
176 return from_pacer(checked); 218 return present_diag(158, from_pacer(checked));
177 switch (issue) { 219 switch (issue) {
178 case FramePacer::TokenIssue::null_token: 220 case FramePacer::TokenIssue::null_token:
179 log_once(logged_null_token_, "PresentX with the null token: presented anyway (real behaviour unmeasured)"); 221 log_once(logged_null_token_, "PresentX with the null token: presented anyway (real behaviour unmeasured)");
@@ -220,10 +262,21 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
220 // Plane 1 is composed over plane 0 only when flagged an overlay (hypothesis). Otherwise it is not shown, as before: in 262 // Plane 1 is composed over plane 0 only when flagged an overlay (hypothesis). Otherwise it is not shown, as before: in
221 // the public HDR sample it is the GameDVR copy of the frame, and blending a plane we cannot tell apart from it risks 263 // the public HDR sample it is the GameDVR copy of the frame, and blending a plane we cannot tell apart from it risks
222 // drawing the frame twice with an alpha that was never meant to be blended. 264 // drawing the frame twice with an alpha that was never meant to be blended.
223 const bool overlay = plane_count == 2 && (planes[1].Flags & xbox_plane_flag_overlay) != 0; 265 // The retail title (GTA VI) presents two planes with Flags 0 on both: plane 0 is the scene, plane 1 is the target
266 // its front end draws on (loading screen, prompts, UI), cleared to transparent every frame, and its own
267 // UI-over-scene composite is skipped in that mode, so the display blends plane 1 over plane 0 (read from the
268 // title's code: docs/re/notes/firstframe.md, F14). So a second plane is composed over the first whether or
269 // not it carries the overlay flag. LEONIDA_PRESENT_PLANE1=0 shows plane 0 alone, as before.
270 static const bool plane1_always = [] {
271 wchar_t value[2]{};
272 return !(GetEnvironmentVariableW(L"LEONIDA_PRESENT_PLANE1", value, 2) == 1 && value[0] == L'0');
273 }();
274 const bool overlay = plane_count == 2 && (plane1_always || (planes[1].Flags & xbox_plane_flag_overlay) != 0);
224 if (plane_count == 2 && !overlay) 275 if (plane_count == 2 && !overlay)
225 log_once(logged_planes_, "PresentX with 2 planes and no overlay flag on plane 1: only plane 0 is shown (plane 1 is taken " 276 log_once(logged_planes_, "PresentX with 2 planes and no overlay flag on plane 1: only plane 0 is shown (plane 1 is taken "
226 "as the GameDVR copy of the frame)"); 277 "as the GameDVR copy of the frame)");
278 else if (plane_count == 2)
279 log_once(logged_planes_, "PresentX with 2 planes: plane 1 is blended over plane 0");
227 const bool compose = hdr0 || placement(planes[0]) || filter_of(planes[0]) != xbox_scale_filter_default || overlay; 280 const bool compose = hdr0 || placement(planes[0]) || filter_of(planes[0]) != xbox_scale_filter_default || overlay;
228 281
229 if (compose) { 282 if (compose) {
@@ -237,12 +290,12 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
237 const XboxPresentPlaneParameters& p = planes[i]; 290 const XboxPresentPlaneParameters& p = planes[i];
238 ID3D12Resource* source = p.ppResources[0]; 291 ID3D12Resource* source = p.ppResources[0];
239 const D3D12_RESOURCE_DESC source_desc = source->GetDesc(); 292 const D3D12_RESOURCE_DESC source_desc = source->GetDesc();
240 if (source_desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return E_INVALIDARG; 293 if (source_desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return present_diag(222, E_INVALIDARG);
241 const D3D12_RECT* source_rect = p.pSrcViewRects; 294 const D3D12_RECT* source_rect = p.pSrcViewRects;
242 if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right || 295 if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right ||
243 source_rect->top >= source_rect->bottom || UINT64(source_rect->right) > source_desc.Width || 296 source_rect->top >= source_rect->bottom || UINT64(source_rect->right) > source_desc.Width ||
244 UINT(source_rect->bottom) > source_desc.Height)) 297 UINT(source_rect->bottom) > source_desc.Height))
245 return E_INVALIDARG; 298 return present_diag(227, E_INVALIDARG);
246 const XboxViewRect* place = placement(p); 299 const XboxViewRect* place = placement(p);
247 leonida::PresentPlane& out = composed.plane[i]; 300 leonida::PresentPlane& out = composed.plane[i];
248 out.source = resource_id(source); 301 out.source = resource_id(source);
@@ -266,7 +319,7 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
266 tear = leonida::decide_present_sync(timing.late_us, timing.period_us, threshold) == leonida::PresentSync::immediate; 319 tear = leonida::decide_present_sync(timing.late_us, timing.period_us, threshold) == leonida::PresentSync::immediate;
267 } 320 }
268 const HRESULT result = presenter_->present_planes(composed, sync_interval, tear); 321 const HRESULT result = presenter_->present_planes(composed, sync_interval, tear);
269 if (FAILED(result)) return pacer_.suspended() ? from_pacer(hr::invalid_state) : result; 322 if (FAILED(result)) return present_diag(251, pacer_.suspended() ? from_pacer(hr::invalid_state) : result);
270 pacer_.commit_present_token(token, plane_count, 323 pacer_.commit_present_token(token, plane_count,
271 tear && presenter_->last_present_tore() ? leonida::PresentSync::immediate 324 tear && presenter_->last_present_tore() ? leonida::PresentSync::immediate
272 : leonida::PresentSync::vblank); 325 : leonida::PresentSync::vblank);
@@ -277,7 +330,7 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
277 const XboxPresentPlaneParameters& plane = planes[0]; 330 const XboxPresentPlaneParameters& plane = planes[0];
278 ID3D12Resource* resource = plane.ppResources[0]; 331 ID3D12Resource* resource = plane.ppResources[0];
279 const D3D12_RESOURCE_DESC desc = resource->GetDesc(); 332 const D3D12_RESOURCE_DESC desc = resource->GetDesc();
280 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return E_INVALIDARG; 333 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return present_diag(262, E_INVALIDARG);
281 D3D12_HEAP_PROPERTIES heap{}; 334 D3D12_HEAP_PROPERTIES heap{};
282 D3D12_HEAP_FLAGS heap_flags = D3D12_HEAP_FLAG_NONE; 335 D3D12_HEAP_FLAGS heap_flags = D3D12_HEAP_FLAG_NONE;
283 if (SUCCEEDED(resource->GetHeapProperties(&heap, &heap_flags)) && !(heap_flags & D3D12_HEAP_FLAG_ALLOW_DISPLAY)) 336 if (SUCCEEDED(resource->GetHeapProperties(&heap, &heap_flags)) && !(heap_flags & D3D12_HEAP_FLAG_ALLOW_DISPLAY))
@@ -286,7 +339,7 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
286 const D3D12_RECT* rect = plane.pSrcViewRects; 339 const D3D12_RECT* rect = plane.pSrcViewRects;
287 if (rect && (rect->left < 0 || rect->top < 0 || rect->left >= rect->right || rect->top >= rect->bottom || 340 if (rect && (rect->left < 0 || rect->top < 0 || rect->left >= rect->right || rect->top >= rect->bottom ||
288 UINT64(rect->right) > desc.Width || UINT(rect->bottom) > desc.Height)) 341 UINT64(rect->right) > desc.Width || UINT(rect->bottom) > desc.Height))
289 return E_INVALIDARG; 342 return present_diag(271, E_INVALIDARG);
290 343
291 // How late the frame is, now that everything that can refuse it has: past the vblank it was due at, by what part of 344 // How late the frame is, now that everything that can refuse it has: past the vblank it was due at, by what part of
292 // the interval the scan has moved on. The presenter says whether it really queued it as a tear. 345 // the interval the scan has moved on. The presenter says whether it really queued it as a tear.
@@ -297,7 +350,7 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara
297 tear = leonida::decide_present_sync(timing.late_us, timing.period_us, threshold) == leonida::PresentSync::immediate; 350 tear = leonida::decide_present_sync(timing.late_us, timing.period_us, threshold) == leonida::PresentSync::immediate;
298 } 351 }
299 const HRESULT result = presenter_->present(resource, sync_interval, rect, tear); 352 const HRESULT result = presenter_->present(resource, sync_interval, rect, tear);
300 if (FAILED(result)) return pacer_.suspended() ? from_pacer(hr::invalid_state) : result; 353 if (FAILED(result)) return present_diag(282, pacer_.suspended() ? from_pacer(hr::invalid_state) : result);
301 pacer_.commit_present_token(token, plane_count, 354 pacer_.commit_present_token(token, plane_count,
302 tear && presenter_->last_present_tore() ? leonida::PresentSync::immediate 355 tear && presenter_->last_present_tore() ? leonida::PresentSync::immediate
303 : leonida::PresentSync::vblank); 356 : leonida::PresentSync::vblank);