Blender V5.3
eevee_depth_of_field.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
19
20#include "DRW_render.hh"
21
22#include "BKE_camera.h"
23#include "DNA_camera_types.h"
24
25#include "GPU_platform.hh"
26#include "GPU_texture.hh"
27
28#include "GPU_debug.hh"
29
30#include "eevee_camera.hh"
31#include "eevee_instance.hh"
32#include "eevee_sampling.hh"
33#include "eevee_shader.hh"
35
37
38namespace blender::eevee {
39
40/* -------------------------------------------------------------------- */
43
45{
46 const SceneEEVEE &sce_eevee = inst_.scene->eevee;
47 const Object *camera_object_eval = inst_.camera_eval_object;
48 const blender::Camera *camera = (camera_object_eval && camera_object_eval->type == OB_CAMERA) ?
49 reinterpret_cast<const blender::Camera *>(
50 camera_object_eval->data) :
51 nullptr;
52
53 /* TODO: Support depth of field for panoramic camera. */
54 enabled_ = camera && (camera->dof.flag & CAM_DOF_ENABLED) != 0 && camera->type != CAM_PANO;
55
56 if (enabled_ == false) {
57 /* Set to invalid value for update detection */
58 data_.scatter_color_threshold = -1.0f;
59 return;
60 }
61 /* Reminder: These are parameters not interpolated by motion blur. */
62 int sce_flag = sce_eevee.flag;
63 do_jitter_ = (sce_flag & SCE_EEVEE_DOF_JITTER) != 0;
64 user_overblur_ = sce_eevee.bokeh_overblur / 100.0f;
65 fx_max_coc_ = sce_eevee.bokeh_max_size;
66 data_.scatter_color_threshold = sce_eevee.bokeh_threshold;
67 data_.scatter_neighbor_max_color = sce_eevee.bokeh_neighbor_max;
68 data_.bokeh_blades = float(camera->dof.aperture_blades);
69}
70
72{
73 const Camera &camera = inst_.camera;
74 const Object *camera_object_eval = inst_.camera_eval_object;
75 const blender::Camera *camera_data = (camera_object_eval &&
76 camera_object_eval->type == OB_CAMERA) ?
77 reinterpret_cast<const blender::Camera *>(
78 camera_object_eval->data) :
79 nullptr;
80
81 if (inst_.debug_mode == DEBUG_DOF_PLANES) {
82 /* Set debug message even if DOF is not enabled. */
83 inst_.info_append(
84 "Debug Mode: Depth Of Field Buffers\n"
85 " - Purple: Gap Fill\n"
86 " - Blue: Background\n"
87 " - Red: Slight Out Of Focus\n"
88 " - Yellow: In Focus\n"
89 " - Green: Foreground\n");
90 }
91
92 if (enabled_ == false) {
93 jitter_radius_ = 0.0f;
94 fx_radius_ = 0.0f;
95 return;
96 }
97
98 float2 anisotropic_scale = {clamp_f(1.0f / camera_data->dof.aperture_ratio, 1e-5f, 1.0f),
99 clamp_f(camera_data->dof.aperture_ratio, 1e-5f, 1.0f)};
100 data_.bokeh_anisotropic_scale = anisotropic_scale;
101 data_.bokeh_rotation = camera_data->dof.aperture_rotation;
102 focus_distance_ = BKE_camera_object_dof_distance(camera_object_eval);
103 data_.bokeh_anisotropic_scale_inv = 1.0f / data_.bokeh_anisotropic_scale;
104
105 float fstop = max_ff(camera_data->dof.aperture_fstop, 1e-5f);
106
107 float aperture = 1.0f / (2.0f * fstop);
108 if (camera.is_perspective()) {
109 aperture *= camera_data->lens * 1e-3f;
110 }
111
112 if (camera.is_orthographic()) {
113 /* FIXME: Why is this needed? Some kind of implicit unit conversion? */
114 aperture *= 0.04f;
115 }
116
117 if (camera_data->dof.aperture_ratio < 1.0) {
118 /* If ratio is scaling the bokeh outwards, we scale the aperture so that
119 * the gather kernel size will encompass the maximum axis. */
120 aperture /= max_ff(camera_data->dof.aperture_ratio, 1e-5f);
121 }
122
123 float jitter_radius, fx_radius;
124
125 /* Balance blur radius between fx dof and jitter dof. */
126 if (do_jitter_ && (inst_.sampling.dof_ring_count_get() > 0) && !camera.is_panoramic() &&
127 !inst_.is_viewport())
128 {
129 /* Compute a minimal overblur radius to fill the gaps between the samples.
130 * This is just the simplified form of dividing the area of the bokeh by
131 * the number of samples. */
132 float minimal_overblur = 1.0f / sqrtf(inst_.sampling.dof_sample_count_get());
133
134 fx_radius = (minimal_overblur + user_overblur_) * aperture;
135 /* Avoid dilating the shape. Over-blur only soften. */
136 jitter_radius = max_ff(0.0f, aperture - fx_radius);
137 }
138 else {
139 jitter_radius = 0.0f;
140 fx_radius = aperture;
141 }
142
143 /* Disable post fx if result wouldn't be noticeable. */
144 if (fx_max_coc_ <= 0.5f) {
145 fx_radius = 0.0f;
146 }
147
148 jitter_radius_ = jitter_radius;
149 fx_radius_ = fx_radius;
150
151 if (fx_radius_ == 0.0f) {
152 return;
153 }
154
155 int2 max_render_res = inst_.film.render_extent_get();
156 int2 half_render_res = math::divide_ceil(max_render_res, int2(2));
157 int2 reduce_size = math::ceil_to_multiple(half_render_res, int2(DOF_REDUCE_GROUP_SIZE));
158
159 data_.gather_uv_fac = 1.0f / float2(reduce_size);
160
161 /* Resize the scatter list to contain enough entry to cover half the screen with sprites (which
162 * is unlikely due to local contrast test). */
163 data_.scatter_max_rect = (reduce_size.x * reduce_size.y / 4) / 2;
164 scatter_fg_list_buf_.resize(data_.scatter_max_rect);
165 scatter_bg_list_buf_.resize(data_.scatter_max_rect);
166
167 bokeh_lut_pass_sync();
168 setup_pass_sync();
169 stabilize_pass_sync();
170 downsample_pass_sync();
171 reduce_pass_sync();
172 tiles_flatten_pass_sync();
173 tiles_dilate_pass_sync();
174 gather_pass_sync();
175 filter_pass_sync();
176 scatter_pass_sync();
177 hole_fill_pass_sync();
178 resolve_pass_sync();
179}
180
182{
183 if (jitter_radius_ == 0.0f) {
184 return;
185 }
186
187 float radius, theta;
188 inst_.sampling.dof_disk_sample_get(&radius, &theta);
189
190 if (data_.bokeh_blades >= 3.0f) {
191 theta = circle_to_polygon_angle(data_.bokeh_blades, theta);
192 radius *= circle_to_polygon_radius(data_.bokeh_blades, theta);
193 }
194 radius *= jitter_radius_;
195 theta += data_.bokeh_rotation;
196
197 /* Sample in View Space. */
198 float2 sample = float2(radius * cosf(theta), radius * sinf(theta));
199 sample *= data_.bokeh_anisotropic_scale;
200 /* Convert to NDC Space. */
201 float3 jitter = float3(UNPACK2(sample), -focus_distance_);
202 float3 center = float3(0.0f, 0.0f, -focus_distance_);
203 mul_project_m4_v3(winmat.ptr(), jitter);
204 mul_project_m4_v3(winmat.ptr(), center);
205
206 const bool is_ortho = (winmat[2][3] != -1.0f);
207 if (is_ortho) {
208 sample *= focus_distance_;
209 }
210 /* Translate origin. */
211 sub_v2_v2(viewmat[3], sample);
212 /* Skew winmat Z axis. */
213 add_v2_v2(winmat[2], center - jitter);
214}
215
217
218/* -------------------------------------------------------------------- */
221
222void DepthOfField::bokeh_lut_pass_sync()
223{
224 const bool has_anisotropy = data_.bokeh_anisotropic_scale != float2(1.0f);
225 if (!has_anisotropy && (data_.bokeh_blades == 0.0)) {
226 /* No need for LUTs in these cases. */
227 use_bokeh_lut_ = false;
228 return;
229 }
230 use_bokeh_lut_ = true;
231
232 /* Precompute bokeh texture. */
233 bokeh_lut_ps_.init();
235 bokeh_lut_ps_.bind_ubo("dof_buf", data_);
236 bokeh_lut_ps_.bind_image("out_gather_lut_img", &bokeh_gather_lut_tx_);
237 bokeh_lut_ps_.bind_image("out_scatter_lut_img", &bokeh_scatter_lut_tx_);
238 bokeh_lut_ps_.bind_image("out_resolve_lut_img", &bokeh_resolve_lut_tx_);
239 bokeh_lut_ps_.dispatch(int3(1, 1, 1));
240}
241
242void DepthOfField::setup_pass_sync()
243{
244 RenderBuffers &render_buffers = inst_.render_buffers;
245
246 setup_ps_.init();
248 setup_ps_.bind_texture("color_tx", &input_color_tx_, no_filter);
249 setup_ps_.bind_texture("depth_tx", &render_buffers.depth_tx, no_filter);
250 setup_ps_.bind_ubo("dof_buf", data_);
251 setup_ps_.bind_image("out_color_img", &setup_color_tx_);
252 setup_ps_.bind_image("out_coc_img", &setup_coc_tx_);
253 setup_ps_.dispatch(&dispatch_setup_size_);
255}
256
257void DepthOfField::stabilize_pass_sync()
258{
259 RenderBuffers &render_buffers = inst_.render_buffers;
260 VelocityModule &velocity = inst_.velocity;
261
262 stabilize_ps_.init();
263 stabilize_ps_.shader_set(inst_.shaders.static_shader_get(DOF_STABILIZE));
264 stabilize_ps_.bind_ubo("camera_prev", &(*velocity.camera_steps[STEP_PREVIOUS]));
265 stabilize_ps_.bind_ubo("camera_curr", &(*velocity.camera_steps[STEP_CURRENT]));
266 /* This is only for temporal stability. The next step is not needed. */
267 stabilize_ps_.bind_ubo("camera_next", &(*velocity.camera_steps[STEP_PREVIOUS]));
268 stabilize_ps_.bind_texture("coc_tx", &setup_coc_tx_, no_filter);
269 stabilize_ps_.bind_texture("color_tx", &setup_color_tx_, no_filter);
270 stabilize_ps_.bind_texture("velocity_tx", &render_buffers.vector_tx, no_filter);
271 stabilize_ps_.bind_texture("in_history_tx", &stabilize_input_, with_filter);
272 stabilize_ps_.bind_texture("depth_tx", &render_buffers.depth_tx, no_filter);
273 stabilize_ps_.bind_ubo("dof_buf", data_);
274 stabilize_ps_.push_constant("u_use_history", &stabilize_valid_history_, 1);
275 stabilize_ps_.bind_image("out_coc_img", &reduced_coc_mip_views_[0]);
276 stabilize_ps_.bind_image("out_color_img", &reduced_color_mip_views_[0]);
277 stabilize_ps_.bind_image("out_history_img", &stabilize_output_tx_);
278 stabilize_ps_.dispatch(&dispatch_stabilize_size_);
280}
281
282void DepthOfField::downsample_pass_sync()
283{
284 downsample_ps_.init();
285 downsample_ps_.shader_set(inst_.shaders.static_shader_get(DOF_DOWNSAMPLE));
286 downsample_ps_.bind_texture("color_tx", &reduced_color_mip_views_[0], no_filter);
287 downsample_ps_.bind_texture("coc_tx", &reduced_coc_mip_views_[0], no_filter);
288 downsample_ps_.bind_image("out_color_img", &downsample_tx_);
289 downsample_ps_.dispatch(&dispatch_downsample_size_);
290 downsample_ps_.barrier(GPU_BARRIER_TEXTURE_FETCH);
291}
292
293void DepthOfField::reduce_pass_sync()
294{
295 reduce_ps_.init();
296 reduce_ps_.shader_set(inst_.shaders.static_shader_get(DOF_REDUCE));
297 reduce_ps_.bind_ubo("dof_buf", data_);
298 reduce_ps_.bind_texture("downsample_tx", &downsample_tx_, no_filter);
299 reduce_ps_.bind_ssbo("scatter_fg_list_buf", scatter_fg_list_buf_);
300 reduce_ps_.bind_ssbo("scatter_bg_list_buf", scatter_bg_list_buf_);
301 reduce_ps_.bind_ssbo("scatter_fg_indirect_buf", scatter_fg_indirect_buf_);
302 reduce_ps_.bind_ssbo("scatter_bg_indirect_buf", scatter_bg_indirect_buf_);
303 reduce_ps_.bind_image("inout_color_lod0_img", &reduced_color_mip_views_[0]);
304 reduce_ps_.bind_image("out_color_lod1_img", &reduced_color_mip_views_[1]);
305 reduce_ps_.bind_image("out_color_lod2_img", &reduced_color_mip_views_[2]);
306 reduce_ps_.bind_image("out_color_lod3_img", &reduced_color_mip_views_[3]);
307 reduce_ps_.bind_image("in_coc_lod0_img", &reduced_coc_mip_views_[0]);
308 reduce_ps_.bind_image("out_coc_lod1_img", &reduced_coc_mip_views_[1]);
309 reduce_ps_.bind_image("out_coc_lod2_img", &reduced_coc_mip_views_[2]);
310 reduce_ps_.bind_image("out_coc_lod3_img", &reduced_coc_mip_views_[3]);
311 reduce_ps_.dispatch(&dispatch_reduce_size_);
312 /* NOTE: Command buffer barrier is done automatically by the GPU backend. */
314}
315
316void DepthOfField::tiles_flatten_pass_sync()
317{
318 tiles_flatten_ps_.init();
319 tiles_flatten_ps_.shader_set(inst_.shaders.static_shader_get(DOF_TILES_FLATTEN));
320 /* NOTE(fclem): We should use the reduced_coc_tx_ as it is stable, but we need the slight focus
321 * flag from the setup pass. A better way would be to do the brute-force in focus gather without
322 * this. */
323 tiles_flatten_ps_.bind_texture("coc_tx", &setup_coc_tx_, no_filter);
324 tiles_flatten_ps_.bind_image("out_tiles_fg_img", &tiles_fg_tx_.current());
325 tiles_flatten_ps_.bind_image("out_tiles_bg_img", &tiles_bg_tx_.current());
326 tiles_flatten_ps_.dispatch(&dispatch_tiles_flatten_size_);
327 tiles_flatten_ps_.barrier(GPU_BARRIER_SHADER_IMAGE_ACCESS);
328}
329
330void DepthOfField::tiles_dilate_pass_sync()
331{
332 for (int pass = 0; pass < 2; pass++) {
333 PassSimple &drw_pass = (pass == 0) ? tiles_dilate_minmax_ps_ : tiles_dilate_minabs_ps_;
335 drw_pass.init();
336 drw_pass.shader_set(inst_.shaders.static_shader_get(sh_type));
337 drw_pass.bind_image("in_tiles_fg_img", &tiles_fg_tx_.previous());
338 drw_pass.bind_image("in_tiles_bg_img", &tiles_bg_tx_.previous());
339 drw_pass.bind_image("out_tiles_fg_img", &tiles_fg_tx_.current());
340 drw_pass.bind_image("out_tiles_bg_img", &tiles_bg_tx_.current());
341 drw_pass.push_constant("ring_count", &tiles_dilate_ring_count_, 1);
342 drw_pass.push_constant("ring_width_multiplier", &tiles_dilate_ring_width_mul_, 1);
343 drw_pass.dispatch(&dispatch_tiles_dilate_size_);
344 drw_pass.barrier(GPU_BARRIER_SHADER_IMAGE_ACCESS);
345 }
346}
347
348void DepthOfField::gather_pass_sync()
349{
350 const GPUSamplerState gather_bilinear = {GPU_SAMPLER_FILTERING_MIPMAP |
352 const GPUSamplerState gather_nearest = {GPU_SAMPLER_FILTERING_MIPMAP};
353
354 for (int pass = 0; pass < 2; pass++) {
355 PassSimple &drw_pass = (pass == 0) ? gather_fg_ps_ : gather_bg_ps_;
356 SwapChain<TextureFromPool, 2> &color_chain = (pass == 0) ? color_fg_tx_ : color_bg_tx_;
357 SwapChain<TextureFromPool, 2> &weight_chain = (pass == 0) ? weight_fg_tx_ : weight_bg_tx_;
358 eShaderType sh_type = (pass == 0) ?
359 (use_bokeh_lut_ ? DOF_GATHER_FOREGROUND_LUT :
362 drw_pass.init();
363 drw_pass.bind_resources(inst_.sampling);
364 drw_pass.shader_set(inst_.shaders.static_shader_get(sh_type));
365 drw_pass.bind_ubo("dof_buf", data_);
366 drw_pass.bind_texture("color_bilinear_tx", &reduced_color_tx_, gather_bilinear);
367 drw_pass.bind_texture("color_tx", &reduced_color_tx_, gather_nearest);
368 drw_pass.bind_texture("coc_tx", &reduced_coc_tx_, gather_nearest);
369 drw_pass.bind_image("in_tiles_fg_img", &tiles_fg_tx_.current());
370 drw_pass.bind_image("in_tiles_bg_img", &tiles_bg_tx_.current());
371 drw_pass.bind_image("out_color_img", &color_chain.current());
372 drw_pass.bind_image("out_weight_img", &weight_chain.current());
373 drw_pass.bind_image("out_occlusion_img", &occlusion_tx_);
374 drw_pass.bind_texture("bokeh_lut_tx", &bokeh_gather_lut_tx_);
375 drw_pass.dispatch(&dispatch_gather_size_);
376 drw_pass.barrier(GPU_BARRIER_TEXTURE_FETCH);
377 }
378}
379
380void DepthOfField::filter_pass_sync()
381{
382 for (int pass = 0; pass < 2; pass++) {
383 PassSimple &drw_pass = (pass == 0) ? filter_fg_ps_ : filter_bg_ps_;
384 SwapChain<TextureFromPool, 2> &color_chain = (pass == 0) ? color_fg_tx_ : color_bg_tx_;
385 SwapChain<TextureFromPool, 2> &weight_chain = (pass == 0) ? weight_fg_tx_ : weight_bg_tx_;
386 drw_pass.init();
387 drw_pass.shader_set(inst_.shaders.static_shader_get(DOF_FILTER));
388 drw_pass.bind_texture("color_tx", &color_chain.previous());
389 drw_pass.bind_texture("weight_tx", &weight_chain.previous());
390 drw_pass.bind_image("out_color_img", &color_chain.current());
391 drw_pass.bind_image("out_weight_img", &weight_chain.current());
392 drw_pass.dispatch(&dispatch_filter_size_);
393 drw_pass.barrier(GPU_BARRIER_TEXTURE_FETCH);
394 }
395}
396
397void DepthOfField::scatter_pass_sync()
398{
399 for (int pass = 0; pass < 2; pass++) {
400 PassSimple &drw_pass = (pass == 0) ? scatter_fg_ps_ : scatter_bg_ps_;
401 drw_pass.init();
403 drw_pass.shader_set(inst_.shaders.static_shader_get(DOF_SCATTER));
404 drw_pass.bind_ubo("dof_buf", data_);
405 drw_pass.push_constant("use_bokeh_lut", use_bokeh_lut_);
406 drw_pass.bind_texture("bokeh_lut_tx", &bokeh_scatter_lut_tx_);
407 drw_pass.bind_texture("occlusion_tx", &occlusion_tx_);
408 if (pass == 0) {
409 drw_pass.bind_ssbo("scatter_list_buf", scatter_fg_list_buf_);
410 drw_pass.draw_procedural_indirect(GPU_PRIM_TRI_STRIP, scatter_fg_indirect_buf_);
411 /* Avoid background gather pass writing to the occlusion_tx mid pass. */
412 drw_pass.barrier(GPU_BARRIER_SHADER_IMAGE_ACCESS);
413 }
414 else {
415 drw_pass.bind_ssbo("scatter_list_buf", scatter_bg_list_buf_);
416 drw_pass.draw_procedural_indirect(GPU_PRIM_TRI_STRIP, scatter_bg_indirect_buf_);
417 }
418 }
419}
420
421void DepthOfField::hole_fill_pass_sync()
422{
423 const GPUSamplerState gather_bilinear = {GPU_SAMPLER_FILTERING_MIPMAP |
425 const GPUSamplerState gather_nearest = {GPU_SAMPLER_FILTERING_MIPMAP};
426
427 hole_fill_ps_.init();
428 hole_fill_ps_.bind_resources(inst_.sampling);
429 hole_fill_ps_.shader_set(inst_.shaders.static_shader_get(DOF_GATHER_HOLE_FILL));
430 hole_fill_ps_.bind_ubo("dof_buf", data_);
431 hole_fill_ps_.bind_texture("color_bilinear_tx", &reduced_color_tx_, gather_bilinear);
432 hole_fill_ps_.bind_texture("color_tx", &reduced_color_tx_, gather_nearest);
433 hole_fill_ps_.bind_texture("coc_tx", &reduced_coc_tx_, gather_nearest);
434 hole_fill_ps_.bind_image("in_tiles_fg_img", &tiles_fg_tx_.current());
435 hole_fill_ps_.bind_image("in_tiles_bg_img", &tiles_bg_tx_.current());
436 hole_fill_ps_.bind_image("out_color_img", &hole_fill_color_tx_);
437 hole_fill_ps_.bind_image("out_weight_img", &hole_fill_weight_tx_);
438 hole_fill_ps_.dispatch(&dispatch_gather_size_);
439 hole_fill_ps_.barrier(GPU_BARRIER_TEXTURE_FETCH);
440}
441
442void DepthOfField::resolve_pass_sync()
443{
444 GPUSamplerState with_filter = {GPU_SAMPLER_FILTERING_LINEAR};
445 RenderBuffers &render_buffers = inst_.render_buffers;
446 gpu::Shader *sh = inst_.shaders.static_shader_get(use_bokeh_lut_ ? DOF_RESOLVE_LUT :
448
449 resolve_ps_.init();
450 resolve_ps_.specialize_constant(sh, "do_debug_color", inst_.debug_mode == DEBUG_DOF_PLANES);
451 resolve_ps_.shader_set(sh);
452 resolve_ps_.bind_ubo("dof_buf", data_);
453 resolve_ps_.bind_texture("depth_tx", &render_buffers.depth_tx, no_filter);
454 resolve_ps_.bind_texture("color_tx", &input_color_tx_, no_filter);
455 resolve_ps_.bind_texture("stable_color_tx", &resolve_stable_color_tx_, no_filter);
456 resolve_ps_.bind_texture("color_bg_tx", &color_bg_tx_.current(), with_filter);
457 resolve_ps_.bind_texture("color_fg_tx", &color_fg_tx_.current(), with_filter);
458 resolve_ps_.bind_image("in_tiles_fg_img", &tiles_fg_tx_.current());
459 resolve_ps_.bind_image("in_tiles_bg_img", &tiles_bg_tx_.current());
460 resolve_ps_.bind_texture("weight_bg_tx", &weight_bg_tx_.current());
461 resolve_ps_.bind_texture("weight_fg_tx", &weight_fg_tx_.current());
462 resolve_ps_.bind_texture("color_hole_fill_tx", &hole_fill_color_tx_);
463 resolve_ps_.bind_texture("weight_hole_fill_tx", &hole_fill_weight_tx_);
464 resolve_ps_.bind_texture("bokeh_lut_tx", &bokeh_resolve_lut_tx_);
465 resolve_ps_.bind_image("out_color_img", &output_color_tx_);
466 resolve_ps_.bind_resources(inst_.sampling);
467 resolve_ps_.barrier(GPU_BARRIER_TEXTURE_FETCH);
468 resolve_ps_.dispatch(&dispatch_resolve_size_);
469 resolve_ps_.barrier(GPU_BARRIER_TEXTURE_FETCH);
470}
471
473
474/* -------------------------------------------------------------------- */
477
478void DepthOfField::update_sample_table()
479{
480 float2 subpixel_offset = inst_.film.pixel_jitter_get();
481 /* Since the film jitter is in full-screen res, divide by 2 to get the jitter in half res. */
482 subpixel_offset *= 0.5;
483
484 /* Same offsets as in dof_spatial_filtering(). */
485 const std::array<int2, 4> plus_offsets = {int2(-1, 0), int2(0, -1), int2(1, 0), int2(0, 1)};
486
487 const float radius = 1.5f;
488 int i = 0;
489 for (int2 offset : plus_offsets) {
490 float2 pixel_ofs = float2(offset) - subpixel_offset;
491 data_.filter_samples_weight[i++] = film_filter_weight(radius, math::length_squared(pixel_ofs));
492 }
493 data_.filter_center_weight = film_filter_weight(radius, math::length_squared(subpixel_offset));
494}
495
497 gpu::Texture **input_tx,
498 gpu::Texture **output_tx,
499 DepthOfFieldBuffer &dof_buffer)
500{
501 if (fx_radius_ == 0.0f) {
502 return;
503 }
504
505 input_color_tx_ = *input_tx;
506 output_color_tx_ = *output_tx;
507 extent_ = {GPU_texture_width(input_color_tx_), GPU_texture_height(input_color_tx_)};
508
509 {
510 const CameraData &cam_data = inst_.camera.data_get();
511 data_.camera_type = cam_data.type;
512 /* OPTI(fclem) Could be optimized. */
513 float3 jitter = float3(fx_radius_, 0.0f, -focus_distance_);
514 float3 center = float3(0.0f, 0.0f, -focus_distance_);
515 mul_project_m4_v3(cam_data.winmat.ptr(), jitter);
516 mul_project_m4_v3(cam_data.winmat.ptr(), center);
517 /* Simplify CoC calculation to a simple MADD. */
518 if (inst_.camera.is_orthographic()) {
519 data_.coc_mul = (center[0] - jitter[0]) * 0.5f * extent_[0];
520 data_.coc_bias = focus_distance_ * data_.coc_mul;
521 }
522 else {
523 data_.coc_bias = -(center[0] - jitter[0]) * 0.5f * extent_[0];
524 data_.coc_mul = focus_distance_ * data_.coc_bias;
525 }
526
527 float min_fg_coc = coc_radius_from_camera_depth(data_, -cam_data.clip_near);
528 float max_bg_coc = coc_radius_from_camera_depth(data_, -cam_data.clip_far);
529 if (data_.camera_type != CAMERA_ORTHO) {
530 /* Background is at infinity so maximum CoC is the limit of coc_radius_from_camera_depth
531 * at -inf. We only do this for perspective camera since orthographic coc limit is inf. */
532 max_bg_coc = data_.coc_bias;
533 }
534 /* Clamp with user defined max. */
535 data_.coc_abs_max = min_ff(max_ff(fabsf(min_fg_coc), fabsf(max_bg_coc)), fx_max_coc_);
536 /* TODO(fclem): Make this dependent of the quality of the gather pass. */
537 data_.scatter_coc_threshold = 4.0f;
538
539 update_sample_table();
540
541 data_.push_update();
542 }
543
544 /* Acquire reduce texture mip chains, and views for each mip level. */
545 {
546 int2 max_render_res = inst_.film.render_extent_get();
547 int2 half_render_res = math::divide_ceil(max_render_res, int2(2));
548 int2 reduce_size = math::ceil_to_multiple(half_render_res, int2(DOF_REDUCE_GROUP_SIZE));
549
550 /* Now we know the maximum render resolution of every view, using depth of field, allocate
551 * reduced buffers. Color needs to be signed format here. See note in shader for explanation */
554 reduced_color_tx_.acquire_2d(
555 reduce_size, gpu::TextureFormat::SFLOAT_16_16_16_16, usage, DOF_MIP_COUNT);
556 reduced_coc_tx_.acquire_2d(reduce_size, gpu::TextureFormat::SFLOAT_16, usage, DOF_MIP_COUNT);
557
558 /* Set pointers to view textures as TextureFromPool is transient. */
559 reduced_color_tx_.ensure_mip_views();
560 reduced_coc_tx_.ensure_mip_views();
561 for (int i = 0; i < DOF_MIP_COUNT; ++i) {
562 reduced_color_mip_views_[i] = reduced_color_tx_.mip_view(i);
563 reduced_coc_mip_views_[i] = reduced_coc_tx_.mip_view(i);
564 }
565 }
566
567 int2 half_res = math::divide_ceil(extent_, int2(2));
568 int2 quarter_res = math::divide_ceil(extent_, int2(4));
569 int2 tile_res = math::divide_ceil(half_res, int2(DOF_TILES_SIZE));
570
571 dispatch_setup_size_ = int3(math::divide_ceil(half_res, int2(DOF_DEFAULT_GROUP_SIZE)), 1);
572 dispatch_stabilize_size_ = int3(math::divide_ceil(half_res, int2(DOF_STABILIZE_GROUP_SIZE)), 1);
573 dispatch_downsample_size_ = int3(math::divide_ceil(quarter_res, int2(DOF_DEFAULT_GROUP_SIZE)),
574 1);
575 dispatch_reduce_size_ = int3(math::divide_ceil(half_res, int2(DOF_REDUCE_GROUP_SIZE)), 1);
576 dispatch_tiles_flatten_size_ = int3(math::divide_ceil(half_res, int2(DOF_TILES_SIZE)), 1);
577 dispatch_tiles_dilate_size_ = int3(
578 math::divide_ceil(tile_res, int2(DOF_TILES_DILATE_GROUP_SIZE)), 1);
579 dispatch_gather_size_ = int3(math::divide_ceil(half_res, int2(DOF_GATHER_GROUP_SIZE)), 1);
580 dispatch_filter_size_ = int3(math::divide_ceil(half_res, int2(DOF_FILTER_GROUP_SIZE)), 1);
581 dispatch_resolve_size_ = int3(math::divide_ceil(extent_, int2(DOF_RESOLVE_GROUP_SIZE)), 1);
582
584 /* On Mesa, there is a sync bug which can make a portion of the main pass (usually one shader)
585 * leave blocks of un-initialized memory. Doing a flush seems to alleviate the issue. */
586 GPU_flush();
587 }
588
589 GPU_debug_group_begin("Depth of Field");
590
591 Manager &drw = *inst_.manager;
592
593 constexpr eGPUTextureUsage usage_readwrite = GPU_TEXTURE_USAGE_SHADER_READ |
595 constexpr eGPUTextureUsage usage_readwrite_attach = usage_readwrite |
597 {
598 GPU_debug_group_begin("Setup");
599 {
600 bokeh_gather_lut_tx_.acquire_2d(int2(DOF_BOKEH_LUT_SIZE), gpu::TextureFormat::SFLOAT_16_16);
601 bokeh_scatter_lut_tx_.acquire_2d(int2(DOF_BOKEH_LUT_SIZE), gpu::TextureFormat::SFLOAT_16);
602 bokeh_resolve_lut_tx_.acquire_2d(int2(DOF_MAX_SLIGHT_FOCUS_RADIUS * 2 + 1),
603 gpu::TextureFormat::SFLOAT_16);
604
605 if (use_bokeh_lut_) {
606 drw.submit(bokeh_lut_ps_, view);
607 }
608 }
609 {
610 setup_color_tx_.acquire_2d(
611 half_res, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_readwrite);
612 setup_coc_tx_.acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16);
613
614 drw.submit(setup_ps_, view);
615 }
616 {
617 stabilize_output_tx_.acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16_16_16_16);
618 stabilize_valid_history_ = !dof_buffer.stabilize_history_tx_.acquire_2d(
619 half_res, gpu::TextureFormat::SFLOAT_16_16_16_16);
620
621 if (stabilize_valid_history_ == false) {
622 /* Avoid uninitialized memory that can contain NaNs. */
623 dof_buffer.stabilize_history_tx_.clear(float4(0.0f));
624 }
625
626 stabilize_input_ = dof_buffer.stabilize_history_tx_;
627 /* Outputs to reduced_*_tx_ mip 0. */
628 drw.submit(stabilize_ps_, view);
629
630 /* Swap history and output buffers, and retain the "new" history buffer until
631 * next cycle so we can reuse it as input. */
632 TextureFromPool::swap(stabilize_output_tx_, dof_buffer.stabilize_history_tx_);
633 dof_buffer.stabilize_history_tx_.retain();
634 /* Used by stabilize pass. */
635 stabilize_output_tx_.release();
636 setup_color_tx_.release();
637 }
638 {
639 GPU_debug_group_begin("Tile Prepare");
640
641 /* WARNING: If format changes, make sure dof_tile_* GLSL constants are properly encoded. */
642 tiles_fg_tx_.previous().acquire_2d(
643 tile_res, gpu::TextureFormat::UFLOAT_11_11_10, usage_readwrite);
644 tiles_bg_tx_.previous().acquire_2d(
645 tile_res, gpu::TextureFormat::UFLOAT_11_11_10, usage_readwrite);
646 tiles_fg_tx_.current().acquire_2d(
647 tile_res, gpu::TextureFormat::UFLOAT_11_11_10, usage_readwrite);
648 tiles_bg_tx_.current().acquire_2d(
649 tile_res, gpu::TextureFormat::UFLOAT_11_11_10, usage_readwrite);
650
651 drw.submit(tiles_flatten_ps_, view);
652
653 /* Used by tile_flatten and stabilize_ps pass. */
654 setup_coc_tx_.release();
655
656 /* Error introduced by gather center jittering. */
657 const float error_multiplier = 1.0f + 1.0f / (DOF_GATHER_RING_COUNT + 0.5f);
658 int dilation_end_radius = ceilf((fx_max_coc_ * error_multiplier) / (DOF_TILES_SIZE * 2));
659
660 /* Run dilation twice. One for minmax and one for minabs. */
661 for (int pass = 0; pass < 2; pass++) {
662 /* This algorithm produce the exact dilation radius by dividing it in multiple passes. */
663 int dilation_radius = 0;
664 while (dilation_radius < dilation_end_radius) {
665 int remainder = dilation_end_radius - dilation_radius;
666 /* Do not step over any unvisited tile. */
667 int max_multiplier = dilation_radius + 1;
668
669 int ring_count = min_ii(DOF_DILATE_RING_COUNT, ceilf(remainder / float(max_multiplier)));
670 int multiplier = min_ii(max_multiplier, floorf(remainder / float(ring_count)));
671
672 dilation_radius += ring_count * multiplier;
673
674 tiles_dilate_ring_count_ = ring_count;
675 tiles_dilate_ring_width_mul_ = multiplier;
676
677 tiles_fg_tx_.swap();
678 tiles_bg_tx_.swap();
679
680 drw.submit((pass == 0) ? tiles_dilate_minmax_ps_ : tiles_dilate_minabs_ps_, view);
681 }
682 }
683
684 tiles_fg_tx_.previous().release();
685 tiles_bg_tx_.previous().release();
686
688 }
689
690 downsample_tx_.acquire_2d(
691 quarter_res, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_readwrite);
692
693 drw.submit(downsample_ps_, view);
694
695 scatter_fg_indirect_buf_.clear_to_zero();
696 scatter_bg_indirect_buf_.clear_to_zero();
697
698 drw.submit(reduce_ps_, view);
699
700 /* Used by reduce pass. */
701 downsample_tx_.release();
702
704 }
705
706 for (int is_background = 0; is_background < 2; is_background++) {
707 GPU_debug_group_begin(is_background ? "Background Convolution" : "Foreground Convolution");
708
709 SwapChain<TextureFromPool, 2> &color_tx = is_background ? color_bg_tx_ : color_fg_tx_;
710 SwapChain<TextureFromPool, 2> &weight_tx = is_background ? weight_bg_tx_ : weight_fg_tx_;
711 Framebuffer &scatter_fb = is_background ? scatter_bg_fb_ : scatter_fg_fb_;
712 PassSimple &gather_ps = is_background ? gather_bg_ps_ : gather_fg_ps_;
713 PassSimple &filter_ps = is_background ? filter_bg_ps_ : filter_fg_ps_;
714 PassSimple &scatter_ps = is_background ? scatter_bg_ps_ : scatter_fg_ps_;
715
716 color_tx.current().acquire_2d(
717 half_res, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_readwrite_attach);
718 weight_tx.current().acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16, usage_readwrite);
719 occlusion_tx_.acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16_16);
720
721 drw.submit(gather_ps, view);
722
723 {
724 /* Filtering pass. */
725 color_tx.swap();
726 weight_tx.swap();
727
728 color_tx.current().acquire_2d(
729 half_res, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_readwrite_attach);
730 weight_tx.current().acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16, usage_readwrite);
731
732 drw.submit(filter_ps, view);
733
734 color_tx.previous().release();
735 weight_tx.previous().release();
736 }
737
739
741
744 {
745 /* WORKAROUND(fclem): Mesa has some synchronization issues between the previous compute
746 * shader and the following graphic pass (see #141198). */
747 GPU_flush();
748 }
749
750 GPU_framebuffer_bind(scatter_fb);
751 drw.submit(scatter_ps, view);
752
753 /* Used by scatter pass. */
754 occlusion_tx_.release();
755
757 }
758 {
759 GPU_debug_group_begin("Hole Fill");
760
761 bokeh_gather_lut_tx_.release();
762 bokeh_scatter_lut_tx_.release();
763
764 hole_fill_color_tx_.acquire_2d(
765 half_res, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_readwrite);
766 hole_fill_weight_tx_.acquire_2d(half_res, gpu::TextureFormat::SFLOAT_16, usage_readwrite);
767
768 drw.submit(hole_fill_ps_, view);
769
770 /* NOTE: We do not filter the hole-fill pass as effect is likely to not be noticeable. */
771
772 reduced_color_tx_.release();
773 reduced_coc_tx_.release();
774
776 }
777 {
778 GPU_debug_group_begin("Resolve");
779
780 resolve_stable_color_tx_ = dof_buffer.stabilize_history_tx_;
781
782 drw.submit(resolve_ps_, view);
783
784 color_bg_tx_.current().release();
785 color_fg_tx_.current().release();
786 weight_bg_tx_.current().release();
787 weight_fg_tx_.current().release();
788 tiles_fg_tx_.current().release();
789 tiles_bg_tx_.current().release();
790 hole_fill_color_tx_.release();
791 hole_fill_weight_tx_.release();
792 bokeh_resolve_lut_tx_.release();
793
795 }
796
798
799 /* Swap buffers so that next effect has the right input. */
800 std::swap(*input_tx, *output_tx);
801}
802
804
805} // namespace blender::eevee
Camera data-block and utility functions.
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define UNPACK2(a)
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
void submit(PassSimple &pass, View &view)
static void swap(TextureFromPool &a, TextureFromPool &b)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
void clear(float4 values)
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void bind_image(const char *name, gpu::Texture *image)
void dispatch(int group_len)
void barrier(GPUBarrier type)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
void jitter_apply(float4x4 &winmat, float4x4 &viewmat)
void render(View &view, gpu::Texture **input_tx, gpu::Texture **output_tx, DepthOfFieldBuffer &dof_buffer)
gpu::Shader * static_shader_get(eShaderType shader_type)
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
detail::Pass< command::DrawCommandBuf > PassSimple
T length_squared(const VecBase< T, Size > &a)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, Size > ceil_to_multiple(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
MINLINE float min_ff(float a, float b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
void mul_project_m4_v3(const float mat[4][4], float vec[3])
@ GPU_PRIM_TRI_STRIP
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
@ GPU_DRIVER_OPENSOURCE
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
float BKE_camera_object_dof_distance(const struct Object *ob)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE void add_v2_v2(float r[2], const float a[2])
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
int GPU_texture_width(const gpu::Texture *texture)
@ GPU_BARRIER_FRAMEBUFFER
Definition GPU_state.hh:36
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
@ DRW_STATE_BLEND_ADD_FULL
Definition draw_state.hh:55
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_flush()
Definition gpu_state.cc:284
int GPU_texture_height(const gpu::Texture *texture)
MINLINE float max_ff(float a, float b)
MINLINE void sub_v2_v2(float r[2], const float a[2])
VecBase< float, 3 > float3
MINLINE int min_ii(int a, int b)
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
MINLINE float clamp_f(float value, float min, float max)
#define floorf
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const c_style_mat & ptr() const
eSceneEEVEE_Flag flag