VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::VulkanResources Class Reference

This class manages resources like textures, descriptor sets, and uniform buffers. More...

#include <Resources.hpp>

Public Member Functions

 VulkanResources (VulkanContext &context, VirtualFileSystem *vfs)
 Simple constructor.
 ~VulkanResources ()
 Simple destructor.
void createUniformBuffers ()
 Creates uniform buffers.
void updateSceneUBO (const SceneUBO &data)
 Updates the scene uniform buffer.
void updateLightUBO (uint32_t frameIndex, uint32_t modelIndex, const SceneLightsUBO &data)
 Updates the light uniform buffer for a specific model.
void createDefaultTexture ()
 Creates a default texture.
void updateTextureDescriptor (uint32_t frameIndex, VkImageView textureView, uint32_t textureIndex)
 Updates the texture descriptor.
const std::string & getDefaultTextureName () const
 Returns the default texture name.
VkDescriptorSet getUBODescriptorSet (uint32_t frameIndex) const
 Returns the uniform buffer descriptor set for the given frame index.
bool loadTexture (const std::string &path, const std::string &name)
 Loads a texture from a file.
void loadTexturesBatched (const std::vector< std::string > &paths)
 Loads multiple textures in batches.
void unloadTexture (const std::string &name)
 Unloads a texture.
VkImageView getTextureView (const std::string &name) const
 Returns the texture view for the given texture name.
void createTextureFromRaw (const std::vector< unsigned char > &rgba, int w, int h, const std::string &name)
 Creates a texture from raw data.
VkDescriptorSet getDescriptorSet (uint32_t frameIndex) const
 Returns the descriptor set for the given frame index.
VkDescriptorSet * getDescriptorSetPtr (uint32_t frameIndex)
 Returns a pointer to the descriptor set for the given frame index.
VkDescriptorSet getTextureDescriptorSet (uint32_t frameIndex, uint32_t textureIndex) const
 Returns a descriptor set for the given frame index and texture index.
VkDescriptorSetLayout getDescriptorLayout () const
 Returns a descriptor set layout.
bool textureExists (const std::string &name) const
 Returns if a texture with the given name exists.
uint32_t getTextureIndex (const std::string &name)
 Returns the index of a texture with the given name.
VkSampler getTextureSampler () const
 Returns the texture sampler.
VkDescriptorSet getBindlessDescriptorSet () const
 Returns the bindless descriptor set.
VkDescriptorSetLayout getBindlessLayout () const
 Returns the bindless descriptor set layout.
VkDescriptorSet getParticleDescriptorSet (uint32_t frameIndex) const
 Returns the particle descriptor set for the given frame index.
void * getParticleMappedData (uint32_t frameIndex) const
 Returns the mapped data for the particle SSBO for the given frame index.
void createColorLUT ()
 Generates and uploads a 32x32x32 3D Lookup Texture (LUT) to the GPU. Bakes PS1 5-bit color quantization, saturation, and brightness boost into a 3D texture. The data is transferred via a staging buffer to a VK_IMAGE_TYPE_3D image stored in the VulkanContext. This allows the composite shader to replace heavy color math with a single hardware-accelerated trilinear texture fetch.

Private Member Functions

void createDescriptorResources ()
 Internal function to create descriptor resources.
void createTextureSampler ()
 Internal function to create texture sampler.
void createPerMeshTextureSets ()
 Internal function to create per-mesh texture sets.
void createParticleSSBOs ()
 Internal function to create particle SSBOs.

Private Attributes

const std::string defaultTextureName = "default"
VulkanContextm_r_context
VirtualFileSystemm_vfs
std::vector< VkBuffer > m_sceneBuffers
std::vector< VkBuffer > m_lightBuffers
std::vector< VmaAllocation > m_sceneAllocs
std::vector< VmaAllocation > m_lightAllocs
std::vector< VkBuffer > m_particleSSBOs
std::vector< VmaAllocation > m_particleAllocs
std::vector< void * > m_particleMappedData
VkDescriptorPool m_particleDescriptorPool = VK_NULL_HANDLE
std::vector< VkDescriptorSet > m_particleDescriptorSets
VkDescriptorSetLayout m_descriptorSetLayout
std::vector< VkDescriptorSet > m_descriptorSets
VkDescriptorPool m_descriptorPool
std::vector< VkDescriptorSet > m_textureDescriptorSets
vex_map< std::string, VkImageView > m_textures
VkSampler m_textureSampler
vex_map< std::string, VkImage > m_textureImages
vex_map< std::string, VmaAllocation > m_textureAllocations
vex_map< std::string, VkImageView > m_textureViews
std::vector< std::string > m_ignoredTexturePaths

