diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b8783791bc..dbfe6057bd 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -6,6 +6,7 @@ A new header is inserted each time a *tag* is created. ## main branch - engine: fix broken picking [⚠️ **Recompile Materials to get the fix**] +- engine: added support for sRGB swapchains. See `SwapChain.h` - bluegl: support Windows32 ## v1.31.3 diff --git a/android/filament-android/src/main/cpp/SwapChain.cpp b/android/filament-android/src/main/cpp/SwapChain.cpp index 0ddc85f12f..3693803ce1 100644 --- a/android/filament-android/src/main/cpp/SwapChain.cpp +++ b/android/filament-android/src/main/cpp/SwapChain.cpp @@ -16,6 +16,7 @@ #include +#include #include #include "common/CallbackUtils.h" @@ -32,3 +33,9 @@ Java_com_google_android_filament_SwapChain_nSetFrameCompletedCallback(JNIEnv* en JniCallback::postToJavaAndDestroy(callback); }, callback); } + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_google_android_filament_SwapChain_nIsSRGBSwapChainSupported(JNIEnv *, jclass, jlong nativeEngine) { + Engine* engine = (Engine*) nativeEngine; + return (bool)SwapChain::isSRGBSwapChainSupported(*engine); +} diff --git a/android/filament-android/src/main/java/com/google/android/filament/SwapChain.java b/android/filament-android/src/main/java/com/google/android/filament/SwapChain.java index 391f1f6e1d..6d621f02ff 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/SwapChain.java +++ b/android/filament-android/src/main/java/com/google/android/filament/SwapChain.java @@ -91,11 +91,34 @@ public class SwapChain { */ public static final long CONFIG_ENABLE_XCB = 0x4; + /** + * Indicates that the SwapChain must automatically perform linear to sRGB encoding. + * + * This flag is ignored if isSRGBSwapChainSupported() is false. + * + * When using this flag, post-processing should be disabled. + * + * @see SwapChain#isSRGBSwapChainSupported + * @see View#setPostProcessingEnabled + */ + public static final long CONFIG_SRGB_COLORSPACE = 0x10; + SwapChain(long nativeSwapChain, Object surface) { mNativeObject = nativeSwapChain; mSurface = surface; } + /** + * Return whether createSwapChain supports the SWAP_CHAIN_CONFIG_SRGB_COLORSPACE flag. + * The default implementation returns false. + * + * @param engine A reference to the filament Engine + * @return true if SWAP_CHAIN_CONFIG_SRGB_COLORSPACE is supported, false otherwise. + */ + public static boolean isSRGBSwapChainSupported(@NonNull Engine engine) { + return nIsSRGBSwapChainSupported(engine.getNativeObject()); + } + /** * @return the native Object this SwapChain was created from or null * for a headless SwapChain. @@ -141,4 +164,5 @@ public class SwapChain { } private static native void nSetFrameCompletedCallback(long nativeSwapChain, Object handler, Runnable callback); + private static native boolean nIsSRGBSwapChainSupported(long nativeEngine); } diff --git a/filament/backend/include/backend/DriverEnums.h b/filament/backend/include/backend/DriverEnums.h index b75d662c53..5fcc2dc223 100644 --- a/filament/backend/include/backend/DriverEnums.h +++ b/filament/backend/include/backend/DriverEnums.h @@ -72,6 +72,12 @@ static constexpr uint64_t SWAP_CHAIN_CONFIG_ENABLE_XCB = 0x4; */ static constexpr uint64_t SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER = 0x8; +/** + * Indicates that the SwapChain must automatically perform linear to srgb encoding. + */ +static constexpr uint64_t SWAP_CHAIN_CONFIG_SRGB_COLORSPACE = 0x10; + + static constexpr size_t MAX_VERTEX_ATTRIBUTE_COUNT = 16; // This is guaranteed by OpenGL ES. static constexpr size_t MAX_SAMPLER_COUNT = 62; // Maximum needed at feature level 3. static constexpr size_t MAX_VERTEX_BUFFER_COUNT = 16; // Max number of bound buffer objects. diff --git a/filament/backend/include/backend/platforms/OpenGLPlatform.h b/filament/backend/include/backend/platforms/OpenGLPlatform.h index d9c8476280..8cdbc0f67c 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 if 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/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 0cfc675754..d6ada095b3 100644 --- a/filament/backend/src/vulkan/VulkanDriver.cpp +++ b/filament/backend/src/vulkan/VulkanDriver.cpp @@ -640,6 +640,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..9f7a328199 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 if 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; diff --git a/web/filament-js/jsbindings.cpp b/web/filament-js/jsbindings.cpp index 117be8e4db..12782d3d4f 100644 --- a/web/filament-js/jsbindings.cpp +++ b/web/filament-js/jsbindings.cpp @@ -544,7 +544,8 @@ class_("Engine") /// SwapChain ::core class:: Represents the platform's native rendering surface. /// See also the [Engine] methods `createSwapChain` and `destroySwapChain`. -class_("SwapChain"); +class_("SwapChain") + .class_function("isSRGBSwapChainSupported", &SwapChain::isSRGBSwapChainSupported); /// Renderer ::core class:: Represents the platform's native window. /// See also the [Engine] methods `createRenderer` and `destroyRenderer`. @@ -572,7 +573,6 @@ class_("Renderer") /// A view is associated with a particular [Scene], [Camera], and viewport. /// See also the [Engine] methods `createView` and `destroyView`. class_("View") - .function("pick", EMBIND_LAMBDA(void, (View* self, uint32_t x, uint32_t y, val cb), { self->pick(x, y, [cb](const View::PickingQueryResult& result) { EM_ASM_ARGS({