Bläddra i källkod

More memory alloc porting

Marko Pintera 12 år sedan
förälder
incheckning
389c22b9b3

+ 2 - 2
CamelotCore/Source/CmGpuParamBlock.cpp

@@ -68,14 +68,14 @@ namespace CamelotEngine
 	void GpuParamBlock::destroy_internal()
 	{
 		if(mBuffer != nullptr)
-			delete mBuffer;
+			CM_DELETE(mBuffer, GpuParamBlockBuffer, PoolAlloc);
 
 		CoreObject::destroy_internal();
 	}
 
 	GpuParamBlockBuffer* GpuParamBlock::createBuffer() const
 	{
-		return new GpuParamBlockBuffer(mSize, mUsage);
+		return CM_NEW(GpuParamBlockBuffer, PoolAlloc) GpuParamBlockBuffer(mSize, mUsage);
 	}
 
 	void GpuParamBlock::write(UINT32 offset, const void* data, UINT32 size)

+ 3 - 1
CamelotD3D11RenderSystem/Include/CmD3D11InputLayoutManager.h

@@ -13,7 +13,7 @@ namespace CamelotEngine
 			size_t bufferDeclHash;
 			UINT32 vertexProgramId;
 
-			const list<VertexElement>::type* bufferDeclElements;
+			list<VertexElement>::type* bufferDeclElements;
 		};
 
 		class HashFunc 
@@ -30,6 +30,8 @@ namespace CamelotEngine
 
 		struct InputLayoutEntry
 		{
+			InputLayoutEntry() {}
+
 			ID3D11InputLayout* inputLayout;
 			UINT32 lastUsedIdx;
 		};

+ 2 - 1
CamelotD3D11RenderSystem/Include/CmD3D11RenderSystemFactory.h

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

+ 15 - 8
CamelotD3D11RenderSystem/Source/CmD3D11Driver.cpp

@@ -35,9 +35,14 @@ namespace CamelotEngine
 	D3D11Driver::~D3D11Driver()
 	{
 		for(UINT32 i = 0; i < mNumOutputs; i++)
-			SAFE_DELETE(mVideoModeList[i]);
+		{
+			if(mVideoModeList[i] != nullptr)
+				CM_DELETE(mVideoModeList[i], D3D11VideoModeList, GenAlloc);
+		}
+
+		if(mVideoModeList != nullptr)
+			CM_DELETE_ARRAY(mVideoModeList, D3D11VideoModeList*, mNumOutputs, GenAlloc);
 
-		SAFE_DELETE(mVideoModeList);
 		SAFE_RELEASE(mDXGIAdapter);
 	}
 
@@ -54,9 +59,9 @@ namespace CamelotEngine
 
 		mNumOutputs = outputIdx;
 
-		mVideoModeList = new D3D11VideoModeList*[mNumOutputs];
+		mVideoModeList = CM_NEW_ARRAY(D3D11VideoModeList*, mNumOutputs, GenAlloc);
 		for(UINT32 i = 0; i < mNumOutputs; i++)
-			mVideoModeList[i] = new D3D11VideoModeList(this, i);
+			mVideoModeList[i] = CM_NEW(D3D11VideoModeList, GenAlloc) D3D11VideoModeList(this, i);
 	}
 
 	D3D11Driver& D3D11Driver::operator=(const D3D11Driver& ob)
