Просмотр исходного кода

Before moving serialization to shared ptrs

Marko Pintera 13 лет назад
Родитель
Сommit
a38fa5a9e3

+ 8 - 0
CamelotRenderer/Include/CmTexture.h

@@ -333,6 +333,14 @@ namespace CamelotEngine {
 		*/
 		virtual void getCustomAttribute(const String& name, void* pData) {}
 
+		/**
+		 * @brief	Retrieves the texture data from the GPU, loads it into system memory
+		 * 			and returns it in the form of TextureData for each face.
+		 *
+		 * @return	The texture data.
+		 */
+		vector<TextureDataPtr>::type getTextureData();
+
     protected:
         size_t mHeight;
         size_t mWidth;

+ 40 - 0
CamelotRenderer/Include/CmTextureST.h

@@ -3,6 +3,8 @@
 #include "CmPrerequisites.h"
 #include "CmSerializableType.h"
 #include "CmTexture.h"
+#include "CmTextureData.h"
+#include "CmManagedDataBlock.h"
 
 namespace CamelotEngine
 {
@@ -21,6 +23,38 @@ namespace CamelotEngine
 		CM_SETGET_MEMBER(mHwGamma, bool, Texture)
 		CM_SETGET_MEMBER(mFSAA, UINT32, Texture)
 		CM_SETGET_MEMBER(mFSAAHint, String, Texture)
+		CM_SETGET_MEMBER(mTextureType, TextureType, Texture)
+		CM_SETGET_MEMBER(mFormat, PixelFormat, Texture)
+		CM_SETGET_MEMBER(mUsage, INT32, Texture)
+		CM_SETGET_MEMBER(mDesiredFormat, PixelFormat, Texture)
+		CM_SETGET_MEMBER(mDesiredIntegerBitDepth, UINT16, Texture)
+		CM_SETGET_MEMBER(mDesiredFloatBitDepth, UINT16, Texture)
+
+		//ManagedDataBlock getFaceData(Texture* obj)
+		//{
+		//	UINT32 totalSize = 0;
+		//	vector<TextureDataPtr>::type faces = obj->getTextureData();
+
+		//	for(auto iter = faces.begin(); iter != faces.end(); ++iter)
+		//	{
+		//		totalSize += (*iter)->getSize();
+		//	}
+
+		//	UINT8* faceData = new UINT8[totalSize]; // TODO - Any way to avoid this reallocation? getTextureData allocates the same amount of memory and then
+		//											// we allocate it again, copy from buffers and throw away getTextureData memory
+		//											//   - TextureData should store everything in one big buffer?
+		//											//   - OR make TextureData ISerializable?!
+
+		//	//ManagedDataBlock dataBlock()
+
+		//	return obj->getTextureData(face).get();
+		//}
+
+		//void setFaceData(Texture* obj, UINT32 face, TextureData* data)
+		//{
+		//	return obj->getTextureData(face).get();
+		//}
+
 	public:
 		TextureST()
 		{
@@ -36,6 +70,12 @@ namespace CamelotEngine
 			CM_ADD_PLAINFIELD(mHwGamma, 9, TextureST)
 			CM_ADD_PLAINFIELD(mFSAA, 10, TextureST)
 			CM_ADD_PLAINFIELD(mFSAAHint, 11, TextureST)
+			CM_ADD_PLAINFIELD(mTextureType, 12, TextureST)
+			CM_ADD_PLAINFIELD(mFormat, 13, TextureST)
+			CM_ADD_PLAINFIELD(mUsage, 14, TextureST)
+			CM_ADD_PLAINFIELD(mDesiredFormat, 15, TextureST)
+			CM_ADD_PLAINFIELD(mDesiredIntegerBitDepth, 16, TextureST)
+			CM_ADD_PLAINFIELD(mDesiredFloatBitDepth, 17, TextureST)
 		}
 	};
 }

+ 41 - 0
CamelotRenderer/Source/CmTexture.cpp

@@ -127,6 +127,47 @@ namespace CamelotEngine {
 		}
 	}
 	//-----------------------------------------------------------------------------
