![]() |
Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
|
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< ImageDataGpu > | GetTextureResources (std::string filename) |
| Retrieves the full texture resource wrapper from cache. | |
| 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. | |
| 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 | |
| IRenderer * | mRenderer |
| std::unordered_map< std::string, std::shared_ptr< ImageDataGpu > > | mTextures |
| size_t | mTotalSizeRAM |
| size_t | mTotalSizeGPU |
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.
| rendering_engine::TextureCache::TextureCache | ( | IRenderer * | renderer | ) |
Constructs the TextureCache with a reference to the renderer.
| renderer | Pointer to the active rendering backend. |
Definition at line 14 of file texture_cache.cpp.
| rendering_engine::TextureCache::~TextureCache | ( | ) |
Definition at line 23 of file texture_cache.cpp.
| size_t rendering_engine::TextureCache::GetSizeInGPU | ( | ) | const |
Gets the total size of all textures currently uploaded to GPU.
Definition at line 197 of file texture_cache.cpp.
| size_t rendering_engine::TextureCache::GetSizeInRAM | ( | ) | const |
Gets the total size of all currently cached textures in RAM.
Definition at line 192 of file texture_cache.cpp.
| 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.
| filename | Base filename used during loading. |
Definition at line 182 of file texture_cache.cpp.
| std::shared_ptr< ImageDataGpu > rendering_engine::TextureCache::GetTextureResources | ( | std::string | filename | ) |
Retrieves the full texture resource wrapper from cache.
| filename | Base filename used during loading. |
Definition at line 172 of file texture_cache.cpp.
| 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.
| pathToFolder | Absolute or relative path to the folder containing texture files. |
Definition at line 28 of file texture_cache.cpp.
| 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.
|
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.
|
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.
| 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.
| 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.
| 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.
| filename | Cache key of the texture to release (base filename). |
Definition at line 141 of file texture_cache.cpp.
| 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.
| filename | Base filename (no path, no extension) used as the cache key. |
Definition at line 124 of file texture_cache.cpp.
| 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.
| path | Full file path to the texture image (JPG/PNG). |
Definition at line 79 of file texture_cache.cpp.
| 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.
| textureFileName | Cache key representing the virtual texture path (e.g., "Textures/myTexture.png" or simply "myTexture"). |
| fileBytes | Raw encoded texture file data (PNG/JPG). |
Definition at line 108 of file texture_cache.cpp.
|
protected |
Definition at line 166 of file texture_cache.hpp.
|
protected |
Definition at line 167 of file texture_cache.hpp.
|
protected |
Definition at line 170 of file texture_cache.hpp.
|
protected |
Definition at line 169 of file texture_cache.hpp.