mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Fix] (MG_Util/Program): program linking
This commit is contained in:
@@ -116,22 +116,22 @@ namespace MobileGL {
|
||||
return Resources;
|
||||
}
|
||||
|
||||
CompilerResult ShaderCompiler::CompileShader(const ShaderAttrib& attrib) {
|
||||
Result<SharedPtr<glslang::TShader>> ShaderCompiler::CompileShader(const ShaderAttrib& attrib) {
|
||||
auto shaderType = attrib.shaderType;
|
||||
auto sourceStr = attrib.sourceStr;
|
||||
|
||||
auto lang = GetEShLanguageByShaderType(shaderType);
|
||||
if (lang == EShLanguage::EShLangCount) {
|
||||
ShaderCompileResult<false> r;
|
||||
ResultInfo r;
|
||||
r.log += "Error: [Preprocess] Unsupported shader type: " +
|
||||
ConvertGLEnumToString(shaderType);
|
||||
r.errc = -1;
|
||||
return std::unexpected(r);
|
||||
}
|
||||
|
||||
ShaderCompileResult<true> res;
|
||||
auto& tshader = res.TShader;
|
||||
tshader = MakeUnique<glslang::TShader>(lang);
|
||||
SharedPtr<glslang::TShader> res;
|
||||
auto& tshader = res;
|
||||
tshader = MakeShared<glslang::TShader>(lang);
|
||||
const char* src[] = { sourceStr.c_str() };
|
||||
tshader->setStrings(src, 1);
|
||||
tshader->setInvertY(true);
|
||||
@@ -144,7 +144,7 @@ namespace MobileGL {
|
||||
if (!tshader->parse(&GetTBuiltInResourceInstance(), 150, ECompatibilityProfile,
|
||||
/*forceDefaultVersionAndProfile: */false,
|
||||
/*forwardCompatible: */true, EShMsgDefault)) {
|
||||
ShaderCompileResult<false> r;
|
||||
ResultInfo r;
|
||||
r.log += "Error: [glslang] Cannot compile " + ConvertGLEnumToString(shaderType) + ":\n"
|
||||
+ std::string(tshader->getInfoLog());
|
||||
r.errc = -2;
|
||||
@@ -153,6 +153,49 @@ namespace MobileGL {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Result<Vector<Vector<unsigned>>> ShaderCompiler::LinkProgram(const ProgramAttrib& attrib) {
|
||||
glslang::TProgram program;
|
||||
for (auto& s : attrib.shaders) {
|
||||
program.addShader(s.get());
|
||||
}
|
||||
|
||||
if (!program.link(EShMsgDefault)) {
|
||||
ResultInfo r;
|
||||
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);
|
||||
if (pResolver) {
|
||||
resolver = UniquePtr<glslang::TIoMapResolver>(pResolver);
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto ioMapper = UniquePtr<glslang::TIoMapper>(glslang::GetGlslIoMapper());
|
||||
|
||||
if (!program.mapIO(resolver.get(), ioMapper.get())) {
|
||||
ResultInfo r;
|
||||
r.log = "Error: [glslang] Cannot mapIO:\n" + std::string(program.getInfoLog());
|
||||
r.errc = -4;
|
||||
return std::unexpected(r);
|
||||
}
|
||||
|
||||
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);
|
||||
allSpirv.push_back(spirv);
|
||||
}
|
||||
|
||||
return allSpirv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace MG_Util {
|
||||
namespace ShaderTranspiler {
|
||||
class ShaderCompiler {
|
||||
public:
|
||||
static CompilerResult CompileShader(const ShaderAttrib& attrib);
|
||||
static Result<SharedPtr<glslang::TShader>> CompileShader(const ShaderAttrib& attrib);
|
||||
static Result<Vector<Vector<unsigned>>> LinkProgram(const ProgramAttrib& attrib);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace MobileGL {
|
||||
struct TUniform<TUniformType::Uniform> {
|
||||
String name;
|
||||
|
||||
glslang::TStorageQualifier storageQualifier;
|
||||
// glslang::TType type;
|
||||
Uint layoutLocation = 0;
|
||||
Uint layoutBinding = 0;
|
||||
glslang::TLayoutPacking layoutPacking;
|
||||
@@ -42,20 +42,18 @@ namespace MobileGL {
|
||||
String sourceStr;
|
||||
};
|
||||
|
||||
struct CompiledTShader {
|
||||
UniquePtr<glslang::TShader> TShader;
|
||||
Vector<TUniform<TUniformType::Uniform>> uniforms;
|
||||
Vector<TUniform<TUniformType::Sampler>> samplers;
|
||||
struct ProgramAttrib {
|
||||
Vector<GLenum> shaderTypes;
|
||||
Vector<SharedPtr<glslang::TShader>> shaders;
|
||||
};
|
||||
|
||||
template <bool Succeeded>
|
||||
struct ShaderCompileResult:
|
||||
public std::conditional_t<Succeeded, CompiledTShader, EmptyType> {
|
||||
struct ResultInfo {
|
||||
Int errc = 0;
|
||||
String log;
|
||||
};
|
||||
|
||||
using CompilerResult = std::expected<ShaderCompileResult<true>, ShaderCompileResult<false>>;
|
||||
template <typename T>
|
||||
using Result = std::expected<T, ResultInfo>;
|
||||
|
||||
struct ShaderPayload {
|
||||
// In
|
||||
|
||||
Reference in New Issue
Block a user