diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8ae052ab2a..264a9e5da9 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -11,6 +11,7 @@ A new header is inserted each time a *tag* is created. - vulkan: fix memory leak in readPixels - engine: added support for draw-commands channels (stronger ordering of commands/renderables) +- engine: added support for sRGB swapchains. See `SwapChain.h` ## v1.31.2 diff --git a/filament/backend/include/backend/DriverEnums.h b/filament/backend/include/backend/DriverEnums.h index b75d662c53..6a13ce66d8 100644 --- a/filament/backend/include/backend/DriverEnums.h +++ b/filament/backend/include/backend/DriverEnums.h @@ -60,6 +60,11 @@ static constexpr uint64_t SWAP_CHAIN_CONFIG_READABLE = 0x2; */ static constexpr uint64_t SWAP_CHAIN_CONFIG_ENABLE_XCB = 0x4; +/** + * Indicates that the SwapChain must automatically perform linear to srgb encoding. + */ +static constexpr uint64_t SWAP_CHAIN_CONFIG_SRGB_COLORSPACE = 0x8; + /** * Indicates that the native window is a CVPixelBufferRef. * diff --git a/filament/backend/include/backend/platforms/OpenGLPlatform.h b/filament/backend/include/backend/platforms/OpenGLPlatform.h index d9c8476280..5f3f3a8b7b 100644 --- a/filament/backend/include/backend/platforms/OpenGLPlatform.h +++ b/filament/backend/include/backend/platforms/OpenGLPlatform.h @@ -68,6 +68,14 @@ public: */ virtual SwapChain* createSwapChain(void* nativeWindow, uint64_t flags) noexcept = 0; + /** + * Return whether createSwapChain supports the SWAP_CHAIN_CONFIG_SRGB_COLORSPACE flag. + * The default implementation returns false. + * + * @return true is SWAP_CHAIN_CONFIG_SRGB_COLORSPACE is supported, false otherwise. + */ + virtual bool isSRGBSwapChainSupported() const noexcept; + /** * Called by the driver create a headless SwapChain. * diff --git a/filament/backend/include/backend/platforms/PlatformEGL.h b/filament/backend/include/backend/platforms/PlatformEGL.h index e72d3ac246..21fc7c961a 100644 --- a/filament/backend/include/backend/platforms/PlatformEGL.h +++ b/filament/backend/include/backend/platforms/PlatformEGL.h @@ -58,6 +58,7 @@ protected: void terminate() noexcept override; + bool isSRGBSwapChainSupported() const noexcept override; SwapChain* createSwapChain(void* nativewindow, uint64_t flags) noexcept override; SwapChain* createSwapChain(uint32_t width, uint32_t height, uint64_t flags) noexcept override; void destroySwapChain(SwapChain* swapChain) noexcept override; @@ -105,6 +106,7 @@ protected: } gl; struct { bool KHR_no_config_context = false; + bool KHR_gl_colorspace = false; } egl; } ext; diff --git a/filament/backend/include/private/backend/DriverAPI.inc b/filament/backend/include/private/backend/DriverAPI.inc index 3e9b39dba8..c83549b6ec 100644 --- a/filament/backend/include/private/backend/DriverAPI.inc +++ b/filament/backend/include/private/backend/DriverAPI.inc @@ -299,6 +299,7 @@ DECL_DRIVER_API_SYNCHRONOUS_0(bool, isFrameBufferFetchSupported) DECL_DRIVER_API_SYNCHRONOUS_0(bool, isFrameBufferFetchMultiSampleSupported) DECL_DRIVER_API_SYNCHRONOUS_0(bool, isFrameTimeSupported) DECL_DRIVER_API_SYNCHRONOUS_0(bool, isAutoDepthResolveSupported) +DECL_DRIVER_API_SYNCHRONOUS_0(bool, isSRGBSwapChainSupported) DECL_DRIVER_API_SYNCHRONOUS_0(uint8_t, getMaxDrawBuffers) DECL_DRIVER_API_SYNCHRONOUS_0(math::float2, getClipSpaceParams) DECL_DRIVER_API_SYNCHRONOUS_0(bool, canGenerateMipmaps) diff --git a/filament/backend/src/metal/MetalDriver.mm b/filament/backend/src/metal/MetalDriver.mm index 77a9cd28a0..27c57e32f9 100644 --- a/filament/backend/src/metal/MetalDriver.mm +++ b/filament/backend/src/metal/MetalDriver.mm @@ -706,6 +706,11 @@ bool MetalDriver::isAutoDepthResolveSupported() { return mContext->supportsAutoDepthResolve; } +bool MetalDriver::isSRGBSwapChainSupported() { + // the SWAP_CHAIN_CONFIG_SRGB_COLORSPACE flag is not supported + return false; +} + bool MetalDriver::isWorkaroundNeeded(Workaround workaround) { switch (workaround) { case Workaround::SPLIT_EASU: diff --git a/filament/backend/src/noop/NoopDriver.cpp b/filament/backend/src/noop/NoopDriver.cpp index a635963847..cc06cd87d7 100644 --- a/filament/backend/src/noop/NoopDriver.cpp +++ b/filament/backend/src/noop/NoopDriver.cpp @@ -173,7 +173,11 @@ bool NoopDriver::isAutoDepthResolveSupported() { return true; } -bool NoopDriver::isWorkaroundNeeded(Workaround workaround) { +bool NoopDriver::isSRGBSwapChainSupported() { + return false; +} + +bool NoopDriver::isWorkaroundNeeded(Workaround) { return false; } diff --git a/filament/backend/src/opengl/OpenGLContext.cpp b/filament/backend/src/opengl/OpenGLContext.cpp index 10c6f541bd..6163e3a1b7 100644 --- a/filament/backend/src/opengl/OpenGLContext.cpp +++ b/filament/backend/src/opengl/OpenGLContext.cpp @@ -328,6 +328,12 @@ void OpenGLContext::setDefaultState() noexcept { } } +#if BACKEND_OPENGL_VERSION == BACKEND_OPENGL_VERSION_GL + // we enable GL_FRAMEBUFFER_SRGB to match GLES. This enables sRGB conversion when the + // framebuffer has an sRGB attachment. GLES doesn't have an enable for this by default. + glEnable(GL_FRAMEBUFFER_SRGB); +#endif + // Point sprite size and seamless cubemap filtering are disabled by default in desktop GL. // In OpenGL ES, these flags do not exist because they are always on. #if BACKEND_OPENGL_VERSION == BACKEND_OPENGL_VERSION_GL diff --git a/filament/backend/src/opengl/OpenGLDriver.cpp b/filament/backend/src/opengl/OpenGLDriver.cpp index 2134459f42..a13ac55815 100644 --- a/filament/backend/src/opengl/OpenGLDriver.cpp +++ b/filament/backend/src/opengl/OpenGLDriver.cpp @@ -1691,6 +1691,10 @@ bool OpenGLDriver::isAutoDepthResolveSupported() { return true; } +bool OpenGLDriver::isSRGBSwapChainSupported() { + return mPlatform.isSRGBSwapChainSupported(); +} + bool OpenGLDriver::isWorkaroundNeeded(Workaround workaround) { switch (workaround) { case Workaround::SPLIT_EASU: diff --git a/filament/backend/src/opengl/OpenGLPlatform.cpp b/filament/backend/src/opengl/OpenGLPlatform.cpp index 5a2645b2ea..d94f8d091c 100644 --- a/filament/backend/src/opengl/OpenGLPlatform.cpp +++ b/filament/backend/src/opengl/OpenGLPlatform.cpp @@ -27,6 +27,10 @@ Driver* OpenGLPlatform::createDefaultDriver(OpenGLPlatform* platform, OpenGLPlatform::~OpenGLPlatform() noexcept = default; +bool OpenGLPlatform::isSRGBSwapChainSupported() const noexcept { + return false; +} + uint32_t OpenGLPlatform::createDefaultRenderTarget() noexcept { return 0; } diff --git a/filament/backend/src/opengl/platforms/PlatformEGL.cpp b/filament/backend/src/opengl/platforms/PlatformEGL.cpp index 9aa6642d76..bfff2f6cae 100644 --- a/filament/backend/src/opengl/platforms/PlatformEGL.cpp +++ b/filament/backend/src/opengl/platforms/PlatformEGL.cpp @@ -103,6 +103,7 @@ Driver* PlatformEGL::createDriver(void* sharedContext, const Platform::DriverCon auto extensions = GLUtils::split(eglQueryString(mEGLDisplay, EGL_EXTENSIONS)); ext.egl.KHR_no_config_context = extensions.has("EGL_KHR_no_config_context"); + ext.egl.KHR_gl_colorspace = extensions.has("EGL_KHR_gl_colorspace"); eglCreateSyncKHR = (PFNEGLCREATESYNCKHRPROC) eglGetProcAddress("eglCreateSyncKHR"); eglDestroySyncKHR = (PFNEGLDESTROYSYNCKHRPROC) eglGetProcAddress("eglDestroySyncKHR"); @@ -281,6 +282,10 @@ EGLConfig PlatformEGL::findSwapChainConfig(uint64_t flags) const { return config; } +bool PlatformEGL::isSRGBSwapChainSupported() const noexcept { + return ext.egl.KHR_gl_colorspace; +} + Platform::SwapChain* PlatformEGL::createSwapChain( void* nativeWindow, uint64_t flags) noexcept { @@ -295,8 +300,20 @@ Platform::SwapChain* PlatformEGL::createSwapChain( return nullptr; } + EGLint attribs[] = { + EGL_NONE, EGL_NONE, + EGL_NONE + }; + + if (ext.egl.KHR_gl_colorspace) { + if (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) { + attribs[0] = EGL_GL_COLORSPACE_KHR; + attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR; + } + } + EGLSurface sur = eglCreateWindowSurface(mEGLDisplay, config, - (EGLNativeWindowType)nativeWindow, nullptr); + (EGLNativeWindowType)nativeWindow, attribs); if (UTILS_UNLIKELY(sur == EGL_NO_SURFACE)) { logEglError("eglCreateWindowSurface"); @@ -323,12 +340,20 @@ Platform::SwapChain* PlatformEGL::createSwapChain( return nullptr; } - const EGLint attribs[] = { + EGLint attribs[] = { EGL_WIDTH, EGLint(width), EGL_HEIGHT, EGLint(height), + EGL_NONE, EGL_NONE, EGL_NONE }; + if (ext.egl.KHR_gl_colorspace) { + if (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) { + attribs[4] = EGL_GL_COLORSPACE_KHR; + attribs[5] = EGL_GL_COLORSPACE_SRGB_KHR; + } + } + EGLSurface sur = eglCreatePbufferSurface(mEGLDisplay, config, attribs); if (UTILS_UNLIKELY(sur == EGL_NO_SURFACE)) { diff --git a/filament/backend/src/vulkan/VulkanDriver.cpp b/filament/backend/src/vulkan/VulkanDriver.cpp index a50abb0017..b40f2a4593 100644 --- a/filament/backend/src/vulkan/VulkanDriver.cpp +++ b/filament/backend/src/vulkan/VulkanDriver.cpp @@ -635,6 +635,11 @@ bool VulkanDriver::isAutoDepthResolveSupported() { return false; } +bool VulkanDriver::isSRGBSwapChainSupported() { + // TODO: implement SWAP_CHAIN_CONFIG_SRGB_COLORSPACE + return false; +} + bool VulkanDriver::isWorkaroundNeeded(Workaround workaround) { VkPhysicalDeviceProperties const& deviceProperties = mContext.physicalDeviceProperties; switch (workaround) { diff --git a/filament/include/filament/SwapChain.h b/filament/include/filament/SwapChain.h index 94d0b7dd67..2eefd23596 100644 --- a/filament/include/filament/SwapChain.h +++ b/filament/include/filament/SwapChain.h @@ -25,6 +25,8 @@ namespace filament { +class Engine; + /** * A swap chain represents an Operating System's *native* renderable surface. * @@ -182,6 +184,29 @@ public: static const uint64_t CONFIG_APPLE_CVPIXELBUFFER = backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER; + /** + * Indicates that the SwapChain must automatically perform linear to sRGB encoding. + * + * This flag is ignored if isSRGBSwapChainSupported() is false. + * + * When using this flag, the output colorspace in ColorGrading should be set to + * Rec709-Linear-D65, or post-processing should be disabled. + * + * @see isSRGBSwapChainSupported() + * @see ColorGrading.outputColorSpace() + * @see View.setPostProcessingEnabled() + */ + static constexpr uint64_t CONFIG_SRGB_COLORSPACE = backend::SWAP_CHAIN_CONFIG_SRGB_COLORSPACE; + + /** + * Return whether createSwapChain supports the SWAP_CHAIN_CONFIG_SRGB_COLORSPACE flag. + * The default implementation returns false. + * + * @param engine A pointer to the filament Engine + * @return true is SWAP_CHAIN_CONFIG_SRGB_COLORSPACE is supported, false otherwise. + */ + static bool isSRGBSwapChainSupported(Engine& engine) noexcept; + void* getNativeWindow() const noexcept; /** diff --git a/filament/src/SwapChain.cpp b/filament/src/SwapChain.cpp index 25242d25e5..ae1498cc91 100644 --- a/filament/src/SwapChain.cpp +++ b/filament/src/SwapChain.cpp @@ -16,6 +16,8 @@ #include "details/SwapChain.h" +#include "details/Engine.h" + namespace filament { void* SwapChain::getNativeWindow() const noexcept { @@ -30,4 +32,8 @@ void SwapChain::setFrameCompletedCallback(FrameCompletedCallback callback, void* return downcast(this)->setFrameCompletedCallback(callback, user); } +bool SwapChain::isSRGBSwapChainSupported(Engine& engine) noexcept { + return FSwapChain::isSRGBSwapChainSupported(downcast(engine)); +} + } // namespace filament diff --git a/filament/src/details/SwapChain.cpp b/filament/src/details/SwapChain.cpp index 07d61a0586..ba13be2e2d 100644 --- a/filament/src/details/SwapChain.cpp +++ b/filament/src/details/SwapChain.cpp @@ -42,4 +42,8 @@ void FSwapChain::setFrameCompletedCallback(FrameCompletedCallback callback, void mEngine.getDriverApi().setFrameCompletedCallback(mSwapChain, callback, user); } +bool FSwapChain::isSRGBSwapChainSupported(FEngine& engine) noexcept { + return engine.getDriverApi().isSRGBSwapChainSupported(); +} + } // namespace filament diff --git a/filament/src/details/SwapChain.h b/filament/src/details/SwapChain.h index 7e397ca693..c1a3f436d2 100644 --- a/filament/src/details/SwapChain.h +++ b/filament/src/details/SwapChain.h @@ -63,6 +63,8 @@ public: void setFrameCompletedCallback(FrameCompletedCallback callback, void* user); + static bool isSRGBSwapChainSupported(FEngine& engine) noexcept; + private: FEngine& mEngine; backend::Handle mSwapChain;