2
0

rendering_shader_container.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. class RenderingShaderContainer : public RefCounted {
  35. GDSOFTCLASS(RenderingShaderContainer, RefCounted);
  36. public:
  37. static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;
  38. static const uint32_t CONTAINER_VERSION = 2;
  39. protected:
  40. struct ContainerHeader {
  41. uint32_t magic_number = 0;
  42. uint32_t version = 0;
  43. uint32_t format = 0;
  44. uint32_t format_version = 0;
  45. uint32_t shader_count = 0;
  46. };
  47. struct ReflectionData {
  48. uint64_t vertex_input_mask = 0;
  49. uint32_t fragment_output_mask = 0;
  50. uint32_t specialization_constants_count = 0;
  51. uint32_t is_compute = 0;
  52. uint32_t has_multiview = 0;
  53. uint32_t compute_local_size[3] = {};
  54. uint32_t set_count = 0;
  55. uint32_t push_constant_size = 0;
  56. uint32_t push_constant_stages_mask = 0;
  57. uint32_t stage_count = 0;
  58. uint32_t shader_name_len = 0;
  59. };
  60. struct ReflectionBindingData {
  61. uint32_t type = 0;
  62. uint32_t binding = 0;
  63. uint32_t stages = 0;
  64. uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).
  65. uint32_t writable = 0;
  66. bool operator<(const ReflectionBindingData &p_other) const {
  67. return binding < p_other.binding;
  68. }
  69. };
  70. struct ReflectionSpecializationData {
  71. uint32_t type = 0;
  72. uint32_t constant_id = 0;
  73. uint32_t int_value = 0;
  74. uint32_t stage_flags = 0;
  75. };
  76. struct ShaderHeader {
  77. uint32_t shader_stage = 0;
  78. uint32_t code_compressed_size = 0;
  79. uint32_t code_compression_flags = 0;
  80. uint32_t code_decompressed_size = 0;
  81. };
  82. ReflectionData reflection_data;
  83. Vector<uint32_t> reflection_binding_set_uniforms_count;
  84. Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
  85. Vector<ReflectionSpecializationData> reflection_specialization_data;
  86. Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;
  87. virtual uint32_t _format() const = 0;
  88. virtual uint32_t _format_version() const = 0;
  89. // These methods will always be called with a valid pointer.
  90. virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);
  91. virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);
  92. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);
  93. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  94. virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);
  95. virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  96. virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);
  97. virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);
  98. virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);
  99. // These methods will be called with a nullptr to retrieve the size of the data.
  100. virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;
  101. virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;
  102. virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  103. virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  104. virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
  105. virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
  106. // This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
  107. virtual void _set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection);
  108. class ReflectedShaderStage {
  109. friend class RenderingShaderContainer;
  110. Vector<uint8_t> _spirv_data;
  111. SpvReflectShaderModule *_module = nullptr;
  112. public:
  113. RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
  114. const SpvReflectShaderModule &module() const;
  115. const Span<uint32_t> spirv() const;
  116. const Vector<uint8_t> spirv_data() const { return _spirv_data; }
  117. ReflectedShaderStage();
  118. ~ReflectedShaderStage();
  119. };
  120. // This method will be called when set_code_from_spirv() is called.
  121. virtual bool _set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) = 0;
  122. void set_from_shader_reflection(const RenderingDeviceCommons::ShaderReflection &p_reflection);
  123. Error reflect_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv, LocalVector<ReflectedShaderStage> &r_refl);
  124. public:
  125. enum CompressionFlags {
  126. COMPRESSION_FLAG_ZSTD = 0x1,
  127. };
  128. struct Shader {
  129. RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
  130. PackedByteArray code_compressed_bytes;
  131. uint32_t code_compression_flags = 0;
  132. uint32_t code_decompressed_size = 0;
  133. };
  134. CharString shader_name;
  135. Vector<Shader> shaders;
  136. bool set_code_from_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv);
  137. RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;
  138. bool from_bytes(const PackedByteArray &p_bytes);
  139. PackedByteArray to_bytes() const;
  140. 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;
  141. 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;
  142. RenderingShaderContainer();
  143. virtual ~RenderingShaderContainer();
  144. };
  145. class RenderingShaderContainerFormat : public RenderingDeviceCommons {
  146. GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);
  147. public:
  148. virtual Ref<RenderingShaderContainer> create_container() const = 0;
  149. virtual ShaderLanguageVersion get_shader_language_version() const = 0;
  150. virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;
  151. };