Skip to content

Commit cc37325

Browse files
old behaviour is back!
1 parent edaed60 commit cc37325

File tree

4 files changed

+82
-76
lines changed

4 files changed

+82
-76
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,75 +26,81 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
2626

2727
class NBL_API2 IIncludeLoader : public core::IReferenceCounted
2828
{
29-
public:
30-
virtual std::optional<std::string> getInclude(const system::path& searchPath, const std::string& includeName) const = 0;
29+
public:
30+
struct found_t
31+
{
32+
system::path absolutePath = {};
33+
std::string contents = {};
34+
35+
explicit inline operator bool() const {return !absolutePath.empty();}
36+
};
37+
virtual found_t getInclude(const system::path& searchPath, const std::string& includeName) const = 0;
3138
};
3239

3340
class NBL_API2 IIncludeGenerator : public core::IReferenceCounted
3441
{
35-
public:
36-
// ! if includeName doesn't begin with prefix from `getPrefix` this function will return an empty string
37-
virtual std::optional<std::string> getInclude(const std::string& includeName) const;
42+
public:
43+
// ! if includeName doesn't begin with prefix from `getPrefix` this function will return an empty string
44+
virtual IIncludeLoader::found_t getInclude(const std::string& includeName) const;
3845

39-
virtual std::string_view getPrefix() const = 0;
46+
virtual std::string_view getPrefix() const = 0;
4047

41-
protected:
48+
protected:
4249

43-
using HandleFunc_t = std::function<std::string(const std::string&)>;
44-
virtual core::vector<std::pair<std::regex, HandleFunc_t>> getBuiltinNamesToFunctionMapping() const = 0;
50+
using HandleFunc_t = std::function<std::string(const std::string&)>;
51+
virtual core::vector<std::pair<std::regex,HandleFunc_t>> getBuiltinNamesToFunctionMapping() const = 0;
4552

46-
// ! Parses arguments from include path
47-
// ! template is path/to/shader.hlsl/arg0/arg1/...
48-
static core::vector<std::string> parseArgumentsFromPath(const std::string& _path);
53+
// ! Parses arguments from include path
54+
// ! template is path/to/shader.hlsl/arg0/arg1/...
55+
static core::vector<std::string> parseArgumentsFromPath(const std::string& _path);
4956
};
5057

5158
class NBL_API2 CFileSystemIncludeLoader : public IIncludeLoader
5259
{
53-
public:
54-
CFileSystemIncludeLoader(core::smart_refctd_ptr<system::ISystem>&& system);
60+
public:
61+
CFileSystemIncludeLoader(core::smart_refctd_ptr<system::ISystem>&& system);
5562

56-
std::optional<std::string> getInclude(const system::path& searchPath, const std::string& includeName) const override;
63+
IIncludeLoader::found_t getInclude(const system::path& searchPath, const std::string& includeName) const override;
5764

58-
protected:
59-
core::smart_refctd_ptr<system::ISystem> m_system;
65+
protected:
66+
core::smart_refctd_ptr<system::ISystem> m_system;
6067
};
6168

