Debian Codegraph @

src/scene/glsl/source/tone_mapping.fs

raw
1 /*
2 Copyright 2020-2020 glTF-Sample-Viewer and Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5 (And it is heavily based on glTF-Sample-Viewer,
6 on a compatible open-source license.)
7
8 "Castle Game Engine" is free software; see the file COPYING.txt,
9 included in this distribution, for details about the copyright.
10
11 "Castle Game Engine" is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 ----------------------------------------------------------------------------
16
17 Shader code used for gamma correction and tone mapping.
18 */
19
20 // Not used by CGE now:
21 // uniform float u_Exposure;
22
23 const float GAMMA = 2.2;
24 const float INV_GAMMA = 1.0 / GAMMA;
25
26 // sRGB to linear approximation
27 // see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
28 vec4 castle_texture_color_to_linear(const in vec4 srgbIn)
29 {
30 #ifdef CASTLE_GAMMA_CORRECTION
31 return vec4(pow(srgbIn.xyz, vec3(GAMMA)), srgbIn.w);
32 #else
33 return srgbIn;
34 #endif
35 }
36
37 vec3 castle_texture_color_to_linear(const in vec3 srgbIn)
38 {
39 #ifdef CASTLE_GAMMA_CORRECTION
40 return pow(srgbIn, vec3(GAMMA));
41 #else
42 return srgbIn;
43 #endif
44 }
45
46 // linear to sRGB approximation
47 // see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
48 vec3 LINEARtoSRGB(const in vec3 color)
49 {
50 #ifdef CASTLE_GAMMA_CORRECTION
51 return pow(color, vec3(INV_GAMMA));
52 #else
53 return color;
54 #endif
55 }
56
57 // Uncharted 2 tone map
58 // see: http://filmicworlds.com/blog/filmic-tonemapping-operators/
59 vec3 toneMapUncharted2Impl(const in vec3 color)
60 {
61 const float A = 0.15;
62 const float B = 0.50;
63 const float C = 0.10;
64 const float D = 0.20;
65 const float E = 0.02;
66 const float F = 0.30;
67 return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F;
68 }
69
70 vec3 toneMapUncharted(in vec3 color)
71 {
72 const float W = 11.2;
73 color = toneMapUncharted2Impl(color * 2.0);
74 vec3 whiteScale = 1.0 / toneMapUncharted2Impl(vec3(W));
75 color = color * whiteScale;
76 #ifdef CASTLE_GAMMA_CORRECTION
77 color = LINEARtoSRGB(color);
78 #endif
79 return color;
80 }
81
82 // Hejl Richard tone map
83 // see: http://filmicworlds.com/blog/filmic-tonemapping-operators/
84 vec3 toneMapHejlRichard(in vec3 color)
85 {
86 color = max(vec3(0.0), color - vec3(0.004));
87 color = (color*(6.2*color+.5))/(color*(6.2*color+1.7)+0.06);
88 #ifndef CASTLE_GAMMA_CORRECTION
89 // The above calculation did equivalent of "pow(color, vec3(1 / 2.2))" already.
90 // So invert it, if no gamma correction.
91 color = pow(color, vec3(GAMMA));
92 #endif
93 return color;
94 }
95
96 // ACES tone map
97 // see: https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
98 vec3 toneMapACES(in vec3 color)
99 {
100 const float A = 2.51;
101 const float B = 0.03;
102 const float C = 2.43;
103 const float D = 0.59;
104 const float E = 0.14;
105 color = clamp((color * (A * color + B)) / (color * (C * color + D) + E), 0.0, 1.0);
106 #ifdef CASTLE_GAMMA_CORRECTION
107 color = LINEARtoSRGB(color);
108 #endif
109 return color;
110 }
111
112 vec3 castle_linear_to_screen(const in vec3 color)
113 {
114 // color *= u_Exposure;
115
116 #ifdef CASTLE_TONE_MAPPING
117 #ifdef CASTLE_TONE_MAPPING_UNCHARTED
118 return toneMapUncharted(color);
119 #endif
120
121 #ifdef CASTLE_TONE_MAPPING_HEJLRICHARD
122 return toneMapHejlRichard(color);
123 #endif
124
125 #ifdef CASTLE_TONE_MAPPING_ACES
126 return toneMapACES(color);
127 #endif
128 #endif
129
130 return LINEARtoSRGB(color);
131 }