evan 2026.1.4
Evan
Loading...
Searching...
No Matches
evan::GPUMesh Class Reference

A class representing a mesh stored on the GPU, containing vertex and index buffers, as well as material information. More...

#include <headers/evan/GPUMesh.hpp>

Inheritance diagram for evan::GPUMesh:
Collaboration diagram for evan::GPUMesh:

Public Member Functions

 GPUMesh (std::shared_ptr< DeviceContext > deviceContext, std::vector< GPUVertex > vertices, std::vector< uint32_t > indices, uint32_t materialID)
 Constructs a GPUMesh instance by creating vertex and index buffers on the GPU using the provided device context, vertex data, index data, and material ID.
 
void destroy (VkDevice device)
 Destroys the GPUMesh instance by cleaning up GPU resources associated with the vertex and index buffers.
 
VkBuffer getVertexBuffer () const
 Retrieves the Vulkan vertex buffer associated with this GPUMesh instance.
 
VkBuffer getIndexBuffer () const
 Retrieves the Vulkan index buffer associated with this GPUMesh instance.
 
uint32_t getIndexCount () const
 Retrieves the count of indices in the index buffer associated with this GPUMesh instance.
 
size_t getVertexCount () const
 Retrieves the number of vertices the vertex buffer was allocated for.
 
uint32_t getMaterialID () const
 Retrieves the material ID associated with this GPUMesh instance.
 
const utility::math::AabbF & getBounds () const
 Retrieves the axis-aligned bounding box of this mesh.
 
void setTransform (const glm::mat4 &transform)
 Sets the model transform of this mesh.
 
const glm::mat4 & getTransform () const
 Retrieves the per-instance model transform of this mesh.
 
void updateVertices (const std::vector< GPUVertex > &vertices)
 Updates the vertex data of the mesh in place.
 

Protected Member Functions

void createIndexBuffer (std::shared_ptr< DeviceContext > deviceContext, std::vector< uint32_t > indices)
 Creates the vertex buffer on the GPU using the provided device context and vertex data.
 
void cleanup ()
 Releases the vertex and index buffers owned by this mesh.
 

Protected Attributes

VkBuffer _vertexBuffer = VK_NULL_HANDLE
 
VkDeviceMemory _vertexBufferMemory = VK_NULL_HANDLE
 
VkBuffer _indexBuffer = VK_NULL_HANDLE
 
VkDeviceMemory _indexBufferMemory = VK_NULL_HANDLE
 
uint32_t _indexCount = 0
 
size_t _vertexCount = 0
 
uint32_t _materialID = 0
 
glm::mat4 _transform = glm::mat4(1.0f)
 
utility::math::AabbF _bounds
 
std::shared_ptr< DeviceContext > _deviceContext
 

Detailed Description

A class representing a mesh stored on the GPU, containing vertex and index buffers, as well as material information.

The GPUMesh class encapsulates the creation and management of vertex and index buffers on the GPU, allowing for efficient rendering of 3D models. It provides methods to retrieve the vertex and index buffers, as well as the count of indices and the material ID associated with the mesh. The class also includes a destructor to clean up GPU resources when the mesh is no longer needed.

Note
The GPUMesh class relies on the DeviceContext for Vulkan device management and assumes that the vertex data is provided in a specific format defined by the GPUVertex structure. It also assumes that the material ID is used to reference material properties for rendering.

Definition at line 41 of file GPUMesh.hpp.

Constructor & Destructor Documentation

◆ GPUMesh()

evan::GPUMesh::GPUMesh ( std::shared_ptr< DeviceContext >  deviceContext,
std::vector< GPUVertex >  vertices,
std::vector< uint32_t >  indices,
uint32_t  materialID 
)

Constructs a GPUMesh instance by creating vertex and index buffers on the GPU using the provided device context, vertex data, index data, and material ID.

This constructor initializes the GPUMesh by creating vertex and index buffers on the GPU using the provided device context. It takes in a vector of GPUVertex structures representing the vertex data, a vector of uint32_t representing the index data, and a uint32_t representing the material ID associated with the mesh. The constructor is responsible for allocating GPU resources and uploading the vertex and index data to the GPU for efficient rendering.

Parameters
deviceContextA reference to the DeviceContext instance used for Vulkan device management and resource creation.
verticesA vector of GPUVertex structures containing the vertex data for the mesh.
indicesA vector of uint32_t containing the index data for the mesh.
materialIDA uint32_t representing the material ID associated with the mesh, which can be used to reference material properties for rendering.
Note
The constructor assumes that the vertex data is provided in a specific format defined by the GPUVertex structure and that the material ID is used for rendering purposes. It also assumes that the DeviceContext is properly initialized and can be used to create GPU resources.