Detailed Description

This class manages resources like textures, descriptor sets, and uniform buffers.

Definition at line 23 of file Resources.hpp.

Constructor & Destructor Documentation

◆ VulkanResources()

vex::VulkanResources::VulkanResources ( VulkanContext & context,
VirtualFileSystem * vfs )

Simple constructor.

Initializes the resource manager: creates default textures, samplers, UBOs, and descriptor pools.

Parameters
VulkanContext&context - The Vulkan context to use.
VirtualFileSystem*vfs - VFS for loading texture files.

Definition at line 27 of file Resources.cpp.

◆ ~VulkanResources()

vex::VulkanResources::~VulkanResources ( )

Simple destructor.

Destroys all textures, buffers, samplers, and descriptor pools.

Definition at line 36 of file Resources.cpp.

Member Function Documentation

◆ createColorLUT()

void vex::VulkanResources::createColorLUT ( )

Generates and uploads a 32x32x32 3D Lookup Texture (LUT) to the GPU. Bakes PS1 5-bit color quantization, saturation, and brightness boost into a 3D texture. The data is transferred via a staging buffer to a VK_IMAGE_TYPE_3D image stored in the VulkanContext. This allows the composite shader to replace heavy color math with a single hardware-accelerated trilinear texture fetch.

Definition at line 125 of file Resources.cpp.

◆ createDefaultTexture()

void vex::VulkanResources::createDefaultTexture ( )

Creates a default texture.

Generates a 1x1 white pixel texture, transitions it to SHADER_READ_ONLY, and assigns it to the "default" key.

Definition at line 802 of file Resources.cpp.

◆ createDescriptorResources()

void vex::VulkanResources::createDescriptorResources ( )
private

Internal function to create descriptor resources.

Definition at line 525 of file Resources.cpp.

◆ createParticleSSBOs()

void vex::VulkanResources::createParticleSSBOs ( )
private

Internal function to create particle SSBOs.

Definition at line 456 of file Resources.cpp.

◆ createPerMeshTextureSets()

void vex::VulkanResources::createPerMeshTextureSets ( )
private

Internal function to create per-mesh texture sets.

Definition at line 882 of file Resources.cpp.

◆ createTextureFromRaw()

void vex::VulkanResources::createTextureFromRaw ( const std::vector< unsigned char > & rgba,
int w,
int h,
const std::string & name )

Creates a texture from raw data.

Creates a GPU texture from a vector of bytes (RGBA). Useful for generated content like UI fonts.

Parameters
conststd::vector<unsigned char>& rgba - Raw pixel data.
intw - Width.
inth - Height.
conststd::string& name - Identifier.

Definition at line 271 of file Resources.cpp.

◆ createTextureSampler()

void vex::VulkanResources::createTextureSampler ( )
private

Internal function to create texture sampler.

Definition at line 937 of file Resources.cpp.

◆ createUniformBuffers()

void vex::VulkanResources::createUniformBuffers ( )

Creates uniform buffers.

Allocates Scene and Light UBOs for each frame in flight using VMA.

Definition at line 431 of file Resources.cpp.

◆ getBindlessDescriptorSet()

VkDescriptorSet vex::VulkanResources::getBindlessDescriptorSet ( ) const
inline

Returns the bindless descriptor set.

Returns
VkDescriptorSet - The bindless descriptor set.

Definition at line 157 of file Resources.hpp.

◆ getBindlessLayout()

VkDescriptorSetLayout vex::VulkanResources::getBindlessLayout ( ) const
inline

Returns the bindless descriptor set layout.

Returns
VkDescriptorSetLayout - The bindless descriptor set layout.

Definition at line 161 of file Resources.hpp.

◆ getDefaultTextureName()

const std::string & vex::VulkanResources::getDefaultTextureName ( ) const
inline

Returns the default texture name.

Returns
std::string&

Definition at line 64 of file Resources.hpp.

◆ getDescriptorLayout()

VkDescriptorSetLayout vex::VulkanResources::getDescriptorLayout ( ) const
inline

Returns a descriptor set layout.

Returns
VkDescriptorSetLayout - The descriptor set layout.

Definition at line 136 of file Resources.hpp.

◆ getDescriptorSet()

VkDescriptorSet vex::VulkanResources::getDescriptorSet ( uint32_t frameIndex) const
inline

Returns the descriptor set for the given frame index.

Parameters
uint32_tframeIndex - The index of the frame.
Returns
VkDescriptorSet - The descriptor set.

Definition at line 105 of file Resources.hpp.

◆ getDescriptorSetPtr()

