Quellcode durchsuchen

Add command pool and buffers

Kajetan Johannes Hammerle vor 3 Monaten
Ursprung
Commit
d0c7d78314
1 geänderte Dateien mit 33 neuen und 2 gelöschten Zeilen
  1. 33 2
      src/VulkanWrapper.c

+ 33 - 2
src/VulkanWrapper.c

@@ -8,9 +8,11 @@
 
 typedef struct {
     VkSwapchainKHR handle;
-    size_t amount;
+    u32 amount;
     VkImage* images;
     VkImageView* imageViews;
+    VkCommandPool commandPool;
+    VkCommandBuffer* commandsBuffers;
 } Swapchain;
 
 typedef struct {
@@ -397,13 +399,41 @@ static bool checkPresentationSupport() {
     return false;
 }
 
+static bool initCommandBuffers() {
+    VkCommandPoolCreateInfo info = {
+        .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
+        .queueFamilyIndex = vk.graphicsFamily};
+    VK_ASSERT(vkCreateCommandPool(vk.device, &info, nullptr,
+                                  &vk.swapchain.commandPool));
+    vk.swapchain.commandsBuffers =
+        coreAllocate(sizeof(VkCommandBuffer) * vk.swapchain.amount);
+    VkCommandBufferAllocateInfo a = {
+        .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
+        .commandPool = vk.swapchain.commandPool,
+        .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
+        .commandBufferCount = vk.swapchain.amount};
+    VK_ASSERT(
+        vkAllocateCommandBuffers(vk.device, &a, vk.swapchain.commandsBuffers));
+    return false;
+}
+
+static void destroyCommandBuffers() {
+    if(vk.swapchain.commandPool != VK_NULL_HANDLE && vk.swapchain.amount > 0) {
+        vkFreeCommandBuffers(vk.device, vk.swapchain.commandPool,
+                             vk.swapchain.amount, vk.swapchain.commandsBuffers);
+    }
+    coreFree(vk.swapchain.commandsBuffers);
+    vkDestroyCommandPool(vk.device, vk.swapchain.commandPool, nullptr);
+}
+
 bool initVulkan() {
     vk.width = 400;
     vk.height = 300;
     return initInstance() || initDebugging() || initSurface() ||
            initPhysicalDevice() || initDevice() || checkPresentationSupport() ||
            initSwapchain() || initSwapchainImages() ||
-           initSemaphore(&vk.semaphore) || initSemaphore(&vk.renderSemaphore);
+           initSemaphore(&vk.semaphore) || initSemaphore(&vk.renderSemaphore) ||
+           initCommandBuffers();
 }
 
 void destroyVulkan(void) {
@@ -412,6 +442,7 @@ void destroyVulkan(void) {
         vkDestroySwapchainKHR(vk.device, vk.swapchain.handle, nullptr);
         destroySemaphore(vk.semaphore);
         destroySemaphore(vk.renderSemaphore);
+        destroyCommandBuffers();
     }
     vkDestroyDevice(vk.device, nullptr);
     if(vk.instance != VK_NULL_HANDLE) {