Selaa lähdekoodia

Moved PixelData to use allocators

Marko Pintera 12 vuotta sitten
vanhempi
sitoutus
e4b2dbb7c7

+ 2 - 2
CamelotCore/Source/CmTexture.cpp

@@ -138,8 +138,8 @@ namespace CamelotEngine {
 			if(depth != 1) depth /= 2;
 			if(depth != 1) depth /= 2;
 		}
 		}
 
 
-		UINT8* buffer = new UINT8[totalSize]; 
-		PixelDataPtr dst(new PixelData(width, height, depth, getFormat(), buffer, true));
+		PixelDataPtr dst(new PixelData(width, height, depth, getFormat()));
+		UINT8* buffer = (UINT8*)dst->allocData(totalSize);
 
 
 		PixelData myData = lock(GBL_READ_ONLY, mip, face);
 		PixelData myData = lock(GBL_READ_ONLY, mip, face);
 
 

+ 2 - 3
CamelotD3D11RenderSystem/Source/CmD3D11Texture.cpp

@@ -515,10 +515,9 @@ namespace CamelotEngine
 		UINT32 sizeOfImage = lock.getConsecutiveSize();
 		UINT32 sizeOfImage = lock.getConsecutiveSize();
 		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
 		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
 
 
-		UINT8* bufferData = new UINT8[sizeOfImage];
-		mStaticBuffer = new PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat(), bufferData, true);
+		mStaticBuffer = new PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
 
 
-		return bufferData;
+		return mStaticBuffer->allocData(sizeOfImage);
 	}
 	}
 
 
 	void D3D11Texture::_unmapstaticbuffer()
 	void D3D11Texture::_unmapstaticbuffer()

+ 2 - 4
CamelotD3D9Renderer/Source/CmD3D9PixelBuffer.cpp

@@ -106,9 +106,8 @@ namespace CamelotEngine
 					dev->TestCooperativeLevel() == D3D_OK)
 					dev->TestCooperativeLevel() == D3D_OK)
 				{
 				{
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
-					PixelData dstBox(fullBufferBox, mFormat);
+					PixelData dstBox(fullBufferBox, mFormat, new char[getSizeInBytes()]);
 
 
-					dstBox.data = new char[getSizeInBytes()];
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
 					SAFE_DELETE_ARRAY(dstBox.data);
 					SAFE_DELETE_ARRAY(dstBox.data);
@@ -163,9 +162,8 @@ namespace CamelotEngine
 					dev->TestCooperativeLevel() == D3D_OK)
 					dev->TestCooperativeLevel() == D3D_OK)
 				{
 				{
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
 					Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
-					PixelData dstBox(fullBufferBox, mFormat);
+					PixelData dstBox(fullBufferBox, mFormat, new char[getSizeInBytes()]);
 
 
-					dstBox.data = new char[getSizeInBytes()];
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitToMemory(fullBufferBox, dstBox, it->second, it->first);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
 					blitFromMemory(dstBox, fullBufferBox, bufferResources);
 					SAFE_DELETE(dstBox.data);
 					SAFE_DELETE(dstBox.data);

+ 3 - 4
CamelotFontImporter/Source/CmFontImporter.cpp

@@ -117,7 +117,6 @@ namespace CamelotEngine
 
 
 				atlasElements.push_back(atlasElement);
 				atlasElements.push_back(atlasElement);
 			}
 			}
-			
 
 
 			// Create an optimal layout for character bitmaps
 			// Create an optimal layout for character bitmaps
 			TexAtlasGenerator texAtlasGen(false, MAXIMUM_TEXTURE_SIZE, MAXIMUM_TEXTURE_SIZE);
 			TexAtlasGenerator texAtlasGen(false, MAXIMUM_TEXTURE_SIZE, MAXIMUM_TEXTURE_SIZE);
