Skip to content

Commit 0786239

Browse files
committed
Bug fixes 1
1 parent 787b887 commit 0786239

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
#include "nbl/asset/utils/ISPIRVOptimizer.h"
99
#include "nbl/asset/utils/IShaderCompiler.h"
10-
#include <wrl.h>
11-
#include <combaseapi.h>
12-
#include <sstream>
13-
#include <dxc/dxcapi.h>
1410

1511

1612

@@ -27,18 +23,7 @@ namespace nbl::asset
2723
class NBL_API2 CHLSLCompiler final : public IShaderCompiler
2824
{
2925
public:
30-
31-
struct DxcCompilationResult
32-
{
33-
Microsoft::WRL::ComPtr<IDxcBlobEncoding> errorMessages;
34-
Microsoft::WRL::ComPtr<IDxcBlob> objectBlob;
35-
Microsoft::WRL::ComPtr<IDxcResult> compileResult;
36-
37-
std::string GetErrorMessagesString()
38-
{
39-
return std::string(reinterpret_cast<char*>(errorMessages->GetBufferPointer()), errorMessages->GetBufferSize());
40-
}
41-
};
26+
4227

4328
IShader::E_CONTENT_TYPE getCodeContentType() const override { return IShader::E_CONTENT_TYPE::ECT_HLSL; };
4429

@@ -70,7 +55,11 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
7055

7156
void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
7257

73-
DxcCompilationResult dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options) const;
58+
struct SdxcCompileResult {
59+
uint8_t *begin;
60+
size_t size;
61+
};
62+
SdxcCompileResult dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options) const;
7463
protected:
7564

7665

include/nbl/system/ISystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class NBL_API2 ISystem : public core::IReferenceCounted
149149
m_cachedArchiveFiles.removeObject(dummy,pathAlias);
150150
}
151151

152+
void unmountBuiltins();
153+
152154
//
153155
struct SystemInfo
154156
{

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include <regex>
1414
#include <iterator>
1515
#include <codecvt>
16+
#include <wrl.h>
17+
#include <combaseapi.h>
18+
#include <sstream>
19+
#include <dxc/dxcapi.h>
1620

1721

1822
using namespace nbl;
@@ -31,6 +35,19 @@ struct DXC
3135
};
3236
}
3337

38+
struct DxcCompilationResult
39+
{
40+
Microsoft::WRL::ComPtr<IDxcBlobEncoding> errorMessages;
41+
Microsoft::WRL::ComPtr<IDxcBlob> objectBlob;
42+
Microsoft::WRL::ComPtr<IDxcResult> compileResult;
43+
44+
std::string GetErrorMessagesString()
45+
{
46+
return std::string(reinterpret_cast<char*>(errorMessages->GetBufferPointer()), errorMessages->GetBufferSize());
47+
}
48+
};
49+
50+
3451
CHLSLCompiler::CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
3552
: IShaderCompiler(std::move(system))
3653
{
@@ -53,7 +70,7 @@ CHLSLCompiler::~CHLSLCompiler()
5370
delete m_dxcCompilerTypes;
5471
}
5572

56-
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options) const
73+
CHLSLCompiler::SdxcCompileResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options) const
5774
{
5875
// Append Commandline options into source only if debugInfoFlags will emit source
5976
auto sourceEmittingFlags =
@@ -63,7 +80,7 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
6380
if ((options.debugInfoFlags.value & sourceEmittingFlags) != CHLSLCompiler::E_DEBUG_INFO_FLAGS::EDIF_NONE)
6481
{
6582
std::ostringstream insertion;
66-
insertion << "#pragma compile_flags ";
83+
insertion << "#pragma wave dxc_compile_flags( ";
6784

6885
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv;
6986
for (uint32_t arg = 0; arg < argCount; arg ++)
@@ -72,7 +89,7 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
7289
insertion << str.c_str() << " ";
7390
}
7491

75-
insertion << "\n";
92+
insertion << ")\n";
7693
insertIntoStart(source, std::move(insertion));
7794
}
7895
nbl::asset::impl::DXC* dxc = m_dxcCompilerTypes;
@@ -114,7 +131,7 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
114131
else
115132
{
116133
options.preprocessorOptions.logger.log("DXC Compilation Failed:\n%s", system::ILogger::ELL_ERROR, errorMessagesString.c_str());
117-
return result;
134+
return { (uint8_t*) result.objectBlob->GetBufferPointer(), result.objectBlob->GetBufferSize() };
118135
}
119136

120137
ComPtr<IDxcBlob> resultingBlob;
@@ -123,7 +140,7 @@ CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& sourc
123140

124141
result.objectBlob = resultingBlob;
125142

126-
return result;
143+
return { (uint8_t*)result.objectBlob->GetBufferPointer(), result.objectBlob->GetBufferSize() };
127144
}
128145

129146

@@ -135,7 +152,7 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
135152
nbl::wave::context context(code.begin(),code.end(),preprocessOptions.sourceIdentifier.data(),{preprocessOptions});
136153
context.add_macro_definition("__HLSL_VERSION");
137154

138-
// instead of defining extraDefines as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D 32768",
155+
// instead of defining extraDefines as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D 32768",
139156
// now define them as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D=32768"
140157
// to match boost wave syntax
141158
// https://www.boost.org/doc/libs/1_82_0/libs/wave/doc/class_reference_context.html#:~:text=Maintain%20defined%20macros-,add_macro_definition,-bool%20add_macro_definition
@@ -307,13 +324,13 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
307324
if (arg_storage)
308325
delete[] arg_storage;
309326

310-
if (!compileResult.objectBlob)
327+
if (!compileResult.begin)
311328
{
312329
return nullptr;
313330
}
314331

