added a vulkan util (vkutil.h/.cc)
authorEleni Maria Stea <estea@igalia.com>
Wed, 4 Oct 2017 05:51:46 +0000 (08:51 +0300)
committerEleni Maria Stea <estea@igalia.com>
Wed, 4 Oct 2017 05:51:46 +0000 (08:51 +0300)
surface, window, swapchain, command buffer, command pool

src/gfxapi.cc
src/gfxapi.h
src/main.cc
src/opengl/opengl.cc
src/vulkan/vk.cc
src/vulkan/vk.h
src/vulkan/vkutil.cc [new file with mode: 0644]
src/vulkan/vkutil.h [new file with mode: 0644]

index 2987a59..5296677 100644 (file)
@@ -16,7 +16,7 @@ void (*gfx_clear)(float r, float g, float b);
 void (*gfx_viewport)(int x, int y, int width, int height);
 void (*gfx_zbuffer)(bool enable);
 void (*gfx_cull_face)(Gfx_cull_face cf);
-
+void (*gfx_reshape)(int width, int height);
 
 bool gfx_init(Gfx_API api)
 {
index 1e6cd2b..62a8a3a 100644 (file)
@@ -21,6 +21,7 @@ extern void (*gfx_clear)(float r, float g, float b);
 extern void (*gfx_viewport)(int x, int y, int width, int height);
 extern void (*gfx_zbuffer)(bool enable);
 extern void (*gfx_cull_face)(Gfx_cull_face cf);
+extern void (*gfx_reshape)(int width, int height);
 
 bool gfx_init(Gfx_API api);
 void gfx_cleanup();
index 6b1050b..4e2c72f 100644 (file)
@@ -102,7 +102,7 @@ int main(int argc, char **argv)
        }
 
        //TODO
-       return 0;
+       //return 0;
 
        glfwSetKeyCallback(win, clbk_key);
        glfwSetCursorPosCallback(win, clbk_motion);
@@ -318,6 +318,7 @@ static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
 
 static void clbk_reshape(GLFWwindow *win, int width, int height)
 {
+       gfx_reshape(width, height);
        gfx_viewport(0, 0, width, height);
        aspect = (float)width / (float)height;
        mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
@@ -388,4 +389,4 @@ static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
                }
        }
        return false;
-}
\ No newline at end of file
+}
index 9b220ab..f6a7cef 100644 (file)
@@ -13,6 +13,7 @@ static void clear(float r, float g, float b);
 static void viewport(int x, int y, int width, int height);
 static void zbuffer(bool enable);
 static void cull_face(Gfx_cull_face cf);
+static void reshape(int width, int height) {};
 
 bool init_opengl()
 {
@@ -41,6 +42,7 @@ bool init_opengl()
        gfx_viewport = viewport;
        gfx_zbuffer = zbuffer;
        gfx_cull_face = cull_face;
+       gfx_reshape = reshape;
 
        // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        return true;
@@ -88,4 +90,4 @@ static void cull_face(Gfx_cull_face cf)
                glCullFace(GL_BACK);
                break;
        }
-}
\ No newline at end of file
+}
index 105caba..871824f 100644 (file)
@@ -4,36 +4,20 @@
 #include <alloca.h>
 #include <stdio.h>
 #include <string.h>
-
 #include <string>
 #include <vector>
 
 #include "gfxapi.h"
+#include "vkutil.h"
 
 /* global variables */
 extern GLFWwindow *win;
 extern int win_w;
 extern int win_h;
 
-/* static variables */
-static std::vector<std::string> enabled_extension_names;
-static VkInstance inst;
-static VkDevice device;
-static VkPhysicalDevice pdev;
-static VkSurfaceKHR surface;
-static uint32_t device_mem_idx;
-static uint32_t num_queues;
-static uint32_t qfamily_idx;
-
-static const char *print_vulkan_error(VkResult error);
-static const char *dev_type_str(VkPhysicalDeviceType type);
-static const char *heap_flags_str(VkMemoryHeapFlags flags);
-static const char *memtype_flags_str(VkMemoryPropertyFlags flags);
-static const char *queue_flags_str(VkQueueFlags flags);
-
-/* static fumctions */
-static bool create_instance();
-static bool create_device();
+/* static functions */
+static void error_callback(int error, const char *descr);
+static void reshape(int width, int height);
 
 bool init_vulkan()
 {
@@ -47,12 +31,14 @@ bool init_vulkan()
                return false;
        }
 
