From 4d71f65aea34a3c959281c61ac1085818ed6ebc6 Mon Sep 17 00:00:00 2001 From: assiduous Date: Fri, 5 Mar 2021 21:37:08 -0800 Subject: Reworked FenceGLImpl to use std::atomic --- .../GraphicsEngineOpenGL/include/FenceGLImpl.hpp | 8 +++++++- Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp | 24 ++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) (limited to 'Graphics/GraphicsEngineOpenGL') diff --git a/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp b/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp index 108a676d..bd2a78a4 100644 --- a/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/FenceGLImpl.hpp @@ -31,6 +31,8 @@ /// Declaration of Diligent::FenceGLImpl class #include +#include + #include "FenceGL.h" #include "RenderDeviceGL.h" #include "FenceBase.hpp" @@ -66,9 +68,13 @@ public: void Wait(Uint64 Value, bool FlushCommands); +private: + void UpdateFenceValue(Uint64 NewValue); + private: std::deque> m_PendingFences; - volatile Uint64 m_LastCompletedFenceValue = 0; + + std::atomic_uint64_t m_LastCompletedFenceValue{0}; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp index 78c840f1..ce23b5c9 100644 --- a/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/FenceGLImpl.cpp @@ -51,6 +51,15 @@ 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()) @@ -64,8 +73,7 @@ Uint64 FenceGLImpl::GetCompletedValue() ); if (res == GL_ALREADY_SIGNALED) { - if (val_fence.first > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = val_fence.first; + UpdateFenceValue(val_fence.first); m_PendingFences.pop_front(); } else @@ -74,7 +82,7 @@ Uint64 FenceGLImpl::GetCompletedValue() } } - return m_LastCompletedFenceValue; + return m_LastCompletedFenceValue.load(); } void FenceGLImpl::Wait(Uint64 Value, bool FlushCommands) @@ -89,17 +97,15 @@ void FenceGLImpl::Wait(Uint64 Value, bool FlushCommands) VERIFY_EXPR(res == GL_ALREADY_SIGNALED || res == GL_CONDITION_SATISFIED); (void)res; - if (val_fence.first > m_LastCompletedFenceValue) - m_LastCompletedFenceValue = val_fence.first; + UpdateFenceValue(val_fence.first); m_PendingFences.pop_front(); } } -void FenceGLImpl::Reset(Uint64 Value) +void FenceGLImpl::Reset(Uint64 NewValue) { - 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(NewValue >= m_LastCompletedFenceValue, "Resetting fence '", m_Desc.Name, "' to the value (", NewValue, ") that is smaller than the last completed value (", m_LastCompletedFenceValue, ")"); + UpdateFenceValue(NewValue); } } // namespace Diligent -- cgit v1.2.3