annotate src/zamikongine/buffer.nim @ 480:14e5151f68d1

did: introduce scene graph, meshs and generic vertex buffers
author Sam <sam@basx.dev>
date Mon, 09 Jan 2023 11:04:19 +0700
parents
children b1b05d4efb52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
9 IndexBuffer = VK_BUFFER_USAGE_INDEX_BUFFER_BIT
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
10 VertexBuffer = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
11 Buffer* = object
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
12 device*: VkDevice
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
13 vkBuffer*: VkBuffer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
14 size*: uint64
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
15 memoryRequirements*: VkMemoryRequirements
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
16 memory*: VkDeviceMemory
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
17 bufferTypes*: set[BufferType]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
18
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
19 proc trash*(buffer: var Buffer) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
20 assert int64(buffer.vkBuffer) != 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
21 assert int64(buffer.memory) != 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
22 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
23 buffer.vkBuffer = VkBuffer(0)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
24 vkFreeMemory(buffer.device, buffer.memory, nil)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
25 buffer.memory = VkDeviceMemory(0)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
26
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
27 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
28 var physicalProperties: VkPhysicalDeviceMemoryProperties
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
29 vkGetPhysicalDeviceMemoryProperties(physicalDevice, addr(physicalProperties))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
30
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
31 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
32 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
33 return i
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
34
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
35 proc InitBuffer*(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
36 device: VkDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
37 physicalDevice: VkPhysicalDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
38 size: uint64,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
39 bufferTypes: set[BufferType],
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
40 properties: set[VkMemoryPropertyFlagBits]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
41 ): Buffer =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
42 result = Buffer(device: device, size: size, bufferTypes: bufferTypes)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
43 var usageFlags = 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
44 for usage in bufferTypes:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
45 usageFlags = ord(usageFlags) or ord(usage)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
46 var bufferInfo = VkBufferCreateInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
47 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
48 size: VkDeviceSize(result.size),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
49 usage: VkBufferUsageFlags(usageFlags),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
50 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
51 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
52 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
53 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
54
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
55 var memoryProperties = 0'u32
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
56 for prop in properties:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
57 memoryProperties = memoryProperties or uint32(prop)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
58
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
59 var allocInfo = VkMemoryAllocateInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
60 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
61 allocationSize: result.memoryRequirements.size,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
62 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
63 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
64 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
65 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
66
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
67
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
68 template withMapping*(buffer: Buffer, data: pointer, body: untyped): untyped =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
69 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
70 body
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
71 vkUnmapMemory(buffer.device, buffer.memory)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
72
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
73
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
74 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
75 assert uint64(src.device) == uint64(dst.device)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
76 var
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
77 allocInfo = VkCommandBufferAllocateInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
78 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
79 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
80 commandPool: commandPool,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
81 commandBufferCount: 1,
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 commandBuffer: VkCommandBuffer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
84 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
85
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
86 var beginInfo = VkCommandBufferBeginInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
87 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
88 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
89 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
90 checkVkResult vkBeginCommandBuffer(commandBuffer, addr(beginInfo))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
91 var copyRegion = VkBufferCopy(size: VkDeviceSize(size))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
92 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
93 checkVkResult vkEndCommandBuffer(commandBuffer)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
94
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
95 var submitInfo = VkSubmitInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
96 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
97 commandBufferCount: 1,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
98 pCommandBuffers: addr(commandBuffer),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
99 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
100
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
101 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
102 checkVkResult vkQueueWaitIdle(queue)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
103 vkFreeCommandBuffers(src.device, commandPool, 1, addr(commandBuffer))