2022-01-14 07:59:38 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
module Main where
|
|
|
|
|
2023-01-02 01:18:06 +00:00
|
|
|
import qualified Control.Concurrent.STM as STM
|
2022-01-14 07:59:38 +00:00
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.Loops
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Control.Monad.Trans.Resource
|
2022-07-10 23:58:58 +00:00
|
|
|
import Data.Bits
|
2023-01-02 04:59:59 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
import qualified Data.Vector as V
|
2022-01-14 07:59:38 +00:00
|
|
|
import Data.Word
|
2022-07-10 17:31:44 +00:00
|
|
|
import qualified SDL hiding (V2)
|
2022-01-14 07:59:38 +00:00
|
|
|
import qualified Vulkan as Vk
|
|
|
|
import qualified Vulkan.Zero as Vk
|
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Init
|
2022-07-14 05:02:10 +00:00
|
|
|
import Draw (drawFrame)
|
2022-07-16 19:50:22 +00:00
|
|
|
import Types
|
2022-01-14 07:59:38 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
2023-01-02 01:18:06 +00:00
|
|
|
main = do
|
2023-01-02 04:59:59 +00:00
|
|
|
renderablesContainer <- STM.newTMVarIO (V.empty)
|
|
|
|
meshMap <- STM.newTMVarIO (M.empty)
|
|
|
|
materialMap <- STM.newTMVarIO (M.empty)
|
2023-01-06 15:54:30 +00:00
|
|
|
frameNumber <- STM.newTMVarIO 0
|
2023-01-02 04:59:59 +00:00
|
|
|
let initState = ReadState
|
|
|
|
{ renderables = renderablesContainer
|
|
|
|
, meshLibrary = meshMap
|
|
|
|
, materialLibrary = materialMap
|
|
|
|
}
|
|
|
|
runRender initState $ do
|
2023-01-02 01:18:06 +00:00
|
|
|
engineData <- initEngine
|
2022-01-14 07:59:38 +00:00
|
|
|
|
2023-01-02 01:18:06 +00:00
|
|
|
SDL.showWindow $ engineWindow engineData
|
2022-01-14 07:59:38 +00:00
|
|
|
|
2023-01-02 01:18:06 +00:00
|
|
|
-- create abort condition for upcoming lop
|
|
|
|
quit <- liftIO $ STM.newTMVarIO True
|
2022-01-14 07:59:38 +00:00
|
|
|
|
2023-01-02 01:18:06 +00:00
|
|
|
-- main loop
|
|
|
|
whileM_
|
|
|
|
(liftIO $ STM.atomically $ STM.readTMVar quit
|
|
|
|
)
|
|
|
|
( do
|
2023-01-06 15:54:30 +00:00
|
|
|
number <- liftIO $ STM.atomically $ do
|
|
|
|
number <- STM.readTMVar frameNumber
|
|
|
|
void $ STM.swapTMVar frameNumber (succ number)
|
|
|
|
return number
|
2023-01-02 01:18:06 +00:00
|
|
|
-- draw
|
2023-01-06 15:54:30 +00:00
|
|
|
drawFrame engineData number
|
2023-01-02 01:18:06 +00:00
|
|
|
-- poll events
|
|
|
|
evs <- liftIO SDL.pollEvents
|
|
|
|
-- flip abort condition on window close
|
|
|
|
mapM_
|
|
|
|
(\e -> case SDL.eventPayload e of
|
|
|
|
SDL.WindowClosedEvent _ ->
|
|
|
|
void $ liftIO $ STM.atomically $ STM.swapTMVar quit False
|
|
|
|
_ -> return ()
|
|
|
|
)
|
|
|
|
evs
|
2022-01-14 07:59:38 +00:00
|
|
|
)
|
|
|
|
|
2023-01-02 01:18:06 +00:00
|
|
|
Vk.deviceWaitIdle $ engineLogicalDevice engineData
|
2022-07-14 09:02:12 +00:00
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
createBufferView
|
|
|
|
:: (MonadResource m)
|
|
|
|
=> Vk.Device
|
|
|
|
-> m (ReleaseKey, Vk.BufferView)
|
|
|
|
createBufferView logicalDevice = do
|
|
|
|
let bufferCreateInfo = Vk.zero
|
|
|
|
{ Vk.size = 1024
|
|
|
|
, Vk.usage =
|
|
|
|
Vk.BUFFER_USAGE_UNIFORM_BUFFER_BIT .|.
|
|
|
|
Vk.BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
|
|
|
|
, Vk.sharingMode = Vk.SHARING_MODE_EXCLUSIVE
|
2022-01-14 07:59:38 +00:00
|
|
|
}
|
2022-07-10 23:58:58 +00:00
|
|
|
buffer <- snd <$> allocate
|
|
|
|
(Vk.createBuffer logicalDevice bufferCreateInfo Nothing)
|
|
|
|
(\buffer -> do
|
2022-07-12 14:31:40 +00:00
|
|
|
putStrLn "destroying buffer"
|
2022-07-10 23:58:58 +00:00
|
|
|
Vk.destroyBuffer logicalDevice buffer Nothing
|
2022-07-10 18:08:12 +00:00
|
|
|
)
|
2022-01-18 05:25:58 +00:00
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
let bufferViewCreateInfo = Vk.zero
|
|
|
|
{ Vk.buffer = buffer
|
|
|
|
, Vk.format = Vk.FORMAT_R32G32B32_SFLOAT
|
|
|
|
, Vk.offset = 0
|
|
|
|
, Vk.range = Vk.WHOLE_SIZE
|
2022-01-18 05:25:58 +00:00
|
|
|
}
|
2022-07-10 17:31:44 +00:00
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
allocate
|
|
|
|
(Vk.createBufferView logicalDevice bufferViewCreateInfo Nothing)
|
|
|
|
(\bufferView -> do
|
2022-07-12 14:31:40 +00:00
|
|
|
putStrLn "destroying bufferView"
|
2022-07-10 23:58:58 +00:00
|
|
|
Vk.destroyBufferView logicalDevice bufferView Nothing
|
2022-07-10 17:31:44 +00:00
|
|
|
)
|
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
allocateMemory
|
2022-07-10 17:31:44 +00:00
|
|
|
:: (MonadResource m)
|
2022-07-10 23:58:58 +00:00
|
|
|
=> Vk.Device
|
|
|
|
-> Vk.Buffer
|
|
|
|
-> m (ReleaseKey, Vk.DeviceMemory)
|
|
|
|
allocateMemory logicalDevice buffer = do
|
|
|
|
memoryRequirements <- Vk.getBufferMemoryRequirements logicalDevice buffer
|
|
|
|
|
|
|
|
let memoryAllocateInfo = Vk.zero
|
|
|
|
{ Vk.allocationSize = (Vk.size :: Vk.MemoryRequirements -> Word64) memoryRequirements
|
|
|
|
, Vk.memoryTypeIndex = error ("createBuffer: TODO: populate memoryTypeIndex!" :: String)
|
2022-07-10 17:31:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 23:58:58 +00:00
|
|
|
allocate
|
|
|
|
(Vk.allocateMemory logicalDevice memoryAllocateInfo Nothing)
|
|
|
|
(\memory -> do
|
2022-07-12 14:31:40 +00:00
|
|
|
putStrLn "Freeing memory"
|
2022-07-10 23:58:58 +00:00
|
|
|
Vk.freeMemory logicalDevice memory Nothing
|
2022-07-10 18:08:12 +00:00
|
|
|
)
|