Skip to content

Commit edaed60

Browse files
we're close to proper handling
1 parent daea09e commit edaed60

File tree

2 files changed

+36
-59
lines changed

2 files changed

+36
-59
lines changed

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset::impl:
152152
std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
153153
{
154154
nbl::wave::context context(code.begin(),code.end(),preprocessOptions.sourceIdentifier.data(),{preprocessOptions});
155-
auto language = boost::wave::support_cpp20 | boost::wave::support_option_preserve_comments | boost::wave::support_option_emit_line_directives;
156-
context.set_language(static_cast<boost::wave::language_support>(language));
157155
context.add_macro_definition("__HLSL_VERSION");
158156

159157
// instead of defining extraDefines as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D 32768",

src/nbl/asset/utils/waveContext.h

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,13 @@ struct preprocessing_hooks final : public boost::wave::context_policies::default
4949
preprocessing_hooks(const IShaderCompiler::SPreprocessorOptions& _preprocessOptions)
5050
: m_includeFinder(_preprocessOptions.includeFinder), m_logger(_preprocessOptions.logger), m_pragmaStage(IShader::ESS_UNKNOWN) {}
5151

52-
5352
template <typename ContextT>
54-
bool locate_include_file(ContextT& ctx, std::string& file_path, bool is_system, char const* current_name, std::string& dir_path, std::string& native_name)
53+
bool locate_include_file(ContextT& ctx, std::string& file_path, bool is_system, char const* current_name, std::string& dir_path, std::string& native_name)
5554
{
56-
assert(current_name==nullptr);
57-
if (!m_includeFinder)
58-
return false;
59-
60-
dir_path = ctx.get_current_directory().string();
61-
std::optional<std::string> result;
62-
if (is_system)
63-
result = m_includeFinder->getIncludeStandard(dir_path, file_path);
64-
else
65-
result = m_includeFinder->getIncludeRelative(dir_path, file_path);
66-
67-
if (!result)
68-
{
69-
m_logger.log("Pre-processor error: Bad include file.\n'%s' does not exist.", nbl::system::ILogger::ELL_ERROR, file_path.c_str());
70-
return false;
71-
}
72-
ctx.set_located_include_content(std::move(*result));
73-
// TODO:
74-
native_name = file_path;
75-
return true;
55+
assert(false); // should never be called
56+
return false;
7657
}
7758

78-
7959
// interpretation of #pragma's of the form 'wave option[(value)]'
8060
template <typename ContextT, typename ContainerT>
8161
bool interpret_pragma(
@@ -172,8 +152,9 @@ class context : private boost::noncopyable
172152
, current_relative_filename(fname)
173153
, macros(*this_())
174154
, language(language_support(
175-
support_cpp
155+
support_cpp20
176156
| support_option_convert_trigraphs
157+
| support_option_preserve_comments
177158
| support_option_emit_line_directives
178159
| support_option_emit_pragma_directives
179160
| support_option_insert_whitespace
@@ -291,13 +272,6 @@ class context : private boost::noncopyable
291272
}
292273

293274
// access current language options
294-
void set_language(boost::wave::language_support language_,
295-
bool reset_macros = true)
296-
{
297-
language = language_;
298-
if (reset_macros)
299-
reset_macro_definitions();
300-
}
301275
boost::wave::language_support get_language() const { return language; }
302276

303277
position_type& get_main_pos() { return macros.get_main_pos(); }
@@ -328,20 +302,13 @@ class context : private boost::noncopyable
328302
}
329303

330304
// Nabla Additions Start
331-
system::path get_current_directory() const
305+
const system::path& get_current_directory() const
332306
{
333307
return current_dir;
334308
}
335-
void set_current_directory(char const* path_)
336-
{
337-
namespace fs = nbl::system;
338-
fs::path filepath(path_);
339-
fs::path filename = current_dir.is_absolute() ? filepath : (current_dir / filepath);
340-
current_dir = filename.parent_path();
341-
}
342-
void set_located_include_content(core::string&& content)
309+
void set_current_directory(const system::path& filepath)
343310
{
344-
located_include_content = std::move(content);
311+
current_dir = filepath.parent_path();
345312
}
346313
const core::string& get_located_include_content() const
347314
{
@@ -362,8 +329,7 @@ class context : private boost::noncopyable
362329
if (has_been_initialized)
363330
return;
364331

365-
nbl::system::path fpath(filename);
366-
set_current_directory(fpath.string().c_str());
332+
set_current_directory(system::path(filename));
367333
has_been_initialized = true; // execute once
368334
}
369335

@@ -445,8 +411,9 @@ class context : private boost::noncopyable
445411
// the main input stream
446412
target_iterator_type first; // underlying input stream
447413
target_iterator_type last;
448-
std::string filename; // associated main filename
414+
const std::string filename; // associated main filename
449415
bool has_been_initialized; // set cwd once
416+
450417
std::string current_relative_filename; // real relative name of current preprocessed file
451418

452419
// Nabla Additions Start
@@ -458,7 +425,7 @@ class context : private boost::noncopyable
458425
boost::wave::util::if_block_stack ifblocks; // conditional compilation contexts
459426
iteration_context_stack_type iter_ctxs; // iteration contexts
460427
macromap_type macros; // map of defined macros
461-
boost::wave::language_support language; // supported language/extensions
428+
const boost::wave::language_support language; // supported language/extensions
462429
preprocessing_hooks hooks; // hook policy instance
463430
};
464431

@@ -472,34 +439,45 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
472439

473440
// try to locate the given file, searching through the include path lists
474441
std::string file_path(s);
475-
std::string dir_path;
476442

477443
// call the 'found_include_directive' hook function
478444
if (ctx.get_hooks().found_include_directive(ctx.derived(),f,include_next))
479445
return true; // client returned false: skip file to include
480-
481446
file_path = util::impl::unescape_lit(file_path);
482-
std::string native_path_str;
483-
if (!ctx.get_hooks().locate_include_file(ctx, file_path, is_system, nullptr, dir_path, native_path_str))
447+
448+
std::optional<std::string> result;
449+
auto* includeFinder = ctx.get_hooks().m_includeFinder;
450+
if (includeFinder)
451+
{
452+
if (is_system)
453+
result = includeFinder->getIncludeStandard(ctx.get_current_directory(),file_path);
454+
else
455+
result = includeFinder->getIncludeRelative(ctx.get_current_directory(),file_path);
456+
}
457+
458+
if (!result)
484459
{
460+
ctx.get_hooks().m_logger.log("Pre-processor error: Bad include file.\n'%s' does not exist.", nbl::system::ILogger::ELL_ERROR, file_path.c_str());
485461
BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_file, file_path.c_str(), act_pos);
486462
return false;
487463
}
488464

