Skip to content

Commit

Permalink
pre-populate the default material's depth variants
Browse files Browse the repository at this point in the history
This restores the old behavior with depth variant caching. We pay the
price at engine init time, instead of when the variant is needed the
first time. We can revisit this later.

Note that the default material doesn't have all possible depth variants
(e.g. VSM), but that pre-caches the most popular ones.

BUGS=[381946222]
  • Loading branch information
pixelflinger committed Dec 5, 2024
1 parent 4ea3f74 commit adc7ae7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions filament/src/details/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,13 @@ void FMaterial::createAndCacheProgram(Program&& p, Variant variant) const noexce
FEngine const& engine = mEngine;
DriverApi& driverApi = mEngine.getDriverApi();

// Check if the default material has this program cached
if (mMaterialDomain == MaterialDomain::SURFACE &&
bool const isSharedVariant =
(mMaterialDomain == MaterialDomain::SURFACE) &&
!mIsDefaultMaterial && !mHasCustomDepthShader &&
Variant::isValidDepthVariant(variant)) {
Variant::isValidDepthVariant(variant);

// Check if the default material has this program cached
if (isSharedVariant) {
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
if (pDefaultMaterial) {
auto program = pDefaultMaterial->mCachedPrograms[variant.key];
Expand All @@ -627,9 +630,7 @@ void FMaterial::createAndCacheProgram(Program&& p, Variant variant) const noexce
// If the default material doesn't already have this program cached, and all caching conditions
// are met (Surface Domain and no custom depth shader), cache it now.
// New Materials will inherit these program automatically.
if (mMaterialDomain == MaterialDomain::SURFACE &&
!mIsDefaultMaterial && !mHasCustomDepthShader &&
Variant::isValidDepthVariant(variant)) {
if (isSharedVariant) {
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
if (pDefaultMaterial && !pDefaultMaterial->mCachedPrograms[variant.key]) {
// set the tag to the default material name
Expand Down Expand Up @@ -1076,6 +1077,21 @@ void FMaterial::processPushConstants(FEngine& engine, MaterialParser const* pars
}

void FMaterial::precacheDepthVariants(FEngine& engine) {
// pre-cache all depth variants inside the default material. Note that this should be
// entirely optional; if we remove this pre-caching, these variants will be populated
// later, when/if needed by createAndCacheProgram(). Doing it now potentially uses more
// memory and increases init time, but reduces hiccups during the first frame.
if (UTILS_UNLIKELY(mIsDefaultMaterial)) {
auto const allDepthVariants = VariantUtils::getDepthVariants();
for (auto const variant: allDepthVariants) {
assert_invariant(Variant::isValidDepthVariant(variant));
if (hasVariant(variant)) {
prepareProgram(variant);
}
}
return;
}

// if possible pre-cache all depth variants from the default material
if (mMaterialDomain == MaterialDomain::SURFACE &&
!mIsDefaultMaterial &&
Expand Down

0 comments on commit adc7ae7

Please sign in to comment.