shader_compiler_rd.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/pair.h"
  33. #include "servers/visual/shader_language.h"
  34. #include "servers/visual/shader_types.h"
  35. #include "servers/visual_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. };
  54. Vector<Texture> texture_uniforms;
  55. Vector<uint32_t> uniform_offsets;
  56. uint32_t uniform_total_size;
  57. String uniforms;
  58. String vertex_global;
  59. String vertex;
  60. String fragment_global;
  61. String fragment;
  62. String light;
  63. bool uses_fragment_time;
  64. bool uses_vertex_time;
  65. };
  66. struct DefaultIdentifierActions {
  67. Map<StringName, String> renames;
  68. Map<StringName, String> render_mode_defines;
  69. Map<StringName, String> usage_defines;
  70. Map<StringName, String> custom_samplers;
  71. ShaderLanguage::TextureFilter default_filter;
  72. ShaderLanguage::TextureRepeat default_repeat;
  73. String sampler_array_name;
  74. int base_texture_binding_index = 0;
  75. int texture_layout_set = 0;
  76. String base_uniform_string;
  77. uint32_t base_varying_index = 0;
  78. };
  79. private:
  80. ShaderLanguage parser;
  81. String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
  82. 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);
  83. 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);
  84. const ShaderLanguage::ShaderNode *shader;
  85. const ShaderLanguage::FunctionNode *function;
  86. StringName current_func_name;
  87. StringName vertex_name;
  88. StringName fragment_name;
  89. StringName light_name;
  90. StringName time_name;
  91. Set<StringName> texture_functions;
  92. Set<StringName> used_name_defines;
  93. Set<StringName> used_flag_pointers;
  94. Set<StringName> used_rmode_defines;
  95. Set<StringName> internal_functions;
  96. DefaultIdentifierActions actions;
  97. public:
  98. Error compile(VS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
  99. void initialize(DefaultIdentifierActions p_actions);
  100. ShaderCompilerRD();
  101. };
  102. #endif // SHADERCOMPILERRD_H