Blender V5.3
eevee_light_shadow_setup.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10#include "eevee_uniform.bsl.hh"
15
16namespace eevee::light {
17
19{
20 return 1 << level;
21}
22
23struct Resources {
24 [[storage(0, read)]] const LightCullingData &light_cull_buf;
25 [[storage(1, read_write)]] LightData (&light_buf)[];
26 [[storage(2, read_write)]] ShadowTileMapData (&tilemaps_buf)[];
27 [[storage(3, read_write)]] ShadowTileMapClip (&tilemaps_clip_buf)[];
28
30
31 void orthographic_sync(int tilemap_id,
32 Transform object_to_world,
33 int2 origin_offset,
34 int clipmap_level,
35 eShadowProjectionType projection_type)
36 {
37 /* Do not check translation. */
38 object_to_world.x.w = 0.0f;
39 object_to_world.y.w = 0.0f;
40 object_to_world.z.w = 0.0f;
41
42 int clip_index = tilemaps_buf[tilemap_id].clip_data_index;
43 /* Avoid qualifier problems on NVidia (see #121968). */
44 Transform object_to_world_history = tilemaps_clip_buf[clip_index].object_to_world;
45
46 if (tilemaps_buf[tilemap_id].is_dirty ||
47 !transform_equal(object_to_world_history, object_to_world))
48 {
49 /* Set dirty as the light direction changed. */
50 tilemaps_buf[tilemap_id].grid_shift = int2(SHADOW_TILEMAP_RES);
51 tilemaps_clip_buf[clip_index].object_to_world = object_to_world;
52 }
53 else {
54 /* Same light direction but camera might have moved. Shift tilemap grid. */
55 tilemaps_buf[tilemap_id].grid_shift = origin_offset -
56 tilemaps_clip_buf[clip_index].grid_offset;
57 }
58 tilemaps_clip_buf[clip_index].grid_offset = origin_offset;
59 /* TODO(fclem): Remove this duplicate. Only needed because of the base offset packing. */
60 tilemaps_buf[tilemap_id].grid_offset = origin_offset;
61
62 float level_size = shadow_directional_coverage_get(clipmap_level);
63 float half_size = level_size / 2.0f;
64 float tile_size = level_size / float(SHADOW_TILEMAP_RES);
65 float2 center_offset = float2(origin_offset) * tile_size;
66
67 /* object_mat is a rotation matrix. Reduce imprecision by taking the transpose which is also
68 * the inverse in this particular case. */
69 tilemaps_buf[tilemap_id].viewmat[0] = float4(object_to_world.x.xyz, 0.0f);
70 tilemaps_buf[tilemap_id].viewmat[1] = float4(object_to_world.y.xyz, 0.0f);
71 tilemaps_buf[tilemap_id].viewmat[2] = float4(object_to_world.z.xyz, 0.0f);
72 tilemaps_buf[tilemap_id].viewmat[3] = float4(0.0f, 0.0f, 0.0f, 1.0f);
73
74 tilemaps_buf[tilemap_id].projection_type = projection_type;
75 tilemaps_buf[tilemap_id].half_size = half_size;
76 tilemaps_buf[tilemap_id].center_offset = center_offset;
77 tilemaps_buf[tilemap_id].winmat = projection_orthographic(
78 -half_size + center_offset.x,
79 half_size + center_offset.x,
80 -half_size + center_offset.y,
81 half_size + center_offset.y,
82 /* Near/far is computed on GPU using casters bounds. */
83 -1.0f,
84 1.0f);
85 }
86
87 void cascade_sync(LightData &light)
88 {
89 int level_min = light.sun.clipmap_lod_min;
90 int level_max = light.sun.clipmap_lod_max;
91 int level_range = level_max - level_min;
92 int level_len = level_range + 1;
93
94 float3 ws_camera_position = uniforms.uniform_buf.camera.viewinv[3].xyz;
95 float3 ws_camera_forward = uniforms.uniform_buf.camera.viewinv[2].xyz;
96 float camera_clip_near = uniforms.uniform_buf.camera.clip_near;
97 float camera_clip_far = uniforms.uniform_buf.camera.clip_far;
98
99 /* All tile-maps use the first level size. */
100 float level_size = shadow_directional_coverage_get(level_min);
101 float half_size = level_size / 2.0f;
102 float tile_size = level_size / float(SHADOW_TILEMAP_RES);
103
104 /* Ideally we should only take the intersection with the scene bounds. */
105 float3 ws_far_point = ws_camera_position - ws_camera_forward * camera_clip_far;
106 float3 ws_near_point = ws_camera_position - ws_camera_forward * camera_clip_near;
107
108 float3 ls_far_point = transform_direction_transposed(light.object_to_world, ws_far_point);
109 float3 ls_near_point = transform_direction_transposed(light.object_to_world, ws_near_point);
110
111 float2 local_view_direction = normalize(ls_far_point.xy - ls_near_point.xy);
112 float2 farthest_tilemap_center = local_view_direction * half_size * level_range;
113
114 /* Offset for smooth level transitions. */
115 light.object_to_world.x.w = ls_near_point.x;
116 light.object_to_world.y.w = ls_near_point.y;
117 light.object_to_world.z.w = ls_near_point.z;
118
119 /* Offset in tiles from the scene origin to the center of the first tile-maps. */
120 int2 origin_offset = int2(round(ls_near_point.xy / tile_size));
121 /* Offset in tiles between the first and the last tile-maps. */
122 int2 offset_vector = int2(round(farthest_tilemap_center / tile_size));
123
124 int2 base_offset_pos = (offset_vector * (1 << 16)) / max(level_range, 1);
125
126 /* \note cascade_level_range starts the range at the unique LOD to apply to all tile-maps. */
127 for (int i = 0; i < level_len; i++) {
128 /* Equal spacing between cascades layers since we want uniform shadow density. */
129 int2 level_offset = origin_offset + shadow_cascade_grid_offset(base_offset_pos, i);
130
131 orthographic_sync(light.tilemap_index + i,
132 light.object_to_world,
133 level_offset,
134 level_min,
135 SHADOW_PROJECTION_CASCADE);
136 }
137
138 float2 clipmap_origin = float2(origin_offset) * tile_size;
139
140 LightSunData sun_data = light.sun;
141 /* Used as origin for the clipmap_base_offset trick. */
142 sun_data.clipmap_origin = clipmap_origin;
143 /* Number of levels is limited to 32 by `clipmap_level_range()` for this reason. */
144 sun_data.clipmap_base_offset_pos = base_offset_pos;
145 sun_data.clipmap_base_offset_neg = int2(0);
146
147 light.sun = sun_data;
148 }
149
150 void clipmap_sync(LightData &light)
151 {
152 float3 ws_camera_position = uniforms.uniform_buf.camera.viewinv[3].xyz;
153 float3 ls_camera_position = transform_direction_transposed(light.object_to_world,
154 ws_camera_position);
155
156 int level_min = light.sun.clipmap_lod_min;
157 int level_max = light.sun.clipmap_lod_max;
158 int level_len = level_max - level_min + 1;
159
160 float2 clipmap_origin;
161 for (int lod = 0; lod < level_len; lod++) {
162 int level = level_min + lod;
163 /* Compute full offset from world origin to the smallest clipmap tile centered around the
164 * camera position. The offset is computed in smallest tile unit. */
165 float tile_size = float(1 << level) / float(SHADOW_TILEMAP_RES);
166 int2 level_offset = int2(round(ls_camera_position.xy / tile_size));
167
168 orthographic_sync(light.tilemap_index + lod,
169 light.object_to_world,
170 level_offset,
171 level,
172 SHADOW_PROJECTION_CLIPMAP);
173
174 clipmap_origin = float2(level_offset) * tile_size;
175 }
176
177 int2 pos_offset = int2(0);
178 int2 neg_offset = int2(0);
179 for (int lod = 0; lod < level_len - 1; lod++) {
180 /* Since offset can only differ by one tile from the higher level, we can compress that as a
181 * single integer where one bit contains offset between 2 levels. Then a single bit shift in
182 * the shader gives the number of tile to offset in the given tile-map space. However we need
183 * also the sign of the offset for each level offset. To this end, we split the negative
184 * offsets to a separate int. */
185 int2 lvl_offset_next = tilemaps_buf[light.tilemap_index + lod + 1].grid_offset;
186 int2 lvl_offset = tilemaps_buf[light.tilemap_index + lod].grid_offset;
187 int2 lvl_delta = lvl_offset - (lvl_offset_next * 2);
188 pos_offset |= max(lvl_delta, int2(0)) << lod;
189 neg_offset |= max(-lvl_delta, int2(0)) << lod;
190 }
191
192 /* Used for selecting the clipmap level. */
193 light.object_to_world.x.w = ls_camera_position.x;
194 light.object_to_world.y.w = ls_camera_position.y;
195 light.object_to_world.z.w = ls_camera_position.z;
196
197 LightSunData sun_data = light.sun;
198 /* Used as origin for the clipmap_base_offset trick. */
199 sun_data.clipmap_origin = clipmap_origin;
200 /* Number of levels is limited to 32 by `clipmap_level_range()` for this reason. */
201 sun_data.clipmap_base_offset_pos = pos_offset;
202 sun_data.clipmap_base_offset_neg = neg_offset;
203
204 light.sun = sun_data;
205 }
206
207 void cubeface_sync(int tilemap_id,
208 Transform object_to_world,
209 eCubeFace cubeface,
210 float3 jitter_offset)
211 {
212 float3 world_jitter_offset = transform_point(object_to_world, jitter_offset);
213 object_to_world.x.w = world_jitter_offset.x;
214 object_to_world.y.w = world_jitter_offset.y;
215 object_to_world.z.w = world_jitter_offset.z;
216
217 int clip_index = tilemaps_buf[tilemap_id].clip_data_index;
218 /* Avoid qualifier problems on NVidia (see #121968). */
219 Transform object_to_world_history = tilemaps_clip_buf[clip_index].object_to_world;
220 if (tilemaps_buf[tilemap_id].is_dirty ||
221 !transform_equal(object_to_world_history, object_to_world))
222 {
223 /* Set dirty as the light direction changed. */
224 tilemaps_buf[tilemap_id].grid_shift = int2(SHADOW_TILEMAP_RES);
225 tilemaps_clip_buf[clip_index].object_to_world = object_to_world;
226 }
227
228 /* Update View Matrix. */
229 /* TODO(fclem): Could avoid numerical inversion since the transform is a unit matrix. */
230 float4x4 viewmat = invert(transform_to_matrix(object_to_world));
231
232 /* Use switch instead of inline array of float3x3. */
233 switch (cubeface) {
234 case Z_NEG:
235 viewmat = to_float4x4(float3x3(+1, +0, +0, +0, +1, +0, +0, +0, +1)) * viewmat;
236 break;
237 case X_POS:
238 viewmat = to_float4x4(float3x3(+0, +0, -1, -1, +0, +0, +0, +1, +0)) * viewmat;
239 break;
240 case X_NEG:
241 viewmat = to_float4x4(float3x3(+0, +0, +1, +1, +0, +0, +0, +1, +0)) * viewmat;
242 break;
243 case Y_POS:
244 viewmat = to_float4x4(float3x3(+1, +0, +0, +0, +0, -1, +0, +1, +0)) * viewmat;
245 break;
246 case Y_NEG:
247 viewmat = to_float4x4(float3x3(-1, +0, +0, +0, +0, +1, +0, +1, +0)) * viewmat;
248 break;
249 case Z_POS:
250 viewmat = to_float4x4(float3x3(+1, +0, +0, +0, -1, +0, +0, +0, -1)) * viewmat;
251 break;
252 }
253
254 tilemaps_buf[tilemap_id].viewmat = viewmat;
255
256 /* Update corners. */
257 tilemaps_buf[tilemap_id].corners[0].xyz += jitter_offset;
258 tilemaps_buf[tilemap_id].corners[1].xyz += jitter_offset;
259 /* Other corners are deltas. They do not change after jitter. */
260 }
261};
262
263[[compute, local_size(CULLING_SELECT_GROUP_SIZE)]]
265 [[resource_table]] const Uniform &uni,
266 [[resource_table]] const Sampling &sampling,
267 [[global_invocation_id]] const uint3 global_id)
268{
269
270 uint l_idx = global_id.x;
271 /* We are dispatching (with padding) over the culled light array.
272 * This array is not contiguous. Visible local lights are packed at the beginning
273 * and directional lights at the end. There is a range of uninitialized value in between that
274 * needs to be avoided. */
275 bool valid_local = (l_idx < srt.light_cull_buf.visible_count);
276 bool valid_directional = (l_idx >= srt.light_cull_buf.local_lights_len) &&
277 (l_idx < srt.light_cull_buf.items_count);
278 if (!valid_local && !valid_directional) {
279 return;
280 }
281
282 LightData light = srt.light_buf[l_idx];
283
284 if (light.tilemap_index == LIGHT_NO_SHADOW) {
285 return;
286 }
287
288 bool use_jitter = (light.flags & LIGHT_USE_SHADOW_JITTER) != 0 &&
289 uni.uniform_buf.shadow.use_jitter;
290
291 if (is_sun_light(light.type)) {
292 /* Distant lights. */
293
294 if (use_jitter) {
295 /* TODO(fclem): Remove atan here. We only need the cosine of the angle. */
296 float shape_angle = atan_fast(light.sun.shape_radius);
297
298 /* Reverse to that first sample is straight up. */
299 float2 rand = 1.0f - sampling.rng_2D_get(SAMPLING_SHADOW_I);
300 float3 shadow_direction = sample_uniform_cone(rand, cos(shape_angle));
301
302 shadow_direction = transform_direction(light.object_to_world, shadow_direction);
303
304 if (light.sun.shadow_angle == 0.0f) {
305 /* The shape is a point. There is nothing to jitter.
306 * `shape_radius` is clamped to a minimum for precision reasons, so `shadow_angle` is
307 * set to 0 only when the light radius is also 0 to detect this case. */
308 }
309 else {
310 light.object_to_world = transform_from_matrix(to_float4x4(from_up_axis(shadow_direction)));
311 }
312 }
313
314 if (light.type == LIGHT_SUN_ORTHO) {
315 srt.cascade_sync(light);
316 }
317 else {
318 srt.clipmap_sync(light);
319 }
320 }
321 else {
322 /* Local lights. */
323 float3 position_on_light = float3(0.0f);
324
325 if (use_jitter) {
326 float3 rand = sampling.rng_3D_get(SAMPLING_SHADOW_I);
327
328 if (is_area_light(light.type)) {
329 float2 point_on_unit_shape = (light.type == LIGHT_RECT) ? rand.xy * 2.0f - 1.0f :
330 sample_disk(rand.xy);
331 position_on_light = float3(point_on_unit_shape * light.area.size, 0.0f);
332 }
333 else {
334 if (light.local.local.shadow_radius == 0.0f) {
335 /* The shape is a point. There is nothing to jitter.
336 * `shape_radius` is clamped to a minimum for precision reasons, so `shadow_radius` is
337 * set to 0 only when the light radius is also 0 to detect this case. */
338 }
339 else {
340 position_on_light = sample_ball(rand) * light.local.local.shape_radius;
341 }
342 }
343 }
344
345 int tilemap_count = light.local_tilemap_count();
346 for (int i = 0; i < tilemap_count; i++) {
347 srt.cubeface_sync(
348 light.tilemap_index + i, light.object_to_world, eCubeFace(i), position_on_light);
349 }
350
351 LightLocalData local_data = light.local;
352 local_data.local.shadow_position = position_on_light;
353 light.local = local_data;
354 }
355
356 srt.light_buf[l_idx] = light;
357}
358
360
361} // namespace eevee::light
btScalar max
#define LIGHT_NO_SHADOW
float3 sample_ball(float3 rand)
float2 sample_disk(float2 rand)
float3 sample_uniform_cone(float2 rand, float cos_angle)
#define storage(slot, qualifiers)
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
#define round
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float atan_fast(float x)
float2x2 invert(float2x2 mat)
float3x3 from_up_axis(float3 up)
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 ...
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
MatBase< float, 4, 4 > float4x4
int shadow_directional_coverage_get(int level)
PipelineCompute shadow_setup(shadow_setup_main)
void shadow_setup_main(Resources &srt, const Uniform &uni, const Sampling &sampling, const uint3 global_id)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const UniformData & uniform_buf
ShadowTileMapClip(& tilemaps_clip_buf)[]
void cubeface_sync(int tilemap_id, Transform object_to_world, eCubeFace cubeface, float3 jitter_offset)
const LightCullingData & light_cull_buf
void orthographic_sync(int tilemap_id, Transform object_to_world, int2 origin_offset, int clipmap_level, eShadowProjectionType projection_type)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline T transform_direction_transposed(const ccl_private Transform *t, const T a)
Definition transform.h:176