-       if(!create_instance()) {
-               fprintf(stderr, "Failed to enable validation.\n");
+       glfwSetErrorCallback(error_callback);
+
+       if(!vku_create_device()) {
+               fprintf(stderr, "Failed to initialize vulkan.\n");
                return false;
        }
 
-       if(!glfwGetPhysicalDevicePresentationSupport(inst, pdev, qfamily_idx)) {
+       if(!glfwGetPhysicalDevicePresentationSupport(vkinst, vkpdev, vkqfamily)) {
                fprintf(stderr, "Presentation support not found.\n");
                return false;
        }
@@ -63,11 +49,13 @@ bool init_vulkan()
                return false;
        }
 
-       if(VkResult err = glfwCreateWindowSurface(inst, win, 0, &surface)) {
-               fprintf(stderr, "Failed to create KHR surface: %s\n", print_vulkan_error(err));
+       if(VkResult err = glfwCreateWindowSurface(vkinst, win, 0, &vksurface)) {
+               fprintf(stderr, "Failed to create KHR surface: %s\n", vku_get_vulkan_error_str(err));
                return false;
        }
 
+       gfx_reshape = reshape;
+
        return true;
 }
 
@@ -75,329 +63,29 @@ void cleanup_vulkan()
 {
        //TODOs according to the book:
        // 1- make sure all threads have been terminated (when I add threads)
-       // 2- destroy objects in *reverse* order
-
-       vkDestroySurfaceKHR(inst, surface, 0);
-
-       if(vkDeviceWaitIdle(device) == VK_SUCCESS) {
-               vkDestroyDevice(device, 0);
-               vkDestroyInstance(inst, 0);
-       }
-}
-
-static bool create_instance()
-{
-       /* enable layers */
-       uint32_t layer_count = 0;
-       std::vector<const char *> enabled_layers;
-
-       if(vkEnumerateInstanceLayerProperties(&layer_count, 0) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to query layer properties.\n");
-               return false;
-       }
-
-       if(layer_count > 0) {
-               VkLayerProperties *layers = (VkLayerProperties *)alloca(layer_count * sizeof *layers);
-               vkEnumerateInstanceLayerProperties(&layer_count, layers);
-               for(uint32_t i=0; i<layer_count; i++) {
-                       if(strcmp(layers[i].layerName, "VK_LAYER_LUNARG_standard_validation")) {
-                               enabled_layers.push_back(layers[i].layerName);
-                       }
-               }
-       }
-
-       /* enable extensions */
-       uint32_t extensions_count = 0;
-       std::vector<const char *> enabled_extensions;
-
-       if(vkEnumerateInstanceExtensionProperties(0, &extensions_count, 0) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to enumerate instance extension properties\n");
-               return false;
-       }
-
-       if(extensions_count > 0) {
-               VkExtensionProperties *extensions = (VkExtensionProperties *)alloca(extensions_count * sizeof *extensions);
-               vkEnumerateInstanceExtensionProperties(0, &extensions_count, extensions);
-
-               for(uint32_t i=0; i<extensions_count; i++) {
-                       printf("Extension %u: %s %u.\n", i, extensions[i].extensionName, extensions[i].specVersion);
-                       //enable all the available extensions
-                       enabled_extensions.push_back(extensions[i].extensionName);
-                       enabled_extension_names.push_back(std::string(extensions[i].extensionName));
-               }
-       }
-
-       /* create instance */
-       VkInstanceCreateInfo create_info;
-       memset(&create_info, 0, sizeof create_info);
-       create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
-
-       if(!enabled_layers.empty()) {
-               create_info.enabledLayerCount = enabled_layers.size();
-               create_info.ppEnabledLayerNames = enabled_layers.data();
-       }
-
-       if(!enabled_extensions.empty()) {
-               create_info.enabledExtensionCount = enabled_extensions.size();
-               create_info.ppEnabledExtensionNames = enabled_extensions.data();
-       }
-
-       if(vkCreateInstance(&create_info, 0, &inst) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to create instance.\n");
-               return false;
-       }
-
-       if(!create_device())
-               return false;
-
-       return true;
-}
-
-static bool create_device()
-{
-       int qfam_idx = -1;
-       int pdev_idx = -1;
 
-       uint32_t dev_count;
-       if(vkEnumeratePhysicalDevices(inst, &dev_count, 0) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to enumerate physical devices.\n");
-               return false;
-       }
-       printf("%u devices found.\n", (unsigned int)dev_count);
-
-       VkPhysicalDevice *phys_dev = (VkPhysicalDevice *)alloca(dev_count * sizeof *phys_dev);
-       vkEnumeratePhysicalDevices(inst, &dev_count, phys_dev);
-       VkPhysicalDeviceMemoryProperties memprop;
-
-       for(uint32_t i=0; i<dev_count; i++) {
-               VkPhysicalDeviceProperties dev_props;
-               vkGetPhysicalDeviceProperties(phys_dev[i], &dev_props);
-
-               //memory heaps:
-               vkGetPhysicalDeviceMemoryProperties(phys_dev[i], &memprop);
-               printf("\tNumber of heaps: %u\n", memprop.memoryHeapCount);
-               for(uint32_t j=0; j<memprop.memoryHeapCount; j++) {
-                       printf("\t\tHeap %u size: %lu\n", j, (unsigned long)memprop.memoryHeaps[j].size);
-                       printf("\t\tHeap %u flags: %s\n", j, heap_flags_str(memprop.memoryHeaps[j].flags));
-               }
-               //memory types
-               printf("\tMemory types: %u\n", memprop.memoryTypeCount);
-               for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
-                       printf("\t\tType %u heap index: %u\n", j, memprop.memoryTypes[j].heapIndex);
-                       printf("\t\tType %u flags: %s\n", j, memtype_flags_str(memprop.memoryTypes[j].propertyFlags));
-               }
-
-               //supported features
-               VkPhysicalDeviceFeatures features;
-               vkGetPhysicalDeviceFeatures(phys_dev[i], &features);
-
-               //queue families
-               uint32_t qfam_count;
-               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, 0);
-               printf("\tQueue Families: %u\n", qfam_count);
-               VkQueueFamilyProperties *qfam_props = new VkQueueFamilyProperties[qfam_count];
-               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, qfam_props);
-               for(uint32_t j=0; j<qfam_count; j++) {
-                       printf("\t\tFamily %u flags: %s\n", j, queue_flags_str(qfam_props[j].queueFlags));
-                       printf("\t\tFamily %u number of queues: %u\n", j, qfam_props[j].queueCount);
-
-                       if((qfam_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) && (pdev_idx == -1)) {
-                               pdev_idx = i;
-                               qfam_idx = j;
-                               num_queues = qfam_props[j].queueCount;
-                       }
-               }
-               delete [] qfam_props;
-       }
-
-       if(pdev_idx == -1) {
-               fprintf(stderr, "No suitable devices found.\n");
-               return false;
-       }
-
-       pdev = *(phys_dev + pdev_idx);
-       qfamily_idx = qfam_idx;
-
-       /*      uint32_t layer_count;
-               if(vkEnumerateDeviceLayerProperties(pdev, &layer_count, 0) != VK_SUCCESS) {
-                       fprintf(stderr, "Failed to enumerate device layers.\n");
-                       return false;
-               }
-               if(layer_count > 0) {
-                       VkLayerProperties *layers = (VkLayerProperties*)alloca(layer_count * sizeof *layers);
-                       vkEnumerateDeviceLayerProperties(pdev, &layer_count, layers);
-                       printf("%u layers found.\n", layer_count);
-                       for(uint32_t i=0; i<layer_count; i++) {
-                               printf("Layer %u: %s (%u, %u)\n", i, layers[i].layerName,
-                                               layers[i].specVersion, layers[i].implementationVersion);
-                               printf("\tDesc: %s\n", layers[i].description);
-                       }
-               }
-       */
-       VkDeviceCreateInfo dev_info;
-       memset(&dev_info, 0, sizeof dev_info);
-       dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
-
-       VkDeviceQueueCreateInfo dev_qinfo;
-       memset(&dev_qinfo, 0, sizeof dev_qinfo);
-       dev_qinfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
-       dev_qinfo.queueFamilyIndex = qfam_idx;
-       dev_qinfo.queueCount = 1;
-
-       dev_info.queueCreateInfoCount = 1;
-       dev_info.pQueueCreateInfos = &dev_qinfo;
-
-       if(vkCreateDevice(pdev, &dev_info, 0, &device) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to create logical device.\n");
-               return false;
-       }
-
-       vkGetPhysicalDeviceMemoryProperties(pdev, &memprop);
-       for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
-               if(memprop.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
-                       device_mem_idx = j;
-                       printf("Selected device memory index: %u\n", device_mem_idx);
-                       break;
-               }
-       }
-
-       return true;
-}
-
-static const char *print_vulkan_error(VkResult error)
-{
-       std::string errmsg;
-       switch(error) {
-       case VK_SUCCESS:
-               errmsg = std::string("VK_SUCCESS");
-               break;
-       case VK_NOT_READY:
-               errmsg = std::string("VK_NOT_READY");
-               break;
-       case VK_TIMEOUT:
-               errmsg = std::string("VK_TIMEOUT");
-               break;
-       case VK_EVENT_SET:
-               errmsg = std::string("VK_EVENT_SET");
-               break;
-       case VK_EVENT_RESET:
-               errmsg = std::string("VK_EVENT_RESET");
-               break;
-       case VK_INCOMPLETE:
-               errmsg = std::string("VK_EVENT");
-               break;
-       case VK_ERROR_OUT_OF_HOST_MEMORY:
-               errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
-               break;
-       case VK_ERROR_OUT_OF_DEVICE_MEMORY:
-               errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
-               break;
-       case VK_ERROR_INITIALIZATION_FAILED:
-               errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
-               break;
-       case VK_ERROR_DEVICE_LOST:
-               errmsg = std::string("VK_ERROR_DEVICE_LOST");
-               break;
-       case VK_ERROR_MEMORY_MAP_FAILED:
-               errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
-               break;
-       case VK_ERROR_LAYER_NOT_PRESENT:
-               errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
-               break;
-       case VK_ERROR_EXTENSION_NOT_PRESENT:
-               errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
-               break;
-       case VK_ERROR_FEATURE_NOT_PRESENT:
-               errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
-               break;
-       case VK_ERROR_INCOMPATIBLE_DRIVER:
-               errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
-               break;
-       case VK_ERROR_TOO_MANY_OBJECTS:
-               errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
-               break;
-       case VK_ERROR_FORMAT_NOT_SUPPORTED:
-               errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
-               break;
-       case VK_ERROR_FRAGMENTED_POOL:
-               errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
-               break;
-       default:
-               errmsg = std::string("UNKNOWN");
-               break;
-       }
+       // 2- destroy objects in *reverse* order
+       vkDestroySurfaceKHR(vkinst, vksurface, 0);
 
-       return errmsg.c_str();
+       vku_cleanup();
 }
 
