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.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vulkan/vulkan.h>
4#include <string>
5#include <unordered_map>
6#include <memory>
11
12class VulkanRenderer;
13
18struct Texture {
20 VkImage image = VK_NULL_HANDLE;
22 VkDeviceMemory memory = VK_NULL_HANDLE;
24 VkImageView imageView = VK_NULL_HANDLE;
26 VkSampler sampler = VK_NULL_HANDLE;
28 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
30 VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
32 int width = 0;
33
35 int height = 0;
37 std::string path;
40};
41
47public:
51 ResourceManager() = default;
55 ~ResourceManager() = default;
56
64 Texture* loadTexture(const std::string& path, VulkanRenderer& renderer, TextureFilterMode filterMode = TextureFilterMode::Bilinear);
65
72 Mesh loadMesh(const std::string& path, VulkanRenderer& renderer, int primitiveIndex = -1);
73
78 void clearMeshCache(const std::string& path);
79
85 int getMeshPrimitiveCount(const std::string& path);
86
94 bool loadSkeletonAndAnimations(const std::string& path, SkeletonComponent& skeleton, AnimatorComponent& animator, bool append = false);
95
104 bool loadBinarySkeletonAndAnimations(const std::string& path, SkeletonComponent& skeleton, AnimatorComponent& animator, bool append = false);
105
113 bool saveBinarySkeletonAndAnimations(const std::string& path, const SkeletonComponent& skeleton, const AnimatorComponent& animator);
114
121 void updateTextureFilterMode(const std::string& path, VulkanRenderer& renderer, TextureFilterMode filterMode);
122
128
136
141
153 Texture* createTextureFromPixels(const std::string& cacheKey,
154 const uint8_t* pixels, int width, int height,
155 VulkanRenderer& renderer,
157
163 void evictTexture(const std::string& cacheKey, VkDevice device);
164
169 void cleanup(VkDevice device);
170
171
175 uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties);
176
177private:
179 std::unordered_map<std::string, std::unique_ptr<Texture>> textureCache;
181 std::unordered_map<std::string, Mesh> meshCache;
186
187private:
191 void createTextureImage(const std::string& path, VulkanRenderer& renderer, Texture& texture);
195 void createTextureImageView(VkDevice device, Texture& texture);
199 void createTextureSampler(VkDevice device, Texture& texture, TextureFilterMode filterMode);
200};
TextureFilterMode
Definition Material.hpp:6
@ Nearest
Definition Material.hpp:7
@ Bilinear
Definition Material.hpp:8
void createDefaultTextures(VulkanRenderer &renderer)
Initializes default 1x1 textures (white, normal, metallic) for fallbacks.
Definition ResourceManager.cpp:350
Texture defaultMetallicTexture
Definition ResourceManager.hpp:185
void evictTexture(const std::string &cacheKey, VkDevice device)
Removes a texture from the cache and frees its GPU memory.
Definition ResourceManager.cpp:1863
void clearMeshCache(const std::string &path)
Clears the cached mesh for a specific path to force a re-import.
Definition ResourceManager.cpp:892
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.
Definition ResourceManager.cpp:253
uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties)
Finds a memory type matching properties in the physical GPU.
Definition ResourceManager.cpp:142
std::unordered_map< std::string, Mesh > meshCache
Cache of loaded meshes mapped by file path.
Definition ResourceManager.hpp:181
ResourceManager()=default
Construct a new Resource Manager object.
Texture defaultWhiteTexture
Fallback default 1x1 texture.
Definition ResourceManager.hpp:183
void createTextureSampler(VkDevice device, Texture &texture, TextureFilterMode filterMode)
Helper to configure texture samplers with the specified filter mode.
Definition ResourceManager.cpp:172
Texture * getDefaultWhiteTexture()
Retrieves the default fallback white texture pointer.
Definition ResourceManager.hpp:133
Texture * getDefaultNormalTexture()
Definition ResourceManager.hpp:134
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.
Definition ResourceManager.cpp:903
std::unordered_map< std::string, std::unique_ptr< Texture > > textureCache
Cache of loaded textures mapped by file path.
Definition ResourceManager.hpp:179
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).
Definition ResourceManager.cpp:1580
~ResourceManager()=default
Destroy the Resource Manager object.
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::buildAtl...
Definition ResourceManager.cpp:1793
bool saveBinarySkeletonAndAnimations(const std::string &path, const SkeletonComponent &skeleton, const AnimatorComponent &animator)
Saves skeletal skin and animations to a custom binary animation file (.anim).
Definition ResourceManager.cpp:1474
Texture * getDefaultMetallicTexture()
Definition ResourceManager.hpp:135
void updateTextureFilterMode(const std::string &path, VulkanRenderer &renderer, TextureFilterMode filterMode)
Updates an already loaded texture's filter settings live and recreates its Vulkan sampler.
Definition ResourceManager.cpp:1767
int getMeshPrimitiveCount(const std::string &path)
Scans a glTF file and returns the count of primitive submesh parts.
Definition ResourceManager.cpp:992
bool loadSkeletonAndAnimations(const std::string &path, SkeletonComponent &skeleton, AnimatorComponent &animator, bool append=false)
Loads skeletal skin and animations from a glTF file.
Definition ResourceManager.cpp:1091
Texture defaultNormalTexture
Definition ResourceManager.hpp:184
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.
Definition ResourceManager.cpp:208
void updateMaterialDescriptorSet(Material &mat, VulkanRenderer &renderer)
Dynamically updates or allocates a descriptor set for a Material, binding its diffuse,...
Definition ResourceManager.cpp:489
void cleanup(VkDevice device)
Safely frees all cached textures and meshes from GPU memory.
Definition ResourceManager.cpp:1028
void createTextureImageView(VkDevice device, Texture &texture)
Helper to create standard 2D image views.
Definition ResourceManager.cpp:154
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
Animator controller managing active clips and tracking playback timing.
Definition Animator.hpp:84
Represents a material component defining rendering properties.
Definition Material.hpp:17
Represents a mesh component that holds geometry data and its Vulkan buffers.
Definition Mesh.hpp:95
Component managing the list of joints and generating the offset matrices palette.
Definition Skeleton.hpp:37
Holds Vulkan handles and metadata for a loaded texture resource.
Definition ResourceManager.hpp:18
int width
Width in pixels.
Definition ResourceManager.hpp:32
VkSampler sampler
Sampler specifying filtering and wrapping modes.
Definition ResourceManager.hpp:26
VkDescriptorSet descriptorSet
Descriptor set updated for shader binding.
Definition ResourceManager.hpp:28
VkImageView imageView
View to read image data in shaders.
Definition ResourceManager.hpp:24
VkFormat format
Vulkan image format (e.g. UNORM or SRGB).
Definition ResourceManager.hpp:30
std::string path
Filepath from which texture was loaded.
Definition ResourceManager.hpp:37
VkImage image
Raw Vulkan image object.
Definition ResourceManager.hpp:20
int height
Height in pixels.
Definition ResourceManager.hpp:35
VkDeviceMemory memory
Allocated device memory backing the image.
Definition ResourceManager.hpp:22
TextureFilterMode filterMode
Filter mode used for this texture.
Definition ResourceManager.hpp:39