Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-populate the default material's depth variants #8300

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading