Blender V5.3
eevee_shadow.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_math_matrix.hh"
12#include "GPU_batch_utils.hh"
13#include "GPU_capabilities.hh"
14#include "GPU_compute.hh"
15
16#include "GPU_context.hh"
17#include "eevee_instance.hh"
18
19#include "GPU_debug.hh"
20#include "draw_cache.hh"
21#include "draw_debug.hh"
22
23namespace blender::eevee {
24
25/* -------------------------------------------------------------------- */
29
31 int2 origin_offset,
32 int clipmap_level,
33 eShadowProjectionType projection_type_,
34 uint2 shadow_set_membership_)
35{
36 if ((projection_type != projection_type_) || (level != clipmap_level) ||
37 (shadow_set_membership_ != shadow_set_membership))
38 {
39 set_dirty();
40 }
41 projection_type = projection_type_;
42 level = clipmap_level;
44 shadow_set_membership = shadow_set_membership_;
45
46 grid_shift = origin_offset - grid_offset;
47 grid_offset = origin_offset;
48
49 if (!equals_m4m4(object_mat.ptr(), object_mat_.ptr())) {
50 object_mat = object_mat_;
51 set_dirty();
52 }
53
54 float tile_size = ShadowDirectional::tile_size_get(level);
55
56 /* object_mat is a rotation matrix. Reduce imprecision by taking the transpose which is also the
57 * inverse in this particular case. */
59
61 center_offset = float2(grid_offset) * tile_size;
62
67 /* Near/far is computed on GPU using casters bounds. */
68 -1.0f,
69 1.0f);
70}
71
73 const float4x4 &object_mat_,
74 float near_,
75 float far_,
76 eCubeFace face,
77 uint2 shadow_set_membership_)
78{
80 (shadow_set_membership_ != shadow_set_membership))
81 {
82 set_dirty();
83 }
85 cubeface = face;
86 grid_offset = int2(0);
87 light_type = light_type_;
88 shadow_set_membership = shadow_set_membership_;
89
90 if ((clip_near != near_) || (clip_far != far_)) {
91 set_dirty();
92 }
93
94 clip_near = near_;
95 half_size = near_;
96 clip_far = far_;
97 center_offset = float2(0.0f);
98
99 if (!equals_m4m4(object_mat.ptr(), object_mat_.ptr())) {
100 object_mat = object_mat_;
101 set_dirty();
102 }
103
107
108 /* Same thing as inversion but avoid precision issues. */
109 float4x4 viewinv = object_mat * float4x4(math::transpose(float3x3(shadow_face_mat[cubeface])));
110 /* Update corners. */
111 corners[0] = float4(viewinv.location(), 0.0f);
112 corners[1] = float4(math::transform_point(viewinv, float3(-far_, -far_, -far_)), 0.0f);
113 corners[2] = float4(math::transform_point(viewinv, float3(far_, -far_, -far_)), 0.0f);
114 corners[3] = float4(math::transform_point(viewinv, float3(-far_, far_, -far_)), 0.0f);
115 /* Store deltas. */
116 corners[2] = (corners[2] - corners[1]) / float(SHADOW_TILEMAP_RES);
117 corners[3] = (corners[3] - corners[1]) / float(SHADOW_TILEMAP_RES);
118}
119
121{
123 const float4 debug_color[6] = {
124 {1.0f, 0.1f, 0.1f, 1.0f},
125 {0.1f, 1.0f, 0.1f, 1.0f},
126 {0.0f, 0.2f, 1.0f, 1.0f},
127 {1.0f, 1.0f, 0.3f, 1.0f},
128 {0.1f, 0.1f, 0.1f, 1.0f},
129 {1.0f, 1.0f, 1.0f, 1.0f},
130 };
131 float4 color = debug_color
132 [((projection_type == SHADOW_PROJECTION_CUBEFACE ? int(cubeface) : level) + 9999) % 6];
133
134 float4x4 persinv = winmat * viewmat;
136}
137
139
140/* -------------------------------------------------------------------- */
144
146{
147 free_indices.reserve(SHADOW_MAX_TILEMAP);
148 /* Reverse order to help debugging (first allocated tile-map will get 0). */
149 for (int i = SHADOW_MAX_TILEMAP - 1; i >= 0; i--) {
150 free_indices.append(i * SHADOW_TILEDATA_PER_TILEMAP);
151 }
152
153 int2 extent;
154 extent.x = min_ii(SHADOW_MAX_TILEMAP, maps_per_row) * ShadowTileMap::tile_map_resolution;
155 extent.y = (SHADOW_MAX_TILEMAP / maps_per_row) * ShadowTileMap::tile_map_resolution;
156
159 tilemap_tx.ensure_2d(gpu::TextureFormat::UINT_32, extent, usage);
161}
162
164{
165 if (free_indices.is_empty()) {
166 /* Grow the tile-map buffer. See `end_sync`. */
167 for (auto i : IndexRange(free_indices.size(), SHADOW_MAX_TILEMAP)) {
168 free_indices.append(i * SHADOW_TILEDATA_PER_TILEMAP);
169 }
170 }
171 int index = free_indices.pop_last();
172 return &tilemap_pool.construct(ShadowTileMap(index));
173}
174
176{
177 for (ShadowTileMap *map : free_list) {
178 free_indices.append(map->tiles_index);
179 tilemap_pool.destruct(*map);
180 }
181}
182
184{
186
187 uint needed_tilemap_capacity = (free_indices.size() + tilemap_pool.size());
188 if (needed_tilemap_capacity != (tiles_data.size() / SHADOW_TILEDATA_PER_TILEMAP)) {
189 tiles_data.resize(needed_tilemap_capacity * SHADOW_TILEDATA_PER_TILEMAP);
190 tilemaps_clip.resize(needed_tilemap_capacity);
191 /* We reallocated the tile-map buffer, discarding all the data it contained.
192 * We need to re-initialize the page heaps. */
193 module.do_full_update_ = true;
194 }
195
197 int64_t newly_unused_count = free_indices.size() - last_free_len;
198 if (newly_unused_count > 0) {
199 /* Upload tile-map indices which pages needs to be pushed back to the free page heap. */
200 Span<uint> newly_unused_indices = free_indices.as_span().slice(last_free_len,
201 newly_unused_count);
202 for (uint index : newly_unused_indices) {
203 /* Push a dummy tile-map to a unused tile-map buffer. It is then processed through the some
204 * of the setup steps to release the pages. */
205 ShadowTileMapData tilemap_data = {};
206 tilemap_data.tiles_index = index;
207 tilemap_data.clip_data_index = -1;
208 tilemap_data.grid_shift = int2(SHADOW_TILEMAP_RES);
210
211 tilemaps_unused.append(tilemap_data);
212 }
214 }
215
217}
218
220
221/* -------------------------------------------------------------------- */
225
227{
228 int tilemaps_needed = light.local_tilemap_count();
229 if (tilemaps_.size() <= tilemaps_needed) {
230 return;
231 }
232 auto span = tilemaps_.as_span();
233 shadows_.tilemap_pool.release(span.drop_front(tilemaps_needed));
234 tilemaps_ = span.take_front(tilemaps_needed);
235}
236
238{
239 ShadowTileMapPool &tilemap_pool = shadows_.tilemap_pool;
240
241 float4x4 object_to_world = light.object_to_world;
242
243 /* Acquire missing tile-maps. */
244 int tilemaps_needed = light.local_tilemap_count();
245 while (tilemaps_.size() < tilemaps_needed) {
246 tilemaps_.append(tilemap_pool.acquire());
247 }
248
249 float near = int_as_float(light.clip_near);
250 float far = int_as_float(light.clip_far);
251 for (int i : tilemaps_.index_range()) {
252 eCubeFace face = eCubeFace(Z_NEG + i);
253 tilemaps_[face]->sync_cubeface(
254 light.type, object_to_world, near, far, face, light.shadow_set_membership);
255 }
256
257 light.local.tilemaps_count = tilemaps_needed;
258 light.tilemap_index = tilemap_pool.tilemaps_data.size();
259 for (ShadowTileMap *tilemap : tilemaps_) {
260 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
261 tilemap_pool.tilemaps_data.append(*tilemap);
262 tilemap->set_updated();
263 }
264}
265
267
268/* -------------------------------------------------------------------- */
284
285eShadowProjectionType ShadowDirectional::directional_distribution_type_get(const Camera &camera)
286{
287 /* TODO(fclem): Enable the cascade projection if the FOV is tiny in perspective mode. */
288 return (camera.is_perspective() || camera.is_panoramic()) ? SHADOW_PROJECTION_CLIPMAP :
290}
291
292/************************************************************************
293 * Cascade Distribution *
294 ************************************************************************/
295
296void ShadowDirectional::cascade_tilemaps_distribution_near_far_points(const Camera &camera,
297 const Light &light,
298 float3 &near_point,
299 float3 &far_point)
300{
301 const CameraData &cam_data = camera.data_get();
302 /* Ideally we should only take the intersection with the scene bounds. */
304 light.object_to_world, camera.position() - camera.forward() * cam_data.clip_far);
306 light.object_to_world, camera.position() - camera.forward() * cam_data.clip_near);
307}
308
309IndexRange ShadowDirectional::cascade_level_range(const Light &light, const Camera &camera)
310{
311 /* NOTE: All tile-maps are meant to have the same LOD
312 * but we still return a range starting at the unique LOD. */
313
314 using namespace blender::math;
315
316 /* 16 is arbitrary. To avoid too much tile-map per directional lights. */
317 const int max_tilemap_per_shadows = 16;
318 const CameraData &cam_data = camera.data_get();
319
320 float3 near_point, far_point;
321 cascade_tilemaps_distribution_near_far_points(camera, light, near_point, far_point);
322
323 /* This gives the maximum resolution in depth we can have with a fixed set of tile-maps. Gives
324 * the best results when view direction is orthogonal to the light direction. */
325 float depth_range_in_shadow_space = distance(far_point.xy(), near_point.xy());
326 float min_depth_tilemap_size = 2 * (depth_range_in_shadow_space / max_tilemap_per_shadows);
327 /* This allow coverage of the whole view with a single tile-map if camera forward is colinear
328 * with the light direction. */
329 float min_diagonal_tilemap_size = cam_data.screen_diagonal_length;
330
331 if (camera.is_perspective()) {
332 /* Use the far plane diagonal if using perspective. */
333 min_diagonal_tilemap_size *= cam_data.clip_far / cam_data.clip_near;
334 }
335
336 /* TODO(fclem): Zoomed in camera can have very small diagonal size which will then result in
337 * negative lod_level. Since negative ranges are not supported inside `IndexRange` we have to
338 * ensure this doesn't happen. */
339 min_diagonal_tilemap_size = max(min_diagonal_tilemap_size, 0.5f);
340
341 /* Level of detail (or size) of every tile-maps of this light. */
342 /* TODO(fclem): Add support for lod bias from light. */
343 int lod_level = ceil(log2(max_ff(min_depth_tilemap_size, min_diagonal_tilemap_size)) + 0.5);
344
345 /* Tile-maps "rotate" around the first one so their effective range is only half their size. */
346 float per_tilemap_coverage = ShadowDirectional::coverage_get(lod_level) * 0.5f;
347 /* Number of tile-maps needed to cover the whole view. */
348 /* NOTE: floor + 0.5 to avoid 0 when parallel. */
349 int tilemap_len = ceil(0.5f + depth_range_in_shadow_space / per_tilemap_coverage);
350 return IndexRange(lod_level, tilemap_len);
351}
352
353void ShadowDirectional::cascade_tilemaps_distribution(Light &light, const Camera &camera)
354{
355 using namespace blender::math;
356
357 float4x4 object_mat = light.object_to_world;
358 object_mat.location() = float3(0.0f);
359
360 /* All tile-maps use the first level size. */
361 float half_size = ShadowDirectional::coverage_get(levels_range.first()) / 2.0f;
362 float tile_size = ShadowDirectional::tile_size_get(levels_range.first());
363
364 float3 near_point, far_point;
365 cascade_tilemaps_distribution_near_far_points(camera, light, near_point, far_point);
366
367 float2 local_view_direction = normalize(far_point.xy() - near_point.xy());
368 float2 farthest_tilemap_center = local_view_direction * half_size * (levels_range.size() - 1);
369
370 /* Offset for smooth level transitions. */
371 light.object_to_world.x.w = near_point.x;
372 light.object_to_world.y.w = near_point.y;
373 light.object_to_world.z.w = near_point.z;
374
375 /* Offset in tiles from the scene origin to the center of the first tile-maps. */
376 int2 origin_offset = int2(round(float2(near_point) / tile_size));
377 /* Offset in tiles between the first and the last tile-maps. */
378 int2 offset_vector = int2(round(farthest_tilemap_center / tile_size));
379
380 light.sun.clipmap_base_offset_neg = int2(0); /* Unused. */
381 light.sun.clipmap_base_offset_pos = (offset_vector * (1 << 16)) /
382 max_ii(levels_range.size() - 1, 1);
383
384 /* \note cascade_level_range starts the range at the unique LOD to apply to all tile-maps. */
385 int level = levels_range.first();
386 for (int i : IndexRange(levels_range.size())) {
387 ShadowTileMap *tilemap = tilemaps_[i];
388
389 /* Equal spacing between cascades layers since we want uniform shadow density. */
390 int2 level_offset = origin_offset +
391 shadow_cascade_grid_offset(light.sun.clipmap_base_offset_pos, i);
392 tilemap->sync_orthographic(
393 object_mat, level_offset, level, SHADOW_PROJECTION_CASCADE, light.shadow_set_membership);
394
395 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
396 shadows_.tilemap_pool.tilemaps_data.append(*tilemap);
397 tilemap->set_updated();
398 }
399
400 light.sun.clipmap_origin = float2(origin_offset) * tile_size;
401
402 light.type = LIGHT_SUN_ORTHO;
403
404 /* Not really clip-maps, but this is in order to make #light_tilemap_max_get() work and determine
405 * the scaling. */
406 light.sun.clipmap_lod_min = levels_range.first();
407 light.sun.clipmap_lod_max = levels_range.last();
408}
409
410/************************************************************************
411 * Clip-map Distribution *
412 ************************************************************************/
413
414IndexRange ShadowDirectional::clipmap_level_range(const Camera &cam)
415{
416 using namespace blender::math;
417 /* Covers the closest points of the view. */
418 /* FIXME: IndexRange does not support negative indices. Clamp to 0 for now. */
419 int min_level = max(0.0f, floor(log2(abs(cam.data_get().clip_near))));
420 /* Covers the farthest points of the view. */
421 int max_level = ceil(log2(cam.bound_radius() + distance(cam.bound_center(), cam.position())));
422 /* We actually need to cover a bit more because of clipmap origin snapping. */
423 max_level = max(min_level, max_level) + 1;
424 IndexRange range(min_level, max_level - min_level + 1);
425 /* 32 to be able to pack offset into a single int2.
426 * The maximum level count is bounded by the mantissa of a 32bit float. */
427 const int max_tilemap_per_shadows = 24;
428 /* Take top-most level to still cover the whole view. */
429 range = range.take_back(max_tilemap_per_shadows);
430
431 return range;
432}
433
434void ShadowDirectional::clipmap_tilemaps_distribution(Light &light, const Camera &camera)
435{
436 float4x4 object_mat = light.object_to_world;
437 object_mat.location() = float3(0.0f);
438
439 for (int lod : IndexRange(levels_range.size())) {
440 ShadowTileMap *tilemap = tilemaps_[lod];
441
442 int level = levels_range.first() + lod;
443 /* Compute full offset from world origin to the smallest clipmap tile centered around the
444 * camera position. The offset is computed in smallest tile unit. */
445 float tile_size = ShadowDirectional::tile_size_get(level);
446 /* Moving to light space by multiplying by the transpose (which is the inverse). */
447 float2 light_space_camera_position = camera.position() * float2x3(object_mat.view<2, 3>());
448 int2 level_offset = int2(math::round(light_space_camera_position / tile_size));
449
450 tilemap->sync_orthographic(
451 object_mat, level_offset, level, SHADOW_PROJECTION_CLIPMAP, light.shadow_set_membership);
452
453 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
454 shadows_.tilemap_pool.tilemaps_data.append(*tilemap);
455 tilemap->set_updated();
456 }
457
458 int2 pos_offset = int2(0);
459 int2 neg_offset = int2(0);
460 for (int lod : IndexRange(levels_range.size() - 1)) {
461 /* Since offset can only differ by one tile from the higher level, we can compress that as a
462 * single integer where one bit contains offset between 2 levels. Then a single bit shift in
463 * the shader gives the number of tile to offset in the given tile-map space. However we need
464 * also the sign of the offset for each level offset. To this end, we split the negative
465 * offsets to a separate int. */
466 int2 lvl_offset_next = tilemaps_[lod + 1]->grid_offset;
467 int2 lvl_offset = tilemaps_[lod]->grid_offset;
468 int2 lvl_delta = lvl_offset - (lvl_offset_next * 2);
469 BLI_assert(math::abs(lvl_delta.x) <= 1 && math::abs(lvl_delta.y) <= 1);
470 pos_offset |= math::max(lvl_delta, int2(0)) << lod;
471 neg_offset |= math::max(-lvl_delta, int2(0)) << lod;
472 }
473
474 /* Number of levels is limited to 32 by `clipmap_level_range()` for this reason. */
475 light.sun.clipmap_base_offset_pos = pos_offset;
476 light.sun.clipmap_base_offset_neg = neg_offset;
477
478 float tile_size_max = ShadowDirectional::tile_size_get(levels_range.last());
479 int2 level_offset_max = tilemaps_[levels_range.size() - 1]->grid_offset;
480
481 light.type = LIGHT_SUN;
482
483 /* Used for selecting the clipmap level. */
484 float3 location = transform_direction_transposed(light.object_to_world, camera.position());
485 /* Offset for smooth level transitions. */
486 light.object_to_world.x.w = location.x;
487 light.object_to_world.y.w = location.y;
488 light.object_to_world.z.w = location.z;
489 /* Used as origin for the clipmap_base_offset trick. */
490 light.sun.clipmap_origin = float2(level_offset_max * tile_size_max);
491
492 light.sun.clipmap_lod_min = levels_range.first();
493 light.sun.clipmap_lod_max = levels_range.last();
494}
495
497{
498 IndexRange levels_new = directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE ?
499 cascade_level_range(light, camera) :
500 clipmap_level_range(camera);
501
502 if (levels_range == levels_new) {
503 return;
504 }
505
506 IndexRange isect_range = levels_range.intersect(levels_new);
507 IndexRange before_range(levels_range.start(), isect_range.start() - levels_range.start());
508 IndexRange after_range(isect_range.one_after_last(),
509 levels_range.one_after_last() - isect_range.one_after_last());
510
511 auto span = tilemaps_.as_span();
512 shadows_.tilemap_pool.release(span.slice(before_range.shift(-levels_range.start())));
513 shadows_.tilemap_pool.release(span.slice(after_range.shift(-levels_range.start())));
514 tilemaps_ = span.slice(isect_range.shift(-levels_range.start()));
515 levels_range = isect_range;
516}
517
519{
520 ShadowTileMapPool &tilemap_pool = shadows_.tilemap_pool;
521 IndexRange levels_new = directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE ?
522 cascade_level_range(light, camera) :
523 clipmap_level_range(camera);
524
525 if (levels_range != levels_new) {
526 /* Acquire missing tile-maps. */
527 IndexRange isect_range = levels_new.intersect(levels_range);
528 int64_t before_range = isect_range.start() - levels_new.start();
529 int64_t after_range = levels_new.one_after_last() - isect_range.one_after_last();
530
531 Vector<ShadowTileMap *> cached_tilemaps = tilemaps_;
532 tilemaps_.clear();
533 for (int64_t i = 0; i < before_range; i++) {
534 tilemaps_.append(tilemap_pool.acquire());
535 }
536 /* Keep cached LOD's. */
537 tilemaps_.extend(cached_tilemaps);
538 for (int64_t i = 0; i < after_range; i++) {
539 tilemaps_.append(tilemap_pool.acquire());
540 }
541 levels_range = levels_new;
542 }
543
544 light.tilemap_index = tilemap_pool.tilemaps_data.size();
545 light.clip_near = 0x7F7FFFFF; /* floatBitsToOrderedInt(FLT_MAX) */
546 light.clip_far = int(0xFF7FFFFFu ^ 0x7FFFFFFFu); /* floatBitsToOrderedInt(-FLT_MAX) */
547
548 if (directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE) {
549 cascade_tilemaps_distribution(light, camera);
550 }
551 else {
552 clipmap_tilemaps_distribution(light, camera);
553 }
554}
555
557
558/* -------------------------------------------------------------------- */
562
564{
565 for (int i = 0; i < statistics_buf_.size(); i++) {
566 UNUSED_VARS(i);
567 statistics_buf_.current().clear_to_zero();
568 statistics_buf_.swap();
569 }
570}
571
573{
574 blender::Scene &scene = *inst_.scene;
575
576 global_lod_bias_ = (1.0f - scene.eevee.shadow_resolution_scale) * SHADOW_TILEMAP_LOD;
577
578 do_full_update_ |= assign_if_different(
579 data_.use_debug_cost, bool32_t(inst_.debug_mode == eDebugMode::DEBUG_SHADOW_ATOMIC_COST));
580
581 bool update_lights = false;
582 bool enable_shadow = (scene.eevee.flag & SCE_EEVEE_SHADOW_ENABLED) != 0;
583 bool use_jitter = enable_shadow &&
584 (inst_.is_image_render ||
585 (!inst_.is_navigating && !inst_.is_transforming && !inst_.is_playback &&
587 update_lights |= assign_if_different(enabled_, enable_shadow);
588 update_lights |= assign_if_different(data_.use_jitter, bool32_t(use_jitter));
589 if (update_lights) {
590 /* Force light reset. */
591 for (Light &light : inst_.lights.light_map_.values()) {
592 light.initialized = false;
593 }
594 }
595
596 if (enabled_) {
597 data_.ray_count = clamp_i(scene.eevee.shadow_ray_count, 1, SHADOW_MAX_RAY);
598 data_.step_count = clamp_i(scene.eevee.shadow_step_count, 1, SHADOW_MAX_STEP);
599 }
600 else {
601 data_.ray_count = 1;
602 data_.step_count = 1;
603 }
604
605 /* Pool size is in MBytes. */
606 const size_t pool_byte_size = enabled_ ? size_t(scene.eevee.shadow_pool_size) * square_i(1024) :
607 1;
608 const size_t page_byte_size = square_i(shadow_page_size_) * sizeof(int);
609 shadow_page_len_ = int(divide_ceil_ul(pool_byte_size, page_byte_size));
610 shadow_page_len_ = min_ii(shadow_page_len_, SHADOW_MAX_PAGE);
611
612 const int2 atlas_extent = shadow_page_size_ * int2(SHADOW_PAGE_PER_ROW, SHADOW_PAGE_PER_COL);
613 const int atlas_layers = divide_ceil_u(shadow_page_len_, SHADOW_PAGE_PER_LAYER);
614
617 if (atlas_tx_.ensure_2d_array(atlas_type, atlas_extent, atlas_layers, tex_usage)) {
618 /* Global update. */
619 do_full_update_ = true;
620 }
621
622 /* Make allocation safe. Avoids crash later on. */
623 if (!atlas_tx_.is_valid()) {
624 atlas_tx_.ensure_2d_array(ShadowModule::atlas_type, int2(1), 1);
625 inst_.info_append_i18n(
626 "Error: Could not allocate shadow atlas. Most likely out of GPU memory.");
627 }
628
629 /* Read end of the swap-chain to avoid stall. */
630 if (inst_.is_viewport()) {
631 if (inst_.sampling.finished_viewport()) {
632 /* Swap enough to read the last one. */
633 for (int i = 0; i < statistics_buf_.size(); i++) {
634 statistics_buf_.swap();
635 }
636 }
637 else {
638 statistics_buf_.swap();
639 }
640 statistics_buf_.current().read();
641 ShadowStatistics stats = statistics_buf_.current();
642
643 if (stats.page_used_count > shadow_page_len_ && enabled_) {
644 inst_.info_append_i18n(
645 "Error: Shadow buffer full, may result in missing shadows and lower "
646 "performance. ({} / {})",
647 stats.page_used_count,
648 shadow_page_len_);
649 }
650 if (stats.view_needed_count > SHADOW_VIEW_MAX && enabled_) {
651 inst_.info_append_i18n("Error: Too many shadow updates, some shadows might be incorrect.");
652 }
653 }
654
655 atlas_tx_.filter_mode(false);
656
657 /* Create different viewport to support different update region size. The most fitting viewport
658 * is then selected during the tilemap finalize stage in `viewport_select`. */
659 for (int i = 0; i < multi_viewports_.size(); i++) {
660 /* IMPORTANT: Reflect changes in TBDR tile vertex shader which assumes viewport index 15
661 * covers the whole framebuffer. */
662 int size_in_tile = min_ii(1 << i, SHADOW_TILEMAP_RES);
663 multi_viewports_[i][0] = 0;
664 multi_viewports_[i][1] = 0;
665 multi_viewports_[i][2] = size_in_tile * shadow_page_size_;
666 multi_viewports_[i][3] = size_in_tile * shadow_page_size_;
667 }
668}
669
671{
672 past_casters_updated_.clear();
673 curr_casters_updated_.clear();
674 curr_casters_.clear();
675 jittered_transparent_casters_.clear();
676 update_casters_ = true;
677
678 if (box_batch_ == nullptr) {
679 box_batch_ = GPU_batch_unit_cube();
680 }
681
682 {
683 Manager &manager = *inst_.manager;
684
685 PassMain &pass = tilemap_usage_ps_;
686 pass.init();
687
688 if (inst_.is_baking()) {
689 SurfelBuf &surfels_buf = inst_.volume_probes.bake.surfels_buf_;
690 CaptureInfoBuf &capture_info_buf = inst_.volume_probes.bake.capture_info_buf_;
691 float surfel_coverage_area = inst_.volume_probes.bake.surfel_density_;
692
693 /* Directional shadows. */
694 float texel_size = ShadowDirectional::tile_size_get(0) / float(SHADOW_PAGE_RES);
695 int directional_level = std::max(0, int(std::ceil(log2(surfel_coverage_area / texel_size))));
696
697 PassMain::Sub &sub = pass.sub("Surfels");
698 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_SURFELS));
699 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
700 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
701 sub.bind_ssbo("surfel_buf", &surfels_buf);
702 sub.bind_ssbo("capture_info_buf", &capture_info_buf);
703 sub.push_constant("directional_level", directional_level);
704 sub.bind_resources(inst_.uniform_data);
705 sub.bind_resources(inst_.lights);
706 sub.dispatch(&inst_.volume_probes.bake.dispatch_per_surfel_);
707
708 /* Skip opaque and transparent tagging for light baking. */
709 return;
710 }
711
712 {
713 /* Use depth buffer to tag needed shadow pages for opaque geometry. */
714 PassMain::Sub &sub = pass.sub("Opaque");
715 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_OPAQUE));
716 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
717 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
718 sub.bind_texture("depth_tx", &src_depth_tx_);
719 sub.push_constant("input_depth_extent", &input_depth_extent_);
720 sub.bind_resources(inst_.lights);
721 sub.bind_resources(inst_.uniform_data);
722 sub.bind_resources(inst_.hiz_buffer.front);
723 sub.dispatch(&dispatch_depth_scan_size_);
724 }
725 {
726 /* Use bounding boxes for transparent geometry. */
727 PassMain::Sub &sub = pass.sub("Transparent");
728 /* WORKAROUND: The DRW_STATE_WRITE_STENCIL is here only to avoid enabling the rasterizer
729 * discard inside draw manager. */
731 sub.state_stencil(0, 0, 0);
732 sub.framebuffer_set(&usage_tag_fb);
733 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_TRANSPARENT));
734 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
735 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
736 sub.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
737 sub.push_constant("fb_resolution", &usage_tag_fb_resolution_);
738 sub.push_constant("fb_lod", &usage_tag_fb_lod_);
739 sub.bind_resources(inst_.uniform_data);
740 sub.bind_resources(inst_.hiz_buffer.front);
741 sub.bind_resources(inst_.lights);
742
743 tilemap_usage_transparent_ps_ = &sub;
744 }
745 }
746}
747
749 bool is_alpha_blend,
750 bool has_transparent_shadows,
751 bool has_time_dependent_shadows,
752 bool has_offset_shadows)
753{
754 if ((is_alpha_blend && !inst_.is_baking()) || has_offset_shadows) {
755 tilemap_usage_transparent_ps_->draw(box_batch_, ob_handle.res_handle);
756 }
757
758 bool is_shadow_caster = !(ob_handle.object->visibility_flag & OB_HIDE_SHADOW);
759 if (!is_shadow_caster) {
760 return;
761 }
762
763 const bool shape_changed = has_time_dependent_shadows && inst_.materials.material_time_changed;
764
765 for (int i : IndexRange(ob_handle.instances_count())) {
766 ShadowObject &shadow_ob = objects_.lookup_or_add_default(ObjectKey(ob_handle, i));
767 shadow_ob.used = true;
768 const bool is_initialized = shadow_ob.resource_handle.is_valid();
769 const bool has_jittered_transparency = has_transparent_shadows && data_.use_jitter;
770 ResourceHandle instance_handle = ob_handle.res_handle.sub_handle(i);
771 if (is_shadow_caster &&
772 (ob_handle.recalc || !is_initialized || has_jittered_transparency || shape_changed))
773 {
774 if (ob_handle.recalc && is_initialized) {
775 past_casters_updated_.append(shadow_ob.resource_handle.raw());
776 }
777
778 if (has_jittered_transparency) {
779 jittered_transparent_casters_.append(instance_handle.raw());
780 }
781 else {
782 curr_casters_updated_.append(instance_handle.raw());
783 }
784 }
785 shadow_ob.resource_handle = instance_handle;
786
787 if (is_shadow_caster) {
788 curr_casters_.append(instance_handle.raw());
789 }
790 }
791}
792
794{
795 /* Delete unused shadows first to release tile-maps that could be reused for new lights. */
796 for (Light &light : inst_.lights.light_map_.values()) {
797 /* Do not discard lights in baking mode. See WORKAROUND in `surfels_create`. */
798 if ((!light.used || !enabled_) && !inst_.is_baking()) {
799 light.shadow_discard_safe(*this);
800 }
801 else if (light.directional != nullptr) {
802 light.directional->release_excess_tilemaps(light, inst_.camera);
803 }
804 else if (light.punctual != nullptr) {
805 light.punctual->release_excess_tilemaps(light);
806 }
807 }
808
809 /* Allocate new tile-maps and fill shadow data of the lights. */
811 for (Light &light : inst_.lights.light_map_.values()) {
812 if (enabled_ == false) {
813 light.tilemap_index = LIGHT_NO_SHADOW;
814 }
815 else if (light.directional != nullptr) {
816 light.directional->end_sync(light, inst_.camera);
817 }
818 else if (light.punctual != nullptr) {
819 light.punctual->end_sync(light);
820 }
821 else {
822 light.tilemap_index = LIGHT_NO_SHADOW;
823 }
824 }
825 tilemap_pool.end_sync(*this);
826
827 /* Search for deleted or updated shadow casters */
828 auto it_end = objects_.items().end();
829 for (auto it = objects_.items().begin(); it != it_end; ++it) {
830 ShadowObject &shadow_ob = (*it).value;
831 /* Do not discard casters in baking mode. See WORKAROUND in `surfels_create`. */
832 if (!shadow_ob.used && !inst_.is_baking()) {
833 /* May not be a caster, but it does not matter, be conservative. */
834 past_casters_updated_.append(shadow_ob.resource_handle.raw());
835 objects_.remove(it);
836 }
837 else {
838 /* Clear for next sync. */
839 shadow_ob.used = false;
840 }
841 }
842 past_casters_updated_.push_update();
843 curr_casters_updated_.push_update();
844 jittered_transparent_casters_.push_update();
845
846 curr_casters_.push_update();
847
848 if (do_full_update_) {
849 do_full_update_ = false;
850 /* Put all pages in the free heap. */
851 for (uint i : IndexRange(shadow_page_len_)) {
852 uint3 page = {i % SHADOW_PAGE_PER_ROW,
853 (i / SHADOW_PAGE_PER_ROW) % SHADOW_PAGE_PER_COL,
854 i / SHADOW_PAGE_PER_LAYER};
855 pages_free_data_[i] = shadow_page_pack(page);
856 }
857 for (uint i : IndexRange(shadow_page_len_, SHADOW_MAX_PAGE - shadow_page_len_)) {
858 pages_free_data_[i] = 0xFFFFFFFFu;
859 }
860 pages_free_data_.push_update();
861
862 /* Clear tiles to not reference any page. */
865
866 /* Clear cached page buffer. */
867 GPU_storagebuf_clear(pages_cached_data_, -1);
868
869 /* Reset info to match new state. */
870 pages_infos_data_.page_free_count = shadow_page_len_;
871 pages_infos_data_.page_alloc_count = 0;
872 pages_infos_data_.page_cached_next = 0u;
873 pages_infos_data_.page_cached_start = 0u;
874 pages_infos_data_.page_cached_end = 0u;
875 pages_infos_data_.push_update();
876 }
877
878 {
879 Manager &manager = *inst_.manager;
880
881 {
882 PassSimple &pass = tilemap_setup_ps_;
883 pass.init();
884
885 {
886 /* Clear tile-map clip buffer. */
887 PassSimple::Sub &sub = pass.sub("ClearClipmap");
888 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_CLIPMAP_CLEAR));
889 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
890 sub.push_constant("tilemaps_clip_buf_len", int(tilemap_pool.tilemaps_clip.size()));
891 sub.dispatch(int3(
892 divide_ceil_u(tilemap_pool.tilemaps_clip.size(), SHADOW_CLIPMAP_GROUP_SIZE), 1, 1));
894 }
895
896 {
897 /* Compute near/far clip distances for directional shadows based on casters bounds. */
898 PassSimple::Sub &sub = pass.sub("DirectionalBounds");
899 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_BOUNDS));
900 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
901 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
902 sub.bind_ssbo("casters_id_buf", curr_casters_);
903 sub.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
904 /* Bind again using a writable binding. */
905 sub.bind_ssbo("light_buf_write", &inst_.lights.culling_light_buf_);
906 sub.push_constant("resource_len", int(curr_casters_.size()));
907 sub.bind_resources(inst_.lights);
908 sub.dispatch(int3(
909 divide_ceil_u(std::max(curr_casters_.size(), int64_t(1)), SHADOW_BOUNDS_GROUP_SIZE),
910 1,
911 1));
913 }
914 {
915 /* Clear usage bits. Tag update from the tile-map for sun shadow clip-maps shifting. */
916 PassSimple::Sub &sub = pass.sub("Init");
917 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_INIT));
918 sub.push_constant("reset_used_flag", &run_tagging_);
919 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
920 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
921 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
922 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
924 /* Free unused tiles from tile-maps not used by any shadow. */
926 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_unused);
928 }
930 }
931 }
932
933 {
934 /* Mark for update all shadow pages touching an updated shadow caster. */
935 PassSimple &pass = caster_update_ps_;
936 pass.init();
937 pass.framebuffer_set(&update_tag_fb_);
939 pass.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_UPDATE));
940 pass.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
941 pass.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
942 pass.push_constant("tilemap_count", int(tilemap_pool.tilemaps_data.size()));
943 /* Past caster transforms. */
944 if (past_casters_updated_.size() > 0) {
945 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.previous());
946 pass.bind_ssbo("resource_ids_buf", past_casters_updated_);
947 pass.draw(box_batch_, past_casters_updated_.size() * tilemap_pool.tilemaps_data.size());
948 }
949 /* Current caster transforms. */
950 if (curr_casters_updated_.size() > 0) {
951 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
952 pass.bind_ssbo("resource_ids_buf", curr_casters_updated_);
953 pass.draw(box_batch_, curr_casters_updated_.size() * tilemap_pool.tilemaps_data.size());
954 }
956 }
957
958 {
959 /* Mark for update all shadow pages touching a jittered transparency shadow caster. */
960 PassSimple &pass = jittered_transparent_caster_update_ps_;
961 pass.init();
962 if (jittered_transparent_casters_.size() > 0) {
963 pass.framebuffer_set(&update_tag_fb_);
965 pass.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_UPDATE));
966 pass.push_constant("tilemap_count", int(tilemap_pool.tilemaps_data.size()));
967 pass.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
968 pass.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
969 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
970 pass.bind_ssbo("resource_ids_buf", jittered_transparent_casters_);
971 pass.draw(box_batch_,
972 jittered_transparent_casters_.size() * tilemap_pool.tilemaps_data.size());
974 }
975 }
976
977 {
978 /* Propagate the update tag to the lower LODs. */
979 PassSimple &pass = update_propagate_ps_;
980 pass.init();
981 if (past_casters_updated_.size() > 0 || curr_casters_updated_.size() > 0 ||
982 jittered_transparent_casters_.size() > 0)
983 {
984 pass.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_UPDATE_PROPAGATE));
985 pass.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
986 pass.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
989 }
990 }
991
992 /* Non volume usage tagging happens between these two steps.
993 * (Setup at begin_sync) */
994
995 if (inst_.volume.needs_shadow_tagging() && !inst_.is_baking()) {
996 PassMain::Sub &sub = tilemap_usage_ps_.sub("World Volume");
997 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_VOLUME));
998 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
999 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1000 sub.bind_resources(inst_.uniform_data);
1001 sub.bind_resources(inst_.hiz_buffer.front);
1002 sub.bind_resources(inst_.sampling);
1003 sub.bind_resources(inst_.lights);
1004 sub.bind_resources(inst_.volume.properties);
1005 sub.bind_resources(inst_.volume.result);
1007 sub.dispatch(math::divide_ceil(inst_.volume.grid_size(), int3(VOLUME_GROUP_SIZE)));
1008 }
1009
1010 {
1011 PassSimple &pass = tilemap_update_ps_;
1012 pass.init();
1013 {
1014 /* Mark tiles that are redundant in the mipmap chain as unused. */
1015 PassSimple::Sub &sub = pass.sub("MaskLod");
1016 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_MASK));
1017 sub.push_constant("max_view_per_tilemap", &max_view_per_tilemap_);
1018 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1019 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
1022 }
1023 {
1024 /* Free unused pages & Reclaim cached pages. */
1025 PassSimple::Sub &sub = pass.sub("Free");
1026 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_FREE));
1027 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1028 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
1029 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1030 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1031 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1033 /* Free unused tiles from tile-maps not used by any shadow. */
1034 if (tilemap_pool.tilemaps_unused.size() > 0) {
1035 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_unused);
1037 }
1039 }
1040 {
1041 /* De-fragment the free page heap after cache reuse phase which can leave hole. */
1042 PassSimple::Sub &sub = pass.sub("Defrag");
1043 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_DEFRAG));
1044 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1045 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1046 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1047 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1048 sub.bind_ssbo("clear_dispatch_buf", clear_dispatch_buf_);
1049 sub.bind_ssbo("tile_draw_buf", tile_draw_buf_);
1050 sub.dispatch(int3(1, 1, 1));
1052 }
1053 {
1054 /* Assign pages to tiles that have been marked as used but possess no page. */
1055 PassSimple::Sub &sub = pass.sub("AllocatePages");
1056 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_ALLOCATE));
1057 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1058 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
1059 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1060 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1061 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1062 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1065 }
1066 {
1067 /* Convert the unordered tiles into a texture used during shading. Creates views. */
1068 PassSimple::Sub &sub = pass.sub("Finalize");
1069 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_FINALIZE));
1070 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
1071 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1072 sub.bind_ssbo("pages_infos_buf", &pages_infos_data_);
1073 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1074 sub.bind_ssbo("view_infos_buf", &shadow_multi_view_.matrices_ubo_get());
1075 sub.bind_ssbo("render_view_buf", &render_view_buf_);
1076 sub.bind_ssbo("tilemaps_clip_buf", &tilemap_pool.tilemaps_clip);
1077 sub.bind_image("tilemaps_img", &tilemap_pool.tilemap_tx);
1078 sub.push_constant("use_multi_viewport", GPU_multi_viewport_support());
1082 }
1083 {
1084 /* Convert the unordered tiles into a texture used during shading. Creates views. */
1085 PassSimple::Sub &sub = pass.sub("RenderMap");
1086 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_RENDERMAP));
1087 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1088 sub.bind_ssbo("render_view_buf", &render_view_buf_);
1089 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1090 sub.bind_ssbo("clear_dispatch_buf", &clear_dispatch_buf_);
1091 sub.bind_ssbo("tile_draw_buf", &tile_draw_buf_);
1092 sub.bind_ssbo("dst_coord_buf", &dst_coord_buf_);
1093 sub.bind_ssbo("src_coord_buf", &src_coord_buf_);
1094 sub.bind_ssbo("render_map_buf", &render_map_buf_);
1095 sub.dispatch(int3(1, 1, SHADOW_VIEW_MAX));
1097 }
1098 {
1099 /* Amend tilemap_tx content to support clipmap LODs. */
1100 PassSimple::Sub &sub = pass.sub("Amend");
1101 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_AMEND));
1102 sub.bind_image("tilemaps_img", tilemap_pool.tilemap_tx);
1103 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1104 sub.bind_resources(inst_.lights);
1105 /* Bind again using a writable binding. */
1106 sub.bind_ssbo("light_buf_write", &inst_.lights.culling_light_buf_);
1107 sub.dispatch(int3(1));
1109 }
1110
1111 {
1113 PassSimple::Sub &sub = pass.sub("RenderClear");
1114 sub.framebuffer_set(&render_fb_);
1116 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_CLEAR));
1117 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1118 sub.bind_ssbo("dst_coord_buf", dst_coord_buf_);
1119 sub.bind_image("shadow_atlas_img", atlas_tx_);
1120 sub.dispatch(clear_dispatch_buf_);
1122 }
1123 }
1124 }
1125
1127}
1128
1130{
1131 if (!ELEM(inst_.debug_mode,
1137 {
1138 return;
1139 }
1140
1141 /* Init but not filled if no active object. */
1142 debug_draw_ps_.init();
1143
1144 int tilemap_index = -1;
1145 Object *object_active = inst_.draw_ctx->obact;
1146 if (object_active != nullptr) {
1147 ObjectKey object_key(ObjectRef(DEG_get_original(object_active)));
1148 if (inst_.lights.light_map_.contains(object_key)) {
1149 Light &light = inst_.lights.light_map_.lookup(object_key);
1150 if (light.tilemap_index < SHADOW_MAX_TILEMAP) {
1151 tilemap_index = light.tilemap_index;
1152 }
1153 }
1154 }
1155
1156 if (tilemap_index == -1 && inst_.debug_mode != eDebugMode::DEBUG_SHADOW_ATOMIC_COST) {
1157 return;
1158 }
1159
1160 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | inst_.film.depth.test_state |
1162
1163 debug_draw_ps_.state_set(state);
1164 debug_draw_ps_.shader_set(inst_.shaders.static_shader_get(SHADOW_DEBUG));
1165 debug_draw_ps_.push_constant("debug_mode", int(inst_.debug_mode));
1166 debug_draw_ps_.push_constant("debug_tilemap_index", tilemap_index);
1167 debug_draw_ps_.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
1168 debug_draw_ps_.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1169 debug_draw_ps_.bind_resources(inst_.uniform_data);
1170 debug_draw_ps_.bind_resources(inst_.hiz_buffer.front);
1171 debug_draw_ps_.bind_resources(inst_.lights);
1172 debug_draw_ps_.bind_resources(inst_.shadows);
1173 debug_draw_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
1174}
1175
1176bool ShadowModule::shadow_update_finished(int loop_count)
1177{
1178 if (loop_count >= (SHADOW_MAX_TILEMAP * SHADOW_TILEMAP_LOD) / SHADOW_VIEW_MAX) {
1179 /* We have reach the maximum theoretical number of updates.
1180 * This can indicate a problem in the statistic buffer read-back or update tagging. */
1181 inst_.info_append_i18n("Error: Reached max shadow updates.");
1182 return true;
1183 }
1184
1185 if (!inst_.is_image_render && !inst_.is_light_bake) {
1186 /* For viewport, only run the shadow update once per redraw.
1187 * This avoids the stall from the read-back and freezes from long shadow update. */
1188 return true;
1189 }
1190
1191 int max_updated_view_count = tilemap_pool.tilemaps_data.size() * SHADOW_TILEMAP_LOD;
1192 if (max_updated_view_count <= SHADOW_VIEW_MAX * loop_count) {
1193 /* There is enough shadow views to cover all tile-map updates.
1194 * No read-back needed as it is guaranteed that all of them will be updated. */
1195 return true;
1196 }
1197
1198 if (loop_count == 1) {
1199 /* Do not read-back for only 1 loop iter. It's cheaper to just resubmit. */
1200 return false;
1201 }
1202
1203 if (loop_count == 2) {
1204 /* Read back and check if there is still tile-map to update. */
1205 /* TODO: Only the first loop should call `async_flush_to_host()`, but we need to fix the VK and
1206 * Metal implementations first. */
1207 statistics_buf_.current().async_flush_to_host();
1208 statistics_buf_.current().read();
1209 ShadowStatistics stats = statistics_buf_.current();
1210
1211 if (stats.page_used_count > shadow_page_len_) {
1212 inst_.info_append_i18n(
1213 "Error: Shadow buffer full, may result in missing shadows and lower "
1214 "performance. ({} / {})",
1215 stats.page_used_count,
1216 shadow_page_len_);
1217 }
1218
1219 needed_views_ = stats.view_needed_count;
1220 }
1221
1222 /* Subtract the amount rendered during this iteration. */
1223 needed_views_ -= SHADOW_VIEW_MAX;
1224 /* We are finished if there is no view left to render. */
1225 return needed_views_ <= 0;
1226}
1227
1228int ShadowModule::max_view_per_tilemap()
1229{
1230 if (inst_.is_image_render) {
1231 /* No need to limit updates per lights as we ensure all lights levels will be rendered.
1232 * is_image_render. */
1233 return SHADOW_TILEMAP_LOD;
1234 }
1235 /* For now very simple heuristic. Can be improved later by taking into consideration how many
1236 * tile-maps are updating, but we cannot know the ones updated by casters. */
1237 int potential_view_count = 0;
1238 for (auto i : IndexRange(tilemap_pool.tilemaps_data.size())) {
1239 if (tilemap_pool.tilemaps_data[i].projection_type == SHADOW_PROJECTION_CUBEFACE) {
1240 potential_view_count += SHADOW_TILEMAP_LOD;
1241 }
1242 else {
1243 potential_view_count += 1;
1244 }
1245 }
1246 int max_view_count = divide_ceil_u(SHADOW_VIEW_MAX, math::max(potential_view_count, 1));
1247 /* For viewport interactivity, have a hard maximum. This allows smoother experience. */
1248 if (inst_.is_transforming || inst_.is_navigating) {
1249 max_view_count = math::min(2, max_view_count);
1250 }
1251 /* For animation playback, we always want the maximum performance. */
1252 if (inst_.is_playback) {
1253 max_view_count = math::min(1, max_view_count);
1254 }
1255
1256 return max_view_count;
1257}
1258
1259void ShadowModule::ShadowView::compute_visibility(ObjectBoundsBuf &bounds,
1260 ObjectInfosBuf &infos,
1261 uint resource_len,
1262 bool /*debug_freeze*/)
1263{
1264 GPU_debug_group_begin("View.compute_visibility");
1265
1266 uint word_per_draw = this->visibility_word_per_draw();
1267 /* Switch between tightly packed and set of whole word per instance. */
1268 uint words_len = (view_len_ == 1) ? divide_ceil_u(resource_len, 32) :
1269 resource_len * word_per_draw;
1270 words_len = ceil_to_multiple_u(max_ii(1, words_len), 4);
1271 /* TODO(fclem): Resize to nearest pow2 to reduce fragmentation. */
1272 visibility_buf_.resize(words_len);
1273
1274 const uint32_t data = 0xFFFFFFFFu;
1276
1277 if (do_visibility_) {
1278 gpu::Shader *shader = inst_.shaders.static_shader_get(SHADOW_VIEW_VISIBILITY);
1279 GPU_shader_bind(shader);
1280 GPU_shader_uniform_1i(shader, "resource_len", resource_len);
1281 GPU_storagebuf_bind(bounds, GPU_shader_get_ssbo_binding(shader, "bounds_buf"));
1283 GPU_storagebuf_bind(render_view_buf_, GPU_shader_get_ssbo_binding(shader, "render_view_buf"));
1287 GPU_compute_dispatch(shader, divide_ceil_u(resource_len, DRW_VISIBILITY_GROUP_SIZE), 1, 1);
1289 }
1290
1292}
1293
1295{
1296 data_.film_pixel_radius = view.screen_pixel_radius(extent);
1297}
1298
1299void ShadowModule::render(View &view, int2 extent)
1300{
1301 if (enabled_ == false) {
1302 /* All lights have been tagged to have no shadow. */
1303 return;
1304 }
1305
1306 input_depth_extent_ = extent;
1307
1309
1310 dispatch_depth_scan_size_ = int3(math::divide_ceil(extent, int2(SHADOW_DEPTH_SCAN_GROUP_SIZE)),
1311 1);
1312 max_view_per_tilemap_ = max_view_per_tilemap();
1313
1314 usage_tag_fb_resolution_ = math::divide_ceil(extent, int2(std::exp2(usage_tag_fb_lod_)));
1315 usage_tag_fb.ensure(usage_tag_fb_resolution_);
1316
1317 /* Create attachment-less framebuffer. */
1318 render_fb_.ensure(int2(SHADOW_TILEMAP_RES * shadow_page_size_));
1319
1320 update_tag_fb_.ensure(int2(SHADOW_TILEMAP_RES));
1321
1322 inst_.hiz_buffer.update();
1323
1324 needed_views_ = 0;
1325 int loop_count = 0;
1326 do {
1327 GPU_debug_group_begin("Shadow");
1328 {
1329 GPU_uniformbuf_clear_to_zero(shadow_multi_view_.matrices_ubo_get());
1330 GPU_storagebuf_clear(render_map_buf_, 0xFFFFFFFFu);
1331
1332 run_tagging_ = (loop_count == 0);
1333
1334 inst_.manager->submit(tilemap_setup_ps_, view);
1335 if (loop_count == 0) {
1336 if (assign_if_different(update_casters_, false)) {
1337 /* Run caster update only once. */
1338 /* TODO(fclem): There is an optimization opportunity here where we can
1339 * test casters only against the static tile-maps instead of all of them. */
1340 inst_.manager->submit(caster_update_ps_, view);
1341 }
1342 inst_.manager->submit(jittered_transparent_caster_update_ps_, view);
1343 inst_.manager->submit(update_propagate_ps_, view);
1344 inst_.manager->submit(tilemap_usage_ps_, view);
1345 }
1346 inst_.manager->submit(tilemap_update_ps_, view);
1347
1348 shadow_multi_view_.compute_procedural_bounds();
1349
1350 statistics_buf_.current().async_flush_to_host();
1351
1352 /* Flush every loop as these passes are very heavy. */
1353 bool use_flush = loop_count != 0;
1354
1355 if (use_flush) {
1356 GPU_flush();
1357 }
1358
1359 GPU_framebuffer_bind(render_fb_);
1360 const int4 &largest_viewport = multi_viewports_[SHADOW_TILEMAP_LOD];
1363 reinterpret_cast<int (*)[4]>(multi_viewports_.data()));
1364 }
1365 else {
1366 /* Fallback for GPU's that do not support multiViewport. Always render to the largest
1367 * viewport. This spawns a lot more fragment shaders, but at least we can draw the
1368 * correct shadows on these systems. See #163697. */
1370 largest_viewport.x,
1371 largest_viewport.y,
1372 largest_viewport.z,
1373 largest_viewport.w);
1374 }
1375
1376 inst_.pipelines.shadow.render(shadow_multi_view_);
1377
1378 if (use_flush) {
1379 GPU_flush();
1380 }
1381
1383 }
1385
1386 loop_count++;
1387
1388 } while (!shadow_update_finished(loop_count));
1389
1390 if (prev_fb) {
1391 GPU_framebuffer_bind(prev_fb);
1392 }
1393}
1394
1396{
1397
1398 switch (inst_.debug_mode) {
1400 inst_.info_append(
1401 "Debug Mode: Shadow Tilemap (active light)\n"
1402 " - Green: Used\n"
1403 " - Yellow: Used & Updated\n"
1404 " - Purple: Cached\n");
1405 break;
1407 inst_.info_append("Debug Mode: Shadow Values (active light)");
1408 break;
1410 inst_.info_append("Debug Mode: Shadow Tile Random Color (active light)");
1411 break;
1413 inst_.info_append("Debug Mode: Shadow Tilemap Random Color (active light)");
1414 break;
1416 inst_.info_append(
1417 "Debug Mode: Shadow Atomic Cost\n"
1418 " - Blue: Low\n"
1419 " - Red: Medium\n"
1420 " - White: High");
1421 break;
1422 default:
1423 return;
1424 }
1425
1426 inst_.hiz_buffer.update();
1427
1428 GPU_framebuffer_bind(view_fb);
1429 inst_.manager->submit(debug_draw_ps_, view);
1430}
1431
1433
1434} // namespace blender::eevee
int32_t bool32_t
#define UNUSED_VARS(...)
#define BLI_assert(a)
#define ELEM(...)
btScalar max
constexpr int64_t one_after_last() const
constexpr IndexRange shift(int64_t n) const
constexpr IndexRange intersect(IndexRange other) const
constexpr IndexRange()=default
constexpr int64_t start() const
SwapChain< ObjectBoundsBuf, 2 > bounds_buf
ObjectRef(Object *ob, Object *dupli_parent=nullptr, DupliObject *dupli_object=nullptr)
ResourceHandle sub_handle(int index) const
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
void clear(float4 values)
UniformArrayBuffer< ViewCullingData, DRW_VIEW_MAX > culling_
Definition draw_view.hh:50
VisibilityBuf visibility_buf_
Definition draw_view.hh:55
static float screen_pixel_radius(const float4x4 &wininv, bool is_perspective, const int2 &extent)
Definition draw_view.cc:349
UniformArrayBuffer< ViewMatrices, DRW_VIEW_MAX > data_
Definition draw_view.hh:49
int visibility_word_per_draw() const
Definition draw_view.hh:178
void bind_resources(U &resources)
Definition draw_pass.hh:459
void framebuffer_set(gpu::FrameBuffer **framebuffer)
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)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
void dispatch(int group_len)
void state_set(DRWState state, int clip_plane_count=0)
void barrier(GPUBarrier type)
void state_stencil(uint8_t write_mask, uint8_t reference, uint8_t compare_mask)
void push_constant(const char *name, const float &data)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:906
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
A running instance of the engine.
void info_append_i18n(const char *msg, Args &&...args)
static float coverage_get(int lvl)
static float tile_size_get(int lvl)
void end_sync(Light &light, const Camera &camera)
void release_excess_tilemaps(const Light &light, const Camera &camera)
ShadowModule(Instance &inst, ShadowSceneData &data)
ShadowTileMapPool tilemap_pool
void sync_object(const ObjectHandle &ob_handle, bool is_alpha_blend_or_has_offset_shadow, bool has_transparent_shadows, bool has_time_dependent_shadows, bool has_offset_shadows)
void set_view(View &view, int2 extent)
void debug_draw(View &view, gpu::FrameBuffer *view_fb)
void render(View &view, int2 extent)
void release_excess_tilemaps(const Light &light)
Simple API to draw debug shapes and log in the viewport.
#define DRW_VISIBILITY_GROUP_SIZE
#define DRW_VIEW_CULLING_UBO_SLOT
#define DRW_OBJ_INFOS_SLOT
#define DRW_VIEW_UBO_SLOT
#define VOLUME_GROUP_SIZE
#define LIGHT_NO_SHADOW
#define round
VecBase< float, D > normalize(VecOp< float, D >) RET
#define log2
#define abs
#define floor
float distance(float, float) RET
#define ceil
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
void drw_debug_matrix_as_bbox(const float4x4 &mat, const float4 color, const uint lifetime)
StorageArrayBuffer< ObjectBounds, 128 > ObjectBoundsBuf
Definition draw_view.hh:33
StorageArrayBuffer< ObjectInfos, 128 > ObjectInfosBuf
Definition draw_view.hh:34
@ SHADOW_TILEMAP_TAG_USAGE_SURFELS
@ SHADOW_TILEMAP_TAG_USAGE_OPAQUE
@ SHADOW_TILEMAP_TAG_USAGE_TRANSPARENT
@ SHADOW_TILEMAP_TAG_USAGE_VOLUME
@ SHADOW_TILEMAP_TAG_UPDATE_PROPAGATE
draw::StorageArrayBuffer< Surfel, 64 > SurfelBuf
draw::StorageBuffer< CaptureInfoData > CaptureInfoBuf
MatBase< T, 4, 4 > orthographic(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
MatBase< T, 4, 4 > perspective(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create a perspective projection matrix using OpenGL coordinate convention: Maps each axis range to [-...
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T min(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
T max(const T &a, const T &b)
T abs(const T &a)
T round(const T &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
VecBase< uint32_t, 2 > uint2
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
gpu::FrameBuffer * GPU_framebuffer_active_get()
__int64 int64_t
Definition BKE_text.h:90
VecBase< int32_t, 4 > int4
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
void GPU_framebuffer_viewport_set(gpu::FrameBuffer *fb, int x, int y, int width, int height)
MINLINE int square_i(int a)
VecBase< uint32_t, 4 > uint4
gpu::Batch * GPU_batch_unit_cube() ATTR_WARN_UNUSED_RESULT
MatBase< float, 2, 3 > float2x3
MINLINE int max_ii(int a, int b)
VecBase< uint32_t, 3 > uint3
MatBase< float, 4, 4 > float4x4
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_storagebuf_clear(gpu::StorageBuf *ssbo, uint32_t clear_value)
VecBase< float, 4 > float4
@ SCE_EEVEE_SHADOW_JITTERED_VIEWPORT
@ SCE_EEVEE_SHADOW_ENABLED
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATOMIC
@ GPU_TEXTURE_USAGE_ATTACHMENT
MINLINE uint64_t divide_ceil_ul(uint64_t a, uint64_t b)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
void GPU_framebuffer_multi_viewports_set(gpu::FrameBuffer *gpu_fb, const int viewport_rects[GPU_MAX_VIEWPORTS][4])
bool equals_m4m4(const float mat1[4][4], const float mat2[4][4])
MINLINE float int_as_float(int i)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_UNIFORM
Definition GPU_state.hh:57
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_CUSTOM
Definition draw_state.hh:65
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:38
@ DRW_STATE_WRITE_STENCIL
Definition draw_state.hh:34
@ DRW_STATE_CULL_FRONT
Definition draw_state.hh:46
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
MatBase< float, 3, 3 > float3x3
void GPU_debug_group_end()
Definition gpu_debug.cc:34
T * DEG_get_original(T *id)
void GPU_flush()
Definition gpu_state.cc:284
MINLINE float max_ff(float a, float b)
MINLINE uint divide_ceil_u(uint a, uint b)
bool GPU_multi_viewport_support()
void GPU_uniformbuf_clear_to_zero(gpu::UniformBuf *ubo)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
VecBase< float, 3 > float3
MINLINE int min_ii(int a, int b)
MINLINE int clamp_i(int value, int min, int max)
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const NodeType * type
Definition graph/node.h:179
const c_style_mat & ptr() const
const MatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view() const
eObject_VisibilityFlag visibility_flag
eSceneEEVEE_Flag flag
struct SceneEEVEE eevee
const ResourceHandleRange res_handle
Definition eevee_sync.hh:39
enum eShadowProjectionType projection_type
ShadowTileMapDataBuf tilemaps_data
ShadowTileMapDataBuf tilemaps_unused
void release(Span< ShadowTileMap * > free_list)
ShadowTileMapClipBuf tilemaps_clip
void end_sync(ShadowModule &module)
Pool< ShadowTileMap > tilemap_pool
static constexpr int64_t maps_per_row
void sync_orthographic(const float4x4 &object_mat_, int2 origin_offset, int clipmap_level, eShadowProjectionType projection_type_, uint2 shadow_set_membership_=~uint2(0))
static constexpr int64_t tile_map_resolution
ShadowTileMap(int tiles_index_)
void sync_cubeface(eLightType light_type_, const float4x4 &object_mat, float near, float far, eCubeFace face, uint2 shadow_set_membership_=~uint2(0))
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline T transform_direction_transposed(const ccl_private Transform *t, const T a)
Definition transform.h:176