Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::TextureCache Class Reference

Manages texture loading, GPU uploading, and caching for reuse. More...

#include <texture_cache.hpp>

Inherits rendering_engine::IRendererObserver.

Public Member Functions

 TextureCache (IRenderer *renderer)
 Constructs the TextureCache with a reference to the renderer.
 ~TextureCache ()
void LoadTexturesFromFolder (std::string pathToFolder)
 Loads all supported texture files (*.jpg, *.png) from a specified folder into RAM and uploads them to the GPU.
void LoadTexturesFromPackage ()
 Loads all textures from the packed asset archive.
std::string UploadTextureToRAM (std::string path)
 Loads a single texture into RAM from the given file path.
std::string UploadTextureToRAM (std::string textureFileName, std::vector< uint8_t > const &fileBytes)
 Loads a single texture into RAM from raw file bytes.
void UploadTextureToGPU (std::string filename)
 Uploads a texture (previously loaded into RAM) to GPU.
void ReleaseTextureFromGPU (std::string filename)
 Releases a texture from GPU memory.
void ReleaseAllFromGPU ()
 Releases all cached textures from GPU memory.
void ReleaseAll ()
 Fully clears the texture cache, including both RAM and GPU resources.
std::shared_ptr< ImageDataGpuGetTextureResources (std::string filename)
 Retrieves the full texture resource wrapper from cache.
ITextureRenderResourcesGetTextureRenderResources (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.
size_t GetSizeInGPU () const
 Gets the total size of all textures currently uploaded to GPU.
Public Member Functions inherited from rendering_engine::IRendererObserver
virtual ~IRendererObserver ()=default
 Virtual destructor.

Protected Member Functions

void OnRenderResourcesRelease () override
 Renderer callback: release all GPU resources (used during device loss/reset).
void OnRenderResourcesRebuild () override
 Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).

Protected Attributes

IRenderermRenderer
std::unordered_map< std::string, std::shared_ptr< ImageDataGpu > > mTextures
size_t mTotalSizeRAM
size_t mTotalSizeGPU

Detailed Description

Manages texture loading, GPU uploading, and caching for reuse.

This class handles texture resource management at runtime. It supports loading image files (JPG and PNG) from a folder or path into RAM, uploading them to GPU, and provides access to the texture resources through string identifiers (filenames without extensions).

Textures are stored using shared pointers to ImageDataGpu, and memory tracking is performed for both RAM and GPU sides.

Definition at line 30 of file texture_cache.hpp.

Constructor & Destructor Documentation

◆ TextureCache()

rendering_engine::TextureCache::TextureCache ( IRenderer * renderer)

Constructs the TextureCache with a reference to the renderer.

Parameters
rendererPointer to the active rendering backend.

Definition at line 14 of file texture_cache.cpp.

15 :
16 mRenderer(renderer),
19{
20 mRenderer->RegisterObserver(this);
21}

◆ ~TextureCache()

rendering_engine::TextureCache::~TextureCache ( )

Definition at line 23 of file texture_cache.cpp.

24{
25 mRenderer->UnregisterObserver(this);
26}

Member Function Documentation

◆ GetSizeInGPU()

size_t rendering_engine::TextureCache::GetSizeInGPU ( ) const

Gets the total size of all textures currently uploaded to GPU.

Returns
GPU memory usage in bytes.

Definition at line 197 of file texture_cache.cpp.

198{
199 return mTotalSizeGPU;
200}

◆ GetSizeInRAM()

size_t rendering_engine::TextureCache::GetSizeInRAM ( ) const

Gets the total size of all currently cached textures in RAM.

Returns
Memory usage in bytes.

Definition at line 192 of file texture_cache.cpp.

193{
194 return mTotalSizeRAM;
195}

◆ GetTextureRenderResources()

ITextureRenderResources * rendering_engine::TextureCache::GetTextureRenderResources ( std::string filename)

Retrieves only the GPU render resources of a cached texture.

Useful when the caller only needs GPU handles and does not care about image metadata.

Parameters
filenameBase filename used during loading.
Returns
Raw pointer to ITextureRenderResources, or nullptr if not found or not uploaded to GPU.

Definition at line 182 of file texture_cache.cpp.

183{
184 if (auto search = mTextures.find(filename); search == mTextures.end())
185 {
186 return nullptr;
187 }
188
189 return mTextures.at(filename)->GetTextureRenderResources();
190}
std::unordered_map< std::string, std::shared_ptr< ImageDataGpu > > mTextures

◆ GetTextureResources()

std::shared_ptr< ImageDataGpu > rendering_engine::TextureCache::GetTextureResources ( std::string filename)

Retrieves the full texture resource wrapper from cache.

Parameters
filenameBase filename used during loading.
Returns
Shared pointer to ImageDataGpu, or nullptr if not found.

Definition at line 172 of file texture_cache.cpp.

