Browse Source

Restructuring, added semaphores

Kajetan Johannes Hammerle 1 month ago
parent
commit
a67e1e19c0
1 changed files with 69 additions and 45 deletions
  1. 69 45
      src/VulkanWrapper.c

+ 69 - 45
src/VulkanWrapper.c

@@ -7,25 +7,31 @@
 #include "core/internal/GLFW.h"
 
 typedef struct {
+    VkSwapchainKHR handle;
+    size_t amount;
+    VkImage* images;
+    VkImageView* imageViews;
+} Swapchain;
+
+typedef struct {
+    // basic setup
     VkInstance instance;
+    VkSurfaceKHR surface;
     VkDebugUtilsMessengerEXT debugMessenger;
     VkDebugReportCallbackEXT debugReportCallback;
+    // render setup
     u32 graphicsFamily;
     VkPhysicalDevice physicalDevice;
     VkDevice device;
-    VkSurfaceKHR surface;
-    VkSwapchainKHR swapchain;
+    VkQueue graphicsQueue;
+    VkSemaphore semaphore;
+    VkSemaphore renderSemaphore;
+    Swapchain swapchain;
     u32 width;
     u32 height;
 } VulkanData;
 static VulkanData vk = {0};
 
-typedef struct {
-    size_t amount;
-    VkImage* images;
-    VkImageView* imageViews;
-} SwapchainImages;
-
 #define VK_ERROR_CASE(error)                                                   \
     case error: return #error
 
@@ -149,14 +155,6 @@ static void destroyDebugReportCallback() {
     f(vk.instance, vk.debugReportCallback, nullptr);
 }
 
-static void destroyDebugging() {
-    if(vk.instance == VK_NULL_HANDLE) {
-        return;
-    }
-    destroyDebugMessenger();
-    destroyDebugReportCallback();
-}
-
 static bool choosePhysicalDevice(VkPhysicalDevice* devices, u32 amount) {
     VK_ASSERT(vkEnumeratePhysicalDevices(vk.instance, &amount, devices));
     int bestPoints = 0;
@@ -237,6 +235,11 @@ static bool initDevice() {
         .pEnabledFeatures = &deviceFeatures};
     VK_ASSERT(vkCreateDevice(vk.physicalDevice, &deviceCreateInfo, nullptr,
                              &vk.device));
+    vkGetDeviceQueue(vk.device, vk.graphicsFamily, 0, &vk.graphicsQueue);
+    if(vk.graphicsQueue == VK_NULL_HANDLE) {
+        LOG_ERROR("Cannot get device graphics queue");
+        return true;
+    }
     return false;
 }
 
@@ -325,7 +328,8 @@ static bool initSwapchain() {
         .presentMode = pm,
         .clipped = VK_TRUE,
         .oldSwapchain = VK_NULL_HANDLE};
-    VK_ASSERT(vkCreateSwapchainKHR(vk.device, &ci, nullptr, &vk.swapchain));
+    VK_ASSERT(
+        vkCreateSwapchainKHR(vk.device, &ci, nullptr, &vk.swapchain.handle));
     return false;
 }
 
@@ -344,56 +348,76 @@ static bool createImageView(VkImage image, VkImageView* view) {
     return false;
 }
 
-static void removeSwapchainImages(SwapchainImages* i) {
-    for(size_t x = 0; x < i->amount; x++) {
-        vkDestroyImageView(vk.device, i->imageViews[x], nullptr);
+static void destroySwapchainImages() {
+    for(size_t x = 0; x < vk.swapchain.amount; x++) {
+        vkDestroyImageView(vk.device, vk.swapchain.imageViews[x], nullptr);
     }
-    coreFree(i->images);
-    coreFree(i->imageViews);
-    *i = (SwapchainImages){0};
+    coreFree(vk.swapchain.images);
+    coreFree(vk.swapchain.imageViews);
 }
 
-static bool createSwapchainImages(SwapchainImages* i) {
-    *i = (SwapchainImages){0};
+static bool initSwapchainImages() {
     u32 c = 0;
-    VK_ASSERT(vkGetSwapchainImagesKHR(vk.device, vk.swapchain, &c, nullptr));
+    VK_ASSERT(
+        vkGetSwapchainImagesKHR(vk.device, vk.swapchain.handle, &c, nullptr));
     LOG_INFO("Found %u images", c);
-    i->amount = c;
-    i->images = coreAllocate(sizeof(VkImage) * c);
-    i->imageViews = coreZeroAllocate(sizeof(VkImageView) * c);
-    VK_ASSERT(vkGetSwapchainImagesKHR(vk.device, vk.swapchain, &c, i->images));
+    vk.swapchain.amount = c;
+    vk.swapchain.images = coreAllocate(sizeof(VkImage) * c);
+    vk.swapchain.imageViews = coreZeroAllocate(sizeof(VkImageView) * c);
+    VK_ASSERT(vkGetSwapchainImagesKHR(vk.device, vk.swapchain.handle, &c,
+                                      vk.swapchain.images));
     for(u32 x = 0; x < c; x++) {
-        if(createImageView(i->images[x], i->imageViews + x)) {
-            removeSwapchainImages(i);
+        if(createImageView(vk.swapchain.images[x],
+                           vk.swapchain.imageViews + x)) {
             return true;
         }
     }
     return false;
 }
 
-bool initVulkan() {
-    vk.width = 400;
-    vk.height = 300;
-    if(initInstance() || initDebugging() || initPhysicalDevice() ||
-       initDevice() || initSurface() || initSwapchain()) {
-        return true;
-    }
-    SwapchainImages images;
-    if(createSwapchainImages(&images)) {
+static bool initSemaphore(VkSemaphore* s) {
+    VkSemaphoreCreateInfo info = {.sType =
+                                      VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
+    VK_ASSERT(vkCreateSemaphore(vk.device, &info, nullptr, s));
+    return false;
+}
+
+static void destroySemaphore(VkSemaphore s) {
+    vkDestroySemaphore(vk.device, s, nullptr);
+}
+
+static bool checkPresentationSupport() {
+    VkBool32 b = false;
+    VK_ASSERT(vkGetPhysicalDeviceSurfaceSupportKHR(
+        vk.physicalDevice, vk.graphicsFamily, vk.surface, &b));
+    if(!b) {
+        LOG_ERROR("Presentation is not supported");
         return true;
     }
-    removeSwapchainImages(&images);
     return false;
 }
 
+bool initVulkan() {
+    vk.width = 400;
+    vk.height = 300;
+    return initInstance() || initDebugging() || initSurface() ||
+           initPhysicalDevice() || initDevice() || checkPresentationSupport() ||
+           initSwapchain() || initSwapchainImages() ||
+           initSemaphore(&vk.semaphore) || initSemaphore(&vk.renderSemaphore);
+}
+
 void destroyVulkan(void) {
     if(vk.device != VK_NULL_HANDLE) {
-        vkDestroySwapchainKHR(vk.device, vk.swapchain, nullptr);
+        destroySwapchainImages();
+        vkDestroySwapchainKHR(vk.device, vk.swapchain.handle, nullptr);
+        destroySemaphore(vk.semaphore);
+        destroySemaphore(vk.renderSemaphore);
     }
+    vkDestroyDevice(vk.device, nullptr);
     if(vk.instance != VK_NULL_HANDLE) {
         vkDestroySurfaceKHR(vk.instance, vk.surface, nullptr);
+        destroyDebugMessenger();
+        destroyDebugReportCallback();
     }
-    vkDestroyDevice(vk.device, nullptr);
-    destroyDebugging();
     vkDestroyInstance(vk.instance, nullptr);
 }