shader_compiler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  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. bool uses_screen_texture;
  74. bool uses_depth_texture;
  75. bool uses_normal_roughness_texture;
  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;
  83. ShaderLanguage::TextureRepeat default_repeat;
  84. String sampler_array_name;
  85. int base_texture_binding_index = 0;
  86. int texture_layout_set = 0;
  87. String base_uniform_string;
  88. String global_buffer_array_variable;
  89. String instance_uniform_index_variable;
  90. uint32_t base_varying_index = 0;
  91. bool apply_luminance_multiplier = false;
  92. bool check_multiview_samplers = false;
  93. };
  94. private:
  95. ShaderLanguage parser;
  96. String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
  97. 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);
  98. 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);
  99. const ShaderLanguage::ShaderNode *shader = nullptr;
  100. const ShaderLanguage::FunctionNode *function = nullptr;
  101. StringName current_func_name;
  102. StringName time_name;
  103. HashSet<StringName> texture_functions;
  104. HashSet<StringName> used_name_defines;
  105. HashSet<StringName> used_flag_pointers;
  106. HashSet<StringName> used_rmode_defines;
  107. HashSet<StringName> internal_functions;
  108. HashSet<StringName> fragment_varyings;
  109. DefaultIdentifierActions actions;
  110. static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_name);
  111. public:
  112. Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
  113. void initialize(DefaultIdentifierActions p_actions);
  114. ShaderCompiler();
  115. };
  116. #endif // SHADER_COMPILER_H