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

Represents a material in the rendering engine, encapsulating texture data, image views, samplers, and descriptor sets for use in Vulkan rendering operations. More...

#include <headers/evan/GPUMaterial.hpp>

Inheritance diagram for evan::GPUMaterial:
Collaboration diagram for evan::GPUMaterial:

Public Member Functions

 GPUMaterial (std::shared_ptr< DeviceContext > deviceContext, const Renderer &renderer, const utility::graphic::Material &material, uint32_t shaderID)
 Constructs a GPUMaterial instance using the provided device context, renderer, and texture path.
 
void destroy (VkDevice device)
 Destroys the Vulkan resources associated with the material.
 
void update (std::shared_ptr< DeviceContext > deviceContext, const Renderer &renderer, const utility::graphic::Material &material, uint32_t shaderID)
 Updates the GPUMaterial with new texture data and resources.
 
const std::vector< VkDescriptorSet > & getDescriptorSets () const
 Retrieves the Vulkan descriptor sets associated with this material.
 
uint32_t getShaderID () const
 Retrieves the unique shader ID associated with this material.
 
BlendMode getBlendMode () const
 Retrieves the blend mode this material has to be drawn with.
 
uint32_t getUploadedVersion () const
 Retrieves the version number of the uploaded material.
 

Protected Member Functions

void createDescriptorSets (VkDevice logicalDevice, VkDescriptorSetLayout descriptorSetLayout, VkDescriptorPool descriptorPool, const std::vector< VkBuffer > &uniformBuffers)
 Creates Vulkan descriptor sets for the material, allowing shaders to access the material's texture data and associated resources during rendering operations.
 
void cleanup ()
 Releases every Vulkan resource owned by this material.
 
void freeDescriptorSets (VkDevice device)
 Frees the descriptor sets allocated for this material and clears the container.
 

Protected Attributes

std::vector< std::shared_ptr< GPUTexture > > _textures
 
uint32_t _shaderID
 
utility::graphic::AlphaMode _alphaMode
 
std::vector< VkDescriptorSet > _descriptorSets
 
VkDescriptorPool _descriptorPool = VK_NULL_HANDLE
 
uint32_t _uploadedVersion = 0
 
std::shared_ptr< DeviceContext > _deviceContext
 

Detailed Description

Represents a material in the rendering engine, encapsulating texture data, image views, samplers, and descriptor sets for use in Vulkan rendering operations.

The GPUMaterial class is responsible for managing the Vulkan resources associated with a material, including creating images from texture files, generating mipmaps, creating image views and samplers, and setting up descriptor sets for shader access. It provides methods for initializing these resources based on a given texture path and the device context and renderer instances. The class also includes functionality for cleaning up Vulkan resources when they are no longer needed.

Definition at line 42 of file GPUMaterial.hpp.

Constructor & Destructor Documentation

◆ GPUMaterial()

evan::GPUMaterial::GPUMaterial ( std::shared_ptr< DeviceContext >  deviceContext,
const Renderer &  renderer,
const utility::graphic::Material &  material,
uint32_t  shaderID 
)

Constructs a GPUMaterial instance using the provided device context, renderer, and texture path.

This constructor initializes the material by creating a Vulkan image from the specified texture file, generating mipmaps for the image, creating an image view and sampler, and setting up descriptor sets for shader access. It utilizes the provided device context and renderer to access necessary Vulkan resources and configurations for these operations.

Parameters
deviceContextA reference to the DeviceContext instance, which provides access to Vulkan device resources and configurations needed for creating images, image views, samplers, and descriptor sets.
rendererA reference to the Renderer instance, which may be used to access rendering configurations or resources needed for material initialization.
materialA reference to the utility::graphic::Material instance, which contains information about the material's shader and textures, used to initialize the Vulkan resources for this material.
shaderIDThe unique ID of the shader associated with this material, used to identify which shader program should be used when rendering objects that utilize this material.
Note
The constructor performs several Vulkan operations, including:
  • Creating a Vulkan image from the specified texture file.
  • Generating mipmaps for the created image.
  • Creating an image view for the Vulkan image.
  • Creating a sampler for the Vulkan image.
  • Setting up descriptor sets for shader access to the material's texture data.

