Skip to content

Commit d58cf3a

Browse files
committed
pre-pre-process
Signed-off-by: Ali Cheraghi <[email protected]>
1 parent 1a03a7d commit d58cf3a

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sstream>
1919
#include <dxc/dxcapi.h>
2020
#include <boost/algorithm/string/predicate.hpp>
21+
#include <boost/algorithm/string/trim.hpp>
2122

2223
using namespace nbl;
2324
using namespace nbl::asset;
@@ -312,6 +313,19 @@ static DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset
312313

313314
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
314315
{
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+
315329
nbl::wave::context context(code.begin(),code.end(),preprocessOptions.sourceIdentifier.data(),{preprocessOptions});
316330
// If dependencies were passed, we assume we want caching
317331
context.set_caching(bool(dependencies));
@@ -329,19 +343,8 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
329343
try
330344
{
331345
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++)
335347
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-
}
345348
resolvedString = stream.str();
346349
}
347350
catch (boost::wave::preprocess_exception& e)

0 commit comments

Comments
 (0)