315-
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob->GetBufferSize());
316-
memcpy(outSpirv->getPointer(), compileResult.objectBlob->GetBufferPointer(), compileResult.objectBlob->GetBufferSize());
332+
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.size);
333+
memcpy(outSpirv->getPointer(), compileResult.begin, compileResult.size);
317334

318335
// Optimizer step
319336
if (hlslOptions.spirvOptimizer)

src/nbl/asset/utils/waveContext.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,28 @@ struct preprocessing_hooks final : public boost::wave::context_policies::default
105105

106106
if (strcmp(optionStr, "dxc_compile_flags") == 0 && hash_token_occurences == 1) {
107107
m_dxc_compile_flags_override.clear();
108+
std::string arg = "";
108109
for (auto valueIter = values.begin(); valueIter != values.end(); valueIter++) {
109110
std::string compiler_option_s = std::string(valueIter->get_value().c_str());
110-
// if this compiler flag is encapsulated in quotation marks, strip them
111-
//if (compiler_option_s[0] == '"' && compiler_option_s[compiler_option_s.length() - 1] == '"')
112-
// compiler_option_s = compiler_option_s.substr(1, compiler_option_s.length() - 2);
113-
114-
// if the compiler flag is a separator that is a comma or whitespace, do not add to list of flag overrides
115-
if (compiler_option_s == "," || compiler_option_s == " ")
116-
continue;
117-
m_dxc_compile_flags_override.push_back(compiler_option_s);
111+
//the compiler_option_s is a token thus can be only part of the actual argument, i.e. "-spirv" will be split into tokens [ "-", "spirv" ]
112+
//for dxc_compile_flags just join the strings until it finds a whitespace or end of args
113+
114+
// if the compiler flag is a separator that is a whitespace, do not add to list of flag overrides
115+
if (compiler_option_s == " ")
116+
{
117+
//reset
118+
arg.clear();
119+
m_dxc_compile_flags_override.push_back(arg);
120+
}
121+
else
122+
{
123+
//append
124+
arg += compiler_option_s;
125+
}
118126
}
127+
if(arg.size() > 0)
128+
m_dxc_compile_flags_override.push_back(arg);
129+
119130
return true;
120131
}
121132

@@ -479,6 +490,9 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
479490
else
480491
result = includeFinder->getIncludeRelative(ctx.get_current_directory(),file_path);
481492
}
493+
else {
494+
ctx.get_hooks().m_logger.log("Include finder not assigned, preprocessor will not include file " + file_path, nbl::system::ILogger::ELL_ERROR);
495+
}
482496

483497
if (!result)
484498
{

src/nbl/system/ISystem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,8 @@ bool ISystem::ICaller::flushMapping(IFile* file, size_t offset, size_t size)
304304
else if (flags&IFile::ECF_COHERENT)
305305
return true;
306306
return flushMapping_impl(file,offset,size);
307+
}
308+
309+
void ISystem::unmountBuiltins() {
310+
307311
}

tools/ndt/main.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class ShaderCompiler final : public system::IApplicationFramework
6060
{
6161
if (argv[i] == "-no-nbl-builtins")
6262
{
63+
m_logger->log("Unmounting builtins.");
64+
m_system->unmountBuiltins();
6365
no_nbl_builtins = true;
6466
}
6567
else if (argv[i] == "-Fo")
@@ -78,9 +80,14 @@ class ShaderCompiler final : public system::IApplicationFramework
7880
auto compilation_result = compile_shader(shader_code, file_to_compile);
7981

8082
// writie compiled shader to file as bytes
81-
std::fstream output_file(output_filepath, std::ios::out | std::ios::binary);
82-
output_file.write((const char*)compilation_result.objectBlob->GetBufferPointer(), compilation_result.objectBlob->GetBufferSize());
83-
output_file.close();
83+
if (compilation_result.begin) {
84+
std::fstream output_file(output_filepath, std::ios::out | std::ios::binary);
85+
output_file.write((const char*)compilation_result.begin, compilation_result.size);
86+
output_file.close();
87+
}
88+
else {
89+
m_logger->log("Shader compilation failed.", ILogger::ELL_ERROR);
90+
}
8491
/*std::string command = "dxc.exe";
8592
for (std::string arg : arguments)
8693
{
@@ -103,7 +110,7 @@ class ShaderCompiler final : public system::IApplicationFramework
103110

104111
private:
105112

106-
CHLSLCompiler::DxcCompilationResult compile_shader(std::string& shader_code, std::string_view sourceIdentifier) {
113+
CHLSLCompiler::SdxcCompileResult compile_shader(std::string& shader_code, std::string_view sourceIdentifier) {
107114
constexpr uint32_t WorkgroupSize = 256;
108115
constexpr uint32_t WorkgroupCount = 2048;
109116
const string WorkgroupSizeAsStr = std::to_string(WorkgroupSize);
@@ -123,6 +130,9 @@ class ShaderCompiler final : public system::IApplicationFramework
123130
options.preprocessorOptions.logger = m_logger.get();
124131
options.preprocessorOptions.extraDefines = { &WorkgroupSizeDefine,&WorkgroupSizeDefine + 1 };
125132

133+
auto includeFinder = make_smart_refctd_ptr<IShaderCompiler::CIncludeFinder>(m_system);
134+
options.preprocessorOptions.includeFinder = includeFinder.get();
135+
126136
std::vector<std::string> dxc_compile_flags_from_pragma = {};
127137
auto preprocessed_shader_code = hlslcompiler->preprocessShader(std::move(shader_code), options.stage, dxc_compile_flags_from_pragma, options.preprocessorOptions);
128138

0 commit comments

Comments
 (0)