X Tutup
Skip to content

Commit 02c88ab

Browse files
committed
Corrected maximum, maximum full frame, and reference luminance calculations. Made it only possible to change maximum luminance.
1 parent 232a6dc commit 02c88ab

File tree

3 files changed

+96
-25
lines changed

3 files changed

+96
-25
lines changed

platform/macos/display_server_macos.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ class DisplayServerMacOS : public DisplayServerMacOSBase {
143143
bool is_visible = true;
144144
bool extend_to_title = false;
145145
bool hide_from_capture = false;
146+
147+
// HDR
146148
bool hdr_output_requested = false;
149+
bool hdr_output_auto_adjust_max_luminance = false;
150+
float hdr_output_max_luminance = 0.0f;
147151

148152
Rect2i parent_safe_rect;
149153
};

platform/macos/display_server_macos.mm

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,21 +1846,34 @@
18461846

18471847
p_screen = _get_screen_index(p_screen);
18481848
NSScreen *screen = NSScreen.screens[p_screen];
1849-
return screen.maximumExtendedDynamicRangeColorComponentValue * 100.0;
1849+
1850+
// Note: this absolute nits value is incorrect when reference luminance is below 100 nits.
1851+
return screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f;
18501852
}
18511853

18521854
float DisplayServerMacOS::screen_get_max_full_frame_luminance(int p_screen) const {
18531855
_THREAD_SAFE_METHOD_
18541856

1855-
p_screen = _get_screen_index(p_screen);
1856-
NSScreen *screen = NSScreen.screens[p_screen];
1857-
return screen.maximumExtendedDynamicRangeColorComponentValue * 100.0;
1857+
return screen_get_reference_luminance(p_screen); // macOS provides no way of reading this information about the screen.
18581858
}
18591859

18601860
float DisplayServerMacOS::screen_get_reference_luminance(int p_screen) const {
18611861
// Per https://developer.apple.com/documentation/metal/performing-your-own-tone-mapping?language=objc#Understand-EDR-Pixel-Values,
18621862
// the SDR white level is always 1.0.
1863-
return 100.0f; // 100 nits.
1863+
1864+
p_screen = _get_screen_index(p_screen);
1865+
NSScreen *screen = NSScreen.screens[p_screen];
1866+
1867+
// Note: this absolute nits value is incorrect when reference luminance is below 100 nits.
1868+
float max_luminance = screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f;
1869+
1870+
float maximum_value = screen.maximumReferenceExtendedDynamicRangeColorComponentValue;
1871+
if (maximum_value <= 0.0) {
1872+
maximum_value = screen.maximumExtendedDynamicRangeColorComponentValue;
1873+
}
1874+
1875+
// Note: this absolute nits value is incorrect when reference luminance is below 100 nits.
1876+
return max_luminance / maximum_value;
18641877
}
18651878

18661879
Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const {
@@ -3011,11 +3024,23 @@
30113024
#pragma clang diagnostic push
30123025
#pragma clang diagnostic ignored "-Wunused-variable"
30133026

3014-
CGFloat max_edr_ccv = screen.maximumExtendedDynamicRangeColorComponentValue * 100.0f; // Convert to nits.
3015-
rendering_context->window_set_hdr_output_max_luminance(p_window, max_edr_ccv);
3027+
// Note: this absolute nits value is incorrect when reference luminance is below 100 nits.
3028+
float max_luminance = screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f;
3029+
3030+
float maximum_value = screen.maximumReferenceExtendedDynamicRangeColorComponentValue;
3031+
if (maximum_value <= 0.0) {
3032+
maximum_value = screen.maximumExtendedDynamicRangeColorComponentValue;
3033+
}
3034+
3035+
// Note: this absolute nits value is incorrect when reference luminance is below 100 nits.
3036+
float reference_luminance = max_luminance / maximum_value;
3037+
3038+
rendering_context->window_set_hdr_output_reference_luminance(p_window, reference_luminance);
3039+
3040+
if (p_wd.hdr_output_auto_adjust_max_luminance) {
3041+
rendering_context->window_set_hdr_output_max_luminance(p_window, max_luminance);
3042+
}
30163043

3017-
// TODO(sgc): Is this correct? 203 nits is also a common reference luminance.
3018-
rendering_context->window_set_hdr_output_reference_luminance(p_window, 100.0);
30193044
#pragma clang diagnostic pop
30203045
}
30213046
#endif
@@ -3083,21 +3108,17 @@
30833108
}
30843109

30853110
void DisplayServerMacOS::window_set_hdr_output_auto_adjust_reference_luminance(const bool p_enabled, WindowID p_window) {
3086-
// Apple platforms only support using screen luminance
3111+
// Apple platforms only support using system reference luminance.
30873112
}
30883113

30893114
bool DisplayServerMacOS::window_is_hdr_output_auto_adjusting_reference_luminance(WindowID p_window) const {
3090-
ERR_FAIL_V_MSG(true, "Not implemented");
3115+
return true;
30913116
}
30923117

