5adfbd71b96624646a60b32e3eba645ff06e1769
[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_swapchain
26 {
27     VkSwapchainKHR swapchain;
28     VkSurfaceFormatKHR surf_fmt;
29
30     /* image properties */
31     VkFormat image_fmt;
32     VkExtent2D extent2d;
33
34     uint32_t num_atts;
35     struct vk_attachment *atts;
36 };
37
38 struct vk_att_props
39 {
40     uint32_t w;
41     uint32_t h;
42     uint32_t depth;
43
44     uint32_t num_samples;
45     uint32_t num_levels;
46     uint32_t num_layers;
47
48     VkFormat format;
49     VkImageUsageFlagBits usage;
50     VkImageTiling tiling;
51
52     VkImageLayout in_layout;
53     VkImageLayout end_layout;
54
55     bool is_depth;
56     bool is_swapchain;
57     bool need_export;
58 };
59
60 struct vk_mem_obj {
61     VkDeviceMemory mem;
62     VkDeviceSize mem_sz;
63
64     bool dedicated;
65 };
66
67 struct vk_image_obj {
68     VkImage img;
69     VkImageView img_view;
70
71     struct vk_mem_obj mobj;
72 };
73
74 struct vk_attachment {
75     struct vk_image_obj obj;
76     struct vk_att_props props;
77 };
78
79 struct vk_vertex_info
80 {
81     int num_verts;
82     int num_components;
83
84     VkPrimitiveTopology topology;
85 };
86
87 struct vk_buf
88 {
89     VkBuffer buf;
90     struct vk_mem_obj mobj;
91 };
92
93 struct vk_renderer
94 {
95     VkPipeline pipeline;
96     VkPipelineLayout pipeline_layout;
97     VkRenderPass renderpass;
98     VkShaderModule vs;
99     VkShaderModule fs;
100     VkFramebuffer fb;
101
102     struct vk_vertex_info vertex_info;
103 };
104
105 struct vk_dims
106 {
107     float w;
108     float h;
109 };
110
111 struct vk_semaphores
112 {
113     VkSemaphore frame_ready;
114     VkSemaphore frame_done;
115 };
116
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120
121 /* context */
122
123 bool vk_init_ctx(struct vk_ctx *ctx,
124                  bool enable_layers);
125
126 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx,
127                                bool enable_cache,
128                                bool enable_layers);
129
130 void vk_cleanup_ctx(struct vk_ctx *ctx);
131
132 /* images */
133
134 bool
135 vk_create_image(struct vk_ctx *ctx,
136                 struct vk_att_props *props,
137                 struct vk_image_obj *img_obj);
138 void
139 vk_destroy_image(struct vk_ctx *ctx,
140                  struct vk_image_obj *img_obj);
141
142 bool
143 vk_fill_image_props(struct vk_ctx *ctx,
144                     uint32_t w, uint32_t h,
145                     uint32_t depth,
146                     uint32_t num_samples,
147                     uint32_t num_levels,
148                     uint32_t num_layers,
149                     VkFormat format,
150                     VkImageTiling tiling,
151                     VkImageLayout in_layout,
152                     VkImageLayout end_layout,
153                     bool is_swapchain,
154                     bool is_depth,
155                     bool need_export,
156                     struct vk_att_props *props);
157
158 /* buffers */
159
160 bool
161 vk_create_buffer(struct vk_ctx *ctx,
162                  bool is_external,
163                  uint32_t sz,
164                  VkBufferUsageFlagBits usage,
165                  void *pnext,
166                  struct vk_buf *bo);
167 void
168 vk_destroy_buffer(struct vk_ctx *ctx,
169                   struct vk_buf *bo);
170 bool
171 vk_update_buffer_data(struct vk_ctx *ctx,
172                       void *data,
173                       uint32_t data_sz,
174                       struct vk_buf *bo);
175
176 bool
177 vk_create_ext_buffer(struct vk_ctx *ctx,
178                      uint32_t sz,
179                      VkBufferUsageFlagBits usage,
180                      struct vk_buf *bo);
181
182
183 /* semaphores and fences */
184
185 bool
186 vk_create_semaphores(struct vk_ctx *ctx,
187                      bool is_external,
188                      struct vk_semaphores *semaphores);
189 void
190 vk_destroy_semaphores(struct vk_ctx *ctx,
191                       struct vk_semaphores *semaphores);
192
193 void
194 vk_destroy_fences(struct vk_ctx *ctc,
195                   int num_fences,
196                   VkFence *fences);
197
198 /* renderer */
199
200 bool
201 vk_create_renderer(struct vk_ctx *ctx,
202                    const char *vs_src,
203                    unsigned int vs_size,
204                    const char *fs_src,
205                    unsigned int fs_size,
206                    int w, int h,
207                    uint32_t num_samples,
208                    bool enable_depth,
209                    bool enable_stencil,
210                    int num_color_att,
211                    struct vk_attachment *color_att,
212                    struct vk_attachment *depth_att,
213                    struct vk_vertex_info *vert_info,
214                    struct vk_renderer *renderer);
215
216 void
217 vk_destroy_renderer(struct vk_ctx *ctx,
218                     struct vk_renderer *pipeline);
219
220 /* fences and command buffers */
221 bool
222 vk_create_fence(struct vk_ctx *ctx,
223                 VkFence *fence);
224
225 VkCommandBuffer
226 vk_create_cmd_buffer(struct vk_ctx *ctx);
227
228 bool
229 vk_record_cmd_buffer(struct vk_ctx *ctx,
230                      VkCommandBuffer cmd_buf,
231                      struct vk_renderer *renderer,
232                      struct vk_buf *vbo,
233                      uint32_t vk_fb_color_count,
234                      float *vk_fb_color,
235                      uint32_t num_atts,
236                      struct vk_attachment *atts,
237                      float x, float y,
238                      float w, float h);
239
240 void
241 vk_destroy_cmd_buffers(struct vk_ctx *ctx,
242                        uint32_t num_buffers,
243                        VkCommandBuffer *buffers);
244
245 /* draw */
246 void
247 vk_draw(struct vk_ctx *ctx,
248         struct vk_semaphores *semaphores,
249         uint32_t num_buffers,
250         VkCommandBuffer *cmd_buf);
251
252 void
253 vk_clear_color(struct vk_ctx *ctx,
254                VkCommandBuffer cmd_buf,
255                struct vk_buf *vbo,
256                struct vk_renderer *renderer,
257                float *vk_fb_color,
258                uint32_t vk_fb_color_count,
259                struct vk_semaphores *semaphores,
260                bool has_wait, bool has_signal,
261                struct vk_attachment *attachments,
262                uint32_t n_attachments,
263                float x, float y, float w, float h);
264
265 void
266 vk_set_viewport(struct vk_ctx *ctx,
267                 VkCommandBuffer cmd_buf,
268                 float x, float y,
269                 float w, float h,
270                 float near, float far);
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_queue_present(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
309 #ifdef __cplusplus
310 }
311 #endif
312
313 #endif /* VK_H */