@@ -121,6 +121,12 @@ class ShaderCompiler final : public system::IApplicationFramework
121
121
output_filepath = outputFlagVector[1 ];
122
122
}
123
123
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
+ }
124
130
125
131
m_logger->log (" Compiled shader code will be saved to " + output_filepath, ILogger::ELL_INFO);
126
132
}
@@ -155,14 +161,52 @@ class ShaderCompiler final : public system::IApplicationFramework
155
161
auto compilation_result = compile_shader (shader.get (), file_to_compile);
156
162
157
163
// 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
+
159
181
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
+
160
189
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
+
161
198
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
+
163
206
return true ;
164
207
}
165
- else {
208
+ else
209
+ {
166
210
m_logger->log (" Shader compilation failed." , ILogger::ELL_ERROR);
167
211
return false ;
168
212
}
0 commit comments