9#include "boost/filesystem.hpp"
34 LOG_INFO(
"Loading textures from folder: " + pathToFolder);
35 auto start = std::chrono::steady_clock::now();
37 boost::filesystem::path pathToDirectory = boost::filesystem::path(pathToFolder);
38 const bool isValidFolderPath = boost::filesystem::exists(boost::filesystem::path(pathToFolder)) && boost::filesystem::is_directory(boost::filesystem::path(pathToFolder));
39 if (!isValidFolderPath)
45 for (boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(pathToDirectory))
48 if (!textureName.empty())
54 auto end = std::chrono::steady_clock::now();
55 float ms = std::chrono::duration<float, std::milli>(end - start).count();
58 " textures from folder in " +
59 std::to_string(ms) +
" ms. RAM usage: " +
67 if (!textureNameUploaded.empty())
75 LOG_INFO(
"Loading textures from package.");
76 auto start = std::chrono::steady_clock::now();
79 std::string folderEntry = {
"Textures/" };
80 for (
auto& entry : entries)
82 const std::string& virtualPath = entry.first;
83 if (virtualPath.rfind(folderEntry, 0) == 0)
85 std::string textureFileName = virtualPath.substr(folderEntry.size());
88 if (binaryFileData.empty())
90 LOG_ERROR(
"[TextureCache] Could not read packed texture: "
97 if (!textureName.empty())
103 auto end = std::chrono::steady_clock::now();
104 float ms = std::chrono::duration<float, std::milli>(end - start).count();
107 " textures from package in " +
108 std::to_string(ms) +
" ms. RAM usage: " +
114 auto filePath = boost::filesystem::path(path);
115 if (!boost::filesystem::is_regular_file(filePath))
117 return std::string{};
120 const std::string ext = filePath.extension().string();
121 const bool isExtensionSupported = (ext ==
".jpg") || (ext ==
".png");
122 if (!isExtensionSupported)
124 return std::string{};
127 std::string filename = filePath.stem().string();
131 return std::string{};
133 LOG_DEBUG(
"Uploading texture to RAM: " + filename);
134 auto start = std::chrono::steady_clock::now();
137 size_t size =
mTextures.at(filename)->GetSizeInRAM();
140 auto end = std::chrono::steady_clock::now();
141 float ms = std::chrono::duration<float, std::milli>(end - start).count();
143 LOG_DEBUG(
"Texture loaded to RAM: " + filename +
144 " (" + std::to_string(size) +
145 " bytes, " + std::to_string(ms) +
" ms)");
152 auto textureName = boost::filesystem::path(textureFileName).stem().string();
156 return std::string{};
158 LOG_DEBUG(
"Uploading texture to RAM: " + textureName);
159 auto start = std::chrono::steady_clock::now();
162 size_t size =
mTextures.at(textureName)->GetSizeInRAM();
164 auto end = std::chrono::steady_clock::now();
165 float ms = std::chrono::duration<float, std::milli>(end - start).count();
167 LOG_DEBUG(
"Texture loaded to RAM: " + textureName +
168 " (" + std::to_string(size) +
169 " bytes, " + std::to_string(ms) +
" ms)");
179 return std::string{};
181 LOG_DEBUG(
"Uploading texture to RAM: " + textureName);
182 auto start = std::chrono::steady_clock::now();
185 size_t size =
mTextures.at(textureName)->GetSizeInRAM();
187 auto end = std::chrono::steady_clock::now();
188 float ms = std::chrono::duration<float, std::milli>(end - start).count();
190 LOG_DEBUG(
"Texture loaded to RAM: " + textureName +
191 " (" + std::to_string(size) +
192 " bytes, " + std::to_string(ms) +
" ms)");
199 LOG_DEBUG(
"Uploading texture to GPU: " + textureName);
200 auto start = std::chrono::steady_clock::now();
212 size_t size =
mTextures[textureName]->GetSizeInGPU();
215 auto end = std::chrono::steady_clock::now();
216 float ms = std::chrono::duration<float, std::milli>(end - start).count();
218 LOG_DEBUG(
"Texture uploaded to GPU: " + textureName +
219 " (" + std::to_string(size) +
220 " bytes, " + std::to_string(ms) +
" ms)");
225 LOG_DEBUG(
"Releasing texture from GPU: " + textureName);
232 size_t size = texture->GetSizeInGPU();
233 texture->ReleaseFromGPU();
242 texture.second->ReleaseFromGPU();
249 LOG_INFO(
"Releasing all textures. RAM usage before clear: " +
266 return search->second;
276 return mTextures.at(filename)->GetTextureRenderResources();
298 texture.second->UploadToGPU();
299 size_t size = texture.second->GetSizeInGPU();
Defines an abstract interface for rendering backends.
virtual void RegisterObserver(IRendererObserver *notifier)=0
Registers an observer for rendering events.
virtual void UnregisterObserver(IRendererObserver *notifier)=0
Unregisters a previously registered observer.
Interface for backend-specific GPU texture resource management.
Represents raw 2D image data stored in memory.
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.
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
Engine-wide logging system for runtime diagnostics and performance tracking.