shader_compiler.h 5.7 KB

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