6269
class NBL_API2 CIncludeFinder : public core::IReferenceCounted
6370
{
64-
public:
65-
CIncludeFinder(core::smart_refctd_ptr<system::ISystem>&& system);
66-
67-
// ! includes within <>
68-
// @param requestingSourceDir: the directory where the incude was requested
69-
// @param includeName: the string within <> of the include preprocessing directive
70-
std::optional<std::string> getIncludeStandard(const system::path& requestingSourceDir, const std::string& includeName) const;
71+
public:
72+
CIncludeFinder(core::smart_refctd_ptr<system::ISystem>&& system);
7173

72-
// ! includes within ""
73-
// @param requestingSourceDir: the directory where the incude was requested
74-
// @param includeName: the string within "" of the include preprocessing directive
75-
std::optional<std::string> getIncludeRelative(const system::path& requestingSourceDir, const std::string& includeName) const;
74+
// ! includes within <>
75+
// @param requestingSourceDir: the directory where the incude was requested
76+
// @param includeName: the string within <> of the include preprocessing directive
77+
IIncludeLoader::found_t getIncludeStandard(const system::path& requestingSourceDir, const std::string& includeName) const;
7678

77-
inline core::smart_refctd_ptr<CFileSystemIncludeLoader> getDefaultFileSystemLoader() const { return m_defaultFileSystemLoader; }
79+
// ! includes within ""
80+
// @param requestingSourceDir: the directory where the incude was requested
81+
// @param includeName: the string within "" of the include preprocessing directive
82+
IIncludeLoader::found_t getIncludeRelative(const system::path& requestingSourceDir, const std::string& includeName) const;
7883

79-
void addSearchPath(const std::string& searchPath, const core::smart_refctd_ptr<IIncludeLoader>& loader);
84+
inline core::smart_refctd_ptr<CFileSystemIncludeLoader> getDefaultFileSystemLoader() const { return m_defaultFileSystemLoader; }
8085

81-
void addGenerator(const core::smart_refctd_ptr<IIncludeGenerator>& generator);
86+
void addSearchPath(const std::string& searchPath, const core::smart_refctd_ptr<IIncludeLoader>& loader);
8287

83-
protected:
88+
void addGenerator(const core::smart_refctd_ptr<IIncludeGenerator>& generator);
8489

85-
std::optional<std::string> trySearchPaths(const std::string& includeName) const;
90+
protected:
91+
IIncludeLoader::found_t trySearchPaths(const std::string& includeName) const;
8692

87-
std::optional<std::string> tryIncludeGenerators(const std::string& includeName) const;
93+
IIncludeLoader::found_t tryIncludeGenerators(const std::string& includeName) const;
8894

89-
struct LoaderSearchPath
90-
{
91-
core::smart_refctd_ptr<IIncludeLoader> loader = nullptr;
92-
std::string searchPath = {};
93-
};
95+
struct LoaderSearchPath
96+
{
97+
core::smart_refctd_ptr<IIncludeLoader> loader = nullptr;
98+
std::string searchPath = {};
99+
};
94100

95-
std::vector<LoaderSearchPath> m_loaders;
96-
std::vector<core::smart_refctd_ptr<IIncludeGenerator>> m_generators;
97-
core::smart_refctd_ptr<CFileSystemIncludeLoader> m_defaultFileSystemLoader;
101+
std::vector<LoaderSearchPath> m_loaders;
102+
std::vector<core::smart_refctd_ptr<IIncludeGenerator>> m_generators;
103+
core::smart_refctd_ptr<CFileSystemIncludeLoader> m_defaultFileSystemLoader;
98104
};
99105

100106
enum class E_SPIRV_VERSION : uint32_t

src/nbl/asset/utils/CGLSLCompiler.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace nbl::asset::impl
7777
if (std::filesystem::exists(name) && !reqBuiltin)
7878
name = std::filesystem::absolute(name);
7979

80-
std::optional<std::string> result;
80+
IShaderCompiler::IIncludeLoader::found_t result;
8181
if (_type == shaderc_include_type_relative)
8282
{
8383
result = m_defaultIncludeFinder->getIncludeRelative(relDir, _requested_source);
@@ -87,16 +87,18 @@ namespace nbl::asset::impl
8787
result = m_defaultIncludeFinder->getIncludeStandard(relDir, _requested_source);
8888
}
8989

