fixes in image structs and functions
[vkrt] / src / main.c
index d12433e..6309b41 100644 (file)
@@ -1,8 +1,208 @@
 #include <stdio.h>
 
+#define GLFW_INCLUDE_VULKAN
+#include <GLFW/glfw3.h>
+#include <stdlib.h>
+#include <string.h>
+
 #include "vk.h"
+#include "util.h"
+
+
+/* static glfw callbacks */
+
+static void
+clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
+
+static void
+clb_reshape(GLFWwindow *win, int width, int height);
+
+/* static functions */
+
+static bool
+init();
+
+static void
+cleanup();
+
+static void
+display();
+
+
+/* static variables */
+
+static GLFWwindow *win;
+
+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;
+
+/* 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)
 {
-       printf("hi\n");
+    atexit(cleanup);
+
+    if (!init()) {
+        return 1;
+    }
+
+    /* reshape window once just in case */
+
+    glfwGetWindowSize(win, &win_w, &win_h);
+    clb_reshape(win, win_w, win_h);
+
+    /* event loop */
+
+    while(!glfwWindowShouldClose(win)) {
+        display();
+        glfwPollEvents();
+    }
+
+    return 0;
+}
+
+/* static functions */
+
+static bool
+init()
+{
+    char *vsdr, *fsdr;
+
+    /* initialize GLFW */
+
+    if (!glfwInit()) {
+        fprintf(stderr, "Failed to initialize GLFW.\n");
+        return false;
+    }
+
+    if (glfwVulkanSupported() != GLFW_TRUE) {
+        fprintf(stderr, "Vulkan is not supported on this device.\n");
+        return false;
+    }
+
+    /* create window */
+
+    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+    win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0);
+
+    if (!win) {
+        fprintf(stderr, "Failed to create GLFW window\n");
+        return false;
+    }
+
+    /* initialize Vulkan context (instance) */
+
+    if (!vk_init_ctx_for_rendering(&vk_core)) {
+        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_surf)
+            != VK_SUCCESS) {
+        fprintf(stderr, "Failed to create XCB surface.\n");
+        glfwTerminate();
+
+        return false;
+    }
+
+    /* create semaphores */
+    vk_create_semaphores(&vk_core, false, &vk_sema);
+
+    /* create swapchain */
+    vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
+
+    /* 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_D24_UNORM_S8_UINT,
+                             VK_IMAGE_TILING_OPTIMAL,
+                             VK_IMAGE_LAYOUT_UNDEFINED,
+                             VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
+                             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");
+        return false;
+    }
+
+    /* create renderer */
+    if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
+                            false, false, 0, &vk_depth_att,
+                            &vk_vert_info, &vk_rnd)) {
+        fprintf(stderr, "Failed to create renderer.\n");
+        return false;
+    }
+
+    /* set GLFW callbacks */
+
+    glfwSetKeyCallback(win, clb_key);
+    glfwSetWindowSizeCallback(win, clb_reshape);
+
+    /*
+    glfwSetCursorPosCallback(win, clb_motion);
+    glfwSetMouseButtonCallback(win, clb_mouse);
+    */
+
+    return true;
+}
+
+static void
+display()
+{
+}
+
+static void
+cleanup()
+{
+    vk_destroy_renderer(&vk_core, &vk_rnd);
+
+    vk_cleanup_ctx(&vk_core);
+
+    glfwTerminate();
+}
+
+/* glfw callbacks */
+
+static void
+clb_key(GLFWwindow *win, int key, int scancode,
+        int action, int mods)
+{
+    if (action == GLFW_PRESS) {
+        switch(key) {
+        case GLFW_KEY_ESCAPE:
+            glfwSetWindowShouldClose(win, GLFW_TRUE);
+            return;
+        }
+    }
+}
+
+static void
+clb_reshape(GLFWwindow *win, int width, int height)
+{
+    /* set viewport */
+
+    win_w = width;
+    win_h = height;
 }