Debian Codegraph @

drivers/gles3/shaders/effects/post.glsl

raw
1 /* clang-format off */
2 #[modes]
3 mode_default =
4
5 #[specializations]
6
7 USE_MULTIVIEW = false
8 USE_GLOW = false
9 USE_LUMINANCE_MULTIPLIER = false
10 USE_BCS = false
11 USE_COLOR_CORRECTION = false
12 USE_1D_LUT = false
13 USE_SSAO_ABYSS = false
14 USE_SSAO_LOW = false
15 USE_SSAO_MED = false
16 USE_SSAO_HIGH = false
17 USE_SSAO_MEGA = false
18
19 #[vertex]
20 layout(location = 0) in vec2 vertex_attrib;
21
22 /* clang-format on */
23
24 out vec2 uv_interp;
25
26 void main() {
27 uv_interp = vertex_attrib * 0.5 + 0.5;
28 gl_Position = vec4(vertex_attrib, 1.0, 1.0);
29 }
30
31 /* clang-format off */
32 #[fragment]
33 /* clang-format on */
34
35 // If we reach this code, we always tonemap.
36 #define APPLY_TONEMAPPING
37
38 #include "../tonemap_inc.glsl"
39
40 #ifdef USE_MULTIVIEW
41 uniform sampler2DArray source_color; // texunit:0
42 #else
43 uniform sampler2D source_color; // texunit:0
44 #endif // USE_MULTIVIEW
45
46 uniform float view;
47 uniform float luminance_multiplier;
48
49 #ifdef USE_GLOW
50 uniform sampler2D glow_color; // texunit:1
51 uniform vec2 pixel_size;
52 uniform float glow_intensity;
53 uniform float srgb_white;
54
55 vec4 get_glow_color(vec2 uv) {
56 vec2 half_pixel = pixel_size * 0.5;
57
58 vec4 color = textureLod(glow_color, uv + vec2(-half_pixel.x * 2.0, 0.0), 0.0);
59 color += textureLod(glow_color, uv + vec2(-half_pixel.x, half_pixel.y), 0.0) * 2.0;
60 color += textureLod(glow_color, uv + vec2(0.0, half_pixel.y * 2.0), 0.0);
61 color += textureLod(glow_color, uv + vec2(half_pixel.x, half_pixel.y), 0.0) * 2.0;
62 color += textureLod(glow_color, uv + vec2(half_pixel.x * 2.0, 0.0), 0.0);
63 color += textureLod(glow_color, uv + vec2(half_pixel.x, -half_pixel.y), 0.0) * 2.0;
64 color += textureLod(glow_color, uv + vec2(0.0, -half_pixel.y * 2.0), 0.0);
65 color += textureLod(glow_color, uv + vec2(-half_pixel.x, -half_pixel.y), 0.0) * 2.0;
66
67 #ifdef USE_LUMINANCE_MULTIPLIER
68 color = color / luminance_multiplier;
69 #endif
70
71 return color / 12.0;
72 }
73 #endif // USE_GLOW
74
75 #ifdef USE_COLOR_CORRECTION
76 #ifdef USE_1D_LUT
77 uniform sampler2D source_color_correction; //texunit:2
78
79 vec3 apply_color_correction(vec3 color) {
80 color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
81 color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
82 color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
83 return color;
84 }
85 #else
86 uniform sampler3D source_color_correction; //texunit:2
87
88 vec3 apply_color_correction(vec3 color) {
89 return textureLod(source_color_correction, color, 0.0).rgb;
90 }
91 #endif // USE_1D_LUT
92 #endif // USE_COLOR_CORRECTION
93
94 #if defined(USE_SSAO_ABYSS) || defined(USE_SSAO_LOW) || defined(USE_SSAO_MED) || defined(USE_SSAO_HIGH) || defined(USE_SSAO_MEGA)
95 #define USE_SOME_SSAO
96 uniform float ssao_intensity;
97 uniform float ssao_radius_frac;
98 uniform vec2 ssao_prn_UV;
99 #ifdef USE_MULTIVIEW
100 // VR will have 2 depth buffers.
101 uniform sampler2DArray depth_buffer_array; // texunit:3
102 #else
103 uniform sampler2D depth_buffer; // texunit:3
104 #endif
105 #if defined(USE_SSAO_ABYSS)
106 // Use the tiny 2-sample version.
107 #include "../s4ao_micro_inc.glsl"
108 #elif defined(USE_SSAO_HIGH) || defined(USE_SSAO_MEGA)
109 // Use the rings version for the higher qualities.
110 #include "../s4ao_mega_inc.glsl"
111 #else
112 // Use the more generic NxN grid version.
113 #include "../s4ao_inc.glsl"
114 #endif
115 #endif
116
117 in vec2 uv_interp;
118
119 layout(location = 0) out vec4 frag_color;
120
121 void main() {
122 #ifdef USE_MULTIVIEW
123 vec4 color = texture(source_color, vec3(uv_interp, view));
124 #else
125 vec4 color = texture(source_color, uv_interp);
126 #endif
127
128 #ifdef USE_LUMINANCE_MULTIPLIER
129 color = color / luminance_multiplier;
130 #endif
131
132 #ifdef USE_GLOW
133 // Glow blending is performed before srgb_to_linear because
134 // the glow texture was created from a nonlinear sRGB-encoded
135 // scene, so it only makes sense to add this glow to an equally
136 // nonlinear sRGB-encoded scene.
137
138 vec4 glow = get_glow_color(uv_interp) * glow_intensity;
139
140 // Glow always uses the screen blend mode in the Compatibility renderer:
141
142 // Glow cannot be above 1.0 after normalizing and should be non-negative
143 // to produce expected results. It is possible that glow can be negative
144 // if negative lights were used in the scene.
145 // We clamp to srgb_white because glow will be normalized to this range.
146 // Note: srgb_white cannot be smaller than the maximum output value (1.0).
147 glow.rgb = clamp(glow.rgb, 0.0, srgb_white);
148
149 // Normalize to srgb_white range.
150 //glow.rgb /= srgb_white;
151 //color.rgb /= srgb_white;
152 //color.rgb = (color.rgb + glow.rgb) - (color.rgb * glow.rgb);
153 // Expand back to original range.
154 //color.rgb *= srgb_white;
155
156 // The following is a mathematically simplified version of the above.
157 color.rgb = color.rgb + glow.rgb - (color.rgb * glow.rgb / srgb_white);
158 #endif // USE_GLOW
159
160 color.rgb = srgb_to_linear(color.rgb);
161
162 #if defined(USE_SOME_SSAO)
163 // Putting SSAO after the conversion to linear color, though it might be better before the glow.
164 color.rgb *= s4ao(uv_interp); // The USE_SSAO_X controls the number of samples.
165 #endif
166
167 color.rgb = apply_tonemapping(color.rgb);
168
169 #ifdef USE_BCS
170 // Apply brightness:
171 // Apply to relative luminance. This ensures that the hue and saturation of
172 // colors is not affected by the adjustment, but requires the multiplication
173 // to be performed on linear-encoded values.
174 color.rgb = color.rgb * brightness;
175
176 color.rgb = linear_to_srgb(color.rgb);
177
178 // Apply contrast:
179 // By applying contrast to RGB values that are perceptually uniform (nonlinear),
180 // the darkest values are not hard-clipped as badly, which produces a
181 // higher quality contrast adjustment and maintains compatibility with
182 // existing projects.
183 color.rgb = mix(vec3(0.5), color.rgb, contrast);
184
185 // Apply saturation:
186 // By applying saturation adjustment to nonlinear sRGB-encoded values with
187 // even weights the preceived brightness of blues are affected, but this
188 // maintains compatibility with existing projects.
189 color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * (1.0 / 3.0)), color.rgb, saturation);
190 #else
191 color.rgb = linear_to_srgb(color.rgb);
192 #endif // USE_BCS
193
194 #ifdef USE_COLOR_CORRECTION
195 color.rgb = apply_color_correction(color.rgb);
196 #endif
197
198 frag_color = color;
199 }