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