From e3bed9a2eb6a75b7544b4944967f98d56b84284e Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 19 Apr 2023 17:26:33 +0200 Subject: [PATCH] make it work! --- shadersrc/rainbow_lit.frag | 4 ++-- src/CommandBuffer.hs | 17 ++++++++++------- src/Init.hs | 8 ++++---- src/Memory.hs | 9 +++++++-- src/Mesh.hs | 6 +++--- src/Types.hs | 2 ++ 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/shadersrc/rainbow_lit.frag b/shadersrc/rainbow_lit.frag index af11724..42943f4 100644 --- a/shadersrc/rainbow_lit.frag +++ b/shadersrc/rainbow_lit.frag @@ -15,7 +15,7 @@ layout(set = 0, binding = 1) uniform SceneData{ } sceneData; void main() { - //outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f); + outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f); //outColor = fragColor; - outColor = sceneData.ambientColor; + //outColor = sceneData.ambientColor; } diff --git a/src/CommandBuffer.hs b/src/CommandBuffer.hs index 7b126ce..07c934b 100644 --- a/src/CommandBuffer.hs +++ b/src/CommandBuffer.hs @@ -150,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 @@ -175,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 @@ -265,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 } @@ -276,8 +277,10 @@ recordCommandBuffer deviceProps - liftIO $ VMA.withMappedMemory allocator (bufferAllocation sceneParameterBuffer) bracket $ - \scenePointer -> + liftIO $ do + -- VMA.withMappedMemory allocator (bufferAllocation (frameSceneBuffer frame)) bracket $ + -- \scenePointer -> + let scenePointer = VMA.mappedData (bufferInfo (frameSceneBuffer frame)) -- dataPointer <- liftIO $ new scenePointer @@ -289,7 +292,7 @@ recordCommandBuffer -- (sizeOf undefinedGPUSceneData) liftIO $ poke - (castPtr scenePointer `plusPtr` (fromIntegral sceneDataOffset * frameIndex)) + (castPtr scenePointer) -- (castPtr scenePointer `plusPtr` -- (fromIntegral $ -- padUniformBufferSize (fromIntegral $ sizeOf (undefinedGPUSceneData)) deviceProps))-- * @@ -356,7 +359,7 @@ recordCommandBuffer (materialPipelineLayout material) 0 (V.singleton $ frameGlobalDescriptor frame) - (V.singleton $ fromIntegral uniformOffset) + V.empty -- (V.singleton $ fromIntegral uniformOffset) Vk.cmdBindDescriptorSets commandBuffer diff --git a/src/Init.hs b/src/Init.hs index 4022386..2c83a0b 100644 --- a/src/Init.hs +++ b/src/Init.hs @@ -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,7 +202,7 @@ initDescriptors device = do Vk.SHADER_STAGE_VERTEX_BIT 0 sceneBind = descriptorsetLayoutBinding - Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC + Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC Vk.SHADER_STAGE_FRAGMENT_BIT 1 objectBind = descriptorsetLayoutBinding @@ -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 diff --git a/src/Memory.hs b/src/Memory.hs index 175f96d..f7bf3cd 100644 --- a/src/Memory.hs +++ b/src/Memory.hs @@ -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 diff --git a/src/Mesh.hs b/src/Mesh.hs index 9675844..4725166 100644 --- a/src/Mesh.hs +++ b/src/Mesh.hs @@ -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 ) diff --git a/src/Types.hs b/src/Types.hs index c840828..27b77ae 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -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