1 #include <gmath/gmath.h>
2 #include <vulkan/vulkan.h>
12 /* global variables */
13 VkPhysicalDevice vk_physical;
15 VkCommandPool vk_pool;
17 VkSurfaceKHR vk_surface;
18 VkSwapchainKHR vk_swapchain;
20 VkPipeline *vkgparent_pipeline;
21 VkFramebuffer *vkfbufs;
24 VkCommandBuffer vkcmdbuf; /* primary command buffer */
27 VkSemaphore vk_img_avail_sema;
28 VkSemaphore vk_rend_done_sema;
29 VkImage *vkswapchain_images;
30 VkImageView *vkswapchain_views;
31 int vknum_swapchain_images;
32 int vk_curr_swapchain_image;
34 /* static functions */
35 static const char *get_device_name_str(VkPhysicalDeviceType type);
36 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
37 static const char *get_queue_flags_str(VkQueueFlags flags);
38 static const char *get_mem_size_str(long sz);
39 static int ver_major(uint32_t ver);
40 static int ver_minor(uint32_t ver);
41 static int ver_patch(uint32_t ver);
43 /* static variables */
44 static VkPhysicalDevice *phys_devices;
47 static int sel_qfamily;
49 static VkExtensionProperties *vkext, *vkdevext;
50 static uint32_t vkext_count, vkdevext_count;
52 bool vku_have_extension(const char *name)
56 vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
58 vkext = new VkExtensionProperties[vkext_count];
59 vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
61 printf("instance extensions:\n");
62 for(int i=0; i<(int)vkext_count; i++) {
63 printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
68 for(int i=0; i<(int)vkext_count; i++) {
69 if(strcmp(vkext[i].extensionName, name) == 0) {
76 bool vku_have_device_extension(const char *name)
78 if(sel_dev < 0) return false;
82 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
84 vkdevext = new VkExtensionProperties[vkdevext_count];
85 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
87 printf("selected device extensions:\n");
88 for(int i=0; i<(int)vkdevext_count; i++) {
89 printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
94 for(int i=0; i<(int)vkdevext_count; i++) {
95 if(strcmp(vkdevext[i].extensionName, name) == 0) {
102 bool vku_create_device()
104 VkInstanceCreateInfo inst_info;
105 VkDeviceCreateInfo dev_info;
106 VkDeviceQueueCreateInfo queue_info;
107 VkCommandPoolCreateInfo cmdpool_info;
108 uint32_t num_devices;
111 static const char *ext_names[] = {
112 "VK_KHR_xcb_surface",
116 static const char *devext_names[] = {
123 for(unsigned int i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
124 if(!vku_have_extension(ext_names[i])) {
125 fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
129 memset(&inst_info, 0, sizeof inst_info);
130 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
131 inst_info.ppEnabledExtensionNames = ext_names;
132 inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
134 if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) {
135 fprintf(stderr, "failed to create vulkan instance\n");
138 printf("created vulkan instance\n");
139 if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) {
140 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
143 phys_devices = new VkPhysicalDevice[num_devices];
144 if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) {
145 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
148 printf("found %u physical device(s)\n", (unsigned int)num_devices);
150 for(int i=0; i<(int)num_devices; i++) {
151 VkPhysicalDeviceProperties dev_prop;
152 VkPhysicalDeviceMemoryProperties mem_prop;
153 VkQueueFamilyProperties *qprop;
154 uint32_t qprop_count;
156 vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
158 printf("Device %d: %s\n", i, dev_prop.deviceName);
159 printf(" type: %s\n", get_device_name_str(dev_prop.deviceType));
160 printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
161 ver_patch(dev_prop.apiVersion));
162 printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
163 ver_patch(dev_prop.driverVersion));
164 printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
167 vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
168 printf(" %d memory heaps:\n", mem_prop.memoryHeapCount);
169 for(unsigned int j=0; j<mem_prop.memoryHeapCount; j++) {
170 VkMemoryHeap heap = mem_prop.memoryHeaps[j];
171 printf(" Heap %d - size: %s, flags: %s\n", j, get_mem_size_str(heap.size),
172 heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
174 printf(" %d memory types:\n", mem_prop.memoryTypeCount);
175 for(unsigned int j=0; j<mem_prop.memoryTypeCount; j++) {
176 VkMemoryType type = mem_prop.memoryTypes[j];
177 printf(" Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
178 get_memtype_flags_str(type.propertyFlags));
181 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
182 if(qprop_count <= 0) {
185 qprop = new VkQueueFamilyProperties[qprop_count];
186 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
188 for(unsigned int j=0; j<qprop_count; j++) {
189 printf(" Queue family %d:\n", j);
190 printf(" flags: %s\n", get_queue_flags_str(qprop[j].queueFlags));
191 printf(" num queues: %u\n", qprop[j].queueCount);
193 if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
201 if(sel_dev < 0 || sel_qfamily < 0) {
202 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
203 vkDestroyDevice(vk_device, 0);
207 for(unsigned int i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
208 if(!vku_have_device_extension(devext_names[i])) {
209 fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
210 ext_names[i], sel_dev);
215 /* create device & command queue */
216 memset(&queue_info, 0, sizeof queue_info);
217 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
218 queue_info.queueFamilyIndex = sel_qfamily;
219 queue_info.queueCount = 1;
220 queue_info.pQueuePriorities = &qprio;
222 memset(&dev_info, 0, sizeof dev_info);
223 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
224 dev_info.queueCreateInfoCount = 1;
225 dev_info.pQueueCreateInfos = &queue_info;
226 dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
227 dev_info.ppEnabledExtensionNames = devext_names;
229 if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vk_device) != 0) {
230 fprintf(stderr, "failed to create device %d\n", sel_dev);
233 printf("created device %d\n", sel_dev);
235 vk_physical = phys_devices[sel_dev];
236 vkqfamily = sel_qfamily;
238 vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue);
240 /* create command buffer pool */
241 memset(&cmdpool_info, 0, sizeof cmdpool_info);
242 cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
243 cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
244 cmdpool_info.queueFamilyIndex = sel_qfamily;
246 if(vkCreateCommandPool(vk_device, &cmdpool_info, 0, &vk_pool) != 0) {
247 fprintf(stderr, "failed to get command queue!\n");
251 /* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
252 fprintf(stderr, "failed to create primary command buffer\n");
262 vkDeviceWaitIdle(vk_device);
263 vkDestroyCommandPool(vk_device, vk_pool, 0);
265 vkDestroySemaphore(vk_device, vk_img_avail_sema, 0);
266 vkDestroySemaphore(vk_device, vk_rend_done_sema, 0);
268 vkDestroyDevice(vk_device, 0);
269 vkDestroyInstance(vkinst, 0);
273 delete [] phys_devices;
277 bool vku_create_semaphores()
279 VkSemaphoreCreateInfo sinf;
280 memset(&sinf, 0, sizeof sinf);
282 sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
283 if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_img_avail_sema) != VK_SUCCESS) {
284 fprintf(stderr, "Failed to create semaphore\n");
288 if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_rend_done_sema) != VK_SUCCESS) {
289 fprintf(stderr, "Failed to create semaphore\n");
296 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
298 VkCommandBuffer cmdbuf;
299 VkCommandBufferAllocateInfo inf;
301 memset(&inf, 0, sizeof inf);
302 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
303 inf.commandPool = pool;
305 inf.commandBufferCount = 1;
307 if(vkAllocateCommandBuffers(vk_device, &inf, &cmdbuf) != 0) {
313 bool vku_alloc_cmdbufs(VkCommandPool pool, VkCommandBufferLevel level, unsigned int count, VkCommandBuffer *buf_array)
315 VkCommandBufferAllocateInfo cinf;
316 memset(&cinf, 0, sizeof cinf);
318 cinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
319 cinf.commandPool = vk_pool;
321 cinf.commandBufferCount = count;
323 if(vkAllocateCommandBuffers(vk_device, &cinf, buf_array) != VK_SUCCESS) {
324 fprintf(stderr, "Failed to allocate command buffer\n");
331 void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
333 vkFreeCommandBuffers(vk_device, pool, 1, &buf);
336 void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
338 VkCommandBufferBeginInfo inf;
340 memset(&inf, 0, sizeof inf);
341 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
344 vkBeginCommandBuffer(buf, &inf);
348 void vku_end_cmdbuf(VkCommandBuffer buf)
350 vkEndCommandBuffer(buf);
353 void vku_reset_cmdbuf(VkCommandBuffer buf)
355 vkResetCommandBuffer(buf, 0);
358 void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
362 memset(&info, 0, sizeof info);
363 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
364 info.commandBufferCount = 1;
365 info.pCommandBuffers = &buf;
367 vkQueueSubmit(q, 1, &info, done_fence);
370 bool vku_get_surface_format(VkPhysicalDevice gpu, VkSurfaceKHR surface, VkSurfaceFormatKHR *sformat)
374 if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical,
375 vk_surface, &fcount, 0) != VK_SUCCESS) {
376 fprintf(stderr, "Failed to get format count for physical device.\n");
380 fprintf(stderr, "No color formats were found.\n");
384 VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount];
385 if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface,
386 &fcount, formats) != VK_SUCCESS) {
388 fprintf(stderr, "Failed to get surface formats.\n");
392 *sformat = formats[0];
394 if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) {
395 sformat->format = VK_FORMAT_B8G8R8_UNORM;
403 int vku_get_next_image(VkSwapchainKHR sc)
407 if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) {
413 void vku_present(VkSwapchainKHR sc, int img_idx)
415 VkPresentInfoKHR inf;
417 uint32_t index = img_idx;
419 memset(&inf, 0, sizeof inf);
420 inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
421 inf.swapchainCount = 1;
422 inf.pSwapchains = ≻
423 inf.pImageIndices = &index;
426 vkQueuePresentKHR(vk_queue, &inf);
429 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
431 struct vku_buffer *buf;
432 VkBufferCreateInfo binfo;
434 buf = new vku_buffer;
436 memset(&binfo, 0, sizeof binfo);
437 binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
440 binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
442 if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) {
443 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
446 // TODO back with memory
450 void vku_destroy_buffer(struct vku_buffer *buf)
453 vkDestroyBuffer(vk_device, buf->buf, 0);
458 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
459 VkBuffer src, int soffs, int size)
463 copy.srcOffset = soffs;
464 copy.dstOffset = doffs;
466 vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©);
469 const char *vku_get_vulkan_error_str(VkResult error)
474 errmsg = std::string("VK_SUCCESS");
477 errmsg = std::string("VK_NOT_READY");
480 errmsg = std::string("VK_TIMEOUT");
483 errmsg = std::string("VK_EVENT_SET");
486 errmsg = std::string("VK_EVENT_RESET");
489 errmsg = std::string("VK_EVENT");
491 case VK_ERROR_OUT_OF_HOST_MEMORY:
492 errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
494 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
495 errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
497 case VK_ERROR_INITIALIZATION_FAILED:
498 errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
500 case VK_ERROR_DEVICE_LOST:
501 errmsg = std::string("VK_ERROR_DEVICE_LOST");
503 case VK_ERROR_MEMORY_MAP_FAILED:
504 errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
506 case VK_ERROR_LAYER_NOT_PRESENT:
507 errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
509 case VK_ERROR_EXTENSION_NOT_PRESENT:
510 errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
512 case VK_ERROR_FEATURE_NOT_PRESENT:
513 errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
514 case VK_ERROR_INCOMPATIBLE_DRIVER:
515 errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
517 case VK_ERROR_TOO_MANY_OBJECTS:
518 errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
520 case VK_ERROR_FORMAT_NOT_SUPPORTED:
521 errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
523 case VK_ERROR_FRAGMENTED_POOL:
524 errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
527 errmsg = std::string("UNKNOWN");
531 return errmsg.c_str();
535 static const char *get_device_name_str(VkPhysicalDeviceType type)
538 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
539 return "integrated GPU";
540 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
541 return "discrete GPU";
542 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
543 return "virtual GPU";
544 case VK_PHYSICAL_DEVICE_TYPE_CPU:
552 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
554 static char str[128];
557 if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
558 strcat(str, "device-local ");
560 if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
561 strcat(str, "host-visible ");
563 if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
564 strcat(str, "host-coherent ");
566 if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
567 strcat(str, "host-cached ");
569 if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
570 strcat(str, "lazily-allocated ");
579 static const char *get_queue_flags_str(VkQueueFlags flags)
581 static char str[128];
584 if(flags & VK_QUEUE_GRAPHICS_BIT) {
585 strcat(str, "graphics ");
587 if(flags & VK_QUEUE_COMPUTE_BIT) {
588 strcat(str, "compute ");
590 if(flags & VK_QUEUE_TRANSFER_BIT) {
591 strcat(str, "transfer ");
593 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
594 strcat(str, "sparse-binding ");
602 static int ver_major(uint32_t ver)
604 return (ver >> 22) & 0x3ff;
607 static int ver_minor(uint32_t ver)
609 return (ver >> 12) & 0x3ff;
612 static int ver_patch(uint32_t ver)
617 static const char *get_mem_size_str(long sz)
620 static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
624 while(sz >= 10240 && unitstr[uidx + 1]) {
628 sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);