Skip to content

Commit 0cdd89a

Browse files
committed
fix NSC binary output write requests causing funny bugs with missing spv file after successful compilation
1 parent c463fd7 commit 0cdd89a

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

tools/nsc/main.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ class ShaderCompiler final : public system::IApplicationFramework
121121
output_filepath = outputFlagVector[1];
122122
}
123123
m_arguments.erase(output_flag_pos, output_flag_pos+2);
124+
125+
if (output_filepath.empty())
126+
{
127+
m_logger->log("Invalid output file path!" + output_filepath, ILogger::ELL_ERROR);
128+
return false;
129+
}
124130

125131
m_logger->log("Compiled shader code will be saved to " + output_filepath, ILogger::ELL_INFO);
126132
}
@@ -155,14 +161,52 @@ class ShaderCompiler final : public system::IApplicationFramework
155161
auto compilation_result = compile_shader(shader.get(), file_to_compile);
156162

157163
// writie compiled shader to file as bytes
158-
if (compilation_result && !output_filepath.empty()) {
164+
if (compilation_result)
165+
{
166+
m_logger->log("Shader compilation successful.", ILogger::ELL_INFO);
167+
{
168+
const auto location = std::filesystem::path(output_filepath);
169+
const auto parentDirectory = location.parent_path();
170+
171+
if (!std::filesystem::exists(parentDirectory))
172+
{
173+
if (!std::filesystem::create_directories(parentDirectory))
174+
{
175+
m_logger->log("Failed to create parent directory for the " + output_filepath + "output!", ILogger::ELL_ERROR);
176+
return false;
177+
}
178+
}
179+
}
180+
159181
std::fstream output_file(output_filepath, std::ios::out | std::ios::binary);
182+
183+
if (!output_file.is_open())
184+
{
185+
m_logger->log("Failed to open output file: " + output_filepath, ILogger::ELL_ERROR);
186+
return false;
187+
}
188+
160189
output_file.write((const char*)compilation_result->getContent()->getPointer(), compilation_result->getContent()->getSize());
190+
191+
if (output_file.fail())
192+
{
193+
m_logger->log("Failed to write to output file: " + output_filepath, ILogger::ELL_ERROR);
194+
output_file.close();
195+
return false;
196+
}
197+
161198
output_file.close();
162-
m_logger->log("Shader compilation successful.", ILogger::ELL_INFO);
199+
200+
if (output_file.fail())
201+
{
202+
m_logger->log("Failed to close output file: " + output_filepath, ILogger::ELL_ERROR);
203+
return false;
204+
}
205+
163206
return true;
164207
}
165-
else {
208+
else
209+
{
166210
m_logger->log("Shader compilation failed.", ILogger::ELL_ERROR);
167211
return false;
168212
}

0 commit comments

Comments
 (0)