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:
Diffstat (limited to 'runtime/shims/xbox_present.cpp')
-rw-r--r--runtime/shims/xbox_present.cpp54
1 files changed, 43 insertions, 11 deletions
diff --git a/runtime/shims/xbox_present.cpp b/runtime/shims/xbox_present.cpp
index a2c16ca19..9e20c28d6 100644
--- a/runtime/shims/xbox_present.cpp
+++ b/runtime/shims/xbox_present.cpp
@@ -2,6 +2,7 @@
2 2
3#include "../src/vulkan_present.h" 3#include "../src/vulkan_present.h"
4 4
5#include <cerrno>
5#include <chrono> 6#include <chrono>
6#include <cstdio> 7#include <cstdio>
7#include <cstdlib> 8#include <cstdlib>
@@ -31,25 +32,56 @@ XboxPresentation::XboxPresentation(std::unique_ptr<WindowPresenter> presenter, l
31 pacer_(clock ? *clock : *own_clock_) { 32 pacer_(clock ? *clock : *own_clock_) {
32 // A present waiting for a swapchain image gives up once SuspendX has been called. 33 // A present waiting for a swapchain image gives up once SuspendX has been called.
33 presenter_->set_abort([this] { return pacer_.suspended(); }); 34 presenter_->set_abort([this] { return pacer_.suspended(); });
34 // LEONIDA_SDR_EOTF=gamma22 (or 2.2) decodes SDR with a pure power of 2.2 instead of piecewise sRGB. 35 // LEONIDA_SDR_EOTF=gamma22 (or 2.2, any case) decodes SDR with a pure power of 2.2 instead of piecewise sRGB; "srgb" or
35 char eotf[16] = {}; 36 // unset keeps the default; anything else (also a value too long for the buffer, ERANGE) is logged once and ignored.
37 char eotf[32] = {};
36 std::size_t eotf_length = 0; 38 std::size_t eotf_length = 0;
37 if (getenv_s(&eotf_length, eotf, "LEONIDA_SDR_EOTF") == 0 && eotf_length > 1 && 39 const errno_t env = getenv_s(&eotf_length, eotf, "LEONIDA_SDR_EOTF");
38 (!std::strcmp(eotf, "gamma22") || !std::strcmp(eotf, "2.2"))) 40 if (env == 0 && eotf_length > 1) {
39 sdr_eotf_ = leonida::PresentPlanes::SdrEotf::gamma22; 41 if (!_stricmp(eotf, "gamma22") || !_stricmp(eotf, "2.2")) sdr_eotf_ = leonida::PresentPlanes::SdrEotf::gamma22;
42 else if (_stricmp(eotf, "srgb") != 0)
43 std::fprintf(stderr, "[rt] present: LEONIDA_SDR_EOTF=%s is not srgb, gamma22 or 2.2: piecewise sRGB used\n", eotf);
44 } else if (env == ERANGE) {
45 std::fprintf(stderr, "[rt] present: LEONIDA_SDR_EOTF is too long to be srgb, gamma22 or 2.2: piecewise sRGB used\n");
46 }
47 display_white_thread_ = std::thread([this] { display_white_worker(); });
48}
49
50XboxPresentation::~XboxPresentation() {
51 {
52 std::lock_guard lock(worker_mutex_);
53 worker_stop_ = true;
54 }
55 worker_wake_.notify_all();
56 display_white_thread_.join();
57 presenter_->set_abort(nullptr);
40} 58}
41 59
42XboxPresentation::~XboxPresentation() { presenter_->set_abort(nullptr); } 60// Windows' SDR white level costs a QueryDisplayConfig and two DisplayConfigGetDeviceInfo calls (measured in
61// xbox_present_test, docs section 4.5): too much for the present path, which would hitch once a second and feed the pacer.
62// So it is read here, about once a second while an HDR10 swapchain is in use, and PresentX reads the cached value.
63void XboxPresentation::display_white_worker() {
64 std::unique_lock lock(worker_mutex_);
65 while (!worker_stop_) {
66 if (white_wanted_.load()) {
67 lock.unlock();
68 const float nits = presenter_->display_sdr_white_nits();
69 if (nits > 0.0f) display_white_ = nits; // a failed query keeps the last good value
70 lock.lock();
71 }
72 worker_wake_.wait_for(lock, std::chrono::seconds(1), [this] { return worker_stop_; });
73 }
74}
43 75
44float XboxPresentation::effective_sdr_white_nits() { 76float XboxPresentation::effective_sdr_white_nits() {
45 if (sdr_white_override_ > 0.0f) return sdr_white_override_; 77 if (sdr_white_override_ > 0.0f) return sdr_white_override_;
46 if (!presenter_->hdr_active()) return 203.0f; // the Windows setting belongs to HDR mode 78 if (!presenter_->hdr_active()) return 203.0f; // the Windows setting belongs to HDR mode
47 const auto now = std::chrono::steady_clock::now(); 79 if (!white_wanted_.exchange(true)) { // first HDR10 present: have it read now
48 if (display_white_time_ == std::chrono::steady_clock::time_point{} || now - display_white_time_ > std::chrono::seconds(1)) { 80 std::lock_guard lock(worker_mutex_);
49 display_white_ = presenter_->display_sdr_white_nits(); 81 worker_wake_.notify_all();
50 display_white_time_ = now;
51 } 82 }
52 return display_white_ > 0.0f ? display_white_ : 203.0f; 83 const float cached = display_white_;
84 return cached > 0.0f ? cached : 203.0f;
53} 85}
54 86
55HRESULT XboxPresentation::SetFrameIntervalX(IUnknown*, UINT interval_period_us, UINT latency, UINT flags) { 87HRESULT XboxPresentation::SetFrameIntervalX(IUnknown*, UINT interval_period_us, UINT latency, UINT flags) {