annotate src/zamikongine/buffer.nim @ 29:da922b506570

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