bcbc2aca0ad0e0dbfe8a0460df14688386d82c89
[vkrt] / src / main.cc
1 #include <stdio.h>
2
3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
5
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <vector>
11
12 #include <gmath/gmath.h>
13
14 /* extern "C": vulkan framework */
15 #include "vk.h"
16 #include "util.h"
17
18 /* C++ */
19 #include "camera.h"
20
21 /* defines */
22 #define FRAME_LAG 2
23 #define FENCE_TIMEOUT UINT64_MAX
24
25 /**************************/
26 /* static glfw callbacks */
27 /**************************/
28
29 static void
30 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
31
32 static void
33 clb_reshape(GLFWwindow *win, int width, int height);
34
35 static void
36 clb_motion(GLFWwindow *win, double x, double y);
37
38 static void
39 clb_mouse(GLFWwindow *win, int button, int action, int mods);
40
41 /**************************/
42 /* static functions */
43 /**************************/
44
45 /* init, cleanup, display */
46
47 static bool
48 init();
49
50 static void
51 cleanup();
52
53 static void
54 display();
55
56 /* vulkan, glfw */
57
58 static bool
59 vk_init();
60
61 static bool
62 vk_create_sync_objects();
63
64 static void
65 vk_cleanup();
66
67 /***************************/
68 /* static/global variables */
69 /***************************/
70
71 static GLFWwindow *win;
72 /* static bool redraw_pending; */
73 static bool move_camera;
74
75 /* camera */
76 static float cam_phi = 25;
77 static float cam_theta = 0;
78 static float cam_dist = 16;
79 static Vec3 cam_pos;
80
81 static OrbitCamera *camera;
82
83 static float aspect;
84 static Mat4 mproj;
85
86 /* win etc */
87 static int win_w = 800;
88 static int win_h = 600;
89
90 /* vulkan */
91 static bool vk_enable_layers = true;
92 static struct vk_ctx vk_core;
93 static VkSurfaceKHR vk_surf;
94
95 static struct vk_swapchain vk_chain;
96 static uint32_t vk_current_image;
97 static int vk_frame_idx;
98 static std::vector<VkFramebuffer>vk_framebuffers;
99
100 /* for the moment: one cmd buffer per swapchain image */
101 static std::vector<VkCommandBuffer>vk_cmd_buffers;
102 static std::vector<VkFence>vk_fences;
103 static struct vk_semaphores vk_semas[FRAME_LAG];
104
105 /* FIXME make them part of each object's functions/params */
106 static struct vk_renderer vk_rnd;
107 static struct vk_attachment vk_depth_att;
108 static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 };
109
110 /* empty for as long as we hardcode the vertices in the vertex shader */
111 #if 0
112 static struct vk_vertex_info vk_vert_info;
113 #endif
114
115 int main(int argc, char** argv)
116 {
117     atexit(cleanup);
118
119     /***********************************************************
120      * GLFW
121      ***********************************************************/
122
123     if (!glfwInit()) {
124         fprintf(stderr, "Failed to initialize GLFW.\n");
125         return 1;
126     }
127
128     if (glfwVulkanSupported() != GLFW_TRUE) {
129         fprintf(stderr, "Vulkan is not supported on this device.\n");
130         return false;
131     }
132
133     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
134     glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
135     if (!(win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0))) {
136         fprintf(stderr, "Failed to create GLFW window\n");
137         return false;
138     }
139
140     glfwSetKeyCallback(win, clb_key);
141     glfwSetWindowSizeCallback(win, clb_reshape);
142     glfwSetCursorPosCallback(win, clb_motion);
143     glfwSetMouseButtonCallback(win, clb_mouse);
144
145     /***********************************************************
146      * VULKAN context
147      ***********************************************************/
148
149     if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
150         fprintf(stderr, "Failed to initialize Vulkan context.\n");
151         return false;
152     }
153
154     /***********************************************************
155      * XCB/GLFW surface
156      ***********************************************************/
157
158     glfwGetFramebufferSize(win, &win_h, &win_h);
159     if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
160             != VK_SUCCESS) {
161         fprintf(stderr, "Failed to create XCB surface.\n");
162         return false;
163     }
164
165     /***********************************************************
166      * Initialize Vulkan structs
167      ***********************************************************/
168
169     if (!vk_init()) {
170         return 1;
171     }
172
173     /***********************************************************
174      * Initialize program objects, classes etc
175      ***********************************************************/
176
177     if (!init()) {
178         return 1;
179     }
180
181     /***********************************************************
182      * Rendering loop
183      ***********************************************************/
184
185         while(!glfwWindowShouldClose(win)) {
186                 glfwPollEvents();
187                 display();
188         }
189
190     vkDeviceWaitIdle(vk_core.dev);
191     return 0;
192 }
193
194 /* static functions */
195
196 static bool
197 vk_init()
198 {
199     /* create swapchain */
200     if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) {
201         fprintf(stderr, "No swapchain was created.\n");
202         return false;
203     }
204
205     if (vk_chain.swapchain == VK_NULL_HANDLE) {
206         fprintf(stderr, "Invalid swapchain handle.\n");
207         return false;
208     }
209
210     if (!vk_create_sync_objects()) {
211         fprintf(stderr, "Failed to create sync objects.\n");
212         return false;
213     }
214
215     /* FIXME for the moment one cmd buf.
216      * But most probably I need to change this later. */
217     VkCommandBuffer cmd_buf;
218     if (!(cmd_buf = vk_create_cmd_buffer(&vk_core))) {
219         fprintf(stderr, "Failed to create command buffer: %d.\n", 0);
220         return false;
221     }
222     vk_cmd_buffers.push_back(cmd_buf);
223
224     /* FIXME: this part is going to be part of each object's
225      * renderpass and pipeline */
226
227     /* create depth attachment (for the moment we are going to use this
228      * for all images */
229     if (!vk_fill_image_props(&vk_core,
230                              win_w, win_h, 1,
231                              1, 1, 1,
232                              VK_FORMAT_D32_SFLOAT_S8_UINT,
233                              VK_IMAGE_TILING_OPTIMAL,
234                              VK_IMAGE_LAYOUT_UNDEFINED,
235                              VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
236                              false, true, false,
237                              &vk_depth_att.props)) {
238         fprintf(stderr, "Unsupported depth image properties\n");
239         return false;
240     }
241     if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
242         fprintf(stderr, "Failed to create depth attachment.\n");
243         return false;
244     }
245     if(!vk_create_image_view(&vk_core, vk_depth_att.obj.img, VK_IMAGE_VIEW_TYPE_2D,
246                              vk_depth_att.props.format, false,
247                              &vk_depth_att.obj.img_view)) {
248         fprintf(stderr, "Failed to create image view for depth attachment.\n");
249         return false;
250     }
251
252     /* load shaders */
253     int vsz, fsz;
254     char *vsdr = sdr_load("data/main.vert.spv", &vsz);
255     char *fsdr = sdr_load("data/main.frag.spv", &fsz);
256
257     /* create renderer */
258     if (!vk_create_renderer(&vk_core,
259                             vsdr, vsz, fsdr, fsz,
260                             win_w, win_h, 1,
261                             true, false,
262                             1, &vk_chain.img_props, &vk_depth_att.props,
263                             0, &vk_rnd)) {
264         goto fail;
265     }
266
267     vk_framebuffers.resize(vk_chain.num_images, VK_NULL_HANDLE);
268     for (uint32_t i = 0; i < vk_chain.num_images; i++) {
269         if (!vk_create_framebuffer(&vk_core,
270                                    win_w, win_h, 1,
271                                    &vk_chain.views[i],
272                                    &vk_depth_att.obj.img_view,
273                                    vk_rnd.renderpass,
274                                    &vk_framebuffers[i]))
275         goto fail;
276     }
277
278     /* record cmd buffer FIXME:
279      * should move this and make it part of each renderer
280      * also add all other stuff needed like uniforms
281      * descriptor sets spec constants push constants etc
282      * renderer
283      */
284
285     free(vsdr);
286     free(fsdr);
287
288     return true;
289
290 fail:
291     free(vsdr);
292     free(fsdr);
293
294     return false;
295 }
296
297 static bool
298 init()
299 {
300     camera = new OrbitCamera;
301     return true;
302 }
303
304 static void
305 cleanup()
306 {
307     delete camera;
308
309     vk_cleanup();
310 }
311
312 /* FIXME */
313 static int count;
314 static void
315 display()
316 {
317     vkWaitForFences(vk_core.dev, 1, &vk_fences[vk_frame_idx], VK_TRUE, UINT64_MAX);
318     vkResetFences(vk_core.dev, 1, &vk_fences[vk_frame_idx]);
319
320     VkResult err;
321     do {
322         err = vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain,
323                                     UINT64_MAX,
324                                     vk_semas[vk_frame_idx].frame_ready,
325                                     0, &vk_current_image);
326         switch(err) {
327         case VK_ERROR_OUT_OF_DATE_KHR:
328             fprintf(stderr, "acquire next image error: VK_ERROR_OUT_OF_DATE_KHR.\n");
329             abort();
330             break;
331         case VK_SUBOPTIMAL_KHR:
332             fprintf(stderr, "AcquireNextImageKHR returned VK_SUBOPTIMAL_KHR, ignored.\n");
333             abort();
334             break;
335         case VK_ERROR_SURFACE_LOST_KHR:
336             vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
337             if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf) !=
338                     VK_SUCCESS) {
339                 fprintf(stderr, "Failed to recreate GLFW/XCB surface.\n");
340             }
341             abort();
342             break;
343         default:
344             assert(!err);
345             break;
346         }
347     } while (err != VK_SUCCESS);
348
349     /* FIXME update buffer data */
350     if (!vk_record_cmd_buffer(&vk_core,
351                 vk_cmd_buffers[0],
352                 &vk_rnd,
353                 4, vk_fb_color,
354                 vk_framebuffers[vk_current_image],
355                 2, 0,
356                 0, 0, win_w, win_h)) {
357         fprintf(stderr, "Failed to record command buffer.\n");
358         abort();
359     }
360
361     /* each object should have a command buffer renderpass etc? */
362     VkSubmitInfo sinfo;
363     VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
364
365     memset(&sinfo, 0, sizeof sinfo);
366     sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
367     sinfo.waitSemaphoreCount = 1;
368     sinfo.pWaitSemaphores = &vk_semas[vk_frame_idx].frame_ready;
369     sinfo.pWaitDstStageMask = &wait_stages;
370     sinfo.commandBufferCount = 1;
371     sinfo.pCommandBuffers = &vk_cmd_buffers[0];
372     sinfo.signalSemaphoreCount = 1;
373         sinfo.pSignalSemaphores = &vk_semas[vk_frame_idx].frame_done;
374
375     if (vkQueueSubmit(vk_core.queue, 1, &sinfo, vk_fences[vk_frame_idx]) != 0) {
376         fprintf(stderr, "Failed to submit draw commands.\n");
377         abort();
378     }
379
380     if (vkQueueWaitIdle(vk_core.queue) != VK_SUCCESS) {
381         fprintf(stderr, "Failed to wait idle.\n");
382         abort();
383     }
384
385     if (!vk_queue_present(&vk_chain, vk_core.queue, vk_current_image,
386                           vk_semas[vk_frame_idx].frame_done)) {
387         abort();
388     }
389
390     vk_frame_idx = (vk_frame_idx + 1) % FRAME_LAG;
391
392     printf("display %d\n", count++);
393     printf("current image %u\n", vk_current_image);
394     printf("frame idx %d\n", vk_frame_idx);
395 }
396
397 static void
398 vk_cleanup()
399 {
400     vk_destroy_image(&vk_core, &vk_depth_att.obj);
401     vk_destroy_renderer(&vk_core, &vk_rnd);
402
403     for (size_t i = 0; i < vk_framebuffers.size(); i++) {
404         vkDestroyFramebuffer(vk_core.dev, vk_framebuffers[i], 0);
405     }
406     vk_framebuffers.clear();
407
408     glfwDestroyWindow(win);
409
410     if (vk_chain.swapchain) {
411         vk_destroy_swapchain(&vk_core, &vk_chain);
412         vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
413     }
414
415     if (vk_enable_layers)
416         return;
417
418     vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
419     vk_cmd_buffers.clear();
420
421     vk_destroy_fences(&vk_core, vk_fences.size(), vk_fences.data());
422     vk_fences.clear();
423
424     for (int i = 0; i < FRAME_LAG; i++)
425         vk_destroy_semaphores(&vk_core, &vk_semas[i]);
426
427     for (size_t i = 0; i < vk_cmd_buffers.size(); i++)
428         vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
429     vk_cmd_buffers.clear();
430
431     glfwTerminate();
432
433     vk_cleanup_ctx(&vk_core);
434 }
435
436 static bool
437 vk_create_sync_objects()
438 {
439     VkFenceCreateInfo finfo;
440     memset(&finfo, 0, sizeof finfo);
441     finfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
442     finfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
443
444     VkSemaphoreCreateInfo sinfo;
445     memset(&sinfo, 0, sizeof sinfo);
446     sinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
447
448     vk_fences.resize(FRAME_LAG, VK_NULL_HANDLE);
449
450     for (int i = 0; i < FRAME_LAG; i++) {
451         if (vkCreateFence(vk_core.dev, &finfo, 0, &vk_fences[i]) != VK_SUCCESS) {
452             fprintf(stderr, "Failed to create fence: %d\n", i);
453             return false;
454         }
455
456         if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_ready) != VK_SUCCESS) {
457             fprintf(stderr, "Failed to create frame_ready semaphore: %d\n", i);
458             return false;
459         }
460
461         if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_done) != VK_SUCCESS) {
462             fprintf(stderr, "Failed to create frame_done semaphore: %d\n", i);
463             return false;
464         }
465     }
466
467     return true;
468 }
469
470 /* glfw callbacks */
471
472 static void
473 clb_reshape(GLFWwindow *win,
474             int width,
475             int height)
476 {
477     aspect = (float)width / (float)height;
478     mproj = calc_projection_matrix(45, aspect, 0.5, 1000.0f);
479
480     /* FIXME near and far clipping planes */
481     vk_set_viewport(&vk_core, vk_cmd_buffers[0],
482                     0, 0, win_w, win_h, 0.0f, 1.0f);
483     win_w = width;
484     win_h = height;
485 }
486
487 static void
488 clb_key(GLFWwindow *win, int key, int scancode,
489         int action, int mods)
490 {
491     if (action == GLFW_REPEAT) return;
492
493     if (action == GLFW_PRESS) {
494         switch(key) {
495         case GLFW_KEY_ESCAPE:
496             glfwSetWindowShouldClose(win, GLFW_TRUE);
497             exit(0);
498         case ' ':
499             move_camera = !move_camera;
500             break;
501         default:
502             break;
503         }
504     }
505 }
506
507 static double prev_x, prev_y;
508 static bool button[8];
509
510 static void
511 clb_motion(GLFWwindow *win,
512            double x,
513            double y)
514 {
515         double dx = x - prev_x;
516         double dy = y - prev_y;
517
518         prev_x = x;
519         prev_y = y;
520
521         if(button[0]) {
522                 cam_theta += dx * 0.5;
523                 cam_phi += dy * 0.5;
524
525                 if(cam_phi < 0)
526                         cam_phi = 0;
527                 if(cam_phi > 90)
528                         cam_phi = 90;
529         }
530
531         if(button[1]) {
532                 cam_dist += dy * 0.1;
533                 if(cam_dist < 0.0) {
534                         cam_dist = 0.0;
535                 }
536         }
537 }
538
539 static void
540 clb_mouse(GLFWwindow *win,
541           int button,
542           int action,
543           int mods)
544 {
545 }