173{
174 auto search = mTextures.find(filename);
175 if (search == mTextures.end())
176 {
177 return nullptr;
178 }
179 return search->second;
180}

◆ LoadTexturesFromFolder()

void rendering_engine::TextureCache::LoadTexturesFromFolder ( std::string pathToFolder)

Loads all supported texture files (*.jpg, *.png) from a specified folder into RAM and uploads them to the GPU.

Each texture is registered under its base filename (without extension) for later access. Invalid paths or unsupported file types are skipped silently.

Parameters
pathToFolderAbsolute or relative path to the folder containing texture files.

Definition at line 28 of file texture_cache.cpp.

29{
30 // 1. Check if path is valid and exist
31 boost::filesystem::path pathToDirectory = boost::filesystem::path(pathToFolder);
32 const bool isValidFolderPath = boost::filesystem::exists(boost::filesystem::path(pathToFolder)) && boost::filesystem::is_directory(boost::filesystem::path(pathToFolder));
33 if (!isValidFolderPath)
34 {
35 return;
36 }
37 // 2. Iterate through files in the folder.
38 // if file is in the list of supported extensions
39 for (boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(pathToDirectory))
40 {
41 auto textureName = UploadTextureToRAM(x.path().string());
42 if (!textureName.empty())
43 {
44 UploadTextureToGPU(textureName);
45 }
46 }
47}
std::string UploadTextureToRAM(std::string path)
Loads a single texture into RAM from the given file path.
void UploadTextureToGPU(std::string filename)
Uploads a texture (previously loaded into RAM) to GPU.

◆ LoadTexturesFromPackage()

void rendering_engine::TextureCache::LoadTexturesFromPackage ( )

Loads all textures from the packed asset archive.

This function behaves similarly to LoadTexturesFromFolder(), but instead of scanning a filesystem directory, it iterates over virtual file entries stored inside the packed content system created by the packaging tool.

Only entries located under the virtual folder: "Textures/" are considered valid texture resources.

For each entry, the raw file bytes are retrieved via Utility::ReadPackedFile(), decoded into an ImageDataGpu instance, and stored in the RAM cache. GPU uploading must still be triggered via UploadTextureToGPU().

Definition at line 49 of file texture_cache.cpp.

50{
51 const auto& entries = Utility::GetPackEntries();
52
53 std::string folderEntry = { "Textures/" };
54 for (auto& entry : entries)
55 {
56 const std::string& virtualPath = entry.first;
57 if (virtualPath.rfind(folderEntry, 0) == 0) // starts with Textures/
58 {
59 std::string textureFileName = virtualPath.substr(folderEntry.size());
60
61 std::vector<uint8_t> binaryFileData = Utility::ReadPackedFile(virtualPath);
62 if (binaryFileData.empty())
63 {
64 std::cerr << "[TextureCache] Could not read packed texture: "
65 << virtualPath << std::endl;
66 continue;
67 }
68
69 // Upload to RAM with textureName + binaryFileData
70 auto textureName = UploadTextureToRAM(textureFileName, binaryFileData);
71 if (!textureName.empty())
72 {
73 UploadTextureToGPU(textureName);
74 }
75 }
76 }
77}
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:219
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
Definition utility.cpp:260

◆ OnRenderResourcesRebuild()

void rendering_engine::TextureCache::OnRenderResourcesRebuild ( )
overrideprotectedvirtual

Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).

This method will be called after the device or swapchain is recreated, allowing the observer to re-upload or recreate all necessary resources for rendering.

Implements rendering_engine::IRendererObserver.

Definition at line 207 of file texture_cache.cpp.

208{
209 for (auto& texture : mTextures)
210 {
211 texture.second->UploadToGPU();
212 size_t size = texture.second->GetSizeInGPU();
213 mTotalSizeGPU += size;
214 }
215}

◆ OnRenderResourcesRelease()

void rendering_engine::TextureCache::OnRenderResourcesRelease ( )
overrideprotectedvirtual

Renderer callback: release all GPU resources (used during device loss/reset).

This method will be called before any device or swapchain is destroyed, allowing the observer to safely release all handles and deallocate any GPU memory.

Implements rendering_engine::IRendererObserver.

Definition at line 202 of file texture_cache.cpp.

203{
205}
void ReleaseAllFromGPU()
Releases all cached textures from GPU memory.

◆ ReleaseAll()

void rendering_engine::TextureCache::ReleaseAll ( )

Fully clears the texture cache, including both RAM and GPU resources.

Definition at line 164 of file texture_cache.cpp.

165{
167 mTextures.clear();
168 mTotalSizeRAM = 0;
169 mTotalSizeGPU = 0;
170}

◆ ReleaseAllFromGPU()

void rendering_engine::TextureCache::ReleaseAllFromGPU ( )

Releases all cached textures from GPU memory.

This does not affect RAM-side caching.

Definition at line 155 of file texture_cache.cpp.

