Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/copy.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #include "../oct_inc.glsl"
8
9 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
10
11 #define FLAG_HORIZONTAL (1 << 0)
12 #define FLAG_USE_BLUR_SECTION (1 << 1)
13 #define FLAG_USE_ORTHOGONAL_PROJECTION (1 << 2)
14 #define FLAG_DOF_NEAR_FIRST_TAP (1 << 3)
15 #define FLAG_GLOW_FIRST_PASS (1 << 4)
16 #define FLAG_FLIP_Y (1 << 5)
17 #define FLAG_FORCE_LUMINANCE (1 << 6)
18 #define FLAG_COPY_ALL_SOURCE (1 << 7)
19 #define FLAG_ALPHA_TO_ONE (1 << 8)
20 #define FLAG_SANITIZE_INF_NAN (1 << 9)
21
22 layout(push_constant, std430) uniform Params {
23 ivec4 section;
24 ivec2 target;
25 uint flags;
26 float luminance_multiplier;
27 // Glow.
28 float glow_strength;
29 float glow_bloom;
30 float glow_hdr_threshold;
31 float glow_hdr_scale;
32
33 float glow_exposure;
34 float glow_white;
35 float glow_luminance_cap;
36 float glow_auto_exposure_scale;
37 // DOF.
38 float camera_z_far;
39 float camera_z_near;
40 // Octmap.
41 vec2 octmap_border_size;
42
43 vec4 set_color;
44 }
45 params;
46
47 #ifdef MODE_OCTMAP_ARRAY_TO_PANORAMA
48 layout(set = 0, binding = 0) uniform sampler2DArray source_color;
49 #elif defined(MODE_OCTMAP_TO_PANORAMA)
50 layout(set = 0, binding = 0) uniform sampler2D source_color;
51 #elif !defined(MODE_SET_COLOR)
52 layout(set = 0, binding = 0) uniform sampler2D source_color;
53 #endif
54
55 #ifdef GLOW_USE_AUTO_EXPOSURE
56 layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
57 #endif
58
59 #if defined(MODE_LINEARIZE_DEPTH_COPY) || defined(MODE_SIMPLE_COPY_DEPTH)
60 layout(r32f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
61 #elif defined(DST_IMAGE_8BIT)
62 layout(rgba8, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
63 #else
64 layout(rgba16f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
65 #endif
66
67 #ifdef MODE_GAUSSIAN_BLUR
68 shared vec4 local_cache[256];
69 shared vec4 temp_cache[128];
70 #endif
71
72 void main() {
73 // Pixel being shaded
74 ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
75
76 #ifndef MODE_GAUSSIAN_BLUR // Gaussian blur needs the extra threads
77 if (any(greaterThanEqual(pos, params.section.zw))) { //too large, do nothing
78 return;
79 }
80 #endif
81
82 #ifdef MODE_MIPMAP
83
84 ivec2 base_pos = (pos + params.section.xy) << 1;
85 vec4 color = texelFetch(source_color, base_pos, 0);
86 color += texelFetch(source_color, base_pos + ivec2(0, 1), 0);
87 color += texelFetch(source_color, base_pos + ivec2(1, 0), 0);
88 color += texelFetch(source_color, base_pos + ivec2(1, 1), 0);
89 color /= 4.0;
90 color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isinf(color));
91 color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isnan(color));
92
93 imageStore(dest_buffer, pos + params.target, color);
94 #endif
95
96 #ifdef MODE_GAUSSIAN_BLUR
97
98 // First pass copy texture into 16x16 local memory for every 8x8 thread block
99 vec2 quad_center_uv = clamp(vec2(params.section.xy + gl_GlobalInvocationID.xy + gl_LocalInvocationID.xy - 3.5) / params.section.zw, vec2(0.5 / params.section.zw), vec2(1.0 - 1.5 / params.section.zw));
100 uint dest_index = gl_LocalInvocationID.x * 2 + gl_LocalInvocationID.y * 2 * 16;
101
102 local_cache[dest_index] = textureLod(source_color, quad_center_uv, 0);
103 local_cache[dest_index + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.z, 0.0), 0);
104 local_cache[dest_index + 16] = textureLod(source_color, quad_center_uv + vec2(0.0, 1.0 / params.section.w), 0);
105 local_cache[dest_index + 16 + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.zw), 0);
106
107 #ifdef MODE_GLOW
108 if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
109 // Tonemap initial samples to reduce weight of fireflies: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
110 vec3 tonemap_col = vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0);
111 local_cache[dest_index] /= 1.0 + dot(local_cache[dest_index].rgb, tonemap_col);
112 local_cache[dest_index + 1] /= 1.0 + dot(local_cache[dest_index + 1].rgb, tonemap_col);
113 local_cache[dest_index + 16] /= 1.0 + dot(local_cache[dest_index + 16].rgb, tonemap_col);
114 local_cache[dest_index + 16 + 1] /= 1.0 + dot(local_cache[dest_index + 16 + 1].rgb, tonemap_col);
115 }
116 const float kernel[5] = { 0.2024, 0.1790, 0.1240, 0.0672, 0.0285 };
117 #else
118 // Simpler blur uses SIGMA2 for the gaussian kernel for a stronger effect.
119 const float kernel[4] = { 0.214607, 0.189879, 0.131514, 0.071303 };
120 #endif
121 memoryBarrierShared();
122 barrier();
123
124 // Horizontal pass. Needs to copy into 8x16 chunk of local memory so vertical pass has full resolution
125 uint read_index = gl_LocalInvocationID.x + gl_LocalInvocationID.y * 32 + 4;
126 vec4 color_top = vec4(0.0);
127 color_top += local_cache[read_index] * kernel[0];
128 color_top += local_cache[read_index + 1] * kernel[1];
129 color_top += local_cache[read_index + 2] * kernel[2];
130 color_top += local_cache[read_index + 3] * kernel[3];
131 color_top += local_cache[read_index - 1] * kernel[1];
132 color_top += local_cache[read_index - 2] * kernel[2];
133 color_top += local_cache[read_index - 3] * kernel[3];
134 #ifdef MODE_GLOW
135 color_top += local_cache[read_index + 4] * kernel[4];
136 color_top += local_cache[read_index - 4] * kernel[4];
137 #endif // MODE_GLOW
138
139 vec4 color_bottom = vec4(0.0);
140 color_bottom += local_cache[read_index + 16] * kernel[0];
141 color_bottom += local_cache[read_index + 1 + 16] * kernel[1];
142 color_bottom += local_cache[read_index + 2 + 16] * kernel[2];
143 color_bottom += local_cache[read_index + 3 + 16] * kernel[3];
144 color_bottom += local_cache[read_index - 1 + 16] * kernel[1];
145 color_bottom += local_cache[read_index - 2 + 16] * kernel[2];
146 color_bottom += local_cache[read_index - 3 + 16] * kernel[3];
147 #ifdef MODE_GLOW
148 color_bottom += local_cache[read_index + 4 + 16] * kernel[4];
149 color_bottom += local_cache[read_index - 4 + 16] * kernel[4];
150 #endif // MODE_GLOW
151
152 // rotate samples to take advantage of cache coherency
153 uint write_index = gl_LocalInvocationID.y * 2 + gl_LocalInvocationID.x * 16;
154
155 temp_cache[write_index] = color_top;
156 temp_cache[write_index + 1] = color_bottom;
157
158 memoryBarrierShared();
159 barrier();
160
161 // If destination outside of texture, can stop doing work now
162 if (any(greaterThanEqual(pos, params.section.zw))) {
163 return;
164 }
165
166 // Vertical pass
167 uint index = gl_LocalInvocationID.y + gl_LocalInvocationID.x * 16 + 4;
168 vec4 color = vec4(0.0);
169
170 color += temp_cache[index] * kernel[0];
171 color += temp_cache[index + 1] * kernel[1];
172 color += temp_cache[index + 2] * kernel[2];
173 color += temp_cache[index + 3] * kernel[3];
174 color += temp_cache[index - 1] * kernel[1];
175 color += temp_cache[index - 2] * kernel[2];
176 color += temp_cache[index - 3] * kernel[3];
177 #ifdef MODE_GLOW
178 color += temp_cache[index + 4] * kernel[4];
179 color += temp_cache[index - 4] * kernel[4];
180 #endif // MODE_GLOW
181
182 #ifdef MODE_GLOW
183 if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
184 // Undo tonemap to restore range: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
185 color /= 1.0 - dot(color.rgb, vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0));
186 }
187
188 color *= params.glow_strength;
189
190 if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
191 #ifdef GLOW_USE_AUTO_EXPOSURE
192
193 color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / params.glow_auto_exposure_scale;
194 #endif
195 color *= params.glow_exposure;
196
197 float luminance = max(color.r, max(color.g, color.b));
198 float feedback = max(smoothstep(params.glow_hdr_threshold, params.glow_hdr_threshold + params.glow_hdr_scale, luminance), params.glow_bloom);
199
200 color = min(color * feedback, vec4(params.glow_luminance_cap));
201 }
202 #endif // MODE_GLOW
203 imageStore(dest_buffer, pos + params.target, color);
204
205 #endif // MODE_GAUSSIAN_BLUR
206
207 #ifdef MODE_SIMPLE_COPY
208
209 vec4 color;
210 if (bool(params.flags & FLAG_COPY_ALL_SOURCE)) {
211 vec2 uv = vec2(pos) / vec2(params.section.zw);
212 if (bool(params.flags & FLAG_FLIP_Y)) {
213 uv.y = 1.0 - uv.y;
214 }
215 color = textureLod(source_color, uv, 0.0);
216
217 } else {
218 color = texelFetch(source_color, pos + params.section.xy, 0);
219
220 if (bool(params.flags & FLAG_FLIP_Y)) {
221 pos.y = params.section.w - pos.y - 1;
222 }
223 }
224
225 if (bool(params.flags & FLAG_FORCE_LUMINANCE)) {
226 color.rgb = vec3(max(max(color.r, color.g), color.b));
227 }
228
229 if (bool(params.flags & FLAG_ALPHA_TO_ONE)) {
230 color.a = 1.0;
231 }
232
233 if (bool(params.flags & FLAG_SANITIZE_INF_NAN)) {
234 color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isinf(color));
235 color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isnan(color));
236 }
237
238 imageStore(dest_buffer, pos + params.target, color);
239
240 #endif // MODE_SIMPLE_COPY
241
242 #ifdef MODE_SIMPLE_COPY_DEPTH
243
244 vec4 color = texelFetch(source_color, pos + params.section.xy, 0);
245
246 if (bool(params.flags & FLAG_FLIP_Y)) {
247 pos.y = params.section.w - pos.y - 1;
248 }
249
250 imageStore(dest_buffer, pos + params.target, vec4(color.r));
251
252 #endif // MODE_SIMPLE_COPY_DEPTH
253
254 #ifdef MODE_LINEARIZE_DEPTH_COPY
255
256 float depth = texelFetch(source_color, pos + params.section.xy, 0).r;
257 depth = depth * 2.0 - 1.0;
258 depth = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near));
259 vec4 color = vec4(depth / params.camera_z_far);
260
261 if (bool(params.flags & FLAG_FLIP_Y)) {
262 pos.y = params.section.w - pos.y - 1;
263 }
264
265 imageStore(dest_buffer, pos + params.target, color);
266 #endif // MODE_LINEARIZE_DEPTH_COPY
267
268 #if defined(MODE_OCTMAP_TO_PANORAMA) || defined(MODE_OCTMAP_ARRAY_TO_PANORAMA)
269
270 const float PI = 3.14159265359;
271 vec2 uv = vec2(pos) / vec2(params.section.zw);
272 if (bool(params.flags & FLAG_FLIP_Y)) {
273 uv.y = 1.0 - uv.y;
274 }
275 float phi = uv.x * 2.0 * PI;
276 float theta = uv.y * PI;
277
278 vec3 normal;
279 normal.x = sin(phi) * sin(theta) * -1.0;
280 normal.y = cos(theta);
281 normal.z = cos(phi) * sin(theta) * -1.0;
282
283 #ifdef MODE_OCTMAP_TO_PANORAMA
284 vec4 color = textureLod(source_color, vec3_to_oct_with_border(normal, params.octmap_border_size), params.camera_z_far); //the biggest the lod the least the acne
285 #else
286 vec4 color = textureLod(source_color, vec3(vec3_to_oct_with_border(normal, params.octmap_border_size), params.camera_z_far), 0.0); //the biggest the lod the least the acne
287 #endif
288 imageStore(dest_buffer, pos + params.target, color * params.luminance_multiplier);
289 #endif // defined(MODE_OCTMAP_TO_PANORAMA) || defined(MODE_OCTMAP_ARRAY_TO_PANORAMA)
290
291 #ifdef MODE_SET_COLOR
292 imageStore(dest_buffer, pos + params.target, params.set_color);
293 #endif
294 }