[Chore] (MG_Util/Program): wraps spirv-cross-c

This commit is contained in:
2025-07-21 21:39:04 +08:00
parent 97a951fabb
commit 01e71d7b5f
2 changed files with 44 additions and 23 deletions
@@ -198,43 +198,29 @@ namespace MobileGL {
}
Result<String> ShaderCompiler::DecompileShader(Vector<unsigned int> spirv) {
spvc_context context = nullptr;
spvc_parsed_ir ir = nullptr;
spvc_compiler compiler_glsl = nullptr;
spvc_compiler_options options = nullptr;
spvc_resources resources = nullptr;
const spvc_reflected_resource *list = nullptr;
const char *result = nullptr;
size_t count;
const SpvId *p_spirv = spirv.data();
size_t word_count = spirv.size();
spvc_context_create(&context);
spvc_context_parse_spirv(context, p_spirv, word_count, &ir);
spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler_glsl);
spvc_compiler_create_shader_resources(compiler_glsl, &resources);
spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, &list, &count);
spvc_compiler_create_compiler_options(compiler_glsl, &options);
SpvcSession session(spirv);
spvc_compiler_options options;
session.CreateOptions(&options);
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_GLSL_VERSION, 450);
spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_GLSL_ES, SPVC_FALSE);
spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_GLSL_VULKAN_SEMANTICS, SPVC_TRUE);
spvc_compiler_install_compiler_options(compiler_glsl, options);
spvc_compiler_compile(compiler_glsl, &result);
session.SetOptions(options);
const char *result = nullptr;
session.Compile(&result);
if (!result) {
ResultInfo r;
r.log += "Failed to compile the shader to GLSL: \n";
r.log += spvc_context_get_last_error_string(context);
r.log += session.GetLastErrorString();
r.errc = -5;
return std::unexpected(r);
}
std::string glsl = result;
spvc_context_destroy(context);
return glsl;
}
}
+35
View File
@@ -28,6 +28,41 @@ namespace MobileGL {
template <typename T>
using Result = std::expected<T, ResultInfo>;
struct SpvcSession {
SpvcSession(Vector<unsigned int> spirv) {
const SpvId *p_spirv = spirv.data();
size_t word_count = spirv.size();
spvc_context_create(&context);
spvc_context_parse_spirv(context, p_spirv, word_count, &ir);
spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler);
}
spvc_result CreateOptions(spvc_compiler_options *options) {
return spvc_compiler_create_compiler_options(compiler, options);
}
spvc_result SetOptions(spvc_compiler_options options) {
compiler_options = options;
return spvc_compiler_install_compiler_options(compiler, options);
}
spvc_result Compile(const char** result) {
return spvc_compiler_compile(compiler, result);
}
const char* GetLastErrorString() {
return spvc_context_get_last_error_string(context);
}
~SpvcSession() { spvc_context_destroy(context); }
private:
spvc_context context = nullptr;
spvc_parsed_ir ir = nullptr;
spvc_compiler compiler = nullptr;
spvc_compiler_options compiler_options = nullptr;
};
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
switch (shaderType) {
case GL_VERTEX_SHADER: