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

GPUTexture class. More...

#include <headers/evan/GPUTexture.hpp>

Inheritance diagram for evan::GPUTexture:
Collaboration diagram for evan::GPUTexture:

Classes

struct  GenerateMipmapsProperties
 Holds properties required for generating mipmaps for a Vulkan image. More...
 
struct  SamplerSettings
 Consumer-overridable sampler parameters. More...
 

Public Types

enum class  TextureType { Albedo , Normal , Roughness , FontAtlas }
 Enum class representing different types of textures that can be associated with a material, such as albedo, normal, and roughness textures. More...
 

Public Member Functions

 GPUTexture (std::shared_ptr< DeviceContext > deviceContext, const utility::graphic::Texture &texture, TextureType type=TextureType::Albedo, const SamplerSettings &settings=SamplerSettings {})
 Constructs a GPUTexture object by creating a Vulkan image, image view, and sampler based on the provided texture data and type.
 
 ~GPUTexture ()
 Destructs the GPUTexture object by cleaning up Vulkan resources such as the image, image view, and sampler.
 
void destroy (VkDevice device)
 Destroys the Vulkan resources associated with this GPUTexture, including the image, image view, and sampler.
 
uint32_t getMipLevels () const
 Retrieves the number of mipmap levels generated for this texture.
 

Static Public Member Functions

static uint32_t computeMipLevels (uint32_t width, uint32_t height)
 Computes the number of mipmap levels for a texture of the given dimensions.
 
static float computeMaxLod (uint32_t mipLevels)
 Computes the maximum sampleable LOD for a texture with the given number of mipmap levels.
 

Public Attributes

VkImageView view = VK_NULL_HANDLE
 The Vulkan image view associated with this GPUTexture.
 
VkSampler sampler = VK_NULL_HANDLE
 The Vulkan sampler associated with this GPUTexture, which defines how the texture is sampled in shader programs.
 
TextureType type
 The type of texture represented by this GPUTexture instance, such as albedo, normal, or roughness. This property is used to categorize the texture based on its intended use in the rendering pipeline, allowing for organized management of texture resources and their bindings in shader programs.
 

Protected Member Functions

void cleanup ()
 Releases every Vulkan resource owned by this texture.
 
void createImage (const ADeviceBackend &deviceBackend, const utility::graphic::Texture &texture, VkCommandPool commandPool, VkQueue graphicsQueue)
 Creates a Vulkan image based on the provided texture data and type, and allocates memory for it.
 
void createImageView (const ADeviceBackend &deviceBackend)
 Creates a Vulkan image view for the Vulkan image associated with this GPUTexture, allowing shader access to the texture data.
 
void createSampler (const ADeviceBackend &deviceBackend, VkSamplerCreateInfo samplerInfo, const SamplerSettings &settings=SamplerSettings {})
 Creates a Vulkan sampler for the Vulkan image associated with this GPUTexture, defining how the texture is sampled in shader programs.
 

Protected Attributes

VkImage _image = VK_NULL_HANDLE
 The Vulkan image associated with this GPUTexture, which holds the texture data in GPU memory for use in rendering operations.
 
VkDeviceMemory _memory = VK_NULL_HANDLE
 The Vulkan device memory allocated for the image associated with this GPUTexture, which is used to store the texture data in GPU memory.
 
uint32_t _mipLevel = 1
 The number of mipmap levels generated for the Vulkan image associated with this GPUTexture, which is used to determine the level of detail for texture sampling in shader programs. This property is important for optimizing rendering performance and visual quality by allowing shaders to sample from different mipmap levels based on the distance and angle of the textured surface relative to the camera.
 
std::shared_ptr< DeviceContext > _deviceContext
 The device context used to create this texture, kept alive for the lifetime of the texture so that cleanup can safely destroy its Vulkan resources.
 

Detailed Description

GPUTexture class.

This class represents a texture that can be used in the GPU for rendering. It encapsulates the Vulkan image, image view, and sampler associated with the texture, as well as the type of texture (e.g., albedo, normal, roughness) for categorization in the rendering pipeline. The GPUTexture class provides functionality for creating and managing Vulkan resources related to textures, including image creation, memory allocation, image view setup, sampler creation, and mipmap generation. It also includes a method for destroying the Vulkan resources when they are no longer needed.

Note
The GPUTexture class is designed to work with the Vulkan API and is intended to be used in conjunction with a device backend that provides access to the Vulkan device and related resources. It is important to ensure that the GPUTexture is properly managed and destroyed to prevent resource leaks in the Vulkan application.

