| 386 | } |
| 387 | |
| 388 | void TextureManager::endDeferredLoading() |
| 389 | { |
| 390 | struct Job |
| 391 | { |
| 392 | TextureKey key; |
| 393 | CpuTextureHandle handle; |
| 394 | }; |
| 395 | |
| 396 | // Get a list of textures to load. |
| 397 | std::vector<Job> jobs; |
| 398 | for (auto& [key, handle] : mKeyToHandle) |
| 399 | { |
| 400 | auto& desc = getDesc(handle); |
| 401 | if (desc.state == TextureState::Referenced) |
| 402 | jobs.push_back(Job{key, handle}); |
| 403 | } |
| 404 | |
| 405 | // Early out if there are no textures to load. |
| 406 | mUseDeferredLoading = false; |
| 407 | if (jobs.empty()) |
| 408 | return; |
| 409 | |
| 410 | // Load textures in parallel. |
| 411 | std::atomic<size_t> texturesLoaded; |
| 412 | NumericRange<size_t> jobRange(0, jobs.size()); |
| 413 | std::for_each( |
| 414 | std::execution::par_unseq, |
| 415 | jobRange.begin(), |
| 416 | jobRange.end(), |
| 417 | [&](size_t i) |
| 418 | { |
| 419 | const auto& job = jobs[i]; |
| 420 | auto& desc = getDesc(job.handle); |
| 421 | if (job.key.fullPaths.size() == 1) |
| 422 | { |
| 423 | desc.pTexture = Texture::createFromFile( |
| 424 | mpDevice, job.key.fullPaths[0], job.key.generateMipLevels, job.key.loadAsSRGB, job.key.bindFlags, job.key.importFlags |
| 425 | ); |
| 426 | logDebug("Loading texture from '{}'", job.key.fullPaths[0]); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | desc.pTexture = |
| 431 | Texture::createMippedFromFiles(mpDevice, job.key.fullPaths, job.key.loadAsSRGB, job.key.bindFlags, job.key.importFlags); |
| 432 | logDebug("Loading mipped texture from '{}'", job.key.fullPaths[0]); |
| 433 | } |
| 434 | if (texturesLoaded.fetch_add(1) % 10 == 9) |
| 435 | { |
| 436 | logDebug("Flush"); |
| 437 | std::lock_guard<std::mutex> lock(mpDevice->getGlobalGfxMutex()); |
| 438 | mpDevice->wait(); |
| 439 | } |
| 440 | } |
| 441 | ); |
| 442 | mpDevice->wait(); |
| 443 | |
| 444 | // Mark loaded textures and add them to lookup table. |
| 445 | for (const auto& job : jobs) |