3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
12 #include <gmath/gmath.h>
14 /* extern "C": vulkan framework */
23 #define FENCE_TIMEOUT UINT64_MAX
25 /**************************/
26 /* static glfw callbacks */
27 /**************************/
30 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
33 clb_reshape(GLFWwindow *win, int width, int height);
36 clb_motion(GLFWwindow *win, double x, double y);
39 clb_mouse(GLFWwindow *win, int button, int action, int mods);
41 /**************************/
42 /* static functions */
43 /**************************/
45 /* init, cleanup, display */
62 vk_create_sync_objects();
67 /***************************/
68 /* static/global variables */
69 /***************************/
71 static GLFWwindow *win;
72 /* static bool redraw_pending; */
73 static bool move_camera;
76 static float cam_phi = 25;
77 static float cam_theta = 0;
78 static float cam_dist = 16;
81 static OrbitCamera *camera;
87 static int win_w = 800;
88 static int win_h = 600;
91 static bool vk_enable_layers = true;
92 static struct vk_ctx vk_core;
93 static VkSurfaceKHR vk_surf;
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;
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];
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 };
110 /* empty for as long as we hardcode the vertices in the vertex shader */
112 static struct vk_vertex_info vk_vert_info;
115 int main(int argc, char** argv)
119 /***********************************************************
121 ***********************************************************/
124 fprintf(stderr, "Failed to initialize GLFW.\n");
128 if (glfwVulkanSupported() != GLFW_TRUE) {
129 fprintf(stderr, "Vulkan is not supported on this device.\n");
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");
140 glfwSetKeyCallback(win, clb_key);
141 glfwSetWindowSizeCallback(win, clb_reshape);
142 glfwSetCursorPosCallback(win, clb_motion);
143 glfwSetMouseButtonCallback(win, clb_mouse);
145 /***********************************************************
147 ***********************************************************/
149 if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) {
150 fprintf(stderr, "Failed to initialize Vulkan context.\n");
154 /***********************************************************
156 ***********************************************************/
158 glfwGetFramebufferSize(win, &win_h, &win_h);
159 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
161 fprintf(stderr, "Failed to create XCB surface.\n");
165 /***********************************************************
166 * Initialize Vulkan structs
167 ***********************************************************/
173 /***********************************************************
174 * Initialize program objects, classes etc
175 ***********************************************************/
181 /***********************************************************
183 ***********************************************************/
185 while(!glfwWindowShouldClose(win)) {
190 vkDeviceWaitIdle(vk_core.dev);
194 /* static functions */
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");
205 if (vk_chain.swapchain == VK_NULL_HANDLE) {
206 fprintf(stderr, "Invalid swapchain handle.\n");
210 if (!vk_create_sync_objects()) {
211 fprintf(stderr, "Failed to create sync objects.\n");
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);
222 vk_cmd_buffers.push_back(cmd_buf);
224 /* FIXME: this part is going to be part of each object's
225 * renderpass and pipeline */
227 /* create depth attachment (for the moment we are going to use this
229 if (!vk_fill_image_props(&vk_core,
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,
237 &vk_depth_att.props)) {
238 fprintf(stderr, "Unsupported depth image properties\n");
241 if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
242 fprintf(stderr, "Failed to create depth attachment.\n");
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");
254 char *vsdr = sdr_load("data/main.vert.spv", &vsz);
255 char *fsdr = sdr_load("data/main.frag.spv", &fsz);
257 /* create renderer */
258 if (!vk_create_renderer(&vk_core,
259 vsdr, vsz, fsdr, fsz,
262 1, &vk_chain.img_props, &vk_depth_att.props,
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,
272 &vk_depth_att.obj.img_view,
274 &vk_framebuffers[i]))
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
300 camera = new OrbitCamera;
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]);
322 err = vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain,
324 vk_semas[vk_frame_idx].frame_ready,
325 0, &vk_current_image);
327 case VK_ERROR_OUT_OF_DATE_KHR:
328 fprintf(stderr, "acquire next image error: VK_ERROR_OUT_OF_DATE_KHR.\n");
331 case VK_SUBOPTIMAL_KHR:
332 fprintf(stderr, "AcquireNextImageKHR returned VK_SUBOPTIMAL_KHR, ignored.\n");
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) !=
339 fprintf(stderr, "Failed to recreate GLFW/XCB surface.\n");
347 } while (err != VK_SUCCESS);
349 /* FIXME update buffer data */
350 if (!vk_record_cmd_buffer(&vk_core, vk_cmd_buffers[0],
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) % FRAME_LAG;
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 vk_destroy_image(&vk_core, &vk_depth_att.obj);
400 vk_destroy_renderer(&vk_core, &vk_rnd);
402 for (size_t i = 0; i < vk_framebuffers.size(); i++) {
403 vkDestroyFramebuffer(vk_core.dev, vk_framebuffers[i], 0);
405 vk_framebuffers.clear();
407 glfwDestroyWindow(win);
409 if (vk_chain.swapchain) {
410 vk_destroy_swapchain(&vk_core, &vk_chain);
411 vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0);
414 if (vk_enable_layers)
417 vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
418 vk_cmd_buffers.clear();
420 vk_destroy_fences(&vk_core, vk_fences.size(), vk_fences.data());
423 for (int i = 0; i < FRAME_LAG; i++)
424 vk_destroy_semaphores(&vk_core, &vk_semas[i]);
426 for (size_t i = 0; i < vk_cmd_buffers.size(); i++)
427 vk_destroy_cmd_buffers(&vk_core, vk_cmd_buffers.size(), vk_cmd_buffers.data());
428 vk_cmd_buffers.clear();
432 vk_cleanup_ctx(&vk_core);
436 vk_create_sync_objects()
438 VkFenceCreateInfo finfo;
439 memset(&finfo, 0, sizeof finfo);
440 finfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
441 finfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
443 VkSemaphoreCreateInfo sinfo;
444 memset(&sinfo, 0, sizeof sinfo);
445 sinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
447 vk_fences.resize(FRAME_LAG, VK_NULL_HANDLE);
449 for (int i = 0; i < FRAME_LAG; 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;
477 mproj = calc_projection_matrix(45, aspect, 0.5, 1000.0f);
479 /* FIXME near and far clipping planes */
480 vk_set_viewport(&vk_core, vk_cmd_buffers[0],
481 0, 0, win_w, win_h, 0.0f, 1.0f);
487 clb_key(GLFWwindow *win, int key, int scancode,
488 int action, int mods)
490 if (action == GLFW_REPEAT) return;
492 if (action == GLFW_PRESS) {
494 case GLFW_KEY_ESCAPE:
495 glfwSetWindowShouldClose(win, GLFW_TRUE);
498 move_camera = !move_camera;
506 static double prev_x, prev_y;
507 static bool button[8];
510 clb_motion(GLFWwindow *win,
514 double dx = x - prev_x;
515 double dy = y - prev_y;
521 cam_theta += dx * 0.5;
531 cam_dist += dy * 0.1;
539 clb_mouse(GLFWwindow *win,