Skip to content

Commit

Permalink
Merge branch 'render_target_fixes' into 'master'
Browse files Browse the repository at this point in the history
[Postprocessing] Fix dirty flag and share luminance calculator between frames

See merge request OpenMW/openmw!3566
  • Loading branch information
jvoisin committed Nov 8, 2023
2 parents d8cd465 + cdaa44f commit 9fb5fe2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
22 changes: 12 additions & 10 deletions apps/openmw/mwrender/pingpongcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

namespace MWRender
{
PingPongCanvas::PingPongCanvas(Shader::ShaderManager& shaderManager)
PingPongCanvas::PingPongCanvas(
Shader::ShaderManager& shaderManager, const std::shared_ptr<LuminanceCalculator>& luminanceCalculator)
: mFallbackStateSet(new osg::StateSet)
, mMultiviewResolveStateSet(new osg::StateSet)
, mLuminanceCalculator(luminanceCalculator)
{
setUseDisplayList(false);
setUseVertexBufferObjects(true);
Expand All @@ -26,8 +28,7 @@ namespace MWRender

addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, 3));

mLuminanceCalculator = LuminanceCalculator(shaderManager);
mLuminanceCalculator.disable();
mLuminanceCalculator->disable();

Shader::ShaderManager::DefineMap defines;
Stereo::shaderStereoDefines(defines);
Expand Down Expand Up @@ -142,7 +143,7 @@ namespace MWRender
.getTexture());
}

mLuminanceCalculator.dirty(mTextureScene->getTextureWidth(), mTextureScene->getTextureHeight());
mLuminanceCalculator->dirty(mTextureScene->getTextureWidth(), mTextureScene->getTextureHeight());

if (Stereo::getStereo())
mRenderViewport
Expand All @@ -158,11 +159,11 @@ namespace MWRender
{ GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT2_EXT },
{ GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT } } };

(mAvgLum) ? mLuminanceCalculator.enable() : mLuminanceCalculator.disable();
(mAvgLum) ? mLuminanceCalculator->enable() : mLuminanceCalculator->disable();

// A histogram based approach is superior way to calculate scene luminance. Using mipmaps is more broadly
// supported, so that's what we use for now.
mLuminanceCalculator.draw(*this, renderInfo, state, ext, frameId);
mLuminanceCalculator->draw(*this, renderInfo, state, ext, frameId);

auto buffer = buffers[0];

Expand Down Expand Up @@ -202,8 +203,8 @@ namespace MWRender
node.mRootStateSet->setTextureAttribute(PostProcessor::Unit_Depth, mTextureDepth);

if (mAvgLum)
node.mRootStateSet->setTextureAttribute(
PostProcessor::TextureUnits::Unit_EyeAdaptation, mLuminanceCalculator.getLuminanceTexture(frameId));
node.mRootStateSet->setTextureAttribute(PostProcessor::TextureUnits::Unit_EyeAdaptation,
mLuminanceCalculator->getLuminanceTexture(frameId));

if (mTextureNormals)
node.mRootStateSet->setTextureAttribute(PostProcessor::TextureUnits::Unit_Normals, mTextureNormals);
Expand Down Expand Up @@ -258,8 +259,6 @@ namespace MWRender
texture->setTextureSize(w, h);
texture->setNumMipmapLevels(pass.mRenderTexture->getNumMipmapLevels());
texture->dirtyTextureObject();

mDirtyAttachments = false;
}

pass.mRenderTarget->apply(state, osg::FrameBufferObject::DRAW_FRAMEBUFFER);
Expand Down Expand Up @@ -336,5 +335,8 @@ namespace MWRender
{
bindDestinationFbo();
}

if (mDirtyAttachments)
mDirtyAttachments = false;
}
}
5 changes: 3 additions & 2 deletions apps/openmw/mwrender/pingpongcanvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace MWRender
class PingPongCanvas : public osg::Geometry
{
public:
PingPongCanvas(Shader::ShaderManager& shaderManager);
PingPongCanvas(
Shader::ShaderManager& shaderManager, const std::shared_ptr<LuminanceCalculator>& luminanceCalculator);

void drawGeometry(osg::RenderInfo& renderInfo) const;

Expand Down Expand Up @@ -72,7 +73,7 @@ namespace MWRender
mutable osg::ref_ptr<osg::FrameBufferObject> mMultiviewResolveFramebuffer;
mutable osg::ref_ptr<osg::FrameBufferObject> mDestinationFBO;
mutable std::array<osg::ref_ptr<osg::FrameBufferObject>, 3> mFbos;
mutable LuminanceCalculator mLuminanceCalculator;
mutable std::shared_ptr<LuminanceCalculator> mLuminanceCalculator;
};
}

Expand Down
14 changes: 8 additions & 6 deletions apps/openmw/mwrender/postprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,14 @@ namespace MWRender
, mUsePostProcessing(Settings::postProcessing().mEnabled)
, mSamples(Settings::video().mAntialiasing)
, mPingPongCull(new PingPongCull(this))
, mCanvases({ new PingPongCanvas(mRendering.getResourceSystem()->getSceneManager()->getShaderManager()),
new PingPongCanvas(mRendering.getResourceSystem()->getSceneManager()->getShaderManager()) })
{
auto& shaderManager = mRendering.getResourceSystem()->getSceneManager()->getShaderManager();

std::shared_ptr<LuminanceCalculator> luminanceCalculator = std::make_shared<LuminanceCalculator>(shaderManager);

for (auto& canvas : mCanvases)
canvas = new PingPongCanvas(shaderManager, luminanceCalculator);

mHUDCamera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
mHUDCamera->setRenderOrder(osg::Camera::POST_RENDER);
mHUDCamera->setClearColor(osg::Vec4(0.45, 0.45, 0.14, 1.0));
Expand All @@ -139,8 +144,7 @@ namespace MWRender
if (Settings::shaders().mSoftParticles || Settings::postProcessing().mTransparentPostpass)
{
mTransparentDepthPostPass
= new TransparentDepthBinCallback(mRendering.getResourceSystem()->getSceneManager()->getShaderManager(),
Settings::postProcessing().mTransparentPostpass);
= new TransparentDepthBinCallback(shaderManager, Settings::postProcessing().mTransparentPostpass);
osgUtil::RenderBin::getRenderBinPrototype("DepthSortedBin")->setDrawCallback(mTransparentDepthPostPass);
}

Expand Down Expand Up @@ -617,8 +621,6 @@ namespace MWRender
subPass.mSize = renderTarget.mSize;
subPass.mRenderTexture = renderTarget.mTarget;
subPass.mMipMap = renderTarget.mMipMap;
subPass.mStateSet->setAttributeAndModes(new osg::Viewport(
0, 0, subPass.mRenderTexture->getTextureWidth(), subPass.mRenderTexture->getTextureHeight()));

subPass.mRenderTarget = new osg::FrameBufferObject;
subPass.mRenderTarget->setAttachment(osg::FrameBufferObject::BufferComponent::COLOR_BUFFER0,
Expand Down

0 comments on commit 9fb5fe2

Please sign in to comment.