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