18
18
#include < sstream>
19
19
#include < dxc/dxcapi.h>
20
20
#include < boost/algorithm/string/predicate.hpp>
21
+ #include < boost/algorithm/string/trim.hpp>
21
22
22
23
using namespace nbl ;
23
24
using namespace nbl ::asset;
@@ -312,6 +313,19 @@ static DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset
312
313
313
314
std::string CHLSLCompiler::preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions, std::vector<std::string>& dxc_compile_flags_override, std::vector<CCache::SEntry::SPreprocessingDependency>* dependencies) const
314
315
{
316
+ // HACK: we do a pre-pre-process here to add \n after every #pragma to neutralize boost::wave's actions
317
+ // See https://github.com/Devsh-Graphics-Programming/Nabla/issues/746
318
+ size_t line_index = 0 ;
319
+ for (size_t i = 0 ; i < code.size (); i++) {
320
+ if (code[i] == ' \n ' ) {
321
+ auto line = code.substr (line_index, i - line_index);
322
+ boost::trim (line);
323
+ if (boost::starts_with (line, " #pragma" ))
324
+ code.insert (i++, 1 , ' \n ' );
325
+ line_index = i;
326
+ }
327
+ }
328
+
315
329
nbl::wave::context context (code.begin (),code.end (),preprocessOptions.sourceIdentifier .data (),{preprocessOptions});
316
330
// If dependencies were passed, we assume we want caching
317
331
context.set_caching (bool (dependencies));
@@ -329,19 +343,8 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
329
343
try
330
344
{
331
345
std::stringstream stream = std::stringstream ();
332
- int32_t emit_nl_after = 0 ;
333
- for (auto i=context.begin (); i!=context.end (); i++) {
334
- auto value = i->get_value ();
346
+ for (auto i=context.begin (); i!=context.end (); i++)
335
347
stream << i->get_value ();
336
-
337
- // TODO: replace this hack with `support_option_emit_contnewlines` flag when Boost::Wave respect it
338
- // See also https://github.com/Devsh-Graphics-Programming/Nabla/issues/746
339
- if (emit_nl_after != -1 ) {
340
- emit_nl_after--;
341
- if (emit_nl_after == 0 ) stream << " \n " ;
342
- }
343
- if (boost::ends_with (stream.str (), " #pragma shader_stage(" )) emit_nl_after = 2 ;
344
- }
345
348
resolvedString = stream.str ();
346
349
}
347
350
catch (boost::wave::preprocess_exception& e)
0 commit comments