Blender V5.3
eevee_shadow_tilemap_finalize.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
9#include "eevee_defines.hh"
11
15
16namespace eevee::shadow {
17
23{
24 /* TODO(fclem): Experiment with non squared viewports. */
25 int max_dim = max(rect_size.x, rect_size.y);
26 /* Assumes max_dim is non-null. */
27 int power_of_two = findMSB(uint(max_dim));
28 if ((1 << power_of_two) != max_dim) {
29 power_of_two += 1;
30 }
31 return power_of_two;
32}
33
35 [[storage(0, read)]] const ShadowTileMapData (&tilemaps_buf)[];
36 [[storage(1, read)]] const uint (&tiles_buf)[];
37 [[storage(2, read_write)]] ShadowPagesInfoData &pages_infos_buf;
38 [[storage(3, read_write)]] ShadowStatistics &statistics_buf;
39 [[storage(4, write)]] ViewMatrices (&view_infos_buf)[SHADOW_VIEW_MAX];
40 [[storage(5, write)]] ShadowRenderView (&render_view_buf)[SHADOW_VIEW_MAX];
41 [[storage(6, read)]] const ShadowTileMapClip (&tilemaps_clip_buf)[];
43
45
51};
52
60[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]]
62 [[global_invocation_id]] const uint3 global_id,
63 [[local_invocation_index]] const uint local_index)
64
65{
66 int tilemap_index = int(global_id.z);
67 int2 tile_co = int2(global_id.xy);
68
69 ShadowTileMapData tilemap_data = srt.tilemaps_buf[tilemap_index];
70 bool is_cubemap = (tilemap_data.projection_type == SHADOW_PROJECTION_CUBEFACE);
71 int lod_max = is_cubemap ? SHADOW_TILEMAP_LOD : 0;
72
73 srt.lod_rendered = 0u;
74
75 for (int lod = lod_max; lod >= 0; lod--) {
76 int2 tile_co_lod = tile_co >> lod;
77 int tile_index = shadow_tile_offset(uint2(tile_co_lod), tilemap_data.tiles_index, lod);
78
79 /* Compute update area. */
80 if (local_index == 0u) {
81 srt.rect_min_x = SHADOW_TILEMAP_RES;
82 srt.rect_min_y = SHADOW_TILEMAP_RES;
83 srt.rect_max_x = 0;
84 srt.rect_max_y = 0;
85 }
86
87 barrier();
88
89 ShadowTileData tile = shadow_tile_unpack(srt.tiles_buf[tile_index]);
90 bool lod_valid_thread = all(equal(tile_co, tile_co_lod << lod));
91 bool do_page_render = tile.is_used && tile.do_update && lod_valid_thread;
92 if (do_page_render) {
93 atomicMin(srt.rect_min_x, tile_co_lod.x);
94 atomicMin(srt.rect_min_y, tile_co_lod.y);
95 atomicMax(srt.rect_max_x, tile_co_lod.x + 1);
96 atomicMax(srt.rect_max_y, tile_co_lod.y + 1);
97 }
98
99 barrier();
100
101 int2 rect_min = int2(srt.rect_min_x, srt.rect_min_y);
102 int2 rect_max = int2(srt.rect_max_x, srt.rect_max_y);
103
104 int viewport_index = viewport_select(rect_max - rect_min);
105 viewport_index = srt.use_multi_viewport ? viewport_index : SHADOW_TILEMAP_LOD;
106 int2 viewport_size = shadow_viewport_size_get(uint(viewport_index));
107
108 /* Issue one view if there is an update in the LOD. */
109 if (local_index == 0u) {
110 bool lod_has_update = rect_min.x < rect_max.x;
111 if (lod_has_update) {
112 int view_index = atomicAdd(srt.statistics_buf.view_needed_count, 1);
113 if (view_index < SHADOW_VIEW_MAX) {
114 srt.lod_rendered |= 1u << lod;
115
116 /* Setup the view. */
117 srt.view_infos_buf[view_index].viewmat = tilemap_data.viewmat;
118 srt.view_infos_buf[view_index].viewinv = inverse(tilemap_data.viewmat);
119
120 float lod_res = float(SHADOW_TILEMAP_RES >> lod);
121
122 /* TODO(fclem): These should be the culling planes. */
123 // float2 cull_region_start = (float2(rect_min) / lod_res) * 2.0f - 1.0f;
124 // float2 cull_region_end = (float2(rect_max) / lod_res) * 2.0f - 1.0f;
125 float2 view_start = (float2(rect_min) / lod_res) * 2.0f - 1.0f;
126 float2 view_end = (float2(rect_min + viewport_size) / lod_res) * 2.0f - 1.0f;
127
128 int clip_index = tilemap_data.clip_data_index;
129 float clip_far = srt.tilemaps_clip_buf[clip_index].clip_far_stored;
130 float clip_near = srt.tilemaps_clip_buf[clip_index].clip_near_stored;
131
132 view_start = view_start * tilemap_data.half_size + tilemap_data.center_offset;
133 view_end = view_end * tilemap_data.half_size + tilemap_data.center_offset;
134
135 float4x4 winmat;
136 if (tilemap_data.projection_type != SHADOW_PROJECTION_CUBEFACE) {
138 view_start.x, view_end.x, view_start.y, view_end.y, clip_near, clip_far);
139 }
140 else {
141 winmat = projection_perspective(
142 view_start.x, view_end.x, view_start.y, view_end.y, clip_near, clip_far);
143 }
144
145 srt.view_infos_buf[view_index].winmat = winmat;
146 srt.view_infos_buf[view_index].wininv = inverse(winmat);
147
148 srt.render_view_buf[view_index].viewport_index = uint(viewport_index);
149 srt.render_view_buf[view_index].is_directional = !is_cubemap;
150 srt.render_view_buf[view_index].clip_near = clip_near;
151 /* Clipping setup. */
152 if (is_point_light(tilemap_data.light_type)) {
153 /* Clip as a sphere around the clip_near cube. */
154 srt.render_view_buf[view_index].clip_distance_inv = M_SQRT1_3 / tilemap_data.clip_near;
155 }
156 else {
157 /* Disable local clipping. */
158 srt.render_view_buf[view_index].clip_distance_inv = 0.0f;
159 }
160 /* For building the render map. */
161 srt.render_view_buf[view_index].tilemap_tiles_index = tilemap_data.tiles_index;
162 srt.render_view_buf[view_index].tilemap_lod = lod;
163 srt.render_view_buf[view_index].rect_min = rect_min;
164 /* For shadow linking. */
165 srt.render_view_buf[view_index].shadow_set_membership =
166 tilemap_data.shadow_set_membership;
167 }
168 }
169 }
170 }
171
172 /* Broadcast result of `lod_rendered`. */
173 barrier();
174
175 /* With all threads (LOD0 size dispatch) load each lod tile from the highest lod
176 * to the lowest, keeping track of the lowest one allocated which will be use for shadowing.
177 * This guarantee a O(1) lookup time.
178 * Add one render view per LOD that has tiles to be rendered. */
179 int valid_tile_index = -1;
180 uint valid_lod = 0u;
181 for (int lod = lod_max; lod >= 0; lod--) {
182 int2 tile_co_lod = tile_co >> lod;
183 int tile_index = shadow_tile_offset(uint2(tile_co_lod), tilemap_data.tiles_index, lod);
184 ShadowTileData tile = shadow_tile_unpack(srt.tiles_buf[tile_index]);
185
186 bool lod_is_rendered = ((srt.lod_rendered >> lod) & 1u) == 1u;
187 if (tile.is_used && tile.is_allocated && (!tile.do_update || lod_is_rendered)) {
188 /* Save highest lod for this thread. */
189 valid_tile_index = tile_index;
190 valid_lod = uint(lod);
191 }
192 }
193
194 /* Store the highest LOD valid page for rendering. */
195 ShadowTileDataPacked tile_packed = (valid_tile_index != -1) ? srt.tiles_buf[valid_tile_index] :
196 SHADOW_NO_DATA;
197 ShadowTileData tile_data = shadow_tile_unpack(tile_packed);
198 ShadowSamplingTile tile_sampling = shadow_sampling_tile_create(tile_data, valid_lod);
199 ShadowSamplingTilePacked tile_sampling_packed = shadow_sampling_tile_pack(tile_sampling);
200
201 uint2 atlas_texel = shadow_tile_coord_in_atlas(uint2(tile_co), tilemap_index);
202 imageStoreFast(srt.tilemaps_img, int2(atlas_texel), uint4(tile_sampling_packed));
203
204 if (all(equal(global_id, uint3(0)))) {
205 /* Clamp it as it can underflow if there is too much tile present on screen. */
206 srt.pages_infos_buf.page_free_count = max(srt.pages_infos_buf.page_free_count, 0);
207 }
208}
209
211 [[storage(0, read_write)]] ShadowStatistics &statistics_buf;
212 [[storage(1, read)]] const ShadowRenderView (&render_view_buf)[SHADOW_VIEW_MAX];
213 [[storage(2, read_write)]] uint (&tiles_buf)[];
214 [[storage(3, read_write)]] DispatchCommand &clear_dispatch_buf;
215 [[storage(4, read_write)]] DrawCommandArray &tile_draw_buf;
216 [[storage(5, write)]] uint (&dst_coord_buf)[SHADOW_RENDER_MAP_SIZE];
217 [[storage(6, write)]] uint (&src_coord_buf)[SHADOW_RENDER_MAP_SIZE];
218 [[storage(7, write)]] uint (&render_map_buf)[SHADOW_RENDER_MAP_SIZE];
219};
220
226[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]]
228 [[global_invocation_id]] const uint3 global_id)
229{
230 int view_index = int(global_id.z);
231 /* Dispatch size if already bounded by SHADOW_VIEW_MAX. */
232 if (view_index >= srt.statistics_buf.view_needed_count) {
233 return;
234 }
235
236 int2 rect_min = srt.render_view_buf[view_index].rect_min;
237 int tilemap_tiles_index = srt.render_view_buf[view_index].tilemap_tiles_index;
238 int lod = srt.render_view_buf[view_index].tilemap_lod;
239 int2 viewport_size = shadow_viewport_size_get(srt.render_view_buf[view_index].viewport_index);
240
241 int2 tile_co = int2(global_id.xy);
242 int2 tile_co_lod = tile_co >> lod;
243 bool lod_valid_thread = all(equal(tile_co, tile_co_lod << lod));
244
245 int tile_index = shadow_tile_offset(uint2(tile_co_lod), tilemap_tiles_index, lod);
246
247 if (lod_valid_thread) {
248 ShadowTileData tile = shadow_tile_unpack(srt.tiles_buf[tile_index]);
249 /* Tile coordinate relative to chosen viewport origin. */
250 int2 viewport_tile_co = tile_co_lod - rect_min;
251 /* We need to add page indirection to the render map for the whole viewport even if this one
252 * might extend outside of the shadow-map range. To this end, we need to wrap the threads to
253 * always cover the whole mip. This is because the viewport cannot be bigger than the mip
254 * level itself. */
255 int lod_res = SHADOW_TILEMAP_RES >> lod;
256 int2 relative_tile_co = (viewport_tile_co + lod_res) % lod_res;
257 if (all(lessThan(relative_tile_co, viewport_size))) {
258 if (tile.is_used && tile.do_update) {
259 uint page_packed = shadow_page_pack(tile.page);
260 /* Add page to render map. */
261 int render_page_index = shadow_render_page_index_get(view_index, relative_tile_co);
262 srt.render_map_buf[render_page_index] = page_packed;
263 /* Add page to clear dispatch. */
264 uint page_index = atomicAdd(srt.clear_dispatch_buf.num_groups_z, 1u);
265 /* Add page to tile processing. */
266 atomicAdd(srt.tile_draw_buf.vertex_len, 6u);
267 /* Add page mapping for indexing the page position in atlas and in the frame-buffer. */
268 srt.dst_coord_buf[page_index] = page_packed;
269 srt.src_coord_buf[page_index] = packUvec4x8(
270 uint4(relative_tile_co.x, relative_tile_co.y, view_index, 0));
271 /* Tag tile as rendered. Should be safe since only one thread is reading and writing. */
272 srt.tiles_buf[tile_index] |= SHADOW_IS_RENDERED;
273 /* Statistics. */
274 atomicAdd(srt.statistics_buf.page_rendered_count, 1);
275 }
276 }
277 }
278}
279
282
283} // namespace eevee::shadow
#define M_SQRT1_3
btScalar max
#define ShadowTileDataPacked
#define ShadowSamplingTilePacked
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
uint2 shadow_tile_coord_in_atlas(uint2 tile, int tilemap_index)
int shadow_render_page_index_get(int view_index, int2 tile_coordinate_in_lod)
int2 shadow_viewport_size_get(uint viewport_index)
#define shared
#define local_invocation_index
#define viewport_index
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
VecBase< int, D > findMSB(VecOp< int, D >) RET
int atomicMin(int &, int) RET
int atomicMax(int &, int) RET
MatBase< C, R > inverse(MatBase< C, R >) RET
int atomicAdd(int &, int) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
ImageBase< uint, 2 > uimage2D
#define imageStoreFast
float4x4 projection_orthographic(float left, float right, float bottom, float top, float near_clip, float far_clip)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
float4x4 projection_perspective(float left, float right, float bottom, float top, float near_clip, float far_clip)
Create a perspective projection matrix using OpenGL coordinate convention: Maps each axis range to [-...
uint packUvec4x8(uint4 a)
const int tile_index
const ccl_global KernelWorkTile * tile
MatBase< float, 4, 4 > float4x4
int viewport_select(int2 rect_size)
PipelineCompute tilemap_finalize(eevee::shadow::tilemap_finalize_main)
void tilemap_finalize_main(TilemapFinalize &srt, const uint3 global_id, const uint local_index)
void rendermap_finalize_main(RendermapFinalize &srt, const uint3 global_id)
PipelineCompute tilemap_rendermap(eevee::shadow::rendermap_finalize_main)
ccl_device_inline int rect_size(const int4 rect)
Definition rect.h:56
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const ShadowRenderView(& render_view_buf)[SHADOW_VIEW_MAX]
ViewMatrices(& view_infos_buf)[SHADOW_VIEW_MAX]
ShadowRenderView(& render_view_buf)[SHADOW_VIEW_MAX]
float x
float y