Skip to content

Commit

Permalink
matinfo: further refactor out redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
elizagamedev committed Oct 11, 2023
1 parent e4a57ce commit 0887e38
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 141 deletions.
4 changes: 1 addition & 3 deletions libs/matdbg/include/matdbg/ShaderInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ struct ShaderInfo {
};

size_t getShaderCount(const filaflat::ChunkContainer& container, filamat::ChunkType type);
bool getMetalShaderInfo(const filaflat::ChunkContainer& container, ShaderInfo* info);
bool getGlShaderInfo(const filaflat::ChunkContainer& container, ShaderInfo* info, filamat::ChunkType chunkType);
bool getVkShaderInfo(const filaflat::ChunkContainer& container, ShaderInfo* info);
bool getShaderInfo(const filaflat::ChunkContainer& container, ShaderInfo* info, filamat::ChunkType chunkType);

} // namespace matdbg
} // namespace filament
Expand Down
12 changes: 6 additions & 6 deletions libs/matdbg/src/DebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class RestRequestHandler : public CivetHandler {
}

FixedCapacityVector<ShaderInfo> info(getShaderCount(package, ChunkType::MaterialGlsl));
if (!getGlShaderInfo(package, info.data(), ChunkType::MaterialGlsl)) {
if (!getShaderInfo(package, info.data(), ChunkType::MaterialGlsl)) {
return error(__LINE__);
}

Expand Down Expand Up @@ -311,7 +311,7 @@ class RestRequestHandler : public CivetHandler {

filaflat::ShaderContent content;
FixedCapacityVector<ShaderInfo> info(getShaderCount(package, ChunkType::MaterialSpirv));
if (!getVkShaderInfo(package, info.data())) {
if (!getShaderInfo(package, info.data(), ChunkType::MaterialSpirv)) {
return error(__LINE__);
}

Expand Down Expand Up @@ -344,7 +344,7 @@ class RestRequestHandler : public CivetHandler {

filaflat::ShaderContent content;
FixedCapacityVector<ShaderInfo> info(getShaderCount(package, ChunkType::MaterialMetal));
if (!getMetalShaderInfo(package, info.data())) {
if (!getShaderInfo(package, info.data(), ChunkType::MaterialMetal)) {
return error(__LINE__);
}

Expand Down Expand Up @@ -600,7 +600,7 @@ bool DebugServer::handleEditCommand(const MaterialKey& key, backend::Backend api
shaderCount = getShaderCount(package, ChunkType::MaterialGlsl);
infos.reserve(shaderCount);
infos.resize(shaderCount);
if (!getGlShaderInfo(package, infos.data(), ChunkType::MaterialGlsl)) {
if (!getShaderInfo(package, infos.data(), ChunkType::MaterialGlsl)) {
return error(__LINE__);
}
break;
Expand All @@ -609,7 +609,7 @@ bool DebugServer::handleEditCommand(const MaterialKey& key, backend::Backend api
shaderCount = getShaderCount(package, ChunkType::MaterialSpirv);
infos.reserve(shaderCount);
infos.resize(shaderCount);
if (!getVkShaderInfo(package, infos.data())) {
if (!getShaderInfo(package, infos.data(), ChunkType::MaterialSpirv)) {
return error(__LINE__);
}
break;
Expand All @@ -618,7 +618,7 @@ bool DebugServer::handleEditCommand(const MaterialKey& key, backend::Backend api
shaderCount = getShaderCount(package, ChunkType::MaterialMetal);
infos.reserve(shaderCount);
infos.resize(shaderCount);
if (!getMetalShaderInfo(package, infos.data())) {
if (!getShaderInfo(package, infos.data(), ChunkType::MaterialMetal)) {
return error(__LINE__);
}
break;
Expand Down
12 changes: 6 additions & 6 deletions libs/matdbg/src/JsonWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static void printShaderInfo(ostream& json, const vector<ShaderInfo>& info, const
static bool printGlslInfo(ostream& json, const ChunkContainer& container) {
std::vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialGlsl));
if (!getGlShaderInfo(container, info.data(), ChunkType::MaterialGlsl)) {
if (!getShaderInfo(container, info.data(), ChunkType::MaterialGlsl)) {
return false;
}
json << "\"opengl\": [\n";
Expand All @@ -151,7 +151,7 @@ static bool printGlslInfo(ostream& json, const ChunkContainer& container) {
static bool printVkInfo(ostream& json, const ChunkContainer& container) {
std::vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialSpirv));
if (!getVkShaderInfo(container, info.data())) {
if (!getShaderInfo(container, info.data(), ChunkType::MaterialSpirv)) {
return false;
}
json << "\"vulkan\": [\n";
Expand All @@ -163,7 +163,7 @@ static bool printVkInfo(ostream& json, const ChunkContainer& container) {
static bool printMetalInfo(ostream& json, const ChunkContainer& container) {
std::vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialMetal));
if (!getMetalShaderInfo(container, info.data())) {
if (!getShaderInfo(container, info.data(), ChunkType::MaterialMetal)) {
return false;
}
json << "\"metal\": [\n";
Expand Down Expand Up @@ -227,17 +227,17 @@ bool JsonWriter::writeActiveInfo(const filaflat::ChunkContainer& package,
switch (backend) {
case Backend::OPENGL:
shaders.resize(getShaderCount(package, ChunkType::MaterialGlsl));
getGlShaderInfo(package, shaders.data(), ChunkType::MaterialGlsl);
getShaderInfo(package, shaders.data(), ChunkType::MaterialGlsl);
json << "opengl";
break;
case Backend::VULKAN:
shaders.resize(getShaderCount(package, ChunkType::MaterialSpirv));
getVkShaderInfo(package, shaders.data());
getShaderInfo(package, shaders.data(), ChunkType::MaterialSpirv);
json << "vulkan";
break;
case Backend::METAL:
shaders.resize(getShaderCount(package, ChunkType::MaterialMetal));
getMetalShaderInfo(package, shaders.data());
getShaderInfo(package, shaders.data(), ChunkType::MaterialMetal);
json << "metal";
break;
default:
Expand Down
94 changes: 2 additions & 92 deletions libs/matdbg/src/ShaderInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,61 +48,16 @@ size_t getShaderCount(const ChunkContainer& container, ChunkType type) {
return shaderCount;
}

bool getMetalShaderInfo(const ChunkContainer& container, ShaderInfo* info) {
if (!container.hasChunk(ChunkType::MaterialMetal)) {
return true;
}

auto [start, end] = container.getChunkRange(ChunkType::MaterialMetal);
Unflattener unflattener(start, end);

uint64_t shaderCount = 0;
if (!unflattener.read(&shaderCount) || shaderCount == 0) {
return false;
}

for (uint64_t i = 0; i < shaderCount; i++) {
uint8_t shaderModelValue;
Variant variant;
uint8_t pipelineStageValue;
uint32_t offsetValue;

if (!unflattener.read(&shaderModelValue)) {
return false;
}

if (!unflattener.read(&variant)) {
return false;
}

if (!unflattener.read(&pipelineStageValue)) {
return false;
}

if (!unflattener.read(&offsetValue)) {
return false;
}

*info++ = {
.shaderModel = ShaderModel(shaderModelValue),
.variant = variant,
.pipelineStage = ShaderStage(pipelineStageValue),
.offset = offsetValue
};
}

return true;
}

bool getGlShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkType chunkType) {
bool getShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkType chunkType) {
if (!container.hasChunk(chunkType)) {
return true;
}

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

uint64_t shaderCount;
uint64_t shaderCount = 0;
if (!unflattener.read(&shaderCount) || shaderCount == 0) {
return false;
}
Expand Down Expand Up @@ -139,49 +94,4 @@ bool getGlShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkTyp
return true;
}

bool getVkShaderInfo(const ChunkContainer& container, ShaderInfo* info) {
if (!container.hasChunk(ChunkType::MaterialSpirv)) {
return true;
}

auto [start, end] = container.getChunkRange(ChunkType::MaterialSpirv);
Unflattener unflattener(start, end);

uint64_t shaderCount;
if (!unflattener.read(&shaderCount) || shaderCount == 0) {
return false;
}

for (uint64_t i = 0; i < shaderCount; i++) {
uint8_t shaderModelValue;
Variant variant;
uint8_t pipelineStageValue;
uint32_t dictionaryIndex;

if (!unflattener.read(&shaderModelValue)) {
return false;
}

if (!unflattener.read(&variant)) {
return false;
}

if (!unflattener.read(&pipelineStageValue)) {
return false;
}

if (!unflattener.read(&dictionaryIndex)) {
return false;
}

*info++ = {
.shaderModel = ShaderModel(shaderModelValue),
.variant = variant,
.pipelineStage = ShaderStage(pipelineStageValue),
.offset = dictionaryIndex
};
}
return true;
}

} // namespace filament
44 changes: 14 additions & 30 deletions libs/matdbg/src/TextWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,10 @@ static void printShaderInfo(ostream& text, const vector<ShaderInfo>& info,
text << endl;
}

static bool printGlslInfo(ostream& text, const ChunkContainer& container, ChunkType chunkType) {
static bool printShaderInfo(ostream& text, const ChunkContainer& container, ChunkType chunkType) {
vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialGlsl));
if (!getGlShaderInfo(container, info.data(), ChunkType::MaterialGlsl)) {
info.resize(getShaderCount(container, chunkType));
if (!getShaderInfo(container, info.data(), chunkType)) {
return false;
}
switch (chunkType) {
Expand All @@ -415,36 +415,20 @@ static bool printGlslInfo(ostream& text, const ChunkContainer& container, ChunkT
case ChunkType::MaterialEssl1:
text << "ESSL1 shaders:" << endl;
break;
case ChunkType::MaterialSpirv:
text << "Vulkan shaders:" << endl;
break;
case ChunkType::MaterialMetal:
text << "Metal shaders:" << endl;
break;
default:
assert(false && "Invalid GLSL ChunkType");
assert(false && "Invalid shader ChunkType");
break;
}
printShaderInfo(text, info, container);
return true;
}

static bool printVkInfo(ostream& text, const ChunkContainer& container) {
vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialSpirv));
if (!getVkShaderInfo(container, info.data())) {
return false;
}
text << "Vulkan shaders:" << endl;
printShaderInfo(text, info, container);
return true;
}

static bool printMetalInfo(ostream& text, const ChunkContainer& container) {
vector<ShaderInfo> info;
info.resize(getShaderCount(container, ChunkType::MaterialMetal));
if (!getMetalShaderInfo(container, info.data())) {
return false;
}
text << "Metal shaders:" << endl;
printShaderInfo(text, info, container);
return true;
}

bool TextWriter::writeMaterialInfo(const filaflat::ChunkContainer& container) {
ostringstream text;
if (!printMaterial(text, container)) {
Expand All @@ -459,16 +443,16 @@ bool TextWriter::writeMaterialInfo(const filaflat::ChunkContainer& container) {
if (!printSubpassesInfo(text, container)) {
return false;
}
if (!printGlslInfo(text, container, ChunkType::MaterialGlsl)) {
if (!printShaderInfo(text, container, ChunkType::MaterialGlsl)) {
return false;
}
if (!printGlslInfo(text, container, ChunkType::MaterialEssl1)) {
if (!printShaderInfo(text, container, ChunkType::MaterialEssl1)) {
return false;
}
if (!printVkInfo(text, container)) {
if (!printShaderInfo(text, container, ChunkType::MaterialSpirv)) {
return false;
}
if (!printMetalInfo(text, container)) {
if (!printShaderInfo(text, container, ChunkType::MaterialMetal)) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions tools/matinfo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ static bool parseChunks(Config config, void* data, size_t size) {
}

info.resize(getShaderCount(container, filamat::ChunkType::MaterialGlsl));
if (!getGlShaderInfo(container, info.data(), filamat::ChunkType::MaterialGlsl)) {
if (!getShaderInfo(container, info.data(), filamat::ChunkType::MaterialGlsl)) {
std::cerr << "Failed to parse GLSL chunk." << std::endl;
return false;
}
Expand All @@ -461,7 +461,7 @@ static bool parseChunks(Config config, void* data, size_t size) {
}

info.resize(getShaderCount(container, filamat::ChunkType::MaterialEssl1));
if (!getGlShaderInfo(container, info.data(), filamat::ChunkType::MaterialEssl1)) {
if (!getShaderInfo(container, info.data(), filamat::ChunkType::MaterialEssl1)) {
std::cerr << "Failed to parse ESSL1 chunk." << std::endl;
return false;
}
Expand All @@ -487,7 +487,7 @@ static bool parseChunks(Config config, void* data, size_t size) {
}

info.resize(getShaderCount(container, filamat::ChunkType::MaterialSpirv));
if (!getVkShaderInfo(container, info.data())) {
if (!getShaderInfo(container, info.data(), filamat::ChunkType::MaterialSpirv)) {
std::cerr << "Failed to parse SPIRV chunk." << std::endl;
return false;
}
Expand Down Expand Up @@ -523,7 +523,7 @@ static bool parseChunks(Config config, void* data, size_t size) {
}

info.resize(getShaderCount(container, filamat::ChunkType::MaterialMetal));
if (!getMetalShaderInfo(container, info.data())) {
if (!getShaderInfo(container, info.data(), filamat::ChunkType::MaterialMetal)) {
std::cerr << "Failed to parse Metal chunk." << std::endl;
return false;
}
Expand Down

0 comments on commit 0887e38

Please sign in to comment.