Skip to content

Commit

Permalink
matdbg: indicate shader model (#8504)
Browse files Browse the repository at this point in the history
Shader model (desktop or mobile) wasn't really accounted for
in the UI. This means that we will get shaders that look like
duplicates (same variant). In this work, we pass the current
shader model from engine into the frontend and filter out
variants of a different shader model.

Moreover, for matinfo, we use a specific dbg shader model (matinfo)
to indicate it is in that mode. We add UI in matinfo to show the
shadermodel.

So UI updates as well.
  • Loading branch information
poweifeng authored Mar 7, 2025
1 parent ee68872 commit e825f43
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 62 deletions.
3 changes: 2 additions & 1 deletion filament/src/details/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ int FEngine::loop() {
#endif
if (portString != nullptr) {
const int port = atoi(portString);
debug.server = new matdbg::DebugServer(mBackend, mDriver->getShaderLanguage(), port);
debug.server = new matdbg::DebugServer(mBackend, mDriver->getShaderLanguage(),
matdbg::DbgShaderModel((uint8_t) mDriver->getShaderModel()), port);

// Sometimes the server can fail to spin up (e.g. if the above port is already in use).
// When this occurs, carry onward, developers can look at civetweb.txt for details.
Expand Down
18 changes: 15 additions & 3 deletions libs/matdbg/include/matdbg/DebugServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ struct MaterialRecord {
VariantList activeVariants;
};

// Matches DriverEnums' ShaderModel
enum class DbgShaderModel {
MOBILE = 1, //!< Mobile level functionality
DESKTOP = 2, //!< Desktop level functionality
MATINFO = 10, //!< To indicate debug server is running from matinfo
};

/**
* Server-side material debugger.
*
Expand All @@ -52,7 +59,8 @@ class DebugServer {
static std::string_view const kSuccessHeader;
static std::string_view const kErrorHeader;

DebugServer(backend::Backend backend, backend::ShaderLanguage shaderLanguage, int port);
DebugServer(backend::Backend backend, backend::ShaderLanguage shaderLanguage,
DbgShaderModel perferredShaderModel, int port);
~DebugServer();

/**
Expand Down Expand Up @@ -85,19 +93,23 @@ class DebugServer {
bool isReady() const { return mServer; }

private:
// called from ApiHandler
MaterialRecord const* getRecord(const MaterialKey& key) const;

// called from ApiHandler
void updateActiveVariants();

// called from ApiHandler
/**
* Replaces the entire content of a particular shader variant. The given shader index uses the
* same ordering that the variants have within the package.
*/
bool handleEditCommand(const MaterialKey& mat, backend::Backend api, int shaderIndex,
const char* newShaderContent, size_t newShaderLength);

const backend::Backend mBackend;
const backend::ShaderLanguage mShaderLanguage;
backend::Backend const mBackend;
backend::ShaderLanguage const mShaderLanguage;
DbgShaderModel const mPreferredShaderModel;

CivetServer* mServer;

Expand Down
5 changes: 4 additions & 1 deletion libs/matdbg/include/matdbg/JsonWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef MATDBG_JSONWRITER_H
#define MATDBG_JSONWRITER_H

#include <matdbg/DebugServer.h>

#include <filaflat/ChunkContainer.h>

#include <backend/DriverEnums.h>
Expand Down Expand Up @@ -47,7 +49,8 @@ class JsonWriter {
// shader index is an active variant. Each bit in the activeVariants bitmask
// represents one of the possible variant combinations.
bool writeActiveInfo(const filaflat::ChunkContainer& package,
backend::ShaderLanguage shaderLanguage, VariantList activeVariants);
backend::ShaderLanguage shaderLanguage, DbgShaderModel shaderModel,
VariantList activeVariants);

private:
utils::CString mJsonString;
Expand Down
37 changes: 19 additions & 18 deletions libs/matdbg/src/ApiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ bool ApiHandler::handleGet(CivetServer* server, struct mg_connection* conn) {
return error(__LINE__, uri);
}
JsonWriter writer;
if (!writer.writeActiveInfo(package, mServer->mShaderLanguage, record.activeVariants)) {
if (!writer.writeActiveInfo(package, mServer->mShaderLanguage,
mServer->mPreferredShaderModel, record.activeVariants)) {
return error(__LINE__, uri);
}
bool const last = (++index) == mServer->mMaterialRecords.size();
Expand All @@ -356,24 +357,30 @@ bool ApiHandler::handleGet(CivetServer* server, struct mg_connection* conn) {
return true;
}

auto writeMaterialRecord = [&](JsonWriter* writer, MaterialRecord const* record) {
ChunkContainer package(record->package, record->packageSize);
if (!package.parse()) {
return error(__LINE__, uri);
}

if (!writer->writeMaterialInfo(package)) {
return error(__LINE__, uri);
}
return true;
};

if (uri == "/api/materials") {
std::unique_lock const lock(mServer->mMaterialRecordsMutex);
mg_printf(conn, kSuccessHeader.data(), "application/json");
mg_printf(conn, "[");
int index = 0;
for (auto const& record: mServer->mMaterialRecords) {
bool const last = (++index) == mServer->mMaterialRecords.size();

ChunkContainer package(record.second.package, record.second.packageSize);
if (!package.parse()) {
return error(__LINE__, uri);
}

auto const& mat = record.second;
JsonWriter writer;
if (!writer.writeMaterialInfo(package)) {
return error(__LINE__, uri);
if (!writeMaterialRecord(&writer, &mat)) {
return false;
}

mg_printf(conn, "{ \"matid\": \"%8.8x\", %s } %s", record.first, writer.getJsonString(),
last ? "" : ",");
}
Expand All @@ -386,15 +393,9 @@ bool ApiHandler::handleGet(CivetServer* server, struct mg_connection* conn) {
if (!result) {
return error(__LINE__, uri);
}

ChunkContainer package(result->package, result->packageSize);
if (!package.parse()) {
return error(__LINE__, uri);
}

JsonWriter writer;
if (!writer.writeMaterialInfo(package)) {
return error(__LINE__, uri);
if (!writeMaterialRecord(&writer, result)) {
return false;
}
mg_printf(conn, kSuccessHeader.data(), "application/json");
mg_printf(conn, "{ %s }", writer.getJsonString());
Expand Down
7 changes: 5 additions & 2 deletions libs/matdbg/src/DebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ class FileRequestHandler : public CivetHandler {
DebugServer* mServer;
};

DebugServer::DebugServer(Backend backend, ShaderLanguage shaderLanguage, int port)
: mBackend(backend), mShaderLanguage(shaderLanguage) {
DebugServer::DebugServer(Backend backend, ShaderLanguage shaderLanguage,
DbgShaderModel perferredShaderModel, int port)
: mBackend(backend),
mShaderLanguage(shaderLanguage),
mPreferredShaderModel(perferredShaderModel) {

#if !SERVE_FROM_SOURCE_TREE
ASSET_MAP["/index.html"] = {
Expand Down
15 changes: 14 additions & 1 deletion libs/matdbg/src/JsonWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ size_t JsonWriter::getJsonSize() const {
}

bool JsonWriter::writeActiveInfo(const filaflat::ChunkContainer& package,
ShaderLanguage shaderLanguage, VariantList activeVariants) {
ShaderLanguage shaderLanguage, DbgShaderModel shaderModel, VariantList activeVariants) {
vector<ShaderInfo> shaders;
ostringstream json;
json << "[\"";
Expand Down Expand Up @@ -260,7 +260,20 @@ bool JsonWriter::writeActiveInfo(const filaflat::ChunkContainer& package,
shaders.resize(getShaderCount(package, chunkType));
getShaderInfo(package, shaders.data(), chunkType);

json << "\", \"";
switch (shaderModel) {
case DbgShaderModel::DESKTOP:
json << toString(ShaderModel::DESKTOP);
break;
case DbgShaderModel::MOBILE:
json << toString(ShaderModel::MOBILE);
break;
case DbgShaderModel::MATINFO:
json << "matinfo";
break;
}
json << "\"";

for (size_t variant = 0; variant < activeVariants.size(); variant++) {
if (activeVariants[variant]) {
json << ", " << variant;
Expand Down
1 change: 0 additions & 1 deletion libs/matdbg/src/ShaderInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ size_t getShaderCount(const ChunkContainer& container, ChunkType type) {
return shaderCount;
}


bool getShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkType chunkType) {
if (!container.hasChunk(chunkType)) {
return true;
Expand Down
7 changes: 3 additions & 4 deletions libs/matdbg/web/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ async function queryActiveShaders() {
const actives = {};
for (matid in activeVariants) {
const backend = activeVariants[matid][0];
const variants = activeVariants[matid].slice(1);
actives[matid] = {
backend, variants
};
const shaderModel = activeVariants[matid][1];
const variants = activeVariants[matid].slice(2);
actives[matid] = { backend, shaderModel, variants };
}
return actives;
}
Expand Down
Loading

0 comments on commit e825f43

Please sign in to comment.