Debian Codegraph @

drivers/metal/rendering_device_driver_metal.h

raw symbols SCIP
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
31 #pragma once
32
33 #import "metal_device_profile.h"
34 #import "metal_objects.h"
35
36 #include "servers/rendering/rendering_device_driver.h"
37
38 #import <Metal/Metal.h>
39 #import <variant>
40
41 class RenderingShaderContainerFormatMetal;
42
43 #ifdef DEBUG_ENABLED
44 #ifndef _DEBUG
45 #define _DEBUG
46 #endif
47 #endif
48
49 class RenderingContextDriverMetal;
50
51 class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingDeviceDriverMetal : public RenderingDeviceDriver {
52 friend struct ShaderCacheEntry;
53 friend class MDCommandBuffer;
54
55 template <typename T>
56 using Result = std::variant<T, Error>;
57
58 #pragma mark - Generic
59
60 RenderingContextDriverMetal *context_driver = nullptr;
61 RenderingContextDriver::Device context_device;
62 id<MTLDevice> device = nil;
63
64 uint32_t _frame_count = 1;
65 /// frame_index is a cyclic counter derived from the current frame number modulo frame_count,
66 /// cycling through values from 0 to frame_count - 1
67 uint32_t _frame_index = 0;
68 uint32_t _frames_drawn = 0;
69
70 MetalDeviceProperties *device_properties = nullptr;
71 MetalDeviceProfile device_profile;
72 RenderingShaderContainerFormatMetal *shader_container_format = nullptr;
73 PixelFormats *pixel_formats = nullptr;
74 std::unique_ptr<MDResourceCache> resource_cache;
75
76 RDD::Capabilities capabilities;
77 RDD::MultiviewCapabilities multiview_capabilities;
78 RDD::FragmentShadingRateCapabilities fsr_capabilities;
79 RDD::FragmentDensityMapCapabilities fdm_capabilities;
80
81 id<MTLBinaryArchive> archive = nil;
82 uint32_t archive_count = 0;
83
84 id<MTLCommandQueue> device_queue = nil;
85 id<MTLCaptureScope> device_scope = nil;
86
87 String pipeline_cache_id;
88
89 Error _create_device();
90 void _check_capabilities();
91
92 #pragma mark - Shader Cache
93
94 ShaderLoadStrategy _shader_load_strategy = ShaderLoadStrategy::DEFAULT;
95
96 /**
97 * The shader cache is a map of hashes of the Metal source to shader cache entries.
98 *
99 * To prevent unbounded growth of the cache, cache entries are automatically freed when
100 * there are no more references to the MDLibrary associated with the cache entry.
101 */
102 HashMap<SHA256Digest, ShaderCacheEntry *> _shader_cache;
103 void shader_cache_free_entry(const SHA256Digest &key);
104
105 public:
106 Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
107
108 #pragma mark - Memory
109
110 #pragma mark - Buffers
111
112 public:
113 struct BufferInfo {
114 id<MTLBuffer> metal_buffer;
115
116 _FORCE_INLINE_ bool is_dynamic() const { return _frame_idx != UINT32_MAX; }
117 _FORCE_INLINE_ uint32_t frame_index() const { return _frame_idx; }
118 _FORCE_INLINE_ void set_frame_index(uint32_t p_frame_index) { _frame_idx = p_frame_index; }
119
120 protected:
121 // If dynamic buffer, then its range is [0; RenderingDeviceDriverMetal::frame_count)
122 // else it's UINT32_MAX.
123 uint32_t _frame_idx = UINT32_MAX;
124 };
125
126 virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type, uint64_t p_frames_drawn) override final;
127 virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
128 virtual void buffer_free(BufferID p_buffer) override final;
129 virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
130 virtual uint8_t *buffer_map(BufferID p_buffer) override final;
131 virtual void buffer_unmap(BufferID p_buffer) override final;
132 virtual uint8_t *buffer_persistent_map_advance(BufferID p_buffer, uint64_t p_frames_drawn) override final;
133 virtual uint64_t buffer_get_dynamic_offsets(Span<BufferID> p_buffers) override final;
134 virtual void buffer_flush(BufferID p_buffer) override final;
135 virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;
136
137 #pragma mark - Texture
138
139 private:
140 // Returns true if the texture is a valid linear format.
141 bool is_valid_linear(TextureFormat const &p_format) const;
142
143 public:
144 virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
145 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, uint32_t p_mipmaps) override final;
146 virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
147 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;
148 virtual void texture_free(TextureID p_texture) override final;
149 virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
150 virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
151 virtual Vector<uint8_t> texture_get_data(TextureID p_texture, uint32_t p_layer) override final;
152 virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
153 virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
154
155 #pragma mark - Sampler
156
157 public:
158 virtual SamplerID sampler_create(const SamplerState &p_state) final override;
159 virtual void sampler_free(SamplerID p_sampler) final override;
160 virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
161
162 #pragma mark - Vertex Array
163
164 private:
165 public:
166 virtual VertexFormatID vertex_format_create(Span<VertexAttribute> p_vertex_attribs, const VertexAttributeBindingsMap &p_vertex_bindings) override final;
167 virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
168
169 #pragma mark - Barriers
170
171 virtual void command_pipeline_barrier(
172 CommandBufferID p_cmd_buffer,
173 BitField<PipelineStageBits> p_src_stages,
174 BitField<PipelineStageBits> p_dst_stages,
175 VectorView<MemoryAccessBarrier> p_memory_barriers,
176 VectorView<BufferBarrier> p_buffer_barriers,
177 VectorView<TextureBarrier> p_texture_barriers) override final;
178
179 #pragma mark - Fences
180
181 private:
182 struct Fence {
183 virtual void signal(id<MTLCommandBuffer> p_cmd_buffer) = 0;
184 virtual Error wait(uint32_t p_timeout_ms) = 0;
185 virtual ~Fence() = default;
186 };
187
188 struct FenceEvent : public Fence {
189 id<MTLSharedEvent> event;
190 uint64_t value;
191 FenceEvent(id<MTLSharedEvent> p_event) :
192 event(p_event),
193 value(0) {}
194
195 virtual void signal(id<MTLCommandBuffer> p_cb) override {
196 if (p_cb) {
197 value++;
198 [p_cb encodeSignalEvent:event value:value];
199 }
200 }
201
202 virtual Error wait(uint32_t p_timeout_ms) override {
203 GODOT_CLANG_WARNING_PUSH
204 GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wunguarded-availability")
205 BOOL signaled = [event waitUntilSignaledValue:value timeoutMS:p_timeout_ms];
206 GODOT_CLANG_WARNING_POP
207 if (!signaled) {
208 #ifdef DEBUG_ENABLED
209 ERR_PRINT("timeout waiting for fence");
210 #endif
211 return ERR_TIMEOUT;
212 }
213
214 return OK;
215 }
216 };
217
218 struct FenceSemaphore : public Fence {
219 dispatch_semaphore_t semaphore;
220 FenceSemaphore() :
221 semaphore(dispatch_semaphore_create(0)) {}
222
223 virtual void signal(id<MTLCommandBuffer> p_cb) override {
224 if (p_cb) {
225 [p_cb addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
226 dispatch_semaphore_signal(semaphore);
227 }];
228 } else {
229 dispatch_semaphore_signal(semaphore);
230 }
231 }
232
233 virtual Error wait(uint32_t p_timeout_ms) override {
234 dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(p_timeout_ms) * 1000000);
235 long result = dispatch_semaphore_wait(semaphore, timeout);
236 if (result != 0) {
237 return ERR_TIMEOUT;
238 }
239 return OK;
240 }
241 };
242
243 public:
244 virtual FenceID fence_create() override final;
245 virtual Error fence_wait(FenceID p_fence) override final;
246 virtual void fence_free(FenceID p_fence) override final;
247
248 #pragma mark - Semaphores
249
250 public:
251 virtual SemaphoreID semaphore_create() override final;
252 virtual void semaphore_free(SemaphoreID p_semaphore) override final;
253
254 #pragma mark - Commands
255 // ----- QUEUE FAMILY -----
256
257 virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;
258
259 // ----- QUEUE -----
260 public:
261 virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override final;
262 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;
263 virtual void command_queue_free(CommandQueueID p_cmd_queue) override final;
264
265 // ----- POOL -----
266
267 virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
268 virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
269 virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
270
271 // ----- BUFFER -----
272
273 private:
274 // Used to maintain references.
275 Vector<MDCommandBuffer *> command_buffers;
276
277 public:
278 virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
279 virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
280 virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
281 virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
282 virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
283
284 #pragma mark - Swapchain
285
286 private:
287 struct SwapChain {
288 RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
289 RenderPassID render_pass;
290 RDD::DataFormat data_format = DATA_FORMAT_MAX;
291 SwapChain() :
292 render_pass(nullptr) {}
293 };
294
295 void _swap_chain_release(SwapChain *p_swap_chain);
296 void _swap_chain_release_buffers(SwapChain *p_swap_chain);
297
298 public:
299 virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;
300 virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;
301 virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;
302 virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;
303 virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;
304 virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;
305 virtual void swap_chain_free(SwapChainID p_swap_chain) override final;
306
307 #pragma mark - Frame Buffer
308
309 virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
310 virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
311
312 #pragma mark - Shader
313
314 private:
315 // Serialization types need access to private state.
316
317 friend struct ShaderStageData;
318 friend struct SpecializationConstantData;
319 friend struct UniformData;
320 friend struct ShaderBinaryData;
321 friend struct PushConstantData;
322
323 public:
324 virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
325 virtual void shader_free(ShaderID p_shader) override final;
326 virtual void shader_destroy_modules(ShaderID p_shader) override final;
327 virtual const RenderingShaderContainerFormat &get_shader_container_format() const override final;
328
329 #pragma mark - Uniform Set
330
331 public:
332 virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
333 virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
334 virtual uint32_t uniform_sets_get_dynamic_offsets(VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) const override final;
335
336 #pragma mark - Commands
337
338 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;
339
340 #pragma mark Transfer
341
342 public:
343 virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
344 virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
345
346 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;
347 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;
348 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;
349 virtual void command_clear_depth_stencil_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, float p_depth, uint8_t p_stencil, const TextureSubresourceRange &p_subresources) override final;
350
351 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;
352 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;
353
354 #pragma mark Pipeline
355
356 private:
357 Result<id<MTLFunction>> _create_function(MDLibrary *p_library, NSString *p_name, VectorView<PipelineSpecializationConstant> &p_specialization_constants);
358
359 public:
360 virtual void pipeline_free(PipelineID p_pipeline_id) override final;
361
362 // ----- BINDING -----
363
364 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;
365
366 // ----- CACHE -----
367 private:
368 String _pipeline_get_cache_path() const;
369
370 public:
371 virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
372 virtual void pipeline_cache_free() override final;
373 virtual size_t pipeline_cache_query_size() override final;
374 virtual Vector<uint8_t> pipeline_cache_serialize() override final;
375
376 #pragma mark Rendering
377
378 // ----- SUBPASS -----
379
380 virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count, AttachmentReference p_fragment_density_map_attachment) override final;
381 virtual void render_pass_free(RenderPassID p_render_pass) override final;
382
383 // ----- COMMANDS -----
384
385 public:
386 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;
387 virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
388 virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
389 virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
390 virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
391 virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
392
393 // Binding.
394 virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
395 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, uint32_t p_dynamic_offsets) override final;
396
397 // Drawing.
398 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;
399 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;
400 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;
401 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;
402 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;
403 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;
404
405 // Buffer binding.
406 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, uint64_t p_dynamic_offsets) override final;
407 virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
408
409 // Dynamic state.
410 virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
411 virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
412
413 // ----- PIPELINE -----
414
415 virtual PipelineID render_pipeline_create(
416 ShaderID p_shader,
417 VertexFormatID p_vertex_format,
418 RenderPrimitive p_render_primitive,
419 PipelineRasterizationState p_rasterization_state,
420 PipelineMultisampleState p_multisample_state,
421 PipelineDepthStencilState p_depth_stencil_state,
422 PipelineColorBlendState p_blend_state,
423 VectorView<int32_t> p_color_attachments,
424 BitField<PipelineDynamicStateFlags> p_dynamic_state,
425 RenderPassID p_render_pass,
426 uint32_t p_render_subpass,
427 VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
428
429 #pragma mark - Compute
430
431 // ----- COMMANDS -----
432
433 // Binding.
434 virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
435 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, uint32_t p_dynamic_offsets) override final;
436
437 // Dispatching.
438 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;
439 virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
440
441 // ----- PIPELINE -----
442
443 virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
444
445 #pragma mark - Queries
446
447 // ----- TIMESTAMP -----
448
449 // Basic.
450 virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
451 virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
452 virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
453 virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
454
455 // Commands.
456 virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
457 virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
458
459 #pragma mark - Labels
460
461 virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
462 virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
463
464 #pragma mark - Debug
465
466 virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
467
468 #pragma mark - Submission
469
470 virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
471 virtual void end_segment() override final;
472
473 #pragma mark - Miscellaneous
474
475 virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
476 virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
477 virtual uint64_t get_total_memory_used() override final;
478 virtual uint64_t get_lazily_memory_used() override final;
479 virtual uint64_t limit_get(Limit p_limit) override final;
480 virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
481 virtual bool has_feature(Features p_feature) override final;
482 virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
483 virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() override final;
484 virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() override final;
485 virtual String get_api_name() const override final { return "Metal"; }
486 virtual String get_api_version() const override final;
487 virtual String get_pipeline_cache_uuid() const override final;
488 virtual const Capabilities &get_capabilities() const override final;
489 virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
490
491 // Metal-specific.
492 id<MTLDevice> get_device() const { return device; }
493 PixelFormats &get_pixel_formats() const { return *pixel_formats; }
494 MDResourceCache &get_resource_cache() const { return *resource_cache; }
495 MetalDeviceProperties const &get_device_properties() const { return *device_properties; }
496
497 _FORCE_INLINE_ uint32_t get_metal_buffer_index_for_vertex_attribute_binding(uint32_t p_binding) {
498 return (device_properties->limits.maxPerStageBufferCount - 1) - p_binding;
499 }
500
501 size_t get_texel_buffer_alignment_for_format(RDD::DataFormat p_format) const;
502 size_t get_texel_buffer_alignment_for_format(MTLPixelFormat p_format) const;
503
504 _FORCE_INLINE_ uint32_t frame_count() const { return _frame_count; }
505 _FORCE_INLINE_ uint32_t frame_index() const { return _frame_index; }
506 _FORCE_INLINE_ uint32_t frames_drawn() const { return _frames_drawn; }
507
508 /******************/
509 RenderingDeviceDriverMetal(RenderingContextDriverMetal *p_context_driver);
510 ~RenderingDeviceDriverMetal();
511 };
512
513 // Defined outside because we need to forward declare it in metal_objects.h
514 struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalBufferDynamicInfo : public RenderingDeviceDriverMetal::BufferInfo {
515 uint64_t size_bytes; // Contains the real buffer size / frame_count.
516 uint32_t next_frame_index(uint32_t p_frame_count) {
517 // This is the next frame index to use for this buffer.
518 _frame_idx = (_frame_idx + 1u) % p_frame_count;
519 return _frame_idx;
520 }
521 #ifdef DEBUG_ENABLED
522 // For tracking that a persistent buffer isn't mapped twice in the same frame.
523 uint64_t last_frame_mapped = 0;
524 #endif
525 };