Member Function Documentation

◆ cleanup()

void evan::GPUMesh::cleanup ( )
protected

Releases the vertex and index buffers owned by this mesh.

Idempotent: every handle is null-checked and reset after being destroyed, making it safe to call from both the destructor and the public destroy() method.

◆ createIndexBuffer()

void evan::GPUMesh::createIndexBuffer ( std::shared_ptr< DeviceContext >  deviceContext,
std::vector< uint32_t >  indices 
)
protected

Creates the vertex buffer on the GPU using the provided device context and vertex data.

This method is responsible for creating the Vulkan vertex buffer and allocating memory for it on the GPU. It takes in a reference to the DeviceContext for Vulkan device management and a vector of GPUVertex structures containing the vertex data to be uploaded to the GPU. The method handles the creation of the vertex buffer, allocation of memory, and uploading of vertex data to ensure that the vertex buffer is properly set up for rendering operations.

Parameters
deviceContextA reference to the DeviceContext instance used for Vulkan device management and resource creation.
indicesA vector of uint32_t values containing the index data for the index buffer.
Note
The method assumes that the DeviceContext is properly initialized and can be used to create GPU resources. It also assumes that the index data is provided in a specific format and that the Vulkan device associated with the DeviceContext can be used for resource management.

◆ destroy()

void evan::GPUMesh::destroy ( VkDevice  device)

Destroys the GPUMesh instance by cleaning up GPU resources associated with the vertex and index buffers.

This method is responsible for releasing GPU resources allocated for the vertex and index buffers when the GPUMesh instance is destroyed. It takes in a VkDevice representing the Vulkan device used for resource management and ensures that all GPU resources are properly cleaned up to prevent memory leaks and ensure efficient resource usage.

Parameters
deviceThe Vulkan device used for resource management, which is required to clean up GPU resources associated with the vertex and index buffers.
Note
The method assumes that the Vulkan device is properly initialized and can be used to manage GPU resources. It also assumes that the vertex and index buffers were created using the same device context and are associated with the provided device for cleanup.

◆ getBounds()

const utility::math::AabbF & evan::GPUMesh::getBounds ( ) const

Retrieves the axis-aligned bounding box of this mesh.

The box is computed from the vertex positions at creation time and refreshed whenever updateVertices() is called. An empty box (isEmpty()) means the mesh has no vertices and must be treated as always visible by culling code.

Returns
A constant reference to the mesh bounding box.

◆ getIndexBuffer()

VkBuffer evan::GPUMesh::getIndexBuffer ( ) const

Retrieves the Vulkan index buffer associated with this GPUMesh instance.

This method returns the Vulkan buffer handle for the index buffer, which can be used for rendering operations. The index buffer contains the index data for the mesh and is created during the initialization of the GPUMesh instance.

Returns
VkBuffer The Vulkan buffer handle for the index buffer associated with this GPUMesh instance.

◆ getIndexCount()

uint32_t evan::GPUMesh::getIndexCount ( ) const

Retrieves the count of indices in the index buffer associated with this GPUMesh instance.

This method returns the number of indices stored in the index buffer, which is used for rendering operations to determine how many vertices to draw based on the index data.

Returns
uint32_t The count of indices in the index buffer associated with this GPUMesh instance.

◆ getMaterialID()

uint32_t evan::GPUMesh::getMaterialID ( ) const

Retrieves the material ID associated with this GPUMesh instance.

This method returns the material ID, which can be used to reference material properties for rendering. The material ID is set during the initialization of the GPUMesh instance and can be used to determine how the mesh should be rendered based on its associated material properties.

Returns
uint32_t The material ID associated with this GPUMesh instance, which can be used for rendering purposes to reference material properties.

◆ getTransform()

const glm::mat4 & evan::GPUMesh::getTransform ( ) const

Retrieves the per-instance model transform of this mesh.

Returns
The 4x4 model matrix (identity by default).

◆ getVertexBuffer()

VkBuffer evan::GPUMesh::getVertexBuffer ( ) const

Retrieves the Vulkan vertex buffer associated with this GPUMesh instance.

This method returns the Vulkan buffer handle for the vertex buffer, which can be used for rendering operations. The vertex buffer contains the vertex data for the mesh and is created during the initialization of the GPUMesh instance.

