SDL: Scale SDR content to SDR whitelevel when HDR is enabled

From 240158f3e80dfaacd15d5e10fa25cafcf88b9a20 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 5 Feb 2024 10:23:25 -0800
Subject: [PATCH] Scale SDR content to SDR whitelevel when HDR is enabled

---
 .../direct3d11/D3D11_PixelShader_Colors.h     |  426 ++++---
 .../direct3d11/D3D11_PixelShader_Colors.hlsl  |   10 +-
 .../direct3d11/D3D11_PixelShader_Common.incl  |   75 ++
 .../direct3d11/D3D11_PixelShader_HDR10.h      | 1030 ++++++++---------
 .../direct3d11/D3D11_PixelShader_HDR10.hlsl   |   40 +-
 .../direct3d11/D3D11_PixelShader_NV12.h       |  884 ++++++++------
 .../direct3d11/D3D11_PixelShader_NV12.hlsl    |   26 +-
 .../direct3d11/D3D11_PixelShader_NV21.h       |  890 ++++++++------
 .../direct3d11/D3D11_PixelShader_NV21.hlsl    |   26 +-
 .../direct3d11/D3D11_PixelShader_Textures.h   |  539 +++++----
 .../D3D11_PixelShader_Textures.hlsl           |    9 +-
 src/render/direct3d11/D3D11_PixelShader_YUV.h |  954 +++++++++------
 .../direct3d11/D3D11_PixelShader_YUV.hlsl     |   26 +-
 src/render/direct3d11/D3D11_VertexShader.h    |  678 +++++------
 src/render/direct3d11/SDL_render_d3d11.c      |   92 +-
 15 files changed, 3295 insertions(+), 2410 deletions(-)
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_Colors.h
 create mode 100644 src/render/direct3d11/D3D11_PixelShader_Common.incl
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_HDR10.h
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_NV12.h
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_NV21.h
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_Textures.h
 mode change 100755 => 100644 src/render/direct3d11/D3D11_PixelShader_YUV.h
 mode change 100755 => 100644 src/render/direct3d11/D3D11_VertexShader.h

