@@ -1441,6 +1441,24 @@ namespace cage
14411441 return data;
14421442 }
14431443
1444+ bool allowShadowmapCacheReuse (const ShadowmapData &shadowmap, const UniShadowedLight &cache) const
1445+ {
1446+ if ((frameIndex % shadowmap.shadowmapComponent .cascadesCount ) == shadowmap.cascade )
1447+ return false ;
1448+
1449+ const auto &get = [&](const UniShadowedLight &l) -> std::pair<Vec3, Vec3>
1450+ {
1451+ const Mat4 &m = l.shadowMat [shadowmap.cascade ];
1452+ const Vec3 f = Vec3 (m * Vec4 (0 , 0 , 0 , 1 )); // origin
1453+ const Vec3 s = Vec3 (m * Vec4 (0 , 0 , -1 , 0 )); // forward
1454+ return { f, s };
1455+ };
1456+
1457+ const auto c = get (shadowmap.shadowUni );
1458+ const auto d = get (cache);
1459+ return distance (c.first , d.first ) < 0.1 && distance (c.second , d.second ) < 0.01 ;
1460+ }
1461+
14441462 void initializeShadowmapCascade ()
14451463 {
14461464 CAGE_ASSERT (shadowmap->lightComponent .lightType == LightTypeEnum::Directional);
@@ -1557,17 +1575,17 @@ namespace cage
15571575
15581576 // progressive rendering
15591577 Holder<UniShadowedLight> cache = provisionalGraphics->cpuBuffer <UniShadowedLight>(Stringizer () + name + " _cache_" + shadowmap->cascade );
1560- if ((frameIndex % sc.cascadesCount ) == shadowmap->cascade )
1561- {
1562- // render the cascade
1563- *cache = shadowmap->shadowUni ;
1564- }
1565- else
1578+ if (allowShadowmapCacheReuse (*shadowmap, *cache))
15661579 {
15671580 // reuse from earlier
15681581 shadowmap->shadowUni = *cache;
15691582 shadowmap->doesRendering = false ;
15701583 }
1584+ else
1585+ {
1586+ // render the cascade
1587+ *cache = shadowmap->shadowUni ;
1588+ }
15711589 }
15721590
15731591 void initializeShadowmapSingle ()
0 commit comments