Skip to content

Commit 63559e4

Browse files
authored
IDeviceContextGL: OpenGL interop improvements. (#629)
Add `PurgeCurrentContextCaches()` method; Remove `PurgeCaches` parameter of `UpdateCurrentGLContext()`.
1 parent 5813d15 commit 63559e4

File tree

9 files changed

+61
-19
lines changed

9 files changed

+61
-19
lines changed

Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ class DeviceContextGLImpl final : public DeviceContextBase<EngineGLImplTraits>
293293
virtual void DILIGENT_CALL_TYPE BindSparseResourceMemory(const BindSparseResourceMemoryAttribs& Attribs) override final;
294294

295295
/// Implementation of IDeviceContextGL::UpdateCurrentGLContext().
296-
virtual bool DILIGENT_CALL_TYPE UpdateCurrentGLContext(bool PurgeCaches) override final;
296+
virtual bool DILIGENT_CALL_TYPE UpdateCurrentGLContext() override final;
297+
298+
/// Implementation of IDeviceContextGL::PurgeCurrentContextCaches().
299+
virtual void DILIGENT_CALL_TYPE PurgeCurrentContextCaches() override final;
297300

298301
GLContextState& GetContextState() { return m_ContextState; }
299302

Graphics/GraphicsEngineOpenGL/include/FBOCache.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class FBOCache
7272

7373
void OnReleaseTexture(ITexture* pTexture);
7474

75+
void Clear();
76+
7577
private:
7678
// This structure is used as the key to find FBO
7779
struct FBOCacheKey

Graphics/GraphicsEngineOpenGL/include/VAOCache.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class VAOCache
7171
void OnDestroyBuffer(const BufferGLImpl& Buffer);
7272
void OnDestroyPSO(const PipelineStateGLImpl& PSO);
7373

74+
// Clears all cached objects
75+
void Clear();
76+
7477
private:
7578
// This structure is used as the key to find VAO
7679
struct VAOHashKey

Graphics/GraphicsEngineOpenGL/interface/DeviceContextGL.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ DILIGENT_BEGIN_INTERFACE(IDeviceContextGL, IDeviceContext)
5858
/// other command to let the engine update active context every time when control flow
5959
/// is passed over from the main application.
6060
///
61-
/// \param[in] PurgeCaches - Whether to purge context caches (e.g. VAO, FBO) before
62-
/// updating the active context. An application should set this
63-
/// flag to true if the last active context will not be used anymore
64-
/// (e.g. it was destroyed) to avoid memory leaks.
65-
///
6661
/// \return false if there is no active GL context, and true otherwise.
67-
VIRTUAL bool METHOD(UpdateCurrentGLContext)(THIS_
68-
bool PurgeCaches DEFAULT_INITIALIZER(false)) PURE;
62+
VIRTUAL Bool METHOD(UpdateCurrentGLContext)(THIS) PURE;
63+
64+
/// Purge current context caches (e.g. VAO, FBO).
65+
66+
/// If an application uses multiple GL contexts, this method must be called
67+
/// before the current context is about to be released,
68+
/// to let the engine cleanup internal OpenGL object caches.
69+
VIRTUAL void METHOD(PurgeCurrentContextCaches)(THIS) PURE;
6970

7071
/// Sets the swap in the device context. The swap chain is used by the device context
7172
/// to obtain the default FBO handle.
@@ -80,8 +81,9 @@ DILIGENT_END_INTERFACE
8081

8182
// clang-format off
8283

83-
# define IDeviceContextGL_UpdateCurrentGLContext(This, ...) CALL_IFACE_METHOD(DeviceContextGL, UpdateCurrentGLContext, This, __VA_ARGS__)
84-
# define IDeviceContextGL_SetSwapChain(This, ...) CALL_IFACE_METHOD(DeviceContextGL, SetSwapChain, This, __VA_ARGS__)
84+
# define IDeviceContextGL_UpdateCurrentGLContext(This) CALL_IFACE_METHOD(DeviceContextGL, UpdateCurrentGLContext, This)
85+
# define IDeviceContextGL_PurgeCurrentContextCaches(This) CALL_IFACE_METHOD(DeviceContextGL, PurgeCurrentContextCaches, This)
86+
# define IDeviceContextGL_SetSwapChain(This, ...) CALL_IFACE_METHOD(DeviceContextGL, SetSwapChain, This, __VA_ARGS__)
8587

8688
// clang-format on
8789

Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,13 +1653,8 @@ void DeviceContextGLImpl::EndQuery(IQuery* pQuery)
16531653
}
16541654
}
16551655

