rendering_shader_container.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**************************************************************************/
  2. /* rendering_shader_container.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/object/ref_counted.h"
  32. #include "servers/rendering/rendering_device_commons.h"
  33. struct SpvReflectShaderModule;
  34. struct SpvReflectDescriptorBinding;
  35. struct SpvReflectSpecializationConstant;
  36. class RenderingShaderContainer : public RefCounted {
  37. GDSOFTCLASS(RenderingShaderContainer, RefCounted);
  38. public:
  39. static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;
  40. static const uint32_t CONTAINER_VERSION = 2;
  41. protected:
  42. using RDC = RenderingDeviceCommons;
  43. struct ContainerHeader {
  44. uint32_t magic_number = 0;
  45. uint32_t version = 0;
  46. uint32_t format = 0;
  47. uint32_t format_version = 0;
  48. uint32_t shader_count = 0;
  49. };
  50. struct ReflectionData {
  51. uint64_t vertex_input_mask = 0;
  52. uint32_t fragment_output_mask = 0;
  53. uint32_t specialization_constants_count = 0;
  54. uint32_t is_compute = 0;
  55. uint32_t has_multiview = 0;
  56. uint32_t has_dynamic_buffers = 0;
  57. uint32_t compute_local_size[3] = {};
  58. uint32_t set_count = 0;
  59. uint32_t push_constant_size = 0;
  60. uint32_t push_constant_stages_mask = 0;
  61. uint32_t stage_count = 0;
  62. uint32_t shader_name_len = 0;
  63. };
  64. struct ReflectionBindingData {
  65. uint32_t type = 0;
  66. uint32_t binding = 0;
  67. uint32_t stages = 0;
  68. uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).
  69. uint32_t writable = 0;
  70. bool operator<(const ReflectionBindingData &p_other) const {
  71. return binding < p_other.binding;
  72. }
  73. };
  74. struct ReflectionSpecializationData {
  75. uint32_t type = 0;
  76. uint32_t constant_id = 0;
  77. uint32_t int_value = 0;
  78. uint32_t stage_flags = 0;
  79. };
  80. struct ShaderHeader {
  81. uint32_t shader_stage = 0;
  82. uint32_t code_compressed_size = 0;
  83. uint32_t code_compression_flags = 0;
  84. uint32_t code_decompressed_size = 0;
  85. };
  86. ReflectionData reflection_data;
  87. Vector<uint32_t> reflection_binding_set_uniforms_count;
  88. Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
  89. Vector<ReflectionSpecializationData> reflection_specialization_data;
  90. Vector<RDC::ShaderStage> reflection_shader_stages;
  91. virtual uint32_t _format() const = 0;
  92. virtual uint32_t _format_version() const = 0;
  93. // These methods will always be called with a valid pointer.
  94. virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);
  95. virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);
  96. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);
  97. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  98. virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);
  99. virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  100. virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);
  101. virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  102. virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);
  103. // These methods will be called with a nullptr to retrieve the size of the data.
  104. virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;
  105. virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;
  106. virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  107. virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  108. virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  109. virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
  110. template <class T>
  111. struct ReflectSymbol {
  112. static constexpr uint32_t STAGE_INDEX[RDC::SHADER_STAGE_MAX] = {
  113. 0, // SHADER_STAGE_VERTEX
  114. 1, // SHADER_STAGE_FRAGMENT
  115. 0, // SHADER_STAGE_TESSELATION_CONTROL
  116. 1, // SHADER_STAGE_TESSELATION_EVALUATION
  117. 0, // SHADER_STAGE_COMPUTE
  118. };
  119. BitField<RDC::ShaderStage> stages = {};
  120. private:
  121. const T *_spv_reflect[2] = { nullptr };
  122. public:
  123. _FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RDC::ShaderStage p_stage) const {
  124. DEV_ASSERT(stages.has_flag((1 << p_stage)));
  125. return STAGE_INDEX[p_stage];
  126. }
  127. const T &get_spv_reflect(RDC::ShaderStage p_stage) const;
  128. /*! Returns the first valid stage if multiple stages are set.
  129. *
  130. * Crashes if no stages are set.
  131. */
  132. const T &get_spv_reflect() const {
  133. for (const T *d : _spv_reflect) {
  134. if (d != nullptr) {
  135. return *d;
  136. }
  137. }
  138. CRASH_NOW_MSG("No stages set in ReflectSymbol");
  139. }
  140. void set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv);
  141. };
  142. struct ReflectImageTraits {
  143. RDC::DataFormat format = RDC::DATA_FORMAT_MAX;
  144. };
  145. struct ReflectUniform : ReflectSymbol<SpvReflectDescriptorBinding> {
  146. RDC::UniformType type = RDC::UniformType::UNIFORM_TYPE_MAX;
  147. uint32_t binding = 0;
  148. ReflectImageTraits image;
  149. uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
  150. bool writable = false;
  151. bool operator<(const ReflectUniform &p_other) const {
  152. if (binding != p_other.binding) {
  153. return binding < p_other.binding;
  154. }
  155. if (type != p_other.type) {
  156. return type < p_other.type;
  157. }
  158. if (writable != p_other.writable) {
  159. return writable < p_other.writable;
  160. }
  161. if (stages != p_other.stages) {
  162. return stages < p_other.stages;
  163. }
  164. if (length != p_other.length) {
  165. return length < p_other.length;
  166. }
  167. return false;
  168. }
  169. };
  170. struct ReflectSpecializationConstant : ReflectSymbol<SpvReflectSpecializationConstant> {
  171. RDC::PipelineSpecializationConstantType type = {};
  172. uint32_t constant_id = 0xffffffff;
  173. union {
  174. uint32_t int_value = 0;
  175. float float_value;
  176. bool bool_value;
  177. };
  178. bool operator<(const ReflectSpecializationConstant &p_other) const { return constant_id < p_other.constant_id; }
  179. };
  180. class ReflectShaderStage {
  181. friend class RenderingShaderContainer;
  182. Vector<uint8_t> _spirv_data;
  183. SpvReflectShaderModule *_module = nullptr;
  184. public:
  185. RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
  186. const SpvReflectShaderModule &module() const;
  187. const Span<uint32_t> spirv() const;
  188. const Vector<uint8_t> spirv_data() const { return _spirv_data; }
  189. ReflectShaderStage();
  190. ~ReflectShaderStage();
  191. };
  192. typedef LocalVector<ReflectUniform> ReflectDescriptorSet;
  193. struct ReflectShader {
  194. uint64_t vertex_input_mask = 0;
  195. uint32_t fragment_output_mask = 0;
  196. uint32_t compute_local_size[3] = {};
  197. uint32_t push_constant_size = 0;
  198. bool has_multiview = false;
  199. bool has_dynamic_buffers = false;
  200. LocalVector<ReflectShaderStage> shader_stages;
  201. LocalVector<ReflectDescriptorSet> uniform_sets;
  202. LocalVector<ReflectSymbol<SpvReflectDescriptorBinding>> reflect_uniforms;
  203. LocalVector<ReflectSpecializationConstant> specialization_constants;
  204. LocalVector<ReflectSymbol<SpvReflectSpecializationConstant>> reflect_specialization_constants;
  205. LocalVector<RDC::ShaderStage> stages_vector;
  206. BitField<RDC::ShaderStage> stages_bits = {};
  207. BitField<RDC::ShaderStage> push_constant_stages = {};
  208. _FORCE_INLINE_ bool is_compute() const {
  209. return stages_bits.has_flag(RDC::SHADER_STAGE_COMPUTE_BIT);
  210. }
  211. /*! Returns the uniform at the specified global index.
  212. *
  213. * This is a flattened view of all uniform sets.
  214. */
  215. ReflectUniform &uniform_at(uint32_t p_index) {
  216. for (LocalVector<ReflectUniform> &set : uniform_sets) {
  217. if (p_index < set.size()) {
  218. return set[p_index];
  219. }
  220. p_index -= set.size();
  221. }
  222. CRASH_NOW_MSG(vformat("Uniform index %d out of range (total %d)", p_index, uniform_count()));
  223. }
  224. uint32_t uniform_count() const {
  225. uint32_t count = 0;
  226. for (const LocalVector<ReflectUniform> &set : uniform_sets) {
  227. count += set.size();
  228. }
  229. return count;
  230. }
  231. };
  232. // This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
  233. virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader);
  234. // This method will be called when set_code_from_spirv() is called.
  235. virtual bool _set_code_from_spirv(const ReflectShader &p_shader) = 0;
  236. void set_from_shader_reflection(const ReflectShader &p_reflection);
  237. Error reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader);
  238. public:
  239. enum CompressionFlags {
  240. COMPRESSION_FLAG_ZSTD = 0x1,
  241. };
  242. struct Shader {
  243. RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
  244. PackedByteArray code_compressed_bytes;
  245. uint32_t code_compression_flags = 0;
  246. uint32_t code_decompressed_size = 0;
  247. };
  248. CharString shader_name;
  249. Vector<Shader> shaders;
  250. bool set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv);
  251. RDC::ShaderReflection get_shader_reflection() const;
  252. bool from_bytes(const PackedByteArray &p_bytes);
  253. PackedByteArray to_bytes() const;
  254. bool compress_code(const uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size, uint8_t *p_compressed_bytes, uint32_t *r_compressed_size, uint32_t *r_compressed_flags) const;
  255. bool decompress_code(const uint8_t *p_compressed_bytes, uint32_t p_compressed_size, uint32_t p_compressed_flags, uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size) const;
  256. RenderingShaderContainer();
  257. virtual ~RenderingShaderContainer();
  258. };
  259. class RenderingShaderContainerFormat : public RenderingDeviceCommons {
  260. GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);
  261. public:
  262. virtual Ref<RenderingShaderContainer> create_container() const = 0;
  263. virtual ShaderLanguageVersion get_shader_language_version() const = 0;
  264. virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;
  265. };