90-
if (!result) {
90+
if (!result)
91+
{
9192
const char* error_str = "Could not open file";
9293
res->content_length = strlen(error_str);
9394
res->content = new char[res->content_length + 1u];
9495
strcpy(const_cast<char*>(res->content), error_str);
9596
res->source_name_length = 0u;
9697
res->source_name = "";
9798
}
98-
else {
99-
auto& res_str = *result;
99+
else
100+
{
101+
auto res_str = std::move(result.contents);
100102
//employ encloseWithinExtraInclGuards() in order to prevent infinite loop of (not necesarilly direct) self-inclusions while other # directives (incl guards among them) are disabled
101103
IShaderCompiler::disableAllDirectivesExceptIncludes(res_str);
102104
disableGlDirectives(res_str);

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,22 @@ std::string IShaderCompiler::preprocessShader(
119119
return preprocessShader(std::move(code), stage, preprocessOptions);
120120
}
121121

122-
std::optional<std::string> IShaderCompiler::IIncludeGenerator::getInclude(const std::string& includeName) const
122+
auto IShaderCompiler::IIncludeGenerator::getInclude(const std::string& includeName) const -> IIncludeLoader::found_t
123123
{
124124
core::vector<std::pair<std::regex, HandleFunc_t>> builtinNames = getBuiltinNamesToFunctionMapping();
125125

126126
for (const auto& pattern : builtinNames)
127-
if (std::regex_match(includeName, pattern.first))
127+
if (std::regex_match(includeName,pattern.first))
128+
{
129+
if (auto contents=pattern.second(includeName); !contents.empty())
128130
{
129-
if(auto contents = pattern.second(includeName); !contents.empty())
130-
return contents;
131+
// Welcome, you've came to a very disused piece of code, please check the first parameter (path) makes sense!
132+
_NBL_DEBUG_BREAK_IF(true);
133+
return {includeName,contents};
131134
}
135+
}
132136

133-
return std::nullopt;
137+
return {};
134138
}
135139

136140
core::vector<std::string> IShaderCompiler::IIncludeGenerator::parseArgumentsFromPath(const std::string& _path)
@@ -148,7 +152,7 @@ core::vector<std::string> IShaderCompiler::IIncludeGenerator::parseArgumentsFrom
148152
IShaderCompiler::CFileSystemIncludeLoader::CFileSystemIncludeLoader(core::smart_refctd_ptr<system::ISystem>&& system) : m_system(std::move(system))
149153
{}
150154

151-
std::optional<std::string> IShaderCompiler::CFileSystemIncludeLoader::getInclude(const system::path& searchPath, const std::string& includeName) const
155+
auto IShaderCompiler::CFileSystemIncludeLoader::getInclude(const system::path& searchPath, const std::string& includeName) const -> found_t
152156
{
153157
system::path path = searchPath / includeName;
154158
if (std::filesystem::exists(path))
@@ -163,7 +167,7 @@ std::optional<std::string> IShaderCompiler::CFileSystemIncludeLoader::getInclude
163167
future.acquire().move_into(f);
164168
}
165169
if (!f)
166-
return std::nullopt;
170+
return {};
167171
const size_t size = f->getSize();
168172

169173
std::string contents(size, '\0');
@@ -172,7 +176,7 @@ std::optional<std::string> IShaderCompiler::CFileSystemIncludeLoader::getInclude
172176
const bool success = bool(succ);
173177
assert(success);
174178

175-
return contents;
179+
return {f->getFileName(),std::move(contents)};
176180
}
177181

178182
IShaderCompiler::CIncludeFinder::CIncludeFinder(core::smart_refctd_ptr<system::ISystem>&& system)
@@ -184,21 +188,21 @@ IShaderCompiler::CIncludeFinder::CIncludeFinder(core::smart_refctd_ptr<system::I
184188
// ! includes within <>
185189
// @param requestingSourceDir: the directory where the incude was requested
186190
// @param includeName: the string within <> of the include preprocessing directive
187-
std::optional<std::string> IShaderCompiler::CIncludeFinder::getIncludeStandard(const system::path& requestingSourceDir, const std::string& includeName) const
191+
auto IShaderCompiler::CIncludeFinder::getIncludeStandard(const system::path& requestingSourceDir, const std::string& includeName) const -> IIncludeLoader::found_t
188192
{
189193
if (auto contents = tryIncludeGenerators(includeName))
190194
return contents;
191195
if (auto contents = trySearchPaths(includeName))
192196
return contents;
193-
return m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(), includeName);
197+
return m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(),includeName);
194198
}
195199

196200
// ! includes within ""
197201
// @param requestingSourceDir: the directory where the incude was requested
198202
// @param includeName: the string within "" of the include preprocessing directive
199-
std::optional<std::string> IShaderCompiler::CIncludeFinder::getIncludeRelative(const system::path& requestingSourceDir, const std::string& includeName) const
203+
auto IShaderCompiler::CIncludeFinder::getIncludeRelative(const system::path& requestingSourceDir, const std::string& includeName) const -> IIncludeLoader::found_t
200204
{
201-
if (auto contents = m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(), includeName))
205+
if (auto contents = m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(),includeName))
202206
return contents;
203207
return trySearchPaths(includeName);
204208
}
@@ -226,19 +230,15 @@ void IShaderCompiler::CIncludeFinder::addGenerator(const core::smart_refctd_ptr<
226230
m_generators.insert(found, generatorToAdd);
227231
}
228232

