SDL: Better check to see whether HDR10 input will have scaling applied

From 0f5f708c5a57daea39eb5ac34f2944b00f927f36 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 23 Jul 2026 12:55:14 -0700
Subject: [PATCH] Better check to see whether HDR10 input will have scaling
 applied

---
 src/render/direct3d11/SDL_render_d3d11.c | 18 ++++++++++++++++--
 src/render/direct3d12/SDL_render_d3d12.c | 18 ++++++++++++++++--
 src/render/vulkan/SDL_render_vulkan.c    | 18 ++++++++++++++++--
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index 9ec760e25dab1..0128ff793acaa 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -2282,13 +2282,27 @@ static void D3D11_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
     }
 }
 
+static bool PQShaderScalesInput(const D3D11_PixelShaderConstants *shader_constants)
+{
+    if (shader_constants->tonemap_method != 0.0f) {
+        // Tone mapping always scales
+        return true;
+    }
+
+    // The shader normalizes the PQ input using the SDR white point and then multiplies by the color scale
+    if (SDL_fabs((shader_constants->sdr_white_point - (shader_constants->color_scale * SCRGB_NITS))) > 1.0f) {
+        return true;
+    }
+
+    return false;
+}
+
 static D3D11_Shader SelectShader(SDL_Renderer *renderer, const D3D11_PixelShaderConstants *shader_constants)
 {
     if (shader_constants) {
         if (renderer->current_colorspace == SDL_COLORSPACE_HDR10) {
             if (shader_constants->input_type == INPUTTYPE_HDR10 &&
-                shader_constants->tonemap_method == 0.0f &&
-                (shader_constants->sdr_white_point / SCRGB_NITS) == shader_constants->color_scale) {
+                !PQShaderScalesInput(shader_constants)) {
                 // Do a simple 1-1 copy
                 return SHADER_RGB_SIMPLE;
             } else {
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index df4acc8a29e35..b6e7c2ef8b6e0 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -2742,13 +2742,27 @@ static void D3D12_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
     }
 }
 
+static bool PQShaderScalesInput(const D3D12_PixelShaderConstants *shader_constants)
+{
+    if (shader_constants->tonemap_method != 0.0f) {
+        // Tone mapping always scales
+        return true;
+    }
+
+    // The shader normalizes the PQ input using the SDR white point and then multiplies by the color scale
+    if (SDL_fabs((shader_constants->sdr_white_point - (shader_constants->color_scale * SCRGB_NITS))) > 1.0f) {
+        return true;
+    }
+
+    return false;
+}
+
 static D3D12_Shader SelectShader(SDL_Renderer *renderer, const D3D12_PixelShaderConstants *shader_constants)
 {
     if (shader_constants) {
         if (renderer->current_colorspace == SDL_COLORSPACE_HDR10) {
             if (shader_constants->input_type == INPUTTYPE_HDR10 &&
-                shader_constants->tonemap_method == 0.0f &&
-                (shader_constants->sdr_white_point / SCRGB_NITS) == shader_constants->color_scale) {
+                !PQShaderScalesInput(shader_constants)) {
                 // Do a simple 1-1 copy
                 return SHADER_RGB_SIMPLE;
             } else {
diff --git a/src/render/vulkan/SDL_render_vulkan.c b/src/render/vulkan/SDL_render_vulkan.c
index cf3e77b9bc002..e75e807d25a49 100644
--- a/src/render/vulkan/SDL_render_vulkan.c
+++ b/src/render/vulkan/SDL_render_vulkan.c
@@ -3554,13 +3554,27 @@ static void VULKAN_SetupShaderConstants(SDL_Renderer *renderer, const SDL_Render
     }
 }
 
+static bool PQShaderScalesInput(const VULKAN_PixelShaderConstants *shader_constants)
+{
+    if (shader_constants->tonemap_method != 0.0f) {
+        // Tone mapping always scales
+        return true;
+    }
+
+    // The shader normalizes the PQ input using the SDR white point and then multiplies by the color scale
+    if (SDL_fabs((shader_constants->sdr_white_point - (shader_constants->color_scale * SCRGB_NITS))) > 1.0f) {
+        return true;
+    }
+
+    return false;
+}
+
 static VULKAN_Shader SelectShader(SDL_Renderer *renderer, const VULKAN_PixelShaderConstants *shader_constants, bool yuv)
 {
     if (shader_constants) {
         if (renderer->current_colorspace == SDL_COLORSPACE_HDR10) {
             if (shader_constants->input_type == INPUTTYPE_HDR10 &&
-                shader_constants->tonemap_method == 0.0f &&
-                (shader_constants->sdr_white_point / SCRGB_NITS) == shader_constants->color_scale) {
+                !PQShaderScalesInput(shader_constants)) {
                 // Do a simple 1-1 copy
                 return SHADER_RGB_SIMPLE;
             } else {