Blender V4.5
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_compute.hh"
14
15#include "GPU_context.hh"
16#include "eevee_instance.hh"
17
18#include "GPU_debug.hh"
19#include "draw_cache.hh"
20#include "draw_debug.hh"
21
22namespace blender::eevee {
23
25
26/* -------------------------------------------------------------------- */
30
32 int2 origin_offset,
33 int clipmap_level,
34 eShadowProjectionType projection_type_,
35 uint2 shadow_set_membership_)
36{
37 if ((projection_type != projection_type_) || (level != clipmap_level) ||
38 (shadow_set_membership_ != shadow_set_membership))
39 {
40 set_dirty();
41 }
42 projection_type = projection_type_;
43 level = clipmap_level;
45 shadow_set_membership = shadow_set_membership_;
46
47 grid_shift = origin_offset - grid_offset;
48 grid_offset = origin_offset;
49
50 if (!equals_m4m4(object_mat.ptr(), object_mat_.ptr())) {
51 object_mat = object_mat_;
52 set_dirty();
53 }
54
55 float tile_size = ShadowDirectional::tile_size_get(level);
56
57 /* object_mat is a rotation matrix. Reduce imprecision by taking the transpose which is also the
58 * inverse in this particular case. */
60
62 center_offset = float2(grid_offset) * tile_size;
63
68 /* Near/far is computed on GPU using casters bounds. */
69 -1.0f,
70 1.0f);
71}
72
74 const float4x4 &object_mat_,
75 float near_,
76 float far_,
77 eCubeFace face,
78 uint2 shadow_set_membership_)
79{
81 (shadow_set_membership_ != shadow_set_membership))
82 {
83 set_dirty();
84 }
86 cubeface = face;
87 grid_offset = int2(0);
88 light_type = light_type_;
89 shadow_set_membership = shadow_set_membership_;
90
91 if ((clip_near != near_) || (clip_far != far_)) {
92 set_dirty();
93 }
94
95 clip_near = near_;
96 half_size = near_;
97 clip_far = far_;
98 center_offset = float2(0.0f);
99
100 if (!equals_m4m4(object_mat.ptr(), object_mat_.ptr())) {
101 object_mat = object_mat_;
102 set_dirty();
103 }
104
108
109 /* Same thing as inversion but avoid precision issues. */
111 /* Update corners. */
112 corners[0] = float4(viewinv.location(), 0.0f);
113 corners[1] = float4(math::transform_point(viewinv, float3(-far_, -far_, -far_)), 0.0f);
114 corners[2] = float4(math::transform_point(viewinv, float3(far_, -far_, -far_)), 0.0f);
115 corners[3] = float4(math::transform_point(viewinv, float3(-far_, far_, -far_)), 0.0f);
116 /* Store deltas. */
117 corners[2] = (corners[2] - corners[1]) / float(SHADOW_TILEMAP_RES);
118 corners[3] = (corners[3] - corners[1]) / float(SHADOW_TILEMAP_RES);
119}
120
122{
124 const float4 debug_color[6] = {
125 {1.0f, 0.1f, 0.1f, 1.0f},
126 {0.1f, 1.0f, 0.1f, 1.0f},
127 {0.0f, 0.2f, 1.0f, 1.0f},
128 {1.0f, 1.0f, 0.3f, 1.0f},
129 {0.1f, 0.1f, 0.1f, 1.0f},
130 {1.0f, 1.0f, 1.0f, 1.0f},
131 };
132 float4 color = debug_color
133 [((projection_type == SHADOW_PROJECTION_CUBEFACE ? int(cubeface) : level) + 9999) % 6];
134
135 float4x4 persinv = winmat * viewmat;
137}
138
140
141/* -------------------------------------------------------------------- */
145
147{
149 /* Reverse order to help debugging (first allocated tile-map will get 0). */
150 for (int i = SHADOW_MAX_TILEMAP - 1; i >= 0; i--) {
152 }
153
154 int2 extent;
157
160 tilemap_tx.ensure_2d(GPU_R32UI, extent, usage);
161 tilemap_tx.clear(uint4(0));
162}
163
165{
166 if (free_indices.is_empty()) {
167 /* Grow the tile-map buffer. See `end_sync`. */
168 for (auto i : IndexRange(free_indices.size(), SHADOW_MAX_TILEMAP)) {
170 }
171 }
172 int index = free_indices.pop_last();
173 return &tilemap_pool.construct(ShadowTileMap(index));
174}
175
177{
178 for (ShadowTileMap *map : free_list) {
179 free_indices.append(map->tiles_index);
180 tilemap_pool.destruct(*map);
181 }
182}
183
185{
186 tilemaps_data.push_update();
187
188 uint needed_tilemap_capacity = (free_indices.size() + tilemap_pool.size());
189 if (needed_tilemap_capacity != (tiles_data.size() / SHADOW_TILEDATA_PER_TILEMAP)) {
190 tiles_data.resize(needed_tilemap_capacity * SHADOW_TILEDATA_PER_TILEMAP);
191 tilemaps_clip.resize(needed_tilemap_capacity);
192 /* We reallocated the tile-map buffer, discarding all the data it contained.
193 * We need to re-initialize the page heaps. */
194 module.do_full_update_ = true;
195 }
196
197 tilemaps_unused.clear();
198 int64_t newly_unused_count = free_indices.size() - last_free_len;
199 if (newly_unused_count > 0) {
200 /* Upload tile-map indices which pages needs to be pushed back to the free page heap. */
201 Span<uint> newly_unused_indices = free_indices.as_span().slice(last_free_len,
202 newly_unused_count);
203 for (uint index : newly_unused_indices) {
204 /* Push a dummy tile-map to a unused tile-map buffer. It is then processed through the some
205 * of the setup steps to release the pages. */
206 ShadowTileMapData tilemap_data = {};
207 tilemap_data.tiles_index = index;
208 tilemap_data.clip_data_index = -1;
209 tilemap_data.grid_shift = int2(SHADOW_TILEMAP_RES);
211
212 tilemaps_unused.append(tilemap_data);
213 }
214 tilemaps_unused.push_update();
215 }
216
218}
219
221
222/* -------------------------------------------------------------------- */
226
228{
229 int tilemaps_needed = light_local_tilemap_count(light);
230 if (tilemaps_.size() <= tilemaps_needed) {
231 return;
232 }
233 auto span = tilemaps_.as_span();
234 shadows_.tilemap_pool.release(span.drop_front(tilemaps_needed));
235 tilemaps_ = span.take_front(tilemaps_needed);
236}
237
239{
240 ShadowTileMapPool &tilemap_pool = shadows_.tilemap_pool;
241
242 float4x4 object_to_world = light.object_to_world;
243
244 /* Acquire missing tile-maps. */
245 int tilemaps_needed = light_local_tilemap_count(light);
246 while (tilemaps_.size() < tilemaps_needed) {
247 tilemaps_.append(tilemap_pool.acquire());
248 }
249
250 float near = int_as_float(light.clip_near);
251 float far = int_as_float(light.clip_far);
252 for (int i : tilemaps_.index_range()) {
253 eCubeFace face = eCubeFace(Z_NEG + i);
254 tilemaps_[face]->sync_cubeface(
255 light.type, object_to_world, near, far, face, light.shadow_set_membership);
256 }
257
258 light.local.tilemaps_count = tilemaps_needed;
259 light.tilemap_index = tilemap_pool.tilemaps_data.size();
260 for (ShadowTileMap *tilemap : tilemaps_) {
261 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
262 tilemap_pool.tilemaps_data.append(*tilemap);
263 tilemap->set_updated();
264 }
265}
266
268
269/* -------------------------------------------------------------------- */
285
286eShadowProjectionType ShadowDirectional::directional_distribution_type_get(const Camera &camera)
287{
288 /* TODO(fclem): Enable the cascade projection if the FOV is tiny in perspective mode. */
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 /* Level of detail (or size) of every tile-maps of this light. */
337 /* TODO(fclem): Add support for lod bias from light. */
338 int lod_level = ceil(log2(max_ff(min_depth_tilemap_size, min_diagonal_tilemap_size)) + 0.5);
339
340 /* Tile-maps "rotate" around the first one so their effective range is only half their size. */
341 float per_tilemap_coverage = ShadowDirectional::coverage_get(lod_level) * 0.5f;
342 /* Number of tile-maps needed to cover the whole view. */
343 /* NOTE: floor + 0.5 to avoid 0 when parallel. */
344 int tilemap_len = ceil(0.5f + depth_range_in_shadow_space / per_tilemap_coverage);
345 return IndexRange(lod_level, tilemap_len);
346}
347
348void ShadowDirectional::cascade_tilemaps_distribution(Light &light, const Camera &camera)
349{
350 using namespace blender::math;
351
352 float4x4 object_mat = light.object_to_world;
353 object_mat.location() = float3(0.0f);
354
355 /* All tile-maps use the first level size. */
356 float half_size = ShadowDirectional::coverage_get(levels_range.first()) / 2.0f;
357 float tile_size = ShadowDirectional::tile_size_get(levels_range.first());
358
359 float3 near_point, far_point;
360 cascade_tilemaps_distribution_near_far_points(camera, light, near_point, far_point);
361
362 float2 local_view_direction = normalize(far_point.xy() - near_point.xy());
363 float2 farthest_tilemap_center = local_view_direction * half_size * (levels_range.size() - 1);
364
365 /* Offset for smooth level transitions. */
366 light.object_to_world.x.w = near_point.x;
367 light.object_to_world.y.w = near_point.y;
368 light.object_to_world.z.w = near_point.z;
369
370 /* Offset in tiles from the scene origin to the center of the first tile-maps. */
371 int2 origin_offset = int2(round(float2(near_point) / tile_size));
372 /* Offset in tiles between the first and the last tile-maps. */
373 int2 offset_vector = int2(round(farthest_tilemap_center / tile_size));
374
375 light.sun.clipmap_base_offset_neg = int2(0); /* Unused. */
376 light.sun.clipmap_base_offset_pos = (offset_vector * (1 << 16)) /
377 max_ii(levels_range.size() - 1, 1);
378
379 /* \note cascade_level_range starts the range at the unique LOD to apply to all tile-maps. */
380 int level = levels_range.first();
381 for (int i : IndexRange(levels_range.size())) {
382 ShadowTileMap *tilemap = tilemaps_[i];
383
384 /* Equal spacing between cascades layers since we want uniform shadow density. */
385 int2 level_offset = origin_offset +
386 shadow_cascade_grid_offset(light.sun.clipmap_base_offset_pos, i);
387 tilemap->sync_orthographic(
388 object_mat, level_offset, level, SHADOW_PROJECTION_CASCADE, light.shadow_set_membership);
389
390 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
391 shadows_.tilemap_pool.tilemaps_data.append(*tilemap);
392 tilemap->set_updated();
393 }
394
395 light.sun.clipmap_origin = float2(origin_offset) * tile_size;
396
397 light.type = LIGHT_SUN_ORTHO;
398
399 /* Not really clip-maps, but this is in order to make #light_tilemap_max_get() work and determine
400 * the scaling. */
401 light.sun.clipmap_lod_min = levels_range.first();
402 light.sun.clipmap_lod_max = levels_range.last();
403}
404
405/************************************************************************
406 * Clip-map Distribution *
407 ************************************************************************/
408
409IndexRange ShadowDirectional::clipmap_level_range(const Camera &cam)
410{
411 using namespace blender::math;
412 /* Covers the closest points of the view. */
413 /* FIXME: IndexRange does not support negative indices. Clamp to 0 for now. */
414 int min_level = max(0.0f, floor(log2(abs(cam.data_get().clip_near))));
415 /* Covers the farthest points of the view. */
416 int max_level = ceil(log2(cam.bound_radius() + distance(cam.bound_center(), cam.position())));
417 /* We actually need to cover a bit more because of clipmap origin snapping. */
418 max_level = max(min_level, max_level) + 1;
419 IndexRange range(min_level, max_level - min_level + 1);
420 /* 32 to be able to pack offset into a single int2.
421 * The maximum level count is bounded by the mantissa of a 32bit float. */
422 const int max_tilemap_per_shadows = 24;
423 /* Take top-most level to still cover the whole view. */
424 range = range.take_back(max_tilemap_per_shadows);
425
426 return range;
427}
428
429void ShadowDirectional::clipmap_tilemaps_distribution(Light &light, const Camera &camera)
430{
431 float4x4 object_mat = light.object_to_world;
432 object_mat.location() = float3(0.0f);
433
434 for (int lod : IndexRange(levels_range.size())) {
435 ShadowTileMap *tilemap = tilemaps_[lod];
436
437 int level = levels_range.first() + lod;
438 /* Compute full offset from world origin to the smallest clipmap tile centered around the
439 * camera position. The offset is computed in smallest tile unit. */
440 float tile_size = ShadowDirectional::tile_size_get(level);
441 /* Moving to light space by multiplying by the transpose (which is the inverse). */
442 float2 light_space_camera_position = camera.position() * float2x3(object_mat.view<2, 3>());
443 int2 level_offset = int2(math::round(light_space_camera_position / tile_size));
444
445 tilemap->sync_orthographic(
446 object_mat, level_offset, level, SHADOW_PROJECTION_CLIPMAP, light.shadow_set_membership);
447
448 /* Add shadow tile-maps grouped by lights to the GPU buffer. */
449 shadows_.tilemap_pool.tilemaps_data.append(*tilemap);
450 tilemap->set_updated();
451 }
452
453 int2 pos_offset = int2(0);
454 int2 neg_offset = int2(0);
455 for (int lod : IndexRange(levels_range.size() - 1)) {
456 /* Since offset can only differ by one tile from the higher level, we can compress that as a
457 * single integer where one bit contains offset between 2 levels. Then a single bit shift in
458 * the shader gives the number of tile to offset in the given tile-map space. However we need
459 * also the sign of the offset for each level offset. To this end, we split the negative
460 * offsets to a separate int. */
461 int2 lvl_offset_next = tilemaps_[lod + 1]->grid_offset;
462 int2 lvl_offset = tilemaps_[lod]->grid_offset;
463 int2 lvl_delta = lvl_offset - (lvl_offset_next * 2);
464 BLI_assert(math::abs(lvl_delta.x) <= 1 && math::abs(lvl_delta.y) <= 1);
465 pos_offset |= math::max(lvl_delta, int2(0)) << lod;
466 neg_offset |= math::max(-lvl_delta, int2(0)) << lod;
467 }
468
469 /* Number of levels is limited to 32 by `clipmap_level_range()` for this reason. */
470 light.sun.clipmap_base_offset_pos = pos_offset;
471 light.sun.clipmap_base_offset_neg = neg_offset;
472
473 float tile_size_max = ShadowDirectional::tile_size_get(levels_range.last());
474 int2 level_offset_max = tilemaps_[levels_range.size() - 1]->grid_offset;
475
476 light.type = LIGHT_SUN;
477
478 /* Used for selecting the clipmap level. */
479 float3 location = transform_direction_transposed(light.object_to_world, camera.position());
480 /* Offset for smooth level transitions. */
481 light.object_to_world.x.w = location.x;
482 light.object_to_world.y.w = location.y;
483 light.object_to_world.z.w = location.z;
484 /* Used as origin for the clipmap_base_offset trick. */
485 light.sun.clipmap_origin = float2(level_offset_max * tile_size_max);
486
487 light.sun.clipmap_lod_min = levels_range.first();
488 light.sun.clipmap_lod_max = levels_range.last();
489}
490
492{
493 IndexRange levels_new = directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE ?
494 cascade_level_range(light, camera) :
495 clipmap_level_range(camera);
496
497 if (levels_range == levels_new) {
498 return;
499 }
500
501 IndexRange isect_range = levels_range.intersect(levels_new);
502 IndexRange before_range(levels_range.start(), isect_range.start() - levels_range.start());
503 IndexRange after_range(isect_range.one_after_last(),
504 levels_range.one_after_last() - isect_range.one_after_last());
505
506 auto span = tilemaps_.as_span();
507 shadows_.tilemap_pool.release(span.slice(before_range.shift(-levels_range.start())));
508 shadows_.tilemap_pool.release(span.slice(after_range.shift(-levels_range.start())));
509 tilemaps_ = span.slice(isect_range.shift(-levels_range.start()));
510 levels_range = isect_range;
511}
512
513void ShadowDirectional::end_sync(Light &light, const Camera &camera)
514{
515 ShadowTileMapPool &tilemap_pool = shadows_.tilemap_pool;
516 IndexRange levels_new = directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE ?
517 cascade_level_range(light, camera) :
518 clipmap_level_range(camera);
519
520 if (levels_range != levels_new) {
521 /* Acquire missing tile-maps. */
522 IndexRange isect_range = levels_new.intersect(levels_range);
523 int64_t before_range = isect_range.start() - levels_new.start();
524 int64_t after_range = levels_new.one_after_last() - isect_range.one_after_last();
525
526 Vector<ShadowTileMap *> cached_tilemaps = tilemaps_;
527 tilemaps_.clear();
528 for (int64_t i = 0; i < before_range; i++) {
529 tilemaps_.append(tilemap_pool.acquire());
530 }
531 /* Keep cached LOD's. */
532 tilemaps_.extend(cached_tilemaps);
533 for (int64_t i = 0; i < after_range; i++) {
534 tilemaps_.append(tilemap_pool.acquire());
535 }
536 levels_range = levels_new;
537 }
538
539 light.tilemap_index = tilemap_pool.tilemaps_data.size();
540 light.clip_near = 0x7F7FFFFF; /* floatBitsToOrderedInt(FLT_MAX) */
541 light.clip_far = int(0xFF7FFFFFu ^ 0x7FFFFFFFu); /* floatBitsToOrderedInt(-FLT_MAX) */
542
543 if (directional_distribution_type_get(camera) == SHADOW_PROJECTION_CASCADE) {
544 cascade_tilemaps_distribution(light, camera);
545 }
546 else {
547 clipmap_tilemaps_distribution(light, camera);
548 }
549}
550
552
553/* -------------------------------------------------------------------- */
557
559{
560 for (int i = 0; i < statistics_buf_.size(); i++) {
561 UNUSED_VARS(i);
562 statistics_buf_.current().clear_to_zero();
563 statistics_buf_.swap();
564 }
565}
566
568{
569 /* Temp: Disable TILE_COPY path while efficient solution for parameter buffer overflow is
570 * identified. This path can be re-enabled in future. */
571#if 0
572 /* Determine shadow update technique and atlas format.
573 * NOTE(Metal): Metal utilizes a tile-optimized approach for Apple Silicon's architecture. */
574 const bool is_metal_backend = (GPU_backend_get_type() == GPU_BACKEND_METAL);
575 const bool is_tile_based_arch = (GPU_platform_architecture() == GPU_ARCHITECTURE_TBDR);
576 if (is_metal_backend && is_tile_based_arch) {
578 }
579 else
580#endif
581 {
583 }
584
585 ::Scene &scene = *inst_.scene;
586
587 global_lod_bias_ = (1.0f - scene.eevee.shadow_resolution_scale) * SHADOW_TILEMAP_LOD;
588
589 bool update_lights = false;
590 bool enable_shadow = (scene.eevee.flag & SCE_EEVEE_SHADOW_ENABLED) != 0;
591 bool use_jitter = enable_shadow &&
592 (inst_.is_image_render ||
593 (!inst_.is_navigating && !inst_.is_transforming && !inst_.is_playback &&
595 update_lights |= assign_if_different(enabled_, enable_shadow);
596 update_lights |= assign_if_different(data_.use_jitter, bool32_t(use_jitter));
597 if (update_lights) {
598 /* Force light reset. */
599 for (Light &light : inst_.lights.light_map_.values()) {
600 light.initialized = false;
601 }
602 }
603
604 data_.ray_count = clamp_i(scene.eevee.shadow_ray_count, 1, SHADOW_MAX_RAY);
605 data_.step_count = clamp_i(scene.eevee.shadow_step_count, 1, SHADOW_MAX_STEP);
606
607 /* Pool size is in MBytes. */
608 const size_t pool_byte_size = enabled_ ? scene.eevee.shadow_pool_size * square_i(1024) : 1;
609 const size_t page_byte_size = square_i(shadow_page_size_) * sizeof(int);
610 shadow_page_len_ = int(divide_ceil_ul(pool_byte_size, page_byte_size));
611 shadow_page_len_ = min_ii(shadow_page_len_, SHADOW_MAX_PAGE);
612
613 const int2 atlas_extent = shadow_page_size_ * int2(SHADOW_PAGE_PER_ROW);
614 const int atlas_layers = divide_ceil_u(shadow_page_len_, SHADOW_PAGE_PER_LAYER);
615
618 tex_usage |= GPU_TEXTURE_USAGE_ATOMIC;
619 }
620 if (atlas_tx_.ensure_2d_array(atlas_type, atlas_extent, atlas_layers, tex_usage)) {
621 /* Global update. */
622 do_full_update_ = true;
623 }
624
625 /* Make allocation safe. Avoids crash later on. */
626 if (!atlas_tx_.is_valid()) {
627 atlas_tx_.ensure_2d_array(ShadowModule::atlas_type, int2(1), 1);
628 inst_.info_append_i18n(
629 "Error: Could not allocate shadow atlas. Most likely out of GPU memory.");
630 }
631
632 /* Read end of the swap-chain to avoid stall. */
633 if (inst_.is_viewport()) {
634 if (inst_.sampling.finished_viewport()) {
635 /* Swap enough to read the last one. */
636 for (int i = 0; i < statistics_buf_.size(); i++) {
637 statistics_buf_.swap();
638 }
639 }
640 else {
641 statistics_buf_.swap();
642 }
643 statistics_buf_.current().read();
644 ShadowStatistics stats = statistics_buf_.current();
645
646 if (stats.page_used_count > shadow_page_len_ && enabled_) {
647 inst_.info_append_i18n(
648 "Error: Shadow buffer full, may result in missing shadows and lower "
649 "performance. ({} / {})",
650 stats.page_used_count,
651 shadow_page_len_);
652 }
653 if (stats.view_needed_count > SHADOW_VIEW_MAX && enabled_) {
654 inst_.info_append_i18n("Error: Too many shadow updates, some shadows might be incorrect.");
655 }
656 }
657
658 atlas_tx_.filter_mode(false);
659
660 /* Create different viewport to support different update region size. The most fitting viewport
661 * is then selected during the tilemap finalize stage in `viewport_select`. */
662 for (int i = 0; i < multi_viewports_.size(); i++) {
665 int size_in_tile = min_ii(1 << i, SHADOW_TILEMAP_RES);
666 multi_viewports_[i][0] = 0;
667 multi_viewports_[i][1] = 0;
668 multi_viewports_[i][2] = size_in_tile * shadow_page_size_;
669 multi_viewports_[i][3] = size_in_tile * shadow_page_size_;
670 }
671}
672
674{
675 past_casters_updated_.clear();
676 curr_casters_updated_.clear();
677 curr_casters_.clear();
678 jittered_transparent_casters_.clear();
679 update_casters_ = true;
680
681 if (box_batch_ == nullptr) {
682 box_batch_ = GPU_batch_unit_cube();
683 }
684
685 {
686 Manager &manager = *inst_.manager;
687
688 PassMain &pass = tilemap_usage_ps_;
689 pass.init();
690
691 if (inst_.is_baking()) {
692 SurfelBuf &surfels_buf = inst_.volume_probes.bake.surfels_buf_;
693 CaptureInfoBuf &capture_info_buf = inst_.volume_probes.bake.capture_info_buf_;
694 float surfel_coverage_area = inst_.volume_probes.bake.surfel_density_;
695
696 /* Directional shadows. */
697 float texel_size = ShadowDirectional::tile_size_get(0) / float(SHADOW_PAGE_RES);
698 int directional_level = std::max(0, int(std::ceil(log2(surfel_coverage_area / texel_size))));
699
700 PassMain::Sub &sub = pass.sub("Surfels");
701 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_SURFELS));
702 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
703 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
704 sub.bind_ssbo("surfel_buf", &surfels_buf);
705 sub.bind_ssbo("capture_info_buf", &capture_info_buf);
706 sub.push_constant("directional_level", directional_level);
707 sub.bind_resources(inst_.uniform_data);
708 sub.bind_resources(inst_.lights);
709 sub.dispatch(&inst_.volume_probes.bake.dispatch_per_surfel_);
710
711 /* Skip opaque and transparent tagging for light baking. */
712 return;
713 }
714
715 {
716 /* Use depth buffer to tag needed shadow pages for opaque geometry. */
717 PassMain::Sub &sub = pass.sub("Opaque");
718 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_OPAQUE));
719 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
720 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
721 sub.bind_texture("depth_tx", &src_depth_tx_);
722 sub.push_constant("input_depth_extent", &input_depth_extent_);
723 sub.bind_resources(inst_.lights);
724 sub.bind_resources(inst_.uniform_data);
725 sub.bind_resources(inst_.hiz_buffer.front);
726 sub.dispatch(&dispatch_depth_scan_size_);
727 }
728 {
729 /* Use bounding boxes for transparent geometry. */
730 PassMain::Sub &sub = pass.sub("Transparent");
731 /* WORKAROUND: The DRW_STATE_WRITE_STENCIL is here only to avoid enabling the rasterizer
732 * discard inside draw manager. */
734 sub.state_stencil(0, 0, 0);
735 sub.framebuffer_set(&usage_tag_fb);
736 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_TRANSPARENT));
737 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
738 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
739 sub.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
740 sub.push_constant("fb_resolution", &usage_tag_fb_resolution_);
741 sub.push_constant("fb_lod", &usage_tag_fb_lod_);
742 sub.bind_resources(inst_.uniform_data);
743 sub.bind_resources(inst_.hiz_buffer.front);
744 sub.bind_resources(inst_.lights);
745
746 tilemap_usage_transparent_ps_ = &sub;
747 }
748 }
749}
750
752 const ObjectHandle &handle,
753 const ResourceHandle &resource_handle,
754 bool is_alpha_blend,
755 bool has_transparent_shadows)
756{
757 bool is_shadow_caster = !(ob->visibility_flag & OB_HIDE_SHADOW);
758 if (!is_shadow_caster && !is_alpha_blend) {
759 return;
760 }
761
762 ShadowObject &shadow_ob = objects_.lookup_or_add_default(handle.object_key);
763 shadow_ob.used = true;
764 const bool is_initialized = shadow_ob.resource_handle.raw != 0;
765 const bool has_jittered_transparency = has_transparent_shadows && data_.use_jitter;
766 if (is_shadow_caster && (handle.recalc || !is_initialized || has_jittered_transparency)) {
767 if (handle.recalc && is_initialized) {
768 past_casters_updated_.append(shadow_ob.resource_handle.raw);
769 }
770
771 if (has_jittered_transparency) {
772 jittered_transparent_casters_.append(resource_handle.raw);
773 }
774 else {
775 curr_casters_updated_.append(resource_handle.raw);
776 }
777 }
778 shadow_ob.resource_handle = resource_handle;
779
780 if (is_shadow_caster) {
781 curr_casters_.append(resource_handle.raw);
782 }
783
784 if (is_alpha_blend && !inst_.is_baking()) {
785 tilemap_usage_transparent_ps_->draw(box_batch_, resource_handle);
786 }
787}
788
790{
791 /* Delete unused shadows first to release tile-maps that could be reused for new lights. */
792 for (Light &light : inst_.lights.light_map_.values()) {
793 /* Do not discard lights in baking mode. See WORKAROUND in `surfels_create`. */
794 if ((!light.used || !enabled_) && !inst_.is_baking()) {
795 light.shadow_discard_safe(*this);
796 }
797 else if (light.directional != nullptr) {
798 light.directional->release_excess_tilemaps(light, inst_.camera);
799 }
800 else if (light.punctual != nullptr) {
801 light.punctual->release_excess_tilemaps(light);
802 }
803 }
804
805 /* Allocate new tile-maps and fill shadow data of the lights. */
806 tilemap_pool.tilemaps_data.clear();
807 for (Light &light : inst_.lights.light_map_.values()) {
808 if (enabled_ == false) {
809 light.tilemap_index = LIGHT_NO_SHADOW;
810 }
811 else if (light.directional != nullptr) {
812 light.directional->end_sync(light, inst_.camera);
813 }
814 else if (light.punctual != nullptr) {
815 light.punctual->end_sync(light);
816 }
817 else {
818 light.tilemap_index = LIGHT_NO_SHADOW;
819 }
820 }
821 tilemap_pool.end_sync(*this);
822
823 /* Search for deleted or updated shadow casters */
824 auto it_end = objects_.items().end();
825 for (auto it = objects_.items().begin(); it != it_end; ++it) {
826 ShadowObject &shadow_ob = (*it).value;
827 /* Do not discard casters in baking mode. See WORKAROUND in `surfels_create`. */
828 if (!shadow_ob.used && !inst_.is_baking()) {
829 /* May not be a caster, but it does not matter, be conservative. */
830 past_casters_updated_.append(shadow_ob.resource_handle.raw);
831 objects_.remove(it);
832 }
833 else {
834 /* Clear for next sync. */
835 shadow_ob.used = false;
836 }
837 }
838 past_casters_updated_.push_update();
839 curr_casters_updated_.push_update();
840 jittered_transparent_casters_.push_update();
841
842 curr_casters_.push_update();
843
844 if (do_full_update_) {
845 do_full_update_ = false;
846 /* Put all pages in the free heap. */
847 for (uint i : IndexRange(shadow_page_len_)) {
848 uint3 page = {i % SHADOW_PAGE_PER_ROW,
851 pages_free_data_[i] = shadow_page_pack(page);
852 }
853 for (uint i : IndexRange(shadow_page_len_, SHADOW_MAX_PAGE - shadow_page_len_)) {
854 pages_free_data_[i] = 0xFFFFFFFFu;
855 }
856 pages_free_data_.push_update();
857
858 /* Clear tiles to not reference any page. */
859 tilemap_pool.tiles_data.clear_to_zero();
860 tilemap_pool.tilemaps_clip.clear_to_zero();
861
862 /* Clear cached page buffer. */
863 GPU_storagebuf_clear(pages_cached_data_, -1);
864
865 /* Reset info to match new state. */
866 pages_infos_data_.page_free_count = shadow_page_len_;
867 pages_infos_data_.page_alloc_count = 0;
868 pages_infos_data_.page_cached_next = 0u;
869 pages_infos_data_.page_cached_start = 0u;
870 pages_infos_data_.page_cached_end = 0u;
871 pages_infos_data_.push_update();
872 }
873
874 {
875 Manager &manager = *inst_.manager;
876
877 {
878 PassSimple &pass = tilemap_setup_ps_;
879 pass.init();
880
881 {
882 /* Clear tile-map clip buffer. */
883 PassSimple::Sub &sub = pass.sub("ClearClipmap");
884 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_CLIPMAP_CLEAR));
885 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
886 sub.push_constant("tilemaps_clip_buf_len", int(tilemap_pool.tilemaps_clip.size()));
887 sub.dispatch(int3(
888 divide_ceil_u(tilemap_pool.tilemaps_clip.size(), SHADOW_CLIPMAP_GROUP_SIZE), 1, 1));
890 }
891
892 {
893 /* Compute near/far clip distances for directional shadows based on casters bounds. */
894 PassSimple::Sub &sub = pass.sub("DirectionalBounds");
895 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_BOUNDS));
896 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
897 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
898 sub.bind_ssbo("casters_id_buf", curr_casters_);
899 sub.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
900 sub.push_constant("resource_len", int(curr_casters_.size()));
901 sub.bind_resources(inst_.lights);
902 sub.dispatch(int3(
903 divide_ceil_u(std::max(curr_casters_.size(), int64_t(1)), SHADOW_BOUNDS_GROUP_SIZE),
904 1,
905 1));
907 }
908 {
909 /* Clear usage bits. Tag update from the tile-map for sun shadow clip-maps shifting. */
910 PassSimple::Sub &sub = pass.sub("Init");
911 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_INIT));
912 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
913 sub.bind_ssbo("tilemaps_clip_buf", tilemap_pool.tilemaps_clip);
914 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
915 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
916 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_data.size()));
917 /* Free unused tiles from tile-maps not used by any shadow. */
918 if (tilemap_pool.tilemaps_unused.size() > 0) {
919 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_unused);
920 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_unused.size()));
921 }
923 }
924 }
925
926 {
927 /* Mark for update all shadow pages touching an updated shadow caster. */
928 PassSimple &pass = caster_update_ps_;
929 pass.init();
930 pass.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_UPDATE));
931 pass.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
932 pass.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
933 /* Past caster transforms. */
934 if (past_casters_updated_.size() > 0) {
935 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.previous());
936 pass.bind_ssbo("resource_ids_buf", past_casters_updated_);
937 pass.dispatch(int3(past_casters_updated_.size(), 1, tilemap_pool.tilemaps_data.size()));
938 }
939 /* Current caster transforms. */
940 if (curr_casters_updated_.size() > 0) {
941 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
942 pass.bind_ssbo("resource_ids_buf", curr_casters_updated_);
943 pass.dispatch(int3(curr_casters_updated_.size(), 1, tilemap_pool.tilemaps_data.size()));
944 }
946 }
947
948 {
949 /* Mark for update all shadow pages touching a jittered transparency shadow caster. */
950 PassSimple &pass = jittered_transparent_caster_update_ps_;
951 pass.init();
952 if (jittered_transparent_casters_.size() > 0) {
953 pass.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_UPDATE));
954 pass.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
955 pass.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
956 pass.bind_ssbo("bounds_buf", &manager.bounds_buf.current());
957 pass.bind_ssbo("resource_ids_buf", jittered_transparent_casters_);
958 pass.dispatch(
959 int3(jittered_transparent_casters_.size(), 1, tilemap_pool.tilemaps_data.size()));
961 }
962 }
963
964 /* Non volume usage tagging happens between these two steps.
965 * (Setup at begin_sync) */
966
967 if (inst_.volume.needs_shadow_tagging() && !inst_.is_baking()) {
968 PassMain::Sub &sub = tilemap_usage_ps_.sub("World Volume");
969 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_TAG_USAGE_VOLUME));
970 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
971 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
972 sub.bind_resources(inst_.uniform_data);
973 sub.bind_resources(inst_.hiz_buffer.front);
974 sub.bind_resources(inst_.sampling);
975 sub.bind_resources(inst_.lights);
976 sub.bind_resources(inst_.volume.properties);
977 sub.bind_resources(inst_.volume.result);
979 sub.dispatch(math::divide_ceil(inst_.volume.grid_size(), int3(VOLUME_GROUP_SIZE)));
980 }
981
982 {
983 PassSimple &pass = tilemap_update_ps_;
984 pass.init();
985 {
986 /* Mark tiles that are redundant in the mipmap chain as unused. */
987 PassSimple::Sub &sub = pass.sub("MaskLod");
988 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_MASK));
989 sub.push_constant("max_view_per_tilemap", &max_view_per_tilemap_);
990 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
991 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
992 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_data.size()));
994 }
995 {
996 /* Free unused pages & Reclaim cached pages. */
997 PassSimple::Sub &sub = pass.sub("Free");
998 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_FREE));
999 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1000 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
1001 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1002 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1003 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1004 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_data.size()));
1005 /* Free unused tiles from tile-maps not used by any shadow. */
1006 if (tilemap_pool.tilemaps_unused.size() > 0) {
1007 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_unused);
1008 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_unused.size()));
1009 }
1011 }
1012 {
1013 /* De-fragment the free page heap after cache reuse phase which can leave hole. */
1014 PassSimple::Sub &sub = pass.sub("Defrag");
1015 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_DEFRAG));
1016 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1017 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1018 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1019 sub.bind_ssbo("statistics_buf", statistics_buf_.current());
1020 sub.bind_ssbo("clear_dispatch_buf", clear_dispatch_buf_);
1021 sub.bind_ssbo("tile_draw_buf", tile_draw_buf_);
1022 sub.dispatch(int3(1, 1, 1));
1024 }
1025 {
1026 /* Assign pages to tiles that have been marked as used but possess no page. */
1027 PassSimple::Sub &sub = pass.sub("AllocatePages");
1028 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_ALLOCATE));
1029 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1030 sub.bind_ssbo("tiles_buf", tilemap_pool.tiles_data);
1031 sub.bind_ssbo("statistics_buf", statistics_buf_.current());
1032 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1033 sub.bind_ssbo("pages_free_buf", pages_free_data_);
1034 sub.bind_ssbo("pages_cached_buf", pages_cached_data_);
1035 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_data.size()));
1037 }
1038 {
1039 /* Convert the unordered tiles into a texture used during shading. Creates views. */
1040 PassSimple::Sub &sub = pass.sub("Finalize");
1041 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_FINALIZE));
1042 sub.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
1043 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1044 sub.bind_ssbo("pages_infos_buf", &pages_infos_data_);
1045 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1046 sub.bind_ssbo("view_infos_buf", &shadow_multi_view_.matrices_ubo_get());
1047 sub.bind_ssbo("render_view_buf", &render_view_buf_);
1048 sub.bind_ssbo("tilemaps_clip_buf", &tilemap_pool.tilemaps_clip);
1049 sub.bind_image("tilemaps_img", &tilemap_pool.tilemap_tx);
1050 sub.dispatch(int3(1, 1, tilemap_pool.tilemaps_data.size()));
1053 }
1054 {
1055 /* Convert the unordered tiles into a texture used during shading. Creates views. */
1056 PassSimple::Sub &sub = pass.sub("RenderMap");
1057 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_RENDERMAP));
1058 sub.bind_ssbo("statistics_buf", &statistics_buf_.current());
1059 sub.bind_ssbo("render_view_buf", &render_view_buf_);
1060 sub.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1061 sub.bind_ssbo("clear_dispatch_buf", &clear_dispatch_buf_);
1062 sub.bind_ssbo("tile_draw_buf", &tile_draw_buf_);
1063 sub.bind_ssbo("dst_coord_buf", &dst_coord_buf_);
1064 sub.bind_ssbo("src_coord_buf", &src_coord_buf_);
1065 sub.bind_ssbo("render_map_buf", &render_map_buf_);
1066 sub.dispatch(int3(1, 1, SHADOW_VIEW_MAX));
1068 }
1069 {
1070 /* Amend tilemap_tx content to support clipmap LODs. */
1071 PassSimple::Sub &sub = pass.sub("Amend");
1072 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_TILEMAP_AMEND));
1073 sub.bind_image("tilemaps_img", tilemap_pool.tilemap_tx);
1074 sub.bind_ssbo("tilemaps_buf", tilemap_pool.tilemaps_data);
1075 sub.bind_resources(inst_.lights);
1076 sub.dispatch(int3(1));
1078 }
1079
1080 /* NOTE: We do not need to run the clear pass when using the TBDR update variant, as tiles
1081 * will be fully cleared as part of the shadow raster step. */
1084 PassSimple::Sub &sub = pass.sub("RenderClear");
1085 sub.framebuffer_set(&render_fb_);
1087 sub.shader_set(inst_.shaders.static_shader_get(SHADOW_PAGE_CLEAR));
1088 sub.bind_ssbo("pages_infos_buf", pages_infos_data_);
1089 sub.bind_ssbo("dst_coord_buf", dst_coord_buf_);
1090 sub.bind_image("shadow_atlas_img", atlas_tx_);
1091 sub.dispatch(clear_dispatch_buf_);
1093 }
1094 }
1095 }
1096
1098}
1099
1101{
1102 if (!ELEM(inst_.debug_mode,
1107 {
1108 return;
1109 }
1110
1111 /* Init but not filled if no active object. */
1112 debug_draw_ps_.init();
1113
1114 Object *object_active = inst_.draw_ctx->obact;
1115 if (object_active == nullptr) {
1116 return;
1117 }
1118
1119 ObjectKey object_key(ObjectRef(DEG_get_original(object_active)));
1120
1121 if (inst_.lights.light_map_.contains(object_key) == false) {
1122 return;
1123 }
1124
1125 Light &light = inst_.lights.light_map_.lookup(object_key);
1126
1127 if (light.tilemap_index >= SHADOW_MAX_TILEMAP) {
1128 return;
1129 }
1130
1131 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | inst_.film.depth.test_state |
1133
1134 debug_draw_ps_.state_set(state);
1135 debug_draw_ps_.shader_set(inst_.shaders.static_shader_get(SHADOW_DEBUG));
1136 debug_draw_ps_.push_constant("debug_mode", int(inst_.debug_mode));
1137 debug_draw_ps_.push_constant("debug_tilemap_index", light.tilemap_index);
1138 debug_draw_ps_.bind_ssbo("tilemaps_buf", &tilemap_pool.tilemaps_data);
1139 debug_draw_ps_.bind_ssbo("tiles_buf", &tilemap_pool.tiles_data);
1140 debug_draw_ps_.bind_resources(inst_.uniform_data);
1141 debug_draw_ps_.bind_resources(inst_.hiz_buffer.front);
1142 debug_draw_ps_.bind_resources(inst_.lights);
1143 debug_draw_ps_.bind_resources(inst_.shadows);
1144 debug_draw_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
1145}
1146
1148 bool is_perspective,
1149 const int2 &extent)
1150{
1151 float min_dim = float(min_ii(extent.x, extent.y));
1152 float3 p0 = float3(-1.0f, -1.0f, 0.0f);
1153 float3 p1 = float3(float2(min_dim / extent) * 2.0f - 1.0f, 0.0f);
1154 p0 = math::project_point(wininv, p0);
1155 p1 = math::project_point(wininv, p1);
1156 /* Compute radius at unit plane from the camera. This is NOT the perspective division. */
1157 if (is_perspective) {
1158 p0 = p0 / p0.z;
1159 p1 = p1 / p1.z;
1160 }
1161 return math::distance(p0, p1) / min_dim;
1162}
1163
1164bool ShadowModule::shadow_update_finished(int loop_count)
1165{
1166 if (loop_count >= (SHADOW_MAX_TILEMAP * SHADOW_TILEMAP_LOD) / SHADOW_VIEW_MAX) {
1167 /* We have reach the maximum theoretical number of updates.
1168 * This can indicate a problem in the statistic buffer read-back or update tagging. */
1169 inst_.info_append_i18n("Error: Reached max shadow updates.");
1170 return true;
1171 }
1172
1173 if (!inst_.is_image_render && !inst_.is_light_bake) {
1174 /* For viewport, only run the shadow update once per redraw.
1175 * This avoids the stall from the read-back and freezes from long shadow update. */
1176 return true;
1177 }
1178
1179 int max_updated_view_count = tilemap_pool.tilemaps_data.size() * SHADOW_TILEMAP_LOD;
1180 if (max_updated_view_count <= SHADOW_VIEW_MAX) {
1181 /* There is enough shadow views to cover all tile-map updates.
1182 * No read-back needed as it is guaranteed that all of them will be updated. */
1183 return true;
1184 }
1185
1186 /* Read back and check if there is still tile-map to update. */
1187 statistics_buf_.current().async_flush_to_host();
1188 statistics_buf_.current().read();
1189 ShadowStatistics stats = statistics_buf_.current();
1190
1191 if (stats.page_used_count > shadow_page_len_) {
1192 inst_.info_append_i18n(
1193 "Error: Shadow buffer full, may result in missing shadows and lower "
1194 "performance. ({} / {})",
1195 stats.page_used_count,
1196 shadow_page_len_);
1197 }
1198
1199 /* Rendering is finished if we rendered all the remaining pages. */
1200 return stats.view_needed_count <= SHADOW_VIEW_MAX;
1201}
1202
1203int ShadowModule::max_view_per_tilemap()
1204{
1205 if (inst_.is_image_render) {
1206 /* No need to limit updates per lights as we ensure all lights levels will be rendered.
1207 * is_image_render. */
1208 return SHADOW_TILEMAP_LOD;
1209 }
1210 /* For now very simple heuristic. Can be improved later by taking into consideration how many
1211 * tile-maps are updating, but we cannot know the ones updated by casters. */
1212 int potential_view_count = 0;
1213 for (auto i : IndexRange(tilemap_pool.tilemaps_data.size())) {
1214 if (tilemap_pool.tilemaps_data[i].projection_type == SHADOW_PROJECTION_CUBEFACE) {
1215 potential_view_count += SHADOW_TILEMAP_LOD;
1216 }
1217 else {
1218 potential_view_count += 1;
1219 }
1220 }
1221 int max_view_count = divide_ceil_u(SHADOW_VIEW_MAX, math::max(potential_view_count, 1));
1222 /* For viewport interactivity, have a hard maximum. This allows smoother experience. */
1223 if (inst_.is_transforming || inst_.is_navigating) {
1224 max_view_count = math::min(2, max_view_count);
1225 }
1226 /* For animation playback, we always want the maximum performance. */
1227 if (inst_.is_playback) {
1228 max_view_count = math::min(1, max_view_count);
1229 }
1230
1231 return max_view_count;
1232}
1233
1234void ShadowModule::ShadowView::compute_visibility(ObjectBoundsBuf &bounds,
1235 ObjectInfosBuf &infos,
1236 uint resource_len,
1237 bool /*debug_freeze*/)
1238{
1239 GPU_debug_group_begin("View.compute_visibility");
1240
1241 uint word_per_draw = this->visibility_word_per_draw();
1242 /* Switch between tightly packed and set of whole word per instance. */
1243 uint words_len = (view_len_ == 1) ? divide_ceil_u(resource_len, 32) :
1244 resource_len * word_per_draw;
1245 words_len = ceil_to_multiple_u(max_ii(1, words_len), 4);
1246 /* TODO(fclem): Resize to nearest pow2 to reduce fragmentation. */
1247 visibility_buf_.resize(words_len);
1248
1249 const uint32_t data = 0xFFFFFFFFu;
1251
1252 if (do_visibility_) {
1253 GPUShader *shader = inst_.shaders.static_shader_get(SHADOW_VIEW_VISIBILITY);
1254 GPU_shader_bind(shader);
1255 GPU_shader_uniform_1i(shader, "resource_len", resource_len);
1256 GPU_shader_uniform_1i(shader, "view_len", view_len_);
1257 GPU_shader_uniform_1i(shader, "visibility_word_per_draw", word_per_draw);
1258 GPU_storagebuf_bind(bounds, GPU_shader_get_ssbo_binding(shader, "bounds_buf"));
1260 GPU_storagebuf_bind(render_view_buf_, GPU_shader_get_ssbo_binding(shader, "render_view_buf"));
1264 GPU_compute_dispatch(shader, divide_ceil_u(resource_len, DRW_VISIBILITY_GROUP_SIZE), 1, 1);
1266 }
1267
1269}
1270
1272{
1273 if (enabled_ == false) {
1274 /* All lights have been tagged to have no shadow. */
1275 return;
1276 }
1277
1278 input_depth_extent_ = extent;
1279
1280 GPUFrameBuffer *prev_fb = GPU_framebuffer_active_get();
1281
1282 dispatch_depth_scan_size_ = int3(math::divide_ceil(extent, int2(SHADOW_DEPTH_SCAN_GROUP_SIZE)),
1283 1);
1284 max_view_per_tilemap_ = max_view_per_tilemap();
1285
1286 data_.film_pixel_radius = screen_pixel_radius(view.wininv(), view.is_persp(), extent);
1287 inst_.uniform_data.push_update();
1288
1289 usage_tag_fb_resolution_ = math::divide_ceil(extent, int2(std::exp2(usage_tag_fb_lod_)));
1290 usage_tag_fb.ensure(usage_tag_fb_resolution_);
1291
1293 int2 fb_size = int2(SHADOW_TILEMAP_RES * shadow_page_size_);
1294 int fb_layers = SHADOW_VIEW_MAX;
1295
1297 /* Create attachment-less framebuffer. */
1298 shadow_depth_fb_tx_.free();
1299 shadow_depth_accum_tx_.free();
1300 render_fb_.ensure(fb_size);
1301 }
1303 /* Create memoryless depth attachment for on-tile surface depth accumulation. */
1304 shadow_depth_fb_tx_.ensure_2d_array(GPU_DEPTH_COMPONENT32F, fb_size, fb_layers, usage);
1305 shadow_depth_accum_tx_.ensure_2d_array(GPU_R32F, fb_size, fb_layers, usage);
1306 render_fb_.ensure(GPU_ATTACHMENT_TEXTURE(shadow_depth_fb_tx_),
1307 GPU_ATTACHMENT_TEXTURE(shadow_depth_accum_tx_));
1308 }
1309 else {
1311 }
1312
1313 inst_.hiz_buffer.update();
1314
1315 int loop_count = 0;
1316 do {
1317 GPU_debug_group_begin("Shadow");
1318 {
1319 GPU_uniformbuf_clear_to_zero(shadow_multi_view_.matrices_ubo_get());
1320
1321 inst_.manager->submit(tilemap_setup_ps_, view);
1322 if (assign_if_different(update_casters_, false)) {
1323 /* Run caster update only once. */
1324 /* TODO(fclem): There is an optimization opportunity here where we can
1325 * test casters only against the static tile-maps instead of all of them. */
1326 inst_.manager->submit(caster_update_ps_, view);
1327 }
1328 if (loop_count == 0) {
1329 inst_.manager->submit(jittered_transparent_caster_update_ps_, view);
1330 }
1331 inst_.manager->submit(tilemap_usage_ps_, view);
1332 inst_.manager->submit(tilemap_update_ps_, view);
1333
1334 shadow_multi_view_.compute_procedural_bounds();
1335
1336 statistics_buf_.current().async_flush_to_host();
1337
1338 /* Isolate shadow update into its own command buffer.
1339 * If parameter buffer exceeds limits, then other work will not be impacted. */
1340 bool use_flush = (shadow_technique == ShadowTechnique::TILE_COPY) &&
1342 /* Flush every loop as these passes are very heavy. */
1343 use_flush |= loop_count != 0;
1344
1345 if (use_flush) {
1346 GPU_flush();
1347 }
1348
1349 /* TODO(fclem): Move all of this to the draw::PassMain. */
1350 if (shadow_depth_fb_tx_.is_valid() && shadow_depth_accum_tx_.is_valid()) {
1352 render_fb_,
1353 {
1354 /* Depth is cleared to 0 for TBDR optimization. */
1355 {GPU_LOADACTION_CLEAR, GPU_STOREACTION_DONT_CARE, {0.0f, 0.0f, 0.0f, 0.0f}},
1359 });
1360 }
1361 else if (shadow_depth_fb_tx_.is_valid()) {
1362 GPU_framebuffer_bind_ex(render_fb_,
1363 {
1367 });
1368 }
1369 else {
1370 GPU_framebuffer_bind(render_fb_);
1371 }
1372
1374 reinterpret_cast<int(*)[4]>(multi_viewports_.data()));
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
1395void ShadowModule::debug_draw(View &view, GPUFrameBuffer *view_fb)
1396{
1397 if (!ELEM(inst_.debug_mode,
1402 {
1403 return;
1404 }
1405
1406 switch (inst_.debug_mode) {
1408 inst_.info_append("Debug Mode: Shadow Tilemap");
1409 break;
1411 inst_.info_append("Debug Mode: Shadow Values");
1412 break;
1414 inst_.info_append("Debug Mode: Shadow Tile Random Color");
1415 break;
1417 inst_.info_append("Debug Mode: Shadow Tilemap Random Color");
1418 break;
1419 default:
1420 break;
1421 }
1422
1423 inst_.hiz_buffer.update();
1424
1425 GPU_framebuffer_bind(view_fb);
1426 inst_.manager->submit(debug_draw_ps_, view);
1427}
1428
1430
1431} // namespace blender::eevee
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE uint ceil_to_multiple_u(uint a, uint b)
MINLINE float max_ff(float a, float b)
MINLINE int min_ii(int a, int b)
MINLINE uint divide_ceil_u(uint a, uint b)
MINLINE int square_i(int a)
MINLINE int max_ii(int a, int b)
MINLINE uint64_t divide_ceil_ul(uint64_t a, uint64_t b)
MINLINE int clamp_i(int value, int min, int max)
MINLINE float int_as_float(int i)
bool equals_m4m4(const float mat1[4][4], const float mat2[4][4])
unsigned int uint
#define UNUSED_VARS(...)
#define ELEM(...)
T * DEG_get_original(T *id)
@ OB_HIDE_SHADOW
@ SCE_EEVEE_SHADOW_ENABLED
@ SCE_EEVEE_SHADOW_JITTERED_VIEWPORT
static AppView * view
blender::gpu::Batch * GPU_batch_unit_cube() ATTR_WARN_UNUSED_RESULT
@ GPU_LOADACTION_CLEAR
@ GPU_STOREACTION_DONT_CARE
void GPU_compute_dispatch(GPUShader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
eGPUBackendType GPU_backend_get_type()
void GPU_debug_group_end()
Definition gpu_debug.cc:33
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:22
void GPU_framebuffer_multi_viewports_set(GPUFrameBuffer *gpu_fb, const int viewport_rects[GPU_MAX_VIEWPORTS][4])
#define GPU_ATTACHMENT_TEXTURE(_texture)
GPUFrameBuffer * GPU_framebuffer_active_get()
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
#define GPU_framebuffer_bind_ex(_fb,...)
@ GPU_ARCHITECTURE_TBDR
GPUArchitectureType GPU_platform_architecture()
@ GPU_PRIM_TRIS
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
int GPU_shader_get_ssbo_binding(GPUShader *shader, const char *name)
int32_t bool32_t
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:385
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_UNIFORM
Definition GPU_state.hh:54
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:35
void GPU_storagebuf_bind(GPUStorageBuf *ssbo, int slot)
void GPU_storagebuf_clear(GPUStorageBuf *ssbo, uint32_t clear_value)
eGPUTextureUsage
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_MEMORYLESS
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_TEXTURE_USAGE_ATOMIC
@ GPU_R32F
@ GPU_R32UI
@ GPU_DEPTH_COMPONENT32F
void GPU_uniformbuf_clear_to_zero(GPUUniformBuf *ubo)
void GPU_uniformbuf_bind(GPUUniformBuf *ubo, int slot)
BMesh const char void * data
long long int int64_t
constexpr int64_t one_after_last() const
constexpr IndexRange shift(int64_t n) const
constexpr IndexRange intersect(IndexRange other) const
constexpr int64_t start() const
SwapChain< ObjectBoundsBuf, 2 > bounds_buf
UniformArrayBuffer< ViewCullingData, DRW_VIEW_MAX > culling_
Definition draw_view.hh:50
VisibilityBuf visibility_buf_
Definition draw_view.hh:55
UniformArrayBuffer< ViewMatrices, DRW_VIEW_MAX > data_
Definition draw_view.hh:49
int visibility_word_per_draw() const
Definition draw_view.hh:167
void bind_texture(const char *name, GPUTexture *texture, GPUSamplerState state=sampler_auto)
void bind_resources(U &resources)
Definition draw_pass.hh:440
void bind_image(const char *name, GPUTexture *image)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:681
void dispatch(int group_len)
Definition draw_pass.hh:994
void barrier(eGPUBarrier type)
void state_set(DRWState state, int clip_plane_count=0)
void framebuffer_set(GPUFrameBuffer **framebuffer)
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, GPUStorageBuf *buffer)
void shader_set(GPUShader *shader)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:490
bool is_perspective() const
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 debug_draw(View &view, GPUFrameBuffer *view_fb)
static float screen_pixel_radius(const float4x4 &wininv, bool is_perspective, const int2 &extent)
void set_view(View &view, int2 extent)
static ShadowTechnique shadow_technique
void sync_object(const Object *ob, const ObjectHandle &handle, const ResourceHandle &resource_handle, bool is_alpha_blend, bool has_transparent_shadows)
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
DRWState
Definition draw_state.hh:25
@ DRW_STATE_CULL_FRONT
Definition draw_state.hh:44
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:29
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:36
@ DRW_STATE_BLEND_CUSTOM
Definition draw_state.hh:63
@ DRW_STATE_WRITE_STENCIL
Definition draw_state.hh:32
#define SHADOW_TILEMAP_RES
#define SHADOW_PAGE_RES
#define SHADOW_MAX_TILEMAP
#define SHADOW_PAGE_PER_ROW
#define SHADOW_CLIPMAP_GROUP_SIZE
#define SHADOW_VIEW_MAX
#define SHADOW_DEPTH_SCAN_GROUP_SIZE
#define VOLUME_GROUP_SIZE
#define SHADOW_MAX_RAY
#define SHADOW_TILEDATA_PER_TILEMAP
#define SHADOW_MAX_STEP
#define SHADOW_BOUNDS_GROUP_SIZE
#define SHADOW_PAGE_PER_COL
#define SHADOW_PAGE_PER_LAYER
#define SHADOW_MAX_PAGE
#define SHADOW_TILEMAP_LOD
#define LIGHT_NO_SHADOW
#define round
VecBase< float, D > normalize(VecOp< float, D >) RET
#define log2
#define abs
#define floor
#define ceil
static ulong state[N]
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
static constexpr const float shadow_face_mat[6][3][3]
static int2 shadow_cascade_grid_offset(int2 base_offset, int level_relative)
static int light_local_tilemap_count(LightData light)
static float3 transform_direction_transposed(Transform t, float3 direction)
draw::StorageArrayBuffer< Surfel, 64 > SurfelBuf
draw::StorageBuffer< CaptureInfoData > CaptureInfoBuf
static uint shadow_page_pack(uint3 page)
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)
T distance(const T &a, const T &b)
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)
VectorT project_point(const MatT &mat, const VectorT &point)
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
VecBase< uint32_t, 4 > uint4
MatBase< float, 2, 3 > float2x3
VecBase< uint32_t, 3 > uint3
bool assign_if_different(T &old_value, T new_value)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
static struct PyModuleDef module
Definition python.cpp:796
#define FLT_MAX
Definition stdcycles.h:14
short type
short visibility_flag
float shadow_resolution_scale
struct SceneEEVEE eevee
const c_style_mat & ptr() const
const MatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view() const
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
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_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
int x
Definition types_int2.h:13
int y
Definition types_int2.h:13
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251