Debian Codegraph @

Data/Sys/Shaders/PerceptualHDR.glsl

raw
1 /*
2 [configuration]
3
4 [OptionRangeFloat]
5 GUIName = Amplificiation
6 OptionName = AMPLIFICATION
7 MinValue = 1.0
8 MaxValue = 6.0
9 StepAmount = 0.25
10 DefaultValue = 2.5
11
12 [/configuration]
13 */
14
15 // ICtCP Colorspace as defined by Dolby here:
16 // https://professional.dolby.com/siteassets/pdfs/ictcp_dolbywhitepaper_v071.pdf
17
18 /***** Transfer Function *****/
19
20 const float a = 0.17883277;
21 const float b = 1.0 - 4.0 * a;
22 const float c = 0.5 - a * log(4.0 * a);
23
24 float HLG_f(float x)
25 {
26 if (x < 0.0) {
27 return 0.0;
28 }
29
30 else if (x < 1.0 / 12.0) {
31 return sqrt(3.0 * x);
32 }
33
34 return a * log(12.0 * x - b) + c;
35 }
36
37 float HLG_inv_f(float x)
38 {
39 if (x < 0.0) {
40 return 0.0;
41 }
42
43 else if (x < 1.0 / 2.0) {
44 return x * x / 3.0;
45 }
46
47 return (exp((x - c) / a) + b) / 12.0;
48 }
49
50 float4 HLG(float4 lms)
51 {
52 return float4(HLG_f(lms.x), HLG_f(lms.y), HLG_f(lms.z), lms.w);
53 }
54
55 float4 HLG_inv(float4 lms)
56 {
57 return float4(HLG_inv_f(lms.x), HLG_inv_f(lms.y), HLG_inv_f(lms.z), lms.w);
58 }
59
60 /***** Linear <--> ICtCp *****/
61
62 const mat4 RGBtoLMS = mat4(
63 1688.0, 683.0, 99.0, 0.0,
64 2146.0, 2951.0, 309.0, 0.0,
65 262.0, 462.0, 3688.0, 0.0,
66 0.0, 0.0, 0.0, 4096.0)
67 / 4096.0;
68
69 const mat4 LMStoICtCp = mat4(
70 +2048.0, +3625.0, +9500.0, 0.0,
71 +2048.0, -7465.0, -9212.0, 0.0,
72 +0.0, +3840.0, -288.0, 0.0,
73 +0.0, +0.0, +0.0, 4096.0)
74 / 4096.0;
75
76 float4 LinearRGBToICtCP(float4 c)
77 {
78 return LMStoICtCp * HLG(RGBtoLMS * c);
79 }
80
81 /***** ICtCp <--> Linear *****/
82
83 mat4 ICtCptoLMS = inverse(LMStoICtCp);
84 mat4 LMStoRGB = inverse(RGBtoLMS);
85
86 float4 ICtCpToLinearRGB(float4 c)
87 {
88 return LMStoRGB * HLG_inv(ICtCptoLMS * c);
89 }
90
91 void main()
92 {
93 float4 color = Sample();
94
95 // Nothing to do here, we are in SDR
96 if (!OptionEnabled(hdr_output) || !OptionEnabled(linear_space_output)) {
97 SetOutput(color);
98 return;
99 }
100
101 // Renormalize Color to be in [0.0 - 1.0] SDR Space. We will revert this later.
102 const float hdr_paper_white = hdr_paper_white_nits / hdr_sdr_white_nits;
103 color.rgb /= hdr_paper_white;
104
105 // Convert Color to Perceptual Color Space. This will allow us to do perceptual
106 // scaling while also being able to use the luminance channel.
107 float4 ictcp_color = LinearRGBToICtCP(color);
108
109 // Scale the color in perceptual space depending on the perceived luminance.
110 //
111 // At low luminances, ~0.0, pow(AMPLIFICATION, ~0.0) ~= 1.0, so the
112 // color will appear to be unchanged. This is important as we don't want to
113 // over expose dark colors which would not have otherwise been seen.
114 //
115 // At high luminances, ~1.0, pow(AMPLIFICATION, ~1.0) ~= AMPLIFICATION,
116 // which is equivalent to scaling the color by AMPLIFICATION. This is
117 // important as we want to get the most out of the display, and we want to
118 // get bright colors to hit their target brightness.
119 //
120 // For more information, see this desmos demonstrating this scaling process:
121 // https://www.desmos.com/calculator/syjyrjsj5c
122 float exposure = length(ictcp_color.xyz);
123 ictcp_color *= pow(HLG_f(AMPLIFICATION), exposure);
124
125 // Convert back to Linear RGB and output the color to the display.
126 // We use hdr_paper_white to renormalize the color to the comfortable
127 // SDR viewing range.
128 SetOutput(hdr_paper_white * ICtCpToLinearRGB(ictcp_color));
129 }