Blender V5.3
eevee_shadow_page_ops.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
5#pragma once
6
8
9static constexpr uint max_page = uint(SHADOW_MAX_PAGE);
10
11namespace eevee::shadow {
12
13struct TileMaps {
14 [[storage(8, read_write)]] ShadowTileMapData (&tilemaps_buf)[];
15};
16
17struct Tiles {
18 [[storage(9, read_write)]] uint (&tiles_buf)[];
19};
20
21struct Statistics {
22 [[storage(7, read_write)]] ShadowStatistics &statistics_buf;
23};
24
32 [[storage(2, read_write)]] ShadowPagesInfoData &pages_infos_buf;
33 /* The tiles_buf only owns a page if it is used. If the page is cached, the tile contains a
34 * reference index inside the pages_cached_buf.*/
35 [[storage(1, read_write)]] uint (&tiles_buf)[];
36 /* Free page stack containing only the page coordinates. */
37 [[storage(3, read_write)]] uint (&pages_free_buf)[];
38 /* The pages_cached_buf is a ring buffer where newly cached pages gets added at the end and the
39 * old cached pages gets defragmented at the start of the used portion. */
40 [[storage(4, read_write)]] uint2 (&pages_cached_buf)[];
41
42 /* Remove page ownership from the tile and append it to the cache. */
43 void page_free(ShadowTileData &tile)
44 {
45 assert(tile.is_allocated);
46
47 int index = atomicAdd(pages_infos_buf.page_free_count, 1);
48 assert(index < SHADOW_MAX_PAGE);
49 /* Insert in heap. */
50 pages_free_buf[index] = shadow_page_pack(tile.page);
51 /* Remove from tile. */
52 tile.page = uint3(~0u);
53 tile.is_cached = false;
54 tile.is_allocated = false;
55 }
56
57 /* Remove last page from the free heap and give ownership to the tile. */
58 void page_alloc(ShadowTileData &tile)
59 {
60 assert(!tile.is_allocated);
61
62 int index = atomicAdd(pages_infos_buf.page_free_count, -1) - 1;
63 /* This can easily happen in really big scene. */
64 if (index < 0) {
65 return;
66 }
67 /* Insert in tile. */
68 tile.page = shadow_page_unpack(pages_free_buf[index]);
69 tile.is_allocated = true;
70 tile.do_update = true;
71 /* Remove from heap. */
72 pages_free_buf[index] = uint(-1);
73 }
74
75 /* Remove page ownership from the tile cache and append it to the cache. */
76 void page_cache_append(ShadowTileData &tile, uint tile_index)
77 {
78 assert(tile.is_allocated);
79
80 /* The page_cached_next is also wrapped in the defragment phase to avoid unsigned overflow. */
81 uint index = atomicAdd(pages_infos_buf.page_cached_next, 1u) % uint(SHADOW_MAX_PAGE);
82 /* Insert in heap. */
83 pages_cached_buf[index] = uint2(shadow_page_pack(tile.page), tile_index);
84 /* Remove from tile. */
85 tile.page = uint3(~0u);
86 tile.cache_index = index;
87 tile.is_cached = true;
88 tile.is_allocated = false;
89 }
90
91 /* Remove page from cache and give ownership to the tile. */
92 void page_cache_remove(ShadowTileData &tile)
93 {
94 assert(!tile.is_allocated);
95 assert(tile.is_cached);
96
97 uint index = tile.cache_index;
98 /* Insert in tile. */
99 tile.page = shadow_page_unpack(pages_cached_buf[index].x);
100 tile.cache_index = uint(-1);
101 tile.is_cached = false;
102 tile.is_allocated = true;
103 /* Remove from heap. Leaves hole in the buffer. This is handled by the defragment phase. */
105 }
106
107 /* Update cached page reference when a cached page moves inside the cached page buffer. */
108 void page_cache_update_page_ref(uint page_index, uint new_page_index)
109 {
110 uint tile_index = pages_cached_buf[page_index].y;
111 ShadowTileData tile = shadow_tile_unpack(tiles_buf[tile_index]);
112 tile.cache_index = new_page_index;
113 tiles_buf[tile_index] = shadow_tile_pack(tile);
114 }
115
116 private:
117 /* Update cached page reference when a tile referencing a cached page moves inside the tile-map.
118 */
119 void page_cache_update_tile_ref(uint page_index, uint new_tile_index)
120 {
121 pages_cached_buf[page_index].y = new_tile_index;
122 }
123
124 void find_first_valid(uint &src, uint dst)
125 {
126 for (uint i = src; i < dst; i++) {
127 if (pages_cached_buf[i % max_page].x != uint(-1)) {
128 src = i;
129 return;
130 }
131 }
132
133 src = dst;
134 }
135
136 void free_cached_page(uint page_index)
137 {
138 uint tile_index = pages_cached_buf[page_index].y;
139 ShadowTileData tile = shadow_tile_unpack(tiles_buf[tile_index]);
140
143
144 tiles_buf[tile_index] = shadow_tile_pack(tile);
145 }
146
147 /* Can be used to debug heap and invalid pages inside the free buffer. */
148 bool check_heap_integrity(int start, int size, const uint invalid_val)
149 {
150 auto &heap = pages_free_buf;
151 bool result = true;
152 for (int i = 0; i < int(max_page); i++) {
153 if ((i >= start) && (i < (start + size))) {
154 result = result && (heap[i] != invalid_val);
155 }
156 else {
157 result = result && (heap[i] == invalid_val);
158 }
159 }
160 return result;
161 }
162
163 public:
164 void defrag()
165 {
166 /* Pages we need to get off the cache for the allocation pass. */
167 int additional_pages = pages_infos_buf.page_alloc_count - pages_infos_buf.page_free_count;
168
169 uint src = pages_infos_buf.page_cached_start;
170 uint end = pages_infos_buf.page_cached_end;
171
172 find_first_valid(src, end);
173
174#if 0 /* Debug */
175 if (!check_heap_integrity(0, pages_infos_buf.page_free_count, uint(-1))) {
176 printf("Corrupted heap before defrag.");
177 }
178#endif
179
180 /* First free as much pages as needed from the end of the cached range to fulfill the
181 * allocation. Avoid defragmenting to then free them. */
182 for (; additional_pages > 0 && src < end; additional_pages--) {
183 free_cached_page(src % max_page);
184 find_first_valid(src, end);
185 }
186
187 /* Defragment page in "old" range. */
188 bool is_empty = (src == end);
189 if (!is_empty) {
190 /* `page_cached_end` refers to the next empty slot.
191 * Decrement by one to refer to the first slot we can defragment. */
192 for (uint dst = end - 1; dst > src; dst--) {
193 /* Find hole. */
194 if (pages_cached_buf[dst % max_page].x != uint(-1)) {
195 continue;
196 }
197 /* Update corresponding reference in tile. */
199 /* Move page. */
201 pages_cached_buf[src % max_page] = uint2(~0u);
202
203 find_first_valid(src, dst);
204 }
205 }
206
207 end = pages_infos_buf.page_cached_next;
208 /* Free pages in the "new" range (these are compact). */
209 for (; additional_pages > 0 && src < end; additional_pages--, src++) {
210 free_cached_page(src % max_page);
211 }
212
213#if 0 /* Debug */
214 if (!check_heap_integrity(0, pages_infos_buf.page_free_count, uint(-1))) {
215 printf("Corrupted heap after defrag.");
216 }
217#endif
218
219 pages_infos_buf.page_cached_start = src;
220 pages_infos_buf.page_cached_end = end;
221 pages_infos_buf.page_alloc_count = 0;
222
223 /* Wrap the cursor to avoid unsigned overflow. We do not do modulo arithmetic because it would
224 * produce a 0 length buffer if the buffer is full. */
225 if (pages_infos_buf.page_cached_start > max_page) {
226 pages_infos_buf.page_cached_next -= max_page;
227 pages_infos_buf.page_cached_start -= max_page;
228 pages_infos_buf.page_cached_end -= max_page;
229 }
230 }
231};
232
233#undef max_page
234
235} // namespace eevee::shadow
#define printf
static constexpr uint max_page
#define assert(assertion)
#define storage(slot, qualifiers)
#define index(i)
int atomicAdd(int &, int) RET
const int tile_index
const ccl_global KernelWorkTile * tile
unsigned int uint
Definition BKE_text.h:55
void page_cache_remove(ShadowTileData &tile)
void page_cache_update_page_ref(uint page_index, uint new_page_index)
void page_cache_append(ShadowTileData &tile, uint tile_index)
void page_free(ShadowTileData &tile)
void page_alloc(ShadowTileData &tile)
ShadowTileMapData(& tilemaps_buf)[]
uint y
Definition types_uint2.h:13