Compare commits

...

7 commits

Author SHA1 Message Date
2d1ac519c5 make it work! 2023-04-19 17:31:22 +02:00
fa984175b3 make runnable on non-discrete GPUs 2023-04-19 17:30:59 +02:00
d1597f4e35 make runnable on non-discrete GPUs 2023-04-19 17:30:59 +02:00
3b69ac0389 flake update 2023-04-19 17:30:56 +02:00
13bd4ac3f1 first little steps 2023-04-19 17:30:01 +02:00
91e33f4b6c make output visible 2023-04-19 15:38:54 +02:00
c4425b4647 flake update 2023-04-19 15:22:20 +02:00
9 changed files with 79 additions and 45 deletions

View file

@ -1,12 +1,15 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1680776469,
"narHash": "sha256-3CXUDK/3q/kieWtdsYpDOBJw3Gw4Af6x+2EiSnIkNQw=",
"lastModified": 1681202837,
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "411e8764155aa9354dbcd6d5faaeb97e9e3dce24",
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
"type": "github"
},
"original": {
@ -17,11 +20,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1680910595,
"narHash": "sha256-LA6EEVZrHO2qdgvwFBbXc0N7IVI2VziXgzejGrtQrjg=",
"lastModified": 1681910883,
"narHash": "sha256-z/21lGaEJ/994LiS1Y1ruTfgVme5w/d3Ww8MluaffDM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d682cbf1548727b57ed1071e02399a4109384bb1",
"rev": "5608f9dbad40f3a0495a351eceb8c4debb9e6099",
"type": "github"
},
"original": {
@ -35,6 +38,21 @@
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",

View file

@ -15,6 +15,7 @@ layout(set = 0, binding = 1) uniform SceneData{
} sceneData;
void main() {
//outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f);
outColor = sceneData.ambientColor;
outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f);
//outColor = fragColor;
//outColor = sceneData.ambientColor;
}

View file

@ -25,6 +25,7 @@ import Devices
import Types
import Memory
import Util
import Control.Exception (bracket, bracket_)
frameOverlap :: Int
frameOverlap = 2
@ -149,7 +150,7 @@ createFrames physicalDevice logicalDevice allocator descriptorPool setLayout obj
cameraInfo
0
sceneWrite = writeDescriptorBuffer
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
(V.head globalDescriptor)
sceneInfo
1
@ -174,6 +175,7 @@ createFrames physicalDevice logicalDevice allocator descriptorPool setLayout obj
, frameCommandPool = commandPool
, frameMainCommandBuffer = V.head commandBuffer
, frameCameraBuffer = cameraBuffer
, frameSceneBuffer = sceneBuffer
, frameObjectBuffer = objectBuffer
, frameGlobalDescriptor = V.head globalDescriptor
, frameObjectDescriptor = V.head objectDescriptor
@ -264,8 +266,8 @@ recordCommandBuffer
Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor)
let framed = fromIntegral frameNumber / 120
-- ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
ambient = V4 1 0 1 1
ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
-- ambient = V4 1 0 1 1
ambientParams = sceneParameters
{ ambientColor = ambient
}
@ -275,30 +277,33 @@ recordCommandBuffer
deviceProps
scenePointer <- VMA.mapMemory allocator (bufferAllocation sceneParameterBuffer)
liftIO $ do
-- VMA.withMappedMemory allocator (bufferAllocation (frameSceneBuffer frame)) bracket $
-- \scenePointer ->
let scenePointer = VMA.mappedData (bufferInfo (frameSceneBuffer frame))
-- dataPointer <- liftIO $ new scenePointer
-- dataPointer <- liftIO $ new scenePointer
-- paramsPtr <- liftIO (new ambientParams)
-- paramsPtr <- liftIO (new ambientParams)
-- liftIO $ copyBytes
-- (scenePointer `plusPtr` (fromIntegral sceneDataOffset * fromIntegral frameIndex))
-- paramsPtr
-- (sizeOf undefinedGPUSceneData)
-- liftIO $ copyBytes
-- (scenePointer `plusPtr` (fromIntegral sceneDataOffset * fromIntegral frameIndex))
-- paramsPtr
-- (sizeOf undefinedGPUSceneData)
liftIO $ poke
(castPtr scenePointer `plusPtr` (fromIntegral sceneDataOffset * frameIndex))
-- (castPtr scenePointer `plusPtr`
-- (fromIntegral $
-- padUniformBufferSize (fromIntegral $ sizeOf (undefinedGPUSceneData)) deviceProps))-- *
-- -- fromIntegral frameIndex))
ambientParams
liftIO $ poke
(castPtr scenePointer)
-- (castPtr scenePointer `plusPtr`
-- (fromIntegral $
-- padUniformBufferSize (fromIntegral $ sizeOf (undefinedGPUSceneData)) deviceProps))-- *
-- -- fromIntegral frameIndex))
ambientParams
-- liftIO $ do
-- test <- peek (castPtr scenePointer :: Ptr GPUSceneData)
-- print test
-- liftIO $ do
-- test <- peek (castPtr scenePointer :: Ptr GPUSceneData)
-- print test
VMA.unmapMemory allocator (bufferAllocation sceneParameterBuffer)
-- VMA.unmapMemory allocator (bufferAllocation sceneParameterBuffer)
renderObjects <- (liftIO . STM.atomically . STM.readTMVar) =<< asks renderables
meshMap <- (liftIO . STM.atomically . STM.readTMVar) =<< asks meshLibrary
@ -330,11 +335,15 @@ recordCommandBuffer
mesh = meshMap M.! meshID
material = materialMap M.! materialID
memoryPointer <- VMA.mapMemory allocator (bufferAllocation $ frameCameraBuffer frame)
liftIO $ VMA.withMappedMemory
allocator
(bufferAllocation $ frameCameraBuffer frame)
bracket
$ \memoryPointer ->
liftIO $ poke (castPtr memoryPointer) cameraData
liftIO $ poke (castPtr memoryPointer) cameraData
VMA.unmapMemory allocator (bufferAllocation $ frameCameraBuffer frame)
-- VMA.unmapMemory allocator (bufferAllocation $ frameCameraBuffer frame)
Vk.cmdBindPipeline commandBuffer Vk.PIPELINE_BIND_POINT_GRAPHICS (materialPipeline material)
@ -350,7 +359,7 @@ recordCommandBuffer
(materialPipelineLayout material)
0
(V.singleton $ frameGlobalDescriptor frame)
(V.singleton $ fromIntegral uniformOffset)
V.empty -- (V.singleton $ fromIntegral uniformOffset)
Vk.cmdBindDescriptorSets
commandBuffer

