shader_preprocessor.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_INCLUDE_PATH,
  50. };
  51. struct FilePosition {
  52. String file;
  53. int line = 0;
  54. };
  55. private:
  56. struct Token {
  57. char32_t text;
  58. int line;
  59. Token();
  60. Token(char32_t p_text, int p_line);
  61. };
  62. // The real preprocessor that understands basic shader and preprocessor language syntax.
  63. class Tokenizer {
  64. public:
  65. String code;
  66. int line;
  67. int index;
  68. int size;
  69. Vector<Token> generated;
  70. private:
  71. void add_generated(const Token &p_t);
  72. char32_t next();
  73. public:
  74. int get_line() const;
  75. int get_index() const;
  76. char32_t peek();
  77. void get_and_clear_generated(Vector<Token> *r_out);
  78. void backtrack(char32_t p_what);
  79. LocalVector<Token> advance(char32_t p_what);
  80. void skip_whitespace();
  81. String get_identifier(bool *r_is_cursor = nullptr, bool p_started = false);
  82. String peek_identifier();
  83. Token get_token();
  84. Tokenizer(const String &p_code);
  85. };
  86. class CommentRemover {
  87. private:
  88. LocalVector<char32_t> stripped;
  89. String code;
  90. int index;
  91. int line;
  92. int comment_line_open;
  93. int comments_open;
  94. int strings_open;
  95. public:
  96. String get_error() const;
  97. int get_error_line() const;
  98. char32_t peek() const;
  99. bool advance(char32_t p_what);
  100. String strip();
  101. CommentRemover(const String &p_code);
  102. };
  103. struct Define {
  104. Vector<String> arguments;
  105. String body;
  106. };
  107. struct SkippedCondition {
  108. int start_line = -1;
  109. int end_line = -1;
  110. };
  111. struct State {
  112. RBMap<String, Define *> defines;
  113. Vector<bool> skip_stack_else;
  114. int condition_depth = 0;
  115. RBSet<String> includes;
  116. List<uint64_t> cyclic_include_hashes; // Holds code hash of includes.
  117. int include_depth = 0;
  118. String current_include;
  119. String current_shader_type;
  120. String error;
  121. List<FilePosition> include_positions;
  122. RBMap<String, Vector<SkippedCondition *>> skipped_conditions;
  123. bool disabled = false;
  124. CompletionType completion_type = COMPLETION_TYPE_NONE;
  125. HashSet<Ref<ShaderInclude>> shader_includes;
  126. };
  127. private:
  128. LocalVector<char32_t> output;
  129. State *state = nullptr;
  130. bool state_owner = false;
  131. private:
  132. static bool is_char_word(char32_t p_char);
  133. static bool is_char_space(char32_t p_char);
  134. static bool is_char_end(char32_t p_char);
  135. static String vector_to_string(const LocalVector<char32_t> &p_v, int p_start = 0, int p_end = -1);
  136. static String tokens_to_string(const LocalVector<Token> &p_tokens);
  137. void process_directive(Tokenizer *p_tokenizer);
  138. void process_define(Tokenizer *p_tokenizer);
  139. void process_else(Tokenizer *p_tokenizer);
  140. void process_endif(Tokenizer *p_tokenizer);
  141. void process_if(Tokenizer *p_tokenizer);
  142. void process_ifdef(Tokenizer *p_tokenizer);
  143. void process_ifndef(Tokenizer *p_tokenizer);
  144. void process_include(Tokenizer *p_tokenizer);
  145. void process_pragma(Tokenizer *p_tokenizer);
  146. void process_undef(Tokenizer *p_tokenizer);
  147. void start_branch_condition(Tokenizer *p_tokenizer, bool p_success);
  148. void expand_output_macros(int p_start, int p_line);
  149. Error expand_macros(const String &p_string, int p_line, String &r_result);
  150. Error expand_macros(const String &p_string, int p_line, Vector<Pair<String, Define *>> p_defines, String &r_result);
  151. Error expand_macros_once(const String &p_line, int p_line_number, Pair<String, Define *> p_define_pair, String &r_expanded);
  152. bool find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start);
  153. String next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives);
  154. void add_to_output(const String &p_str);
  155. void set_error(const String &p_error, int p_line);
  156. static Define *create_define(const String &p_body);
  157. void clear();
  158. Error preprocess(State *p_state, const String &p_code, String &r_result);
  159. public:
  160. typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *);
  161. Error preprocess(const String &p_code, String &r_result, String *r_error_text = nullptr, List<FilePosition> *r_error_position = nullptr, HashSet<Ref<ShaderInclude>> *r_includes = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_options = nullptr, IncludeCompletionFunction p_include_completion_func = nullptr);
  162. static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords);
  163. static void get_pragma_list(List<String> *r_pragmas);
  164. ShaderPreprocessor();
  165. ~ShaderPreprocessor();
  166. };
  167. #endif // SHADER_PREPROCESSOR_H