Blender V5.3
eevee_depth_of_field_lib.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
10
11#include "draw_view.bsl.hh"
14
15/* -------------------------------------------------------------------- */
18
19#ifndef DOF_SLIGHT_FOCUS_DENSITY
20# define DOF_SLIGHT_FOCUS_DENSITY 2
21#endif
22
23/* Debug options */
24#define debug_gather_perf false
25#define debug_scatter_perf false
26#define debug_resolve_perf false
27
28#define no_smooth_intersection false
29#define no_gather_occlusion false
30#define no_gather_mipmaps false
31#define no_gather_random false
32#define no_gather_filtering false
33#define no_scatter_occlusion false
34#define no_scatter_pass false
35#define no_foreground_pass false
36#define no_background_pass false
37#define no_slight_focus_pass false
38#define no_focus_pass false
39#define no_hole_fill_pass false
40
41/* Distribute weights between near/slight-focus/far fields (slide 117). */
42#define dof_layer_threshold (4.0f)
43/* Make sure it overlaps. */
44#define dof_layer_offset_fg (0.5f + 1.0f)
45/* Extra offset for convolution layers to avoid light leaking from background. */
46#define dof_layer_offset (0.5f + 0.5f)
47
48#define dof_max_slight_focus_radius DOF_MAX_SLIGHT_FOCUS_RADIUS
49
50const uint2 quad_offsets_u[4] = {uint2(0, 1), uint2(1, 1), uint2(1, 0), uint2(0, 0)};
51const float2 quad_offsets[4] = {
52 float2(-0.5f, 0.5f), float2(0.5f, 0.5f), float2(0.5f, -0.5f), float2(-0.5f, -0.5f)};
53
55
56/* -------------------------------------------------------------------- */
59
61{
62 /* Very fast "luma" weighting. */
63 float luma = (color.g * 2.0f) + (color.r + color.b);
64 /* TODO(fclem) Pass correct exposure. */
65 constexpr float exposure = 1.0f;
66 return 1.0f / (luma * exposure + 4.0f);
67}
68
70{
71 /* Select biggest coc. */
72 float selected_coc = cocs.x;
73 if (abs(cocs.y) > abs(selected_coc)) {
74 selected_coc = cocs.y;
75 }
76 if (abs(cocs.z) > abs(selected_coc)) {
77 selected_coc = cocs.z;
78 }
79 if (abs(cocs.w) > abs(selected_coc)) {
80 selected_coc = cocs.w;
81 }
82 return selected_coc;
83}
84
85/* NOTE: Do not forget to normalize weights afterwards. */
87{
88 float chosen_coc = dof_coc_select(cocs);
89
90 constexpr float scale = 4.0f; /* TODO(fclem) revisit. */
91 /* NOTE: The difference between the cocs should be inside a abs() function,
92 * but we follow UE4 implementation to improve how dithered transparency looks (see slide 19). */
93 return saturate(1.0f - (chosen_coc - cocs) * scale);
94}
95
96/* NOTE: Do not forget to normalize weights afterwards. */
98{
99 float4 weights;
100 for (int i = 0; i < 4; i++) {
101 weights[i] = dof_hdr_color_weight(colors[i]);
102 }
103 return weights;
104}
105
106/* Returns signed Circle of confusion radius (in pixel) based on depth buffer value [0..1]. */
108 DepthOfFieldData dof_data,
109 float2 uv,
110 float depth)
111{
112 const ViewMatrices view = views.get(0);
113
114 if (is_panoramic(dof_data.camera_type)) {
115 /* Use radial depth. */
116 depth = -length(view.point_screen_to_view(float3(uv, depth)));
117 }
118 else {
119 depth = view.depth_screen_to_view(depth);
120 }
121 return coc_radius_from_camera_depth(dof_data, depth);
122}
123
125
126/* -------------------------------------------------------------------- */
129
130float dof_layer_weight(float coc, const bool is_foreground, const bool is_resolve)
131{
132 /* NOTE: These are full-resolution pixel CoC value. */
133 if (is_resolve) {
135 float(is_foreground ? (coc <= 0.5f) : (coc > -0.5f));
136 }
137
138 coc *= 2.0f; /* Account for half pixel gather. */
139 float threshold = dof_layer_threshold -
140 ((is_foreground) ? dof_layer_offset_fg : dof_layer_offset);
141 return saturate(((is_foreground) ? -coc : coc) - threshold);
142}
143
145{
146 /* NOTE: Used for scatter pass which already flipped the sign correctly. */
147 coc *= 2.0f; /* Account for half pixel gather. */
149}
150
151/* NOTE: This is half-resolution CoC radius. */
152float dof_sample_weight(float coc)
153{
154#if 1 /* Optimized */
155 return min(1.0f, 1.0f / square(coc));
156#else
157 /* Full intensity if CoC radius is below the pixel footprint. */
158 constexpr float min_coc = 1.0f;
159 coc = max(min_coc, abs(coc));
160 return (M_PI * min_coc * min_coc) / (M_PI * coc * coc);
161#endif
162}
164{
165#if 1 /* Optimized */
166 return min(float4(1.0f), 1.0f / square(coc));
167#else
168 /* Full intensity if CoC radius is below the pixel footprint. */
169 constexpr float min_coc = 1.0f;
170 coc = max(float4(min_coc), abs(coc));
171 return (M_PI * min_coc * min_coc) / (M_PI * coc * coc);
172#endif
173}
174
176
177/* -------------------------------------------------------------------- */
180
189
190/* WATCH: Might have to change depending on the texture format. */
191static constexpr float dof_tile_large_coc = 1024.0f;
192
193/* Init a CoC tile for reduction algorithms. */
195{
197 tile.fg_min_coc = 0.0f;
198 tile.fg_max_coc = -dof_tile_large_coc;
199 tile.fg_max_intersectable_coc = dof_tile_large_coc;
200 tile.bg_min_coc = dof_tile_large_coc;
201 tile.bg_max_coc = 0.0f;
202 tile.bg_min_intersectable_coc = dof_tile_large_coc;
203 return tile;
204}
205
207{
209 tile.fg_min_coc = -fg.x;
210 tile.fg_max_coc = -fg.y;
211 tile.fg_max_intersectable_coc = -fg.z;
212 tile.bg_min_coc = bg.x;
213 tile.bg_max_coc = bg.y;
214 tile.bg_min_intersectable_coc = bg.z;
215 return tile;
216}
217
219{
220 out_fg.x = -tile.fg_min_coc;
221 out_fg.y = -tile.fg_max_coc;
222 out_fg.z = -tile.fg_max_intersectable_coc;
223 out_bg.x = tile.bg_min_coc;
224 out_bg.y = tile.bg_max_coc;
225 out_bg.z = tile.bg_min_intersectable_coc;
226}
227
228[[force_inline]] CocTile dof_coc_tile_load(image2D tiles_fg_img, image2D tiles_bg_img, int2 texel)
229{
230 return dof_coc_tile_unpack(
231 imageLoad(tiles_fg_img, clamp(texel, int2(0), imageSize(tiles_fg_img) - 1)).xyz,
232 imageLoad(tiles_bg_img, clamp(texel, int2(0), imageSize(tiles_bg_img) - 1)).xyz);
233}
234
236 image2D tiles_bg_img,
237 int2 texel_out,
238 CocTile tile_data)
239{
240 float3 out_fg;
241 float3 out_bg;
242 dof_coc_tile_pack(tile_data, out_fg, out_bg);
243 imageStore(tiles_fg_img, texel_out, out_fg.xyzz);
244 imageStore(tiles_bg_img, texel_out, out_bg.xyzz);
245}
246
247bool dof_do_fast_gather(float max_absolute_coc,
248 float min_absolute_coc,
249 const bool is_foreground,
250 const bool is_resolve)
251{
252 float min_weight = dof_layer_weight(
253 (is_foreground) ? -min_absolute_coc : min_absolute_coc, is_foreground, is_resolve);
254 if (min_weight < 1.0f) {
255 return false;
256 }
257 /* FIXME(fclem): This is a workaround to fast gather triggering too early. Since we use custom
258 * opacity mask, the opacity is not given to be 100% even for after normal threshold. */
259 if (is_foreground && min_absolute_coc < dof_layer_threshold) {
260 return false;
261 }
262 return (max_absolute_coc - min_absolute_coc) < (DOF_FAST_GATHER_COC_ERROR * max_absolute_coc);
263}
264
272
278{
279 /* Based on tile value, predict what pass we need to load. */
280 CocTilePrediction predict;
281
282 predict.do_foreground = (-tile.fg_min_coc > dof_layer_threshold - dof_layer_offset_fg);
283 bool fg_fully_opaque = predict.do_foreground &&
284 dof_do_fast_gather(-tile.fg_min_coc, -tile.fg_max_coc, true, is_resolve);
285 predict.do_background = !fg_fully_opaque &&
287#if 0 /* Unused. */
288 bool bg_fully_opaque = predict.do_background &&
289 dof_do_fast_gather(-tile.bg_max_coc, tile.bg_min_coc, false, is_resolve);
290#endif
291 predict.do_hole_fill = !fg_fully_opaque && -tile.fg_min_coc > 0.0f;
292 predict.do_focus = !fg_fully_opaque;
293 predict.do_slight_focus = !fg_fully_opaque;
294
295#if 0 /* Debug */
296 predict.do_foreground = predict.do_background = predict.do_hole_fill = true;
297#endif
298 return predict;
299}
300
302
303/* -------------------------------------------------------------------- */
306
319int2 dof_square_ring_sample_offset(int ring_distance, int sample_id)
320{
321 int2 offset;
322 if (sample_id < ring_distance) {
323 offset.x = ring_distance;
324 offset.y = sample_id;
325 }
326 else if (sample_id < ring_distance * 3) {
327 offset.x = ring_distance - sample_id + ring_distance;
328 offset.y = ring_distance;
329 }
330 else {
331 offset.x = -ring_distance;
332 offset.y = ring_distance - sample_id + 3 * ring_distance;
333 }
334 return offset;
335}
336
#define M_PI
btScalar max
void dof_coc_tile_store(image2D tiles_fg_img, image2D tiles_bg_img, int2 texel_out, CocTile tile_data)
#define dof_layer_offset
#define dof_layer_offset_fg
void dof_coc_tile_pack(CocTile tile, float3 &out_fg, float3 &out_bg)
float dof_coc_select(float4 cocs)
float4 dof_bilateral_color_weights(float4 colors[4])
float dof_sample_weight(float coc)
bool dof_do_fast_gather(float max_absolute_coc, float min_absolute_coc, const bool is_foreground, const bool is_resolve)
CocTile dof_coc_tile_unpack(float3 fg, float3 bg)
CocTilePrediction dof_coc_tile_prediction_get(CocTile tile, const bool is_resolve)
#define dof_layer_threshold
float4 dof_bilateral_coc_weights(float4 cocs)
float dof_hdr_color_weight(float4 color)
const uint2 quad_offsets_u[4]
int2 dof_square_ring_sample_offset(int ring_distance, int sample_id)
float dof_layer_weight(float coc, const bool is_foreground, const bool is_resolve)
CocTile dof_coc_tile_load(image2D tiles_fg_img, image2D tiles_bg_img, int2 texel)
static constexpr float dof_tile_large_coc
const float2 quad_offsets[4]
CocTile dof_coc_tile_init()
float dof_coc_from_depth(const draw::View &views, DepthOfFieldData dof_data, float2 uv, float depth)
#define resource_table
#define force_inline
#define abs
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)
float square(float v)
float3x3 scale(float3x3 mat, float2 scale)
const ccl_global KernelWorkTile * tile
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
float depth_screen_to_view(float ss_depth) const
float3 point_screen_to_view(float3 ssP) const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
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