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