class SGFXAPI::VertexBuffer

The VertexBuffer (VBO) actually represents the vertex data array sent to the GPU.

The format of the data is as follows (n is the number of vertices in the mesh/chunk/draw call):

A VBO with n vertices:
| vertex 0 data || vertex 1 data || vertex 2 data || vertex 3 data |  ...  | vertex n data |

A single vertex's data, with k elements:
| vertex element 0 | vertex element 1 | vertex element 2 | ... | vertex element k |

Example VBO:

| pos.x | pos.y | pos.z | uv.u | uv.v | | pos.x | pos.y | pos.z | uv.u | uv.v | ...
In the above example the VertexDeclaration looks like:

| "pos", float X 3 | "uv", float X 2 |

Public Functions

VertexBuffer(std::weak_ptr<Mesh> mesh, int numVertices, const VertexDeclaration &dec, Usage usage, bool allocateCpu)

See
UpdateToGpu(), UpdateToCpu(), AllocateCpuMemory().
Parameters
  • mesh -

    the mesh this VBO will be rendered with; this is for safety, to check that the mesh’s VAO (vertex attributes object) is bound when doing VBO operations.

  • numVertices -

    the length of the buffer, in number of vertices.

  • dec -

    the layout of each vertex within the buffer’s vertex-data-array.

  • usage -

    indicates if this VBO will be subsequently read from, or written to during rendering.

  • allocateCpu -

    allocates a CPU-side buffer for filling. The CPU side buffer is not strictly necessary if you upload the data already properly formatted.

int NumVertices() const

Returns the number of vertices in the VBO.

void SetNumVertices(std::size_t size, bool preserve_old_cpu_data)

Resize the VBO.

Parameters
  • preserve_old_cpu_data -

    if true, this will preserve the old CPU data; slicing it as necessary.

void UpdateToGpu(const uint8_t *data, int size)

Send data to the GPU side of this buffer.

HasGpuMemory() will return true after this call.

This call ignores any allocated CPU buffer in the VBO and explicitly sends the specified buffer.

See
UpdateToGpu(), UpdateToCpu()
Parameters
  • data -

    the buffer

  • size -

    the length of the buffer

void UpdateToGpu()

Send data to the GPU side of this buffer.

HasGpuMemory() will return true after this call.

This call used the allocated CPU side of the VBO. There must be an allocated CPU buffer to use this function

See
UpdateToGpu(const uint8_t*,int), AllocateCpuMemory(), CpuPtr(), UpdateToCpu()

void AllocateCpuMemory()

If HasCpuMemory() returns false, this will allocate a CPU-side buffer to store the vertex-data for this VBO.

HasCpuMemory() will return true after this call.

See
UpdateToGpu(), UpdateToCpu(), VertexBuffer::VertexBuffer()

void AllocateGpuMemory()

If HasGpuMemory() returns false, this will allocate a GPU-side buffer to store the vertex-data for this VBO.

Note
The VertexBuffer must be bound to call this method.
See
Bind()

HasGpuMemory() will return true after this call.

Note
Note that you can call UpdateToGpu(), even if there is no allocated GPU buffer yet; it will both allocate the GPU buffer and move the data there; making this function mostly redundant.
See
UpdateToGpu(), UpdateToCpu()

bool HasCpuMemory() const

Returns true of there is an allocated CPU-side buffer for this VBO.

See
AllocateCpuMemory()

bool HasGpuMemory() const

Returns true of there is an allocated GPU-side buffer for this VBO.

See
AllocateGpuMemory(), UpdateToGpu()

const VertexDeclaration &Declaration() const

Returns the VertexDeclaration associated with this buffer; the VertexDeclaration describes the layout of the individual elements in the array of vertex data.

unsigned char * SGFXAPI::VertexBuffer::CpuPtr()

Returns a raw pointer to the CPU data; throws a std::runtime_error if there is no CPU data allocated for this VertexBuffer.

const unsigned char * SGFXAPI::VertexBuffer::CpuPtr() const

Returns a raw pointer to the CPU data; throws a std::runtime_error if there is no CPU data allocated for this VertexBuffer.

void Bind()

Binds this vertex buffer.

Certain member functions require that the buffer is bound before usage.

Note
Note that the VAO (Mesh object) must be bound before binding the VBO (VertexBuffer), and this function will assert (in debug mode) as such.
See
AllocateGpuMemory(), UpdateToGpu(), VAOIsBound(), UnBind()

void UnBind()

See
Bind()

bool IsBound() const

Returns true if this VertexBuffer is bound.

bool VAOIsBound() const

Returns true if associated Mesh/VAO is bound.

Public Static Functions

void UnBindAll()

See
Bind()