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