3#include "boost/filesystem.hpp"
27 boost::filesystem::path pathToDirectory = boost::filesystem::path(pathToFolder);
28 const bool isValidFolderPath = boost::filesystem::exists(boost::filesystem::path(pathToFolder)) && boost::filesystem::is_directory(boost::filesystem::path(pathToFolder));
29 if (!isValidFolderPath)
35 for (boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(pathToDirectory))
45 std::string folderEntry = {
"Models/" };
46 for (
auto& entry : entries)
48 const std::string& virtualPath = entry.first;
49 if (virtualPath.rfind(folderEntry, 0) == 0)
51 std::string modelName = virtualPath.substr(folderEntry.size());
54 if (binaryFileData.empty())
56 std::cerr <<
"[TextureCache] Could not read packed texture: "
57 << virtualPath << std::endl;
70 mModels[
"Quad2D"]->CreateQuad2D();
72 const size_t sizeVertices =
mModels.at(
"Quad2D")->GetCpuVertexBufferSize();
74 const size_t sizeIndices =
mModels.at(
"Quad2D")->GetCpuIndexBufferSize();
80 auto filePath = boost::filesystem::path(path);
81 if (!boost::filesystem::is_regular_file(filePath))
86 const std::string ext = filePath.extension().string();
87 const bool isExtensionSupported = (ext ==
".fbx");
88 if (!isExtensionSupported)
93 std::string filename = filePath.stem().string();
99 mModels[filename] = std::make_shared<MeshDataGpu>(filePath.string(),
mRenderer);
101 const size_t sizeVertices =
mModels.at(filename)->GetCpuVertexBufferSize();
103 const size_t sizeIndices =
mModels.at(filename)->GetCpuIndexBufferSize();
111 auto modelName = boost::filesystem::path(fileName).stem().string();
114 if (
auto search =
mModels.find(modelName); search !=
mModels.end())
116 return std::string{};
121 const size_t sizeVertices =
mModels.at(modelName)->GetCpuVertexBufferSize();
123 const size_t sizeIndices =
mModels.at(modelName)->GetCpuIndexBufferSize();
132 if (
auto search =
mModels.find(filename); search ==
mModels.end())
136 if (
mModels[filename]->IsOnGPU())
141 mModels[filename]->UploadToGPU();
142 size_t sizeVertices =
mModels[filename]->GetGpuVertexBufferSize();
144 size_t sizeIndices =
mModels[filename]->GetGpuIndexBufferSize();
150 if (
auto search =
mModels.find(filename); search ==
mModels.end())
155 auto& model =
mModels[filename];
156 size_t sizeVertices = model->GetGpuVertexBufferSize();
157 size_t sizeIndices = model->GetGpuIndexBufferSize();
158 model->ReleaseFromGPU();
168 texture.second->ReleaseFromGPU();
182 auto search =
mModels.find(filename);
187 return search->second;
192 if (
auto search =
mModels.find(filename); search ==
mModels.end())
197 return mModels.at(filename)->GetMeshRenderResources();
219 model.second->UploadToGPU();
220 size_t sizeVertices = model.second->GetGpuVertexBufferSize();
222 size_t sizeIndices = model.second->GetGpuIndexBufferSize();
Interface for GPU mesh resource management.
Defines an abstract interface for rendering backends.
void CreateQuad2D()
Creates a built-in 2D quad mesh.
void ReleaseAll()
Remove all models from both GPU and RAM, clearing the cache.
void UploadModelToGPU(std::string filename)
Upload a cached model's mesh data to the GPU.
std::shared_ptr< MeshDataGpu > GetMeshResources(std::string filename)
Get a shared pointer to the MeshDataGpu for a model.
IMeshRenderResources * GetMeshRenderResources(std::string filename)
Get the IMeshRenderResources interface for a model's GPU resources.
std::unordered_map< std::string, std::shared_ptr< MeshDataGpu > > mModels
~ModelCache()
Destructor. Releases all resources and unregisters observer.
size_t GetSizeInRAM() const
Get the total size (in bytes) of all models loaded in RAM.
void LoadModelsFromFolder(std::string pathToFolder)
Load all models found in the specified folder into RAM.
size_t GetSizeInGPU() const
Get the total size (in bytes) of all models currently resident on GPU.
void OnRenderResourcesRebuild() override
Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).
std::string UploadModelToRAM(std::string path)
Load a single model from file into RAM.
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).
void LoadModelsFromPackage()
Load all models from the packed asset container.
void ReleaseAllFromGPU()
Release all model mesh data from GPU memory.
ModelCache(IRenderer *renderer)
Construct a ModelCache and register it with the given renderer.
void ReleaseModelFromGPU(std::string filename)
Release a model's mesh data from GPU memory.
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.