489-
// test, if this file is known through a #pragma once directive
490-
{
491-
// the new include file determines the actual current directory
492-
ctx.set_current_directory(native_path_str.c_str());
465+
ctx.located_include_content = std::move(*result);
466+
// 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);
493470

471+
{
494472
// preprocess the opened file
495473
boost::shared_ptr<base_iteration_context_type> new_iter_ctx(
496-
new iteration_context_type(ctx, native_path_str.c_str(), act_pos,
474+
new iteration_context_type(ctx,abs_path.string().c_str(),act_pos,
497475
boost::wave::enable_prefer_pp_numbers(ctx.get_language()),
498476
is_system ? base_iteration_context_type::system_header :
499477
base_iteration_context_type::user_header));
500478

501479
// call the include policy trace function
502-
ctx.get_hooks().opened_include_file(ctx.derived(), dir_path, file_path, is_system);
480+
ctx.get_hooks().opened_include_file(ctx.derived(), file_path, abs_path.string(), is_system);
503481

504482
// store current file position
505483
iter_ctx->real_relative_filename = ctx.get_current_relative_filename().c_str();
@@ -516,12 +494,13 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
516494

517495
act_pos.set_file(iter_ctx->filename); // initialize file position
518496

519-
ctx.set_current_relative_filename(dir_path.c_str());
520-
iter_ctx->real_relative_filename = dir_path.c_str();
497+
ctx.set_current_relative_filename(file_path.c_str());
498+
iter_ctx->real_relative_filename = file_path.c_str();
521499

522500
act_pos.set_line(iter_ctx->line);
523501
act_pos.set_column(0);
524502
}
503+
525504
return true;
526505
}
527506
#endif

0 commit comments

Comments
 (0)