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