This commit is contained in:
parent
988c1821af
commit
df9436c51d
3 changed files with 67 additions and 31 deletions
|
|
@ -59,65 +59,80 @@ namespace Crafter {
|
|||
std::string Shader::Compile(const fs::path& outputDir) const {
|
||||
EShLanguage glslangType = ToEShLanguage(type);
|
||||
glslang::InitializeProcess();
|
||||
|
||||
// Every error path returns a non-empty string; the caller treats
|
||||
// empty as success (see Crafter.Build-Clang.cpp BuildOnce shader
|
||||
// worker). Prefixing with the source path is what tells the user
|
||||
// which shader actually failed when several compile in parallel.
|
||||
auto fail = [&](std::string_view stage, std::string log) {
|
||||
glslang::FinalizeProcess();
|
||||
std::string out = path.string();
|
||||
out += ": ";
|
||||
out += stage;
|
||||
if (!log.empty()) {
|
||||
out += '\n';
|
||||
out += log;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
EShMessages messages = static_cast<EShMessages>(EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules);
|
||||
std::ifstream fileStream(path, std::ios::in | std::ios::binary);
|
||||
if (!fileStream) {
|
||||
throw std::ios_base::failure("Failed to open file: " + path.string());
|
||||
return fail("failed to open shader source", {});
|
||||
}
|
||||
std::ostringstream contents;
|
||||
contents << fileStream.rdbuf();
|
||||
std::string src = contents.str();
|
||||
|
||||
const char *file_name_list[1] = {""};
|
||||
const char *shader_source = reinterpret_cast<const char *>(src.data());
|
||||
std::string pathStr = path.string();
|
||||
const char* file_name_list[1] = { pathStr.c_str() };
|
||||
const char* shader_source = src.data();
|
||||
const int shader_source_len = static_cast<int>(src.size());
|
||||
|
||||
glslang::TShader shader(glslangType);
|
||||
shader.setStringsWithLengthsAndNames(&shader_source, nullptr, file_name_list, 1);
|
||||
shader.setStringsWithLengthsAndNames(&shader_source, &shader_source_len, file_name_list, 1);
|
||||
shader.setEntryPoint(entrypoint.c_str());
|
||||
shader.setSourceEntryPoint(entrypoint.c_str());
|
||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_4);
|
||||
DirStackFileIncluder includeDir;
|
||||
includeDir.pushExternalLocalDirectory(path.parent_path().generic_string());
|
||||
std::string info_log;
|
||||
if (!shader.parse(GetDefaultResources(), 100, false, messages, includeDir))
|
||||
{
|
||||
info_log = std::string(shader.getInfoLog()) + std::string(shader.getInfoDebugLog());
|
||||
|
||||
if (!shader.parse(GetDefaultResources(), 100, false, messages, includeDir)) {
|
||||
return fail("GLSL parse failed", std::string(shader.getInfoLog()) + shader.getInfoDebugLog());
|
||||
}
|
||||
// Add shader to new program object.
|
||||
|
||||
glslang::TProgram program;
|
||||
program.addShader(&shader);
|
||||
|
||||
// Link program.
|
||||
if (!program.link(messages))
|
||||
{
|
||||
info_log = std::string(program.getInfoLog()) + std::string(program.getInfoDebugLog());
|
||||
}
|
||||
|
||||
// Save any info log that was generated.
|
||||
if (shader.getInfoLog())
|
||||
{
|
||||
info_log += std::string(shader.getInfoLog()) + std::string(shader.getInfoDebugLog());
|
||||
}
|
||||
|
||||
if (program.getInfoLog())
|
||||
{
|
||||
info_log += std::string(program.getInfoLog()) + std::string(program.getInfoDebugLog());
|
||||
if (!program.link(messages)) {
|
||||
return fail("GLSL link failed", std::string(program.getInfoLog()) + program.getInfoDebugLog());
|
||||
}
|
||||
|
||||
glslang::TIntermediate* intermediate = program.getIntermediate(glslangType);
|
||||
if (!intermediate)
|
||||
{
|
||||
info_log += "Failed to get shared intermediate code.";
|
||||
if (!intermediate) {
|
||||
// Defensive: parse+link succeeded above, so this should be
|
||||
// unreachable. If glslang ever changes that contract we'd
|
||||
// rather surface a clear error than dereference null (the
|
||||
// pre-fix bug that masqueraded as a silent SIGSEGV).
|
||||
return fail("glslang produced no intermediate code", {});
|
||||
}
|
||||
|
||||
spv::SpvBuildLogger logger;
|
||||
std::vector<std::uint32_t> spirv;
|
||||
glslang::GlslangToSpv(*intermediate, spirv, &logger);
|
||||
std::string errorLog = logger.getAllMessages();
|
||||
glslang::FinalizeProcess();
|
||||
std::string spvLog = logger.getAllMessages();
|
||||
|
||||
fs::path filename = path.filename().replace_extension("spv");
|
||||
std::ofstream file(outputDir/filename, std::ios::binary);
|
||||
if (!file) {
|
||||
return fail("failed to open SPIR-V output", (outputDir/filename).string());
|
||||
}
|
||||
file.write(reinterpret_cast<const char*>(spirv.data()), spirv.size() * sizeof(std::uint32_t));
|
||||
return errorLog;
|
||||
|
||||
if (!spvLog.empty()) {
|
||||
return fail("SPIR-V codegen reported issues", std::move(spvLog));
|
||||
}
|
||||
glslang::FinalizeProcess();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue