annotate semiconginev2/rendering/vulkan_wrappers.nim @ 1226:c8e3037aca66 compiletime-tests

add: contrib stuff
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 23:41:51 +0700
parents 56781cc0fc7c
children 5dcb503ef0c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
1 proc GetBestPhysicalDevice(instance: VkInstance): VkPhysicalDevice =
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
2 var nDevices: uint32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
3 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
4 var devices = newSeq[VkPhysicalDevice](nDevices)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
5 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), devices.ToCPointer)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
6
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
7 var score = 0'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
8 for pDevice in devices:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
9 var props: VkPhysicalDeviceProperties
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
10 # CANNOT use svkGetPhysicalDeviceProperties (not initialized yet)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
11 vkGetPhysicalDeviceProperties(pDevice, addr(props))
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
12 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU and props.limits.maxImageDimension2D > score:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
13 score = props.limits.maxImageDimension2D
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
14 result = pDevice
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
15
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
16 if score == 0:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
17 for pDevice in devices:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
18 var props: VkPhysicalDeviceProperties
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
19 # CANNOT use svkGetPhysicalDeviceProperties (not initialized yet)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
20 vkGetPhysicalDeviceProperties(pDevice, addr(props))
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
21 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU and props.limits.maxImageDimension2D > score:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
22 score = props.limits.maxImageDimension2D
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
23 result = pDevice
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
24
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
25 assert score > 0, "Unable to find integrated or discrete GPU"
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
26
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
27 proc svkGetPhysicalDeviceSurfaceSupportKHR*(queueFamily: uint32): bool =
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
28 var presentation = VkBool32(false)
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
29 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkan.physicalDevice, queueFamily, vulkan.surface, addr(presentation))
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
30 return bool(presentation)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
31
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
32 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 =
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
33 var nQueuefamilies: uint32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
34 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, nil)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
35 var queuFamilies = newSeq[VkQueueFamilyProperties](nQueuefamilies)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
36 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, queuFamilies.ToCPointer)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
37 for i in 0'u32 ..< nQueuefamilies:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
38 if qType in toEnums(queuFamilies[i].queueFlags):
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
39 # for graphics queues we always also want prsentation, they seem never to be separated in practice
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
40 if svkGetPhysicalDeviceSurfaceSupportKHR(i) or qType != VK_QUEUE_GRAPHICS_BIT:
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
41 return i
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
42 assert false, &"Queue of type {qType} not found"
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
43
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
44 proc svkGetDeviceQueue*(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue =
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
45 vkGetDeviceQueue(
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
46 device,
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
47 queueFamilyIndex,
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
48 0,
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
49 addr(result),
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
50 )
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
51
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
52 proc DefaultSurfaceFormat(): VkFormat =
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
53 # EVERY windows driver and almost every linux driver should support this
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
54 VK_FORMAT_B8G8R8A8_SRGB
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
55
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
56 func size(format: VkFormat): uint64 =
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
57 const formatSize = [
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
58 VK_FORMAT_B8G8R8A8_SRGB.int: 4'u64,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
59 ]
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
60 return formatSize[format.int]
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
61
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
62 proc svkGetPhysicalDeviceSurfacePresentModesKHR*(): seq[VkPresentModeKHR] =
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
63 var n_modes: uint32
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
64 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(vulkan.physicalDevice, vulkan.surface, addr(n_modes), nil)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
65 result = newSeq[VkPresentModeKHR](n_modes)
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
66 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(vulkan.physicalDevice, vulkan.surface, addr(n_modes), result.ToCPointer)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
67
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
68 proc svkGetPhysicalDeviceSurfaceFormatsKHR(): seq[VkSurfaceFormatKHR] =
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
69 var n_formats: uint32
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
70 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(vulkan.physicalDevice, vulkan.surface, addr(n_formats), nil)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
71 result = newSeq[VkSurfaceFormatKHR](n_formats)
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
72 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(vulkan.physicalDevice, vulkan.surface, addr(n_formats), result.ToCPointer)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
73
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
74 proc hasValidationLayer*(): bool =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
75 var n_layers: uint32
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
76 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), nil)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
77 if n_layers > 0:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
78 var layers = newSeq[VkLayerProperties](n_layers)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
79 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), layers.ToCPointer)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
80 for layer in layers:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
81 if layer.layerName.CleanString == "VK_LAYER_KHRONOS_validation":
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
82 return true
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
83 return false
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
84
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
85 proc svkGetPhysicalDeviceProperties*(): VkPhysicalDeviceProperties =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
86 vkGetPhysicalDeviceProperties(vulkan.physicalDevice, addr(result))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
87
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
88 proc svkCreateBuffer*(size: uint64, usage: openArray[VkBufferUsageFlagBits]): VkBuffer =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
89 var createInfo = VkBufferCreateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
90 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
91 flags: VkBufferCreateFlags(0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
92 size: size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
93 usage: usage.toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
94 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
95 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
96 checkVkResult vkCreateBuffer(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
97 device = vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
98 pCreateInfo = addr(createInfo),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
99 pAllocator = nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
100 pBuffer = addr(result),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
101 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
102
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
103 proc svkAllocateMemory*(size: uint64, typeIndex: uint32): VkDeviceMemory =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
104 var memoryAllocationInfo = VkMemoryAllocateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
105 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
106 allocationSize: size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
107 memoryTypeIndex: typeIndex,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
108 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
109 checkVkResult vkAllocateMemory(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
110 vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
111 addr(memoryAllocationInfo),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
112 nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
113 addr(result),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
114 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
115
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1200
diff changeset
116 proc svkCreate2DImage*(width, height: uint32, format: VkFormat, usage: openArray[VkImageUsageFlagBits], samples = VK_SAMPLE_COUNT_1_BIT): VkImage =
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
117 var imageProps: VkImageFormatProperties
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
118 checkVkResult vkGetPhysicalDeviceImageFormatProperties(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
119 vulkan.physicalDevice,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
120 format,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
121 VK_IMAGE_TYPE_2D,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
122 VK_IMAGE_TILING_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
123 usage.toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
124 VkImageCreateFlags(0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
125 addr(imageProps)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
126 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
127
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
128 var imageInfo = VkImageCreateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
129 sType: VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
130 imageType: VK_IMAGE_TYPE_2D,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
131 extent: VkExtent3D(width: width, height: height, depth: 1),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
132 mipLevels: min(1'u32, imageProps.maxMipLevels),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
133 arrayLayers: min(1'u32, imageProps.maxArrayLayers),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
134 format: format,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
135 tiling: VK_IMAGE_TILING_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
136 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
137 usage: usage.toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
138 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1200
diff changeset
139 samples: samples,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
140 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
141 checkVkResult vkCreateImage(vulkan.device, addr imageInfo, nil, addr(result))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
142
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
143 proc svkCreate2DImageView(image: VkImage, format: VkFormat): VkImageView =
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
144 var createInfo = VkImageViewCreateInfo(
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
145 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
146 image: image,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
147 viewType: VK_IMAGE_VIEW_TYPE_2D,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
148 format: format,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
149 components: VkComponentMapping(
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
150 r: VK_COMPONENT_SWIZZLE_IDENTITY,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
151 g: VK_COMPONENT_SWIZZLE_IDENTITY,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
152 b: VK_COMPONENT_SWIZZLE_IDENTITY,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
153 a: VK_COMPONENT_SWIZZLE_IDENTITY,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
154 ),
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
155 subresourceRange: VkImageSubresourceRange(
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
156 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
157 baseMipLevel: 0,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
158 levelCount: 1,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
159 baseArrayLayer: 0,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
160 layerCount: 1,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
161 ),
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
162 )
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
163 checkVkResult vkCreateImageView(vulkan.device, addr(createInfo), nil, addr(result))
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
164
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
165 proc svkCreateFramebuffer*(renderpass: VkRenderPass, width, height: uint32, attachments: openArray[VkImageView]): VkFramebuffer =
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
166 var framebufferInfo = VkFramebufferCreateInfo(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
167 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
168 renderPass: renderpass,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
169 attachmentCount: attachments.len.uint32,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
170 pAttachments: attachments.ToCPointer,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
171 width: width,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
172 height: height,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
173 layers: 1,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
174 )
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
175 checkVkResult vkCreateFramebuffer(vulkan.device, addr(framebufferInfo), nil, addr(result))
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
176
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
177 proc svkGetBufferMemoryRequirements*(buffer: VkBuffer): tuple[size: uint64, alignment: uint64, memoryTypes: seq[uint32]] =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
178 var reqs: VkMemoryRequirements
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
179 vkGetBufferMemoryRequirements(vulkan.device, buffer, addr(reqs))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
180 result.size = reqs.size
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
181 result.alignment = reqs.alignment
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
182 for i in 0'u32 ..< VK_MAX_MEMORY_TYPES:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
183 if ((1'u32 shl i) and reqs.memoryTypeBits) > 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
184 result.memoryTypes.add i
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
185
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
186 proc svkGetImageMemoryRequirements*(image: VkImage): tuple[size: uint64, alignment: uint64, memoryTypes: seq[uint32]] =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
187 var reqs: VkMemoryRequirements
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
188 vkGetImageMemoryRequirements(vulkan.device, image, addr(reqs))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
189 result.size = reqs.size
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
190 result.alignment = reqs.alignment
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
191 for i in 0'u32 ..< VK_MAX_MEMORY_TYPES:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
192 if ((1'u32 shl i) and reqs.memoryTypeBits) > 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
193 result.memoryTypes.add i
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
194
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
195 proc svkCreateFence*(signaled = false): VkFence =
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
196 var fenceInfo = VkFenceCreateInfo(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
197 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
198 flags: if signaled: toBits [VK_FENCE_CREATE_SIGNALED_BIT] else: VkFenceCreateFlags(0)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
199 )
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
200 checkVkResult vkCreateFence(vulkan.device, addr(fenceInfo), nil, addr(result))
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
201
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
202 proc svkCreateSemaphore*(): VkSemaphore =
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
203 var semaphoreInfo = VkSemaphoreCreateInfo(sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
204 checkVkResult vkCreateSemaphore(vulkan.device, addr(semaphoreInfo), nil, addr(result))
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
205
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
206 proc Await*(fence: VkFence, timeout = high(uint64)): bool =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
207 let waitResult = vkWaitForFences(vulkan.device, 1, addr(fence), false, timeout)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
208 if waitResult == VK_TIMEOUT:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
209 return false
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
210 checkVkResult waitResult
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
211 return true
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
212
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
213 proc svkResetFences*(fence: VkFence) =
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
214 checkVkResult vkResetFences(vulkan.device, 1, addr(fence))
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
215
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
216 proc svkCmdBindDescriptorSets(commandBuffer: VkCommandBuffer, descriptorSets: openArray[VkDescriptorSet], layout: VkPipelineLayout) =
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
217 vkCmdBindDescriptorSets(
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
218 commandBuffer = commandBuffer,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
219 pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
220 layout = layout,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
221 firstSet = 0,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
222 descriptorSetCount = descriptorSets.len.uint32,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
223 pDescriptorSets = descriptorSets.ToCPointer,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
224 dynamicOffsetCount = 0,
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
225 pDynamicOffsets = nil
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
226 )
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
227
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
228
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
229 proc svkCreateRenderPass(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
230 attachments: openArray[VkAttachmentDescription],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
231 colorAttachments: openArray[VkAttachmentReference],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
232 resolveAttachments: openArray[VkAttachmentReference],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
233 dependencies: openArray[VkSubpassDependency],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
234 ): VkRenderPass =
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
235 assert colorAttachments.len == resolveAttachments.len or resolveAttachments.len == 0
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
236 var subpass = VkSubpassDescription(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
237 flags: VkSubpassDescriptionFlags(0),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
238 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
239 inputAttachmentCount: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
240 pInputAttachments: nil,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
241 colorAttachmentCount: colorAttachments.len.uint32,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
242 pColorAttachments: colorAttachments.ToCPointer,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
243 pResolveAttachments: resolveAttachments.ToCPointer,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
244 pDepthStencilAttachment: nil,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
245 preserveAttachmentCount: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
246 pPreserveAttachments: nil,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
247 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
248 var createInfo = VkRenderPassCreateInfo(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
249 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
250 attachmentCount: uint32(attachments.len),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
251 pAttachments: attachments.ToCPointer,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
252 subpassCount: 1,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
253 pSubpasses: addr(subpass),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
254 dependencyCount: uint32(dependencies.len),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
255 pDependencies: dependencies.ToCPointer,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
256 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
257 checkVkResult vkCreateRenderPass(vulkan.device, addr(createInfo), nil, addr(result))
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1205
diff changeset
258
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
259 proc BestMemory*(mappable: bool, filter: seq[uint32] = @[]): uint32 =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
260 var physicalProperties: VkPhysicalDeviceMemoryProperties
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
261 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
262
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
263 var maxScore: float = -1
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
264 var maxIndex: uint32 = 0
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
265 for index in 0'u32 ..< physicalProperties.memoryTypeCount:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
266 if filter.len == 0 or index in filter:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
267 let flags = toEnums(physicalProperties.memoryTypes[index].propertyFlags)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
268 if not mappable or VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
269 var score: float = 0
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
270 if VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT in flags: score += 1_000_000
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
271 if VK_MEMORY_PROPERTY_HOST_CACHED_BIT in flags: score += 1_000
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
272 score += float(physicalProperties.memoryHeaps[physicalProperties.memoryTypes[index].heapIndex].size) / 1_000_000_000
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
273 if score > maxScore:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
274 maxScore = score
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
275 maxIndex = index
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
276 assert maxScore > 0, &"Unable to find memory type (mappable: {mappable}, filter: {filter})"
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
277 return maxIndex
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
278
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
279 template WithSingleUseCommandBuffer*(cmd, body: untyped): untyped =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
280 block:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
281 var
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
282 commandBufferPool: VkCommandPool
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
283 createInfo = VkCommandPoolCreateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
284 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1196
diff changeset
285 flags: VkCommandPoolCreateFlags(0),
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
286 queueFamilyIndex: vulkan.graphicsQueueFamily,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
287 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
288 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr(commandBufferPool))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
289 var
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
290 `cmd` {.inject.}: VkCommandBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
291 allocInfo = VkCommandBufferAllocateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
292 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
293 commandPool: commandBufferPool,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
294 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
295 commandBufferCount: 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
296 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
297 checkVkResult vulkan.device.vkAllocateCommandBuffers(addr allocInfo, addr(`cmd`))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
298 var beginInfo = VkCommandBufferBeginInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
299 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
300 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
301 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
302 checkVkResult `cmd`.vkBeginCommandBuffer(addr beginInfo)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
303
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
304 body
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
305
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
306 checkVkResult `cmd`.vkEndCommandBuffer()
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
307 var submitInfo = VkSubmitInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
308 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
309 commandBufferCount: 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
310 pCommandBuffers: addr(`cmd`),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
311 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
312
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
313 var fence = svkCreateFence()
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
314 checkVkResult vkQueueSubmit(vulkan.graphicsQueue, 1, addr(submitInfo), fence)
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
315 discard fence.Await()
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
316 vkDestroyFence(vulkan.device, fence, nil)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
317 vkDestroyCommandPool(vulkan.device, commandBufferPool, nil)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
318
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
319 template WithStagingBuffer*[T: (VkBuffer, uint64)|(VkImage, uint32, uint32)](
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
320 target: T,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
321 bufferSize: uint64,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
322 dataPointer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
323 body: untyped
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
324 ): untyped =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
325 var `dataPointer` {.inject.}: pointer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
326 let stagingBuffer = svkCreateBuffer(bufferSize, [VK_BUFFER_USAGE_TRANSFER_SRC_BIT])
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
327 let memoryRequirements = svkGetBufferMemoryRequirements(stagingBuffer)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
328 let memoryType = BestMemory(mappable = true, filter = memoryRequirements.memoryTypes)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
329 let stagingMemory = svkAllocateMemory(memoryRequirements.size, memoryType)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
330 checkVkResult vkMapMemory(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
331 device = vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
332 memory = stagingMemory,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
333 offset = 0'u64,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
334 size = VK_WHOLE_SIZE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
335 flags = VkMemoryMapFlags(0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
336 ppData = addr(`dataPointer`)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
337 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
338 checkVkResult vkBindBufferMemory(vulkan.device, stagingBuffer, stagingMemory, 0)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
339
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
340 block:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
341 # usually: write data to dataPointer in body
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
342 body
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
343
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
344 var stagingRange = VkMappedMemoryRange(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
345 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
346 memory: stagingMemory,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
347 size: VK_WHOLE_SIZE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
348 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
349 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(stagingRange))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
350
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
351 WithSingleUseCommandBuffer(commandBuffer):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
352 when T is (VkBuffer, uint64):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
353 let copyRegion = VkBufferCopy(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
354 size: bufferSize,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
355 dstOffset: target[1],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
356 srcOffset: 0
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
357 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
358 vkCmdCopyBuffer(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
359 commandBuffer = commandBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
360 srcBuffer = stagingBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
361 dstBuffer = target[0],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
362 regionCount = 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
363 pRegions = addr(copyRegion)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
364 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
365 elif T is (VkImage, uint32, uint32):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
366 let region = VkBufferImageCopy(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
367 bufferOffset: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
368 bufferRowLength: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
369 bufferImageHeight: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
370 imageSubresource: VkImageSubresourceLayers(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
371 aspectMask: toBits [VK_IMAGE_ASPECT_COLOR_BIT],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
372 mipLevel: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
373 baseArrayLayer: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
374 layerCount: 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
375 ),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
376 imageOffset: VkOffset3D(x: 0, y: 0, z: 0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
377 imageExtent: VkExtent3D(width: target[1], height: target[2], depth: 1)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
378 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
379 vkCmdCopyBufferToImage(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
380 commandBuffer = commandBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
381 srcBuffer = stagingBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
382 dstImage = target[0],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
383 dstImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
384 regionCount = 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
385 pRegions = addr(region)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
386 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
387
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
388 vkDestroyBuffer(vulkan.device, stagingBuffer, nil)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
389 vkFreeMemory(vulkan.device, stagingMemory, nil)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
390