Blender V5.3
eevee_shadow_tilemap_lib.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
7#include "draw_shape.bsl.hh"
12
18{
19 /* TODO(fclem): Experiment with non squared viewports. */
20 return int2(int(1u << viewport_index));
21}
22
23/* ---------------------------------------------------------------------- */
26
28{
29 return tile.x + tile.y * SHADOW_TILEMAP_RES;
30}
31
33{
34 return uint2(uint(tile_index % SHADOW_TILEMAP_RES), uint(tile_index / SHADOW_TILEMAP_RES));
35}
36
37/* Return bottom left pixel position of the tile-map inside the tile-map atlas. */
38uint2 shadow_tilemap_start(int tilemap_index)
39{
40 return SHADOW_TILEMAP_RES * uint2(uint(tilemap_index % SHADOW_TILEMAP_PER_ROW),
41 uint(tilemap_index / SHADOW_TILEMAP_PER_ROW));
42}
43
45{
46 return shadow_tilemap_start(tilemap_index) + tile;
47}
48
53int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
54{
55#ifdef GLSL_CPP_STUBS
56 static_assert(SHADOW_TILEMAP_LOD == 5);
57#endif
58 constexpr int lod0_width = SHADOW_TILEMAP_RES / 1;
59 constexpr int lod1_width = SHADOW_TILEMAP_RES / 2;
60 constexpr int lod2_width = SHADOW_TILEMAP_RES / 4;
61 constexpr int lod3_width = SHADOW_TILEMAP_RES / 8;
62 constexpr int lod4_width = SHADOW_TILEMAP_RES / 16;
63 constexpr int lod5_width = SHADOW_TILEMAP_RES / 32;
64 constexpr int lod0_size = lod0_width * lod0_width;
65 constexpr int lod1_size = lod1_width * lod1_width;
66 constexpr int lod2_size = lod2_width * lod2_width;
67 constexpr int lod3_size = lod3_width * lod3_width;
68 constexpr int lod4_size = lod4_width * lod4_width;
69
70 /* TODO(fclem): Convert everything to uint. */
71 int offset = tiles_index;
72 switch (lod) {
73 case 5:
74 offset += lod0_size + lod1_size + lod2_size + lod3_size + lod4_size;
75 offset += int(tile.y) * lod5_width;
76 break;
77 case 4:
78 offset += lod0_size + lod1_size + lod2_size + lod3_size;
79 offset += int(tile.y) * lod4_width;
80 break;
81 case 3:
82 offset += lod0_size + lod1_size + lod2_size;
83 offset += int(tile.y) * lod3_width;
84 break;
85 case 2:
86 offset += lod0_size + lod1_size;
87 offset += int(tile.y) * lod2_width;
88 break;
89 case 1:
90 offset += lod0_size;
91 offset += int(tile.y) * lod1_width;
92 break;
93 case 0:
94 default:
95 offset += int(tile.y) * lod0_width;
96 break;
97 }
98 offset += int(tile.x);
99 return offset;
100}
101
103
104/* ---------------------------------------------------------------------- */
107
109ShadowSamplingTile shadow_tile_load(usampler2D tilemaps_tx, uint2 tile_co, int tilemap_index)
110{
111 /* NOTE(@fclem): This clamp can hide some small imprecision at clip-map transition.
112 * Can be disabled to check if the clip-map is well centered. */
113 tile_co = clamp(tile_co, uint2(0), uint2(SHADOW_TILEMAP_RES - 1));
114 uint2 texel = shadow_tile_coord_in_atlas(tile_co, tilemap_index);
115 uint tile_data = texelFetch(tilemaps_tx, int2(texel), 0).x;
116 return shadow_sampling_tile_unpack(tile_data);
117}
118
119#if 0 /* TODO(fclem): Finish. We can simplify sampling logic and only tag radially. */
120
129int shadow_directional_tilemap_index(LightData light, float3 lP)
130{
131 LightSunData sun = light.sun;
132 int lvl;
133 if (light.type == LIGHT_SUN) {
134 /* We need to hide one tile worth of data to hide the moving transition. */
135 constexpr float narrowing = float(SHADOW_TILEMAP_RES) / (float(SHADOW_TILEMAP_RES) - 1.0001f);
136 /* Avoid using log2 when we can just get the exponent from the floating point. */
137 frexp(reduce_max(abs(lP)) * narrowing * 2.0f, lvl);
138 }
139 else {
140 /* Since we want half of the size, bias the level by -1. */
141 /* TODO(fclem): Precompute. */
142 float lod_min_half_size = exp2(float(sun.clipmap_lod_min - 1));
143 lvl = reduce_max(lP.xy) / lod_min_half_size;
144 }
145 return light.clamp(lvl, sun.clipmap_lod_min, sun.clipmap_lod_max);
146}
147
148#endif
149
157{
158 float lod;
159 if (light.type == LIGHT_SUN) {
160 /* We need to hide one tile worth of data to hide the moving transition. */
161 constexpr float narrowing = float(SHADOW_TILEMAP_RES) / (float(SHADOW_TILEMAP_RES) - 1.0001f);
162 /* Since the distance is centered around the camera (and thus by extension the tile-map),
163 * we need to multiply by 2 to get the lod level which covers the following range:
164 * [-coverage_get(lod)/2..coverage_get(lod)/2] */
165 lod = log2(length(lP) * narrowing * 2.0f);
166 /* Apply light LOD bias. */
167 lod = max(lod + light.lod_bias, light.lod_min);
168 }
169 else {
170 /* The narrowing need to be stronger since the tile-map position is not rounded but floored. */
171 constexpr float narrowing = float(SHADOW_TILEMAP_RES) / (float(SHADOW_TILEMAP_RES) - 2.5001f);
172 /* Since we want half of the size, bias the level by -1. */
173 float clipmap_lod_min_minus_one = float(light.sun.clipmap_lod_min - 1);
174 float lod_min_half_size = exp2(clipmap_lod_min_minus_one);
175 lod = length(lP.xy) * narrowing / lod_min_half_size;
176 /* Apply cascade lod bias. Light bias is not supported here. */
177 lod += clipmap_lod_min_minus_one;
178 }
179 return clamp(lod, float(light.sun.clipmap_lod_min), float(light.sun.clipmap_lod_max));
180}
181
182int shadow_directional_level(LightData light, float3 lP)
183{
184 /* The level can be negative and is increasing with the distance.
185 * So we have to ceil instead of flooring. */
186 return int(ceil(shadow_directional_level_fractional(light, lP)));
187}
188
193float shadow_punctual_pixel_ratio(LightData light,
194 float3 lP,
195 bool is_perspective,
196 float distance_to_camera,
197 float film_pixel_radius)
198{
199 film_pixel_radius *= exp2(light.lod_bias);
200
201 float distance_to_light = reduce_max(abs(lP));
202 /* We project a shadow map pixel (as a sphere for simplicity) to the receiver plane.
203 * We then reproject this sphere onto the camera screen and compare it to the film pixel size.
204 * This gives a good approximation of what LOD to select to get a somewhat uniform shadow map
205 * resolution in screen space. */
206 float film_pixel_footprint = (is_perspective) ? film_pixel_radius * distance_to_camera :
207 film_pixel_radius;
208 /* Clamp in world space. */
209 film_pixel_footprint = max(film_pixel_footprint, light.lod_min);
210 /* Project onto light's unit plane (per cubeface). */
211 film_pixel_footprint /= distance_to_light;
212 /* Clamp in shadow space. */
213 film_pixel_footprint = max(film_pixel_footprint, -light.lod_min);
214 /* Cube-face diagonal divided by LOD0 resolution. */
215 constexpr float shadow_pixel_radius = (2.0f * M_SQRT2) / SHADOW_MAP_MAX_RES;
216 return saturate(shadow_pixel_radius / film_pixel_footprint);
217}
218
224 float3 lP,
225 bool is_perspective,
226 float distance_to_camera,
227 float film_pixel_radius)
228{
229 float ratio = shadow_punctual_pixel_ratio(
230 light, lP, is_perspective, distance_to_camera, film_pixel_radius);
231 return clamp(-log2(ratio), 0.0f, float(SHADOW_TILEMAP_LOD));
232}
233
234int shadow_punctual_level(LightData light,
235 float3 lP,
236 bool is_perspective,
237 float distance_to_camera,
238 float film_pixel_radius)
239{
240 /* Conversion to positive int is the same as floor. */
242 light, lP, is_perspective, distance_to_camera, film_pixel_radius));
243}
244
246 /* Index of the tile-map to containing the tile. */
248 /* Texel coordinates in [0..SHADOW_MAP_MAX_RES) range. */
250 /* Tile coordinate in [0..SHADOW_TILEMAP_RES) range. */
252};
253
254/* Assumes tilemap_uv is already saturated. */
256{
258 ret.tilemap_index = tilemap_index;
259 ret.tilemap_texel = uint2(tilemap_uv * (float(SHADOW_MAP_MAX_RES) - 1e-2f));
260 ret.tilemap_tile = ret.tilemap_texel >> uint(SHADOW_PAGE_LOD);
261 return ret;
262}
263
264/* Retain sign bit and avoid costly int division. */
266 int2 offset_neg,
267 int2 offset_pos,
268 int level_relative)
269{
270 if (light_type == LIGHT_SUN_ORTHO) {
271 return shadow_cascade_grid_offset(offset_pos, level_relative);
272 }
273 return (offset_pos >> level_relative) - (offset_neg >> level_relative);
274}
275
280{
281 /* This difference needs to be less than 32 for the later shift to be valid.
282 * This is ensured by `ShadowDirectional::clipmap_level_range()`. */
283 int level_relative = level - light.sun.clipmap_lod_min;
284 int lod_relative = (light.type == LIGHT_SUN_ORTHO) ? light.sun.clipmap_lod_min : level;
285 /* Compute offset in tile. */
286 int2 clipmap_offset = shadow_decompress_grid_offset(light.type,
287 light.sun.clipmap_base_offset_neg,
288 light.sun.clipmap_base_offset_pos,
289 level_relative);
290 /* UV in [0..1] range over the tilemap. */
291 float2 tilemap_uv = lP.xy - light.sun.clipmap_origin;
292 tilemap_uv *= exp2(float(-lod_relative));
293 tilemap_uv -= float2(clipmap_offset) * (1.0f / float(SHADOW_TILEMAP_RES));
294 tilemap_uv = saturate(tilemap_uv + 0.5f);
295
296 return shadow_coordinate_from_uvs(light.tilemap_index + level_relative, tilemap_uv);
297}
298
303{
304 int level = shadow_directional_level(light, lP - light.position());
305 return shadow_directional_coordinates_at_level(light, lP, level);
306}
307
308/* Transform vector to face local coordinate. */
310{
311 switch (face_id) {
312 case 1:
313 return float3(-lL.y, lL.z, -lL.x);
314 case 2:
315 return float3(lL.y, lL.z, lL.x);
316 case 3:
317 return float3(lL.x, lL.z, -lL.y);
318 case 4:
319 return float3(-lL.x, lL.z, lL.y);
320 case 5:
321 return float3(lL.x, -lL.y, -lL.z);
322 default:
323 return lL;
324 }
325}
326
328{
329 switch (face_id) {
330 case 1:
331 return float3(-fL.z, -fL.x, fL.y);
332 case 2:
333 return float3(fL.z, fL.x, fL.y);
334 case 3:
335 return float3(fL.x, -fL.z, fL.y);
336 case 4:
337 return float3(-fL.x, fL.z, fL.y);
338 case 5:
339 return float3(fL.x, -fL.y, -fL.z);
340 default:
341 return fL;
342 }
343}
344
345/* Turns local light coordinate into shadow region index. Matches eCubeFace order.
346 * \note lL does not need to be normalized. */
348{
349 float3 aP = abs(lL);
350 if (all(greaterThan(aP.xx, aP.yz))) {
351 return (lL.x > 0.0f) ? 1 : 2;
352 }
353 if (all(greaterThan(aP.yy, aP.xz))) {
354 return (lL.y > 0.0f) ? 3 : 4;
355 }
356 return (lL.z > 0.0f) ? 5 : 0;
357}
358
363ShadowCoordinates shadow_punctual_coordinates(LightData light, float3 lP, int face_id)
364{
365 /* UVs in [-1..+1] range. */
366 float2 tilemap_uv = lP.xy / abs(lP.z);
367 /* UVs in [0..1] range. */
368 tilemap_uv = saturate(tilemap_uv * 0.5f + 0.5f);
369
370 return shadow_coordinate_from_uvs(light.tilemap_index + face_id, tilemap_uv);
371}
372
374
375/* ---------------------------------------------------------------------- */
378
379float3 shadow_tile_corner_persp(ShadowTileMapData tilemap, int2 tile)
380{
381 return tilemap.corners[1].xyz + tilemap.corners[2].xyz * float(tile.x) +
382 tilemap.corners[3].xyz * float(tile.y);
383}
384
385Pyramid shadow_tilemap_cubeface_bounds(ShadowTileMapData tilemap,
386 int2 tile_start,
387 const int2 extent)
388{
389 Pyramid shape;
390 shape.corners[0] = tilemap.corners[0].xyz;
391 shape.corners[1] = shadow_tile_corner_persp(tilemap, tile_start + int2(0, 0));
392 shape.corners[2] = shadow_tile_corner_persp(tilemap, tile_start + int2(extent.x, 0));
393 shape.corners[3] = shadow_tile_corner_persp(tilemap, tile_start + extent);
394 shape.corners[4] = shadow_tile_corner_persp(tilemap, tile_start + int2(0, extent.y));
395 return shape;
396}
397
399
400/* ---------------------------------------------------------------------- */
406
407int shadow_render_page_index_get(int view_index, int2 tile_coordinate_in_lod)
408{
409 return view_index * SHADOW_TILEMAP_LOD0_LEN + tile_coordinate_in_lod.y * SHADOW_TILEMAP_RES +
410 tile_coordinate_in_lod.x;
411}
412
#define M_SQRT2
btScalar max
ShadowCoordinates shadow_directional_coordinates_at_level(LightData light, float3 lP, int level)
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
ShadowCoordinates shadow_coordinate_from_uvs(int tilemap_index, float2 tilemap_uv)
ShadowCoordinates shadow_punctual_coordinates(LightData light, float3 lP, int face_id)
int shadow_tile_index(int2 tile)
float shadow_punctual_level_fractional(LightData light, float3 lP, bool is_perspective, float distance_to_camera, float film_pixel_radius)
int shadow_punctual_face_index_get(float3 lL)
float3 shadow_punctual_face_local_to_local_position(int face_id, float3 fL)
int shadow_punctual_level(LightData light, float3 lP, bool is_perspective, float distance_to_camera, float film_pixel_radius)
float shadow_directional_level_fractional(LightData light, float3 lP)
Pyramid shadow_tilemap_cubeface_bounds(ShadowTileMapData tilemap, int2 tile_start, const int2 extent)
float shadow_punctual_pixel_ratio(LightData light, float3 lP, bool is_perspective, float distance_to_camera, float film_pixel_radius)
uint2 shadow_tile_coord(int tile_index)
ShadowCoordinates shadow_directional_coordinates(LightData light, float3 lP)
uint2 shadow_tilemap_start(int tilemap_index)
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_decompress_grid_offset(eLightType light_type, int2 offset_neg, int2 offset_pos, int level_relative)
ShadowSamplingTile shadow_tile_load(usampler2D tilemaps_tx, uint2 tile_co, int tilemap_index)
float3 shadow_punctual_local_position_to_face_local(int face_id, float3 lL)
float3 shadow_tile_corner_persp(ShadowTileMapData tilemap, int2 tile)
int shadow_directional_level(LightData light, float3 lP)
int2 shadow_viewport_size_get(uint viewport_index)
#define viewport_index
#define exp2
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
#define log2
#define abs
#define ceil
bool all(VecOp< bool, D >) RET
#define frexp
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< uint, 2 > usampler2D
float reduce_max(float2 a)
const int tile_index
const ccl_global KernelWorkTile * tile
@ LIGHT_SUN
VecBase< uint32_t, 2 > uint2
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
float3 corners[5]
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136