Skip to content

Commit

Permalink
Multithread texture loading
Browse files Browse the repository at this point in the history
Twice faster
Thanks tatarinrafa, @qweasdd136963 for initial code and @jazzvaz for thread pool
  • Loading branch information
Xottab-DUTY committed Jan 8, 2018
1 parent 08a3d68 commit b274a40
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/Layers/xrRender/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -337,14 +338,44 @@ void CResourceManager::Delete(const Shader* S)
Msg("! ERROR: Failed to find complete shader");
}

xr_vector<CTexture*> 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 ()
Expand Down

0 comments on commit b274a40

Please sign in to comment.