Skip to content

Commit 209d600

Browse files
authored
Merge pull request #747 from Devsh-Graphics-Programming/ali_nl
HLSL-Preprocessor fixes
2 parents 291d67e + d58cf3a commit 209d600

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <combaseapi.h>
1818
#include <sstream>
1919
#include <dxc/dxcapi.h>
20+
#include <boost/algorithm/string/predicate.hpp>
21+
#include <boost/algorithm/string/trim.hpp>
2022

2123
using namespace nbl;
2224
using namespace nbl::asset;
@@ -311,6 +313,19 @@ static DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset
311313

312314
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
313315
{
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+
314329
nbl::wave::context context(code.begin(),code.end(),preprocessOptions.sourceIdentifier.data(),{preprocessOptions});
315330
// If dependencies were passed, we assume we want caching
316331
context.set_caching(bool(dependencies));
@@ -385,6 +400,7 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV_impl(const std:
385400
std::vector<std::string> dxc_compile_flags = {};
386401
IShader::E_SHADER_STAGE stage = options.stage;
387402
auto newCode = preprocessShader(std::string(code), stage, hlslOptions.preprocessorOptions, dxc_compile_flags, dependencies);
403+
if (newCode.empty()) return nullptr;
388404

389405
// Suffix is the shader model version
390406
std::wstring targetProfile(SHADER_MODEL_PROFILE);

0 commit comments

Comments
 (0)