3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
12 #include <gmath/gmath.h>
14 /* extern "C": vulkan framework */
22 #define FENCE_TIMEOUT UINT64_MAX
24 /**************************/
25 /* static glfw callbacks */
26 /**************************/
29 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
32 clb_reshape(GLFWwindow *win, int width, int height);
35 clb_motion(GLFWwindow *win, double x, double y);
38 clb_mouse(GLFWwindow *win, int button, int action, int mods);
40 /**************************/
41 /* static functions */
42 /**************************/
44 /* init, cleanup, display */
61 vk_create_sync_objects();
66 /***************************/
67 /* static/global variables */
68 /***************************/
70 static GLFWwindow *win;
71 /* static bool redraw_pending; */
72 static bool move_camera;
75 static float cam_phi = 25;
76 static float cam_theta = 0;
77 static float cam_dist = 16;
80 static OrbitCamera *camera;
86 static int win_w = 800;
87 static int win_h = 600;
90 static bool vk_enable_layers = true;
91 static struct vk_ctx vk_core;
92 static VkSurfaceKHR vk_surf;
94 static struct vk_swapchain vk_chain;
95 static uint32_t vk_current_image;
96 static int vk_frame_idx;
97 static std::vector<VkFramebuffer>vk_framebuffers;
99 /* for the moment: one cmd buffer per swapchain image */
100 static std::vector<VkCommandBuffer>vk_cmd_buffers;
101 static std::vector<VkFence>vk_fences;
102 static struct vk_semaphores *vk_semas;
104 /* FIXME make them part of each object's functions/params */
105 static struct vk_renderer vk_rnd;
106 static struct vk_attachment vk_depth_att;
107 static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 };
109 /* empty for as long as we hardcode the vertices in the vertex shader */
111 static struct vk_vertex_info vk_vert_info;
114 int main(int argc, char** argv)
118 /***********************************************************
120 ***********************************************************/
123 fprintf(stderr, "Failed to initialize GLFW.\n");
127 if (glfwVulkanSupported() != GLFW_TRUE) {
128 fprintf(stderr, "Vulkan is not supported on this device.\n");
132 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
133 glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
134 if (!(win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0))) {
135 fprintf(stderr, "Failed to create GLFW window\n");
139 glfwSetKeyCallback(win, clb_key);
140 glfwSetWindowSizeCallback(win, clb_reshape);
141 glfwSetCursorPosCallback(win, clb_motion);
142 glfwSetMouseButtonCallback(win, clb_mouse);
144 /***********************************************************
146 ***********************************************************/
148 if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
149 fprintf(stderr, "Failed to initialize Vulkan context.\n");
153 /***********************************************************
155 ***********************************************************/
157 glfwGetFramebufferSize(win, &win_h, &win_h);
158 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
160 fprintf(stderr, "Failed to create XCB surface.\n");
164 /***********************************************************
165 * Initialize Vulkan structs
166 ***********************************************************/
172 /***********************************************************
173 * Initialize program objects, classes etc
174 ***********************************************************/
180 /***********************************************************
182 ***********************************************************/
184 while(!glfwWindowShouldClose(win)) {
189 vkDeviceWaitIdle(vk_core.dev);
193 /* static functions */
198 /* create swapchain */
199 if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) {
200 fprintf(stderr, "No swapchain was created.\n");
204 if (vk_chain.swapchain == VK_NULL_HANDLE) {
205 fprintf(stderr, "Invalid swapchain handle.\n");
209 if (!vk_create_sync_objects()) {
210 fprintf(stderr, "Failed to create sync objects.\n");
214 /* FIXME for the moment one cmd buf.
215 * But most probably I need to change this later. */
216 VkCommandBuffer cmd_buf;
217 if (!(cmd_buf = vk_create_cmd_buffer(&vk_core))) {
218 fprintf(stderr, "Failed to create command buffer: %d.\n", 0);
221 vk_cmd_buffers.push_back(cmd_buf);
223 /* FIXME: this part is going to be part of each object's
224 * renderpass and pipeline */
226 /* create depth attachment (for the moment we are going to use this
228 if (!vk_fill_image_props(&vk_core,
231 VK_FORMAT_D32_SFLOAT_S8_UINT,
232 VK_IMAGE_TILING_OPTIMAL,
233 VK_IMAGE_LAYOUT_UNDEFINED,
234 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
236 &vk_depth_att.props)) {
237 fprintf(stderr, "Unsupported depth image properties\n");
240 if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
241 fprintf(stderr, "Failed to create depth attachment.\n");
244 if(!vk_create_image_view(&vk_core, vk_depth_att.obj.img, VK_IMAGE_VIEW_TYPE_2D,
245 vk_depth_att.props.format, false,
246 &vk_depth_att.obj.img_view)) {
247 fprintf(stderr, "Failed to create image view for depth attachment.\n");
253 char *vsdr = sdr_load("data/main.vert.spv", &vsz);
254 char *fsdr = sdr_load("data/main.frag.spv", &fsz);
256 /* create renderer */
257 if (!vk_create_renderer(&vk_core,
258 vsdr, vsz, fsdr, fsz,
261 1, &vk_chain.img_props, &vk_depth_att.props,
266 vk_framebuffers.resize(vk_chain.num_images, VK_NULL_HANDLE);
267 for (uint32_t i = 0; i < vk_chain.num_images; i++) {
268 if (!vk_create_framebuffer(&vk_core,
271 &vk_depth_att.obj.img_view,
273 &vk_framebuffers[i]))
277 /* record cmd buffer FIXME:
278 * should move this and make it part of each renderer
279 * also add all other stuff needed like uniforms
280 * descriptor sets spec constants push constants etc
299 camera = new OrbitCamera;
316 vkWaitForFences(vk_core.dev, 1, &vk_fences[vk_frame_idx], VK_TRUE, UINT64_MAX);
317 vkResetFences(vk_core.dev, 1, &vk_fences[vk_frame_idx]);
321 err = vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain,
323 vk_semas[vk_frame_idx].frame_ready,
324 0, &vk_current_image);
326 case VK_ERROR_OUT_OF_DATE_KHR:
327 fprintf(stderr, "acquire next image error: VK_ERROR_OUT_OF_DATE_KHR.\n");
330 case VK_SUBOPTIMAL_KHR:
331 fprintf(stderr, "AcquireNextImageKHR returned VK_SUBOPTIMAL_KHR, ignored.\n");
334 case VK_ERROR_SURFACE_LOST_KHR:
335 vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
336 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf) !=
338 fprintf(stderr, "Failed to recreate GLFW/XCB surface.\n");
346 } while (err != VK_SUCCESS);
348 /* FIXME update buffer data */
349 if (!vk_record_cmd_buffer(&vk_core,
353 vk_framebuffers[vk_current_image],
355 0, 0, win_w, win_h)) {
356 fprintf(stderr, "Failed to record command buffer.\n");
360 /* each object should have a command buffer renderpass etc? */
362 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
364 memset(&sinfo, 0, sizeof sinfo);
365 sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
366 sinfo.waitSemaphoreCount = 1;
367 sinfo.pWaitSemaphores = &vk_semas[vk_frame_idx].frame_ready;
368 sinfo.pWaitDstStageMask = &wait_stages;
369 sinfo.commandBufferCount = 1;
370 sinfo.pCommandBuffers = &vk_cmd_buffers[0];
371 sinfo.signalSemaphoreCount = 1;
372 sinfo.pSignalSemaphores = &vk_semas[vk_frame_idx].frame_done;
374 if (vkQueueSubmit(vk_core.queue, 1, &sinfo, vk_fences[vk_frame_idx]) != 0) {
375 fprintf(stderr, "Failed to submit draw commands.\n");
379 if (vkQueueWaitIdle(vk_core.queue) != VK_SUCCESS) {
380 fprintf(stderr, "Failed to wait idle.\n");
384 if (!vk_queue_present(&vk_chain, vk_core.queue, vk_current_image,
385 vk_semas[vk_frame_idx].frame_done)) {
389 vk_frame_idx = (vk_frame_idx + 1) % (vk_chain.num_images - 1);
391 printf("display %d\n", count++);
392 printf("current image %u\n", vk_current_image);
393 printf("frame idx %d\n", vk_frame_idx);
399 int num_images = vk_chain.num_images - 1;
401 vk_destroy_image(&vk_core, &vk_depth_att.obj);
402 vk_destroy_renderer(&vk_core, &vk_rnd);
404 for (size_t i = 0; i < vk_framebuffers.size(); i++) {
405 vkDestroyFramebuffer(vk_core.dev, vk_framebuffers[i], 0);
407 vk_framebuffers.clear();
409 glfwDestroyWindow(win);
411 if (vk_chain.swapchain) {
412 vk_destroy_swapchain(&vk_core, &vk_chain);
413 vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
416 if (vk_enable_layers)
419 vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
420 vk_cmd_buffers.clear();
422 vk_destroy_fences(&vk_core, vk_fences.size(), vk_fences.data());
425 for (int i = 0; i < num_images; i++)
426 vk_destroy_semaphores(&vk_core, &vk_semas[i]);
429 for (size_t i = 0; i < vk_cmd_buffers.size(); i++)
430 vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
431 vk_cmd_buffers.clear();
435 vk_cleanup_ctx(&vk_core);
439 vk_create_sync_objects()
441 VkFenceCreateInfo finfo;
442 memset(&finfo, 0, sizeof finfo);
443 finfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
444 finfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
446 VkSemaphoreCreateInfo sinfo;
447 memset(&sinfo, 0, sizeof sinfo);
448 sinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
450 vk_fences.resize(vk_chain.num_images - 1, VK_NULL_HANDLE);
452 vk_semas = (vk_semaphores *)malloc(sizeof(vk_semaphores) * (vk_chain.num_images - 1));
453 for (uint32_t i = 0; i < (vk_chain.num_images - 1); i++) {
454 if (vkCreateFence(vk_core.dev, &finfo, 0, &vk_fences[i]) != VK_SUCCESS) {
455 fprintf(stderr, "Failed to create fence: %d\n", i);
459 if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_ready) != VK_SUCCESS) {
460 fprintf(stderr, "Failed to create frame_ready semaphore: %d\n", i);
464 if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_done) != VK_SUCCESS) {
465 fprintf(stderr, "Failed to create frame_done semaphore: %d\n", i);
476 clb_reshape(GLFWwindow *win,
480 aspect = (float)width / (float)height;
481 mproj = calc_projection_matrix(45, aspect, 0.5, 1000.0f);
483 /* FIXME near and far clipping planes */
484 vk_set_viewport(&vk_core, vk_cmd_buffers[0],
485 0, 0, win_w, win_h, 0.0f, 1.0f);
491 clb_key(GLFWwindow *win, int key, int scancode,
492 int action, int mods)
494 if (action == GLFW_REPEAT) return;
496 if (action == GLFW_PRESS) {
498 case GLFW_KEY_ESCAPE:
499 glfwSetWindowShouldClose(win, GLFW_TRUE);
502 move_camera = !move_camera;
510 static double prev_x, prev_y;
511 static bool button[8];
514 clb_motion(GLFWwindow *win,
518 double dx = x - prev_x;
519 double dy = y - prev_y;
525 cam_theta += dx * 0.5;
535 cam_dist += dy * 0.1;
543 clb_mouse(GLFWwindow *win,