From 51acbb5df4958b2bfafb07074b3a0cbcecd74630 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Tue, 6 Aug 2024 16:43:12 -0700 Subject: [PATCH 01/14] Increased texSize only for dir lights and kept texSize same for spot/point lights Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 17b870cc2..e4ccdc742 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -651,15 +651,15 @@ void Ogre2Scene::UpdateShadowNode() // directional lights unsigned int atlasId = 0u; - unsigned int texSize = 2048u; - unsigned int halfTexSize = static_cast(texSize * 0.5); + unsigned int dirTexSize = 4096u; + unsigned int halfTexSize = static_cast(dirTexSize * 0.5); for (unsigned int i = 0; i < dirLightCount; ++i) { shadowParam.technique = Ogre::SHADOWMAP_PSSM; shadowParam.atlasId = atlasId; shadowParam.numPssmSplits = 3u; - shadowParam.resolution[0].x = texSize; - shadowParam.resolution[0].y = texSize; + shadowParam.resolution[0].x = dirTexSize; + shadowParam.resolution[0].y = dirTexSize; shadowParam.resolution[1].x = halfTexSize; shadowParam.resolution[1].y = halfTexSize; shadowParam.resolution[2].x = halfTexSize; @@ -667,9 +667,9 @@ void Ogre2Scene::UpdateShadowNode() shadowParam.atlasStart[0].x = 0u; shadowParam.atlasStart[0].y = 0u; shadowParam.atlasStart[1].x = 0u; - shadowParam.atlasStart[1].y = texSize; + shadowParam.atlasStart[1].y = dirTexSize; shadowParam.atlasStart[2].x = halfTexSize; - shadowParam.atlasStart[2].y = texSize; + shadowParam.atlasStart[2].y = dirTexSize; shadowParam.supportedLightTypes = 0u; shadowParam.addLightType(Ogre::Light::LT_DIRECTIONAL); shadowParams.push_back(shadowParam); @@ -678,19 +678,20 @@ void Ogre2Scene::UpdateShadowNode() // others unsigned int maxTexSize = 8192u; + unsigned int spotPointTexSize = 2048u; unsigned int rowIdx = 0; unsigned int colIdx = 0; - unsigned int rowSize = maxTexSize / texSize; + unsigned int rowSize = maxTexSize / spotPointTexSize; unsigned int colSize = rowSize; for (unsigned int i = 0; i < spotPointLightCount; ++i) { shadowParam.technique = Ogre::SHADOWMAP_FOCUSED; shadowParam.atlasId = atlasId; - shadowParam.resolution[0].x = texSize; - shadowParam.resolution[0].y = texSize; - shadowParam.atlasStart[0].x = colIdx * texSize; - shadowParam.atlasStart[0].y = rowIdx * texSize; + shadowParam.resolution[0].x = spotPointTexSize; + shadowParam.resolution[0].y = spotPointTexSize; + shadowParam.atlasStart[0].x = colIdx * spotPointTexSize; + shadowParam.atlasStart[0].y = rowIdx * spotPointTexSize; shadowParam.supportedLightTypes = 0u; shadowParam.addLightType(Ogre::Light::LT_DIRECTIONAL); From 44b1f530708b5f5090134b6960adc7c4229cfe68 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Wed, 7 Aug 2024 14:22:47 -0700 Subject: [PATCH 02/14] Expose texsize in sdf Signed-off-by: Athena Z --- include/gz/rendering/Scene.hh | 2 ++ include/gz/rendering/base/BaseScene.hh | 2 ++ ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh | 2 ++ ogre2/src/Ogre2Scene.cc | 9 ++++++++- src/base/BaseScene.cc | 5 +++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 3d15d960f..743e66083 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1270,6 +1270,8 @@ namespace gz /// \return true to sky is enabled, false otherwise public: virtual bool SkyEnabled() const = 0; + public: virtual void SetTexSize(unsigned int _texSize) = 0; + /// \brief Sets the given GI as the current new active GI solution /// \param[in] _gi GI solution that should be active. Nullptr to disable public: virtual void SetActiveGlobalIllumination( diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index d5c35f3e3..05b6e96d7 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -628,6 +628,8 @@ namespace gz // Documentation inherited. public: virtual bool SkyEnabled() const override; + public: virtual void SetTexSize(unsigned int _texSize) override; + // Documentation inherited. public: virtual void SetActiveGlobalIllumination( GlobalIlluminationBasePtr _gi) override; diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index b54625953..1dd0f89a9 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -102,6 +102,8 @@ namespace gz // Documentation inherited public: virtual bool SkyEnabled() const override; + public: void SetTexSize(unsigned int _texSize); + // Documentation inherited public: virtual void SetActiveGlobalIllumination( GlobalIlluminationBasePtr _gi) override; diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index e4ccdc742..84231fc7b 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -90,6 +90,8 @@ class gz::rendering::Ogre2ScenePrivate /// \brief Flag to indicate if sky is enabled or not public: bool skyEnabled = false; + public: unsigned int dirTexSize = 2048u; + /// \brief Flag to alert the user its usage of PreRender/PostRender /// is incorrect public: bool frameUpdateStarted = false; @@ -651,7 +653,7 @@ void Ogre2Scene::UpdateShadowNode() // directional lights unsigned int atlasId = 0u; - unsigned int dirTexSize = 4096u; + unsigned int dirTexSize = this->dataPtr->dirTexSize; unsigned int halfTexSize = static_cast(dirTexSize * 0.5); for (unsigned int i = 0; i < dirLightCount; ++i) { @@ -1555,6 +1557,11 @@ bool Ogre2Scene::SkyEnabled() const return this->dataPtr->skyEnabled; } +void Ogre2Scene::SetTexSize(unsigned int _texSize) +{ + this->dataPtr->dirTexSize = _texSize; +} + ////////////////////////////////////////////////// void Ogre2Scene::SetActiveGlobalIllumination(GlobalIlluminationBasePtr _gi) { diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index bdd997438..a750d5d39 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1475,6 +1475,11 @@ bool BaseScene::SkyEnabled() const return false; } +void BaseScene::SetTexSize(unsigned int _texSize) +{ + return this->SetTexSize(_texSize); +} + ////////////////////////////////////////////////// void BaseScene::SetActiveGlobalIllumination(GlobalIlluminationBasePtr _gi) { From 377549993ebaf14cfc729187e1e76fb93f5ecd01 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Fri, 9 Aug 2024 09:45:34 -0700 Subject: [PATCH 03/14] For clarity, renamed func/vars. Also if using OpenGL API, ensure maxTexSize can use GL_MAX_TEXTURE_SIZE instead of currently hardcoded val Signed-off-by: Athena Z --- include/gz/rendering/Scene.hh | 2 +- include/gz/rendering/base/BaseScene.hh | 2 +- ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh | 2 +- ogre2/src/Ogre2Scene.cc | 17 ++++++++++++++--- src/base/BaseScene.cc | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 743e66083..5b2b0136c 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1270,7 +1270,7 @@ namespace gz /// \return true to sky is enabled, false otherwise public: virtual bool SkyEnabled() const = 0; - public: virtual void SetTexSize(unsigned int _texSize) = 0; + public: virtual void SetShadowTextureSize(unsigned int _textureSize) = 0; /// \brief Sets the given GI as the current new active GI solution /// \param[in] _gi GI solution that should be active. Nullptr to disable diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index 05b6e96d7..d367548a7 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -628,7 +628,7 @@ namespace gz // Documentation inherited. public: virtual bool SkyEnabled() const override; - public: virtual void SetTexSize(unsigned int _texSize) override; + public: virtual void SetShadowTextureSize(unsigned int _textureSize) override; // Documentation inherited. public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 1dd0f89a9..83d9e210b 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -102,7 +102,7 @@ namespace gz // Documentation inherited public: virtual bool SkyEnabled() const override; - public: void SetTexSize(unsigned int _texSize); + public: void SetShadowTextureSize(unsigned int _textureSize); // Documentation inherited public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 84231fc7b..b8519b8d5 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -15,6 +15,8 @@ * */ +#include + #include #include "gz/rendering/base/SceneExt.hh" @@ -678,8 +680,17 @@ void Ogre2Scene::UpdateShadowNode() atlasId++; } - // others unsigned int maxTexSize = 8192u; + + if (engine->GraphicsAPI() == GraphicsAPI::OPENGL) + { + GLint glMaxTexSize; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexSize); + + maxTexSize = glMaxTexSize; + } + + // others unsigned int spotPointTexSize = 2048u; unsigned int rowIdx = 0; unsigned int colIdx = 0; @@ -1557,9 +1568,9 @@ bool Ogre2Scene::SkyEnabled() const return this->dataPtr->skyEnabled; } -void Ogre2Scene::SetTexSize(unsigned int _texSize) +void Ogre2Scene::SetShadowTextureSize(unsigned int _textureSize) { - this->dataPtr->dirTexSize = _texSize; + this->dataPtr->dirTexSize = _textureSize; } ////////////////////////////////////////////////// diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index a750d5d39..ba316b098 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1475,9 +1475,9 @@ bool BaseScene::SkyEnabled() const return false; } -void BaseScene::SetTexSize(unsigned int _texSize) +void BaseScene::SetShadowTextureSize(unsigned int _textureSize) { - return this->SetTexSize(_texSize); + return this->SetShadowTextureSize(_textureSize); } ////////////////////////////////////////////////// From e33d973a9402db086880dbe184fc43e5082fb584 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Sun, 11 Aug 2024 15:39:38 -0700 Subject: [PATCH 04/14] Add LightType enum to Light, and also as func param Signed-off-by: Athena Z --- include/gz/rendering/Light.hh | 9 +++++++++ include/gz/rendering/Scene.hh | 6 +++++- include/gz/rendering/base/BaseScene.hh | 3 ++- ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh | 2 +- ogre2/src/Ogre2Scene.cc | 7 +++++-- src/base/BaseScene.cc | 9 +++++++-- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/include/gz/rendering/Light.hh b/include/gz/rendering/Light.hh index 1af5dbe96..95ffa462e 100644 --- a/include/gz/rendering/Light.hh +++ b/include/gz/rendering/Light.hh @@ -26,6 +26,15 @@ namespace gz namespace rendering { inline namespace GZ_RENDERING_VERSION_NAMESPACE { + + enum GZ_RENDERING_VISIBLE LightType + { + LT_EMPTY = 0, + LT_POINT = 1, + LT_DIRECTIONAL = 2, + LT_SPOT = 3 + }; + // /// \class Light Light.hh gz/rendering/Light.hh /// \brief Represents a light source in the scene graph diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 5b2b0136c..559187579 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -34,6 +34,7 @@ #include "gz/rendering/RenderTypes.hh" #include "gz/rendering/Storage.hh" #include "gz/rendering/Export.hh" +#include "gz/rendering/Light.hh" namespace gz { @@ -1270,7 +1271,10 @@ namespace gz /// \return true to sky is enabled, false otherwise public: virtual bool SkyEnabled() const = 0; - public: virtual void SetShadowTextureSize(unsigned int _textureSize) = 0; + /// @brief \brief Set the shadow texture size for the given light type. + /// @param _lightType Light type that creates the shadow + /// @param _textureSize Shadow texture size + public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) = 0; /// \brief Sets the given GI as the current new active GI solution /// \param[in] _gi GI solution that should be active. Nullptr to disable diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index d367548a7..56dce3d8a 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -628,7 +628,8 @@ namespace gz // Documentation inherited. public: virtual bool SkyEnabled() const override; - public: virtual void SetShadowTextureSize(unsigned int _textureSize) override; + // Documentation inherited. + public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) override; // Documentation inherited. public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 83d9e210b..d91258ff0 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -102,7 +102,7 @@ namespace gz // Documentation inherited public: virtual bool SkyEnabled() const override; - public: void SetShadowTextureSize(unsigned int _textureSize); + public: void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize); // Documentation inherited public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index b8519b8d5..ad25cbef8 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1568,9 +1568,12 @@ bool Ogre2Scene::SkyEnabled() const return this->dataPtr->skyEnabled; } -void Ogre2Scene::SetShadowTextureSize(unsigned int _textureSize) +void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { - this->dataPtr->dirTexSize = _textureSize; + if (_lightType == LightType::LT_DIRECTIONAL) + { + this->dataPtr->dirTexSize = _textureSize; + } } ////////////////////////////////////////////////// diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index ba316b098..ccf854707 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1475,9 +1475,14 @@ bool BaseScene::SkyEnabled() const return false; } -void BaseScene::SetShadowTextureSize(unsigned int _textureSize) +////////////////////////////////////////////////// +void BaseScene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { - return this->SetShadowTextureSize(_textureSize); + if (_lightType || _textureSize) + { + gzerr << "Setting shadow texture size not supported by: " + << this->Engine()->Name() << std::endl; + } } ////////////////////////////////////////////////// From eaf1300b759af27e0f5b9e155e43ab6879d5a5c4 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Sun, 11 Aug 2024 17:00:04 -0700 Subject: [PATCH 05/14] If tex size from SDF is not valid, still use default (do not set it) Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index ad25cbef8..8e6d75d55 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -92,6 +92,10 @@ class gz::rendering::Ogre2ScenePrivate /// \brief Flag to indicate if sky is enabled or not public: bool skyEnabled = false; + /// \brief Max shadow texture size + public: unsigned int maxTexSize = 8192u; + + /// \brief Shadow texture size for directional light public: unsigned int dirTexSize = 2048u; /// \brief Flag to alert the user its usage of PreRender/PostRender @@ -680,21 +684,19 @@ void Ogre2Scene::UpdateShadowNode() atlasId++; } - unsigned int maxTexSize = 8192u; - if (engine->GraphicsAPI() == GraphicsAPI::OPENGL) { GLint glMaxTexSize; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexSize); - maxTexSize = glMaxTexSize; + this->dataPtr->maxTexSize = glMaxTexSize; } // others unsigned int spotPointTexSize = 2048u; unsigned int rowIdx = 0; unsigned int colIdx = 0; - unsigned int rowSize = maxTexSize / spotPointTexSize; + unsigned int rowSize = this->dataPtr->maxTexSize / spotPointTexSize; unsigned int colSize = rowSize; for (unsigned int i = 0; i < spotPointLightCount; ++i) @@ -1570,6 +1572,37 @@ bool Ogre2Scene::SkyEnabled() const void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { + // If _textureSize exceeds max possible tex size, then use default + if (_textureSize > this->dataPtr->maxTexSize / 2) + { + gzerr << " of '" << _textureSize + << "' exceeds maximum possible texture size," + << " using default texture size" << std::endl; + return; + } + + // Generate list of possible tex size options, each a power of 2, starting at 1024 + std::vector texSizeOptions; + unsigned int texSizeOption = 1024u; + while (texSizeOption <= this->dataPtr->maxTexSize / 2) + { + texSizeOptions.push_back(texSizeOption); + texSizeOption *= 2; + } + + // if _textureSize is not a valid texture size, then use default + bool validTexSize = std::find(texSizeOptions.begin(), + texSizeOptions.end(), _textureSize) + != texSizeOptions.end() ? true : false; + if (!validTexSize) + { + gzerr << " of '" << _textureSize + << "' is not a valid texture size," + << " using default texture size" << std::endl; + return; + } + + // Set shadow texture size as _textureSize if value is valid if (_lightType == LightType::LT_DIRECTIONAL) { this->dataPtr->dirTexSize = _textureSize; From 896efae19f20d60abf2fbde90b606d343715b38e Mon Sep 17 00:00:00 2001 From: Athena Z Date: Sun, 11 Aug 2024 17:33:31 -0700 Subject: [PATCH 06/14] Added getter func Signed-off-by: Athena Z --- include/gz/rendering/Scene.hh | 4 ++++ include/gz/rendering/base/BaseScene.hh | 3 +++ ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh | 4 ++++ ogre2/src/Ogre2Scene.cc | 13 +++++++++++++ src/base/BaseScene.cc | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 559187579..633373502 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1276,6 +1276,10 @@ namespace gz /// @param _textureSize Shadow texture size public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) = 0; + /// @brief \brief Get the shadow texture size for the given light type. + /// @param _lightType Light type that creates the shadow + public: virtual unsigned int ShadowTextureSize(LightType _lightType) = 0; + /// \brief Sets the given GI as the current new active GI solution /// \param[in] _gi GI solution that should be active. Nullptr to disable public: virtual void SetActiveGlobalIllumination( diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index 56dce3d8a..aec08a600 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -631,6 +631,9 @@ namespace gz // Documentation inherited. public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) override; + // Documentation inherited. + public: virtual unsigned int ShadowTextureSize(LightType _lightType) override; + // Documentation inherited. public: virtual void SetActiveGlobalIllumination( GlobalIlluminationBasePtr _gi) override; diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index d91258ff0..0e78ca4f8 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -102,8 +102,12 @@ namespace gz // Documentation inherited public: virtual bool SkyEnabled() const override; + // Documentation inherited public: void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize); + // Documentation inherited + public: unsigned int ShadowTextureSize(LightType _lightType); + // Documentation inherited public: virtual void SetActiveGlobalIllumination( GlobalIlluminationBasePtr _gi) override; diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 8e6d75d55..1ddcf7c4a 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1570,6 +1570,7 @@ bool Ogre2Scene::SkyEnabled() const return this->dataPtr->skyEnabled; } +////////////////////////////////////////////////// void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { // If _textureSize exceeds max possible tex size, then use default @@ -1609,6 +1610,18 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textur } } +////////////////////////////////////////////////// +unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) +{ + // todo: return based on light type, currently only dir light is supported + if (_lightType) + { + return this->dataPtr->dirTexSize; + } + + return this->dataPtr->dirTexSize; +} + ////////////////////////////////////////////////// void Ogre2Scene::SetActiveGlobalIllumination(GlobalIlluminationBasePtr _gi) { diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index ccf854707..ae1304d24 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1485,6 +1485,12 @@ void BaseScene::SetShadowTextureSize(LightType _lightType, unsigned int _texture } } +////////////////////////////////////////////////// +unsigned int BaseScene::ShadowTextureSize(LightType _lightType) +{ + return this->ShadowTextureSize(_lightType); +} + ////////////////////////////////////////////////// void BaseScene::SetActiveGlobalIllumination(GlobalIlluminationBasePtr _gi) { From 693cb12c56a9fe9f3d456d820c98dfbcbc3d01f4 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Mon, 12 Aug 2024 14:48:52 -0700 Subject: [PATCH 07/14] Cleaned up style with code check, added unit tests Signed-off-by: Athena Z --- include/gz/rendering/Scene.hh | 8 +++-- include/gz/rendering/base/BaseScene.hh | 6 ++-- .../include/gz/rendering/ogre2/Ogre2Scene.hh | 6 ++-- ogre2/src/Ogre2Scene.cc | 14 +++++---- src/base/BaseScene.cc | 7 +++-- test/common_test/Scene_TEST.cc | 29 +++++++++++++++++++ 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 633373502..338dfea91 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1273,12 +1273,14 @@ namespace gz /// @brief \brief Set the shadow texture size for the given light type. /// @param _lightType Light type that creates the shadow - /// @param _textureSize Shadow texture size - public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) = 0; + /// @param _textureSize Shadow texture size + public: virtual void SetShadowTextureSize(LightType _lightType, + unsigned int _textureSize) = 0; /// @brief \brief Get the shadow texture size for the given light type. /// @param _lightType Light type that creates the shadow - public: virtual unsigned int ShadowTextureSize(LightType _lightType) = 0; + public: virtual unsigned int ShadowTextureSize(LightType _lightType) + const = 0; /// \brief Sets the given GI as the current new active GI solution /// \param[in] _gi GI solution that should be active. Nullptr to disable diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index aec08a600..cb8906333 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -629,10 +629,12 @@ namespace gz public: virtual bool SkyEnabled() const override; // Documentation inherited. - public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) override; + public: virtual void SetShadowTextureSize(LightType _lightType, + unsigned int _textureSize) override; // Documentation inherited. - public: virtual unsigned int ShadowTextureSize(LightType _lightType) override; + public: virtual unsigned int ShadowTextureSize(LightType _lightType) const + override; // Documentation inherited. public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 0e78ca4f8..924435cb3 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -103,10 +103,12 @@ namespace gz public: virtual bool SkyEnabled() const override; // Documentation inherited - public: void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize); + public: void SetShadowTextureSize(LightType _lightType, + unsigned int _textureSize); // Documentation inherited - public: unsigned int ShadowTextureSize(LightType _lightType); + public: unsigned int ShadowTextureSize(LightType _lightType) const + override; // Documentation inherited public: virtual void SetActiveGlobalIllumination( diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 1ddcf7c4a..af98843be 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1571,7 +1571,8 @@ bool Ogre2Scene::SkyEnabled() const } ////////////////////////////////////////////////// -void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) +void Ogre2Scene::SetShadowTextureSize(LightType _lightType, + unsigned int _textureSize) { // If _textureSize exceeds max possible tex size, then use default if (_textureSize > this->dataPtr->maxTexSize / 2) @@ -1580,9 +1581,10 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textur << "' exceeds maximum possible texture size," << " using default texture size" << std::endl; return; - } + } - // Generate list of possible tex size options, each a power of 2, starting at 1024 + // Generate list of possible tex size options, each a power of 2, + // starting at 1024 std::vector texSizeOptions; unsigned int texSizeOption = 1024u; while (texSizeOption <= this->dataPtr->maxTexSize / 2) @@ -1591,11 +1593,11 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textur texSizeOption *= 2; } - // if _textureSize is not a valid texture size, then use default + // if _textureSize is an invalid texture size, then use default bool validTexSize = std::find(texSizeOptions.begin(), texSizeOptions.end(), _textureSize) != texSizeOptions.end() ? true : false; - if (!validTexSize) + if (!validTexSize) { gzerr << " of '" << _textureSize << "' is not a valid texture size," @@ -1611,7 +1613,7 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textur } ////////////////////////////////////////////////// -unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) +unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) const { // todo: return based on light type, currently only dir light is supported if (_lightType) diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index ae1304d24..cd9a28adb 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1476,9 +1476,10 @@ bool BaseScene::SkyEnabled() const } ////////////////////////////////////////////////// -void BaseScene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) +void BaseScene::SetShadowTextureSize(LightType _lightType, + unsigned int _textureSize) { - if (_lightType || _textureSize) + if (_lightType || _textureSize) { gzerr << "Setting shadow texture size not supported by: " << this->Engine()->Name() << std::endl; @@ -1486,7 +1487,7 @@ void BaseScene::SetShadowTextureSize(LightType _lightType, unsigned int _texture } ////////////////////////////////////////////////// -unsigned int BaseScene::ShadowTextureSize(LightType _lightType) +unsigned int BaseScene::ShadowTextureSize(LightType _lightType) const { return this->ShadowTextureSize(_lightType); } diff --git a/test/common_test/Scene_TEST.cc b/test/common_test/Scene_TEST.cc index 7803547f5..39fd68f2d 100644 --- a/test/common_test/Scene_TEST.cc +++ b/test/common_test/Scene_TEST.cc @@ -751,3 +751,32 @@ TEST_F(SceneTest, Sky) // Clean up engine->DestroyScene(scene); } + +///////////////////////////////////////////////// +TEST_F(SceneTest, ShadowTexture) +{ + CHECK_SUPPORTED_ENGINE("ogre2"); + + auto scene = engine->CreateScene("scene"); + ASSERT_NE(nullptr, scene); + + // Default shadow texture size for directional light is 2048u + EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 2048u); + + // Currently only support setting shadow texture size for + // directional light + // If set shadow texture size for other light types, it is ignored + scene->SetShadowTextureSize(LightType::LT_POINT, 4096u); + EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_POINT), 2048u); + + scene->SetShadowTextureSize(LightType::LT_SPOT, 4096u); + EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_SPOT), 2048u); + + // If set shadow texture size to a valid value, change it + scene->SetShadowTextureSize(LightType::LT_DIRECTIONAL, 8192u); + EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 8192u); + + // If set shadow texture size to an invalid value, use default + scene->SetShadowTextureSize(LightType::LT_DIRECTIONAL, 1000u); + EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 8192u); +} From b258c8a45907b488701f75dbc4aee7d8d7f840fc Mon Sep 17 00:00:00 2001 From: "Athena Z." Date: Mon, 12 Aug 2024 19:07:10 -0700 Subject: [PATCH 08/14] Update include/gz/rendering/Light.hh Co-authored-by: Ian Chen Signed-off-by: Athena Z. Signed-off-by: Athena Z --- include/gz/rendering/Light.hh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/gz/rendering/Light.hh b/include/gz/rendering/Light.hh index 95ffa462e..0d50d1435 100644 --- a/include/gz/rendering/Light.hh +++ b/include/gz/rendering/Light.hh @@ -27,13 +27,13 @@ namespace gz { inline namespace GZ_RENDERING_VERSION_NAMESPACE { - enum GZ_RENDERING_VISIBLE LightType - { - LT_EMPTY = 0, - LT_POINT = 1, - LT_DIRECTIONAL = 2, - LT_SPOT = 3 - }; + enum GZ_RENDERING_VISIBLE LightType + { + LT_EMPTY = 0, + LT_POINT = 1, + LT_DIRECTIONAL = 2, + LT_SPOT = 3 + }; // /// \class Light Light.hh gz/rendering/Light.hh From e62b161a8832a34f09fc1a02e57ed7b71f36c7b8 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Wed, 14 Aug 2024 10:44:58 -0700 Subject: [PATCH 09/14] Modify get/set ShadowTextureSize funcs, use ifdef for file paths Signed-off-by: Athena Z --- include/gz/rendering/Scene.hh | 10 +++---- ogre2/src/Ogre2Scene.cc | 53 ++++++++++++++++++++++------------- src/base/BaseScene.cc | 9 ++++-- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 338dfea91..ab6bddf7d 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1271,14 +1271,14 @@ namespace gz /// \return true to sky is enabled, false otherwise public: virtual bool SkyEnabled() const = 0; - /// @brief \brief Set the shadow texture size for the given light type. - /// @param _lightType Light type that creates the shadow - /// @param _textureSize Shadow texture size + /// \brief Set the shadow texture size for the given light type. + /// \param _lightType Light type that creates the shadow + /// \param _textureSize Shadow texture size public: virtual void SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) = 0; - /// @brief \brief Get the shadow texture size for the given light type. - /// @param _lightType Light type that creates the shadow + /// \brief Get the shadow texture size for the given light type. + /// \param _lightType Light type that creates the shadow public: virtual unsigned int ShadowTextureSize(LightType _lightType) const = 0; diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index af98843be..dc757bd2b 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -15,7 +15,15 @@ * */ -#include +#ifdef __APPLE__ + #define GL_SILENCE_DEPRECATION + #include + #include +#else +#ifndef _WIN32 + #include +#endif +#endif #include @@ -98,6 +106,9 @@ class gz::rendering::Ogre2ScenePrivate /// \brief Shadow texture size for directional light public: unsigned int dirTexSize = 2048u; + /// \brief Shadow texture size for spot and point lights + public: unsigned int spotPointTexSize = 2048u; + /// \brief Flag to alert the user its usage of PreRender/PostRender /// is incorrect public: bool frameUpdateStarted = false; @@ -693,7 +704,7 @@ void Ogre2Scene::UpdateShadowNode() } // others - unsigned int spotPointTexSize = 2048u; + unsigned int spotPointTexSize = this->dataPtr->spotPointTexSize; unsigned int rowIdx = 0; unsigned int colIdx = 0; unsigned int rowSize = this->dataPtr->maxTexSize / spotPointTexSize; @@ -1574,6 +1585,14 @@ bool Ogre2Scene::SkyEnabled() const void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { + // If _lightType is not supported, block with gzerr message + if (_lightType != LightType::LT_DIRECTIONAL) + { + gzerr << "Light type [" << _lightType << "] is not supported." + << std::endl; + return; + } + // If _textureSize exceeds max possible tex size, then use default if (_textureSize > this->dataPtr->maxTexSize / 2) { @@ -1583,21 +1602,9 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, return; } - // Generate list of possible tex size options, each a power of 2, - // starting at 1024 - std::vector texSizeOptions; - unsigned int texSizeOption = 1024u; - while (texSizeOption <= this->dataPtr->maxTexSize / 2) - { - texSizeOptions.push_back(texSizeOption); - texSizeOption *= 2; - } - // if _textureSize is an invalid texture size, then use default - bool validTexSize = std::find(texSizeOptions.begin(), - texSizeOptions.end(), _textureSize) - != texSizeOptions.end() ? true : false; - if (!validTexSize) + if (_textureSize < 1024u || _textureSize > (this->dataPtr->maxTexSize / 2) + || !math::isPowerOfTwo(_textureSize)) { gzerr << " of '" << _textureSize << "' is not a valid texture size," @@ -1616,12 +1623,18 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) const { // todo: return based on light type, currently only dir light is supported - if (_lightType) + switch (_lightType) { - return this->dataPtr->dirTexSize; + case LightType::LT_DIRECTIONAL: + return this->dataPtr->dirTexSize; + case LightType::LT_SPOT: + case LightType::LT_POINT: + return this->dataPtr->spotPointTexSize; + default: + case LightType::LT_EMPTY: + gzerr << "Invalid light type [" << _lightType << "]" << std::endl; + return 0; } - - return this->dataPtr->dirTexSize; } ////////////////////////////////////////////////// diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index cd9a28adb..5754b8d57 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1482,14 +1482,19 @@ void BaseScene::SetShadowTextureSize(LightType _lightType, if (_lightType || _textureSize) { gzerr << "Setting shadow texture size not supported by: " - << this->Engine()->Name() << std::endl; + << this->Engine()->Name() << std::endl; } } ////////////////////////////////////////////////// unsigned int BaseScene::ShadowTextureSize(LightType _lightType) const { - return this->ShadowTextureSize(_lightType); + if (_lightType) + { + gzerr << "Shadow texture size not supported by: " + << this->Engine()->Name() << std::endl; + } + return 0; } ////////////////////////////////////////////////// From 640d0b234ca3429b2dad5ba3eb75ce8f740e331f Mon Sep 17 00:00:00 2001 From: Athena Z Date: Wed, 14 Aug 2024 17:28:44 -0700 Subject: [PATCH 10/14] Changed LightType from enum to enum class, modify SetShadowTextureSize to return bool Signed-off-by: Athena Z --- include/gz/rendering/Light.hh | 19 +++++++++---- include/gz/rendering/Scene.hh | 2 +- include/gz/rendering/base/BaseScene.hh | 2 +- .../include/gz/rendering/ogre2/Ogre2Scene.hh | 2 +- ogre2/src/Ogre2Scene.cc | 28 ++++++++++--------- src/base/BaseScene.cc | 7 +++-- test/common_test/Scene_TEST.cc | 20 ++++++------- 7 files changed, 46 insertions(+), 34 deletions(-) diff --git a/include/gz/rendering/Light.hh b/include/gz/rendering/Light.hh index 0d50d1435..3ba82e372 100644 --- a/include/gz/rendering/Light.hh +++ b/include/gz/rendering/Light.hh @@ -27,12 +27,21 @@ namespace gz { inline namespace GZ_RENDERING_VERSION_NAMESPACE { - enum GZ_RENDERING_VISIBLE LightType + /// \enum LightType + /// \brief Enum for Light types. + enum class GZ_RENDERING_VISIBLE LightType { - LT_EMPTY = 0, - LT_POINT = 1, - LT_DIRECTIONAL = 2, - LT_SPOT = 3 + /// \brief No light type specified + EMPTY = 0, + + /// \brief Point light + POINT = 1, + + /// \brief Directional light + DIRECTIONAL = 2, + + /// \brief Spot light + SPOT = 3 }; // diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index ab6bddf7d..fdaa6436d 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1274,7 +1274,7 @@ namespace gz /// \brief Set the shadow texture size for the given light type. /// \param _lightType Light type that creates the shadow /// \param _textureSize Shadow texture size - public: virtual void SetShadowTextureSize(LightType _lightType, + public: virtual bool SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) = 0; /// \brief Get the shadow texture size for the given light type. diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index cb8906333..200246a2b 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -629,7 +629,7 @@ namespace gz public: virtual bool SkyEnabled() const override; // Documentation inherited. - public: virtual void SetShadowTextureSize(LightType _lightType, + public: virtual bool SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) override; // Documentation inherited. diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 924435cb3..323c2b30b 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -103,7 +103,7 @@ namespace gz public: virtual bool SkyEnabled() const override; // Documentation inherited - public: void SetShadowTextureSize(LightType _lightType, + public: bool SetShadowTextureSize(LightType _lightType, unsigned int _textureSize); // Documentation inherited diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index dc757bd2b..a8e383e68 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1582,15 +1582,15 @@ bool Ogre2Scene::SkyEnabled() const } ////////////////////////////////////////////////// -void Ogre2Scene::SetShadowTextureSize(LightType _lightType, +bool Ogre2Scene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { // If _lightType is not supported, block with gzerr message - if (_lightType != LightType::LT_DIRECTIONAL) + if (_lightType != LightType::DIRECTIONAL) { - gzerr << "Light type [" << _lightType << "] is not supported." - << std::endl; - return; + gzerr << "Light type [" << static_cast(_lightType) + << "] is not supported." << std::endl; + return false; } // If _textureSize exceeds max possible tex size, then use default @@ -1599,7 +1599,7 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, gzerr << " of '" << _textureSize << "' exceeds maximum possible texture size," << " using default texture size" << std::endl; - return; + return false; } // if _textureSize is an invalid texture size, then use default @@ -1609,14 +1609,15 @@ void Ogre2Scene::SetShadowTextureSize(LightType _lightType, gzerr << " of '" << _textureSize << "' is not a valid texture size," << " using default texture size" << std::endl; - return; + return false; } // Set shadow texture size as _textureSize if value is valid - if (_lightType == LightType::LT_DIRECTIONAL) + if (_lightType == LightType::DIRECTIONAL) { this->dataPtr->dirTexSize = _textureSize; } + return true; } ////////////////////////////////////////////////// @@ -1625,14 +1626,15 @@ unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) const // todo: return based on light type, currently only dir light is supported switch (_lightType) { - case LightType::LT_DIRECTIONAL: + case LightType::DIRECTIONAL: return this->dataPtr->dirTexSize; - case LightType::LT_SPOT: - case LightType::LT_POINT: + case LightType::SPOT: + case LightType::POINT: return this->dataPtr->spotPointTexSize; default: - case LightType::LT_EMPTY: - gzerr << "Invalid light type [" << _lightType << "]" << std::endl; + case LightType::EMPTY: + gzerr << "Invalid light type [" << static_cast(_lightType) << "]" + << std::endl; return 0; } } diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index 5754b8d57..b65112fb3 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1476,20 +1476,21 @@ bool BaseScene::SkyEnabled() const } ////////////////////////////////////////////////// -void BaseScene::SetShadowTextureSize(LightType _lightType, +bool BaseScene::SetShadowTextureSize(LightType _lightType, unsigned int _textureSize) { - if (_lightType || _textureSize) + if (static_cast(_lightType) || _textureSize) { gzerr << "Setting shadow texture size not supported by: " << this->Engine()->Name() << std::endl; } + return false; } ////////////////////////////////////////////////// unsigned int BaseScene::ShadowTextureSize(LightType _lightType) const { - if (_lightType) + if (static_cast(_lightType)) { gzerr << "Shadow texture size not supported by: " << this->Engine()->Name() << std::endl; diff --git a/test/common_test/Scene_TEST.cc b/test/common_test/Scene_TEST.cc index 39fd68f2d..1824ea203 100644 --- a/test/common_test/Scene_TEST.cc +++ b/test/common_test/Scene_TEST.cc @@ -753,7 +753,7 @@ TEST_F(SceneTest, Sky) } ///////////////////////////////////////////////// -TEST_F(SceneTest, ShadowTexture) +TEST_F(SceneTest, ShadowTextureSize) { CHECK_SUPPORTED_ENGINE("ogre2"); @@ -761,22 +761,22 @@ TEST_F(SceneTest, ShadowTexture) ASSERT_NE(nullptr, scene); // Default shadow texture size for directional light is 2048u - EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 2048u); + EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 2048u); // Currently only support setting shadow texture size for // directional light // If set shadow texture size for other light types, it is ignored - scene->SetShadowTextureSize(LightType::LT_POINT, 4096u); - EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_POINT), 2048u); + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::POINT, 4096u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::POINT), 2048u); - scene->SetShadowTextureSize(LightType::LT_SPOT, 4096u); - EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_SPOT), 2048u); + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::SPOT, 4096u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::SPOT), 2048u); // If set shadow texture size to a valid value, change it - scene->SetShadowTextureSize(LightType::LT_DIRECTIONAL, 8192u); - EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 8192u); + EXPECT_TRUE(scene->SetShadowTextureSize(LightType::DIRECTIONAL, 8192u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 8192u); // If set shadow texture size to an invalid value, use default - scene->SetShadowTextureSize(LightType::LT_DIRECTIONAL, 1000u); - EXPECT_EQ(scene->ShadowTextureSize(LightType::LT_DIRECTIONAL), 8192u); + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::DIRECTIONAL, 1000u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 8192u); } From b3691539d3b4483a353802559cfaa07c23c3971c Mon Sep 17 00:00:00 2001 From: "Athena Z." Date: Thu, 15 Aug 2024 11:49:33 -0700 Subject: [PATCH 11/14] Update ogre2/src/Ogre2Scene.cc Co-authored-by: Ian Chen Signed-off-by: Athena Z. Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index a8e383e68..b245df96f 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1597,8 +1597,9 @@ bool Ogre2Scene::SetShadowTextureSize(LightType _lightType, if (_textureSize > this->dataPtr->maxTexSize / 2) { gzerr << " of '" << _textureSize - << "' exceeds maximum possible texture size," - << " using default texture size" << std::endl; + << "' exceeds maximum possible texture size of " + << this->dataPtr->maxTexSize << + << ", using default texture size" << std::endl; return false; } From ddde4d9a9686780b93296a1f79658540be698cb0 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Thu, 15 Aug 2024 12:54:38 -0700 Subject: [PATCH 12/14] Hard cap maxTexSize to 16K and modify logic for checking tex size inputs Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index b245df96f..86b4938ca 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -101,7 +101,7 @@ class gz::rendering::Ogre2ScenePrivate public: bool skyEnabled = false; /// \brief Max shadow texture size - public: unsigned int maxTexSize = 8192u; + public: unsigned int maxTexSize = 16384u; /// \brief Shadow texture size for directional light public: unsigned int dirTexSize = 2048u; @@ -700,7 +700,8 @@ void Ogre2Scene::UpdateShadowNode() GLint glMaxTexSize; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexSize); - this->dataPtr->maxTexSize = glMaxTexSize; + this->dataPtr->maxTexSize = std::min(this->dataPtr->maxTexSize, + static_cast(glMaxTexSize)); } // others @@ -1594,18 +1595,17 @@ bool Ogre2Scene::SetShadowTextureSize(LightType _lightType, } // If _textureSize exceeds max possible tex size, then use default - if (_textureSize > this->dataPtr->maxTexSize / 2) + if (_textureSize > this->dataPtr->maxTexSize) { gzerr << " of '" << _textureSize << "' exceeds maximum possible texture size of " - << this->dataPtr->maxTexSize << + << this->dataPtr->maxTexSize << ", using default texture size" << std::endl; return false; } // if _textureSize is an invalid texture size, then use default - if (_textureSize < 1024u || _textureSize > (this->dataPtr->maxTexSize / 2) - || !math::isPowerOfTwo(_textureSize)) + if (_textureSize < 512u || !math::isPowerOfTwo(_textureSize)) { gzerr << " of '" << _textureSize << "' is not a valid texture size," From a8a3b65d5891e4f4a50d57c4d3665f54fbd0a5c2 Mon Sep 17 00:00:00 2001 From: Athena Z Date: Thu, 15 Aug 2024 15:32:45 -0700 Subject: [PATCH 13/14] Add TODO note re issue setting texsize to val larger than 16K Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 86b4938ca..28edfcff5 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -700,6 +700,8 @@ void Ogre2Scene::UpdateShadowNode() GLint glMaxTexSize; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexSize); + // todo: there are issues when setting dirTexSize to a val larger + // than 16K this->dataPtr->maxTexSize = std::min(this->dataPtr->maxTexSize, static_cast(glMaxTexSize)); } From eca40a8d844a9dfff0d30505507973418ba27f9c Mon Sep 17 00:00:00 2001 From: Athena Z Date: Fri, 16 Aug 2024 11:33:44 -0700 Subject: [PATCH 14/14] Add unit tests Signed-off-by: Athena Z --- ogre2/src/Ogre2Scene.cc | 2 +- test/common_test/Scene_TEST.cc | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 28edfcff5..d7bc1ff91 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1638,7 +1638,7 @@ unsigned int Ogre2Scene::ShadowTextureSize(LightType _lightType) const case LightType::EMPTY: gzerr << "Invalid light type [" << static_cast(_lightType) << "]" << std::endl; - return 0; + return 0u; } } diff --git a/test/common_test/Scene_TEST.cc b/test/common_test/Scene_TEST.cc index 1824ea203..570e2d738 100644 --- a/test/common_test/Scene_TEST.cc +++ b/test/common_test/Scene_TEST.cc @@ -766,12 +766,18 @@ TEST_F(SceneTest, ShadowTextureSize) // Currently only support setting shadow texture size for // directional light // If set shadow texture size for other light types, it is ignored + auto spotLight = scene->CreateSpotLight("spot_light"); + auto pointLight = scene->CreatePointLight("point_light"); + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::POINT, 4096u)); EXPECT_EQ(scene->ShadowTextureSize(LightType::POINT), 2048u); EXPECT_FALSE(scene->SetShadowTextureSize(LightType::SPOT, 4096u)); EXPECT_EQ(scene->ShadowTextureSize(LightType::SPOT), 2048u); + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::EMPTY, 4096u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::EMPTY), 0u); + // If set shadow texture size to a valid value, change it EXPECT_TRUE(scene->SetShadowTextureSize(LightType::DIRECTIONAL, 8192u)); EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 8192u); @@ -779,4 +785,9 @@ TEST_F(SceneTest, ShadowTextureSize) // If set shadow texture size to an invalid value, use default EXPECT_FALSE(scene->SetShadowTextureSize(LightType::DIRECTIONAL, 1000u)); EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 8192u); + + // If set shadow texture size to a value larger than maxTexSize, + // use default + EXPECT_FALSE(scene->SetShadowTextureSize(LightType::DIRECTIONAL, 32768u)); + EXPECT_EQ(scene->ShadowTextureSize(LightType::DIRECTIONAL), 8192u); }