Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
ResourceManager Class Reference

Manages loading, caching, and GPU memory uploads for textures and glTF model meshes. More...

#include <ResourceManager.hpp>

Public Member Functions

 ResourceManager ()=default
 Construct a new Resource Manager object.
 ~ResourceManager ()=default
 Destroy the Resource Manager object.
Texture * loadTexture (const std::string &path, VulkanRenderer &renderer, TextureFilterMode filterMode=TextureFilterMode::Bilinear)
 Loads a texture from file, uploading it to the GPU and caching the resource.
Mesh loadMesh (const std::string &path, VulkanRenderer &renderer, int primitiveIndex=-1)
 Loads a glTF mesh from file, uploading geometry buffers to the GPU and caching it.
void clearMeshCache (const std::string &path)
 Clears the cached mesh for a specific path to force a re-import.
int getMeshPrimitiveCount (const std::string &path)
 Scans a glTF file and returns the count of primitive submesh parts.
bool loadSkeletonAndAnimations (const std::string &path, SkeletonComponent &skeleton, AnimatorComponent &animator, bool append=false)
 Loads skeletal skin and animations from a glTF file.
bool loadBinarySkeletonAndAnimations (const std::string &path, SkeletonComponent &skeleton, AnimatorComponent &animator, bool append=false)
 Loads skeletal skin and animations from a custom binary animation file (.anim).
bool saveBinarySkeletonAndAnimations (const std::string &path, const SkeletonComponent &skeleton, const AnimatorComponent &animator)
 Saves skeletal skin and animations to a custom binary animation file (.anim).
void updateTextureFilterMode (const std::string &path, VulkanRenderer &renderer, TextureFilterMode filterMode)
 Updates an already loaded texture's filter settings live and recreates its Vulkan sampler.
void createDefaultTextures (VulkanRenderer &renderer)
 Initializes default 1x1 textures (white, normal, metallic) for fallbacks.
Texture * getDefaultWhiteTexture ()
 Retrieves the default fallback white texture pointer.
Texture * getDefaultNormalTexture ()
Texture * getDefaultMetallicTexture ()
void updateMaterialDescriptorSet (Material &mat, VulkanRenderer &renderer)
 Dynamically updates or allocates a descriptor set for a Material, binding its diffuse, normal, and metallic maps.
Texture * createTextureFromPixels (const std::string &cacheKey, const uint8_t *pixels, int width, int height, VulkanRenderer &renderer, TextureFilterMode filterMode=TextureFilterMode::Nearest)
 Creates and caches a Vulkan texture directly from raw RGBA pixel data. Used by TilesetAsset::buildAtlas() to upload the packed tile atlas.
void evictTexture (const std::string &cacheKey, VkDevice device)
 Removes a texture from the cache and frees its GPU memory.
void cleanup (VkDevice device)
 Safely frees all cached textures and meshes from GPU memory.
uint32_t findMemoryType (VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties)
 Finds a memory type matching properties in the physical GPU.

Private Member Functions

void createTextureImage (const std::string &path, VulkanRenderer &renderer, Texture &texture)
 Helper to load pixel data, create a VkImage, and copy staging data onto the GPU.
void createTextureImageView (VkDevice device, Texture &texture)
 Helper to create standard 2D image views.
void createTextureSampler (VkDevice device, Texture &texture, TextureFilterMode filterMode)
 Helper to configure texture samplers with the specified filter mode.

Private Attributes

std::unordered_map< std::string, std::unique_ptr< Texture > > textureCache
 Cache of loaded textures mapped by file path.
std::unordered_map< std::string, Mesh > meshCache
 Cache of loaded meshes mapped by file path.
Texture defaultWhiteTexture
 Fallback default 1x1 texture.
Texture defaultNormalTexture
Texture defaultMetallicTexture

Detailed Description

Manages loading, caching, and GPU memory uploads for textures and glTF model meshes.

Constructor & Destructor Documentation

◆ ResourceManager()

ResourceManager::ResourceManager ( )
default

Construct a new Resource Manager object.

◆ ~ResourceManager()

ResourceManager::~ResourceManager ( )
default

Destroy the Resource Manager object.

Member Function Documentation

◆ cleanup()

void ResourceManager::cleanup ( VkDevice device)

Safely frees all cached textures and meshes from GPU memory.

Parameters
deviceVulkan logical device context.

◆ clearMeshCache()

void ResourceManager::clearMeshCache ( const std::string & path)

Clears the cached mesh for a specific path to force a re-import.

Parameters
pathThe file path.

◆ createDefaultTextures()

void ResourceManager::createDefaultTextures ( VulkanRenderer & renderer)

Initializes default 1x1 textures (white, normal, metallic) for fallbacks.

Parameters
rendererReference to active VulkanRenderer.

◆ createTextureFromPixels()

Texture * ResourceManager::createTextureFromPixels ( const std::string & cacheKey,
const uint8_t * pixels,
int width,
int height,
VulkanRenderer & renderer,
TextureFilterMode filterMode = TextureFilterMode::Nearest )

Creates and caches a Vulkan texture directly from raw RGBA pixel data. Used by TilesetAsset::buildAtlas() to upload the packed tile atlas.

Parameters
cacheKeyUnique string key used for the texture cache (e.g. "tileset:forest").
pixelsPointer to RGBA8 pixel data (4 bytes per pixel, row-major).
widthWidth in pixels.
heightHeight in pixels.
rendererActive VulkanRenderer.
filterModeDesired sampler filter mode.
Returns
Pointer to the uploaded Texture, or nullptr on failure.