@@ -132,10 +131,10 @@ namespace CamelotEngine
 			{
 			{
 				UINT32 bufferSize = pageIter->width * pageIter->height * 2;
 				UINT32 bufferSize = pageIter->width * pageIter->height * 2;
 
 
-				UINT8* pixelBuffer = new UINT8[bufferSize];
-				memset(pixelBuffer, 0, bufferSize);
+				PixelData pixelData(pageIter->width, pageIter->height, 1, PF_R8G8);
 
 
-				PixelData pixelData(pageIter->width, pageIter->height, 1, PF_R8G8, pixelBuffer, true);
+				UINT8* pixelBuffer = pixelData.allocData(bufferSize);
+				memset(pixelBuffer, 0, bufferSize);
 
 
 				for(size_t elementIdx = 0; elementIdx < atlasElements.size(); elementIdx++)
 				for(size_t elementIdx = 0; elementIdx < atlasElements.size(); elementIdx++)
 				{
 				{

+ 2 - 0
CamelotUtility/Include/CmMemoryAllocator.h

@@ -86,8 +86,10 @@ namespace CamelotEngine
 }
 }
 
 
 #define CM_NEW(T, category) new (MemoryAllocator<category>::allocate(sizeof(T)))
 #define CM_NEW(T, category) new (MemoryAllocator<category>::allocate(sizeof(T)))
+#define CM_NEW_BYTES(count, category) (UINT8*)MemoryAllocator<category>::allocate(count)
 #define CM_NEW_ARRAY(T, count, category) __cm_construct_array<T, category>(count)
 #define CM_NEW_ARRAY(T, count, category) __cm_construct_array<T, category>(count)
 #define CM_DELETE(ptr, T, category) __cm_destruct<T, category>(ptr)
 #define CM_DELETE(ptr, T, category) __cm_destruct<T, category>(ptr)
+#define CM_DELETE_BYTES(ptr, category) MemoryAllocator<category>::free(ptr)
 #define CM_DELETE_ARRAY(ptr, T, count, category) __cm_destruct_array<T, category>(ptr, count)
 #define CM_DELETE_ARRAY(ptr, T, count, category) __cm_destruct_array<T, category>(ptr, count)
 
 
 
 

+ 11 - 8
CamelotUtility/Include/CmPixelData.h

@@ -174,7 +174,7 @@ namespace CamelotEngine
 		~PixelData() 
 		~PixelData() 
 		{
 		{
 			if(ownsData && data != nullptr)
 			if(ownsData && data != nullptr)
-				delete[] data;
+				CM_DELETE_BYTES(data, ScratchAlloc);
 
 
 			data = nullptr;
 			data = nullptr;
 		}
 		}
@@ -184,13 +184,13 @@ namespace CamelotEngine
     		@param extents	    Extents of the region defined by data
     		@param extents	    Extents of the region defined by data
     		@param pixelFormat	Format of this buffer
     		@param pixelFormat	Format of this buffer
     		@param pixelData	Pointer to the actual data
     		@param pixelData	Pointer to the actual data
-			@param ownsPixelData  If true then PixelData owns the data buffer and will release it when destroyed.
     	*/
     	*/
-		PixelData(const Box &extents, PixelFormat pixelFormat, void *pixelData = 0, bool ownsPixelData = false):
-			Box(extents), data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
+		PixelData(const Box &extents, PixelFormat pixelFormat, void *pixelData = nullptr):
+			Box(extents), data(pixelData), format(pixelFormat), ownsData(false)
 		{
 		{
 			setConsecutive();
 			setConsecutive();
 		}
 		}
+
     	/** Constructor providing width, height and depth. This constructor
     	/** Constructor providing width, height and depth. This constructor
     		assumes the pixel data is laid out consecutively in memory. (this
     		assumes the pixel data is laid out consecutively in memory. (this
     		means row after row, slice after slice, with no space in between)
     		means row after row, slice after slice, with no space in between)
@@ -199,18 +199,21 @@ namespace CamelotEngine
     		@param depth	    Depth of the region
     		@param depth	    Depth of the region
     		@param pixelFormat	Format of this buffer
     		@param pixelFormat	Format of this buffer
     		@param pixelData    Pointer to the actual data
     		@param pixelData    Pointer to the actual data
-			@param ownsPixelData  If true then PixelData owns the data buffer and will release it when destroyed.
     	*/
     	*/
-    	PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat, 
-				void *pixelData = 0, bool ownsPixelData = false):
+    	PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat, void *pixelData = nullptr):
     		Box(0, 0, 0, width, height, depth),
     		Box(0, 0, 0, width, height, depth),
-    		data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
+    		data(pixelData), format(pixelFormat), ownsData(false)
     	{
     	{
     		setConsecutive();
     		setConsecutive();
     	}
     	}
 
 
 		PixelData(const PixelData& copy);
 		PixelData(const PixelData& copy);
     	
     	
+		/**
+		 * @brief	Allocates an internal buffer for storing data.
+		 */
+		UINT8* allocData(UINT32 size);
+
         /// The data pointer 
         /// The data pointer 
         void *data;
         void *data;
         /// The pixel format 
         /// The pixel format 

+ 6 - 1
CamelotUtility/Include/CmPixelDataRTTI.h

@@ -47,6 +47,11 @@ namespace CamelotEngine
 			obj->data = val.getData();
 			obj->data = val.getData();
 		}
 		}
 
 
