Skip to content

Commit 94de465

Browse files
committed
Mesh Component: "Multi-threaded" texture loading
1 parent 023a00c commit 94de465

File tree

2 files changed

+28
-6
lines changed
  • src
    • private/Core/GameObject/Component
    • public/Core/GameObject/Component

2 files changed

+28
-6
lines changed

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ void Mesh::LoadModel(std::string filename) {
392392
Assimp::Importer importer;
393393
const aiScene* scene = importer.ReadFile(filename, NULL);
394394

395+
ResourceManager* resMgr = ResourceManager::GetInstance();
396+
std::atomic<int> taskPending = 0;
397+
std::vector<std::thread> threads;
398+
395399
for (UINT i = 0; i < scene->mNumMeshes; i++) {
396400
aiMesh* mesh = scene->mMeshes[i];
397401
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
@@ -443,26 +447,41 @@ void Mesh::LoadModel(std::string filename) {
443447
if (material->GetTextureCount(aiTextureType_BASE_COLOR) > 0 && material->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS) {
444448
const aiTexture* texture = scene->GetEmbeddedTexture(texPath.C_Str());
445449
if (texture != nullptr) {
446-
ResourceManager* resMgr = ResourceManager::GetInstance();
447450
std::string name = std::string(texPath.C_Str());
448-
ComPtr<ID3D12Resource> resource;
449-
resMgr->LoadTexture((BYTE*)texture->pcData, texture->mWidth, name, resource);
450-
resource->SetName(L"Mesh Base color");
451-
this->m_textures[i] = resource;
451+
taskPending++;
452+
threads.emplace_back([=, &taskPending, this]() {
453+
if (!texture) {
454+
taskPending--;
455+
return;
456+
}
457+
ComPtr<ID3D12Resource> resource;
458+
resMgr->LoadTexture((BYTE*)texture->pcData, texture->mWidth, name, resource);
459+
resource->SetName(L"Mesh Base color");
460+
this->m_textures[i] = resource;
461+
taskPending--;
462+
});
452463
}
453464
}
454465

455466
if (material->GetTextureCount(aiTextureType_METALNESS) > 0 && material->GetTexture(aiTextureType_METALNESS, 0, &metalPath) == AI_SUCCESS) {
456467
const aiTexture* texture = scene->GetEmbeddedTexture(metalPath.C_Str());
457468

458469
if (texture != nullptr) {
459-
ResourceManager* resMgr = ResourceManager::GetInstance();
460470
std::string name = std::string(metalPath.C_Str());
471+
if (!texture) {
472+
taskPending--;
473+
return;
474+
}
461475
ComPtr<ID3D12Resource> resource;
462476
resMgr->LoadTexture((BYTE*)texture->pcData, texture->mWidth, name, resource);
463477
resource->SetName(L"Mesh Metallic Roughness");
464478
this->m_ORMTextures[i] = resource;
479+
taskPending--;
465480
}
466481
}
467482
}
483+
484+
for (std::thread& t : threads) {
485+
if (t.joinable()) t.join();
486+
}
468487
}

src/public/Core/GameObject/Component/Mesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "Util.h"
1313
#include <string>
1414

15+
#include <thread>
16+
#include <atomic>
17+
1518
#include "Core/GameObject/Component/Component.h"
1619
#include "Core/Renderer/Shader.h"
1720
#include "Core/Renderer/Descriptor.h"

0 commit comments

Comments
 (0)