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