Blender V5.3
eevee_shadow_tilemap_amend.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
17
18#pragma once
19
20#include "eevee_defines.hh"
24
25namespace eevee {
26
28 [[storage(4, read)]] const ShadowTileMapData (&tilemaps_buf)[];
29 [[storage(5, read_write)]] LightData (&light_buf_write)[];
30
31 [[shared]] uint tiles_local[SHADOW_TILEMAP_RES][SHADOW_TILEMAP_RES];
32
33 [[image(0, read_write, UINT_32)]] uimage2D tilemaps_img;
34};
35
36struct AmendCtx {
38
39 void eval_directional(TilemapAmend &srt, uint /*index*/, LightData light)
40 {
41 /* This only works on clip-maps. Cascade have already the same LOD for every tile-maps. */
42 if (light.tilemap_index == LIGHT_NO_SHADOW || light.type != LIGHT_SUN) {
43 return;
44 }
45
46 int2 base_offset_neg = light.sun.clipmap_base_offset_neg;
47 int2 base_offset_pos = light.sun.clipmap_base_offset_pos;
48 /* LOD relative max with respect to clipmap_lod_min. */
49 int lod_max = light.sun.clipmap_lod_max - light.sun.clipmap_lod_min;
50 /* Iterate in reverse. */
51 for (int lod = lod_max; lod >= 0; lod--) {
52 int tilemap_index = light.tilemap_index + lod;
53 uint2 atlas_texel = shadow_tile_coord_in_atlas(uint2(tile_co), tilemap_index);
54
55 ShadowSamplingTilePacked tile_packed = imageLoad(srt.tilemaps_img, int2(atlas_texel)).x;
56 ShadowSamplingTile tile = shadow_sampling_tile_unpack(tile_packed);
57
58 if (lod != lod_max && !tile.is_valid) {
59 /* Offset this LOD has with the previous one. In unit of tile of the current LOD. */
60 int2 offset_binary = ((base_offset_pos >> lod) & 1) - ((base_offset_neg >> lod) & 1);
61 int2 offset_centered = int2(SHADOW_TILEMAP_RES / 2) + offset_binary;
62 int2 tile_co_prev = (tile_co + offset_centered) >> 1;
63
64 /* Load tile from the previous LOD. */
65 ShadowSamplingTilePacked tile_prev_packed =
66 srt.tiles_local[tile_co_prev.y][tile_co_prev.x];
67 ShadowSamplingTile tile_prev = shadow_sampling_tile_unpack(tile_prev_packed);
68
69 /* We can only propagate LODs up to a certain level.
70 * Afterwards we run out of bits to store the offsets. */
71 if (tile_prev.is_valid && tile_prev.lod < SHADOW_TILEMAP_MAX_CLIPMAP_LOD - 1) {
72 /* Relative LOD. Used for reducing pixel rate at sampling time.
73 * Increase with each new invalid level. */
74 tile_prev.lod += 1;
75
76 /* The offset (in tile of current LOD) is equal to the offset from the bottom left
77 * corner of both LODs modulo the size of a tile of the source LOD (in tile of current
78 * LOD). */
79
80 /* Offset corner to center. */
81 tile_prev.lod_offset = uint2(SHADOW_TILEMAP_RES / 2) << tile_prev.lod;
82 /* Align center of both LODs. */
83 tile_prev.lod_offset -= uint2(SHADOW_TILEMAP_RES / 2);
84 /* Add the offset relative to the source LOD. */
85 tile_prev.lod_offset += uint2(bitfieldExtract(base_offset_pos, lod, int(tile_prev.lod)) -
86 bitfieldExtract(base_offset_neg, lod, int(tile_prev.lod)));
87 /* Wrap to valid range. */
88 tile_prev.lod_offset &= ~(~0u << tile_prev.lod);
89
90 tile_prev_packed = shadow_sampling_tile_pack(tile_prev);
91 /* Replace the missing page with the one from the lower LOD. */
92 imageStoreFast(srt.tilemaps_img, int2(atlas_texel), uint4(tile_prev_packed));
93 /* Push this amended tile to the local tiles. */
94 tile_packed = tile_prev_packed;
95 tile.is_valid = true;
96 }
97 }
98
99 barrier();
100 srt.tiles_local[tile_co.y][tile_co.x] = (tile.is_valid) ? tile_packed : SHADOW_NO_DATA;
101 barrier();
102 }
103 }
104
105 void eval_local(TilemapAmend &srt, uint index, LightData light)
106 {
107 if (light.tilemap_index == LIGHT_NO_SHADOW) {
108 return;
109 }
110
111 int lod_min = 0;
112 int tilemap_count = light.local_tilemap_count();
113 for (int i = 0; i < tilemap_count; i++) {
114 ShadowTileMapData tilemap = srt.tilemaps_buf[light.tilemap_index + i];
115 lod_min = max(lod_min, tilemap.effective_lod_min);
116 }
117 if (lod_min > 0) {
118 /* Override the effective lod min distance in absolute mode (negative).
119 * Note that this only changes the sampling for this AA sample. */
120 constexpr float projection_diagonal = 2.0f * M_SQRT2;
121 srt.light_buf_write[index].lod_min = -(projection_diagonal /
122 float(SHADOW_MAP_MAX_RES >> lod_min));
123 }
124 }
125};
126
127template void light::foreach<AmendCtx, TilemapAmend>(const LightRenderData &,
128 AmendCtx &,
129 TilemapAmend &);
130
131[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]] void tilemap_amend(
134 [[global_invocation_id]] const uint3 global_id)
135{
136 AmendCtx ctx = {.tile_co = int2(global_id.xy)};
137 light::foreach(lrd, ctx, srt);
138}
139
141
142} // namespace eevee
#define M_SQRT2
btScalar max
#define LIGHT_NO_SHADOW
#define ShadowSamplingTilePacked
uint2 shadow_tile_coord_in_atlas(uint2 tile, int tilemap_index)
#define shared
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define index(i)
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
VecBase< int, D > bitfieldExtract(VecOp< int, D >, int, int) RET
void barrier()
ImageBase< uint, 2 > uimage2D
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
#define imageStoreFast
const ccl_global KernelWorkTile * tile
@ LIGHT_SUN
void foreach(const LightRenderData &srt, CallbackT &cb, ResourceT &res)
void tilemap_amend(TilemapAmend &srt, LightRenderData &lrd, const uint3 global_id)
PipelineCompute shadow_tilemap_amend(tilemap_amend)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void eval_directional(TilemapAmend &srt, uint, LightData light)
void eval_local(TilemapAmend &srt, uint index, LightData light)
const ShadowTileMapData(& tilemaps_buf)[]
uint tiles_local[SHADOW_TILEMAP_RES][SHADOW_TILEMAP_RES]