MCPcopy Create free account
hub / github.com/NVIDIAGameWorks/Falcor / optimizeMaterials

Method optimizeMaterials

Source/Falcor/Scene/Material/MaterialSystem.cpp:380–446  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

378 }
379
380 void MaterialSystem::optimizeMaterials()
381 {
382 // Gather a list of all textures to analyze.
383 std::vector<std::pair<ref<Material>, Material::TextureSlot>> materialSlots;
384 std::vector<ref<Texture>> textures;
385 size_t maxCount = mMaterials.size() * (size_t)Material::TextureSlot::Count;
386 materialSlots.reserve(maxCount);
387 textures.reserve(maxCount);
388
389 for (const auto& pMaterial : mMaterials)
390 {
391 for (uint32_t i = 0; i < (uint32_t)Material::TextureSlot::Count; i++)
392 {
393 auto slot = (Material::TextureSlot)i;
394 if (auto pTexture = pMaterial->getTexture(slot))
395 {
396 materialSlots.push_back({ pMaterial, slot });
397 textures.push_back(pTexture);
398 }
399 }
400 }
401
402 if (textures.empty()) return;
403
404 // Analyze the textures.
405 logInfo("Analyzing {} material textures.", textures.size());
406
407 RenderContext* pRenderContext = mpDevice->getRenderContext();
408
409 TextureAnalyzer analyzer(mpDevice);
410 auto pResults = mpDevice->createBuffer(textures.size() * TextureAnalyzer::getResultSize(), ResourceBindFlags::UnorderedAccess);
411 analyzer.analyze(pRenderContext, textures, pResults);
412
413 // Copy result to staging buffer for readback.
414 // This is mostly to avoid a full flush and the associated perf warning.
415 // We do not have any other useful GPU work, but unrelated GPU tasks can be in flight.
416 auto pResultsStaging = mpDevice->createBuffer(textures.size() * TextureAnalyzer::getResultSize(), ResourceBindFlags::None, MemoryType::ReadBack);
417 pRenderContext->copyResource(pResultsStaging.get(), pResults.get());
418 pRenderContext->submit(false);
419 pRenderContext->signal(mpFence.get());
420
421 // Wait for results to become available. Then optimize the materials.
422 mpFence->wait();
423 const TextureAnalyzer::Result* results = static_cast<const TextureAnalyzer::Result*>(pResultsStaging->map());
424 Material::TextureOptimizationStats stats = {};
425
426 for (size_t i = 0; i < textures.size(); i++)
427 {
428 materialSlots[i].first->optimizeTexture(materialSlots[i].second, results[i], stats);
429 }
430
431 pResultsStaging->unmap();
432
433 // Log optimization stats.
434 if (size_t totalRemoved = std::accumulate(stats.texturesRemoved.begin(), stats.texturesRemoved.end(), 0ull); totalRemoved > 0)
435 {
436 logInfo("Optimized materials by removing {} constant textures.", totalRemoved);
437 for (size_t slot = 0; slot < (size_t)Material::TextureSlot::Count; slot++)

Callers

nothing calls this directly

Calls 15

logInfoFunction · 0.85
padStringToLengthFunction · 0.85
createBufferMethod · 0.80
analyzeMethod · 0.80
copyResourceMethod · 0.80
mapMethod · 0.80
optimizeTextureMethod · 0.80
unmapMethod · 0.80
to_stringFunction · 0.70
logWarningFunction · 0.50
sizeMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected