comparison semiconginev2/old/vulkan/commandbuffer.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/old/vulkan/commandbuffer.nim@a3eb305bcac2
children
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 import ../core
2 import ./device
3 import ./physicaldevice
4 import ./syncing
5
6 type
7 CommandBufferPool* = object
8 device: Device
9 vk*: VkCommandPool
10 family*: QueueFamily
11 buffers*: seq[VkCommandBuffer]
12
13 proc CreateCommandBufferPool*(device: Device, family: QueueFamily, nBuffers: int): CommandBufferPool =
14 assert device.vk.Valid
15 var createInfo = VkCommandPoolCreateInfo(
16 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
17 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
18 queueFamilyIndex: family.index,
19 )
20 result.family = family
21 result.device = device
22 checkVkResult device.vk.vkCreateCommandPool(addr createInfo, nil, addr result.vk)
23
24 var allocInfo = VkCommandBufferAllocateInfo(
25 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
26 commandPool: result.vk,
27 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
28 commandBufferCount: uint32(nBuffers),
29 )
30 result.buffers = newSeq[VkCommandBuffer](nBuffers)
31 checkVkResult device.vk.vkAllocateCommandBuffers(addr allocInfo, result.buffers.ToCPointer)
32
33 proc PipelineBarrier*(
34 commandBuffer: VkCommandBuffer,
35 srcStages: openArray[VkPipelineStageFlagBits],
36 dstStages: openArray[VkPipelineStageFlagBits],
37 memoryBarriers: openArray[VkMemoryBarrier] = [],
38 bufferMemoryBarriers: openArray[VkBufferMemoryBarrier] = [],
39 imageBarriers: openArray[VkImageMemoryBarrier] = [],
40 ) =
41
42 vkCmdPipelineBarrier(
43 commandBuffer,
44 srcStageMask = srcStages.toBits,
45 dstStageMask = dstStages.toBits,
46 dependencyFlags = VkDependencyFlags(0),
47 memoryBarrierCount = uint32(memoryBarriers.len),
48 pMemoryBarriers = memoryBarriers.ToCPointer,
49 bufferMemoryBarrierCount = uint32(bufferMemoryBarriers.len),
50 pBufferMemoryBarriers = bufferMemoryBarriers.ToCPointer,
51 imageMemoryBarrierCount = uint32(imageBarriers.len),
52 pImageMemoryBarriers = imageBarriers.ToCPointer,
53 )
54
55
56 template WithSingleUseCommandBuffer*(device: Device, queue: Queue, commandBuffer, body: untyped): untyped =
57 # TODO? This is super slow, because we call vkQueueWaitIdle
58 block:
59 assert device.vk.Valid
60 assert queue.vk.Valid
61
62 var
63 commandBufferPool = CreateCommandBufferPool(device, queue.family, 1)
64 commandBuffer = commandBufferPool.buffers[0]
65 beginInfo = VkCommandBufferBeginInfo(
66 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
67 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
68 )
69 checkVkResult commandBuffer.vkBeginCommandBuffer(addr beginInfo)
70
71 block:
72 body
73
74 checkVkResult commandBuffer.vkEndCommandBuffer()
75 var submitInfo = VkSubmitInfo(
76 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
77 commandBufferCount: 1,
78 pCommandBuffers: addr commandBuffer,
79 )
80 checkVkResult queue.vk.vkQueueSubmit(1, addr submitInfo, VkFence(0))
81 checkVkResult queue.vk.vkQueueWaitIdle()
82 commandBufferPool.Destroy()
83
84
85 proc Destroy*(commandpool: var CommandBufferPool) =
86 assert commandpool.device.vk.Valid
87 assert commandpool.vk.Valid
88 commandpool.device.vk.vkDestroyCommandPool(commandpool.vk, nil)
89 commandpool.vk.Reset
90