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)

This commit is contained in:
Gered 2013-04-01 17:37:56 -04:00
parent d8c19411ec
commit 9f42c16c93
8 changed files with 70 additions and 25 deletions

View file

@ -14,11 +14,19 @@ BufferObject::BufferObject()
m_sizeInBytes = 0; m_sizeInBytes = 0;
} }
BufferObject::~BufferObject() void BufferObject::Release()
{ {
STACK_TRACE; STACK_TRACE;
if (!IsClientSideBuffer()) if (!IsClientSideBuffer())
FreeBufferObject(); 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) BOOL BufferObject::Initialize(GraphicsDevice *graphicsDevice, BUFFEROBJECT_TYPE type, BUFFEROBJECT_USAGE usage)

View file

@ -27,28 +27,30 @@ class BufferObject : public GraphicsContextResource
{ {
public: public:
BufferObject(); BufferObject();
virtual ~BufferObject(); virtual ~BufferObject() { Release(); }
virtual void Release();
/** /**
* @return TRUE if this buffer object is holding client side data only * @return TRUE if this buffer object is holding client side data only
* (hasn't been created as a buffer object in video memory) * (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 * @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 * @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) * @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 * @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 * @return TRUE if some or all of the buffer data has been changed since
* the last Update() call * the last Update() call
*/ */
BOOL IsDirty() const { return m_isDirty; } BOOL IsDirty() const { return m_isDirty; }
/** /**
* Uploads the current buffer data to video memory. * Uploads the current buffer data to video memory.
@ -79,7 +81,7 @@ public:
/** /**
* @return the OpenGL buffer object ID for this buffer * @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 OnNewContext();
void OnLostContext(); void OnLostContext();
@ -116,7 +118,7 @@ protected:
void CreateBufferObject(); void CreateBufferObject();
void FreeBufferObject(); void FreeBufferObject();
void SizeBufferObject(); void SizeBufferObject();
void SetDirty() { m_isDirty = TRUE; } void SetDirty() { m_isDirty = TRUE; }
private: private:

View file

@ -9,11 +9,13 @@ GraphicsContextResource::GraphicsContextResource()
m_graphicsDevice = NULL; m_graphicsDevice = NULL;
} }
GraphicsContextResource::~GraphicsContextResource() void GraphicsContextResource::Release()
{ {
STACK_TRACE; STACK_TRACE;
if (m_graphicsDevice != NULL) if (m_graphicsDevice != NULL)
m_graphicsDevice->UnregisterManagedResource(this); m_graphicsDevice->UnregisterManagedResource(this);
m_graphicsDevice = NULL;
} }
BOOL GraphicsContextResource::Initialize() BOOL GraphicsContextResource::Initialize()

View file

@ -14,7 +14,9 @@ class GraphicsContextResource
{ {
public: public:
GraphicsContextResource(); GraphicsContextResource();
virtual ~GraphicsContextResource(); virtual ~GraphicsContextResource() { Release(); }
virtual void Release();
/** /**
* New OpenGL graphics context creation callback. * New OpenGL graphics context creation callback.

View file

@ -10,9 +10,13 @@ IndexBuffer::IndexBuffer()
m_currentIndex = 0; m_currentIndex = 0;
} }
IndexBuffer::~IndexBuffer() void IndexBuffer::Release()
{ {
STACK_TRACE; m_buffer.clear();
stl::vector<uint16_t>().swap(m_buffer);
m_currentIndex = 0;
BufferObject::Release();
} }
BOOL IndexBuffer::Initialize(uint32_t numIndices, BUFFEROBJECT_USAGE usage) BOOL IndexBuffer::Initialize(uint32_t numIndices, BUFFEROBJECT_USAGE usage)

View file

@ -17,7 +17,12 @@ class IndexBuffer : public BufferObject
{ {
public: public:
IndexBuffer(); IndexBuffer();
virtual ~IndexBuffer(); virtual ~IndexBuffer() { Release(); }
/**
* Releases all resources associated with this index buffer.
*/
virtual void Release();
/** /**
* Initializes the index buffer. * Initializes the index buffer.
@ -94,24 +99,24 @@ public:
/** /**
* Moves the current index position to the beginning of the buffer. * 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. * 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. * Moves the current index position to the position specified.
* @param index the position to move to * @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. * Sets the index at the current position to a new value.
* @param index the new value to set * @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. * 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 * @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 * @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 * @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 * @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 * @return the number of index spaces left between the current position
* and the end of the buffer * and the end of the buffer
*/ */
uint32_t GetRemainingSpace() const { return (GetNumElements() - 1) - GetCurrentPosition(); } uint32_t GetRemainingSpace() const { return (GetNumElements() - 1) - GetCurrentPosition(); }
private: private:
stl::vector<uint16_t> m_buffer; stl::vector<uint16_t> m_buffer;

View file

@ -24,9 +24,26 @@ VertexBuffer::VertexBuffer()
m_numGPUAttributeSlotsUsed = 0; m_numGPUAttributeSlotsUsed = 0;
} }
VertexBuffer::~VertexBuffer() void VertexBuffer::Release()
{ {
STACK_TRACE; STACK_TRACE;
m_buffer.clear();
stl::vector<float>().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) BOOL VertexBuffer::Initialize(const VERTEX_ATTRIBS *attributes, uint32_t numAttributes, uint32_t numVertices, BUFFEROBJECT_USAGE usage)

View file

@ -26,7 +26,12 @@ class VertexBuffer : public BufferObject
{ {
public: public:
VertexBuffer(); VertexBuffer();
virtual ~VertexBuffer(); virtual ~VertexBuffer() { Release(); }
/**
* Releases all resources associated with this vertex buffer.
*/
virtual void Release();
/** /**
* Initializes the vertex buffer. * Initializes the vertex buffer.