fixed swapchain sync for standard win, needs recreation at resize
[vkrt] / src / vk.h
1 #ifndef VK_H
2 #define VK_H
3
4 #include <vulkan/vulkan.h>
5 #include <stdbool.h>
6
7 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
8
9 struct vk_ctx
10 {
11     VkInstance inst;
12
13     VkPhysicalDevice pdev;
14     VkDevice dev;
15
16     VkCommandPool cmd_pool;
17
18     VkQueue queue;
19     int qfam_idx;
20
21     uint8_t deviceUUID[VK_UUID_SIZE];
22     uint8_t driverUUID[VK_UUID_SIZE];
23 };
24
25 struct vk_att_props
26 {
27     uint32_t w;
28     uint32_t h;
29     uint32_t depth;
30
31     uint32_t num_samples;
32     uint32_t num_levels;
33     uint32_t num_layers;
34
35     VkFormat format;
36     VkImageUsageFlagBits usage;
37     VkImageTiling tiling;
38
39     VkImageLayout in_layout;
40     VkImageLayout end_layout;
41
42     bool is_depth;
43     bool is_swapchain;
44     bool need_export;
45 };
46
47 struct vk_swapchain
48 {
49     VkSwapchainKHR swapchain;
50     VkSurfaceFormatKHR surf_fmt;
51
52     /* image properties */
53     /* FIXME: do I really need those 2?*/
54     VkFormat image_fmt;
55     VkExtent2D extent2d;
56
57     struct vk_att_props img_props;
58
59     uint32_t num_images;
60     VkImage *images;
61     VkImageView *views;
62 };
63
64 /* for allocated images */
65
66 struct vk_mem_obj {
67     VkDeviceMemory mem;
68     VkDeviceSize mem_sz;
69
70     bool dedicated;
71 };
72
73 struct vk_image_obj {
74     VkImage img;
75     VkImageView img_view;
76
77     struct vk_mem_obj mobj;
78 };
79
80 struct vk_attachment {
81     struct vk_image_obj obj;
82     struct vk_att_props props;
83 };
84
85 struct vk_vertex_info
86 {
87     int num_verts;
88     int num_components;
89
90     VkPrimitiveTopology topology;
91 };
92
93 struct vk_buf
94 {
95     VkBuffer buf;
96     struct vk_mem_obj mobj;
97 };
98
99 struct vk_renderer
100 {
101     VkPipeline pipeline;
102     VkPipelineLayout pipeline_layout;
103     VkRenderPass renderpass;
104     VkShaderModule vs;
105     VkShaderModule fs;
106
107     struct vk_vertex_info vertex_info;
108 };
109
110 struct vk_dims
111 {
112     float w;
113     float h;
114 };
115
116 struct vk_semaphores
117 {
118     VkSemaphore frame_ready;
119     VkSemaphore frame_done;
120 };
121
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125
126 /* context */
127
128 bool vk_init_ctx(struct vk_ctx *ctx,
129                  bool enable_layers);
130
131 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx,
132                                bool enable_cache,
133                                bool enable_layers);
134
135 void vk_cleanup_ctx(struct vk_ctx *ctx);
136
137 /* images */
138 bool
139 vk_create_ext_image(struct vk_ctx *ctx,
140                     struct vk_att_props *props,
141                     struct vk_image_obj *img);
142
143 bool
144 vk_create_image(struct vk_ctx *ctx,
145                 struct vk_att_props *props,
146                 struct vk_image_obj *img_obj);
147 void
148 vk_destroy_image(struct vk_ctx *ctx,
149                  struct vk_image_obj *img_obj);
150
151 bool
152 vk_create_image_view(struct vk_ctx *ctx,
153                      VkImage image,
154                      VkImageViewType view_type,
155                      VkFormat format,
156                      bool is_swapchain,
157                      VkImageView *image_view);
158
159 bool
160 vk_fill_image_props(struct vk_ctx *ctx,
161                     uint32_t w, uint32_t h,
162                     uint32_t depth,
163                     uint32_t num_samples,
164                     uint32_t num_levels,
165                     uint32_t num_layers,
166                     VkFormat format,
167                     VkImageTiling tiling,
168                     VkImageLayout in_layout,
169                     VkImageLayout end_layout,
170                     bool is_swapchain,
171                     bool is_depth,
172                     bool need_export,
173                     struct vk_att_props *props);
174
175 struct vk_attachment
176 vk_create_attachment_from_obj(struct vk_image_obj *obj,
177                               struct vk_att_props *props);
178
179 struct vk_attachment
180 vk_create_attachment(VkImage image,
181                      VkImageView view,
182                      struct vk_att_props *props);
183
184 /* buffers */
185
186 bool
187 vk_create_buffer(struct vk_ctx *ctx,
188                  bool is_external,
189                  uint32_t sz,
190                  VkBufferUsageFlagBits usage,
191                  void *pnext,
192                  struct vk_buf *bo);
193 void
194 vk_destroy_buffer(struct vk_ctx *ctx,
195                   struct vk_buf *bo);
196 bool
197 vk_update_buffer_data(struct vk_ctx *ctx,
198                       void *data,
199                       uint32_t data_sz,
200                       struct vk_buf *bo);
201
202 bool
203 vk_create_ext_buffer(struct vk_ctx *ctx,
204                      uint32_t sz,
205                      VkBufferUsageFlagBits usage,
206                      struct vk_buf *bo);
207
208
209 /* semaphores and fences */
210
211 bool
212 vk_create_semaphores(struct vk_ctx *ctx,
213                      bool is_external,
214                      struct vk_semaphores *semaphores);
215 void
216 vk_destroy_semaphores(struct vk_ctx *ctx,
217                       struct vk_semaphores *semaphores);
218
219 void
220 vk_destroy_fences(struct vk_ctx *ctc,
221                   int num_fences,
222                   VkFence *fences);
223
224 /* renderer */
225 bool
226 vk_create_framebuffer(struct vk_ctx *ctx,
227                       int width, int height,
228                       int num_color_atts,
229                       VkImageView *color_views,
230                       VkImageView *depth_view,
231                       VkRenderPass rb,
232                       VkFramebuffer *fb);
233
234 bool
235 vk_create_renderpass(struct vk_ctx *ctx,
236                      uint32_t num_color_atts,
237                      struct vk_att_props *color_props,
238                      struct vk_att_props *depth_props,
239                      VkRenderPass *renderpass);
240
241 bool
242 vk_create_renderer(struct vk_ctx *ctx,
243                    const char *vs_src,
244                    unsigned int vs_size,
245                    const char *fs_src,
246                    unsigned int fs_size,
247                    int w, int h,
248                    uint32_t num_samples,
249                    bool enable_depth,
250                    bool enable_stencil,
251                    int num_color_att,
252                    struct vk_att_props *color_props,
253                    struct vk_att_props *depth_props,
254                    struct vk_vertex_info *vert_info,
255                    struct vk_renderer *renderer);
256
257 void
258 vk_destroy_renderer(struct vk_ctx *ctx,
259                     struct vk_renderer *pipeline);
260
261 /* fences and command buffers */
262 bool
263 vk_create_fence(struct vk_ctx *ctx,
264                 VkFence *fence);
265
266 VkCommandBuffer
267 vk_create_cmd_buffer(struct vk_ctx *ctx);
268
269 bool
270 vk_record_cmd_buffer(struct vk_ctx *ctx,
271                      VkCommandBuffer cmd_buf,
272                      struct vk_renderer *renderer,
273                      struct vk_buf *vbo,
274                      uint32_t vk_fb_color_count,
275                      float *vk_fb_color,
276                      VkFramebuffer fb,
277                      uint32_t num_atts,
278                      struct vk_attachment *atts,
279                      float x, float y,
280                      float w, float h);
281
282 void
283 vk_destroy_cmd_buffers(struct vk_ctx *ctx,
284                        uint32_t num_buffers,
285                        VkCommandBuffer *buffers);
286
287 /* draw */
288 void
289 vk_draw(struct vk_ctx *ctx,
290         struct vk_semaphores *semaphores,
291         uint32_t num_buffers,
292         VkCommandBuffer *cmd_buf);
293
294 void
295 vk_clear_color(struct vk_ctx *ctx,
296                VkCommandBuffer cmd_buf,
297                struct vk_buf *vbo,
298                struct vk_renderer *renderer,
299                float *vk_fb_color,
300                uint32_t vk_fb_color_count,
301                VkFramebuffer fb,
302                struct vk_semaphores *semaphores,
303                bool has_wait, bool has_signal,
304                struct vk_attachment *attachments,
305                uint32_t n_attachments,
306                float x, float y, float w, float h);
307
308 void
309 vk_set_viewport(struct vk_ctx *ctx,
310                 VkCommandBuffer cmd_buf,
311                 float x, float y,
312                 float w, float h,
313                 float near, float far);
314
315 /* swapchain */
316
317 bool
318 vk_create_swapchain(struct vk_ctx *ctx,
319                     int width, int height,
320                     bool vsync,
321                     VkSurfaceKHR surf,
322                     struct vk_swapchain *old_swapchain,
323                     struct vk_swapchain *swapchain);
324 void
325 vk_destroy_swapchain(struct vk_ctx *ctx,
326                      struct vk_swapchain *swapchain);
327
328 bool
329 vk_queue_present(struct vk_swapchain *swapchain,
330                  VkQueue queue,
331                  uint32_t image_idx,
332                  VkSemaphore wait_sema);
333
334 /* transitions */
335
336 void
337 vk_copy_image_to_buffer(struct vk_ctx *ctx,
338                         VkCommandBuffer cmd_buf,
339                         struct vk_attachment *src_img,
340                         struct vk_buf *dst_bo,
341                         float w, float h);
342
343 void
344 vk_transition_image_layout(struct vk_attachment *img_att,
345                            VkCommandBuffer cmd_buf,
346                            VkImageLayout old_layout,
347                            VkImageLayout new_layout,
348                            uint32_t src_queue_family_index,
349                            uint32_t dst_queue_family_index);
350
351
352 #ifdef __cplusplus
353 }
354 #endif
355
356 #endif /* VK_H */