1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-27 14:37:04 +00:00

Merge branch 'refactor/postprocessor' into 'master'

Refactor PostProcessor code

See merge request OpenMW/openmw!1888
This commit is contained in:
Cody Glassman 2022-05-21 06:42:05 +00:00
commit 5fc530335b
5 changed files with 79 additions and 71 deletions

View File

@ -1,5 +1,6 @@
#include "postprocessor.hpp" #include "postprocessor.hpp"
#include <algorithm>
#include <SDL_opengl_glext.h> #include <SDL_opengl_glext.h>
#include <osg/Texture1D> #include <osg/Texture1D>
@ -172,6 +173,21 @@ namespace MWRender
Stereo::Manager::instance().screenResolutionChanged(); Stereo::Manager::instance().screenResolutionChanged();
} }
void PostProcessor::populateTechniqueFiles()
{
for (const auto& name : mVFS->getRecursiveDirectoryIterator(fx::Technique::sSubdir))
{
std::filesystem::path path = name;
std::string fileExt = Misc::StringUtils::lowerCase(path.extension().string());
if (!path.parent_path().has_parent_path() && fileExt == fx::Technique::sExt)
{
auto absolutePath = std::filesystem::path(mVFS->getAbsoluteFileName(name));
mTechniqueFileMap[absolutePath.stem().string()] = absolutePath;
}
}
}
void PostProcessor::enable(bool usePostProcessing) void PostProcessor::enable(bool usePostProcessing)
{ {
mReload = true; mReload = true;
@ -193,17 +209,8 @@ namespace MWRender
if (mUsePostProcessing && mTechniqueFileMap.empty()) if (mUsePostProcessing && mTechniqueFileMap.empty())
{ {
for (const auto& name : mVFS->getRecursiveDirectoryIterator(fx::Technique::sSubdir)) populateTechniqueFiles();
{
std::filesystem::path path = name;
std::string fileExt = Misc::StringUtils::lowerCase(path.extension().string());
if (!path.parent_path().has_parent_path() && fileExt == fx::Technique::sExt)
{
auto absolutePath = std::filesystem::path(mVFS->getAbsoluteFileName(name));
mTechniqueFileMap[absolutePath.stem().string()] = absolutePath;
}
}
} }
mMainTemplate->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); mMainTemplate->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
@ -357,47 +364,51 @@ namespace MWRender
} }
} }
void PostProcessor::update(size_t frameId) void PostProcessor::updateLiveReload()
{ {
static const bool liveReload = Settings::Manager::getBool("live reload", "Post Processing"); static const bool liveReload = Settings::Manager::getBool("live reload", "Post Processing");
if (!liveReload)
return;
if (liveReload) for (auto& technique : mTechniques)
{ {
for (auto& technique : mTechniques) if (technique->getStatus() == fx::Technique::Status::File_Not_exists)
{ continue;
if (technique->getStatus() == fx::Technique::Status::File_Not_exists)
continue;
technique->setLastModificationTime(std::filesystem::last_write_time(mTechniqueFileMap[technique->getName()])); const auto lastWriteTime = std::filesystem::last_write_time(mTechniqueFileMap[technique->getName()]);
const bool isDirty = technique->setLastModificationTime(lastWriteTime);
if(technique->isValid() && !technique->isDirty()) if (technique->isValid() && !isDirty)
continue; continue;
if (technique->isDirty()) if (technique->compile())
{ Log(Debug::Info) << "Reloaded technique : " << mTechniqueFileMap[technique->getName()].string();
technique->compile();
if (technique->isValid()) mReload = technique->isValid();
Log(Debug::Info) << "Reloaded technique : " << mTechniqueFileMap[technique->getName()].string();
if (!mReload)
mReload = technique->isValid();
}
}
} }
}
if (mReload) void PostProcessor::reloadIfRequired()
{ {
mReload = false; if (!mReload)
return;
if (!mTechniques.empty()) mReload = false;
reloadMainPass(*mTechniques[0]);
reloadTechniques(); if (!mTechniques.empty())
reloadMainPass(*mTechniques[0]);
if (!mUsePostProcessing) reloadTechniques();
resize();
} if (!mUsePostProcessing)
resize();
}
void PostProcessor::update(size_t frameId)
{
updateLiveReload();
reloadIfRequired();
if (mDirty && mDirtyFrameId == frameId) if (mDirty && mDirtyFrameId == frameId)
{ {
@ -647,29 +658,23 @@ namespace MWRender
bool PostProcessor::disableTechnique(std::shared_ptr<fx::Technique> technique, bool dirty) bool PostProcessor::disableTechnique(std::shared_ptr<fx::Technique> technique, bool dirty)
{ {
for (size_t i = 1; i < mTechniques.size(); ++i) auto it = std::find(mTechniques.begin(), mTechniques.end(), technique);
{ if (it == std::end(mTechniques))
if (technique.get() == mTechniques[i].get()) return false;
{
mTechniques.erase(mTechniques.begin() + i);
if (dirty)
dirtyTechniques();
return true;
}
}
return false; mTechniques.erase(it);
if (dirty)
dirtyTechniques();
return true;
} }
bool PostProcessor::isTechniqueEnabled(const std::shared_ptr<fx::Technique>& technique) const bool PostProcessor::isTechniqueEnabled(const std::shared_ptr<fx::Technique>& technique) const
{ {
for (const auto& t : mTechniques) if (auto it = std::find(mTechniques.begin(), mTechniques.end(), technique); it == mTechniques.end())
{ return false;
if (technique.get() == t.get())
return technique->isValid();
}
return false; return technique->isValid();
} }
void PostProcessor::createTexturesAndCamera(size_t frameId) void PostProcessor::createTexturesAndCamera(size_t frameId)
@ -762,7 +767,7 @@ namespace MWRender
technique->compile(); technique->compile();
if (technique->getStatus() != fx::Technique::Status::File_Not_exists) if (technique->getStatus() != fx::Technique::Status::File_Not_exists)
technique->setLastModificationTime(std::filesystem::last_write_time(mTechniqueFileMap[technique->getName()]), false); technique->setLastModificationTime(std::filesystem::last_write_time(mTechniqueFileMap[technique->getName()]));
if (!insert) if (!insert)
return technique; return technique;

View File

@ -161,6 +161,9 @@ namespace MWRender
void setRenderTargetSize(int width, int height) { mWidth = width; mHeight = height; } void setRenderTargetSize(int width, int height) { mWidth = width; mHeight = height; }
private: private:
void populateTechniqueFiles();
size_t frame() const { return mViewer->getFrameStamp()->getFrameNumber(); } size_t frame() const { return mViewer->getFrameStamp()->getFrameNumber(); }
void createObjectsForFrame(size_t frameId); void createObjectsForFrame(size_t frameId);
@ -175,6 +178,10 @@ namespace MWRender
void update(size_t frameId); void update(size_t frameId);
void reloadIfRequired();
void updateLiveReload();
void cull(size_t frameId, osgUtil::CullVisitor* cv); void cull(size_t frameId, osgUtil::CullVisitor* cv);
osg::ref_ptr<osg::Group> mRootNode; osg::ref_ptr<osg::Group> mRootNode;

View File

@ -55,7 +55,6 @@ namespace fx
{ {
mTextures.clear(); mTextures.clear();
mStatus = Status::Uncompiled; mStatus = Status::Uncompiled;
mDirty = false;
mValid = false; mValid = false;
mHDR = false; mHDR = false;
mNormals = false; mNormals = false;
@ -180,12 +179,11 @@ namespace fx
return mFileName; return mFileName;
} }
void Technique::setLastModificationTime(std::filesystem::file_time_type timeStamp, bool dirty) bool Technique::setLastModificationTime(std::filesystem::file_time_type timeStamp)
{ {
if (dirty && mLastModificationTime != timeStamp) const bool isDirty = timeStamp != mLastModificationTime;
mDirty = true;
mLastModificationTime = timeStamp; mLastModificationTime = timeStamp;
return isDirty;
} }
[[noreturn]] void Technique::error(const std::string& msg) [[noreturn]] void Technique::error(const std::string& msg)
@ -541,6 +539,9 @@ namespace fx
constexpr bool isVec = std::is_same_v<osg::Vec2f, SrcT> || std::is_same_v<osg::Vec3f, SrcT> || std::is_same_v<osg::Vec4f, SrcT>; constexpr bool isVec = std::is_same_v<osg::Vec2f, SrcT> || std::is_same_v<osg::Vec3f, SrcT> || std::is_same_v<osg::Vec4f, SrcT>;
constexpr bool isFloat = std::is_same_v<float, SrcT>; constexpr bool isFloat = std::is_same_v<float, SrcT>;
constexpr bool isInt = std::is_same_v<int, SrcT>; constexpr bool isInt = std::is_same_v<int, SrcT>;
constexpr bool isBool = std::is_same_v<bool, SrcT>;
static_assert(isVec || isFloat || isInt || isBool, "Unsupported type");
std::optional<double> step; std::optional<double> step;
@ -555,7 +556,7 @@ namespace fx
data.mDefault = parseFloat(); data.mDefault = parseFloat();
else if constexpr (isInt) else if constexpr (isInt)
data.mDefault = parseInteger(); data.mDefault = parseInteger();
else else if constexpr (isBool)
data.mDefault = parseBool(); data.mDefault = parseBool();
} }
else if (key == "min") else if (key == "min")
@ -566,7 +567,7 @@ namespace fx
data.mMin = parseFloat(); data.mMin = parseFloat();
else if constexpr (isInt) else if constexpr (isInt)
data.mMin = parseInteger(); data.mMin = parseInteger();
else else if constexpr (isBool)
data.mMin = parseBool(); data.mMin = parseBool();
} }
else if (key == "max") else if (key == "max")
@ -577,7 +578,7 @@ namespace fx
data.mMax = parseFloat(); data.mMax = parseFloat();
else if constexpr (isInt) else if constexpr (isInt)
data.mMax = parseInteger(); data.mMax = parseInteger();
else else if constexpr (isBool)
data.mMax = parseBool(); data.mMax = parseBool();
} }
else if (key == "step") else if (key == "step")

View File

@ -117,11 +117,7 @@ namespace fx
std::string getFileName() const; std::string getFileName() const;
void setLastModificationTime(std::filesystem::file_time_type timeStamp, bool dirty = true); bool setLastModificationTime(std::filesystem::file_time_type timeStamp);
bool isDirty() const { return mDirty; }
void setDirty(bool dirty) { mDirty = dirty; }
bool isValid() const { return mValid; } bool isValid() const { return mValid; }
@ -251,7 +247,6 @@ namespace fx
bool mEnabled; bool mEnabled;
std::filesystem::file_time_type mLastModificationTime; std::filesystem::file_time_type mLastModificationTime;
bool mDirty;
bool mValid; bool mValid;
bool mHDR; bool mHDR;
bool mNormals; bool mNormals;

View File

@ -95,7 +95,7 @@ namespace Settings
return value.as<T>(); return value.as<T>();
} }
catch(const YAML::BadConversion& e) catch(const YAML::BadConversion&)
{ {
Log(Debug::Warning) << "Failed retrieving " << tname << ", " << uname << " : mismatched types in config file."; Log(Debug::Warning) << "Failed retrieving " << tname << ", " << uname << " : mismatched types in config file.";
} }