From 0887e388dbb9fd496fa474a5af8896e39ece6226 Mon Sep 17 00:00:00 2001 From: Eliza Velasquez Date: Wed, 11 Oct 2023 14:10:11 -0700 Subject: [PATCH] matinfo: further refactor out redundant code --- libs/matdbg/include/matdbg/ShaderInfo.h | 4 +- libs/matdbg/src/DebugServer.cpp | 12 ++-- libs/matdbg/src/JsonWriter.cpp | 12 ++-- libs/matdbg/src/ShaderInfo.cpp | 94 +------------------------ libs/matdbg/src/TextWriter.cpp | 44 ++++-------- tools/matinfo/src/main.cpp | 8 +-- 6 files changed, 33 insertions(+), 141 deletions(-) diff --git a/libs/matdbg/include/matdbg/ShaderInfo.h b/libs/matdbg/include/matdbg/ShaderInfo.h index 2960b28e1c2..89d5a8377a4 100644 --- a/libs/matdbg/include/matdbg/ShaderInfo.h +++ b/libs/matdbg/include/matdbg/ShaderInfo.h @@ -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 diff --git a/libs/matdbg/src/DebugServer.cpp b/libs/matdbg/src/DebugServer.cpp index 219959d3c11..b52375197ef 100644 --- a/libs/matdbg/src/DebugServer.cpp +++ b/libs/matdbg/src/DebugServer.cpp @@ -280,7 +280,7 @@ class RestRequestHandler : public CivetHandler { } FixedCapacityVector info(getShaderCount(package, ChunkType::MaterialGlsl)); - if (!getGlShaderInfo(package, info.data(), ChunkType::MaterialGlsl)) { + if (!getShaderInfo(package, info.data(), ChunkType::MaterialGlsl)) { return error(__LINE__); } @@ -311,7 +311,7 @@ class RestRequestHandler : public CivetHandler { filaflat::ShaderContent content; FixedCapacityVector info(getShaderCount(package, ChunkType::MaterialSpirv)); - if (!getVkShaderInfo(package, info.data())) { + if (!getShaderInfo(package, info.data(), ChunkType::MaterialSpirv)) { return error(__LINE__); } @@ -344,7 +344,7 @@ class RestRequestHandler : public CivetHandler { filaflat::ShaderContent content; FixedCapacityVector info(getShaderCount(package, ChunkType::MaterialMetal)); - if (!getMetalShaderInfo(package, info.data())) { + if (!getShaderInfo(package, info.data(), ChunkType::MaterialMetal)) { return error(__LINE__); } @@ -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; @@ -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; @@ -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; diff --git a/libs/matdbg/src/JsonWriter.cpp b/libs/matdbg/src/JsonWriter.cpp index 619022dd92a..c9e94c8143d 100644 --- a/libs/matdbg/src/JsonWriter.cpp +++ b/libs/matdbg/src/JsonWriter.cpp @@ -139,7 +139,7 @@ static void printShaderInfo(ostream& json, const vector& info, const static bool printGlslInfo(ostream& json, const ChunkContainer& container) { std::vector 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"; @@ -151,7 +151,7 @@ static bool printGlslInfo(ostream& json, const ChunkContainer& container) { static bool printVkInfo(ostream& json, const ChunkContainer& container) { std::vector info; info.resize(getShaderCount(container, ChunkType::MaterialSpirv)); - if (!getVkShaderInfo(container, info.data())) { + if (!getShaderInfo(container, info.data(), ChunkType::MaterialSpirv)) { return false; } json << "\"vulkan\": [\n"; @@ -163,7 +163,7 @@ static bool printVkInfo(ostream& json, const ChunkContainer& container) { static bool printMetalInfo(ostream& json, const ChunkContainer& container) { std::vector info; info.resize(getShaderCount(container, ChunkType::MaterialMetal)); - if (!getMetalShaderInfo(container, info.data())) { + if (!getShaderInfo(container, info.data(), ChunkType::MaterialMetal)) { return false; } json << "\"metal\": [\n"; @@ -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: diff --git a/libs/matdbg/src/ShaderInfo.cpp b/libs/matdbg/src/ShaderInfo.cpp index 2c39e8f9dca..cba7bf648df 100644 --- a/libs/matdbg/src/ShaderInfo.cpp +++ b/libs/matdbg/src/ShaderInfo.cpp @@ -48,53 +48,8 @@ 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; } @@ -102,7 +57,7 @@ bool getGlShaderInfo(const ChunkContainer& container, ShaderInfo* info, ChunkTyp 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; } @@ -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 diff --git a/libs/matdbg/src/TextWriter.cpp b/libs/matdbg/src/TextWriter.cpp index a6ebcc5d6b7..b74596f6a42 100644 --- a/libs/matdbg/src/TextWriter.cpp +++ b/libs/matdbg/src/TextWriter.cpp @@ -402,10 +402,10 @@ static void printShaderInfo(ostream& text, const vector& info, text << endl; } -static bool printGlslInfo(ostream& text, const ChunkContainer& container, ChunkType chunkType) { +static bool printShaderInfo(ostream& text, const ChunkContainer& container, ChunkType chunkType) { vector 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) { @@ -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 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 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)) { @@ -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; } diff --git a/tools/matinfo/src/main.cpp b/tools/matinfo/src/main.cpp index f9fff121c36..d5920023721 100644 --- a/tools/matinfo/src/main.cpp +++ b/tools/matinfo/src/main.cpp @@ -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; } @@ -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; } @@ -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; } @@ -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; }