shader_compiler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* shader_compiler.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_COMPILER_H
  31. #define SHADER_COMPILER_H
  32. #include "core/templates/pair.h"
  33. #include "servers/rendering/shader_language.h"
  34. #include "servers/rendering_server.h"
  35. class ShaderCompiler {
  36. public:
  37. enum Stage {
  38. STAGE_VERTEX,
  39. STAGE_FRAGMENT,
  40. STAGE_COMPUTE,
  41. STAGE_MAX
  42. };
  43. struct IdentifierActions {
  44. HashMap<StringName, Stage> entry_point_stages;
  45. HashMap<StringName, Pair<int *, int>> render_mode_values;
  46. HashMap<StringName, bool *> render_mode_flags;
  47. HashMap<StringName, bool *> usage_flag_pointers;
  48. HashMap<StringName, bool *> write_flag_pointers;
  49. HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms;
  50. };
  51. struct GeneratedCode {
  52. Vector<String> defines;
  53. struct Texture {
  54. StringName name;
  55. ShaderLanguage::DataType type;
  56. ShaderLanguage::ShaderNode::Uniform::Hint hint;
  57. bool use_color = false;
  58. ShaderLanguage::TextureFilter filter;
  59. ShaderLanguage::TextureRepeat repeat;
  60. bool global;
  61. int array_size;
  62. };
  63. Vector<Texture> texture_uniforms;
  64. Vector<uint32_t> uniform_offsets;
  65. uint32_t uniform_total_size;
  66. String uniforms;
  67. String stage_globals[STAGE_MAX];
  68. HashMap<String, String> code;
  69. bool uses_global_textures;
  70. bool uses_fragment_time;
  71. bool uses_vertex_time;
  72. bool uses_screen_texture_mipmaps;
  73. };
  74. struct DefaultIdentifierActions {
  75. HashMap<StringName, String> renames;
  76. HashMap<StringName, String> render_mode_defines;
  77. HashMap<StringName, String> usage_defines;
  78. HashMap<StringName, String> custom_samplers;
  79. ShaderLanguage::TextureFilter default_filter;
  80. ShaderLanguage::TextureRepeat default_repeat;
  81. String sampler_array_name;
  82. int base_texture_binding_index = 0;
  83. int texture_layout_set = 0;
  84. String base_uniform_string;
  85. String global_buffer_array_variable;
  86. String instance_uniform_index_variable;
  87. uint32_t base_varying_index = 0;
  88. bool apply_luminance_multiplier = false;
  89. };
  90. private:
  91. ShaderLanguage parser;
  92. String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
  93. void _dump_function_deps(const ShaderLanguage::ShaderNode *p_node, const StringName &p_for_func, const HashMap<StringName, String> &p_func_code, String &r_to_add, HashSet<StringName> &added);
  94. String _dump_node_code(const ShaderLanguage::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_scope = true);
  95. const ShaderLanguage::ShaderNode *shader = nullptr;
  96. const ShaderLanguage::FunctionNode *function = nullptr;
  97. StringName current_func_name;
  98. StringName time_name;
  99. HashSet<StringName> texture_functions;
  100. HashSet<StringName> used_name_defines;
  101. HashSet<StringName> used_flag_pointers;
  102. HashSet<StringName> used_rmode_defines;
  103. HashSet<StringName> internal_functions;
  104. HashSet<StringName> fragment_varyings;
  105. DefaultIdentifierActions actions;
  106. static ShaderLanguage::DataType _get_variable_type(const StringName &p_type);
  107. public:
  108. Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
  109. void initialize(DefaultIdentifierActions p_actions);
  110. ShaderCompiler();
  111. };
  112. #endif // SHADER_COMPILER_H