From ba7b6080892a77ae0b3caf5f795a3f3ff563db46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 23 Oct 2026 12:09:54 -0700 Subject: [PATCH] GLES: Fixed swap chain MSAA and sRGB back buffer. (#4086) --- src/glcontext_egl.cpp | 34 +++++++++++++++++++++++++++++++--- src/glcontext_egl.h | 4 ++++ src/renderer_gl.cpp | 5 ----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 9d77ea573..0e42832c2 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -170,6 +170,28 @@ WL_EGL_IMPORT static EGLint s_contextAttrs[16]; + static EGLSurface createWindowSurface(EGLDisplay _display, EGLConfig _config, EGLNativeWindowType _nwh, bool _colorspaceSupport, bool _srgb) + { + if (_colorspaceSupport + && _srgb) + { + const EGLint attribs[] = + { + EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR, + EGL_NONE + }; + + EGLSurface surface = eglCreateWindowSurface(_display, _config, _nwh, attribs); + + if (EGL_NO_SURFACE != surface) + { + return surface; + } + } + + return eglCreateWindowSurface(_display, _config, _nwh, NULL); + } + struct SwapChainGL { SwapChainGL(EGLDisplay _display, EGLConfig _config, EGLContext _context, EGLNativeWindowType _nwh, int32_t _width, int32_t _height) @@ -359,6 +381,9 @@ WL_EGL_IMPORT BX_TRACE("Supported EGL extensions:"); dumpExtensions(extensions); + m_colorspaceSupport = !bx::findIdentifierMatch(extensions, "EGL_KHR_gl_colorspace").isEmpty(); + m_srgb = 0 != (_swapChain.flags & BGFX_SWAP_CHAIN_SRGB_BACKBUFFER); + if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) ) { EGLBoolean ok = eglBindAPI(EGL_OPENGL_API); @@ -639,7 +664,7 @@ WL_EGL_IMPORT } # endif // BX_PLATFORM_LINUX - m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); + m_surface = createWindowSurface(m_display, m_config, nwh, m_colorspaceSupport, m_srgb); } if (EGL_NO_SURFACE == m_surface) @@ -985,6 +1010,8 @@ WL_EGL_IMPORT m_readSurface = eglGetCurrentSurface(EGL_READ); } + const bool srgb = 0 != (_swapChain.flags & BGFX_SWAP_CHAIN_SRGB_BACKBUFFER); + const bool hasWindow = true && m_ownsContext && NULL != m_display @@ -993,7 +1020,7 @@ WL_EGL_IMPORT ; bool recreateSurface = hasWindow - && m_nwh != _swapChain.nwh + && (m_nwh != _swapChain.nwh || m_srgb != srgb) ; # if BX_PLATFORM_ANDROID @@ -1004,6 +1031,7 @@ WL_EGL_IMPORT { BX_TRACE("EGL window changed: %p -> %p", m_nwh, _swapChain.nwh); m_nwh = _swapChain.nwh; + m_srgb = srgb; eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglDestroySurface(m_display, m_surface); @@ -1028,7 +1056,7 @@ WL_EGL_IMPORT } # endif // BX_PLATFORM_LINUX - m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); + m_surface = createWindowSurface(m_display, m_config, nwh, m_colorspaceSupport, m_srgb); BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface."); m_readSurface = m_surface; diff --git a/src/glcontext_egl.h b/src/glcontext_egl.h index be7044614..019e74f6e 100644 --- a/src/glcontext_egl.h +++ b/src/glcontext_egl.h @@ -53,6 +53,8 @@ namespace bgfx { namespace gl #endif // BX_PLATFORM_* , m_ownsContext(false) , m_msaaContext(false) + , m_colorspaceSupport(false) + , m_srgb(false) , m_swapInterval(0) { } @@ -92,6 +94,8 @@ namespace bgfx { namespace gl bool m_ownsContext; bool m_msaaContext; + bool m_colorspaceSupport; + bool m_srgb; int m_swapInterval; }; } /* namespace gl */ } // namespace bgfx diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index c6771fb2c..20866095c 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -8221,11 +8221,6 @@ namespace bgfx { namespace gl uint32_t msaa = (_desc.flags & BGFX_SWAP_CHAIN_MSAA_MASK)>>BGFX_SWAP_CHAIN_MSAA_SHIFT; msaa = bx::min(s_renderGL->m_maxMsaa, 0 == msaa ? 1 : 1<