Skip to content

Commit

Permalink
matdbg: fix variant issue for surface materials (#8503)
Browse files Browse the repository at this point in the history
- The set of active variants were not set correctly for surface
   materials since the frag/vert pairs could have different
   variant.
 - Fix the default selection logic in the UI
 - Random clean-ups

FIXES=328699979
  • Loading branch information
poweifeng authored Mar 7, 2025
1 parent 7199961 commit 5091b31
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
9 changes: 8 additions & 1 deletion filament/src/details/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ class FMaterial : public Material {
#if FILAMENT_ENABLE_MATDBG
assert_invariant((size_t)variant.key < VARIANT_COUNT);
std::unique_lock<utils::Mutex> lock(mActiveProgramsLock);
mActivePrograms.set(variant.key);
if (getMaterialDomain() == MaterialDomain::SURFACE) {
auto vert = Variant::filterVariantVertex(variant);
auto frag = Variant::filterVariantFragment(variant);
mActivePrograms.set(vert.key);
mActivePrograms.set(frag.key);
} else {
mActivePrograms.set(variant.key);
}
lock.unlock();

if (isSharedVariant(variant)) {
Expand Down
2 changes: 1 addition & 1 deletion libs/matdbg/src/ApiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ bool ApiHandler::handleGet(CivetServer* server, struct mg_connection* conn) {
return error(__LINE__, uri);
}
bool const last = (++index) == mServer->mMaterialRecords.size();
mg_printf(conn, "\"%8.8x\": %s %s", pair.first, writer.getJsonString(),
mg_printf(conn, "\"%8.8x\": %s%s", pair.first, writer.getJsonString(),
last ? "" : ",");
}
mg_printf(conn, "}");
Expand Down
13 changes: 13 additions & 0 deletions libs/matdbg/src/ShaderInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <matdbg/ShaderInfo.h>

#include "CommonWriter.h"

#include <filaflat/ChunkContainer.h>
#include <filaflat/DictionaryReader.h>
#include <filaflat/MaterialChunk.h>
Expand Down Expand Up @@ -54,6 +56,11 @@ bool getShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkType
return true;
}

MaterialDomain domain;
if (!read(container, ChunkType::MaterialDomain, reinterpret_cast<uint8_t*>(&domain))) {
return false;
}

auto [start, end] = container.getChunkRange(chunkType);
Unflattener unflattener(start, end);

Expand Down Expand Up @@ -84,6 +91,12 @@ bool getShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkType
return false;
}

auto stage = ShaderStage(pipelineStageValue);
if (domain == MaterialDomain::SURFACE) {
variant = stage == ShaderStage::VERTEX ?
Variant::filterVariantVertex(variant) :
Variant::filterVariantFragment(variant);
}
*info++ = {
.shaderModel = ShaderModel(shaderModelValue),
.variant = variant,
Expand Down
8 changes: 4 additions & 4 deletions libs/matdbg/web/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ async function fetchMatIds() {
}

async function queryActiveShaders() {
const activeMaterials = await _fetchJson("api/active");
const activeVariants = await _fetchJson("api/active");
const actives = {};
for (matid in activeMaterials) {
const backend = activeMaterials[matid][0];
const variants = activeMaterials[matid].slice(1);
for (matid in activeVariants) {
const backend = activeVariants[matid][0];
const variants = activeVariants[matid].slice(1);
actives[matid] = {
backend, variants
};
Expand Down
29 changes: 9 additions & 20 deletions libs/matdbg/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,29 +702,19 @@ class MaterialSidePanel extends LitElement {

updated(props) {
if (props.has('database')) {
const items = [];

// Names need not be unique, so we display a numeric suffix for non-unique names.
// To achieve stable ordering of anonymous materials, we first sort by matid.
const labels = new Set();
const matids = Object.keys(this.database).sort();
const duplicatedLabels = {};
for (const matid of matids) {
const name = this.database[matid].name || kUntitledPlaceholder;
if (labels.has(name)) {
duplicatedLabels[name] = 0;
} else {
labels.add(name);
}
}
const names = matids.map(matid => (this.database[matid].name || kUntitledPlaceholder));
const labelCount = {};
names.forEach(name => labelCount[name] = (labelCount[name] || 0) + 1);
const duplicates = names.filter(name => labelCount[name] > 1);

this.materials = matids.map((matid) => {
const material = this.database[matid];
let name = material.name || kUntitledPlaceholder;
if (name in duplicatedLabels) {
const index = duplicatedLabels[name];
duplicatedLabels[name] = index + 1;
name = `${name} (${index})`;
if (duplicates.includes(name)) {
name = `${name} (${labelCount[name]--})`;
}
return {
matid: matid,
Expand Down Expand Up @@ -938,8 +928,7 @@ class MatdbgViewer extends LitElement {
}
);

let materials = await fetchMaterials();
this.database = materials;
this.database = await fetchMaterials();

// This is the user preferences stored in localStorage
let hideInactiveVariantsVal = localStorage.getItem('option-hide-inactive-variants');
Expand Down Expand Up @@ -1070,8 +1059,8 @@ class MatdbgViewer extends LitElement {
const activeVariants = this.activeShaders[this.currentMaterial].variants;
const materialShaders = material[this.currentBackend];
for (let shader in materialShaders) {
let ind = activeVariants.indexOf(+shader);
if (ind >= 0) {
const shaderVariant = materialShaders[shader].variant;
if (activeVariants.indexOf(shaderVariant) >= 0) {
this.currentShaderIndex = +shader;
break;
}
Expand Down

0 comments on commit 5091b31

Please sign in to comment.