Skip to content

Commit

Permalink
IDeviceContextGL: OpenGL interop improvements.
Browse files Browse the repository at this point in the history
Add `PurgeCurrentContextCaches()` method;
Remove `PurgeCaches` parameter of  `UpdateCurrentGLContext()`.
  • Loading branch information
WangHoi authored and TheMostDiligent committed Oct 20, 2024
1 parent 5813d15 commit 7816066
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ class DeviceContextGLImpl final : public DeviceContextBase<EngineGLImplTraits>
virtual void DILIGENT_CALL_TYPE BindSparseResourceMemory(const BindSparseResourceMemoryAttribs& Attribs) override final;

/// Implementation of IDeviceContextGL::UpdateCurrentGLContext().
virtual bool DILIGENT_CALL_TYPE UpdateCurrentGLContext(bool PurgeCaches) override final;
virtual bool DILIGENT_CALL_TYPE UpdateCurrentGLContext() override final;

/// Implementation of IDeviceContextGL::PurgeCurrentContextCaches().
virtual void DILIGENT_CALL_TYPE PurgeCurrentContextCaches() override final;

GLContextState& GetContextState() { return m_ContextState; }

Expand Down
2 changes: 2 additions & 0 deletions Graphics/GraphicsEngineOpenGL/include/FBOCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class FBOCache

void OnReleaseTexture(ITexture* pTexture);

void Clear();

private:
// This structure is used as the key to find FBO
struct FBOCacheKey
Expand Down
3 changes: 3 additions & 0 deletions Graphics/GraphicsEngineOpenGL/include/VAOCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class VAOCache
void OnDestroyBuffer(const BufferGLImpl& Buffer);
void OnDestroyPSO(const PipelineStateGLImpl& PSO);

// Clears all cached objects
void Clear();

private:
// This structure is used as the key to find VAO
struct VAOHashKey
Expand Down
15 changes: 8 additions & 7 deletions Graphics/GraphicsEngineOpenGL/interface/DeviceContextGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ DILIGENT_BEGIN_INTERFACE(IDeviceContextGL, IDeviceContext)
/// other command to let the engine update active context every time when control flow
/// is passed over from the main application.
///
/// \param[in] PurgeCaches - Whether to purge context caches (e.g. VAO, FBO) before
/// updating the active context. An application should set this
/// flag to true if the last active context will not be used anymore
/// (e.g. it was destroyed) to avoid memory leaks.
///
/// \return false if there is no active GL context, and true otherwise.
VIRTUAL bool METHOD(UpdateCurrentGLContext)(THIS_
bool PurgeCaches DEFAULT_INITIALIZER(false)) PURE;
VIRTUAL bool METHOD(UpdateCurrentGLContext)(THIS_) PURE;

/// Purge current context caches (e.g. VAO, FBO).

/// If an application uses multiple GL contexts, this method must be called
/// before the current context is about to be released,
/// to let the engine cleanup internal OpenGL object caches.
VIRTUAL void METHOD(PurgeCurrentContextCaches)(THIS_) PURE;

/// Sets the swap in the device context. The swap chain is used by the device context
/// to obtain the default FBO handle.
Expand Down
14 changes: 8 additions & 6 deletions Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,13 +1653,8 @@ void DeviceContextGLImpl::EndQuery(IQuery* pQuery)
}
}

bool DeviceContextGLImpl::UpdateCurrentGLContext(bool PurgeCaches)
bool DeviceContextGLImpl::UpdateCurrentGLContext()
{
if (PurgeCaches)
{
m_pDevice->PurgeContextCaches(m_ContextState.GetCurrentGLContext());
}

auto NativeGLContext = m_pDevice->m_GLContext.GetCurrentNativeGLContext();
if (NativeGLContext == NULL)
return false;
Expand All @@ -1668,6 +1663,13 @@ bool DeviceContextGLImpl::UpdateCurrentGLContext(bool PurgeCaches)
return true;
}

void DeviceContextGLImpl::PurgeCurrentContextCaches()
{
auto NativeGLContext = m_pDevice->m_GLContext.GetCurrentNativeGLContext();
if (NativeGLContext != NULL)
m_pDevice->PurgeContextCaches(NativeGLContext);
}

void DeviceContextGLImpl::UpdateBuffer(IBuffer* pBuffer,
Uint64 Offset,
Uint64 Size,
Expand Down
8 changes: 8 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ void FBOCache::OnReleaseTexture(ITexture* pTexture)
m_TexIdToKey.erase(EqualRange.first, EqualRange.second);
}

void FBOCache::Clear()
{
Threading::SpinLockGuard CacheGuard{m_CacheLock};

m_Cache.clear();
m_TexIdToKey.clear();
}

GLObjectWrappers::GLFrameBufferObj FBOCache::CreateFBO(GLContextState& ContextState,
Uint32 NumRenderTargets,
TextureViewGLImpl* ppRTVs[],
Expand Down
16 changes: 14 additions & 2 deletions Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,11 +1687,23 @@ void RenderDeviceGLImpl::PurgeContextCaches(GLContext::NativeGLContextType Conte
{
{
Threading::SpinLockGuard FBOCacheGuard{m_FBOCacheLock};
m_FBOCache.erase(Context);

auto it = m_FBOCache.find(Context);
if (it != m_FBOCache.end())
{
it->second.Clear();
m_FBOCache.erase(it);
}
}
{
Threading::SpinLockGuard VAOCacheGuard{m_VAOCacheLock};
m_VAOCache.erase(Context);

auto it = m_VAOCache.find(Context);
if (it != m_VAOCache.end())
{
it->second.Clear();
m_VAOCache.erase(it);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/VAOCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ void VAOCache::OnDestroyPSO(const PipelineStateGLImpl& PSO)
ClearStaleKeys(StaleKeys);
}

void VAOCache::Clear()
{
Threading::SpinLockGuard CacheGuard{m_CacheLock};

m_Cache.clear();
m_PSOToKey.clear();
m_BuffToKey.clear();
}

void VAOCache::ClearStaleKeys(const std::vector<VAOHashKey>& StaleKeys)
{
// Collect unique PSOs and buffers used in stale keys.
Expand Down

0 comments on commit 7816066

Please sign in to comment.