+		static UINT8* allocateData(UINT32 numBytes)
+		{
+			return CM_NEW_BYTES(numBytes, ScratchAlloc);
+		}
+
 	public:
 	public:
 		PixelDataRTTI()
 		PixelDataRTTI()
 		{
 		{
@@ -60,7 +65,7 @@ namespace CamelotEngine
 			addPlainField("slicePitch", 7, &PixelDataRTTI::getSlicePitch, &PixelDataRTTI::setSlicePitch);
 			addPlainField("slicePitch", 7, &PixelDataRTTI::getSlicePitch, &PixelDataRTTI::setSlicePitch);
 			addPlainField("format", 8, &PixelDataRTTI::getFormat, &PixelDataRTTI::setFormat);
 			addPlainField("format", 8, &PixelDataRTTI::getFormat, &PixelDataRTTI::setFormat);
 
 
-			addDataBlockField("data", 9, &PixelDataRTTI::getData, &PixelDataRTTI::setData);
+			addDataBlockField("data", 9, &PixelDataRTTI::getData, &PixelDataRTTI::setData, 0, &PixelDataRTTI::allocateData);
 		}
 		}
 
 
 		virtual const String& getRTTIName()
 		virtual const String& getRTTIName()

+ 4 - 4
CamelotUtility/Include/CmRTTIType.h

@@ -458,11 +458,11 @@ namespace CamelotEngine
 
 
 		template<class ObjectType>
 		template<class ObjectType>
 		void addDataBlockField(const std::string& name, UINT32 uniqueId, ManagedDataBlock (ObjectType::*getter)(), 
 		void addDataBlockField(const std::string& name, UINT32 uniqueId, ManagedDataBlock (ObjectType::*getter)(), 
-			void (ObjectType::*setter)(ManagedDataBlock) = nullptr, UINT64 flags = 0)
+			void (ObjectType::*setter)(ManagedDataBlock) = nullptr, UINT64 flags = 0, boost::function<UINT8*(UINT32)> customAllocator = 0)
 		{
 		{
 			addDataBlockField<ObjectType>(name, uniqueId, 
 			addDataBlockField<ObjectType>(name, uniqueId, 
 				boost::function<ManagedDataBlock(ObjectType*)>(getter),  
 				boost::function<ManagedDataBlock(ObjectType*)>(getter),  
-				boost::function<void(ObjectType*, ManagedDataBlock)>(setter), flags);
+				boost::function<void(ObjectType*, ManagedDataBlock)>(setter), flags, customAllocator);
 		}	
 		}	
 
 
 	protected:
 	protected:
@@ -558,11 +558,11 @@ namespace CamelotEngine
 
 
 		template<class InterfaceType, class ObjectType>
 		template<class InterfaceType, class ObjectType>
 		void addDataBlockField(const std::string& name, UINT32 uniqueId, ManagedDataBlock (InterfaceType::*getter)(ObjectType*), 
 		void addDataBlockField(const std::string& name, UINT32 uniqueId, ManagedDataBlock (InterfaceType::*getter)(ObjectType*), 
-			void (InterfaceType::*setter)(ObjectType*, ManagedDataBlock), UINT64 flags = 0)
+			void (InterfaceType::*setter)(ObjectType*, ManagedDataBlock), UINT64 flags = 0, boost::function<UINT8*(UINT32)> customAllocator = 0)
 		{
 		{
 			addDataBlockField<ObjectType>(name, uniqueId, 
 			addDataBlockField<ObjectType>(name, uniqueId, 
 				boost::function<ManagedDataBlock(ObjectType*)>(boost::bind(getter, static_cast<InterfaceType*>(this), _1)),  
 				boost::function<ManagedDataBlock(ObjectType*)>(boost::bind(getter, static_cast<InterfaceType*>(this), _1)),  
-				boost::function<void(ObjectType*, ManagedDataBlock)>(boost::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags);
+				boost::function<void(ObjectType*, ManagedDataBlock)>(boost::bind(setter, static_cast<InterfaceType*>(this), _1, _2)), flags, customAllocator);
 		}	
 		}	
 
 
 	private:
 	private:

+ 9 - 0
CamelotUtility/Source/CmPixelData.cpp

@@ -1,4 +1,5 @@
 #include "CmPixelData.h"
 #include "CmPixelData.h"
+#include "CmPixelUtil.h"
 #include "CmPixelDataRTTI.h"
 #include "CmPixelDataRTTI.h"
 
 
 namespace CamelotEngine
 namespace CamelotEngine
@@ -13,6 +14,14 @@ namespace CamelotEngine
 		ownsData = false;
 		ownsData = false;
 	}
 	}
 
 
+	UINT8* PixelData::allocData(UINT32 size)
+	{
+		data = CM_NEW_BYTES(size, ScratchAlloc);
+		ownsData = true;
+
+		return (UINT8*)data;
+	}
+
 	/************************************************************************/
 	/************************************************************************/
 	/* 								SERIALIZATION                      		*/
 	/* 								SERIALIZATION                      		*/
 	/************************************************************************/
 	/************************************************************************/