Skip to content

Commit

Permalink
Merge pull request #202 from expipiplus1/joe-semaphore
Browse files Browse the repository at this point in the history
More helpers in vulkan-utils and some bug fixes in vulkan
  • Loading branch information
expipiplus1 authored Nov 16, 2020
2 parents e426fc4 + 474ea37 commit db6c581
Show file tree
Hide file tree
Showing 139 changed files with 1,284 additions and 216 deletions.
4 changes: 2 additions & 2 deletions VulkanMemoryAllocator/VulkanMemoryAllocator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 2.2
--
-- see: https://github.com/sol/hpack
--
-- hash: fc6e88640fbfd21d257ac23c8012464900c1691882a5213592c472a5c787ac03
-- hash: 85c6abee27ec9e1ff6046b5a20bbe8324856a6d97fe197fbede65cb24c461ff0

name: VulkanMemoryAllocator
version: 0.3.8
version: 0.3.9
synopsis: Bindings to the VulkanMemoryAllocator library
category: Graphics
homepage: https://github.com/expipiplus1/vulkan#readme
Expand Down
4 changes: 4 additions & 0 deletions VulkanMemoryAllocator/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## WIP

## [0.3.9] - 2020-11-15

- Derive `FiniteBits` for bitmasks

## [0.3.8] - 2020-11-12

- Bump VMA
Expand Down
2 changes: 1 addition & 1 deletion VulkanMemoryAllocator/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: VulkanMemoryAllocator
version: "0.3.8"
version: "0.3.9"
synopsis: Bindings to the VulkanMemoryAllocator library
category: Graphics
maintainer: Joe Hermaszewski <[email protected]>
Expand Down
11 changes: 6 additions & 5 deletions VulkanMemoryAllocator/src/VulkanMemoryAllocator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ import Vulkan.Zero (Zero)
import Vulkan.Zero (Zero(..))
import Control.Monad.IO.Class (MonadIO)
import Data.Bits (Bits)
import Data.Bits (FiniteBits)
import Data.Typeable (Typeable)
import Foreign.C.Types (CChar)
import Foreign.C.Types (CSize)
Expand Down Expand Up @@ -2583,7 +2584,7 @@ instance Zero DeviceMemoryCallbacks where

-- | Flags for created 'Allocator'.
newtype AllocatorCreateFlagBits = AllocatorCreateFlagBits Flags
deriving newtype (Eq, Ord, Storable, Zero, Bits)
deriving newtype (Eq, Ord, Storable, Zero, Bits, FiniteBits)

-- | Allocator and all objects created from it will not be synchronized
-- internally, so you must guarantee they are used from only one thread at
Expand Down Expand Up @@ -2870,7 +2871,7 @@ instance Zero VulkanFunctions where

-- | Flags to be used in /VmaRecordSettings::flags/.
newtype RecordFlagBits = RecordFlagBits Flags
deriving newtype (Eq, Ord, Storable, Zero, Bits)
deriving newtype (Eq, Ord, Storable, Zero, Bits, FiniteBits)

-- | Enables flush after recording every function call.
--
Expand Down Expand Up @@ -3584,7 +3585,7 @@ instance Read MemoryUsage where

-- | Flags to be passed as /VmaAllocationCreateInfo::flags/.
newtype AllocationCreateFlagBits = AllocationCreateFlagBits Flags
deriving newtype (Eq, Ord, Storable, Zero, Bits)
deriving newtype (Eq, Ord, Storable, Zero, Bits, FiniteBits)

-- | Set this flag if the allocation should have its own memory block.
--
Expand Down Expand Up @@ -3845,7 +3846,7 @@ instance Zero AllocationCreateInfo where

-- | Flags to be passed as /VmaPoolCreateInfo::flags/.
newtype PoolCreateFlagBits = PoolCreateFlagBits Flags
deriving newtype (Eq, Ord, Storable, Zero, Bits)
deriving newtype (Eq, Ord, Storable, Zero, Bits, FiniteBits)

-- | Use this flag if you always allocate only buffers and linear images or
-- only optimal images out of this pool and so Buffer-Image Granularity can
Expand Down Expand Up @@ -4259,7 +4260,7 @@ instance Show DefragmentationContext where
-- | Flags to be used in 'defragmentationBegin'. None at the moment. Reserved
-- for future use.
newtype DefragmentationFlagBits = DefragmentationFlagBits Flags
deriving newtype (Eq, Ord, Storable, Zero, Bits)
deriving newtype (Eq, Ord, Storable, Zero, Bits, FiniteBits)


pattern DEFRAGMENTATION_FLAG_INCREMENTAL = DefragmentationFlagBits 0x00000001
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## WIP

## [3.6.14] - 2020-11-15

- Add `FiniteBits` instance for Flags
- Fix getting function pointers for functions which have aliases (those which
have been promoted to core versions mostly)

## [3.6.13] - 2020-11-09
- Bump API version to v1.2.160

Expand Down
3 changes: 3 additions & 0 deletions examples/hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ cradle:

- path: "./resize/"
component: "exe:resize"

- path: "./timeline-semaphore/"
component: "exe:timeline-semaphore"
26 changes: 22 additions & 4 deletions examples/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ executables:
- vulkan
- vulkan-utils

timeline-semaphore:
main: Main.hs
source-dirs: timeline-semaphore
dependencies:
- VulkanMemoryAllocator
- autoapply >= 0.4
- base <5
- bytestring
- extra
- resourcet
- safe-exceptions
- say
- text
- transformers
- unliftio
- vector
- vulkan
- vulkan-utils

when:
- condition: os(windows)
ghc-options: -optl-mconsole
Expand All @@ -117,10 +136,11 @@ flags:
manual: yes

default-extensions:
- AllowAmbiguousTypes
- DataKinds
- DefaultSignatures
- DeriveAnyClass
- DeriveFoldable
- DeriveFunctor
- DeriveTraversable
- DerivingStrategies
- DuplicateRecordFields
- FlexibleContexts
Expand All @@ -133,7 +153,6 @@ default-extensions:
- NoMonomorphismRestriction
- NumDecimals
- OverloadedStrings
- PartialTypeSignatures
- PatternSynonyms
- PolyKinds
- QuantifiedConstraints
Expand All @@ -148,7 +167,6 @@ default-extensions:
- TypeFamilyDependencies
- TypeOperators
- TypeSynonymInstances
- UndecidableInstances
- ViewPatterns

ghc-options:
Expand Down
174 changes: 174 additions & 0 deletions examples/timeline-semaphore/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-missing-signatures #-}

module Main
( main
) where

import AutoApply
import Control.Exception ( throwIO )
import Control.Monad ( guard )
import Control.Monad.IO.Class
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Resource
import Data.Vector ( Vector )
import Data.Word
import GHC.Exception ( SomeException )
import Say
import UnliftIO ( Exception(displayException)
, catch
)
import Vulkan.CStruct.Extends
import Vulkan.Core10
import Vulkan.Core12
import Vulkan.Core12.Promoted_From_VK_KHR_timeline_semaphore
as Timeline
import Vulkan.Exception
import Vulkan.Extensions.VK_KHR_get_physical_device_properties2
import Vulkan.Extensions.VK_KHR_timeline_semaphore
import Vulkan.Utils.Initialization
import Vulkan.Utils.QueueAssignment
import Vulkan.Zero

noAllocationCallbacks :: Maybe AllocationCallbacks
noAllocationCallbacks = Nothing

--
-- Wrap a bunch of Vulkan commands so that they automatically pull global
-- handles from 'V'
--
-- Wrapped functions are suffixed with "'"
--
autoapplyDecs
(<> "'")
[ 'noAllocationCallbacks
]
-- Allocate doesn't subsume the continuation type on the "with" commands, so
-- put it in the unifying group.
[ 'allocate ]
[ 'deviceWaitIdle
, 'withSemaphore
, 'Timeline.waitSemaphores
]

main :: IO ()
main = runResourceT . traceException $ do
inst <- Main.createInstance
(_phys, dev, MyQueues computeQueue) <- Main.createDevice inst
timelineTest dev computeQueue

traceException :: MonadUnliftIO m => m a -> m a
traceException m =
m
`catch` (\(e :: SomeException) ->
sayErrString (displayException e) >> liftIO (throwIO e)
)

timelineTest :: (MonadResource m) => Device -> Queue -> m ()
timelineTest dev computeQueue = do
(_, sem) <- withSemaphore'
dev
(zero ::& SemaphoreTypeCreateInfo SEMAPHORE_TYPE_TIMELINE 1 :& ())

-- Create some GPU work which waits for the semaphore to be '2' and then
-- bumps it to '3'
queueSubmit
computeQueue
[ SomeStruct
( zero { Vulkan.Core10.waitSemaphores = [sem]
, signalSemaphores = [sem]
, commandBuffers = []
, waitDstStageMask = [PIPELINE_STAGE_TOP_OF_PIPE_BIT]
}
::& zero { waitSemaphoreValues = [2], signalSemaphoreValues = [3] }
:& ()
)
]
zero

-- Bump the semaphore to '2' to start the GPU work
signalSemaphore dev zero { semaphore = sem, value = 2 }

-- Wait for the GPU to set it to '3'
waitSemaphores' dev zero { semaphores = [sem], values = [3] } 1e9 >>= \case
TIMEOUT -> sayErr "Timed out waiting for semaphore"
SUCCESS -> sayErr "Waited for semaphore"
e -> do
sayErrShow e
liftIO $ throwIO (VulkanException e)

deviceWaitIdle' dev

----------------------------------------------------------------
-- Vulkan utils
----------------------------------------------------------------

-- | Create an instance with a debug messenger
createInstance :: MonadResource m => m Instance
createInstance =
let createInfo = zero
{ applicationInfo = Just zero { applicationName = Nothing
, apiVersion = API_VERSION_1_0
}
}
in createDebugInstanceWithExtensions
[]
[]
[KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME]
[]
createInfo

createDevice
:: forall m
. (MonadResource m)
=> Instance
-> m (PhysicalDevice, Device, MyQueues Queue)
createDevice inst = do
(pdi, phys) <- pickPhysicalDevice inst physicalDeviceInfo pdiScore
sayErr . ("Using device: " <>) =<< physicalDeviceName phys
let deviceCreateInfo =
zero { queueCreateInfos = SomeStruct <$> pdiQueueCreateInfos pdi }
::& PhysicalDeviceTimelineSemaphoreFeatures True
:& ()
dev <- createDeviceWithExtensions phys
[]
[KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME]
deviceCreateInfo
queues <- liftIO $ pdiGetQueues pdi dev
pure (phys, dev, queues)


----------------------------------------------------------------
-- Physical device tools
----------------------------------------------------------------

-- | The Ord instance prioritises devices with more memory
data PhysicalDeviceInfo = PhysicalDeviceInfo
{ pdiTotalMemory :: Word64
, pdiQueueCreateInfos :: Vector (DeviceQueueCreateInfo '[])
, pdiGetQueues :: Device -> IO (MyQueues Queue)
}

pdiScore :: PhysicalDeviceInfo -> Word64
pdiScore = pdiTotalMemory

newtype MyQueues a = MyQueues { _myComputeQueue :: a }
deriving (Functor, Foldable, Traversable)

physicalDeviceInfo
:: MonadIO m => PhysicalDevice -> m (Maybe PhysicalDeviceInfo)
physicalDeviceInfo phys = runMaybeT $ do
feats <- getPhysicalDeviceFeatures2KHR phys
let
_ ::& (PhysicalDeviceTimelineSemaphoreFeatures hasTimelineSemaphores :& ())
= feats
guard hasTimelineSemaphores
pdiTotalMemory <- do
heaps <- memoryHeaps <$> getPhysicalDeviceMemoryProperties phys
pure $ sum ((size :: MemoryHeap -> DeviceSize) <$> heaps)
(pdiQueueCreateInfos, pdiGetQueues) <- MaybeT $ assignQueues
phys
(MyQueues (QueueSpec 1 (const (pure . isComputeQueueFamily))))
pure PhysicalDeviceInfo { .. }
Loading

0 comments on commit db6c581

Please sign in to comment.