SDL: Fixed HDR properties on Wayland

From 1ad33adf1ad7282a8cddece8adb1ebc68433cd2c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 16 Oct 2026 08:59:50 -0700
Subject: [PATCH] Fixed HDR properties on Wayland

Make sure that the HDR headroom is always valid.

The SDR white level is more complicated and driver and swap chain dependent, so we set that to 1.0 for the window and will have more advanced logic in the renderer. For now we'll use a reference white value of 203 nits, which is correct for HDR10 and for linear swapchains on NVIDIA drivers. Mesa uses a reference white value of 80 nits, so linear swapchains will be ~2.5 times too bright. We'll eventually fix this by using VK_COLOR_SPACE_PASS_THROUGH_EXT and defining our own reference white value.
---
 src/render/SDL_render.c              | 12 +++++++++++-
 src/video/wayland/SDL_waylandcolor.c |  8 ++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 7004c78da244c..2501b96c7e9ea 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -858,7 +858,17 @@ static void UpdateHDRProperties(SDL_Renderer *renderer)
 
     if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR ||
         renderer->output_colorspace == SDL_COLORSPACE_HDR10) {
-        renderer->SDR_white_point = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_SDR_WHITE_LEVEL_FLOAT, 1.0f);
+        if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) {
+            // The Vulkan drivers don't set luminance for HDR10 swapchains, so the
+            // protocol default reference white of 203 nits is used. For linear
+            // swapchains, NVIDIA uses a reference white of 203 nits and Mesa uses
+            // 80 nits. Eventually we'll use VK_COLOR_SPACE_PASS_THROUGH_EXT and
+            // define the reference white ourselves, so always use 203 for now.
+            const float SDR_reference_white_nits = 203.0f;
+            renderer->SDR_white_point = SDR_reference_white_nits / SCRGB_NITS;
+        } else {
+            renderer->SDR_white_point = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_SDR_WHITE_LEVEL_FLOAT, 1.0f);
+        }
         renderer->HDR_headroom = SDL_GetFloatProperty(window_props, SDL_PROP_WINDOW_HDR_HEADROOM_FLOAT, 1.0f);
     } else {
         renderer->SDR_white_point = 1.0f;
diff --git a/src/video/wayland/SDL_waylandcolor.c b/src/video/wayland/SDL_waylandcolor.c
index fb7934bf319b2..95f18e63452db 100644
--- a/src/video/wayland/SDL_waylandcolor.c
+++ b/src/video/wayland/SDL_waylandcolor.c
@@ -171,8 +171,12 @@ static void image_description_info_handle_luminances(void *data,
                                                      uint32_t reference_lum)
 {
     Wayland_ColorInfoState *state = (Wayland_ColorInfoState *)data;
-    state->HDR.HDR_headroom = (float)max_lum / (float)reference_lum;
-    state->HDR.SDR_white_level = (float)reference_lum / 80.0f;
+    if (max_lum > reference_lum && reference_lum > 0) {
+        state->HDR.HDR_headroom = (float)max_lum / (float)reference_lum;
+    } else {
+        state->HDR.HDR_headroom = 1.0f;
+    }
+    state->HDR.SDR_white_level = 1.0f;
 }
 
 static void image_description_info_handle_target_primaries(void *data,