Browse Source

Application using OpenGL renderer shuts down properly

Marko Pintera 13 years ago
parent
commit
7c61af0cd7

+ 11 - 3
CamelotClient/CamelotClient.cpp

@@ -216,8 +216,8 @@ int CALLBACK WinMain(
 
 
 	//_ASSERT(_CrtCheckMemory());
 	//_ASSERT(_CrtCheckMemory());
 
 
-	//testRenderable->setMesh(dbgMesh);
-	//testRenderable->setMaterial(testMaterial);
+	testRenderable->setMesh(dbgMesh);
+	testRenderable->setMaterial(testMaterial);
 
 
 	//// Set the new state for the flag
 	//// Set the new state for the flag
 	//_CrtSetDbgFlag( tmpFlag );
 	//_CrtSetDbgFlag( tmpFlag );
@@ -225,7 +225,7 @@ int CALLBACK WinMain(
 	gApplication().runMainLoop();
 	gApplication().runMainLoop();
 
 
 	// Release everything before shutdown
 	// Release everything before shutdown
-	//testMaterial->destroy();
+	testMaterial->destroy();
 	testMaterial.reset();
 	testMaterial.reset();
 
 
 	testTex->destroy();
 	testTex->destroy();
@@ -244,6 +244,12 @@ int CALLBACK WinMain(
 
 
 	testModelGO->destroy();
 	testModelGO->destroy();
 	testModelGO = nullptr;
 	testModelGO = nullptr;
+	testRenderable = nullptr;
+
+	cameraGO->destroy();
+	cameraGO = nullptr;
+	camera = nullptr;
+	debugCamera = nullptr;
 
 
 	newPassGL = nullptr;
 	newPassGL = nullptr;
 	newTechniqueGL = nullptr;
 	newTechniqueGL = nullptr;
@@ -255,6 +261,8 @@ int CALLBACK WinMain(
 	newTechniqueDX11 = nullptr;
 	newTechniqueDX11 = nullptr;
 
 
 	testShader = nullptr;
 	testShader = nullptr;
+	
+	renderWindow = nullptr;
 
 
 	gApplication().shutDown();
 	gApplication().shutDown();
 
 

+ 0 - 24
CamelotGLRenderer/Source/CmGLRenderSystem.cpp

@@ -211,12 +211,6 @@ namespace CamelotEngine
 		if(mProgramPipelineManager != nullptr)
 		if(mProgramPipelineManager != nullptr)
 			delete mProgramPipelineManager;
 			delete mProgramPipelineManager;
 
 
-		// Destroy render windows
-		for (auto i = mRenderTargets.begin(); i != mRenderTargets.end(); ++i)
-		{
-			delete *i;
-		}
-
 		mRenderTargets.clear();
 		mRenderTargets.clear();
 
 
 		if(mGLSupport)
 		if(mGLSupport)
@@ -231,24 +225,6 @@ namespace CamelotEngine
 		GpuProgram* bindingPrg = prg->getBindingDelegate();
 		GpuProgram* bindingPrg = prg->getBindingDelegate();
 		GLSLGpuProgram* glprg = static_cast<GLSLGpuProgram*>(bindingPrg);
 		GLSLGpuProgram* glprg = static_cast<GLSLGpuProgram*>(bindingPrg);
 
 
-		// Unbind previous gpu program first.
-		//
-		// Note:
-		//  1. Even if both previous and current are the same object, we can't
-		//     bypass re-bind completely since the object itself maybe modified.
-		//     But we can bypass unbind based on the assumption that object
-		//     internally GL program type shouldn't be changed after it has
-		//     been created. The behavior of bind to a GL program type twice
-		//     should be same as unbind and rebind that GL program type, even
-		//     for difference objects.
-		//  2. We also assumed that the program's type (vertex or fragment) should
-		//     not be changed during it's in using. If not, the following switch
-		//     statement will confuse GL state completely, and we can't fix it
-		//     here. To fix this case, we must coding the program implementation
-		//     itself, if type is changing (during load/unload, etc), and it's inuse,
-		//     unbind and notify render system to correct for its state.
-		//
-
 		switch (glprg->getType())
 		switch (glprg->getType())
 		{
 		{
 		case GPT_VERTEX_PROGRAM:
 		case GPT_VERTEX_PROGRAM:

+ 2 - 0
CamelotRenderer/Include/CmMaterial.h

@@ -31,6 +31,8 @@ namespace CamelotEngine
 	class CM_EXPORT Material : public Resource
 	class CM_EXPORT Material : public Resource
 	{
 	{
 	public:
 	public:
+		~Material();
+
 		/**
 		/**
 		 * @brief	Sets a shader that will be used by the material. 
 		 * @brief	Sets a shader that will be used by the material. 
 		 * 			Shaders best technique will be retrieved and used in all subsequent Material
 		 * 			Shaders best technique will be retrieved and used in all subsequent Material

+ 12 - 0
CamelotRenderer/Include/CmRenderStateManager.h

@@ -8,6 +8,8 @@ namespace CamelotEngine
 	class CM_EXPORT RenderStateManager : public Module<RenderStateManager>
 	class CM_EXPORT RenderStateManager : public Module<RenderStateManager>
 	{
 	{
 	public:
 	public:
+		RenderStateManager();
+
 		/**
 		/**
 		 * @brief	Creates and initializes a new SamplerState.
 		 * @brief	Creates and initializes a new SamplerState.
 		 */
 		 */
@@ -55,5 +57,15 @@ namespace CamelotEngine
 		 * 			as it requires additional manual initialization that is not required normally.
 		 * 			as it requires additional manual initialization that is not required normally.
 		 */
 		 */
 		virtual BlendStatePtr createEmptyBlendState() const;
 		virtual BlendStatePtr createEmptyBlendState() const;
+
+		const SamplerStatePtr& getDefaultSamplerState() const { return mDefaultSamplerState; }
+		const BlendStatePtr& getDefaultBlendState() const { return mDefaultBlendState; }
+		const RasterizerStatePtr& getDefaultRasterizerState() const { return mDefaultRasterizerState; }
+		const DepthStencilStatePtr& getDefaultDepthStencilState() const { return mDefaultDepthStencilState; }
+	private:
+		SamplerStatePtr mDefaultSamplerState;
+		BlendStatePtr mDefaultBlendState;
+		RasterizerStatePtr mDefaultRasterizerState;
+		DepthStencilStatePtr mDefaultDepthStencilState;
 	};
 	};
 }
 }

+ 3 - 0
CamelotRenderer/Source/CmApplication.cpp

@@ -147,6 +147,9 @@ namespace CamelotEngine
 
 
 	void Application::shutDown()
 	void Application::shutDown()
 	{
 	{
+		mPrimaryRenderWindow->destroy();
+		mPrimaryRenderWindow = nullptr;
+
 		Importer::shutDown();
 		Importer::shutDown();
 		MeshManager::shutDown();
 		MeshManager::shutDown();
 
 

+ 1 - 3
CamelotRenderer/Source/CmBlendState.cpp

@@ -14,9 +14,7 @@ namespace CamelotEngine
 
 
 	const BlendStatePtr& BlendState::getDefault()
 	const BlendStatePtr& BlendState::getDefault()
 	{
 	{
-		static BlendStatePtr blendState = RenderStateManager::instance().createBlendState(BLEND_STATE_DESC());
-
-		return blendState;
+		return RenderStateManager::instance().getDefaultBlendState();
 	}
 	}
 
 
 	bool BlendState::getBlendEnabled(UINT32 renderTargetIdx) const
 	bool BlendState::getBlendEnabled(UINT32 renderTargetIdx) const

+ 8 - 0
CamelotRenderer/Source/CmCoreGpuObject.cpp

@@ -21,6 +21,14 @@ namespace CamelotEngine
 			LOGWRN("Destructor called but object is not destroyed. Object will leak.")
 			LOGWRN("Destructor called but object is not destroyed. Object will leak.")
 		}
 		}
 
 
+#if CM_DEBUG_MODE
+		if(!mThis.expired())
+		{
+			CM_EXCEPT(InternalErrorException, "Shared pointer to this object still has active references but " \
+				"the object is being deleted? You shouldn't delete CoreGpuObjects manually.");
+		}
+#endif
+
 		CoreGpuObjectManager::instance().unregisterObject(this);
 		CoreGpuObjectManager::instance().unregisterObject(this);
 	}
 	}
 
 

+ 1 - 0
CamelotRenderer/Source/CmDeferredRenderContext.cpp

@@ -179,6 +179,7 @@ namespace CamelotEngine
 	void DeferredRenderContext::cancelAll()
 	void DeferredRenderContext::cancelAll()
 	{
 	{
 		vector<CommandQueue::Command>::type* commands = mCommandQueue->flush();
 		vector<CommandQueue::Command>::type* commands = mCommandQueue->flush();
+		delete commands;
 
 
 		// TODO - This cancels the commands but doesn't revert the state variables
 		// TODO - This cancels the commands but doesn't revert the state variables
 	}
 	}

+ 1 - 3
CamelotRenderer/Source/CmDepthStencilState.cpp

@@ -15,9 +15,7 @@ namespace CamelotEngine
 
 
 	const DepthStencilStatePtr& DepthStencilState::getDefault()
 	const DepthStencilStatePtr& DepthStencilState::getDefault()
 	{
 	{
-		static DepthStencilStatePtr depthStencilState = RenderStateManager::instance().createDepthStencilState(DEPTH_STENCIL_STATE_DESC());
-
-		return depthStencilState;
+		return RenderStateManager::instance().getDefaultDepthStencilState();
 	}
 	}
 
 
 	DepthStencilStatePtr DepthStencilState::create(const DEPTH_STENCIL_STATE_DESC& desc)
 	DepthStencilStatePtr DepthStencilState::create(const DEPTH_STENCIL_STATE_DESC& desc)

+ 5 - 0
CamelotRenderer/Source/CmMaterial.cpp

@@ -18,6 +18,11 @@ namespace CamelotEngine
 
 
 	}
 	}
 
 
+	Material::~Material()
+	{
+
+	}
+
 	void Material::setShader(ShaderPtr shader)
 	void Material::setShader(ShaderPtr shader)
 	{
 	{
 		mShader = shader;
 		mShader = shader;

+ 1 - 3
CamelotRenderer/Source/CmRasterizerState.cpp

@@ -14,9 +14,7 @@ namespace CamelotEngine
 
 
 	const RasterizerStatePtr& RasterizerState::getDefault()
 	const RasterizerStatePtr& RasterizerState::getDefault()
 	{
 	{
-		static RasterizerStatePtr rasterizerState = RenderStateManager::instance().createRasterizerState(RASTERIZER_STATE_DESC());
-
-		return rasterizerState;
+		return RenderStateManager::instance().getDefaultRasterizerState();
 	}
 	}
 
 
 	RasterizerStatePtr RasterizerState::create(const RASTERIZER_STATE_DESC& desc)
 	RasterizerStatePtr RasterizerState::create(const RASTERIZER_STATE_DESC& desc)

+ 8 - 0
CamelotRenderer/Source/CmRenderStateManager.cpp

@@ -6,6 +6,14 @@
 
 
 namespace CamelotEngine
 namespace CamelotEngine
 {
 {
+	RenderStateManager::RenderStateManager()
+	{
+		mDefaultSamplerState = createSamplerState(SAMPLER_STATE_DESC());
+		mDefaultBlendState = createBlendState(BLEND_STATE_DESC());
+		mDefaultRasterizerState = createRasterizerState(RASTERIZER_STATE_DESC());
+		mDefaultDepthStencilState = createDepthStencilState(DEPTH_STENCIL_STATE_DESC());
+	}
+
 	SamplerStatePtr RenderStateManager::createSamplerState(const SAMPLER_STATE_DESC& desc) const
 	SamplerStatePtr RenderStateManager::createSamplerState(const SAMPLER_STATE_DESC& desc) const
 	{
 	{
 		SamplerStatePtr samplerState = SamplerStatePtr(new SamplerState());
 		SamplerStatePtr samplerState = SamplerStatePtr(new SamplerState());

+ 0 - 10
CamelotRenderer/Source/CmRenderSystem.cpp

@@ -113,16 +113,6 @@ namespace CamelotEngine {
 	void RenderSystem::destroy_internal()
 	void RenderSystem::destroy_internal()
 	{
 	{
 		// Remove all the render targets.
 		// Remove all the render targets.
-		// (destroy primary target last since others may depend on it)
-		RenderTarget* primary = 0;
-		for (auto it = mRenderTargets.begin(); it != mRenderTargets.end(); ++it)
-		{
-			if (!primary && (*it)->isPrimary())
-				primary = *it;
-			else
-				delete *it;
-		}
-		delete primary;
 		mRenderTargets.clear();
 		mRenderTargets.clear();
 
 
 		mPrioritisedRenderTargets.clear();
 		mPrioritisedRenderTargets.clear();

+ 1 - 3
CamelotRenderer/Source/CmSamplerState.cpp

@@ -15,9 +15,7 @@ namespace CamelotEngine
 
 
 	const SamplerStatePtr& SamplerState::getDefault()
 	const SamplerStatePtr& SamplerState::getDefault()
 	{
 	{
-		static SamplerStatePtr samplerState = RenderStateManager::instance().createSamplerState(SAMPLER_STATE_DESC());
-
-		return samplerState;
+		return RenderStateManager::instance().getDefaultSamplerState();
 	}
 	}
 
 
 	FilterOptions SamplerState::getTextureFiltering(FilterType ft) const
 	FilterOptions SamplerState::getTextureFiltering(FilterType ft) const