Quellcode durchsuchen

Ported D3D9 to use memory allocator

Marko Pintera vor 12 Jahren
Ursprung
Commit
63c189c3c6

+ 2 - 2
CamelotClient/CamelotClient.cpp

@@ -27,8 +27,8 @@
 #include "CmTestTextSprite.h"
 
 //#define DX11
-//#define DX9
-#define GL
+#define DX9
+//#define GL
 
 using namespace CamelotEngine;
 

+ 2 - 1
CamelotD3D9Renderer/Include/CmD3D9RenderSystemFactory.h

@@ -24,7 +24,8 @@ namespace CamelotEngine
 						static RenderSystemFactoryPtr newFactory;
 						if(newFactory == nullptr)
 						{
-							newFactory = RenderSystemFactoryPtr(new D3D9RenderSystemFactory());
+							newFactory = RenderSystemFactoryPtr(CM_NEW(D3D9RenderSystemFactory, GenAlloc) D3D9RenderSystemFactory(),
+								&MemAllocDeleter<D3D9RenderSystemFactory, GenAlloc>::deleter);
 							RenderSystemManager::instance().registerRenderSystemFactory(newFactory);
 						}
 					}

+ 14 - 6
CamelotD3D9Renderer/Source/CmD3D9Device.cpp

@@ -84,7 +84,7 @@ namespace CamelotEngine
 
 		if (it == mMapRenderWindowToResoruces.end())
 		{
-			RenderWindowResources* renderWindowResources = new RenderWindowResources;
+			RenderWindowResources* renderWindowResources = CM_NEW(RenderWindowResources, PoolAlloc) RenderWindowResources;
 
 			memset(renderWindowResources, 0, sizeof(RenderWindowResources));						
 			renderWindowResources->adapterOrdinalInGroupIndex = 0;					
@@ -116,7 +116,8 @@ namespace CamelotEngine
 
 			releaseRenderWindowResources(renderWindowResources);
 
-			SAFE_DELETE(renderWindowResources);
+			if(renderWindowResources != nullptr)
+				CM_DELETE(renderWindowResources, RenderWindowResources, PoolAlloc);
 			
 			mMapRenderWindowToResoruces.erase(it);		
 		}
@@ -281,7 +282,9 @@ namespace CamelotEngine
 			if (it->first->getWindowHandle() == msSharedFocusWindow)
 				setSharedWindowHandle(NULL);
 
-			SAFE_DELETE(it->second);
+			if(it->second != nullptr)
+				CM_DELETE(it->second, RenderWindowResources, PoolAlloc);
+
 			++it;
 		}
 		mMapRenderWindowToResoruces.clear();
@@ -289,7 +292,10 @@ namespace CamelotEngine
 		// Reset dynamic attributes.		
 		mFocusWindow			= NULL;		
 		mD3D9DeviceCapsValid	= false;
-		SAFE_DELETE_ARRAY(mPresentationParams);
+
+		if(mPresentationParams != nullptr)
+			CM_DELETE_ARRAY(mPresentationParams, D3DPRESENT_PARAMETERS, mPresentationParamsCount, PoolAlloc);
+
 		mPresentationParamsCount = 0;
 
 		// Notify the device manager on this instance destruction.	
