[Patch] Compositor: texture_ref as rendertarget

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Post Reply
moagames
Halfling
Posts: 70
Joined: Thu Apr 10, 2008 8:10 pm
x 1

[Patch] Compositor: texture_ref as rendertarget

Post by moagames »

Hi all,

following problem:

1. first compositor renders to a texture with the scope set to chain_scope
2. last compositor in the chain uses this texture for his calculation
3. one third compositor is optional. If activated, it comes in between the first two compositors and modifies the texture from the first one

The problem here was, that the texture referenced using texture_ref could only be used as source texture, but not as a render target.
So I modified Ogre in this way:

Code: Select all

diff --git a/OgreMain/src/OgreCompositorInstance.cpp b/OgreMain/src/OgreCompositorInstance.cpp
--- a/OgreMain/src/OgreCompositorInstance.cpp
+++ b/OgreMain/src/OgreCompositorInstance.cpp
@@ -962,8 +962,72 @@
 	LocalMRTMap::iterator mi = mLocalMRTs.find(name);
 	if (mi != mLocalMRTs.end())
 		return mi->second;
-	else
-		OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Non-existent local texture name", 
+	
+	//Find the instance and check if it is before us
+	CompositionTechnique::TextureDefinition* texDef = mTechnique->getTextureDefinition(name);
+	if (!texDef->refCompName.empty()) 
+	{
+		//This is a reference - find the compositor and referenced texture definition
+		const CompositorPtr& refComp = CompositorManager::getSingleton().getByName(texDef->refCompName);
+		if (refComp.isNull())
+		{
+			OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Referencing non-existent compositor",
+				"CompositorInstance::getTargetForTex");
+		}
+		CompositionTechnique::TextureDefinition* refTexDef = refComp->getSupportedTechnique()->getTextureDefinition(texDef->refTexName);
+		if (refTexDef == 0)
+		{
+			OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Referencing non-existent compositor texture",
+				"CompositorInstance::getTargetForTex");
+		}
+
+		switch (refTexDef->scope) 
+		{
+			case CompositionTechnique::TS_CHAIN: 
+			{
+				//Find the instance and check if it is before us
+				CompositorInstance* refCompInst = 0;
+				CompositorChain::InstanceIterator it = mChain->getCompositors();
+				bool beforeMe = true;
+				while (it.hasMoreElements())
+				{
+					CompositorInstance* nextCompInst = it.getNext();
+					if (nextCompInst->getCompositor()->getName() == texDef->refCompName)
+					{
+						refCompInst = nextCompInst;
+						break;
+					}
+					if (nextCompInst == this)
+					{
+						//We encountered ourselves while searching for the compositor -
+						//we are earlier in the chain.
+						beforeMe = false;
+					}
+				}
+				
+				if (refCompInst == 0 || !refCompInst->getEnabled()) 
+				{
+					OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Referencing inactive compositor texture",
+						"CompositorInstance::getTargetForTex");
+				}
+				if (!beforeMe)
+				{
+					OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Referencing compositor that is later in the chain",
+						"CompositorInstance::getTargetForTex");
+				}
+				return refCompInst->getRenderTarget(texDef->refTexName);
+			}
+			case CompositionTechnique::TS_GLOBAL:
+				//Chain and global case - the referenced compositor will know how to handle
+				return refComp->getRenderTarget(texDef->refTexName);
+			case CompositionTechnique::TS_LOCAL:
+			default:
+				OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Referencing local compositor texture",
+					"CompositorInstance::getTargetForTex");
+		}
+	}
+
+	OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Non-existent local texture name", 
 			"CompositorInstance::getTargetForTex");
 
 }

And now it works :)
Hope it's helpful for anyone
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Re: [Patch] Compositor: texture_ref as rendertarget

Post by sinbad »

Interesting - yes the texture_ref feature was envisaged to be for pulling in the results of previous compositors, rather than rendering to them again. But I don't see any problem immediately with allowing this.

Please can you officially submit this as a patch as described here: http://www.ogre3d.org/developers/submit-patch . The patch won't get lost if it's on the patch list, and also we'll need a contributor license from you to accept this into the core.

Thanks!
moagames
Halfling
Posts: 70
Joined: Thu Apr 10, 2008 8:10 pm
x 1

Re: [Patch] Compositor: texture_ref as rendertarget

Post by moagames »

done :)
User avatar
Noman
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 714
Joined: Mon Jan 31, 2005 7:21 pm
Location: Israel
x 2
Contact:

Re: [Patch] Compositor: texture_ref as rendertarget

Post by Noman »

Applied!

Thanks!
Post Reply