mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-09 12:48:32 +09:00
[Feat] (MG_Util/ShaderCompiler): separates GetSpirvBinaryFromProgram() from LinkProgram()
This commit is contained in:
@@ -26,7 +26,7 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
void ProgramObject::Link() {
|
||||
PreLink();
|
||||
// PreLink();
|
||||
|
||||
Vector<GLenum> shaderTypes(m_shaders.size());
|
||||
Vector<SharedPtr<glslang::TShader>> shaders(m_shaders.size());
|
||||
@@ -43,7 +43,7 @@ namespace MobileGL {
|
||||
auto result = MG_Util::ShaderTranspiler::ShaderCompiler::LinkProgram(attrib);
|
||||
if (result) {
|
||||
m_linkStatus = true;
|
||||
m_programBinary = Move(result.value());
|
||||
m_program = result.value();
|
||||
} else {
|
||||
m_linkStatus = false;
|
||||
m_infoLog = result.error().log;
|
||||
@@ -53,7 +53,7 @@ namespace MobileGL {
|
||||
THROW_EXCEPTION(e);
|
||||
}
|
||||
|
||||
PostLink();
|
||||
// PostLink();
|
||||
}
|
||||
|
||||
void ProgramObject::MarkAsDeleted() {
|
||||
@@ -127,35 +127,35 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
void ProgramObject::PostLink() {
|
||||
if (m_programBinary.empty()) {
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
MG_Util::ShaderTranspiler::SpvcSession session(m_programBinary[0]);
|
||||
const char* src = nullptr; // we don't care the source atm
|
||||
auto result = session.Compile(&src);
|
||||
if (result != SPVC_SUCCESS) {
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
m_metadata = session.GetMetadata();
|
||||
auto& uniformOffsets = m_metadata.plainUniformOffsetsInUBO;
|
||||
for (const auto& [name, offset] : uniformOffsets) {
|
||||
assert(m_uniforms.find(name) != m_uniforms.end());
|
||||
assert(m_uniforms[name] < m_uniformOffsets.size());
|
||||
m_uniformOffsets[m_uniforms[name]] = offset;
|
||||
}
|
||||
m_uboScratch.resize(m_metadata.uboSize, 0);
|
||||
|
||||
auto& types = m_metadata.plainUniformMemberTypes;
|
||||
|
||||
assert(types.size() == m_uniformOffsets.size());
|
||||
m_uniformTypes.resize(m_uniformOffsets.size());
|
||||
for (const auto& [name, type] : types) {
|
||||
auto gltype = MG_Util::ConvertSpvcTypeToGLEnum(type);
|
||||
auto location = m_uniforms[name];
|
||||
m_uniformTypes[location] = gltype;
|
||||
}
|
||||
// if (m_programBinary.empty()) {
|
||||
// assert(false);
|
||||
// return;
|
||||
// }
|
||||
// MG_Util::ShaderTranspiler::SpvcSession session(m_programBinary[0]);
|
||||
// const char* src = nullptr; // we don't care the source atm
|
||||
// auto result = session.Compile(&src);
|
||||
// if (result != SPVC_SUCCESS) {
|
||||
// assert(false);
|
||||
// return;
|
||||
// }
|
||||
// m_metadata = session.GetMetadata();
|
||||
// auto& uniformOffsets = m_metadata.plainUniformOffsetsInUBO;
|
||||
// for (const auto& [name, offset] : uniformOffsets) {
|
||||
// assert(m_uniforms.find(name) != m_uniforms.end());
|
||||
// assert(m_uniforms[name] < m_uniformOffsets.size());
|
||||
// m_uniformOffsets[m_uniforms[name]] = offset;
|
||||
// }
|
||||
// m_uboScratch.resize(m_metadata.uboSize, 0);
|
||||
//
|
||||
// auto& types = m_metadata.plainUniformMemberTypes;
|
||||
//
|
||||
// assert(types.size() == m_uniformOffsets.size());
|
||||
// m_uniformTypes.resize(m_uniformOffsets.size());
|
||||
// for (const auto& [name, type] : types) {
|
||||
// auto gltype = MG_Util::ConvertSpvcTypeToGLEnum(type);
|
||||
// auto location = m_uniforms[name];
|
||||
// m_uniformTypes[location] = gltype;
|
||||
// }
|
||||
}
|
||||
} // namespace GLState
|
||||
} // namespace MG_State
|
||||
|
||||
@@ -35,8 +35,8 @@ namespace MobileGL {
|
||||
|
||||
const Uint m_id = 0;
|
||||
Vector<SharedPtr<ShaderObject>> m_shaders;
|
||||
// basically this contains SPIR-V in binary format
|
||||
Vector<Vector<Uint>> m_programBinary;
|
||||
|
||||
SharedPtr<glslang::TProgram> m_program;
|
||||
|
||||
// Uniforms
|
||||
MG_Util::ShaderTranspiler::SpvcMetadata m_metadata;
|
||||
|
||||
@@ -113,6 +113,9 @@ TEST_F(ProgramTest, CompileAndLink) {
|
||||
LinkProgram(program);
|
||||
printf("Program linked.\n");
|
||||
|
||||
// FIXME: fix these later, refactoring uniform location reflection stuff
|
||||
FAIL() << "GetUniformLocation not implemented yet!";
|
||||
|
||||
EXPECT_EQ(GetUniformLocation(program, "ProjMat"), 0);
|
||||
EXPECT_EQ(GetUniformLocation(program, "Gray"), 1);
|
||||
EXPECT_EQ(GetUniformLocation(program, "Saturation"), 6);
|
||||
|
||||
@@ -143,8 +143,15 @@ TEST_F(ProgramUtilTest, CompileFragmentShaderWithDiscard) {
|
||||
FAIL() << "errc: " << program_res.error().errc << "\nlog: " << program_res.error().log;
|
||||
}
|
||||
|
||||
auto program = program_res.value();
|
||||
|
||||
auto spirvs = program_res.value();
|
||||
ProgramBinaryAttrib binaryAttrib {
|
||||
.shaderTypes = { GL_FRAGMENT_SHADER },
|
||||
.program = *program,
|
||||
};
|
||||
auto bin_res = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib);
|
||||
|
||||
auto spirvs = bin_res.value();
|
||||
|
||||
Vector<SpvcSession> sessions(spirvs.size());
|
||||
for (SizeT i = 0; i < spirvs.size(); ++i) {
|
||||
@@ -282,7 +289,13 @@ TEST_F(ProgramUtilTest, DecompProgram) {
|
||||
FAIL() << "errc: " << program_res.error().errc << "\nlog: " << program_res.error().log;
|
||||
}
|
||||
|
||||
auto spirvs = program_res.value();
|
||||
ProgramBinaryAttrib binaryAttrib {
|
||||
.shaderTypes = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER },
|
||||
.program = *program_res.value(),
|
||||
};
|
||||
auto bin_res = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib);
|
||||
|
||||
auto spirvs = bin_res.value();
|
||||
|
||||
Vector<SpvcSession> sessions(spirvs.size());
|
||||
for (SizeT i = 0; i < spirvs.size(); ++i) {
|
||||
|
||||
@@ -159,22 +159,22 @@ namespace MobileGL {
|
||||
return res;
|
||||
}
|
||||
|
||||
Result<Vector<Vector<unsigned>>> ShaderCompiler::LinkProgram(const ProgramAttrib& attrib) {
|
||||
glslang::TProgram program;
|
||||
Result<SharedPtr<glslang::TProgram>> ShaderCompiler::LinkProgram(const ProgramAttrib& attrib) {
|
||||
SharedPtr<glslang::TProgram> program = MakeShared<glslang::TProgram>();
|
||||
for (auto& s : attrib.shaders) {
|
||||
program.addShader(s.get());
|
||||
program->addShader(s.get());
|
||||
}
|
||||
|
||||
if (!program.link(EShMsgDefault)) {
|
||||
if (!program->link(EShMsgDefault)) {
|
||||
ResultInfo r;
|
||||
r.log = "Error: [glslang] Cannot link the program:\n" + std::string(program.getInfoLog());
|
||||
r.log = "Error: [glslang] Cannot link the program:\n" + std::string(program->getInfoLog());
|
||||
r.errc = -3;
|
||||
return std::unexpected(r);
|
||||
}
|
||||
|
||||
UniquePtr<glslang::TIoMapResolver> resolver;
|
||||
for (unsigned stage = 0; stage < EShLangCount; stage++) {
|
||||
auto* pResolver = program.getGlslIoResolver((EShLanguage)stage);
|
||||
auto* pResolver = program->getGlslIoResolver((EShLanguage)stage);
|
||||
if (pResolver) {
|
||||
resolver = UniquePtr<glslang::TIoMapResolver>(pResolver);
|
||||
break;
|
||||
@@ -182,20 +182,24 @@ namespace MobileGL {
|
||||
}
|
||||
auto ioMapper = UniquePtr<glslang::TIoMapper>(glslang::GetGlslIoMapper());
|
||||
|
||||
if (!program.mapIO(resolver.get(), ioMapper.get())) {
|
||||
if (!program->mapIO(resolver.get(), ioMapper.get())) {
|
||||
ResultInfo r;
|
||||
r.log = "Error: [glslang] Cannot mapIO:\n" + std::string(program.getInfoLog());
|
||||
r.log = "Error: [glslang] Cannot mapIO:\n" + std::string(program->getInfoLog());
|
||||
r.errc = -4;
|
||||
return std::unexpected(r);
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
Result<Vector<Vector<unsigned>>> ShaderCompiler::GetSpirvBinaryFromProgram(const ProgramBinaryAttrib &attrib) {
|
||||
glslang::SpvOptions spvOptions;
|
||||
spvOptions.disableOptimizer = false;
|
||||
|
||||
Vector<Vector<unsigned>> allSpirv;
|
||||
for (auto type : attrib.shaderTypes) {
|
||||
Vector<unsigned> spirv;
|
||||
GlslangToSpv(*program.getIntermediate(ConvertGLEnumToEShLanguage(type)), spirv, &spvOptions);
|
||||
GlslangToSpv(*attrib.program.getIntermediate(ConvertGLEnumToEShLanguage(type)), spirv, &spvOptions);
|
||||
allSpirv.push_back(spirv);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ namespace MobileGL {
|
||||
class ShaderCompiler {
|
||||
public:
|
||||
static Result<SharedPtr<glslang::TShader>> CompileShader(const ShaderAttrib& attrib);
|
||||
static Result<Vector<Vector<unsigned>>> LinkProgram(const ProgramAttrib& attrib);
|
||||
static Result<SharedPtr<glslang::TProgram>> LinkProgram(const ProgramAttrib& attrib);
|
||||
static Result<Vector<Vector<unsigned>>> GetSpirvBinaryFromProgram(const ProgramBinaryAttrib& attrib);
|
||||
static Result<String> DecompileShader(SpvcSession& session);
|
||||
};
|
||||
} // namespace ShaderTranspiler
|
||||
|
||||
@@ -24,6 +24,11 @@ namespace MobileGL {
|
||||
Vector<SharedPtr<glslang::TShader>> shaders;
|
||||
};
|
||||
|
||||
struct ProgramBinaryAttrib {
|
||||
Vector<GLenum> shaderTypes;
|
||||
const glslang::TProgram& program;
|
||||
};
|
||||
|
||||
struct ResultInfo {
|
||||
Int errc = 0;
|
||||
String log;
|
||||
|
||||
Reference in New Issue
Block a user