fixed some validation bugs
[vktest3] / src / app.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include "app.h"
5 #include "vk.h"
6
7 int win_width, win_height;
8
9 static int rpass, pipeln, *fb, num_swap_img;
10 static VkSemaphore sem_getimg, sem_draw;
11 static VkQueue queue;
12
13 int app_init(void)
14 {
15         int i;
16         unsigned int flags;
17
18         if(vk_init(VKINIT_DEPTH, &flags) == -1) {
19                 return -1;
20         }
21
22         if(!(queue = vk_getq(VKQ_GFX | VKQ_PRESENT, 0))) {
23                 return -1;
24         }
25
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) {
29                 return -1;
30         }
31         if(!(fb = malloc(num_swap_img * sizeof *fb))) {
32                 return -1;
33         }
34
35         rpass = vk_create_rpass();
36
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));
42         }
43
44         pipeln = vk_create_pipeln();
45         vk_pipeln_rpass(pipeln, vk_rpass(rpass));
46         vk_pipeln_viewport(pipeln, 0, 0, win_width, win_height);
47
48         sem_getimg = vk_create_sem();
49         sem_draw = vk_create_sem();
50         return 0;
51 }
52
53 void app_cleanup(void)
54 {
55         vk_free_sem(sem_getimg);
56         vk_free_sem(sem_draw);
57         vk_cleanup();
58 }
59
60
61 void app_display(void)
62 {
63         int imgid;
64         VkCommandBuffer cmdbuf;
65
66         /* get the next image from the swap chain */
67         imgid = vk_next_swap_image(sem_getimg);
68         cmdbuf = vk_get_cmdbuf(imgid);
69
70         {
71                 VkCommandBufferBeginInfo cmdbegin = {0};
72                 VkRenderPassBeginInfo rpbegin = {0};
73                 VkClearValue clear[2];
74
75                 cmdbegin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
76                 cmdbegin.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
77
78                 if(vkBeginCommandBuffer(cmdbuf, &cmdbegin) != 0) {
79                         fprintf(stderr, "failed to begin command buffer recording\n");
80                         abort();
81                 }
82
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;
89
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;
96
97                 vkCmdBeginRenderPass(cmdbuf, &rpbegin, VK_SUBPASS_CONTENTS_INLINE);
98
99                 /* ... */
100
101                 vkCmdEndRenderPass(cmdbuf);
102                 vkEndCommandBuffer(cmdbuf);
103         }
104
105         /* submit the command buffer, wait for one semaphore, signal another */
106         vk_submit(queue, cmdbuf, sem_getimg, sem_draw);
107
108         /* swap buffers after drawing is finished */
109         vk_present(queue, imgid, sem_draw);
110
111         usleep(10000);
112 }
113
114 void app_reshape(int x, int y)
115 {
116         int i;
117
118         return; /* XXX */
119
120         if(vk_reshape(x, y) == -1) {
121                 abort();
122         }
123
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));
127         }
128 }
129
130 void app_keyboard(int key, int press)
131 {
132         if(!press) return;
133
134         switch(key) {
135         case 27:
136                 app_quit();
137                 break;
138         }
139 }
140
141 void app_mouse(int bn, int press, int x, int y)
142 {
143 }
144
145 void app_motion(int x, int y)
146 {
147 }