156{
157 for (auto& texture : mTextures)
158 {
159 texture.second->ReleaseFromGPU();
160 }
161 mTotalSizeGPU = 0;
162}

◆ ReleaseTextureFromGPU()

void rendering_engine::TextureCache::ReleaseTextureFromGPU ( std::string filename)

Releases a texture from GPU memory.

The texture remains in RAM cache and can be uploaded again later.

Parameters
filenameCache key of the texture to release (base filename).

Definition at line 141 of file texture_cache.cpp.

142{
143 if (auto search = mTextures.find(filename); search == mTextures.end())
144 {
145 return;
146 }
147
148 auto& texture = mTextures[filename];
149 size_t size = texture->GetSizeInGPU();
150 texture->ReleaseFromGPU();
151
152 mTotalSizeGPU -= size;
153}

◆ UploadTextureToGPU()

void rendering_engine::TextureCache::UploadTextureToGPU ( std::string filename)

Uploads a texture (previously loaded into RAM) to GPU.

If the texture is not found in RAM cache, the function does nothing.

Parameters
filenameBase filename (no path, no extension) used as the cache key.

Definition at line 124 of file texture_cache.cpp.

125{
126 // If texture is not loaded in RAM yet, skip loading to GPU.
127 if (auto search = mTextures.find(filename); search == mTextures.end())
128 {
129 return;
130 }
131 if (mTextures[filename]->IsOnGPU())
132 {
133 return;
134 }
135
136 mTextures[filename]->UploadToGPU();
137 size_t size = mTextures[filename]->GetSizeInGPU();
138 mTotalSizeGPU += size;
139}

◆ UploadTextureToRAM() [1/2]

std::string rendering_engine::TextureCache::UploadTextureToRAM ( std::string path)

Loads a single texture into RAM from the given file path.

The texture will not be uploaded to GPU automatically. The caller must use UploadTextureToGPU() after this call if GPU usage is needed.

Parameters
pathFull file path to the texture image (JPG/PNG).
Returns
The base filename used as the cache key, or an empty string if loading failed.

Definition at line 79 of file texture_cache.cpp.

80{
81 auto filePath = boost::filesystem::path(path);
82 if (!boost::filesystem::is_regular_file(filePath))
83 {
84 return std::string{};
85 }
86
87 const std::string ext = filePath.extension().string();
88 const bool isExtensionSupported = (ext == ".jpg") || (ext == ".png");
89 if (!isExtensionSupported)
90 {
91 return std::string{};
92 }
93
94 std::string filename = filePath.stem().string();
95 // If texture is already loaded into RAM yet, do not add again.
96 if (auto search = mTextures.find(filename); search != mTextures.end())
97 {
98 return std::string{};
99 }
100 mTextures[filename] = std::make_shared<ImageDataGpu>(path, mRenderer);
101
102 size_t size = mTextures.at(filename)->GetSizeInRAM();
103 mTotalSizeRAM += size;
104
105 return filename;
106}

◆ UploadTextureToRAM() [2/2]

std::string rendering_engine::TextureCache::UploadTextureToRAM ( std::string textureFileName,
std::vector< uint8_t > const & fileBytes )

Loads a single texture into RAM from raw file bytes.

This overload enables texture loading from virtual files stored in a packed asset archive or from any memory-based source rather than the OS filesystem.

The fileBytes buffer must contain the complete encoded texture data (e.g., PNG or JPEG). The data is decoded and stored inside ImageDataGpu.

Parameters
textureFileNameCache key representing the virtual texture path (e.g., "Textures/myTexture.png" or simply "myTexture").
fileBytesRaw encoded texture file data (PNG/JPG).
Returns
The cache key on success, or an empty string on failure.

Definition at line 108 of file texture_cache.cpp.

109{
110 auto textureName = boost::filesystem::path(textureFileName).stem().string();
111 // If texture is already loaded into RAM yet, do not add again.
112 if (auto search = mTextures.find(textureName); search != mTextures.end())
113 {
114 return std::string{};
115 }
116 mTextures[textureName] = std::make_shared<ImageDataGpu>(fileBytes, mRenderer);
117
118 size_t size = mTextures.at(textureName)->GetSizeInRAM();
119 mTotalSizeRAM += size;
120
121 return textureName;
122}

Member Data Documentation

◆ mRenderer

IRenderer* rendering_engine::TextureCache::mRenderer
protected

Definition at line 166 of file texture_cache.hpp.

◆ mTextures

std::unordered_map<std::string, std::shared_ptr<ImageDataGpu> > rendering_engine::TextureCache::mTextures
protected

Definition at line 167 of file texture_cache.hpp.

◆ mTotalSizeGPU

size_t rendering_engine::TextureCache::mTotalSizeGPU
protected

Definition at line 170 of file texture_cache.hpp.

◆ mTotalSizeRAM

size_t rendering_engine::TextureCache::mTotalSizeRAM
protected

Definition at line 169 of file texture_cache.hpp.


The documentation for this class was generated from the following files: