Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 224ed60

Browse files
committed
Added a faster sRGB path
1 parent dee1d8c commit 224ed60

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

PostProcessing/Shaders/Colors.hlsl

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@
77
#define LUT_SPACE_ENCODE(x) LinearToLogC(x)
88
#define LUT_SPACE_DECODE(x) LogCToLinear(x)
99

10-
// Set to 1 to use more precise but more expensive log/linear conversions. I haven't found a proper
11-
// use case for the high precision version yet so I'm leaving this to 0.
12-
#define USE_PRECISE_LOGC 0
10+
#ifndef USE_PRECISE_LOGC
11+
// Set to 1 to use more precise but more expensive log/linear conversions. I haven't found a proper
12+
// use case for the high precision version yet so I'm leaving this to 0.
13+
#define USE_PRECISE_LOGC 0
14+
#endif
1315

14-
// Set to 1 to use the full reference ACES tonemapper. This should only be used for research
15-
// purposes as it's quite heavy and generally overkill.
16-
#define TONEMAPPING_USE_FULL_ACES 0
16+
#ifndef TONEMAPPING_USE_FULL_ACES
17+
// Set to 1 to use the full reference ACES tonemapper. This should only be used for research
18+
// purposes as it's quite heavy and generally overkill.
19+
#define TONEMAPPING_USE_FULL_ACES 0
20+
#endif
1721

18-
// PQ ST.2048 max value
19-
// 1.0 = 100nits, 100.0 = 10knits
20-
#define DEFAULT_MAX_PQ 100.0
22+
#ifndef DEFAULT_MAX_PQ
23+
// PQ ST.2048 max value
24+
// 1.0 = 100nits, 100.0 = 10knits
25+
#define DEFAULT_MAX_PQ 100.0
26+
#endif
27+
28+
#ifndef USE_FAST_SRGB
29+
#ifdef SHADER_API_MOBILE
30+
#define USE_FAST_SRGB 1
31+
#else
32+
#define USE_FAST_SRGB 0
33+
#endif
34+
#endif
2135

2236
//
2337
// Alexa LogC converters (El 1000)
@@ -132,21 +146,30 @@ float3 PQToLinear(float3 x)
132146

133147
//
134148
// sRGB transfer functions
149+
// Fast path ref: http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
135150
//
136151
half SRGBToLinear(half c)
137152
{
153+
#if USE_FAST_SRGB
154+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
155+
#else
138156
half linearRGBLo = c / 12.92;
139157
half linearRGBHi = PositivePow((c + 0.055) / 1.055, 2.4);
140158
half linearRGB = (c <= 0.04045) ? linearRGBLo : linearRGBHi;
141159
return linearRGB;
160+
#endif
142161
}
143162

144163
half3 SRGBToLinear(half3 c)
145164
{
165+
#if USE_FAST_SRGB
166+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
167+
#else
146168
half3 linearRGBLo = c / 12.92;
147169
half3 linearRGBHi = PositivePow((c + 0.055) / 1.055, half3(2.4, 2.4, 2.4));
148170
half3 linearRGB = (c <= 0.04045) ? linearRGBLo : linearRGBHi;
149171
return linearRGB;
172+
#endif
150173
}
151174

152175
half4 SRGBToLinear(half4 c)
@@ -156,18 +179,26 @@ half4 SRGBToLinear(half4 c)
156179

157180
half LinearToSRGB(half c)
158181
{
182+
#if USE_FAST_SRGB
183+
return max(1.055 * PositivePow(c, 0.416666667) - 0.055, 0.0);
184+
#else
159185
half sRGBLo = c * 12.92;
160186
half sRGBHi = (PositivePow(c, 1.0 / 2.4) * 1.055) - 0.055;
161187
half sRGB = (c <= 0.0031308) ? sRGBLo : sRGBHi;
162188
return sRGB;
189+
#endif
163190
}
164191

165192
half3 LinearToSRGB(half3 c)
166193
{
194+
#if USE_FAST_SRGB
195+
return max(1.055 * PositivePow(c, 0.416666667) - 0.055, 0.0);
196+
#else
167197
half3 sRGBLo = c * 12.92;
168198
half3 sRGBHi = (PositivePow(c, half3(1.0 / 2.4, 1.0 / 2.4, 1.0 / 2.4)) * 1.055) - 0.055;
169199
half3 sRGB = (c <= 0.0031308) ? sRGBLo : sRGBHi;
170200
return sRGB;
201+
#endif
171202
}
172203

173204
half4 LinearToSRGB(half4 c)

0 commit comments

Comments
 (0)