Blender V5.3
eevee_shadow_page_mask.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
11
12#pragma once
13
16
17namespace eevee::shadow {
18
20
21/* Reuse the same enum values for these transient flag during the amend phase.
22 * They are never written to the tile data SSBO. */
23static constexpr uint SHADOW_TILE_AMENDED = SHADOW_IS_RENDERED;
24/* Visibility value to write back. */
25static constexpr uint SHADOW_TILE_MASKED = SHADOW_IS_ALLOCATED;
26
28{
29 return shadow_tile_offset(uint2(tile), 0, lod);
30}
31
32/* Deactivate threads that are not part of this LOD. Will only let pass threads which tile
33 * coordinate fits the given tilemap LOD. */
34bool thread_mask(int2 tile_co, int lod)
35{
36 constexpr uint lod_size = uint(SHADOW_TILEMAP_RES);
37 return all(lessThan(tile_co, int2(lod_size >> lod)));
38}
39
40struct PageMask {
41 [[shared]] uint tiles_local[SHADOW_TILEDATA_PER_TILEMAP];
45 [[storage(1, read_write)]] uint (&tiles_buf)[];
46};
47
48[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]]
50 [[resource_table]] TileMaps &tilemaps,
51 [[global_invocation_id]] const uint3 global_id,
52 [[local_invocation_index]] const uint local_tile_index)
53{
54 int2 tile_co = int2(global_id.xy);
55 uint tilemap_index = global_id.z;
56 ShadowTileMapData tilemap = tilemaps.tilemaps_buf[tilemap_index];
57
58 /* NOTE: Barriers are ok since this branch is taken by all threads. */
59 if (tilemap.projection_type == SHADOW_PROJECTION_CUBEFACE) {
60 /* Check if any page is allocated in this tilemap. Force base page if that's the case to avoid
61 * artifact during shadow tracing. */
62 if (local_tile_index == 0u) {
63 srt.force_base_page = 0u;
64 }
65 barrier();
66
67 /* Load all data to LDS. Allows us to do some modification on the flag bits and only flush to
68 * main memory the usage bit. */
69 for (int lod = 0; lod <= SHADOW_TILEMAP_LOD; lod++) {
70 if (thread_mask(tile_co, lod)) {
71 int tile_offset = shadow_tile_offset(uint2(tile_co), tilemap.tiles_index, lod);
72 ShadowTileDataPacked tile_data = srt.tiles_buf[tile_offset];
73
74 if ((tile_data & SHADOW_IS_USED) == 0) {
75 /* Do not consider this tile as going to be rendered if it is not used.
76 * Simplify checks later. This is a local modification. */
77 tile_data &= ~SHADOW_DO_UPDATE;
78 }
79 else {
80 /* Tag base level to be used. */
81 srt.force_base_page = 1u;
82 }
83 /* Clear these flags as they could contain any values. */
85
86 int tile_lds = shadow_tile_offset_lds(tile_co, lod);
87 srt.tiles_local[tile_lds] = tile_data;
88 }
89 }
90
91#if 1 /* Can be disabled for debugging. */
92 /* For each level collect the number of used (or masked) tile that are covering the tile from
93 * the level underneath. If this adds up to 4 the underneath tile is flag unused as its data
94 * is not needed for rendering.
95 *
96 * This is because 2 receivers can tag "used" the same area of the shadow-map but with
97 * different LODs. */
98 for (int lod = 1; lod <= SHADOW_TILEMAP_LOD; lod++) {
99 barrier();
100 if (thread_mask(tile_co, lod)) {
101 int2 tile_co_prev_lod = tile_co * 2;
102 int prev_lod = lod - 1;
103
104 int tile_0 = shadow_tile_offset_lds(tile_co_prev_lod + int2(0, 0), prev_lod);
105 int tile_1 = shadow_tile_offset_lds(tile_co_prev_lod + int2(1, 0), prev_lod);
106 int tile_2 = shadow_tile_offset_lds(tile_co_prev_lod + int2(0, 1), prev_lod);
107 int tile_3 = shadow_tile_offset_lds(tile_co_prev_lod + int2(1, 1), prev_lod);
108 /* Is masked if all tiles from the previous level were tagged as used. */
109 bool is_masked = ((srt.tiles_local[tile_0] & srt.tiles_local[tile_1] &
110 srt.tiles_local[tile_2] & srt.tiles_local[tile_3]) &
111 SHADOW_IS_USED) != 0;
112
113 int tile_offset = shadow_tile_offset_lds(tile_co, lod);
114 if (is_masked) {
115 /* Consider this tile occluding lower levels. Use SHADOW_IS_USED flag for that. */
116 srt.tiles_local[tile_offset] |= SHADOW_IS_USED;
117 /* Do not consider this tile when checking which tilemap level to render in next loop. */
118 srt.tiles_local[tile_offset] &= ~SHADOW_DO_UPDATE;
119 /* Tag as modified so that we can amend it inside the `tiles_buf`. */
120 srt.tiles_local[tile_offset] |= SHADOW_TILE_AMENDED;
121 /* Visibility value to write back. */
122 srt.tiles_local[tile_offset] |= SHADOW_TILE_MASKED;
123 }
124 }
125 }
126#endif
127
128#if 1 /* Can be disabled for debugging. */
129 /* Count the number of LOD level to render for this tilemap and to clamp it to a maximum number
130 * of view per tilemap.
131 * This avoid flooding the 64 view limit per redraw with ~3-4 LOD levels per tilemaps leaving
132 * some lights unshadowed.
133 * The clamped LOD levels' tiles need to be merged to the highest LOD allowed. */
134
135 /* Construct bitmask of LODs that contain tiles to render (i.e: that will request a view). */
136 if (local_tile_index == 0u) {
137 srt.levels_rendered = 0u;
138 }
139 barrier();
140 for (int lod = 0; lod <= SHADOW_TILEMAP_LOD; lod++) {
141 /* TODO(fclem): Could maybe speedup using WaveAllBitOr. */
142 if (thread_mask(tile_co, lod)) {
143 int tile_offset = shadow_tile_offset_lds(tile_co, lod);
144 if ((srt.tiles_local[tile_offset] & SHADOW_DO_UPDATE) != 0) {
145 atomicOr(srt.levels_rendered, 1u << lod);
146 }
147 }
148 }
149 barrier();
150
151 /* If there is more LODs to update than the load balancing heuristic allows. */
152 if (bitCount(srt.levels_rendered) > srt.max_view_per_tilemap) {
153 /* Find the cutoff LOD that contain tiles to render. */
154 int max_lod = findMSB(srt.levels_rendered);
155 /* Allow more than one level. */
156 for (int i = 1; i < srt.max_view_per_tilemap; i++) {
157 max_lod = findMSB(srt.levels_rendered & ~(~0u << max_lod));
158 }
159 /* NOTE: Concurrent writing of the same value to the same data. */
160 tilemaps.tilemaps_buf[tilemap_index].effective_lod_min = max_lod;
161 /* Collapse all bits to highest level. */
162 for (int lod = 0; lod < max_lod; lod++) {
163 if (thread_mask(tile_co, lod)) {
164 int tile_offset = shadow_tile_offset_lds(tile_co, lod);
165 if ((srt.tiles_local[tile_offset] & SHADOW_DO_UPDATE) != 0) {
166 /* This tile is now masked and not considered for rendering. */
167 srt.tiles_local[tile_offset] |= SHADOW_TILE_MASKED | SHADOW_TILE_AMENDED;
168 /* Note that we can have multiple thread writing to this tile. */
169 int tile_bottom_offset = shadow_tile_offset_lds(tile_co >> (max_lod - lod), max_lod);
170 /* Tag the associated tile in max_lod to be used as it contains the shadow-map area
171 * covered by this collapsed tile. */
172 atomicOr(srt.tiles_local[tile_bottom_offset], uint(SHADOW_TILE_AMENDED));
173 /* This tile could have been masked by the masking phase.
174 * Make sure the flag is unset. */
175 atomicAnd(srt.tiles_local[tile_bottom_offset], ~uint(SHADOW_TILE_MASKED));
176 }
177 }
178 }
179 }
180 else {
181 /* NOTE: Concurrent writing of the same value to the same data. */
182 tilemaps.tilemaps_buf[tilemap_index].effective_lod_min = 0;
183 }
184#else
185 /* NOTE: Concurrent writing of the same value to the same data. */
186 tilemaps.tilemaps_buf[tilemap_index].effective_lod_min = 0;
187#endif
188
189 barrier();
190
191#if 1 /* Can be disabled for debugging. */
192 if (local_tile_index == 0u) {
193 /* WATCH: To be kept in sync with `max_view_per_tilemap()` function. */
194 bool is_render = srt.max_view_per_tilemap == SHADOW_TILEMAP_LOD;
195 /* Tag base page to be rendered if any other tile is needed by this shadow.
196 * Fixes issue with shadow map ray tracing sampling invalid tiles.
197 * Only do this in for final render or if all the main levels were already rendered.
198 * This last heuristic avoids very low quality shadows during viewport animation, transform
199 * or jittered shadows. */
200 if ((srt.force_base_page != 0u) && ((srt.levels_rendered == 0u) || is_render)) {
201 int tile_offset = shadow_tile_offset_lds(int2(0), SHADOW_TILEMAP_LOD);
202 /* Tag as modified so that we can amend it inside the `tiles_buf`. */
203 srt.tiles_local[tile_offset] |= SHADOW_TILE_AMENDED;
204 /* Visibility value to write back. */
205 srt.tiles_local[tile_offset] &= ~SHADOW_TILE_MASKED;
206 }
207 }
208#endif
209
210 /* Flush back visibility bits to the tile SSBO. */
211 for (int lod = 0; lod <= SHADOW_TILEMAP_LOD; lod++) {
212 if (thread_mask(tile_co, lod)) {
213 int tile_lds = shadow_tile_offset_lds(tile_co, lod);
214 if ((srt.tiles_local[tile_lds] & SHADOW_TILE_AMENDED) != 0) {
215 int tile_offset = shadow_tile_offset(uint2(tile_co), tilemap.tiles_index, lod);
216 /* Note that we only flush the visibility so that cached pages can be reused. */
217 if ((srt.tiles_local[tile_lds] & SHADOW_TILE_MASKED) != 0) {
218 srt.tiles_buf[tile_offset] &= ~SHADOW_IS_USED;
219 }
220 else {
221 srt.tiles_buf[tile_offset] |= SHADOW_IS_USED;
222 }
223 }
224 }
225 }
226 }
227}
228
230
231} // namespace eevee::shadow
#define ShadowTileDataPacked
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
#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(...)
VecBase< int, D > findMSB(VecOp< int, D >) RET
int atomicAnd(int &, int) RET
bool all(VecOp< bool, D >) RET
VecBase< int, D > bitCount(VecOp< int, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
int atomicOr(int &, int) RET
const ccl_global KernelWorkTile * tile
void mask(PageMask &srt, TileMaps &tilemaps, const uint3 global_id, const uint local_tile_index)
PipelineCompute page_mask(mask)
static constexpr uint SHADOW_TILE_MASKED
eevee::shadow::TileMaps TileMaps
static constexpr uint SHADOW_TILE_AMENDED
int shadow_tile_offset_lds(int2 tile, int lod)
bool thread_mask(int2 tile_co, int lod)
uint tiles_local[SHADOW_TILEDATA_PER_TILEMAP]