Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
texture_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
14namespace rendering_engine
15{
16class IRenderer;
17class ITextureRenderResources;
18class ImageDataGpu;
19class ImageData;
20
21/**
22 * @class TextureCache
23 * @brief Manages texture loading, GPU uploading, and caching for reuse.
24 *
25 * This class handles texture resource management at runtime.
26 * It supports loading image files (JPG and PNG) from a folder or path into RAM, uploading them to GPU,
27 * and provides access to the texture resources through string identifiers (filenames without extensions).
28 *
29 * Textures are stored using shared pointers to ImageDataGpu, and memory tracking is performed for both RAM and GPU sides.
30 */
32{
33public:
34 /**
35 * @brief Constructs the TextureCache with a reference to the renderer.
36 * @param renderer Pointer to the active rendering backend.
37 */
38 TextureCache(IRenderer* renderer);
40
41 /**
42 * @brief Loads all supported texture files (*.jpg, *.png) from a specified folder into RAM and uploads them to the GPU.
43 *
44 * Each texture is registered under its base filename (without extension) for later access.
45 * Invalid paths or unsupported file types are skipped silently.
46 *
47 * @param pathToFolder Absolute or relative path to the folder containing texture files.
48 */
49 void LoadTexturesFromFolder(std::string pathToFolder);
50
51 void LoadTexture(std::string textureName, ImageData imageData);
52
53 /**
54 * @brief Loads all textures from the packed asset archive.
55 *
56 * This function behaves similarly to LoadTexturesFromFolder(), but instead of
57 * scanning a filesystem directory, it iterates over virtual file entries
58 * stored inside the packed content system created by the packaging tool.
59 *
60 * Only entries located under the virtual folder:
61 * "Textures/"
62 * are considered valid texture resources.
63 *
64 * For each entry, the raw file bytes are retrieved via Utility::ReadPackedFile(),
65 * decoded into an ImageDataGpu instance, and stored in the RAM cache.
66 * GPU uploading must still be triggered via UploadTextureToGPU().
67 */
69
70 /**
71 * @brief Loads a single texture into RAM from the given file path.
72 *
73 * The texture will not be uploaded to GPU automatically. The caller must use UploadTextureToGPU()
74 * after this call if GPU usage is needed.
75 *
76 * @param path Full file path to the texture image (JPG/PNG).
77 * @return The base filename used as the cache key, or an empty string if loading failed.
78 */
79 std::string UploadTextureToRAM(std::string path);
80
81 /**
82 * @brief Loads a single texture into RAM from raw file bytes.
83 *
84 * This overload enables texture loading from virtual files stored in a
85 * packed asset archive or from any memory-based source rather than the OS
86 * filesystem.
87 *
88 * The fileBytes buffer must contain the complete encoded texture data
89 * (e.g., PNG or JPEG). The data is decoded and stored inside ImageDataGpu.
90 *
91 * @param textureFileName Cache key representing the virtual texture path
92 * (e.g., "Textures/myTexture.png" or simply "myTexture").
93 * @param fileBytes Raw encoded texture file data (PNG/JPG).
94 * @return The cache key on success, or an empty string on failure.
95 */
96 std::string UploadTextureToRAM(std::string textureFileName, std::vector<uint8_t> const& fileBytes);
97
98 std::string UploadTextureToRAM(std::string textureName, ImageData imageData);
99 /**
100 * @brief Uploads a texture (previously loaded into RAM) to GPU.
101 *
102 * If the texture is not found in RAM cache, the function does nothing.
103 *
104 * @param textureName Base texture name (no path, no extension) used as the cache key.
105 */
106 void UploadTextureToGPU(std::string textureName);
107
108 /**
109 * @brief Releases a texture from GPU memory.
110 *
111 * The texture remains in RAM cache and can be uploaded again later.
112 *
113 * @param filename Cache key of the texture to release (base filename).
114 */
115 void ReleaseTextureFromGPU(std::string textureName);
116
117 /**
118 * @brief Releases all cached textures from GPU memory.
119 *
120 * This does not affect RAM-side caching.
121 */
122 void ReleaseAllFromGPU();
123
124 /**
125 * @brief Fully clears the texture cache, including both RAM and GPU resources.
126 */
127 void ReleaseAll();
128
129 /**
130 * @brief Retrieves the full texture resource wrapper from cache.
131 *
132 * @param filename Base filename used during loading.
133 * @return Shared pointer to ImageDataGpu, or nullptr if not found.
134 */
135 std::shared_ptr<ImageDataGpu> GetTextureResources(std::string filename);
136
137 /**
138 * @brief Retrieves only the GPU render resources of a cached texture.
139 *
140 * Useful when the caller only needs GPU handles and does not care about image metadata.
141 *
142 * @param filename Base filename used during loading.
143 * @return Raw pointer to ITextureRenderResources, or nullptr if not found or not uploaded to GPU.
144 */
146
147 /**
148 * @brief Gets the total size of all currently cached textures in RAM.
149 * @return Memory usage in bytes.
150 */
151 size_t GetSizeInRAM() const;
152
153 /**
154 * @brief Gets the total size of all textures currently uploaded to GPU.
155 * @return GPU memory usage in bytes.
156 */
157 size_t GetSizeInGPU() const;
158
159protected:
160 /**
161 * @copydoc IRendererObserver::OnRenderResourcesRelease
162 */
163 void OnRenderResourcesRelease() override;
164 /**
165 * @copydoc IRendererObserver::OnRenderResourcesRebuild
166 */
167 void OnRenderResourcesRebuild() override;
168
169protected:
171 std::unordered_map<std::string, std::shared_ptr<ImageDataGpu>> mTextures;
172
175};
176
177} // namespace rendering_engine
Interface for observing renderer resource lifecycle events.
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Interface for backend-specific GPU texture resource management.
Represents raw 2D image data stored in memory.
Definition: image_data.hpp:80
Manages texture loading, GPU uploading, and caching for reuse.
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).
std::unordered_map< std::string, std::shared_ptr< ImageDataGpu > > mTextures
void LoadTexturesFromPackage()
Loads all textures from the packed asset archive.
void ReleaseAll()
Fully clears the texture cache, including both RAM and GPU resources.
void ReleaseTextureFromGPU(std::string textureName)
Releases a texture from GPU memory.
void OnRenderResourcesRebuild() override
Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).
std::shared_ptr< ImageDataGpu > GetTextureResources(std::string filename)
Retrieves the full texture resource wrapper from cache.
void ReleaseAllFromGPU()
Releases all cached textures from GPU memory.
std::string UploadTextureToRAM(std::string path)
Loads a single texture into RAM from the given file path.
ITextureRenderResources * GetTextureRenderResources(std::string filename)
Retrieves only the GPU render resources of a cached texture.
void LoadTexture(std::string textureName, ImageData imageData)
size_t GetSizeInRAM() const
Gets the total size of all currently cached textures in RAM.
void LoadTexturesFromFolder(std::string pathToFolder)
Loads all supported texture files (*.jpg, *.png) from a specified folder into RAM and uploads them to...
TextureCache(IRenderer *renderer)
Constructs the TextureCache with a reference to the renderer.
size_t GetSizeInGPU() const
Gets the total size of all textures currently uploaded to GPU.
void UploadTextureToGPU(std::string textureName)
Uploads a texture (previously loaded into RAM) to GPU.