Mercurial > games > semicongine
annotate src/zamikongine/buffer.nim @ 500:8025ab67d931
fix: hello cube, add: run_all command
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 18 Jan 2023 13:49:12 +0700 |
parents | 0c18638c7217 |
children |
rev | line source |
---|---|
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import ./vulkan |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import ./vulkan_helpers |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 type |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 BufferType* = enum |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 None = 0 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 TransferSrc = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 TransferDst = VK_BUFFER_USAGE_TRANSFER_DST_BIT |
489 | 9 UniformBuffer = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 IndexBuffer = VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 VertexBuffer = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 Buffer* = object |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 device*: VkDevice |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 vkBuffer*: VkBuffer |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 size*: uint64 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 memoryRequirements*: VkMemoryRequirements |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 memory*: VkDeviceMemory |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 bufferTypes*: set[BufferType] |
489 | 19 persistentMapping: bool |
20 mapped: pointer | |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 proc trash*(buffer: var Buffer) = |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 assert int64(buffer.vkBuffer) != 0 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 buffer.vkBuffer = VkBuffer(0) |
494
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
26 if buffer.size == 0: # for zero-size buffers there are no memory allocations |
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
27 return |
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
28 assert int64(buffer.memory) != 0 |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 vkFreeMemory(buffer.device, buffer.memory, nil) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 buffer.memory = VkDeviceMemory(0) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 proc findMemoryType(buffer: Buffer, physicalDevice: VkPhysicalDevice, properties: VkMemoryPropertyFlags): uint32 = |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 var physicalProperties: VkPhysicalDeviceMemoryProperties |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 vkGetPhysicalDeviceMemoryProperties(physicalDevice, addr(physicalProperties)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 for i in 0'u32 ..< physicalProperties.memoryTypeCount: |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 if bool(buffer.memoryRequirements.memoryTypeBits and (1'u32 shl i)) and (uint32(physicalProperties.memoryTypes[i].propertyFlags) and uint32(properties)) == uint32(properties): |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 return i |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 proc InitBuffer*( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 device: VkDevice, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 physicalDevice: VkPhysicalDevice, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 size: uint64, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 bufferTypes: set[BufferType], |
489 | 45 properties: set[VkMemoryPropertyFlagBits], |
46 persistentMapping: bool = false | |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 ): Buffer = |
489 | 48 result = Buffer(device: device, size: size, bufferTypes: bufferTypes, persistentMapping: persistentMapping) |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 var usageFlags = 0 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 for usage in bufferTypes: |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 usageFlags = ord(usageFlags) or ord(usage) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 var bufferInfo = VkBufferCreateInfo( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 size: VkDeviceSize(result.size), |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 usage: VkBufferUsageFlags(usageFlags), |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 sharingMode: VK_SHARING_MODE_EXCLUSIVE, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 ) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 checkVkResult vkCreateBuffer(result.device, addr(bufferInfo), nil, addr(result.vkBuffer)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 vkGetBufferMemoryRequirements(result.device, result.vkBuffer, addr(result.memoryRequirements)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 var memoryProperties = 0'u32 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 for prop in properties: |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 memoryProperties = memoryProperties or uint32(prop) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 var allocInfo = VkMemoryAllocateInfo( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 allocationSize: result.memoryRequirements.size, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 ) |
494
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
70 if result.size > 0: |
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
71 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory)) |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0)) |
489 | 73 if persistentMapping: |
74 checkVkResult vkMapMemory( | |
75 result.device, | |
76 result.memory, | |
77 offset=VkDeviceSize(0), | |
78 VkDeviceSize(result.size), | |
79 VkMemoryMapFlags(0), | |
80 addr(result.mapped) | |
81 ) | |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 template withMapping*(buffer: Buffer, data: pointer, body: untyped): untyped = |
489 | 85 assert not buffer.persistentMapping |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 checkVkResult vkMapMemory(buffer.device, buffer.memory, offset=VkDeviceSize(0), VkDeviceSize(buffer.size), VkMemoryMapFlags(0), addr(data)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 body |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 vkUnmapMemory(buffer.device, buffer.memory) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 |
493 | 90 # note: does not work with seq, because of sizeof |
489 | 91 proc updateData*[T](buffer: Buffer, data: var T) = |
92 if buffer.persistentMapping: | |
93 copyMem(buffer.mapped, addr(data), sizeof(T)) | |
94 else: | |
95 var p: pointer | |
96 buffer.withMapping(p): | |
97 copyMem(p, addr(data), sizeof(T)) | |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 proc copyBuffer*(commandPool: VkCommandPool, queue: VkQueue, src, dst: Buffer, size: uint64) = |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 assert uint64(src.device) == uint64(dst.device) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 var |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 allocInfo = VkCommandBufferAllocateInfo( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 commandPool: commandPool, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 commandBufferCount: 1, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 ) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 commandBuffer: VkCommandBuffer |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 checkVkResult vkAllocateCommandBuffers(src.device, addr(allocInfo), addr(commandBuffer)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 var beginInfo = VkCommandBufferBeginInfo( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 ) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 checkVkResult vkBeginCommandBuffer(commandBuffer, addr(beginInfo)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 var copyRegion = VkBufferCopy(size: VkDeviceSize(size)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 vkCmdCopyBuffer(commandBuffer, src.vkBuffer, dst.vkBuffer, 1, addr(copyRegion)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
118 checkVkResult vkEndCommandBuffer(commandBuffer) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 var submitInfo = VkSubmitInfo( |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 commandBufferCount: 1, |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 pCommandBuffers: addr(commandBuffer), |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
124 ) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
126 checkVkResult vkQueueSubmit(queue, 1, addr(submitInfo), VkFence(0)) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
127 checkVkResult vkQueueWaitIdle(queue) |
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 vkFreeCommandBuffers(src.device, commandPool, 1, addr(commandBuffer)) |