@@ -159,80 +159,74 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
159
159
{
160
160
insertExtraDefines (code, preprocessOptions.extraDefines );
161
161
}
162
- if (preprocessOptions.includeFinder != nullptr )
163
- {
164
- IShaderCompiler::disableAllDirectivesExceptIncludes (code);
165
-
166
- tcpp::StringInputStream codeIs = tcpp::StringInputStream (code);
167
- tcpp::Lexer lexer (codeIs);
168
- tcpp::Preprocessor proc (
169
- lexer,
170
- [&](auto errorInfo) {
171
- preprocessOptions.logger .log (" Pre-processor error at line %i:\n %s" , nbl::system::ILogger::ELL_ERROR, errorInfo.mLine , tcpp::ErrorTypeToString (errorInfo.mType ).c_str ());
172
- },
173
- [&](auto path, auto isSystemPath) {
162
+
163
+ IShaderCompiler::disableAllDirectivesExceptIncludes (code);
164
+
165
+ tcpp::StringInputStream codeIs = tcpp::StringInputStream (code);
166
+ tcpp::Lexer lexer (codeIs);
167
+ tcpp::Preprocessor proc (
168
+ lexer,
169
+ [&](auto errorInfo) {
170
+ preprocessOptions.logger .log (" Pre-processor error at line %i:\n %s" , nbl::system::ILogger::ELL_ERROR, errorInfo.mLine , tcpp::ErrorTypeToString (errorInfo.mType ).c_str ());
171
+ },
172
+ [&](auto path, auto isSystemPath) {
173
+ if (preprocessOptions.includeFinder )
174
+ {
174
175
return getInputStreamInclude (
175
176
preprocessOptions.includeFinder , m_system.get (), preprocessOptions.maxSelfInclusionCount + 1u ,
176
177
preprocessOptions.sourceIdentifier .data (), path.c_str (), !isSystemPath
177
178
);
178
179
}
179
- );
180
-
181
- auto pragmaShaderStageCallback = [&](IShader::E_SHADER_STAGE _stage) {
182
- return [&](tcpp::Preprocessor& preprocessor, tcpp::Lexer& lexer, const std::string& text) {
183
- stage = _stage;
184
- return std::string (" " );
185
- };
186
- };
187
-
188
- proc.AddCustomDirectiveHandler (std::string (" pragma shader_stage" ), [&](tcpp::Preprocessor& preprocessor, tcpp::Lexer& lexer, const std::string& text) {
189
- if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
190
- auto token = lexer.GetNextToken ();
191
- if (token.mType != tcpp::E_TOKEN_TYPE::OPEN_BRACKET) return std::string (" #error Malformed shader_stage pragma" );
192
-
193
- if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
194
- token = lexer.GetNextToken ();
195
- if (token.mType != tcpp::E_TOKEN_TYPE::IDENTIFIER) return std::string (" #error Malformed shader_stage pragma" );
196
-
197
- auto & shaderStageIdentifier = token.mRawView ;
198
- core::unordered_map<std::string, IShader::E_SHADER_STAGE> stageFromIdent = {
199
- { " vertex" , IShader::ESS_VERTEX },
200
- { " fragment" , IShader::ESS_FRAGMENT },
201
- { " tesscontrol" , IShader::ESS_TESSELLATION_CONTROL },
202
- { " tesseval" , IShader::ESS_TESSELLATION_EVALUATION },
203
- { " geometry" , IShader::ESS_GEOMETRY },
204
- { " compute" , IShader::ESS_COMPUTE }
205
- };
206
-
207
- auto found = stageFromIdent.find (shaderStageIdentifier);
208
- if (found == stageFromIdent.end ())
180
+ else
209
181
{
210
- return std::string (" #error Malformed shader_stage pragma, unknown stage " );
182
+ return static_cast <tcpp::IInputStream*>( new tcpp::StringInputStream ( std::string (" #error No include handler " )) );
211
183
}
212
- stage = found->second ;
184
+ }
185
+ );
186
+
187
+ proc.AddCustomDirectiveHandler (std::string (" pragma shader_stage" ), [&](tcpp::Preprocessor& preprocessor, tcpp::Lexer& lexer, const std::string& text) {
188
+ if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
189
+ auto token = lexer.GetNextToken ();
190
+ if (token.mType != tcpp::E_TOKEN_TYPE::OPEN_BRACKET) return std::string (" #error Malformed shader_stage pragma" );
191
+
192
+ if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
193
+ token = lexer.GetNextToken ();
194
+ if (token.mType != tcpp::E_TOKEN_TYPE::IDENTIFIER) return std::string (" #error Malformed shader_stage pragma" );
195
+
196
+ auto & shaderStageIdentifier = token.mRawView ;
197
+ core::unordered_map<std::string, IShader::E_SHADER_STAGE> stageFromIdent = {
198
+ { " vertex" , IShader::ESS_VERTEX },
199
+ { " fragment" , IShader::ESS_FRAGMENT },
200
+ { " tesscontrol" , IShader::ESS_TESSELLATION_CONTROL },
201
+ { " tesseval" , IShader::ESS_TESSELLATION_EVALUATION },
202
+ { " geometry" , IShader::ESS_GEOMETRY },
203
+ { " compute" , IShader::ESS_COMPUTE }
204
+ };
213
205
214
- if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
215
- token = lexer.GetNextToken ();
216
- if (token.mType != tcpp::E_TOKEN_TYPE::CLOSE_BRACKET) return std::string (" #error Malformed shader_stage pragma" );
206
+ auto found = stageFromIdent.find (shaderStageIdentifier);
207
+ if (found == stageFromIdent.end ())
208
+ {
209
+ return std::string (" #error Malformed shader_stage pragma, unknown stage" );
210
+ }
211
+ stage = found->second ;
217
212
218
- while (lexer.HasNextToken ()) {
219
- auto token = lexer.GetNextToken ();
220
- if (token.mType == tcpp::E_TOKEN_TYPE::NEWLINE) break ;
221
- if (token.mType != tcpp::E_TOKEN_TYPE::SPACE) return std::string (" #error Malformed shader_stage pragma" );
222
- }
213
+ if (!lexer.HasNextToken ()) return std::string (" #error Malformed shader_stage pragma" );
214
+ token = lexer.GetNextToken ();
215
+ if (token.mType != tcpp::E_TOKEN_TYPE::CLOSE_BRACKET) return std::string (" #error Malformed shader_stage pragma" );
223
216
224
- return std::string (" " );
225
- });
217
+ while (lexer.HasNextToken ()) {
218
+ auto token = lexer.GetNextToken ();
219
+ if (token.mType == tcpp::E_TOKEN_TYPE::NEWLINE) break ;
220
+ if (token.mType != tcpp::E_TOKEN_TYPE::SPACE) return std::string (" #error Malformed shader_stage pragma" );
221
+ }
226
222
227
- auto resolvedString = proc. Process ( );
228
- IShaderCompiler::reenableDirectives (resolvedString );
223
+ return std::string ( " " );
224
+ } );
229
225
230
- return resolvedString;
231
- }
232
- else
233
- {
234
- return code;
235
- }
226
+ auto resolvedString = proc.Process ();
227
+ IShaderCompiler::reenableDirectives (resolvedString);
228
+
229
+ return resolvedString;
236
230
}
237
231
238
232
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV (const char * code, const IShaderCompiler::SCompilerOptions& options) const
0 commit comments