From f5a87ee46def4892ee736790fced2435cfe63dd4 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Fri, 26 Feb 2021 19:01:27 +0000 Subject: [PATCH] Refactor out duplicated RTT setup code --- apps/openmw/mwrender/localmap.cpp | 13 ++----------- apps/openmw/mwrender/water.cpp | 25 +++---------------------- components/sceneutil/util.cpp | 16 ++++++++++++++++ components/sceneutil/util.hpp | 4 ++++ 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/apps/openmw/mwrender/localmap.cpp b/apps/openmw/mwrender/localmap.cpp index a487fb1e94..64931aa880 100644 --- a/apps/openmw/mwrender/localmap.cpp +++ b/apps/openmw/mwrender/localmap.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "../mwbase/environment.hpp" @@ -237,17 +238,7 @@ void LocalMap::setupRenderToTexture(osg::ref_ptr camera, int x, int texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - unsigned int samples = 0; - unsigned int colourSamples = 0; - if (Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1) - { - // Alpha-to-coverage requires a multisampled framebuffer. - // OSG will set that up automatically and resolve it to the specified single-sample texture for us. - // For some reason, two samples are needed, at least with some drivers. - samples = 2; - colourSamples = 1; - } - camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, 0, false, samples, colourSamples); + SceneUtil::attachAlphaToCoverageFriendlyFramebufferToCamera(camera, osg::Camera::COLOR_BUFFER, texture); camera->addChild(mSceneRoot); mRoot->addChild(camera); diff --git a/apps/openmw/mwrender/water.cpp b/apps/openmw/mwrender/water.cpp index 0ab3de7efd..71b1b49855 100644 --- a/apps/openmw/mwrender/water.cpp +++ b/apps/openmw/mwrender/water.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -271,17 +272,7 @@ public: mRefractionTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); mRefractionTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - unsigned int samples = 0; - unsigned int colourSamples = 0; - if (Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1) - { - // Alpha-to-coverage requires a multisampled framebuffer. - // OSG will set that up automatically and resolve it to the specified single-sample texture for us. - // For some reason, two samples are needed, at least with some drivers. - samples = 2; - colourSamples = 1; - } - attach(osg::Camera::COLOR_BUFFER, mRefractionTexture, 0, 0, false, samples, colourSamples); + SceneUtil::attachAlphaToCoverageFriendlyFramebufferToCamera(this, osg::Camera::COLOR_BUFFER, mRefractionTexture); mRefractionDepthTexture = new osg::Texture2D; mRefractionDepthTexture->setTextureSize(rttSize, rttSize); @@ -366,17 +357,7 @@ public: mReflectionTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); mReflectionTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - unsigned int samples = 0; - unsigned int colourSamples = 0; - if (Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1) - { - // Alpha-to-coverage requires a multisampled framebuffer. - // OSG will set that up automatically and resolve it to the specified single-sample texture for us. - // For some reason, two samples are needed, at least with some drivers. - samples = 2; - colourSamples = 1; - } - attach(osg::Camera::COLOR_BUFFER, mReflectionTexture, 0, 0, false, samples, colourSamples); + SceneUtil::attachAlphaToCoverageFriendlyFramebufferToCamera(this, osg::Camera::COLOR_BUFFER, mReflectionTexture); // XXX: should really flip the FrontFace on each renderable instead of forcing clockwise. osg::ref_ptr frontFace (new osg::FrontFace); diff --git a/components/sceneutil/util.cpp b/components/sceneutil/util.cpp index 2c0d8efa0c..8a381681b1 100644 --- a/components/sceneutil/util.cpp +++ b/components/sceneutil/util.cpp @@ -11,6 +11,7 @@ #include #include +#include namespace SceneUtil { @@ -260,4 +261,19 @@ osg::ref_ptr addEnchantedGlow(osg::ref_ptr node, Resourc return glowUpdater; } +bool attachAlphaToCoverageFriendlyFramebufferToCamera(osg::Camera* camera, osg::Camera::BufferComponent buffer, osg::Texture * texture, unsigned int level, unsigned int face, bool mipMapGeneration) +{ + unsigned int samples = 0; + unsigned int colourSamples = 0; + if (Settings::Manager::getBool("antialias alpha test", "Shaders") && Settings::Manager::getInt("antialiasing", "Video") > 1) + { + // Alpha-to-coverage requires a multisampled framebuffer. + // OSG will set that up automatically and resolve it to the specified single-sample texture for us. + // For some reason, two samples are needed, at least with some drivers. + samples = 2; + colourSamples = 1; + } + camera->attach(buffer, texture, level, face, mipMapGeneration, samples, colourSamples); +} + } diff --git a/components/sceneutil/util.hpp b/components/sceneutil/util.hpp index 303d609f57..8103ed87a8 100644 --- a/components/sceneutil/util.hpp +++ b/components/sceneutil/util.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -60,6 +61,9 @@ namespace SceneUtil bool hasUserDescription(const osg::Node* node, const std::string pattern); osg::ref_ptr addEnchantedGlow(osg::ref_ptr node, Resource::ResourceSystem* resourceSystem, osg::Vec4f glowColor, float glowDuration=-1); + + // Alpha-to-coverage requires a multisampled framebuffer, so we need to set that up for RTTs + bool attachAlphaToCoverageFriendlyFramebufferToCamera(osg::Camera* camera, osg::Camera::BufferComponent buffer, osg::Texture* texture, unsigned int level = 0, unsigned int face = 0, bool mipMapGeneration = false); } #endif