-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvulkanhelper.cxx
More file actions
363 lines (318 loc) · 14.9 KB
/
vulkanhelper.cxx
File metadata and controls
363 lines (318 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "vulkanhelper.hxx"
#include <string.h>
#include <stdio.h>
#if defined(__linux__)
#include <dlfcn.h>
#endif
#define DEFINE_VK_FUNCTION(name) \
PFN_##name name = nullptr
#define INIT_VK_GLOBAL_FUNCTION(methodname) \
do { \
methodname = reinterpret_cast<PFN_##methodname>(vkGetInstanceProcAddr(nullptr, #methodname)); \
if (!methodname) { \
printf("Could not get pointer for %s pointer\n", #methodname); \
} \
} while (0)
#define INIT_VK_INSTANCE_FUNCTION(methodname) \
do { \
methodname = reinterpret_cast<PFN_##methodname>(vkGetInstanceProcAddr(instance, #methodname)); \
if (!methodname) { \
printf("Could not get pointer for %s pointer\n", #methodname); \
} \
} while (0)
#define INIT_VK_DEVICE_FUNCTION(methodname) \
do { \
methodname = reinterpret_cast<PFN_##methodname>(vkGetDeviceProcAddr(device, #methodname)); \
if (!methodname) { \
printf("Could not get pointer for %s pointer\n", #methodname); \
} \
} while (0)
/*
* Vulkan Core functions
*/
DEFINE_VK_FUNCTION(vkGetInstanceProcAddr);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceMemoryProperties);
DEFINE_VK_FUNCTION(vkGetDeviceProcAddr);
DEFINE_VK_FUNCTION(vkCreateShaderModule);
DEFINE_VK_FUNCTION(vkMapMemory);
DEFINE_VK_FUNCTION(vkUnmapMemory);
DEFINE_VK_FUNCTION(vkCreateImage);
DEFINE_VK_FUNCTION(vkGetImageMemoryRequirements);
DEFINE_VK_FUNCTION(vkAllocateMemory);
DEFINE_VK_FUNCTION(vkBindImageMemory);
DEFINE_VK_FUNCTION(vkCreateImageView);
DEFINE_VK_FUNCTION(vkAllocateCommandBuffers);
DEFINE_VK_FUNCTION(vkBeginCommandBuffer);
DEFINE_VK_FUNCTION(vkUpdateDescriptorSets);
DEFINE_VK_FUNCTION(vkCmdPipelineBarrier);
DEFINE_VK_FUNCTION(vkEndCommandBuffer);
DEFINE_VK_FUNCTION(vkQueueSubmit);
DEFINE_VK_FUNCTION(vkQueueWaitIdle);
DEFINE_VK_FUNCTION(vkFreeCommandBuffers);
DEFINE_VK_FUNCTION(vkCreateBuffer);
DEFINE_VK_FUNCTION(vkGetBufferMemoryRequirements);
DEFINE_VK_FUNCTION(vkBindBufferMemory);
DEFINE_VK_FUNCTION(vkEnumerateInstanceLayerProperties);
DEFINE_VK_FUNCTION(vkEnumerateInstanceExtensionProperties);
DEFINE_VK_FUNCTION(vkCreateInstance);
DEFINE_VK_FUNCTION(vkEnumeratePhysicalDevices);
DEFINE_VK_FUNCTION(vkEnumerateDeviceLayerProperties);
DEFINE_VK_FUNCTION(vkEnumerateDeviceExtensionProperties);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceFeatures);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceFeatures2);
DEFINE_VK_FUNCTION(vkCreateDevice);
DEFINE_VK_FUNCTION(vkGetDeviceQueue);
DEFINE_VK_FUNCTION(vkCreateCommandPool);
DEFINE_VK_FUNCTION(vkCreateDescriptorPool);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceProperties);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceProperties2);
DEFINE_VK_FUNCTION(vkCreateDescriptorSetLayout);
DEFINE_VK_FUNCTION(vkAllocateDescriptorSets);
DEFINE_VK_FUNCTION(vkCreatePipelineLayout);
DEFINE_VK_FUNCTION(vkCreateRenderPass);
DEFINE_VK_FUNCTION(vkCreateFramebuffer);
DEFINE_VK_FUNCTION(vkCmdBindPipeline);
DEFINE_VK_FUNCTION(vkCmdBindDescriptorSets);
DEFINE_VK_FUNCTION(vkCmdCopyImage);
DEFINE_VK_FUNCTION(vkCreateSemaphore);
DEFINE_VK_FUNCTION(vkCreateFence);
DEFINE_VK_FUNCTION(vkWaitForFences);
DEFINE_VK_FUNCTION(vkResetFences);
DEFINE_VK_FUNCTION(vkGetBufferDeviceAddress);
DEFINE_VK_FUNCTION(vkDestroyFence);
/*
* Vulkan WSI functions
*/
#ifdef VK_USE_PLATFORM_WIN32_KHR
DEFINE_VK_FUNCTION(vkCreateWin32SurfaceKHR);
#endif
#ifdef VK_USE_PLATFORM_XCB_KHR
DEFINE_VK_FUNCTION(vkCreateXcbSurfaceKHR);
#endif
/*
* Vulkan Surface extension functions
*/
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceSurfaceFormatsKHR);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
DEFINE_VK_FUNCTION(vkGetPhysicalDeviceSurfacePresentModesKHR);
DEFINE_VK_FUNCTION(vkCreateSwapchainKHR);
DEFINE_VK_FUNCTION(vkGetSwapchainImagesKHR);
DEFINE_VK_FUNCTION(vkAcquireNextImageKHR);
DEFINE_VK_FUNCTION(vkQueuePresentKHR);
/*
* Vulkan NVIDIA Raytracing extension functions
*/
DEFINE_VK_FUNCTION(vkCreateAccelerationStructureNV);
DEFINE_VK_FUNCTION(vkDestroyAccelerationStructureNV);
DEFINE_VK_FUNCTION(vkGetAccelerationStructureMemoryRequirementsNV);
DEFINE_VK_FUNCTION(vkBindAccelerationStructureMemoryNV);
DEFINE_VK_FUNCTION(vkCmdBuildAccelerationStructureNV);
DEFINE_VK_FUNCTION(vkCmdCopyAccelerationStructureNV);
DEFINE_VK_FUNCTION(vkCmdTraceRaysNV);
DEFINE_VK_FUNCTION(vkCreateRayTracingPipelinesNV);
DEFINE_VK_FUNCTION(vkGetRayTracingShaderGroupHandlesNV);
DEFINE_VK_FUNCTION(vkGetAccelerationStructureHandleNV);
DEFINE_VK_FUNCTION(vkCmdWriteAccelerationStructuresPropertiesNV);
DEFINE_VK_FUNCTION(vkCompileDeferredNV);
/*
* Vulkan KHR Acceleration Structure extension functions
*/
DEFINE_VK_FUNCTION(vkBuildAccelerationStructuresKHR);
DEFINE_VK_FUNCTION(vkCmdBuildAccelerationStructuresIndirectKHR);
DEFINE_VK_FUNCTION(vkCmdBuildAccelerationStructuresKHR);
DEFINE_VK_FUNCTION(vkCmdCopyAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkCmdCopyAccelerationStructureToMemoryKHR);
DEFINE_VK_FUNCTION(vkCmdCopyMemoryToAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkCmdWriteAccelerationStructuresPropertiesKHR);
DEFINE_VK_FUNCTION(vkCopyAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkCopyAccelerationStructureToMemoryKHR);
DEFINE_VK_FUNCTION(vkCopyMemoryToAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkCreateAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkDestroyAccelerationStructureKHR);
DEFINE_VK_FUNCTION(vkGetAccelerationStructureBuildSizesKHR);
DEFINE_VK_FUNCTION(vkGetAccelerationStructureDeviceAddressKHR);
DEFINE_VK_FUNCTION(vkGetDeviceAccelerationStructureCompatibilityKHR);
DEFINE_VK_FUNCTION(vkWriteAccelerationStructuresPropertiesKHR);
/*
* Vulkan KHR Ray Tracing Pipeline extension functions
*/
DEFINE_VK_FUNCTION(vkCmdSetRayTracingPipelineStackSizeKHR);
DEFINE_VK_FUNCTION(vkCmdTraceRaysIndirectKHR);
DEFINE_VK_FUNCTION(vkCmdTraceRaysKHR);
DEFINE_VK_FUNCTION(vkCreateRayTracingPipelinesKHR);
DEFINE_VK_FUNCTION(vkGetRayTracingCaptureReplayShaderGroupHandlesKHR);
DEFINE_VK_FUNCTION(vkGetRayTracingShaderGroupHandlesKHR);
DEFINE_VK_FUNCTION(vkGetRayTracingShaderGroupStackSizeKHR);
static void initVulkanDynamicLoadLibrary() {
#if defined(WIN32)
HMODULE vulkanLibrary = LoadLibrary("vulkan-1.dll");
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)GetProcAddress(vulkanLibrary, "vkGetInstanceProcAddr");
#elif defined(__linux__)
void* vulkanLibrary = dlopen("libvulkan.so", RTLD_NOW);
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)dlsym(vulkanLibrary, "vkGetInstanceProcAddr");
#endif
}
CVulkanHelper::CVulkanHelper(VkInstance instance, VkDevice device, VkPhysicalDevice gpu)
: m_instance(instance)
, m_device(device)
, m_gpu(gpu)
{
vkGetPhysicalDeviceMemoryProperties(gpu, &m_gpuMemoryProperties);
}
void CVulkanHelper::initVulkan() {
initVulkanDynamicLoadLibrary();
INIT_VK_GLOBAL_FUNCTION(vkEnumerateInstanceLayerProperties);
INIT_VK_GLOBAL_FUNCTION(vkEnumerateInstanceExtensionProperties);
INIT_VK_GLOBAL_FUNCTION(vkCreateInstance);
}
void CVulkanHelper::initVulkanInstanceFunctions(VkInstance instance) {
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceFeatures);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceFeatures2);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties2);
INIT_VK_INSTANCE_FUNCTION(vkEnumeratePhysicalDevices);
INIT_VK_INSTANCE_FUNCTION(vkCreateDevice);
INIT_VK_INSTANCE_FUNCTION(vkGetDeviceProcAddr);
INIT_VK_INSTANCE_FUNCTION(vkEnumerateDeviceLayerProperties);
INIT_VK_INSTANCE_FUNCTION(vkEnumerateDeviceExtensionProperties);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceMemoryProperties);
#ifdef VK_USE_PLATFORM_WIN32_KHR
INIT_VK_INSTANCE_FUNCTION(vkCreateWin32SurfaceKHR);
#endif
#ifdef VK_USE_PLATFORM_XCB_KHR
INIT_VK_INSTANCE_FUNCTION(vkCreateXcbSurfaceKHR);
#endif
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceFormatsKHR);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
INIT_VK_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfacePresentModesKHR);
}
void CVulkanHelper::initVulkanDeviceFunctions(VkDevice device) {
INIT_VK_DEVICE_FUNCTION(vkCreateShaderModule);
INIT_VK_DEVICE_FUNCTION(vkMapMemory);
INIT_VK_DEVICE_FUNCTION(vkUnmapMemory);
INIT_VK_DEVICE_FUNCTION(vkCreateImage);
INIT_VK_DEVICE_FUNCTION(vkGetImageMemoryRequirements);
INIT_VK_DEVICE_FUNCTION(vkAllocateMemory);
INIT_VK_DEVICE_FUNCTION(vkBindImageMemory);
INIT_VK_DEVICE_FUNCTION(vkCreateImageView);
INIT_VK_DEVICE_FUNCTION(vkAllocateCommandBuffers);
INIT_VK_DEVICE_FUNCTION(vkBeginCommandBuffer);
INIT_VK_DEVICE_FUNCTION(vkUpdateDescriptorSets);
INIT_VK_DEVICE_FUNCTION(vkCmdPipelineBarrier);
INIT_VK_DEVICE_FUNCTION(vkEndCommandBuffer);
INIT_VK_DEVICE_FUNCTION(vkQueueSubmit);
INIT_VK_DEVICE_FUNCTION(vkQueueWaitIdle);
INIT_VK_DEVICE_FUNCTION(vkFreeCommandBuffers);
INIT_VK_DEVICE_FUNCTION(vkCreateBuffer);
INIT_VK_DEVICE_FUNCTION(vkGetBufferMemoryRequirements);
INIT_VK_DEVICE_FUNCTION(vkBindBufferMemory);
INIT_VK_DEVICE_FUNCTION(vkGetDeviceQueue);
INIT_VK_DEVICE_FUNCTION(vkCreateCommandPool);
INIT_VK_DEVICE_FUNCTION(vkCreateDescriptorPool);
INIT_VK_DEVICE_FUNCTION(vkCreateDescriptorSetLayout);
INIT_VK_DEVICE_FUNCTION(vkAllocateDescriptorSets);
INIT_VK_DEVICE_FUNCTION(vkCreatePipelineLayout);
INIT_VK_DEVICE_FUNCTION(vkCreateRenderPass);
INIT_VK_DEVICE_FUNCTION(vkCreateFramebuffer);
INIT_VK_DEVICE_FUNCTION(vkCmdBindPipeline);
INIT_VK_DEVICE_FUNCTION(vkCmdBindDescriptorSets);
INIT_VK_DEVICE_FUNCTION(vkCmdCopyImage);
INIT_VK_DEVICE_FUNCTION(vkCreateSemaphore);
INIT_VK_DEVICE_FUNCTION(vkCreateFence);
INIT_VK_DEVICE_FUNCTION(vkWaitForFences);
INIT_VK_DEVICE_FUNCTION(vkResetFences);
INIT_VK_DEVICE_FUNCTION(vkGetBufferDeviceAddress);
INIT_VK_DEVICE_FUNCTION(vkDestroyFence);
INIT_VK_DEVICE_FUNCTION(vkCreateSwapchainKHR);
INIT_VK_DEVICE_FUNCTION(vkGetSwapchainImagesKHR);
INIT_VK_DEVICE_FUNCTION(vkAcquireNextImageKHR);
INIT_VK_DEVICE_FUNCTION(vkQueuePresentKHR);
INIT_VK_DEVICE_FUNCTION(vkCreateAccelerationStructureNV);
INIT_VK_DEVICE_FUNCTION(vkDestroyAccelerationStructureNV);
INIT_VK_DEVICE_FUNCTION(vkGetAccelerationStructureMemoryRequirementsNV);
INIT_VK_DEVICE_FUNCTION(vkBindAccelerationStructureMemoryNV);
INIT_VK_DEVICE_FUNCTION(vkCmdBuildAccelerationStructureNV);
INIT_VK_DEVICE_FUNCTION(vkCmdCopyAccelerationStructureNV);
INIT_VK_DEVICE_FUNCTION(vkCmdTraceRaysNV);
INIT_VK_DEVICE_FUNCTION(vkCreateRayTracingPipelinesNV);
INIT_VK_DEVICE_FUNCTION(vkGetRayTracingShaderGroupHandlesNV);
INIT_VK_DEVICE_FUNCTION(vkGetAccelerationStructureHandleNV);
INIT_VK_DEVICE_FUNCTION(vkCmdWriteAccelerationStructuresPropertiesNV);
INIT_VK_DEVICE_FUNCTION(vkCompileDeferredNV);
INIT_VK_DEVICE_FUNCTION(vkBuildAccelerationStructuresKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdBuildAccelerationStructuresIndirectKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdBuildAccelerationStructuresKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdCopyAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdCopyAccelerationStructureToMemoryKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdCopyMemoryToAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdWriteAccelerationStructuresPropertiesKHR);
INIT_VK_DEVICE_FUNCTION(vkCopyAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkCopyAccelerationStructureToMemoryKHR);
INIT_VK_DEVICE_FUNCTION(vkCopyMemoryToAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkCreateAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkDestroyAccelerationStructureKHR);
INIT_VK_DEVICE_FUNCTION(vkGetAccelerationStructureBuildSizesKHR);
INIT_VK_DEVICE_FUNCTION(vkGetAccelerationStructureDeviceAddressKHR);
INIT_VK_DEVICE_FUNCTION(vkGetDeviceAccelerationStructureCompatibilityKHR);
INIT_VK_DEVICE_FUNCTION(vkWriteAccelerationStructuresPropertiesKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdSetRayTracingPipelineStackSizeKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdTraceRaysIndirectKHR);
INIT_VK_DEVICE_FUNCTION(vkCmdTraceRaysKHR);
INIT_VK_DEVICE_FUNCTION(vkCreateRayTracingPipelinesKHR);
INIT_VK_DEVICE_FUNCTION(vkGetRayTracingCaptureReplayShaderGroupHandlesKHR);
INIT_VK_DEVICE_FUNCTION(vkGetRayTracingShaderGroupHandlesKHR);
INIT_VK_DEVICE_FUNCTION(vkGetRayTracingShaderGroupStackSizeKHR);
}
uint32_t CVulkanHelper::getMemoryType(VkMemoryRequirements& memoryRequirements,
VkMemoryPropertyFlags memoryProperties) {
uint32_t memoryType = 0;
for (uint32_t memoryTypeIndex = 0; memoryTypeIndex < VK_MAX_MEMORY_TYPES; ++memoryTypeIndex) {
if (memoryRequirements.memoryTypeBits & (1 << memoryTypeIndex)) {
if ((m_gpuMemoryProperties.memoryTypes[memoryTypeIndex].propertyFlags & memoryProperties) == memoryProperties) {
memoryType = memoryTypeIndex;
break;
}
}
}
return memoryType;
}
VulkanBuffer CVulkanHelper::createBuffer(VkBufferUsageFlags usage,
VkDeviceSize size,
VkMemoryPropertyFlags memoryProperties) {
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
bufferInfo.usage = usage;
bufferInfo.size = size;
VkBuffer buffer;
vkCreateBuffer(m_device, &bufferInfo, nullptr, &buffer);
VkMemoryRequirements bufferMemoryRequirements;
vkGetBufferMemoryRequirements(m_device, buffer, &bufferMemoryRequirements);
uint32_t memoryType = getMemoryType(bufferMemoryRequirements, memoryProperties);
VkMemoryAllocateFlagsInfo memoryAllocFlagsInfo = {};
memoryAllocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO;
memoryAllocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo memoryAllocInfo = {};
memoryAllocInfo.pNext = &memoryAllocFlagsInfo;
memoryAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memoryAllocInfo.allocationSize = bufferMemoryRequirements.size;
memoryAllocInfo.memoryTypeIndex = memoryType;
VkDeviceMemory bufferMemory;
vkAllocateMemory(m_device, &memoryAllocInfo, nullptr, &bufferMemory);
vkBindBufferMemory(m_device, buffer, bufferMemory, 0);
VkBufferDeviceAddressInfo bufferDeviceAddressInfo = {};
bufferDeviceAddressInfo.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
bufferDeviceAddressInfo.buffer = buffer;
VkDeviceAddress address = vkGetBufferDeviceAddress(m_device, &bufferDeviceAddressInfo);
return {buffer, bufferMemory, size, address};
}
void CVulkanHelper::copyToBuffer(const VulkanBuffer &buffer, void* data, uint32_t size) {
void* localData = nullptr;
vkMapMemory(m_device, buffer.memory, 0, buffer.size, 0, &localData);
memcpy(localData, data, size);
vkUnmapMemory(m_device, buffer.memory);
}