Skip to content

Commit 5a972ef

Browse files
authored
Merge pull request #25 from alt-romes/wip/romes/dear-imgui
Basic DearImGui support
2 parents d5eb20b + 1800a9f commit 5a972ef

13 files changed

Lines changed: 253 additions & 197 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Solid examples:
2222
* `simple-camera` example of camera with projection
2323
* `full-pipeline` a full shader pipeline example ported from [FIR](https://gitlab.com/sheaf/fir)
2424
* `function-drawing` attempt at drawing functions using shaders
25+
* `dear-imgui` is a trivial demo which setups and renders the DearImGui demo window
2526

2627
Experimental examples:
2728

examples/book-of-shaders/shaping-functions/Main.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
{-# LANGUAGE OverloadedRecordDot #-}
1+
{-#LANGUAGE OverloadedRecordDot #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE UndecidableInstances #-}
44
{-# LANGUAGE PartialTypeSignatures #-}
55
{-# LANGUAGE PostfixOperators #-}
66
{-# OPTIONS_GHC -Wno-name-shadowing #-}
77
module Main where
88

9-
import Geomancy.Vec3
109
import Ghengin.Core
11-
import Ghengin.Core.Mesh
12-
import Ghengin.Core.Material
1310
import Ghengin.Core.Prelude as Linear
1411
import Ghengin.Core.Render
1512
import Ghengin.Core.Render.Pipeline
@@ -55,7 +52,7 @@ main = do
5552

5653
(rp1, rp2) <- (Alias.share =<< createSimpleRenderPass )
5754

58-
pipeline <- (makeRenderPipelineWith GPS{cullMode=CullBack} rp1 shaderPipelineSimple GHNil )
55+
pipeline <- (makeRenderPipelineWith defaultGraphicsPipelineSettings{cullMode=CullBack} rp1 shaderPipelineSimple GHNil )
5956
(rq, Ur pkey) <- pure (insertPipeline pipeline LMon.mempty)
6057

6158
rq <- gameLoop rp2 rq

examples/book-of-shaders/shaping-functions/Shaders.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ vertexSimple = shader do
5050
let ux = convert ((vix `shiftL` (1 :: Code Word32)) .&. 2)
5151
uy = convert (vix .&. 2)
5252
#out_uv .= Vec2 ux uy
53-
#gl_Position .= Vec4 (ux*2 - 1) (- (uy*2 - 1) {- in Vulkan, y points down -}) 0 1
53+
#gl_Position .= Vec4 (ux*2 - 1) (- (uy*2 - 1) {- in Vulkan, y points down -}) 0.1 1
5454

5555
plot :: Code Float -> Code Float -> Code Float
5656
plot y pct = smoothstep (pct-0.005) pct y - smoothstep pct (pct+0.005) y

examples/book-of-shaders/uniforms/Main.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
{-# OPTIONS_GHC -Wno-name-shadowing #-}
77
module Main where
88

9-
import Geomancy.Vec3
109
import Ghengin.Core
11-
import Ghengin.Core.Mesh
12-
import Ghengin.Core.Material
1310
import Ghengin.Core.Prelude as Linear
1411
import Ghengin.Core.Render
1512
import Ghengin.Core.Render.Pipeline
@@ -55,7 +52,7 @@ main = do
5552

5653
(rp1, rp2) <- (Alias.share =<< createSimpleRenderPass )
5754

58-
pipeline <- (makeRenderPipelineWith GPS{cullMode=CullFront} rp1 shaderPipelineSimple GHNil )
55+
pipeline <- (makeRenderPipelineWith defaultGraphicsPipelineSettings{cullMode=CullFront} rp1 shaderPipelineSimple GHNil )
5956
(rq, Ur pkey) <- pure (insertPipeline pipeline LMon.mempty)
6057

6158
rq <- gameLoop rp2 rq

examples/book-of-shaders/uniforms/Shaders.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ vertexSimple = shader do
5050
let ux = convert ((vix `shiftL` (1 :: Code Word32)) .&. 2)
5151
uy = convert (vix .&. 2)
5252
#out_uv .= Vec2 ux uy
53-
#gl_Position .= Vec4 (ux*2 - 1) (uy*2 - 1) 0 1
53+
#gl_Position .= Vec4 (ux*2 - 1) (uy*2 - 1) 0.1 1
5454

5555
fragmentSimple :: G.FragmentShaderModule '[
5656
"in_uv" ':-> Input '[ Location 0 ] (V 2 Float)

examples/dear-imgui/Main.hs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{-# LANGUAGE OverloadedRecordDot #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE UndecidableInstances #-}
4+
{-# LANGUAGE PartialTypeSignatures #-}
5+
{-# LANGUAGE PostfixOperators #-}
6+
{-# OPTIONS_GHC -Wno-name-shadowing #-}
7+
module Main where
8+
9+
import Ghengin.Core
10+
import Ghengin.Core.Prelude as Linear
11+
import Ghengin.Core.Render
12+
import Ghengin.Core.Render.Pipeline
13+
import Ghengin.Core.Render.Queue
14+
import qualified Data.Monoid.Linear as LMon
15+
import qualified Prelude
16+
import qualified Data.Linear.Alias as Alias
17+
import Shaders
18+
19+
import qualified Ghengin.DearImGui.Vulkan as ImGui
20+
21+
--------------------------------------------------------------------------------
22+
-- Bufferless rendering as seen in:
23+
-- https://www.saschawillems.de/blog/2016/08/13/vulkan-tutorial-on-rendering-a-fullscreen-quad-without-buffers/
24+
--------------------------------------------------------------------------------
25+
26+
gameLoop :: Alias RenderPass RenderQueue () Core (RenderQueue ())
27+
gameLoop rp rq = Linear.do
28+
should_close <- (shouldCloseWindow )
29+
if should_close then (Alias.forget rp ) >> return rq else Linear.do
30+
(pollWindowEvents )
31+
32+
-- Prepare Imgui data
33+
ImGui.withNewFrame $ do
34+
35+
ImGui.showDemoWindow
36+
37+
(rp, rq) <- renderWith $ Linear.do
38+
39+
(rp1, rp2) <- lift (Alias.share rp)
40+
41+
Ur extent <- lift getRenderExtent
42+
43+
renderPassCmd extent rp1 $ Linear.do
44+
45+
rq <- renderQueueCmd rq
46+
draw 3
47+
48+
-- Render Imgui data!
49+
ImGui.renderDrawData
50+
51+
return (rp2, rq)
52+
53+
gameLoop rp rq
54+
55+
main :: Prelude.IO ()
56+
main = do
57+
withLinearIO $
58+
runCore (width, height) Linear.do
59+
60+
(rp1, rp2) <- (Alias.share =<< createSimpleRenderPass )
61+
62+
-- Init imgui
63+
(rp1, imctx) <- (Alias.useM rp1 ImGui.initImGui )
64+
65+
pipeline <- (makeRenderPipelineWith defaultGraphicsPipelineSettings{cullMode=CullFront} rp1 shaderPipelineSimple GHNil )
66+
(rq, Ur pkey) <- pure (insertPipeline pipeline LMon.mempty)
67+
68+
rq <- gameLoop rp2 rq
69+
70+
(freeRenderQueue rq )
71+
-- Then destroy it
72+
(ImGui.destroyImCtx imctx )
73+
74+
return (Ur ())
75+
76+
77+

examples/dear-imgui/Shaders.hs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{-# OPTIONS_GHC -Wno-missing-local-signatures #-}
2+
3+
{-# LANGUAGE CPP #-}
4+
{-# LANGUAGE BlockArguments #-}
5+
{-# LANGUAGE DataKinds #-}
6+
{-# LANGUAGE FlexibleContexts #-}
7+
{-# LANGUAGE PartialTypeSignatures #-}
8+
{-# LANGUAGE OverloadedLabels #-}
9+
{-# LANGUAGE RebindableSyntax #-}
10+
{-# LANGUAGE ScopedTypeVariables #-}
11+
{-# LANGUAGE TypeApplications #-}
12+
{-# LANGUAGE NamedWildCards #-}
13+
{-# LANGUAGE TypeFamilies #-}
14+
{-# LANGUAGE TypeOperators #-}
15+
{-# LANGUAGE ViewPatterns #-}
16+
17+
module Shaders where
18+
19+
import qualified Prelude
20+
21+
-- fir
22+
import FIR
23+
import FIR.Syntax.Labels
24+
import Math.Linear
25+
26+
-- ghengin
27+
import qualified Ghengin.Core.Shader as G
28+
29+
width, height :: Prelude.Int
30+
(width, height) = (640, 480)
31+
32+
--------------------------------------------------------------------------------
33+
-- Book of Shader: Uniforms
34+
--
35+
-- Bufferless rendering as seen in:
36+
-- https://www.saschawillems.de/blog/2016/08/13/vulkan-tutorial-on-rendering-a-fullscreen-quad-without-buffers/
37+
--------------------------------------------------------------------------------
38+
39+
shaderPipelineSimple :: G.ShaderPipeline _
40+
shaderPipelineSimple
41+
= G.ShaderPipeline (StructInput @'[] @(Triangle List))
42+
G.:>-> vertexSimple
43+
G.:>-> fragmentSimple
44+
45+
vertexSimple :: G.VertexShaderModule '[
46+
"out_uv" ':-> Output '[ Location 0 ] (V 2 Float)
47+
] _
48+
vertexSimple = shader do
49+
vix <- #gl_VertexIndex
50+
let ux = convert ((vix `shiftL` (1 :: Code Word32)) .&. 2)
51+
uy = convert (vix .&. 2)
52+
#out_uv .= Vec2 ux uy
53+
#gl_Position .= Vec4 (ux*2 - 1) (uy*2 - 1) 0.1 1
54+
55+
fragmentSimple :: G.FragmentShaderModule '[
56+
"in_uv" ':-> Input '[ Location 0 ] (V 2 Float)
57+
] _
58+
fragmentSimple = shader do
59+
~(Vec2 ix iy) <- #in_uv
60+
#out_colour .= Vec4 ix iy 0 1
61+

examples/ghengin-games.cabal

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ executable function-drawing
226226
other-modules: Shaders
227227
hs-source-dirs: function-drawing/
228228

229+
-- DearImGui basic demo
230+
executable dear-imgui
231+
import: common-flags
232+
main-is: Main.hs
233+
other-modules: Shaders
234+
hs-source-dirs: dear-imgui
235+
build-depends: ghengin:dear-imgui
236+
229237
--------------------------------------------------------------------------------
230238
-- Experiments
231239
--------------------------------------------------------------------------------

examples/oscilloscope/Shaders.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ vertexSimple = shader do
6464
let ux = convert ((vix `shiftL` (1 :: Code Word32)) .&. 2)
6565
uy = convert (vix .&. 2)
6666
#out_uv .= Vec2 ux uy
67-
#gl_Position .= Vec4 (ux*2 - 1) (- (uy*2 - 1) {- in Vulkan, y points down -}) 0 1
67+
#gl_Position .= Vec4 (ux*2 - 1) (- (uy*2 - 1) {- in Vulkan, y points down -}) 0.1 1
6868

6969
--------------------------------------------------------------------------------
7070
-- Fragment

ghengin-core/ghengin-vulkan/Ghengin/Vulkan/Renderer/Command.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ module Ghengin.Vulkan.Renderer.Command
5252
, transitionImageLayout
5353

5454
, clearColorImage
55+
56+
-- * Unsafe bits
57+
, unsafeCmd
58+
, unsafeCmd_
59+
, unsafeRenderPassCmd
60+
, unsafeRenderPassCmd_
5561
) where
5662

5763
import GHC.TypeLits

0 commit comments

Comments
 (0)