Skip to content

Commit

Permalink
matdbg: Fix material collision (#8322)
Browse files Browse the repository at this point in the history
Fix 3 bugs wrt matdbg:

1. The numbering scheme for materials with same name was buggy
2. The shared depth variants of the default material should also
   show up as active material variants.
3. When two instantiation of FMaterial point to the exact same
   bits, we still need to treat them as two materials. (Otherwise
   we wouldn't be able to modify one of them in matdbg).
  • Loading branch information
poweifeng authored Dec 19, 2024
1 parent 1f1389e commit aa09d86
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 3 additions & 6 deletions filament/src/details/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,10 @@ void FMaterial::createAndCacheProgram(Program&& p, Variant variant) const noexce
FEngine const& engine = mEngine;
DriverApi& driverApi = mEngine.getDriverApi();

bool const isSharedVariant =
(mMaterialDomain == MaterialDomain::SURFACE) &&
!mIsDefaultMaterial && !mHasCustomDepthShader &&
Variant::isValidDepthVariant(variant);
bool const isShared = isSharedVariant(variant);

// Check if the default material has this program cached
if (isSharedVariant) {
if (isShared) {
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
if (pDefaultMaterial) {
auto program = pDefaultMaterial->mCachedPrograms[variant.key];
Expand All @@ -636,7 +633,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 (isSharedVariant) {
if (isShared) {
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
if (pDefaultMaterial && !pDefaultMaterial->mCachedPrograms[variant.key]) {
// set the tag to the default material name
Expand Down
12 changes: 12 additions & 0 deletions filament/src/details/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ class FMaterial : public Material {
std::unique_lock<utils::Mutex> lock(mActiveProgramsLock);
mActivePrograms.set(variant.key);
lock.unlock();

if (isSharedVariant(variant)) {
FMaterial const* const pDefaultMaterial = mEngine.getDefaultMaterial();
if (pDefaultMaterial && pDefaultMaterial->mCachedPrograms[variant.key]) {
return pDefaultMaterial->getProgram(variant);
}
}
#endif
assert_invariant(mCachedPrograms[variant.key]);
return mCachedPrograms[variant.key];
Expand Down Expand Up @@ -290,6 +297,11 @@ class FMaterial : public Material {

void createAndCacheProgram(backend::Program&& p, Variant variant) const noexcept;

inline bool isSharedVariant(Variant variant) const {
return (mMaterialDomain == MaterialDomain::SURFACE) && !mIsDefaultMaterial &&
!mHasCustomDepthShader && Variant::isValidDepthVariant(variant);
}

// try to order by frequency of use
mutable std::array<backend::Handle<backend::HwProgram>, VARIANT_COUNT> mCachedPrograms;
DescriptorSetLayout mPerViewDescriptorSetLayout;
Expand Down
8 changes: 6 additions & 2 deletions libs/matdbg/src/DebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ DebugServer::addMaterial(const CString& name, const void* data, size_t size, voi
return {};
}

const uint32_t seed = 42;
const MaterialKey key = utils::hash::murmurSlow((const uint8_t*) data, size, seed);
// Note that it's possible to have two materials with the exact same content (however wasteful),
// but they refer to different instantiation of FMaterial. Hence we hash on userdata and the
// material data.
constexpr uint32_t seed = 42;
uint64_t dataSpace[2] = {(uint64_t) data, (uint64_t) userdata};
uint32_t const key = utils::hash::murmur3((const uint32_t*) dataSpace, sizeof(dataSpace), seed);

// Retain a copy of the package to permit queries after the client application has
// freed up the original material package.
Expand Down
2 changes: 1 addition & 1 deletion libs/matdbg/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ class MaterialSidePanel extends LitElement {
let name = material.name || kUntitledPlaceholder;
if (name in duplicatedLabels) {
const index = duplicatedLabels[name];
name = `${name} (${index})`;
duplicatedLabels[name] = index + 1;
name = `${name} (${index})`;
}
return {
matid: matid,
Expand Down

0 comments on commit aa09d86

Please sign in to comment.