Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CVertexBuffer Class Reference

CVertexBuffer: encapsulation of backend buffers, also supplying some additional functionality for sharing buffers between multiple objects. More...

#include <VertexBuffer.h>

Classes

struct  VBChunk
 

Public Member Functions

 CVertexBuffer (Renderer::Backend::IDevice *device, const char *name, const size_t vertexSize, const Renderer::Backend::IBuffer::Type type, const uint32_t usage)
 
 CVertexBuffer (Renderer::Backend::IDevice *device, const char *name, const size_t vertexSize, const Renderer::Backend::IBuffer::Type type, const uint32_t usage, const size_t maximumBufferSize)
 
 ~CVertexBuffer ()
 
void UploadIfNeeded (Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
 
void PrepareForRendering (VBChunk *chunk)
 Make the vertex data available for the next usage. More...
 
void UpdateChunkVertices (VBChunk *chunk, void *data)
 Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer. More...
 
size_t GetVertexSize () const
 
size_t GetBytesReserved () const
 
size_t GetBytesAllocated () const
 
bool CompatibleVertexType (const size_t vertexSize, const Renderer::Backend::IBuffer::Type type, const uint32_t usage) const
 Returns true if this vertex buffer is compatible with the specified vertex type and intended usage. More...
 
void DumpStatus () const
 
Renderer::Backend::IBufferGetBuffer ()
 

Static Public Member Functions

static bool UseStreaming (const uint32_t usage)
 Given the usage flags of a buffer that has been (or will be) allocated: More...
 

Private Member Functions

 NONCOPYABLE (CVertexBuffer)
 
VBChunkAllocate (const size_t vertexSize, const size_t numberOfVertices, const Renderer::Backend::IBuffer::Type type, const uint32_t usage, void *backingStore)
 Try to allocate a buffer of given number of vertices (each of given size), and with the given type - return null if no free chunks available. More...
 
void Release (VBChunk *chunk)
 Return given chunk to this buffer. More...
 

Private Attributes

size_t m_VertexSize
 Vertex size of this vertex buffer. More...
 
size_t m_MaxVertices
 Number of vertices of above size in this buffer. More...
 
std::vector< VBChunk * > m_FreeList
 List of free chunks in this buffer. More...
 
std::vector< VBChunk * > m_AllocList
 List of allocated chunks. More...
 
size_t m_FreeVertices
 Available free vertices - total of all free vertices in the free list. More...
 
std::unique_ptr< Renderer::Backend::IBufferm_Buffer
 
bool m_HasNeededChunks
 

Friends

class CVertexBufferManager
 

Detailed Description

CVertexBuffer: encapsulation of backend buffers, also supplying some additional functionality for sharing buffers between multiple objects.

The class can be used in two modes, depending on the usage parameter:

Static buffer: Call Allocate() with backingStore = nullptr. Then call UpdateChunkVertices() with any pointer - the data will be immediately copied to the buffer. This should be used for vertex data that rarely changes.

Dynamic buffer: Call Allocate() with backingStore pointing at some memory that will remain valid for the lifetime of the CVertexBuffer. This should be used for vertex data that may change every frame. Rendering is expected to occur in two phases:

  • "Prepare" phase: If this chunk is going to be used for rendering during the next rendering phase, you must call PrepareForRendering(). If the vertex data in backingStore has been modified since the last uploading phase, you must call UpdateChunkVertices().
  • "Upload" phase: UploadedIfNeeded() can be called (multiple times). The vertex data will be uploaded to the GPU if necessary. It is okay to have multiple prepare/upload cycles per frame (though slightly less efficient), but they must occur sequentially.

Constructor & Destructor Documentation

◆ CVertexBuffer() [1/2]

CVertexBuffer::CVertexBuffer ( Renderer::Backend::IDevice device,
const char *  name,
const size_t  vertexSize,
const Renderer::Backend::IBuffer::Type  type,
const uint32_t  usage 
)

◆ CVertexBuffer() [2/2]

CVertexBuffer::CVertexBuffer ( Renderer::Backend::IDevice device,
const char *  name,
const size_t  vertexSize,
const Renderer::Backend::IBuffer::Type  type,
const uint32_t  usage,
const size_t  maximumBufferSize 
)

◆ ~CVertexBuffer()

CVertexBuffer::~CVertexBuffer ( )

Member Function Documentation

◆ Allocate()

CVertexBuffer::VBChunk * CVertexBuffer::Allocate ( const size_t  vertexSize,
const size_t  numberOfVertices,
const Renderer::Backend::IBuffer::Type  type,
const uint32_t  usage,
void *  backingStore 
)
private

Try to allocate a buffer of given number of vertices (each of given size), and with the given type - return null if no free chunks available.

◆ CompatibleVertexType()

bool CVertexBuffer::CompatibleVertexType ( const size_t  vertexSize,
const Renderer::Backend::IBuffer::Type  type,
const uint32_t  usage 
) const

Returns true if this vertex buffer is compatible with the specified vertex type and intended usage.

◆ DumpStatus()

void CVertexBuffer::DumpStatus ( ) const

◆ GetBuffer()

Renderer::Backend::IBuffer * CVertexBuffer::GetBuffer ( )
inline

◆ GetBytesAllocated()

size_t CVertexBuffer::GetBytesAllocated ( ) const

◆ GetBytesReserved()

size_t CVertexBuffer::GetBytesReserved ( ) const

◆ GetVertexSize()

size_t CVertexBuffer::GetVertexSize ( ) const
inline

◆ NONCOPYABLE()

CVertexBuffer::NONCOPYABLE ( CVertexBuffer  )
private

◆ PrepareForRendering()

void CVertexBuffer::PrepareForRendering ( VBChunk chunk)

Make the vertex data available for the next usage.

◆ Release()

void CVertexBuffer::Release ( VBChunk chunk)
private

Return given chunk to this buffer.

◆ UpdateChunkVertices()

void CVertexBuffer::UpdateChunkVertices ( VBChunk chunk,
void *  data 
)

Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer.

◆ UploadIfNeeded()

void CVertexBuffer::UploadIfNeeded ( Renderer::Backend::IDeviceCommandContext deviceCommandContext)

◆ UseStreaming()

bool CVertexBuffer::UseStreaming ( const uint32_t  usage)
static

Given the usage flags of a buffer that has been (or will be) allocated:

If true, we assume the buffer is going to be modified on every frame, so we will re-upload the entire buffer every frame using glMapBuffer. This requires the buffer's owner to hold onto its backing store.

If false, we assume it will change rarely, and use direct upload to update it incrementally. The backing store can be freed to save memory.

Friends And Related Function Documentation

◆ CVertexBufferManager

friend class CVertexBufferManager
friend

Member Data Documentation

◆ m_AllocList

std::vector<VBChunk*> CVertexBuffer::m_AllocList
private

List of allocated chunks.

◆ m_Buffer

std::unique_ptr<Renderer::Backend::IBuffer> CVertexBuffer::m_Buffer
private

◆ m_FreeList

std::vector<VBChunk*> CVertexBuffer::m_FreeList
private

List of free chunks in this buffer.

◆ m_FreeVertices

size_t CVertexBuffer::m_FreeVertices
private

Available free vertices - total of all free vertices in the free list.

◆ m_HasNeededChunks

bool CVertexBuffer::m_HasNeededChunks
private

◆ m_MaxVertices

size_t CVertexBuffer::m_MaxVertices
private

Number of vertices of above size in this buffer.

◆ m_VertexSize

size_t CVertexBuffer::m_VertexSize
private

Vertex size of this vertex buffer.


The documentation for this class was generated from the following files: