Parcourir la source

Add render pass

Kajetan Johannes Hammerle il y a 3 mois
Parent
commit
55028c685e
5 fichiers modifiés avec 56 ajouts et 7 suppressions
  1. 1 0
      include/core/VulkanWrapper.h
  2. 50 4
      src/VulkanWrapper.c
  3. 2 0
      src/WindowManager.c
  4. 1 1
      test/Main.c
  5. 2 2
      test/modules/WindowManagerTests.c

+ 1 - 0
include/core/VulkanWrapper.h

@@ -2,6 +2,7 @@
 #define CORE_VULKAN_WRAPPER_H
 
 bool initVulkan(void);
+void renderVulkan(void);
 void destroyVulkan(void);
 
 #endif

+ 50 - 4
src/VulkanWrapper.c

@@ -7,12 +7,14 @@
 #include "core/internal/GLFW.h"
 
 typedef struct {
+    VkFormat format;
     VkSwapchainKHR handle;
     u32 amount;
     VkImage* images;
     VkImageView* imageViews;
     VkCommandPool commandPool;
     VkCommandBuffer* commandsBuffers;
+    VkFramebuffer* framebuffers;
 } Swapchain;
 
 typedef struct {
@@ -29,6 +31,7 @@ typedef struct {
     VkSemaphore semaphore;
     VkSemaphore renderSemaphore;
     Swapchain swapchain;
+    VkRenderPass renderPass;
     u32 width;
     u32 height;
 } VulkanData;
@@ -308,6 +311,7 @@ static bool initSwapchain() {
     if(getSurfaceFormat(&sf)) {
         return true;
     }
+    vk.swapchain.format = sf.format;
     VkPresentModeKHR pm = {0};
     if(getPresentMode(&pm)) {
         return true;
@@ -426,6 +430,32 @@ static void destroyCommandBuffers() {
     vkDestroyCommandPool(vk.device, vk.swapchain.commandPool, nullptr);
 }
 
+static bool initRenderPass() {
+    VkAttachmentDescription c = {
+        .format = vk.swapchain.format,
+        .samples = VK_SAMPLE_COUNT_1_BIT,
+        .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
+        .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
+        .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
+        .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
+        .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
+        .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR};
+    VkAttachmentReference ca = {
+        .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
+    VkSubpassDescription subpass = {.pipelineBindPoint =
+                                        VK_PIPELINE_BIND_POINT_GRAPHICS,
+                                    .colorAttachmentCount = 1,
+                                    .pColorAttachments = &ca};
+    VkRenderPassCreateInfo info = {
+        .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
+        .attachmentCount = 1,
+        .pAttachments = &c,
+        .subpassCount = 1,
+        .pSubpasses = &subpass};
+    VK_ASSERT(vkCreateRenderPass(vk.device, &info, nullptr, &vk.renderPass));
+    return false;
+}
+
 bool initVulkan() {
     vk.width = 400;
     vk.height = 300;
@@ -433,16 +463,32 @@ bool initVulkan() {
            initPhysicalDevice() || initDevice() || checkPresentationSupport() ||
            initSwapchain() || initSwapchainImages() ||
            initSemaphore(&vk.semaphore) || initSemaphore(&vk.renderSemaphore) ||
-           initCommandBuffers();
+           initCommandBuffers() || initRenderPass();
+}
+
+static bool fillCommandBuffer(u32 index) {
+    VkCommandBufferBeginInfo info = {
+        .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
+        .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT};
+    VK_ASSERT(vkBeginCommandBuffer(vk.swapchain.commandsBuffers[index], &info));
+    return false;
+}
+
+void renderVulkan() {
+    for(u32 i = 0; i < vk.swapchain.amount; i++) {
+        (void)fillCommandBuffer;
+        // fillCommandBuffer(i);
+    }
 }
 
 void destroyVulkan(void) {
     if(vk.device != VK_NULL_HANDLE) {
-        destroySwapchainImages();
-        vkDestroySwapchainKHR(vk.device, vk.swapchain.handle, nullptr);
+        vkDestroyRenderPass(vk.device, vk.renderPass, nullptr);
+        destroyCommandBuffers();
         destroySemaphore(vk.semaphore);
         destroySemaphore(vk.renderSemaphore);
-        destroyCommandBuffers();
+        destroySwapchainImages();
+        vkDestroySwapchainKHR(vk.device, vk.swapchain.handle, nullptr);
     }
     vkDestroyDevice(vk.device, nullptr);
     if(vk.instance != VK_NULL_HANDLE) {

+ 2 - 0
src/WindowManager.c

@@ -343,6 +343,7 @@ static void tick() {
 }
 
 void runWindow(void) {
+    searchForGamepad(); // this is slow the first time
     tps.last = getNanos();
     fps.last = getNanos();
     i64 lag = 0;
@@ -352,6 +353,7 @@ void runWindow(void) {
             lag -= nanosPerTick;
             tick();
         }
+        renderVulkan();
         renderHandler(renderHandlerData, (float)lag / (float)nanosPerTick);
         endFrame();
     }

+ 1 - 1
test/Main.c

@@ -8,7 +8,7 @@
 
 int main(int argAmount, char** args) {
     if(argAmount >= 2 && strcmp(args[1], "help") == 0) {
-        puts("test");
+        // puts("test");
         puts("window");
         return 0;
     }

+ 2 - 2
test/modules/WindowManagerTests.c

@@ -5,7 +5,7 @@
 #include "../Tests.h"
 #include "core/WindowManager.h"
 
-static int ticks = 5;
+static int ticks = 2;
 static Button closeButton = 0;
 static Button testButton = 0;
 static Button textButton = 0;
@@ -62,7 +62,7 @@ void testWindow(void) {
     setWindowRunHandler(isRunning, nullptr);
     setWindowTickHandler(tick, nullptr);
     setWindowRenderHandler(render, nullptr);
-    setWindowNanosPerTick(50000000);
+    setWindowNanosPerTick(50'000'000);
     runWindow();
     closeWindow();
 }