keid-ui-dearimgui 0.1.5.0 → 0.2.0.0
raw patch · 5 files changed
+163/−136 lines, 5 filesdep +dear-imgui-impl-glfwdep +dear-imgui-impl-vulkandep +dear-imgui-rawdep ~dear-imguidep ~keid-core
Dependencies added: dear-imgui-impl-glfw, dear-imgui-impl-vulkan, dear-imgui-raw
Dependency ranges changed: dear-imgui, keid-core
Files
- ChangeLog.md +12/−0
- keid-ui-dearimgui.cabal +6/−3
- src/Render/ImGui.hs +90/−82
- src/Render/ImGui/Widget.hs +28/−25
- src/Render/ImGui/Window.hs +27/−26
ChangeLog.md view
@@ -1,5 +1,17 @@ # Changelog for keid-dearimgui +## 0.2.0.0++- Bump upstream to `dear-imgui-3.0.0`, with `dear-imgui-raw`, `dear-imgui-impl-glfw` and `dear-imgui-impl-vulkan`.+ * Dear ImGui is updated to 1.93.0. See the `dear-imgui` changelog for the API changes.+ * `Render.ImGui.Window.Window` flags are `ImGuiWindowFlags` from `DearImGui.Raw.Enums.ImGuiWindowFlags`.+ * The descriptor pool holds only sampled images and samplers, sized to the backend minimums.+ * `allocateWith` and `allocateWithFonts` take a number of extra sampled image sets for textures registered with `DearImGui.Raw.Impl.Vulkan.addTexture`.+- `allocate`, `allocateWith`, `allocateWithFonts` and `renderWith` take a `HasRendering` target. Dear ImGui renders with dynamic rendering using the target's formats and sample count.+- Removed the subpass index argument.+- Fixed `Render.ImGui.allocateWith` destroying its descriptor pool before `vulkanShutdown` when the stage is interrupted.+- Fixed ImGui texture uploads using the graphics queue while another thread submits to it.+ ## 0.1.5.0 - Bump upstream to `dear-imgui-2.5.0'.
keid-ui-dearimgui.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: keid-ui-dearimgui-version: 0.1.5.0+version: 0.2.0.0 synopsis: DearImGui elements for Keid engine. category: Game Engine author: IC Rainbow@@ -64,10 +64,13 @@ , base >=4.7 && <5 , binary , bytestring- , dear-imgui >=2.5.0 && <3+ , dear-imgui >=3.0.0 && <4+ , dear-imgui-impl-glfw >=1.0.0 && <1.1+ , dear-imgui-impl-vulkan >=1.0.0 && <1.1+ , dear-imgui-raw >=1.0.0 && <1.1 , geomancy , geomancy-layout- , keid-core >=0.1.8.0+ , keid-core >=0.2.0.0 , keid-frp-banana , reactive-banana , resourcet
src/Render/ImGui.hs view
@@ -28,104 +28,108 @@ import Data.Vector qualified as Vector import DearImGui qualified as ImGui import DearImGui.FontAtlas qualified as FontAtlas-import DearImGui.GLFW (glfwNewFrame, glfwShutdown)-import DearImGui.GLFW.Vulkan (glfwInitForVulkan)-import DearImGui.Vulkan (InitInfo(..))-import DearImGui.Vulkan qualified as ImGui+import DearImGui.Impl.GLFW qualified as ImplGLFW+import DearImGui.Raw.Impl.GLFW qualified as RawGLFW+import DearImGui.Raw.Impl.Vulkan qualified as ImplVulkan import Engine.Setup.Window qualified as Window import Engine.Stage.Component qualified as Stage import Engine.Types (GlobalHandles(..), StageFrameRIO, StageRIO, askWindowSize)-import Engine.Vulkan.Types (HasRenderPass(..), HasSwapchain(..), Queues(..), getDevice, getMultisample, MonadVulkan)+import Engine.Vulkan.Types (HasSwapchain(..), Queue(..), getDevice, getQueues, MonadVulkan, waitDeviceIdle, withQueue) import Engine.Worker qualified as Worker+import Foreign (FunPtr, castPtr, freeHaskellFunPtr, fromBool, poke, toBool) import Geomancy.Layout.Box as Box import Geomancy.Layout.View as View import RIO.App (appEnv)+import Render.Target (HasRendering(..), RenderingFormats(..), pipelineRendering) import Resource.Region qualified as Region import Resource.Vulkan.Named qualified as Named import Vulkan.Core10 qualified as Vk-import Vulkan.Exception (VulkanException(..))-import Vulkan.NamedType (type (:::))+import Vulkan.Core13 (pattern API_VERSION_1_3)+import Vulkan.CStruct (ToCStruct(..)) import Vulkan.Utils.QueueAssignment (QueueFamilyIndex(..))+import Vulkan.Utils.Queues (Queues(..)) import Vulkan.Zero (zero) allocate :: ( HasSwapchain swapchain- , HasRenderPass renderpass+ , HasRendering target ) => swapchain- -> renderpass- -> Word32+ -> target -> ResourceT (StageRIO st) ReleaseKey-allocate swapchain renderpass subpassIx =+allocate swapchain target = fmap fst $- allocateWith swapchain renderpass subpassIx $+ allocateWith swapchain target 0 $ pure () allocateWithFonts :: ( HasSwapchain swapchain- , HasRenderPass renderpass+ , HasRendering target , Traversable t ) => swapchain- -> renderpass+ -> target -> Word32 -> t FontAtlas.FontSource -> ResourceT (StageRIO st) (ReleaseKey, t ImGui.Font)-allocateWithFonts swapchain renderpass subpassIx fonts =- allocateWith swapchain renderpass subpassIx do+allocateWithFonts swapchain target extraTextures fonts =+ allocateWith swapchain target extraTextures do loaded <- FontAtlas.rebuild fonts _atlasKey <- Resource.register FontAtlas.clear pure loaded allocateWith :: ( HasSwapchain swapchain- , HasRenderPass renderpass+ , HasRendering target ) => swapchain- -> renderpass+ -> target -> Word32 -> ResourceT (StageRIO st) a -> ResourceT (StageRIO st) (ReleaseKey, a)-allocateWith swapchain renderpass subpassIx action = do+allocateWith swapchain target extraTextures action = do logDebug "Initializing DearImGui" debugReleaseFinished <- toIO (logDebug "Released DearImGui") void $! Resource.register debugReleaseFinished + (_poolKey, pool) <- allocateDescPool extraTextures+ ctx <- ImGui.createContext Region.register_ $ ImGui.destroyContext ctx GlobalHandles{..} <- asks appEnv - let (QueueFamilyIndex queueFamily, queue) = qGraphics ghQueues-- pool <- Region.local allocateDescPool-- let- initInfo = InitInfo- { instance' = ghInstance- , physicalDevice = ghPhysicalDevice- , device = ghDevice- , queueFamily = queueFamily- , queue = queue-- , rendering = Left (getRenderPass renderpass)- , minImageCount = getMinImageCount swapchain- , imageCount = getImageCount swapchain- , msaaSamples = getMultisample swapchain- , subpass = subpassIx- , pipelineCache = Vk.NULL_HANDLE-- , descriptorPool = pool+ let Queue{family = QueueFamilyIndex queueFamily, queue} = qGraphics ghQueues+ let formats = getRenderingFormats target - , mbAllocator = Nothing- , checkResult = \case { Vk.SUCCESS -> pure (); e -> throwM $ VulkanException e }- }+ runInIO <- askRunInIO+ checkResult <- liftIO $ wrapCheckResult \result ->+ unless (result == Vk.SUCCESS) $+ runInIO $ logBackendResult result+ Region.register_ $ freeHaskellFunPtr checkResult - res@(_cb, initOk) <- ImGui.vulkanInit initInfo+ initOk <- liftIO $ ImplVulkan.withDefaultImGui_ImplVulkan_InitInfo \info -> do+ poke info.apiVersion API_VERSION_1_3+ poke info.instance_ $ Vk.instanceHandle ghInstance+ poke info.physicalDevice $ Vk.physicalDeviceHandle ghPhysicalDevice+ poke info.device $ Vk.deviceHandle ghDevice+ poke info.queueFamily queueFamily+ poke info.queue $ Vk.queueHandle queue+ poke info.descriptorPool pool+ poke info.minImageCount $ getMinImageCount swapchain+ poke info.imageCount $ getImageCount swapchain+ poke info.useDynamicRendering $ fromBool True+ poke info.checkVkResultFn checkResult+ let mainPipeline = info.pipelineInfoMain+ poke mainPipeline.mSAASamples $ rfSamples formats+ pokeCStruct+ (castPtr mainPipeline.pipelineRenderingCreateInfo)+ (pipelineRendering formats)+ (toBool <$> ImplVulkan.init info) unless initOk do logError "DearImGui vulkan initialization failed" exitFailure- key <- Resource.register $ ImGui.vulkanShutdown res+ key <- Resource.register ImplVulkan.shutdown actionRes <- action @@ -138,8 +142,9 @@ :: ( MonadVulkan env m , MonadResource m )- => m (ReleaseKey, Vk.DescriptorPool)-allocateDescPool = do+ => Word32+ -> m (ReleaseKey, Vk.DescriptorPool)+allocateDescPool extraTextures = do device <- asks getDevice res <- Vk.withDescriptorPool device@@ -150,31 +155,32 @@ Named.object (snd res) "ImGui" pure res where+ sampledImages =+ ImplVulkan.IMGUI_IMPL_VULKAN_MINIMUM_SAMPLED_IMAGE_POOL_SIZE + extraTextures+ samplers =+ ImplVulkan.IMGUI_IMPL_VULKAN_MINIMUM_SAMPLER_POOL_SIZE poolCI = zero { Vk.flags = Vk.DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT , Vk.maxSets =- 8+ sampledImages + samplers , Vk.poolSizes =- Vector.fromList $- map (uncurry Vk.DescriptorPoolSize) dsSizes+ Vector.fromList+ [ Vk.DescriptorPoolSize Vk.DESCRIPTOR_TYPE_SAMPLED_IMAGE sampledImages+ , Vk.DescriptorPoolSize Vk.DESCRIPTOR_TYPE_SAMPLER samplers+ ] } -dsSizes :: [(Vk.DescriptorType, Word32)]-dsSizes = map (, 128)- [ Vk.DESCRIPTOR_TYPE_SAMPLER- , Vk.DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER- , Vk.DESCRIPTOR_TYPE_SAMPLED_IMAGE- , Vk.DESCRIPTOR_TYPE_STORAGE_IMAGE- , Vk.DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER- , Vk.DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER- , Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER- , Vk.DESCRIPTOR_TYPE_STORAGE_BUFFER- , Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC- , Vk.DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC- , Vk.DESCRIPTOR_TYPE_INPUT_ATTACHMENT- ]+foreign import ccall "wrapper"+ wrapCheckResult :: (Vk.Result -> IO ()) -> IO (FunPtr (Vk.Result -> IO ())) +logBackendResult :: (MonadReader env m, MonadIO m, HasLogFunc env) => Vk.Result -> m ()+logBackendResult result+ | result < Vk.SUCCESS = logError message+ | otherwise = logWarn message+ where+ message = "DearImGui Vulkan backend: " <> displayShow result+ allocateLoop :: Bool -> ResourceT (StageRIO st) () allocateLoop installCallbacks = do lift $ beforeLoop installCallbacks@@ -183,7 +189,7 @@ {- | Initialize context to serve the draws in the current render loop. -You will need to trigger callbacks from DearImGui.GLFW if you're opting out of them here.+You will need to trigger callbacks from DearImGui.Impl.GLFW if you're opting out of them here. DearImgui will run your previously installed GLFW callbacks. -}@@ -191,31 +197,34 @@ beforeLoop installCallbacks = do logDebug "glfwInitForVulkan" window <- asks $ ghWindow . appEnv- success <- glfwInitForVulkan window installCallbacks+ success <- liftIO $ ImplGLFW.initForVulkan window installCallbacks unless success do logWarn "glfwInitForVulkan failed" afterLoop :: StageRIO st () afterLoop = do logDebug "glfwShutdown"- asks getDevice >>= Vk.deviceWaitIdle+ waitDeviceIdle logDebug "glfwShutdown: Device idle"- glfwShutdown+ liftIO RawGLFW.shutdown logDebug "glfwShutdown finished" mkDrawData :: MonadIO m => m a -> m (a, ImGui.DrawData) mkDrawData action = do- ImGui.vulkanNewFrame- glfwNewFrame+ liftIO do+ ImplVulkan.newFrame+ RawGLFW.newFrame ImGui.newFrame result <- action ImGui.render drawData <- ImGui.getDrawData pure (result, drawData) -draw :: MonadIO m => ImGui.DrawData -> Vk.CommandBuffer -> m ()+draw :: MonadVulkan env m => ImGui.DrawData -> Vk.CommandBuffer -> m () draw drawData commandBuffer = do- ImGui.vulkanRenderDrawData drawData commandBuffer Nothing+ graphics <- asks $ qGraphics . getQueues+ withQueue graphics \_queue ->+ liftIO $ ImplVulkan.renderDrawData drawData (Vk.commandBufferHandle commandBuffer) Vk.NULL_HANDLE capturingKeyboard :: MonadIO m => m () -> m () capturingKeyboard action =@@ -226,31 +235,30 @@ ImGui.wantCaptureMouse >>= (`unless` action) renderWith- :: HasRenderPass renderpass- => (t -> renderpass)- -> "subpass index" ::: Word32+ :: HasRendering target+ => (t -> target) -> Stage.Rendering t p st -> Stage.Rendering t p st-renderWith getRP subpassIx Stage.Rendering{..} = Stage.Rendering- { rAllocateRP- , rAllocateP = \swapchain rps -> do- void $! allocate swapchain (getRP rps) subpassIx- rAllocateP swapchain rps+renderWith getTarget Stage.Rendering{..} = Stage.Rendering+ { rAllocateTargets+ , rAllocateP = \swapchain rts -> do+ void $! allocate swapchain (getTarget rts)+ rAllocateP swapchain rts } -- | Run a full-screen layout using ImGui coordinates (origin at top-left) {-# INLINE rootLayout #-} rootLayout- :: (LayoutView stuff, Placed stuff ~ StageFrameRIO rp p rr st ())+ :: (LayoutView stuff, Placed stuff ~ StageFrameRIO rt p rr st ()) => View () stuff- -> StageFrameRIO rp p rr st ()+ -> StageFrameRIO rt p rr st () rootLayout viewTree = do root <- askWindowBox View.foldWith id root viewTree -- | Get a window box using ImGui coordinates (origin at top-left) {-# INLINE askWindowBox #-}-askWindowBox :: StageFrameRIO rp p rr st Box+askWindowBox :: StageFrameRIO rt p rr st Box askWindowBox = do Window.Size{window} <- mapRIO fst askWindowSize >>= Worker.getOutputData pure Box{size=window, position=window/2}
src/Render/ImGui/Widget.hs view
@@ -15,18 +15,19 @@ -- import Data.These (These(..)) -- import Data.These.Combinators (justHere, justThere) import DearImGui.Internal.Text (withCString)-import DearImGui.Raw qualified+import DearImGui.Raw.Enums.ImGuiSliderFlags qualified as ImGuiSliderFlags+import DearImGui.Raw.ImGui qualified+import DearImGui.Raw.ImVec2 (ImVec2(..)) import Engine.ReactiveBanana.Widget (Widget(..), Widget', draw_) import Foreign (withForeignPtr, mallocForeignPtr) import Foreign qualified-import Foreign.C.Types (CFloat(..)) import Geomancy (Vec2, UVec2) import Reactive.Banana qualified as RB import Reactive.Banana.Frameworks qualified as RBF import DearImGui qualified separator :: Widget Void ()-separator = draw_ DearImGui.Raw.separator+separator = draw_ DearImGui.Raw.ImGui.separator sliderFloat :: MonadIO m@@ -39,7 +40,7 @@ ref <- mallocForeignPtr withForeignPtr ref \ptr ->- Foreign.poke ptr (CFloat initial)+ Foreign.poke ptr initial pure Widget { plug = do@@ -49,14 +50,15 @@ , draw = withCString label \labelPtr -> -- FIXME: make this static withForeignPtr ref \refPtr -> do- changed <- DearImGui.Raw.sliderFloat+ changed <- DearImGui.Raw.ImGui.sliderFloat labelPtr refPtr- (CFloat start)- (CFloat end)+ start+ end Foreign.nullPtr- when changed do- CFloat current <- Foreign.peek refPtr+ ImGuiSliderFlags.None+ when (Foreign.toBool changed) do+ current <- Foreign.peek refPtr fire current } @@ -81,13 +83,14 @@ , draw = withCString label \labelPtr -> -- FIXME: make this static withForeignPtr ref \refPtr -> do- changed <- DearImGui.Raw.sliderFloat2+ changed <- DearImGui.Raw.ImGui.sliderFloat2 labelPtr (Foreign.castPtr refPtr)- (CFloat start)- (CFloat end)+ start+ end Foreign.nullPtr- when changed do+ ImGuiSliderFlags.None+ when (Foreign.toBool changed) do current <- Foreign.peek refPtr fire current }@@ -103,7 +106,7 @@ ref <- mallocForeignPtr withForeignPtr ref \ptr ->- Foreign.poke ptr (CFloat initial)+ Foreign.poke ptr initial pure Widget { plug = do@@ -113,15 +116,15 @@ , draw = withCString label \labelPtr -> -- FIXME: make this static withForeignPtr ref \refPtr -> do- changed <- DearImGui.Raw.sliderAngle+ changed <- DearImGui.Raw.ImGui.sliderAngle labelPtr refPtr- (CFloat start)- (CFloat end)+ start+ end Foreign.nullPtr- DearImGui.Raw.ImGuiSliderFlags_None- when changed do- CFloat current <- Foreign.peek refPtr+ ImGuiSliderFlags.None+ when (Foreign.toBool changed) do+ current <- Foreign.peek refPtr fire current } @@ -146,14 +149,14 @@ , draw = withCString label \labelPtr -> -- FIXME: make this static withForeignPtr ref \refPtr -> do- changed <- DearImGui.Raw.sliderInt2+ changed <- DearImGui.Raw.ImGui.sliderInt2 labelPtr (Foreign.castPtr refPtr) (fromIntegral start) (fromIntegral end) Foreign.nullPtr- DearImGui.Raw.ImGuiSliderFlags_None- when changed do+ ImGuiSliderFlags.None+ when (Foreign.toBool changed) do current <- Foreign.peek refPtr fire current }@@ -170,8 +173,8 @@ pure (event, pure ()) , draw = withCString label \labelPtr -> do- clicked <- DearImGui.Raw.button labelPtr- when clicked do+ clicked <- DearImGui.Raw.ImGui.button labelPtr (ImVec2 0 0)+ when (Foreign.toBool clicked) do fire () }
src/Render/ImGui/Window.hs view
@@ -10,10 +10,13 @@ import Data.Bits ((.|.)) import Data.ByteString.Unsafe (unsafeInit, unsafeUseAsCString)-import DearImGui.Raw qualified as Raw-import Foreign (castPtr, free, new, nullPtr, peek, with)-import Foreign.C.Types (CFloat(..))-import Geomancy (Vec2, vec2)+import DearImGui.Raw.Enums.ImGuiCond qualified as ImGuiCond+import DearImGui.Raw.Enums.ImGuiWindowFlags (ImGuiWindowFlags)+import DearImGui.Raw.Enums.ImGuiWindowFlags qualified as ImGuiWindowFlags+import DearImGui.Raw.ImGui qualified as Raw+import DearImGui.Raw.ImVec2 (ImVec2(..))+import Foreign (free, new, nullFunPtr, nullPtr, peek, toBool)+import Geomancy (Vec2, withVec2) import Geomancy.Layout.Box (Box(..)) import Geomancy.Layout.View as LV import GHC.OverloadedLabels (IsLabel(..))@@ -28,7 +31,7 @@ , focus :: Bool -- ^ Set focus state. , scroll :: Maybe Vec2 -- ^ Set scrolling value (use < 0.0f to not affect a given axis). , bgAlpha :: Maybe Float -- ^ Set background color alpha. helper to easily override the Alpha component of ImGuiCol_WindowBg, ChildBg, PopupBg. you may also use ImGuiWindowFlags_NoBackground.- , flags :: Raw.ImGuiWindowFlags -- ^ Set window flags.+ , flags :: ImGuiWindowFlags -- ^ Set window flags. , onClose :: Maybe (IO ()) -- ^ Allow window to be closed and set a callback. } @@ -39,7 +42,7 @@ fromString = pack . fromString instance KnownSymbol label => IsLabel label Window where- fromLabel = base{flags = base.flags .|. Raw.ImGuiWindowFlags_NoTitleBar}+ fromLabel = base{flags = base.flags .|. ImGuiWindowFlags.NoTitleBar} where base = pack . fromString . ("##" ++) . symbolVal $ Proxy @label @@ -52,7 +55,7 @@ , scroll = Nothing , focus = False , bgAlpha = Nothing- , flags = Raw.ImGuiWindowFlags_NoSavedSettings+ , flags = ImGuiWindowFlags.NoSavedSettings , onClose = Nothing } @@ -81,33 +84,31 @@ let flags = config.flags .|.- Raw.ImGuiWindowFlags_NoMove .|.- Raw.ImGuiWindowFlags_NoResize .|.- Raw.ImGuiWindowFlags_NoCollapse+ ImGuiWindowFlags.NoMove .|.+ ImGuiWindowFlags.NoResize .|.+ ImGuiWindowFlags.NoCollapse run (config{flags}) action run :: MonadUnliftIO m => Window -> m () -> m () run window action = do liftIO do- for_ window.contentSize \size ->- with size $ Raw.setNextWindowContentSize . castPtr+ for_ window.contentSize $+ Raw.setNextWindowContentSize . imVec2 for_ (window.sizeConstraints <|> (Just (-1, -1))) \(minSize, maxSize) ->- with minSize \minPtr ->- with maxSize \maxPtr ->- Raw.setNextWindowSizeConstraints (castPtr minPtr) (castPtr maxPtr)+ Raw.setNextWindowSizeConstraints (imVec2 minSize) (imVec2 maxSize) nullFunPtr nullPtr for_ window.collapsed $ \collapsed ->- Raw.setNextWindowCollapsed (bool 0 1 collapsed) Raw.ImGuiCond_Always+ Raw.setNextWindowCollapsed (bool 0 1 collapsed) ImGuiCond.Always when window.focus Raw.setNextWindowFocus- for_ window.scroll \scroll ->- with scroll $ Raw.setNextWindowScroll . castPtr- for_ window.bgAlpha $- Raw.setNextWindowBgAlpha . CFloat+ for_ window.scroll $+ Raw.setNextWindowScroll . imVec2+ for_ window.bgAlpha+ Raw.setNextWindowBgAlpha bracket ( liftIO do open_ <- for window.onClose \callback -> (callback,) <$> new 1 unsafeUseAsCString window.name0 \namePtr ->- (open_,) <$> Raw.begin namePtr (Just $ maybe nullPtr snd open_) (Just window.flags)+ (open_,) . toBool <$> Raw.begin namePtr (maybe nullPtr snd open_) window.flags ) ( \(open_, _) -> liftIO do case open_ of@@ -123,8 +124,8 @@ {-# INLINE setNextWindowBox #-} setNextWindowBox :: MonadIO m => Box -> m () setNextWindowBox box = liftIO do- with box.position \pos ->- with (vec2 0.5 0.5) \pivot ->- Raw.setNextWindowPos (castPtr pos) Raw.ImGuiCond_Always (Just $ castPtr pivot)- with box.size \size ->- Raw.setNextWindowSize (castPtr size) Raw.ImGuiCond_Always+ Raw.setNextWindowPos (imVec2 box.position) ImGuiCond.Always (ImVec2 0.5 0.5)+ Raw.setNextWindowSize (imVec2 box.size) ImGuiCond.Always++imVec2 :: Vec2 -> ImVec2+imVec2 v = withVec2 v ImVec2