25#ifndef SRT_CONSTANT_is_hole_fill
26# define SRT_CONSTANT_is_hole_fill 0
30#if SRT_CONSTANT_is_hole_fill
32# define gather_ring_count int(3)
33# define gather_ring_density int(3)
34# define gather_max_density_change int(0)
35# define gather_density_change_ring int(1)
37# define gather_ring_count int(DOF_GATHER_RING_COUNT)
38# define gather_ring_density int(3)
39# define gather_max_density_change int(50)
40# define gather_density_change_ring int(1)
64#define unit_ring_radius float(1.0f / float(gather_ring_count))
65#define unit_sample_radius float(1.0f / float(gather_ring_count + 0.5f))
66#define large_kernel_radius float(0.5f + float(gather_ring_count))
67#define smaller_kernel_radius float(0.5f + float(gather_ring_count - gather_density_change_ring))
69#define radius_downscale_factor float(smaller_kernel_radius / large_kernel_radius)
70#define change_density_at_ring int((gather_ring_count - gather_density_change_ring + 1))
71#define coc_radius_error float(2.0f)
98 float distance_from_center,
99 float intersection_multiplier)
102 return step(0.0f, (
abs(coc) - distance_from_center));
105 return saturate((
abs(coc) - distance_from_center) * intersection_multiplier + 0.5f);
116 return saturate(coc - bordering_radius);
121 sample_data.
color *= weight;
122 sample_data.
coc *= weight;
124 sample_data.
weight *= weight;
131 accum_data.
color += sample_data.
color * weight;
132 accum_data.
coc += sample_data.
coc * weight;
133 accum_data.
coc_sqr += sample_data.
coc * (sample_data.
coc * weight);
134 accum_data.
weight += weight;
138 float bordering_radius,
139 float intersection_multiplier,
141 const bool do_fast_gather,
142 const bool foreground,
146 if (do_fast_gather) {
147 for (
int i = 0; i < 2; i++) {
157 constexpr float mirroring_min_distance = 15.0f;
158 if (pair_data[0].coc < mirroring_threshold &&
159 (pair_data[1].coc - mirroring_min_distance) > pair_data[0].coc)
161 pair_data[1].
coc = pair_data[0].
coc;
163 else if (pair_data[1].coc < mirroring_threshold &&
164 (pair_data[0].coc - mirroring_min_distance) > pair_data[1].coc)
166 pair_data[0].
coc = pair_data[1].
coc;
170 for (
int i = 0; i < 2; i++) {
174 pair_data[i].coc, pair_data[i].dist, intersection_multiplier);
175 float weight = inter_weight * layer_weight * sample_weight;
188 ring_data.
transparency += 1.0f - inter_weight * layer_weight;
191 float coc = foreground ? -pair_data[i].
coc : pair_data[i].
coc;
200 const bool do_fast_gather,
202 const bool reversed_occlusion,
205 if (do_fast_gather) {
214 accum_data.
coc = ring_data.
coc;
222 if (ring_data.
weight == 0.0f) {
226 float ring_avg_coc = ring_data.
coc / ring_data.
weight;
227 float accum_avg_coc = accum_data.
coc / accum_data.
weight;
231 float ring_occlu =
saturate(accum_avg_coc - ring_avg_coc);
234 float accum_occlu =
saturate((ring_avg_coc - accum_avg_coc) * 0.1f - 1.0f);
237 ring_occlu = accum_occlu = 0.0f;
249 if (reversed_occlusion) {
251 float alpha = (accum_data.
weight == 0.0f) ? 0.0f : accum_opacity * accum_occlu;
252 float one_minus_alpha = 1.0f - alpha;
254 accum_data.
color += ring_data.
color * one_minus_alpha;
255 accum_data.
coc += ring_data.
coc * one_minus_alpha;
257 accum_data.
weight += ring_data.
weight * one_minus_alpha;
263 float alpha = (accum_data.
weight == 0.0f) ? 1.0f : (ring_opacity * ring_occlu);
264 float one_minus_alpha = 1.0f - alpha;
266 accum_data.
color = accum_data.
color * one_minus_alpha + ring_data.
color;
267 accum_data.
coc = accum_data.
coc * one_minus_alpha + ring_data.
coc;
278 return (ring_count * ring_count - ring_count) * ring_density + 1;
282 float bordering_radius,
284 const bool do_fast_gather,
285 const bool foreground,
291 float weight = layer_weight * sample_weight;
294 if (do_fast_gather) {
297 sample_weight = 1.0f;
306 if (!do_fast_gather) {
317 weight *= (1.0f - accum_weight);
318 center_data.
coc_sqr = center_data.
coc * (center_data.
coc * weight);
319 center_data.
color *= weight;
320 center_data.
coc *= weight;
321 center_data.
weight = weight;
325 float ring_area =
square(bordering_radius);
331 center_data, 1,
false, do_fast_gather, foreground, accum_data);
336 const int ring_density,
345 sample_count_per_density_change * density_change;
355 out_col = accum_data.
color * weight_inv;
361 else if (accum_data.
weight > 0.0f) {
368 if (out_weight > 0.99f) {
371 else if (out_weight < 0.01f) {
375 if (out_col.a > 0.993f) {
378 else if (out_col.a < 0.003f) {
385 float coc =
textureLod(gather_input_coc_tx, uv, lod).r;
400 min_intersectable_radius *= 0.1f;
402 bool need_new_density = (base_radius *
unit_ring_radius > min_intersectable_radius);
406 return need_new_density && larger_than_min_density;
414 float &intersection_multiplier)
419 jitter_ofs *=
dof_buf.bokeh_anisotropic_scale;
425 constexpr float lod_bias = -2.0f;
432 intersection_multiplier =
pow(0.5f, lod);
440 float min_intersectable_radius,
441 const bool do_fast_gather,
442 const bool do_density_change,
453 if (!do_fast_gather) {
465 float lod, isect_mul;
469 bool first_ring =
true;
473 int density_change = 0;
477 float step_rot =
M_PI /
float(sample_pair_count);
480 float angle_offset =
noise.y * step_rot;
486 float bordering_radius = ring_radius +
489 for (
int sample_pair = 0; sample_pair < sample_pair_count; sample_pair++) {
490 offset = step_rot_mat * offset;
493 for (
int i = 0; i < 2; i++) {
494 float2 offset_co = ((i == 0) ? offset : -offset);
499 dof_buf.bokeh_anisotropic_scale;
501 float2 sample_co = center_co + offset_co * ring_radius;
503 if (do_fast_gather) {
510 pair_data[i].
dist = ring_radius;
533 sample_pair_count * 2,
549 const float outer_rings_weight = 1.0f /
566 if (do_fast_gather) {
573 center_data.
dist = 0.0f;
590 total_sample_count, accum_data, out_color, out_weight, out_occlusion);
593 float fac =
saturate(
float(density_change) /
float(10.0f));
597 out_color.rgb =
average(out_color.rgb) *
float3(0.0f, 1.0f, 0.0f);
600 out_color.rgb =
average(out_color.rgb) *
float3(0.0f, 1.0f, 0.0f);
604 out_color *= out_weight;
622 float &out_center_coc)
635 constexpr float sample_count_max =
float(DOF_SLIGHT_FOCUS_SAMPLE_MAX);
639 bool first_ring =
true;
641 for (
float s = 0.0f; s < sample_count; s++) {
644 float ring_dist =
length(offset);
647 for (
int i = 0; i < 2; i++) {
648 float2 sample_offset = ((i == 0) ? offset : -offset);
654 pair_data[i].
dist = ring_dist;
663 float bordering_radius = ring_dist + 0.5f;
664 constexpr float isect_mul = 1.0f;
667 pair_data, bordering_radius, isect_mul, first_ring,
false,
false, bg_ring, bg_accum);
673 float tmp = pair_data[0].
dist;
674 pair_data[0].
dist = pair_data[1].
dist;
675 pair_data[1].
dist = tmp;
679 pair_data, bordering_radius, isect_mul, first_ring,
false,
true, fg_ring, fg_accum);
693 center_data.
dist = 0.0f;
695 out_center_coc = center_data.
coc;
698 float bordering_radius = 0.5f;
701 center_data, bordering_radius, i_radius,
false,
true,
true, fg_accum);
703 center_data, bordering_radius, i_radius,
false,
false,
true, bg_accum);
706 float bg_weight, fg_weight;
709 int total_sample_count = int(sample_count) * 2 + 1;
711 total_sample_count, bg_accum, bg_col, bg_weight, unused_occlusion);
713 total_sample_count, fg_accum, fg_col, fg_weight, unused_occlusion);
716 if (
abs(center_data.
coc) < 0.5f) {
717 bg_col = center_data.
color;
722 float alpha = 1.0f - fg_weight;
723 out_weight = bg_weight * alpha + fg_weight;
724 out_color = bg_col * bg_weight * alpha + fg_col * fg_weight;
#define change_density_at_ring
#define gather_density_change_ring
#define radius_downscale_factor
#define gather_ring_density
#define gather_max_density_change
#define unit_sample_radius
#define gather_ring_count
#define no_gather_occlusion
#define debug_gather_perf
float dof_sample_weight(float coc)
#define dof_max_slight_focus_radius
#define dof_layer_threshold
float dof_layer_weight(float coc, const bool is_foreground, const bool is_resolve)
#define debug_scatter_perf
#define no_smooth_intersection
#define DOF_SLIGHT_FOCUS_DENSITY
#define no_gather_mipmaps
float dof_coc_from_depth(const draw::View &views, DepthOfFieldData dof_data, float2 uv, float depth)
float2 sample_disk(float2 rand)
#define compilation_constant
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
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
float3 neon_gradient(float t)
float2x2 from_rotation(AngleRadian rotation)
MatBase< float, 2, 2 > float2x2
float4 safe_color(float4 c)
float interleaved_gradient(float2 pixel, float seed, float offset)
float2 hammersley_2d(T i, T sample_count)
T read(T depth_buffer_value)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float2 rng_2D_get(const eSamplingDimension dim) const
void dof_slight_focus_gather(float2 frag_coord, sampler2DDepth depth_tx, sampler2D color_tx, sampler2D bkh_lut_tx, float radius, float4 &out_color, float &out_weight, float &out_center_coc)
float dof_gather_accum_weight(float coc, float bordering_radius, bool first_ring)
bool dof_do_density_change(float base_radius, float min_intersectable_radius)
void dof_gather_accumulate_sample(DofGatherData sample_data, float weight, DofGatherData &accum_data)
AccumulatorConstants consts
float dof_load_gather_coc(sampler2D gather_input_coc_tx, float2 uv, float lod)
void dof_gather_accumulate_sample_pair(DofGatherData pair_data[2], float bordering_radius, float intersection_multiplier, bool first_ring, const bool do_fast_gather, const bool foreground, DofGatherData &ring_data, DofGatherData &accum_data)
int dof_gather_total_sample_count(const int ring_count, const int ring_density)
void dof_gather_accumulator(float2 frag_coord, sampler2D color_tx, sampler2D color_bilinear_tx, sampler2D coc_tx, float base_radius, float min_intersectable_radius, const bool do_fast_gather, const bool do_density_change, float4 &out_color, float &out_weight, float2 &out_occlusion)
const DepthOfFieldData & dof_buf
void dof_gather_init(float2 frag_coord, float base_radius, float2 noise, float2 ¢er_co, float &lod, float &intersection_multiplier)
void dof_gather_accumulate_resolve(int total_sample_count, DofGatherData accum_data, float4 &out_col, float &out_weight, float2 &out_occlusion)
int dof_gather_total_sample_count_with_density_change(const int ring_count, const int ring_density, int density_change)
void dof_gather_amend_weight(DofGatherData &sample_data, float weight)
float dof_intersection_weight(float coc, float distance_from_center, float intersection_multiplier)
void dof_gather_accumulate_sample_ring(DofGatherData ring_data, int sample_count, bool first_ring, const bool do_fast_gather, const bool reversed_occlusion, DofGatherData &accum_data)
void dof_gather_accumulate_center_sample(DofGatherData center_data, float bordering_radius, int i_radius, const bool do_fast_gather, const bool foreground, const bool resolve, DofGatherData &accum_data)