@@ -464,12 +470,14 @@ namespace CamelotEngine
 	void D3D9Device::updatePresentationParameters()
 	{		
 		// Clear old presentation parameters.
-		SAFE_DELETE_ARRAY(mPresentationParams);
+		if(mPresentationParams != nullptr)
+			CM_DELETE_ARRAY(mPresentationParams, D3DPRESENT_PARAMETERS, mPresentationParamsCount, PoolAlloc);
+
 		mPresentationParamsCount = 0;		
 
 		if (mMapRenderWindowToResoruces.size() > 0)
 		{
-			mPresentationParams = new D3DPRESENT_PARAMETERS[mMapRenderWindowToResoruces.size()];
+			mPresentationParams = CM_NEW_ARRAY(D3DPRESENT_PARAMETERS, (UINT32)mMapRenderWindowToResoruces.size(), PoolAlloc);
 
 			RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
 

+ 4 - 2
CamelotD3D9Renderer/Source/CmD3D9DeviceManager.cpp

@@ -222,7 +222,7 @@ namespace CamelotEngine
 		// No matching device found -> create new one.
 		if (renderDevice == NULL)
 		{
-			renderDevice = new D3D9Device(this, nAdapterOrdinal, direct3D9->GetAdapterMonitor(nAdapterOrdinal), devType, extraFlags);
+			renderDevice = CM_NEW(D3D9Device, GenAlloc) D3D9Device(this, nAdapterOrdinal, direct3D9->GetAdapterMonitor(nAdapterOrdinal), devType, extraFlags);
 			mRenderDevices.push_back(renderDevice);
 			if (mActiveDevice == NULL)			
 				setActiveDevice(renderDevice);											
@@ -272,7 +272,9 @@ namespace CamelotEngine
 			{			
 				if (*itDevice == device)
 				{					
-					SAFE_DELETE(device);
+					if(device != nullptr)
+						CM_DELETE(device, D3D9Device, GenAlloc);
+
 					mRenderDevices.erase(itDevice);
 					break;
 				}												

+ 3 - 2
CamelotD3D9Renderer/Source/CmD3D9Driver.cpp

@@ -63,7 +63,8 @@ namespace CamelotEngine
 
 	D3D9Driver::~D3D9Driver()
 	{
-		SAFE_DELETE( mpVideoModeList );		
+		if(mpVideoModeList != nullptr)
+			CM_DELETE(mpVideoModeList, D3D9VideoModeList, GenAlloc);
 	}
 
 	String D3D9Driver::DriverName() const
@@ -84,7 +85,7 @@ namespace CamelotEngine
 	D3D9VideoModeList* D3D9Driver::getVideoModeList()
 	{
 		if( !mpVideoModeList )
-			mpVideoModeList = new D3D9VideoModeList( this );
+			mpVideoModeList = CM_NEW(D3D9VideoModeList, GenAlloc) D3D9VideoModeList( this );
 
 		return mpVideoModeList;
 	}	

+ 2 - 1
CamelotD3D9Renderer/Source/CmD3D9GpuProgram.cpp

@@ -136,7 +136,8 @@ namespace CamelotEngine {
     //-----------------------------------------------------------------------
 	GpuParamsPtr D3D9GpuProgram::createParameters()
 	{
-		GpuParamsPtr params(new GpuParams(mParametersDesc));
+		GpuParamsPtr params(CM_NEW(GpuParams, PoolAlloc) GpuParams(mParametersDesc), 
+			&MemAllocDeleter<GpuParams, PoolAlloc>::deleter);
 		params->setTransposeMatrices(mColumnMajorMatrices);
 
 		return params;

+ 2 - 1
CamelotD3D9Renderer/Source/CmD3D9HLSLProgram.cpp

@@ -243,7 +243,8 @@ namespace CamelotEngine {
 	//-----------------------------------------------------------------------
 	GpuParamsPtr D3D9HLSLProgram::createParameters()
 	{
-		GpuParamsPtr params(new GpuParams(mParametersDesc));
+		GpuParamsPtr params(CM_NEW(GpuParams, PoolAlloc) GpuParams(mParametersDesc),
+			&MemAllocDeleter<GpuParams, PoolAlloc>::deleter);
 		params->setTransposeMatrices(mColumnMajorMatrices);
 
 		return params;

+ 13 - 5
CamelotD3D9Renderer/Source/CmD3D9IndexBuffer.cpp

@@ -63,7 +63,7 @@ namespace CamelotEngine {
 		mBufferDesc.Pool = eResourcePool;
 
 		// Allocate the system memory buffer.
-		mSystemMemoryBuffer = new char [getSizeInBytes()];
+		mSystemMemoryBuffer = (char*) CM_NEW_BYTES(getSizeInBytes(), ScratchAlloc);
 		memset(mSystemMemoryBuffer, 0, getSizeInBytes());
 
 		// Case we have to create this buffer resource on loading.
@@ -89,11 +89,16 @@ namespace CamelotEngine {
 		while (it != mMapDeviceToBufferResources.end())
 		{
 			SAFE_RELEASE(it->second->mBuffer);
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			++it;
 		}	
 		mMapDeviceToBufferResources.clear();   
-		SAFE_DELETE_ARRAY(mSystemMemoryBuffer);
+
+		if(mSystemMemoryBuffer != nullptr)
+			CM_DELETE_BYTES(mSystemMemoryBuffer, ScratchAlloc);
 
 		IndexBuffer::destroy_internal();
 	}
@@ -201,7 +206,10 @@ namespace CamelotEngine {
 		if (it != mMapDeviceToBufferResources.end())	
 		{									
 			SAFE_RELEASE(it->second->mBuffer);
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			mMapDeviceToBufferResources.erase(it);
 		}
 	}
@@ -247,7 +255,7 @@ namespace CamelotEngine {
 		}
 		else
 		{
-			bufferResources = new BufferResources;			
+			bufferResources = CM_NEW(BufferResources, PoolAlloc) BufferResources;			
 			mMapDeviceToBufferResources[d3d9Device] = bufferResources;
 		}
 

+ 22 - 10
CamelotD3D9Renderer/Source/CmD3D9PixelBuffer.cpp

@@ -53,7 +53,10 @@ namespace CamelotEngine
 		{
 			SAFE_RELEASE(it->second->surface);
 			SAFE_RELEASE(it->second->volume);
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			DeviceToBufferResourcesIterator deadi = it++;
 			mMapDeviceToBufferResources.erase(deadi);
 		}
@@ -106,11 +109,14 @@ namespace CamelotEngine
 					dev->TestCooperativeLevel() == D3D_OK)
 				{
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
-					PixelData dstBox(fullBufferBox, mFormat, new char[getSizeInBytes()]);
+					PixelData dstBox(fullBufferBox, mFormat, (char*)CM_NEW_BYTES(getSizeInBytes(), ScratchAlloc));
 
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
-					SAFE_DELETE_ARRAY(dstBox.data);
+
+					if(dstBox.data != nullptr)
+						CM_DELETE_BYTES(dstBox.data, ScratchAlloc);
+
 					break;
 				}
 				++it;			
@@ -162,11 +168,14 @@ namespace CamelotEngine
 					dev->TestCooperativeLevel() == D3D_OK)
 				{
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
-					PixelData dstBox(fullBufferBox, mFormat, new char[getSizeInBytes()]);
+					PixelData dstBox(fullBufferBox, mFormat, (char*)CM_NEW_BYTES(getSizeInBytes(), ScratchAlloc));
 
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
-					SAFE_DELETE(dstBox.data);
+					
+					if(dstBox.data != nullptr)
+						CM_DELETE_BYTES(dstBox.data, ScratchAlloc);
+
 					break;
 				}
 				++it;			
@@ -188,7 +197,7 @@ namespace CamelotEngine
 	//-----------------------------------------------------------------------------  
 	D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::createBufferResources()
 	{
-		BufferResources* newResources = new BufferResources;
+		BufferResources* newResources = CM_NEW(BufferResources, PoolAlloc) BufferResources;
 
 		memset(newResources, 0, sizeof(BufferResources));
 
@@ -206,7 +215,10 @@ namespace CamelotEngine
 		{
 			SAFE_RELEASE(it->second->surface);
 			SAFE_RELEASE(it->second->volume);	
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			mMapDeviceToBufferResources.erase(it);
 		}
 	}
@@ -592,7 +604,7 @@ namespace CamelotEngine
 		// convert to pixelbuffer's native format if necessary
 		if (D3D9Mappings::_getPF(src.format) == D3DFMT_UNKNOWN)
 		{
-			data = new void*[PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat)];
+			data = (void*)CM_NEW_BYTES(PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat), ScratchAlloc);
 			converted = PixelData(src.getWidth(), src.getHeight(), src.getDepth(), mFormat, data);
 			PixelUtil::bulkPixelConversion(src, converted);
 		}
@@ -670,8 +682,8 @@ namespace CamelotEngine
 		if(mDoMipmapGen)
 			_genMipmaps(dstBufferResources->mipTex);
 
-		if(data != NULL)
-			delete[] data;
+		if(data != nullptr)
+			CM_DELETE_BYTES(data, ScratchAlloc);
 	}
 
 	//-----------------------------------------------------------------------------  

+ 8 - 5
CamelotD3D9Renderer/Source/CmD3D9Texture.cpp

@@ -231,7 +231,9 @@ namespace CamelotEngine
 		{
 			TextureResources* textureResource = it->second;
 
-			SAFE_DELETE(textureResource);
+			if(textureResource != nullptr)
+				CM_DELETE(textureResource, TextureResources, PoolAlloc);
+
 			++it;
 		}		
 
@@ -258,7 +260,7 @@ namespace CamelotEngine
 	{
 		assert(mMapDeviceToTextureResources.find(d3d9Device) == mMapDeviceToTextureResources.end());
 
-		TextureResources* textureResources = new TextureResources;
+		TextureResources* textureResources = CM_NEW(TextureResources, PoolAlloc) TextureResources;
 
 		textureResources->pNormTex		= NULL;
 		textureResources->pCubeTex		= NULL;
@@ -987,8 +989,8 @@ namespace CamelotEngine
 			{
 				for(UINT32 mip=0; mip<=mNumMipmaps; ++mip)
 				{
-					buffer = new D3D9PixelBuffer((GpuBufferUsage)bufusage, this);
-					mSurfaceList.push_back(PixelBufferPtr(buffer));
+					buffer = CM_NEW(D3D9PixelBuffer, PoolAlloc) D3D9PixelBuffer((GpuBufferUsage)bufusage, this);
+					mSurfaceList.push_back(PixelBufferPtr(buffer, &MemAllocDeleter<D3D9PixelBuffer, PoolAlloc>::deleter));
 				}
 			}
 		}
@@ -1146,7 +1148,8 @@ namespace CamelotEngine
 			// after device reset.
 			freeTextureResources(d3d9Device, textureResource);
 
-			SAFE_DELETE(textureResource);
+			if(textureResource != nullptr)
+				CM_DELETE(textureResource, TextureResources, PoolAlloc);
 
 			mMapDeviceToTextureResources.erase(it);
 		}	

+ 13 - 5
CamelotD3D9Renderer/Source/CmD3D9VertexBuffer.cpp

@@ -147,7 +147,10 @@ namespace CamelotEngine {
 		if (it != mMapDeviceToBufferResources.end())	
 		{								
 			SAFE_RELEASE(it->second->mBuffer);
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			mMapDeviceToBufferResources.erase(it);
 		}	
 	}
@@ -193,7 +196,7 @@ namespace CamelotEngine {
 		}
 		else
 		{
-			bufferResources = new BufferResources;			
+			bufferResources = CM_NEW(BufferResources, PoolAlloc) BufferResources;			
 			mMapDeviceToBufferResources[d3d9Device] = bufferResources;
 		}
 
@@ -313,7 +316,7 @@ namespace CamelotEngine {
 		mBufferDesc.Pool = eResourcePool;
 
 		// Allocate the system memory buffer.
-		mSystemMemoryBuffer = new char [getSizeInBytes()];
+		mSystemMemoryBuffer = (char*)CM_NEW_BYTES(getSizeInBytes(), ScratchAlloc);
 		memset(mSystemMemoryBuffer, 0, getSizeInBytes());	
 
 		// Case we have to create this buffer resource on loading.
@@ -339,11 +342,16 @@ namespace CamelotEngine {
 		while (it != mMapDeviceToBufferResources.end())
 		{
 			SAFE_RELEASE(it->second->mBuffer);
-			SAFE_DELETE(it->second);
+
+			if(it->second != nullptr)
+				CM_DELETE(it->second, BufferResources, PoolAlloc);
+
 			++it;
 		}	
 		mMapDeviceToBufferResources.clear();   
-		SAFE_DELETE_ARRAY(mSystemMemoryBuffer);
+
+		if(mSystemMemoryBuffer != nullptr)
+			CM_DELETE_BYTES(mSystemMemoryBuffer, ScratchAlloc);
 
 		VertexBuffer::destroy_internal();
 	}

+ 2 - 2
CamelotD3D9Renderer/Source/CmD3D9VertexDeclaration.cpp

@@ -121,7 +121,7 @@ namespace CamelotEngine {
 		// Case we have to create the declaration for this device.
 		if (it == mMapDeviceToDeclaration.end() || it->second == NULL)
 		{
-			D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1];
+			D3DVERTEXELEMENT9* d3delems = CM_NEW_ARRAY(D3DVERTEXELEMENT9, (UINT32)(mElementList.size() + 1), PoolAlloc);
 
 			VertexElementList::const_iterator i, iend;
 			unsigned int idx;
@@ -164,7 +164,7 @@ namespace CamelotEngine {
 				CM_EXCEPT(InternalErrorException, "Cannot create D3D9 vertex declaration: ");
 			}
 
-			delete [] d3delems;
+			CM_DELETE_ARRAY(d3delems, D3DVERTEXELEMENT9, (UINT32)(mElementList.size() + 1), PoolAlloc);
 
 			mMapDeviceToDeclaration[pCurDevice] = lpVertDecl;
 		}