-static const char *dev_type_str(VkPhysicalDeviceType type)
+static void error_callback(int error, const char *description)
 {
-       switch(type) {
-       case VK_PHYSICAL_DEVICE_TYPE_OTHER:
-               return "other";
-       case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
-               return "integrated GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
-               return "discrete GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
-               return "virtual GPU";
-       case VK_PHYSICAL_DEVICE_TYPE_CPU:
-               return "CPU";
-       default:
-               break;
-       }
-       return "unknown";
+       fprintf(stderr, "GLFW error %d: %s.\n", error, description);
 }
 
-static const char *heap_flags_str(VkMemoryHeapFlags flags)
+static void reshape(int width, int height)
 {
-       static std::string str;
-       str.clear();
-       if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
-               str += "device-local ";
+       VkSwapchainKHR sc;
+       if(!(sc = vku_create_swapchain(vksurface, width, height, 2, VK_PRESENT_MODE_FIFO_KHR,
+                                      vkswapchain))) {
+               fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", width, height);
+               return;
        }
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
-}
+       vkswapchain = sc;
 
-static const char *memtype_flags_str(VkMemoryPropertyFlags flags)
-{
-       static std::string str;
-       str.clear();
-       if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
-               str += "device-local ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
-               str += "host-visible ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
-               str += "host-coherent ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
-               str += "host-cached ";
-       }
-       if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
-               str += "lazily-allocated ";
-       }
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
-}
-
-static const char *queue_flags_str(VkQueueFlags flags)
-{
-       static std::string str;
-       str.clear();
-       if(flags & VK_QUEUE_GRAPHICS_BIT)
-               str += "graphics ";
-       if(flags & VK_QUEUE_COMPUTE_BIT)
-               str += "compute ";
-       if(flags & VK_QUEUE_TRANSFER_BIT)
-               str += "transfer ";
-       if(flags & VK_QUEUE_SPARSE_BINDING_BIT)
-               str += "sparse-binding ";
-       if(!str.empty())
-               str.pop_back();
-       return str.c_str();
+       free(vkswapchain_images);
+       vkswapchain_images = vku_get_swapchain_images(sc, 0);
+       vknext_swapchain_image = vku_get_next_image(vkswapchain);
 }