30933118
void DisplayServerMacOS::window_set_hdr_output_reference_luminance(const float p_reference_luminance, WindowID p_window) {
3094-
_THREAD_SAFE_METHOD_
3095-
3096-
#if defined(RD_ENABLED)
3097-
if (rendering_context) {
3098-
rendering_context->window_set_hdr_output_reference_luminance(p_window, p_reference_luminance);
3099-
}
3100-
#endif
3119+
// Apple platforms do not support adjusting reference luminance because
3120+
// there are easy to find user-facing brightness settings built into the OS.
3121+
ERR_FAIL_MSG("Cannot set reference luminance on Mac OS; use system brightness setting instead.");
31013122
}
31023123

31033124
float DisplayServerMacOS::window_get_hdr_output_reference_luminance(WindowID p_window) const {
@@ -3112,14 +3133,55 @@
31123133
}
31133134

31143135
void DisplayServerMacOS::window_set_hdr_output_auto_adjust_max_luminance(const bool p_enabled, WindowID p_window) {
3115-
// Apple platforms only support using screen luminance
3136+
_THREAD_SAFE_METHOD_
3137+
3138+
WindowData &wd = windows[p_window];
3139+
if (wd.hdr_output_auto_adjust_max_luminance == p_enabled) {
3140+
return;
3141+
}
3142+
3143+
wd.hdr_output_auto_adjust_max_luminance = p_enabled;
3144+
3145+
if (wd.hdr_output_auto_adjust_max_luminance) {
3146+
WindowData *wd = windows.getptr(p_window);
3147+
ERR_FAIL_NULL(wd);
3148+
wd->hdr_output_requested = p_enabled;
3149+
_update_hdr_output_for_window(p_window, *wd);
3150+
} else {
3151+
// Apply the previously set maximum luminance.
3152+
#if defined(RD_ENABLED)
3153+
if (rendering_context) {
3154+
rendering_context->window_set_hdr_output_max_luminance(p_window, wd.hdr_output_max_luminance);
3155+
}
3156+
#endif
3157+
}
31163158
}
31173159

31183160
bool DisplayServerMacOS::window_is_hdr_output_auto_adjusting_max_luminance(WindowID p_window) const {
3119-
ERR_FAIL_V_MSG(true, "Not implemented");
3161+
_THREAD_SAFE_METHOD_
3162+
3163+
const WindowData &wd = windows[p_window];
3164+
return wd.hdr_output_auto_adjust_max_luminance;
31203165
}
31213166

31223167
void DisplayServerMacOS::window_set_hdr_output_max_luminance(const float p_max_luminance, WindowID p_window) {
3168+
_THREAD_SAFE_METHOD_
3169+
3170+
ERR_FAIL_COND_MSG(p_max_luminance < 0.0f, "Maximum luminance must be non-negative.");
3171+
3172+
WindowData &wd = windows[p_window];
3173+
wd.hdr_output_max_luminance = p_max_luminance;
3174+
3175+
// If the maximum luminance is set to auto-adjust, don't apply it.
3176+
if (wd.hdr_output_auto_adjust_max_luminance) {
3177+
return;
3178+
}
3179+
3180+
#if defined(RD_ENABLED)
3181+
if (rendering_context) {
3182+
rendering_context->window_set_hdr_output_max_luminance(p_window, p_max_luminance);
3183+
}
3184+
#endif
31233185
}
31243186

31253187
float DisplayServerMacOS::window_get_hdr_output_max_luminance(WindowID p_window) const {
@@ -3134,9 +3196,15 @@
31343196
}
31353197

31363198
float DisplayServerMacOS::window_get_output_max_value(WindowID p_window) const {
3137-
// allenwp: temporary rebase hack (this should return 1.0 when HDR is disabled.
3138-
// Also, this whole function should probably be handled in a driver independent way.
3139-
return window_get_hdr_output_max_luminance(p_window) / window_get_hdr_output_reference_luminance(p_window);
3199+
_THREAD_SAFE_METHOD_
3200+
3201+
#if defined(RD_ENABLED)
3202+
if (rendering_context) {
3203+
return rendering_context->window_get_output_max_value(p_window);
3204+
}
3205+
#endif
3206+
3207+
return 1.0f; // SDR
31403208
}
31413209

31423210
int DisplayServerMacOS::accessibility_should_increase_contrast() const {

servers/rendering/renderer_rd/renderer_compositor_rd.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ float RendererCompositorRD::_compute_reference_multiplier(RD::ColorSpace p_color
205205
// Windows expects multiples of 80 nits.
206206
return p_reference_luminance / 80.0f;
207207
#else
208-
// Default to 100 nits.
209-
return p_reference_luminance / 100.0f;
208+
return 1.0f;
210209
#endif
211210
default:
212211
return 1.0f;

0 commit comments

Comments
 (0)
X Tutup