From 9f42c16c93945c4fcdafd5b36ecbfe18454a1cfc Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 1 Apr 2013 17:37:56 -0400 Subject: [PATCH] add Release() method to handle resource/mem freeing instead of doing it only in the destructor (the destructor in these classes just calls Release() now) --- src/framework/graphics/bufferobject.cpp | 10 +++++++- src/framework/graphics/bufferobject.h | 18 +++++++------ .../graphics/graphicscontextresource.cpp | 4 ++- .../graphics/graphicscontextresource.h | 4 ++- src/framework/graphics/indexbuffer.cpp | 8 ++++-- src/framework/graphics/indexbuffer.h | 25 +++++++++++-------- src/framework/graphics/vertexbuffer.cpp | 19 +++++++++++++- src/framework/graphics/vertexbuffer.h | 7 +++++- 8 files changed, 70 insertions(+), 25 deletions(-) diff --git a/src/framework/graphics/bufferobject.cpp b/src/framework/graphics/bufferobject.cpp index 657ae14..746fe79 100644 --- a/src/framework/graphics/bufferobject.cpp +++ b/src/framework/graphics/bufferobject.cpp @@ -14,11 +14,19 @@ BufferObject::BufferObject() m_sizeInBytes = 0; } -BufferObject::~BufferObject() +void BufferObject::Release() { STACK_TRACE; if (!IsClientSideBuffer()) FreeBufferObject(); + + m_type = BUFFEROBJECT_TYPE_VERTEX; + m_usage = BUFFEROBJECT_USAGE_STATIC; + m_bufferId = 0; + m_isDirty = FALSE; + m_sizeInBytes = 0; + + GraphicsContextResource::Release(); } BOOL BufferObject::Initialize(GraphicsDevice *graphicsDevice, BUFFEROBJECT_TYPE type, BUFFEROBJECT_USAGE usage) diff --git a/src/framework/graphics/bufferobject.h b/src/framework/graphics/bufferobject.h index 66855fc..a1951a2 100644 --- a/src/framework/graphics/bufferobject.h +++ b/src/framework/graphics/bufferobject.h @@ -27,28 +27,30 @@ class BufferObject : public GraphicsContextResource { public: BufferObject(); - virtual ~BufferObject(); + virtual ~BufferObject() { Release(); } + + virtual void Release(); /** * @return TRUE if this buffer object is holding client side data only * (hasn't been created as a buffer object in video memory) */ - BOOL IsClientSideBuffer() const { return m_bufferId == 0; } + BOOL IsClientSideBuffer() const { return m_bufferId == 0; } /** * @return the type of data this buffer holds */ - BUFFEROBJECT_TYPE GetType() const { return m_type; } + BUFFEROBJECT_TYPE GetType() const { return m_type; } /** * @return the expected usage pattern of this buffer object */ - BUFFEROBJECT_USAGE GetUsage() const { return m_usage; } + BUFFEROBJECT_USAGE GetUsage() const { return m_usage; } /** * @return the size in bytes of this buffer object (including it's data) */ - size_t GetSizeInBytes() const { return m_sizeInBytes; } + size_t GetSizeInBytes() const { return m_sizeInBytes; } /** * @return the number of elements contained in this buffer object @@ -64,7 +66,7 @@ public: * @return TRUE if some or all of the buffer data has been changed since * the last Update() call */ - BOOL IsDirty() const { return m_isDirty; } + BOOL IsDirty() const { return m_isDirty; } /** * Uploads the current buffer data to video memory. @@ -79,7 +81,7 @@ public: /** * @return the OpenGL buffer object ID for this buffer */ - uint32_t GetBufferId() const { return m_bufferId; } + uint32_t GetBufferId() const { return m_bufferId; } void OnNewContext(); void OnLostContext(); @@ -116,7 +118,7 @@ protected: void CreateBufferObject(); void FreeBufferObject(); void SizeBufferObject(); - void SetDirty() { m_isDirty = TRUE; } + void SetDirty() { m_isDirty = TRUE; } private: diff --git a/src/framework/graphics/graphicscontextresource.cpp b/src/framework/graphics/graphicscontextresource.cpp index 3691157..f0f6375 100644 --- a/src/framework/graphics/graphicscontextresource.cpp +++ b/src/framework/graphics/graphicscontextresource.cpp @@ -9,11 +9,13 @@ GraphicsContextResource::GraphicsContextResource() m_graphicsDevice = NULL; } -GraphicsContextResource::~GraphicsContextResource() +void GraphicsContextResource::Release() { STACK_TRACE; if (m_graphicsDevice != NULL) m_graphicsDevice->UnregisterManagedResource(this); + + m_graphicsDevice = NULL; } BOOL GraphicsContextResource::Initialize() diff --git a/src/framework/graphics/graphicscontextresource.h b/src/framework/graphics/graphicscontextresource.h index 23166ab..090a3bb 100644 --- a/src/framework/graphics/graphicscontextresource.h +++ b/src/framework/graphics/graphicscontextresource.h @@ -14,7 +14,9 @@ class GraphicsContextResource { public: GraphicsContextResource(); - virtual ~GraphicsContextResource(); + virtual ~GraphicsContextResource() { Release(); } + + virtual void Release(); /** * New OpenGL graphics context creation callback. diff --git a/src/framework/graphics/indexbuffer.cpp b/src/framework/graphics/indexbuffer.cpp index 2ea29b6..d40b9ce 100644 --- a/src/framework/graphics/indexbuffer.cpp +++ b/src/framework/graphics/indexbuffer.cpp @@ -10,9 +10,13 @@ IndexBuffer::IndexBuffer() m_currentIndex = 0; } -IndexBuffer::~IndexBuffer() +void IndexBuffer::Release() { - STACK_TRACE; + m_buffer.clear(); + stl::vector().swap(m_buffer); + m_currentIndex = 0; + + BufferObject::Release(); } BOOL IndexBuffer::Initialize(uint32_t numIndices, BUFFEROBJECT_USAGE usage) diff --git a/src/framework/graphics/indexbuffer.h b/src/framework/graphics/indexbuffer.h index 2f270c7..0c339dd 100644 --- a/src/framework/graphics/indexbuffer.h +++ b/src/framework/graphics/indexbuffer.h @@ -17,7 +17,12 @@ class IndexBuffer : public BufferObject { public: IndexBuffer(); - virtual ~IndexBuffer(); + virtual ~IndexBuffer() { Release(); } + + /** + * Releases all resources associated with this index buffer. + */ + virtual void Release(); /** * Initializes the index buffer. @@ -94,24 +99,24 @@ public: /** * Moves the current index position to the beginning of the buffer. */ - void MoveToStart() { m_currentIndex = 0; } + void MoveToStart() { m_currentIndex = 0; } /** * Moves the current index position to the end of the buffer. */ - void MoveToEnd() { m_currentIndex = GetNumElements() - 1; } + void MoveToEnd() { m_currentIndex = GetNumElements() - 1; } /** * Moves the current index position to the position specified. * @param index the position to move to */ - void MoveTo(uint32_t index) { m_currentIndex = index; } + void MoveTo(uint32_t index) { m_currentIndex = index; } /** * Sets the index at the current position to a new value. * @param index the new value to set */ - void SetCurrent(uint16_t index) { SetIndex(m_currentIndex, index); } + void SetCurrent(uint16_t index) { SetIndex(m_currentIndex, index); } /** * Resizes the buffer capacity to hold the specified number of indices. @@ -129,28 +134,28 @@ public: /** * @return the number of indices contained in this buffer */ - uint32_t GetNumElements() const { return m_buffer.size(); } + uint32_t GetNumElements() const { return m_buffer.size(); } /** * @return the size in bytes of each index in this buffer object */ - size_t GetElementWidthInBytes() const { return sizeof(uint16_t); } + size_t GetElementWidthInBytes() const { return sizeof(uint16_t); } /** * @return pointer to this buffer object's raw data */ - const void* GetBuffer() const { return &m_buffer[0]; } + const void* GetBuffer() const { return &m_buffer[0]; } /** * @return the current position in the buffer */ - uint32_t GetCurrentPosition() const { return m_currentIndex; } + uint32_t GetCurrentPosition() const { return m_currentIndex; } /** * @return the number of index spaces left between the current position * and the end of the buffer */ - uint32_t GetRemainingSpace() const { return (GetNumElements() - 1) - GetCurrentPosition(); } + uint32_t GetRemainingSpace() const { return (GetNumElements() - 1) - GetCurrentPosition(); } private: stl::vector m_buffer; diff --git a/src/framework/graphics/vertexbuffer.cpp b/src/framework/graphics/vertexbuffer.cpp index 9e63bdf..33fd415 100644 --- a/src/framework/graphics/vertexbuffer.cpp +++ b/src/framework/graphics/vertexbuffer.cpp @@ -24,9 +24,26 @@ VertexBuffer::VertexBuffer() m_numGPUAttributeSlotsUsed = 0; } -VertexBuffer::~VertexBuffer() +void VertexBuffer::Release() { STACK_TRACE; + m_buffer.clear(); + stl::vector().swap(m_buffer); + + m_numVertices = 0; + m_currentVertex = 0; + m_standardTypeAttribs = 0; + m_elementWidth = 0; + m_colorOffset = 0; + m_position2Offset = 0; + m_position3Offset = 0; + m_normalOffset = 0; + m_texCoordOffset = 0; + m_numAttributes = 0; + m_attribs = NULL; + m_numGPUAttributeSlotsUsed = 0; + + BufferObject::Release(); } BOOL VertexBuffer::Initialize(const VERTEX_ATTRIBS *attributes, uint32_t numAttributes, uint32_t numVertices, BUFFEROBJECT_USAGE usage) diff --git a/src/framework/graphics/vertexbuffer.h b/src/framework/graphics/vertexbuffer.h index 8930d8d..14550f7 100644 --- a/src/framework/graphics/vertexbuffer.h +++ b/src/framework/graphics/vertexbuffer.h @@ -26,7 +26,12 @@ class VertexBuffer : public BufferObject { public: VertexBuffer(); - virtual ~VertexBuffer(); + virtual ~VertexBuffer() { Release(); } + + /** + * Releases all resources associated with this vertex buffer. + */ + virtual void Release(); /** * Initializes the vertex buffer.