X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fvulkan%2Fvk.cc;h=871824fef12a14cc084f1fa04ab805c21f27c79e;hp=105cabae25dd07f5fd287114dda061288f158e3f;hb=9148ff7614b1dc22fd5d1dcf6d74e2fb4ff13706;hpb=0c5fa3525b2c8151bf83a215eee992c257d6fa28 diff --git a/src/vulkan/vk.cc b/src/vulkan/vk.cc index 105caba..871824f 100644 --- a/src/vulkan/vk.cc +++ b/src/vulkan/vk.cc @@ -4,36 +4,20 @@ #include #include #include - #include #include #include "gfxapi.h" +#include "vkutil.h" /* global variables */ extern GLFWwindow *win; extern int win_w; extern int win_h; -/* static variables */ -static std::vector 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 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 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 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