diff --git a/src/render/direct3d11/D3D11_PixelShader_Colors.h b/src/render/direct3d11/D3D11_PixelShader_Colors.h
old mode 100755
new mode 100644
index 60b5335aa1fe..955c2532dcde
--- a/src/render/direct3d11/D3D11_PixelShader_Colors.h
+++ b/src/render/direct3d11/D3D11_PixelShader_Colors.h
@@ -1,134 +1,292 @@
-#if 0
-//
-// Generated by Microsoft (R) HLSL Shader Compiler 10.1
-//
-//
-//
-// Input signature:
-//
-// Name                 Index   Mask Register SysValue  Format   Used
-// -------------------- ----- ------ -------- -------- ------- ------
-// SV_POSITION              0   xyzw        0      POS   float       
-// TEXCOORD                 0   xy          1     NONE   float       
-// COLOR                    0   xyzw        2     NONE   float   xyzw
-//
-//
-// Output signature:
-//
-// Name                 Index   Mask Register SysValue  Format   Used
-// -------------------- ----- ------ -------- -------- ------- ------
-// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
-//
-//
-// Level9 shader bytecode:
-//
-    ps_2_0
-    dcl t1
-    mov oC0, t1
-
-// approximately 1 instruction slot used
-ps_4_0
-dcl_input_ps linear v2.xyzw
-dcl_output o0.xyzw
-mov o0.xyzw, v2.xyzw
-ret 
-// Approximately 2 instruction slots used
-#endif
-
-const BYTE g_main[] =
-{
-     68,  88,  66,  67, 226, 124, 
-     10,  63, 141, 181,  49, 167, 
-    218, 135, 248, 147,  77, 222, 
-     95, 123,   1,   0,   0,   0, 
-     52,   2,   0,   0,   6,   0, 
-      0,   0,  56,   0,   0,   0, 
-    132,   0,   0,   0, 196,   0, 
-      0,   0,  64,   1,   0,   0, 
-    140,   1,   0,   0,   0,   2, 
-      0,   0,  65, 111, 110,  57, 
-     68,   0,   0,   0,  68,   0, 
-      0,   0,   0,   2, 255, 255, 
-     32,   0,   0,   0,  36,   0, 
-      0,   0,   0,   0,  36,   0, 
-      0,   0,  36,   0,   0,   0, 
-     36,   0,   0,   0,  36,   0, 
-      0,   0,  36,   0,   0,   2, 
-    255, 255,  31,   0,   0,   2, 
-      0,   0,   0, 128,   1,   0, 
-     15, 176,   1,   0,   0,   2, 
-      0,   8,  15, 128,   1,   0, 
-    228, 176, 255, 255,   0,   0, 
-     83,  72,  68,  82,  56,   0, 
-      0,   0,  64,   0,   0,   0, 
-     14,   0,   0,   0,  98,  16, 
-      0,   3, 242,  16,  16,   0, 
-      2,   0,   0,   0, 101,   0, 
-      0,   3, 242,  32,  16,   0, 
-      0,   0,   0,   0,  54,   0, 
-      0,   5, 242,  32,  16,   0, 
-      0,   0,   0,   0,  70,  30, 
-     16,   0,   2,   0,   0,   0, 
-     62,   0,   0,   1,  83,  84, 
-     65,  84, 116,   0,   0,   0, 
-      2,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      2,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   1,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   1,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,  82,  68,  69,  70, 
-     68,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,  28,   0, 
-      0,   0,   0,   4, 255, 255, 
-      0,   1,   0,   0,  28,   0, 
-      0,   0,  77, 105,  99, 114, 
-    111, 115, 111, 102, 116,  32, 
-     40,  82,  41,  32,  72,  76, 
-     83,  76,  32,  83, 104,  97, 
-    100, 101, 114,  32,  67, 111, 
-    109, 112, 105, 108, 101, 114, 
-     32,  49,  48,  46,  49,   0, 
-     73,  83,  71,  78, 108,   0, 
-      0,   0,   3,   0,   0,   0, 
-      8,   0,   0,   0,  80,   0, 
-      0,   0,   0,   0,   0,   0, 
-      1,   0,   0,   0,   3,   0, 
-      0,   0,   0,   0,   0,   0, 
-     15,   0,   0,   0,  92,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   3,   0, 
-      0,   0,   1,   0,   0,   0, 
-      3,   0,   0,   0, 101,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   3,   0, 
-      0,   0,   2,   0,   0,   0, 
-     15,  15,   0,   0,  83,  86, 
-     95,  80,  79,  83,  73,  84, 
-     73,  79,  78,   0,  84,  69, 
-     88,  67,  79,  79,  82,  68, 
-      0,  67,  79,  76,  79,  82, 
-      0, 171,  79,  83,  71,  78, 
-     44,   0,   0,   0,   1,   0, 
-      0,   0,   8,   0,   0,   0, 
-     32,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      3,   0,   0,   0,   0,   0, 
-      0,   0,  15,   0,   0,   0, 
-     83,  86,  95,  84,  65,  82, 
-     71,  69,  84,   0, 171, 171
-};
+#if 0
+//
+// Generated by Microsoft (R) HLSL Shader Compiler 10.1
+//
+//
+// Buffer Definitions: 
+//
+// cbuffer Constants
+// {
+//
+//   float scRGB_output;                // Offset:    0 Size:     4
+//   float SDR_whitelevel;              // Offset:    4 Size:     4
+//   float HDR_whitelevel;              // Offset:    8 Size:     4 [unused]
+//   float maxCLL;                      // Offset:   12 Size:     4 [unused]
+//   float4 Yoffset;                    // Offset:   16 Size:    16 [unused]
+//   float4 Rcoeff;                     // Offset:   32 Size:    16 [unused]
+//   float4 Gcoeff;                     // Offset:   48 Size:    16 [unused]
+//   float4 Bcoeff;                     // Offset:   64 Size:    16 [unused]
+//
+// }
+//
+//
+// Resource Bindings:
+//
+// Name                                 Type  Format         Dim      HLSL Bind  Count
+// ------------------------------ ---------- ------- ----------- -------------- ------
+// Constants                         cbuffer      NA          NA            cb0      1 
+//
+//
+//
+// Input signature:
+//
+// Name                 Index   Mask Register SysValue  Format   Used
+// -------------------- ----- ------ -------- -------- ------- ------
+// SV_POSITION              0   xyzw        0      POS   float       
+// TEXCOORD                 0   xy          1     NONE   float       
+// COLOR                    0   xyzw        2     NONE   float   xyzw
+//
+//
+// Output signature:
+//
+// Name                 Index   Mask Register SysValue  Format   Used
+// -------------------- ----- ------ -------- -------- ------- ------
+// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
+//
+//
+// Constant buffer to DX9 shader constant mappings:
+//
+// Target Reg Buffer  Start Reg # of Regs        Data Conversion
+// ---------- ------- --------- --------- ----------------------
+// c0         cb0             0         1  ( FLT, FLT, FLT, FLT)
+//
+//
+// Level9 shader bytecode:
+//
+    ps_2_0
+    def c1, 0.0125000002, 1, 0, 0
+    dcl t1
+    mul r0.w, c0.x, c0.x
+    mov r0.x, c1.x
+    mul r0.x, r0.x, c0.y
+    cmp r0.xyz, -r0.w, c1.y, r0.x
+    mov r0.w, c1.y
+    mul r0, r0, t1
+    mov oC0, r0
+
+// approximately 7 instruction slots used
+ps_4_0
+dcl_constantbuffer CB0[1], immediateIndexed
+dcl_input_ps linear v2.xyzw
+dcl_output o0.xyzw
+dcl_temps 1
+ne r0.x, l(0.000000, 0.000000, 0.000000, 0.000000), cb0[0].x
+mul r0.y, cb0[0].y, l(0.012500)
+movc r0.xyz, r0.xxxx, r0.yyyy, l(1.000000,1.000000,1.000000,0)
+mov r0.w, l(1.000000)
+mul o0.xyzw, r0.xyzw, v2.xyzw
+ret 
+// Approximately 6 instruction slots used
+#endif
+
+const BYTE g_main[] =
+{
+     68,  88,  66,  67, 132,  13, 
+    165,  35,  17, 157, 163, 217, 
+    158,  71, 117, 171,  46, 252, 
+      9, 215,   1,   0,   0,   0, 
+    224,   4,   0,   0,   6,   0, 
+      0,   0,  56,   0,   0,   0, 
+      4,   1,   0,   0, 244,   1, 
+      0,   0, 112,   2,   0,   0, 
+     56,   4,   0,   0, 172,   4, 
+      0,   0,  65, 111, 110,  57, 
+    196,   0,   0,   0, 196,   0, 
+      0,   0,   0,   2, 255, 255, 
+    148,   0,   0,   0,  48,   0, 
+      0,   0,   1,   0,  36,   0, 
+      0,   0,  48,   0,   0,   0, 
+     48,   0,   0,   0,  36,   0, 
+      0,   0,  48,   0,   0,   0, 
+      0,   0,   1,   0,   0,   0, 
+      0,   0,   0,   0,   0,   2, 
+    255, 255,  81,   0,   0,   5, 
+      1,   0,  15, 160, 205, 204, 
+     76,  60,   0,   0, 128,  63, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,  31,   0,   0,   2, 
+      0,   0,   0, 128,   1,   0, 
+     15, 176,   5,   0,   0,   3, 
+      0,   0,   8, 128,   0,   0, 
+      0, 160,   0,   0,   0, 160, 
+      1,   0,   0,   2,   0,   0, 
+      1, 128,   1,   0,   0, 160, 
+      5,   0,   0,   3,   0,   0, 
+      1, 128,   0,   0,   0, 128, 
+      0,   0,  85, 160,  88,   0, 
+      0,   4,   0,   0,   7, 128, 
+      0,   0, 255, 129,   1,   0, 
+     85, 160,   0,   0,   0, 128, 
+      1,   0,   0,   2,   0,   0, 
+      8, 128,   1,   0,  85, 160, 
+      5,   0,   0,   3,   0,   0, 
+     15, 128,   0,   0, 228, 128, 
+      1,   0, 228, 176,   1,   0, 
+      0,   2,   0,   8,  15, 128, 
+      0,   0, 228, 128, 255, 255, 
+      0,   0,  83,  72,  68,  82, 
+    232,   0,   0,   0,  64,   0, 
+      0,   0,  58,   0,   0,   0, 
+     89,   0,   0,   4,  70, 142, 
+     32,   0,   0,   0,   0,   0, 
+      1,   0,   0,   0,  98,  16, 
+      0,   3, 242,  16,  16,   0, 
+      2,   0,   0,   0, 101,   0, 
+      0,   3, 242,  32,  16,   0, 
+      0,   0,   0,   0, 104,   0, 
+      0,   2,   1,   0,   0,   0, 
+     57,   0,   0,  11,  18,   0, 
+     16,   0,   0,   0,   0,   0, 
+      2,  64,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,  10, 128,  32,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,  56,   0,   0,   8, 
+     34,   0,  16,   0,   0,   0, 
+      0,   0,  26, 128,  32,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   1,  64,   0,   0, 
+    205, 204,  76,  60,  55,   0, 
+      0,  12, 114,   0,  16,   0, 
+      0,   0,   0,   0,   6,   0, 
+     16,   0,   0,   0,   0,   0, 
+     86,   5,  16,   0,   0,   0, 
+      0,   0,   2,  64,   0,   0, 
+      0,   0, 128,  63,   0,   0, 
+    128,  63,   0,   0, 128,  63, 
+      0,   0,   0,   0,  54,   0, 
+      0,   5, 130,   0,  16,   0, 
+      0,   0,   0,   0,   1,  64, 
+      0,   0,   0,   0, 128,  63, 
+     56,   0,   0,   7, 242,  32, 
+     16,   0,   0,   0,   0,   0, 
+     70,  14,  16,   0,   0,   0, 
+      0,   0,  70,  30,  16,   0, 
+      2,   0,   0,   0,  62,   0, 
+      0,   1,  83,  84,  65,  84, 
+    116,   0,   0,   0,   6,   0, 
+      0,   0,   1,   0,   0,   0, 
+      0,   0,   0,   0,   2,   0, 
+      0,   0,   2,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   1,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   1,   0,   0,   0, 
+      1,   0,   0,   0,   1,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+     82,  68,  69,  70, 192,   1, 
+      0,   0,   1,   0,   0,   0, 
+     72,   0,   0,   0,   1,   0, 
+      0,   0,  28,   0,   0,   0, 
+      0,   4, 255, 255,   0,   1, 
+      0,   0, 149,   1,   0,   0, 
+     60,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      1,   0,   0,   0,   1,   0, 
+      0,   0,  67, 111, 110, 115, 
+    116,  97, 110, 116, 115,   0, 
+    171, 171,  60,   0,   0,   0, 
+      8,   0,   0,   0,  96,   0, 
+      0,   0,  80,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,  32,   1,   0,   0, 
+      0,   0,   0,   0,   4,   0, 
+      0,   0,   2,   0,   0,   0, 
+     48,   1,   0,   0,   0,   0, 
+      0,   0,  64,   1,   0,   0, 
+      4,   0,   0,   0,   4,   0, 
+      0,   0,   2,   0,   0,   0, 
+     48,   1,   0,   0,   0,   0, 
+      0,   0,  79,   1,   0,   0, 
+      8,   0,   0,   0,   4,   0, 
+      0,   0,   0,   0,   0,   0, 
+     48,   1,   0,   0,   0,   0, 
+      0,   0,  94,   1,   0,   0, 
+     12,   0,   0,   0,   4,   0, 
+      0,   0,   0,   0,   0,   0, 
+     48,   1,   0,   0,   0,   0, 
+      0,   0, 101,   1,   0,   0, 
+     16,   0,   0,   0,  16,   0, 
+      0,   0,   0,   0,   0,   0, 
+    112,   1,   0,   0,   0,   0, 
+      0,   0, 128,   1,   0,   0, 
+     32,   0,   0,   0,  16,   0, 
+      0,   0,   0,   0,   0,   0, 
+    112,   1,   0,   0,   0,   0, 
+      0,   0, 135,   1,   0,   0, 
+     48,   0,   0,   0,  16,   0, 
+      0,   0,   0,   0,   0,   0, 
+    112,   1,   0,   0,   0,   0, 
+      0,   0, 142,   1,   0,   0, 
+     64,   0,   0,   0,  16,   0, 
+      0,   0,   0,   0,   0,   0, 
+    112,   1,   0,   0,   0,   0, 
+      0,   0, 115,  99,  82,  71, 
+     66,  95, 111, 117, 116, 112, 
+    117, 116,   0, 171, 171, 171, 
+      0,   0,   3,   0,   1,   0, 
+      1,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,  83,  68, 
+     82,  95, 119, 104, 105, 116, 
+    101, 108, 101, 118, 101, 108, 
+      0,  72,  68,  82,  95, 119, 
+    104, 105, 116, 101, 108, 101, 
+    118, 101, 108,   0, 109,  97, 
+    120,  67,  76,  76,   0,  89, 
+    111, 102, 102, 115, 101, 116, 
+      0, 171, 171, 171,   1,   0, 
+      3,   0,   1,   0,   4,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,  82,  99, 111, 101, 
+    102, 102,   0,  71,  99, 111, 
+    101, 102, 102,   0,  66,  99, 
+    111, 101, 102, 102,   0,  77, 
+    105,  99, 114, 111, 115, 111, 
+    102, 116,  32,  40,  82,  41, 
+     32,  72,  76,  83,  76,  32, 
+     83, 104,  97, 100, 101, 114, 
+     32,  67, 111, 109, 112, 105, 
+    108, 101, 114,  32,  49,  48, 
+     46,  49,   0, 171, 171, 171, 
+     73,  83,  71,  78, 108,   0, 
+      0,   0,   3,   0,   0,   0, 
+      8,   0,   0,   0,  80,   0, 
+      0,   0,   0,   0,   0,   0, 
+      1,   0,   0,   0,   3,   0, 
+      0,   0,   0,   0,   0,   0, 
+     15,   0,   0,   0,  92,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   3,   0, 
+      0,   0,   1,   0,   0,   0, 
+      3,   0,   0,   0, 101,   0, 
+      0,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   3,   0, 
+      0,   0,   2,   0,   0,   0, 
+     15,  15,   0,   0,  83,  86, 
+     95,  80,  79,  83,  73,  84, 
+     73,  79,  78,   0,  84,  69, 
+     88,  67,  79,  79,  82,  68, 
+      0,  67,  79,  76,  79,  82, 
+      0, 171,  79,  83,  71,  78, 
+     44,   0,   0,   0,   1,   0, 
+      0,   0,   8,   0,   0,   0, 
+     32,   0,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
+      3,   0,   0,   0,   0,   0, 
+      0,   0,  15,   0,   0,   0, 
+     83,  86,  95,  84,  65,  82, 
+     71,  69,  84,   0, 171, 171
+};
diff --git a/src/render/direct3d11/D3D11_PixelShader_Colors.hlsl b/src/render/direct3d11/D3D11_PixelShader_Colors.hlsl
index 9a0c1b34fba1..b8a26e917732 100644
--- a/src/render/direct3d11/D3D11_PixelShader_Colors.hlsl
+++ b/src/render/direct3d11/D3D11_PixelShader_Colors.hlsl
@@ -1,11 +1,7 @@
-struct PixelShaderInput
-{
-    float4 pos : SV_POSITION;
-    float2 tex : TEXCOORD0;
-    float4 color : COLOR0;
-};
+
+#include "D3D11_PixelShader_Common.incl"
 
 float4 main(PixelShaderInput input) : SV_TARGET
 {
-    return input.color;
+    return GetOutputColor(1.0) * input.color;
 }
