shader_compiler_rd.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* shader_compiler_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_RD_H
  31. #define SHADER_COMPILER_RD_H
  32. #include "core/templates/pair.h"
  33. #include "servers/rendering/shader_language.h"
  34. #include "servers/rendering/shader_types.h"
  35. #include "servers/rendering_server.h"
  36. class ShaderCompilerRD {
  37. public:
  38. struct IdentifierActions {
  39. Map<StringName, Pair<int *, int>> render_mode_values;
  40. Map<StringName, bool *> render_mode_flags;
  41. Map<StringName, bool *> usage_flag_pointers;
  42. Map<StringName, bool *> write_flag_pointers;
  43. Map<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms;
  44. };
  45. struct GeneratedCode {
  46. Vector<String> defines;
  47. struct Texture {
  48. StringName name;
  49. ShaderLanguage::DataType type;
  50. ShaderLanguage::ShaderNode::Uniform::Hint hint;
  51. ShaderLanguage::TextureFilter filter;
  52. ShaderLanguage::TextureRepeat repeat;
  53. bool global;
  54. };
  55. Vector<Texture> texture_uniforms;
  56. Vector<uint32_t> uniform_offsets;
  57. uint32_t uniform_total_size;
  58. String uniforms;
  59. String vertex_global;
  60. String vertex;
  61. String fragment_global;
  62. String fragment;
  63. String light;
  64. String compute_global;
  65. String compute;
  66. bool uses_global_textures;
  67. bool uses_fragment_time;
  68. bool uses_vertex_time;
  69. };
  70. struct DefaultIdentifierActions {
  71. Map<StringName, String> renames;
  72. Map<StringName, String> render_mode_defines;
  73. Map<StringName, String> usage_defines;
  74. Map<StringName, String> custom_samplers;
  75. ShaderLanguage::TextureFilter default_filter;
  76. ShaderLanguage::TextureRepeat default_repeat;
  77. String sampler_array_name;
  78. int base_texture_binding_index = 0;
  79. int texture_layout_set = 0;
  80. String base_uniform_string;
  81. String global_buffer_array_variable;
  82. String instance_uniform_index_variable;
  83. uint32_t base_varying_index = 0;
  84. };
  85. private:
  86. ShaderLanguage parser;
  87. String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
  88. void _dump_function_deps(const ShaderLanguage::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added);
  89. 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);
  90. const ShaderLanguage::ShaderNode *shader;
  91. const ShaderLanguage::FunctionNode *function;
  92. StringName current_func_name;
  93. StringName vertex_name;
  94. StringName fragment_name;
  95. StringName light_name;
  96. StringName compute_name;
  97. StringName time_name;
  98. Set<StringName> texture_functions;
  99. Set<StringName> used_name_defines;
  100. Set<StringName> used_flag_pointers;
  101. Set<StringName> used_rmode_defines;
  102. Set<StringName> internal_functions;
  103. Set<StringName> fragment_varyings;
  104. DefaultIdentifierActions actions;
  105. static ShaderLanguage::DataType _get_variable_type(const StringName &p_type);
  106. public:
  107. Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
  108. void initialize(DefaultIdentifierActions p_actions);
  109. ShaderCompilerRD();
  110. };
  111. #endif // SHADERCOMPILERRD_H