X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fvulkan%2Fvk.cc;h=f5829ab13c53e95de8179357f3e2a5358cb973c4;hb=8fb0cca684e078cd2537070c53ad970ebbc2e9a7;hp=105cabae25dd07f5fd287114dda061288f158e3f;hpb=0c5fa3525b2c8151bf83a215eee992c257d6fa28;p=demo diff --git a/src/vulkan/vk.cc b/src/vulkan/vk.cc index 105caba..f5829ab 100644 --- a/src/vulkan/vk.cc +++ b/src/vulkan/vk.cc @@ -2,38 +2,58 @@ #include #include +#include #include #include - #include #include +#include + +#include "allocator.h" #include "gfxapi.h" +#include "image.h" +#include "vkutil.h" +#include "vk.h" /* global variables */ extern GLFWwindow *win; extern int win_w; extern int win_h; +static VkCommandBuffer cmd_buf; // command buffer used at zbuffer creation +//depth buffer +static VkImage dimg; +static VkImageView dview; +//swapchain +static VkImage *images; +static VkImageView *iviews; +static uint32_t num_images; + /* 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 VkDeviceMemory gpu_mem; // to be replaced when I fix the allocator +// zbuffer image, view: + +VkCommandBuffer *swapchain_cmd_bufs; + +/* static functions */ +static void error_callback(int error, const char *descr); +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); +static void swapbuffers(); +static void begin_drawing(); +static void end_drawing(); + +static bool create_swapchain(); +static bool begin_command_buffer(); +static bool create_swapchain_image_views(VkFormat format); +static bool create_zbuffer(); + +static bool create_swapchain_cmd_bufs(VkCommandPool vk_pool); +static bool record_cmd_clear(float r, float g, float b); bool init_vulkan() { @@ -47,12 +67,16 @@ bool init_vulkan() return false; } - if(!create_instance()) { - fprintf(stderr, "Failed to enable validation.\n"); + //TODO: remove later + glfwSetErrorCallback(error_callback); + + /* create device and command pool! */ + if(!vku_create_device()) { + fprintf(stderr, "Failed to initialize vulkan.\n"); return false; } - if(!glfwGetPhysicalDevicePresentationSupport(inst, pdev, qfamily_idx)) { + if(!glfwGetPhysicalDevicePresentationSupport(vkinst, vk_physical, vkqfamily)) { fprintf(stderr, "Presentation support not found.\n"); return false; } @@ -63,341 +87,501 @@ 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)); + VkResult res = glfwCreateWindowSurface(vkinst, win, 0, &vk_surface); + if(res != VK_SUCCESS) { + fprintf(stderr, "Failed to create KHR surface: %s\n", vku_get_vulkan_error_str(res)); return false; } - return true; -} + if(!create_swapchain()) { + fprintf(stderr, "Failed to create swapchain.\n"); + return false; + } -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 + cmd_buf = VK_NULL_HANDLE; + if(!begin_command_buffer()) { + fprintf(stderr, "Failed to start VK_NULL_HANDLE command buffer.\n"); + return false; + } - vkDestroySurfaceKHR(inst, surface, 0); + if(!create_zbuffer()) { + fprintf(stderr, "Failed to create depth buffer.\n"); + return false; + } + + gfx_clear = clear; + gfx_viewport = viewport; + gfx_zbuffer = zbuffer; + gfx_cull_face = cull_face; + gfx_reshape = reshape; + gfx_swapbuffers = swapbuffers; + gfx_begin_drawing = begin_drawing; + gfx_end_drawing = end_drawing; + + +/* + if(!vku_create_semaphores()) + return false; + + if(!(vk_swapchain = vku_create_swapchain(vk_surface, win_w, win_h, 2, + VK_PRESENT_MODE_IMMEDIATE_KHR, 0))) { + fprintf(stderr, "Failed to create swapchain.\n"); + return false; + } - if(vkDeviceWaitIdle(device) == VK_SUCCESS) { - vkDestroyDevice(device, 0); - vkDestroyInstance(inst, 0); + vkswapchain_images = vku_get_swapchain_images(vk_swapchain, &vknum_swapchain_images); + if(!vkswapchain_images) { + fprintf(stderr, "Failed to get swapchain images.\n"); + return false; } + +// vkswapchain_views = vku_create_image_views(vkswapchain_images, vknum_swapchain_images); +// if(!vkswapchain_views) { +// fprintf(stderr, "Failed to create swapchain image views.\n"); +// delete [] vkswapchain_images; +// return false; +// } + + if(!create_swapchain_cmd_bufs(vk_pool)) { + return false; + } + + if(!record_cmd_clear(1.0, 0.1, 0.1)) + return false; + */ + + return true; } -static bool create_instance() +static bool create_swapchain() { - /* enable layers */ - uint32_t layer_count = 0; - std::vector enabled_layers; + /* surface capabilities */ + VkSurfaceCapabilitiesKHR scap; + if(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical, vk_surface, &scap) != VK_SUCCESS) { + fprintf(stderr, "Failed to get physical device surface capabilities\n"); + return false; + } - if(vkEnumerateInstanceLayerProperties(&layer_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to query layer properties.\n"); + /* presentation modes */ + uint32_t prmode_cnt; + if(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, vk_surface, &prmode_cnt, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to get physical device surface presentation modes count.\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; + VkPresentModeKHR scmode = VK_PRESENT_MODE_FIFO_KHR; + VkPresentModeKHR *modes = new VkPresentModeKHR[prmode_cnt]; - if(vkEnumerateInstanceExtensionProperties(0, &extensions_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to enumerate instance extension properties\n"); + if(vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, vk_surface, &prmode_cnt, modes) != VK_SUCCESS) { + fprintf(stderr, "Failed to get physical device presentation modes.\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 && num_images > scap.maxImageCount) + num_images = scap.maxImageCount; + + printf("num_images : %u\n", num_images); + assert(num_images > 0); + + /* transform flags */ + VkSurfaceTransformFlagBitsKHR pre_transf = scap.currentTransform; + if(scap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) + pre_transf = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; + + /* find suitable colorspace, format */ + uint32_t fcount; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface, &fcount, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to get format count for physical device.\n"); + return false; + } + if(fcount == 0) { + fprintf(stderr, "No color formats were found.\n"); return false; } - if(!create_device()) + VkFormat format; + VkColorSpaceKHR colorspace; + + VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount]; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface, &fcount, formats) != VK_SUCCESS) { + fprintf(stderr, "Failed to get surface formats.\n"); + return false; + } + if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) { + format = VK_FORMAT_B8G8R8_UNORM; + } else { + format = formats[0].format; + } + colorspace = formats[0].colorSpace; + + /* creating the swapchain */ + VkSwapchainCreateInfoKHR sinfo; + memset(&sinfo, 0, sizeof sinfo); + + sinfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + sinfo.surface = vk_surface; + sinfo.minImageCount = num_images; + sinfo.imageFormat = format; + sinfo.imageColorSpace = colorspace; + sinfo.imageExtent = scextent; + sinfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + sinfo.preTransform = pre_transf; + sinfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + sinfo.imageArrayLayers = 1; + sinfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; + sinfo.presentMode = scmode; + sinfo.oldSwapchain = VK_NULL_HANDLE; //TODO + sinfo.clipped = VK_TRUE; //TODO + + if(vkCreateSwapchainKHR(vk_device, &sinfo, 0, &vk_swapchain) != VK_SUCCESS) { + fprintf(stderr, "Failed to create swapchain.\n"); return false; + } - return true; -} + if(vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &num_images, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to get the number of the swapchain images.\n"); + return false; + } -static bool create_device() -{ - int qfam_idx = -1; - int pdev_idx = -1; + images = new VkImage[num_images]; + if(vkGetSwapchainImagesKHR(vk_device, vk_swapchain, &num_images, images) != VK_SUCCESS) { + fprintf(stderr, "Failed to get the swapchain images.\n"); + return false; + } + assert(images); - uint32_t dev_count; - if(vkEnumeratePhysicalDevices(inst, &dev_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to enumerate physical devices.\n"); + if(!create_swapchain_image_views(format)) { + fprintf(stderr, "Failed to create image views for the swapchain images.\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; + // create renderpass - 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