diff --git a/src/render/direct3d11/D3D11_PixelShader_Common.incl b/src/render/direct3d11/D3D11_PixelShader_Common.incl
new file mode 100644
index 000000000000..2b547c7b5247
--- /dev/null
+++ b/src/render/direct3d11/D3D11_PixelShader_Common.incl
@@ -0,0 +1,75 @@
+struct PixelShaderInput
+{
+    float4 pos : SV_POSITION;
+    float2 tex : TEXCOORD0;
+    float4 color : COLOR0;
+};
+
+cbuffer Constants : register(b0)
+{
+    float scRGB_output;
+    float SDR_whitelevel;
+    float HDR_whitelevel;
+    float maxCLL;
+
+    float4 Yoffset;
+    float4 Rcoeff;
+    float4 Gcoeff;
+    float4 Bcoeff;
+};
+
+float3 scRGBtoNits(float3 v)
+{
+    return v * 80.0;
+}
+
+float3 scRGBfromNits(float3 v)
+{
+    return v / 80.0;
+}
+
+float sRGBtoLinear(float v)
+{
+    if (v <= 0.04045) {
+        v = (v / 12.92);
+    } else {
+        v = pow(abs(v + 0.055) / 1.055, 2.4);
+    }
+    return v;
+}
+
+float sRGBfromLinear(float v)
+{
+    if (v <= 0.0031308) {
+        v = (v * 12.92);
+    } else {
+        v = (pow(abs(v), 1.0 / 2.4) * 1.055 - 0.055);
+    }
+    return v;
+}
+
+float4 GetOutputColor(float4 rgba)
+{
+    if (scRGB_output) {
+        rgba.rgb = scRGBfromNits(rgba.rgb * SDR_whitelevel);
+    }
+
+    return rgba;
+}
+
+float4 GetOutputColorFromSRGB(float3 rgb)
+{
+    float4 output;
+
+    if (scRGB_output) {
+        output.r = sRGBtoLinear(rgb.r);
+        output.g = sRGBtoLinear(rgb.g);
+        output.b = sRGBtoLinear(rgb.b);
+        rgb = scRGBfromNits(rgb * SDR_whitelevel);
+    } else {
+        output.rgb = rgb.rgb;
+    }
+    output.a = 1.0;
+
+    return output;
+}
diff --git a/src/render/direct3d11/D3D11_PixelShader_HDR10.h b/src/render/direct3d11/D3D11_PixelShader_HDR10.h
old mode 100755
new mode 100644
index afd6772f6b9d..c71c13a0a69f
--- a/src/render/direct3d11/D3D11_PixelShader_HDR10.h
+++ b/src/render/direct3d11/D3D11_PixelShader_HDR10.h
@@ -1,515 +1,515 @@
-#if 0
-//
-// Generated by Microsoft (R) HLSL Shader Compiler 10.1
-//
-//
-// Buffer Definitions: 
-//
-// cbuffer Constants
-// {
-//
-//   float4 Yoffset;                    // Offset:    0 Size:    16
-//   float4 Rcoeff;                     // Offset:   16 Size:    16
-//   float4 Gcoeff;                     // Offset:   32 Size:    16
-//   float4 Bcoeff;                     // Offset:   48 Size:    16
-//   float scRGB_output;                // Offset:   64 Size:     4
-//   float SDR_whitelevel;              // Offset:   68 Size:     4 [unused]
-//   float HDR_whitelevel;              // Offset:   72 Size:     4
-//   float maxCLL;                      // Offset:   76 Size:     4
-//
-// }
-//
-//
-// Resource Bindings:
-//
-// Name                                 Type  Format         Dim      HLSL Bind  Count
-// ------------------------------ ---------- ------- ----------- -------------- ------
-// theSampler                        sampler      NA          NA             s0      1 
-// theTextureY                       texture  float4          2d             t0      1 
-// theTextureUV                      texture  float4          2d             t1      1 
-// Constants                         cbuffer      NA          NA            cb0      1 
-//
-//
-//
-// Input signature:
-//
-// Name                 Index   Mask Register SysValue  Format   Used
-// -------------------- ----- ------ -------- -------- ------- ------
-// SV_POSITION              0   xyzw        0      POS   float       
-// TEXCOORD                 0   xy          1     NONE   float   xy  
-// COLOR                    0   xyzw        2     NONE   float   xyzw
-//
-//
-// Output signature:
-//
-// Name                 Index   Mask Register SysValue  Format   Used
-// -------------------- ----- ------ -------- -------- ------- ------
-// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
-//
-ps_5_0
-dcl_globalFlags refactoringAllowed
-dcl_constantbuffer CB0[5], immediateIndexed
-dcl_sampler s0, mode_default
-dcl_resource_texture2d (float,float,float,float) t0
-dcl_resource_texture2d (float,float,float,float) t1
-dcl_input_ps linear v1.xy
-dcl_input_ps linear v2.xyzw
-dcl_output o0.xyzw
-dcl_temps 4
-sample_indexable(texture2d)(float,float,float,float) r0.x, v1.xyxx, t0.xyzw, s0
-sample_indexable(texture2d)(float,float,float,float) r0.yz, v1.xyxx, t1.zxyw, s0
-add r0.xyz, r0.xyzx, cb0[0].xyzx
-dp3 r1.x, r0.xyzx, cb0[1].xyzx
-dp3 r1.y, r0.xyzx, cb0[2].xyzx
-dp3 r1.z, r0.xyzx, cb0[3].xyzx
-log r0.xyz, |r1.xyzx|
-mul r0.xyz, r0.xyzx, l(0.012683, 0.012683, 0.012683, 0.000000)
-exp r0.xyz, r0.xyzx
-add r1.xyz, r0.xyzx, l(-0.835938, -0.835938, -0.835938, 0.000000)
-max r1.xyz, r1.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
-mad r0.xyz, -r0.xyzx, l(18.687500, 18.687500, 18.687500, 0.000000), l(18.851562, 18.851562, 18.851562, 0.000000)
-div r0.xyz, r1.xyzx, r0.xyzx
-log r0.xyz, |r0.xyzx|
-mul r0.xyz, r0.xyzx, l(6.277395, 6.277395, 6.277395, 0.000000)
-exp r0.xyz, r0.xyzx
-mul r0.xyz, r0.xyzx, l(10000.000000, 10000.000000, 10000.000000, 0.000000)
-dp3 r1.x, l(1.660496, -0.587656, -0.072840, 0.000000), r0.xyzx
-dp3 r1.y, l(-0.124547, 1.132895, -0.008348, 0.000000), r0.xyzx
-dp3 r1.z, l(-0.018154, -0.100597, 1.118751, 0.000000), r0.xyzx
-div r0.xyz, r1.xyzx, cb0[4].wwww
-mul r0.xyz, r0.xyzx, cb0[4].zzzz
-mul r1.xyz, r0.xyzx, l(0.012500, 0.012500, 0.012500, 0.000000)
-ne r0.w, l(0.000000, 0.000000, 0.000000, 0.000000), cb0[4].x
-if_z r0.w
-  ge r2.xyz, l(0.250464, 0.250464, 0.250464, 0.000000), r0.xyzx
-  mul r0.xyz, r0.xyzx, l(0.161500, 0.161500, 0.161500, 0.000000)
-  log r3.xyz, |r1.xyzx|
-  mul r3.xyz, r3.xyzx, l(0.416667, 0.416667, 0.416667, 0.000000)
-  exp r3.xyz, r3.xyzx
-  mad r3.xyz, r3.xyzx, l(1.055000, 1.055000, 1.055000, 0.000000), l(-0.055000, -0.055000, -0.055000, 0.000000)
-  movc_sat r1.xyz, r2.xyzx, r0.xyzx, r3.xyzx
-endif 
-mov r1.w, l(1.000000)
-mul o0.xyzw, r1.xyzw, v2.xyzw
-ret 
-// Approximately 36 instruction slots used
-#endif
-
-const BYTE g_main[] =
-{
-     68,  88,  66,  67, 110, 205, 
-    144, 108,   2, 231, 108, 209, 
-    128, 137, 101, 210,  29,  69, 
-     41, 122,   1,   0,   0,   0, 
-    184,   9,   0,   0,   5,   0, 
-      0,   0,  52,   0,   0,   0, 
-     80,   3,   0,   0, 196,   3, 
-      0,   0, 248,   3,   0,   0, 
-     28,   9,   0,   0,  82,  68, 
-     69,  70,  20,   3,   0,   0, 
-      1,   0,   0,   0, 236,   0, 
-      0,   0,   4,   0,   0,   0, 
-     60,   0,   0,   0,   0,   5, 
-    255, 255,   0,   1,   0,   0, 
-    233,   2,   0,   0,  82,  68, 
-     49,  49,  60,   0,   0,   0, 
-     24,   0,   0,   0,  32,   0, 
-      0,   0,  40,   0,   0,   0, 
-     36,   0,   0,   0,  12,   0, 
-      0,   0,   0,   0,   0,   0, 
-    188,   0,   0,   0,   3,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      1,   0,   0,   0,   1,   0, 
-      0,   0, 199,   0,   0,   0, 
-      2,   0,   0,   0,   5,   0, 
-      0,   0,   4,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0,   1,   0,   0,   0, 
-     13,   0,   0,   0, 211,   0, 
-      0,   0,   2,   0,   0,   0, 
-      5,   0,   0,   0,   4,   0, 
-      0,   0, 255, 255, 255, 255, 
-      1,   0,   0,   0,   1,   0, 
-      0,   0,  13,   0,   0,   0, 
-    224,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      1,   0,   0,   0,   1,   0, 
-      0,   0, 116, 104, 101,  83, 
-     97, 109, 112, 108, 101, 114, 
-      0, 116, 104, 101,  84, 101, 
-    120, 116, 117, 114, 101,  89, 
-      0, 116, 104, 101,  84, 101, 
-    120, 116, 117, 114, 101,  85, 
-     86,   0,  67, 111, 110, 115, 
-    116,  97, 110, 116, 115,   0, 
-    171, 171, 224,   0,   0,   0, 
-      8,   0,   0,   0,   4,   1, 
-      0,   0,  80,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,  68,   2,   0,   0, 
-      0,   0,   0,   0,  16,   0, 
-      0,   0,   2,   0,   0,   0, 
-     84,   2,   0,   0,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0, 255, 255, 
-    255, 255,   0,   0,   0,   0, 
-    120,   2,   0,   0,  16,   0, 
-      0,   0,  16,   0,   0,   0, 
-      2,   0,   0,   0,  84,   2, 
-      0,   0,   0,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0, 127,   2, 
-      0,   0,  32,   0,   0,   0, 
-     16,   0,   0,   0,   2,   0, 
-      0,   0,  84,   2,   0,   0, 
-      0,   0,   0,   0, 255, 255, 
-    255, 255,   0,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0, 134,   2,   0,   0, 
-     48,   0,   0,   0,  16,   0, 
-      0,   0,   2,   0,   0,   0, 
-     84,   2,   0,   0,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0, 255, 255, 
-    255, 255,   0,   0,   0,   0, 
-    141,   2,   0,   0,  64,   0, 
-      0,   0,   4,   0,   0,   0, 
-      2,   0,   0,   0, 160,   2, 
-      0,   0,   0,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0, 196,   2, 
-      0,   0,  68,   0,   0,   0, 
-      4,   0,   0,   0,   0,   0, 
-      0,   0, 160,   2,   0,   0, 
-      0,   0,   0,   0, 255, 255, 
-    255, 255,   0,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0, 211,   2,   0,   0, 
-     72,   0,   0,   0,   4,   0, 
-      0,   0,   2,   0,   0,   0, 
-    160,   2,   0,   0,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0, 255, 255, 
-    255, 255,   0,   0,   0,   0, 
-    226,   2,   0,   0,  76,   0, 
-      0,   0,   4,   0,   0,   0, 
-      2,   0,   0,   0, 160,   2, 
-      0,   0,   0,   0,   0,   0, 
-    255, 255, 255, 255,   0,   0, 
-      0,   0, 255, 255, 255, 255, 
-      0,   0,   0,   0,  89, 111, 
-    102, 102, 115, 101, 116,   0, 
-    102, 108, 111,  97, 116,  52, 
-      0, 171,   1,   0,   3,   0, 
-      1,   0,   4,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,  76,   2, 
-      0,   0,  82,  99, 111, 101, 
-    102, 102,   0,  71,  99, 111, 
-    101, 102, 102,   0,  66,  99, 
-    111, 101, 102, 102,   0, 115, 
-     99,  82,  71,  66,  95, 111, 
-    117, 116, 112, 117, 116,   0, 
-    102, 108, 111,  97, 116,   0, 
-      0,   0,   3,   0,   1,   0, 
-      1,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0, 154,   2,   0,   0, 
-     83,  68,  82,  95, 119, 104, 
-    105, 116, 101, 108, 101, 118, 
-    101, 108,   0,  72,  68,  82, 
-     95, 119, 104, 105, 116, 101, 
-    108, 101, 118, 101, 108,   0, 
-    109,  97, 120,  67,  76,  76, 
-      0,  77, 105,  99, 114, 111, 
-    115, 111, 102, 116,  32,  40, 
-     82,  41,  32,  72,  76,  83, 
-     76,  32,  83, 104,  97, 100, 
-    101, 114,  32,  67, 111, 109, 
-    112, 105, 108, 101, 114,  32, 
-     49,  48,  46,  49,   0, 171, 
-    171, 171,  73,  83,  71,  78, 
-    108,   0,   0,   0,   3,   0, 
-      0,   0,   8,   0,   0,   0, 
-     80,   0,   0,   0,   0,   0, 
-      0,   0,   1,   0,   0,   0, 
-      3,   0,   0,   0,   0,   0, 
-      0,   0,  15,   0,   0,   0, 
-     92,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      3,   0,   0,   0,   1,   0, 
-      0,   0,   3,   3,   0,   0, 
-    101,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      3,   0,   0,   0,   2,   0, 
-      0,   0,  15,  15,   0,   0, 
-     83,  86,  95,  80,  79,  83, 
-     73,  84,  73,  79,  78,   0, 
-     84,  69,  88,  67,  79,  79, 
-     82,  68,   0,  67,  79,  76, 
-     79,  82,   0, 171,  79,  83, 
-     71,  78,  44,   0,   0,   0, 
-      1,   0,   0,   0,   8,   0, 
-      0,   0,  32,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   0,   3,   0,   0,   0, 
-      0,   0,   0,   0,  15,   0, 
-      0,   0,  83,  86,  95,  84, 
-     65,  82,  71,  69,  84,   0, 
-    171, 171,  83,  72,  69,  88, 
-     28,   5,   0,   0,  80,   0, 
-      0,   0,  71,   1,   0,   0, 
-    106,   8,   0,   1,  89,   0, 
-      0,   4,  70, 142,  32,   0, 
-      0,   0,   0,   0,   5,   0, 
-      0,   0,  90,   0,   0,   3, 
-      0,  96,  16,   0,   0,   0, 
-      0,   0,  88,  24,   0,   4, 
-      0, 112,  16,   0,   0,   0, 
-      0,   0,  85,  85,   0,   0, 
-     88,  24,   0,   4,   0, 112, 
-     16,   0,   1,   0,   0,   0, 
-     85,  85,   0,   0,  98,  16, 
-      0,   3,  50,  16,  16,   0, 
-      1,   0,   0,   0,  98,  16, 
-      0,   3, 242,  16,  16,   0, 
-      2,   0,   0,   0, 101,   0, 
-      0,   3, 242,  32,  16,   0, 
-      0,   0,   0,   0, 104,   0, 
-      0,   2,   4,   0,   0,   0, 
-     69,   0,   0, 139, 194,   0, 
-      0, 128,  67,  85,  21,   0, 
-     18,   0,  16,   0,   0,   0, 
-      0,   0,  70,  16,  16,   0, 
-      1,   0,   0,   0,  70, 126, 
-     16,   0,   0,   0,   0,   0, 
-      0,  96,  16,   0,   0,   0, 
-      0,   0,  69,   0,   0, 139, 
-    194,   0,   0, 128,  67,  85, 
-     21,   0,  98,   0,  16,   0, 
-      0,   0,   0,   0,  70,  16, 
-     16,   0,   1,   0,   0,   0, 
-     38, 125,  16,   0,   1,   0, 
-      0,   0,   0,  96,  16,   0, 
-      0,   0,   0,   0,   0,   0, 
-      0,   8, 114,   0,  16,   0, 
-      0,   0,   0,   0,  70,   2, 
-     16,   0,   0,   0,   0,   0, 
-     70, 130,  32,   0,   0,   0, 
-      0,   0,   0,   0,   0,   0, 
-     16,   0,   0,   8,  18,   0, 
-     16,   0,   1,   0,   0,   0, 
-     70,   2,  16,   0,   0,   0, 
-      0,   0,  70, 130,  32,   0, 
-      0,   0,   0,   0,   1,   0, 
-      0,   0,  16,   0,   0,   8, 
-     34,   0,  16,   0,   1,   0, 
-      0,   0,  70,   2,  16,   0, 
-      0,   0,   0,   0,  70, 130, 
-     32,   0,   0,   0,   0,   0, 
-      2,   0,   0,   0,  16,   0, 
-      0,   8,  66,   0,  16,   0, 
-      1,   0,   0,   0,  70,   2, 
-     16,   0,   0,   0,   0,   0, 
-    

(Patch may be truncated, please check the link at the top of this post.)