VkDescriptorSet * vex::VulkanResources::getDescriptorSetPtr ( uint32_t frameIndex)
inline

Returns a pointer to the descriptor set for the given frame index.

Parameters
uint32_tframeIndex - The index of the frame.
Returns
VkDescriptorSet* - The pointer to the descriptor set, or nullptr on invalid index.

Definition at line 118 of file Resources.hpp.

◆ getParticleDescriptorSet()

VkDescriptorSet vex::VulkanResources::getParticleDescriptorSet ( uint32_t frameIndex) const
inline

Returns the particle descriptor set for the given frame index.

Parameters
frameIndex- The frame index.
Returns
VkDescriptorSet - The particle descriptor set, or VK_NULL_HANDLE on invalid index.

Definition at line 166 of file Resources.hpp.

◆ getParticleMappedData()

void * vex::VulkanResources::getParticleMappedData ( uint32_t frameIndex) const
inline

Returns the mapped data for the particle SSBO for the given frame index.

Parameters
frameIndex- The frame index.
Returns
void* - The mapped data, or nullptr on invalid index.

Definition at line 179 of file Resources.hpp.

◆ getTextureDescriptorSet()

VkDescriptorSet vex::VulkanResources::getTextureDescriptorSet ( uint32_t frameIndex,
uint32_t textureIndex ) const

Returns a descriptor set for the given frame index and texture index.

Parameters
uint32_tframeIndex - Frame index.
uint32_ttextureIndex - Texture slot index.
Returns
VkDescriptorSet - The specific descriptor set for this texture/frame combo (used in non-bindless mode).

Definition at line 926 of file Resources.cpp.

◆ getTextureIndex()

uint32_t vex::VulkanResources::getTextureIndex ( const std::string & name)

Returns the index of a texture with the given name.

If the texture isn't loaded, attempts to load it from disk using the name as the path.

Parameters
conststd::string& name - Texture path/name.
Returns
uint32_t - The texture index, or 0 (default) if loading fails.

Definition at line 957 of file Resources.cpp.

◆ getTextureSampler()

VkSampler vex::VulkanResources::getTextureSampler ( ) const
inline

Returns the texture sampler.

Returns
VkSampler - The texture sampler.

Definition at line 153 of file Resources.hpp.

◆ getTextureView()

VkImageView vex::VulkanResources::getTextureView ( const std::string & name) const

Returns the texture view for the given texture name.

Parameters
conststd::string& name - The texture identifier.
Returns
VkImageView - The view, or the default texture view if not found.

Definition at line 1425 of file Resources.cpp.

◆ getUBODescriptorSet()

VkDescriptorSet vex::VulkanResources::getUBODescriptorSet ( uint32_t frameIndex) const

Returns the uniform buffer descriptor set for the given frame index.

Parameters
uint32_tframeIndex - The frame index.
Returns
VkDescriptorSet - The descriptor set binding UBOs.

Definition at line 705 of file Resources.cpp.

◆ loadTexture()

bool vex::VulkanResources::loadTexture ( const std::string & path,
const std::string & name )

Loads a texture from a file.

Uses stbi_load_from_memory via VFS. Creates Vulkan Image, View, and uploads data via staging buffer. Recycles texture indices if available.

Parameters
conststd::string& path - File path.
conststd::string& name - Unique identifier key.
Returns
bool - True if loaded successfully or already exists.

Definition at line 971 of file Resources.cpp.

◆ loadTexturesBatched()

void vex::VulkanResources::loadTexturesBatched ( const std::vector< std::string > & paths)

Loads multiple textures in batches.

Parameters
conststd::vector<std::string>& paths - List of file paths to load.

Definition at line 1204 of file Resources.cpp.

◆ textureExists()

bool vex::VulkanResources::textureExists ( const std::string & name) const
inline

Returns if a texture with the given name exists.

Parameters
conststd::string& name - The name of the texture.
Returns
bool - True if the texture exists, false otherwise.

Definition at line 141 of file Resources.hpp.

◆ unloadTexture()

void vex::VulkanResources::unloadTexture ( const std::string & name)

Unloads a texture.

Destroys the image/view and pushes the index to the recycled queue. Resets descriptors to the default texture.

Parameters
conststd::string& name - Identifier of the texture to unload.

Definition at line 1359 of file Resources.cpp.

◆ updateLightUBO()

void vex::VulkanResources::updateLightUBO ( uint32_t frameIndex,
uint32_t modelIndex,
const SceneLightsUBO & data )

Updates the light uniform buffer for a specific model.

Updates the dynamic UBO section corresponding to a specific model index.

