2
0
Эх сурвалжийг харах

Implemented CoreGpuObject initialize/destroy for four state classes

Marko Pintera 13 жил өмнө
parent
commit
a516036ec6

+ 3 - 0
CamelotClient/CamelotClient.cpp

@@ -239,6 +239,9 @@ int CALLBACK WinMain(
 	vertProgRef.reset();
 	vertProg = nullptr;
 
+	testModelGO->destroy();
+	testModelGO = nullptr;
+
 	newPassGL = nullptr;
 	newTechniqueGL = nullptr;
 

+ 8 - 0
CamelotD3D11RenderSystem/Include/CmD3D11BlendState.h

@@ -17,8 +17,16 @@ namespace CamelotEngine
 
 		D3D11BlendState();
 
+		/**
+		 * @copydoc BlendState::initialize_internal().
+		 */
 		void initialize_internal();
 
+		/**
+		 * @copydoc BlendState::destroy_internal().
+		 */
+		void destroy_internal();
+
 		ID3D11BlendState* mBlendState;
 	};
 }

+ 8 - 0
CamelotD3D11RenderSystem/Include/CmD3D11DepthStencilState.h

@@ -17,8 +17,16 @@ namespace CamelotEngine
 
 		D3D11DepthStencilState();
 
+		/**
+		 * @copydoc DepthStencilState::initialize_internal().
+		 */
 		void initialize_internal();
 
+		/**
+		 * @copydoc DepthStencilState::destroy_internal().
+		 */
+		void destroy_internal();
+
 		ID3D11DepthStencilState* mDepthStencilState;
 	};
 }

+ 8 - 0
CamelotD3D11RenderSystem/Include/CmD3D11RasterizerState.h

@@ -16,8 +16,16 @@ namespace CamelotEngine
 
 		D3D11RasterizerState();
 
+		/**
+		 * @copydoc RasterizerState::initialize_internal().
+		 */
 		void initialize_internal();
 
+		/**
+		 * @copydoc RasterizerState::destroy_internal().
+		 */
+		void destroy_internal();
+
 		ID3D11RasterizerState* mRasterizerState;
 	};
 }

+ 8 - 0
CamelotD3D11RenderSystem/Include/CmD3D11SamplerState.h

@@ -16,8 +16,16 @@ namespace CamelotEngine
 
 		D3D11SamplerState();
 
+		/**
+		 * @copydoc SamplerState::initialize_internal().
+		 */
 		void initialize_internal();
 
+		/**
+		 * @copydoc SamplerState::destroy_internal().
+		 */
+		void destroy_internal();
+
 		ID3D11SamplerState* mSamplerState;
 	};
 }

+ 7 - 1
CamelotD3D11RenderSystem/Source/CmD3D11BlendState.cpp

@@ -11,7 +11,6 @@ namespace CamelotEngine
 
 	D3D11BlendState::~D3D11BlendState()
 	{
-		SAFE_RELEASE(mBlendState);
 	}
 
 	void D3D11BlendState::initialize_internal()
@@ -46,4 +45,11 @@ namespace CamelotEngine
 
 		BlendState::initialize_internal();
 	}
+
+	void D3D11BlendState::destroy_internal()
+	{
+		SAFE_RELEASE(mBlendState);
+
+		BlendState::destroy_internal();
+	}
 }

+ 8 - 1
CamelotD3D11RenderSystem/Source/CmD3D11DepthStencilState.cpp

@@ -11,7 +11,7 @@ namespace CamelotEngine
 
 	D3D11DepthStencilState::~D3D11DepthStencilState()
 	{
-		SAFE_RELEASE(mDepthStencilState);
+		
 	}
 
 	void D3D11DepthStencilState::initialize_internal()
@@ -46,4 +46,11 @@ namespace CamelotEngine
 
 		DepthStencilState::initialize_internal();
 	}
+
+	void D3D11DepthStencilState::destroy_internal()
+	{
+		SAFE_RELEASE(mDepthStencilState);
+
+		DepthStencilState::destroy_internal();
+	}
 }

+ 7 - 1
CamelotD3D11RenderSystem/Source/CmD3D11RasterizerState.cpp

@@ -11,7 +11,6 @@ namespace CamelotEngine
 
 	D3D11RasterizerState::~D3D11RasterizerState()
 	{
-		SAFE_RELEASE(mRasterizerState);
 	}
 
 	void D3D11RasterizerState::initialize_internal()
@@ -41,4 +40,11 @@ namespace CamelotEngine
 
 		RasterizerState::initialize_internal();
 	}
+
+	void D3D11RasterizerState::destroy_internal()
+	{
+		SAFE_RELEASE(mRasterizerState);
+
+		RasterizerState::destroy_internal();
+	}
 }

+ 7 - 1
CamelotD3D11RenderSystem/Source/CmD3D11SamplerState.cpp

