Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDeviceContextGL: OpenGL interop improvements. #629

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 11 additions & 9 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 All @@ -80,8 +81,9 @@ DILIGENT_END_INTERFACE

// clang-format off

# define IDeviceContextGL_UpdateCurrentGLContext(This, ...) CALL_IFACE_METHOD(DeviceContextGL, UpdateCurrentGLContext, This, __VA_ARGS__)
# define IDeviceContextGL_SetSwapChain(This, ...) CALL_IFACE_METHOD(DeviceContextGL, SetSwapChain, This, __VA_ARGS__)
# define IDeviceContextGL_UpdateCurrentGLContext(This) CALL_IFACE_METHOD(DeviceContextGL, UpdateCurrentGLContext, This)
# define IDeviceContextGL_PurgeCurrentContextCaches(This) CALL_IFACE_METHOD(DeviceContextGL, PurgeCurrentContextCaches, This)
# define IDeviceContextGL_SetSwapChain(This, ...) CALL_IFACE_METHOD(DeviceContextGL, SetSwapChain, This, __VA_ARGS__)

// clang-format on

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();
TheMostDiligent marked this conversation as resolved.
Show resolved Hide resolved
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();
TheMostDiligent marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

void TestDeviceContextGL_CInterface(IDeviceContextGL* pCtxGL)
{
bool res = IDeviceContextGL_UpdateCurrentGLContext(pCtxGL, true);
bool res = IDeviceContextGL_UpdateCurrentGLContext(pCtxGL);
(void)res;
IDeviceContextGL_PurgeCurrentContextCaches(pCtxGL);
IDeviceContextGL_SetSwapChain(pCtxGL, (struct ISwapChainGL*)NULL);
}
Loading