Godot Engine – Multi-platform 2D and 3D game engine
Metal: ensure synchronisation fences carry across command buffers
Stuart Carnie81776ed6dfe3
diff --git a/drivers/metal/metal3_objects.cpp b/drivers/metal/metal3_objects.cppindex e8db8f6a78c..25818488719 100644--- a/drivers/metal/metal3_objects.cpp+++ b/drivers/metal/metal3_objects.cpp@@ -59,14 +59,12 @@using namespace MTL3;-MDCommandBuffer::MDCommandBuffer(MTL::CommandQueue *p_queue, ::RenderingDeviceDriverMetal *p_device_driver) :+MDCommandBuffer::MDCommandBuffer(MTL::CommandQueue *p_queue, ::RenderingDeviceDriverMetal *p_device_driver, MDLevelFences *p_level_fences) :_scratch(p_device_driver->get_allocator()), queue(p_queue) {device_driver = p_device_driver;type = MDCommandBufferStateType::None;sync_mode = device_driver->sync_mode;- if (sync_mode != RDM::SyncMode::HazardTracking) {- _create_level_fences(p_device_driver->get_device());- }+ _level_fences = p_level_fences;}void MDCommandBuffer::_encode_residency(MTL::RenderCommandEncoder *p_enc) {diff --git a/drivers/metal/metal3_objects.h b/drivers/metal/metal3_objects.hindex 22ec04be35a..7d7d4fad9d2 100644--- a/drivers/metal/metal3_objects.h+++ b/drivers/metal/metal3_objects.h@@ -643,7 +643,7 @@ public:void begin_label(const char *p_label_name, const Color &p_color) override;void end_label() override;- MDCommandBuffer(MTL::CommandQueue *p_queue, ::RenderingDeviceDriverMetal *p_device_driver);+ MDCommandBuffer(MTL::CommandQueue *p_queue, ::RenderingDeviceDriverMetal *p_device_driver, MDLevelFences *p_level_fences = nullptr);MDCommandBuffer() = default;};diff --git a/drivers/metal/metal_objects_shared.cpp b/drivers/metal/metal_objects_shared.cppindex 305c404b128..c2572adc92f 100644--- a/drivers/metal/metal_objects_shared.cpp+++ b/drivers/metal/metal_objects_shared.cpp@@ -428,79 +428,60 @@ MDCommandBufferBase::~MDCommandBufferBase() {release_resources();}-void MDCommandBufferBase::_create_level_fences(MTL::Device *p_device) {+void MDLevelFences::create(MTL::Device *p_device) {for (uint32_t i = 0; i < 2; i++) {- _fences[i] = NS::TransferPtr(p_device->newFence());+ _fences[i] = p_device->newFence();#ifdef DEV_ENABLED_fences[i]->setLabel(i == 0 ? MTLSTR("Level Fence 0") : MTLSTR("Level Fence 1"));#endif}}+MDLevelFences::~MDLevelFences() {+ for (MTL::Fence *fence : _fences) {+ if (fence) {+ fence->release();+ }+ }+}+MTL::Fence *MDCommandBufferBase::_fence_to_wait() {- if (!_fences[0]) {+ if (!_level_fences) {return nullptr;}- uint32_t i = _fence_wait_current_level ? (_fence_level & 1) : ((_fence_level + 1) & 1);+ bool current_level = _fence_wait_current_level;_fence_wait_current_level = false;#ifdef DEV_ENABLED_fence_wait_issued = true;#endif- return _fence_updated[i] ? _fences[i].get() : nullptr;+ return _level_fences->to_wait(current_level);}MTL::Fence *MDCommandBufferBase::_fence_to_update() {- if (!_fences[0]) {+ if (!_level_fences) {return nullptr;}#ifdef DEV_ENABLEDDEV_ASSERT(_fence_wait_issued);_fence_wait_issued = false;#endif- uint32_t i = _fence_level & 1;- _fence_updated[i] = true;- _fence_level_dirty = true;- return _fences[i].get();+ return _level_fences->to_update();}void MDCommandBufferBase::advance_sync_level() {// No encoder may straddle a level boundary: per-encoder fences are only// sufficient if every encoder in level k closed before level k + 1 opens.DEV_ASSERT(type == MDCommandBufferStateType::None);- // Must stay a no-op on a clean level: external_pass_fence() relies on the- // next level still waiting F_prev when nothing here updated F_cur.- if (!_fence_level_dirty) {- return;+ if (_level_fences) {+ _level_fences->advance();}- _fence_level_dirty = false;- _fence_level++;}MTL::Fence *MDCommandBufferBase::external_pass_fence() {- // _fence_level_dirty only reflects closed encoders.DEV_ASSERT(type == MDCommandBufferStateType::None);- if (!_fences[0]) {- return nullptr;- }// MetalFX exposes a single fence that it both waits for and updates, which// the alternating pair cannot express directly.- if (_fence_level_dirty) {- // An encoder in this level already waited F_prev and updated F_cur, so- // waiting F_cur transitively covers the prior level, and updating F_cur- // is what the next level waits on.-#ifdef DEV_ENABLED- _fence_wait_issued = true; // The pass waits F_cur itself.-#endif- return _fence_to_update();- }- // Nothing in this level has updated F_cur yet, so the pass joins the prior- // level from the fence's point of view: it waits F_prev and updates F_prev.- // Any later encoder in this level waits F_prev and so orders after the- // pass; if the level stays clean, the next level is still this one and- // waits F_prev too.- uint32_t i = (_fence_level + 1) & 1;- _fence_updated[i] = true;- return _fences[i].get();+ return _level_fences ? _level_fences->external_pass() : nullptr;}void MDCommandBufferBase::begin() {diff --git a/drivers/metal/metal_objects_shared.h b/drivers/metal/metal_objects_shared.hindex ddf5756d8e5..d280d6131b6 100644--- a/drivers/metal/metal_objects_shared.h+++ b/drivers/metal/metal_objects_shared.h@@ -574,6 +574,66 @@ struct RenderStateBase {BitField<DirtyFlag> dirty = DIRTY_NONE;};+/// Alternating fences between render graph levels.+/// Every encoder in level k waits on fences[(k + 1) & 1] and updates fences[k & 1].+/// Command buffers recorded in order share one, so levels stay ordered across them.+class MDLevelFences {+ MTL::Fence *_fences[2] = { nullptr, nullptr };+ uint32_t _level = 0;+ // true when a fence was updated in the prior level and therefore must be+ // waited in the current level.+ bool _updated[2] = { false, false };+ bool _level_dirty = false;++public:+ void create(MTL::Device *p_device);+ ~MDLevelFences();++ /// Returns the fence an encoder in the current level must wait on, or nullptr.+ /// Pass true for p_current_level when the encoder continues the one just closed.+ MTL::Fence *to_wait(bool p_current_level) const {+ uint32_t i = (p_current_level ? _level : _level + 1) & 1;+ return _updated[i] ? _fences[i] : nullptr;+ }++ /// Returns the fence an encoder in the current level must update.+ MTL::Fence *to_update() {+ uint32_t i = _level & 1;+ _updated[i] = true;+ _level_dirty = true;+ return _fences[i];+ }++ /// Closes the current level and opens the next one.+ void advance() {+ // Must stay a no-op on a clean level: external_pass() relies on the+ // next level still waiting F_prev when nothing here updated F_cur.+ if (_level_dirty) {+ _level_dirty = false;+ _level++;+ }+ }++ /// Returns the single fence an external pass must both wait on and update.+ /// No encoder may be open, as _level_dirty only reflects closed encoders.+ MTL::Fence *external_pass() {+ if (_level_dirty) {+ // An encoder in this level already waited F_prev and updated F_cur, so+ // waiting F_cur transitively covers the prior level, and updating F_cur+ // is what the next level waits on.+ return to_update();+ }+ // Nothing in this level has updated F_cur yet, so the pass joins the prior+ // level from the fence's point of view: it waits F_prev and updates F_prev.+ // Any later encoder in this level waits F_prev and so orders after the+ // pass; if the level stays clean, the next level is still this one and+ // waits F_prev too.+ uint32_t i = (_level + 1) & 1;+ _updated[i] = true;+ return _fences[i];+ }+};+/// Abstract base class for Metal command buffers, shared between MTL3 and MTL4 implementations.class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0), visionos(2.0)) MDCommandBufferBase {LocalVector<CFTypeRef> _retained_resources;@@ -604,17 +664,9 @@ protected:#pragma mark - Level Fences- // Alternating fences between render graph levels.- // Every encoder in level k waits on _fences[(k + 1) & 1]- // and updates _fences[k & 1]. _fence_level is used for k.- //- // Unused for hazard tracking- NS::SharedPtr<MTL::Fence> _fences[2];- uint32_t _fence_level = 0;- // true when a fence was updated in the prior level and therefore must be- // waited in the current level.- bool _fence_updated[2] = { false, false };- bool _fence_level_dirty = false;+ // nullptr for hazard tracking, and for command buffers that record+ // concurrently, which are ordered by semaphores.+ MDLevelFences *_level_fences = nullptr;// true when the next encoder continues the encoder just closed and must// therefore wait F_cur rather than F_prev. Metal does not order encoders, so// subpasses split across encoders require an explicit dependency.@@ -625,7 +677,6 @@ protected:bool _fence_wait_issued = false;#endif- void _create_level_fences(MTL::Device *p_device);MTL::Fence *_fence_to_wait();MTL::Fence *_fence_to_update();diff --git a/drivers/metal/rendering_device_driver_metal.cpp b/drivers/metal/rendering_device_driver_metal.cppindex ac0094ad1e0..daa6d75b37f 100644--- a/drivers/metal/rendering_device_driver_metal.cpp+++ b/drivers/metal/rendering_device_driver_metal.cpp@@ -3013,6 +3013,9 @@ Error RenderingDeviceDriverMetal::_initialize(uint32_t p_device_index, uint32_t// Must be resolved before the allocator is created, so use_heaps sees the// final sync_mode._resolve_sync_mode();+ if (sync_mode != HazardTracking) {+ main_level_fences.create(device);+ }// Must be created before _copy_queue_initialize(), which sources its scratch// buffer from the allocator. Barriers mode implies heap suballocation:diff --git a/drivers/metal/rendering_device_driver_metal.h b/drivers/metal/rendering_device_driver_metal.hindex 38ffbf1f0ce..8e66000b4b0 100644--- a/drivers/metal/rendering_device_driver_metal.h+++ b/drivers/metal/rendering_device_driver_metal.h@@ -162,6 +162,9 @@ protected:GODOT_CLANG_WARNING_POPSyncMode sync_mode = Barriers;+ // Shared by the command buffers of the graphics family, which record in order.+ // Created only when sync_mode uses fences.+ MDLevelFences main_level_fences;MTL::ResourceOptions base_hazard_tracking = MTL::ResourceHazardTrackingModeTracked;/// Textures imported via texture_create_from_extension. They are not heap-backed,diff --git a/drivers/metal/rendering_device_driver_metal3.cpp b/drivers/metal/rendering_device_driver_metal3.cppindex d3ec19fa4de..6be6744f595 100644--- a/drivers/metal/rendering_device_driver_metal3.cpp+++ b/drivers/metal/rendering_device_driver_metal3.cpp@@ -267,7 +267,8 @@ void RenderingDeviceDriverMetal::command_queue_free(CommandQueueID p_cmd_queue)RDD::CommandPoolID RenderingDeviceDriverMetal::command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) {DEV_ASSERT(p_cmd_buffer_type == COMMAND_BUFFER_TYPE_PRIMARY);- return CommandPoolID(reinterpret_cast<uint64_t>(device_queue.get()));+ // There is one queue, so the pool only records the family.+ return CommandPoolID(p_cmd_queue_family.id);}bool RenderingDeviceDriverMetal::command_pool_reset(CommandPoolID p_cmd_pool) {@@ -324,8 +325,10 @@ void RenderingDeviceDriverMetal::command_timestamp_write(CommandBufferID p_cmd_b#pragma mark - Command BuffersRDD::CommandBufferID RenderingDeviceDriverMetal::command_buffer_create(CommandPoolID p_cmd_pool) {- MTL::CommandQueue *queue = reinterpret_cast<MTL::CommandQueue *>(p_cmd_pool.id);- MDCommandBuffer *obj = memnew(MDCommandBuffer(queue, this));+ // Transfer workers record concurrently and are ordered by semaphores, so they use no level fences.+ bool use_level_fences = sync_mode != HazardTracking && p_cmd_pool.id == COMMAND_QUEUE_FAMILY_GRAPHICS_BIT;+ MDLevelFences *level_fences = use_level_fences ? &main_level_fences : nullptr;+ MDCommandBuffer *obj = memnew(MDCommandBuffer(device_queue.get(), this, level_fences));command_buffers.push_back(obj);return CommandBufferID(obj);}