Blender V5.3
eevee_shadow.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "BLI_pool.hh"
14#include "BLI_vector.hh"
15
16#include "GPU_batch.hh"
17
18#include "eevee_camera.hh"
19#include "eevee_material.hh"
21#include "eevee_sync.hh"
23
24namespace blender::eevee {
25
26class Instance;
27class ShadowModule;
28class ShadowPipeline;
29struct Light;
30
31/* To be applied after view matrix. Follow same order as eCubeFace. */
32constexpr static const float shadow_face_mat[6][3][3] = {
33 {{+1, +0, +0}, {+0, +1, +0}, {+0, +0, +1}}, /* Z_NEG */
34 {{+0, +0, -1}, {-1, +0, +0}, {+0, +1, +0}}, /* X_POS */
35 {{+0, +0, +1}, {+1, +0, +0}, {+0, +1, +0}}, /* X_NEG */
36 {{+1, +0, +0}, {+0, +0, -1}, {+0, +1, +0}}, /* Y_POS */
37 {{-1, +0, +0}, {+0, +0, +1}, {+0, +1, +0}}, /* Y_NEG */
38 {{+1, +0, +0}, {+0, -1, +0}, {+0, +0, -1}}, /* Z_POS */
39};
40
41/* Converts to [-SHADOW_TILEMAP_RES / 2..SHADOW_TILEMAP_RES / 2] for XY and [0..1] for Z. */
42constexpr static const float shadow_clipmap_scale_mat[4][4] = {{SHADOW_TILEMAP_RES / 2, 0, 0, 0},
43 {0, SHADOW_TILEMAP_RES / 2, 0, 0},
44 {0, 0, 0.5, 0},
45 {0, 0, 0.5, 1}};
46
55
56/* -------------------------------------------------------------------- */
64
66 static constexpr int64_t tile_map_resolution = SHADOW_TILEMAP_RES;
68
75
76 public:
77 ShadowTileMap(int tiles_index_) : ShadowTileMapData{}
78 {
79 tiles_index = tiles_index_;
80 /* For now just the same index. */
81 clip_data_index = tiles_index_ / SHADOW_TILEDATA_PER_TILEMAP;
82 /* Avoid uninitialized data. */
83 this->grid_offset = int2(0);
84 this->grid_shift = int2(0);
85 this->set_dirty();
86 }
87
88 void sync_orthographic(const float4x4 &object_mat_,
89 int2 origin_offset,
90 int clipmap_level,
91 eShadowProjectionType projection_type_,
92 uint2 shadow_set_membership_ = ~uint2(0));
93
94 void sync_cubeface(eLightType light_type_,
95 const float4x4 &object_mat,
96 float near,
97 float far,
98 eCubeFace face,
99 uint2 shadow_set_membership_ = ~uint2(0));
100
101 void debug_draw() const;
102
104 {
105 is_dirty = true;
106 }
107
109 {
110 is_dirty = false;
111 }
112};
113
124 public:
126 static constexpr int64_t maps_per_row = SHADOW_TILEMAP_PER_ROW;
127
141 Texture tilemap_tx = {"tilemap_tx"};
144
145 public:
147
149
153 void release(Span<ShadowTileMap *> free_list);
154
155 void end_sync(ShadowModule &module);
156};
157
159
160/* -------------------------------------------------------------------- */
164
165/* Can be either a shadow caster or a shadow receiver. */
170
172
173/* -------------------------------------------------------------------- */
178
179class ShadowPunctual;
181
183 friend ShadowPunctual;
184 friend ShadowDirectional;
185 friend ShadowPipeline;
186 friend ShadowTileMapPool;
187
188 public:
190 ShadowTileMapPool tilemap_pool;
191
194
195 private:
196 Instance &inst_;
197
198 ShadowSceneData &data_;
199
202
203 /* Used to call caster_update_ps_ only once per sync (Initialized on begin_sync). */
204 bool update_casters_ = false;
205
206 /* When rendering shadows in a loop, we only tag for usage once and setup multiple times.
207 * In this case `run_tagging_` is set to false after the first iteration. */
208 bool32_t run_tagging_ = false;
209
210 /* Number of shadow view needed until we have finished the shadow updates. Render only. */
211 int needed_views_ = 0;
212
213 /* -------------------------------------------------------------------- */
216
217 PassSimple tilemap_setup_ps_ = {"TilemapSetup"};
218 PassMain tilemap_usage_ps_ = {"TagUsage"};
219 PassSimple tilemap_update_ps_ = {"TilemapUpdate"};
220
221 PassMain::Sub *tilemap_usage_transparent_ps_ = nullptr;
222 gpu::Batch *box_batch_ = nullptr;
223 /* Source texture for depth buffer analysis. */
224 gpu::Texture *src_depth_tx_ = nullptr;
225
226 Framebuffer usage_tag_fb;
227
228 PassSimple caster_update_ps_ = {"CasterUpdate"};
229 PassSimple jittered_transparent_caster_update_ps_ = {"TransparentCasterUpdate"};
230 PassSimple update_propagate_ps_ = {"CasterUpdatePropagate"};
232 StorageVectorBuffer<uint, 128> past_casters_updated_ = {"PastCastersUpdated"};
233 StorageVectorBuffer<uint, 128> curr_casters_updated_ = {"CurrCastersUpdated"};
234 StorageVectorBuffer<uint, 128> jittered_transparent_casters_ = {"JitteredTransparentCasters"};
236 StorageVectorBuffer<uint, 128> curr_casters_ = {"CurrCasters"};
238 Framebuffer update_tag_fb_ = {"update_tag_fb"};
239
241 DispatchIndirectBuf clear_dispatch_buf_ = {"clear_dispatch_buf"};
243 DrawIndirectBuf tile_draw_buf_ = {"tile_draw_buf"};
245 StorageArrayBuffer<uint, SHADOW_RENDER_MAP_SIZE, true> dst_coord_buf_ = {"dst_coord_buf"};
247 StorageArrayBuffer<uint, SHADOW_RENDER_MAP_SIZE, true> src_coord_buf_ = {"src_coord_buf"};
249 StorageArrayBuffer<uint, SHADOW_RENDER_MAP_SIZE, true> render_map_buf_ = {"render_map_buf"};
251 ShadowRenderViewBuf render_view_buf_ = {"render_view_buf"};
252
253 int3 dispatch_depth_scan_size_;
254 int2 usage_tag_fb_resolution_;
255 int usage_tag_fb_lod_ = 5;
256 int max_view_per_tilemap_ = 1;
257 int2 input_depth_extent_;
258
259 /* Statistics that are read back to CPU after a few frame (to avoid stall). */
260 SwapChain<ShadowStatisticsBuf, 5> statistics_buf_;
261
263
264 /* -------------------------------------------------------------------- */
267
268 static constexpr gpu::TextureFormat atlas_type = gpu::TextureFormat::UINT_32;
270 Texture atlas_tx_ = {"shadow_atlas_tx_"};
271
273 ShadowPageHeapBuf pages_free_data_ = {"PagesFreeBuf"};
275 ShadowPageCacheBuf pages_cached_data_ = {"PagesCachedBuf"};
277 ShadowPagesInfoDataBuf pages_infos_data_ = {"PagesInfosBuf"};
278
279 int3 copy_dispatch_size_;
280 int3 scan_dispatch_size_;
281 int rendering_tilemap_;
282 int rendering_lod_;
283 bool do_full_update_ = true;
284
286
287 /* -------------------------------------------------------------------- */
290
291 class ShadowView : public View {
292 Instance &inst_;
293 ShadowRenderViewBuf &render_view_buf_;
294
295 public:
296 ShadowView(const char *name, Instance &inst, ShadowRenderViewBuf &render_view_buf)
297 : View(name, SHADOW_VIEW_MAX, true), inst_(inst), render_view_buf_(render_view_buf)
298 {
299 }
300
301 protected:
303 void compute_visibility(ObjectBoundsBuf &bounds,
304 ObjectInfosBuf &infos,
305 uint resource_len,
306 bool debug_freeze) override;
307 };
308
310 ShadowView shadow_multi_view_ = {"ShadowMultiView", inst_, render_view_buf_};
312 Framebuffer render_fb_ = {"shadow_write_framebuffer"};
313
315 std::array<int4, 16> multi_viewports_;
316
318
319 /* -------------------------------------------------------------------- */
322
324 PassSimple debug_draw_ps_ = {"Shadow.Debug"};
325
327
329
330 /* Render setting that reduces the LOD for every light. */
331 float global_lod_bias_ = 0.0f;
333 int shadow_page_size_ = SHADOW_PAGE_RES;
335 int shadow_page_len_ = SHADOW_MAX_TILEMAP;
337 bool enabled_ = true;
338
339 public:
340 ShadowModule(Instance &inst, ShadowSceneData &data);
341
343 {
344 GPU_BATCH_DISCARD_SAFE(box_batch_);
345 }
346
347 void init();
348
349 void begin_sync();
351 void sync_object(const ObjectHandle &ob_handle,
352 bool is_alpha_blend_or_has_offset_shadow,
353 bool has_transparent_shadows,
354 bool has_time_dependent_shadows,
355 bool has_offset_shadows);
356 void end_sync();
357
359
360 void set_view(View &view, int2 extent);
361
362 /* Update all shadow regions visible inside the view.
363 * If called multiple time for the same view, it will only do the depth buffer scanning
364 * to check any new opaque surfaces.
365 * Needs to be called after `LightModule::set_view();` and after `ShadowModule::set_view();`. */
366 void render(View &view, int2 extent);
367
368 void debug_end_sync();
369 void debug_draw(View &view, gpu::FrameBuffer *view_fb);
370
371 template<typename PassType> void bind_resources(PassType &pass)
372 {
373 pass.bind_texture(SHADOW_ATLAS_TEX_SLOT, &atlas_tx_);
375 }
376
378 {
379 return data_;
380 }
381
382 float global_lod_bias() const
383 {
384 return global_lod_bias_;
385 }
386
387 /* Set all shadows to update. To be called before `end_sync`. */
388 void reset()
389 {
390 do_full_update_ = true;
391 }
392
393 private:
394 void remove_unused();
395 bool shadow_update_finished(int loop_count);
396
398 float tilemap_pixel_radius();
399
400 /* Returns the maximum number of view per shadow projection for a single update loop. */
401 int max_view_per_tilemap();
402};
403
405
406/* -------------------------------------------------------------------- */
411
413 private:
414 ShadowModule &shadows_;
416 Vector<ShadowTileMap *> tilemaps_;
417
418 public:
419 ShadowPunctual(ShadowModule &module) : shadows_(module) {};
421 : shadows_(other.shadows_), tilemaps_(std::move(other.tilemaps_)) {};
422
424 {
425 shadows_.tilemap_pool.release(tilemaps_);
426 }
427
431 void release_excess_tilemaps(const Light &light);
432
436 void end_sync(Light &light);
437};
438
440 private:
441 ShadowModule &shadows_;
443 Vector<ShadowTileMap *> tilemaps_;
445 IndexRange levels_range = IndexRange(0);
446
447 public:
448 ShadowDirectional(ShadowModule &module) : shadows_(module) {};
450 : shadows_(other.shadows_), tilemaps_(std::move(other.tilemaps_)) {};
451
453 {
454 shadows_.tilemap_pool.release(tilemaps_);
455 }
456
460 void release_excess_tilemaps(const Light &light, const Camera &camera);
461
465 void end_sync(Light &light, const Camera &camera);
466
467 /* Return coverage of the whole tile-map in world unit. */
468 static float coverage_get(int lvl)
469 {
470 /* This function should be kept in sync with shadow_directional_level(). */
471 /* \note If we would to introduce a global scaling option it would be here. */
472 return exp2(lvl);
473 }
474
475 /* Return coverage of a single tile for a tile-map of this LOD in world unit. */
476 static float tile_size_get(int lvl)
477 {
478 return coverage_get(lvl) / SHADOW_TILEMAP_RES;
479 }
480
481 private:
482 IndexRange clipmap_level_range(const Camera &camera);
483 IndexRange cascade_level_range(const Light &light, const Camera &camera);
484
489 void cascade_tilemaps_distribution(Light &light, const Camera &camera);
490 void clipmap_tilemaps_distribution(Light &light, const Camera &camera);
491
492 void cascade_tilemaps_distribution_near_far_points(const Camera &camera,
493 const Light &light,
494 float3 &near_point,
495 float3 &far_point);
496
497 /* Choose between clip-map and cascade distribution of shadow-map precision depending on the
498 * camera projection type and bounds. */
499 static eShadowProjectionType directional_distribution_type_get(const Camera &camera);
500};
501
503
504} // namespace blender::eevee
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
int32_t bool32_t
constexpr IndexRange()=default
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
View(const char *name, int view_len=1, bool procedural=false)
Definition draw_view.hh:70
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
A running instance of the engine.
static float coverage_get(int lvl)
ShadowDirectional(ShadowModule &module)
static float tile_size_get(int lvl)
void end_sync(Light &light, const Camera &camera)
ShadowDirectional(ShadowDirectional &&other)
void release_excess_tilemaps(const Light &light, const Camera &camera)
ShadowModule(Instance &inst, ShadowSceneData &data)
ShadowTileMapPool tilemap_pool
const ShadowSceneData & get_data()
Pool< ShadowDirectional > directional_pool
void bind_resources(PassType &pass)
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)
Pool< ShadowPunctual > punctual_pool
void debug_draw(View &view, gpu::FrameBuffer *view_fb)
void render(View &view, int2 extent)
ShadowPunctual(ShadowPunctual &&other)
ShadowPunctual(ShadowModule &module)
void release_excess_tilemaps(const Light &light)
#define SHADOW_ATLAS_TEX_SLOT
#define SHADOW_TILEMAPS_TEX_SLOT
#define INT_MAX
#define exp2
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
PassType
detail::Pass< command::DrawCommandBuf > PassSimple
draw::StorageBuffer< DrawCommand, true > DrawIndirectBuf
Definition draw_pass.hh:73
detail::Pass< command::DrawMultiBuf > PassMain
StorageArrayBuffer< ObjectBounds, 128 > ObjectBoundsBuf
Definition draw_view.hh:33
draw::StorageBuffer< DispatchCommand > DispatchIndirectBuf
Definition draw_pass.hh:72
StorageArrayBuffer< ObjectInfos, 128 > ObjectInfosBuf
Definition draw_view.hh:34
draw::StorageBuffer< ShadowPagesInfoData > ShadowPagesInfoDataBuf
static constexpr const float shadow_face_mat[6][3][3]
draw::StorageArrayBuffer< ShadowTileDataPacked, SHADOW_MAX_TILE, true > ShadowTileDataBuf
draw::StorageArrayBuffer< ShadowTileMapClip, SHADOW_MAX_TILEMAP, true > ShadowTileMapClipBuf
draw::StorageArrayBuffer< uint2, SHADOW_MAX_PAGE, true > ShadowPageCacheBuf
draw::StorageArrayBuffer< ShadowRenderView, SHADOW_VIEW_MAX, true > ShadowRenderViewBuf
draw::StorageBuffer< ShadowStatistics > ShadowStatisticsBuf
draw::StorageVectorBuffer< ShadowTileMapData, SHADOW_MAX_TILEMAP > ShadowTileMapDataBuf
static constexpr const float shadow_clipmap_scale_mat[4][4]
draw::StorageVectorBuffer< uint, SHADOW_MAX_PAGE > ShadowPageHeapBuf
VecBase< uint32_t, 2 > uint2
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
BMesh const char void * data
VecBase< float, 3 > float3
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
static constexpr int64_t tiles_count
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))