◆ createTextureImage()

void ResourceManager::createTextureImage ( const std::string & path,
VulkanRenderer & renderer,
Texture & texture )
private

Helper to load pixel data, create a VkImage, and copy staging data onto the GPU.

◆ createTextureImageView()

void ResourceManager::createTextureImageView ( VkDevice device,
Texture & texture )
private

Helper to create standard 2D image views.

◆ createTextureSampler()

void ResourceManager::createTextureSampler ( VkDevice device,
Texture & texture,
TextureFilterMode filterMode )
private

Helper to configure texture samplers with the specified filter mode.

◆ evictTexture()

void ResourceManager::evictTexture ( const std::string & cacheKey,
VkDevice device )

Removes a texture from the cache and frees its GPU memory.

Parameters
cacheKeyThe key used when the texture was created.
deviceVulkan logical device.

◆ findMemoryType()

uint32_t ResourceManager::findMemoryType ( VkPhysicalDevice physicalDevice,
uint32_t typeFilter,
VkMemoryPropertyFlags properties )

Finds a memory type matching properties in the physical GPU.

◆ getDefaultMetallicTexture()

Texture * ResourceManager::getDefaultMetallicTexture ( )
inline

◆ getDefaultNormalTexture()

Texture * ResourceManager::getDefaultNormalTexture ( )
inline

◆ getDefaultWhiteTexture()

Texture * ResourceManager::getDefaultWhiteTexture ( )
inline

Retrieves the default fallback white texture pointer.

Returns
Pointer to default Texture.

◆ getMeshPrimitiveCount()

int ResourceManager::getMeshPrimitiveCount ( const std::string & path)

Scans a glTF file and returns the count of primitive submesh parts.

Parameters
pathThe glTF file path.
Returns
Number of primitive submesh parts.

◆ loadBinarySkeletonAndAnimations()

bool ResourceManager::loadBinarySkeletonAndAnimations ( const std::string & path,
SkeletonComponent & skeleton,
AnimatorComponent & animator,
bool append = false )

Loads skeletal skin and animations from a custom binary animation file (.anim).

Parameters
pathFile path of the binary animation.
skeletonTarget SkeletonComponent to populate.
animatorTarget AnimatorComponent to populate.
appendIf true, animation clips are appended instead of clearing the animator's cache.
Returns
True if loaded successfully.

◆ loadMesh()

Mesh ResourceManager::loadMesh ( const std::string & path,
VulkanRenderer & renderer,
int primitiveIndex = -1 )

Loads a glTF mesh from file, uploading geometry buffers to the GPU and caching it.

Parameters
pathThe glTF/glb file path.
rendererReference to active VulkanRenderer.
Returns
The loaded Mesh component.

◆ loadSkeletonAndAnimations()

bool ResourceManager::loadSkeletonAndAnimations ( const std::string & path,
SkeletonComponent & skeleton,
AnimatorComponent & animator,
bool append = false )

Loads skeletal skin and animations from a glTF file.

Parameters
pathThe glTF/glb file path.
skeletonTarget SkeletonComponent to populate.
animatorTarget AnimatorComponent to populate.
Returns
True if skin/animations were loaded successfully.

◆ loadTexture()

Texture * ResourceManager::loadTexture ( const std::string & path,
VulkanRenderer & renderer,
TextureFilterMode filterMode = TextureFilterMode::Bilinear )

Loads a texture from file, uploading it to the GPU and caching the resource.

Parameters
pathThe texture file path.
rendererReference to active VulkanRenderer.
filterModeDesired filtering mode (will be overridden by .meta if exists).
Returns
Pointer to the loaded Texture object, or nullptr if failed.

◆ saveBinarySkeletonAndAnimations()

bool ResourceManager::saveBinarySkeletonAndAnimations ( const std::string & path,
const SkeletonComponent & skeleton,
const AnimatorComponent & animator )

Saves skeletal skin and animations to a custom binary animation file (.anim).

Parameters
pathFile path to save the binary animation.
skeletonSource SkeletonComponent.
animatorSource AnimatorComponent.
Returns
True if saved successfully.

◆ updateMaterialDescriptorSet()

void ResourceManager::updateMaterialDescriptorSet ( Material & mat,
VulkanRenderer & renderer )

Dynamically updates or allocates a descriptor set for a Material, binding its diffuse, normal, and metallic maps.

◆ updateTextureFilterMode()

void ResourceManager::updateTextureFilterMode ( const std::string & path,
VulkanRenderer & renderer,
TextureFilterMode filterMode )

Updates an already loaded texture's filter settings live and recreates its Vulkan sampler.

Parameters
pathThe texture file path.
rendererReference to VulkanRenderer.
filterModeThe new texture filter mode to apply.

Member Data Documentation

◆ defaultMetallicTexture

Texture ResourceManager::defaultMetallicTexture
private

◆ defaultNormalTexture

Texture ResourceManager::defaultNormalTexture
private

◆ defaultWhiteTexture

Texture ResourceManager::defaultWhiteTexture
private

Fallback default 1x1 texture.

◆ meshCache

std::unordered_map<std::string, Mesh> ResourceManager::meshCache
private

Cache of loaded meshes mapped by file path.

◆ textureCache

std::unordered_map<std::string, std::unique_ptr<Texture> > ResourceManager::textureCache
private

Cache of loaded textures mapped by file path.


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