mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Feat] (MG_Util/ShaderTranspiler): separate SpvcSession
This commit is contained in:
@@ -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/GLToMG/DataTypeConverter.h"
|
||||||
#include "MG_Util/Converters/GLToStr/GLEnumConverter.h"
|
#include "MG_Util/Converters/GLToStr/GLEnumConverter.h"
|
||||||
#include "MG_Util/Converters/GLToGlslang/GLShaderLangConverter.h"
|
#include "MG_Util/Converters/GLToGlslang/GLShaderLangConverter.h"
|
||||||
|
|
||||||
|
#include "MG_Util/ShaderTranspiler/SpvcSession.h"
|
||||||
|
#include "MG_Util/ShaderTranspiler/ShaderCompiler.h"
|
||||||
|
|||||||
@@ -57,11 +57,9 @@ add_executable(
|
|||||||
ProgramTest.cpp
|
ProgramTest.cpp
|
||||||
|
|
||||||
${MGL_ROOT}/MobileGL/MG_Util/Converters/GLToStr/GLEnumConverter.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/Converters/GLToGlslang/GLShaderLangConverter.cpp
|
||||||
${MGL_ROOT}/MobileGL/MG_Util/ShaderTranspiler/ShaderCompiler.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
|
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
|
||||||
@@ -37,102 +37,102 @@ namespace MobileGL {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpvcSession {
|
// class SpvcSession {
|
||||||
public:
|
// public:
|
||||||
SpvcSession() {}
|
// SpvcSession() {}
|
||||||
|
//
|
||||||
explicit SpvcSession(const Vector<unsigned int>& spirv) {
|
// explicit SpvcSession(const Vector<unsigned int>& spirv) {
|
||||||
const SpvId *p_spirv = spirv.data();
|
// const SpvId *p_spirv = spirv.data();
|
||||||
size_t word_count = spirv.size();
|
// size_t word_count = spirv.size();
|
||||||
|
//
|
||||||
spvc_context_create(&context);
|
// spvc_context_create(&context);
|
||||||
spvc_context_parse_spirv(context, p_spirv, word_count, &ir);
|
// 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_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler);
|
||||||
spvc_compiler_create_shader_resources(compiler, &resources);
|
// spvc_compiler_create_shader_resources(compiler, &resources);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
SpvcSession(SpvcSession&) = delete;
|
// SpvcSession(SpvcSession&) = delete;
|
||||||
|
//
|
||||||
SpvcSession(SpvcSession&& that) {
|
// SpvcSession(SpvcSession&& that) {
|
||||||
std::swap(this->context, that.context);
|
// std::swap(this->context, that.context);
|
||||||
std::swap(this->compiler, that.compiler);
|
// std::swap(this->compiler, that.compiler);
|
||||||
std::swap(this->ir, that.ir);
|
// std::swap(this->ir, that.ir);
|
||||||
std::swap(this->compiler_options, that.compiler_options);
|
// std::swap(this->compiler_options, that.compiler_options);
|
||||||
std::swap(this->resources, that.resources);
|
// std::swap(this->resources, that.resources);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
SpvcSession& operator=(SpvcSession& session) = delete;
|
// SpvcSession& operator=(SpvcSession& session) = delete;
|
||||||
|
//
|
||||||
SpvcSession& operator=(SpvcSession&& that) {
|
// SpvcSession& operator=(SpvcSession&& that) {
|
||||||
std::swap(this->context, that.context);
|
// std::swap(this->context, that.context);
|
||||||
std::swap(this->compiler, that.compiler);
|
// std::swap(this->compiler, that.compiler);
|
||||||
std::swap(this->ir, that.ir);
|
// std::swap(this->ir, that.ir);
|
||||||
std::swap(this->compiler_options, that.compiler_options);
|
// std::swap(this->compiler_options, that.compiler_options);
|
||||||
std::swap(this->resources, that.resources);
|
// std::swap(this->resources, that.resources);
|
||||||
return *this;
|
// return *this;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
spvc_result CreateOptions(spvc_compiler_options *options) {
|
// spvc_result CreateOptions(spvc_compiler_options *options) {
|
||||||
return spvc_compiler_create_compiler_options(compiler, options);
|
// return spvc_compiler_create_compiler_options(compiler, options);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
spvc_result SetOptions(spvc_compiler_options options) {
|
// spvc_result SetOptions(spvc_compiler_options options) {
|
||||||
compiler_options = options;
|
// compiler_options = options;
|
||||||
return spvc_compiler_install_compiler_options(compiler, options);
|
// return spvc_compiler_install_compiler_options(compiler, options);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const {
|
// Vector<InterfaceVariable> GetShaderInterface(spvc_resource_type resource_type) const {
|
||||||
const spvc_reflected_resource *list = nullptr;
|
// const spvc_reflected_resource *list = nullptr;
|
||||||
size_t count = 0;
|
// size_t count = 0;
|
||||||
spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
|
// spvc_resources_get_resource_list_for_type(resources, resource_type, &list, &count);
|
||||||
|
//
|
||||||
Vector<InterfaceVariable> variables;
|
// Vector<InterfaceVariable> variables;
|
||||||
for (size_t i = 0; i < count; ++i) {
|
// for (size_t i = 0; i < count; ++i) {
|
||||||
if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
|
// if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
InterfaceVariable var;
|
// InterfaceVariable var;
|
||||||
var.name = list[i].name;
|
// var.name = list[i].name;
|
||||||
var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
|
// var.location = spvc_compiler_get_decoration(compiler, list[i].id, SpvDecorationLocation);
|
||||||
variables.push_back(var);
|
// variables.push_back(var);
|
||||||
|
//
|
||||||
if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
|
// if (resource_type == SPVC_RESOURCE_TYPE_UNIFORM_BUFFER) {
|
||||||
spvc_type type = spvc_compiler_get_type_handle(compiler, list[i].base_type_id);
|
// 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);
|
// size_t num_members = spvc_type_get_num_member_types(type);
|
||||||
printf("uniform %s {\n", var.name.c_str());
|
// printf("uniform %s {\n", var.name.c_str());
|
||||||
for (size_t j = 0; j < num_members; ++j) {
|
// for (size_t j = 0; j < num_members; ++j) {
|
||||||
const char *memberName = spvc_compiler_get_member_name(compiler, list[i].base_type_id, 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 memberTypeId = spvc_type_get_member_type(type, j);
|
||||||
// auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
|
// // auto memberType = spvc_compiler_get_type_handle(compiler, memberTypeId);
|
||||||
|
//
|
||||||
unsigned memberOffset = 0;
|
// unsigned memberOffset = 0;
|
||||||
spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
|
// spvc_compiler_type_struct_member_offset(compiler, type, j, &memberOffset);
|
||||||
printf("\b%s; // %u\n", memberName, memberOffset);
|
// printf("\b%s; // %u\n", memberName, memberOffset);
|
||||||
}
|
// }
|
||||||
printf("}\n");
|
// printf("}\n");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
std::sort(variables.begin(), variables.end());
|
// std::sort(variables.begin(), variables.end());
|
||||||
return variables;
|
// return variables;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
spvc_result Compile(const char** result) const {
|
// spvc_result Compile(const char** result) const {
|
||||||
return spvc_compiler_compile(compiler, result);
|
// return spvc_compiler_compile(compiler, result);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
const char* GetLastErrorString() const {
|
// const char* GetLastErrorString() const {
|
||||||
return spvc_context_get_last_error_string(context);
|
// return spvc_context_get_last_error_string(context);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
~SpvcSession() { spvc_context_destroy(context); }
|
// ~SpvcSession() { spvc_context_destroy(context); }
|
||||||
private:
|
// private:
|
||||||
spvc_context context = nullptr;
|
// spvc_context context = nullptr;
|
||||||
spvc_parsed_ir ir = nullptr;
|
// spvc_parsed_ir ir = nullptr;
|
||||||
spvc_compiler compiler = nullptr;
|
// spvc_compiler compiler = nullptr;
|
||||||
spvc_compiler_options compiler_options = nullptr;
|
// spvc_compiler_options compiler_options = nullptr;
|
||||||
spvc_resources resources = nullptr;
|
// spvc_resources resources = nullptr;
|
||||||
};
|
// };
|
||||||
|
|
||||||
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
|
inline static EShLanguage GetEShLanguageByShaderType(GLenum shaderType) {
|
||||||
switch (shaderType) {
|
switch (shaderType) {
|
||||||
@@ -155,5 +155,3 @@ namespace MobileGL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "ShaderCompiler.h"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user