Debian Codegraph @

Data/Sys/Shaders/AutoHDR.glsl

raw
1 // Based on https://github.com/Filoppi/PumboAutoHDR
2
3 /*
4 [configuration]
5
6 [OptionRangeFloat]
7 GUIName = HDR Display Max Nits
8 OptionName = HDR_DISPLAY_MAX_NITS
9 MinValue = 80
10 MaxValue = 2000
11 StepAmount = 1
12 DefaultValue = 400
13
14 [OptionRangeFloat]
15 GUIName = Shoulder Start Alpha
16 OptionName = AUTO_HDR_SHOULDER_START_ALPHA
17 MinValue = 0
18 MaxValue = 1
19 StepAmount = 0.01
20 DefaultValue = 0
21
22 [OptionRangeFloat]
23 GUIName = Shoulder Pow
24 OptionName = AUTO_HDR_SHOULDER_POW
25 MinValue = 1
26 MaxValue = 10
27 StepAmount = 0.05
28 DefaultValue = 2.5
29
30 [/configuration]
31 */
32
33 float luminance(float3 color)
34 {
35 return dot(color, float3(0.2126f, 0.7152f, 0.0722f));
36 }
37
38 void main()
39 {
40 float4 color = Sample();
41
42 // Nothing to do here, we are in SDR
43 if (!OptionEnabled(hdr_output) || !OptionEnabled(linear_space_output))
44 {
45 SetOutput(color);
46 return;
47 }
48
49 const float hdr_paper_white = hdr_paper_white_nits / hdr_sdr_white_nits;
50
51 // Restore the original SDR (0-1) brightness (we might or might not restore it later)
52 color.rgb /= hdr_paper_white;
53
54 // Find the color luminance (it works better than average)
55 float sdr_ratio = luminance(color.rgb);
56
57 const float auto_hdr_max_white = max(HDR_DISPLAY_MAX_NITS / (hdr_paper_white_nits / hdr_sdr_white_nits), hdr_sdr_white_nits) / hdr_sdr_white_nits;
58 if (sdr_ratio > AUTO_HDR_SHOULDER_START_ALPHA && AUTO_HDR_SHOULDER_START_ALPHA < 1.0)
59 {
60 const float auto_hdr_shoulder_ratio = 1.0 - (max(1.0 - sdr_ratio, 0.0) / (1.0 - AUTO_HDR_SHOULDER_START_ALPHA));
61 const float auto_hdr_extra_ratio = pow(auto_hdr_shoulder_ratio, AUTO_HDR_SHOULDER_POW) * (auto_hdr_max_white - 1.0);
62 const float auto_hdr_total_ratio = sdr_ratio + auto_hdr_extra_ratio;
63 color.rgb *= auto_hdr_total_ratio / sdr_ratio;
64 }
65
66 color.rgb *= hdr_paper_white;
67
68 SetOutput(color);
69 }