From 553be5cabad804a29291c7289e840c8ebacef8b4 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Sun, 1 Sep 2024 16:03:30 +0200 Subject: [PATCH] Main: CompositorInstance - simplify local Texture creation --- OgreMain/include/OgreCompositorInstance.h | 15 +- OgreMain/src/OgreCompositorInstance.cpp | 181 +++++++++------------- 2 files changed, 82 insertions(+), 114 deletions(-) diff --git a/OgreMain/include/OgreCompositorInstance.h b/OgreMain/include/OgreCompositorInstance.h index 9a211814772..24c0a0fe4d5 100644 --- a/OgreMain/include/OgreCompositorInstance.h +++ b/OgreMain/include/OgreCompositorInstance.h @@ -350,13 +350,11 @@ namespace Ogre { and queued with queueRenderSystemOp. */ virtual void collectPasses(TargetOperation &finalState, const CompositionTargetPass *target); - - /** Create a local dummy material with one technique but no passes. - The material is detached from the Material Manager to make sure it is destroyed - when going out of scope. - */ - MaterialPtr createLocalMaterial(const String& srcName); - + + TexturePtr getLocalTexture(const CompositionTechnique::TextureDefinition& def, PixelFormat p, + const String& fsaaHint, const String& localName, + std::set& assignedTextures); + /** Create local rendertextures and other resources. Builds mLocalTextures. */ void createResources(bool forResizeOnly); @@ -392,8 +390,7 @@ namespace Ogre { /** Search for options like AA and hardware gamma which we may want to inherit from the main render target to which we're attached. */ - void deriveTextureRenderTargetOptions(const String& texname, - bool *hwGammaWrite, uint *fsaa, String* fsaaHint); + void deriveOptionsFromRenderTarget(CompositionTechnique::TextureDefinition& def, String& fsaaHint); /// Notify this instance that the primary viewport's camera has changed. void notifyCameraChanged(Camera* camera); diff --git a/OgreMain/src/OgreCompositorInstance.cpp b/OgreMain/src/OgreCompositorInstance.cpp index fd851fe13be..f83c773aa08 100644 --- a/OgreMain/src/OgreCompositorInstance.cpp +++ b/OgreMain/src/OgreCompositorInstance.cpp @@ -300,6 +300,22 @@ class RSComputeOperation : public CompositorInstance::RenderSystemOperation } }; +//----------------------------------------------------------------------- +/** Create a local dummy material with one technique but no passes. + The material is detached from the Material Manager to make sure it is destroyed + when going out of scope. +*/ +static MaterialPtr createLocalMaterial(const String& srcName) +{ + static size_t dummyCounter = 0; + auto mat = MaterialManager::getSingleton().create(StringUtil::format("c%zu/%s", dummyCounter++, srcName.c_str()), + RGN_INTERNAL); + /// This is safe, as we hold a private reference + MaterialManager::getSingleton().remove(mat); + /// Remove all passes from first technique + mat->getTechnique(0)->removeAllPasses(); + return mat; +} void CompositorInstance::collectPasses(TargetOperation &finalState, const CompositionTargetPass *target) { /// Here, passes are converted into render target operations @@ -584,19 +600,6 @@ const TexturePtr& CompositorInstance::getTextureInstance(const String& name, siz return nullPtr; } -//----------------------------------------------------------------------- -MaterialPtr CompositorInstance::createLocalMaterial(const String& srcName) -{ - static size_t dummyCounter = 0; - MaterialPtr mat = MaterialManager::getSingleton().create( - StringUtil::format("c%zu/%s", dummyCounter++, srcName.c_str()), - ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); - /// This is safe, as we hold a private reference - MaterialManager::getSingleton().remove(mat); - /// Remove all passes from first technique - mat->getTechnique(0)->removeAllPasses(); - return mat; -} //--------------------------------------------------------------------- void CompositorInstance::notifyResized() { @@ -605,6 +608,32 @@ void CompositorInstance::notifyResized() /// Notify chain state needs recompile. mChain->_markDirty(); } + +TexturePtr CompositorInstance::getLocalTexture(const CompositionTechnique::TextureDefinition& def, PixelFormat p, + const String& fsaaHint, const String& localName, + std::set& assignedTextures) +{ + bool hwGamma = def.hwGammaWrite && !PixelUtil::isFloatingPoint(p); + + TexturePtr tex; + if (def.pooled) + { + // get / create pooled texture + tex = CompositorManager::getSingleton().getPooledTexture(def.name, localName, def.width, def.height, p, + def.fsaa, fsaaHint, hwGamma, assignedTextures, this, + def.scope, def.type); + } + else + { + tex = TextureManager::getSingleton().createManual(def.name, RGN_INTERNAL, def.type, def.width, def.height, 0, p, + TU_RENDERTARGET, 0, hwGamma, def.fsaa, fsaaHint); + } + + // Also add to local textures so we can look up + mLocalTextures[localName] = tex; + return tex; +} + //----------------------------------------------------------------------- void CompositorInstance::createResources(bool forResizeOnly) { @@ -627,15 +656,12 @@ void CompositorInstance::createResources(bool forResizeOnly) Compositor* parentComp = mTechnique->getParent(); if (def->formatList.size() > 1) { - size_t atch = 0; - for (PixelFormatList::iterator p = def->formatList.begin(); - p != def->formatList.end(); ++p, ++atch) + for (size_t atch = 0; atch < def->formatList.size(); atch++) { Ogre::TexturePtr tex = parentComp->getTextureInstance(def->name, atch); mLocalTextures[getMRTTexLocalName(def->name, atch)] = tex; } - MultiRenderTarget* mrt = static_cast - (parentComp->getRenderTarget(def->name)); + auto mrt = static_cast(parentComp->getRenderTarget(def->name)); mLocalMRTs[def->name] = mrt; setupRenderTarget(mrt, def->depthBufferId); @@ -648,41 +674,26 @@ void CompositorInstance::createResources(bool forResizeOnly) } } else { - /// Determine width and height - uint32 width = def->width; - uint32 height = def->height; - uint fsaa = 0; - String fsaaHint; - bool hwGamma = false; - // Skip this one if we're only (re)creating for a resize & it's not derived // from the target size - if (forResizeOnly && width != 0 && height != 0) + if (forResizeOnly && def->width != 0 && def->height != 0) continue; + + /// Determine width and height + CompositionTechnique::TextureDefinition derivedDef = *def; + String fsaaHint; + deriveOptionsFromRenderTarget(derivedDef, fsaaHint); - deriveTextureRenderTargetOptions(def->name, &hwGamma, &fsaa, &fsaaHint); - - if(width == 0) - { - width = static_cast(mChain->getViewport()->getActualWidth()) * def->widthFactor; - width = width == 0 ? 1 : width; - } - if(height == 0) + if(def->width == 0) { - height = static_cast(mChain->getViewport()->getActualHeight()) * def->heightFactor; - height = height == 0 ? 1 : height; + derivedDef.width = static_cast(mChain->getViewport()->getActualWidth()) * def->widthFactor; + derivedDef.width = derivedDef.width == 0 ? 1 : derivedDef.width; } - - // determine options as a combination of selected options and possible options - if (def->fsaa == 0) + if(def->height == 0) { - fsaa = 0; - fsaaHint = BLANKSTRING; + derivedDef.height = static_cast(mChain->getViewport()->getActualHeight()) * def->heightFactor; + derivedDef.height = derivedDef.height == 0 ? 1 : derivedDef.height; } - else if(def->fsaa > 1) - fsaa = def->fsaa; - - hwGamma = hwGamma || def->hwGammaWrite; // need dummy counter as there may be multiple definitions with the same name in the chain String baseName = StringUtil::format("%s.chain%zu.%s", def->name.c_str(), dummyCounter++, @@ -695,70 +706,29 @@ void CompositorInstance::createResources(bool forResizeOnly) mLocalMRTs[def->name] = mrt; // create and bind individual surfaces - size_t atch = 0; - for (PixelFormatList::iterator p = def->formatList.begin(); - p != def->formatList.end(); ++p, ++atch) + uint8 atch = 0; + for (auto p : def->formatList) { - - String texname = StringUtil::format("mrt%zu.%s", atch, baseName.c_str()); + derivedDef.name = StringUtil::format("mrt%d.%s", atch, baseName.c_str()); String mrtLocalName = getMRTTexLocalName(def->name, atch); - TexturePtr tex; - if (def->pooled) - { - // get / create pooled texture - tex = CompositorManager::getSingleton().getPooledTexture(texname, - mrtLocalName, - width, height, *p, fsaa, fsaaHint, - hwGamma && !PixelUtil::isFloatingPoint(*p), - assignedTextures, this, def->scope, def->type); - } - else - { - tex = TextureManager::getSingleton().createManual(texname, - RGN_INTERNAL, def->type, - width, height, 0, *p, TU_RENDERTARGET, 0, - hwGamma && !PixelUtil::isFloatingPoint(*p), fsaa, fsaaHint ); - } - + TexturePtr tex = getLocalTexture(derivedDef, p, fsaaHint, mrtLocalName, assignedTextures); RenderTexture* rt = tex->getBuffer()->getRenderTarget(); rt->setAutoUpdated(false); mrt->bindSurface(atch, rt); - - // Also add to local textures so we can look up - mLocalTextures[mrtLocalName] = tex; - + + atch++; } setupRenderTarget(mrt, def->depthBufferId); } else { - String texName = baseName; - + derivedDef.name = baseName; // space in the name mixup the cegui in the compositor demo // this is an auto generated name - so no spaces can't hart us. - std::replace( texName.begin(), texName.end(), ' ', '_' ); - - hwGamma = hwGamma && !PixelUtil::isFloatingPoint(def->formatList[0]); - - TexturePtr tex; - if (def->pooled) - { - // get / create pooled texture - tex = CompositorManager::getSingleton().getPooledTexture(texName, - def->name, width, height, def->formatList[0], fsaa, fsaaHint, - hwGamma, assignedTextures, - this, def->scope, def->type); - } - else - { - tex = TextureManager::getSingleton().createManual( - texName, RGN_INTERNAL, def->type, width, height, 0, def->formatList[0], - TU_RENDERTARGET, 0, hwGamma, fsaa, fsaaHint); - } - - mLocalTextures[def->name] = tex; + std::replace( derivedDef.name.begin(), derivedDef.name.end(), ' ', '_' ); + TexturePtr tex = getLocalTexture(derivedDef, def->formatList[0], fsaaHint, def->name, assignedTextures); for(size_t i = 0; i < tex->getNumFaces(); i++) setupRenderTarget(tex->getBuffer(i)->getRenderTarget(), def->depthBufferId); } @@ -811,8 +781,8 @@ void CompositorInstance::setupRenderTarget(RenderTarget* rendTarget, uint16 dept } //--------------------------------------------------------------------- -void CompositorInstance::deriveTextureRenderTargetOptions( - const String& texname, bool *hwGammaWrite, uint *fsaa, String* fsaaHint) +void CompositorInstance::deriveOptionsFromRenderTarget(CompositionTechnique::TextureDefinition& def, + String& fsaaHint) { // search for passes on this texture def that either include a render_scene // or use input previous @@ -821,7 +791,7 @@ void CompositorInstance::deriveTextureRenderTargetOptions( const CompositionTechnique::TargetPasses& passes = mTechnique->getTargetPasses(); for (auto *tp : passes) { - if (tp->getOutputName() == texname) + if (tp->getOutputName() == def.name) { if (tp->getInputMode() == CompositionTargetPass::IM_PREVIOUS) { @@ -866,15 +836,16 @@ void CompositorInstance::deriveTextureRenderTargetOptions( { // Ok, inherit settings from target RenderTarget* target = mChain->getViewport()->getTarget(); - *hwGammaWrite = target->isHardwareGammaEnabled(); - *fsaa = target->getFSAA(); - *fsaaHint = target->getFSAAHint(); + def.hwGammaWrite = def.hwGammaWrite || target->isHardwareGammaEnabled(); + if(def.fsaa == 1) + { + def.fsaa = target->getFSAA(); + fsaaHint = target->getFSAAHint(); + } } - else + else if (def.fsaa == 1) { - *hwGammaWrite = false; - *fsaa = 0; - *fsaaHint = BLANKSTRING; + def.fsaa = 0; } }