shader_compiler.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* shader_compiler.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_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 = nullptr;
  50. };
  51. struct GeneratedCode {
  52. Vector<String> defines;
  53. struct Texture {
  54. StringName name;
  55. ShaderLanguage::DataType type = ShaderLanguage::DataType::TYPE_VOID;
  56. ShaderLanguage::ShaderNode::Uniform::Hint hint = ShaderLanguage::ShaderNode::Uniform::Hint::HINT_NONE;
  57. bool use_color = false;
  58. ShaderLanguage::TextureFilter filter = ShaderLanguage::TextureFilter::FILTER_DEFAULT;
  59. ShaderLanguage::TextureRepeat repeat = ShaderLanguage::TextureRepeat::REPEAT_DEFAULT;
  60. bool global = false;
  61. int array_size = 0;
  62. };
  63. Vector<Texture> texture_uniforms;
  64. Vector<uint32_t> uniform_offsets;
  65. uint32_t uniform_total_size = 0;
  66. String uniforms;
  67. String stage_globals[STAGE_MAX];
  68. HashMap<String, String> code;
  69. bool uses_global_textures = false;
  70. bool uses_fragment_time = false;
  71. bool uses_vertex_time = false;
  72. bool uses_screen_texture_mipmaps = false;
  73. bool uses_screen_texture = false;
  74. bool uses_depth_texture = false;
  75. bool uses_normal_roughness_texture = false;
  76. };
  77. struct DefaultIdentifierActions {
  78. HashMap<StringName, String> renames;
  79. HashMap<StringName, String> render_mode_defines;
  80. HashMap<StringName, String> usage_defines;
  81. HashMap<StringName, String> custom_samplers;
  82. ShaderLanguage::TextureFilter default_filter = ShaderLanguage::TextureFilter::FILTER_NEAREST;
  83. ShaderLanguage::TextureRepeat default_repeat = ShaderLanguage::TextureRepeat::REPEAT_DISABLE;
  84. int base_texture_binding_index = 0;
  85. int texture_layout_set = 0;
  86. String base_uniform_string;
  87. String global_buffer_array_variable;
  88. String instance_uniform_index_variable;
  89. uint32_t base_varying_index = 0;
  90. bool apply_luminance_multiplier = false;
  91. bool check_multiview_samplers = false;
  92. };
  93. private:
  94. ShaderLanguage parser;
  95. String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
  96. 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);
  97. 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);
  98. const ShaderLanguage::ShaderNode *shader = nullptr;
  99. const ShaderLanguage::FunctionNode *function = nullptr;
  100. StringName current_func_name;
  101. StringName time_name;
  102. HashSet<StringName> texture_functions;
  103. HashSet<StringName> used_name_defines;
  104. HashSet<StringName> used_flag_pointers;
  105. HashSet<StringName> used_rmode_defines;
  106. HashSet<StringName> internal_functions;
  107. HashSet<StringName> fragment_varyings;
  108. DefaultIdentifierActions actions;
  109. static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_name);
  110. public:
  111. Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
  112. void initialize(DefaultIdentifierActions p_actions);
  113. ShaderCompiler();
  114. };
  115. #endif // SHADER_COMPILER_H