fixed bug at cleanup and removed the surface from the swapchain struct
[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 /* context */
124
125 bool vk_init_ctx(struct vk_ctx *ctx,
126                  bool enable_layers);
127
128 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx,
129                                bool enable_cache,
130                                bool enable_layers);
131
132 void vk_cleanup_ctx(struct vk_ctx *ctx,
133                     bool enable_layers);
134
135 /* images */
136
137 bool
138 vk_create_image(struct vk_ctx *ctx,
139                 struct vk_att_props *props,
140                 struct vk_image_obj *img_obj);
141 void
142 vk_destroy_image(struct vk_ctx *ctx,
143                  struct vk_image_obj *img_obj);
144
145 bool
146 vk_fill_image_props(struct vk_ctx *ctx,
147                     uint32_t w, uint32_t h,
148                     uint32_t depth,
149                     uint32_t num_samples,
150                     uint32_t num_levels,
151                     uint32_t num_layers,
152                     VkFormat format,
153                     VkImageTiling tiling,
154                     VkImageLayout in_layout,
155                     VkImageLayout end_layout,
156                     bool is_swapchain,
157                     bool is_depth,
158                     bool need_export,
159                     struct vk_att_props *props);
160
161 /* buffers */
162
163 bool
164 vk_create_buffer(struct vk_ctx *ctx,
165                  bool is_external,
166                  uint32_t sz,
167                  VkBufferUsageFlagBits usage,
168                  void *pnext,
169                  struct vk_buf *bo);
170 void
171 vk_destroy_buffer(struct vk_ctx *ctx,
172                   struct vk_buf *bo);
173 bool
174 vk_update_buffer_data(struct vk_ctx *ctx,
175                       void *data,
176                       uint32_t data_sz,
177                       struct vk_buf *bo);
178
179 bool
180 vk_create_ext_buffer(struct vk_ctx *ctx,
181                      uint32_t sz,
182                      VkBufferUsageFlagBits usage,
183                      struct vk_buf *bo);
184
185
186 /* semaphores and fences */
187
188 bool
189 vk_create_semaphores(struct vk_ctx *ctx,
190                      bool is_external,
191                      struct vk_semaphores *semaphores);
192 void
193 vk_destroy_semaphores(struct vk_ctx *ctx,
194                       struct vk_semaphores *semaphores);
195
196 bool
197 vk_create_fences(struct vk_ctx *ctx,
198                  int num_cmd_buf,
199                  VkFenceCreateFlagBits flags,
200                  VkFence *fences);
201
202 void
203 vk_destroy_fences(struct vk_ctx *ctc,
204                   int num_fences,
205                   VkFence *fences);
206
207 /* renderer */
208
209 bool
210 vk_create_renderer(struct vk_ctx *ctx,
211                    const char *vs_src,
212                    unsigned int vs_size,
213                    const char *fs_src,
214                    unsigned int fs_size,
215                    int w, int h,
216                    uint32_t num_samples,
217                    bool enable_depth,
218                    bool enable_stencil,
219                    int num_color_att,
220                    struct vk_attachment *color_att,
221                    struct vk_attachment *depth_att,
222                    struct vk_vertex_info *vert_info,
223                    struct vk_renderer *renderer);
224
225 void
226 vk_destroy_renderer(struct vk_ctx *ctx,
227                     struct vk_renderer *pipeline);
228
229 /* draw */
230 VkCommandBuffer
231 vk_create_cmd_buffer(struct vk_ctx *ctx);
232
233 bool
234 vk_record_cmd_buffer(struct vk_ctx *ctx,
235                      VkCommandBuffer cmd_buf,
236                      struct vk_renderer *renderer,
237                      struct vk_buf *vbo,
238                      uint32_t vk_fb_color_count,
239                      float *vk_fb_color,
240                      uint32_t num_atts,
241                      struct vk_attachment *atts,
242                      float x, float y,
243                      float w, float h);
244
245 void
246 vk_reset_cmd_buf(struct vk_cmd_buffer *cmd_buf);
247
248 void
249 vk_destroy_cmd_bufs(struct vk_ctx *ctx,
250                     uint32_t num_buffers,
251                     VkCommandBuffer *buffers);
252
253 void
254 vk_draw(struct vk_ctx *ctx,
255         struct vk_semaphores *semaphores,
256         uint32_t num_buffers,
257         VkCommandBuffer *cmd_buf);
258
259 void
260 vk_clear_color(struct vk_ctx *ctx,
261                VkCommandBuffer cmd_buf,
262                struct vk_buf *vbo,
263                struct vk_renderer *renderer,
264                float *vk_fb_color,
265                uint32_t vk_fb_color_count,
266                struct vk_semaphores *semaphores,
267                bool has_wait, bool has_signal,
268                struct vk_attachment *attachments,
269                uint32_t n_attachments,
270                float x, float y, float w, float h);
271
272 /* swapchain */
273
274 bool
275 vk_create_swapchain(struct vk_ctx *ctx,
276                     int width, int height,
277                     bool vsync,
278                     VkSurfaceKHR surf,
279                     struct vk_swapchain *old_swapchain,
280                     struct vk_swapchain *swapchain);
281 void
282 vk_destroy_swapchain(struct vk_ctx *ctx,
283                      struct vk_swapchain *swapchain);
284
285 bool
286 vk_present_queue(struct vk_swapchain *swapchain,
287                  VkQueue queue,
288                  uint32_t image_idx,
289                  VkSemaphore wait_sema);
290
291 /* transitions */
292
293 void
294 vk_copy_image_to_buffer(struct vk_ctx *ctx,
295                         VkCommandBuffer cmd_buf,
296                         struct vk_attachment *src_img,
297                         struct vk_buf *dst_bo,
298                         float w, float h);
299
300 void
301 vk_transition_image_layout(struct vk_attachment *img_att,
302                            VkCommandBuffer cmd_buf,
303                            VkImageLayout old_layout,
304                            VkImageLayout new_layout,
305                            uint32_t src_queue_family_index,
306                            uint32_t dst_queue_family_index);
307
308 #endif /* VK_H */