Definition at line 44 of file GPUTexture.hpp.

Member Enumeration Documentation

◆ TextureType

Enum class representing different types of textures that can be associated with a material, such as albedo, normal, and roughness textures.

This enum class is used to categorize textures based on their intended use in the rendering pipeline. Each texture type corresponds to a specific role in defining the visual properties of a material, allowing for organized management of texture resources and their bindings in shader programs.

Enumerator
Albedo 

Represents the base color texture of a material, defining its diffuse color.

Normal 

Represents the normal map texture, which encodes surface normals for detailed lighting effects.

Roughness 

Represents the roughness texture, which defines the surface roughness for specular reflections.

FontAtlas 

Represents the font atlas texture, which contains pre-rendered glyphs for text rendering.

Definition at line 60 of file GPUTexture.hpp.

Constructor & Destructor Documentation

◆ GPUTexture()

evan::GPUTexture::GPUTexture ( std::shared_ptr< DeviceContext >  deviceContext,
const utility::graphic::Texture &  texture,
TextureType  type = TextureType::Albedo,
const SamplerSettings &  settings = SamplerSettings {} 
)

Constructs a GPUTexture object by creating a Vulkan image, image view, and sampler based on the provided texture data and type.

This constructor initializes a GPUTexture instance by creating a Vulkan image using the provided texture data, setting up the corresponding image view and sampler for use in shader programs. The constructor takes into account the type of texture being created (e.g., albedo, normal, roughness) to determine the appropriate format and usage flags for the Vulkan image, ensuring that the texture is correctly configured for its intended role in the rendering pipeline.

Parameters
deviceContextA reference to the DeviceContext instance, which provides access to the Vulkan device and related resources needed for creating the image, image view, and sampler.
textureA reference to a utility::graphic::Texture object containing the pixel data and dimensions of the texture to be created.
typeThe type of texture being created (e.g., albedo, normal, roughness), which determines the format and usage flags for the Vulkan image.
settingsOptional sampler settings (min/max LOD, anisotropy, address modes) used to override the default sampler configuration.
Note
The constructor performs several Vulkan operations, including:
  • Creating a Vulkan image with the appropriate format and usage flags based on the texture type.
  • Allocating memory for the Vulkan image and binding it to the image object.
  • Creating an image view for the Vulkan image to allow shader access.
  • Creating a sampler for the Vulkan image to define how the texture is sampled in shaders.
  • Generating mipmaps for the Vulkan image if necessary, based on the texture dimensions

Member Function Documentation

◆ cleanup()

void evan::GPUTexture::cleanup ( )
protected

Releases every Vulkan resource owned by this texture.

This method is idempotent: each handle is checked against VK_NULL_HANDLE before being destroyed and is reset afterwards, so it is safe to call more than once or after a partially failed construction.

◆ computeMaxLod()

static float evan::GPUTexture::computeMaxLod ( uint32_t  mipLevels)
static

Computes the maximum sampleable LOD for a texture with the given number of mipmap levels.

LOD is zero-based: a texture with mipLevels levels has valid LODs in the range [0, mipLevels - 1].

Parameters
mipLevelsThe number of mipmap levels.
Returns
The maximum LOD value covering the last mip level.

◆ computeMipLevels()

static uint32_t evan::GPUTexture::computeMipLevels ( uint32_t  width,
uint32_t  height 
)
static

Computes the number of mipmap levels for a texture of the given dimensions.

Parameters
widthThe width of the texture in pixels.
heightThe height of the texture in pixels.
Returns
The number of mipmap levels (including the base level).

◆ createImage()

void evan::GPUTexture::createImage ( const ADeviceBackend &  deviceBackend,
const utility::graphic::Texture &  texture,
VkCommandPool  commandPool,
VkQueue  graphicsQueue 
)
protected

Creates a Vulkan image based on the provided texture data and type, and allocates memory for it.

This method is responsible for creating a Vulkan image object using the pixel data and dimensions from the provided utility::graphic::Texture, as well as determining the appropriate format and usage flags based on the specified TextureType. It also allocates memory for the Vulkan image and binds it to the image object, ensuring that the texture data is properly stored in GPU memory for use in rendering operations.

Parameters
deviceBackendA reference to the ADeviceBackend instance, which provides access to the Vulkan device and related resources needed for creating the image and allocating memory.
textureA reference to a utility::graphic::Texture object containing the pixel data and dimensions of the texture to be created.
commandPoolThe Vulkan command pool used to allocate command buffers for any necessary staging operations during image creation.
graphicsQueueThe Vulkan graphics queue used to submit command buffers for execution during image creation and any necessary staging operations.

