shader_preprocessor.h 8.0 KB

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