rendering_device_driver_metal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************/
  2. /* rendering_device_driver_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. #ifndef RENDERING_DEVICE_DRIVER_METAL_H
  31. #define RENDERING_DEVICE_DRIVER_METAL_H
  32. #import "metal_objects.h"
  33. #import "servers/rendering/rendering_device_driver.h"
  34. #import <Metal/Metal.h>
  35. #import <spirv.hpp>
  36. #import <variant>
  37. #ifdef DEBUG_ENABLED
  38. #ifndef _DEBUG
  39. #define _DEBUG
  40. #endif
  41. #endif
  42. class RenderingContextDriverMetal;
  43. class API_AVAILABLE(macos(11.0), ios(14.0)) RenderingDeviceDriverMetal : public RenderingDeviceDriver {
  44. friend struct ShaderCacheEntry;
  45. template <typename T>
  46. using Result = std::variant<T, Error>;
  47. #pragma mark - Generic
  48. RenderingContextDriverMetal *context_driver = nullptr;
  49. RenderingContextDriver::Device context_device;
  50. id<MTLDevice> device = nil;
  51. uint32_t version_major = 2;
  52. uint32_t version_minor = 0;
  53. MetalDeviceProperties *device_properties = nullptr;
  54. PixelFormats *pixel_formats = nullptr;
  55. std::unique_ptr<MDResourceCache> resource_cache;
  56. RDD::Capabilities capabilities;
  57. RDD::MultiviewCapabilities multiview_capabilities;
  58. id<MTLBinaryArchive> archive = nil;
  59. uint32_t archive_count = 0;
  60. id<MTLCommandQueue> device_queue = nil;
  61. id<MTLCaptureScope> device_scope = nil;
  62. String pipeline_cache_id;
  63. Error _create_device();
  64. Error _check_capabilities();
  65. #pragma mark - Shader Cache
  66. ShaderLoadStrategy _shader_load_strategy = ShaderLoadStrategy::DEFAULT;
  67. /**
  68. * The shader cache is a map of hashes of the Metal source to shader cache entries.
  69. *
  70. * To prevent unbounded growth of the cache, cache entries are automatically freed when
  71. * there are no more references to the MDLibrary associated with the cache entry.
  72. */
  73. HashMap<SHA256Digest, ShaderCacheEntry *, HashableHasher<SHA256Digest>> _shader_cache;
  74. void shader_cache_free_entry(const SHA256Digest &key);
  75. public:
  76. Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
  77. #pragma mark - Memory
  78. #pragma mark - Buffers
  79. public:
  80. virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) override final;
  81. virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
  82. virtual void buffer_free(BufferID p_buffer) override final;
  83. virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
  84. virtual uint8_t *buffer_map(BufferID p_buffer) override final;
  85. virtual void buffer_unmap(BufferID p_buffer) override final;
  86. #pragma mark - Texture
  87. private:
  88. // Returns true if the texture is a valid linear format.
  89. Result<bool> is_valid_linear(TextureFormat const &p_format) const;
  90. void _get_sub_resource(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) const;
  91. public:
  92. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
  93. virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil) override final;
  94. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
  95. virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;
  96. virtual void texture_free(TextureID p_texture) override final;
  97. virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
  98. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
  99. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) override final;
  100. virtual void texture_unmap(TextureID p_texture) override final;
  101. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
  102. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
  103. #pragma mark - Sampler
  104. public:
  105. virtual SamplerID sampler_create(const SamplerState &p_state) final override;
  106. virtual void sampler_free(SamplerID p_sampler) final override;
  107. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
  108. #pragma mark - Vertex Array
  109. private:
  110. public:
  111. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) override final;
  112. virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
  113. #pragma mark - Barriers
  114. virtual void command_pipeline_barrier(
  115. CommandBufferID p_cmd_buffer,
  116. BitField<PipelineStageBits> p_src_stages,
  117. BitField<PipelineStageBits> p_dst_stages,
  118. VectorView<MemoryBarrier> p_memory_barriers,
  119. VectorView<BufferBarrier> p_buffer_barriers,
  120. VectorView<TextureBarrier> p_texture_barriers) override final;
  121. #pragma mark - Fences
  122. private:
  123. struct Fence {
  124. dispatch_semaphore_t semaphore;
  125. Fence() :
  126. semaphore(dispatch_semaphore_create(0)) {}
  127. };
  128. public:
  129. virtual FenceID fence_create() override final;
  130. virtual Error fence_wait(FenceID p_fence) override final;
  131. virtual void fence_free(FenceID p_fence) override final;
  132. #pragma mark - Semaphores
  133. public:
  134. virtual SemaphoreID semaphore_create() override final;
  135. virtual void semaphore_free(SemaphoreID p_semaphore) override final;
  136. #pragma mark - Commands
  137. // ----- QUEUE FAMILY -----
  138. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;
  139. // ----- QUEUE -----
  140. public:
  141. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override final;
  142. virtual Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) override final;
  143. virtual void command_queue_free(CommandQueueID p_cmd_queue) override final;
  144. // ----- POOL -----
  145. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
  146. virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
  147. virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
  148. // ----- BUFFER -----
  149. private:
  150. // Used to maintain references.
  151. Vector<MDCommandBuffer *> command_buffers;
  152. public:
  153. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
  154. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
  155. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
  156. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
  157. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
  158. #pragma mark - Swapchain
  159. private:
  160. struct SwapChain {
  161. RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
  162. RenderPassID render_pass;
  163. RDD::DataFormat data_format = DATA_FORMAT_MAX;
  164. SwapChain() :
  165. render_pass(nullptr) {}
  166. };
  167. void _swap_chain_release(SwapChain *p_swap_chain);
  168. void _swap_chain_release_buffers(SwapChain *p_swap_chain);
  169. public:
  170. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;
  171. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;
  172. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;
  173. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;
  174. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;
  175. virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;
  176. virtual void swap_chain_free(SwapChainID p_swap_chain) override final;
  177. #pragma mark - Frame Buffer
  178. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
  179. virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
  180. #pragma mark - Shader
  181. private:
  182. // Serialization types need access to private state.
  183. friend struct ShaderStageData;
  184. friend struct SpecializationConstantData;
  185. friend struct UniformData;
  186. friend struct ShaderBinaryData;
  187. friend struct PushConstantData;
  188. private:
  189. /// Contains additional metadata about the shader.
  190. struct ShaderMeta {
  191. /// Indicates whether the shader uses multiview.
  192. bool has_multiview = false;
  193. };
  194. Error _reflect_spirv16(VectorView<ShaderStageSPIRVData> p_spirv, ShaderReflection &r_reflection, ShaderMeta &r_shader_meta);
  195. public:
  196. virtual String shader_get_binary_cache_key() override final;
  197. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) override final;
  198. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
  199. virtual void shader_free(ShaderID p_shader) override final;
  200. virtual void shader_destroy_modules(ShaderID p_shader) override final;
  201. #pragma mark - Uniform Set
  202. public:
  203. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
  204. virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
  205. #pragma mark - Commands
  206. virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  207. #pragma mark Transfer
  208. private:
  209. enum class CopySource {
  210. Buffer,
  211. Texture,
  212. };
  213. void _copy_texture_buffer(CommandBufferID p_cmd_buffer,
  214. CopySource p_source,
  215. TextureID p_texture,
  216. BufferID p_buffer,
  217. VectorView<BufferTextureCopyRegion> p_regions);
  218. public:
  219. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
  220. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
  221. virtual void command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) override final;
  222. virtual void command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) override final;
  223. virtual void command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) override final;
  224. virtual void command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) override final;
  225. virtual void command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_dst_buffer, VectorView<BufferTextureCopyRegion> p_regions) override final;
  226. #pragma mark Pipeline
  227. private:
  228. Result<id<MTLFunction>> _create_function(MDLibrary *p_library, NSString *p_name, VectorView<PipelineSpecializationConstant> &p_specialization_constants);
  229. public:
  230. virtual void pipeline_free(PipelineID p_pipeline_id) override final;
  231. // ----- BINDING -----
  232. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) override final;
  233. // ----- CACHE -----
  234. private:
  235. String _pipeline_get_cache_path() const;
  236. public:
  237. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
  238. virtual void pipeline_cache_free() override final;
  239. virtual size_t pipeline_cache_query_size() override final;
  240. virtual Vector<uint8_t> pipeline_cache_serialize() override final;
  241. #pragma mark Rendering
  242. // ----- SUBPASS -----
  243. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) override final;
  244. virtual void render_pass_free(RenderPassID p_render_pass) override final;
  245. // ----- COMMANDS -----
  246. public:
  247. virtual void command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_clear_values) override final;
  248. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
  249. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
  250. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
  251. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
  252. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
  253. // Binding.
  254. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  255. virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  256. virtual void command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) override final;
  257. // Drawing.
  258. virtual void command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) override final;
  259. virtual void command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) override final;
  260. virtual void command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  261. virtual void command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  262. virtual void command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  263. virtual void command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  264. // Buffer binding.
  265. virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets) override final;
  266. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
  267. // Dynamic state.
  268. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
  269. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
  270. // ----- PIPELINE -----
  271. virtual PipelineID render_pipeline_create(
  272. ShaderID p_shader,
  273. VertexFormatID p_vertex_format,
  274. RenderPrimitive p_render_primitive,
  275. PipelineRasterizationState p_rasterization_state,
  276. PipelineMultisampleState p_multisample_state,
  277. PipelineDepthStencilState p_depth_stencil_state,
  278. PipelineColorBlendState p_blend_state,
  279. VectorView<int32_t> p_color_attachments,
  280. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  281. RenderPassID p_render_pass,
  282. uint32_t p_render_subpass,
  283. VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  284. #pragma mark - Compute
  285. // ----- COMMANDS -----
  286. // Binding.
  287. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  288. virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  289. virtual void command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) override final;
  290. // Dispatching.
  291. virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) override final;
  292. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
  293. // ----- PIPELINE -----
  294. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  295. #pragma mark - Queries
  296. // ----- TIMESTAMP -----
  297. // Basic.
  298. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
  299. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
  300. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
  301. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
  302. // Commands.
  303. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
  304. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
  305. #pragma mark - Labels
  306. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
  307. virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
  308. #pragma mark - Debug
  309. virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
  310. #pragma mark - Submission
  311. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
  312. virtual void end_segment() override final;
  313. #pragma mark - Miscellaneous
  314. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
  315. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
  316. virtual uint64_t get_total_memory_used() override final;
  317. virtual uint64_t get_lazily_memory_used() override final;
  318. virtual uint64_t limit_get(Limit p_limit) override final;
  319. virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
  320. virtual bool has_feature(Features p_feature) override final;
  321. virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
  322. virtual String get_api_name() const override final { return "Metal"; }
  323. virtual String get_api_version() const override final;
  324. virtual String get_pipeline_cache_uuid() const override final;
  325. virtual const Capabilities &get_capabilities() const override final;
  326. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
  327. // Metal-specific.
  328. id<MTLDevice> get_device() const { return device; }
  329. PixelFormats &get_pixel_formats() const { return *pixel_formats; }
  330. MDResourceCache &get_resource_cache() const { return *resource_cache; }
  331. MetalDeviceProperties const &get_device_properties() const { return *device_properties; }
  332. _FORCE_INLINE_ uint32_t get_metal_buffer_index_for_vertex_attribute_binding(uint32_t p_binding) {
  333. return (device_properties->limits.maxPerStageBufferCount - 1) - p_binding;
  334. }
  335. size_t get_texel_buffer_alignment_for_format(RDD::DataFormat p_format) const;
  336. size_t get_texel_buffer_alignment_for_format(MTLPixelFormat p_format) const;
  337. /******************/
  338. RenderingDeviceDriverMetal(RenderingContextDriverMetal *p_context_driver);
  339. ~RenderingDeviceDriverMetal();
  340. };
  341. #endif // RENDERING_DEVICE_DRIVER_METAL_H