Blender V5.3
eevee_ray_denoise.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#pragma once
14
15#include "draw_math_geom.bsl.hh"
16#include "draw_view.bsl.hh"
17#include "eevee_closure.bsl.hh"
19#include "eevee_defines.hh"
20#include "eevee_filter.bsl.hh"
24#include "eevee_uniform.bsl.hh"
25#include "gpu_shader_codegen_lib.glsl"
29
31
33{
34 /* From top left in clockwise order. */
35 float4 weights;
36 weights.x = (1.0f - co.x) * co.y;
37 weights.y = co.x * co.y;
38 weights.z = co.x * (1.0f - co.y);
39 weights.w = (1.0f - co.x) * (1.0f - co.y);
40 return weights;
41}
42
43struct TileBuffer {
44 [[storage(4, read)]] uint (&tiles_coord_buf)[];
45
46 uint2 tile_coord(uint tile_id)
47 {
48 return unpackUvec2x16(tiles_coord_buf[tile_id]);
49 }
50};
51
56
58
62
65
67
69
71
72 /* Tag pixel radiance as invalid. */
79
80 /* Used for bilateral sampling. */
82 [[resource_table]] const Uniform &uni,
83 ViewMatrices view,
84 float3 center_N,
85 float3 center_P,
86 int2 sample_texel) const
87 {
88 int2 sample_texel_fullres = sample_texel * uni.raytrace_buf.trace_pixel_scale +
89 uni.raytrace_buf.trace_pixel_offset;
90
91 float sample_depth = texelFetch(depth_tx, sample_texel_fullres, 0).r;
92
93 float2 sample_uv = float2(sample_texel_fullres) * uni.raytrace_buf.full_resolution_inv;
94 float3 sample_N = reader.read_bin(sample_texel_fullres, closure_index).N;
95 float3 sample_P = view.point_screen_to_world(float3(sample_uv, sample_depth));
96
97 /* TODO(fclem): Scene parameter. 10000.0f is dependent on scene scale. */
98 float depth_weight = filters::planar_weight(center_N, center_P, sample_P, 10000.0f);
99 float normal_weight = filters::angle_weight(center_N, sample_N);
100 /* Some pixels might have no correct weight (depth & normal weights being very small).
101 * To avoid them have invalid energy (because of float precision),
102 * we weight all valid samples by a very small amount. */
103 float epsilon_weight = 1e-4f;
104
105 return max(epsilon_weight, depth_weight * normal_weight);
106 }
107};
108
110{
111 switch (cl.type) {
114 break;
115 case CLOSURE_NONE_ID:
118 case CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID:
121 break;
122 }
123}
124
133[[compute,
134 local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE),
135 /* Metal: Provide compiler with hint to tune per-thread resource allocation. */
138 [[resource_table]] const gbuffer::Reader &reader,
139 [[resource_table]] const Uniform &uni,
140 [[resource_table]] const draw::View &views,
141 [[resource_table]] const Sampling &sampling,
142 [[resource_table]] const UtilityTexture &util_tx,
143 [[work_group_id]] const uint3 work_group,
144 [[local_invocation_id]] const uint3 local_id)
145{
146 [[resource_table]] TileBuffer &tiles = srt.tiles;
147 uint2 tile_coord = tiles.tile_coord(work_group.x);
148
149 const ViewMatrices view = views.get(0);
150
151 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
152 int2 texel_fullres = int2(local_id.xy + tile_coord * tile_size);
153
154 /* Tracing resolution texel. */
155 int2 texel_shifted = max(int2(0), texel_fullres - uni.raytrace_buf.trace_pixel_offset);
156 int2 texel_nearest = texel_shifted / srt.raytrace_resolution_scale;
157 float2 bilinear_co = fract(float2(texel_shifted) / float2(srt.raytrace_resolution_scale));
158
159 if (srt.skip_denoise) {
160 if (srt.raytrace_resolution_scale == 1) {
161 /* No need for complicated upsampling. */
163 srt.out_radiance_img, texel_fullres, imageLoad(srt.ray_radiance_img, texel_fullres));
164 return;
165 }
166
167 /* Simple bilateral upsampling without any denoising. */
168 float center_depth = texelFetch(srt.depth_tx, texel_fullres, 0).r;
169 float2 center_uv = float2(texel_fullres) * uni.raytrace_buf.full_resolution_inv;
170 float3 center_N = reader.read_bin(texel_fullres, srt.closure_index).N;
171 float3 center_P = view.point_screen_to_world(float3(center_uv, center_depth));
172
173 float4 bilinear_weights = bilinear_weights_from_subpixel_coord(bilinear_co);
174
175 float4 bilateral_weights = float4(
176 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest + int2(0, 1)),
177 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest + int2(1, 1)),
178 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest + int2(1, 0)),
179 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest + int2(0, 0)));
180
181 float4 ray_pdf_inv = float4(imageLoad(srt.ray_data_img, texel_nearest + int2(0, 1)).w,
182 imageLoad(srt.ray_data_img, texel_nearest + int2(1, 1)).w,
183 imageLoad(srt.ray_data_img, texel_nearest + int2(1, 0)).w,
184 imageLoad(srt.ray_data_img, texel_nearest + int2(0, 0)).w);
185 float4 ray_validity = float4(not(equal(ray_pdf_inv, float4(0.0f))));
186
187 float4 ray_radiance0 = imageLoad(srt.ray_radiance_img, texel_nearest + int2(0, 1));
188 float4 ray_radiance1 = imageLoad(srt.ray_radiance_img, texel_nearest + int2(1, 1));
189 float4 ray_radiance2 = imageLoad(srt.ray_radiance_img, texel_nearest + int2(1, 0));
190 float4 ray_radiance3 = imageLoad(srt.ray_radiance_img, texel_nearest + int2(0, 0));
191
192 float4 weights = ray_validity * bilinear_weights * bilateral_weights;
193
194 float4 radiance;
195 radiance = colorspace::log_from_scene_linear(ray_radiance0) * weights.x;
196 radiance += colorspace::log_from_scene_linear(ray_radiance1) * weights.y;
197 radiance += colorspace::log_from_scene_linear(ray_radiance2) * weights.z;
198 radiance += colorspace::log_from_scene_linear(ray_radiance3) * weights.w;
199 radiance *= safe_rcp(radiance.w);
200 radiance = colorspace::scene_linear_from_log(radiance);
201
202 imageStore(srt.out_radiance_img, texel_fullres, radiance);
203 return;
204 }
205
206 /* Clear neighbor tiles that will not be processed. */
207 /* TODO(fclem): Optimize this. We don't need to clear the whole ring. */
208 for (int x = -1; x <= 1; x++) {
209 for (int y = -1; y <= 1; y++) {
210 if (x == 0 && y == 0) {
211 continue;
212 }
213
214 int2 tile_coord_neighbor = int2(tile_coord) + int2(x, y);
215 if (!in_image_range(tile_coord_neighbor, srt.tile_mask_img)) {
216 continue;
217 }
218
219 int3 sample_tile = int3(tile_coord_neighbor, srt.closure_index);
220
221 uint tile_mask = imageLoadFast(srt.tile_mask_img, sample_tile).r;
222 bool tile_is_unused = !flag_test(tile_mask, 1u << 0u);
223 if (tile_is_unused) {
224 int2 texel_fullres_neighbor = texel_fullres + int2(x, y) * int(tile_size);
225
226 if (in_texture_range(texel_fullres, reader.gbuf_header_tx)) {
227 srt.invalid_pixel_write(texel_fullres_neighbor);
228 }
229 }
230 }
231 }
232
233 gbuffer::Header gbuf_header = reader.read_header(texel_fullres);
234
235 ClosureUndetermined closure = reader.read_bin(texel_fullres, srt.closure_index);
236
237 if (closure.type == CLOSURE_NONE_ID) {
238 srt.invalid_pixel_write(texel_fullres);
239 return;
240 }
241
242 float2 noise = util_tx.fetch(float2(texel_fullres), UTIL_BLUE_NOISE_LAYER).ba;
243 noise = fract(noise + sampling.rng_1D_get(SAMPLING_CLOSURE));
244
245 int2 center_sample_texel = texel_nearest;
246 if (srt.raytrace_resolution_scale != 1) {
247 /* Jitter sample position to recover bilinear interpolation. */
248 center_sample_texel += int2(greaterThan(bilinear_co, noise));
249 }
250 /* Denoise using the tracing pixel context (view vector and position) instead of the full
251 * resolution pixel. This allows to have perfect weighting for the center sample of the denoising
252 * kernel. If the center sample is always valid (yielding a nearest interpolation upsampling for
253 * mirror reflection), we can then use the above jittering to recover the bilinear filtering at
254 * lower tracing resolution. */
255 float2 uv = (float2(center_sample_texel) + 0.5f) * uni.raytrace_buf.full_resolution_inv *
256 float(srt.raytrace_resolution_scale);
257
258 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
259 float3 vs_P = view.point_screen_to_view(float3(uv, depth));
260 float scene_z = vs_P.z;
261 float3 P = view.point_view_to_world(vs_P);
263
264 Thickness thickness = reader.read_thickness(gbuf_header, texel_fullres);
265 if (thickness.value() != 0.0f) {
267 }
268
269 /* Compute filter size and needed sample count */
270 float apparent_roughness = closure_apparent_roughness_get(closure);
271 /* Max filter size at 0.25 roughness. */
272 float filter_size_factor = saturate(apparent_roughness * 8.0f);
273 uint sample_count = 1u + uint(floor(15.0f * filter_size_factor + 0.5f));
274 float filter_radius = 8.0f * sqrt(filter_size_factor);
275 /* NOTE: filter_size should never be greater than RAYTRACE_GROUP_SIZE. Otherwise, the
276 * reconstruction can becomes ill defined since we don't know if further tiles are valid. */
277 float max_filter_radius = float(RAYTRACE_GROUP_SIZE - 1);
278 float min_filter_radius = 0.0f;
279 if (srt.raytrace_resolution_scale > 1) {
280 /* Filter at least 1 trace pixel to fight the undersampling. */
281 min_filter_radius = 1.5f;
282 sample_count = max(sample_count, 5u);
283 }
284
285 float3 rgb_moment = float3(0.0f);
286 float3 radiance_accum = float3(0.0f);
287 float weight_accum = 0.0f;
288 float closest_hit_time = 1.0e10f;
289
290 /* In order to avoid costly texture fetches, we assume the neighbors to be on the same plane as
291 * the shading point. We compute the fake surface derivatives form the normal. */
292 float3 vs_N = view.normal_world_to_view(closure.N);
293 float2 pixel_uv_size = uni.raytrace_buf.full_resolution_inv *
294 float(srt.raytrace_resolution_scale);
295 float3 vs_Pdx = view.point_screen_to_view(float3(uv + float2(pixel_uv_size.x, 0.0), depth));
296 float3 vs_Pdy = view.point_screen_to_view(float3(uv + float2(0.0, pixel_uv_size.y), depth));
297 float2x3 dPdxy;
298 dPdxy[0] = line_plane_intersect(vs_Pdx, view.view_incident_vector(vs_Pdx), vs_P, vs_N) - vs_P;
299 dPdxy[1] = line_plane_intersect(vs_Pdy, view.view_incident_vector(vs_Pdy), vs_P, vs_N) - vs_P;
300 dPdxy[0] = view.normal_view_to_world(dPdxy[0]);
301 dPdxy[1] = view.normal_view_to_world(dPdxy[1]);
302 /* Unfortunately the above heuristic introduces high variance at higher roughness.
303 * Contacts are not so important at higher roughness so we can roll off the heuristic. */
304 float bias = max(0.2f, saturate((0.75f - apparent_roughness) / (0.75f - 0.3f)));
305 dPdxy[0] *= bias;
306 dPdxy[1] *= bias;
307
308 /* Orient filter in reflection direction. */
309 /* Note: Anisotropic BSDFs will likely need to align rotation with their tangent instead and
310 * override the aspect ratio computation. */
311 float2 filter_up = safe_normalize(float2(vs_N.xy));
312 float2x2 filter_rotation = float2x2(filter_up, orthogonal(filter_up));
313 /* Small roughness GGX lobe is quite stretched. Stretch the filter kernel in that direction.
314 * Modulate by quality since this increases variance. */
315 float aspect = 1.0f - 0.5f * saturate(min(apparent_roughness * 12.0f,
316 2.0f - apparent_roughness * 5.0f));
317
318 filter_rotation[0] *= clamp(filter_radius, min_filter_radius, max_filter_radius);
319 filter_rotation[1] *= clamp(filter_radius * aspect, min_filter_radius, max_filter_radius);
320
321 for (uint i = 0u; i < sample_count; i++) {
322 float2 Xi = random::hammersley_2d(i, sample_count);
323 /* Only randomize rotation (Y component of the noise). We want to always sample the center
324 * pixel. Scaling the noise instead of rotating preserve cache locality. */
325 Xi.y *= 1.0f - (noise.x / float(sample_count));
326
327 float2 offset_f = filter_rotation * sample_disk(Xi);
328 int2 offset = int2(floor(offset_f + 0.5f));
329
330 int2 sample_texel = center_sample_texel + offset;
331
332 float4 ray_data = imageLoad(srt.ray_data_img, sample_texel);
333 float ray_time = imageLoad(srt.ray_time_img, sample_texel).r;
334 float4 ray_radiance = imageLoad(srt.ray_radiance_img, sample_texel);
335
336 float3 ray_direction = ray_data.xyz;
337 float ray_pdf_inv = abs(ray_data.w);
338 /* Skip invalid pixels. */
339 if (ray_pdf_inv == 0.0f) {
340 continue;
341 }
342
343 closest_hit_time = min(closest_hit_time, ray_time);
344
345 /* Correct ray hit position.
346 * Instead of just reusing the ray direction (leading to screen space blur, loosing contact
347 * sharpness), get the neighbor pixel position and reconstruct the actual hit position. */
348 ray_direction = safe_normalize(dPdxy * float2(offset) + ray_direction * ray_time);
349
350 /* Slide 54. */
351 /* The reference is wrong.
352 * The ratio estimator is `pdf_local / pdf_ray` instead of `bsdf_local / pdf_ray`. */
353 float pdf = closure_evaluate_pdf(closure, ray_direction, V, thickness);
354 /* Avoid the weight exploding and summing to infinity. */
355 float weight = min(1e30f, pdf * ray_pdf_inv);
356
357 float3 log_radiance = colorspace::log_from_scene_linear(ray_radiance.rgb);
358
359 radiance_accum += log_radiance * weight;
360 weight_accum += weight;
361
362 /* Use scene linear radiance to better estimate noise. */
363 rgb_moment += square(log_radiance) * weight;
364 }
365 float inv_weight = safe_rcp(weight_accum);
366 radiance_accum *= inv_weight;
367
368 /* Use radiance sum as signal mean. */
369 float3 rgb_mean = radiance_accum;
370 rgb_moment *= inv_weight;
371
372 float3 rgb_variance = abs(rgb_moment - square(rgb_mean)) * safe_rcp(rgb_mean);
373 float hit_variance = reduce_max(rgb_variance);
374
375 float hit_depth = view.depth_view_to_screen(scene_z - closest_hit_time);
376
377 radiance_accum = colorspace::scene_linear_from_log(radiance_accum);
378 imageStoreFast(srt.out_radiance_img, texel_fullres, float4(radiance_accum, 0.0f));
379 imageStoreFast(srt.out_variance_img, texel_fullres, float4(hit_variance));
380 imageStoreFast(srt.out_hit_depth_img, texel_fullres, float4(hit_depth));
381}
382
391
394
397
399
401
403
406
409
411
413
415 {
416 float3 center_radiance_YCoCg = colorspace::YCoCg_from_scene_linear(center_radiance);
417
418 /* Build Local statistics (slide 46). */
419 LocalStatistics result;
420 result.mean = center_radiance_YCoCg;
421 result.moment = square(center_radiance_YCoCg);
422 float weight_accum = 1.0f;
423
424 for (int x = -1; x <= 1; x++) {
425 for (int y = -1; y <= 1; y++) {
426 if (x == 0 && y == 0) {
427 continue;
428 }
429
430 int2 neighbor_texel = texel + int2(x, y);
431 if (!in_image_range(neighbor_texel, in_radiance_img)) {
432 continue;
433 }
434 float3 radiance = imageLoad(in_radiance_img, neighbor_texel).rgb;
435 /* Exclude unprocessed pixels. */
436 if (all(equal(radiance, FLT_11_11_10_MAX))) {
437 continue;
438 }
439
440 /* Weight corners less to avoid box artifacts.
441 * Same idea as in "High Quality Temporal Supersampling" by Brian Karis at SIGGRAPH 2014
442 * (Slide 32) Simple clamp to min/max of 8 neighbors results in 3x3 box artifacts. */
443 /* TODO(@fclem): Evaluate if beneficial. Currently not use to soak more noise? Unsure. */
444 // float weight = (abs(x) == abs(y)) ? 0.25f : 1.0f;
445 /* Use YCoCg for clamping and accumulation to avoid color shift artifacts. */
446 float3 radiance_YCoCg = colorspace::YCoCg_from_scene_linear(radiance.rgb);
447 result.mean += radiance_YCoCg;
448 result.moment += square(radiance_YCoCg);
449 weight_accum += 1.0f;
450 }
451 }
452 float inv_weight = safe_rcp(weight_accum);
453 result.mean *= inv_weight;
454 result.moment *= inv_weight;
455 result.variance = abs(result.moment - square(result.mean));
456 result.deviation = max(float3(1e-4f), sqrt(result.variance));
457 result.clamp_min = result.mean - result.deviation;
458 result.clamp_max = result.mean + result.deviation;
459 return result;
460 }
461
462 float4 history_validate(int2 texel, float bilinear_weight, float3 history_radiance)
463 {
464 /* Out of history view. Return sample without weight. */
466 return float4(0.0f);
467 }
468
469 int3 history_tile = int3(texel / RAYTRACE_GROUP_SIZE, closure_index);
470 /* Fetch previous tilemask to avoid loading invalid data. */
471 bool is_valid_history = texelFetch(tilemask_history_tx, history_tile, 0).r != 0;
472 /* Exclude unprocessed pixels. */
473 if (!is_valid_history) {
474 return float4(0.0f);
475 }
476 /* Exclude unprocessed pixels. */
477 if (all(equal(history_radiance, FLT_11_11_10_MAX))) {
478 return float4(0.0f);
479 }
480 return float4(history_radiance * bilinear_weight, bilinear_weight);
481 }
482
484 float3 P,
485 LocalStatistics local)
486 {
487 float2 uv = project_point(uni.raytrace_buf.denoise_history_persmat, P).xy * 0.5f + 0.5f;
488
489 float2 tex_size = float2(textureSize(radiance_history_tx, 0).xy);
490 float2 texel_co = uv * tex_size - 0.5f;
491 float2 round_co = floor(texel_co);
492 float2 fract_co = texel_co - round_co;
493 float4 bilinear_weights = bilinear_weights_from_subpixel_coord(fract_co);
494 int2 texel = int2(round_co);
495
496 /* Make sure to sample the same quad with textureGather. */
497 float2 safe_uv = (round_co + 0.5f) / tex_size;
498 /* Radiance needs to be manually interpolated because any pixel might contain invalid data. */
499 float4 r_samples = textureGather(radiance_history_tx, safe_uv, 0);
500 float4 g_samples = textureGather(radiance_history_tx, safe_uv, 1);
501 float4 b_samples = textureGather(radiance_history_tx, safe_uv, 2);
502 float4x3 gather4 = transpose(float3x4(r_samples, g_samples, b_samples));
503
504 float4 history_radiance;
505 history_radiance = history_validate(texel + int2(0, 1), bilinear_weights.x, gather4[0]);
506 history_radiance += history_validate(texel + int2(1, 1), bilinear_weights.y, gather4[1]);
507 history_radiance += history_validate(texel + int2(1, 0), bilinear_weights.z, gather4[2]);
508 history_radiance += history_validate(texel + int2(0, 0), bilinear_weights.w, gather4[3]);
509
510 /* Use YCoCg for clamping and accumulation to avoid color shift artifacts. */
511 float4 history_radiance_YCoCg;
512 history_radiance_YCoCg.rgb = colorspace::YCoCg_from_scene_linear(history_radiance.rgb);
513 history_radiance_YCoCg.a = history_radiance.a;
514
515 /* Weighted contribution (slide 46). */
516 float3 dist = abs(history_radiance_YCoCg.rgb - local.mean) / local.deviation;
517 float weight = exp2(-4.0f * dot(dist, float3(1.0f)));
518
519 return history_radiance_YCoCg * weight;
520 }
521
523 {
524 float2 uv = project_point(uni.raytrace_buf.denoise_history_persmat, P).xy * 0.5f + 0.5f;
525
526 if (!in_range_exclusive(uv, float2(0.0f), float2(1.0f))) {
527 /* Out of history view. Return sample without weight. */
528 return float2(0.0f);
529 }
530
531 /* This samples the history with bilinear filter. This can read invalid data that are outside
532 * the validity mask. But we make sure to clear the texture to 0 to avoid sampling NaN.
533 * The resulting undefined behavior (sampling untouched history form multiple frames ago) is
534 * not a huge concern as it will decay into a finite variance value. Moreover, the result is
535 * deterministic in render. */
536 float history_variance = texture(variance_history_tx, uv).r;
537
538 int2 history_texel = int2(floor(uv * float2(textureSize(variance_history_tx, 0).xy)));
539 int3 history_tile = int3(history_texel / RAYTRACE_GROUP_SIZE, closure_index);
540 /* Fetch previous tilemask to avoid loading invalid data. */
541 bool is_valid_history = texelFetch(tilemask_history_tx, history_tile, 0).r != 0;
542
543 if (is_valid_history) {
544 return float2(history_variance, 1.0f);
545 }
546 return float2(0.0f);
547 }
548};
549
558[[compute,
559 local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE),
560 /* Metal: Provide compiler with hint to tune per-thread resource allocation. */
563 [[resource_table]] const draw::View &views,
564 [[resource_table]] const Uniform &uni,
565 [[work_group_id]] const uint3 work_group,
566 [[local_invocation_id]] const uint3 local_id)
567{
568 [[resource_table]] TileBuffer &tiles = srt.tiles;
569 uint2 tile_coord = tiles.tile_coord(work_group.x);
570
571 const ViewMatrices view = views.get(0);
572
573 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
574 int2 texel_fullres = int2(local_id.xy + tile_coord * tile_size);
575 float2 uv = (float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
576
577 /* Clear neighbor tiles that will not be processed. */
578 /* TODO(fclem): Optimize this. We don't need to clear the whole ring. */
579 for (int x = -1; x <= 1; x++) {
580 for (int y = -1; y <= 1; y++) {
581 if (x == 0 && y == 0) {
582 continue;
583 }
584
585 int2 tile_coord_neighbor = int2(tile_coord) + int2(x, y);
586 if (!in_image_range(tile_coord_neighbor, srt.tile_mask_img)) {
587 continue;
588 }
589
590 int3 sample_tile = int3(tile_coord_neighbor, srt.closure_index);
591
592 uint tile_mask = imageLoadFast(srt.tile_mask_img, sample_tile).r;
593 bool tile_is_unused = !flag_test(tile_mask, 1u << 0u);
594 if (tile_is_unused) {
595 int2 texel_fullres_neighbor = texel_fullres + int2(x, y) * int(tile_size);
596 imageStore(srt.out_variance_img, texel_fullres_neighbor, float4(0.0f));
597 }
598 }
599 }
600
601 /* Check if texel is out of bounds,
602 * so we can utilize fast texture functions and early-out if not. */
603 if (any(greaterThanEqual(texel_fullres, imageSize(srt.in_radiance_img).xy))) {
604 return;
605 }
606
607 float in_variance = imageLoadFast(srt.in_variance_img, texel_fullres).r;
608 float3 in_radiance = imageLoadFast(srt.in_radiance_img, texel_fullres).rgb;
609
610 if (all(equal(in_radiance, FLT_11_11_10_MAX))) {
611 /* Early out on pixels that were marked unprocessed by the previous pass. */
612 imageStoreFast(srt.out_radiance_img, texel_fullres, float4(FLT_11_11_10_MAX, 0.0f));
613 imageStoreFast(srt.out_variance_img, texel_fullres, float4(0.0f));
614 return;
615 }
616
617 LocalStatistics local = srt.local_statistics_get(texel_fullres, in_radiance);
618
619 /* Radiance. */
620
621 /* Surface reprojection. */
622 /* TODO(fclem): Use per pixel velocity. Is this worth it? */
623 float scene_depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
624 float3 P = view.point_screen_to_world(float3(uv, scene_depth));
625 float4 history_radiance = srt.radiance_history_sample(uni, P, local);
626 /* Reflection reprojection. */
627 float hit_depth = imageLoadFast(srt.hit_depth_img, texel_fullres).r;
628 float3 P_hit = view.point_screen_to_world(float3(uv, hit_depth));
629 history_radiance += srt.radiance_history_sample(uni, P_hit, local);
630 /* Finalize accumulation. */
631 history_radiance *= safe_rcp(history_radiance.w);
632 /* Clamp resulting history radiance (slide 47). */
633 history_radiance.rgb = clamp(history_radiance.rgb, local.clamp_min, local.clamp_max);
634 /* Go back from YCoCg for final blend. */
635 history_radiance.rgb = colorspace::scene_linear_from_YCoCg(history_radiance.rgb);
636 /* Blend history with new radiance. */
637 float mix_fac = (history_radiance.w > 1e-3f) ? 0.97f : 0.0f;
638 /* Reduce blend factor to improve low roughness reflections. Use variance instead for speed. */
639 mix_fac *= mix(0.75f, 1.0f, saturate(in_variance * 20.0f));
640 float3 out_radiance = mix(
641 colorspace::safe_color(in_radiance), colorspace::safe_color(history_radiance.rgb), mix_fac);
642 /* This is feedback next frame as radiance_history_tx. */
643 imageStoreFast(srt.out_radiance_img, texel_fullres, float4(out_radiance, 0.0f));
644
645 /* Variance. */
646
647 /* Reflection reprojection. */
648 float2 history_variance = srt.variance_history_sample(uni, P_hit);
649 /* Blend history with new variance. */
650 float mix_variance_fac = (history_variance.y == 0.0f) ? 0.0f : 0.85f;
651 /* Avoid variance exploding. */
652 history_variance.x = clamp(history_variance.x, 0.0f, 100.0f);
653 /* Do the mix in squared space to make high variance dominate. */
654 float out_variance = sqrt(
655 mix(square(in_variance), square(history_variance.x), mix_variance_fac));
656 /* This is feedback next frame as variance_history_tx. */
657 imageStoreFast(srt.out_variance_img, texel_fullres, float4(out_variance));
658}
659
674
683[[compute, local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE)]]
685 [[resource_table]] const Uniform &uni,
686 [[resource_table]] const Sampling &sampling,
687 [[resource_table]] const draw::View &views,
688 [[resource_table]] const gbuffer::Reader &reader,
689 [[work_group_id]] const uint3 work_group,
690 [[local_invocation_id]] const uint3 local_id)
691{
692 [[resource_table]] TileBuffer &tiles = srt.tiles;
693 uint2 tile_coord = tiles.tile_coord(work_group.x);
694
695 const ViewMatrices view = views.get(0);
696
697 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
698 int2 texel_fullres = int2(local_id.xy + tile_coord * tile_size);
699 float2 center_uv = (float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
700
701 float center_depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
702 float3 center_P = view.point_screen_to_world(float3(center_uv, center_depth));
703
704 ClosureUndetermined center_closure = reader.read_bin(texel_fullres, srt.closure_index);
705
706 if (center_closure.type == CLOSURE_NONE_ID) {
707 /* Output nothing. This shouldn't even be loaded. */
708 return;
709 }
710
711 float roughness = closure_apparent_roughness_get(center_closure);
712 float variance = imageLoadFast(srt.in_variance_img, texel_fullres).r;
713 float3 in_radiance = imageLoadFast(srt.in_radiance_img, texel_fullres).rgb;
714
715 bool is_background = (center_depth == 0.0f);
716 bool is_smooth = (roughness < 0.05f);
717 bool is_low_variance = (variance < 0.1f);
718
719 /* Width of the box filter in pixels. */
720 float filter_size_factor = saturate(roughness * 8.0f) * saturate(variance * 2.0);
721 float filter_size = mix(0.0f, 9.0f, filter_size_factor);
722 uint sample_count = uint(mix(1.0f, 15.0f, saturate(variance * 2.0)));
723
724 if (is_smooth || is_background || is_low_variance) {
725 /* Early out cases. */
726 imageStoreFast(srt.out_radiance_img, texel_fullres, float4(in_radiance, 0.0f));
727 return;
728 }
729
731 float2(texel_fullres) + 0.5f, float2(3, 5), float2(0.0f));
732 noise += sampling.rng_2D_get(SAMPLING_RAYTRACE_W);
733
734 /* In order to remove more fireflies, "tone-map" the color samples during the accumulation. */
735 float3 accum_radiance = colorspace::log_from_scene_linear(in_radiance);
736 float accum_weight = 1.0f;
737 /* We want to resize the blur depending on the roughness and keep the amount of sample low.
738 * So we do a random sampling around the center point. */
739 for (uint i = 0u; i < sample_count; i++) {
740 /* Essentially a box radius overtime. */
741 float2 offset_f = (fract(random::hammersley_2d(i, sample_count) + noise) - 0.5f) * filter_size;
742 int2 offset = int2(floor(offset_f + 0.5f));
743
744 int2 sample_texel = texel_fullres + offset;
745 int3 sample_tile = int3(sample_texel / RAYTRACE_GROUP_SIZE, srt.closure_index);
746 /* Make sure the sample has been processed and do not contain garbage data. */
747 if (imageLoad(srt.tile_mask_img, sample_tile).r == 0u) {
748 continue;
749 }
750
751 float sample_depth = reverse_z::read(texelFetch(srt.depth_tx, sample_texel, 0).r);
752 float2 sample_uv = (float2(sample_texel) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
753 float3 sample_P = view.point_screen_to_world(float3(sample_uv, sample_depth));
754
755 /* Background case. */
756 if (sample_depth == 0.0f) {
757 continue;
758 }
759
760 float3 radiance = imageLoadFast(srt.in_radiance_img, sample_texel).rgb;
761
762 /* Do not gather unprocessed pixels. */
763 if (all(equal(radiance, FLT_11_11_10_MAX))) {
764 continue;
765 }
766
767 ClosureUndetermined sample_closure = reader.read_bin(sample_texel, srt.closure_index);
768
769 if (sample_closure.type == CLOSURE_NONE_ID) {
770 continue;
771 }
772
773 float gauss = filters::gaussian_factor(filter_size, 1.5f);
774
775 /* TODO(fclem): Scene parameter. 10000.0f is dependent on scene scale. */
776 float depth_weight = filters::planar_weight(center_closure.N, center_P, sample_P, 10000.0f);
777 float spatial_weight = filters::gaussian_weight(gauss, length_squared(float2(offset)));
778 float normal_weight = filters::angle_weight(center_closure.N, sample_closure.N);
779 float weight = depth_weight * spatial_weight * normal_weight;
780
781 accum_radiance += colorspace::log_from_scene_linear(radiance) * weight;
782 accum_weight += weight;
783 }
784
785 float3 out_radiance = accum_radiance * safe_rcp(accum_weight);
786 out_radiance = colorspace::scene_linear_from_log(out_radiance);
787
788 imageStoreFast(srt.out_radiance_img, texel_fullres, float4(out_radiance, 0.0f));
789}
790
794
795} // namespace eevee::raytracing::denoise
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
float3 line_plane_intersect(float3 line_origin, float3 line_direction, float3 plane_origin, float3 plane_normal)
void bxdf_ggx_context_amend_transmission(ClosureUndetermined &cl, float3 &V, Thickness thickness)
float closure_apparent_roughness_get(ClosureUndetermined cl)
float closure_evaluate_pdf(ClosureUndetermined cl, float3 L, float3 V, Thickness thickness)
#define RAYTRACE_VARIANCE_FORMAT
#define RAYTRACE_TILEMASK_FORMAT
#define RAYTRACE_RAYTIME_FORMAT
#define RAYTRACE_RADIANCE_FORMAT
#define UTIL_BLUE_NOISE_LAYER
float2 sample_disk(float2 rand)
@ SFLOAT_16_16_16_16
#define metal_max_total_threads_per_threadgroup(count)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define resource_table
#define sampler(slot)
#define specialization_constant(default_value)
#define local_invocation_id
#define compute
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
#define exp2
T fract(T) RET
bool any(VecOp< bool, D >) RET
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
bool all(VecOp< bool, D >) RET
#define sqrt
MatBase< R, C > transpose(MatBase< C, R >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< uint, 2, true > uimage2DArray
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageLoadFast
#define imageStoreFast
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
MatBase< 2, 3 > float2x3
MatBase< 4, 3 > float4x3
SamplerBase< uint, 2, false, true > usampler2DArray
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureGather(T, FltCoord, int=0) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float noise(float p)
float square(float v)
float2 project_point(float3x3 mat, float2 point)
float safe_rcp(float a)
float length_squared(VecT a)
VecT orthogonal(VecT v)
float reduce_max(float2 a)
VecT safe_normalize(VecT vector)
uint2 unpackUvec2x16(uint a)
bool flag_test(uint flag, uint val)
#define in_image_range(texel, tex)
#define FLT_11_11_10_MAX
#define in_texture_range(texel, tex)
#define in_range_exclusive(val, min_v, max_v)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_NONE_ID
@ CLOSURE_BSDF_THIN_GLASS_TRANSMISSION_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
MatBase< float, 2, 2 > float2x2
MatBase< float, 3, 4 > float3x4
float3 scene_linear_from_log(float3 color)
float3 log_from_scene_linear(float3 color)
float4 safe_color(float4 c)
float3 scene_linear_from_YCoCg(float3 ycocg_color)
float3 YCoCg_from_scene_linear(float3 rgb_color)
float4 bilinear_weights_from_subpixel_coord(float2 co)
void transmission_thickness_amend_closure(ClosureUndetermined &cl, float3 &V, Thickness thickness)
PipelineCompute bilateral(bilateral_main)
PipelineCompute temporal(temporal_main)
PipelineCompute spatial(spatial_main)
void bilateral_main(DenoiseBilateral &srt, const Uniform &uni, const Sampling &sampling, const draw::View &views, const gbuffer::Reader &reader, const uint3 work_group, const uint3 local_id)
void temporal_main(DenoiseTemporal &srt, const draw::View &views, const Uniform &uni, const uint3 work_group, const uint3 local_id)
void spatial_main(DenoiseSpatial &srt, const gbuffer::Reader &reader, const Uniform &uni, const draw::View &views, const Sampling &sampling, const UtilityTexture &util_tx, const uint3 work_group, const uint3 local_id)
float gaussian_factor(float linear_distance, float standard_deviation)
float angle_weight(float3 center_N, float3 sample_N)
float gaussian_weight(float factor, float square_distance)
float planar_weight(float3 plane_N, float3 plane_P, float3 P, float scale)
float2 interleaved_gradient_2d(float2 pixel, float2 seed, float2 offset)
float2 hammersley_2d(T i, T sample_count)
T read(T depth_buffer_value)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
float3 point_screen_to_world(float3 ssP) const
float3 view_incident_vector(float3 vP) const
float depth_view_to_screen(float v_depth) const
float3 point_view_to_world(float3 vP) const
float3 world_incident_vector(float3 P) const
float3 point_screen_to_view(float3 ssP) const
float sample_weight_get(const gbuffer::Reader &reader, const Uniform &uni, ViewMatrices view, float3 center_N, float3 center_P, int2 sample_texel) const
float4 history_validate(int2 texel, float bilinear_weight, float3 history_radiance)
LocalStatistics local_statistics_get(int2 texel, float3 center_radiance)
float4 radiance_history_sample(const Uniform &uni, float3 P, LocalStatistics local)
float2 variance_history_sample(const Uniform &uni, float3 P)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V