@@ -11,7 +11,6 @@ namespace CamelotEngine
 
 	D3D11SamplerState::~D3D11SamplerState()
 	{
-		SAFE_RELEASE(mSamplerState);
 	}
 
 	void D3D11SamplerState::initialize_internal()
@@ -99,4 +98,11 @@ namespace CamelotEngine
 
 		SamplerState::initialize_internal();
 	}
+
+	void D3D11SamplerState::destroy_internal()
+	{
+		SAFE_RELEASE(mSamplerState);
+
+		SamplerState::destroy_internal();
+	}
 }

+ 1 - 1
CamelotRenderer/Source/CmBlendState.cpp

@@ -9,7 +9,7 @@ namespace CamelotEngine
 	{
 		mData = desc;
 
-		RenderSystem::instancePtr()->queueCommand(boost::bind(&BlendState::initialize_internal, this));
+		CoreGpuObject::initialize();
 	}
 
 	const BlendStatePtr& BlendState::getDefault()

+ 1 - 1
CamelotRenderer/Source/CmDepthStencilState.cpp

@@ -10,7 +10,7 @@ namespace CamelotEngine
 	{
 		mData = desc;
 
-		RenderSystem::instancePtr()->queueCommand(boost::bind(&DepthStencilState::initialize_internal, this));
+		CoreGpuObject::initialize();
 	}
 
 	const DepthStencilStatePtr& DepthStencilState::getDefault()

+ 8 - 1
CamelotRenderer/Source/CmMesh.cpp

@@ -23,7 +23,8 @@ namespace CamelotEngine
 	}
 
 	Mesh::~Mesh()
-	{ }
+	{
+	}
 
 	void Mesh::setMeshData(MeshDataPtr meshData)
 	{
@@ -39,6 +40,12 @@ namespace CamelotEngine
 			CM_EXCEPT(InternalErrorException, "Cannot load mesh. Mesh data is null.");
 		}
 
+		if(mVertexData != nullptr)
+			delete mVertexData;
+
+		if(mIndexData != nullptr)
+			delete mIndexData;
+
 		// Submeshes
 		for(UINT32 i = 0; i < meshData->subMeshes.size(); i++)
 			mSubMeshes.push_back(SubMesh(meshData->subMeshes[i].indexOffset, meshData->subMeshes[i].indexCount));

+ 1 - 1
CamelotRenderer/Source/CmRasterizerState.cpp

@@ -9,7 +9,7 @@ namespace CamelotEngine
 	{
 		mData = desc;
 
-		RenderSystem::instancePtr()->queueCommand(boost::bind(&RasterizerState::initialize_internal, this));
+		CoreGpuObject::initialize();
 	}
 
 	const RasterizerStatePtr& RasterizerState::getDefault()

+ 20 - 4
CamelotRenderer/Source/CmRenderStateManager.cpp

