annotate src/zamikongine/buffer.nim @ 489:54a1f8ee208e

big refactoring, part1
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 14:08:00 +0700
parents 14e5151f68d1
children 9edca5dc4e93
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
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
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
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
19 persistentMapping: bool
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
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 assert int64(buffer.memory) != 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
25 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
26 buffer.vkBuffer = VkBuffer(0)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
27 vkFreeMemory(buffer.device, buffer.memory, nil)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
28 buffer.memory = VkDeviceMemory(0)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
29
14e5151f68d1 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 =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
31 var physicalProperties: VkPhysicalDeviceMemoryProperties
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
32 vkGetPhysicalDeviceMemoryProperties(physicalDevice, addr(physicalProperties))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
33
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
34 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
14e5151f68d1 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):
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
36 return i
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
37
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
38 proc InitBuffer*(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
39 device: VkDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
40 physicalDevice: VkPhysicalDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
41 size: uint64,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
42 bufferTypes: set[BufferType],
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
43 properties: set[VkMemoryPropertyFlagBits],
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
44 persistentMapping: bool = false
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
45 ): Buffer =
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
46 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
47 var usageFlags = 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
48 for usage in bufferTypes:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
49 usageFlags = ord(usageFlags) or ord(usage)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
50 var bufferInfo = VkBufferCreateInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
51 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
52 size: VkDeviceSize(result.size),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
53 usage: VkBufferUsageFlags(usageFlags),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
54 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
55 )
14e5151f68d1 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))
14e5151f68d1 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))
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 memoryProperties = 0'u32
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
60 for prop in properties:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
61 memoryProperties = memoryProperties or uint32(prop)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
62
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
63 var allocInfo = VkMemoryAllocateInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
64 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
65 allocationSize: result.memoryRequirements.size,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
66 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties))
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 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
69 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0))
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
70 if persistentMapping:
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
71 checkVkResult vkMapMemory(
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
72 result.device,
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
73 result.memory,
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
74 offset=VkDeviceSize(0),
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
75 VkDeviceSize(result.size),
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
76 VkMemoryMapFlags(0),
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
77 addr(result.mapped)
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
78 )
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
79
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
80
14e5151f68d1 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 =
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
82 assert not buffer.persistentMapping
480
14e5151f68d1 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))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
84 body
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
85 vkUnmapMemory(buffer.device, buffer.memory)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
86
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
87 # note: does not work with seq
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
88 proc updateData*[T](buffer: Buffer, data: var T) =
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
89 if buffer.persistentMapping:
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
90 copyMem(buffer.mapped, addr(data), sizeof(T))
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
91 else:
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
92 var p: pointer
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
93 buffer.withMapping(p):
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
94 copyMem(p, addr(data), sizeof(T))
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
95
14e5151f68d1 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) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
97 assert uint64(src.device) == uint64(dst.device)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
98 var
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
99 allocInfo = VkCommandBufferAllocateInfo(
14e5151f68d1 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,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
101 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
102 commandPool: commandPool,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
103 commandBufferCount: 1,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
104 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
105 commandBuffer: VkCommandBuffer
14e5151f68d1 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))
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 var beginInfo = VkCommandBufferBeginInfo(
14e5151f68d1 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,
14e5151f68d1 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),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
111 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
112 checkVkResult vkBeginCommandBuffer(commandBuffer, addr(beginInfo))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
113 var copyRegion = VkBufferCopy(size: VkDeviceSize(size))
14e5151f68d1 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))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
115 checkVkResult vkEndCommandBuffer(commandBuffer)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
116
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
117 var submitInfo = VkSubmitInfo(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
118 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
119 commandBufferCount: 1,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
120 pCommandBuffers: addr(commandBuffer),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
121 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
122
14e5151f68d1 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))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
124 checkVkResult vkQueueWaitIdle(queue)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
125 vkFreeCommandBuffers(src.device, commandPool, 1, addr(commandBuffer))