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]) *
53 float2 quad_center =
float2(global_id.xy * 2 + 1) * fullres_texel_size;
57 for (
int i = 0; i < 4; i++) {
65 cocs =
clamp(cocs, -srt.dof_buf.coc_abs_max, srt.dof_buf.coc_abs_max);
74 imageStore(srt.out_color_img, out_texel, out_color);
76 float out_coc =
dot(cocs, weights);
112 constexpr float exposure_scale = 1.0f;
113 return 1.0f / (4.0f + luma * exposure_scale);
122 return saturate(1.0f - (sample_coc - reference_coc) /
max(1.0f,
abs(reference_coc)));
148 [[
shared]]
float coc_cache[DOF_STABILIZE_GROUP_SIZE + 2][DOF_STABILIZE_GROUP_SIZE + 2];
172 constexpr uint cache_size = uint(DOF_STABILIZE_GROUP_SIZE + 2);
173 constexpr uint cache_depth_size = uint(DOF_STABILIZE_GROUP_SIZE + 4);
176 for (
int y = 0;
y < 2;
y++) {
177 for (
int x = 0;
x < 2;
x++) {
181 int2 cache_texel =
int2(local_id.xy) + offset;
191 int2 cache_texel =
int2(local_id.xy) + offset;
206 int2 coord = offset + 1 +
int2(local_id.xy);
212 int2 coord = offset + 2 +
int2(local_id.xy);
224 float accum_weight = 0.0f;
225 for (
int i = 0; i < 4; i++) {
231 accum.
coc += samp.
coc * weight;
232 accum_weight += weight;
237 accum.
coc += center.
coc * weight;
238 accum_weight += weight;
240 float rcp_weight = 1.0f / accum_weight;
241 accum.
color *= rcp_weight;
242 accum.
coc *= rcp_weight;
257 for (
int i = 0; i < 4; i++) {
269 for (
int i = 0; i < 4; i++) {
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;
281 return {min_c, max_c};
294 for (
int i = 0; i < 4; i++) {
296 if (min_depth > depth) {
298 nearest_texel = corners[i];
302 int2 velocity_texel = (texel_sample + nearest_texel) * 2;
319 float2 inter_texel =
modf(input_texel, center_texel);
321 film_get_catmull_rom_weights(inter_texel, weights);
329 center_texel += 0.5f;
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;
338 float4 weight_cross = weight_12.xyyx *
float4(weights[0].yx, weights[3].xy);
339 float weight_center = weight_12.
x * weight_12.
y;
347 color /= (weight_center +
reduce_add(weight_cross));
350 return {color.xyzz, color.
w};
358 float luma_history = dst.
color.
x;
370 float2(luma_history - luma_min, luma_max - luma_history));
372 distance_to_luma_clip *= 2.0f *
safe_rcp(luma_max - luma_min);
374 blend *=
saturate(distance_to_luma_clip * 4.0f + 0.1f);
379 blend =
mix(blend, 1.0f, coc_diff_ratio);
401 float3 clip_dir = src.color.rgb - history.
color.rgb;
421 srt.cache_init(global_id, local_id);
423 int2 src_texel =
int2(global_id.xy);
430 DofSample src = srt.spatial_filtering(local_id);
433 float2 motion = srt.pixel_history_motion_vector(src_texel, local_id);
438 DofSample dst = srt.sample_history(history_texel);
443 float blend = srt.history_blend_factor(
velocity, history_texel, bbox, src, dst);
455 result.color /= weight_src + weight_dst;
458 imageStore(srt.out_history_img, src_texel, result.color);
461 result.coc = result.color.a;
467 imageStore(srt.out_color_img, src_texel, result.color);
492 float2 quad_center =
float2(global_id.xy * 2 + 1) * halfres_texel_size;
496 for (
int i = 0; i < 4; i++) {
498 colors[i] =
textureLod(srt.color_tx, sample_uv, 0.0f);
499 cocs[i] =
textureLod(srt.coc_tx, sample_uv, 0.0f).r;
528 return (2.0f * color.g) + color.r + color.b;
590 float validity = 0.0f;
594 float2 uv = ((
float2(texel) + 0.5f) * 0.5f) * texel_size;
596 for (
int i = 0; i < 4; i++) {
603 constexpr float rejection_threshold = 0.7f;
605 validity =
max(validity,
diff);
617 float2 screen_pos = uv * screen_size;
618 float min_screen_border_distance =
reduce_min(
min(screen_pos, screen_size - screen_pos));
622 constexpr float rejection_hardness = 1.0f / 10.0f;
623 return saturate((min_screen_border_distance -
abs(coc)) * rejection_hardness + 1.0f);
628 constexpr float rejection_hardness = 1.0f;
634 constexpr float rejection_hardness = 0.3f;
646 uint2 thread_co = local_id.xy;
649 float local_coc =
imageLoad(srt.in_coc_lod0_img, texel).r;
652 float do_scatter = srt.scatter_luminosity_rejection(local_color.rgb);
654 do_scatter *= srt.scatter_coc_radius_rejection(local_coc);
656 do_scatter *= srt.scatter_screen_border_rejection(local_coc, texel);
658 do_scatter *= srt.scatter_neighborhood_rejection(global_id.xy, local_color.rgb);
665 srt.store_coc_cache(thread_co, do_scatter);
669 float4 do_scatter4 = srt.load4_coc_cache(thread_co & ~1u);
673 srt.store_color_cache(thread_co, local_color);
674 srt.store_coc_cache(thread_co, local_coc);
681 do_scatter4 *=
reduce_max(srt.dof_buf.bokeh_anisotropic_scale_inv);
683 float4 coc4 = srt.load4_coc_cache(thread_co);
699 srt.scatter_fg_indirect_buf.vertex_len = 4u;
701 uint rect_id =
atomicAdd(srt.scatter_fg_indirect_buf.instance_len, 1u);
702 if (rect_id < srt.dof_buf.scatter_max_rect) {
710 rect_fg.offset = offset;
712 rect_fg.half_extent = -half_extent;
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];
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];
727 srt.scatter_fg_list_buf[rect_id] = rect_fg;
732 srt.scatter_bg_indirect_buf.vertex_len = 4u;
734 uint rect_id =
atomicAdd(srt.scatter_bg_indirect_buf.instance_len, 1u);
735 if (rect_id < srt.dof_buf.scatter_max_rect) {
742 rect_bg.offset = offset;
743 rect_bg.half_extent = half_extent;
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];
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];
756 srt.scatter_bg_list_buf[rect_id] = rect_bg;
762 float4 color_lod0 = srt.load_color_cache(thread_co) * (1.0f - do_scatter);
763 srt.store_color_cache(thread_co, color_lod0);
765 imageStore(srt.inout_color_lod0_img, texel, color_lod0);
768 for (uint i = 1u; i < DOF_MIP_COUNT; i++) {
770 uint
mask = ~(~0u << i);
772 uint ofs = 1u << (i - 1u);
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));
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));
791 float coc_lod =
dot(coc4, weights);
793 srt.store_color_cache(thread_co, color_lod);
794 srt.store_coc_cache(thread_co, coc_lod);
796 int2 texel =
int2(global_id.xy >> i);
799 imageStore(srt.out_color_lod1_img, texel, color_lod);
803 imageStore(srt.out_color_lod2_img, texel, color_lod);
807 imageStore(srt.out_color_lod3_img, texel, color_lod);
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)
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)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define global_invocation_id
#define local_invocation_id
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
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
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 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)
float fast_luma(float3 color)
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)
float luma_weight(float luma)
float4 weighted_sum_array(float4 val[4], float4 weights)
T read(T depth_buffer_value)
float scatter_coc_radius_rejection(float coc) const
float4 load_color_cache(uint2 coord) const
DrawCommandArray & scatter_bg_indirect_buf
ScatterRect(& scatter_bg_list_buf)[]
image2D out_color_lod1_img
void store_color_cache(uint2 coord, float4 value)
const DepthOfFieldData & dof_buf
image2D out_color_lod2_img
float load_coc_cache(uint2 coord) const
DrawCommandArray & scatter_fg_indirect_buf
float scatter_screen_border_rejection(float coc, int2 texel) const
float4 load4_coc_cache(uint2 coord) const
void store_coc_cache(uint2 coord, float value)
float scatter_neighborhood_rejection(uint2 texel, float3 color)
image2D out_color_lod3_img
image2D inout_color_lod0_img
float coc_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE]
const image2D in_coc_lod0_img
float scatter_luminosity_rejection(float3 color) const
ScatterRect(& scatter_fg_list_buf)[]
float4 color_cache[DOF_REDUCE_GROUP_SIZE][DOF_REDUCE_GROUP_SIZE]
const DepthOfFieldData & dof_buf
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
DofSample sample_history(float2 input_texel) const
DofSample spatial_filtering(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]
const DepthOfFieldData & dof_buf