Blender V5.3
eevee_depth_of_field_scatter.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#pragma once
12
16
17float4 linearstep(float p0, float p1, float4 v)
18{
19 return saturate((v - p0) / abs(p1 - p0));
20}
21
23
38
39struct Resources {
40 [[storage(0, read)]] const ScatterRect (&scatter_list_buf)[];
41
43
44 [[uniform(0)]] const DepthOfFieldData &dof_buf;
45
48};
49
50[[vertex]]
52 [[instance_id]] const int inst_id,
53 [[vertex_id]] const int vert_id,
54 [[out]] VertOut &v_out,
55 [[position]] float4 &out_position)
56{
57 if (uint(inst_id) >= srt.dof_buf.scatter_max_rect) {
58 /* Very unlikely to happen but better avoid out of bound access. */
59 out_position = float4(0.0f);
60 return;
61 }
62
63 ScatterRect rect = srt.scatter_list_buf[inst_id];
64
65 v_out.color_and_coc1 = rect.color_and_coc[0];
66 v_out.color_and_coc2 = rect.color_and_coc[1];
67 v_out.color_and_coc3 = rect.color_and_coc[2];
68 v_out.color_and_coc4 = rect.color_and_coc[3];
69
70 float2 uv = float2(vert_id & 1, vert_id >> 1) * 2.0f - 1.0f;
71 uv = uv * rect.half_extent;
72
73 out_position = float4(uv + rect.offset, 0.0f, 1.0f);
74 /* NDC range [-1..1]. */
75 out_position.xy = (out_position.xy / float2(textureSize(srt.occlusion_tx, 0).xy)) * 2.0f - 1.0f;
76
77 if (srt.use_bokeh_lut) {
78 /* Bias scale to avoid sampling at the texture's border. */
79 v_out.distance_scale = (float(DOF_BOKEH_LUT_SIZE) / float(DOF_BOKEH_LUT_SIZE - 1));
80 float2 uv_div = 1.0f / (v_out.distance_scale * abs(rect.half_extent));
81 v_out.rect_uv1 = ((uv + quad_offsets[0]) * uv_div) * 0.5f + 0.5f;
82 v_out.rect_uv2 = ((uv + quad_offsets[1]) * uv_div) * 0.5f + 0.5f;
83 v_out.rect_uv3 = ((uv + quad_offsets[2]) * uv_div) * 0.5f + 0.5f;
84 v_out.rect_uv4 = ((uv + quad_offsets[3]) * uv_div) * 0.5f + 0.5f;
85 /* Only for sampling. */
86 v_out.distance_scale *= reduce_max(abs(rect.half_extent));
87 }
88 else {
89 v_out.distance_scale = 1.0f;
90 v_out.rect_uv1 = uv + quad_offsets[0];
91 v_out.rect_uv2 = uv + quad_offsets[1];
92 v_out.rect_uv3 = uv + quad_offsets[2];
93 v_out.rect_uv4 = uv + quad_offsets[3];
94 }
95}
96
97struct FragOut {
99};
100
101[[fragment]]
103 [[frag_coord]] const float4 frag_co,
104 [[in]] const VertOut &v_out,
105 [[out]] FragOut &frag_out)
106{
107 float4 coc4 = float4(v_out.color_and_coc1.w,
108 v_out.color_and_coc2.w,
109 v_out.color_and_coc3.w,
110 v_out.color_and_coc4.w);
111 float4 shapes;
112 if (srt.use_bokeh_lut) {
113 shapes = float4(texture(srt.bokeh_lut_tx, v_out.rect_uv1).r,
114 texture(srt.bokeh_lut_tx, v_out.rect_uv2).r,
115 texture(srt.bokeh_lut_tx, v_out.rect_uv3).r,
116 texture(srt.bokeh_lut_tx, v_out.rect_uv4).r);
117 }
118 else {
119 shapes = float4(length(v_out.rect_uv1),
120 length(v_out.rect_uv2),
121 length(v_out.rect_uv3),
122 length(v_out.rect_uv4));
123 }
124 shapes *= v_out.distance_scale;
125 /* Becomes signed distance field in pixel units. */
126 shapes -= coc4;
127 /* Smooth the edges a bit to fade out the undersampling artifacts. */
128 shapes = saturate(1.0f - linearstep(-0.8f, 0.8f, shapes));
129 /* Outside of bokeh shape. Try to avoid overloading ROPs. */
130 if (reduce_max(shapes) == 0.0f) {
132 return;
133 }
134
136 /* Works because target is the same size as occlusion_tx. */
137 float2 uv = frag_co.xy / float2(textureSize(srt.occlusion_tx, 0).xy);
138 float2 occlusion_data = texture(srt.occlusion_tx, uv).rg;
139 /* Fix tilling artifacts. (Slide 90) */
140 constexpr float correction_fac = 1.0f - DOF_FAST_GATHER_COC_ERROR;
141 /* Occlude the sprite with geometry from the same field using a chebychev test (slide 85). */
142 float mean = occlusion_data.x;
143 float variance = occlusion_data.y;
144 shapes *= variance * safe_rcp(variance + square(max(coc4 * correction_fac - mean, 0.0f)));
145 }
146
147 frag_out.color = (v_out.color_and_coc1 * shapes[0] + v_out.color_and_coc2 * shapes[1] +
148 v_out.color_and_coc3 * shapes[2] + v_out.color_and_coc4 * shapes[3]);
149 /* Do not accumulate alpha. This has already been accumulated by the gather pass. */
150 frag_out.color.a = 0.0f;
151
152 if (debug_scatter_perf) {
153 frag_out.color.rgb = average(frag_out.color.rgb) * float3(1.0f, 0.0f, 0.0f);
154 }
155}
156
157} // namespace eevee::dof::scatter
158
btScalar max
#define no_scatter_occlusion
#define debug_scatter_perf
const float2 quad_offsets[4]
PipelineGraphic eevee_depth_of_field_scatter(eevee::dof::scatter::vert_main, eevee::dof::scatter::frag_main)
float4 linearstep(float p0, float p1, float4 v)
#define instance_id
#define uniform(slot)
#define storage(slot, qualifiers)
#define position
#define flat
#define frag_coord
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define no_perspective
#define vertex
#define frag_color(slot)
void gpu_discard_fragment()
#define abs
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float square(float v)
float safe_rcp(float a)
float average(float2 a)
float reduce_max(float2 a)
void frag_main(Resources &srt, const float4 frag_co, const VertOut &v_out, FragOut &frag_out)
void vert_main(Resources &srt, const int inst_id, const int vert_id, VertOut &v_out, float4 &out_position)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
float x
float y