Mercurial > games > semicongine
annotate semicongine/vulkan/commandbuffer.nim @ 1137:a4aa9f374d44
did: more renaming
author | sam <sam@basx.dev> |
---|---|
date | Tue, 04 Jun 2024 20:51:22 +0700 |
parents | cca6f1a675db |
children | 02e1d2658ff5 |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import ../core |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import ./device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import ./physicaldevice |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
4 import ./syncing |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 CommandBufferPool* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 device: Device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 vk*: VkCommandPool |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 family*: QueueFamily |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 buffers*: seq[VkCommandBuffer] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 proc createCommandBufferPool*(device: Device, family: QueueFamily, nBuffers: int): CommandBufferPool = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 assert device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 var createInfo = VkCommandPoolCreateInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 queueFamilyIndex: family.index, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 result.family = family |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 result.device = device |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
22 checkVkResult device.vk.vkCreateCommandPool(addr createInfo, nil, addr result.vk) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 var allocInfo = VkCommandBufferAllocateInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 commandPool: result.vk, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 commandBufferCount: uint32(nBuffers), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 result.buffers = newSeq[VkCommandBuffer](nBuffers) |
1137 | 31 checkVkResult device.vk.vkAllocateCommandBuffers(addr allocInfo, result.buffers.ToCPointer) |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
32 |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
33 proc pipelineBarrier*( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
34 commandBuffer: VkCommandBuffer, |
945
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
35 srcStages: openArray[VkPipelineStageFlagBits], |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
36 dstStages: openArray[VkPipelineStageFlagBits], |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
37 memoryBarriers: openArray[VkMemoryBarrier] = [], |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
38 bufferMemoryBarriers: openArray[VkBufferMemoryBarrier] = [], |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
39 imageBarriers: openArray[VkImageMemoryBarrier] = [], |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
40 ) = |
945
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
41 |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
42 vkCmdPipelineBarrier( |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
43 commandBuffer, |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
44 srcStageMask = srcStages.toBits, |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
45 dstStageMask = dstStages.toBits, |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
46 dependencyFlags = VkDependencyFlags(0), |
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
47 memoryBarrierCount = uint32(memoryBarriers.len), |
1137 | 48 pMemoryBarriers = memoryBarriers.ToCPointer, |
945
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
49 bufferMemoryBarrierCount = uint32(bufferMemoryBarriers.len), |
1137 | 50 pBufferMemoryBarriers = bufferMemoryBarriers.ToCPointer, |
945
b65068f5f246
del: do not use vulkan 1.3 functions to support older hardware
sam <sam@basx.dev>
parents:
943
diff
changeset
|
51 imageMemoryBarrierCount = uint32(imageBarriers.len), |
1137 | 52 pImageMemoryBarriers = imageBarriers.ToCPointer, |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
53 ) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
54 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
56 template withSingleUseCommandBuffer*(device: Device, queue: Queue, commandBuffer, body: untyped): untyped = |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
945
diff
changeset
|
57 # TODO? This is super slow, because we call vkQueueWaitIdle |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
58 block: |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
59 assert device.vk.valid |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
60 assert queue.vk.valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
62 var |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
63 commandBufferPool = createCommandBufferPool(device, queue.family, 1) |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
64 commandBuffer = commandBufferPool.buffers[0] |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
65 beginInfo = VkCommandBufferBeginInfo( |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
66 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
67 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
68 ) |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
69 checkVkResult commandBuffer.vkBeginCommandBuffer(addr beginInfo) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
71 block: |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
72 body |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
74 checkVkResult commandBuffer.vkEndCommandBuffer() |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
75 var submitInfo = VkSubmitInfo( |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
76 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
77 commandBufferCount: 1, |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
78 pCommandBuffers: addr commandBuffer, |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
79 ) |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
80 checkVkResult queue.vk.vkQueueSubmit(1, addr submitInfo, VkFence(0)) |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
81 checkVkResult queue.vk.vkQueueWaitIdle() |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
82 commandBufferPool.destroy() |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
83 |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
84 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 proc destroy*(commandpool: var CommandBufferPool) = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 assert commandpool.device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 assert commandpool.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 commandpool.device.vk.vkDestroyCommandPool(commandpool.vk, nil) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 commandpool.vk.reset |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
920
diff
changeset
|
90 |