Blender V5.3
eevee_shadow_tilemap_init.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#pragma once
14
15#include "eevee_defines.hh"
19
20namespace eevee::shadow {
21
23 [[storage(0, read_write)]] ShadowTileMapData (&tilemaps_buf)[];
24 [[storage(1, read_write)]] uint (&tiles_buf)[];
25 [[storage(2, read_write)]] ShadowTileMapClip (&tilemaps_clip_buf)[];
26 [[storage(4, read_write)]] uint2 (&pages_cached_buf)[];
27
28 /* When rendering shadows in a loop, we only tag for usage once and setup multiple times.
29 * In this case `reset_used_flag` is set to false after the first iteration. */
31
33
35 {
36 if (flag_test(tile, SHADOW_IS_RENDERED)) {
37 tile &= ~(SHADOW_DO_UPDATE | SHADOW_IS_RENDERED);
38 }
39 if (do_update) {
40 tile |= SHADOW_DO_UPDATE;
41 }
42 if (reset_used_flag) {
43 tile &= ~SHADOW_IS_USED;
44 }
45 return tile;
46 }
47};
48
49[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]]
51 [[global_invocation_id]] const uint3 global_id,
52 [[local_invocation_index]] const uint local_index)
53{
54 uint tilemap_index = global_id.z;
55 ShadowTileMapData tilemap = srt.tilemaps_buf[tilemap_index];
56
57 barrier();
58
59 if (local_index == 0u) {
60 /* Reset shift to not tag for update more than once per sync cycle. */
61 srt.tilemaps_buf[tilemap_index].grid_shift = int2(0);
62 srt.tilemaps_buf[tilemap_index].is_dirty = false;
63
64 srt.directional_range_changed = 0;
65
66 const int clip_index = tilemap.clip_data_index;
67 if (clip_index == -1) {
68 /* NOP. This is the case for unused tile-maps that are getting pushed to the free heap. */
69 }
70 else if (tilemap.projection_type != SHADOW_PROJECTION_CUBEFACE) {
71 ShadowTileMapClip &clip_data = srt.tilemaps_clip_buf[clip_index];
72 float clip_near_new = orderedIntBitsToFloat(clip_data.clip_near);
73 float clip_far_new = orderedIntBitsToFloat(clip_data.clip_far);
74 bool near_changed = clip_near_new != clip_data.clip_near_stored;
75 bool far_changed = clip_far_new != clip_data.clip_far_stored;
76 srt.directional_range_changed = int(near_changed || far_changed);
77 /* NOTE(fclem): This assumes clip near/far are computed each time the initial phase runs. */
78 clip_data.clip_near_stored = clip_near_new;
79 clip_data.clip_far_stored = clip_far_new;
80 /* Reset for next update. */
81 clip_data.clip_near = floatBitsToOrderedInt(FLT_MAX);
82 clip_data.clip_far = floatBitsToOrderedInt(-FLT_MAX);
83 }
84 else {
85 /* For cube-faces, simply use the light near and far distances. */
86 ShadowTileMapClip &clip_data = srt.tilemaps_clip_buf[clip_index];
87 clip_data.clip_near_stored = tilemap.clip_near;
88 clip_data.clip_far_stored = tilemap.clip_far;
89 }
90 }
91
92 barrier();
93
94 int2 tile_co = int2(global_id.xy);
95 int2 tile_shifted = tile_co +
96 clamp(tilemap.is_dirty ? int2(SHADOW_TILEMAP_RES) : tilemap.grid_shift,
97 int2(-SHADOW_TILEMAP_RES),
98 int2(SHADOW_TILEMAP_RES));
99 int2 tile_wrapped = int2((int2(SHADOW_TILEMAP_RES) + tile_shifted) % SHADOW_TILEMAP_RES);
100
101 /* If this tile was shifted in and contains old information, update it.
102 * Note that cube-map always shift all tiles on update. */
103 bool do_update = !in_range_inclusive(tile_shifted, int2(0), int2(SHADOW_TILEMAP_RES - 1));
104
105 /* TODO(fclem): Might be better to resize the depth stored instead of a full render update. */
106 if (srt.directional_range_changed != 0) {
107 do_update = true;
108 }
109
110 int lod_max = (tilemap.projection_type == SHADOW_PROJECTION_CUBEFACE) ? SHADOW_TILEMAP_LOD : 0;
111 uint lod_size = uint(SHADOW_TILEMAP_RES);
112 for (int lod = 0; lod <= lod_max; lod++, lod_size >>= 1u) {
113 bool thread_active = all(lessThan(tile_co, int2(int(lod_size))));
115 int tile_load = shadow_tile_offset(uint2(tile_wrapped), tilemap.tiles_index, lod);
116 if (thread_active) {
117 tile = srt.init_tile_data(srt.tiles_buf[tile_load], do_update);
118 }
119
120 /* Uniform control flow for barrier. Needed to avoid race condition on shifted loads. */
121 barrier();
122
123 if (thread_active) {
124 int tile_store = shadow_tile_offset(uint2(tile_co), tilemap.tiles_index, lod);
125 if ((tile_load != tile_store) && flag_test(tile, SHADOW_IS_CACHED)) {
126 /* Inlining of shadow_page_cache_update_tile_ref to avoid buffer dependencies. */
127 srt.pages_cached_buf[shadow_tile_unpack(tile).cache_index].y = uint(tile_store);
128 }
129 srt.tiles_buf[tile_store] = tile;
130 }
131 }
132}
133
135
136} // namespace eevee::shadow
#define ShadowTileDataPacked
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
#define FLT_MAX
#define shared
#define local_invocation_index
#define storage(slot, qualifiers)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
constexpr T clamp(T, U, U) RET
bool flag_test(uint flag, uint val)
#define in_range_inclusive(val, min_v, max_v)
int floatBitsToOrderedInt(float value)
float orderedIntBitsToFloat(int int_value)
const ccl_global KernelWorkTile * tile
void tilemap_init_main(TilemapInit &srt, const uint3 global_id, const uint local_index)
PipelineCompute tilemap_init(tilemap_init_main)
ShadowTileDataPacked init_tile_data(ShadowTileDataPacked tile, bool do_update)