annotate vulkan_utils.nim @ 1188:427c11c0aeb1 compiletime-tests

add: missing file
author sam <sam@basx.dev>
date Sun, 07 Jul 2024 15:09:14 +0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1188
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
1 import std/strformat
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
2
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine/core/vulkanapi
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
4
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
5 type
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
6 VulkanGlobals* = object
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
7 instance*: VkInstance
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
8 device*: VkDevice
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
9 physicalDevice*: VkPhysicalDevice
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
10 queueFamilyIndex*: uint32
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
11 queue*: VkQueue
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
12 anisotropy*: float32 = 0 # needs to be enable during device creation
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
13
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
14 var vulkan*: VulkanGlobals
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
15
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
16 proc svkGetPhysicalDeviceProperties*(): VkPhysicalDeviceProperties =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
17 vkGetPhysicalDeviceProperties(vulkan.physicalDevice, addr(result))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
18
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
19 proc svkCreateBuffer*(size: uint64, usage: openArray[VkBufferUsageFlagBits]): VkBuffer =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
20 var createInfo = VkBufferCreateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
21 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
22 flags: VkBufferCreateFlags(0),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
23 size: size,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
24 usage: usage.toBits,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
25 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
26 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
27 checkVkResult vkCreateBuffer(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
28 device = vulkan.device,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
29 pCreateInfo = addr(createInfo),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
30 pAllocator = nil,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
31 pBuffer = addr(result),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
32 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
33
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
34 proc svkAllocateMemory*(size: uint64, typeIndex: uint32): VkDeviceMemory =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
35 var memoryAllocationInfo = VkMemoryAllocateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
36 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
37 allocationSize: size,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
38 memoryTypeIndex: typeIndex,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
39 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
40 checkVkResult vkAllocateMemory(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
41 vulkan.device,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
42 addr(memoryAllocationInfo),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
43 nil,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
44 addr(result),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
45 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
46
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
47 proc svkCreate2DImage*(width, height: uint32, format: VkFormat, usage: openArray[VkImageUsageFlagBits]): VkImage =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
48 var imageProps: VkImageFormatProperties
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
49 checkVkResult vkGetPhysicalDeviceImageFormatProperties(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
50 vulkan.physicalDevice,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
51 format,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
52 VK_IMAGE_TYPE_2D,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
53 VK_IMAGE_TILING_OPTIMAL,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
54 usage.toBits,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
55 VkImageCreateFlags(0),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
56 addr(imageProps)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
57 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
58
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
59 var imageInfo = VkImageCreateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
60 sType: VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
61 imageType: VK_IMAGE_TYPE_2D,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
62 extent: VkExtent3D(width: width, height: height, depth: 1),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
63 mipLevels: min(1'u32, imageProps.maxMipLevels),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
64 arrayLayers: min(1'u32, imageProps.maxArrayLayers),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
65 format: format,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
66 tiling: VK_IMAGE_TILING_OPTIMAL,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
67 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
68 usage: usage.toBits,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
69 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
70 samples: VK_SAMPLE_COUNT_1_BIT,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
71 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
72 checkVkResult vkCreateImage(vulkan.device, addr imageInfo, nil, addr(result))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
73
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
74 proc svkGetDeviceQueue*(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
75 vkGetDeviceQueue(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
76 device,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
77 queueFamilyIndex,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
78 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
79 addr(result),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
80 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
81
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
82 proc svkGetBufferMemoryRequirements*(buffer: VkBuffer): tuple[size: uint64, alignment: uint64, memoryTypes: seq[uint32]] =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
83 var reqs: VkMemoryRequirements
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
84 vkGetBufferMemoryRequirements(vulkan.device, buffer, addr(reqs))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
85 result.size = reqs.size
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
86 result.alignment = reqs.alignment
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
87 for i in 0'u32 ..< VK_MAX_MEMORY_TYPES:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
88 if ((1'u32 shl i) and reqs.memoryTypeBits) > 0:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
89 result.memoryTypes.add i
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
90
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
91 proc svkGetImageMemoryRequirements*(image: VkImage): tuple[size: uint64, alignment: uint64, memoryTypes: seq[uint32]] =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
92 var reqs: VkMemoryRequirements
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
93 vkGetImageMemoryRequirements(vulkan.device, image, addr(reqs))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
94 result.size = reqs.size
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
95 result.alignment = reqs.alignment
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
96 for i in 0'u32 ..< VK_MAX_MEMORY_TYPES:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
97 if ((1'u32 shl i) and reqs.memoryTypeBits) > 0:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
98 result.memoryTypes.add i
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
99
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
100 proc BestMemory*(mappable: bool, filter: seq[uint32] = @[]): uint32 =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
101 var physicalProperties: VkPhysicalDeviceMemoryProperties
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
102 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
103
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
104 var maxScore: float = -1
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
105 var maxIndex: uint32 = 0
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
106 for index in 0'u32 ..< physicalProperties.memoryTypeCount:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
107 if filter.len == 0 or index in filter:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
108 let flags = toEnums(physicalProperties.memoryTypes[index].propertyFlags)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
109 if not mappable or VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
110 var score: float = 0
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
111 if VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT in flags: score += 1_000_000
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
112 if VK_MEMORY_PROPERTY_HOST_CACHED_BIT in flags: score += 1_000
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
113 score += float(physicalProperties.memoryHeaps[physicalProperties.memoryTypes[index].heapIndex].size) / 1_000_000_000
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
114 if score > maxScore:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
115 maxScore = score
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
116 maxIndex = index
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
117 assert maxScore > 0, &"Unable to find memory type (mappable: {mappable}, filter: {filter})"
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
118 return maxIndex
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
119
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
120 template WithSingleUseCommandBuffer*(cmd, body: untyped): untyped =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
121 block:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
122 var
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
123 commandBufferPool: VkCommandPool
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
124 createInfo = VkCommandPoolCreateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
125 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
126 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
127 queueFamilyIndex: vulkan.queueFamilyIndex,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
128 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
129 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr(commandBufferPool))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
130 var
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
131 `cmd` {.inject.}: VkCommandBuffer
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
132 allocInfo = VkCommandBufferAllocateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
133 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
134 commandPool: commandBufferPool,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
135 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
136 commandBufferCount: 1,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
137 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
138 checkVkResult vulkan.device.vkAllocateCommandBuffers(addr allocInfo, addr(`cmd`))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
139 var beginInfo = VkCommandBufferBeginInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
140 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
141 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
142 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
143 checkVkResult `cmd`.vkBeginCommandBuffer(addr beginInfo)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
144
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
145 body
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
146
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
147 checkVkResult `cmd`.vkEndCommandBuffer()
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
148 var submitInfo = VkSubmitInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
149 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
150 commandBufferCount: 1,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
151 pCommandBuffers: addr(`cmd`),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
152 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
153
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
154 var
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
155 fence: VkFence
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
156 fenceInfo = VkFenceCreateInfo(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
157 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
158 # flags: toBits [VK_FENCE_CREATE_SIGNALED_BIT]
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
159 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
160 checkVkResult vulkan.device.vkCreateFence(addr(fenceInfo), nil, addr(fence))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
161 checkVkResult vkQueueSubmit(vulkan.queue, 1, addr(submitInfo), fence)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
162 checkVkResult vkWaitForFences(vulkan.device, 1, addr fence, false, high(uint64))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
163 vkDestroyCommandPool(vulkan.device, commandBufferPool, nil)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
164
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
165 template WithStagingBuffer*[T: (VkBuffer, uint64)|(VkImage, uint32, uint32)](
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
166 target: T,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
167 bufferSize: uint64,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
168 dataPointer,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
169 body: untyped
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
170 ): untyped =
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
171 var `dataPointer` {.inject.}: pointer
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
172 let stagingBuffer = svkCreateBuffer(bufferSize, [VK_BUFFER_USAGE_TRANSFER_SRC_BIT])
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
173 let memoryRequirements = svkGetBufferMemoryRequirements(stagingBuffer)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
174 let memoryType = BestMemory(mappable = true, filter = memoryRequirements.memoryTypes)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
175 let stagingMemory = svkAllocateMemory(memoryRequirements.size, memoryType)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
176 checkVkResult vkMapMemory(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
177 device = vulkan.device,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
178 memory = stagingMemory,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
179 offset = 0'u64,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
180 size = VK_WHOLE_SIZE,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
181 flags = VkMemoryMapFlags(0),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
182 ppData = addr(`dataPointer`)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
183 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
184 checkVkResult vkBindBufferMemory(vulkan.device, stagingBuffer, stagingMemory, 0)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
185
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
186 block:
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
187 # usually: write data to dataPointer in body
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
188 body
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
189
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
190 var stagingRange = VkMappedMemoryRange(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
191 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
192 memory: stagingMemory,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
193 size: VK_WHOLE_SIZE,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
194 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
195 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(stagingRange))
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
196
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
197 WithSingleUseCommandBuffer(commandBuffer):
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
198 when T is (VkBuffer, uint64):
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
199 let copyRegion = VkBufferCopy(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
200 size: bufferSize,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
201 dstOffset: target[1],
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
202 srcOffset: 0
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
203 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
204 vkCmdCopyBuffer(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
205 commandBuffer = commandBuffer,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
206 srcBuffer = stagingBuffer,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
207 dstBuffer = target[0],
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
208 regionCount = 1,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
209 pRegions = addr(copyRegion)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
210 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
211 elif T is (VkImage, uint32, uint32):
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
212 let region = VkBufferImageCopy(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
213 bufferOffset: 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
214 bufferRowLength: 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
215 bufferImageHeight: 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
216 imageSubresource: VkImageSubresourceLayers(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
217 aspectMask: toBits [VK_IMAGE_ASPECT_COLOR_BIT],
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
218 mipLevel: 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
219 baseArrayLayer: 0,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
220 layerCount: 1,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
221 ),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
222 imageOffset: VkOffset3D(x: 0, y: 0, z: 0),
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
223 imageExtent: VkExtent3D(width: target[1], height: target[2], depth: 1)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
224 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
225 vkCmdCopyBufferToImage(
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
226 commandBuffer = commandBuffer,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
227 srcBuffer = stagingBuffer,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
228 dstImage = target[0],
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
229 dstImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
230 regionCount = 1,
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
231 pRegions = addr(region)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
232 )
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
233
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
234 vkDestroyBuffer(vulkan.device, stagingBuffer, nil)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
235 vkFreeMemory(vulkan.device, stagingMemory, nil)
427c11c0aeb1 add: missing file
sam <sam@basx.dev>
parents:
diff changeset
236