@@ -78,24 +83,26 @@ namespace CamelotEngine
 	String D3D11Driver::getDriverName() const
 	{
 		size_t size = wcslen(mAdapterIdentifier.Description);
-		char* str = new char[size + 1];
+		char* str = (char*)CM_NEW_BYTES((UINT32)(size + 1), ScratchAlloc);
 
 		wcstombs(str, mAdapterIdentifier.Description, size);
 		str[size] = '\0';
 		String Description = str;
-		delete str;
+		
+		CM_DELETE_BYTES(str, ScratchAlloc);
 		return String(Description );
 	}
 
 	String D3D11Driver::getDriverDescription() const
 	{
 		size_t size = wcslen(mAdapterIdentifier.Description);
-		char* str = new char[size + 1];
+		char* str = (char*)CM_NEW_BYTES((UINT32)(size + 1), ScratchAlloc);
 
 		wcstombs(str, mAdapterIdentifier.Description, size);
 		str[size] = '\0';
 		String driverDescription = str;
-		delete [] str;
+
+		CM_DELETE_BYTES(str, ScratchAlloc);
 		StringUtil::trim(driverDescription);
 
 		return driverDescription;

+ 2 - 2
CamelotD3D11RenderSystem/Source/CmD3D11DriverList.cpp

@@ -13,7 +13,7 @@ namespace CamelotEngine
 	{
 		for(size_t i = 0; i < mDriverList.size(); i++)
 		{
-			delete (mDriverList[i]);
+			CM_DELETE(mDriverList[i], D3D11Driver, GenAlloc);
 		}
 
 		mDriverList.clear();
@@ -33,7 +33,7 @@ namespace CamelotEngine
 				CM_EXCEPT(InternalErrorException, "Enumerating adapters failed.");
 			}
 
-			mDriverList.push_back(new D3D11Driver(adapterIdx, dxgiAdapter));
+			mDriverList.push_back(CM_NEW(D3D11Driver, GenAlloc) D3D11Driver(adapterIdx, dxgiAdapter));
 
 			SAFE_RELEASE(dxgiAdapter);
 			adapterIdx++;

+ 4 - 4
CamelotD3D11RenderSystem/Source/CmD3D11GpuBuffer.cpp

@@ -39,7 +39,7 @@ namespace CamelotEngine
 			CM_EXCEPT(InvalidParametersException, "Unsupported buffer type " + toString(mType));
 		}
 
-		mBuffer = new D3D11HardwareBuffer(bufferType, mUsage, mElementCount, mElementSize, 
+		mBuffer = CM_NEW(D3D11HardwareBuffer, PoolAlloc) D3D11HardwareBuffer(bufferType, mUsage, mElementCount, mElementSize, 
 			d3d11rs->getPrimaryDevice(), false, false, mRandomGpuWrite, mUseCounter);
 
 		GpuBuffer::initialize_internal();
@@ -47,7 +47,7 @@ namespace CamelotEngine
 
 	void D3D11GpuBuffer::destroy_internal()
 	{
-		delete mBuffer;
+		CM_DELETE(mBuffer, D3D11HardwareBuffer, PoolAlloc);
 
 		GpuBuffer::destroy_internal();
 	}
@@ -87,12 +87,12 @@ namespace CamelotEngine
 
 	GpuBufferView* D3D11GpuBuffer::createView()
 	{
-		return new D3D11GpuBufferView();
+		return CM_NEW(D3D11GpuBufferView, PoolAlloc) D3D11GpuBufferView();
 	}
 
 	void D3D11GpuBuffer::destroyView(GpuBufferView* view)
 	{
 		if(view != nullptr)
-			delete view;
+			CM_DELETE(view, GpuBufferView, PoolAlloc);
 	}
 }

+ 4 - 4
CamelotD3D11RenderSystem/Source/CmD3D11GpuParamBlock.cpp

@@ -12,9 +12,9 @@ namespace CamelotEngine
 		D3D11Device& device = d3d11rs->getPrimaryDevice();
 
 		if(mUsage == GPBU_STATIC)
-			mBuffer = new D3D11HardwareBuffer(D3D11HardwareBuffer::BT_CONSTANT, GBU_STATIC, 1, mSize, device);
+			mBuffer = CM_NEW(D3D11HardwareBuffer, PoolAlloc) D3D11HardwareBuffer(D3D11HardwareBuffer::BT_CONSTANT, GBU_STATIC, 1, mSize, device);
 		else if(mUsage == GPBU_DYNAMIC)
-			mBuffer = new D3D11HardwareBuffer(D3D11HardwareBuffer::BT_CONSTANT, GBU_DYNAMIC, 1, mSize, device);
+			mBuffer = CM_NEW(D3D11HardwareBuffer, PoolAlloc) D3D11HardwareBuffer(D3D11HardwareBuffer::BT_CONSTANT, GBU_DYNAMIC, 1, mSize, device);
 		else
 			CM_EXCEPT(InternalErrorException, "Invalid gpu param block usage.");
 	}
@@ -22,7 +22,7 @@ namespace CamelotEngine
 	D3D11GpuParamBlockBuffer::~D3D11GpuParamBlockBuffer()
 	{
 		if(mBuffer != nullptr)
-			delete mBuffer;
+			CM_DELETE(mBuffer, D3D11HardwareBuffer, PoolAlloc);
 	}
 
 	ID3D11Buffer* D3D11GpuParamBlockBuffer::getD3D11Buffer() const
