mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Chore] (MG_Util/Program): use SPIRV-Cross C++ API instead of C ones
This commit is contained in:
@@ -48,6 +48,8 @@
|
|||||||
#include <glslang/Public/ShaderLang.h>
|
#include <glslang/Public/ShaderLang.h>
|
||||||
#include <glslang/Include/Types.h>
|
#include <glslang/Include/Types.h>
|
||||||
#include <glslang/Public/ShaderLang.h>
|
#include <glslang/Public/ShaderLang.h>
|
||||||
|
#include <spirv_cross/spirv_cross.hpp>
|
||||||
|
#include <spirv_cross/spirv_glsl.hpp>
|
||||||
#include <spirv_cross/spirv_cross_c.h>
|
#include <spirv_cross/spirv_cross_c.h>
|
||||||
#include <glslang/SPIRV/GlslangToSpv.h>
|
#include <glslang/SPIRV/GlslangToSpv.h>
|
||||||
#include <glslang/Include/intermediate.h>
|
#include <glslang/Include/intermediate.h>
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
|||||||
|
|
||||||
set(LINK_LIBRARIES
|
set(LINK_LIBRARIES
|
||||||
${VULKAN_LIB_DIR}/libglslang.a
|
${VULKAN_LIB_DIR}/libglslang.a
|
||||||
${VULKAN_LIB_DIR}/libspirv-cross-c-shared.dylib
|
|
||||||
${VULKAN_LIB_DIR}/libGenericCodeGen.a
|
${VULKAN_LIB_DIR}/libGenericCodeGen.a
|
||||||
${VULKAN_LIB_DIR}/libglslang-default-resource-limits.a
|
${VULKAN_LIB_DIR}/libglslang-default-resource-limits.a
|
||||||
${VULKAN_LIB_DIR}/libMachineIndependent.a
|
${VULKAN_LIB_DIR}/libMachineIndependent.a
|
||||||
@@ -19,7 +18,18 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
|||||||
${VULKAN_LIB_DIR}/libSPIRV-Tools-reduce.a
|
${VULKAN_LIB_DIR}/libSPIRV-Tools-reduce.a
|
||||||
${VULKAN_LIB_DIR}/libSPIRV-Tools.a
|
${VULKAN_LIB_DIR}/libSPIRV-Tools.a
|
||||||
${VULKAN_LIB_DIR}/libSPIRV.a
|
${VULKAN_LIB_DIR}/libSPIRV.a
|
||||||
${VULKAN_LIB_DIR}/libSPVRemapper.a)
|
${VULKAN_LIB_DIR}/libSPVRemapper.a
|
||||||
|
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-c-shared.dylib
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-core.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-cpp.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-glsl.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-hlsl.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-msl.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-reflect.a
|
||||||
|
${VULKAN_LIB_DIR}/libspirv-cross-util.a
|
||||||
|
|
||||||
|
)
|
||||||
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
|
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
|
||||||
find_package(Vulkan REQUIRED)
|
find_package(Vulkan REQUIRED)
|
||||||
get_filename_component(VULKAN_LIB_DIR ${Vulkan_LIBRARY} PATH)
|
get_filename_component(VULKAN_LIB_DIR ${Vulkan_LIBRARY} PATH)
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ TEST_F(ProgramTest, DecompProgram) {
|
|||||||
ShaderAttrib fs_attrib {
|
ShaderAttrib fs_attrib {
|
||||||
.shaderType = GL_FRAGMENT_SHADER,
|
.shaderType = GL_FRAGMENT_SHADER,
|
||||||
.sourceStr = fs
|
.sourceStr = fs
|
||||||
};
|
};
|
||||||
auto fs_res = ShaderCompiler::CompileShader(fs_attrib);
|
auto fs_res = ShaderCompiler::CompileShader(fs_attrib);
|
||||||
if (!fs_res) {
|
if (!fs_res) {
|
||||||
ASSERT_NE(fs_res.error().errc, 0);
|
ASSERT_NE(fs_res.error().errc, 0);
|
||||||
@@ -168,14 +168,10 @@ TEST_F(ProgramTest, DecompProgram) {
|
|||||||
|
|
||||||
auto spirvs = program_res.value();
|
auto spirvs = program_res.value();
|
||||||
|
|
||||||
Vector<SpvcSession> sessions(spirvs.size());
|
|
||||||
for (SizeT i = 0; i < spirvs.size(); ++i) {
|
|
||||||
sessions[i] = SpvcSession(spirvs[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (SizeT i = 0; i < spirvs.size(); ++i) {
|
for (SizeT i = 0; i < spirvs.size(); ++i) {
|
||||||
std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(programAttrib.shaderTypes[i]) << std::endl;
|
std::cout << "Decompiling " << MG_Util::ConvertGLEnumToString(programAttrib.shaderTypes[i]) << std::endl;
|
||||||
auto src = ShaderCompiler::DecompileShader(sessions[i]);
|
spirv_cross::CompilerGLSL compiler(spirvs[i]);
|
||||||
|
auto src = ShaderCompiler::DecompileShader(compiler);
|
||||||
if (!src) {
|
if (!src) {
|
||||||
ASSERT_NE(src.error().errc, 0);
|
ASSERT_NE(src.error().errc, 0);
|
||||||
FAIL() << "errc: " << src.error().errc << "\nlog: " << src.error().log;
|
FAIL() << "errc: " << src.error().errc << "\nlog: " << src.error().log;
|
||||||
@@ -185,42 +181,42 @@ TEST_F(ProgramTest, DecompProgram) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// spirv link check
|
// spirv link check
|
||||||
auto vs_outputs = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_STAGE_OUTPUT);
|
// auto vs_outputs = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_STAGE_OUTPUT);
|
||||||
auto fs_inputs = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_STAGE_INPUT);
|
// auto fs_inputs = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_STAGE_INPUT);
|
||||||
|
//
|
||||||
ASSERT_EQ(vs_outputs.size(), fs_inputs.size());
|
// ASSERT_EQ(vs_outputs.size(), fs_inputs.size());
|
||||||
|
//
|
||||||
for (size_t i = 0; i < vs_outputs.size(); ++i) {
|
// for (size_t i = 0; i < vs_outputs.size(); ++i) {
|
||||||
EXPECT_EQ(vs_outputs[i].location, fs_inputs[i].location);
|
// EXPECT_EQ(vs_outputs[i].location, fs_inputs[i].location);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
auto vs_uniforms = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_GL_PLAIN_UNIFORM);
|
// auto vs_uniforms = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_GL_PLAIN_UNIFORM);
|
||||||
auto fs_uniforms = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_GL_PLAIN_UNIFORM);
|
// auto fs_uniforms = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_GL_PLAIN_UNIFORM);
|
||||||
|
//
|
||||||
std::unordered_map<std::string, uint32_t> uniform_locations;
|
// std::unordered_map<std::string, uint32_t> uniform_locations;
|
||||||
for (const auto& uniform : vs_uniforms) {
|
// for (const auto& uniform : vs_uniforms) {
|
||||||
uniform_locations[uniform.name] = uniform.location;
|
// uniform_locations[uniform.name] = uniform.location;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
for (const auto& uniform : fs_uniforms) {
|
// for (const auto& uniform : fs_uniforms) {
|
||||||
auto it = uniform_locations.find(uniform.name);
|
// auto it = uniform_locations.find(uniform.name);
|
||||||
if (it != uniform_locations.end()) {
|
// if (it != uniform_locations.end()) {
|
||||||
EXPECT_EQ(it->second, uniform.location);
|
// EXPECT_EQ(it->second, uniform.location);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
auto vs_samplers = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_SAMPLED_IMAGE);
|
// auto vs_samplers = sessions[0].GetShaderInterface(SPVC_RESOURCE_TYPE_SAMPLED_IMAGE);
|
||||||
auto fs_samplers = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_SAMPLED_IMAGE);
|
// auto fs_samplers = sessions[1].GetShaderInterface(SPVC_RESOURCE_TYPE_SAMPLED_IMAGE);
|
||||||
|
//
|
||||||
std::unordered_map<std::string, uint32_t> sampler_locations;
|
// std::unordered_map<std::string, uint32_t> sampler_locations;
|
||||||
for (const auto& uniform : vs_uniforms) {
|
// for (const auto& uniform : vs_uniforms) {
|
||||||
sampler_locations[uniform.name] = uniform.location;
|
// sampler_locations[uniform.name] = uniform.location;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
for (const auto& uniform : fs_uniforms) {
|
// for (const auto& uniform : fs_uniforms) {
|
||||||
auto it = sampler_locations.find(uniform.name);
|
// auto it = sampler_locations.find(uniform.name);
|
||||||
if (it != sampler_locations.end()) {
|
// if (it != sampler_locations.end()) {
|
||||||
EXPECT_EQ(it->second, uniform.location);
|
// EXPECT_EQ(it->second, uniform.location);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,10 +27,8 @@ namespace MobileGL {
|
|||||||
WideCharToMultiByte(CP_UTF8, 0, desc, -1, buffer, sizeof(buffer), nullptr, nullptr);
|
WideCharToMultiByte(CP_UTF8, 0, desc, -1, buffer, sizeof(buffer), nullptr, nullptr);
|
||||||
LocalFree(desc);
|
LocalFree(desc);
|
||||||
}
|
}
|
||||||
#elif defined(__ANDROID__) || defined(__linux__)
|
#elif defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
|
||||||
pthread_getname_np(pthread_self(), buffer, sizeof(buffer));
|
pthread_getname_np(pthread_self(), buffer, sizeof(buffer));
|
||||||
#elif defined(__APPLE__)
|
|
||||||
pthread_getname_np(buffer, sizeof(buffer));
|
|
||||||
#endif
|
#endif
|
||||||
return buffer[0] ? buffer : "UnknownThread";
|
return buffer[0] ? buffer : "UnknownThread";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,18 +207,37 @@ namespace MobileGL {
|
|||||||
const char *result = nullptr;
|
const char *result = nullptr;
|
||||||
session.Compile(&result);
|
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 += session.GetLastErrorString();
|
// 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;
|
||||||
|
|
||||||
return glsl;
|
return glsl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<String> ShaderCompiler::DecompileShader(spirv_cross::CompilerGLSL& compiler) {
|
||||||
|
try {
|
||||||
|
spirv_cross::CompilerGLSL::Options options;
|
||||||
|
options.version = 450;
|
||||||
|
options.es = false;
|
||||||
|
options.vulkan_semantics = true;
|
||||||
|
compiler.set_common_options(options);
|
||||||
|
|
||||||
|
auto result = compiler.compile();
|
||||||
|
return result;
|
||||||
|
} catch (const spirv_cross::CompilerError& e) {
|
||||||
|
ResultInfo r;
|
||||||
|
r.log += "Failed to compile the shader to GLSL: \n";
|
||||||
|
r.log += e.what();
|
||||||
|
r.errc = -5;
|
||||||
|
return std::unexpected(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ namespace ShaderTranspiler {
|
|||||||
static Result<SharedPtr<glslang::TShader>> CompileShader(const ShaderAttrib& attrib);
|
static Result<SharedPtr<glslang::TShader>> CompileShader(const ShaderAttrib& attrib);
|
||||||
static Result<Vector<Vector<unsigned>>> LinkProgram(const ProgramAttrib& attrib);
|
static Result<Vector<Vector<unsigned>>> LinkProgram(const ProgramAttrib& attrib);
|
||||||
static Result<String> DecompileShader(SpvcSession& session);
|
static Result<String> DecompileShader(SpvcSession& session);
|
||||||
|
static Result<String> DecompileShader(spirv_cross::CompilerGLSL& compiler);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user