@@ -14,14 +14,6 @@ namespace nbl::asset
14
14
class NBL_API2 CGLSLCompiler final : public IShaderCompiler
15
15
{
16
16
public:
17
- // string to be replaced with all "#" except those in "#include"
18
- static constexpr const char * PREPROC_DIRECTIVE_DISABLER = " _this_is_a_hash_" ;
19
- static constexpr const char * PREPROC_DIRECTIVE_ENABLER = PREPROC_DIRECTIVE_DISABLER;
20
- static constexpr const char * PREPROC_GL__DISABLER = " _this_is_a_GL__prefix_" ;
21
- static constexpr const char * PREPROC_GL__ENABLER = PREPROC_GL__DISABLER;
22
- static constexpr const char * PREPROC_LINE_CONTINUATION_DISABLER = " _this_is_a_line_continuation_\n " ;
23
- static constexpr const char * PREPROC_LINE_CONTINUATION_ENABLER = " _this_is_a_line_continuation_" ;
24
-
25
17
26
18
IShader::E_CONTENT_TYPE getCodeContentType () const override { return IShader::E_CONTENT_TYPE::ECT_GLSL; };
27
19
@@ -134,69 +126,28 @@ class NBL_API2 CGLSLCompiler final : public IShaderCompiler
134
126
}
135
127
}
136
128
137
- static void disableAllDirectivesExceptIncludes (std::string& _code)
129
+ std::string preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const override ;
130
+
131
+ static constexpr const char * PREPROC_GL__DISABLER = " _this_is_a_GL__prefix_" ;
132
+ static constexpr const char * PREPROC_GL__ENABLER = PREPROC_GL__DISABLER;
133
+ static constexpr const char * PREPROC_LINE_CONTINUATION_DISABLER = " _this_is_a_line_continuation_\n " ;
134
+ static constexpr const char * PREPROC_LINE_CONTINUATION_ENABLER = " _this_is_a_line_continuation_" ;
135
+
136
+ static void disableGlDirectives (std::string& _code)
138
137
{
139
- // TODO: replace this with a proper-ish proprocessor and includer one day
140
- std::regex directive (" #(?!(include|version|pragma shader_stage|line))" );// all # not followed by "include" nor "version" nor "pragma shader_stage"
141
- // `#pragma shader_stage(...)` is needed for determining shader stage when `_stage` param of IShaderCompiler functions is set to ESS_UNKNOWN
142
- auto result = std::regex_replace (_code, directive, PREPROC_DIRECTIVE_DISABLER);
143
138
std::regex glMacro (" [ \t\r\n\v\f ]GL_" );
144
- result = std::regex_replace (result , glMacro, PREPROC_GL__DISABLER);
139
+ auto result = std::regex_replace (_code , glMacro, PREPROC_GL__DISABLER);
145
140
std::regex lineContinuation (" \\\\ [ \t\r\n\v\f ]*\n " );
146
141
_code = std::regex_replace (result, lineContinuation, PREPROC_LINE_CONTINUATION_DISABLER);
147
142
}
148
- static void reenableDirectives (std::string& _code)
143
+
144
+ static void reenableGlDirectives (std::string& _code)
149
145
{
150
146
std::regex lineContinuation (PREPROC_LINE_CONTINUATION_ENABLER);
151
147
auto result = std::regex_replace (_code, lineContinuation, " \\ " );
152
148
std::regex glMacro (PREPROC_GL__ENABLER);
153
- result = std::regex_replace (result, glMacro, " GL_" );
154
- std::regex directive (PREPROC_DIRECTIVE_ENABLER);
155
- _code = std::regex_replace (result, directive, " #" );
149
+ _code = std::regex_replace (result, glMacro, " GL_" );
156
150
}
157
- static std::string encloseWithinExtraInclGuards (std::string&& _code, uint32_t _maxInclusions, const char * _identifier)
158
- {
159
- assert (_maxInclusions != 0u );
160
-
161
- using namespace std ::string_literals;
162
- std::string defBase_ = " _GENERATED_INCLUDE_GUARD_" s + _identifier + " _" ;
163
- std::replace_if (defBase_.begin (), defBase_.end (), [](char c) ->bool { return !::isalpha (c) && !::isdigit (c); }, ' _' );
164
-
165
- auto genDefs = [&defBase_, _maxInclusions, _identifier] {
166
- auto defBase = [&defBase_](uint32_t n) { return defBase_ + std::to_string (n); };
167
- std::string defs = " #ifndef " + defBase (0 ) + " \n\t #define " + defBase (0 ) + " \n " ;
168
- for (uint32_t i = 1u ; i <= _maxInclusions; ++i) {
169
- const std::string defname = defBase (i);
170
- defs += " #elif !defined(" + defname + " )\n\t #define " + defname + " \n " ;
171
- }
172
- defs += " #endif\n " ;
173
- return defs;
174
- };
175
- auto genUndefs = [&defBase_, _maxInclusions, _identifier] {
176
- auto defBase = [&defBase_](int32_t n) { return defBase_ + std::to_string (n); };
177
- std::string undefs = " #ifdef " + defBase (_maxInclusions) + " \n\t #undef " + defBase (_maxInclusions) + " \n " ;
178
- for (int32_t i = _maxInclusions - 1 ; i >= 0 ; --i) {
179
- const std::string defname = defBase (i);
180
- undefs += " #elif defined(" + defname + " )\n\t #undef " + defname + " \n " ;
181
- }
182
- undefs += " #endif\n " ;
183
- return undefs;
184
- };
185
-
186
- return
187
- genDefs () +
188
- " \n "
189
- " #ifndef " + defBase_ + std::to_string (_maxInclusions) +
190
- " \n " +
191
- " #line 1 \" " + _identifier + " \"\n " +
192
- _code +
193
- " \n "
194
- " #endif"
195
- " \n\n " +
196
- genUndefs ();
197
- }
198
-
199
- std::string preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const override ;
200
151
201
152
protected:
202
153
0 commit comments