@@ -39,6 +39,6 @@ namespace CamelotEngine
 
 	GpuParamBlockBuffer* D3D11GpuParamBlock::createBuffer() const
 	{
-		return new D3D11GpuParamBlockBuffer(mSize, mUsage);
+		return CM_NEW(D3D11GpuParamBlockBuffer, PoolAlloc) D3D11GpuParamBlockBuffer(mSize, mUsage);
 	}
 }

+ 2 - 1
CamelotD3D11RenderSystem/Source/CmD3D11HLSLProgram.cpp

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

+ 9 - 3
CamelotD3D11RenderSystem/Source/CmD3D11HardwareBuffer.cpp

@@ -90,7 +90,9 @@ namespace CamelotEngine
 	D3D11HardwareBuffer::~D3D11HardwareBuffer()
 	{
 		SAFE_RELEASE(mD3DBuffer);
-		SAFE_DELETE(mpTempStagingBuffer); // should never be nonzero unless destroyed while locked
+
+		if(mpTempStagingBuffer != nullptr)
+			CM_DELETE(mpTempStagingBuffer, D3D11HardwareBuffer, PoolAlloc);
 	}
 
 	void* D3D11HardwareBuffer::lockImpl(UINT32 offset, 
@@ -186,7 +188,7 @@ namespace CamelotEngine
 			if (!mpTempStagingBuffer)
 			{
 				// create another buffer instance but use system memory
-				mpTempStagingBuffer = new D3D11HardwareBuffer(mBufferType, mUsage, 1, mSizeInBytes, mDevice, true);
+				mpTempStagingBuffer = CM_NEW(D3D11HardwareBuffer, PoolAlloc) D3D11HardwareBuffer(mBufferType, mUsage, 1, mSizeInBytes, mDevice, true);
 			}
 
 			// schedule a copy to the staging
@@ -216,7 +218,11 @@ namespace CamelotEngine
 
 			// delete
 			// not that efficient, but we should not be locking often
-			SAFE_DELETE(mpTempStagingBuffer);
+			if(mpTempStagingBuffer != nullptr)
+			{
+				CM_DELETE(mpTempStagingBuffer, D3D11HardwareBuffer, PoolAlloc);
+				mpTempStagingBuffer = nullptr;
+			}
 		}
 		else
 		{

+ 4 - 4
CamelotD3D11RenderSystem/Source/CmD3D11IndexBuffer.cpp

@@ -4,9 +4,9 @@ namespace CamelotEngine
 {
 	D3D11IndexBuffer::D3D11IndexBuffer(D3D11Device& device, HardwareBufferManager* mgr, IndexType idxType, UINT32 numIndexes, 
 		GpuBufferUsage usage, bool useSystemMem)
-		:IndexBuffer(mgr, idxType, numIndexes, usage, useSystemMem), mDevice(device)
+		:IndexBuffer(mgr, idxType, numIndexes, usage, useSystemMem), mDevice(device), mBuffer(nullptr)
 	{
-		mBuffer = new D3D11HardwareBuffer(D3D11HardwareBuffer::BT_INDEX, usage, 1, mSizeInBytes, device, useSystemMem);
+
 	}
 
 	D3D11IndexBuffer::~D3D11IndexBuffer()
@@ -16,7 +16,7 @@ namespace CamelotEngine
 
 	void D3D11IndexBuffer::initialize_internal()
 	{
-		mBuffer = new D3D11HardwareBuffer(D3D11HardwareBuffer::BT_INDEX, mUsage, 1, mSizeInBytes, mDevice, mSystemMemory);
+		mBuffer = CM_NEW(D3D11HardwareBuffer, PoolAlloc) D3D11HardwareBuffer(D3D11HardwareBuffer::BT_INDEX, mUsage, 1, mSizeInBytes, mDevice, mSystemMemory);
 
 		IndexBuffer::initialize_internal();
 	}
@@ -24,7 +24,7 @@ namespace CamelotEngine
 	void D3D11IndexBuffer::destroy_internal()
 	{
 		if(mBuffer != nullptr)
-			delete mBuffer;
+			CM_DELETE(mBuffer, D3D11HardwareBuffer, PoolAlloc) ;
 
 		IndexBuffer::destroy_internal();
 	}

+ 8 - 6
CamelotD3D11RenderSystem/Source/CmD3D11InputLayoutManager.cpp

@@ -55,9 +55,9 @@ namespace CamelotEngine
 		{
 			auto firstElem = mInputLayoutMap.begin();
 
-			delete firstElem->first.bufferDeclElements;
+			CM_DELETE(firstElem->first.bufferDeclElements, list<VertexElement>::type, PoolAlloc);
 			SAFE_RELEASE(firstElem->second->inputLayout);
-			delete firstElem->second;
+			CM_DELETE(firstElem->second, InputLayoutEntry, PoolAlloc);
 
 			mInputLayoutMap.erase(firstElem);
 		}
@@ -67,7 +67,7 @@ namespace CamelotEngine
 	{
 		VertexDeclarationKey pair;
 		pair.bufferDeclHash = vertexBufferDecl->getHash();
-		pair.bufferDeclElements = &vertexBufferDecl->getElements();
+		pair.bufferDeclElements = const_cast<list<VertexElement>::type*>(&vertexBufferDecl->getElements());
 		pair.vertexProgramId = vertexProgram.getProgramId();
 
 		auto iterFind = mInputLayoutMap.find(pair);
@@ -94,7 +94,7 @@ namespace CamelotEngine
 			return; // Error was already reported, so just quit here
 
 		UINT32 numElements = vertexBufferDecl->getElementCount();
-		D3D11_INPUT_ELEMENT_DESC* declElements = new D3D11_INPUT_ELEMENT_DESC[numElements];
+		D3D11_INPUT_ELEMENT_DESC* declElements = CM_NEW_ARRAY(D3D11_INPUT_ELEMENT_DESC, numElements, ScratchAlloc);
 		ZeroMemory(declElements, sizeof(D3D11_INPUT_ELEMENT_DESC) * numElements);
 
 		unsigned int idx = 0;
@@ -116,7 +116,7 @@ namespace CamelotEngine
 
 		const HLSLMicroCode& microcode = vertexProgram.getMicroCode();
 
-		InputLayoutEntry* newEntry = new InputLayoutEntry();
+		InputLayoutEntry* newEntry = CM_NEW(InputLayoutEntry, PoolAlloc) InputLayoutEntry();
 		newEntry->lastUsedIdx = ++mLastUsedCounter;
 		newEntry->inputLayout = nullptr; 
 		HRESULT hr = device.getD3D11Device()->CreateInputLayout( 
@@ -126,6 +126,8 @@ namespace CamelotEngine
 			microcode.size(),
 			&newEntry->inputLayout);
 
+		//CM_DELETE_ARRAY(declElements, D3D11_INPUT_ELEMENT_DESC, numElements, ScratchAlloc);
+
 		if (FAILED(hr)|| device.hasError())
 			CM_EXCEPT(RenderingAPIException, "Unable to set D3D11 vertex declaration" + device.getErrorDescription());
 
@@ -133,7 +135,7 @@ namespace CamelotEngine
 		VertexDeclarationKey pair;
 		pair.bufferDeclHash = vertexBufferDecl->getHash();
 
-		list<VertexElement>::type* bufferDeclElements = new list<VertexElement>::type(); 
+		list<VertexElement>::type* bufferDeclElements = CM_NEW(list<VertexElement>::type, PoolAlloc) list<VertexElement>::type(); 
 		pair.bufferDeclElements = bufferDeclElements;
 		pair.vertexProgramId = vertexProgram.getProgramId();
 

+ 3 - 2
CamelotD3D11RenderSystem/Source/CmD3D11Texture.cpp

@@ -515,7 +515,7 @@ namespace CamelotEngine
 		UINT32 sizeOfImage = lock.getConsecutiveSize();
 		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
 
-		mStaticBuffer = new PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
+		mStaticBuffer = CM_NEW(PixelData, PoolAlloc) PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
 
 		return mStaticBuffer->allocData(sizeOfImage);
 	}
@@ -535,7 +535,8 @@ namespace CamelotEngine
 			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
 		}
 
-		SAFE_DELETE(mStaticBuffer);
+		if(mStaticBuffer != nullptr)
+			CM_DELETE(mStaticBuffer, PixelData, PoolAlloc);
 	}
 
 	void D3D11Texture::_createStagingBuffer()

