c1ca1d56d856e6e2bef5ae63660a777683c9d3da
[demo] / src / vulkan / vkutil.cc
1 #include <gmath/gmath.h>
2 #include <vulkan/vulkan.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <string>
8 #include <vector>
9
10 #include "allocator.h"
11 #include "vkutil.h"
12
13 /* global variables */
14 VkPhysicalDevice vk_physical;
15 VkDevice vk_device;
16 VkInstance vk_instance;
17 VkQueue vk_queue;
18 int vk_qfamily;
19 VkCommandPool vk_pool;
20 VkSurfaceKHR vk_surface;
21 VkSwapchainKHR vk_swapchain;
22 VkDescriptorPool vk_dpool;
23
24 /* static functions */
25 static const char *get_device_name_str(VkPhysicalDeviceType type);
26 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
27 static const char *get_queue_flags_str(VkQueueFlags flags);
28 static const char *get_mem_size_str(long sz);
29 static int ver_major(uint32_t ver);
30 static int ver_minor(uint32_t ver);
31 static int ver_patch(uint32_t ver);
32
33 /* static variables */
34 static VkPhysicalDevice *phys_devices;
35
36 static int sel_dev;
37 static int sel_qfamily;
38
39 static VkExtensionProperties *vkext, *vkdevext;
40 static uint32_t vkext_count, vkdevext_count;
41
42 bool vku_have_extension(const char *name)
43 {
44         if(!vkext) {
45                 vkext_count = 0;
46                 vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
47                 if(vkext_count) {
48                         vkext = new VkExtensionProperties[vkext_count];
49                         vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
50
51                         printf("instance extensions:\n");
52                         for(int i=0; i<(int)vkext_count; i++) {
53                                 printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
54                         }
55                 }
56         }
57
58         for(int i=0; i<(int)vkext_count; i++) {
59                 if(strcmp(vkext[i].extensionName, name) == 0) {
60                         return true;
61                 }
62         }
63         return false;
64 }
65
66 bool vku_have_device_extension(const char *name)
67 {
68         if(sel_dev < 0) return false;
69
70         if(!vkdevext) {
71                 vkdevext_count = 0;
72                 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
73                 if(vkdevext_count) {
74                         vkdevext = new VkExtensionProperties[vkdevext_count];
75                         vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
76
77                         printf("selected device extensions:\n");
78                         for(int i=0; i<(int)vkdevext_count; i++) {
79                                 printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
80                         }
81                 }
82         }
83
84         for(int i=0; i<(int)vkdevext_count; i++) {
85                 if(strcmp(vkdevext[i].extensionName, name) == 0) {
86                         return true;
87                 }
88         }
89         return false;
90 }
91
92 bool vku_create_device()
93 {
94         VkInstanceCreateInfo inst_info;
95         VkDeviceCreateInfo dev_info;
96         VkDeviceQueueCreateInfo queue_info;
97         VkCommandPoolCreateInfo cmdpool_info;
98         uint32_t num_devices;
99         float qprio = 0.0f;
100
101         static const char *ext_names[] = {
102                 "VK_KHR_xcb_surface",
103                 "VK_KHR_surface"
104         };
105
106         static const char *devext_names[] = {
107                 "VK_KHR_swapchain"
108         };
109
110         sel_dev = -1;
111         sel_qfamily = -1;
112
113         for(unsigned int i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
114                 if(!vku_have_extension(ext_names[i])) {
115                         fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
116                         return false;
117                 }
118         }
119         memset(&inst_info, 0, sizeof inst_info);
120         inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
121         inst_info.ppEnabledExtensionNames = ext_names;
122         inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
123
124         if(vkCreateInstance(&inst_info, 0, &vk_instance) != 0) {
125                 fprintf(stderr, "failed to create vulkan instance\n");
126                 return false;
127         }
128         printf("created vulkan instance\n");
129         if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, 0) != 0) {
130                 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
131                 return false;
132         }
133         phys_devices = new VkPhysicalDevice[num_devices];
134         if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, phys_devices) != 0) {
135                 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
136                 return false;
137         }
138         printf("found %u physical device(s)\n", (unsigned int)num_devices);
139
140         for(int i=0; i<(int)num_devices; i++) {
141                 VkPhysicalDeviceProperties dev_prop;
142                 VkPhysicalDeviceMemoryProperties mem_prop;
143                 VkQueueFamilyProperties *qprop;
144                 uint32_t qprop_count;
145
146                 vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
147
148                 printf("Device %d: %s\n", i, dev_prop.deviceName);
149                 printf("  type: %s\n", get_device_name_str(dev_prop.deviceType));
150                 printf("  API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
151                        ver_patch(dev_prop.apiVersion));
152                 printf("  driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
153                        ver_patch(dev_prop.driverVersion));
154                 printf("  vendor id: %x  device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
155
156
157                 vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
158                 printf("  %d memory heaps:\n", mem_prop.memoryHeapCount);
159                 for(unsigned int j=0; j<mem_prop.memoryHeapCount; j++) {
160                         VkMemoryHeap heap = mem_prop.memoryHeaps[j];
161                         printf("    Heap %d - size: %s, flags: %s\n", j, get_mem_size_str(heap.size),
162                                heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
163                 }
164                 printf("  %d memory types:\n", mem_prop.memoryTypeCount);
165                 for(unsigned int j=0; j<mem_prop.memoryTypeCount; j++) {
166                         VkMemoryType type = mem_prop.memoryTypes[j];
167                         printf("    Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
168                                get_memtype_flags_str(type.propertyFlags));
169                 }
170
171                 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
172                 if(qprop_count <= 0) {
173                         continue;
174                 }
175                 qprop = new VkQueueFamilyProperties[qprop_count];
176                 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
177
178                 for(unsigned int j=0; j<qprop_count; j++) {
179                         printf("  Queue family %d:\n", j);
180                         printf("    flags: %s\n", get_queue_flags_str(qprop[j].queueFlags));
181                         printf("    num queues: %u\n", qprop[j].queueCount);
182
183                         if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
184                                 sel_dev = i;
185                                 sel_qfamily = j;
186                         }
187                 }
188                 delete [] qprop;
189         }
190
191         if(sel_dev < 0 || sel_qfamily < 0) {
192                 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
193                 vkDestroyDevice(vk_device, 0);
194                 return false;
195         }
196
197         for(unsigned int i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
198                 if(!vku_have_device_extension(devext_names[i])) {
199                         fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
200                                 ext_names[i], sel_dev);
201                         return false;
202                 }
203         }
204
205         /* create device & command queue */
206         memset(&queue_info, 0, sizeof queue_info);
207         queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
208         queue_info.queueFamilyIndex = sel_qfamily;
209         queue_info.queueCount = 1;
210         queue_info.pQueuePriorities = &qprio;
211
212         memset(&dev_info, 0, sizeof dev_info);
213         dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
214         dev_info.queueCreateInfoCount = 1;
215         dev_info.pQueueCreateInfos = &queue_info;
216         dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
217         dev_info.ppEnabledExtensionNames = devext_names;
218
219         if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vk_device) != 0) {
220                 fprintf(stderr, "failed to create device %d\n", sel_dev);
221                 return false;
222         }
223         printf("created device %d\n", sel_dev);
224
225         vk_physical = phys_devices[sel_dev];
226         vk_qfamily = sel_qfamily;
227
228         vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue);
229
230         /* create command buffer pool */
231         memset(&cmdpool_info, 0, sizeof cmdpool_info);
232         cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
233         cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
234         cmdpool_info.queueFamilyIndex = sel_qfamily;
235
236         if(vkCreateCommandPool(vk_device, &cmdpool_info, 0, &vk_pool) != 0) {
237                 fprintf(stderr, "failed to get command queue!\n");
238                 return false;
239         }
240
241         /*      if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
242                         fprintf(stderr, "failed to create primary command buffer\n");
243                         return false;
244                 } */
245
246         return true;
247 }
248
249 void vku_cleanup()
250 {
251         if(vk_instance) {
252                 vkDeviceWaitIdle(vk_device);
253                 vkDestroyCommandPool(vk_device, vk_pool, 0);
254
255                 vkDestroyDevice(vk_device, 0);
256                 vkDestroyInstance(vk_instance, 0);
257                 vk_instance = 0;
258         }
259
260         delete [] phys_devices;
261         phys_devices = 0;
262 }
263
264 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
265 {
266         VkCommandBuffer cmdbuf;
267         VkCommandBufferAllocateInfo inf;
268
269         memset(&inf, 0, sizeof inf);
270         inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
271         inf.commandPool = pool;
272         inf.level = level;
273         inf.commandBufferCount = 1;
274
275         if(vkAllocateCommandBuffers(vk_device, &inf, &cmdbuf) != 0) {
276                 return 0;
277         }
278         return cmdbuf;
279 }
280
281 bool vku_alloc_cmdbufs(VkCommandPool pool, VkCommandBufferLevel level, unsigned int count, VkCommandBuffer *buf_array)
282 {
283         VkCommandBufferAllocateInfo cinf;
284         memset(&cinf, 0, sizeof cinf);
285
286         cinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
287         cinf.commandPool = vk_pool;
288         cinf.level = level;
289         cinf.commandBufferCount = count;
290
291         if(vkAllocateCommandBuffers(vk_device, &cinf, buf_array) != VK_SUCCESS) {
292                 fprintf(stderr, "Failed to allocate command buffer\n");
293                 return false;
294         }
295
296         return true;
297 }
298
299 void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
300 {
301         vkFreeCommandBuffers(vk_device, pool, 1, &buf);
302 }
303
304 void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
305 {
306         VkCommandBufferBeginInfo inf;
307
308         memset(&inf, 0, sizeof inf);
309         inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
310         inf.flags = flags;
311
312         vkBeginCommandBuffer(buf, &inf);
313 }
314
315
316 void vku_end_cmdbuf(VkCommandBuffer buf)
317 {
318         vkEndCommandBuffer(buf);
319 }
320
321 void vku_reset_cmdbuf(VkCommandBuffer buf)
322 {
323         vkResetCommandBuffer(buf, 0);
324 }
325
326 void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
327 {
328         VkSubmitInfo info;
329
330         memset(&info, 0, sizeof info);
331         info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
332         info.commandBufferCount = 1;
333         info.pCommandBuffers = &buf;
334
335         vkQueueSubmit(q, 1, &info, done_fence);
336 }
337
338 bool vku_get_surface_format(VkPhysicalDevice gpu, VkSurfaceKHR surface, VkSurfaceFormatKHR *sformat)
339 {
340         uint32_t fcount;
341
342         if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical,
343                                                 vk_surface, &fcount, 0) != VK_SUCCESS) {
344                 fprintf(stderr, "Failed to get format count for physical device.\n");
345                 return false;
346         }
347         if(fcount == 0) {
348                 fprintf(stderr, "No color formats were found.\n");
349                 return false;
350         }
351
352         VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount];
353         if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface,
354                                                 &fcount, formats) != VK_SUCCESS) {
355                 delete [] formats;
356                 fprintf(stderr, "Failed to get surface formats.\n");
357                 return false;
358         }
359
360         *sformat = formats[0];
361
362         if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) {
363                 sformat->format = VK_FORMAT_B8G8R8_UNORM;
364         }
365
366         delete [] formats;
367         return true;
368 }
369
370
371 int vku_get_next_image(VkSwapchainKHR sc)
372 {
373         uint32_t next;
374
375         if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) {
376                 return -1;
377         }
378         return (int)next;
379 }
380
381 void vku_present(VkSwapchainKHR sc, int img_idx)
382 {
383         VkPresentInfoKHR inf;
384         VkResult res;
385         uint32_t index = img_idx;
386
387         memset(&inf, 0, sizeof inf);
388         inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
389         inf.swapchainCount = 1;
390         inf.pSwapchains = &sc;
391         inf.pImageIndices = &index;
392         inf.pResults = &res;
393
394         vkQueuePresentKHR(vk_queue, &inf);
395 }
396
397 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
398 {
399         struct vku_buffer *buf;
400         VkBufferCreateInfo binfo;
401
402         buf = new vku_buffer;
403
404         memset(&binfo, 0, sizeof binfo);
405         binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
406         binfo.size = sz;
407         binfo.usage = usage;
408         binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
409
410         if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) {
411                 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
412                 return 0;
413         }
414
415         VkMemoryRequirements mr;
416         vkGetBufferMemoryRequirements(vk_device, buf->buf, &mr);
417
418         DevMemBlock block;
419         if(!vku_allocate(mr.size, &block))
420                 return 0;
421
422         buf->mem_pool = block.dev_mem;
423
424         return buf;
425 }
426
427 void vku_destroy_buffer(struct vku_buffer *buf)
428 {
429         if(buf) {
430                 //TODO change when the allocator changes
431                 vku_free(buf->mem_pool);
432
433                 vkDestroyBuffer(vk_device, buf->buf, 0);
434                 delete buf;
435         }
436 }
437
438 bool vku_update_buffer(vku_buffer *buf, int size, void *data)
439 {
440         uint8_t *pdata;
441         if(vkMapMemory(vk_device, buf->mem_pool, 0, size, 0, (void**)&pdata) != VK_SUCCESS) {
442                 fprintf(stderr, "Failed to map memory.\n");
443                 return false;
444         }
445         memcpy(pdata, data, size);
446         vkUnmapMemory(vk_device, buf->mem_pool);
447
448         return true;
449 }
450
451 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
452                      VkBuffer src, int soffs, int size)
453 {
454         VkBufferCopy copy;
455         copy.size = size;
456         copy.srcOffset = soffs;
457         copy.dstOffset = doffs;
458
459         vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
460 }
461
462 const char *vku_get_vulkan_error_str(VkResult error)
463 {
464         std::string errmsg;
465         switch(error) {
466         case VK_SUCCESS:
467                 errmsg = std::string("VK_SUCCESS");
468                 break;
469         case VK_NOT_READY:
470                 errmsg = std::string("VK_NOT_READY");
471                 break;
472         case VK_TIMEOUT:
473                 errmsg = std::string("VK_TIMEOUT");
474                 break;
475         case VK_EVENT_SET:
476                 errmsg = std::string("VK_EVENT_SET");
477                 break;
478         case VK_EVENT_RESET:
479                 errmsg = std::string("VK_EVENT_RESET");
480                 break;
481         case VK_INCOMPLETE:
482                 errmsg = std::string("VK_EVENT");
483                 break;
484         case VK_ERROR_OUT_OF_HOST_MEMORY:
485                 errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
486                 break;
487         case VK_ERROR_OUT_OF_DEVICE_MEMORY:
488                 errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
489                 break;
490         case VK_ERROR_INITIALIZATION_FAILED:
491                 errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
492                 break;
493         case VK_ERROR_DEVICE_LOST:
494                 errmsg = std::string("VK_ERROR_DEVICE_LOST");
495                 break;
496         case VK_ERROR_MEMORY_MAP_FAILED:
497                 errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
498                 break;
499         case VK_ERROR_LAYER_NOT_PRESENT:
500                 errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
501                 break;
502         case VK_ERROR_EXTENSION_NOT_PRESENT:
503                 errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
504                 break;
505         case VK_ERROR_FEATURE_NOT_PRESENT:
506                 errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
507         case VK_ERROR_INCOMPATIBLE_DRIVER:
508                 errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
509                 break;
510         case VK_ERROR_TOO_MANY_OBJECTS:
511                 errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
512                 break;
513         case VK_ERROR_FORMAT_NOT_SUPPORTED:
514                 errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
515                 break;
516         case VK_ERROR_FRAGMENTED_POOL:
517                 errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
518                 break;
519         default:
520                 errmsg = std::string("UNKNOWN");
521                 break;
522         }
523
524         return errmsg.c_str();
525 }
526
527
528 static const char *get_device_name_str(VkPhysicalDeviceType type)
529 {
530         switch(type) {
531         case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
532                 return "integrated GPU";
533         case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
534                 return "discrete GPU";
535         case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
536                 return "virtual GPU";
537         case VK_PHYSICAL_DEVICE_TYPE_CPU:
538                 return "CPU";
539         default:
540                 break;
541         }
542         return "unknown";
543 }
544
545 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
546 {
547         static char str[128];
548
549         str[0] = 0;
550         if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
551                 strcat(str, "device-local ");
552         }
553         if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
554                 strcat(str, "host-visible ");
555         }
556         if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
557                 strcat(str, "host-coherent ");
558         }
559         if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
560                 strcat(str, "host-cached ");
561         }
562         if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
563                 strcat(str, "lazily-allocated ");
564         }
565
566         if(!*str) {
567                 strcat(str, "-");
568         }
569         return str;
570 }
571
572 static const char *get_queue_flags_str(VkQueueFlags flags)
573 {
574         static char str[128];
575
576         str[0] = 0;
577         if(flags & VK_QUEUE_GRAPHICS_BIT) {
578                 strcat(str, "graphics ");
579         }
580         if(flags & VK_QUEUE_COMPUTE_BIT) {
581                 strcat(str, "compute ");
582         }
583         if(flags & VK_QUEUE_TRANSFER_BIT) {
584                 strcat(str, "transfer ");
585         }
586         if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
587                 strcat(str, "sparse-binding ");
588         }
589         if(!*str) {
590                 strcat(str, "-");
591         }
592         return str;
593 }
594
595 static int ver_major(uint32_t ver)
596 {
597         return (ver >> 22) & 0x3ff;
598 }
599
600 static int ver_minor(uint32_t ver)
601 {
602         return (ver >> 12) & 0x3ff;
603 }
604
605 static int ver_patch(uint32_t ver)
606 {
607         return ver & 0xfff;
608 }
609
610 static const char *get_mem_size_str(long sz)
611 {
612         static char str[64];
613         static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
614         int uidx = 0;
615         sz *= 10;
616
617         while(sz >= 10240 && unitstr[uidx + 1]) {
618                 sz /= 1024;
619                 ++uidx;
620         }
621         sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
622         return str;
623 }
624
625 vku_descriptor *vku_create_descriptor(VkDescriptorType type,
626                 VkFlags stage, int binding_point,
627                 int size, int num_desc)
628 {
629         vku_descriptor *desc = new vku_descriptor;
630
631         desc->type = type;
632         desc->size = size;
633         desc->stage_flags = stage;
634         desc->binding_point = binding_point;
635
636         VkDescriptorSetLayoutBinding dslb;
637         memset(&dslb, 0, sizeof dslb);
638
639         dslb.binding = binding_point;
640         dslb.descriptorType = type;
641         dslb.descriptorCount = num_desc;
642         dslb.stageFlags = stage;
643
644         VkDescriptorSetLayoutCreateInfo dslinf;
645         memset(&dslinf, 0, sizeof dslinf);
646
647         dslinf.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
648         dslinf.bindingCount = 1;
649         dslinf.pBindings = &dslb;
650
651         if(vkCreateDescriptorSetLayout(vk_device, &dslinf, 0, &desc->layout) != VK_SUCCESS) {
652                 fprintf(stderr, "Failed to create vku_descriptor.\n");
653                 return 0;
654         }
655
656         return desc;
657 }
658
659 void vku_destroy_descriptor(vku_descriptor *desc)
660 {
661         vkDestroyDescriptorSetLayout(vk_device, desc->layout, 0);
662         delete desc;
663 }
664
665 bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc)
666 {
667         if(vk_dpool)
668                 vkDestroyDescriptorPool(vk_device, vk_dpool, 0);
669
670         VkDescriptorPoolCreateInfo dpinf;
671         memset(&dpinf, 0, sizeof dpinf);
672         dpinf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
673         dpinf.maxSets = 1;
674
675         dpinf.poolSizeCount = num_desc;
676
677         std::vector<VkDescriptorPoolSize> sizes;
678         for(int i=0; i<num_desc; i++) {
679                 VkDescriptorPoolSize psz;
680                 psz.type = descriptors[i]->type;
681                 psz.descriptorCount = 1;
682                 sizes.push_back(psz);
683         }
684
685         dpinf.pPoolSizes = sizes.data();
686
687         if(vkCreateDescriptorPool(vk_device, &dpinf, 0, &vk_dpool) != VK_SUCCESS) {
688                 fprintf(stderr, "Failed to create descriptor pool.\n");
689                 return false;
690         }
691
692         return true;
693 }
694
695 void vku_destroy_descriptor_pool()
696 {
697         vkDestroyDescriptorPool(vk_device, vk_dpool, 0);
698 }
699
700 bool vku_allocate_descriptor_sets(vku_descriptor **desc, int num_desc, VkDescriptorSet set)
701 {
702         VkDescriptorSetAllocateInfo dainf;
703         memset(&dainf, 0, sizeof dainf);
704
705         dainf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
706         dainf.descriptorPool = vk_dpool;
707         dainf.descriptorSetCount = num_desc;
708
709         std::vector<VkDescriptorSetLayout> layouts;
710         for(int i=0; i<num_desc; i++) {
711                 layouts.push_back(desc[i]->layout);
712         }
713
714         dainf.pSetLayouts = layouts.data();
715
716         if(vkAllocateDescriptorSets(vk_device, &dainf, &set) != VK_SUCCESS) {
717                 fprintf(stderr, "Failed to allocate descriptor sets.\n");
718                 return false;
719         }
720         return true;
721 }
722
723 void vku_free_descriptor_sets(VkDescriptorSet *sets, int num)
724 {
725         vkFreeDescriptorSets(vk_device, vk_dpool, num, sets);
726 }
727
728