|
39 | 39 |
|
40 | 40 | #include "nabla.h" |
41 | 41 | #include <boost/exception/diagnostic_information.hpp> |
| 42 | +#include <boost/wave/util/insert_whitespace_detection.hpp> |
| 43 | +#include <optional> |
42 | 44 |
|
43 | 45 | using namespace nbl; |
44 | 46 | using namespace nbl::asset; |
@@ -115,75 +117,144 @@ std::string makeWaveFailureContext( |
115 | 117 |
|
116 | 118 | return stream.str(); |
117 | 119 | } |
| 120 | + |
| 121 | +template<typename TokenT> |
| 122 | +bool isWhitespaceLikeToken(const TokenT& token) |
| 123 | +{ |
| 124 | + using namespace boost::wave; |
| 125 | + |
| 126 | + const auto id = token_id(token); |
| 127 | + return id == T_NEWLINE || id == T_GENERATEDNEWLINE || id == T_CONTLINE || IS_CATEGORY(token, WhiteSpaceTokenType); |
118 | 128 | } |
119 | 129 |
|
120 | | -namespace nbl::wave |
| 130 | +template<typename TokenT> |
| 131 | +std::string tokenValueToString(const TokenT& token) |
121 | 132 | { |
122 | | - std::string preprocess(std::string& code, const nbl::asset::IShaderCompiler::SPreprocessorOptions& preprocessOptions, bool withCaching, std::function<void(nbl::wave::context&)> post) |
| 133 | + const auto& value = token.get_value(); |
| 134 | + return std::string(value.data(), value.size()); |
| 135 | +} |
| 136 | + |
| 137 | +core::string renderPreprocessedOutput(nbl::wave::context& context) |
| 138 | +{ |
| 139 | + using namespace boost::wave; |
| 140 | + |
| 141 | + core::string output; |
| 142 | + util::insert_whitespace_detection whitespace(true); |
| 143 | + std::optional<nbl::wave::context::position_type> previousPosition = std::nullopt; |
| 144 | + bool previousWasExplicitWhitespace = false; |
| 145 | + |
| 146 | + for (auto it = context.begin(); it != context.end(); ++it) |
123 | 147 | { |
124 | | - nbl::wave::context context(code.begin(), code.end(), preprocessOptions.sourceIdentifier.data(), { preprocessOptions }); |
125 | | - context.set_caching(withCaching); |
126 | | - context.add_macro_definition("__HLSL_VERSION"); |
127 | | - context.add_macro_definition("__SPIRV_MAJOR_VERSION__=" + std::to_string(IShaderCompiler::getSpirvMajor(preprocessOptions.targetSpirvVersion))); |
128 | | - context.add_macro_definition("__SPIRV_MINOR_VERSION__=" + std::to_string(IShaderCompiler::getSpirvMinor(preprocessOptions.targetSpirvVersion))); |
129 | | - |
130 | | - // instead of defining extraDefines as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D 32768", |
131 | | - // now define them as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D=32768" |
132 | | - // to match boost wave syntax |
133 | | - // 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 |
134 | | - |
135 | | - // preprocess |
136 | | - core::string resolvedString; |
137 | | - const char* phase = "registering built-in macros"; |
138 | | - std::string activeMacroDefinition; |
139 | | - try |
| 148 | + const auto& token = *it; |
| 149 | + const auto id = token_id(token); |
| 150 | + if (id == T_EOF || id == T_EOI) |
| 151 | + continue; |
| 152 | + |
| 153 | + const auto explicitWhitespace = isWhitespaceLikeToken(token); |
| 154 | + const auto& position = token.get_position(); |
| 155 | + const auto value = tokenValueToString(token); |
| 156 | + |
| 157 | + if (previousPosition.has_value() && !explicitWhitespace) |
140 | 158 | { |
141 | | - phase = "registering extra macro definitions"; |
142 | | - for (const auto& define : preprocessOptions.extraDefines) |
| 159 | + const auto movedToNewLogicalLine = |
| 160 | + position.get_file() != previousPosition->get_file() || |
| 161 | + position.get_line() > previousPosition->get_line(); |
| 162 | + |
| 163 | + if (movedToNewLogicalLine) |
143 | 164 | { |
144 | | - activeMacroDefinition = define.identifier; |
145 | | - activeMacroDefinition.push_back('='); |
146 | | - activeMacroDefinition.append(define.definition); |
147 | | - context.add_macro_definition(activeMacroDefinition); |
| 165 | + if (output.empty() || output.back() != '\n') |
| 166 | + { |
| 167 | + output.push_back('\n'); |
| 168 | + whitespace.shift_tokens(T_NEWLINE); |
| 169 | + } |
| 170 | + } |
| 171 | + else if (!previousWasExplicitWhitespace && whitespace.must_insert(id, value)) |
| 172 | + { |
| 173 | + if (output.empty() || (output.back() != ' ' && output.back() != '\n' && output.back() != '\r' && output.back() != '\t')) |
| 174 | + { |
| 175 | + output.push_back(' '); |
| 176 | + whitespace.shift_tokens(T_SPACE); |
| 177 | + } |
148 | 178 | } |
149 | | - activeMacroDefinition.clear(); |
150 | | - |
151 | | - phase = "expanding translation unit"; |
152 | | - auto stream = std::stringstream(); |
153 | | - for (auto i = context.begin(); i != context.end(); i++) |
154 | | - stream << i->get_value(); |
155 | | - resolvedString = stream.str(); |
156 | | - } |
157 | | - catch (boost::wave::preprocess_exception& e) |
158 | | - { |
159 | | - const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, e.file_name(), e.line_no(), e.column_no()); |
160 | | - preprocessOptions.logger.log("%s exception caught. %s [%s:%d:%d]\n%s", system::ILogger::ELL_ERROR, e.what(), e.description(), e.file_name(), e.line_no(), e.column_no(), failureContext.c_str()); |
161 | | - preprocessOptions.logger.log("Boost diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::diagnostic_information(e).c_str()); |
162 | | - return {}; |
163 | | - } |
164 | | - catch (const boost::exception& e) |
165 | | - { |
166 | | - const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
167 | | - preprocessOptions.logger.log("Boost exception caught during Wave preprocessing.\n%s", system::ILogger::ELL_ERROR, failureContext.c_str()); |
168 | | - preprocessOptions.logger.log("Boost diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::diagnostic_information(e).c_str()); |
169 | | - return {}; |
170 | | - } |
171 | | - catch (const std::exception& e) |
172 | | - { |
173 | | - const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
174 | | - preprocessOptions.logger.log("std::exception caught during Wave preprocessing. %s\n%s", system::ILogger::ELL_ERROR, e.what(), failureContext.c_str()); |
175 | | - return {}; |
176 | 179 | } |
177 | | - catch (...) |
| 180 | + |
| 181 | + output += value; |
| 182 | + whitespace.shift_tokens(id); |
| 183 | + previousPosition = position; |
| 184 | + previousWasExplicitWhitespace = explicitWhitespace; |
| 185 | + } |
| 186 | + |
| 187 | + return output; |
| 188 | +} |
| 189 | + |
| 190 | +std::string preprocessImpl( |
| 191 | + std::string& code, |
| 192 | + const nbl::asset::IShaderCompiler::SPreprocessorOptions& preprocessOptions, |
| 193 | + const bool withCaching, |
| 194 | + std::function<void(nbl::wave::context&)> post) |
| 195 | +{ |
| 196 | + nbl::wave::context context(code.begin(), code.end(), preprocessOptions.sourceIdentifier.data(), { preprocessOptions }); |
| 197 | + context.set_caching(withCaching); |
| 198 | + context.add_macro_definition("__HLSL_VERSION"); |
| 199 | + context.add_macro_definition("__SPIRV_MAJOR_VERSION__=" + std::to_string(IShaderCompiler::getSpirvMajor(preprocessOptions.targetSpirvVersion))); |
| 200 | + context.add_macro_definition("__SPIRV_MINOR_VERSION__=" + std::to_string(IShaderCompiler::getSpirvMinor(preprocessOptions.targetSpirvVersion))); |
| 201 | + |
| 202 | + core::string resolvedString; |
| 203 | + const char* phase = "registering built-in macros"; |
| 204 | + std::string activeMacroDefinition; |
| 205 | + try |
| 206 | + { |
| 207 | + phase = "registering extra macro definitions"; |
| 208 | + for (const auto& define : preprocessOptions.extraDefines) |
178 | 209 | { |
179 | | - const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
180 | | - preprocessOptions.logger.log("Unknown exception caught during Wave preprocessing.\n%s", system::ILogger::ELL_ERROR, failureContext.c_str()); |
181 | | - preprocessOptions.logger.log("Current exception diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::current_exception_diagnostic_information().c_str()); |
182 | | - return {}; |
| 210 | + activeMacroDefinition = define.identifier; |
| 211 | + activeMacroDefinition.push_back('='); |
| 212 | + activeMacroDefinition.append(define.definition); |
| 213 | + context.add_macro_definition(activeMacroDefinition); |
183 | 214 | } |
| 215 | + activeMacroDefinition.clear(); |
| 216 | + |
| 217 | + phase = "expanding translation unit"; |
| 218 | + resolvedString = renderPreprocessedOutput(context); |
| 219 | + } |
| 220 | + catch (boost::wave::preprocess_exception& e) |
| 221 | + { |
| 222 | + const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, e.file_name(), e.line_no(), e.column_no()); |
| 223 | + preprocessOptions.logger.log("%s exception caught. %s [%s:%d:%d]\n%s", system::ILogger::ELL_ERROR, e.what(), e.description(), e.file_name(), e.line_no(), e.column_no(), failureContext.c_str()); |
| 224 | + preprocessOptions.logger.log("Boost diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::diagnostic_information(e).c_str()); |
| 225 | + return {}; |
| 226 | + } |
| 227 | + catch (const boost::exception& e) |
| 228 | + { |
| 229 | + const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
| 230 | + preprocessOptions.logger.log("Boost exception caught during Wave preprocessing.\n%s", system::ILogger::ELL_ERROR, failureContext.c_str()); |
| 231 | + preprocessOptions.logger.log("Boost diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::diagnostic_information(e).c_str()); |
| 232 | + return {}; |
| 233 | + } |
| 234 | + catch (const std::exception& e) |
| 235 | + { |
| 236 | + const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
| 237 | + preprocessOptions.logger.log("std::exception caught during Wave preprocessing. %s\n%s", system::ILogger::ELL_ERROR, e.what(), failureContext.c_str()); |
| 238 | + return {}; |
| 239 | + } |
| 240 | + catch (...) |
| 241 | + { |
| 242 | + const auto failureContext = makeWaveFailureContext(preprocessOptions, code, phase, activeMacroDefinition, preprocessOptions.sourceIdentifier.data(), 0, 0); |
| 243 | + preprocessOptions.logger.log("Unknown exception caught during Wave preprocessing.\n%s", system::ILogger::ELL_ERROR, failureContext.c_str()); |
| 244 | + preprocessOptions.logger.log("Current exception diagnostic information:\n%s", system::ILogger::ELL_ERROR, boost::current_exception_diagnostic_information().c_str()); |
| 245 | + return {}; |
| 246 | + } |
184 | 247 |
|
185 | | - post(context); |
| 248 | + post(context); |
186 | 249 |
|
187 | | - return resolvedString; |
| 250 | + return resolvedString; |
| 251 | +} |
| 252 | +} |
| 253 | + |
| 254 | +namespace nbl::wave |
| 255 | +{ |
| 256 | + std::string preprocess(std::string& code, const nbl::asset::IShaderCompiler::SPreprocessorOptions& preprocessOptions, bool withCaching, std::function<void(nbl::wave::context&)> post) |
| 257 | + { |
| 258 | + return preprocessImpl(code, preprocessOptions, withCaching, std::move(post)); |
188 | 259 | } |
189 | 260 | } |
0 commit comments