mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Chore] (MG_Util/Program): wraps spirv-cross-c
This commit is contained in:
@@ -198,43 +198,29 @@ namespace MobileGL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Result<String> ShaderCompiler::DecompileShader(Vector<unsigned int> spirv) {
|
Result<String> ShaderCompiler::DecompileShader(Vector<unsigned int> spirv) {
|
||||||
spvc_context context = nullptr;
|
SpvcSession session(spirv);
|
||||||
spvc_parsed_ir ir = nullptr;
|
spvc_compiler_options options;
|
||||||
spvc_compiler compiler_glsl = nullptr;
|
session.CreateOptions(&options);
|
||||||
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);
|
|
||||||
|
|
||||||
spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_GLSL_VERSION, 450);
|
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_ES, SPVC_FALSE);
|
||||||
spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_GLSL_VULKAN_SEMANTICS, SPVC_TRUE);
|
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) {
|
if (!result) {
|
||||||
ResultInfo r;
|
ResultInfo r;
|
||||||
r.log += "Failed to compile the shader to GLSL: \n";
|
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;
|
r.errc = -5;
|
||||||
return std::unexpected(r);
|
return std::unexpected(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string glsl = result;
|
std::string glsl = result;
|
||||||
|
|
||||||
spvc_context_destroy(context);
|
|
||||||
|
|
||||||
return glsl;
|
return glsl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,41 @@ namespace MobileGL {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
using Result = std::expected<T, ResultInfo>;
|
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) {
|
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
|
||||||
switch (shaderType) {
|
switch (shaderType) {
|
||||||
case GL_VERTEX_SHADER:
|
case GL_VERTEX_SHADER:
|
||||||
|
|||||||
Reference in New Issue
Block a user