EEVEE-Next: Shadow: Add LOD system to directional clipmap shadows #120031

Merged
Clément Foucault merged 16 commits from fclem/blender:eevee-next-shadow-directional-lod into main 2024-03-29 16:23:06 +01:00
2 changed files with 126 additions and 105 deletions
Showing only changes of commit eb98c89f10 - Show all commits

View File

@ -32,6 +32,8 @@ void main()
if (light.type != LIGHT_SUN) {
break;
}
ivec2 base_offset_neg = light_sun_data_get(light).clipmap_base_offset_neg;
ivec2 base_offset_pos = light_sun_data_get(light).clipmap_base_offset_pos;
/* LOD relative max with respect to clipmap_lod_min. */
int lod_max = light_sun_data_get(light).clipmap_lod_max -
light_sun_data_get(light).clipmap_lod_min;
@ -45,17 +47,22 @@ void main()
if (lod != lod_max && !tile.is_valid) {
/* Offset this LOD has with the previous one. In unit of tile of the current LOD. */
ivec2 offset = ivec2(SHADOW_TILEMAP_RES / 2) +
0 /* TODO offset from clipmap_base_offset. */;
/* Load tile from the previous LOD. */
ivec2 tile_co_prev = (tile_co + offset) >> 1;
ivec2 offset_binary = ((base_offset_pos >> lod) & 1) - ((base_offset_neg >> lod) & 1);
ivec2 offset_centered = ivec2(SHADOW_TILEMAP_RES / 2) + offset_binary;
ivec2 tile_co_prev = (tile_co + offset_centered) >> 1;
/* Load tile from the previous LOD. */
ShadowSamplingTilePacked tile_prev_packed = tiles_local[tile_co_prev.y][tile_co_prev.x];
ShadowSamplingTile tile_prev = shadow_sampling_tile_unpack(tile_prev_packed);
if (tile_prev.is_valid) {
/* Add the offset of this tilemap to the tile data. */
tile_prev.lod_offset += offset;
/* Previous level is now twice as big as this level.
* Double the offset to the valid page. */
tile_prev.lod_offset = tile_prev.lod_offset * 2;
/* Add the offset of this tile relative to the previous level to the tile data.
* There is only an offset if offset is odd since previous level is twice as big. */
tile_prev.lod_offset += ivec2(not(equal(offset_binary, ivec2(0))));
tile_prev_packed = shadow_sampling_tile_pack(tile_prev);
/* Replace the missing page with the one from the lower LOD. */
imageStore(tilemaps_img, atlas_texel, uvec4(tile_prev_packed));

View File

@ -1186,7 +1186,7 @@ static void test_eevee_shadow_tilemap_amend()
tile.page = uint3(2, 0, 0);
pixel_get(17, 16, 2) = shadow_sampling_tile_pack(tile);
tile.page = uint3(3, 0, 0);
pixel_get(17, 16, 1) = shadow_sampling_tile_pack(tile);
pixel_get(20, 20, 1) = shadow_sampling_tile_pack(tile);
tile.page = uint3(4, 0, 0);
pixel_get(17, 16, 0) = shadow_sampling_tile_pack(tile);
@ -1203,6 +1203,10 @@ static void test_eevee_shadow_tilemap_amend()
light.type = LIGHT_SUN;
light.sun.clipmap_lod_min = 0;
light.sun.clipmap_lod_max = 2;
/* Shift LOD0 by 1 tile towards bottom. */
light.sun.clipmap_base_offset_neg = int2(0, 1 << 0);
/* Shift LOD1 by 1 tile towards right. */
light.sun.clipmap_base_offset_pos = int2(1 << 1, 0);
light.tilemap_index = 0;
LightDataBuf culling_light_buf = {"Lights_culled"};
@ -1246,118 +1250,128 @@ static void test_eevee_shadow_tilemap_amend()
tilemap_index * SHADOW_TILEMAP_RES;
ShadowSamplingTile tile = shadow_sampling_tile_unpack(pixels[tile_ofs]);
result += std::to_string(tile.page.x + tile.page.y * SHADOW_PAGE_PER_ROW);
if (x + 1 == SHADOW_TILEMAP_RES / 2) {
result += " ";
}
}
result += "\n";
if (y + 1 == SHADOW_TILEMAP_RES / 2) {
result += "\n";
}
}
return result;
};
/** The layout of these expected strings is Y down. */
StringRefNull expected_pages_lod2 =
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000001200000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n";
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"\n"
"0000000000000000 1200000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n";
StringRefNull expected_pages_lod1 =
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000001322000000000000\n"
"00000000000000001122000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n";
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"\n"
"0000000000000001 1220000000000000\n"
"0000000000000001 1220000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000300000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n";
StringRefNull expected_pages_lod0 =
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000001433222200000000\n"
"00000000000000001133222200000000\n"
"00000000000000001111222200000000\n"
"00000000000000001111222200000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n"
"00000000000000000000000000000000\n";
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"\n"
"0000000000000000 0400000000000000\n"
"0000000000000011 1122220000000000\n"
"0000000000000011 1122220000000000\n"
"0000000000000011 1122220000000000\n"
"0000000000000011 1122220000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000033000000\n"
"0000000000000000 0000000033000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n"
"0000000000000000 0000000000000000\n";
EXPECT_EQ(expected_pages_lod0, stringify_tilemap(0));
EXPECT_EQ(expected_pages_lod1, stringify_tilemap(1));
EXPECT_EQ(expected_pages_lod2, stringify_tilemap(2));
EXPECT_EQ(expected_pages_lod1, stringify_tilemap(1));
EXPECT_EQ(expected_pages_lod0, stringify_tilemap(0));
MEM_SAFE_FREE(pixels);
}