diff --git a/libs/gltfio/src/ArchiveCache.cpp b/libs/gltfio/src/ArchiveCache.cpp index 47798741fa..c71065263a 100644 --- a/libs/gltfio/src/ArchiveCache.cpp +++ b/libs/gltfio/src/ArchiveCache.cpp @@ -118,21 +118,6 @@ Material* ArchiveCache::getMaterial(const ArchiveRequirements& reqs) { mMaterials[i] = Material::Builder() .package(spec.package, spec.packageByteCount) .build(mEngine); - - // Don't attempt to precompile shaders on WebGL. - // Chrome already suffers from slow shader compilation: - // https://github.com/google/filament/issues/6615 - // Precompiling shaders exacerbates the problem. - #if !defined(__EMSCRIPTEN__) - // First compile high priority variants - mMaterials[i]->compile(Material::CompilerPriorityQueue::HIGH, - UserVariantFilterBit::DIRECTIONAL_LIGHTING | - UserVariantFilterBit::DYNAMIC_LIGHTING | - UserVariantFilterBit::SHADOW_RECEIVER); - - // and then, everything else at low priority - mMaterials[i]->compile(Material::CompilerPriorityQueue::LOW); - #endif } return mMaterials[i]; diff --git a/samples/gltf_viewer.cpp b/samples/gltf_viewer.cpp index a7861e0438..1a09e79f8d 100644 --- a/samples/gltf_viewer.cpp +++ b/samples/gltf_viewer.cpp @@ -60,6 +60,7 @@ #include #include #include +#include #include #include "generated/resources/gltf_demo.h" @@ -597,6 +598,41 @@ int main(int argc, char** argv) { exit(1); } + // pre-compile all material variants + std::set materials; + RenderableManager const& rcm = app.engine->getRenderableManager(); + Slice const renderables{ + app.asset->getRenderableEntities(), app.asset->getRenderableEntityCount() }; + for (Entity const e: renderables) { + auto ri = rcm.getInstance(e); + size_t const c = rcm.getPrimitiveCount(ri); + for (size_t i = 0; i < c; i++) { + MaterialInstance* const mi = rcm.getMaterialInstanceAt(ri, i); + Material* ma = const_cast(mi->getMaterial()); + materials.insert(ma); + } + } + for (Material* ma : materials) { + // Don't attempt to precompile shaders on WebGL. + // Chrome already suffers from slow shader compilation: + // https://github.com/google/filament/issues/6615 + // Precompiling shaders exacerbates the problem. +#if !defined(__EMSCRIPTEN__) + // First compile high priority variants + ma->compile(Material::CompilerPriorityQueue::HIGH, + UserVariantFilterBit::DIRECTIONAL_LIGHTING | + UserVariantFilterBit::DYNAMIC_LIGHTING | + UserVariantFilterBit::SHADOW_RECEIVER); + + // and then, everything else at low priority, except STE, which is very uncommon. + ma->compile(Material::CompilerPriorityQueue::LOW, + UserVariantFilterBit::FOG | + UserVariantFilterBit::SKINNING | + UserVariantFilterBit::SSR | + UserVariantFilterBit::VSM); +#endif + } + app.instance = app.asset->getInstance(); buffer.clear(); buffer.shrink_to_fit();