Ver Fonte

Ported GL renderer memory allocations

Marko Pintera há 12 anos atrás
pai
commit
724a22fd65

+ 5 - 5
CamelotClient/CamelotClient.cpp

@@ -26,9 +26,9 @@
 #include "CmDebugCamera.h"
 #include "CmTestTextSprite.h"
 
-#define DX11
+//#define DX11
 //#define DX9
-//#define GL
+#define GL
 
 using namespace CamelotEngine;
 
@@ -66,8 +66,8 @@ int CALLBACK WinMain(
 	HSceneObject testModelGO = SceneObject::create("TestMesh");
 	HRenderable testRenderable = testModelGO->addComponent<Renderable>();
 
-	HSceneObject testTextGO = SceneObject::create("TestText");
-	GameObjectHandle<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
+	//HSceneObject testTextGO = SceneObject::create("TestText");
+	//GameObjectHandle<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
 
 	HFont font;
 	
@@ -85,7 +85,7 @@ int CALLBACK WinMain(
 		font = Importer::instance().import("C:\\arial.ttf", fontImportOptions);
 	}
 
-	textSprite->setText(camera, "TESTfAV", font, 12);
+	//textSprite->setText(camera, "TESTfAV", font, 12);
 
 #if defined DX9
 	///////////////// HLSL 9 SHADERS //////////////////////////

+ 0 - 6
CamelotGLRenderer/Include/CmGLContext.h

@@ -57,12 +57,6 @@ namespace CamelotEngine {
         bool getInitialized() { return initialized; };
         void setInitialized() { initialized = true; };
 
-		/** Create a new context based on the same window/pbuffer as this
-			context - mostly useful for additional threads.
-		@note The caller is responsible for deleting the returned context.
-		*/
-		virtual GLContext* clone() const = 0;
-
 		/**
 		* Release the render context.
 		*/

+ 2 - 1
CamelotGLRenderer/Include/CmGLRenderSystemFactory.h

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

+ 0 - 2
CamelotGLRenderer/Include/CmWin32Context.h

@@ -44,8 +44,6 @@ namespace CamelotEngine {
         virtual void setCurrent();
 		/** See GLContext */
 		virtual void endCurrent();
-		/// @copydoc GLContext::clone
-		GLContext* clone() const;
 
 		virtual void releaseContext();
 

+ 1 - 1
CamelotGLRenderer/Source/CmGLContext.cpp

@@ -53,7 +53,7 @@ GLEWContext * glewGetContext()
 	GLEWContext * currentGLEWContextsPtr =  CM_THREAD_POINTER_GET(GLEWContextsPtr);
 	if (currentGLEWContextsPtr == NULL)
 	{
-		currentGLEWContextsPtr = new GLEWContext();
+		currentGLEWContextsPtr = CM_NEW(GLEWContext, GenAlloc) GLEWContext();
 		CM_THREAD_POINTER_SET(GLEWContextsPtr, currentGLEWContextsPtr);
 		memset(currentGLEWContextsPtr, 0, sizeof(GLEWContext));
 		glewInit();

+ 3 - 3
CamelotGLRenderer/Source/CmGLMultiRenderTexture.cpp

@@ -17,9 +17,9 @@ namespace CamelotEngine
 	void GLMultiRenderTexture::initialize_internal()
 	{
 		if(mFB != nullptr)
-			delete mFB;
+			CM_DELETE(mFB, GLFrameBufferObject, GenAlloc);
 
-		mFB = new GLFrameBufferObject(mFSAA);
+		mFB = CM_NEW(GLFrameBufferObject, PoolAlloc) GLFrameBufferObject(mFSAA);
 
 		for(size_t i = 0; i < mColorSurfaces.size(); i++)
 		{
@@ -61,7 +61,7 @@ namespace CamelotEngine
 	void GLMultiRenderTexture::destroy_internal()
 	{
 		if(mFB != nullptr)
-			delete mFB;
+			CM_DELETE(mFB, GLFrameBufferObject, GenAlloc);
 
 		MultiRenderTexture::destroy_internal();
 	}

+ 5 - 5
CamelotGLRenderer/Source/CmGLPixelBuffer.cpp

@@ -50,7 +50,7 @@ namespace CamelotEngine
 	GLPixelBuffer::~GLPixelBuffer()
 	{
 		// Force free buffer
-		delete [] (UINT8*)mBuffer.data;
+		CM_DELETE_BYTES(mBuffer.data, ScratchAlloc);
 	}
 	//-----------------------------------------------------------------------------  
 	void GLPixelBuffer::allocateBuffer()
@@ -58,7 +58,7 @@ namespace CamelotEngine
 		if(mBuffer.data)
 			// Already allocated
 			return;
-		mBuffer.data = new UINT8[mSizeInBytes];
+		mBuffer.data = CM_NEW_BYTES(mSizeInBytes, ScratchAlloc);
 		// TODO: use PBO if we're HBU_DYNAMIC
 	}
 	//-----------------------------------------------------------------------------  
@@ -67,7 +67,7 @@ namespace CamelotEngine
 		// Free buffer if we're STATIC to save memory
 		if(mUsage & GBU_STATIC)
 		{
-			delete [] (UINT8*)mBuffer.data;
+			CM_DELETE_BYTES(mBuffer.data, ScratchAlloc);
 			mBuffer.data = 0;
 		}
 	}
@@ -699,7 +699,7 @@ namespace CamelotEngine
 		if(GLPixelUtil::getGLOriginFormat(src_orig.format) == 0)
 		{
 			/// Convert to buffer internal format
-			data = new void*[PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat)];
+			data = CM_NEW_BYTES(PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat), ScratchAlloc);
 			src = PixelData(src_orig.getWidth(), src_orig.getHeight(), src_orig.getDepth(), mFormat, data);
 			PixelUtil::bulkPixelConversion(src_orig, src);
 		}
@@ -747,7 +747,7 @@ namespace CamelotEngine
 		glDeleteTextures(1, &id);
 
 		if(data != NULL)
-			delete[] data;
+			CM_DELETE_BYTES(data, ScratchAlloc);
 	}
 	//********* GLRenderBuffer
 	//----------------------------------------------------------------------------- 

+ 3 - 3
CamelotGLRenderer/Source/CmGLRenderTexture.cpp

@@ -45,7 +45,7 @@ namespace CamelotEngine
 	void GLRenderTexture::destroy_internal()
 	{
 		if(mFB != nullptr)
-			delete mFB;
+			CM_DELETE(mFB, GLFrameBufferObject, PoolAlloc);
 
 		RenderTexture::destroy_internal();
 	}
@@ -53,9 +53,9 @@ namespace CamelotEngine
 	void GLRenderTexture::initialize_internal()
 	{
 		if(mFB != nullptr)
-			delete mFB;
+			CM_DELETE(mFB, GLFrameBufferObject, PoolAlloc);
 
-		mFB = new GLFrameBufferObject(mFSAA);
+		mFB = CM_NEW(GLFrameBufferObject, PoolAlloc) GLFrameBufferObject(mFSAA);
 
 		GLSurfaceDesc surfaceDesc;
 		surfaceDesc.numSamples = mFSAA;

+ 2 - 2
CamelotGLRenderer/Source/CmGLTexture.cpp

@@ -286,9 +286,9 @@ namespace CamelotEngine {
 		{
 			for(UINT32 mip=0; mip<=getNumMipmaps(); mip++)
 			{
-                GLPixelBuffer *buf = new GLTextureBuffer("", getGLTextureTarget(), mTextureID, face, mip,
+                GLPixelBuffer *buf = CM_NEW(GLTextureBuffer, PoolAlloc) GLTextureBuffer("", getGLTextureTarget(), mTextureID, face, mip,
 						static_cast<GpuBufferUsage>(mUsage), false, mHwGamma, mFSAA);
-				mSurfaceList.push_back(PixelBufferPtr(buf));
+				mSurfaceList.push_back(PixelBufferPtr(buf, &MemAllocDeleter<GLPixelBuffer, PoolAlloc>::deleter));
                 
                 /// Check for error
                 if(buf->getWidth()==0 || buf->getHeight()==0 || buf->getDepth()==0)

+ 2 - 2
CamelotGLRenderer/Source/CmGLTextureManager.cpp

@@ -71,7 +71,7 @@ namespace CamelotEngine {
 		// Generate warning texture
 		UINT32 width = 8;
 		UINT32 height = 8;
-		UINT32 *data = new UINT32[width*height];		// 0xXXRRGGBB
+		UINT32 *data = (UINT32*)CM_NEW_BYTES(sizeof(UINT32)*width*height, ScratchAlloc);		// 0xXXRRGGBB
 		// Yellow/black stripes
 		for(UINT32 y=0; y<height; ++y)
 		{
@@ -93,7 +93,7 @@ namespace CamelotEngine {
 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT, (void*)data);
 		}
 		// Free memory
-		delete [] data;
+		CM_DELETE_BYTES(data, ScratchAlloc);
 	}
 	//-----------------------------------------------------------------------------
 	PixelFormat GLTextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)

+ 1 - 2
CamelotGLRenderer/Source/CmWin32GLSupport.cpp

@@ -363,10 +363,9 @@ namespace CamelotEngine
 	String translateWGLError()
 	{
 		int winError = GetLastError();
-		char* errDesc;
+		char errDesc[255];
 		int i;
 
-		errDesc = new char[255];
 		// Try windows errors first
 		i = FormatMessage(
 			FORMAT_MESSAGE_FROM_SYSTEM |

+ 7 - 7
CamelotGLRenderer/Source/CmWin32Window.cpp

@@ -150,7 +150,7 @@ namespace CamelotEngine {
 			GetMonitorInfo(hMonitor, &monitorInfoEx);
 
 			size_t devNameLen = strlen(monitorInfoEx.szDevice);
-			mDeviceName = new char[devNameLen + 1];
+			mDeviceName = (char*)CM_NEW_BYTES((UINT32)(devNameLen + 1), ScratchAlloc);
 
 			strcpy_s(mDeviceName, devNameLen + 1, monitorInfoEx.szDevice);
 
@@ -374,7 +374,7 @@ namespace CamelotEngine {
 		}
 
 		// Create RenderSystem context
-		mContext = new Win32Context(mHDC, mGlrc);
+		mContext = CM_NEW(Win32Context, GenAlloc) Win32Context(mHDC, mGlrc);
 
 		mActive = true;
 
@@ -391,8 +391,8 @@ namespace CamelotEngine {
 		if (!mHWnd)
 			return;
 
-		// Unregister and destroy OGRE GLContext
-		delete mContext;
+		// Unregister and destroy GLContext
+		CM_DELETE(mContext, Win32Context, GenAlloc);
 
 		if (!mIsExternalGLContext && mGlrc)
 		{
@@ -420,7 +420,7 @@ namespace CamelotEngine {
 
 		if (mDeviceName != NULL)
 		{
-			delete[] mDeviceName;
+			CM_DELETE_BYTES(mDeviceName, ScratchAlloc);
 			mDeviceName = NULL;
 		}
 
@@ -677,7 +677,7 @@ namespace CamelotEngine {
 		{
 			size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.format);
 			size_t height = dst.getHeight();
-			UINT8 *tmpData = new UINT8[rowSpan * height];
+			UINT8 *tmpData = CM_NEW_BYTES((UINT32)(rowSpan * height), ScratchAlloc);
 			UINT8 *srcRow = (UINT8 *)dst.data, *tmpRow = tmpData + (height - 1) * rowSpan;
 
 			while (tmpRow >= tmpData)
@@ -688,7 +688,7 @@ namespace CamelotEngine {
 			}
 			memcpy(dst.data, tmpData, rowSpan * height);
 
-			delete [] tmpData;
+			CM_DELETE_BYTES(tmpData, ScratchAlloc);
 		}
 	}
 

+ 4 - 4
CamelotGLRenderer/Source/GLSL/include/CmGLSLParamParser.h

@@ -68,7 +68,7 @@ namespace CamelotEngine
 		GLint maxNameSize = 0;
 		glGetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxNameSize);
 
-		GLchar* attributeName = new GLchar[maxNameSize];
+		GLchar* attributeName = (GLchar*)CM_NEW_BYTES(sizeof(GLchar) * maxNameSize, ScratchAlloc);
 
 		for(GLint i = 0; i < numAttributes; i++)
 		{
@@ -90,7 +90,7 @@ namespace CamelotEngine
 			}
 		}
 
-		delete[] attributeName;
+		CM_DELETE_BYTES(attributeName, ScratchAlloc);
 	}
 
 	VertexElementType GLSLParamParser::glTypeToAttributeType(GLenum glType)
@@ -152,7 +152,7 @@ namespace CamelotEngine
 		if(maxBlockNameBufferSize > maxBufferSize)
 			maxBufferSize = maxBlockNameBufferSize;
 
-		GLchar* uniformName = new GLchar[maxBufferSize];
+		GLchar* uniformName = (GLchar*)CM_NEW_BYTES(sizeof(GLchar) * maxBufferSize, ScratchAlloc);
 
 		GpuParamBlockDesc newGlobalBlockDesc;
 		newGlobalBlockDesc.slot = 0;
@@ -464,7 +464,7 @@ namespace CamelotEngine
 		}
 #endif
 
-		delete[] uniformName;
+		CM_DELETE_BYTES(uniformName, ScratchAlloc);
 	}
 
 	void GLSLParamParser::determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex)

+ 1 - 2
CamelotGLRenderer/Source/GLSL/include/CmGLSLPreprocessor.h

@@ -196,8 +196,7 @@ class CPreprocessor
             ExpandFunc (NULL), Expanding (false)
         { }
 
-        ~Macro ()
-        { delete [] Args; delete Next; }
+        ~Macro ();
 
         /// Expand the macro value (will not work for functions)
         Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros);

+ 4 - 4
CamelotGLRenderer/Source/GLSL/src/CmGLSLExtSupport.cpp

@@ -95,12 +95,12 @@ namespace CamelotEngine
 			{
 				GLint charsWritten  = 0;
 
-				GLchar* infoLog = new GLchar[infologLength];
+				GLchar* infoLog = (GLchar*)CM_NEW_BYTES(sizeof(GLchar) * infologLength, ScratchAlloc);
 
 				glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
 				logMessage += String(infoLog);
 
-				delete [] infoLog;
+				CM_DELETE_BYTES(infoLog, ScratchAlloc);
 
 				if(charsWritten > 0)
 					return true;
@@ -122,12 +122,12 @@ namespace CamelotEngine
 			{
 				GLint charsWritten  = 0;
 
-				GLchar* infoLog = new GLchar[infologLength];
+				GLchar* infoLog = (GLchar*)CM_NEW_BYTES(sizeof(GLchar) * infologLength, ScratchAlloc);
 
 				glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
 				msg += String(infoLog);
 
-				delete [] infoLog;
+				CM_DELETE_BYTES(infoLog, ScratchAlloc);
 
 				if(charsWritten > 0)
 					return true;

+ 16 - 9
CamelotGLRenderer/Source/GLSL/src/CmGLSLPreprocessor.cpp

@@ -243,6 +243,12 @@ static void DefaultError (void *iData, int iLine, const char *iError,
 
 CPreprocessor::ErrorHandlerFunc CPreprocessor::ErrorHandler = DefaultError;
 
+CPreprocessor::Macro::~Macro()
+{ 
+	CM_DELETE_ARRAY(Args, Token, NumArgs, ScratchAlloc); 
+	CM_DELETE(Next, Macro, ScratchAlloc); 
+}
+
 CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL)
 {
     Source = iToken.String;
@@ -254,7 +260,8 @@ CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL)
 
 CPreprocessor::~CPreprocessor ()
 {
-    delete MacroList;
+	if(MacroList != nullptr)
+		CM_DELETE(MacroList, Macro, ScratchAlloc);
 }
 
 void CPreprocessor::Error (int iLine, const char *iError, const Token *iToken)
@@ -420,7 +427,7 @@ CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken)
             Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true);
             if (t.Type == Token::TK_ERROR)
             {
-                delete [] args;
+                CM_DELETE_ARRAY(args, Token, nargs, ScratchAlloc);
                 return t;
             }
 
@@ -449,7 +456,7 @@ CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken)
             cur->Expand (nargs, args, MacroList);
         t.AppendNL (Line - old_line);
 
-        delete [] args;
+		CM_DELETE_ARRAY(args, Token, nargs, ScratchAlloc);
 
         return t;
     }
@@ -860,7 +867,7 @@ CPreprocessor::Token CPreprocessor::GetArguments (int &oNumArgs, Token *&oArgs,
 
 Done:
     oNumArgs = nargs;
-    oArgs = new Token [nargs];
+    oArgs = CM_NEW_ARRAY(Token, nargs, ScratchAlloc);
     for (int i = 0; i < nargs; i++)
         oArgs [i] = args [i];
     return t;
@@ -878,7 +885,7 @@ bool CPreprocessor::HandleDefine (Token &iBody, int iLine)
         return false;
     }
 
-    Macro *m = new Macro (t);
+    Macro *m = CM_NEW(Macro, ScratchAlloc) Macro (t);
     m->Body = iBody;
     t = cpp.GetArguments (m->NumArgs, m->Args, false);
     while (t.Type == Token::TK_WHITESPACE)
@@ -893,7 +900,7 @@ bool CPreprocessor::HandleDefine (Token &iBody, int iLine)
             break;
 
         case Token::TK_ERROR:
-            delete m;
+            CM_DELETE(m, Macro, ScratchAlloc);
             return false;
 
         default:
@@ -1146,7 +1153,7 @@ Done:
 void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
                             const char *iMacroValue, size_t iMacroValueLen)
 {
-    Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
+    Macro *m = CM_NEW(Macro, ScratchAlloc) Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
     m->Value = Token (Token::TK_TEXT, iMacroValue, iMacroValueLen);
     m->Next = MacroList;
     MacroList = m;
@@ -1155,7 +1162,7 @@ void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
 void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
                             long iMacroValue)
 {
-    Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
+    Macro *m = CM_NEW(Macro, ScratchAlloc) Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
     m->Value.SetValue (iMacroValue);
     m->Next = MacroList;
     MacroList = m;
@@ -1171,7 +1178,7 @@ bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen)
         {
             Macro *next = (*cur)->Next;
             (*cur)->Next = NULL;
-            delete (*cur);
+            CM_DELETE((*cur), Macro, ScratchAlloc);
             *cur = next;
             return true;
         }

+ 1 - 28
CamelotGLRenderer/Source/win32/CmWin32Context.cpp

@@ -59,33 +59,6 @@ namespace CamelotEngine {
 		wglMakeCurrent(NULL, NULL);
 	}
 
-	GLContext* Win32Context::clone() const
-	{
-		// Create new context based on own HDC
-		HGLRC newCtx = wglCreateContext(mHDC);
-		
-		if (!newCtx)
-		{
-			CM_EXCEPT(InternalErrorException, "Error calling wglCreateContext");
-		}
-
-		HGLRC oldrc = wglGetCurrentContext();
-		HDC oldhdc = wglGetCurrentDC();
-		wglMakeCurrent(NULL, NULL);
-		// Share lists with old context
-	    if (!wglShareLists(mGlrc, newCtx))
-		{
-			String errorMsg = translateWGLError();
-			wglDeleteContext(newCtx);
-			CM_EXCEPT(RenderingAPIException, String("wglShareLists() failed: ") + errorMsg);
-		}
-		// restore old context
-		wglMakeCurrent(oldhdc, oldrc);
-		
-
-		return new Win32Context(mHDC, newCtx);
-	}
-
 	void Win32Context::releaseContext()
 	{
 		if (mGlrc != NULL)
@@ -108,7 +81,7 @@ WGLEWContext * wglewGetContext()
 	WGLEWContext * currentWGLEWContextsPtr = CM_THREAD_POINTER_GET(WGLEWContextsPtr);
 	if (currentWGLEWContextsPtr == NULL)
 	{
-		currentWGLEWContextsPtr = new WGLEWContext();
+		currentWGLEWContextsPtr = CM_NEW(WGLEWContext, GenAlloc) WGLEWContext();
 		CM_THREAD_POINTER_SET(WGLEWContextsPtr, currentWGLEWContextsPtr);
 		ZeroMemory(currentWGLEWContextsPtr, sizeof(WGLEWContext));
 		wglewInit();