2 * Copyright (C) 2005-2015 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #if defined(KODI_3DLUT)
22 float2 m_LUTParams; // x- scale, y- offset
25 SamplerState LutSampler : IMMUTABLE
30 Filter = MIN_MAG_MIP_LINEAR;
33 #if defined(KODI_DITHER)
34 float3 m_ditherParams;
35 texture2D m_ditherMatrix;
37 SamplerState DitherSampler : IMMUTABLE
41 Filter = MIN_MAG_MIP_POINT;
44 #if (defined(KODI_TONE_MAPPING_ACES) || defined(KODI_TONE_MAPPING_HABLE) || defined(KODI_HLG_TO_PQ))
45 static const float ST2084_m1 = 2610.0f / (4096.0f * 4.0f);
46 static const float ST2084_m2 = (2523.0f / 4096.0f) * 128.0f;
47 static const float ST2084_c1 = 3424.0f / 4096.0f;
48 static const float ST2084_c2 = (2413.0f / 4096.0f) * 32.0f;
49 static const float ST2084_c3 = (2392.0f / 4096.0f) * 32.0f;
51 #if defined(KODI_TONE_MAPPING_REINHARD)
55 float reinhard(float x)
57 return x * (1.0f + x / (g_toneP1 * g_toneP1)) / (1.0f + x);
60 #if defined(KODI_TONE_MAPPING_ACES)
66 const float A = 2.51f;
67 const float B = 0.03f;
68 const float C = 2.43f;
69 const float D = 0.59f;
70 const float E = 0.14f;
71 return (x * (A * x + B)) / (x * (C * x + D) + E);
74 #if defined(KODI_TONE_MAPPING_HABLE)
78 float3 hable(float3 x)
80 const float A = 0.15f;
84 const float E = 0.02f;
86 return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
89 #if (defined(KODI_TONE_MAPPING_ACES) || defined(KODI_TONE_MAPPING_HABLE))
90 float3 inversePQ(float3 x)
92 x = pow(max(x, 0.0f), 1.0f / ST2084_m2);
93 x = max(x - ST2084_c1, 0.0f) / (ST2084_c2 - ST2084_c3 * x);
94 x = pow(x, 1.0f / ST2084_m1);
98 #if defined(KODI_HLG_TO_PQ)
100 // HLG inverse OETF - BT.2100
101 // input: non-linear signal [0,1] range
102 // output: linear [0,1] range
103 float3 inverseHLG(float3 x)
105 static const float B67_a = 0.17883277f;
106 static const float B67_b = 0.28466892f; // b = 1 - 4*a
107 static const float B67_c = 0.55991073f; // c = 0.5 - a*log(4*a)
108 x = (x <= 0.5f) ? x * x / 3.0f : (exp((x - B67_c) / B67_a) + B67_b) / 12.0f;
112 // PQ inverse EOTF, BT.2100
113 // input: linear cd/m2 [0,10000] range
114 // output: non-linear [0,1] range
115 float3 tranferPQ(float3 x)
117 x = pow(x / 10000.0f, ST2084_m1);
118 x = (ST2084_c1 + ST2084_c2 * x) / (1.0f + ST2084_c3 * x);
119 x = pow(x, ST2084_m2);
125 float4 output4(float4 color, float2 uv)
127 #if defined(KODI_TONE_MAPPING_REINHARD)
128 float luma = dot(color.rgb, g_coefsDst);
129 color.rgb *= reinhard(luma) / luma;
131 #if defined(KODI_TONE_MAPPING_ACES)
132 color.rgb = inversePQ(color.rgb);
133 color.rgb *= (10000.0f / g_luminance) * (2.0f / g_toneP1);
134 color.rgb = aces(color.rgb);
135 color.rgb *= (1.24f / g_toneP1);
136 color.rgb = pow(color.rgb, 0.27f);
138 #if defined(KODI_TONE_MAPPING_HABLE)
139 color.rgb = inversePQ(color.rgb);
140 color.rgb *= g_toneP1;
141 color.rgb = hable(color.rgb * g_toneP2) / hable(g_toneP2);
142 color.rgb = pow(color.rgb, 1.0f / 2.2f);
144 #if defined(KODI_HLG_TO_PQ)
146 // Reference: BT.2100, Table 5, HLG Reference EOTF
148 // Display peak luminance in cd/m2
149 static const float HLG_Lw = 1000.0f;
150 static const float HLG_gamma = 1.2f + 0.42f * log10(HLG_Lw / 1000.0f);
152 // color.rgb: E', range [0,1]
153 color.rgb = inverseHLG(color.rgb);
154 // color.rgb: E, range [0,1]
155 static const float3 bt2020_lum_rgbweights = float3(0.2627f, 0.6780f, 0.0593f);
156 float HLG_Ys = dot(bt2020_lum_rgbweights, color.rgb);
157 color.rgb *= HLG_Lw * pow(HLG_Ys, HLG_gamma - 1.0f);
159 // color.rgb: FD, in cd/m2
160 color.rgb = tranferPQ(color.rgb);
162 #if defined(KODI_3DLUT)
163 half3 scale = m_LUTParams.x;
164 half3 offset = m_LUTParams.y;
165 float3 lutRGB = m_LUT.Sample(LutSampler, color.rgb*scale + offset).rgb;
166 color.rgb = scale.x ? lutRGB : color.rgb;
168 #if defined(KODI_DITHER)
169 half2 ditherpos = uv * m_ditherParams.xy;
170 // scale ditherval to [0,1)
171 float ditherval = m_ditherMatrix.Sample(DitherSampler, ditherpos).r * 16.0f;
172 color.rgb = floor(color.rgb * m_ditherParams.z + ditherval) / m_ditherParams.z;
177 float4 output(float3 color, float2 uv)
179 return output4(float4(color, 1.0), uv);
182 #if defined(KODI_OUTPUT_T)
183 #include "convolution_d3d.fx"
185 #if (defined(KODI_TONE_MAPPING_ACES) || defined(KODI_TONE_MAPPING_HABLE) || defined(KODI_HLG_TO_PQ))
186 #define PS_PROFILE ps_4_0_level_9_3
188 #define PS_PROFILE ps_4_0_level_9_1
191 float3 m_params; // 0 - range (0 - full, 1 - limited), 1 - contrast, 2 - brightness
193 float4 OUTPUT_PS(VS_OUTPUT In) : SV_TARGET
195 float4 color = g_Texture.Sample(KernelSampler, In.TextureUV);
196 [flatten] if (m_params.x)
197 color = saturate((64.0 / 1023.0) + color * (940.0 - 64.0) / 1023.0);
199 color *= m_params.y * 2.0;
200 color += m_params.z - 0.5;
203 return output4(color, In.TextureUV);
210 SetVertexShader( VS_SHADER );
211 SetPixelShader( CompileShader( PS_PROFILE, OUTPUT_PS() ) );