]> git.lizzy.rs Git - minetest.git/blob - client/shaders/second_stage/opengl_fragment.glsl
09b49ef0edd53c249a9b3cac825d1ae6a231aed2
[minetest.git] / client / shaders / second_stage / opengl_fragment.glsl
1 #define rendered texture0
2 #define bloom texture1
3
4 uniform sampler2D rendered;
5 uniform sampler2D bloom;
6 uniform mediump float exposureFactor;
7 uniform lowp float bloomIntensity;
8 uniform lowp float saturation;
9
10 #ifdef GL_ES
11 varying mediump vec2 varTexCoord;
12 #else
13 centroid varying vec2 varTexCoord;
14 #endif
15
16 #ifdef ENABLE_BLOOM
17
18 vec4 applyBloom(vec4 color, vec2 uv)
19 {
20         vec3 light = texture2D(bloom, uv).rgb;
21 #ifdef ENABLE_BLOOM_DEBUG
22         if (uv.x > 0.5 && uv.y < 0.5)
23                 return vec4(light, color.a);
24         if (uv.x < 0.5)
25                 return color;
26 #endif
27         color.rgb = mix(color.rgb, light, bloomIntensity);
28         return color;
29 }
30
31 #endif
32
33 #if ENABLE_TONE_MAPPING
34
35 /* Hable's UC2 Tone mapping parameters
36         A = 0.22;
37         B = 0.30;
38         C = 0.10;
39         D = 0.20;
40         E = 0.01;
41         F = 0.30;
42         W = 11.2;
43         equation used:  ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
44 */
45
46 vec3 uncharted2Tonemap(vec3 x)
47 {
48         return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
49 }
50
51 vec4 applyToneMapping(vec4 color)
52 {
53         const float exposureBias = 2.0;
54         color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
55         // Precalculated white_scale from
56         //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
57         vec3 whiteScale = vec3(1.036015346);
58         color.rgb *= whiteScale;
59         return color;
60 }
61
62 vec3 applySaturation(vec3 color, float factor)
63 {
64         // Calculate the perceived luminosity from the RGB color.
65         // See also: https://www.w3.org/WAI/GL/wiki/Relative_luminance
66         float brightness = dot(color, vec3(0.2125, 0.7154, 0.0721));
67         return mix(vec3(brightness), color, factor);
68 }
69 #endif
70
71 void main(void)
72 {
73         vec2 uv = varTexCoord.st;
74         vec4 color = texture2D(rendered, uv).rgba;
75
76         // translate to linear colorspace (approximate)
77         color.rgb = pow(color.rgb, vec3(2.2));
78
79 #ifdef ENABLE_BLOOM_DEBUG
80         if (uv.x > 0.5 || uv.y > 0.5)
81 #endif
82         {
83                 color.rgb *= exposureFactor;
84         }
85
86
87 #ifdef ENABLE_BLOOM
88         color = applyBloom(color, uv);
89 #endif
90
91 #ifdef ENABLE_BLOOM_DEBUG
92         if (uv.x > 0.5 || uv.y > 0.5)
93 #endif
94         {
95 #if ENABLE_TONE_MAPPING
96                 color = applyToneMapping(color);
97                 color.rgb = applySaturation(color.rgb, saturation);
98 #endif
99         }
100
101         color.rgb = clamp(color.rgb, vec3(0.), vec3(1.));
102
103         // return to sRGB colorspace (approximate)
104         color.rgb = pow(color.rgb, vec3(1.0 / 2.2));
105
106         gl_FragColor = vec4(color.rgb, 1.0); // force full alpha to avoid holes in the image.
107 }