annotate src/buffer.nim @ 18:90e117952f74

add: basic vertex buffer functionality
author Sam <sam@basx.dev>
date Thu, 05 Jan 2023 01:16:48 +0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
1 import ./vulkan
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
2 import ./vulkan_helpers
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
3
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
4 type
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
5 BufferType* = enum
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
6 VertexBuffer = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
7 Buffer* = object
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
8 device*: VkDevice
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
9 vkBuffer*: VkBuffer
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
10 size*: uint64
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
11 memoryRequirements*: VkMemoryRequirements
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
12 memory*: VkDeviceMemory
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
13
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
14 proc findMemoryType(buffer: Buffer, physicalDevice: VkPhysicalDevice, properties: VkMemoryPropertyFlags): uint32 =
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
15 var physicalProperties: VkPhysicalDeviceMemoryProperties
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
16 vkGetPhysicalDeviceMemoryProperties(physicalDevice, addr(physicalProperties))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
17
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
18 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
19 if bool(buffer.memoryRequirements.memoryTypeBits and (1'u32 shl i)) and (uint32(physicalProperties.memoryTypes[i].propertyFlags) and uint32(properties)) == uint32(properties):
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
20 return i
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
21
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
22 proc InitBuffer*(device: VkDevice, physicalDevice: VkPhysicalDevice, size: uint64, bufferType: BufferType): Buffer =
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
23 result.device = device
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
24 result.size = size
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
25 var bufferInfo = VkBufferCreateInfo(
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
26 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
27 size: VkDeviceSize(result.size),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
28 usage: VkBufferUsageFlags(bufferType),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
29 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
30
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
31 )
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
32 checkVkResult vkCreateBuffer(result.device, addr(bufferInfo), nil, addr(result.vkBuffer))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
33 vkGetBufferMemoryRequirements(result.device, result.vkBuffer, addr(result.memoryRequirements))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
34
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
35 var allocInfo = VkMemoryAllocateInfo(
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
36 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
37 allocationSize: result.memoryRequirements.size,
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
38 memoryTypeIndex: result.findMemoryType(
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
39 physicalDevice,
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
40 VkMemoryPropertyFlags(uint32(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) or uint32(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
41 )
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
42 )
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
43 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
44 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
45
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
46
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
47 template withMapping*(buffer: Buffer, data: pointer, body: untyped): untyped =
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
48 checkVkResult buffer.device.vkMapMemory(buffer.memory, offset=VkDeviceSize(0), VkDeviceSize(buffer.size), VkMemoryMapFlags(0), addr(data));
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
49 body
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
50 buffer.device.vkUnmapMemory(buffer.memory);
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
51
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
52
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
53 proc `=copy`(a: var Buffer, b: Buffer){.error.}
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
54
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
55 proc `=destroy`*(buffer: var Buffer) =
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
56 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents:
diff changeset
57 vkFreeMemory(buffer.device, buffer.memory, nil);