Skip to content

Commit b274a40

Browse files
committed
Multithread texture loading
Twice faster Thanks tatarinrafa, @qweasdd136963 for initial code and @jazzvaz for thread pool
1 parent 08a3d68 commit b274a40

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/Layers/xrRender/ResourceManager.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "tss.h"
1515
#include "blenders\blender.h"
1616
#include "blenders\blender_recorder.h"
17+
#include "xrCore/Threading/ThreadPool.hpp"
1718

1819
// Already defined in Texture.cpp
1920
void fix_texture_name(LPSTR fn);
@@ -337,14 +338,44 @@ void CResourceManager::Delete(const Shader* S)
337338
Msg("! ERROR: Failed to find complete shader");
338339
}
339340

341+
xr_vector<CTexture*> textures_to_load;
342+
343+
void LoadTextures(const size_t thread_num, const size_t textures_per_worker)
344+
{
345+
const auto upperbound = thread_num * textures_per_worker;
346+
const auto lowerbound = upperbound - textures_per_worker;
347+
for (auto i = lowerbound; i < upperbound; i++)
348+
{
349+
if (i < textures_to_load.size())
350+
textures_to_load[i]->Load();
351+
else
352+
break;
353+
}
354+
}
355+
340356
void CResourceManager::DeferredUpload()
341357
{
342358
if (!RDEVICE.b_is_Ready)
343359
return;
344-
for (auto t = m_textures.begin(); t != m_textures.end(); t++)
345-
{
346-
t->second->Load();
347-
}
360+
361+
Msg("%s, amount of textures = %d", __FUNCTION__ , m_textures.size());
362+
363+
CTimer timer;
364+
timer.Start();
365+
366+
const auto nWorkers = ttapi.threads.size();
367+
const auto textures_per_worker = m_textures.size() / nWorkers;
368+
369+
for (auto& t : m_textures)
370+
textures_to_load.push_back(t.second);
371+
372+
for (auto i = 1; i < nWorkers; ++i)
373+
ttapi.threads[i]->addJob([=] { LoadTextures(i, textures_per_worker); });
374+
375+
ttapi.wait();
376+
377+
textures_to_load.clear();
378+
Msg("%s, texture loading time = %d", __FUNCTION__, timer.GetElapsed_ms());
348379
}
349380
/*
350381
void CResourceManager::DeferredUnload ()

0 commit comments

Comments
 (0)