Skip to content

Commit

Permalink
matdbg: take desktop/mobile into account for SPIR-V to GLSL translati…
Browse files Browse the repository at this point in the history
…on (google#6488)
  • Loading branch information
bejado authored and plepers committed Dec 9, 2023
1 parent dd8b0f4 commit d9d5e23
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion libs/matdbg/include/matdbg/ShaderExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ShaderExtractor {
Variant variant, backend::ShaderStage stage, filaflat::ShaderContent& shader) noexcept;
bool getDictionary(filaflat::BlobDictionary& dictionary) noexcept;

static utils::CString spirvToGLSL(const uint32_t* data, size_t wordCount);
static utils::CString spirvToGLSL(backend::ShaderModel shaderModel, const uint32_t* data,
size_t wordCount);
static utils::CString spirvToText(const uint32_t* data, size_t wordCount);

private:
Expand Down
7 changes: 4 additions & 3 deletions libs/matdbg/src/DebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ static void spirvToAsm(struct mg_connection *conn, const uint32_t* spirv, size_t
spvContextDestroy(context);
}

static void spirvToGlsl(struct mg_connection *conn, const uint32_t* spirv, size_t size) {
auto glsl = ShaderExtractor::spirvToGLSL(spirv, size / 4);
static void spirvToGlsl(ShaderModel shaderModel, struct mg_connection *conn, const uint32_t* spirv,
size_t size) {
auto glsl = ShaderExtractor::spirvToGLSL(shaderModel, spirv, size / 4);
mg_printf(conn, kSuccessHeader.data(), "application/txt");
mg_printf(conn, glsl.c_str(), glsl.size());
}
Expand Down Expand Up @@ -335,7 +336,7 @@ class RestRequestHandler : public CivetHandler {
}

if (language == glsl) {
spirvToGlsl(conn, (const uint32_t*) content.data(), content.size());
spirvToGlsl(item.shaderModel, conn, (const uint32_t*) content.data(), content.size());
return true;
}

Expand Down
12 changes: 9 additions & 3 deletions libs/matdbg/src/ShaderExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ bool ShaderExtractor::getShader(ShaderModel shaderModel,
uint8_t(shaderModel), variant, uint8_t(stage));
}

CString ShaderExtractor::spirvToGLSL(const uint32_t* data, size_t wordCount) {
CString ShaderExtractor::spirvToGLSL(ShaderModel shaderModel, const uint32_t* data,
size_t wordCount) {
using namespace spirv_cross;

CompilerGLSL::Options emitOptions;
emitOptions.es = false;
emitOptions.version = 410;
if (shaderModel == ShaderModel::MOBILE) {
emitOptions.es = true;
emitOptions.version = 310;
} else if (shaderModel == ShaderModel::DESKTOP) {
emitOptions.es = false;
emitOptions.version = 410;
}
emitOptions.vulkan_semantics = true;

std::vector<uint32_t> spirv(data, data + wordCount);
Expand Down
7 changes: 4 additions & 3 deletions tools/matinfo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ static std::ifstream::pos_type getFileSize(const char* filename) {
}

// Consumes SPIRV binary and produces a GLSL-ES string.
static void transpileSpirv(const std::vector<uint32_t>& spirv) {
std::cout << ShaderExtractor::spirvToGLSL(spirv.data(), spirv.size()).c_str();
static void transpileSpirv(filament::backend::ShaderModel shaderModel,
const std::vector<uint32_t>& spirv) {
std::cout << ShaderExtractor::spirvToGLSL(shaderModel, spirv.data(), spirv.size()).c_str();
}

// Consumes SPIRV binary and produces an ordered map from "line number" to "GLSL string" where
Expand Down Expand Up @@ -464,7 +465,7 @@ static bool parseChunks(Config config, void* data, size_t size) {
const std::vector<uint32_t> spirv(words, words + content.size() / 4);

if (config.transpile) {
transpileSpirv(spirv);
transpileSpirv(item.shaderModel, spirv);
} else if (config.binary) {
dumpSpirvBinary(spirv, "out.spv");
} else {
Expand Down

0 comments on commit d9d5e23

Please sign in to comment.