Blender V5.3
eevee_shadow_shared.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
10
11#pragma once
12
13#include "eevee_light_shared.hh"
14#include "eevee_transform.hh"
15
16#ifndef GPU_SHADER
17namespace blender::eevee {
18#endif
19
20/* -------------------------------------------------------------------- */
32
34 /* Ordering by culling order. If cone aperture is shallow, we cull the later view. */
35 Z_NEG = 0u,
36 X_POS = 1u,
37 X_NEG = 2u,
38 Y_POS = 3u,
39 Y_NEG = 4u,
40 Z_POS = 5u,
41};
42
48
49static inline int2 shadow_cascade_grid_offset(int2 base_offset, int level_relative)
50{
51 return (base_offset * level_relative) / (1 << 16);
52}
53
92
120
133 /* Transform the shadow is rendered with. Used to detect updates on GPU. */
135 /* Integer offset of the center of the 16x16 tiles from the origin of the tile space. */
137 int _pad0;
138 int _pad1;
139};
140
157
176
194
195#define ShadowTileDataPacked uint
196
199 SHADOW_IS_CACHED = (1u << 27u),
200 SHADOW_IS_ALLOCATED = (1u << 28u),
201 SHADOW_DO_UPDATE = (1u << 29u),
202 SHADOW_IS_RENDERED = (1u << 30u),
203 SHADOW_IS_USED = (1u << 31u),
204 /* Reuse the same flag for tagging update before LOD propagation.
205 * Assume usage tagging is done afterwards. */
206 SHADOW_TAG_UPDATE = (1u << 31u)
207};
208
209/* NOTE: Trust the input to be in valid range (max is [7,7,127]).
210 * If it is in valid range, it should pack to 14bits so that `shadow_tile_pack()` can use it.
211 * But sometime this is used to encode invalid pages uint3(-1) and it needs to output uint(-1).
212 */
213static inline uint shadow_page_pack(uint3 page)
214{
215 return (page.x << 0u) | (page.y << 3u) | (page.z << 6u);
216}
218{
219 uint3 page;
220 page.x = (data >> 0u) & 7u;
221 page.y = (data >> 3u) & 7u;
222 page.z = (data >> 6u) & 127u;
223#ifndef GPU_SHADER
224 BLI_STATIC_ASSERT(SHADOW_PAGE_PER_ROW <= 8 && SHADOW_PAGE_PER_COL <= 8 &&
225 SHADOW_PAGE_MAX_LAYER <= 128,
226 "Update page packing")
227 BLI_STATIC_ASSERT(SHADOW_MAX_PAGE ==
228 (SHADOW_PAGE_PER_ROW * SHADOW_PAGE_PER_COL * SHADOW_PAGE_MAX_LAYER),
229 "Update page packing")
230#endif
231 return page;
232}
233
235{
238/* -- 13 bits -- */
239/* Unused bits. */
240/* -- 14 bits -- */
241#ifndef GPU_SHADER
242 BLI_STATIC_ASSERT(SHADOW_MAX_PAGE <= 8192, "Update page packing")
243#endif
244 tile.cache_index = (data >> 14u) & 8191u;
245 /* -- 27 bits -- */
246 tile.is_used = (data & SHADOW_IS_USED) != 0;
247 tile.is_cached = (data & SHADOW_IS_CACHED) != 0;
248 tile.is_allocated = (data & SHADOW_IS_ALLOCATED) != 0;
249 tile.is_rendered = (data & SHADOW_IS_RENDERED) != 0;
250 tile.do_update = (data & SHADOW_DO_UPDATE) != 0;
251 return tile;
252}
253
255{
256 uint data;
257 /* NOTE: Page might be set to invalid values for tracking invalid usages.
258 * So we have to mask the result. */
259 data = shadow_page_pack(tile.page) & uint(SHADOW_MAX_PAGE - 1);
260#ifndef GPU_SHADER
261 BLI_STATIC_ASSERT(SHADOW_MAX_PAGE <= 8192, "Update page packing")
262#endif
263 data |= (tile.cache_index & 8191u) << 14u;
264 data |= (tile.is_used ? uint(SHADOW_IS_USED) : 0);
265 data |= (tile.is_allocated ? uint(SHADOW_IS_ALLOCATED) : 0);
266 data |= (tile.is_cached ? uint(SHADOW_IS_CACHED) : 0);
267 data |= (tile.is_rendered ? uint(SHADOW_IS_RENDERED) : 0);
268 data |= (tile.do_update ? uint(SHADOW_DO_UPDATE) : 0);
269 return data;
270}
271
287
288#define ShadowSamplingTilePacked uint
289
290/* NOTE: Trust the input to be in valid range [0, (1 << SHADOW_TILEMAP_MAX_CLIPMAP_LOD) - 1].
291 * Maximum LOD level index we can store is SHADOW_TILEMAP_MAX_CLIPMAP_LOD,
292 * so we need SHADOW_TILEMAP_MAX_CLIPMAP_LOD bits to store the offset in each dimension.
293 * Result fits into SHADOW_TILEMAP_MAX_CLIPMAP_LOD * 2 bits. */
295{
296#ifndef GPU_SHADER
297 BLI_STATIC_ASSERT(SHADOW_TILEMAP_MAX_CLIPMAP_LOD <= 8, "Update page packing")
298#endif
299 return ofs.x | (ofs.y << SHADOW_TILEMAP_MAX_CLIPMAP_LOD);
300}
302{
303 return (uint2(data) >> uint2(0, SHADOW_TILEMAP_MAX_CLIPMAP_LOD)) &
304 uint2((1 << SHADOW_TILEMAP_MAX_CLIPMAP_LOD) - 1);
305}
306
308{
311 /* -- 13 bits -- */
312 /* Max value is actually SHADOW_TILEMAP_MAX_CLIPMAP_LOD but we mask the bits. */
313 tile.lod = (data >> 13u) & 7u;
314#ifndef GPU_SHADER
315 BLI_STATIC_ASSERT(SHADOW_TILEMAP_MAX_CLIPMAP_LOD <= 8u, "Update lod packing")
316#endif
317 /* -- 16 bits -- */
318 tile.lod_offset = shadow_lod_offset_unpack(data >> 16u);
319 /* -- 32 bits -- */
320 tile.is_valid = data != 0u;
321#ifndef GPU_SHADER
322 /* Make tests pass on CPU but it is not required for proper rendering. */
323 if (tile.lod == 0) {
324 tile.lod_offset.x = 0;
325 }
326#endif
327 return tile;
328}
329
331{
332 if (!tile.is_valid) {
333 return 0u;
334 }
335 /* Tag a valid tile of LOD0 valid by setting their offset to 1.
336 * This doesn't change the sampling and allows to use of all bits for data.
337 * This makes sure no valid packed tile is 0u. */
338 if (tile.lod == 0) {
339 tile.lod_offset.x = 1;
340 }
342#ifndef GPU_SHADER
343 BLI_STATIC_ASSERT(SHADOW_MAX_PAGE <= 8192, "Update page packing")
344 /* Max value is actually SHADOW_TILEMAP_MAX_CLIPMAP_LOD but we mask the bits. */
345 BLI_STATIC_ASSERT(SHADOW_TILEMAP_MAX_CLIPMAP_LOD <= 8u, "Update lod packing")
346#endif
347 data |= (tile.lod & 7u) << 13u;
348 data |= shadow_lod_offset_pack(tile.lod_offset) << 16u;
349 return data;
350}
351
353{
355 tile.page = tile_data.page;
356 tile.lod = lod;
357 tile.lod_offset = uint2(0, 0); /* Computed during tilemap amend phase. */
358 /* At this point, it should be the case that all given tiles that have been tagged as used are
359 * ready for sampling. Otherwise tile_data should be SHADOW_NO_DATA. */
360 tile.is_valid = tile_data.is_used;
361 return tile;
362}
363
365
366#ifndef GPU_SHADER
367} // namespace blender::eevee
368#endif
#define host_shared
int32_t bool32_t
#define ShadowTileDataPacked
#define ShadowSamplingTilePacked
const ccl_global KernelWorkTile * tile
BLI_STATIC_ASSERT(MBC_BATCH_LEN< 64, "Number of batches exceeded the limit of bit fields")
static int2 shadow_cascade_grid_offset(int2 base_offset, int level_relative)
static ShadowTileDataPacked shadow_tile_pack(ShadowTileData tile)
static ShadowSamplingTilePacked shadow_sampling_tile_pack(ShadowSamplingTile tile)
static ShadowTileData shadow_tile_unpack(ShadowTileDataPacked data)
static uint2 shadow_lod_offset_unpack(uint data)
static uint3 shadow_page_unpack(uint data)
static uint shadow_page_pack(uint3 page)
static uint shadow_lod_offset_pack(uint2 ofs)
static ShadowSamplingTile shadow_sampling_tile_create(ShadowTileData tile_data, uint lod)
static ShadowSamplingTile shadow_sampling_tile_unpack(ShadowSamplingTilePacked data)
VecBase< uint32_t, 2 > uint2
unsigned int uint
Definition BKE_text.h:55
VecBase< uint32_t, 3 > uint3
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
enum eShadowProjectionType projection_type