1656-
bool DeviceContextGLImpl::UpdateCurrentGLContext(bool PurgeCaches)
1656+
bool DeviceContextGLImpl::UpdateCurrentGLContext()
16571657
{
1658-
if (PurgeCaches)
1659-
{
1660-
m_pDevice->PurgeContextCaches(m_ContextState.GetCurrentGLContext());
1661-
}
1662-
16631658
auto NativeGLContext = m_pDevice->m_GLContext.GetCurrentNativeGLContext();
16641659
if (NativeGLContext == NULL)
16651660
return false;
@@ -1668,6 +1663,13 @@ bool DeviceContextGLImpl::UpdateCurrentGLContext(bool PurgeCaches)
16681663
return true;
16691664
}
16701665

1666+
void DeviceContextGLImpl::PurgeCurrentContextCaches()
1667+
{
1668+
auto NativeGLContext = m_pDevice->m_GLContext.GetCurrentNativeGLContext();
1669+
if (NativeGLContext != NULL)
1670+
m_pDevice->PurgeContextCaches(NativeGLContext);
1671+
}
1672+
16711673
void DeviceContextGLImpl::UpdateBuffer(IBuffer* pBuffer,
16721674
Uint64 Offset,
16731675
Uint64 Size,

Graphics/GraphicsEngineOpenGL/src/FBOCache.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ void FBOCache::OnReleaseTexture(ITexture* pTexture)
115115
m_TexIdToKey.erase(EqualRange.first, EqualRange.second);
116116
}
117117

118+
void FBOCache::Clear()
119+
{
120+
Threading::SpinLockGuard CacheGuard{m_CacheLock};
121+
122+
m_Cache.clear();
123+
m_TexIdToKey.clear();
124+
}
125+
118126
GLObjectWrappers::GLFrameBufferObj FBOCache::CreateFBO(GLContextState& ContextState,
119127
Uint32 NumRenderTargets,
120128
TextureViewGLImpl* ppRTVs[],

Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,11 +1687,23 @@ void RenderDeviceGLImpl::PurgeContextCaches(GLContext::NativeGLContextType Conte
16871687
{
16881688
{
16891689
Threading::SpinLockGuard FBOCacheGuard{m_FBOCacheLock};
1690-
m_FBOCache.erase(Context);
1690+
1691+
auto it = m_FBOCache.find(Context);
1692+
if (it != m_FBOCache.end())
1693+
{
1694+
it->second.Clear();
1695+
m_FBOCache.erase(it);
1696+
}
16911697
}
16921698
{
16931699
Threading::SpinLockGuard VAOCacheGuard{m_VAOCacheLock};
1694-
m_VAOCache.erase(Context);
1700+
1701+
auto it = m_VAOCache.find(Context);
1702+
if (it != m_VAOCache.end())
1703+
{
1704+
it->second.Clear();
1705+
m_VAOCache.erase(it);
1706+
}
16951707
}
16961708
}
16971709

Graphics/GraphicsEngineOpenGL/src/VAOCache.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ void VAOCache::OnDestroyPSO(const PipelineStateGLImpl& PSO)
105105
ClearStaleKeys(StaleKeys);
106106
}
107107

108+
void VAOCache::Clear()
109+
{
110+
Threading::SpinLockGuard CacheGuard{m_CacheLock};
111+
112+
m_Cache.clear();
113+
m_PSOToKey.clear();
114+
m_BuffToKey.clear();
115+
}
116+
108117
void VAOCache::ClearStaleKeys(const std::vector<VAOHashKey>& StaleKeys)
109118
{
110119
// Collect unique PSOs and buffers used in stale keys.

Tests/IncludeTest/GraphicsEngineOpenGL/DeviceContextGLH_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
void TestDeviceContextGL_CInterface(IDeviceContextGL* pCtxGL)
3131
{
32-
bool res = IDeviceContextGL_UpdateCurrentGLContext(pCtxGL, true);
32+
bool res = IDeviceContextGL_UpdateCurrentGLContext(pCtxGL);
3333
(void)res;
34+
IDeviceContextGL_PurgeCurrentContextCaches(pCtxGL);
3435
IDeviceContextGL_SetSwapChain(pCtxGL, (struct ISwapChainGL*)NULL);
3536
}

0 commit comments

Comments
 (0)