From 1f81f5940939b7add2431cddd7e4e51ecf78a3ca Mon Sep 17 00:00:00 2001 From: Zi Ree Date: Thu, 28 Jul 2022 17:53:47 +0200 Subject: [PATCH] Add support for shared OpenGL contexts when using SDL2 for multithreaded OpenGL image processing --- indra/llwindow/llwindowsdl2.cpp | 46 ++++++++++++++++++++++++---- indra/llwindow/llwindowsdl2.h | 11 ++++--- indra/newview/featuretable_linux.txt | 7 ++--- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/indra/llwindow/llwindowsdl2.cpp b/indra/llwindow/llwindowsdl2.cpp index bf93216ff8..152f02d287 100644 --- a/indra/llwindow/llwindowsdl2.cpp +++ b/indra/llwindow/llwindowsdl2.cpp @@ -716,6 +716,8 @@ BOOL LLWindowSDL::createContext(int x, int y, int width, int height, int bits, B SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mFSAASamples); } + // Make shared context work on Linux for multithreaded OpenGL + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); mWindow = SDL_CreateWindow( mWindowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, mSDLFlags ); if( mWindow ) @@ -2563,25 +2565,57 @@ std::vector LLWindowSDL::getDynamicFallbackFontList() return rtns; } +// Make shared context work on Linux for multithreaded OpenGL +class sharedContext +{ + public: + SDL_GLContext mContext; +}; void* LLWindowSDL::createSharedContext() { - return SDL_GL_CreateContext(mWindow); + sharedContext* sc = new sharedContext(); + sc->mContext = SDL_GL_CreateContext(mWindow); + if (sc->mContext) + { + SDL_GL_SetSwapInterval(0); + SDL_GL_MakeCurrent(mWindow, mContext); + + LLCoordScreen size; + if (getSize(&size)) + { + setSize(size); + } + + LL_WARNS() << "Creating shared OpenGL context successful!" << LL_ENDL; + + return (void*)sc; + } + + LL_WARNS() << "Creating shared OpenGL context failed!" << LL_ENDL; + + return nullptr; } -void LLWindowSDL::makeContextCurrent(void* contextPtr) +void LLWindowSDL::makeContextCurrent(void* context) { - LL_PROFILER_GPU_CONTEXT; - SDL_GL_MakeCurrent( mWindow, contextPtr ); + LL_PROFILER_GPU_CONTEXT; + SDL_GL_MakeCurrent(mWindow, ((sharedContext*)context)->mContext); } -void LLWindowSDL::destroySharedContext(void* contextPtr) +void LLWindowSDL::destroySharedContext(void* context) { - SDL_GL_DeleteContext( contextPtr ); + sharedContext* sc = (sharedContext*)context; + + SDL_GL_DeleteContext(sc->mContext); + + delete sc; } void LLWindowSDL::toggleVSync(bool enable_vsync) { + SDL_GL_SetSwapInterval(enable_vsync); } +// #endif // LL_SDL diff --git a/indra/llwindow/llwindowsdl2.h b/indra/llwindow/llwindowsdl2.h index 42c6a0d325..20adba802e 100644 --- a/indra/llwindow/llwindowsdl2.h +++ b/indra/llwindow/llwindowsdl2.h @@ -66,6 +66,12 @@ public: /*virtual*/ BOOL setSizeImpl(LLCoordScreen size); /*virtual*/ BOOL setSizeImpl(LLCoordWindow size); /*virtual*/ BOOL switchContext(BOOL fullscreen, const LLCoordScreen &size, BOOL disable_vsync, const LLCoordScreen * const posp = NULL); + // Make shared context work on Linux for multithreaded OpenGL + void* createSharedContext() override; + void makeContextCurrent(void* context) override; + void destroySharedContext(void* context) override; + /*virtual*/ void toggleVSync(bool enable_vsync); + // /*virtual*/ BOOL setCursorPosition(LLCoordWindow position); /*virtual*/ BOOL getCursorPosition(LLCoordWindow *position); /*virtual*/ void showCursor(); @@ -148,11 +154,6 @@ public: static Display* get_SDL_Display(void); #endif // LL_X11 - void* createSharedContext() override; - void makeContextCurrent(void* context) override; - void destroySharedContext(void* context) override; - void toggleVSync(bool enable_vsync) override; - protected: LLWindowSDL(LLWindowCallbacks* callbacks, const std::string& title, int x, int y, int width, int height, U32 flags, diff --git a/indra/newview/featuretable_linux.txt b/indra/newview/featuretable_linux.txt index 1ee21efc12..fb16e7005d 100644 --- a/indra/newview/featuretable_linux.txt +++ b/indra/newview/featuretable_linux.txt @@ -33,7 +33,6 @@ RenderAvatarLODFactor 1 1.0 RenderAvatarPhysicsLODFactor 1 1.0 RenderAvatarMaxNonImpostors 1 16 RenderAvatarMaxComplexity 1 350000 -RenderAvatarVP 1 1 RenderAutoMuteSurfaceAreaLimit 1 1000.0 RenderCubeMap 1 1 RenderDelayVBUpdate 1 0 @@ -70,7 +69,7 @@ RenderShadowDetail 1 2 RenderUseStreamVBO 1 1 RenderFSAASamples 1 16 RenderMaxTextureIndex 1 16 -RenderGLCoreProfile 1 1 +RenderGLContextCoreProfile 1 1 RenderGLMultiThreaded 1 1 @@ -316,12 +315,12 @@ RenderAnisotropic 1 0 RenderCubeMap 0 0 RenderFSAASamples 1 0 RenderGLMultiThreaded 1 0 -RenderGLCoreProfile 1 0 +RenderGLContextCoreProfile 1 0 // AMD cards generally perform better when not using VBOs for streaming data // AMD cards also prefer an OpenGL Compatibility Profile Context list AMD RenderUseStreamVBO 1 0 -RenderGLCoreProfile 1 0 +RenderGLContextCoreProfile 1 0