+	vector<TextureDataPtr>::type Texture::getTextureData()
+	{
+		vector<TextureDataPtr>::type output;
+
+		UINT32 numFaces = getNumFaces();
+		UINT32 numMips = getNumMipmaps();
+
+		for(int i = 0; i < numFaces; i++)
+		{
+			UINT32 totalSize = 0;
+			UINT32 width = getWidth();
+			UINT32 height = getHeight();
+			UINT32 depth = getDepth();
+
+			for(int j = 0; j < numMips; j++)
+			{
+				UINT32 currentMipSize = PixelUtil::getMemorySize(
+						width, height, depth, mFormat);
+
+				totalSize += currentMipSize;
+
+				if(width != 1) width /= 2;
+				if(height != 1) height /= 2;
+				if(depth != 1) depth /= 2;
+			}
+
+			UINT8* buffer = new UINT8[totalSize]; // TextureData frees this
+			TextureDataPtr texData(new TextureData(getWidth(), getHeight(), totalSize, mFormat, buffer, getDepth(), 0, getNumMipmaps()));
+
+			for(int j = 0; j < numMips; j++)
+			{
+				PixelData pixels = texData->getPixels(j);
+				getBuffer(i, j)->blitToMemory(pixels);
+			}
+
+			output.push_back(texData);
+		}
+
+		return output;
+	}
+	//-----------------------------------------------------------------------------
 	void Texture::loadFromTextureData(const vector<TextureDataPtr>::type& textureData)
 	{
 		if(textureData.size() < 1)

+ 1 - 1
CamelotUtility/CamelotUtility.vcxproj

@@ -98,9 +98,9 @@
     <ClInclude Include="Include\CmRTTIReflectableField.h" />
     <ClInclude Include="Include\CmRTTIReflectablePtrField.h" />
     <ClInclude Include="Include\CmRTTIType.h" />
-    <ClInclude Include="Include\CmRTTITypes.h" />
     <ClInclude Include="Include\CmSerializableType.h" />
     <ClInclude Include="Include\CmString.h" />
+    <ClInclude Include="Include\CmTextureDataST.h" />
     <ClInclude Include="Include\CmThreadDefines.h" />
     <ClInclude Include="Include\CmTypes.h" />
     <ClInclude Include="Include\CmFwdDeclUtil.h" />

+ 5 - 2
CamelotUtility/CamelotUtility.vcxproj.filters

@@ -37,6 +37,9 @@
     <Filter Include="Source Files\Serialization">
       <UniqueIdentifier>{155906c8-9f79-4f86-835a-eb8f5c2f66bf}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Header Files\SerializationTypes">
+      <UniqueIdentifier>{e58e84a8-5f39-4bbd-874e-cfa000ebd881}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Include\CmTypes.h">
@@ -174,8 +177,8 @@
     <ClInclude Include="Include\CmSerializableType.h">
       <Filter>Header Files\Serialization</Filter>
     </ClInclude>
-    <ClInclude Include="Include\CmRTTITypes.h">
-      <Filter>Header Files\Prerequisites</Filter>
+    <ClInclude Include="Include\CmTextureDataST.h">
+      <Filter>Header Files\SerializationTypes</Filter>
     </ClInclude>
   </ItemGroup>
   <ItemGroup>

+ 0 - 3
CamelotUtility/Include/CmPrerequisitesUtil.h

@@ -56,9 +56,6 @@ THE SOFTWARE
 // Short-hand names for various built-in types
 #include "CmTypes.h"
 
-// RTTI supported types and a macro for defining new ones
-#include "CmRTTITypes.h"
-
 // Useful threading defines
 #include "CmThreadDefines.h"
 

+ 48 - 11
CamelotUtility/Include/CmRTTIField.h

@@ -15,17 +15,54 @@ namespace CamelotEngine
 	class RTTITypeBase;
 	struct RTTIField;
 
-	CM_SERIALIZABLE_SIMPLE_TYPE(UINT8, 0);
-	CM_SERIALIZABLE_SIMPLE_TYPE(UINT16, 1);
-	CM_SERIALIZABLE_SIMPLE_TYPE(UINT32, 2);
-	CM_SERIALIZABLE_SIMPLE_TYPE(UINT64, 3);
-	CM_SERIALIZABLE_SIMPLE_TYPE(INT8, 4);
-	CM_SERIALIZABLE_SIMPLE_TYPE(INT16, 5);
-	CM_SERIALIZABLE_SIMPLE_TYPE(INT32, 6);
-	CM_SERIALIZABLE_SIMPLE_TYPE(INT64, 7);
-	CM_SERIALIZABLE_SIMPLE_TYPE(float, 8);
-	CM_SERIALIZABLE_SIMPLE_TYPE(double, 9);
-	CM_SERIALIZABLE_SIMPLE_TYPE(bool, 10);
+	template<class T>
+	struct SerializableSimpleType 
+	{ 
+		//BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, // We need this hacky check, otherwise if we only use "false" compiler will evaluate the template and trigger this message 
+		//	"Provided data type isn't marked as serializable. Declare SerializableSimpleType template specialization to define a unique ID and needed methods for the type.");
+
+		enum { id = 0 }; 
+		enum { hasDynamicSize = 0 };
+
+		static void toMemory(T& data, char* memory)	
+		{ 
+			memcpy(memory, &data, sizeof(T)); 
+		}
+
+		static void fromMemory(T& data, char* memory)
+		{
+			memcpy(&data, memory, sizeof(T)); 
+		}
+
+		static UINT32 getDynamicSize(T& data)
+		{ 
+			assert(false); 
+			return sizeof(T);
+		}
+	};
+
+#define CM_SERIALIZABLE_SIMPLE_TYPE(type, type_id)				\
+	template<> struct SerializableSimpleType<##type##>			\
+	{	enum { id=##type_id }; enum { hasDynamicSize = 0 };		\
+	static void toMemory(##type##& data, char* memory)		\
+	{ memcpy(memory, &data, sizeof(##type##)); }			\
+	static void fromMemory(##type##& data, char* memory)	\
+	{ memcpy(&data, memory, sizeof(##type##)); }			\
+	static UINT32 getDynamicSize(##type##& data)			\
+	{ assert(false); return sizeof(##type##); }				\
+	}; 
+
+	CM_SERIALIZABLE_SIMPLE_TYPE(UINT8, 1);
+	CM_SERIALIZABLE_SIMPLE_TYPE(UINT16, 2);
+	CM_SERIALIZABLE_SIMPLE_TYPE(UINT32, 3);
+	CM_SERIALIZABLE_SIMPLE_TYPE(UINT64, 4);
+	CM_SERIALIZABLE_SIMPLE_TYPE(INT8, 5);
+	CM_SERIALIZABLE_SIMPLE_TYPE(INT16, 6);
+	CM_SERIALIZABLE_SIMPLE_TYPE(INT32, 7);
+	CM_SERIALIZABLE_SIMPLE_TYPE(INT64, 8);
+	CM_SERIALIZABLE_SIMPLE_TYPE(float, 9);
+	CM_SERIALIZABLE_SIMPLE_TYPE(double, 10);
+	CM_SERIALIZABLE_SIMPLE_TYPE(bool, 11);
 		
 	/**
 	 * @brief	Strings need to copy their data in a slightly more intricate way than just memcpy.

+ 0 - 31
CamelotUtility/Include/CmRTTITypes.h

@@ -1,31 +0,0 @@
-#pragma once
-
-#include "boost/static_assert.hpp"
-
-namespace CamelotEngine
-{
-	template<class T>
-	struct SerializableSimpleType 
-	{ 
-		BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, // We need this hacky check, otherwise if we only use "false" compiler will evaluate the template and trigger this message 
-			"Provided data type doesn't isn't marked as serializable. Declare SerializableSimpleType template specialization to define a unique ID and needed methods for the type.");
-
-		enum { id = T::TYPE_ID }; 
-		enum { hasDynamicSize = 0 };
-
-		void toMemory(T& data, char* memory) {}
-		void fromMemory(T& data, char* memory) {}
-		UINT32 getDynamicSize(T& data) {}
-	};
-
-#define CM_SERIALIZABLE_SIMPLE_TYPE(type, type_id)				\
-	template<> struct SerializableSimpleType<##type##>			\
-	{	enum { id=##type_id }; enum { hasDynamicSize = 0 };		\
-		static void toMemory(##type##& data, char* memory)		\
-		 { memcpy(memory, &data, sizeof(##type##)); }			\
-		static void fromMemory(##type##& data, char* memory)	\
-		 { memcpy(&data, memory, sizeof(##type##)); }			\
-		static UINT32 getDynamicSize(##type##& data)			\
-		{ assert(false); return sizeof(##type##); }				\
-	}; 
-}

+ 12 - 1
CamelotUtility/Include/CmTextureData.h

@@ -2,6 +2,7 @@
 
 #include "CmPrerequisitesUtil.h"
 #include "CmPixelUtil.h"
+#include "CmISerializable.h"
 
 namespace CamelotEngine
 {
@@ -12,7 +13,7 @@ namespace CamelotEngine
 		TDF_3D_TEXTURE = 0x00000004
 	};
 
-	class CM_UTILITY_EXPORT TextureData
+	class CM_UTILITY_EXPORT TextureData : public ISerializable
 	{
 	public:
 		TextureData(UINT32 width, UINT32 height, UINT32 size, 
@@ -83,5 +84,15 @@ namespace CamelotEngine
 		UINT8 mBPP;
 		PixelFormat mFormat;
 		UINT8* mData;
+
+	/************************************************************************/
+	/* 								SERIALIZATION                      		*/
+	/************************************************************************/
+	public:
+		friend class TextureDataST;
+		virtual SerializableType* getSerializable() const;
+		static TextureData* newObject();
+	private:
+		TextureData() {} // Only for serialization
 	};
 }

+ 47 - 0
CamelotUtility/Include/CmTextureDataST.h

@@ -0,0 +1,47 @@
+#pragma once
+
+#include "CmPrerequisitesUtil.h"
+#include "CmTextureData.h"
+#include "CmSerializableType.h"
+#include "CmManagedDataBlock.h"
+
+namespace CamelotEngine
+{
+	class TextureDataST : public SerializableType
+	{
+		CM_SETGET_MEMBER(mNumMipmaps, UINT32, TextureData)
+		CM_SETGET_MEMBER(mWidth, UINT32, TextureData)
+		CM_SETGET_MEMBER(mHeight, UINT32, TextureData)
+		CM_SETGET_MEMBER(mSize, UINT32, TextureData)
+		CM_SETGET_MEMBER(mDepth, UINT32, TextureData)
+		CM_SETGET_MEMBER(mFlags, INT32, TextureData)
+		CM_SETGET_MEMBER(mBPP, UINT8, TextureData)
+		CM_SETGET_MEMBER(mFormat, PixelFormat, TextureData)
+		
+		ManagedDataBlock getData(TextureData* obj) 
+		{
+			return ManagedDataBlock(obj->mData, obj->mSize, false);
+		}
+
+		void setData(TextureData* obj, ManagedDataBlock val) 
+		{ 
+			obj->mData = val.getData();
+			obj->mSize = val.getSize();
+		} 
+
+	public:
+		TextureDataST()
+		{
+			CM_ADD_PLAINFIELD(mNumMipmaps, 0, TextureDataST);
+			CM_ADD_PLAINFIELD(mWidth, 1, TextureDataST);
+			CM_ADD_PLAINFIELD(mHeight, 2, TextureDataST);
+			CM_ADD_PLAINFIELD(mSize, 3, TextureDataST);
+			CM_ADD_PLAINFIELD(mDepth, 4, TextureDataST);
+			CM_ADD_PLAINFIELD(mFlags, 5, TextureDataST);
+			CM_ADD_PLAINFIELD(mBPP, 6, TextureDataST);
+			CM_ADD_PLAINFIELD(mFormat, 7, TextureDataST);
+
+			addDataBlockField("Data", 8, &TextureDataST::getData, &TextureDataST::setData);
+		}
+	};
+}

+ 0 - 2
CamelotUtility/Include/CmUUID.h

@@ -6,6 +6,4 @@
 namespace CamelotEngine
 {
 	typedef boost::uuids::uuid UUID;
-
-	CM_SERIALIZABLE_SIMPLE_TYPE(UUID, 51);
 }

+ 15 - 0
CamelotUtility/Source/CmTextureData.cpp

@@ -1,5 +1,6 @@
 #include "CmTextureData.h"
 #include "CmException.h"
+#include "CmTextureDataST.h"
 
 namespace CamelotEngine
 {
@@ -52,4 +53,18 @@ namespace CamelotEngine
 		PixelData src(finalWidth, finalHeight, finalDepth, getFormat(), offset);
 		return src;
 	}
+
+	/************************************************************************/
+	/* 								SERIALIZATION                      		*/
+	/************************************************************************/
+	SerializableType* TextureData::getSerializable() const
+	{
+		static TextureDataST serializableType;
+		return &serializableType;
+	}
+
+	TextureData* TextureData::newObject()
+	{
+		return new TextureData();
+	}
 }