shader_preprocessor.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* shader_preprocessor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. void get_and_clear_generated(Vector<Token> *r_out);
  86. void backtrack(char32_t p_what);
  87. LocalVector<Token> advance(char32_t p_what);
  88. void skip_whitespace();
  89. String get_identifier(bool *r_is_cursor = nullptr, bool p_started = false);
  90. String peek_identifier();
  91. Token get_token();
  92. Tokenizer(const String &p_code);
  93. };
  94. class CommentRemover {
  95. private:
  96. LocalVector<char32_t> stripped;
  97. String code;
  98. int index;
  99. int line;
  100. int comment_line_open;
  101. int comments_open;
  102. int strings_open;
  103. public:
  104. String get_error() const;
  105. int get_error_line() const;
  106. char32_t peek() const;
  107. bool advance(char32_t p_what);
  108. String strip();
  109. CommentRemover(const String &p_code);
  110. };
  111. struct Define {
  112. Vector<String> arguments;
  113. String body;
  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. bool state_owner = false;
  148. private:
  149. static bool is_char_word(char32_t p_char);
  150. static bool is_char_space(char32_t p_char);
  151. static bool is_char_end(char32_t p_char);
  152. static String vector_to_string(const LocalVector<char32_t> &p_v, int p_start = 0, int p_end = -1);
  153. static String tokens_to_string(const LocalVector<Token> &p_tokens);
  154. void _set_expected_error(const String &p_what, int p_line) {
  155. set_error(vformat(RTR("Expected a '%s'."), p_what), p_line);
  156. }
  157. void _set_unexpected_token_error(const String &p_what, int p_line) {
  158. set_error(vformat(RTR("Unexpected token '%s'."), p_what), p_line);
  159. }
  160. void process_directive(Tokenizer *p_tokenizer);
  161. void process_define(Tokenizer *p_tokenizer);
  162. void process_elif(Tokenizer *p_tokenizer);
  163. void process_else(Tokenizer *p_tokenizer);
  164. void process_endif(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. String next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives);
  179. void add_to_output(const String &p_str);
  180. void set_error(const String &p_error, int p_line);
  181. static Define *create_define(const String &p_body);
  182. void clear();
  183. Error preprocess(State *p_state, const String &p_code, String &r_result);
  184. public:
  185. typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *);
  186. 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);
  187. static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords = false);
  188. static void get_pragma_list(List<String> *r_pragmas);
  189. ShaderPreprocessor();
  190. ~ShaderPreprocessor();
  191. };
  192. #endif // SHADER_PREPROCESSOR_H