| 1 | /**************************************************************************/ |
| 2 | /* shader_preprocessor.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef SHADER_PREPROCESSOR_H |
| 32 | #define SHADER_PREPROCESSOR_H |
| 33 | |
| 34 | #include "core/string/ustring.h" |
| 35 | #include "core/templates/list.h" |
| 36 | #include "core/templates/local_vector.h" |
| 37 | #include "core/templates/rb_map.h" |
| 38 | #include "core/templates/rb_set.h" |
| 39 | #include "core/typedefs.h" |
| 40 | |
| 41 | #include "core/io/resource_loader.h" |
| 42 | #include "core/object/script_language.h" |
| 43 | #include "core/os/os.h" |
| 44 | #include "scene/resources/shader.h" |
| 45 | #include "scene/resources/shader_include.h" |
| 46 | |
| 47 | class ShaderPreprocessor { |
| 48 | public: |
| 49 | enum CompletionType { |
| 50 | COMPLETION_TYPE_NONE, |
| 51 | COMPLETION_TYPE_DIRECTIVE, |
| 52 | COMPLETION_TYPE_PRAGMA_DIRECTIVE, |
| 53 | COMPLETION_TYPE_PRAGMA, |
| 54 | COMPLETION_TYPE_CONDITION, |
| 55 | COMPLETION_TYPE_INCLUDE_PATH, |
| 56 | }; |
| 57 | |
| 58 | struct FilePosition { |
| 59 | String file; |
| 60 | int line = 0; |
| 61 | }; |
| 62 | |
| 63 | struct Region { |
| 64 | String file; |
| 65 | int from_line = -1; |
| 66 | int to_line = -1; |
| 67 | bool enabled = false; |
| 68 | Region *parent = nullptr; |
| 69 | }; |
| 70 | |
| 71 | private: |
| 72 | struct Token { |
| 73 | char32_t text; |
| 74 | int line; |
| 75 | |
| 76 | Token(); |
| 77 | Token(char32_t p_text, int p_line); |
| 78 | }; |
| 79 | |
| 80 | // The real preprocessor that understands basic shader and preprocessor language syntax. |
| 81 | class Tokenizer { |
| 82 | public: |
| 83 | String code; |
| 84 | int line; |
| 85 | int index; |
| 86 | int size; |
| 87 | LocalVector<Token> generated; |
| 88 | |
| 89 | private: |
| 90 | void add_generated(const Token &p_t); |
| 91 | char32_t next(); |
| 92 | |
| 93 | public: |
| 94 | int get_line() const; |
| 95 | int get_index() const; |
| 96 | char32_t peek(); |
| 97 | int consume_line_continuations(int p_offset); |
| 98 | |
| 99 | void get_and_clear_generated(LocalVector<char32_t> *r_out); |
| 100 | void backtrack(char32_t p_what); |
| 101 | LocalVector<Token> advance(char32_t p_what); |
| 102 | void skip_whitespace(); |
| 103 | bool consume_empty_line(); |
| 104 | String get_identifier(bool *r_is_cursor = nullptr, bool p_started = false); |
| 105 | String peek_identifier(); |
| 106 | Token get_token(); |
| 107 | |
| 108 | Tokenizer(const String &p_code); |
| 109 | }; |
| 110 | |
| 111 | class { |
| 112 | private: |
| 113 | LocalVector<char32_t> ; |
| 114 | String ; |
| 115 | int ; |
| 116 | int ; |
| 117 | int ; |
| 118 | int ; |
| 119 | int ; |
| 120 | |
| 121 | public: |
| 122 | String () const; |
| 123 | int () const; |
| 124 | char32_t () const; |
| 125 | |
| 126 | bool (char32_t p_what); |
| 127 | String (); |
| 128 | |
| 129 | (const String &p_code); |
| 130 | }; |
| 131 | |
| 132 | struct Define { |
| 133 | Vector<String> arguments; |
| 134 | String body; |
| 135 | }; |
| 136 | |
| 137 | struct Branch { |
| 138 | Vector<bool> conditions; |
| 139 | Branch *parent = nullptr; |
| 140 | bool else_defined = false; |
| 141 | |
| 142 | Branch() {} |
| 143 | |
| 144 | Branch(bool p_condition, Branch *p_parent) : |
| 145 | parent(p_parent) { |
| 146 | conditions.push_back(p_condition); |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | struct State { |
| 151 | RBMap<String, Define *> defines; |
| 152 | List<Branch> branches; |
| 153 | Branch *current_branch = nullptr; |
| 154 | int condition_depth = 0; |
| 155 | RBSet<String> includes; |
| 156 | List<uint64_t> cyclic_include_hashes; // Holds code hash of includes. |
| 157 | int include_depth = 0; |
| 158 | String current_filename; |
| 159 | String current_shader_type; |
| 160 | String error; |
| 161 | List<FilePosition> include_positions; |
| 162 | bool save_regions = false; |
| 163 | RBMap<String, List<Region>> regions; |
| 164 | Region *previous_region = nullptr; |
| 165 | bool disabled = false; |
| 166 | CompletionType completion_type = COMPLETION_TYPE_NONE; |
| 167 | HashSet<Ref<ShaderInclude>> shader_includes; |
| 168 | }; |
| 169 | |
| 170 | private: |
| 171 | LocalVector<char32_t> output; |
| 172 | State *state = nullptr; |
| 173 | |
| 174 | private: |
| 175 | static bool is_char_word(char32_t p_char); |
| 176 | static bool is_char_space(char32_t p_char); |
| 177 | static bool is_char_end(char32_t p_char); |
| 178 | static String vector_to_string(const LocalVector<char32_t> &p_v, int p_start = 0, int p_end = -1); |
| 179 | static String tokens_to_string(const LocalVector<Token> &p_tokens); |
| 180 | |
| 181 | void _set_expected_error(const String &p_what, int p_line) { |
| 182 | set_error(vformat(RTR("Expected a '%s'." ), p_what), p_line); |
| 183 | } |
| 184 | |
| 185 | void _set_unexpected_token_error(const String &p_what, int p_line) { |
| 186 | set_error(vformat(RTR("Unexpected token: '%s'." ), p_what), p_line); |
| 187 | } |
| 188 | |
| 189 | void process_directive(Tokenizer *p_tokenizer); |
| 190 | void process_define(Tokenizer *p_tokenizer); |
| 191 | void process_elif(Tokenizer *p_tokenizer); |
| 192 | void process_else(Tokenizer *p_tokenizer); |
| 193 | void process_endif(Tokenizer *p_tokenizer); |
| 194 | void process_if(Tokenizer *p_tokenizer); |
| 195 | void process_ifdef(Tokenizer *p_tokenizer); |
| 196 | void process_ifndef(Tokenizer *p_tokenizer); |
| 197 | void process_include(Tokenizer *p_tokenizer); |
| 198 | void process_pragma(Tokenizer *p_tokenizer); |
| 199 | void process_undef(Tokenizer *p_tokenizer); |
| 200 | |
| 201 | void add_region(int p_line, bool p_enabled, Region *p_parent_region); |
| 202 | void start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue = false); |
| 203 | |
| 204 | Error expand_condition(const String &p_string, int p_line, String &r_result); |
| 205 | void expand_output_macros(int p_start, int p_line); |
| 206 | Error expand_macros(const String &p_string, int p_line, String &r_result); |
| 207 | bool expand_macros_once(const String &p_line, int p_line_number, const RBMap<String, Define *>::Element *p_define_pair, String &r_expanded); |
| 208 | bool find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start); |
| 209 | void concatenate_macro_body(String &r_body); |
| 210 | |
| 211 | String next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives); |
| 212 | void add_to_output(const String &p_str); |
| 213 | void set_error(const String &p_error, int p_line); |
| 214 | |
| 215 | static Define *create_define(const String &p_body); |
| 216 | |
| 217 | void clear_state(); |
| 218 | |
| 219 | Error preprocess(State *p_state, const String &p_code, String &r_result); |
| 220 | |
| 221 | public: |
| 222 | typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *); |
| 223 | |
| 224 | Error preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text = nullptr, List<FilePosition> *r_error_position = nullptr, List<Region> *r_regions = nullptr, HashSet<Ref<ShaderInclude>> *r_includes = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_options = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines = nullptr, IncludeCompletionFunction p_include_completion_func = nullptr); |
| 225 | |
| 226 | static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords = false); |
| 227 | static void get_pragma_list(List<String> *r_pragmas); |
| 228 | |
| 229 | ShaderPreprocessor(); |
| 230 | ~ShaderPreprocessor(); |
| 231 | }; |
| 232 | |
| 233 | #endif // SHADER_PREPROCESSOR_H |
| 234 | |