Skip to content

Commit cfecaff

Browse files
committed
matdbg: Fix material collision
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).
1 parent 49d8929 commit cfecaff

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

filament/src/details/Material.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,10 @@ void FMaterial::createAndCacheProgram(Program&& p, Variant variant) const noexce
605605
FEngine const& engine = mEngine;
606606
DriverApi& driverApi = mEngine.getDriverApi();
607607

608-
bool const isSharedVariant =
609-
(mMaterialDomain == MaterialDomain::SURFACE) &&
610-
!mIsDefaultMaterial && !mHasCustomDepthShader &&
611-
Variant::isValidDepthVariant(variant);
608+
bool const isShared = isSharedVariant(variant);
612609

613610
// Check if the default material has this program cached
614-
if (isSharedVariant) {
611+
if (isShared) {
615612
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
616613
if (pDefaultMaterial) {
617614
auto program = pDefaultMaterial->mCachedPrograms[variant.key];
@@ -630,7 +627,7 @@ void FMaterial::createAndCacheProgram(Program&& p, Variant variant) const noexce
630627
// If the default material doesn't already have this program cached, and all caching conditions
631628
// are met (Surface Domain and no custom depth shader), cache it now.
632629
// New Materials will inherit these program automatically.
633-
if (isSharedVariant) {
630+
if (isShared) {
634631
FMaterial const* const pDefaultMaterial = engine.getDefaultMaterial();
635632
if (pDefaultMaterial && !pDefaultMaterial->mCachedPrograms[variant.key]) {
636633
// set the tag to the default material name

filament/src/details/Material.h

+12
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ class FMaterial : public Material {
156156
std::unique_lock<utils::Mutex> lock(mActiveProgramsLock);
157157
mActivePrograms.set(variant.key);
158158
lock.unlock();
159+
160+
if (isSharedVariant(variant)) {
161+
FMaterial const* const pDefaultMaterial = mEngine.getDefaultMaterial();
162+
if (pDefaultMaterial && pDefaultMaterial->mCachedPrograms[variant.key]) {
163+
return pDefaultMaterial->getProgram(variant);
164+
}
165+
}
159166
#endif
160167
assert_invariant(mCachedPrograms[variant.key]);
161168
return mCachedPrograms[variant.key];
@@ -290,6 +297,11 @@ class FMaterial : public Material {
290297

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

300+
inline bool isSharedVariant(Variant variant) const {
301+
return (mMaterialDomain == MaterialDomain::SURFACE) && !mIsDefaultMaterial &&
302+
!mHasCustomDepthShader && Variant::isValidDepthVariant(variant);
303+
}
304+
293305
// try to order by frequency of use
294306
mutable std::array<backend::Handle<backend::HwProgram>, VARIANT_COUNT> mCachedPrograms;
295307
DescriptorSetLayout mPerViewDescriptorSetLayout;

libs/matdbg/src/DebugServer.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,17 @@ DebugServer::addMaterial(const CString& name, const void* data, size_t size, voi
193193
return {};
194194
}
195195

196-
const uint32_t seed = 42;
197-
const MaterialKey key = utils::hash::murmurSlow((const uint8_t*) data, size, seed);
196+
// Note that it's possible to have two materials with the exact same content (however wasteful),
197+
// but they refer to different instantiation of FMaterial. For that case, we need to make sure
198+
// that the key for the hash is different by relying on the user provided pointer. Hence we run
199+
// the hash function twice.
200+
constexpr uint32_t seed = 42;
201+
uint64_t appendedData[2] = {
202+
utils::hash::murmurSlow((const uint8_t*) data, size, seed),
203+
(uint64_t) userdata,
204+
};
205+
const uint32_t key =
206+
utils::hash::murmurSlow((const uint8_t*) appendedData, sizeof(appendedData), seed);
198207

199208
// Retain a copy of the package to permit queries after the client application has
200209
// freed up the original material package.

libs/matdbg/web/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ class MaterialSidePanel extends LitElement {
662662
let name = material.name || kUntitledPlaceholder;
663663
if (name in duplicatedLabels) {
664664
const index = duplicatedLabels[name];
665-
name = `${name} (${index})`;
666665
duplicatedLabels[name] = index + 1;
666+
name = `${name} (${index})`;
667667
}
668668
return {
669669
matid: matid,

0 commit comments

Comments
 (0)