View file

@ -39,7 +39,7 @@ pickPhysicalDevice vkInstance = do
let discretePhysDevices = V.filter
(\(_, devProps, devFeatures) ->
Vk.deviceType devProps == Vk.PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
-- Vk.deviceType devProps == Vk.PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
Vk.geometryShader devFeatures
)
devicesWithPropsAndFeatures

View file

@ -156,9 +156,9 @@ initVulkan window = do
)
<*> createAllocatedBuffer
allocator
(sizeOf undefinedGPUSceneData)
(sizeOf undefinedGPUSceneData * frameOverlap)
Vk.BUFFER_USAGE_UNIFORM_BUFFER_BIT
VMA.MEMORY_USAGE_CPU_TO_GPU
VMA.MEMORY_USAGE_GPU_ONLY
initScene :: (MonadReader ReadState m, MonadIO m) => m ()
@ -202,8 +202,8 @@ initDescriptors device = do
Vk.SHADER_STAGE_VERTEX_BIT
0
sceneBind = descriptorsetLayoutBinding
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
(Vk.SHADER_STAGE_VERTEX_BIT .|. Vk.SHADER_STAGE_FRAGMENT_BIT)
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
Vk.SHADER_STAGE_FRAGMENT_BIT
1
objectBind = descriptorsetLayoutBinding
Vk.DESCRIPTOR_TYPE_STORAGE_BUFFER
@ -229,7 +229,7 @@ initDescriptors device = do
, Vk.descriptorCount = 10
}
, Vk.zero
{ Vk.type' = Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
{ Vk.type' = Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
, Vk.descriptorCount = 10
}
, Vk.zero

View file

@ -6,6 +6,8 @@ module Memory where
import Control.Monad.IO.Class
import Control.Monad.Trans.Resource
import Data.Bits ((.|.))
import Foreign.Ptr
import qualified VulkanMemoryAllocator as VMA
@ -61,13 +63,16 @@ createAllocatedBuffer allocator allocationSize usage memoryUsage = do
}
vmaAllocationInfo = Vk.zero
{ VMA.usage = memoryUsage
, VMA.flags = VMA.ALLOCATION_CREATE_MAPPED_BIT
, VMA.requiredFlags = Vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT .|. Vk.MEMORY_PROPERTY_HOST_COHERENT_BIT
, VMA.preferredFlags = Vk.MEMORY_PROPERTY_DEVICE_LOCAL_BIT
} :: VMA.AllocationCreateInfo
(_, (buffer, newAllocation, _)) <- allocate
(_, (buffer, newAllocation, info)) <- allocate
(VMA.createBuffer allocator bufferInfo vmaAllocationInfo)
(\(buffer, newAllocation, _) -> do
putStrLn "destroying buffer"
VMA.destroyBuffer allocator buffer newAllocation
)
return $ AllocatedBuffer buffer newAllocation
return $ AllocatedBuffer buffer newAllocation info

View file

@ -64,10 +64,10 @@ uploadMesh vertices allocator = do
} :: VMA.AllocationCreateInfo
(_, mesh) <- allocate
(do
(buffer, bAllocation, _) <- VMA.createBuffer allocator bufferCreateInfo allocationCreateInfo
return (Mesh vertices (AllocatedBuffer buffer bAllocation))
(buffer, bAllocation, bInfo) <- VMA.createBuffer allocator bufferCreateInfo allocationCreateInfo
return (Mesh vertices (AllocatedBuffer buffer bAllocation bInfo))
)
(\(Mesh _ (AllocatedBuffer buffer bAllocation)) -> do
(\(Mesh _ (AllocatedBuffer buffer bAllocation bInfo)) -> do
putStrLn "destroying mesh"
VMA.destroyBuffer allocator buffer bAllocation
)

View file

@ -51,6 +51,7 @@ data EngineData = EngineData
data AllocatedBuffer = AllocatedBuffer
{ allocatedBuffer :: Vk.Buffer
, bufferAllocation :: VMA.Allocation
, bufferInfo :: VMA.AllocationInfo
}
deriving (Show)
@ -198,6 +199,7 @@ data FrameData = FrameData
, frameCommandPool :: Vk.CommandPool
, frameMainCommandBuffer :: Vk.CommandBuffer
, frameCameraBuffer :: AllocatedBuffer
, frameSceneBuffer :: AllocatedBuffer
, frameObjectBuffer :: AllocatedBuffer
, frameGlobalDescriptor :: Vk.DescriptorSet
, frameObjectDescriptor :: Vk.DescriptorSet

View file

@ -42,7 +42,6 @@ executable vulkan-tutorial
build-depends: base >=4.14.3.0
, sdl2
, vulkan
, vulkan-api
, vulkan-utils
, VulkanMemoryAllocator
, wavefront