diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2021-02-19 00:07:52 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2021-03-19 00:38:08 +0000 |
| commit | a49615e192d75a5be73c71c065b44b61bfb75493 (patch) | |
| tree | 7d0eb67e58dddd6f00f335434b4a7eebdd2f84c2 /Graphics/GraphicsEngineVulkan | |
| parent | Reworked CommandQueueD3D12Impl and CommandQueueVkImpl to use atomics (diff) | |
| download | DiligentCore-a49615e192d75a5be73c71c065b44b61bfb75493.tar.gz DiligentCore-a49615e192d75a5be73c71c065b44b61bfb75493.zip | |
Reworked FenceVkImpl to use atomics
Diffstat (limited to 'Graphics/GraphicsEngineVulkan')
| -rw-r--r-- | Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp | 6 | ||||
| -rw-r--r-- | Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp | 22 |
2 files changed, 19 insertions, 9 deletions
diff --git a/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp b/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp index ab8da006..303fe6cf 100644 --- a/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp +++ b/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp @@ -31,6 +31,8 @@ /// Declaration of Diligent::FenceVkImpl class #include <deque> +#include <atomic> + #include "FenceVk.h" #include "FenceBase.hpp" #include "VulkanUtilities/VulkanFencePool.hpp" @@ -76,9 +78,11 @@ public: void Wait(Uint64 Value); private: + inline void UpdateLastCompletedFenceValue(uint64_t NewValue); + VulkanUtilities::VulkanFencePool m_FencePool; std::deque<std::pair<Uint64, VulkanUtilities::FenceWrapper>> m_PendingFences; - volatile Uint64 m_LastCompletedFenceValue = 0; + std::atomic_uint64_t m_LastCompletedFenceValue{0}; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp index 0d109b38..fa4c4536 100644 --- a/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp @@ -64,6 +64,15 @@ FenceVkImpl::~FenceVkImpl() } } +void FenceVkImpl::UpdateLastCompletedFenceValue(uint64_t NewValue) +{ + auto LastCompletedValue = m_LastCompletedFenceValue.load(); + while (!m_LastCompletedFenceValue.compare_exchange_strong(LastCompletedValue, std::max(LastCompletedValue, NewValue))) + { + // If exchange fails, LastCompletedValue will hold the actual value of m_LastCompletedFenceValue. + } +} + Uint64 FenceVkImpl::GetCompletedValue() { const auto& LogicalDevice = m_pDevice->GetLogicalDevice(); @@ -74,8 +83,7 @@ Uint64 FenceVkImpl::GetCompletedValue() auto status = LogicalDevice.GetFenceStatus(Value_Fence.second); if (status == VK_SUCCESS) { - if (Value_Fence.first > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = Value_Fence.first; + UpdateLastCompletedFenceValue(Value_Fence.first); m_FencePool.DisposeFence(std::move(Value_Fence.second)); m_PendingFences.pop_front(); } @@ -85,14 +93,13 @@ Uint64 FenceVkImpl::GetCompletedValue() } } - return m_LastCompletedFenceValue; + return m_LastCompletedFenceValue.load(); } void FenceVkImpl::Reset(Uint64 Value) { - DEV_CHECK_ERR(Value >= m_LastCompletedFenceValue, "Resetting fence '", m_Desc.Name, "' to the value (", Value, ") that is smaller than the last completed value (", m_LastCompletedFenceValue, ")"); - if (Value > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = Value; + DEV_CHECK_ERR(Value >= m_LastCompletedFenceValue.load(), "Resetting fence '", m_Desc.Name, "' to the value (", Value, ") that is smaller than the last completed value (", m_LastCompletedFenceValue, ")"); + UpdateLastCompletedFenceValue(Value); } @@ -115,8 +122,7 @@ void FenceVkImpl::Wait(Uint64 Value) DEV_CHECK_ERR(status == VK_SUCCESS, "All pending fences must now be complete!"); (void)status; - if (val_fence.first > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = val_fence.first; + UpdateLastCompletedFenceValue(val_fence.first); m_FencePool.DisposeFence(std::move(val_fence.second)); m_PendingFences.pop_front(); |