+ 1 - 1
CamelotFBXImporter/Include/CmFBXImporter.h

@@ -23,7 +23,7 @@ namespace CamelotEngine
 			static FBXImporter* importer = nullptr;
 			if(importer == nullptr)
 			{
-				importer = new FBXImporter();
+				importer = CM_NEW(FBXImporter, GenAlloc) FBXImporter();
 				Importer::instance().registerAssetImporter(importer);
 			}
 		}

+ 1 - 1
CamelotFBXImporter/Source/CmFBXImporter.cpp

@@ -175,7 +175,7 @@ namespace CamelotEngine
 
 	MeshDataPtr FBXImporter::parseMesh(FbxMesh* mesh, bool createTangentsIfMissing)
 	{
-		MeshDataPtr meshData = MeshDataPtr(new MeshData());
+		MeshDataPtr meshData = MeshDataPtr(CM_NEW(MeshData, ScratchAlloc) MeshData(), &MemAllocDeleter<MeshData, ScratchAlloc>::deleter);
 
 		if (!mesh->GetNode())
 			return meshData;

+ 1 - 1
CamelotFreeImgImporter/Include/CmFreeImgImporter.h

@@ -22,7 +22,7 @@ namespace CamelotEngine
 			static FreeImgImporter* importer = nullptr;
 			if(importer == nullptr)
 			{
-				importer = new FreeImgImporter();
+				importer = CM_NEW(FreeImgImporter, GenAlloc) FreeImgImporter();
 				Importer::instance().registerAssetImporter(importer);
 			}
 		}

