From b28e6305577f9fccc34443ed7452bba6a6004bc8 Mon Sep 17 00:00:00 2001 From: Wojciech Jarosz Date: Fri, 12 Jan 2024 15:24:35 +1300 Subject: [PATCH] requestFloatBuffer option --- .../internal/backend_impls/rendering_metal.mm | 17 ++++++++ .../backend_impls/rendering_metal_glfw.mm | 2 +- .../backend_impls/rendering_metal_sdl.mm | 2 +- src/hello_imgui/renderer_backend_options.cpp | 11 +++++ src/hello_imgui/renderer_backend_options.h | 40 +++++++++---------- .../hello_imgui_demodocking.main.cpp | 8 ++++ 6 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 src/hello_imgui/renderer_backend_options.cpp diff --git a/src/hello_imgui/internal/backend_impls/rendering_metal.mm b/src/hello_imgui/internal/backend_impls/rendering_metal.mm index 940c6166..e9380710 100644 --- a/src/hello_imgui/internal/backend_impls/rendering_metal.mm +++ b/src/hello_imgui/internal/backend_impls/rendering_metal.mm @@ -4,9 +4,26 @@ #include #include "hello_imgui/hello_imgui.h" +#import namespace HelloImGui { + bool hasEdrSupport() + { + NSArray * screens = [NSScreen screens]; + bool buffer_edr = false; + + for (NSScreen * screen in screens) { + if ([screen respondsToSelector:@selector + (maximumPotentialExtendedDynamicRangeColorComponentValue)]) { + if ([screen maximumPotentialExtendedDynamicRangeColorComponentValue] >= 2.f) + buffer_edr = true; + } + } + + return buffer_edr; + } + MetalGlobals& GetMetalGlobals() { static MetalGlobals sMetalGlobals; diff --git a/src/hello_imgui/internal/backend_impls/rendering_metal_glfw.mm b/src/hello_imgui/internal/backend_impls/rendering_metal_glfw.mm index 0d5a9203..ac2c8fe8 100644 --- a/src/hello_imgui/internal/backend_impls/rendering_metal_glfw.mm +++ b/src/hello_imgui/internal/backend_impls/rendering_metal_glfw.mm @@ -44,7 +44,7 @@ void PrepareGlfwForMetal(GLFWwindow* glfwWindow, const RendererBackendOptions& r gMetalGlobals.caMetalLayer = [CAMetalLayer layer]; gMetalGlobals.caMetalLayer.device = gMetalGlobals.mtlDevice; - if (rendererBackendOptions.metalOptions.fixmeDummyOptionName) + if (rendererBackendOptions.requestFloatBuffer) { gMetalGlobals.caMetalLayer.pixelFormat = MTLPixelFormatRGBA16Float; gMetalGlobals.caMetalLayer.wantsExtendedDynamicRangeContent = YES; diff --git a/src/hello_imgui/internal/backend_impls/rendering_metal_sdl.mm b/src/hello_imgui/internal/backend_impls/rendering_metal_sdl.mm index 5a119932..ebff1757 100644 --- a/src/hello_imgui/internal/backend_impls/rendering_metal_sdl.mm +++ b/src/hello_imgui/internal/backend_impls/rendering_metal_sdl.mm @@ -42,7 +42,7 @@ void PrepareSdlForMetal(SDL_Window* sdlWindow, const RendererBackendOptions& ren // Setup Platform/Renderer backends gMetalGlobals.caMetalLayer = (__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(gSdlMetalGlobals.sdlRenderer); - if (rendererBackendOptions.metalOptions.fixmeDummyOptionName) + if (rendererBackendOptions.requestFloatBuffer) { gMetalGlobals.caMetalLayer.pixelFormat = MTLPixelFormatRGBA16Float; gMetalGlobals.caMetalLayer.wantsExtendedDynamicRangeContent = YES; diff --git a/src/hello_imgui/renderer_backend_options.cpp b/src/hello_imgui/renderer_backend_options.cpp new file mode 100644 index 00000000..f505afed --- /dev/null +++ b/src/hello_imgui/renderer_backend_options.cpp @@ -0,0 +1,11 @@ +#include "renderer_backend_options.h" + +namespace HelloImGui +{ + +// currently, only the metal backend has support for this +#ifndef HELLOIMGUI_HAS_METAL +bool hasEdrSupport() { return false; } +#endif + +} // namespace HelloImGui diff --git a/src/hello_imgui/renderer_backend_options.h b/src/hello_imgui/renderer_backend_options.h index 4db31368..b5f41ec7 100644 --- a/src/hello_imgui/renderer_backend_options.h +++ b/src/hello_imgui/renderer_backend_options.h @@ -1,19 +1,27 @@ #pragma once - namespace HelloImGui { + /** - @@md#RendererBackendOptions + Check whether extended dynamic range (EDR), i.e. the ability to reproduce intensities exceeding the + standard dynamic range from 0.0-1.0, is supported. -**RendererBackendOptions** is a struct that contains options for the renderer backend (Metal, Vulkan, DirectX, ...). - Members: -* `metalOptions`: _MetalOptions_. Options for the Metal backend (only filled if the Metal backend is available) + To leverage EDR support, you will need to set `floatBuffer=true` in `RendererBackendOptions`. + Only the macOS Metal backend currently supports this. + \return This currently returns false on all backends except Metal, where it checks whether this is + supported on the current displays. +*/ +bool hasEdrSupport(); + +/** + @@md#RendererBackendOptions -**MetalOptions** is a struct that contains options for the Metal backend. - Members: -* `fixmeDummyOptionName`: _bool, default=false_. Dummy option for Metal backend (to be completed) +**RendererBackendOptions** is a struct that contains options for the renderer backend (Metal, Vulkan, DirectX, +...). Members: +* `requestFloatBuffer`: _bool, default=false_. Set to true to request a floating-point framebuffer. + Before setting this to true, first check `hasEdrSupport` Note: If using the Metal, Vulkan or DirectX rendering backend, you can find some interesting pointers inside @@ -25,20 +33,10 @@ Note: If using the Metal, Vulkan or DirectX rendering backend, you can find some @@md */ - -#ifdef HELLOIMGUI_HAS_METAL -struct MetalOptions -{ - // to be completed - bool fixmeDummyOptionName = false; -}; -#endif - struct RendererBackendOptions { -#ifdef HELLOIMGUI_HAS_METAL - MetalOptions metalOptions; -#endif + + bool requestFloatBuffer = false; }; -} // namespace HelloImGui \ No newline at end of file +} // namespace HelloImGui \ No newline at end of file diff --git a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp index d4b6ba66..bb048209 100644 --- a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp +++ b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp @@ -13,6 +13,7 @@ It demonstrates how to: */ #include "hello_imgui/hello_imgui.h" +#include "hello_imgui/renderer_backend_options.h" #include "imgui.h" #include "misc/cpp/imgui_stdlib.h" #include "imgui_internal.h" @@ -629,6 +630,13 @@ int main(int, char**) runnerParams.appWindowParams.borderlessResizable = true; runnerParams.appWindowParams.borderlessClosable = true; + // test EDR support on macOS/Metal + bool requestEDR = HelloImGui::hasEdrSupport(); + runnerParams.rendererBackendOptions.requestFloatBuffer = requestEDR; + HelloImGui::Log(HelloImGui::LogLevel::Info, + "Creating a %s framebuffer.", + requestEDR ? "floating-point precision" : "standard precision"); + // Load additional font runnerParams.callbacks.LoadAdditionalFonts = [&appState]() { LoadFonts(appState); };