glslangValidator: Exit with an error if output file cannot be written

Propagate the error from glslang::OutputSpv[Hex|Bin] and exit with an error code if there is an error.
This commit is contained in:
Allan MacKinnon 2023-05-23 16:07:48 -04:00 committed by GitHub
parent a5bf69936d
commit 9fbc561947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

View File

@ -10036,27 +10036,32 @@ int GetSpirvGeneratorVersion()
} }
// Write SPIR-V out to a binary file // Write SPIR-V out to a binary file
void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) bool OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
{ {
std::ofstream out; std::ofstream out;
out.open(baseName, std::ios::binary | std::ios::out); out.open(baseName, std::ios::binary | std::ios::out);
if (out.fail()) if (out.fail()) {
printf("ERROR: Failed to open file: %s\n", baseName); printf("ERROR: Failed to open file: %s\n", baseName);
return false;
}
for (int i = 0; i < (int)spirv.size(); ++i) { for (int i = 0; i < (int)spirv.size(); ++i) {
unsigned int word = spirv[i]; unsigned int word = spirv[i];
out.write((const char*)&word, 4); out.write((const char*)&word, 4);
} }
out.close(); out.close();
return true;
} }
// Write SPIR-V out to a text file with 32-bit hexadecimal words // Write SPIR-V out to a text file with 32-bit hexadecimal words
void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) bool OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
{ {
#if !defined(GLSLANG_WEB) #if !defined(GLSLANG_WEB)
std::ofstream out; std::ofstream out;
out.open(baseName, std::ios::binary | std::ios::out); out.open(baseName, std::ios::binary | std::ios::out);
if (out.fail()) if (out.fail()) {
printf("ERROR: Failed to open file: %s\n", baseName); printf("ERROR: Failed to open file: %s\n", baseName);
return false;
}
out << "\t// " << out << "\t// " <<
GetSpirvGeneratorVersion() << GetSpirvGeneratorVersion() <<
GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH <<
@ -10083,6 +10088,7 @@ void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName,
} }
out.close(); out.close();
#endif #endif
return true;
} }
// //

View File

@ -55,7 +55,7 @@ void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsign
SpvOptions* options = nullptr); SpvOptions* options = nullptr);
void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
spv::SpvBuildLogger* logger, SpvOptions* options = nullptr); spv::SpvBuildLogger* logger, SpvOptions* options = nullptr);
void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName); bool OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName);
void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName); bool OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName);
} }

View File

@ -1526,9 +1526,11 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
if (! (Options & EOptionMemoryLeakMode)) { if (! (Options & EOptionMemoryLeakMode)) {
printf("%s", logger.getAllMessages().c_str()); printf("%s", logger.getAllMessages().c_str());
if (Options & EOptionOutputHexadecimal) { if (Options & EOptionOutputHexadecimal) {
glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName); if (!glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName))
exit(EFailUsage);
} else { } else {
glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage)); if (!glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage)))
exit(EFailUsage);
} }
outputFiles.push_back(GetBinaryName((EShLanguage)stage)); outputFiles.push_back(GetBinaryName((EShLanguage)stage));