shader_preprocessor.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifndef SHADER_PREPROCESSOR_H
  31. #define SHADER_PREPROCESSOR_H
  32. #include "core/string/ustring.h"
  33. #include "core/templates/list.h"
  34. #include "core/templates/local_vector.h"
  35. #include "core/templates/rb_map.h"
  36. #include "core/templates/rb_set.h"
  37. #include "core/object/script_language.h"
  38. #include "scene/resources/shader_include.h"
  39. class ShaderPreprocessor {
  40. public:
  41. enum CompletionType {
  42. COMPLETION_TYPE_NONE,
  43. COMPLETION_TYPE_DIRECTIVE,
  44. COMPLETION_TYPE_PRAGMA_DIRECTIVE,
  45. COMPLETION_TYPE_PRAGMA,
  46. COMPLETION_TYPE_CONDITION,
  47. COMPLETION_TYPE_INCLUDE_PATH,
  48. };
  49. struct FilePosition {
  50. String file;
  51. int line = 0;
  52. };
  53. struct Region {
  54. String file;
  55. int from_line = -1;
  56. int to_line = -1;
  57. bool enabled = false;
  58. Region *parent = nullptr;
  59. };
  60. private:
  61. struct Token {
  62. char32_t text;
  63. int line;
  64. Token();
  65. Token(char32_t p_text, int p_line);
  66. };
  67. // The real preprocessor that understands basic shader and preprocessor language syntax.
  68. class Tokenizer {
  69. public:
  70. String code;
  71. int line;
  72. int index;
  73. int size;
  74. LocalVector<Token> generated;
  75. private:
  76. void add_generated(const Token &p_t);
  77. char32_t next();
  78. public:
  79. int get_line() const;
  80. int get_index() const;
  81. char32_t peek();
  82. int consume_line_continuations(int p_offset);
  83. void get_and_clear_generated(LocalVector<char32_t> *r_out);
  84. void backtrack(char32_t p_what);
  85. LocalVector<Token> advance(char32_t p_what);
  86. void skip_whitespace();
  87. bool consume_empty_line();
  88. String get_identifier(bool *r_is_cursor = nullptr, bool p_started = false);
  89. String peek_identifier();
  90. Token get_token();
  91. Tokenizer(const String &p_code);
  92. };
  93. class CommentRemover {
  94. private:
  95. LocalVector<char32_t> stripped;
  96. String code;
  97. int index;
  98. int line;
  99. int comment_line_open;
  100. int comments_open;
  101. int strings_open;
  102. public:
  103. String get_error() const;
  104. int get_error_line() const;
  105. char32_t peek() const;
  106. bool advance(char32_t p_what);
  107. String strip();
  108. CommentRemover(const String &p_code);
  109. };
  110. struct Define {
  111. Vector<String> arguments;
  112. String body;
  113. bool is_builtin = false;
  114. };
  115. struct Branch {
  116. Vector<bool> conditions;
  117. Branch *parent = nullptr;
  118. bool else_defined = false;
  119. Branch() {}
  120. Branch(bool p_condition, Branch *p_parent) :
  121. parent(p_parent) {
  122. conditions.push_back(p_condition);
  123. }
  124. };
  125. struct State {
  126. RBMap<String, Define *> defines;
  127. List<Branch> branches;
  128. Branch *current_branch = nullptr;
  129. int condition_depth = 0;
  130. RBSet<String> includes;
  131. List<uint64_t> cyclic_include_hashes; // Holds code hash of includes.
  132. int include_depth = 0;
  133. String current_filename;
  134. String current_shader_type;
  135. String error;
  136. List<FilePosition> include_positions;
  137. bool save_regions = false;
  138. RBMap<String, List<Region>> regions;
  139. Region *previous_region = nullptr;
  140. bool disabled = false;
  141. CompletionType completion_type = COMPLETION_TYPE_NONE;
  142. HashSet<Ref<ShaderInclude>> shader_includes;
  143. };
  144. private:
  145. LocalVector<char32_t> output;
  146. State *state = nullptr;
  147. private:
  148. static bool is_char_word(char32_t p_char);
  149. static bool is_char_space(char32_t p_char);
  150. static bool is_char_end(char32_t p_char);
  151. static String vector_to_string(const LocalVector<char32_t> &p_v, int p_start = 0, int p_end = -1);
  152. static String tokens_to_string(const LocalVector<Token> &p_tokens);
  153. void _set_expected_error(const String &p_what, int p_line) {
  154. set_error(vformat(RTR("Expected a '%s'."), p_what), p_line);
  155. }
  156. void _set_unexpected_token_error(const String &p_what, int p_line) {
  157. set_error(vformat(RTR("Unexpected token: '%s'."), p_what), p_line);
  158. }
  159. void process_directive(Tokenizer *p_tokenizer);
  160. void process_define(Tokenizer *p_tokenizer);
  161. void process_elif(Tokenizer *p_tokenizer);
  162. void process_else(Tokenizer *p_tokenizer);
  163. void process_endif(Tokenizer *p_tokenizer);
  164. void process_error(Tokenizer *p_tokenizer);
  165. void process_if(Tokenizer *p_tokenizer);
  166. void process_ifdef(Tokenizer *p_tokenizer);
  167. void process_ifndef(Tokenizer *p_tokenizer);
  168. void process_include(Tokenizer *p_tokenizer);
  169. void process_pragma(Tokenizer *p_tokenizer);
  170. void process_undef(Tokenizer *p_tokenizer);
  171. void add_region(int p_line, bool p_enabled, Region *p_parent_region);
  172. void start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue = false);
  173. Error expand_condition(const String &p_string, int p_line, String &r_result);
  174. void expand_output_macros(int p_start, int p_line);
  175. Error expand_macros(const String &p_string, int p_line, String &r_result);
  176. bool expand_macros_once(const String &p_line, int p_line_number, const RBMap<String, Define *>::Element *p_define_pair, String &r_expanded);
  177. bool find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start);
  178. void concatenate_macro_body(String &r_body);
  179. String next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives);
  180. void add_to_output(const String &p_str);
  181. void set_error(const String &p_error, int p_line);
  182. static Define *create_define(const String &p_body);
  183. void insert_builtin_define(String p_name, String p_value, State &p_state);
  184. void clear_state();
  185. Error preprocess(State *p_state, const String &p_code, String &r_result);
  186. public:
  187. typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *);
  188. 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);
  189. static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords = false);
  190. static void get_pragma_list(List<String> *r_pragmas);
  191. ShaderPreprocessor();
  192. ~ShaderPreprocessor();
  193. };
  194. #endif // SHADER_PREPROCESSOR_H