diff --git a/src/Layers/xrRender/ResourceManager.cpp b/src/Layers/xrRender/ResourceManager.cpp index 44281579186..3b96979a390 100644 --- a/src/Layers/xrRender/ResourceManager.cpp +++ b/src/Layers/xrRender/ResourceManager.cpp @@ -14,6 +14,7 @@ #include "tss.h" #include "blenders\blender.h" #include "blenders\blender_recorder.h" +#include "xrCore/Threading/ThreadPool.hpp" // Already defined in Texture.cpp void fix_texture_name(LPSTR fn); @@ -337,14 +338,44 @@ void CResourceManager::Delete(const Shader* S) Msg("! ERROR: Failed to find complete shader"); } +xr_vector textures_to_load; + +void LoadTextures(const size_t thread_num, const size_t textures_per_worker) +{ + const auto upperbound = thread_num * textures_per_worker; + const auto lowerbound = upperbound - textures_per_worker; + for (auto i = lowerbound; i < upperbound; i++) + { + if (i < textures_to_load.size()) + textures_to_load[i]->Load(); + else + break; + } +} + void CResourceManager::DeferredUpload() { if (!RDEVICE.b_is_Ready) return; - for (auto t = m_textures.begin(); t != m_textures.end(); t++) - { - t->second->Load(); - } + + Msg("%s, amount of textures = %d", __FUNCTION__ , m_textures.size()); + + CTimer timer; + timer.Start(); + + const auto nWorkers = ttapi.threads.size(); + const auto textures_per_worker = m_textures.size() / nWorkers; + + for (auto& t : m_textures) + textures_to_load.push_back(t.second); + + for (auto i = 1; i < nWorkers; ++i) + ttapi.threads[i]->addJob([=] { LoadTextures(i, textures_per_worker); }); + + ttapi.wait(); + + textures_to_load.clear(); + Msg("%s, texture loading time = %d", __FUNCTION__, timer.GetElapsed_ms()); } /* void CResourceManager::DeferredUnload ()