From ae289a9d49ab469a5fc1962274b4b0fd7eb8dfb1 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 13 Mar 2021 18:57:23 -0800 Subject: Unified fence implementations in all backends --- Graphics/GraphicsEngine/include/FenceBase.hpp | 14 ++++++++++++++ Graphics/GraphicsEngineD3D11/include/FenceD3D11Impl.hpp | 6 +++--- Graphics/GraphicsEngineD3D11/src/FenceD3D11Impl.cpp | 14 +++++--------- Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp | 5 ----- Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp | 15 +++------------ Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp | 3 --- Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp | 9 --------- 7 files changed, 25 insertions(+), 41 deletions(-) (limited to 'Graphics') diff --git a/Graphics/GraphicsEngine/include/FenceBase.hpp b/Graphics/GraphicsEngine/include/FenceBase.hpp index 0bcaae36..e9dc0f9f 100644 --- a/Graphics/GraphicsEngine/include/FenceBase.hpp +++ b/Graphics/GraphicsEngine/include/FenceBase.hpp @@ -30,6 +30,8 @@ /// \file /// Implementation of the Diligent::FenceBase template class +#include + #include "DeviceObjectBase.hpp" #include "GraphicsTypes.h" #include "RefCntAutoPtr.hpp" @@ -66,6 +68,18 @@ public: } IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_Fence, TDeviceObjectBase) + +protected: + void 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. + } + } + + std::atomic_uint64_t m_LastCompletedFenceValue{0}; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/include/FenceD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/FenceD3D11Impl.hpp index 9385d3c4..49b5ea54 100644 --- a/Graphics/GraphicsEngineD3D11/include/FenceD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/FenceD3D11Impl.hpp @@ -31,10 +31,11 @@ /// Declaration of Diligent::FenceD3D11Impl class #include +#include + +#include "EngineD3D11ImplTraits.hpp" #include "FenceD3D11.h" -#include "RenderDeviceD3D11.h" #include "FenceBase.hpp" -#include "RenderDeviceD3D11Impl.hpp" namespace Diligent { @@ -81,7 +82,6 @@ private: {} }; std::deque m_PendingQueries; - volatile Uint64 m_LastCompletedFenceValue = 0; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/src/FenceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/FenceD3D11Impl.cpp index 28da90ff..2bb2bdf5 100644 --- a/Graphics/GraphicsEngineD3D11/src/FenceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/FenceD3D11Impl.cpp @@ -26,9 +26,9 @@ */ #include "pch.h" -#include #include "FenceD3D11Impl.hpp" +#include "RenderDeviceD3D11Impl.hpp" #include "EngineMemory.h" namespace Diligent @@ -55,8 +55,7 @@ Uint64 FenceD3D11Impl::GetCompletedValue() if (res == S_OK) { VERIFY_EXPR(Data == TRUE); - if (QueryData.Value > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = QueryData.Value; + UpdateLastCompletedFenceValue(QueryData.Value); m_PendingQueries.pop_front(); } else @@ -65,7 +64,7 @@ Uint64 FenceD3D11Impl::GetCompletedValue() } } - return m_LastCompletedFenceValue; + return m_LastCompletedFenceValue.load(); } void FenceD3D11Impl::Wait(Uint64 Value, bool FlushCommands) @@ -81,9 +80,7 @@ void FenceD3D11Impl::Wait(Uint64 Value, bool FlushCommands) std::this_thread::yield(); VERIFY_EXPR(Data == TRUE); - if (QueryData.Value > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = QueryData.Value; - + UpdateLastCompletedFenceValue(QueryData.Value); m_PendingQueries.pop_front(); } } @@ -91,8 +88,7 @@ void FenceD3D11Impl::Wait(Uint64 Value, bool FlushCommands) void FenceD3D11Impl::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; + UpdateLastCompletedFenceValue(Value); } } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp b/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp index d0095402..727886cb 100644 --- a/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp @@ -67,13 +67,8 @@ public: void Wait(Uint64 Value, bool FlushCommands); -private: - void UpdateFenceValue(Uint64 NewValue); - private: std::deque> m_PendingFences; - - std::atomic_uint64_t m_LastCompletedFenceValue{0}; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp index fe88669e..677e1db9 100644 --- a/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp @@ -52,15 +52,6 @@ FenceGLImpl::~FenceGLImpl() { } -void FenceGLImpl::UpdateFenceValue(Uint64 NewValue) -{ - auto CurrValue = m_LastCompletedFenceValue.load(); - while (!m_LastCompletedFenceValue.compare_exchange_strong(CurrValue, std::max(CurrValue, NewValue))) - { - // If exchange fails, CurrValue will hold the actual value of m_LastCompletedFenceValue - } -} - Uint64 FenceGLImpl::GetCompletedValue() { while (!m_PendingFences.empty()) @@ -74,7 +65,7 @@ Uint64 FenceGLImpl::GetCompletedValue() ); if (res == GL_ALREADY_SIGNALED) { - UpdateFenceValue(val_fence.first); + UpdateLastCompletedFenceValue(val_fence.first); m_PendingFences.pop_front(); } else @@ -98,7 +89,7 @@ void FenceGLImpl::Wait(Uint64 Value, bool FlushCommands) VERIFY_EXPR(res == GL_ALREADY_SIGNALED || res == GL_CONDITION_SATISFIED); (void)res; - UpdateFenceValue(val_fence.first); + UpdateLastCompletedFenceValue(val_fence.first); m_PendingFences.pop_front(); } } @@ -106,7 +97,7 @@ void FenceGLImpl::Wait(Uint64 Value, bool FlushCommands) void FenceGLImpl::Reset(Uint64 NewValue) { DEV_CHECK_ERR(NewValue >= m_LastCompletedFenceValue, "Resetting fence '", m_Desc.Name, "' to the value (", NewValue, ") that is smaller than the last completed value (", m_LastCompletedFenceValue, ")"); - UpdateFenceValue(NewValue); + UpdateLastCompletedFenceValue(NewValue); } } // namespace Diligent diff --git a/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp b/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp index b720d604..21b2a7b9 100644 --- a/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp +++ b/Graphics/GraphicsEngineVulkan/include/FenceVkImpl.hpp @@ -76,11 +76,8 @@ public: void Wait(Uint64 Value); private: - inline void UpdateLastCompletedFenceValue(uint64_t NewValue); - VulkanUtilities::VulkanFencePool m_FencePool; std::deque> m_PendingFences; - std::atomic_uint64_t m_LastCompletedFenceValue{0}; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp index 9170516a..656e3333 100644 --- a/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/FenceVkImpl.cpp @@ -64,15 +64,6 @@ 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(); -- cgit v1.2.3