Compare commits
7 commits
main
...
buffer_sha
Author | SHA1 | Date | |
---|---|---|---|
2d1ac519c5 | |||
fa984175b3 | |||
d1597f4e35 | |||
3b69ac0389 | |||
13bd4ac3f1 | |||
91e33f4b6c | |||
c4425b4647 |
9 changed files with 79 additions and 45 deletions
30
flake.lock
30
flake.lock
|
@ -1,12 +1,15 @@
|
||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"flake-utils": {
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1680776469,
|
"lastModified": 1681202837,
|
||||||
"narHash": "sha256-3CXUDK/3q/kieWtdsYpDOBJw3Gw4Af6x+2EiSnIkNQw=",
|
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "411e8764155aa9354dbcd6d5faaeb97e9e3dce24",
|
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -17,11 +20,11 @@
|
||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1680910595,
|
"lastModified": 1681910883,
|
||||||
"narHash": "sha256-LA6EEVZrHO2qdgvwFBbXc0N7IVI2VziXgzejGrtQrjg=",
|
"narHash": "sha256-z/21lGaEJ/994LiS1Y1ruTfgVme5w/d3Ww8MluaffDM=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "d682cbf1548727b57ed1071e02399a4109384bb1",
|
"rev": "5608f9dbad40f3a0495a351eceb8c4debb9e6099",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -35,6 +38,21 @@
|
||||||
"flake-utils": "flake-utils",
|
"flake-utils": "flake-utils",
|
||||||
"nixpkgs": "nixpkgs"
|
"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",
|
"root": "root",
|
||||||
|
|
|
@ -15,6 +15,7 @@ layout(set = 0, binding = 1) uniform SceneData{
|
||||||
} sceneData;
|
} sceneData;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
//outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f);
|
outColor = vec4(fragColor.xyz + sceneData.ambientColor.xyz, 1.0f);
|
||||||
outColor = sceneData.ambientColor;
|
//outColor = fragColor;
|
||||||
|
//outColor = sceneData.ambientColor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import Devices
|
||||||
import Types
|
import Types
|
||||||
import Memory
|
import Memory
|
||||||
import Util
|
import Util
|
||||||
|
import Control.Exception (bracket, bracket_)
|
||||||
|
|
||||||
frameOverlap :: Int
|
frameOverlap :: Int
|
||||||
frameOverlap = 2
|
frameOverlap = 2
|
||||||
|
@ -149,7 +150,7 @@ createFrames physicalDevice logicalDevice allocator descriptorPool setLayout obj
|
||||||
cameraInfo
|
cameraInfo
|
||||||
0
|
0
|
||||||
sceneWrite = writeDescriptorBuffer
|
sceneWrite = writeDescriptorBuffer
|
||||||
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
|
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
|
||||||
(V.head globalDescriptor)
|
(V.head globalDescriptor)
|
||||||
sceneInfo
|
sceneInfo
|
||||||
1
|
1
|
||||||
|
@ -174,6 +175,7 @@ createFrames physicalDevice logicalDevice allocator descriptorPool setLayout obj
|
||||||
, frameCommandPool = commandPool
|
, frameCommandPool = commandPool
|
||||||
, frameMainCommandBuffer = V.head commandBuffer
|
, frameMainCommandBuffer = V.head commandBuffer
|
||||||
, frameCameraBuffer = cameraBuffer
|
, frameCameraBuffer = cameraBuffer
|
||||||
|
, frameSceneBuffer = sceneBuffer
|
||||||
, frameObjectBuffer = objectBuffer
|
, frameObjectBuffer = objectBuffer
|
||||||
, frameGlobalDescriptor = V.head globalDescriptor
|
, frameGlobalDescriptor = V.head globalDescriptor
|
||||||
, frameObjectDescriptor = V.head objectDescriptor
|
, frameObjectDescriptor = V.head objectDescriptor
|
||||||
|
@ -264,8 +266,8 @@ recordCommandBuffer
|
||||||
Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor)
|
Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor)
|
||||||
|
|
||||||
let framed = fromIntegral frameNumber / 120
|
let framed = fromIntegral frameNumber / 120
|
||||||
-- ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
|
ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
|
||||||
ambient = V4 1 0 1 1
|
-- ambient = V4 1 0 1 1
|
||||||
ambientParams = sceneParameters
|
ambientParams = sceneParameters
|
||||||
{ ambientColor = ambient
|
{ ambientColor = ambient
|
||||||
}
|
}
|
||||||
|
@ -275,30 +277,33 @@ recordCommandBuffer
|
||||||
deviceProps
|
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
|
-- liftIO $ copyBytes
|
||||||
-- (scenePointer `plusPtr` (fromIntegral sceneDataOffset * fromIntegral frameIndex))
|
-- (scenePointer `plusPtr` (fromIntegral sceneDataOffset * fromIntegral frameIndex))
|
||||||
-- paramsPtr
|
-- paramsPtr
|
||||||
-- (sizeOf undefinedGPUSceneData)
|
-- (sizeOf undefinedGPUSceneData)
|
||||||
|
|
||||||
liftIO $ poke
|
liftIO $ poke
|
||||||
(castPtr scenePointer `plusPtr` (fromIntegral sceneDataOffset * frameIndex))
|
(castPtr scenePointer)
|
||||||
-- (castPtr scenePointer `plusPtr`
|
-- (castPtr scenePointer `plusPtr`
|
||||||
-- (fromIntegral $
|
-- (fromIntegral $
|
||||||
-- padUniformBufferSize (fromIntegral $ sizeOf (undefinedGPUSceneData)) deviceProps))-- *
|
-- padUniformBufferSize (fromIntegral $ sizeOf (undefinedGPUSceneData)) deviceProps))-- *
|
||||||
-- -- fromIntegral frameIndex))
|
-- -- fromIntegral frameIndex))
|
||||||
ambientParams
|
ambientParams
|
||||||
|
|
||||||
-- liftIO $ do
|
-- liftIO $ do
|
||||||
-- test <- peek (castPtr scenePointer :: Ptr GPUSceneData)
|
-- test <- peek (castPtr scenePointer :: Ptr GPUSceneData)
|
||||||
-- print test
|
-- print test
|
||||||
|
|
||||||
VMA.unmapMemory allocator (bufferAllocation sceneParameterBuffer)
|
-- VMA.unmapMemory allocator (bufferAllocation sceneParameterBuffer)
|
||||||
|
|
||||||
renderObjects <- (liftIO . STM.atomically . STM.readTMVar) =<< asks renderables
|
renderObjects <- (liftIO . STM.atomically . STM.readTMVar) =<< asks renderables
|
||||||
meshMap <- (liftIO . STM.atomically . STM.readTMVar) =<< asks meshLibrary
|
meshMap <- (liftIO . STM.atomically . STM.readTMVar) =<< asks meshLibrary
|
||||||
|
@ -330,11 +335,15 @@ recordCommandBuffer
|
||||||
mesh = meshMap M.! meshID
|
mesh = meshMap M.! meshID
|
||||||
material = materialMap M.! materialID
|
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)
|
Vk.cmdBindPipeline commandBuffer Vk.PIPELINE_BIND_POINT_GRAPHICS (materialPipeline material)
|
||||||
|
|
||||||
|
@ -350,7 +359,7 @@ recordCommandBuffer
|
||||||
(materialPipelineLayout material)
|
(materialPipelineLayout material)
|
||||||
0
|
0
|
||||||
(V.singleton $ frameGlobalDescriptor frame)
|
(V.singleton $ frameGlobalDescriptor frame)
|
||||||
(V.singleton $ fromIntegral uniformOffset)
|
V.empty -- (V.singleton $ fromIntegral uniformOffset)
|
||||||
|
|
||||||
Vk.cmdBindDescriptorSets
|
Vk.cmdBindDescriptorSets
|
||||||
commandBuffer
|
commandBuffer
|
||||||
|
|
|
@ -39,7 +39,7 @@ pickPhysicalDevice vkInstance = do
|
||||||
|
|
||||||
let discretePhysDevices = V.filter
|
let discretePhysDevices = V.filter
|
||||||
(\(_, devProps, devFeatures) ->
|
(\(_, devProps, devFeatures) ->
|
||||||
Vk.deviceType devProps == Vk.PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
-- Vk.deviceType devProps == Vk.PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
||||||
Vk.geometryShader devFeatures
|
Vk.geometryShader devFeatures
|
||||||
)
|
)
|
||||||
devicesWithPropsAndFeatures
|
devicesWithPropsAndFeatures
|
||||||
|
|
10
src/Init.hs
10
src/Init.hs
|
@ -156,9 +156,9 @@ initVulkan window = do
|
||||||
)
|
)
|
||||||
<*> createAllocatedBuffer
|
<*> createAllocatedBuffer
|
||||||
allocator
|
allocator
|
||||||
(sizeOf undefinedGPUSceneData)
|
(sizeOf undefinedGPUSceneData * frameOverlap)
|
||||||
Vk.BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
Vk.BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
||||||
VMA.MEMORY_USAGE_CPU_TO_GPU
|
VMA.MEMORY_USAGE_GPU_ONLY
|
||||||
|
|
||||||
|
|
||||||
initScene :: (MonadReader ReadState m, MonadIO m) => m ()
|
initScene :: (MonadReader ReadState m, MonadIO m) => m ()
|
||||||
|
@ -202,8 +202,8 @@ initDescriptors device = do
|
||||||
Vk.SHADER_STAGE_VERTEX_BIT
|
Vk.SHADER_STAGE_VERTEX_BIT
|
||||||
0
|
0
|
||||||
sceneBind = descriptorsetLayoutBinding
|
sceneBind = descriptorsetLayoutBinding
|
||||||
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
|
Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
|
||||||
(Vk.SHADER_STAGE_VERTEX_BIT .|. Vk.SHADER_STAGE_FRAGMENT_BIT)
|
Vk.SHADER_STAGE_FRAGMENT_BIT
|
||||||
1
|
1
|
||||||
objectBind = descriptorsetLayoutBinding
|
objectBind = descriptorsetLayoutBinding
|
||||||
Vk.DESCRIPTOR_TYPE_STORAGE_BUFFER
|
Vk.DESCRIPTOR_TYPE_STORAGE_BUFFER
|
||||||
|
@ -229,7 +229,7 @@ initDescriptors device = do
|
||||||
, Vk.descriptorCount = 10
|
, Vk.descriptorCount = 10
|
||||||
}
|
}
|
||||||
, Vk.zero
|
, Vk.zero
|
||||||
{ Vk.type' = Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
|
{ Vk.type' = Vk.DESCRIPTOR_TYPE_UNIFORM_BUFFER -- _DYNAMIC
|
||||||
, Vk.descriptorCount = 10
|
, Vk.descriptorCount = 10
|
||||||
}
|
}
|
||||||
, Vk.zero
|
, Vk.zero
|
||||||
|
|
|
@ -6,6 +6,8 @@ module Memory where
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
import Control.Monad.Trans.Resource
|
import Control.Monad.Trans.Resource
|
||||||
|
|
||||||
|
import Data.Bits ((.|.))
|
||||||
|
|
||||||
import Foreign.Ptr
|
import Foreign.Ptr
|
||||||
|
|
||||||
import qualified VulkanMemoryAllocator as VMA
|
import qualified VulkanMemoryAllocator as VMA
|
||||||
|
@ -61,13 +63,16 @@ createAllocatedBuffer allocator allocationSize usage memoryUsage = do
|
||||||
}
|
}
|
||||||
vmaAllocationInfo = Vk.zero
|
vmaAllocationInfo = Vk.zero
|
||||||
{ VMA.usage = memoryUsage
|
{ 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
|
} :: VMA.AllocationCreateInfo
|
||||||
|
|
||||||
(_, (buffer, newAllocation, _)) <- allocate
|
(_, (buffer, newAllocation, info)) <- allocate
|
||||||
(VMA.createBuffer allocator bufferInfo vmaAllocationInfo)
|
(VMA.createBuffer allocator bufferInfo vmaAllocationInfo)
|
||||||
(\(buffer, newAllocation, _) -> do
|
(\(buffer, newAllocation, _) -> do
|
||||||
putStrLn "destroying buffer"
|
putStrLn "destroying buffer"
|
||||||
VMA.destroyBuffer allocator buffer newAllocation
|
VMA.destroyBuffer allocator buffer newAllocation
|
||||||
)
|
)
|
||||||
|
|
||||||
return $ AllocatedBuffer buffer newAllocation
|
return $ AllocatedBuffer buffer newAllocation info
|
||||||
|
|
|
@ -64,10 +64,10 @@ uploadMesh vertices allocator = do
|
||||||
} :: VMA.AllocationCreateInfo
|
} :: VMA.AllocationCreateInfo
|
||||||
(_, mesh) <- allocate
|
(_, mesh) <- allocate
|
||||||
(do
|
(do
|
||||||
(buffer, bAllocation, _) <- VMA.createBuffer allocator bufferCreateInfo allocationCreateInfo
|
(buffer, bAllocation, bInfo) <- VMA.createBuffer allocator bufferCreateInfo allocationCreateInfo
|
||||||
return (Mesh vertices (AllocatedBuffer buffer bAllocation))
|
return (Mesh vertices (AllocatedBuffer buffer bAllocation bInfo))
|
||||||
)
|
)
|
||||||
(\(Mesh _ (AllocatedBuffer buffer bAllocation)) -> do
|
(\(Mesh _ (AllocatedBuffer buffer bAllocation bInfo)) -> do
|
||||||
putStrLn "destroying mesh"
|
putStrLn "destroying mesh"
|
||||||
VMA.destroyBuffer allocator buffer bAllocation
|
VMA.destroyBuffer allocator buffer bAllocation
|
||||||
)
|
)
|
||||||
|
|
|
@ -51,6 +51,7 @@ data EngineData = EngineData
|
||||||
data AllocatedBuffer = AllocatedBuffer
|
data AllocatedBuffer = AllocatedBuffer
|
||||||
{ allocatedBuffer :: Vk.Buffer
|
{ allocatedBuffer :: Vk.Buffer
|
||||||
, bufferAllocation :: VMA.Allocation
|
, bufferAllocation :: VMA.Allocation
|
||||||
|
, bufferInfo :: VMA.AllocationInfo
|
||||||
}
|
}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
@ -198,6 +199,7 @@ data FrameData = FrameData
|
||||||
, frameCommandPool :: Vk.CommandPool
|
, frameCommandPool :: Vk.CommandPool
|
||||||
, frameMainCommandBuffer :: Vk.CommandBuffer
|
, frameMainCommandBuffer :: Vk.CommandBuffer
|
||||||
, frameCameraBuffer :: AllocatedBuffer
|
, frameCameraBuffer :: AllocatedBuffer
|
||||||
|
, frameSceneBuffer :: AllocatedBuffer
|
||||||
, frameObjectBuffer :: AllocatedBuffer
|
, frameObjectBuffer :: AllocatedBuffer
|
||||||
, frameGlobalDescriptor :: Vk.DescriptorSet
|
, frameGlobalDescriptor :: Vk.DescriptorSet
|
||||||
, frameObjectDescriptor :: Vk.DescriptorSet
|
, frameObjectDescriptor :: Vk.DescriptorSet
|
||||||
|
|
|
@ -42,7 +42,6 @@ executable vulkan-tutorial
|
||||||
build-depends: base >=4.14.3.0
|
build-depends: base >=4.14.3.0
|
||||||
, sdl2
|
, sdl2
|
||||||
, vulkan
|
, vulkan
|
||||||
, vulkan-api
|
|
||||||
, vulkan-utils
|
, vulkan-utils
|
||||||
, VulkanMemoryAllocator
|
, VulkanMemoryAllocator
|
||||||
, wavefront
|
, wavefront
|
||||||
|
|
Loading…
Reference in a new issue