Mercurial > games > semicongine
annotate semiconginev2/old/vulkan/commandbuffer.nim @ 1220:59e683c4728f compiletime-tests
del: outdated examples
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:04:09 +0700 |
parents | 56781cc0fc7c |
children |
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 |
1138 | 13 proc CreateCommandBufferPool*(device: Device, family: QueueFamily, nBuffers: int): CommandBufferPool = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
14 assert device.vk.Valid |
840
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 |
1138 | 33 proc PipelineBarrier*( |
943
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 |
1138 | 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: |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
59 assert device.vk.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
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 |
1138 | 63 commandBufferPool = CreateCommandBufferPool(device, queue.family, 1) |
951
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() |
1138 | 82 commandBufferPool.Destroy() |
951
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 |
1138 | 85 proc Destroy*(commandpool: var CommandBufferPool) = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
86 assert commandpool.device.vk.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
87 assert commandpool.vk.Valid |
840
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) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
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 |