Parameters
uint32_tframeIndex - Current frame.
uint32_tmodelIndex - Index of the model to update lights for.
constSceneLightsUBO& data - Light data.

Definition at line 773 of file Resources.cpp.

◆ updateSceneUBO()

void vex::VulkanResources::updateSceneUBO ( const SceneUBO & data)

Updates the scene uniform buffer.

Maps memory and copies the SceneUBO struct (camera, lighting, time data) to the GPU.

Parameters
constSceneUBO& data - The data to update.

Definition at line 752 of file Resources.cpp.

◆ updateTextureDescriptor()

void vex::VulkanResources::updateTextureDescriptor ( uint32_t frameIndex,
VkImageView textureView,
uint32_t textureIndex )

Updates the texture descriptor.

Updates the global descriptor set (or Bindless set) to point to the new texture view.

Parameters
uint32_tframeIndex - Frame index.
VkImageViewtextureView - The new view.
uint32_ttextureIndex - The slot index to update.

Definition at line 710 of file Resources.cpp.

Member Data Documentation

◆ defaultTextureName

const std::string vex::VulkanResources::defaultTextureName = "default"
private

Definition at line 196 of file Resources.hpp.

◆ m_descriptorPool

VkDescriptorPool vex::VulkanResources::m_descriptorPool
private

Definition at line 214 of file Resources.hpp.

◆ m_descriptorSetLayout

VkDescriptorSetLayout vex::VulkanResources::m_descriptorSetLayout
private

Definition at line 212 of file Resources.hpp.

◆ m_descriptorSets

std::vector<VkDescriptorSet> vex::VulkanResources::m_descriptorSets
private

Definition at line 213 of file Resources.hpp.

◆ m_ignoredTexturePaths

std::vector<std::string> vex::VulkanResources::m_ignoredTexturePaths
private

Definition at line 224 of file Resources.hpp.

◆ m_lightAllocs

std::vector<VmaAllocation> vex::VulkanResources::m_lightAllocs
private

Definition at line 204 of file Resources.hpp.

◆ m_lightBuffers

std::vector<VkBuffer> vex::VulkanResources::m_lightBuffers
private

Definition at line 202 of file Resources.hpp.

◆ m_particleAllocs

std::vector<VmaAllocation> vex::VulkanResources::m_particleAllocs
private

Definition at line 207 of file Resources.hpp.

◆ m_particleDescriptorPool

VkDescriptorPool vex::VulkanResources::m_particleDescriptorPool = VK_NULL_HANDLE
private

Definition at line 209 of file Resources.hpp.

◆ m_particleDescriptorSets

std::vector<VkDescriptorSet> vex::VulkanResources::m_particleDescriptorSets
private

Definition at line 210 of file Resources.hpp.

◆ m_particleMappedData

std::vector<void*> vex::VulkanResources::m_particleMappedData
private

Definition at line 208 of file Resources.hpp.

◆ m_particleSSBOs

std::vector<VkBuffer> vex::VulkanResources::m_particleSSBOs
private

Definition at line 206 of file Resources.hpp.

◆ m_r_context

VulkanContext& vex::VulkanResources::m_r_context
private

Definition at line 198 of file Resources.hpp.

◆ m_sceneAllocs

std::vector<VmaAllocation> vex::VulkanResources::m_sceneAllocs
private

Definition at line 203 of file Resources.hpp.

◆ m_sceneBuffers

std::vector<VkBuffer> vex::VulkanResources::m_sceneBuffers
private

Definition at line 201 of file Resources.hpp.

◆ m_textureAllocations

vex_map<std::string, VmaAllocation> vex::VulkanResources::m_textureAllocations
private

Definition at line 221 of file Resources.hpp.

◆ m_textureDescriptorSets

std::vector<VkDescriptorSet> vex::VulkanResources::m_textureDescriptorSets
private

Definition at line 215 of file Resources.hpp.

◆ m_textureImages

vex_map<std::string, VkImage> vex::VulkanResources::m_textureImages
private

Definition at line 220 of file Resources.hpp.

◆ m_textures

vex_map<std::string, VkImageView> vex::VulkanResources::m_textures
private

Definition at line 217 of file Resources.hpp.

◆ m_textureSampler

VkSampler vex::VulkanResources::m_textureSampler
private

Definition at line 218 of file Resources.hpp.

◆ m_textureViews

vex_map<std::string, VkImageView> vex::VulkanResources::m_textureViews
private

Definition at line 222 of file Resources.hpp.

◆ m_vfs

VirtualFileSystem* vex::VulkanResources::m_vfs
private

Definition at line 199 of file Resources.hpp.


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