3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
10 /* extern "C": vulkan framework */
15 #define FENCE_TIMEOUT UINT64_MAX
17 /**************************/
18 /* static glfw callbacks */
19 /**************************/
22 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
25 clb_reshape(GLFWwindow *win, int width, int height);
28 clb_motion(GLFWwindow *win, double x, double y);
31 clb_mouse(GLFWwindow *win, int button, int action, int mods);
33 /**************************/
34 /* static functions */
35 /**************************/
37 /* init, cleanup, display */
54 vk_create_sync_objects();
59 /***************************/
60 /* static/global variables */
61 /***************************/
63 static GLFWwindow *win;
64 /* static bool redraw_pending; */
65 static bool move_camera;
68 static float cam_phi = 25;
69 static float cam_theta = 0;
70 static float cam_dist = 16;
74 static OrbitCamera *camera;
81 static int win_w = 800;
82 static int win_h = 600;
85 static bool vk_enable_layers = true;
86 static struct vk_ctx vk_core;
87 static VkSurfaceKHR vk_surf;
89 static struct vk_swapchain vk_chain;
90 static uint32_t vk_current_image;
91 static int vk_frame_idx;
92 static VkFramebuffer *vk_framebuffers;
94 /* for the moment: one cmd buffer per swapchain image */
95 static VkCommandBuffer *vk_cmd_buffers;
96 static VkFence *vk_fences;
97 static struct vk_semaphores *vk_semas;
99 /* FIXME make them part of each object's functions/params */
100 static struct vk_renderer vk_rnd;
101 static struct vk_attachment vk_depth_att;
102 static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 };
104 /* empty for as long as we hardcode the vertices in the vertex shader */
106 static struct vk_vertex_info vk_vert_info;
109 int main(int argc, char** argv)
113 /***********************************************************
115 ***********************************************************/
118 fprintf(stderr, "Failed to initialize GLFW.\n");
122 if (glfwVulkanSupported() != GLFW_TRUE) {
123 fprintf(stderr, "Vulkan is not supported on this device.\n");
127 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
128 glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
129 if (!(win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0))) {
130 fprintf(stderr, "Failed to create GLFW window\n");
134 glfwSetKeyCallback(win, clb_key);
135 glfwSetWindowSizeCallback(win, clb_reshape);
136 glfwSetCursorPosCallback(win, clb_motion);
137 glfwSetMouseButtonCallback(win, clb_mouse);
139 /***********************************************************
141 ***********************************************************/
143 if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
144 fprintf(stderr, "Failed to initialize Vulkan context.\n");
148 /***********************************************************
150 ***********************************************************/
152 glfwGetFramebufferSize(win, &win_h, &win_h);
153 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
155 fprintf(stderr, "Failed to create XCB surface.\n");
159 /***********************************************************
160 * Initialize Vulkan structs
161 ***********************************************************/
167 /***********************************************************
168 * Initialize program objects, classes etc
169 ***********************************************************/
175 /***********************************************************
177 ***********************************************************/
179 while(!glfwWindowShouldClose(win)) {
184 vkDeviceWaitIdle(vk_core.dev);
188 /* static functions */
195 /* create swapchain */
196 if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) {
197 fprintf(stderr, "No swapchain was created.\n");
201 if (vk_chain.swapchain == VK_NULL_HANDLE) {
202 fprintf(stderr, "Invalid swapchain handle.\n");
206 if (!vk_create_sync_objects()) {
207 fprintf(stderr, "Failed to create sync objects.\n");
211 /* FIXME for the moment one cmd buf.
212 * But most probably I need to change this later. */
213 VkCommandBuffer cmd_buf;
214 if (!(cmd_buf = vk_create_cmd_buffer(&vk_core))) {
215 fprintf(stderr, "Failed to create command buffer: %d.\n", 0);
219 vk_cmd_buffers = (VkCommandBuffer*)malloc(vk_chain.num_images * sizeof *vk_cmd_buffers);
220 memset(vk_cmd_buffers, 0, vk_chain.num_images * sizeof *vk_cmd_buffers);
221 vk_cmd_buffers[0] = 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 = (VkFramebuffer*)malloc(vk_chain.num_images * sizeof vk_framebuffers[0]);
267 memset(vk_framebuffers, VK_NULL_HANDLE, vk_chain.num_images * sizeof vk_framebuffers[0]);
269 for (i = 0; i < vk_chain.num_images; i++) {
270 if (!vk_create_framebuffer(&vk_core,
273 &vk_depth_att.obj.img_view,
275 &vk_framebuffers[i]))
279 /* record cmd buffer FIXME:
280 * should move this and make it part of each renderer
281 * also add all other stuff needed like uniforms
282 * descriptor sets spec constants push constants etc
315 vkWaitForFences(vk_core.dev, 1, &vk_fences[vk_frame_idx], VK_TRUE, UINT64_MAX);
316 vkResetFences(vk_core.dev, 1, &vk_fences[vk_frame_idx]);
320 err = vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain,
322 vk_semas[vk_frame_idx].frame_ready,
323 0, &vk_current_image);
325 case VK_ERROR_OUT_OF_DATE_KHR:
326 fprintf(stderr, "acquire next image error: VK_ERROR_OUT_OF_DATE_KHR.\n");
329 case VK_SUBOPTIMAL_KHR:
330 fprintf(stderr, "AcquireNextImageKHR returned VK_SUBOPTIMAL_KHR, ignored.\n");
333 case VK_ERROR_SURFACE_LOST_KHR:
334 vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
335 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf) !=
337 fprintf(stderr, "Failed to recreate GLFW/XCB surface.\n");
345 } while (err != VK_SUCCESS);
347 /* FIXME update buffer data */
348 if (!vk_record_cmd_buffer(&vk_core,
352 vk_framebuffers[vk_current_image],
354 0, 0, win_w, win_h)) {
355 fprintf(stderr, "Failed to record command buffer.\n");
359 /* each object should have a command buffer renderpass etc? */
361 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
363 memset(&sinfo, 0, sizeof sinfo);
364 sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
365 sinfo.waitSemaphoreCount = 1;
366 sinfo.pWaitSemaphores = &vk_semas[vk_frame_idx].frame_ready;
367 sinfo.pWaitDstStageMask = &wait_stages;
368 sinfo.commandBufferCount = 1;
369 sinfo.pCommandBuffers = &vk_cmd_buffers[0];
370 sinfo.signalSemaphoreCount = 1;
371 sinfo.pSignalSemaphores = &vk_semas[vk_frame_idx].frame_done;
373 if (vkQueueSubmit(vk_core.queue, 1, &sinfo, vk_fences[vk_frame_idx]) != 0) {
374 fprintf(stderr, "Failed to submit draw commands.\n");
378 if (vkQueueWaitIdle(vk_core.queue) != VK_SUCCESS) {
379 fprintf(stderr, "Failed to wait idle.\n");
383 if (!vk_queue_present(&vk_chain, vk_core.queue, vk_current_image,
384 vk_semas[vk_frame_idx].frame_done)) {
388 vk_frame_idx = (vk_frame_idx + 1) % (vk_chain.num_images - 1);
390 printf("display %d\n", count++);
391 printf("current image %u\n", vk_current_image);
392 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 (i = 0; i < vk_chain.num_images; i++) {
405 vkDestroyFramebuffer(vk_core.dev, vk_framebuffers[i], 0);
408 glfwDestroyWindow(win);
410 if (vk_chain.swapchain) {
411 vk_destroy_swapchain(&vk_core, &vk_chain);
412 vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
415 if (vk_enable_layers)
418 vk_destroy_cmd_buffers(&vk_core, vk_chain.num_images, vk_cmd_buffers);
420 vk_destroy_fences(&vk_core, vk_chain.num_images, vk_fences);
422 for (int i = 0; i < num_images; i++)
423 vk_destroy_semaphores(&vk_core, &vk_semas[i]);
428 vk_cleanup_ctx(&vk_core);
432 vk_create_sync_objects()
434 VkFenceCreateInfo finfo;
435 memset(&finfo, 0, sizeof finfo);
436 finfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
437 finfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
439 VkSemaphoreCreateInfo sinfo;
440 memset(&sinfo, 0, sizeof sinfo);
441 sinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
443 vk_fences = (VkFence*)malloc((vk_chain.num_images - 1) * sizeof *vk_fences);
444 memset(vk_fences, VK_NULL_HANDLE, (vk_chain.num_images - 1) * sizeof *vk_fences);
446 vk_semas = (struct vk_semaphores *)malloc(sizeof(struct vk_semaphores) * (vk_chain.num_images - 1));
447 memset(vk_semas, VK_NULL_HANDLE, (vk_chain.num_images - 1) * sizeof *vk_fences);
449 for (uint32_t i = 0; i < (vk_chain.num_images - 1); i++) {
450 if (vkCreateFence(vk_core.dev, &finfo, 0, &vk_fences[i]) != VK_SUCCESS) {
451 fprintf(stderr, "Failed to create fence: %d\n", i);
455 if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_ready) != VK_SUCCESS) {
456 fprintf(stderr, "Failed to create frame_ready semaphore: %d\n", i);
460 if (vkCreateSemaphore(vk_core.dev, &sinfo, 0, &vk_semas[i].frame_done) != VK_SUCCESS) {
461 fprintf(stderr, "Failed to create frame_done semaphore: %d\n", i);
472 clb_reshape(GLFWwindow *win,
476 aspect = (float)width / (float)height;
478 mproj = calc_projection_matrix(45, aspect, 0.5, 1000.0f);
481 /* FIXME near and far clipping planes */
482 vk_set_viewport(&vk_core, vk_cmd_buffers[0],
483 0, 0, win_w, win_h, 0.0f, 1.0f);
489 clb_key(GLFWwindow *win, int key, int scancode,
490 int action, int mods)
492 if (action == GLFW_REPEAT) return;
494 if (action == GLFW_PRESS) {
496 case GLFW_KEY_ESCAPE:
497 glfwSetWindowShouldClose(win, GLFW_TRUE);
500 move_camera = !move_camera;
508 static double prev_x, prev_y;
509 static bool button[8];
512 clb_motion(GLFWwindow *win,
516 double dx = x - prev_x;
517 double dy = y - prev_y;
523 cam_theta += dx * 0.5;
533 cam_dist += dy * 0.1;
541 clb_mouse(GLFWwindow *win,