Path: blob/master/drivers/metal/rendering_device_driver_metal.h
21798 views
/**************************************************************************/1/* rendering_device_driver_metal.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "metal_device_profile.h"33#include "metal_objects_shared.h"3435#include "servers/rendering/rendering_device_driver.h"3637#include <Metal/Metal.hpp>38#include <variant>3940class RenderingShaderContainerFormatMetal;4142#ifdef DEBUG_ENABLED43#ifndef _DEBUG44#define _DEBUG45#endif46#endif4748class RenderingContextDriverMetal;4950namespace MTL3 {51class MDCommandBuffer;52}53namespace MTL4 {54class MDCommandBuffer;55}5657class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingDeviceDriverMetal : public RenderingDeviceDriver {58friend struct ShaderCacheEntry;59friend class MTL3::MDCommandBuffer;60friend class MTL4::MDCommandBuffer;61friend class MDUniformSet;6263template <typename T>64using Result = std::variant<T, Error>;6566#pragma mark - Generic6768protected:69RenderingContextDriverMetal *context_driver = nullptr;70RenderingContextDriver::Device context_device;71MTL::Device *device = nullptr;7273uint32_t _frame_count = 1;74/// frame_index is a cyclic counter derived from the current frame number modulo frame_count,75/// cycling through values from 0 to frame_count - 176uint32_t _frame_index = 0;77uint32_t _frames_drawn = 0;7879MetalDeviceProperties *device_properties = nullptr;80MetalDeviceProfile device_profile;81RenderingShaderContainerFormatMetal *shader_container_format = nullptr;82PixelFormats *pixel_formats = nullptr;83std::unique_ptr<MDResourceCache> resource_cache;8485RDD::Capabilities capabilities;86RDD::MultiviewCapabilities multiview_capabilities;87RDD::FragmentShadingRateCapabilities fsr_capabilities;88RDD::FragmentDensityMapCapabilities fdm_capabilities;8990NS::SharedPtr<MTL::BinaryArchive> archive;91uint32_t archive_count = 0;92// DEV: When true, attempting to create a pipeline will fail if it cannot use the archive.93bool archive_fail_on_miss = false;9495/// Resources to be added to the `main_residency_set`.96LocalVector<MTL::Resource *> _residency_add;97/// Resources to be removed from the `main_residency_set`.98LocalVector<MTL::Resource *> _residency_del;99100#pragma mark - Copy Queue101102Mutex copy_queue_mutex;103/// A command queue used for internal copy operations.104NS::SharedPtr<MTL::CommandQueue> copy_queue;105GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wunguarded-availability")106NS::SharedPtr<MTL::ResidencySet> copy_queue_rs;107GODOT_CLANG_WARNING_POP108// If this is not nullptr, there are pending copy operations.109NS::SharedPtr<MTL::CommandBuffer> copy_queue_command_buffer;110NS::SharedPtr<MTL::BlitCommandEncoder> copy_queue_blit_encoder;111NS::SharedPtr<MTL::Buffer> copy_queue_buffer;112NS::UInteger copy_queue_buffer_offset = 0;113114_FORCE_INLINE_ NS::UInteger _copy_queue_buffer_available() const {115return copy_queue_buffer.get()->length() - copy_queue_buffer_offset;116}117118/// Marks p_size bytes as consumed from the copy queue buffer, aligning the offset to 16 bytes.119_FORCE_INLINE_ void _copy_queue_buffer_consume(NS::UInteger p_size) {120NS::UInteger aligned_offset = round_up_to_alignment(copy_queue_buffer_offset, 16);121copy_queue_buffer_offset = aligned_offset + p_size;122}123124/// Returns a pointer to the current position in the copy queue buffer.125_FORCE_INLINE_ void *_copy_queue_buffer_ptr() const {126return static_cast<uint8_t *>(copy_queue_buffer.get()->contents()) + copy_queue_buffer_offset;127}128129_FORCE_INLINE_ MTL::CommandBuffer *_copy_queue_command_buffer() {130if (!copy_queue_command_buffer) {131DEV_ASSERT(!copy_queue_blit_encoder);132copy_queue_command_buffer = NS::RetainPtr(copy_queue.get()->commandBufferWithUnretainedReferences());133}134return copy_queue_command_buffer.get();135}136137_FORCE_INLINE_ MTL::BlitCommandEncoder *_copy_queue_blit_encoder() {138if (!copy_queue_blit_encoder) {139MTL::BlitCommandEncoder *enc = _copy_queue_command_buffer()->blitCommandEncoder();140copy_queue_blit_encoder = NS::RetainPtr(enc);141}142return copy_queue_blit_encoder.get();143}144145void _copy_queue_copy_to_buffer(Span<uint8_t> p_src_data, MTL::Buffer *p_dst_buffer, uint64_t p_dst_offset = 0);146void _copy_queue_flush();147Error _copy_queue_initialize();148149NS::SharedPtr<MTL::CaptureScope> device_scope;150151String pipeline_cache_id;152153virtual MTL::CommandQueue *get_command_queue() const = 0;154GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wunguarded-availability")155virtual void add_residency_set_to_main_queue(MTL::ResidencySet *p_set) = 0;156virtual void remove_residency_set_to_main_queue(MTL::ResidencySet *p_set) = 0;157NS::SharedPtr<MTL::ResidencySet> main_residency_set;158GODOT_CLANG_WARNING_POP159160bool use_barriers = false;161MTL::ResourceOptions base_hazard_tracking = MTL::ResourceHazardTrackingModeTracked;162163virtual Error _create_device();164virtual void _track_resource(MTL::Resource *p_resource);165virtual void _untrack_resource(MTL::Resource *p_resource);166void _check_capabilities();167Error _initialize(uint32_t p_device_index, uint32_t p_frame_count);168169#pragma mark - Shader Cache170171ShaderLoadStrategy _shader_load_strategy = ShaderLoadStrategy::DEFAULT;172173/**174* The shader cache is a map of hashes of the Metal source to shader cache entries.175*176* To prevent unbounded growth of the cache, cache entries are automatically freed when177* there are no more references to the MDLibrary associated with the cache entry.178*/179HashMap<SHA256Digest, ShaderCacheEntry *> _shader_cache;180void shader_cache_free_entry(const SHA256Digest &key);181182public:183virtual Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override = 0;184185#pragma mark - Memory186187#pragma mark - Buffers188189public:190struct BufferInfo {191NS::SharedPtr<MTL::Buffer> metal_buffer;192193_FORCE_INLINE_ bool is_dynamic() const { return _frame_idx != UINT32_MAX; }194_FORCE_INLINE_ uint32_t frame_index() const { return _frame_idx; }195_FORCE_INLINE_ void set_frame_index(uint32_t p_frame_index) { _frame_idx = p_frame_index; }196197protected:198// If dynamic buffer, then its range is [0; RenderingDeviceDriverMetal::frame_count)199// else it's UINT32_MAX.200uint32_t _frame_idx = UINT32_MAX;201};202203virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type, uint64_t p_frames_drawn) override final;204virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;205virtual void buffer_free(BufferID p_buffer) override final;206virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;207virtual uint8_t *buffer_map(BufferID p_buffer) override final;208virtual void buffer_unmap(BufferID p_buffer) override final;209virtual uint8_t *buffer_persistent_map_advance(BufferID p_buffer, uint64_t p_frames_drawn) override final;210virtual uint64_t buffer_get_dynamic_offsets(Span<BufferID> p_buffers) override final;211virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;212213#pragma mark - Texture214215private:216// Returns true if the texture is a valid linear format.217bool is_valid_linear(TextureFormat const &p_format) const;218219public:220virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;221virtual 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;222virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;223virtual 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;224virtual void texture_free(TextureID p_texture) override final;225virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;226virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;227virtual Vector<uint8_t> texture_get_data(TextureID p_texture, uint32_t p_layer) override final;228virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;229virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;230231#pragma mark - Sampler232233public:234virtual SamplerID sampler_create(const SamplerState &p_state) final override;235virtual void sampler_free(SamplerID p_sampler) final override;236virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;237238#pragma mark - Vertex Array239240private:241public:242virtual VertexFormatID vertex_format_create(Span<VertexAttribute> p_vertex_attribs, const VertexAttributeBindingsMap &p_vertex_bindings) override final;243virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;244245#pragma mark - Barriers246247public:248virtual void command_pipeline_barrier(249CommandBufferID p_cmd_buffer,250BitField<PipelineStageBits> p_src_stages,251BitField<PipelineStageBits> p_dst_stages,252VectorView<MemoryAccessBarrier> p_memory_barriers,253VectorView<BufferBarrier> p_buffer_barriers,254VectorView<TextureBarrier> p_texture_barriers,255VectorView<AccelerationStructureBarrier> p_acceleration_structure_barriers) override final;256257#pragma mark - Fences258259public:260virtual FenceID fence_create() override = 0;261virtual Error fence_wait(FenceID p_fence) override = 0;262virtual void fence_free(FenceID p_fence) override = 0;263264#pragma mark - Semaphores265266public:267virtual SemaphoreID semaphore_create() override = 0;268virtual void semaphore_free(SemaphoreID p_semaphore) override = 0;269270#pragma mark - Commands271// ----- QUEUE FAMILY -----272273virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;274275// ----- QUEUE -----276277public:278virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override = 0;279virtual 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 = 0;280virtual void command_queue_free(CommandQueueID p_cmd_queue) override = 0;281282// ----- POOL -----283284virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override = 0;285virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override = 0;286virtual void command_pool_free(CommandPoolID p_cmd_pool) override = 0;287288// ----- BUFFER -----289290public:291virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override = 0;292virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;293virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;294virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;295virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;296297#pragma mark - Swapchain298299protected:300struct SwapChain {301RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();302RenderPassID render_pass;303RDD::DataFormat data_format = DATA_FORMAT_MAX;304SwapChain() :305render_pass(nullptr) {}306};307308void _swap_chain_release(SwapChain *p_swap_chain);309void _swap_chain_release_buffers(SwapChain *p_swap_chain);310311public:312virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;313virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;314virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;315virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;316virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;317virtual ColorSpace swap_chain_get_color_space(SwapChainID p_swap_chain) override final;318virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;319virtual void swap_chain_free(SwapChainID p_swap_chain) override final;320321#pragma mark - Frame Buffer322323virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;324virtual void framebuffer_free(FramebufferID p_framebuffer) override final;325326#pragma mark - Shader327328private:329// Serialization types need access to private state.330331friend struct ShaderStageData;332friend struct SpecializationConstantData;333friend struct UniformData;334friend struct ShaderBinaryData;335friend struct PushConstantData;336337public:338virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) override final;339virtual void shader_free(ShaderID p_shader) override final;340virtual void shader_destroy_modules(ShaderID p_shader) override final;341virtual const RenderingShaderContainerFormat &get_shader_container_format() const override final;342343#pragma mark - Uniform Set344345public:346virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;347virtual void uniform_set_free(UniformSetID p_uniform_set) override final;348virtual 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;349350#pragma mark - Commands351352virtual 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;353354#pragma mark Transfer355356public:357virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;358virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;359360virtual 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;361virtual 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;362virtual 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;363virtual 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;364365virtual 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;366virtual 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;367368#pragma mark Pipeline369370private:371Result<NS::SharedPtr<MTL::Function>> _create_function(MDLibrary *p_library, NS::String *p_name, VectorView<PipelineSpecializationConstant> &p_specialization_constants);372373public:374virtual void pipeline_free(PipelineID p_pipeline_id) override final;375376// ----- BINDING -----377378virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) override final;379380// ----- CACHE -----381private:382String _pipeline_get_cache_path() const;383384public:385virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;386virtual void pipeline_cache_free() override final;387virtual size_t pipeline_cache_query_size() override final;388virtual Vector<uint8_t> pipeline_cache_serialize() override final;389390#pragma mark Rendering391392// ----- SUBPASS -----393394virtual 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;395virtual void render_pass_free(RenderPassID p_render_pass) override final;396397// ----- COMMANDS -----398399public:400virtual 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;401virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;402virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;403virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;404virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;405virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;406407// Binding.408virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;409virtual 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;410411// Drawing.412virtual 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;413virtual 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;414virtual 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;415virtual 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;416virtual 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;417virtual 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;418419// Buffer binding.420virtual 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;421virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;422423// Dynamic state.424virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;425virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;426427// ----- PIPELINE -----428429virtual PipelineID render_pipeline_create(430ShaderID p_shader,431VertexFormatID p_vertex_format,432RenderPrimitive p_render_primitive,433PipelineRasterizationState p_rasterization_state,434PipelineMultisampleState p_multisample_state,435PipelineDepthStencilState p_depth_stencil_state,436PipelineColorBlendState p_blend_state,437VectorView<int32_t> p_color_attachments,438BitField<PipelineDynamicStateFlags> p_dynamic_state,439RenderPassID p_render_pass,440uint32_t p_render_subpass,441VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;442443#pragma mark - Compute444445// ----- COMMANDS -----446447// Binding.448virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;449virtual 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;450451// Dispatching.452virtual 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;453virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;454455// ----- PIPELINE -----456457virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;458459#pragma mark - Raytracing460461// ----- ACCELERATION STRUCTURE -----462463virtual AccelerationStructureID blas_create(BufferID p_vertex_buffer, uint64_t p_vertex_offset, VertexFormatID p_vertex_format, uint32_t p_vertex_count, uint32_t p_position_attribute_location, BufferID p_index_buffer, IndexBufferFormat p_index_format, uint64_t p_index_offset_bytes, uint32_t p_index_count, BitField<AccelerationStructureGeometryBits> p_geometry_bits) override final;464virtual uint32_t tlas_instances_buffer_get_size_bytes(uint32_t p_instance_count) override final;465virtual void tlas_instances_buffer_fill(BufferID p_instances_buffer, VectorView<AccelerationStructureID> p_blases, VectorView<Transform3D> p_transforms) override final;466virtual AccelerationStructureID tlas_create(BufferID p_instances_buffer) override final;467virtual void acceleration_structure_free(AccelerationStructureID p_acceleration_structure) override final;468virtual uint32_t acceleration_structure_get_scratch_size_bytes(AccelerationStructureID p_acceleration_structure) override final;469470// ----- PIPELINE -----471472virtual RaytracingPipelineID raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;473virtual void raytracing_pipeline_free(RaytracingPipelineID p_pipeline) override final;474475// ----- COMMANDS -----476477virtual void command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure, BufferID p_scratch_buffer) override final;478virtual void command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) override final;479virtual void command_bind_raytracing_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;480virtual void command_trace_rays(CommandBufferID p_cmd_buffer, uint32_t p_width, uint32_t p_height) override final;481482#pragma mark - Queries483484// ----- TIMESTAMP -----485486// Basic.487virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;488virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;489virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;490virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;491492// Commands.493virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;494virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;495496#pragma mark - Labels497498virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;499virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;500501#pragma mark - Debug502503virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;504505#pragma mark - Submission506507virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;508virtual void end_segment() override final;509510#pragma mark - Miscellaneous511512virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;513virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;514virtual uint64_t get_total_memory_used() override final;515virtual uint64_t get_lazily_memory_used() override final;516virtual uint64_t limit_get(Limit p_limit) override final;517virtual uint64_t api_trait_get(ApiTrait p_trait) override final;518virtual bool has_feature(Features p_feature) override final;519virtual const MultiviewCapabilities &get_multiview_capabilities() override final;520virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() override final;521virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() override final;522virtual String get_api_version() const override final;523virtual String get_pipeline_cache_uuid() const override final;524virtual const Capabilities &get_capabilities() const override final;525virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;526527// Metal-specific.528MTL::Device *get_device() const { return device; }529PixelFormats &get_pixel_formats() const { return *pixel_formats; }530MDResourceCache &get_resource_cache() const { return *resource_cache; }531MetalDeviceProperties const &get_device_properties() const { return *device_properties; }532533_FORCE_INLINE_ uint32_t get_metal_buffer_index_for_vertex_attribute_binding(uint32_t p_binding) {534return (device_properties->limits.maxPerStageBufferCount - 1) - p_binding;535}536537size_t get_texel_buffer_alignment_for_format(RDD::DataFormat p_format) const;538size_t get_texel_buffer_alignment_for_format(MTL::PixelFormat p_format) const;539540_FORCE_INLINE_ uint32_t frame_count() const { return _frame_count; }541_FORCE_INLINE_ uint32_t frame_index() const { return _frame_index; }542_FORCE_INLINE_ uint32_t frames_drawn() const { return _frames_drawn; }543544/******************/545RenderingDeviceDriverMetal(RenderingContextDriverMetal *p_context_driver);546~RenderingDeviceDriverMetal();547};548549// Defined outside because we need to forward declare it in metal3_objects.h550struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalBufferDynamicInfo : public RenderingDeviceDriverMetal::BufferInfo {551uint64_t size_bytes; // Contains the real buffer size / frame_count.552uint32_t next_frame_index(uint32_t p_frame_count) {553// This is the next frame index to use for this buffer.554_frame_idx = (_frame_idx + 1u) % p_frame_count;555return _frame_idx;556}557#ifdef DEBUG_ENABLED558// For tracking that a persistent buffer isn't mapped twice in the same frame.559uint64_t last_frame_mapped = 0;560#endif561};562563564