Blender V5.3
eevee_depth_of_field_setup.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11#include "eevee_velocity.bsl.hh"
13
15
17{
18 auto weight_sum = weights[0] + weights[1] + weights[2] + weights[3];
19 return (val[0] * weights[0] + val[1] * weights[1] + val[2] * weights[2] + val[3] * weights[3]) *
20 safe_rcp(weight_sum);
21}
22
34namespace setup {
35
36struct Resources {
37 [[uniform(0)]] const DepthOfFieldData &dof_buf;
38
41
44};
45
46[[compute, local_size(DOF_DEFAULT_GROUP_SIZE, DOF_DEFAULT_GROUP_SIZE)]]
48 [[resource_table]] const draw::View &views,
49 [[global_invocation_id]] const uint3 global_id)
50{
51 float2 fullres_texel_size = 1.0f / float2(textureSize(srt.color_tx, 0).xy);
52 /* Center uv around the 4 full-resolution pixels. */
53 float2 quad_center = float2(global_id.xy * 2 + 1) * fullres_texel_size;
54
55 float4 colors[4];
56 float4 cocs;
57 for (int i = 0; i < 4; i++) {
58 float2 sample_uv = quad_center + quad_offsets[i] * fullres_texel_size;
59 float depth = reverse_z::read(textureLod(srt.depth_tx, sample_uv, 0.0f).r);
60 /* NOTE: We use samplers without filtering. */
61 colors[i] = colorspace::safe_color(textureLod(srt.color_tx, sample_uv, 0.0f));
62 cocs[i] = dof_coc_from_depth(views, srt.dof_buf, sample_uv, depth);
63 }
64
65 cocs = clamp(cocs, -srt.dof_buf.coc_abs_max, srt.dof_buf.coc_abs_max);
66
67 float4 weights = dof_bilateral_coc_weights(cocs);
68 weights *= dof_bilateral_color_weights(colors);
69 /* Normalize so that the sum is 1. */
70 weights *= safe_rcp(reduce_add(weights));
71
72 int2 out_texel = int2(global_id.xy);
73 float4 out_color = weighted_sum_array(colors, weights);
74 imageStore(srt.out_color_img, out_texel, out_color);
75
76 float out_coc = dot(cocs, weights);
77 imageStore(srt.out_coc_img, out_texel, float4(out_coc));
78}
79
80} // namespace setup
81
96namespace stabilize {
97
98struct DofSample {
100 float coc;
101};
102
107
108float luma_weight(float luma)
109{
110 /* Slide 20 of "High Quality Temporal Supersampling" by Brian Karis at SIGGRAPH 2014. */
111 /* To preserve more details in dark areas, we use a bigger bias. */
112 constexpr float exposure_scale = 1.0f; /* TODO. */
113 return 1.0f / (4.0f + luma * exposure_scale);
114}
115
116float bilateral_weight(float reference_coc, float sample_coc)
117{
118 /* NOTE: The difference between the COCS should be inside a abs() function,
119 * but we follow UE4 implementation to improve how dithered transparency looks (see slide 19).
120 * Effectively bleed background into foreground.
121 * Compared to dof_bilateral_coc_weights() this saturates as 2x the reference CoC. */
122 return saturate(1.0f - (sample_coc - reference_coc) / max(1.0f, abs(reference_coc)));
123}
124
125struct Resources {
128
130
131 [[uniform(0)]] const DepthOfFieldData &dof_buf;
132
138
142
143 /* -------------------------------------------------------------------- */
146
147 [[shared]] float4 color_cache[DOF_STABILIZE_GROUP_SIZE + 2][DOF_STABILIZE_GROUP_SIZE + 2];
148 [[shared]] float coc_cache[DOF_STABILIZE_GROUP_SIZE + 2][DOF_STABILIZE_GROUP_SIZE + 2];
149 /* Need 2 pixel border for depth. */
150 [[shared]] float depth_cache[DOF_STABILIZE_GROUP_SIZE + 4][DOF_STABILIZE_GROUP_SIZE + 4];
151
152 void cache_init(uint3 global_id, uint3 local_id)
153 {
172 constexpr uint cache_size = uint(DOF_STABILIZE_GROUP_SIZE + 2);
173 constexpr uint cache_depth_size = uint(DOF_STABILIZE_GROUP_SIZE + 4);
174
175 int2 texel = int2(global_id.xy);
176 for (int y = 0; y < 2; y++) {
177 for (int x = 0; x < 2; x++) {
178 /* 1 Pixel border. */
179 if (all(lessThan(local_id.xy, uint2(cache_size / 2u)))) {
180 int2 offset = int2(x, y) * int2(cache_size / 2u);
181 int2 cache_texel = int2(local_id.xy) + offset;
182 int2 load_texel = clamp(texel + offset - 1, int2(0), textureSize(color_tx, 0) - 1);
183
184 float4 color = texelFetch(color_tx, load_texel, 0);
185 color_cache[cache_texel.y][cache_texel.x] = colorspace::YCoCg_from_scene_linear(color);
186 coc_cache[cache_texel.y][cache_texel.x] = texelFetch(coc_tx, load_texel, 0).x;
187 }
188 /* 2 Pixels border. */
189 if (all(lessThan(local_id.xy, uint2(cache_depth_size / 2u)))) {
190 int2 offset = int2(x, y) * int2(cache_depth_size / 2u);
191 int2 cache_texel = int2(local_id.xy) + offset;
192 /* Depth is full-resolution. Load every 2 pixels. */
193 int2 load_texel = clamp((texel + offset - 2) * 2, int2(0), textureSize(depth_tx, 0) - 1);
194
195 float depth = reverse_z::read(texelFetch(depth_tx, load_texel, 0).x);
196 depth_cache[cache_texel.y][cache_texel.x] = depth;
197 }
198 }
199 }
200 barrier();
201 }
202
203 /* NOTE: Sample color space is already in YCoCg space. */
204 DofSample fetch_input_sample(int2 offset, uint3 local_id) const
205 {
206 int2 coord = offset + 1 + int2(local_id.xy);
207 return {color_cache[coord.y][coord.x], coc_cache[coord.y][coord.x]};
208 }
209
210 float fetch_half_depth(int2 offset, uint3 local_id) const
211 {
212 int2 coord = offset + 2 + int2(local_id.xy);
213 return depth_cache[coord.y][coord.x];
214 }
215
217
219 {
220 /* Plus (+) shape offsets. */
221 const int2 plus_offsets[4] = {int2(-1, 0), int2(0, -1), int2(1, 0), int2(0, 1)};
222 DofSample center = fetch_input_sample(int2(0), local_id);
223 DofSample accum{float4(0.0f), 0.0f};
224 float accum_weight = 0.0f;
225 for (int i = 0; i < 4; i++) {
226 DofSample samp = fetch_input_sample(plus_offsets[i], local_id);
227 float weight = dof_buf.filter_samples_weight[i] * luma_weight(samp.color.x) *
228 bilateral_weight(center.coc, samp.coc);
229
230 accum.color += samp.color * weight;
231 accum.coc += samp.coc * weight;
232 accum_weight += weight;
233 }
234 /* Accumulate center sample last as it does not need bilateral_weights. */
235 float weight = dof_buf.filter_center_weight * luma_weight(center.color.x);
236 accum.color += center.color * weight;
237 accum.coc += center.coc * weight;
238 accum_weight += weight;
239
240 float rcp_weight = 1.0f / accum_weight;
241 accum.color *= rcp_weight;
242 accum.coc *= rcp_weight;
243 return accum;
244 }
245
246 /* Return history clipping bounding box in YCoCg color space. */
248 {
249 /* Plus (+) shape offsets. */
250 const int2 plus_offsets[4] = {int2(-1, 0), int2(0, -1), int2(1, 0), int2(0, 1)};
255 DofSample min_c = fetch_input_sample(int2(0), local_id);
256 DofSample max_c = min_c;
257 for (int i = 0; i < 4; i++) {
258 DofSample samp = fetch_input_sample(plus_offsets[i], local_id);
259 min_c.color = min(min_c.color, samp.color);
260 max_c.color = max(max_c.color, samp.color);
261 min_c.coc = min(min_c.coc, samp.coc);
262 max_c.coc = max(max_c.coc, samp.coc);
263 }
264 /* (Slide 32) Simple clamp to min/max of 8 neighbors results in 3x3 box artifacts.
265 * Round bbox shape by averaging 2 different min/max from 2 different neighborhood. */
266 DofSample min_c_3x3 = min_c;
267 DofSample max_c_3x3 = max_c;
268 const int2 corners[4] = {int2(-1, -1), int2(1, -1), int2(-1, 1), int2(1, 1)};
269 for (int i = 0; i < 4; i++) {
270 DofSample samp = fetch_input_sample(corners[i], local_id);
271 min_c_3x3.color = min(min_c_3x3.color, samp.color);
272 max_c_3x3.color = max(max_c_3x3.color, samp.color);
273 min_c_3x3.coc = min(min_c_3x3.coc, samp.coc);
274 max_c_3x3.coc = max(max_c_3x3.coc, samp.coc);
275 }
276 min_c.color = (min_c.color + min_c_3x3.color) * 0.5f;
277 max_c.color = (max_c.color + max_c_3x3.color) * 0.5f;
278 min_c.coc = (min_c.coc + min_c_3x3.coc) * 0.5f;
279 max_c.coc = (max_c.coc + max_c_3x3.coc) * 0.5f;
280
281 return {min_c, max_c};
282 }
283
284 /* Returns motion in pixel space to retrieve the pixel history. */
285 float2 pixel_history_motion_vector(int2 texel_sample, uint3 local_id) const
286 {
291 const int2 corners[4] = {int2(-2, -2), int2(2, -2), int2(-2, 2), int2(2, 2)};
292 float min_depth = fetch_half_depth(int2(0), local_id);
293 int2 nearest_texel = int2(0);
294 for (int i = 0; i < 4; i++) {
295 float depth = fetch_half_depth(corners[i], local_id);
296 if (min_depth > depth) {
297 min_depth = depth;
298 nearest_texel = corners[i];
299 }
300 }
301 /* Convert to full resolution buffer pixel. */
302 int2 velocity_texel = (texel_sample + nearest_texel) * 2;
303 velocity_texel = clamp(velocity_texel, int2(0), textureSize(velocity_tx, 0).xy - 1);
304 float4 vector = camera.resolve_local(views, velocity_tx, velocity_texel, min_depth);
305 /* Transform to **half** pixel space. */
306 return vector.xy * float2(textureSize(color_tx, 0));
307 }
308
309 /* Load color using a special filter to avoid losing detail.
310 * \a texel is sample position with sub-pixel accuracy. */
311 DofSample sample_history(float2 input_texel) const
312 {
313#if 1 /* Bilinear. */
314 float2 uv = float2(input_texel + 0.5f) / float2(textureSize(in_history_tx, 0));
315 float4 color = textureLod(in_history_tx, uv, 0.0f);
316
317#else /* Catmull Rom interpolation. 5 Bilinear Taps. */
318 float2 center_texel;
319 float2 inter_texel = modf(input_texel, center_texel);
320 float2 weights[4];
321 film_get_catmull_rom_weights(inter_texel, weights);
322
329 center_texel += 0.5f;
330
331 /* Slide 92. */
332 float2 weight_12 = weights[1] + weights[2];
333 float2 uv_12 = (center_texel + weights[2] / weight_12) * uniform_buf.film.extent_inv;
334 float2 uv_0 = (center_texel - 1.0f) * uniform_buf.film.extent_inv;
335 float2 uv_3 = (center_texel + 2.0f) * uniform_buf.film.extent_inv;
336
337 float4 color;
338 float4 weight_cross = weight_12.xyyx * float4(weights[0].yx, weights[3].xy);
339 float weight_center = weight_12.x * weight_12.y;
340
341 color = textureLod(in_history_tx, uv_12, 0.0f) * weight_center;
342 color += textureLod(in_history_tx, float2(uv_12.x, uv_0.y), 0.0f) * weight_cross.x;
343 color += textureLod(in_history_tx, float2(uv_0.x, uv_12.y), 0.0f) * weight_cross.y;
344 color += textureLod(in_history_tx, float2(uv_3.x, uv_12.y), 0.0f) * weight_cross.z;
345 color += textureLod(in_history_tx, float2(uv_12.x, uv_3.y), 0.0f) * weight_cross.w;
346 /* Re-normalize for the removed corners. */
347 color /= (weight_center + reduce_add(weight_cross));
348#endif
349 /* NOTE(fclem): Opacity is wrong on purpose. Final Opacity does not rely on history. */
350 return {color.xyzz, color.w};
351 }
352
354 float velocity, float2 texel, DofNeighborhoodMinMax bbox, DofSample src, DofSample dst) const
355 {
356 float luma_min = bbox.min.color.x;
357 float luma_max = bbox.max.color.x;
358 float luma_history = dst.color.x;
359
360 /* 5% of incoming color by default. */
361 float blend = 0.05f;
362 /* Blend less history if the pixel has substantial velocity. */
363 /* NOTE(fclem): velocity threshold multiplied by 2 because of half resolution. */
364 blend = mix(blend, 0.20f, saturate(velocity * 0.02f * 2.0f));
369 float distance_to_luma_clip = reduce_min(
370 float2(luma_history - luma_min, luma_max - luma_history));
371 /* Divide by bbox size to get a factor. 2 factor to compensate the line above. */
372 distance_to_luma_clip *= 2.0f * safe_rcp(luma_max - luma_min);
373 /* Linearly blend when history gets below to 25% of the bbox size. */
374 blend *= saturate(distance_to_luma_clip * 4.0f + 0.1f);
375 /* Progressively discard history until history CoC is twice as big as the filtered CoC.
376 * Note we use absolute diff here because we are not comparing neighbors and thus do not risk
377 * to dilate thin features like hair (slide 19). */
378 float coc_diff_ratio = saturate(abs(src.coc - dst.coc) / max(1.0f, abs(src.coc)));
379 blend = mix(blend, 1.0f, coc_diff_ratio);
380 /* Discard out of view history. */
381 if (any(lessThan(texel, float2(0))) ||
383 {
384 blend = 1.0f;
385 }
386 /* Discard history if invalid. */
387 if (u_use_history == false) {
388 blend = 1.0f;
389 }
390 return blend;
391 }
392};
393
394/* Modulate the history color to avoid ghosting artifact. */
396 DofSample history,
397 [[maybe_unused]] DofSample src)
398{
399#if 0
400 /* Clip instead of clamping to avoid color accumulating in the AABB corners. */
401 float3 clip_dir = src.color.rgb - history.color.rgb;
402
403 float t = line_aabb_clipping_dist(
404 history.color.rgb, clip_dir, bbox.min.color.rgb, bbox.max.color.rgb);
405 history.color.rgb += clip_dir * saturate(t);
406#else
407 /* More responsive. */
408 history.color = clamp(history.color, bbox.min.color, bbox.max.color);
409#endif
410 /* Clamp CoC to reduce convergence time. Otherwise the result lags. */
411 history.coc = clamp(history.coc, bbox.min.coc, bbox.max.coc);
412
413 return history;
414}
415
416[[compute, local_size(DOF_STABILIZE_GROUP_SIZE, DOF_STABILIZE_GROUP_SIZE)]]
418 [[global_invocation_id]] const uint3 global_id,
419 [[local_invocation_id]] const uint3 local_id)
420{
421 srt.cache_init(global_id, local_id);
422
423 int2 src_texel = int2(global_id.xy);
424
430 DofSample src = srt.spatial_filtering(local_id);
431
432 /* Reproject by finding where this pixel was in the previous frame. */
433 float2 motion = srt.pixel_history_motion_vector(src_texel, local_id);
434 float2 history_texel = float2(src_texel) + motion;
435
436 float velocity = length(motion);
437
438 DofSample dst = srt.sample_history(history_texel);
439
440 /* Get local color bounding box of source neighborhood. */
441 DofNeighborhoodMinMax bbox = srt.neighbor_boundbox(local_id);
442
443 float blend = srt.history_blend_factor(velocity, history_texel, bbox, src, dst);
444
445 dst = amend_history(bbox, dst, src);
446
447 /* Luma weighted blend to reduce flickering. */
448 float weight_dst = luma_weight(dst.color.x) * (1.0f - blend);
449 float weight_src = luma_weight(src.color.x) * (blend);
450
451 DofSample result;
452 /* Weighted blend. */
453 result.color = float4(dst.color.rgb, dst.coc) * weight_dst +
454 float4(src.color.rgb, src.coc) * weight_src;
455 result.color /= weight_src + weight_dst;
456
457 /* Save history for next iteration. Still in YCoCg space with CoC in alpha. */
458 imageStore(srt.out_history_img, src_texel, result.color);
459
460 /* Un-swizzle. */
461 result.coc = result.color.a;
462 /* Clamp opacity since we don't store it in history. */
463 result.color.a = clamp(src.color.a, bbox.min.color.a, bbox.max.color.a);
464
465 result.color = colorspace::scene_linear_from_YCoCg(result.color);
466
467 imageStore(srt.out_color_img, src_texel, result.color);
468 imageStore(srt.out_coc_img, src_texel, float4(result.coc));
469}
470
471} // namespace stabilize
472
479namespace downsample {
480
486
487[[compute, local_size(DOF_DEFAULT_GROUP_SIZE, DOF_DEFAULT_GROUP_SIZE)]]
489{
490 float2 halfres_texel_size = 1.0f / float2(textureSize(srt.color_tx, 0).xy);
491 /* Center uv around the 4 half-resolution pixels. */
492 float2 quad_center = float2(global_id.xy * 2 + 1) * halfres_texel_size;
493
494 float4 colors[4];
495 float4 cocs;
496 for (int i = 0; i < 4; i++) {
497 float2 sample_uv = quad_center + quad_offsets[i] * halfres_texel_size;
498 colors[i] = textureLod(srt.color_tx, sample_uv, 0.0f);
499 cocs[i] = textureLod(srt.coc_tx, sample_uv, 0.0f).r;
500 }
501
502 float4 weights = dof_bilateral_coc_weights(cocs);
503 /* Normalize so that the sum is 1. */
504 weights *= safe_rcp(reduce_add(weights));
505
506 float4 out_color = weighted_sum_array(colors, weights);
507
508 imageStore(srt.out_color_img, int2(global_id.xy), out_color);
509}
510} // namespace downsample
511
524namespace reduce {
525
526float fast_luma(float3 color)
527{
528 return (2.0f * color.g) + color.r + color.b;
529}
530
531struct Resources {
532 [[storage(0, write)]] ScatterRect (&scatter_fg_list_buf)[];
533 [[storage(1, write)]] ScatterRect (&scatter_bg_list_buf)[];
534
535 [[storage(2, read_write)]] DrawCommandArray &scatter_fg_indirect_buf;
536 [[storage(3, read_write)]] DrawCommandArray &scatter_bg_indirect_buf;
537
538 [[uniform(0)]] const DepthOfFieldData &dof_buf;
539
541
550
551 /* -------------------------------------------------------------------- */
554
555 [[shared]] float4 color_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE];
556 [[shared]] float coc_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE];
557
558 void store_color_cache(uint2 coord, float4 value)
559 {
560 color_cache[coord.y][coord.x] = value;
561 }
563 {
564 return color_cache[coord.y][coord.x];
565 }
566
567 void store_coc_cache(uint2 coord, float value)
568 {
569 coc_cache[coord.y][coord.x] = value;
570 }
571 float load_coc_cache(uint2 coord) const
572 {
573 return coc_cache[coord.y][coord.x];
574 }
576 {
577 return float4(load_coc_cache(coord + quad_offsets_u[0]),
578 load_coc_cache(coord + quad_offsets_u[1]),
579 load_coc_cache(coord + quad_offsets_u[2]),
580 load_coc_cache(coord + quad_offsets_u[3]));
581 }
582
584
585 /* NOTE: Do not compare alpha as it is not scattered by the scatter pass. */
587 {
588 color = min(float3(dof_buf.scatter_neighbor_max_color), color);
589
590 float validity = 0.0f;
591
592 /* Centered in the middle of 4 quarter res texel. */
593 float2 texel_size = 1.0f / float2(textureSize(downsample_tx, 0).xy);
594 float2 uv = ((float2(texel) + 0.5f) * 0.5f) * texel_size;
595
596 for (int i = 0; i < 4; i++) {
597 float2 sample_uv = uv + quad_offsets[i] * texel_size;
598 float3 ref = textureLod(downsample_tx, sample_uv, 0.0f).rgb;
599
600 ref = min(float3(dof_buf.scatter_neighbor_max_color), ref);
601 float diff = reduce_max(max(float3(0.0f), abs(ref - color)));
602
603 constexpr float rejection_threshold = 0.7f;
604 diff = saturate(diff / rejection_threshold - 1.0f);
605 validity = max(validity, diff);
606 }
607
608 return validity;
609 }
610
611 /* This avoids Bokeh sprite popping in and out at the screen border and
612 * drawing Bokeh sprites larger than the screen. */
613 float scatter_screen_border_rejection(float coc, int2 texel) const
614 {
616 float2 uv = (float2(texel) + 0.5f) / screen_size;
617 float2 screen_pos = uv * screen_size;
618 float min_screen_border_distance = reduce_min(min(screen_pos, screen_size - screen_pos));
619 /* Full-resolution to half-resolution CoC. */
620 coc *= 0.5f;
621 /* Allow 10px transition. */
622 constexpr float rejection_hardness = 1.0f / 10.0f;
623 return saturate((min_screen_border_distance - abs(coc)) * rejection_hardness + 1.0f);
624 }
625
627 {
628 constexpr float rejection_hardness = 1.0f;
629 return saturate(reduce_max(color - dof_buf.scatter_color_threshold) * rejection_hardness);
630 }
631
632 float scatter_coc_radius_rejection(float coc) const
633 {
634 constexpr float rejection_hardness = 0.3f;
635 return saturate((abs(coc) - dof_buf.scatter_coc_threshold) * rejection_hardness);
636 }
637};
638
639[[compute, local_size(DOF_REDUCE_GROUP_SIZE, DOF_REDUCE_GROUP_SIZE)]]
641 [[global_invocation_id]] const uint3 global_id,
642 [[local_invocation_id]] const uint3 local_id)
643{
644
645 int2 texel = min(int2(global_id.xy), imageSize(srt.inout_color_lod0_img) - 1);
646 uint2 thread_co = local_id.xy;
647
648 float4 local_color = imageLoad(srt.inout_color_lod0_img, texel);
649 float local_coc = imageLoad(srt.in_coc_lod0_img, texel).r;
650
651 /* Only scatter if luminous enough. */
652 float do_scatter = srt.scatter_luminosity_rejection(local_color.rgb);
653 /* Only scatter if CoC is big enough. */
654 do_scatter *= srt.scatter_coc_radius_rejection(local_coc);
655 /* Only scatter if CoC is not too big to avoid performance issues. */
656 do_scatter *= srt.scatter_screen_border_rejection(local_coc, texel);
657 /* Only scatter if neighborhood is different enough. */
658 do_scatter *= srt.scatter_neighborhood_rejection(global_id.xy, local_color.rgb);
659 /* For debugging. */
660 if (no_scatter_pass) {
661 do_scatter = 0.0f;
662 }
663
664 /* Use coc_cache for broadcasting the do_scatter result. */
665 srt.store_coc_cache(thread_co, do_scatter);
666 barrier();
667
668 /* Load the same value for each thread quad. */
669 float4 do_scatter4 = srt.load4_coc_cache(thread_co & ~1u);
670 barrier();
671
672 /* Load level 0 into cache. */
673 srt.store_color_cache(thread_co, local_color);
674 srt.store_coc_cache(thread_co, local_coc);
675 barrier();
676
677 /* Add a scatter sprite for each 2x2 pixel neighborhood passing the threshold. */
678 bool any_scatter = any(greaterThan(do_scatter4, float4(0.0f)));
679 if (all(equal(thread_co & 1u, uint2(0))) && any_scatter) {
680 /* Apply energy conservation to anamorphic scattered bokeh. */
681 do_scatter4 *= reduce_max(srt.dof_buf.bokeh_anisotropic_scale_inv);
682 /* Circle of Confusion. */
683 float4 coc4 = srt.load4_coc_cache(thread_co);
684 /* We are scattering at half resolution, so divide CoC by 2. */
685 coc4 *= 0.5f;
686 /* Sprite center position. Center sprite around the 4 texture taps. */
687 float2 offset = float2(global_id.xy) + 1;
688 /* Add 2.5 to max_coc because the max_coc may not be centered on the sprite origin
689 * and because we smooth the bokeh shape a bit in the pixel shader. */
690 float2 half_extent = reduce_max(abs(coc4)) * srt.dof_buf.bokeh_anisotropic_scale + 2.5f;
691 /* Follows quad_offsets order. */
692 float3 color4_0 = srt.load_color_cache(thread_co + quad_offsets_u[0]).rgb;
693 float3 color4_1 = srt.load_color_cache(thread_co + quad_offsets_u[1]).rgb;
694 float3 color4_2 = srt.load_color_cache(thread_co + quad_offsets_u[2]).rgb;
695 float3 color4_3 = srt.load_color_cache(thread_co + quad_offsets_u[3]).rgb;
696 /* Issue a sprite for each field if any CoC matches. */
697 if (any(lessThan(do_scatter4 * sign(coc4), float4(0.0f)))) {
698 /* Same value for all threads. Not an issue if we don't sync access to it. */
699 srt.scatter_fg_indirect_buf.vertex_len = 4u;
700 /* Issue 1 strip instance per sprite. */
701 uint rect_id = atomicAdd(srt.scatter_fg_indirect_buf.instance_len, 1u);
702 if (rect_id < srt.dof_buf.scatter_max_rect) {
703
704 float4 coc4_fg = max(float4(0.0f), -coc4);
705 float4 fg_weights = dof_layer_weight(coc4_fg) * dof_sample_weight(coc4_fg) * do_scatter4;
706 /* Filter NaNs. */
707 fg_weights = select(fg_weights, float4(0.0f), equal(coc4_fg, float4(0.0f)));
708
709 ScatterRect rect_fg;
710 rect_fg.offset = offset;
711 /* Negate extent to flip the sprite. Mimics optical phenomenon. */
712 rect_fg.half_extent = -half_extent;
713 /* NOTE: Since we flipped the quad along (1,-1) line, we need to also swap the (1,1) and
714 * (0,0) values so that quad_offsets is in the right order in the vertex shader. */
715
716 /* Circle of Confusion absolute radius in half-resolution pixels. */
717 rect_fg.color_and_coc[0].a = coc4_fg[0];
718 rect_fg.color_and_coc[1].a = coc4_fg[3];
719 rect_fg.color_and_coc[2].a = coc4_fg[2];
720 rect_fg.color_and_coc[3].a = coc4_fg[1];
721 /* Apply weights. */
722 rect_fg.color_and_coc[0].rgb = color4_0 * fg_weights[0];
723 rect_fg.color_and_coc[1].rgb = color4_3 * fg_weights[3];
724 rect_fg.color_and_coc[2].rgb = color4_2 * fg_weights[2];
725 rect_fg.color_and_coc[3].rgb = color4_1 * fg_weights[1];
726
727 srt.scatter_fg_list_buf[rect_id] = rect_fg;
728 }
729 }
730 if (any(greaterThan(do_scatter4 * sign(coc4), float4(0.0f)))) {
731 /* Same value for all threads. Not an issue if we don't sync access to it. */
732 srt.scatter_bg_indirect_buf.vertex_len = 4u;
733 /* Issue 1 strip instance per sprite. */
734 uint rect_id = atomicAdd(srt.scatter_bg_indirect_buf.instance_len, 1u);
735 if (rect_id < srt.dof_buf.scatter_max_rect) {
736 float4 coc4_bg = max(float4(0.0f), coc4);
737 float4 bg_weights = dof_layer_weight(coc4_bg) * dof_sample_weight(coc4_bg) * do_scatter4;
738 /* Filter NaNs. */
739 bg_weights = select(bg_weights, float4(0.0f), equal(coc4_bg, float4(0.0f)));
740
741 ScatterRect rect_bg;
742 rect_bg.offset = offset;
743 rect_bg.half_extent = half_extent;
744
745 /* Circle of Confusion absolute radius in half-resolution pixels. */
746 rect_bg.color_and_coc[0].a = coc4_bg[0];
747 rect_bg.color_and_coc[1].a = coc4_bg[1];
748 rect_bg.color_and_coc[2].a = coc4_bg[2];
749 rect_bg.color_and_coc[3].a = coc4_bg[3];
750 /* Apply weights. */
751 rect_bg.color_and_coc[0].rgb = color4_0 * bg_weights[0];
752 rect_bg.color_and_coc[1].rgb = color4_1 * bg_weights[1];
753 rect_bg.color_and_coc[2].rgb = color4_2 * bg_weights[2];
754 rect_bg.color_and_coc[3].rgb = color4_3 * bg_weights[3];
755
756 srt.scatter_bg_list_buf[rect_id] = rect_bg;
757 }
758 }
759 }
760
761 /* Remove scatter color from gather. */
762 float4 color_lod0 = srt.load_color_cache(thread_co) * (1.0f - do_scatter);
763 srt.store_color_cache(thread_co, color_lod0);
764
765 imageStore(srt.inout_color_lod0_img, texel, color_lod0);
766
767 /* Recursive downsample. */
768 for (uint i = 1u; i < DOF_MIP_COUNT; i++) {
769 barrier();
770 uint mask = ~(~0u << i);
771 if (all(equal(local_id.xy & mask, uint2(0)))) {
772 uint ofs = 1u << (i - 1u);
773
774 /* TODO(fclem): Could use wave shuffle intrinsics to avoid LDS as suggested by the paper. */
775 float4 coc4;
776 coc4[0] = srt.load_coc_cache(thread_co + uint2(ofs, 0));
777 coc4[1] = srt.load_coc_cache(thread_co + uint2(ofs, ofs));
778 coc4[2] = srt.load_coc_cache(thread_co + uint2(0, ofs));
779 coc4[3] = srt.load_coc_cache(thread_co + uint2(0, 0));
780 float4 colors[4];
781 colors[0] = srt.load_color_cache(thread_co + uint2(ofs, 0));
782 colors[1] = srt.load_color_cache(thread_co + uint2(ofs, ofs));
783 colors[2] = srt.load_color_cache(thread_co + uint2(0, ofs));
784 colors[3] = srt.load_color_cache(thread_co + uint2(0, 0));
785
787 /* Normalize so that the sum is 1. */
788 weights *= safe_rcp(reduce_add(weights));
789
790 float4 color_lod = weighted_sum_array(colors, weights);
791 float coc_lod = dot(coc4, weights);
792
793 srt.store_color_cache(thread_co, color_lod);
794 srt.store_coc_cache(thread_co, coc_lod);
795
796 int2 texel = int2(global_id.xy >> i);
797
798 if (i == 1) {
799 imageStore(srt.out_color_lod1_img, texel, color_lod);
800 imageStore(srt.out_coc_lod1_img, texel, float4(coc_lod));
801 }
802 else if (i == 2) {
803 imageStore(srt.out_color_lod2_img, texel, color_lod);
804 imageStore(srt.out_coc_lod2_img, texel, float4(coc_lod));
805 }
806 else /* if (i == 3) */ {
807 imageStore(srt.out_color_lod3_img, texel, color_lod);
808 imageStore(srt.out_coc_lod3_img, texel, float4(coc_lod));
809 }
810 }
811 }
812}
813
814} // namespace reduce
815
816} // namespace eevee::dof::setup
817
btScalar max
float line_aabb_clipping_dist(float3 line_origin, float3 line_direction, float3 aabb_min, float3 aabb_max)
float4 dof_bilateral_color_weights(float4 colors[4])
float dof_sample_weight(float coc)
#define no_scatter_pass
float4 dof_bilateral_coc_weights(float4 cocs)
const uint2 quad_offsets_u[4]
float dof_layer_weight(float coc, const bool is_foreground, const bool is_resolve)
const float2 quad_offsets[4]
float dof_coc_from_depth(const draw::View &views, DepthOfFieldData dof_data, float2 uv, float depth)
PipelineCompute eevee_depth_of_field_reduce(eevee::dof::setup::reduce::comp_main)
PipelineCompute eevee_depth_of_field_downsample(eevee::dof::setup::downsample::comp_main)
PipelineCompute eevee_depth_of_field_setup(eevee::dof::setup::setup::comp_main)
PipelineCompute eevee_depth_of_field_stabilize(eevee::dof::setup::stabilize::comp_main)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
@ SFLOAT_16_16_16_16
#define shared
#define uniform(slot)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define push_constant
#define resource_table
#define sampler(slot)
#define global_invocation_id
#define local_invocation_id
#define compute
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) 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
constexpr T sign(T) RET
#define modf
#define abs
#define select(A, B, C)
int atomicAdd(int &, int) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float safe_rcp(float a)
float reduce_add(float2 a)
float reduce_min(float2 a)
float reduce_max(float2 a)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
float4 safe_color(float4 c)
float3 scene_linear_from_YCoCg(float3 ycocg_color)
float3 YCoCg_from_scene_linear(float3 rgb_color)
void comp_main(Resources &srt, const uint3 global_id)
void comp_main(Resources &srt, const uint3 global_id, const uint3 local_id)
void comp_main(Resources &srt, const draw::View &views, const uint3 global_id)
void comp_main(Resources &srt, const uint3 global_id, const uint3 local_id)
DofSample amend_history(DofNeighborhoodMinMax bbox, DofSample history, DofSample src)
float bilateral_weight(float reference_coc, float sample_coc)
float4 weighted_sum_array(float4 val[4], float4 weights)
T read(T depth_buffer_value)
#define mix
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
float scatter_screen_border_rejection(float coc, int2 texel) const
float scatter_neighborhood_rejection(uint2 texel, float3 color)
float coc_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE]
float4 color_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE]
float history_blend_factor(float velocity, float2 texel, DofNeighborhoodMinMax bbox, DofSample src, DofSample dst) const
float fetch_half_depth(int2 offset, uint3 local_id) const
float depth_cache[DOF_STABILIZE_GROUP_SIZE+4][DOF_STABILIZE_GROUP_SIZE+4]
DofSample fetch_input_sample(int2 offset, uint3 local_id) const
float2 pixel_history_motion_vector(int2 texel_sample, uint3 local_id) const
void cache_init(uint3 global_id, uint3 local_id)
float coc_cache[DOF_STABILIZE_GROUP_SIZE+2][DOF_STABILIZE_GROUP_SIZE+2]
DofNeighborhoodMinMax neighbor_boundbox(uint3 local_id) const
float4 color_cache[DOF_STABILIZE_GROUP_SIZE+2][DOF_STABILIZE_GROUP_SIZE+2]
float x
float y
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
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13