backup
[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                 //TODO change when the allocator changes
432                 vku_free(buf->mem_pool);
433
434                 vkDestroyBuffer(vk_device, buf->buf, 0);
435                 delete buf;
436         }
437 }
438
439 bool vku_update_buffer(vku_buffer *buf, int size, void *data)
440 {
441         uint8_t *pdata;
442         if(vkMapMemory(vk_device, buf->mem_pool, 0, size, 0, (void**)&pdata) != VK_SUCCESS) {
443                 fprintf(stderr, "Failed to map memory.\n");
444                 return false;
445         }
446         memcpy(pdata, data, size);
447         vkUnmapMemory(vk_device, buf->mem_pool);
448
449         return true;
450 }
451
452 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
453                      VkBuffer src, int soffs, int size)
454 {
455         VkBufferCopy copy;
456         copy.size = size;
457         copy.srcOffset = soffs;
458         copy.dstOffset = doffs;
459
460         vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
461 }
462
463 const char *vku_get_vulkan_error_str(VkResult error)
464 {
465         std::string errmsg;
466         switch(error) {
467         case VK_SUCCESS:
468                 errmsg = std::string("VK_SUCCESS");
469                 break;
470         case VK_NOT_READY:
471                 errmsg = std::string("VK_NOT_READY");
472                 break;
473         case VK_TIMEOUT:
474                 errmsg = std::string("VK_TIMEOUT");
475                 break;
476         case VK_EVENT_SET:
477                 errmsg = std::string("VK_EVENT_SET");
478                 break;
479         case VK_EVENT_RESET:
480                 errmsg = std::string("VK_EVENT_RESET");
481                 break;
482         case VK_INCOMPLETE:
483                 errmsg = std::string("VK_EVENT");
484                 break;
485         case VK_ERROR_OUT_OF_HOST_MEMORY:
486                 errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
487                 break;
488         case VK_ERROR_OUT_OF_DEVICE_MEMORY:
489                 errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
490                 break;
491         case VK_ERROR_INITIALIZATION_FAILED:
492                 errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
493                 break;
494         case VK_ERROR_DEVICE_LOST:
495                 errmsg = std::string("VK_ERROR_DEVICE_LOST");
496                 break;
497         case VK_ERROR_MEMORY_MAP_FAILED:
498                 errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
499                 break;
500         case VK_ERROR_LAYER_NOT_PRESENT:
501                 errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
502                 break;
503         case VK_ERROR_EXTENSION_NOT_PRESENT:
504                 errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
505                 break;
506         case VK_ERROR_FEATURE_NOT_PRESENT:
507                 errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
508         case VK_ERROR_INCOMPATIBLE_DRIVER:
509                 errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
510                 break;
511         case VK_ERROR_TOO_MANY_OBJECTS:
512                 errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
513                 break;
514         case VK_ERROR_FORMAT_NOT_SUPPORTED:
515                 errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
516                 break;
517         case VK_ERROR_FRAGMENTED_POOL:
518                 errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
519                 break;
520         default:
521                 errmsg = std::string("UNKNOWN");
522                 break;
523         }
524
525         return errmsg.c_str();
526 }
527
528
529 static const char *get_device_name_str(VkPhysicalDeviceType type)
530 {
531         switch(type) {
532         case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
533                 return "integrated GPU";
534         case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
535                 return "discrete GPU";
536         case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
537                 return "virtual GPU";
538         case VK_PHYSICAL_DEVICE_TYPE_CPU:
539                 return "CPU";
540         default:
541                 break;
542         }
543         return "unknown";
544 }
545
546 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
547 {
548         static char str[128];
549
550         str[0] = 0;
551         if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
552                 strcat(str, "device-local ");
553         }
554         if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
555                 strcat(str, "host-visible ");
556         }
557         if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
558                 strcat(str, "host-coherent ");
559         }
560         if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
561                 strcat(str, "host-cached ");
562         }
563         if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
564                 strcat(str, "lazily-allocated ");
565         }
566
567         if(!*str) {
568                 strcat(str, "-");
569         }
570         return str;
571 }
572
573 static const char *get_queue_flags_str(VkQueueFlags flags)
574 {
575         static char str[128];
576
577         str[0] = 0;
578         if(flags & VK_QUEUE_GRAPHICS_BIT) {
579                 strcat(str, "graphics ");
580         }
581         if(flags & VK_QUEUE_COMPUTE_BIT) {
582                 strcat(str, "compute ");
583         }
584         if(flags & VK_QUEUE_TRANSFER_BIT) {
585                 strcat(str, "transfer ");
586         }
587         if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
588                 strcat(str, "sparse-binding ");
589         }
590         if(!*str) {
591                 strcat(str, "-");
592         }
593         return str;
594 }
595
596 static int ver_major(uint32_t ver)
597 {
598         return (ver >> 22) & 0x3ff;
599 }
600
601 static int ver_minor(uint32_t ver)
602 {
603         return (ver >> 12) & 0x3ff;
604 }
605
606 static int ver_patch(uint32_t ver)
607 {
608         return ver & 0xfff;
609 }
610
611 static const char *get_mem_size_str(long sz)
612 {
613         static char str[64];
614         static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
615         int uidx = 0;
616         sz *= 10;
617
618         while(sz >= 10240 && unitstr[uidx + 1]) {
619                 sz /= 1024;
620                 ++uidx;
621         }
622         sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
623         return str;
624 }
625
626 vku_descriptor *vku_create_descriptor(VkDescriptorType type,
627                 VkFlags stage, int binding_point,
628                 int size, int num_desc)
629 {
630         vku_descriptor *desc = new vku_descriptor;
631
632         desc->type = type;
633         desc->size = size;
634         desc->stage_flags = stage;
635         desc->binding_point = binding_point;
636
637         VkDescriptorSetLayoutBinding dslb;
638         memset(&dslb, 0, sizeof dslb);
639
640         dslb.binding = binding_point;
641         dslb.descriptorType = type;
642         dslb.descriptorCount = num_desc;
643         dslb.stageFlags = stage;
644
645         VkDescriptorSetLayoutCreateInfo dslinf;
646         memset(&dslinf, 0, sizeof dslinf);
647
648         dslinf.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
649         dslinf.bindingCount = 1;
650         dslinf.pBindings = &dslb;
651
652         if(vkCreateDescriptorSetLayout(vk_device, &dslinf, 0, &desc->layout) != VK_SUCCESS) {
653                 fprintf(stderr, "Failed to create vku_descriptor.\n");
654                 return 0;
655         }
656
657         return desc;
658 }
659
660 void vku_destroy_descriptor(vku_descriptor *desc)
661 {
662         vkDestroyDescriptorSetLayout(vk_device, desc->layout, 0);
663         delete desc;
664 }
665
666 bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc)
667 {
668         if(vk_dpool)
669                 vkDestroyDescriptorPool(vk_device, vk_dpool, 0);
670
671         VkDescriptorPoolCreateInfo dpinf;
672         memset(&dpinf, 0, sizeof dpinf);
673         dpinf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
674         dpinf.maxSets = 1;
675
676         dpinf.poolSizeCount = num_desc;
677
678         std::vector<VkDescriptorPoolSize> sizes;
679         for(int i=0; i<num_desc; i++) {
680                 VkDescriptorPoolSize psz;
681                 psz.type = descriptors[i]->type;
682                 psz.descriptorCount = 1;
683                 sizes.push_back(psz);
684         }
685
686         dpinf.pPoolSizes = sizes.data();
687
688         if(vkCreateDescriptorPool(vk_device, &dpinf, 0, &vk_dpool) != VK_SUCCESS) {
689                 fprintf(stderr, "Failed to create descriptor pool.\n");
690                 return false;
691         }
692
693         return true;
694 }
695
696 void vku_destroy_descriptor_pool()
697 {
698         vkDestroyDescriptorPool(vk_device, vk_dpool, 0);
699 }
700
701 bool vku_allocate_descriptor_sets(vku_descriptor **desc, int num_desc, VkDescriptorSet set)
702 {
703         VkDescriptorSetAllocateInfo dainf;
704         memset(&dainf, 0, sizeof dainf);
705
706         dainf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
707         dainf.descriptorPool = vk_dpool;
708         dainf.descriptorSetCount = num_desc;
709
710         std::vector<VkDescriptorSetLayout> layouts;
711         for(int i=0; i<num_desc; i++) {
712                 layouts.push_back(desc[i]->layout);
713         }
714
715         dainf.pSetLayouts = layouts.data();
716
717         if(vkAllocateDescriptorSets(vk_device, &dainf, &set) != VK_SUCCESS) {
718                 fprintf(stderr, "Failed to allocate descriptor sets.\n");
719                 return false;
720         }
721         return true;
722 }
723
724 void vku_free_descriptor_sets(VkDescriptorSet *sets, int num)
725 {
726         vkFreeDescriptorSets(vk_device, vk_dpool, num, sets);
727 }
728
729 bool vku_update_descriptor_sets(VkDescriptorSet *sets, int num_sets)
730 {
731 //      std::vector
732         return true;
733 }
734
735 VkPipelineCache vku_create_pipeline_cache()
736 {
737         VkPipelineCacheCreateInfo cinf;
738         memset(&cinf, 0, sizeof cinf);
739         cinf.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
740
741         VkPipelineCache cache;
742         if(vkCreatePipelineCache(vk_device, &cinf, 0, &cache) != VK_SUCCESS) {
743                 fprintf(stderr, "Failed to create pipeline cache.\n");
744                 return 0;
745         }
746         return cache;
747 }
748
749 void vku_destroy_pipeline_cache(VkPipelineCache cache)
750 {
751         vkDestroyPipelineCache(vk_device, cache, 0);
752 }
753
754 void vku_pl_init_shader_stage_state_info(VkPipelineShaderStageCreateInfo *ssinf,
755                 VkShaderStageFlagBits stage, VkShaderModule sm)
756 {
757         memset(ssinf, 0, sizeof *ssinf);
758         ssinf->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
759         ssinf->stage = stage;
760         ssinf->module = sm;
761         ssinf->pName = "main";
762 }
763
764 void vku_pl_init_input_asm_state_info(VkPipelineInputAssemblyStateCreateInfo *iasinf)
765 {
766         memset(iasinf, 0, sizeof *iasinf);
767         iasinf->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
768         iasinf->topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
769 }
770
771 void vku_pl_init_viewport_state_info(VkPipelineViewportStateCreateInfo *vsinf,
772                 VkViewport *viewports, int num_viewports, VkRect2D *scissors,
773                 int num_scissors)
774 {
775         memset(vsinf, 0, sizeof *vsinf);
776         vsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
777         vsinf->viewportCount = num_viewports;
778         vsinf->scissorCount = num_scissors;
779         vsinf->pViewports = viewports;
780         vsinf->pScissors = scissors;
781 }
782
783 void vku_pl_init_rasterization_state_info(VkPipelineRasterizationStateCreateInfo *rsinf)
784 {
785         memset(rsinf, 0, sizeof *rsinf);
786         rsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
787         rsinf->polygonMode = VK_POLYGON_MODE_FILL;
788         rsinf->cullMode = VK_CULL_MODE_FRONT_BIT;
789         rsinf->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
790 }
791
792 void vku_pl_init_multisample_state_info(VkPipelineMultisampleStateCreateInfo *msinf)
793 {
794         memset(msinf, 0, sizeof *msinf);
795         msinf->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
796         msinf->rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
797 }
798
799 void vku_pl_init_depth_stencil_state_info(VkPipelineDepthStencilStateCreateInfo *dsinf,
800                 bool enable)
801 {
802         memset(dsinf, 0, sizeof *dsinf);
803         dsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
804         dsinf->depthTestEnable = enable ? VK_TRUE : VK_FALSE;
805 }
806
807 void vku_pl_init_color_blend_state_info(VkPipelineColorBlendStateCreateInfo *cbsinf,
808                 bool enable)
809 {
810         memset(cbsinf, 0, sizeof *cbsinf);
811         cbsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
812         //TODO
813 }