Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trenderresourcemanager.h"
Toshihiro Shimizu 890ddd
#include "trenderer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Resource managers builder for instance-scoped resource managers
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
This manager class is used to create and destroy instance-scope managers through the
Toshihiro Shimizu 890ddd
renderStart/renderEnd notifications. Observe that this involves maintenance of a container
Toshihiro Shimizu 890ddd
structure of the active renderIds against resource managers.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RenderInstanceManagersBuilder : public TRenderResourceManager
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	T_RENDER_RESOURCE_MANAGER
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef std::vector<trenderresourcemanager *=""> ManagersVector;</trenderresourcemanager>
Toshihiro Shimizu 890ddd
	std::map<unsigned long,="" managersvector=""> m_managersMap;</unsigned>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder() {}
Toshihiro Shimizu 890ddd
	~RenderInstanceManagersBuilder() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static RenderInstanceManagersBuilder *instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRenderResourceManager *getManager(unsigned long renderId, unsigned int idx) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void onRenderInstanceStart(unsigned long id);
Toshihiro Shimizu 890ddd
	void onRenderInstanceEnd(unsigned long id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool renderHasOwnership() { return false; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RenderInstanceManagersBuilderGenerator : public TRenderResourceManagerGenerator
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TRenderResourceManager *operator()(void)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return RenderInstanceManagersBuilder::instance();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
MANAGER_FILESCOPE_DECLARATION(RenderInstanceManagersBuilder, RenderInstanceManagersBuilderGenerator);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Stub managers and generators
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
These manager-generator stubs are substitutes used to maintain dependency order about managers
Toshihiro Shimizu 890ddd
which have render instance scope. They retrieve ordered event calls that are passed to the
Toshihiro Shimizu 890ddd
dedicated instanceScope handler.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class InstanceResourceManagerStub : public TRenderResourceManager
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRenderResourceManagerGenerator *m_generator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	InstanceResourceManagerStub(TRenderResourceManagerGenerator *generator)
Toshihiro Shimizu 890ddd
		: m_generator(generator)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void onRenderInstanceStart(unsigned long id);
Toshihiro Shimizu 890ddd
	void onRenderInstanceEnd(unsigned long id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void onRenderFrameStart(double f);
Toshihiro Shimizu 890ddd
	void onRenderFrameEnd(double f);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void onRenderStatusStart(int renderStatus);
Toshihiro Shimizu 890ddd
	virtual void onRenderStatusEnd(int renderStatus);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class StubGenerator : public TRenderResourceManagerGenerator
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRenderResourceManagerGenerator *m_generator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	StubGenerator(TRenderResourceManagerGenerator *generator)
Toshihiro Shimizu 890ddd
		: m_generator(generator)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRenderResourceManager *operator()()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return new InstanceResourceManagerStub(m_generator);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    TRenderResourceManagerGenerator methods
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::vector<trenderresourcemanagergenerator *=""> &TRenderResourceManagerGenerator::generators()</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static std::vector<trenderresourcemanagergenerator *=""> generatorsInstance;</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
	return generatorsInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::vector<trenderresourcemanagergenerator *=""> &TRenderResourceManagerGenerator::generators(bool instanceScope)</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static std::vector<trenderresourcemanagergenerator *=""> generatorsInstance;</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
	static std::vector<trenderresourcemanagergenerator *=""> generatorsRenderer;</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
	return instanceScope ? generatorsInstance : generatorsRenderer;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRenderResourceManagerGenerator::TRenderResourceManagerGenerator(bool renderInstanceScope)
Toshihiro Shimizu 890ddd
	: m_instanceScope(renderInstanceScope)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//In case this has a renderInstanceScope, build a stub generator
Toshihiro Shimizu 890ddd
	if (renderInstanceScope) {
Toshihiro Shimizu 890ddd
		RenderInstanceManagersBuilder::gen(); //Stubs depend on this manager
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static std::vector<trenderresourcemanagergenerator *=""> stubGenerators;</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
		stubGenerators.push_back(new StubGenerator(this));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	generators().push_back(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::vector<trenderresourcemanagergenerator *=""> &scopeGenerators =</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
		generators(renderInstanceScope);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	scopeGenerators.push_back(this);
Toshihiro Shimizu 890ddd
	m_managerIndex = scopeGenerators.size() - 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRenderResourceManager *TRenderResourceManagerGenerator::getManager(const TRenderer &renderer) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_instanceScope ? 0 : renderer.getManager(m_managerIndex);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRenderResourceManager *TRenderResourceManagerGenerator::getManager(unsigned long renderId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_instanceScope ? RenderInstanceManagersBuilder::instance()->getManager(renderId, m_managerIndex) : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    "Instance-scoped Managers" - Management methods
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RenderInstanceManagersBuilder *RenderInstanceManagersBuilder::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static RenderInstanceManagersBuilder theInstance;
Toshihiro Shimizu 890ddd
	return &theInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline TRenderResourceManager *RenderInstanceManagersBuilder::
Toshihiro Shimizu 890ddd
	getManager(unsigned long renderId, unsigned int idx) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::map<unsigned long,="" managersvector="">::const_iterator it = m_managersMap.find(renderId);</unsigned>
Toshihiro Shimizu 890ddd
	return it == m_managersMap.end() ? 0 : it->second[idx];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RenderInstanceManagersBuilder::onRenderInstanceStart(unsigned long id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Build the instance managers
Toshihiro Shimizu 890ddd
	std::map<unsigned long,="" managersvector="">::iterator it =</unsigned>
Toshihiro Shimizu 890ddd
		m_managersMap.insert(std::make_pair(id, ManagersVector())).first;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::vector<trenderresourcemanagergenerator *=""> &instanceScopeGenerators =</trenderresourcemanagergenerator>
Toshihiro Shimizu 890ddd
		TRenderResourceManagerGenerator::generators(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	unsigned int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < instanceScopeGenerators.size(); ++i)
Toshihiro Shimizu 890ddd
		it->second.push_back((*instanceScopeGenerators[i])());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RenderInstanceManagersBuilder::onRenderInstanceEnd(unsigned long id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Delete the instance managers
Toshihiro Shimizu 890ddd
	std::map<unsigned long,="" managersvector="">::iterator it =</unsigned>
Toshihiro Shimizu 890ddd
		m_managersMap.find(id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(it != m_managersMap.end());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	unsigned int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < it->second.size(); ++i) {
Toshihiro Shimizu 890ddd
		if (it->second[i]->renderHasOwnership())
Toshihiro Shimizu 890ddd
			delete it->second[i];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_managersMap.erase(it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderInstanceStart(unsigned long id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(id,
Toshihiro Shimizu 890ddd
														  m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderInstanceStart(id);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderInstanceEnd(unsigned long id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(id,
Toshihiro Shimizu 890ddd
														  m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderInstanceEnd(id);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderFrameStart(double f)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(
Toshihiro Shimizu 890ddd
												 TRenderer::renderId(),
Toshihiro Shimizu 890ddd
												 m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderFrameStart(f);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderFrameEnd(double f)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(
Toshihiro Shimizu 890ddd
												 TRenderer::renderId(),
Toshihiro Shimizu 890ddd
												 m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderFrameEnd(f);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderStatusStart(int renderStatus)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(
Toshihiro Shimizu 890ddd
												 TRenderer::renderId(),
Toshihiro Shimizu 890ddd
												 m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderStatusStart(renderStatus);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InstanceResourceManagerStub::onRenderStatusEnd(int renderStatus)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	RenderInstanceManagersBuilder::instance()->getManager(
Toshihiro Shimizu 890ddd
												 TRenderer::renderId(),
Toshihiro Shimizu 890ddd
												 m_generator->getGeneratorIndex())
Toshihiro Shimizu 890ddd
		->onRenderStatusEnd(renderStatus);
Toshihiro Shimizu 890ddd
}