extern "C" so that I can also use c++
[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 struct vk_ctx
9 {
10     VkInstance inst;
11     VkPhysicalDevice pdev;
12     VkDevice dev;
13
14     VkCommandPool cmd_pool;
15     VkCommandBuffer *cmd_buffers;
16     uint32_t num_cmd_buffers;
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_cmd_buffer
26 {
27     VkCommandBuffer buffer;
28     VkSubmitInfo submit_info;
29 };
30
31 struct vk_swapchain
32 {
33     VkSwapchainKHR swapchain;
34     VkSurfaceFormatKHR surf_fmt;
35
36     /* image properties */
37     VkFormat image_fmt;
38     VkExtent2D extent2d;
39
40     uint32_t num_atts;
41     struct vk_attachment *atts;
42 };
43
44 struct vk_att_props
45 {
46     uint32_t w;
47     uint32_t h;
48     uint32_t depth;
49
50     uint32_t num_samples;
51     uint32_t num_levels;
52     uint32_t num_layers;
53
54     VkFormat format;
55     VkImageUsageFlagBits usage;
56     VkImageTiling tiling;
57
58     VkImageLayout in_layout;
59     VkImageLayout end_layout;
60
61     bool is_depth;
62     bool is_swapchain;
63     bool need_export;
64 };
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     VkFramebuffer fb;
107
108     struct vk_vertex_info vertex_info;
109 };
110
111 struct vk_dims
112 {
113     float w;
114     float h;
115 };
116
117 struct vk_semaphores
118 {
119     VkSemaphore frame_ready;
120     VkSemaphore frame_done;
121 };
122
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126
127 /* context */
128
129 bool vk_init_ctx(struct vk_ctx *ctx,
130                  bool enable_layers);
131
132 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx,
133                                bool enable_cache,
134                                bool enable_layers);
135
136 void vk_cleanup_ctx(struct vk_ctx *ctx,
137                     bool enable_layers);
138
139 /* images */
140
141 bool
142 vk_create_image(struct vk_ctx *ctx,
143                 struct vk_att_props *props,
144                 struct vk_image_obj *img_obj);
145 void
146 vk_destroy_image(struct vk_ctx *ctx,
147                  struct vk_image_obj *img_obj);
148
149 bool
150 vk_fill_image_props(struct vk_ctx *ctx,
151                     uint32_t w, uint32_t h,
152                     uint32_t depth,
153                     uint32_t num_samples,
154                     uint32_t num_levels,
155                     uint32_t num_layers,
156                     VkFormat format,
157                     VkImageTiling tiling,
158                     VkImageLayout in_layout,
159                     VkImageLayout end_layout,
160                     bool is_swapchain,
161                     bool is_depth,
162                     bool need_export,
163                     struct vk_att_props *props);
164
165 /* buffers */
166
167 bool
168 vk_create_buffer(struct vk_ctx *ctx,
169                  bool is_external,
170                  uint32_t sz,
171                  VkBufferUsageFlagBits usage,
172                  void *pnext,
173                  struct vk_buf *bo);
174 void
175 vk_destroy_buffer(struct vk_ctx *ctx,
176                   struct vk_buf *bo);
177 bool
178 vk_update_buffer_data(struct vk_ctx *ctx,
179                       void *data,
180                       uint32_t data_sz,
181                       struct vk_buf *bo);
182
183 bool
184 vk_create_ext_buffer(struct vk_ctx *ctx,
185                      uint32_t sz,
186                      VkBufferUsageFlagBits usage,
187                      struct vk_buf *bo);
188
189
190 /* semaphores and fences */
191
192 bool
193 vk_create_semaphores(struct vk_ctx *ctx,
194                      bool is_external,
195                      struct vk_semaphores *semaphores);
196 void
197 vk_destroy_semaphores(struct vk_ctx *ctx,
198                       struct vk_semaphores *semaphores);
199
200 bool
201 vk_create_fences(struct vk_ctx *ctx,
202                  int num_cmd_buf,
203                  VkFenceCreateFlagBits flags,
204                  VkFence *fences);
205
206 void
207 vk_destroy_fences(struct vk_ctx *ctc,
208                   int num_fences,
209                   VkFence *fences);
210
211 /* renderer */
212
213 bool
214 vk_create_renderer(struct vk_ctx *ctx,
215                    const char *vs_src,
216                    unsigned int vs_size,
217                    const char *fs_src,
218                    unsigned int fs_size,
219                    int w, int h,
220                    uint32_t num_samples,
221                    bool enable_depth,
222                    bool enable_stencil,
223                    int num_color_att,
224                    struct vk_attachment *color_att,
225                    struct vk_attachment *depth_att,
226                    struct vk_vertex_info *vert_info,
227                    struct vk_renderer *renderer);
228
229 void
230 vk_destroy_renderer(struct vk_ctx *ctx,
231                     struct vk_renderer *pipeline);
232
233 /* draw */
234 VkCommandBuffer
235 vk_create_cmd_buffer(struct vk_ctx *ctx);
236
237 bool
238 vk_record_cmd_buffer(struct vk_ctx *ctx,
239                      VkCommandBuffer cmd_buf,
240                      struct vk_renderer *renderer,
241                      struct vk_buf *vbo,
242                      uint32_t vk_fb_color_count,
243                      float *vk_fb_color,
244                      uint32_t num_atts,
245                      struct vk_attachment *atts,
246                      float x, float y,
247                      float w, float h);
248
249 void
250 vk_reset_cmd_buf(struct vk_cmd_buffer *cmd_buf);
251
252 void
253 vk_destroy_cmd_bufs(struct vk_ctx *ctx,
254                     uint32_t num_buffers,
255                     VkCommandBuffer *buffers);
256
257 void
258 vk_draw(struct vk_ctx *ctx,
259         struct vk_semaphores *semaphores,
260         uint32_t num_buffers,
261         VkCommandBuffer *cmd_buf);
262
263 void
264 vk_clear_color(struct vk_ctx *ctx,
265                VkCommandBuffer cmd_buf,
266                struct vk_buf *vbo,
267                struct vk_renderer *renderer,
268                float *vk_fb_color,
269                uint32_t vk_fb_color_count,
270                struct vk_semaphores *semaphores,
271                bool has_wait, bool has_signal,
272                struct vk_attachment *attachments,
273                uint32_t n_attachments,
274                float x, float y, float w, float h);
275
276 /* swapchain */
277
278 bool
279 vk_create_swapchain(struct vk_ctx *ctx,
280                     int width, int height,
281                     bool vsync,
282                     VkSurfaceKHR surf,
283                     struct vk_swapchain *old_swapchain,
284                     struct vk_swapchain *swapchain);
285 void
286 vk_destroy_swapchain(struct vk_ctx *ctx,
287                      struct vk_swapchain *swapchain);
288
289 bool
290 vk_present_queue(struct vk_swapchain *swapchain,
291                  VkQueue queue,
292                  uint32_t image_idx,
293                  VkSemaphore wait_sema);
294
295 /* transitions */
296
297 void
298 vk_copy_image_to_buffer(struct vk_ctx *ctx,
299                         VkCommandBuffer cmd_buf,
300                         struct vk_attachment *src_img,
301                         struct vk_buf *dst_bo,
302                         float w, float h);
303
304 void
305 vk_transition_image_layout(struct vk_attachment *img_att,
306                            VkCommandBuffer cmd_buf,
307                            VkImageLayout old_layout,
308                            VkImageLayout new_layout,
309                            uint32_t src_queue_family_index,
310                            uint32_t dst_queue_family_index);
311
312
313 #ifdef __cplusplus
314 }
315 #endif
316
317 #endif /* VK_H */