Member Function Documentation

◆ cleanup()

void evan::GPUMaterial::cleanup ( )
protected

Releases every Vulkan resource owned by this material.

Descriptor sets are freed before the textures they reference are destroyed. This method is idempotent, so it is safe to call from both the destructor and the public destroy() method.

◆ createDescriptorSets()

void evan::GPUMaterial::createDescriptorSets ( VkDevice  logicalDevice,
VkDescriptorSetLayout  descriptorSetLayout,
VkDescriptorPool  descriptorPool,
const std::vector< VkBuffer > &  uniformBuffers 
)
protected

Creates Vulkan descriptor sets for the material, allowing shaders to access the material's texture data and associated resources during rendering operations.

This method sets up descriptor sets for the material, which can be bound to shader stages to provide access to the material's Vulkan image, image view, sampler, and other associated resources. The descriptor sets are configured based on the provided descriptor set layout and uniform buffers.

Parameters
logicalDeviceThe Vulkan logical device used to create the descriptor sets. This device should be the same one used to create the material's Vulkan resources.
descriptorSetLayoutThe Vulkan descriptor set layout that defines the structure of the descriptor sets to be created for this material. The layout should match the expected bindings in the shader programs that will access the material's resources.
descriptorPoolThe Vulkan descriptor pool from which the descriptor sets will be allocated. This pool should have sufficient capacity to allocate the required number of descriptor sets for this material.
uniformBuffersA vector of Vulkan buffer handles representing uniform buffers that may be used in the descriptor sets for shader access to uniform data associated with the material.
Note
The method performs several Vulkan operations, including:
  • Allocating descriptor sets from the specified descriptor pool based on the provided descriptor set layout.
  • Configuring the allocated descriptor sets with the material's Vulkan image, image view, sampler, and any provided uniform buffers according to the expected bindings in the shader programs.
  • Storing the created descriptor sets for use in shader access and rendering operations.

◆ destroy()

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

Destroys the Vulkan resources associated with the material.

This method cleans up all Vulkan resources allocated for the material, including the Vulkan image, image view, sampler, and descriptor sets. It should be called when the material is no longer needed to free up GPU resources and prevent memory leaks.

Parameters
deviceThe Vulkan logical device used to destroy the associated resources. This device should be the same one used to create the resources in the first place.
Note
The method performs several Vulkan cleanup operations, including:
  • Destroying the Vulkan image associated with the material.
  • Freeing the memory allocated for the Vulkan image.
  • Destroying the image view associated with the Vulkan image.
  • Destroying the sampler associated with the material.
  • Cleaning up any descriptor sets associated with the material.

◆ freeDescriptorSets()

void evan::GPUMaterial::freeDescriptorSets ( VkDevice  device)
protected

Frees the descriptor sets allocated for this material and clears the container.

Parameters
deviceThe Vulkan logical device used to free the descriptor sets. When VK_NULL_HANDLE or when no descriptor sets are present, this method only clears the container.

◆ getBlendMode()

BlendMode evan::GPUMaterial::getBlendMode ( ) const

Retrieves the blend mode this material has to be drawn with.

Derived from the alpha semantics declared by the material (see utility::graphic::Material::getAlphaMode). AlphaMode::Mask is projected onto the alpha pipeline for now: a masked texel whose alpha is close to zero contributes nothing once blended, which keeps alpha-tested content visually correct until shaders can discard below the cutoff.

Returns
BlendMode::Alpha for Mask and Blend materials, BlendMode::Opaque otherwise.

◆ getDescriptorSets()

const std::vector< VkDescriptorSet > & evan::GPUMaterial::getDescriptorSets ( ) const

Retrieves the Vulkan descriptor sets associated with this material.

