Mercurial > games > semicongine
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 |
rev | line source |
---|---|
1192 | 1 proc GetBestPhysicalDevice(instance: VkInstance): VkPhysicalDevice = |
2 var nDevices: uint32 | |
3 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil) | |
4 var devices = newSeq[VkPhysicalDevice](nDevices) | |
5 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), devices.ToCPointer) | |
6 | |
7 var score = 0'u32 | |
8 for pDevice in devices: | |
9 var props: VkPhysicalDeviceProperties | |
10 # CANNOT use svkGetPhysicalDeviceProperties (not initialized yet) | |
11 vkGetPhysicalDeviceProperties(pDevice, addr(props)) | |
12 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU and props.limits.maxImageDimension2D > score: | |
13 score = props.limits.maxImageDimension2D | |
14 result = pDevice | |
15 | |
16 if score == 0: | |
17 for pDevice in devices: | |
18 var props: VkPhysicalDeviceProperties | |
19 # CANNOT use svkGetPhysicalDeviceProperties (not initialized yet) | |
20 vkGetPhysicalDeviceProperties(pDevice, addr(props)) | |
21 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU and props.limits.maxImageDimension2D > score: | |
22 score = props.limits.maxImageDimension2D | |
23 result = pDevice | |
24 | |
25 assert score > 0, "Unable to find integrated or discrete GPU" | |
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 | 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 | 32 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 = |
33 var nQueuefamilies: uint32 | |
34 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, nil) | |
35 var queuFamilies = newSeq[VkQueueFamilyProperties](nQueuefamilies) | |
36 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, queuFamilies.ToCPointer) | |
37 for i in 0'u32 ..< nQueuefamilies: | |
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 | 42 assert false, &"Queue of type {qType} not found" |
43 | |
44 proc svkGetDeviceQueue*(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue = | |
45 vkGetDeviceQueue( | |
46 device, | |
47 queueFamilyIndex, | |
48 0, | |
49 addr(result), | |
50 ) | |
51 | |
1194 | 52 proc DefaultSurfaceFormat(): VkFormat = |
53 # EVERY windows driver and almost every linux driver should support this | |
54 VK_FORMAT_B8G8R8A8_SRGB | |
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 | 62 proc svkGetPhysicalDeviceSurfacePresentModesKHR*(): seq[VkPresentModeKHR] = |
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 | 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 | 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 | 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 | 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 | 73 |
1191 | 74 proc hasValidationLayer*(): bool = |
75 var n_layers: uint32 | |
76 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), nil) | |
77 if n_layers > 0: | |
78 var layers = newSeq[VkLayerProperties](n_layers) | |
79 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), layers.ToCPointer) | |
80 for layer in layers: | |
81 if layer.layerName.CleanString == "VK_LAYER_KHRONOS_validation": | |
82 return true | |
83 return false | |
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 | 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 | 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 | 143 proc svkCreate2DImageView(image: VkImage, format: VkFormat): VkImageView = |
144 var createInfo = VkImageViewCreateInfo( | |
145 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | |
146 image: image, | |
147 viewType: VK_IMAGE_VIEW_TYPE_2D, | |
148 format: format, | |
149 components: VkComponentMapping( | |
150 r: VK_COMPONENT_SWIZZLE_IDENTITY, | |
151 g: VK_COMPONENT_SWIZZLE_IDENTITY, | |
152 b: VK_COMPONENT_SWIZZLE_IDENTITY, | |
153 a: VK_COMPONENT_SWIZZLE_IDENTITY, | |
154 ), | |
155 subresourceRange: VkImageSubresourceRange( | |
156 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT), | |
157 baseMipLevel: 0, | |
158 levelCount: 1, | |
159 baseArrayLayer: 0, | |
160 layerCount: 1, | |
161 ), | |
162 ) | |
163 checkVkResult vkCreateImageView(vulkan.device, addr(createInfo), nil, addr(result)) | |
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 | 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 | 206 proc Await*(fence: VkFence, timeout = high(uint64)): bool = |
207 let waitResult = vkWaitForFences(vulkan.device, 1, addr(fence), false, timeout) | |
208 if waitResult == VK_TIMEOUT: | |
209 return false | |
210 checkVkResult waitResult | |
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 | 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 | 285 flags: VkCommandPoolCreateFlags(0), |
1192 | 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 | 314 checkVkResult vkQueueSubmit(vulkan.graphicsQueue, 1, addr(submitInfo), fence) |
1199 | 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 |