shader_preprocessor.h 8.0 KB

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