This method returns a vector of Vulkan descriptor sets that have been created for this material. These descriptor sets can be used in shader programs to access the material's texture data and other associated resources during rendering operations.

Returns
A const reference to the vector of VkDescriptorSet objects representing the descriptor sets associated with this material. These descriptor sets can be bound to shader stages to provide access to the material's resources during rendering.
Note
The returned reference is invalidated when the descriptor sets are rebuilt (createDescriptorSets, update) or when the material is destroyed. Callers must not hold it beyond the current bind.

◆ getShaderID()

uint32_t evan::GPUMaterial::getShaderID ( ) const

Retrieves the unique shader ID associated with this material.

This method returns the unique ID of the shader that is associated with this material. The shader ID is used to identify which shader program should be used when rendering objects that utilize this material, allowing for proper rendering of the material's visual properties based on the shader's functionality and characteristics.

Returns
uint32_t The unique shader ID associated with this material, used to identify the shader program for rendering objects that use this material.

◆ getUploadedVersion()

uint32_t evan::GPUMaterial::getUploadedVersion ( ) const

Retrieves the version number of the uploaded material.

This method returns the version number of the material that was uploaded to the GPU. This is useful for tracking changes and updates to the material's properties and resources.

Returns
uint32_t The version number of the uploaded material.

◆ update()

void evan::GPUMaterial::update ( std::shared_ptr< DeviceContext >  deviceContext,
const Renderer &  renderer,
const utility::graphic::Material &  material,
uint32_t  shaderID 
)

Updates the GPUMaterial with new texture data and resources.

This method updates the GPUMaterial by reinitializing its Vulkan resources based on the provided material's texture data. It creates new Vulkan images, image views, samplers, and descriptor sets as needed to reflect any changes in the material's properties or associated textures.

Parameters
deviceContextA shared pointer to the DeviceContext instance, which provides access to Vulkan device resources and configurations needed for updating images, image views, samplers, and descriptor sets.
rendererA reference to the Renderer instance, which may be used to access rendering configurations or resources needed for material updates.
materialA reference to the utility::graphic::Material instance, which contains updated information about the material's shader and textures, used to reinitialize the Vulkan resources for this material.
shaderIDThe unique ID of the shader associated with this material, used to identify which shader program should be used when rendering objects that utilize this material.

Member Data Documentation

◆ _alphaMode

utility::graphic::AlphaMode evan::GPUMaterial::_alphaMode
protected
Initial value:
=
utility::graphic::AlphaMode::Opaque

The alpha semantics declared by the material this object uploads.

Kept so the renderer can pick the opaque or the alpha-blended pipeline variant without reaching back into the CPU-side material.

Definition at line 275 of file GPUMaterial.hpp.

◆ _descriptorPool

VkDescriptorPool evan::GPUMaterial::_descriptorPool = VK_NULL_HANDLE
protected

The Vulkan descriptor pool from which the descriptor sets were allocated. Stored so they can be freed when the material is destroyed.

Definition at line 288 of file GPUMaterial.hpp.

◆ _descriptorSets

std::vector<VkDescriptorSet> evan::GPUMaterial::_descriptorSets
protected

Vector of Vulkan descriptor sets associated with the material.

Definition at line 281 of file GPUMaterial.hpp.

◆ _deviceContext

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

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

Definition at line 301 of file GPUMaterial.hpp.

◆ _shaderID

uint32_t evan::GPUMaterial::_shaderID
protected

The unique ID associated with the shader used by this material.

Definition at line 267 of file GPUMaterial.hpp.

◆ _textures

std::vector<std::shared_ptr<GPUTexture> > evan::GPUMaterial::_textures
protected

Textures associated with the material, categorized by their intended use in the rendering pipeline.

Definition at line 262 of file GPUMaterial.hpp.

◆ _uploadedVersion

uint32_t evan::GPUMaterial::_uploadedVersion = 0
protected

The version number of the material, used for tracking changes and updates to the material's properties and resources.

Definition at line 294 of file GPUMaterial.hpp.


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