[Feat] (MG_Util/ShaderTranspiler): separate SpvcSession

This commit is contained in:
2025-08-07 12:53:51 +08:00
parent 70d66d320f
commit 38c0a54fcc
5 changed files with 236 additions and 101 deletions
+3
View File
@@ -131,3 +131,6 @@ inline int __android_log_print(int prio, const char* tag, const char* fmt, ...)
#include "MG_Util/Converters/GLToMG/DataTypeConverter.h"
#include "MG_Util/Converters/GLToStr/GLEnumConverter.h"
#include "MG_Util/Converters/GLToGlslang/GLShaderLangConverter.h"
#include "MG_Util/ShaderTranspiler/SpvcSession.h"
#include "MG_Util/ShaderTranspiler/ShaderCompiler.h"
+1 -3
View File
@@ -57,11 +57,9 @@ add_executable(
ProgramTest.cpp
${MGL_ROOT}/MobileGL/MG_Util/Converters/GLToStr/GLEnumConverter.cpp
# ${MGL_ROOT}/MobileGL/MG_Util/Pipelines/ShaderCompilationPipeline.cpp
# ${MGL_ROOT}/MobileGL/MG_Util/Pipelines/ProgramLinkingPipeline.cpp
${MGL_ROOT}/MobileGL/MG_Util/Converters/GLToGlslang/GLShaderLangConverter.cpp
${MGL_ROOT}/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.cpp
# ${MGL_ROOT}/MobileGL/MG_Util/ShaderTranspiler/glslang/UniformTraverser.cpp
${MGL_ROOT}/MobileGL/MG_Util/ShaderTranspiler/SpvcSession.cpp
)
target_include_directories(ProgramTest PRIVATE
@@ -0,0 +1,89 @@
#include "../../Includes.h"
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
SpvcSession::SpvcSession(const 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_compiler_create_shader_resources(compiler, &resources);
}
SpvcSession::SpvcSession(SpvcSession&& that) {
std::swap(this->context, that.context);
std::swap(this->compiler, that.compiler);
std::swap(this->ir, that.ir);
std::swap(this->compiler_options, that.compiler_options);
std::swap(this->resources, that.resources);
}
SpvcSession& SpvcSession::operator=(SpvcSession&& that) {
std::swap(this->context, that.context);
std::swap(this->compiler, that.compiler);
std::swap(this->ir, that.ir);
std::swap(this->compiler_options, that.compiler_options);
std::swap(this->resources, that.resources);
return *this;
}
spvc_result SpvcSession::CreateOptions(spvc_compiler_options *options) {
return spvc_compiler_create_compiler_options(compiler, options);
}
spvc_result SpvcSession::SetOptions(spvc_compiler_options options) {
compiler_options = options;
return spvc_compiler_install_compiler_options(compiler, options);
}
Vector<InterfaceVariable> SpvcSession::GetShaderInterface(spvc_resource_type resource_type) const {
const spvc_reflected_resource *list = nullptr;
size_t count = 0;
spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
Vector<InterfaceVariable> variables;
for (size_t i = 0; i < count; ++i) {
if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
continue;
}
InterfaceVariable var;
var.name = list[i].name;
var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
variables.push_back(var);
if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
size_t num_members = spvc_type_get_num_member_types(type);
printf("uniform %s {\n", var.name.c_str());
for (size_t j = 0; j < num_members; ++j) {
const char *memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, j);
// auto memberTypeId = spvc_type_get_member_type(type, j);
// auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
unsigned memberOffset = 0;
spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
printf("\b%s; // %u\n", memberName, memberOffset);
}
printf("}\n");
}
}
std::sort(variables.begin(), variables.end());
return variables;
}
spvc_result SpvcSession::Compile(const char** result) const {
return spvc_compiler_compile(compiler, result);
}
const char* SpvcSession::GetLastErrorString() const {
return spvc_context_get_last_error_string(context);
}
SpvcSession::~SpvcSession() { spvc_context_destroy(context); }
}
}
}
@@ -0,0 +1,47 @@
//
// Created by Swung0x48 on 2025/8/7.
//
#ifndef SPVCSESSION_H
#define SPVCSESSION_H
#pragma once
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
class SpvcSession {
public:
SpvcSession() {}
explicit SpvcSession(const Vector<unsigned int>& spirv);
SpvcSession(SpvcSession&) = delete;
SpvcSession(SpvcSession&& that);
~SpvcSession();
SpvcSession& operator=(SpvcSession& session) = delete;
SpvcSession& operator=(SpvcSession&& that);
spvc_result CreateOptions(spvc_compiler_options *options);
spvc_result SetOptions(spvc_compiler_options options);
Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const;
spvc_result Compile(const char** result) const;
const char* GetLastErrorString() const;
private:
spvc_context context = nullptr;
spvc_parsed_ir ir = nullptr;
spvc_compiler compiler = nullptr;
spvc_compiler_options compiler_options = nullptr;
spvc_resources resources = nullptr;
};
}
}
}
#endif //SPVCSESSION_H
+96 -98
View File
@@ -37,102 +37,102 @@ namespace MobileGL {
}
};
class SpvcSession {
public:
SpvcSession() {}
explicit SpvcSession(const 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_compiler_create_shader_resources(compiler, &resources);
}
SpvcSession(SpvcSession&) = delete;
SpvcSession(SpvcSession&& that) {
std::swap(this->context, that.context);
std::swap(this->compiler, that.compiler);
std::swap(this->ir, that.ir);
std::swap(this->compiler_options, that.compiler_options);
std::swap(this->resources, that.resources);
}
SpvcSession& operator=(SpvcSession& session) = delete;
SpvcSession& operator=(SpvcSession&& that) {
std::swap(this->context, that.context);
std::swap(this->compiler, that.compiler);
std::swap(this->ir, that.ir);
std::swap(this->compiler_options, that.compiler_options);
std::swap(this->resources, that.resources);
return *this;
}
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);
}
Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const {
const spvc_reflected_resource *list = nullptr;
size_t count = 0;
spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
Vector<InterfaceVariable> variables;
for (size_t i = 0; i < count; ++i) {
if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
continue;
}
InterfaceVariable var;
var.name = list[i].name;
var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
variables.push_back(var);
if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
size_t num_members = spvc_type_get_num_member_types(type);
printf("uniform %s {\n", var.name.c_str());
for (size_t j = 0; j < num_members; ++j) {
const char *memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, j);
// auto memberTypeId = spvc_type_get_member_type(type, j);
// auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
unsigned memberOffset = 0;
spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
printf("\b%s; // %u\n", memberName, memberOffset);
}
printf("}\n");
}
}
std::sort(variables.begin(), variables.end());
return variables;
}
spvc_result Compile(const char** result) const {
return spvc_compiler_compile(compiler, result);
}
const char* GetLastErrorString() const {
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;
spvc_resources resources = nullptr;
};
// class SpvcSession {
// public:
// SpvcSession() {}
//
// explicit SpvcSession(const 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_compiler_create_shader_resources(compiler, &resources);
// }
//
// SpvcSession(SpvcSession&) = delete;
//
// SpvcSession(SpvcSession&& that) {
// std::swap(this->context, that.context);
// std::swap(this->compiler, that.compiler);
// std::swap(this->ir, that.ir);
// std::swap(this->compiler_options, that.compiler_options);
// std::swap(this->resources, that.resources);
// }
//
// SpvcSession& operator=(SpvcSession& session) = delete;
//
// SpvcSession& operator=(SpvcSession&& that) {
// std::swap(this->context, that.context);
// std::swap(this->compiler, that.compiler);
// std::swap(this->ir, that.ir);
// std::swap(this->compiler_options, that.compiler_options);
// std::swap(this->resources, that.resources);
// return *this;
// }
//
// 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);
// }
//
// Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const {
// const spvc_reflected_resource *list = nullptr;
// size_t count = 0;
// spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
//
// Vector<InterfaceVariable> variables;
// for (size_t i = 0; i < count; ++i) {
// if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
// continue;
// }
//
// InterfaceVariable var;
// var.name = list[i].name;
// var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
// variables.push_back(var);
//
// if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
// spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
// size_t num_members = spvc_type_get_num_member_types(type);
// printf("uniform %s {\n", var.name.c_str());
// for (size_t j = 0; j < num_members; ++j) {
// const char *memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, j);
// // auto memberTypeId = spvc_type_get_member_type(type, j);
// // auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
//
// unsigned memberOffset = 0;
// spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
// printf("\b%s; // %u\n", memberName, memberOffset);
// }
// printf("}\n");
// }
// }
// std::sort(variables.begin(), variables.end());
// return variables;
// }
//
// spvc_result Compile(const char** result) const {
// return spvc_compiler_compile(compiler, result);
// }
//
// const char* GetLastErrorString() const {
// 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;
// spvc_resources resources = nullptr;
// };
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
switch (shaderType) {
@@ -155,5 +155,3 @@ namespace MobileGL {
}
}
}
#include "ShaderCompiler.h"