@@ -9,6 +9,7 @@ namespace CamelotEngine
 	SamplerStatePtr RenderStateManager::createSamplerState(const SAMPLER_STATE_DESC& desc) const
 	{
 		SamplerStatePtr samplerState = SamplerStatePtr(new SamplerState());
+		samplerState->setThisPtr(samplerState);
 		samplerState->initialize(desc);
 
 		return samplerState;
@@ -17,6 +18,7 @@ namespace CamelotEngine
 	DepthStencilStatePtr RenderStateManager::createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const
 	{
 		DepthStencilStatePtr depthStencilState = DepthStencilStatePtr(new DepthStencilState());
+		depthStencilState->setThisPtr(depthStencilState);
 		depthStencilState->initialize(desc);
 
 		return depthStencilState;
@@ -25,6 +27,7 @@ namespace CamelotEngine
 	RasterizerStatePtr RenderStateManager::createRasterizerState(const RASTERIZER_STATE_DESC& desc) const
 	{
 		RasterizerStatePtr rasterizerState = RasterizerStatePtr(new RasterizerState());
+		rasterizerState->setThisPtr(rasterizerState);
 		rasterizerState->initialize(desc);
 
 		return rasterizerState;
@@ -33,6 +36,7 @@ namespace CamelotEngine
 	BlendStatePtr RenderStateManager::createBlendState(const BLEND_STATE_DESC& desc) const
 	{
 		BlendStatePtr blendState = BlendStatePtr(new BlendState());
+		blendState->setThisPtr(blendState);
 		blendState->initialize(desc);
 
 		return blendState;
@@ -40,21 +44,33 @@ namespace CamelotEngine
 
 	SamplerStatePtr RenderStateManager::createEmptySamplerState() const
 	{
-		return SamplerStatePtr(new SamplerState());
+		SamplerStatePtr samplerState = SamplerStatePtr(new SamplerState());
+		samplerState->setThisPtr(samplerState);
+
+		return samplerState;
 	}
 
 	DepthStencilStatePtr RenderStateManager::createEmptyDepthStencilState() const
 	{
-		return DepthStencilStatePtr(new DepthStencilState());
+		DepthStencilStatePtr depthStencilState = DepthStencilStatePtr(new DepthStencilState());
+		depthStencilState->setThisPtr(depthStencilState);
+
+		return depthStencilState;
 	}
 
 	RasterizerStatePtr RenderStateManager::createEmptyRasterizerState() const
 	{
-		return RasterizerStatePtr(new RasterizerState());
+		RasterizerStatePtr rasterizerState = RasterizerStatePtr(new RasterizerState());
+		rasterizerState->setThisPtr(rasterizerState);
+
+		return rasterizerState;
 	}
 
 	BlendStatePtr RenderStateManager::createEmptyBlendState() const
 	{
-		return BlendStatePtr(new BlendState());
+		BlendStatePtr blendState = BlendStatePtr(new BlendState());
+		blendState->setThisPtr(blendState);
+
+		return blendState;
 	}
 }

+ 1 - 1
CamelotRenderer/Source/CmSamplerState.cpp

@@ -10,7 +10,7 @@ namespace CamelotEngine
 	{
 		mData = desc;
 
-		RenderSystem::instancePtr()->queueCommand(boost::bind(&SamplerState::initialize_internal, this));
+		CoreGpuObject::initialize();
 	}
 
 	const SamplerStatePtr& SamplerState::getDefault()

+ 2 - 2
CamelotRenderer/Source/CmVertexData.cpp

@@ -37,7 +37,7 @@ THE SOFTWARE.
 namespace CamelotEngine 
 {
 	VertexData::VertexData(HardwareBufferManager* mgr)
-		:mOwnsDeclaration(false)
+		:mOwnsDeclaration(true)
 	{
 		mMgr = mgr ? mgr : HardwareBufferManager::instancePtr();
 		vertexDeclaration = mMgr->createVertexDeclaration();
@@ -46,7 +46,7 @@ namespace CamelotEngine
 	}
 
 	VertexData::VertexData(VertexDeclarationPtr dcl)
-		:mOwnsDeclaration(true)
+		:mOwnsDeclaration(false)
 	{
 		// this is a fallback rather than actively used
 		mMgr = HardwareBufferManager::instancePtr();

+ 8 - 13
CamelotRenderer/TODO.txt

@@ -17,17 +17,13 @@
 Pass
  - A way to bind buffers to a Pass, while specifying buffer range
  - GpuParams support for bools, buffers, structs
- - Fix MaterialRTTI saving (Save params per gpuprogram is simplest and cleanest) (Maybe wait until I have the Parser sorted out?)
- - Fix how and when is GpuParamBlock created/destroyed. Needs to happen on the render thread
 
-Resource destruction:
-Resources keep a permanent reference to each resource so they are never freed
- - Keep just a weak ptr?
-
-OR
-
-Keep a strong ptr, when Resource::destroy() internally calls Resources to remove the reference?
-And Resources.DestroyUnusedAssets destroys all with just 1 reference?
+ Null mesh VertexDeclaration never gets freed.
+  - Similar issue with all the four states and their defaults
+ Add MeshManager which creates a mesh and also creates and releases null mesh.
+Even if all resources are released I still get a crash on exit. Probably because I don't properly release all factories.
+ Test DX9 and DX11 versions, and see if everything is released properly.
+When rendering with renderable a lot of references don't get released so I need to check where are they kept
 
 Stuff that needs to derive from CoreGpuObject:
  - GpuProgram (DONE)
@@ -41,7 +37,7 @@ Stuff that needs to derive from CoreGpuObject:
  - RenderWindow (DONE)
  - RenderTexture (DONE)
  - MultiRenderTexture (DONE)
- - Blend/DepthStencil/Rasterizer/SamplerState
+ - Blend/DepthStencil/Rasterizer/SamplerState (DONE)
  - GpuParamBlock
 ---
  Maybe
@@ -59,8 +55,6 @@ RenderTextures and MultiRenderTextures will only work properly if used directly
 OpenGL render window no longer looks for a monitor index
 
 CoreGpuObject and CoreGpuObjectManager
-  - Attempt to provide a debug version that tracks the callstack where resource was allocated, so user can know exactly which resource is he failing to release
- - Need to make sure I can release handles (ResourceHandle::reset()?)
  - make ::Destroy() virtual as Resources need to make it private
  
 ResourceHandle::reset removes ALL references to a resource. It's probably smarter if it just removes the one reference
@@ -98,6 +92,7 @@ Can be delayed:
   - RENDERWINDOWDESC accepts a "externalWindow" flag and an "externalHandle" so when creating the primary window with RenderSystem::initialize we don't always need to create a new window
   - Actually new OpenGL seems to support creating context without a window with the help of wglCreateContextAttribsARB and wglMakeCurrent:
  ImportOptions
+ Instead of doing setThisPtr on every CoreGpuObject, use intrusive shared_ptr instead?
 >>>>>>>>>>>>>>>START WORKING ON THE EDITOR!