extern "C" so that I can also use c++
[vkrt] / src / main.c
index cd2f140..af4277c 100644 (file)
@@ -5,7 +5,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "ui.h"
+#include "vk.h"
+#include "util.h"
 
 
 /* static glfw callbacks */
@@ -19,24 +20,37 @@ 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 = true;
 
 static int win_w = 800;
 static int win_h = 600;
 
 static struct vk_ctx vk_core;
-static struct vk_swapchain vk_swap;
+static VkSurfaceKHR vk_surf;
+static struct vk_renderer vk_rnd;
+static struct vk_swapchain vk_chain;
+static struct vk_semaphores vk_sema;
+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;
 
 int main(int argc, char** argv)
 {
@@ -52,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;
@@ -64,8 +82,12 @@ int main(int argc, char** argv)
 /* static functions */
 
 static bool
-init()
+init(void)
 {
+    char *vsdr = 0;
+    char *fsdr = 0;
+    int vsz, fsz;
+
     /* initialize GLFW */
 
     if (!glfwInit()) {
@@ -78,15 +100,6 @@ init()
         return false;
     }
 
-    /* initialize Vulkan context */
-
-    if (!vk_init_ctx_for_rendering(&vk_core)) {
-        fprintf(stderr, "Failed to initialize Vulkan context.\n");
-        return false;
-    }
-
-    memset(&vk_swap, 0, sizeof vk_swap);
-
     /* create window */
 
     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
@@ -97,29 +110,96 @@ init()
         return false;
     }
 
+    glfwSetKeyCallback(win, clb_key);
+
+    /* initialize Vulkan context (instance) */
+
+    if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
+        fprintf(stderr, "Failed to initialize Vulkan context.\n");
+        return false;
+    }
+
     /* create (Xcb) surface */
 
     glfwGetFramebufferSize(win, &win_h, &win_h);
-    if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_swap.surface)
+    if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
             != VK_SUCCESS) {
         fprintf(stderr, "Failed to create XCB surface.\n");
         return false;
     }
 
-    /* create Vulkan swapchain */
+    /* create semaphores */
+    if (!vk_create_semaphores(&vk_core, false, &vk_sema)) {
+        fprintf(stderr, "No semaphores were created.\n");
+        goto fail;
+    }
 
-    /* aquire images? */
+    /* create swapchain */
+    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;
+    }
 
-    /* create shaders */
+    if (vk_chain.swapchain == VK_NULL_HANDLE) {
+        fprintf(stderr, "Invalid swapchain handle.\n");
+        goto fail;
+    }
 
-    /* create semaphores? */
+    /* create shaders */
+    vsdr = sdr_load("data/main.vert.spv", &vsz);
+    fsdr = sdr_load("data/main.frag.spv", &fsz);
+
+    /* create depth attachment (for the moment we are going to use this
+     * for all images */
+    if (!vk_fill_image_props(&vk_core,
+                             win_w, win_h, 1,
+                             1, 1, 1,
+                             VK_FORMAT_D32_SFLOAT_S8_UINT,
+                             VK_IMAGE_TILING_OPTIMAL,
+                             VK_IMAGE_LAYOUT_UNDEFINED,
+                             VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
+                             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)) {
+        fprintf(stderr, "Failed to create depth attachment.\n");
+        goto fail;
+    }
 
     /* create renderer */
+    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)) {
+        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);
@@ -127,17 +207,76 @@ 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)
 {
-    vk_cleanup_ctx(&vk_core);
+    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_semaphores(&vk_core, &vk_sema);
+    vk_destroy_renderer(&vk_core, &vk_rnd);
+
+    if (vk_chain.swapchain) {
+        vk_destroy_swapchain(&vk_core, &vk_chain);
+        vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
+    }
+
+    glfwDestroyWindow(win);
+
+    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();
 }
 
@@ -159,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;
 }