◆ createImageView()

void evan::GPUTexture::createImageView ( const ADeviceBackend &  deviceBackend)
protected

Creates a Vulkan image view for the Vulkan image associated with this GPUTexture, allowing shader access to the texture data.

This method sets up the image view for the Vulkan image, which is necessary for shaders to access the texture data correctly. The image view defines how the image data is interpreted and accessed in shader programs, including aspects such as format and subresource range. Properly creating the image view ensures that the texture can be sampled correctly in rendering operations.

Parameters
deviceBackendA reference to the ADeviceBackend instance, which provides access to the Vulkan device and related resources needed for creating the image view.

◆ createSampler()

void evan::GPUTexture::createSampler ( const ADeviceBackend &  deviceBackend,
VkSamplerCreateInfo  samplerInfo,
const SamplerSettings &  settings = SamplerSettings {} 
)
protected

Creates a Vulkan sampler for the Vulkan image associated with this GPUTexture, defining how the texture is sampled in shader programs.

This method sets up the sampler for the Vulkan image, which specifies how the texture is sampled in shader programs, including filtering modes, addressing modes, and mipmapping behavior. Properly creating the sampler ensures that the texture is sampled correctly in rendering operations, allowing for effects such as smooth filtering and proper handling of texture coordinates outside the [0, 1] range.

Parameters
deviceBackendA reference to the ADeviceBackend instance, which provides access to the Vulkan device and related resources needed for creating the sampler.
samplerInfoA VkSamplerCreateInfo structure containing the parameters for creating the Vulkan sampler, such as filtering modes, addressing modes, and mipmapping behavior. This information is used to

◆ destroy()

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

Destroys the Vulkan resources associated with this GPUTexture, including the image, image view, and sampler.

Parameters
deviceThe Vulkan logical device used to destroy the associated resources. This parameter is required to ensure that the correct device context is used for resource cleanup, preventing potential issues with resource management and ensuring proper deallocation of Vulkan objects.
Note
This method should be called when the GPUTexture is no longer needed to free up Vulkan resources and prevent memory leaks. It is important to ensure that the device parameter corresponds to the same logical device used to create the Vulkan resources associated with this GPUTexture. Failure to call this method or providing an incorrect device context may result in resource leaks or undefined behavior in the Vulkan application.

◆ getMipLevels()

uint32_t evan::GPUTexture::getMipLevels ( ) const
inline

Retrieves the number of mipmap levels generated for this texture.

Returns
The number of mipmap levels.

Definition at line 282 of file GPUTexture.hpp.

Member Data Documentation

◆ _deviceContext

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

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

Definition at line 416 of file GPUTexture.hpp.

◆ _image

VkImage evan::GPUTexture::_image = VK_NULL_HANDLE
protected

The Vulkan image associated with this GPUTexture, which holds the texture data in GPU memory for use in rendering operations.

Definition at line 392 of file GPUTexture.hpp.

◆ _memory

VkDeviceMemory evan::GPUTexture::_memory = VK_NULL_HANDLE
protected

The Vulkan device memory allocated for the image associated with this GPUTexture, which is used to store the texture data in GPU memory.

Definition at line 399 of file GPUTexture.hpp.

◆ _mipLevel

uint32_t evan::GPUTexture::_mipLevel = 1
protected

The number of mipmap levels generated for the Vulkan image associated with this GPUTexture, which is used to determine the level of detail for texture sampling in shader programs. This property is important for optimizing rendering performance and visual quality by allowing shaders to sample from different mipmap levels based on the distance and angle of the textured surface relative to the camera.

Definition at line 409 of file GPUTexture.hpp.

◆ sampler

VkSampler evan::GPUTexture::sampler = VK_NULL_HANDLE

The Vulkan sampler associated with this GPUTexture, which defines how the texture is sampled in shader programs.

Definition at line 296 of file GPUTexture.hpp.

◆ type

TextureType evan::GPUTexture::type

The type of texture represented by this GPUTexture instance, such as albedo, normal, or roughness. This property is used to categorize the texture based on its intended use in the rendering pipeline, allowing for organized management of texture resources and their bindings in shader programs.

Definition at line 305 of file GPUTexture.hpp.

◆ view

VkImageView evan::GPUTexture::view = VK_NULL_HANDLE

The Vulkan image view associated with this GPUTexture.

Definition at line 290 of file GPUTexture.hpp.


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