+ 1 - 1
CamelotFreeImgImporter/Include/CmTextureData.h

@@ -16,7 +16,7 @@ namespace CamelotEngine
 	{
 	public:
 		TextureData(UINT32 width, UINT32 height, UINT32 size, 
-			PixelFormat format, UINT8* data, UINT32 depth = 1, INT32 flags = 0, UINT32 numMipmaps = 1);
+			PixelFormat format, UINT32 depth = 1, INT32 flags = 0, UINT32 numMipmaps = 1);
 		~TextureData();
 
 		/** Returns a pointer to the internal image buffer.

+ 4 - 2
CamelotFreeImgImporter/Source/CmFreeImgImporter.cpp

@@ -308,7 +308,9 @@ namespace CamelotEngine
 		UINT32 size = dstPitch * height;
 
 		// Bind output buffer
-		UINT8* output = new UINT8[size]; // TextureData frees this when its released
+		TextureDataPtr texData(CM_NEW(TextureData, ScratchAlloc) TextureData(width, height, size, format, 1, 0, 0),
+			&MemAllocDeleter<TextureData, ScratchAlloc>::deleter);
+		UINT8* output = texData->getData();
 
 		UINT8* pSrc;
 		UINT8* pDst = output;
@@ -322,7 +324,7 @@ namespace CamelotEngine
 		FreeImage_Unload(fiBitmap);
 		FreeImage_CloseMemory(fiMem);
 
-		TextureDataPtr texData(new TextureData(width, height, size, format, output, 1, 0, 0));
+		
 		return texData;
 	}
 }

+ 4 - 3
CamelotFreeImgImporter/Source/CmTextureData.cpp

@@ -4,17 +4,18 @@
 namespace CamelotEngine
 {
 	TextureData::TextureData(UINT32 width, UINT32 height, UINT32 size, 
-		PixelFormat format, UINT8* data, UINT32 depth, INT32 flags, UINT32 numMipmaps)
+		PixelFormat format, UINT32 depth, INT32 flags, UINT32 numMipmaps)
 		:mWidth(width), mHeight(height), mDepth(depth), mSize(size), mFormat(format),
-		mFlags(flags), mNumMipmaps(numMipmaps), mData(data)
+		mFlags(flags), mNumMipmaps(numMipmaps)
 	{
 		mBPP = static_cast<UINT8>(PixelUtil::getNumElemBytes(mFormat)) * 8;
+		mData = CM_NEW_BYTES(size, ScratchAlloc);
 	}
 
 	TextureData::~TextureData()
 	{
 		if(mData != nullptr)
-			delete[] mData;
+			CM_DELETE_BYTES(mData, ScratchAlloc);
 	}
 
 	PixelData TextureData::getPixels(UINT32 mip)

+ 1 - 1
CamelotGLRenderer/Source/CmGLGpuParamBlock.cpp

@@ -34,6 +34,6 @@ namespace CamelotEngine
 
 	GpuParamBlockBuffer* GLGpuParamBlock::createBuffer() const
 	{
-		return new GLGpuParamBlockBuffer(mSize, mUsage);
+		return CM_NEW(GLGpuParamBlockBuffer, PoolAlloc) GLGpuParamBlockBuffer(mSize, mUsage);
 	}
 }