working swapchain
[vkrt] / src / main.c
index 6309b41..eb1cf18 100644 (file)
@@ -20,31 +20,34 @@ clb_reshape(GLFWwindow *win, int width, int height);
 /* static functions */
 
 static bool
-init();
+init(void);
 
 static void
-cleanup();
+cleanup(void);
 
 static void
-display();
-
+display(void);
 
 /* static variables */
 
 static GLFWwindow *win;
+static bool redraw_pending;
+
+static bool vk_enable_layers;
 
 static int win_w = 800;
 static int win_h = 600;
 
 static struct vk_ctx vk_core;
 static VkSurfaceKHR vk_surf;
-static int vsz, fsz;
 static struct vk_renderer vk_rnd;
 static struct vk_swapchain vk_chain;
 static struct vk_semaphores vk_sema;
-static struct vk_image_attachment vk_color_att;
-static struct vk_image_attachment vk_depth_att;
-static struct vk_image_props vk_depth_att_props;
+static struct vk_attachment vk_depth_att;
+static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 };
+
+/* make array later if many cmd buffers */
+static VkCommandBuffer vk_cmd_buf;
 
 /* empty for as long as we hardcode the vertices in the vertex shader */
 static struct vk_vertex_info vk_vert_info;
@@ -63,10 +66,14 @@ int main(int argc, char** argv)
     clb_reshape(win, win_w, win_h);
 
     /* event loop */
+    redraw_pending = true;
 
     while(!glfwWindowShouldClose(win)) {
-        display();
-        glfwPollEvents();
+        glfwWaitEvents();
+        if (redraw_pending) {
+            redraw_pending = false;
+            display();
+        }
     }
 
     return 0;
@@ -75,9 +82,11 @@ int main(int argc, char** argv)
 /* static functions */
 
 static bool
-init()
+init(void)
 {
-    char *vsdr, *fsdr;
+    char *vsdr = 0;
+    char *fsdr = 0;
+    int vsz, fsz;
 
     /* initialize GLFW */
 
@@ -101,9 +110,11 @@ init()
         return false;
     }
 
+    glfwSetKeyCallback(win, clb_key);
+
     /* initialize Vulkan context (instance) */
 
-    if (!vk_init_ctx_for_rendering(&vk_core)) {
+    if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
         fprintf(stderr, "Failed to initialize Vulkan context.\n");
         return false;
     }
@@ -114,16 +125,25 @@ init()
     if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
             != VK_SUCCESS) {
         fprintf(stderr, "Failed to create XCB surface.\n");
-        glfwTerminate();
-
         return false;
     }
 
     /* create semaphores */
-    vk_create_semaphores(&vk_core, false, &vk_sema);
+    if (!vk_create_semaphores(&vk_core, false, &vk_sema)) {
+        fprintf(stderr, "No semaphores were created.\n");
+        goto fail;
+    }
 
     /* create swapchain */
-    vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
+    if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) {
+        fprintf(stderr, "No swapchain was created.\n");
+        goto fail;
+    }
+
+    if (vk_chain.swapchain == VK_NULL_HANDLE) {
+        fprintf(stderr, "Invalid swapchain handle.\n");
+        goto fail;
+    }
 
     /* create shaders */
     vsdr = sdr_load("data/main.vert.spv", &vsz);
@@ -138,27 +158,48 @@ init()
                              VK_IMAGE_TILING_OPTIMAL,
                              VK_IMAGE_LAYOUT_UNDEFINED,
                              VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
-                             false, &vk_depth_att_props)) {
+                             false, true, false,
+                             &vk_depth_att.props)) {
         fprintf(stderr, "Unsupported depth image properties\n");
         return false;
     }
-    if (!vk_create_image(&vk_core, &vk_depth_att_props, &vk_depth_att.obj)) {
+    if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
         fprintf(stderr, "Failed to create depth attachment.\n");
-        return false;
+        goto fail;
     }
 
     /* create renderer */
-    if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
-                            false, false, 0, &vk_depth_att,
+    if (!vk_create_renderer(&vk_core,
+                            vsdr, vsz, fsdr, fsz,
+                            win_w, win_h, 1,
+                            false, false,
+                            vk_chain.num_atts, vk_chain.atts, &vk_depth_att,
                             &vk_vert_info, &vk_rnd)) {
-        fprintf(stderr, "Failed to create renderer.\n");
-        return false;
+        goto fail;
+    }
+
+    /* create cmd buffer */
+    if ((vk_cmd_buf = vk_create_cmd_buffer(&vk_core)) == VK_NULL_HANDLE) {
+        fprintf(stderr, "Failed to create command buffer.\n");
+        goto fail;
+    }
+
+    /* record cmd buffer */
+    if (!vk_record_cmd_buffer(&vk_core, vk_cmd_buf,
+                              &vk_rnd, 0,
+                              4, vk_fb_color,
+                              vk_chain.num_atts + 1, 0,
+                              0, 0, win_w, win_h)) {
+        fprintf(stderr, "Failed to record command buffer.\n");
+        goto fail;
     }
 
+    free(vsdr);
+    free(fsdr);
+
     /* set GLFW callbacks */
 
-    glfwSetKeyCallback(win, clb_key);
-    glfwSetWindowSizeCallback(win, clb_reshape);
+    /* glfwSetWindowSizeCallback(win, clb_reshape); */
 
     /*
     glfwSetCursorPosCallback(win, clb_motion);
@@ -166,19 +207,75 @@ init()
     */
 
     return true;
+
+fail:
+    free(vsdr);
+    free(fsdr);
+
+    return false;
 }
 
 static void
-display()
+display(void)
 {
+    uint32_t img_idx;
+    VkSubmitInfo sinfo;
+    VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
+       VkPresentInfoKHR pinfo;
+
+    vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain,
+                          UINT64_MAX, vk_sema.frame_ready, 0, &img_idx);
+
+    memset(&sinfo, 0, sizeof sinfo);
+    sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+    sinfo.waitSemaphoreCount = 1;
+    sinfo.pWaitSemaphores = &vk_sema.frame_ready;
+    sinfo.pWaitDstStageMask = &wait_stages;
+    sinfo.commandBufferCount = 1;
+    sinfo.pCommandBuffers = &vk_cmd_buf;
+    sinfo.signalSemaphoreCount = 1;
+       sinfo.pSignalSemaphores = &vk_sema.frame_done;
+
+    if (vkQueueSubmit(vk_core.queue, 1, &sinfo, 0) != 0) {
+        fprintf(stderr, "Failed to submit draw commands.\n");
+        abort();
+    }
+
+    memset(&pinfo, 0, sizeof pinfo);
+    pinfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
+    pinfo.waitSemaphoreCount = 1;
+    pinfo.pWaitSemaphores = &vk_sema.frame_done;
+    pinfo.swapchainCount = 1;
+    pinfo.pSwapchains = &vk_chain.swapchain;
+    pinfo.pImageIndices = &img_idx;
+
+    vkQueuePresentKHR(vk_core.queue, &pinfo);
 }
 
 static void
-cleanup()
+cleanup(void)
 {
+    vkQueueWaitIdle(vk_core.queue);
+
+    if (vk_cmd_buf != VK_NULL_HANDLE) {
+        vkResetCommandBuffer(vk_cmd_buf, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
+    }
+
+    vk_destroy_image(&vk_core, &vk_depth_att.obj);
     vk_destroy_renderer(&vk_core, &vk_rnd);
+    vk_destroy_semaphores(&vk_core, &vk_sema);
+
+    if (vk_chain.swapchain) {
+        vk_destroy_swapchain(&vk_core, &vk_chain);
+        vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
+    }
+
+    glfwDestroyWindow(win);
 
-    vk_cleanup_ctx(&vk_core);
+    if (!vk_enable_layers)
+        vkFreeCommandBuffers(vk_core.dev, vk_core.cmd_pool, 1, &vk_cmd_buf);
+
+    vk_cleanup_ctx(&vk_core, vk_enable_layers);
 
     glfwTerminate();
 }
@@ -201,8 +298,4 @@ clb_key(GLFWwindow *win, int key, int scancode,
 static void
 clb_reshape(GLFWwindow *win, int width, int height)
 {
-    /* set viewport */
-
-    win_w = width;
-    win_h = height;
 }