rendering_shader_container_metal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**************************************************************************/
  2. /* rendering_shader_container_metal.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. #import "sha256_digest.h"
  32. #import "servers/rendering/rendering_device_driver.h"
  33. #import "servers/rendering/rendering_shader_container.h"
  34. constexpr uint32_t R32UI_ALIGNMENT_CONSTANT_ID = 65535;
  35. /// Metal buffer index for the view mask when rendering multi-view.
  36. const uint32_t VIEW_MASK_BUFFER_INDEX = 24;
  37. class RenderingShaderContainerFormatMetal;
  38. /// @brief A minimal structure that defines a device profile for Metal.
  39. ///
  40. /// This structure is used by the `RenderingShaderContainerMetal` class to
  41. /// determine options for compiling SPIR-V to Metal source. It currently only
  42. /// contains the minimum properties required to transform shaders from SPIR-V to Metal
  43. /// and potentially compile to a `.metallib`.
  44. struct MetalDeviceProfile {
  45. enum class Platform : uint32_t {
  46. macOS = 0,
  47. iOS = 1,
  48. };
  49. /// @brief The GPU family.
  50. enum class GPU : uint32_t {
  51. Apple1,
  52. Apple2,
  53. Apple3,
  54. Apple4,
  55. Apple5,
  56. Apple6,
  57. Apple7,
  58. Apple8,
  59. Apple9,
  60. };
  61. enum class ArgumentBuffersTier : uint32_t {
  62. Tier1 = 0,
  63. Tier2 = 1,
  64. };
  65. struct Features {
  66. uint32_t mslVersionMajor = 0;
  67. uint32_t mslVersionMinor = 0;
  68. ArgumentBuffersTier argument_buffers_tier = ArgumentBuffersTier::Tier1;
  69. bool simdPermute = false;
  70. };
  71. Platform platform = Platform::macOS;
  72. GPU gpu = GPU::Apple4;
  73. Features features;
  74. static const MetalDeviceProfile *get_profile(Platform p_platform, GPU p_gpu);
  75. MetalDeviceProfile() = default;
  76. private:
  77. static Mutex profiles_lock; ///< Mutex to protect access to the profiles map.
  78. static HashMap<uint32_t, MetalDeviceProfile> profiles;
  79. };
  80. class RenderingShaderContainerMetal : public RenderingShaderContainer {
  81. GDSOFTCLASS(RenderingShaderContainerMetal, RenderingShaderContainer);
  82. public:
  83. struct HeaderData {
  84. enum Flags : uint32_t {
  85. NONE = 0,
  86. NEEDS_VIEW_MASK_BUFFER = 1 << 0,
  87. USES_ARGUMENT_BUFFERS = 1 << 1,
  88. };
  89. /// The base profile that was used to generate this shader.
  90. MetalDeviceProfile profile;
  91. /// The Metal language version specified when compiling SPIR-V to MSL.
  92. /// Format is major * 10000 + minor * 100 + patch.
  93. uint32_t msl_version = UINT32_MAX;
  94. uint32_t flags = NONE;
  95. /// @brief Returns `true` if the shader is compiled with multi-view support.
  96. bool needs_view_mask_buffer() const {
  97. return flags & NEEDS_VIEW_MASK_BUFFER;
  98. }
  99. void set_needs_view_mask_buffer(bool p_value) {
  100. if (p_value) {
  101. flags |= NEEDS_VIEW_MASK_BUFFER;
  102. } else {
  103. flags &= ~NEEDS_VIEW_MASK_BUFFER;
  104. }
  105. }
  106. /// @brief Returns `true` if the shader was compiled with argument buffer support.
  107. bool uses_argument_buffers() const {
  108. return flags & USES_ARGUMENT_BUFFERS;
  109. }
  110. void set_uses_argument_buffers(bool p_value) {
  111. if (p_value) {
  112. flags |= USES_ARGUMENT_BUFFERS;
  113. } else {
  114. flags &= ~USES_ARGUMENT_BUFFERS;
  115. }
  116. }
  117. };
  118. struct StageData {
  119. uint32_t vertex_input_binding_mask = 0;
  120. uint32_t is_position_invariant = 0; ///< <c>true</c> if the position output is invariant
  121. uint32_t supports_fast_math = 0;
  122. SHA256Digest hash; ///< SHA 256 hash of the shader code
  123. uint32_t source_size = 0; ///< size of the source code in the returned bytes
  124. uint32_t library_size = 0; ///< size of the compiled library in the returned bytes, 0 if it is not compiled
  125. uint32_t push_constant_binding = UINT32_MAX; ///< Metal binding slot for the push constant data
  126. };
  127. struct BindingInfoData {
  128. uint32_t shader_stage = UINT32_MAX; ///< The shader stage this binding is used in, or UINT32_MAX if not used.
  129. uint32_t data_type = 0; // MTLDataTypeNone
  130. uint32_t index = 0;
  131. uint32_t access = 0; // MTLBindingAccessReadOnly
  132. uint32_t usage = 0; // MTLResourceUsage (none)
  133. uint32_t texture_type = 2; // MTLTextureType2D
  134. uint32_t image_format = 0;
  135. uint32_t array_length = 0;
  136. uint32_t is_multisampled = 0;
  137. };
  138. struct UniformData {
  139. /// Specifies the index into the `bindings` array for the shader stage.
  140. ///
  141. /// For example, a vertex and fragment shader use slots 0 and 1 of the bindings and bindings_secondary arrays.
  142. static constexpr uint32_t STAGE_INDEX[RenderingDeviceCommons::SHADER_STAGE_MAX] = {
  143. 0, // SHADER_STAGE_VERTEX
  144. 1, // SHADER_STAGE_FRAGMENT
  145. 0, // SHADER_STAGE_TESSELATION_CONTROL
  146. 1, // SHADER_STAGE_TESSELATION_EVALUATION
  147. 0, // SHADER_STAGE_COMPUTE
  148. };
  149. /// Specifies the stages the uniform data is
  150. /// used by the Metal shader.
  151. uint32_t active_stages = 0;
  152. /// The primary binding information for the uniform data.
  153. ///
  154. /// A maximum of two stages is expected for any given pipeline, such as a vertex and fragment, so
  155. /// the array size is fixed to 2.
  156. BindingInfoData bindings[2];
  157. /// The secondary binding information for the uniform data.
  158. ///
  159. /// This is typically a sampler for an image-sampler uniform
  160. BindingInfoData bindings_secondary[2];
  161. _FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RenderingDeviceCommons::ShaderStage p_stage) const {
  162. return STAGE_INDEX[p_stage];
  163. }
  164. _FORCE_INLINE_ BindingInfoData &get_binding_for_stage(RenderingDeviceCommons::ShaderStage p_stage) {
  165. BindingInfoData &info = bindings[get_index_for_stage(p_stage)];
  166. DEV_ASSERT(info.shader_stage == UINT32_MAX || info.shader_stage == p_stage); // make sure this uniform isn't used in the other stage
  167. info.shader_stage = p_stage;
  168. return info;
  169. }
  170. _FORCE_INLINE_ BindingInfoData &get_secondary_binding_for_stage(RenderingDeviceCommons::ShaderStage p_stage) {
  171. BindingInfoData &info = bindings_secondary[get_index_for_stage(p_stage)];
  172. DEV_ASSERT(info.shader_stage == UINT32_MAX || info.shader_stage == p_stage); // make sure this uniform isn't used in the other stage
  173. info.shader_stage = p_stage;
  174. return info;
  175. }
  176. };
  177. struct SpecializationData {
  178. uint32_t used_stages = 0;
  179. };
  180. HeaderData mtl_reflection_data; // compliment to reflection_data
  181. Vector<StageData> mtl_shaders; // compliment to shaders
  182. private:
  183. const MetalDeviceProfile *device_profile = nullptr;
  184. bool export_mode = false;
  185. Vector<UniformData> mtl_reflection_binding_set_uniforms_data; // compliment to reflection_binding_set_uniforms_data
  186. Vector<SpecializationData> mtl_reflection_specialization_data; // compliment to reflection_specialization_data
  187. Error compile_metal_source(const char *p_source, const StageData &p_stage_data, Vector<uint8_t> &r_binary_data);
  188. public:
  189. static constexpr uint32_t FORMAT_VERSION = 1;
  190. void set_export_mode(bool p_export_mode) { export_mode = p_export_mode; }
  191. void set_device_profile(const MetalDeviceProfile *p_device_profile) { device_profile = p_device_profile; }
  192. struct MetalShaderReflection {
  193. Vector<Vector<UniformData>> uniform_sets;
  194. Vector<SpecializationData> specialization_constants;
  195. };
  196. MetalShaderReflection get_metal_shader_reflection() const;
  197. protected:
  198. virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes) override;
  199. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) override;
  200. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
  201. virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) override;
  202. virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
  203. virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes) override;
  204. virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
  205. virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const override;
  206. virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
  207. virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
  208. virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
  209. virtual uint32_t _format() const override;
  210. virtual uint32_t _format_version() const override;
  211. virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) override;
  212. };
  213. class RenderingShaderContainerFormatMetal : public RenderingShaderContainerFormat {
  214. bool export_mode = false;
  215. const MetalDeviceProfile *device_profile = nullptr;
  216. public:
  217. virtual Ref<RenderingShaderContainer> create_container() const override;
  218. virtual ShaderLanguageVersion get_shader_language_version() const override;
  219. virtual ShaderSpirvVersion get_shader_spirv_version() const override;
  220. RenderingShaderContainerFormatMetal(const MetalDeviceProfile *p_device_profile, bool p_export = false);
  221. virtual ~RenderingShaderContainerFormatMetal() = default;
  222. };