7 int win_width, win_height;
9 static int rpass, pipeln, *fb, num_swap_img;
10 static VkSemaphore sem_getimg, sem_draw;
18 if(vk_init(VKINIT_DEPTH, &flags) == -1) {
22 if(!(queue = vk_getq(VKQ_GFX | VKQ_PRESENT, 0))) {
26 /* force swapchain creation, to find out how many framebuffers to create */
27 vk_reshape(win_width, win_height);
28 if((num_swap_img = vk_num_swap_images()) <= 0) {
31 if(!(fb = malloc(num_swap_img * sizeof *fb))) {
35 rpass = vk_create_rpass();
37 for(i=0; i<num_swap_img; i++) {
38 fb[i] = vk_create_fb();
39 vk_fb_size(fb[i], win_width, win_height);
40 vk_fb_rpass(fb[i], rpass);
41 vk_fb_images(fb[i], 1, vk_swap_image(i));
44 pipeln = vk_create_pipeln();
45 vk_pipeln_rpass(pipeln, vk_rpass(rpass));
46 vk_pipeln_viewport(pipeln, 0, 0, win_width, win_height);
48 sem_getimg = vk_create_sem();
49 sem_draw = vk_create_sem();
53 void app_cleanup(void)
55 vk_free_sem(sem_getimg);
56 vk_free_sem(sem_draw);
61 void app_display(void)
64 VkCommandBuffer cmdbuf;
66 /* get the next image from the swap chain */
67 imgid = vk_next_swap_image(sem_getimg);
68 cmdbuf = vk_get_cmdbuf(imgid);
71 VkCommandBufferBeginInfo cmdbegin = {0};
72 VkRenderPassBeginInfo rpbegin = {0};
73 VkClearValue clear[2];
75 cmdbegin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
76 cmdbegin.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
78 if(vkBeginCommandBuffer(cmdbuf, &cmdbegin) != 0) {
79 fprintf(stderr, "failed to begin command buffer recording\n");
83 clear[0].color.float32[0] = 0.5f;
84 clear[0].color.float32[1] = 0.1f;
85 clear[0].color.float32[2] = 0.2f;
86 clear[0].color.float32[3] = 1.0f;
87 clear[1].depthStencil.depth = 1.0f;
88 clear[1].depthStencil.stencil = 0;
90 rpbegin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
91 rpbegin.renderPass = vk_rpass(rpass);
92 rpbegin.framebuffer = vk_fb(fb[imgid]);
93 vk_rect(&rpbegin.renderArea, 0, 0, win_width, win_height);
94 rpbegin.pClearValues = clear;
95 rpbegin.clearValueCount = 1;
97 vkCmdBeginRenderPass(cmdbuf, &rpbegin, VK_SUBPASS_CONTENTS_INLINE);
101 vkCmdEndRenderPass(cmdbuf);
102 vkEndCommandBuffer(cmdbuf);
105 /* submit the command buffer, wait for one semaphore, signal another */
106 vk_submit(queue, cmdbuf, sem_getimg, sem_draw);
108 /* swap buffers after drawing is finished */
109 vk_present(queue, imgid, sem_draw);
114 void app_reshape(int x, int y)
120 if(vk_reshape(x, y) == -1) {
124 for(i=0; i<vk_num_swap_images(); i++) {
125 vk_fb_size(fb[i], x, y);
126 vk_fb_images(fb[i], 1, vk_swap_image(i));
130 void app_keyboard(int key, int press)
141 void app_mouse(int bn, int press, int x, int y)
145 void app_motion(int x, int y)