\ No newline at end of file
index 5e62b41..424384e 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef VK_H_
 #define VK_H_
 
-#include <vulkan/vulkan.h>
-
 bool init_vulkan();
 void cleanup_vulkan();
 
diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc
new file mode 100644 (file)
index 0000000..051ee4d
--- /dev/null
@@ -0,0 +1,852 @@
+#include <vulkan/vulkan.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+#include <vector>
+
+#include "vkutil.h"
+
+/* global variables */
+
+VkSwapchainKHR vkswapchain;
+VkImage *vkswapchain_images;
+int vknext_swapchain_image;
+VkSurfaceKHR vksurface;
+VkInstance vkinst;
+VkPhysicalDevice vkpdev;
+VkDevice vkdev;
+VkQueue vkq;
+VkCommandPool vkcmdpool;
+VkCommandBuffer vkcmdbuf;      /* primary command buffer */
+int vkqfamily;
+
+/* static functions */
+static const char *get_device_name_str(VkPhysicalDeviceType type);
+static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
+static const char *get_queue_flags_str(VkQueueFlags flags);
+static const char *get_mem_size_str(long sz);
+static int ver_major(uint32_t ver);
+static int ver_minor(uint32_t ver);
+static int ver_patch(uint32_t ver);
+
+/* static variables */
+static VkPhysicalDevice *phys_devices;
+
+static int sel_dev;
+static int sel_qfamily;
+
+static VkExtensionProperties *vkext, *vkdevext;
+static uint32_t vkext_count, vkdevext_count;
+
+bool vku_have_extension(const char *name)
+{
+       if(!vkext) {
+               vkext_count = 0;
+               vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
+               if(vkext_count) {
+                       if(!(vkext = (VkExtensionProperties *)malloc(vkext_count * sizeof *vkext))) {
+                               perror("failed to allocate instance extension list");
+                               return false;
+                       }
+                       vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
+
+                       printf("instance extensions:\n");
+                       for(int i=0; i<(int)vkext_count; i++) {
+                               printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
+                       }
+               }
+       }
+
+       for(int i=0; i<(int)vkext_count; i++) {
+               if(strcmp(vkext[i].extensionName, name) == 0) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+bool vku_have_device_extension(const char *name)
+{
+       if(sel_dev < 0) return false;
+
+       if(!vkdevext) {
+               vkdevext_count = 0;
+               vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
+               if(vkdevext_count) {
+                       if(!(vkdevext = (VkExtensionProperties *)malloc(vkdevext_count * sizeof *vkdevext))) {
+                               perror("failed to allocate device extension list");
+                               return false;
+                       }
+                       vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
+
+                       printf("selected device extensions:\n");
+                       for(int i=0; i<(int)vkdevext_count; i++) {
+                               printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
+                       }
+               }
+       }
+
+       for(int i=0; i<(int)vkdevext_count; i++) {
+               if(strcmp(vkdevext[i].extensionName, name) == 0) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+bool vku_create_device()
+{
+       VkInstanceCreateInfo inst_info;
+       VkDeviceCreateInfo dev_info;
+       VkDeviceQueueCreateInfo queue_info;
+       VkCommandPoolCreateInfo cmdpool_info;
+       uint32_t num_devices;
+       float qprio = 0.0f;
+
+       static const char *ext_names[] = {
+               "VK_KHR_xcb_surface",
+               "VK_KHR_surface"
+       };
+
+       static const char *devext_names[] = {
+               "VK_KHR_swapchain"
+       };
+
+       sel_dev = -1;
+       sel_qfamily = -1;
+
+       for(unsigned int i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
+               if(!vku_have_extension(ext_names[i])) {
+                       fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
+                       return false;
+               }
+       }
+       memset(&inst_info, 0, sizeof inst_info);
+       inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
+       inst_info.ppEnabledExtensionNames = ext_names;
+       inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
+
+       if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) {
+               fprintf(stderr, "failed to create vulkan instance\n");
+               return false;
+       }
+       printf("created vulkan instance\n");
+       if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) {
+               fprintf(stderr, "failed to enumerate vulkan physical devices\n");
+               return false;
+       }
+       phys_devices = (VkPhysicalDevice *)malloc(num_devices * sizeof *phys_devices);
+       if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) {
+               fprintf(stderr, "failed to enumerate vulkan physical devices\n");
+               return false;
+       }
+       printf("found %u physical device(s)\n", (unsigned int)num_devices);
+
+       for(int i=0; i<(int)num_devices; i++) {
+               VkPhysicalDeviceProperties dev_prop;
+               VkPhysicalDeviceMemoryProperties mem_prop;
+               VkQueueFamilyProperties *qprop;
+               uint32_t qprop_count;
+
+               vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
+
+               printf("Device %d: %s\n", i, dev_prop.deviceName);
+               printf("  type: %s\n", get_device_name_str(dev_prop.deviceType));
+               printf("  API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
+                      ver_patch(dev_prop.apiVersion));
+               printf("  driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
+                      ver_patch(dev_prop.driverVersion));
+               printf("  vendor id: %x  device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
+
+
+               vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
+               printf("  %d memory heaps:\n", mem_prop.memoryHeapCount);
+               for(unsigned int j=0; j<mem_prop.memoryHeapCount; j++) {
+                       VkMemoryHeap heap = mem_prop.memoryHeaps[j];
+                       printf("    Heap %d - size: %s, flags: %s\n", j, get_mem_size_str(heap.size),
+                              heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
+               }
+               printf("  %d memory types:\n", mem_prop.memoryTypeCount);
+               for(unsigned int j=0; j<mem_prop.memoryTypeCount; j++) {
+                       VkMemoryType type = mem_prop.memoryTypes[j];
+                       printf("    Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
+                              get_memtype_flags_str(type.propertyFlags));
+               }
+
+               vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
+               if(qprop_count <= 0) {
+                       continue;
+               }
+               qprop = (VkQueueFamilyProperties *)malloc(qprop_count * sizeof *qprop);
+               vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
+
+               for(unsigned int j=0; j<qprop_count; j++) {
+                       printf("  Queue family %d:\n", j);
+                       printf("    flags: %s\n", get_queue_flags_str(qprop[j].queueFlags));
+                       printf("    num queues: %u\n", qprop[j].queueCount);
+
+                       if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
+                               sel_dev = i;
+                               sel_qfamily = j;
+                       }
+               }
+               free(qprop);
+       }
+
+       if(sel_dev < 0 || sel_qfamily < 0) {
+               fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
+               vkDestroyDevice(vkdev, 0);
+               return false;
+       }
+
+       for(unsigned int i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
+               if(!vku_have_device_extension(devext_names[i])) {
+                       fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
+                               ext_names[i], sel_dev);
+                       return false;
+               }
+       }
+
+       /* create device & command queue */
+       memset(&queue_info, 0, sizeof queue_info);
+       queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
+       queue_info.queueFamilyIndex = sel_qfamily;
+       queue_info.queueCount = 1;
+       queue_info.pQueuePriorities = &qprio;
+
+       memset(&dev_info, 0, sizeof dev_info);
+       dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
+       dev_info.queueCreateInfoCount = 1;
+       dev_info.pQueueCreateInfos = &queue_info;
+       dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
+       dev_info.ppEnabledExtensionNames = devext_names;
+
+       if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
+               fprintf(stderr, "failed to create device %d\n", sel_dev);
+               return false;
+       }
+       printf("created device %d\n", sel_dev);
+
+       vkpdev = phys_devices[sel_dev];
+       vkqfamily = sel_qfamily;
+
+       vkGetDeviceQueue(vkdev, sel_qfamily, 0, &vkq);
+
+       /* create command buffer pool */
+       memset(&cmdpool_info, 0, sizeof cmdpool_info);
+       cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+       cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
+       cmdpool_info.queueFamilyIndex = sel_qfamily;
+
+       if(vkCreateCommandPool(vkdev, &cmdpool_info, 0, &vkcmdpool) != 0) {
+               fprintf(stderr, "failed to get command quque!\n");
+               return false;
+       }
+
+       if(!(vkcmdbuf = vku_alloc_cmdbuf(vkcmdpool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
+               fprintf(stderr, "failed to create primary command buffer\n");
+               return false;
+       }
+
+       return true;
+}
+
+void vku_cleanup()
+{
+       if(vkinst) {
+               vkDeviceWaitIdle(vkdev);
+               vkDestroyCommandPool(vkdev, vkcmdpool, 0);
+               vkDestroyDevice(vkdev, 0);
+               vkDestroyInstance(vkinst, 0);
+               vkinst = 0;
+       }
+
+       free(phys_devices);
+       phys_devices = 0;
+}
+
+VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
+{
+       VkCommandBuffer cmdbuf;
+       VkCommandBufferAllocateInfo inf;
+
+       memset(&inf, 0, sizeof inf);
+       inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+       inf.commandPool = pool;
+       inf.level = level;
+       inf.commandBufferCount = 1;
+
+       if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
+               return 0;
+       }
+       return cmdbuf;
+}
+
+void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
+{
+       vkFreeCommandBuffers(vkdev, pool, 1, &buf);
+}
+
+void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
+{
+       VkCommandBufferBeginInfo inf;
+
+       memset(&inf, 0, sizeof inf);
+       inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+       inf.flags = flags;
+
+       vkBeginCommandBuffer(buf, &inf);
+}
+
+
+void vku_end_cmdbuf(VkCommandBuffer buf)
+{
+       vkEndCommandBuffer(buf);
+}
+
+void vku_reset_cmdbuf(VkCommandBuffer buf)
+{
+       vkResetCommandBuffer(buf, 0);
+}
+
+void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
+{
+       VkSubmitInfo info;
+
+       memset(&info, 0, sizeof info);
+       info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+       info.commandBufferCount = 1;
+       info.pCommandBuffers = &buf;
+
+       vkQueueSubmit(q, 1, &info, done_fence);
+}
+
+VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
+                                    VkPresentModeKHR pmode, VkSwapchainKHR prev)
+{
+       VkSwapchainKHR sc;
+       VkSwapchainCreateInfoKHR inf;
+
+       memset(&inf, 0, sizeof inf);
+       inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
+       inf.surface = surf;
+       inf.minImageCount = n;
+       inf.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;     /* TODO enumerate and choose */
+       inf.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
+       inf.imageExtent.width = xsz;
+       inf.imageExtent.height = ysz;
+       inf.imageArrayLayers = 1;
+       inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
+       inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;       /* XXX make this an option? */
+       inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
+       inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
+       inf.presentMode = pmode;
+       inf.oldSwapchain = prev;
+
+       if(vkCreateSwapchainKHR(vkdev, &inf, 0, &sc) != 0) {
+               return 0;
+       }
+       return sc;
+}
+
+VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count)
+{
+       uint32_t nimg;
+       VkImage *images;
+
+       if(vkGetSwapchainImagesKHR(vkdev, sc, &nimg, 0) != 0) {
+               return 0;
+       }
+       if(!(images = (VkImage *)malloc(nimg * sizeof *images))) {
+               return 0;
+       }
+       vkGetSwapchainImagesKHR(vkdev, sc, &nimg, images);
+
+       if(count) *count = (int)nimg;
+       return images;
+}
+
+int vku_get_next_image(VkSwapchainKHR sc)
+{
+       uint32_t next;
+
+       if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) {
+               return -1;
+       }
+       return (int)next;
+}
+
+void vku_present(VkSwapchainKHR sc, int img_idx)
+{
+       VkPresentInfoKHR inf;
+       VkResult res;
+       uint32_t index = img_idx;
+
+       memset(&inf, 0, sizeof inf);
+       inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
+       inf.swapchainCount = 1;
+       inf.pSwapchains = &sc;
+       inf.pImageIndices = &index;
+       inf.pResults = &res;
+
+       vkQueuePresentKHR(vkq, &inf);
+}
+
+struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
+{
+       struct vku_buffer *buf;
+       VkBufferCreateInfo binfo;
+
+       if(!(buf = (vku_buffer *)malloc(sizeof *buf))) {
+               perror("failed to allocate vk_buffer structure");
+               return 0;
+       }
+
+       memset(&binfo, 0, sizeof binfo);
+       binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+       binfo.size = sz;
+       binfo.usage = usage;
+       binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+
+       if(vkCreateBuffer(vkdev, &binfo, 0, &buf->buf) != 0) {
+               fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
+               return 0;
+       }
+       // TODO back with memory
+       return buf;
+}
+
+void vku_destroy_buffer(struct vku_buffer *buf)
+{
+       if(buf) {
+               vkDestroyBuffer(vkdev, buf->buf, 0);
+               free(buf);
+       }
+}
+
+void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
+                     VkBuffer src, int soffs, int size)
+{
+       VkBufferCopy copy;
+       copy.size = size;
+       copy.srcOffset = soffs;
+       copy.dstOffset = doffs;
+
+       vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
+}
+
+/* paste
+
+static bool create_instance()
+{
+       uint32_t layer_count = 0;
+       std::vector<const char *> enabled_layers;
+
+       if(vkEnumerateInstanceLayerProperties(&layer_count, 0) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to query layer properties.\n");
+               return false;
+       }
+
+       if(layer_count > 0) {
+               VkLayerProperties *layers = (VkLayerProperties *)alloca(layer_count * sizeof *layers);
+               vkEnumerateInstanceLayerProperties(&layer_count, layers);
+               for(uint32_t i=0; i<layer_count; i++) {
+                       if(strcmp(layers[i].layerName, "VK_LAYER_LUNARG_standard_validation")) {
+                               enabled_layers.push_back(layers[i].layerName);
+                       }
+               }
+       }
+
+       uint32_t extensions_count = 0;
+       std::vector<const char *> enabled_extensions;
+
+       if(vkEnumerateInstanceExtensionProperties(0, &extensions_count, 0) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to enumerate instance extension properties\n");
+               return false;
+       }
+
+       if(extensions_count > 0) {
+               VkExtensionProperties *extensions = (VkExtensionProperties *)alloca(extensions_count * sizeof *extensions);
+               vkEnumerateInstanceExtensionProperties(0, &extensions_count, extensions);
+
+               for(uint32_t i=0; i<extensions_count; i++) {
+                       printf("Extension %u: %s %u.\n", i, extensions[i].extensionName, extensions[i].specVersion);
+                       //enable all the available extensions
+                       enabled_extensions.push_back(extensions[i].extensionName);
+                       enabled_extension_names.push_back(std::string(extensions[i].extensionName));
+               }
+       }
+
+       VkInstanceCreateInfo create_info;
+       memset(&create_info, 0, sizeof create_info);
+       create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
+
+       if(!enabled_layers.empty()) {
+               create_info.enabledLayerCount = enabled_layers.size();
+               create_info.ppEnabledLayerNames = enabled_layers.data();
+       }
+
+       if(!enabled_extensions.empty()) {
+               create_info.enabledExtensionCount = enabled_extensions.size();
+               create_info.ppEnabledExtensionNames = enabled_extensions.data();
+       }
+
+       if(vkCreateInstance(&create_info, 0, &inst) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to create instance.\n");
+               return false;
+       }
+
+       if(!create_device())
+               return false;
+
+       return true;
+}
+
+static bool create_device()
+{
+       int qfam_idx = -1;
+       int pdev_idx = -1;
+
+       uint32_t dev_count;
+       if(vkEnumeratePhysicalDevices(inst, &dev_count, 0) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to enumerate physical devices.\n");
+               return false;
+       }
+       printf("%u devices found.\n", (unsigned int)dev_count);
+
+       VkPhysicalDevice *phys_dev = (VkPhysicalDevice *)alloca(dev_count * sizeof *phys_dev);
+       vkEnumeratePhysicalDevices(inst, &dev_count, phys_dev);
+       VkPhysicalDeviceMemoryProperties memprop;
+
+       for(uint32_t i=0; i<dev_count; i++) {
+               VkPhysicalDeviceProperties dev_props;
+               vkGetPhysicalDeviceProperties(phys_dev[i], &dev_props);
+
+               //memory heaps:
+               vkGetPhysicalDeviceMemoryProperties(phys_dev[i], &memprop);
+               printf("\tNumber of heaps: %u\n", memprop.memoryHeapCount);
+               for(uint32_t j=0; j<memprop.memoryHeapCount; j++) {
+                       printf("\t\tHeap %u size: %lu\n", j, (unsigned long)memprop.memoryHeaps[j].size);
+                       printf("\t\tHeap %u flags: %s\n", j, heap_flags_str(memprop.memoryHeaps[j].flags));
+               }
+               //memory types
+               printf("\tMemory types: %u\n", memprop.memoryTypeCount);
+               for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
+                       printf("\t\tType %u heap index: %u\n", j, memprop.memoryTypes[j].heapIndex);
+                       printf("\t\tType %u flags: %s\n", j, memtype_flags_str(memprop.memoryTypes[j].propertyFlags));
+               }
+
+               //supported features
+               VkPhysicalDeviceFeatures features;
+               vkGetPhysicalDeviceFeatures(phys_dev[i], &features);
+
+               //queue families
+               uint32_t qfam_count;
+               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, 0);
+               printf("\tQueue Families: %u\n", qfam_count);
+               VkQueueFamilyProperties *qfam_props = new VkQueueFamilyProperties[qfam_count];
+               vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, qfam_props);
+               for(uint32_t j=0; j<qfam_count; j++) {
+                       printf("\t\tFamily %u flags: %s\n", j, queue_flags_str(qfam_props[j].queueFlags));
+                       printf("\t\tFamily %u number of queues: %u\n", j, qfam_props[j].queueCount);
+
+                       if((qfam_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) && (pdev_idx == -1)) {
+                               pdev_idx = i;
+                               qfam_idx = j;
+                               num_queues = qfam_props[j].queueCount;
+                       }
+               }
+               delete [] qfam_props;
+       }
+
+       if(pdev_idx == -1) {
+               fprintf(stderr, "No suitable devices found.\n");
+               return false;
+       }
+
+       pdev = *(phys_dev + pdev_idx);
+       qfamily_idx = qfam_idx;
+
+               uint32_t layer_count;
+               if(vkEnumerateDeviceLayerProperties(pdev, &layer_count, 0) != VK_SUCCESS) {
+                       fprintf(stderr, "Failed to enumerate device layers.\n");
+                       return false;
+               }
+               if(layer_count > 0) {
+                       VkLayerProperties *layers = (VkLayerProperties*)alloca(layer_count * sizeof *layers);
+                       vkEnumerateDeviceLayerProperties(pdev, &layer_count, layers);
+                       printf("%u layers found.\n", layer_count);
+                       for(uint32_t i=0; i<layer_count; i++) {
+                               printf("Layer %u: %s (%u, %u)\n", i, layers[i].layerName,
+                                               layers[i].specVersion, layers[i].implementationVersion);
+                               printf("\tDesc: %s\n", layers[i].description);
+                       }
+               }
+
+       VkDeviceCreateInfo dev_info;
+       memset(&dev_info, 0, sizeof dev_info);
+       dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
+
+       VkDeviceQueueCreateInfo dev_qinfo;
+       memset(&dev_qinfo, 0, sizeof dev_qinfo);
+       dev_qinfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
+       dev_qinfo.queueFamilyIndex = qfam_idx;
+       dev_qinfo.queueCount = 1;
+
+       dev_info.queueCreateInfoCount = 1;
+       dev_info.pQueueCreateInfos = &dev_qinfo;
+
+       if(vkCreateDevice(pdev, &dev_info, 0, &device) != VK_SUCCESS) {
+               fprintf(stderr, "Failed to create logical device.\n");
+               return false;
+       }
+
+       vkGetPhysicalDeviceMemoryProperties(pdev, &memprop);
+       for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
+               if(memprop.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
+                       device_mem_idx = j;
+                       printf("Selected device memory index: %u\n", device_mem_idx);
+                       break;
+               }
+       }
+
+       return true;
+}
+
+static const char *dev_type_str(VkPhysicalDeviceType type)
+{
+       switch(type) {
+       case VK_PHYSICAL_DEVICE_TYPE_OTHER:
+               return "other";
+       case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
+               return "integrated GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
+               return "discrete GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
+               return "virtual GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_CPU:
+               return "CPU";
+       default:
+               break;
+       }
+       return "unknown";
+}
+
+static const char *heap_flags_str(VkMemoryHeapFlags flags)
+{
+       static std::string str;
+       str.clear();
+       if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
+               str += "device-local ";
+       }
+       if(!str.empty())
+               str.pop_back();
+       return str.c_str();
+}
+
+static const char *memtype_flags_str(VkMemoryPropertyFlags flags)
+{
+       static std::string str;
+       str.clear();
+       if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
+               str += "device-local ";
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
+               str += "host-visible ";
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
+               str += "host-coherent ";
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
+               str += "host-cached ";
+       }
+       if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
+               str += "lazily-allocated ";
+       }
+       if(!str.empty())
+               str.pop_back();
+       return str.c_str();
+}
+
+static const char *queue_flags_str(VkQueueFlags flags)
+{
+       static std::string str;
+       str.clear();
+       if(flags & VK_QUEUE_GRAPHICS_BIT)
+               str += "graphics ";
+       if(flags & VK_QUEUE_COMPUTE_BIT)
+               str += "compute ";
+       if(flags & VK_QUEUE_TRANSFER_BIT)
+               str += "transfer ";
+       if(flags & VK_QUEUE_SPARSE_BINDING_BIT)
+               str += "sparse-binding ";
+       if(!str.empty())
+               str.pop_back();
+       return str.c_str();
+}
+*/
+
+const char *vku_get_vulkan_error_str(VkResult error)
+{
+       std::string errmsg;
+       switch(error) {
+       case VK_SUCCESS:
+               errmsg = std::string("VK_SUCCESS");
+               break;
+       case VK_NOT_READY:
+               errmsg = std::string("VK_NOT_READY");
+               break;
+       case VK_TIMEOUT:
+               errmsg = std::string("VK_TIMEOUT");
+               break;
+       case VK_EVENT_SET:
+               errmsg = std::string("VK_EVENT_SET");
+               break;
+       case VK_EVENT_RESET:
+               errmsg = std::string("VK_EVENT_RESET");
+               break;
+       case VK_INCOMPLETE:
+               errmsg = std::string("VK_EVENT");
+               break;
+       case VK_ERROR_OUT_OF_HOST_MEMORY:
+               errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
+               break;
+       case VK_ERROR_OUT_OF_DEVICE_MEMORY:
+               errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
+               break;
+       case VK_ERROR_INITIALIZATION_FAILED:
+               errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
+               break;
+       case VK_ERROR_DEVICE_LOST:
+               errmsg = std::string("VK_ERROR_DEVICE_LOST");
+               break;
+       case VK_ERROR_MEMORY_MAP_FAILED:
+               errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
+               break;
+       case VK_ERROR_LAYER_NOT_PRESENT:
+               errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
+               break;
+       case VK_ERROR_EXTENSION_NOT_PRESENT:
+               errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
+               break;
+       case VK_ERROR_FEATURE_NOT_PRESENT:
+               errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
+               break;
+       case VK_ERROR_INCOMPATIBLE_DRIVER:
+               errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
+               break;
+       case VK_ERROR_TOO_MANY_OBJECTS:
+               errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
+               break;
+       case VK_ERROR_FORMAT_NOT_SUPPORTED:
+               errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
+               break;
+       case VK_ERROR_FRAGMENTED_POOL:
+               errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
+               break;
+       default:
+               errmsg = std::string("UNKNOWN");
+               break;
+       }
+
+       return errmsg.c_str();
+}
+
+static const char *get_device_name_str(VkPhysicalDeviceType type)
+{
+       switch(type) {
+       case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
+               return "integrated GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
+               return "discrete GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
+               return "virtual GPU";
+       case VK_PHYSICAL_DEVICE_TYPE_CPU:
+               return "CPU";
+       default:
+               break;
+       }
+       return "unknown";
+}
+
+static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
+{
+       static char str[128];
+
+       str[0] = 0;
+       if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
+               strcat(str, "device-local ");
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
+               strcat(str, "host-visible ");
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
+               strcat(str, "host-coherent ");
+       }
+       if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
+               strcat(str, "host-cached ");
+       }
+       if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
+               strcat(str, "lazily-allocated ");
+       }
+
+       if(!*str) {
+               strcat(str, "-");
+       }
+       return str;
+}
+
+static const char *get_queue_flags_str(VkQueueFlags flags)
+{
+       static char str[128];
+
+       str[0] = 0;
+       if(flags & VK_QUEUE_GRAPHICS_BIT) {
+               strcat(str, "graphics ");
+       }
+       if(flags & VK_QUEUE_COMPUTE_BIT) {
+               strcat(str, "compute ");
+       }
+       if(flags & VK_QUEUE_TRANSFER_BIT) {
+               strcat(str, "transfer ");
+       }
+       if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
+               strcat(str, "sparse-binding ");
+       }
+       if(!*str) {
+               strcat(str, "-");
+       }
+       return str;
+}
+
+static int ver_major(uint32_t ver)
+{
+       return (ver >> 22) & 0x3ff;
+}
+
+static int ver_minor(uint32_t ver)
+{
+       return (ver >> 12) & 0x3ff;
+}
+
+static int ver_patch(uint32_t ver)
+{
+       return ver & 0xfff;
+}
+
+static const char *get_mem_size_str(long sz)
+{
+       static char str[64];
+       static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
+       int uidx = 0;
+       sz *= 10;
+
+       while(sz >= 10240 && unitstr[uidx + 1]) {
+               sz /= 1024;
+               ++uidx;
+       }
+       sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
+       return str;
+}
diff --git a/src/vulkan/vkutil.h b/src/vulkan/vkutil.h
new file mode 100644 (file)
index 0000000..f9cbf94
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef VKUTIL_H_
+#define VKUTIL_H_
+
+#include <vulkan/vulkan.h>
+
+extern VkSwapchainKHR vkswapchain;
+extern VkImage *vkswapchain_images;
+extern VkSurfaceKHR vksurface;
+extern VkInstance vkinst;
+extern VkPhysicalDevice vkpdev;
+extern VkDevice vkdev;
+extern VkQueue vkq;
+extern VkCommandPool vkcmdpool;
+extern VkCommandBuffer vkcmdbuf;       /* primary command buffer */
+extern int vkqfamily;
+extern int vknext_swapchain_image;
+
+struct vku_buffer {
+       VkBuffer buf;
+       VkDeviceMemory mem_pool;
+       int mem_start, mem_size;
+};
+
+/* extensions */
+bool vku_have_extension(const char *name);
+bool vku_have_device_extension(const char *name);
+
+/* device */
+bool vku_create_device();
+void vku_cleanup();
+
+/* command buffers */
+VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level);
+void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf);
+
+void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags);
+void vku_end_cmdbuf(VkCommandBuffer buf);
+void vku_reset_cmdbuf(VkCommandBuffer buf);
+
+void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence);
+
+/* swapchain */
+VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
+               VkPresentModeKHR pmode, VkSwapchainKHR prev);
+VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count);
+int vku_get_next_image(VkSwapchainKHR sc);
+void vku_present(VkSwapchainKHR sc, int img_idx);
+
+/* buffers */
+vku_buffer *vku_create_buffer(int sz, unsigned int usage);
+void vku_destroy_buffer(vku_buffer *buf);
+
+void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
+       VkBuffer src, int soffs, int size);
+
+/* other */
+const char *vku_get_vulkan_error_str(VkResult error);
+
+#endif // VKUTIL_H_
\ No newline at end of file