Returns
VkBuffer The Vulkan buffer handle for the vertex buffer associated with this GPUMesh instance.

◆ getVertexCount()

size_t evan::GPUMesh::getVertexCount ( ) const

Retrieves the number of vertices the vertex buffer was allocated for.

Used by RenderObject::updateMeshes() to decide whether an incoming mesh can be re-uploaded in place or requires a buffer rebuild.

Returns
The vertex count of this mesh.

◆ setTransform()

void evan::GPUMesh::setTransform ( const glm::mat4 &  transform)

Sets the model transform of this mesh.

This is the single source of truth for the pose of the mesh: the transform is written into the frame's instance buffer and applied by the vertex shader, so moving or scaling the mesh never touches its vertex data. The stored vertices are local-space.

Parameters
transformThe 4x4 model matrix of this mesh.

◆ updateVertices()

void evan::GPUMesh::updateVertices ( const std::vector< GPUVertex > &  vertices)

Updates the vertex data of the mesh in place.

This method re-uploads vertex data to the already-allocated vertex buffer, reusing the existing buffer and memory instead of creating and destroying Vulkan resources. It is intended for meshes whose topology is static but whose vertex positions change over time (for example a debug ray that follows the cursor).

Parameters
verticesA vector of GPUVertex structures containing the new vertex data. The number of vertices must match the count the mesh was created with.
Note
The method performs a staging upload followed by a buffer copy, so it should not be called every frame for large meshes. It avoids buffer creation and destruction, but still performs a device transfer.

Member Data Documentation

◆ _bounds

utility::math::AabbF evan::GPUMesh::_bounds
protected

The mesh bounding box, computed from vertex positions and refreshed by updateVertices(). Empty when the mesh has no vertices.

Definition at line 320 of file GPUMesh.hpp.

◆ _deviceContext

std::shared_ptr<DeviceContext> evan::GPUMesh::_deviceContext
protected

The device context used to create this mesh, kept alive for the lifetime of the mesh so that cleanup can safely destroy its Vulkan resources.

Definition at line 327 of file GPUMesh.hpp.

◆ _indexBuffer

VkBuffer evan::GPUMesh::_indexBuffer = VK_NULL_HANDLE
protected

Vulkan buffer handle for the index buffer associated with this GPUMesh instance, which contains the index data for rendering operations.

Definition at line 278 of file GPUMesh.hpp.

◆ _indexBufferMemory

VkDeviceMemory evan::GPUMesh::_indexBufferMemory = VK_NULL_HANDLE
protected

Vulkan device memory handle for the index buffer associated with this GPUMesh instance, which is used to manage the memory allocated for the index buffer on the GPU.

Definition at line 286 of file GPUMesh.hpp.

◆ _indexCount

uint32_t evan::GPUMesh::_indexCount = 0
protected

The count of indices stored in the index buffer associated with this GPUMesh instance, which is used for rendering operations to determine how many vertices to draw based on the index data.

Definition at line 294 of file GPUMesh.hpp.

◆ _materialID

uint32_t evan::GPUMesh::_materialID = 0
protected

The material ID associated with this GPUMesh instance, which can be used for rendering purposes to reference material properties.

Definition at line 308 of file GPUMesh.hpp.

◆ _transform

glm::mat4 evan::GPUMesh::_transform = glm::mat4(1.0f)
protected

Per-instance model transform. Identity by default; only consumed when renderer instancing is enabled.

Definition at line 314 of file GPUMesh.hpp.

◆ _vertexBuffer

VkBuffer evan::GPUMesh::_vertexBuffer = VK_NULL_HANDLE
protected

Vulkan buffer handle for the vertex buffer associated with this GPUMesh instance, which contains the vertex data for rendering operations.

Definition at line 263 of file GPUMesh.hpp.

◆ _vertexBufferMemory

VkDeviceMemory evan::GPUMesh::_vertexBufferMemory = VK_NULL_HANDLE
protected

Vulkan device memory handle for the vertex buffer associated with this GPUMesh instance, which is used to manage the memory allocated for the vertex buffer on the GPU.

Definition at line 271 of file GPUMesh.hpp.

◆ _vertexCount

size_t evan::GPUMesh::_vertexCount = 0
protected

The number of vertices the vertex buffer was allocated for. Used to validate updateVertices() calls and prevent writing past the end of the allocated buffer.

Definition at line 301 of file GPUMesh.hpp.


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