fixes in image structs and functions
[vkrt] / src / main.c
1 #include <stdio.h>
2
3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "vk.h"
9 #include "util.h"
10
11
12 /* static glfw callbacks */
13
14 static void
15 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
16
17 static void
18 clb_reshape(GLFWwindow *win, int width, int height);
19
20 /* static functions */
21
22 static bool
23 init();
24
25 static void
26 cleanup();
27
28 static void
29 display();
30
31
32 /* static variables */
33
34 static GLFWwindow *win;
35
36 static int win_w = 800;
37 static int win_h = 600;
38
39 static struct vk_ctx vk_core;
40 static VkSurfaceKHR vk_surf;
41 static int vsz, fsz;
42 static struct vk_renderer vk_rnd;
43 static struct vk_swapchain vk_chain;
44 static struct vk_semaphores vk_sema;
45 static struct vk_image_attachment vk_color_att;
46 static struct vk_image_attachment vk_depth_att;
47 static struct vk_image_props vk_depth_att_props;
48
49 /* empty for as long as we hardcode the vertices in the vertex shader */
50 static struct vk_vertex_info vk_vert_info;
51
52 int main(int argc, char** argv)
53 {
54     atexit(cleanup);
55
56     if (!init()) {
57         return 1;
58     }
59
60     /* reshape window once just in case */
61
62     glfwGetWindowSize(win, &win_w, &win_h);
63     clb_reshape(win, win_w, win_h);
64
65     /* event loop */
66
67     while(!glfwWindowShouldClose(win)) {
68         display();
69         glfwPollEvents();
70     }
71
72     return 0;
73 }
74
75 /* static functions */
76
77 static bool
78 init()
79 {
80     char *vsdr, *fsdr;
81
82     /* initialize GLFW */
83
84     if (!glfwInit()) {
85         fprintf(stderr, "Failed to initialize GLFW.\n");
86         return false;
87     }
88
89     if (glfwVulkanSupported() != GLFW_TRUE) {
90         fprintf(stderr, "Vulkan is not supported on this device.\n");
91         return false;
92     }
93
94     /* create window */
95
96     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
97     win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0);
98
99     if (!win) {
100         fprintf(stderr, "Failed to create GLFW window\n");
101         return false;
102     }
103
104     /* initialize Vulkan context (instance) */
105
106     if (!vk_init_ctx_for_rendering(&vk_core)) {
107         fprintf(stderr, "Failed to initialize Vulkan context.\n");
108         return false;
109     }
110
111     /* create (Xcb) surface */
112
113     glfwGetFramebufferSize(win, &win_h, &win_h);
114     if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
115             != VK_SUCCESS) {
116         fprintf(stderr, "Failed to create XCB surface.\n");
117         glfwTerminate();
118
119         return false;
120     }
121
122     /* create semaphores */
123     vk_create_semaphores(&vk_core, false, &vk_sema);
124
125     /* create swapchain */
126     vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
127
128     /* create shaders */
129     vsdr = sdr_load("data/main.vert.spv", &vsz);
130     fsdr = sdr_load("data/main.frag.spv", &fsz);
131
132     /* create depth attachment (for the moment we are going to use this
133      * for all images */
134     if (!vk_fill_image_props(&vk_core,
135                              win_w, win_h, 1,
136                              1, 1, 1,
137                              VK_FORMAT_D24_UNORM_S8_UINT,
138                              VK_IMAGE_TILING_OPTIMAL,
139                              VK_IMAGE_LAYOUT_UNDEFINED,
140                              VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
141                              false, &vk_depth_att_props)) {
142         fprintf(stderr, "Unsupported depth image properties\n");
143         return false;
144     }
145     if (!vk_create_image(&vk_core, &vk_depth_att_props, &vk_depth_att.obj)) {
146         fprintf(stderr, "Failed to create depth attachment.\n");
147         return false;
148     }
149
150     /* create renderer */
151     if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
152                             false, false, 0, &vk_depth_att,
153                             &vk_vert_info, &vk_rnd)) {
154         fprintf(stderr, "Failed to create renderer.\n");
155         return false;
156     }
157
158     /* set GLFW callbacks */
159
160     glfwSetKeyCallback(win, clb_key);
161     glfwSetWindowSizeCallback(win, clb_reshape);
162
163     /*
164     glfwSetCursorPosCallback(win, clb_motion);
165     glfwSetMouseButtonCallback(win, clb_mouse);
166     */
167
168     return true;
169 }
170
171 static void
172 display()
173 {
174 }
175
176 static void
177 cleanup()
178 {
179     vk_destroy_renderer(&vk_core, &vk_rnd);
180
181     vk_cleanup_ctx(&vk_core);
182
183     glfwTerminate();
184 }
185
186 /* glfw callbacks */
187
188 static void
189 clb_key(GLFWwindow *win, int key, int scancode,
190         int action, int mods)
191 {
192     if (action == GLFW_PRESS) {
193         switch(key) {
194         case GLFW_KEY_ESCAPE:
195             glfwSetWindowShouldClose(win, GLFW_TRUE);
196             return;
197         }
198     }
199 }
200
201 static void
202 clb_reshape(GLFWwindow *win, int width, int height)
203 {
204     /* set viewport */
205
206     win_w = width;
207     win_h = height;
208 }