Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
model_cache.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
9#include <string>
10#include <unordered_map>
11#include <memory>
12#include <vector>
13
14#include <glm/glm.hpp>
15
16namespace rendering_engine
17{
18class IRenderer;
19class IMeshRenderResources;
20class MeshDataGpu;
21
22/**
23 * @brief Manages loading, caching, and GPU residency of all model/mesh resources.
24 *
25 * Responsible for importing model files into RAM, uploading mesh data to GPU memory,
26 * and managing the lifetime and statistics of all managed models in the rendering engine.
27 * Also observes renderer resource lifecycle events (release/rebuild).
28 */
30{
31public:
32 /**
33 * @brief Construct a ModelCache and register it with the given renderer.
34 * @param renderer Pointer to the owning IRenderer implementation.
35 */
36 ModelCache(IRenderer* renderer);
37
38 /**
39 * @brief Destructor. Releases all resources and unregisters observer.
40 */
42
43 /**
44 * @brief Load all models found in the specified folder into RAM.
45 * @param pathToFolder Path to the directory containing model files.
46 */
47 void LoadModelsFromFolder(std::string pathToFolder);
48
49 /**
50 * @brief Load all models from the packed asset container.
51 *
52 * This function behaves similarly to LoadModelsFromFolder(), but instead
53 * retrieves model files from the packed asset system created by the
54 * Packaging Tool. Each packed file is read into memory and processed via
55 * UploadModelToRAM(std::string, const std::vector<uint8_t>&).
56 *
57 * Only file entries located under the virtual folder:
58 * "Models/"
59 * are considered.
60 *
61 * The resulting MeshDataGpu objects remain cached and ready for GPU upload.
62 */
64
65 /**
66 * @brief Creates a built-in 2D quad mesh.
67 *
68 * This helper is used by 2D systems (e.g., UI, sprites) to create a reusable
69 * quad mesh without external model files.
70 */
71 void CreateQuad2D();
72
73 void LoadCustomMesh(std::string meshName,
74 std::vector<glm::vec2> positions2D,
75 std::vector<glm::vec2> texCoords,
76 std::vector<glm::vec4> colors,
77 std::vector<std::uint32_t> indices);
78
79 /**
80 * @brief Load a single model from file into RAM.
81 * @param path Path to the model file.
82 * @return The filename used as a cache key, or empty string on failure.
83 */
84 std::string UploadModelToRAM(std::string path);
85
86 /**
87 * @brief Load a single model into RAM from a raw file buffer.
88 *
89 * This overload is used when the model originates from a packed asset
90 * archive or any virtual filesystem source rather than the OS filesystem.
91 *
92 * @param fileName Cache key (typically relative virtual path, e.g. "Models/cube.fbx").
93 * @param fileBytes Raw contents of the model file.
94 * @return The cache key on success, or an empty string on failure.
95 */
96 std::string UploadModelToRAM(std::string fileName, std::vector<uint8_t> const& fileBytes);
97
98 /**
99 * @brief Upload a cached model's mesh data to the GPU.
100 * @param filename The model's cache key (filename without extension).
101 */
102 void UploadModelToGPU(std::string filename);
103
104 /**
105 * @brief Release a model's mesh data from GPU memory.
106 * @param filename The model's cache key (filename without extension).
107 */
108 void ReleaseModelFromGPU(std::string filename);
109
110 /**
111 * @brief Release all model mesh data from GPU memory.
112 */
113 void ReleaseAllFromGPU();
114
115 /**
116 * @brief Remove all models from both GPU and RAM, clearing the cache.
117 */
118 void ReleaseAll();
119
120 /**
121 * @brief Get a shared pointer to the MeshDataGpu for a model.
122 * @param filename The model's cache key (filename without extension).
123 * @return Shared pointer to MeshDataGpu, or nullptr if not found.
124 */
125 std::shared_ptr<MeshDataGpu> GetMeshResources(std::string filename);
126
127 /**
128 * @brief Get the IMeshRenderResources interface for a model's GPU resources.
129 * @param filename The model's cache key (filename without extension).
130 * @return Pointer to IMeshRenderResources, or nullptr if not found or not on GPU.
131 */
132 IMeshRenderResources* GetMeshRenderResources(std::string filename);
133
134 /**
135 * @brief Get the total size (in bytes) of all models loaded in RAM.
136 * @return Total RAM usage in bytes.
137 */
138 inline size_t GetSizeInRAM() const;
139
140 /**
141 * @brief Get the total size (in bytes) of all models currently resident on GPU.
142 * @return Total GPU usage in bytes.
143 */
144 inline size_t GetSizeInGPU() const;
145
146protected:
147 /**
148 * @copydoc IRendererObserver::OnRenderResourcesRelease
149 */
150 void OnRenderResourcesRelease() override;
151 /**
152 * @copydoc IRendererObserver::OnRenderResourcesRebuild
153 */
154 void OnRenderResourcesRebuild() override;
155
156protected:
158 std::unordered_map<std::string, std::shared_ptr<MeshDataGpu>> mModels;
159
162};
163
164
165
166} // namespace rendering_engine
Interface for GPU mesh resource management.
Interface for observing renderer resource lifecycle events.
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Manages loading, caching, and GPU residency of all model/mesh resources.
Definition: model_cache.hpp:30
void CreateQuad2D()
Creates a built-in 2D quad mesh.
Definition: model_cache.cpp:90
void ReleaseAll()
Remove all models from both GPU and RAM, clearing the cache.
void UploadModelToGPU(std::string filename)
Upload a cached model's mesh data to the GPU.
std::shared_ptr< MeshDataGpu > GetMeshResources(std::string filename)
Get a shared pointer to the MeshDataGpu for a model.
IMeshRenderResources * GetMeshRenderResources(std::string filename)
Get the IMeshRenderResources interface for a model's GPU resources.
std::unordered_map< std::string, std::shared_ptr< MeshDataGpu > > mModels
~ModelCache()
Destructor. Releases all resources and unregisters observer.
Definition: model_cache.cpp:23
size_t GetSizeInRAM() const
Get the total size (in bytes) of all models loaded in RAM.
void LoadModelsFromFolder(std::string pathToFolder)
Load all models found in the specified folder into RAM.
Definition: model_cache.cpp:29
size_t GetSizeInGPU() const
Get the total size (in bytes) of all models currently resident on GPU.
void OnRenderResourcesRebuild() override
Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).
std::string UploadModelToRAM(std::string path)
Load a single model from file into RAM.
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).
void LoadModelsFromPackage()
Load all models from the packed asset container.
Definition: model_cache.cpp:56
void ReleaseAllFromGPU()
Release all model mesh data from GPU memory.
ModelCache(IRenderer *renderer)
Construct a ModelCache and register it with the given renderer.
Definition: model_cache.cpp:13
void ReleaseModelFromGPU(std::string filename)
Release a model's mesh data from GPU memory.
void LoadCustomMesh(std::string meshName, std::vector< glm::vec2 > positions2D, std::vector< glm::vec2 > texCoords, std::vector< glm::vec4 > colors, std::vector< std::uint32_t > indices)