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

matdbg: Fix material collision #8322

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
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
Loading