Blender V5.3
eevee_depth_of_field_resolve.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
13
14#pragma once
15
18
20
21struct Resources {
24
26
36
38
40
45 {
46 float local_abs_max = 0.0f;
47 /* Sample in a cross (X) pattern. This covers all pixels over the whole tile, as long as
48 * dof_max_slight_focus_radius is less than the group size. */
49 for (int i = 0; i < 4; i++) {
50 float2 sample_uv = (frag_coord + quad_offsets[i] * 2.0f * dof_max_slight_focus_radius) /
52 float depth = reverse_z::read(textureLod(depth_tx, sample_uv, 0.0f).r);
53 float coc = dof_coc_from_depth(views, accumulator.dof_buf, sample_uv, depth);
54 coc = clamp(coc, -accumulator.dof_buf.coc_abs_max, accumulator.dof_buf.coc_abs_max);
56 local_abs_max = max(local_abs_max, abs(coc));
57 }
58 }
59
60 if (local_index == 0u) {
62 }
63 barrier();
64 /* Use atomic reduce operation. */
66 /* "Broadcast" result across all threads. */
67 barrier();
68
70 }
71
72 float3 dof_neighborhood_clamp(float2 frag_coord, float3 color, float center_coc, float weight)
73 {
74 /* Stabilize color by clamping with the stable half res neighborhood. */
75 float3 neighbor_min, neighbor_max;
76 const float2 corners[4] = {float2(-1, -1), float2(1, -1), float2(-1, 1), float2(1, 1)};
77 for (int i = 0; i < 4; i++) {
92 float2 uv_sample = ((frag_coord + corners[i]) * 0.5f) /
94 /* Reminder: The content of this buffer is YCoCg + CoC. */
95 float3 ycocg_sample = textureLod(stable_color_tx, uv_sample, 0.0f).rgb;
96 neighbor_min = (i == 0) ? ycocg_sample : min(neighbor_min, ycocg_sample);
97 neighbor_max = (i == 0) ? ycocg_sample : max(neighbor_max, ycocg_sample);
98 }
99 /* Pad the bounds in the near in focus region to get back a bit of detail. */
100 float padding = 0.125f * saturate(1.0f - square(center_coc) / square(8.0f));
101 neighbor_max += abs(neighbor_min) * padding;
102 neighbor_min -= abs(neighbor_min) * padding;
103 /* Progressively apply the clamp to avoid harsh transition. Also mask by weight. */
104 float fac = saturate(square(max(0.0f, abs(center_coc) - 0.5f)) * 4.0f) * weight;
105 /* Clamp in YCoCg space to avoid too much color drift. */
107 color = mix(color, clamp(color, neighbor_min, neighbor_max), fac);
109 return color;
110 }
111};
112
113[[compute, local_size(DOF_RESOLVE_GROUP_SIZE, DOF_RESOLVE_GROUP_SIZE)]]
115 [[resource_table]] const draw::View &views,
117 [[global_invocation_id]] const uint3 global_id,
118 [[local_invocation_index]] const uint local_index)
119{
120 [[resource_table]] Accumulator &accum = srt.accumulator;
121
122 float2 frag_coord = float2(global_id.xy) + 0.5f;
123 int2 tile_co = int2(frag_coord / float(DOF_TILES_SIZE * 2));
124
125 CocTile coc_tile = dof_coc_tile_load(tiles.in_tiles_fg_img, tiles.in_tiles_bg_img, tile_co);
126 CocTilePrediction prediction = dof_coc_tile_prediction_get(coc_tile, true);
127
128 float2 uv = frag_coord / float2(textureSize(srt.color_tx, 0));
129 float2 uv_halfres = (frag_coord * 0.5f) / float2(textureSize(srt.color_bg_tx, 0));
130
131 float slight_focus_max_coc = 0.0f;
132 if (prediction.do_slight_focus) {
133 slight_focus_max_coc = srt.slight_focus_coc_tile_get(frag_coord, local_index);
134 prediction.do_slight_focus = slight_focus_max_coc >= 0.5f;
135 if (prediction.do_slight_focus) {
136 prediction.do_focus = false;
137 }
138 }
139
140 if (prediction.do_focus) {
141 float depth = reverse_z::read(textureLod(srt.depth_tx, uv, 0.0f).r);
142 float center_coc = dof_coc_from_depth(views, accum.dof_buf, uv, depth);
143 prediction.do_focus = abs(center_coc) <= 0.5f;
144 }
145
146 float4 out_color = float4(0.0f);
147 float weight = 0.0f;
148
149 float4 layer_color;
150 float layer_weight;
151
152 constexpr float3 hole_fill_color = float3(0.2f, 0.1f, 1.0f);
153 constexpr float3 background_color = float3(0.1f, 0.2f, 1.0f);
154 constexpr float3 slight_focus_color = float3(1.0f, 0.2f, 0.1f);
155 constexpr float3 focus_color = float3(1.0f, 1.0f, 0.1f);
156 constexpr float3 foreground_color = float3(0.2f, 1.0f, 0.1f);
157
158 if (!no_hole_fill_pass && prediction.do_hole_fill) {
159 layer_color = textureLod(srt.color_hole_fill_tx, uv_halfres, 0.0f);
160 layer_weight = textureLod(srt.weight_hole_fill_tx, uv_halfres, 0.0f).r;
161 if (srt.do_debug_color) {
162 layer_color.rgb *= hole_fill_color;
163 }
164 out_color = layer_color * safe_rcp(layer_weight);
165 weight = float(layer_weight > 0.0f);
166 }
167
168 if (!no_background_pass && prediction.do_background) {
169 layer_color = textureLod(srt.color_bg_tx, uv_halfres, 0.0f);
170 layer_weight = textureLod(srt.weight_bg_tx, uv_halfres, 0.0f).r;
171 if (srt.do_debug_color) {
172 layer_color.rgb *= background_color;
173 }
174 /* Always prefer background to hole_fill pass. */
175 layer_color *= safe_rcp(layer_weight);
176 layer_weight = float(layer_weight > 0.0f);
177 /* Composite background. */
178 out_color = out_color * (1.0f - layer_weight) + layer_color;
179 weight = weight * (1.0f - layer_weight) + layer_weight;
180 /* Fill holes with the composited background. */
181 out_color *= safe_rcp(weight);
182 weight = float(weight > 0.0f);
183 }
184
185 if (!no_slight_focus_pass && prediction.do_slight_focus) {
186 float center_coc;
187 if (accum.consts.use_lut) [[static_branch]] {
188 accum.dof_slight_focus_gather(float2(global_id.xy) + 0.5f,
189 srt.depth_tx,
190 srt.color_tx,
191 accum.bokeh_lut_tx,
192 slight_focus_max_coc,
193 layer_color,
194 layer_weight,
195 center_coc);
196 }
197 else {
198 accum.dof_slight_focus_gather(float2(global_id.xy) + 0.5f,
199 srt.depth_tx,
200 srt.color_tx,
201 srt.color_tx, /* Dummy. */
202 slight_focus_max_coc,
203 layer_color,
204 layer_weight,
205 center_coc);
206 }
207
208 if (srt.do_debug_color) {
209 layer_color.rgb *= slight_focus_color;
210 }
211
212 /* Composite slight defocus. */
213 out_color = out_color * (1.0f - layer_weight) + layer_color;
214 weight = weight * (1.0f - layer_weight) + layer_weight;
215
216 // out_color.rgb = dof_neighborhood_clamp(frag_coord, out_color.rgb, center_coc, layer_weight);
217 }
218
219 if (!no_focus_pass && prediction.do_focus) {
220 layer_color = colorspace::safe_color(textureLod(srt.color_tx, uv, 0.0f));
221 layer_weight = 1.0f;
222 if (srt.do_debug_color) {
223 layer_color.rgb *= focus_color;
224 }
225 /* Composite in focus. */
226 out_color = out_color * (1.0f - layer_weight) + layer_color;
227 weight = weight * (1.0f - layer_weight) + layer_weight;
228 }
229
230 if (!no_foreground_pass && prediction.do_foreground) {
231 layer_color = textureLod(srt.color_fg_tx, uv_halfres, 0.0f);
232 layer_weight = textureLod(srt.weight_fg_tx, uv_halfres, 0.0f).r;
233 if (srt.do_debug_color) {
234 layer_color.rgb *= foreground_color;
235 }
236 /* Composite foreground. */
237 out_color = out_color * (1.0f - layer_weight) + layer_color;
238 }
239
240 /* Fix float precision issue in alpha compositing. */
241 if (out_color.a > 0.99f) {
242 out_color.a = 1.0f;
243 }
244
245 if (debug_resolve_perf && prediction.do_slight_focus) {
246 out_color.rgb *= float3(1.0f, 0.1f, 0.1f);
247 }
248
249 imageStore(srt.out_color_img, int2(global_id.xy), out_color);
250}
251
252} // namespace eevee::dof::resolve
253
256 .is_hole_fill = false,
257 .is_resolve = true,
258 .is_foreground = false,
259 .use_lut = true,
260 });
263 .is_hole_fill = false,
264 .is_resolve = true,
265 .is_foreground = false,
266 .use_lut = false,
267 });
btScalar max
#define no_foreground_pass
#define dof_max_slight_focus_radius
CocTilePrediction dof_coc_tile_prediction_get(CocTile tile, const bool is_resolve)
#define no_hole_fill_pass
CocTile dof_coc_tile_load(image2D tiles_fg_img, image2D tiles_bg_img, int2 texel)
const float2 quad_offsets[4]
#define debug_resolve_perf
#define no_focus_pass
#define no_background_pass
#define no_slight_focus_pass
float dof_coc_from_depth(const draw::View &views, DepthOfFieldData dof_data, float2 uv, float depth)
PipelineCompute eevee_depth_of_field_resolve_lut(eevee::dof::resolve::comp_main, eevee::dof::AccumulatorConstants{ .is_hole_fill=false,.is_resolve=true,.is_foreground=false,.use_lut=true, })
PipelineCompute eevee_depth_of_field_resolve_no_lut(eevee::dof::resolve::comp_main, eevee::dof::AccumulatorConstants{ .is_hole_fill=false,.is_resolve=true,.is_foreground=false,.use_lut=false, })
@ SFLOAT_16_16_16_16
#define shared
#define local_invocation_index
#define image(slot, qualifiers, format)
#define frag_coord
#define resource_table
#define sampler(slot)
#define static_branch
#define specialization_constant(default_value)
#define global_invocation_id
#define compute
#define local_size(...)
int atomicMax(int &, int) RET
#define abs
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
void barrier()
constexpr T clamp(T, U, U) RET
ImageBase< float, 2 > image2D
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
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 square(float v)
float safe_rcp(float a)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
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 draw::View &views, Tiles &tiles, const uint3 global_id, const uint local_index)
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
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 slight_focus_coc_tile_get(float2 frag_coord, uint local_index)
float3 dof_neighborhood_clamp(float2 frag_coord, float3 color, float center_coc, float weight)