229-
std::optional<std::string> IShaderCompiler::CIncludeFinder::trySearchPaths(const std::string& includeName) const
233+
auto IShaderCompiler::CIncludeFinder::trySearchPaths(const std::string& includeName) const -> IIncludeLoader::found_t
230234
{
231235
for (const auto& itr : m_loaders)
232-
{
233-
if (auto contents = itr.loader->getInclude(itr.searchPath, includeName))
234-
{
235-
return contents;
236-
}
237-
}
238-
return std::nullopt;
236+
if (auto contents = itr.loader->getInclude(itr.searchPath,includeName))
237+
return contents;
238+
return {};
239239
}
240240

241-
std::optional<std::string> IShaderCompiler::CIncludeFinder::tryIncludeGenerators(const std::string& includeName) const
241+
auto IShaderCompiler::CIncludeFinder::tryIncludeGenerators(const std::string& includeName) const -> IIncludeLoader::found_t
242242
{
243243
// Need custom function because std::filesystem doesn't consider the parameters we use after the extension like CustomShader.hlsl/512/64
244244
auto removeExtension = [](const std::string& str)
@@ -287,5 +287,5 @@ std::optional<std::string> IShaderCompiler::CIncludeFinder::tryIncludeGenerators
287287
path = path.parent_path();
288288
}
289289

290-
return std::nullopt;
290+
return {};
291291
}

src/nbl/asset/utils/waveContext.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,11 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
441441
std::string file_path(s);
442442

443443
// call the 'found_include_directive' hook function
444-
if (ctx.get_hooks().found_include_directive(ctx.derived(),f,include_next))
444+
if (ctx.get_hooks().found_include_directive(ctx.derived(),f,false))
445445
return true; // client returned false: skip file to include
446446
file_path = util::impl::unescape_lit(file_path);
447447

448-
std::optional<std::string> result;
448+
IShaderCompiler::IIncludeLoader::found_t result;
449449
auto* includeFinder = ctx.get_hooks().m_includeFinder;
450450
if (includeFinder)
451451
{
@@ -462,22 +462,20 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
462462
return false;
463463
}
464464

465-
ctx.located_include_content = std::move(*result);
465+
ctx.located_include_content = std::move(result.contents);
466466
// the new include file determines the actual current directory
467-
// TODO: the found file can be in a completely different place
468-
nbl::system::path abs_path = ctx.get_current_directory()/file_path;
469-
ctx.set_current_directory(abs_path);
467+
ctx.set_current_directory(result.absolutePath);
470468

471469
{
472470
// preprocess the opened file
473471
boost::shared_ptr<base_iteration_context_type> new_iter_ctx(
474-
new iteration_context_type(ctx,abs_path.string().c_str(),act_pos,
472+
new iteration_context_type(ctx,result.absolutePath.string().c_str(),act_pos,
475473
boost::wave::enable_prefer_pp_numbers(ctx.get_language()),
476474
is_system ? base_iteration_context_type::system_header :
477475
base_iteration_context_type::user_header));
478476

479477
// call the include policy trace function
480-
ctx.get_hooks().opened_include_file(ctx.derived(), file_path, abs_path.string(), is_system);
478+
ctx.get_hooks().opened_include_file(ctx.derived(),file_path,result.absolutePath.string(),is_system);
481479

482480
// store current file position
483481
iter_ctx->real_relative_filename = ctx.get_current_relative_filename().c_str();

0 commit comments

Comments
 (0)