shader_rd.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**************************************************************************/
  2. /* shader_rd.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_RD_H
  31. #define SHADER_RD_H
  32. #include "core/os/mutex.h"
  33. #include "core/string/string_builder.h"
  34. #include "core/templates/hash_map.h"
  35. #include "core/templates/local_vector.h"
  36. #include "core/templates/rb_map.h"
  37. #include "core/templates/rid_owner.h"
  38. #include "core/variant/variant.h"
  39. #include "servers/rendering_server.h"
  40. class ShaderRD {
  41. public:
  42. struct VariantDefine {
  43. int group = 0;
  44. CharString text;
  45. bool default_enabled = true;
  46. VariantDefine() {}
  47. VariantDefine(int p_group, const String &p_text, bool p_default_enabled) {
  48. group = p_group;
  49. default_enabled = p_default_enabled;
  50. text = p_text.utf8();
  51. }
  52. };
  53. private:
  54. //versions
  55. CharString general_defines;
  56. Vector<VariantDefine> variant_defines;
  57. Vector<bool> variants_enabled;
  58. Vector<uint32_t> variant_to_group;
  59. HashMap<int, LocalVector<int>> group_to_variant_map;
  60. Vector<bool> group_enabled;
  61. Vector<RD::PipelineImmutableSampler> immutable_samplers;
  62. struct Version {
  63. CharString uniforms;
  64. CharString vertex_globals;
  65. CharString compute_globals;
  66. CharString fragment_globals;
  67. HashMap<StringName, CharString> code_sections;
  68. Vector<CharString> custom_defines;
  69. Vector<WorkerThreadPool::GroupID> group_compilation_tasks;
  70. Vector<Vector<uint8_t>> variant_data;
  71. Vector<RID> variants;
  72. bool valid;
  73. bool dirty;
  74. bool initialize_needed;
  75. };
  76. Mutex variant_set_mutex;
  77. struct CompileData {
  78. Version *version;
  79. int group = 0;
  80. };
  81. void _compile_variant(uint32_t p_variant, CompileData p_data);
  82. void _initialize_version(Version *p_version);
  83. void _clear_version(Version *p_version);
  84. void _compile_version_start(Version *p_version, int p_group);
  85. void _compile_version_end(Version *p_version, int p_group);
  86. void _compile_ensure_finished(Version *p_version);
  87. void _allocate_placeholders(Version *p_version, int p_group);
  88. RID_Owner<Version> version_owner;
  89. struct StageTemplate {
  90. struct Chunk {
  91. enum Type {
  92. TYPE_VERSION_DEFINES,
  93. TYPE_MATERIAL_UNIFORMS,
  94. TYPE_VERTEX_GLOBALS,
  95. TYPE_FRAGMENT_GLOBALS,
  96. TYPE_COMPUTE_GLOBALS,
  97. TYPE_CODE,
  98. TYPE_TEXT
  99. };
  100. Type type;
  101. StringName code;
  102. CharString text;
  103. };
  104. LocalVector<Chunk> chunks;
  105. };
  106. bool is_compute = false;
  107. String name;
  108. CharString base_compute_defines;
  109. String base_sha256;
  110. LocalVector<String> group_sha256;
  111. static String shader_cache_dir;
  112. static bool shader_cache_cleanup_on_start;
  113. static bool shader_cache_save_compressed;
  114. static bool shader_cache_save_compressed_zstd;
  115. static bool shader_cache_save_debug;
  116. bool shader_cache_dir_valid = false;
  117. enum StageType {
  118. STAGE_TYPE_VERTEX,
  119. STAGE_TYPE_FRAGMENT,
  120. STAGE_TYPE_COMPUTE,
  121. STAGE_TYPE_MAX,
  122. };
  123. StageTemplate stage_templates[STAGE_TYPE_MAX];
  124. void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template);
  125. void _add_stage(const char *p_code, StageType p_stage_type);
  126. String _version_get_sha1(Version *p_version) const;
  127. String _get_cache_file_path(Version *p_version, int p_group);
  128. bool _load_from_cache(Version *p_version, int p_group);
  129. void _save_to_cache(Version *p_version, int p_group);
  130. void _initialize_cache();
  131. protected:
  132. ShaderRD();
  133. void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);
  134. public:
  135. RID version_create();
  136. void version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines);
  137. void version_set_compute_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_compute_globals, const Vector<String> &p_custom_defines);
  138. _FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {
  139. ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID());
  140. ERR_FAIL_COND_V(!variants_enabled[p_variant], RID());
  141. Version *version = version_owner.get_or_null(p_version);
  142. ERR_FAIL_NULL_V(version, RID());
  143. if (version->dirty) {
  144. _initialize_version(version);
  145. for (int i = 0; i < group_enabled.size(); i++) {
  146. if (!group_enabled[i]) {
  147. _allocate_placeholders(version, i);
  148. continue;
  149. }
  150. _compile_version_start(version, i);
  151. }
  152. }
  153. uint32_t group = variant_to_group[p_variant];
  154. if (version->group_compilation_tasks[group] != 0) {
  155. _compile_version_end(version, group);
  156. }
  157. if (!version->valid) {
  158. return RID();
  159. }
  160. return version->variants[p_variant];
  161. }
  162. bool version_is_valid(RID p_version);
  163. bool version_free(RID p_version);
  164. // Enable/disable variants for things that you know won't be used at engine initialization time .
  165. void set_variant_enabled(int p_variant, bool p_enabled);
  166. bool is_variant_enabled(int p_variant) const;
  167. // Enable/disable groups for things that might be enabled at run time.
  168. void enable_group(int p_group);
  169. bool is_group_enabled(int p_group) const;
  170. static void set_shader_cache_dir(const String &p_dir);
  171. static void set_shader_cache_save_compressed(bool p_enable);
  172. static void set_shader_cache_save_compressed_zstd(bool p_enable);
  173. static void set_shader_cache_save_debug(bool p_enable);
  174. RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);
  175. void initialize(const Vector<String> &p_variant_defines, const String &p_general_defines = "", const Vector<RD::PipelineImmutableSampler> &r_immutable_samplers = Vector<RD::PipelineImmutableSampler>());
  176. void initialize(const Vector<VariantDefine> &p_variant_defines, const String &p_general_defines = "");
  177. virtual ~ShaderRD();
  178. };
  179. #endif // SHADER_RD_H