Debian Codegraph @

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

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
8
9 layout(set = 0, binding = 0) uniform sampler2D source_last_frame;
10 layout(set = 0, binding = 1) uniform sampler2D source_hiz;
11 layout(set = 0, binding = 2) uniform sampler2D source_normal_roughness;
12 layout(rgba16f, set = 0, binding = 3) uniform restrict writeonly image2D output_color;
13 layout(r8, set = 0, binding = 4) uniform restrict writeonly image2D output_mip_level;
14
15 layout(set = 0, binding = 5, std140) uniform SceneData {
16 mat4 projection[2];
17 mat4 inv_projection[2];
18 mat4 reprojection[2];
19 vec4 eye_offset[2];
20 }
21 scene_data;
22
23 layout(push_constant, std430) uniform Params {
24 ivec2 screen_size;
25 int mipmaps;
26 int num_steps;
27 float distance_fade;
28 float curve_fade_in;
29 float depth_tolerance;
30 bool orthogonal;
31 int view_index;
32 }
33 params;
34
35 vec2 compute_cell_count(int level) {
36 int cell_count_x = max(1, params.screen_size.x >> level);
37 int cell_count_y = max(1, params.screen_size.y >> level);
38 return vec2(cell_count_x, cell_count_y);
39 }
40
41 float linearize_depth(float depth) {
42 vec4 pos = vec4(0.0, 0.0, depth, 1.0);
43 pos = scene_data.inv_projection[params.view_index] * pos;
44 return pos.z / pos.w;
45 }
46
47 vec3 compute_view_pos(vec3 screen_pos) {
48 vec4 pos;
49 pos.xy = screen_pos.xy * 2.0 - 1.0;
50 pos.z = screen_pos.z;
51 pos.w = 1.0;
52 pos = scene_data.inv_projection[params.view_index] * pos;
53 return pos.xyz / pos.w;
54 }
55
56 vec3 compute_screen_pos(vec3 pos) {
57 vec4 screen_pos = scene_data.projection[params.view_index] * vec4(pos, 1.0);
58 screen_pos.xyz /= screen_pos.w;
59 screen_pos.xy = screen_pos.xy * 0.5 + 0.5;
60 return screen_pos.xyz;
61 }
62
63 // https://habr.com/ru/articles/744336/
64 vec3 compute_geometric_normal(ivec2 pixel_pos, float depth_c, vec3 view_c, float pixel_offset) {
65 vec4 H = vec4(
66 texelFetch(source_hiz, pixel_pos + ivec2(-1, 0), 0).x,
67 texelFetch(source_hiz, pixel_pos + ivec2(-2, 0), 0).x,
68 texelFetch(source_hiz, pixel_pos + ivec2(1, 0), 0).x,
69 texelFetch(source_hiz, pixel_pos + ivec2(2, 0), 0).x);
70
71 vec4 V = vec4(
72 texelFetch(source_hiz, pixel_pos + ivec2(0, -1), 0).x,
73 texelFetch(source_hiz, pixel_pos + ivec2(0, -2), 0).x,
74 texelFetch(source_hiz, pixel_pos + ivec2(0, 1), 0).x,
75 texelFetch(source_hiz, pixel_pos + ivec2(0, 2), 0).x);
76
77 vec2 he = abs((2.0 * H.xz - H.yw) - depth_c);
78 vec2 ve = abs((2.0 * V.xz - V.yw) - depth_c);
79
80 int h_sign = he.x < he.y ? -1 : 1;
81 int v_sign = ve.x < ve.y ? -1 : 1;
82
83 vec3 view_h = compute_view_pos(vec3((pixel_pos + vec2(h_sign, 0) + pixel_offset) / params.screen_size, H[1 + int(h_sign)]));
84 vec3 view_v = compute_view_pos(vec3((pixel_pos + vec2(0, v_sign) + pixel_offset) / params.screen_size, V[1 + int(v_sign)]));
85
86 vec3 h_der = h_sign * (view_h - view_c);
87 vec3 v_der = v_sign * (view_v - view_c);
88
89 return cross(v_der, h_der);
90 }
91
92 #define M_PI 3.14159265359
93
94 void main() {
95 ivec2 pixel_pos = ivec2(gl_GlobalInvocationID.xy);
96
97 if (any(greaterThanEqual(pixel_pos, params.screen_size))) {
98 return;
99 }
100
101 vec4 color = vec4(0.0);
102 float mip_level = 0.0;
103
104 vec3 screen_pos;
105 screen_pos.xy = vec2(pixel_pos + 0.5) / params.screen_size;
106 screen_pos.z = texelFetch(source_hiz, pixel_pos, 0).x;
107
108 bool should_trace = screen_pos.z != 0.0;
109 if (should_trace) {
110 vec3 pos = compute_view_pos(screen_pos);
111
112 vec4 normal_roughness = texelFetch(source_normal_roughness, pixel_pos, 0);
113 vec3 normal = normalize(normal_roughness.xyz * 2.0 - 1.0);
114 float roughness = normal_roughness.w;
115 if (roughness > 0.5) {
116 roughness = 1.0 - roughness;
117 }
118 roughness /= (127.0 / 255.0);
119
120 // Do not compute SSR for rough materials to improve
121 // performance at the cost of subtle artifacting.
122 if (roughness >= 0.7) {
123 imageStore(output_color, pixel_pos, vec4(0.0));
124 imageStore(output_mip_level, pixel_pos, vec4(0.0));
125 return;
126 }
127
128 vec3 geom_normal = normalize(compute_geometric_normal(pixel_pos, screen_pos.z, pos, 0.5));
129
130 // Add a small bias towards the geometry normal to prevent self intersections.
131 pos += geom_normal * (1.0 - pow(clamp(dot(normal, geom_normal), 0.0, 1.0), 8.0));
132 screen_pos = compute_screen_pos(pos);
133
134 vec3 view_dir = params.orthogonal ? vec3(0.0, 0.0, -1.0) : normalize(pos + scene_data.eye_offset[params.view_index].xyz);
135 vec3 ray_dir = normalize(reflect(view_dir, normal));
136
137 // Check if the ray is immediately intersecting with itself. If so, bounce!
138 if (dot(ray_dir, geom_normal) < 0.0) {
139 ray_dir = normalize(reflect(ray_dir, geom_normal));
140 }
141
142 vec3 end_pos = pos + ray_dir;
143
144 // Clip to near plane. Add a small bias so we don't go to infinity.
145 if (end_pos.z > 0.0) {
146 end_pos -= ray_dir / ray_dir.z * (end_pos.z + 0.00001);
147 }
148
149 vec3 screen_end_pos = compute_screen_pos(end_pos);
150
151 // Normalize Z to -1.0 or +1.0 and do parametric T tracing as suggested here:
152 // https://hacksoflife.blogspot.com/2020/10/a-tip-for-hiz-ssr-parametric-t-tracing.html
153 vec3 screen_ray_dir = screen_end_pos - screen_pos;
154 screen_ray_dir /= abs(screen_ray_dir.z);
155
156 bool facing_camera = screen_ray_dir.z >= 0.0;
157
158 // Find the screen edge point where we will stop tracing.
159 vec2 t0 = (vec2(0.0) - screen_pos.xy) / screen_ray_dir.xy;
160 vec2 t1 = (vec2(1.0) - screen_pos.xy) / screen_ray_dir.xy;
161 vec2 t2 = max(t0, t1);
162 float t_max = min(t2.x, t2.y);
163
164 vec2 cell_step = vec2(screen_ray_dir.x < 0.0 ? -1.0 : 1.0, screen_ray_dir.y < 0.0 ? -1.0 : 1.0);
165
166 int cur_level = 0;
167 int cur_iteration = params.num_steps;
168
169 // Advance the start point to the closest next cell to prevent immediate self intersection.
170 float t;
171 {
172 vec2 cell_index = floor(screen_pos.xy * params.screen_size);
173 vec2 new_cell_index = cell_index + clamp(cell_step, vec2(0.0), vec2(1.0));
174 vec2 new_cell_pos = (new_cell_index / params.screen_size) + cell_step * 0.000001;
175 vec2 pos_t = (new_cell_pos - screen_pos.xy) / screen_ray_dir.xy;
176 float edge_t = min(pos_t.x, pos_t.y);
177
178 t = edge_t;
179 }
180
181 while (cur_level >= 0 && cur_iteration > 0 && t < t_max) {
182 vec3 cur_screen_pos = screen_pos + screen_ray_dir * t;
183
184 vec2 cell_count = compute_cell_count(cur_level);
185 vec2 cell_index = floor(cur_screen_pos.xy * cell_count);
186 float cell_depth = texelFetch(source_hiz, ivec2(cell_index), cur_level).x;
187 float depth_t = (cell_depth - screen_pos.z) * screen_ray_dir.z; // Z is either -1.0 or 1.0 so we don't need to do a divide.
188
189 vec2 new_cell_index = cell_index + clamp(cell_step, vec2(0.0), vec2(1.0));
190 vec2 new_cell_pos = (new_cell_index / cell_count) + cell_step * 0.000001;
191 vec2 pos_t = (new_cell_pos - screen_pos.xy) / screen_ray_dir.xy;
192 float edge_t = min(pos_t.x, pos_t.y);
193
194 bool hit = facing_camera ? (t <= depth_t) : (depth_t <= edge_t);
195 int mip_offset = hit ? -1 : +1;
196
197 if (cur_level == 0) {
198 float z0 = linearize_depth(cell_depth);
199 float z1 = linearize_depth(cur_screen_pos.z);
200
201 if ((z0 - z1) > params.depth_tolerance) {
202 hit = false;
203 mip_offset = 0; // Keep the mip index the same to prevent it from decreasing and increasing in repeat.
204 }
205 }
206
207 if (hit) {
208 if (!facing_camera) {
209 t = max(t, depth_t);
210 }
211 } else {
212 t = edge_t;
213 }
214
215 cur_level = min(cur_level + mip_offset, params.mipmaps - 1);
216 --cur_iteration;
217 }
218
219 vec3 cur_screen_pos = screen_pos + screen_ray_dir * t;
220
221 vec4 reprojected_pos;
222 reprojected_pos.xy = cur_screen_pos.xy * 2.0 - 1.0;
223 reprojected_pos.z = cur_screen_pos.z;
224 reprojected_pos.w = 1.0;
225 reprojected_pos = scene_data.reprojection[params.view_index] * reprojected_pos;
226 reprojected_pos.xy = reprojected_pos.xy / reprojected_pos.w * 0.5 + 0.5;
227
228 // Instead of hard rejecting samples, write sample validity to the alpha channel.
229 // This allows invalid samples to write mip levels to let valid samples have smoother roughness transitions.
230 float validity = 1.0;
231
232 // Hit validation logic is referenced from here:
233 // https://github.com/GPUOpen-Effects/FidelityFX-SSSR/blob/master/ffx-sssr/ffx_sssr.h
234
235 ivec2 cur_pixel_pos = ivec2(cur_screen_pos.xy * params.screen_size);
236
237 float hit_depth = texelFetch(source_hiz, cur_pixel_pos, 0).x;
238 if (t >= t_max || hit_depth == 0.0) {
239 validity = 0.0;
240 }
241
242 if (all(lessThan(abs(screen_ray_dir.xy * t), 2.0 / params.screen_size))) {
243 vec3 hit_normal = texelFetch(source_normal_roughness, cur_pixel_pos, 0).xyz * 2.0 - 1.0;
244 if (dot(ray_dir, hit_normal) >= 0.0) {
245 validity = 0.0;
246 }
247 }
248
249 vec3 cur_pos = compute_view_pos(cur_screen_pos);
250 vec3 hit_pos = compute_view_pos(vec3(cur_screen_pos.xy, hit_depth));
251
252 float delta = length(cur_pos - hit_pos);
253 float confidence = 1.0 - smoothstep(0.0, params.depth_tolerance, delta);
254 validity *= clamp(confidence * confidence, 0.0, 1.0);
255
256 float margin_blend = 1.0;
257 vec2 reprojected_pixel_pos = reprojected_pos.xy * params.screen_size;
258
259 vec2 margin = vec2((params.screen_size.x + params.screen_size.y) * 0.05); // Make a uniform margin.
260 {
261 // Blend fading out towards inner margin.
262 // 0.5 = midpoint of reflection
263 vec2 margin_grad = mix(params.screen_size - reprojected_pixel_pos, reprojected_pixel_pos, lessThan(reprojected_pixel_pos, params.screen_size * 0.5));
264 margin_blend = smoothstep(0.0, margin.x * margin.y, margin_grad.x * margin_grad.y);
265 }
266
267 float ray_len = length(screen_ray_dir.xy * t);
268
269 // Fade In / Fade Out
270 float grad = ray_len;
271 float fade_in = params.curve_fade_in == 0.0 ? 1.0 : pow(clamp(grad, 0.0, 1.0), params.curve_fade_in);
272 float fade_out = params.distance_fade == 0.0 ? 1.0 : pow(clamp(1.0 - grad, 0.0, 1.0), params.distance_fade);
273 float fade = fade_in * fade_out;
274
275 // Ensure that precision errors do not introduce any fade. Even if it is just slightly below 1.0,
276 // strong specular light can leak through the reflection.
277 if (fade > 0.999) {
278 fade = 1.0;
279 }
280
281 validity *= fade * margin_blend;
282
283 if (validity > 0.0) {
284 color = vec4(textureLod(source_last_frame, reprojected_pos.xy, 0).xyz, 1.0) * validity;
285
286 // Tone map the SSR color to have smoother roughness filtering across samples with varying luminance.
287 const vec3 rec709_luminance_weights = vec3(0.2126, 0.7152, 0.0722);
288 color.rgb /= 1.0 + dot(color.rgb, rec709_luminance_weights);
289 }
290
291 if (roughness > 0.001) {
292 float cone_angle = min(roughness, 0.999) * M_PI * 0.5;
293 float cone_len = ray_len;
294 float op_len = 2.0 * tan(cone_angle) * cone_len; // Opposite side of iso triangle.
295 float blur_radius;
296 {
297 // Fit to sphere inside cone (sphere ends at end of cone), something like this:
298 // ___
299 // \O/
300 // V
301 //
302 // as it avoids bleeding from beyond the reflection as much as possible. As a plus
303 // it also makes the rough reflection more elongated.
304 float a = op_len;
305 float h = cone_len;
306 float a2 = a * a;
307 float fh2 = 4.0 * h * h;
308 blur_radius = (a * (sqrt(a2 + fh2) - a)) / (4.0 * h);
309 }
310
311 mip_level = clamp(log2(blur_radius * max(params.screen_size.x, params.screen_size.y) / 16.0), 0, params.mipmaps - 1);
312 }
313
314 // Because we still write mip level for invalid pixels to allow for smooth roughness transitions,
315 // this sometimes ends up creating a pyramid-like shape at very rough levels.
316 // We can fade the mip level near the end to make it significantly less visible.
317 mip_level *= pow(clamp(1.25 - ray_len, 0.0, 1.0), 0.2);
318 }
319
320 imageStore(output_color, pixel_pos, color);
321 imageStore(output_mip_level, pixel_pos, vec4(mip_level / 14.0, 0.0, 0.0, 0.0));
322 }