Skip to content

Commit

Permalink
feat: Add some "global" types
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed Jun 25, 2023
1 parent d821075 commit 91035be
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 10 deletions.
3 changes: 3 additions & 0 deletions nix/hix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
]);

shell = {
# Speed up local builds a bit
crossPlatforms = _: [];

tools = {
cabal = "latest";
fourmolu = "latest";
Expand Down
17 changes: 14 additions & 3 deletions seangine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ common common-options
build-depends:
base ^>= 4.16.0.0,
relude
default-extensions:
DerivingStrategies
RecordWildCards
default-language: GHC2021

library
import: common-options
exposed-modules:
Graphics.Seangine
build-depends: base ^>=4.16.0.0
Graphics.Seangine,
Graphics.Seangine.Types,
Graphics.Seangine.Types.Config,
Graphics.Seangine.Types.R
build-depends:
VulkanMemoryAllocator,
resourcet,
unliftio-core,
vector,
vulkan
hs-source-dirs: src
default-language: GHC2021

Expand All @@ -57,5 +69,4 @@ test-suite seangine-test
hs-source-dirs: test
main-is: Main.hs
build-depends:
base ^>=4.16.0.0,
seangine
13 changes: 6 additions & 7 deletions src/Graphics/Seangine/Types.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module Graphics.Seangine.Types (SeangineOptions (..)) where
module Graphics.Seangine.Types
( module Graphics.Seangine.Types.Config,
module Graphics.Seangine.Types.R,
) where

data SeangineOptions = SeangineOptions
{ optFile :: !FilePath,
optDevice :: !(Maybe String),
optDebug :: !Bool
}
deriving (Eq)
import Graphics.Seangine.Types.Config
import Graphics.Seangine.Types.R
84 changes: 84 additions & 0 deletions src/Graphics/Seangine/Types/Config.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module Graphics.Seangine.Types.Config
( SeangineConfig (..),
SeangineOptions (..),
VulkanHandles (..),
HasSeangineOptions (..),
HasVulkanHandles (..),
) where

import Data.Vector (Vector)
import Vulkan.Core10
import Vulkan.Extensions.VK_KHR_surface
( PresentModeKHR,
SurfaceCapabilitiesKHR,
SurfaceFormatKHR,
)
import VulkanMemoryAllocator (Allocator (..))

data SeangineConfig = SeangineConfig
{ options :: SeangineOptions,
handles :: VulkanHandles
}

data SeangineOptions = SeangineOptions
{ optFile :: !FilePath,
optDevice :: !(Maybe String),
optDebug :: !Bool
}
deriving (Eq)

data VulkanHandles = VulkanHandles
{ vhDataDir :: FilePath,
vhInstance :: Instance,
vhPhysicalDevice :: PhysicalDevice,
vhDevice :: Device,
vhAllocator :: Allocator,
vhGraphicsQueue :: Queue,
vhGraphicsQueueFamily :: Word32,
vhPresentQueue :: Queue,
vhPresentQueueFamily :: Word32,
vhSurfaceCapabilities :: SurfaceCapabilitiesKHR,
vhSurfaceFormats :: Vector SurfaceFormatKHR,
vhPresentModes :: Vector PresentModeKHR,
vhCommandPool :: CommandPool
}

class HasSeangineOptions m where
getOptFile :: m FilePath
getOptDevice :: m (Maybe String)
getOptDebug :: m Bool

class HasVulkanHandles m where
getDataDir :: m FilePath
getInstance :: m Instance
getPhysicalDevice :: m PhysicalDevice
getDevice :: m Device
getAllocator :: m Allocator
getGraphicsQueue :: m Queue
getGraphicsQueueFamily :: m Word32
getPresentQueue :: m Queue
getPresentQueueFamily :: m Word32
getSurfaceCapabilities :: m SurfaceCapabilitiesKHR
getSurfaceFormats :: m (Vector SurfaceFormatKHR)
getPresentModes :: m (Vector PresentModeKHR)
getCommandPool :: m CommandPool

instance (Monad m, HasSeangineOptions m) => HasSeangineOptions (ReaderT r m) where
getOptFile = lift getOptFile
getOptDevice = lift getOptDevice
getOptDebug = lift getOptDebug

instance (Monad m, HasVulkanHandles m) => HasVulkanHandles (ReaderT r m) where
getDataDir = lift getDataDir
getInstance = lift getInstance
getPhysicalDevice = lift getPhysicalDevice
getDevice = lift getDevice
getAllocator = lift getAllocator
getGraphicsQueue = lift getGraphicsQueue
getGraphicsQueueFamily = lift getGraphicsQueueFamily
getPresentQueue = lift getPresentQueue
getPresentQueueFamily = lift getPresentQueueFamily
getSurfaceCapabilities = lift getSurfaceCapabilities
getSurfaceFormats = lift getSurfaceFormats
getPresentModes = lift getPresentModes
getCommandPool = lift getCommandPool
27 changes: 27 additions & 0 deletions src/Graphics/Seangine/Types/R.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Graphics.Seangine.Types.R
( R (..),
runR,
) where

import Graphics.Seangine.Types.Config

import Control.Monad.IO.Unlift (MonadUnliftIO (..))
import Control.Monad.Trans.Resource

newtype R a = R {unR :: ReaderT SeangineConfig (ResourceT IO) a}
deriving newtype
( Functor,
Applicative,
Monad,
MonadReader SeangineConfig,
MonadResource,
MonadIO,
MonadFail
)

instance MonadUnliftIO R where
withRunInIO :: ((forall a. R a -> IO a) -> IO b) -> R b
withRunInIO a = R $ withRunInIO (\r -> a $ r . unR)

runR :: SeangineConfig -> R a -> ResourceT IO a
runR config = usingReaderT config . unR

0 comments on commit 91035be

Please sign in to comment.