From d67a878e65ce47e02d3f0478b494f52dc2a44b35 Mon Sep 17 00:00:00 2001 From: Zhou Zhenglong Date: Tue, 9 Jul 2024 13:29:05 +0800 Subject: [PATCH] fix disabled shadow inconsistency --- .../pipeline/custom/NativeBuiltinUtils.cpp | 89 ++++++++++++------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp b/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp index dd39e58dbc7..3d8ed820733 100644 --- a/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp @@ -792,47 +792,24 @@ void setLightUBO( } } -const BuiltinCascadedShadowMap *getBuiltinShadowCSM( - const PipelineRuntime &pplRuntime, - const scene::Camera &camera, - const scene::DirectionalLight *mainLight) { - const auto &ppl = dynamic_cast(pplRuntime); - // no main light - if (!mainLight) { - return nullptr; - } - // not attached to a node - if (!mainLight->getNode()) { - return nullptr; - } - const pipeline::PipelineSceneData &pplSceneData = *pplRuntime.getPipelineSceneData(); - auto &csmLayers = *pplSceneData.getCSMLayers(); - const auto &shadows = *pplSceneData.getShadows(); - // shadow not enabled - if (!shadows.isEnabled()) { - return nullptr; - } - // shadow type is planar - if (shadows.getType() == scene::ShadowType::PLANAR) { - return nullptr; - } - - // find csm - const BuiltinCascadedShadowMapKey key{&camera, mainLight}; - auto iter = ppl.builtinCSMs.find(key); - if (iter != ppl.builtinCSMs.end()) { - return &iter->second; - } +namespace { +const BuiltinCascadedShadowMap *addBuiltinCSMInfo( + const NativePipeline &ppl, + const pipeline::PipelineSceneData &pplSceneData, + const scene::Camera &camera, + const scene::DirectionalLight *mainLight, + const BuiltinCascadedShadowMapKey &key, + pipeline::CSMLayers& csmLayers) { // add new csm info bool added = false; - std::tie(iter, added) = ppl.builtinCSMs.emplace( + auto res = ppl.builtinCSMs.emplace( std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple()); - CC_ENSURES(added); + CC_ENSURES(res.second); - auto &csm = iter->second; + auto &csm = res.first->second; // update csm layers csmLayers.update(&pplSceneData, &camera); @@ -873,6 +850,50 @@ const BuiltinCascadedShadowMap *getBuiltinShadowCSM( return &csm; } +} // namespace + +const BuiltinCascadedShadowMap *getBuiltinShadowCSM( + const PipelineRuntime &pplRuntime, + const scene::Camera &camera, + const scene::DirectionalLight *mainLight) { + const auto &ppl = dynamic_cast(pplRuntime); + // no main light + if (!mainLight) { + return nullptr; + } + // not attached to a node + if (!mainLight->getNode()) { + return nullptr; + } + const pipeline::PipelineSceneData &pplSceneData = *pplRuntime.getPipelineSceneData(); + auto &csmLayers = *pplSceneData.getCSMLayers(); + const auto &shadows = *pplSceneData.getShadows(); + + const BuiltinCascadedShadowMap *result = nullptr; + + { // find or create csm info + const BuiltinCascadedShadowMapKey key{&camera, mainLight}; + auto iter = ppl.builtinCSMs.find(key); + if (iter != ppl.builtinCSMs.end()) { + result = &iter->second; + } else { + result = addBuiltinCSMInfo(ppl, pplSceneData, camera, mainLight, key, csmLayers); + } + } + CC_ENSURES(result); + + // shadow not enabled + if (!shadows.isEnabled()) { + return nullptr; + } + // shadow type is planar + if (shadows.getType() == scene::ShadowType::PLANAR) { + return nullptr; + } + + return result; +} + const geometry::Frustum &getBuiltinShadowFrustum( const PipelineRuntime &pplRuntime, const scene::Camera &camera,