dfe6aa685ec307030df1984581bafeac8f717ea3
[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     VkSurfaceKHR surface;
35     VkSurfaceFormatKHR surf_fmt;
36
37     /* image properties */
38     VkFormat image_fmt;
39     VkExtent2D extent2d;
40
41     uint32_t num_atts;
42     struct vk_attachment *atts;
43 };
44
45 struct vk_att_props
46 {
47     uint32_t w;
48     uint32_t h;
49     uint32_t depth;
50
51     uint32_t num_samples;
52     uint32_t num_levels;
53     uint32_t num_layers;
54
55     VkFormat format;
56     VkImageUsageFlagBits usage;
57     VkImageTiling tiling;
58
59     VkImageLayout in_layout;
60     VkImageLayout end_layout;
61
62     bool is_depth;
63     bool is_swapchain;
64     bool need_export;
65 };
66
67 struct vk_mem_obj {
68     VkDeviceMemory mem;
69     VkDeviceSize mem_sz;
70
71     bool dedicated;
72 };
73
74 struct vk_image_obj {
75     VkImage img;
76     VkImageView img_view;
77
78     struct vk_mem_obj mobj;
79 };
80
81 struct vk_attachment {
82     struct vk_image_obj obj;
83     struct vk_att_props props;
84 };
85
86 struct vk_vertex_info
87 {
88     int num_verts;
89     int num_components;
90
91     VkPrimitiveTopology topology;
92 };
93
94 struct vk_buf
95 {
96     VkBuffer buf;
97     struct vk_mem_obj mobj;
98 };
99
100 struct vk_renderer
101 {
102     VkPipeline pipeline;
103     VkPipelineLayout pipeline_layout;
104     VkRenderPass renderpass;
105     VkShaderModule vs;
106     VkShaderModule fs;
107     VkFramebuffer fb;
108
109     struct vk_vertex_info vertex_info;
110 };
111
112 struct vk_dims
113 {
114     float w;
115     float h;
116 };
117
118 struct vk_semaphores
119 {
120     VkSemaphore frame_ready;
121     VkSemaphore frame_done;
122 };
123
124 /* context */
125
126 bool vk_init_ctx(struct vk_ctx *ctx,
127                  bool enable_layers);
128
129 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx,
130                                bool enable_cache,
131                                bool enable_layers);
132
133 void vk_cleanup_ctx(struct vk_ctx *ctx,
134                     bool enable_layers);
135
136 /* images */
137
138 bool
139 vk_create_image(struct vk_ctx *ctx,
140                 struct vk_att_props *props,
141                 struct vk_image_obj *img_obj);
142 void
143 vk_destroy_image(struct vk_ctx *ctx,
144                  struct vk_image_obj *img_obj);
145
146 bool
147 vk_fill_image_props(struct vk_ctx *ctx,
148                     uint32_t w, uint32_t h,
149                     uint32_t depth,
150                     uint32_t num_samples,
151                     uint32_t num_levels,
152                     uint32_t num_layers,
153                     VkFormat format,
154                     VkImageTiling tiling,
155                     VkImageLayout in_layout,
156                     VkImageLayout end_layout,
157                     bool is_swapchain,
158                     bool is_depth,
159                     bool need_export,
160                     struct vk_att_props *props);
161
162 /* buffers */
163
164 bool
165 vk_create_buffer(struct vk_ctx *ctx,
166                  bool is_external,
167                  uint32_t sz,
168                  VkBufferUsageFlagBits usage,
169                  void *pnext,
170                  struct vk_buf *bo);
171 void
172 vk_destroy_buffer(struct vk_ctx *ctx,
173                   struct vk_buf *bo);
174 bool
175 vk_update_buffer_data(struct vk_ctx *ctx,
176                       void *data,
177                       uint32_t data_sz,
178                       struct vk_buf *bo);
179
180 bool
181 vk_create_ext_buffer(struct vk_ctx *ctx,
182                      uint32_t sz,
183                      VkBufferUsageFlagBits usage,
184                      struct vk_buf *bo);
185
186
187 /* semaphores and fences */
188
189 bool
190 vk_create_semaphores(struct vk_ctx *ctx,
191                      bool is_external,
192                      struct vk_semaphores *semaphores);
193 void
194 vk_destroy_semaphores(struct vk_ctx *ctx,
195                       struct vk_semaphores *semaphores);
196
197 bool
198 vk_create_fences(struct vk_ctx *ctx,
199                  int num_cmd_buf,
200                  VkFenceCreateFlagBits flags,
201                  VkFence *fences);
202
203 void
204 vk_destroy_fences(struct vk_ctx *ctc,
205                   int num_fences,
206                   VkFence *fences);
207
208 /* renderer */
209
210 bool
211 vk_create_renderer(struct vk_ctx *ctx,
212                    const char *vs_src,
213                    unsigned int vs_size,
214                    const char *fs_src,
215                    unsigned int fs_size,
216                    int w, int h,
217                    uint32_t num_samples,
218                    bool enable_depth,
219                    bool enable_stencil,
220                    int num_color_att,
221                    struct vk_attachment *color_att,
222                    struct vk_attachment *depth_att,
223                    struct vk_vertex_info *vert_info,
224                    struct vk_renderer *renderer);
225
226 void
227 vk_destroy_renderer(struct vk_ctx *ctx,
228                     struct vk_renderer *pipeline);
229
230 /* draw */
231 VkCommandBuffer
232 vk_create_cmd_buffer(struct vk_ctx *ctx);
233
234 bool
235 vk_record_cmd_buffer(struct vk_ctx *ctx,
236                      VkCommandBuffer cmd_buf,
237                      struct vk_renderer *renderer,
238                      struct vk_buf *vbo,
239                      uint32_t vk_fb_color_count,
240                      float *vk_fb_color,
241                      uint32_t num_atts,
242                      struct vk_attachment *atts,
243                      float x, float y,
244                      float w, float h);
245
246 void
247 vk_reset_cmd_buf(struct vk_cmd_buffer *cmd_buf);
248
249 void
250 vk_destroy_cmd_bufs(struct vk_ctx *ctx,
251                     uint32_t num_buffers,
252                     VkCommandBuffer *buffers);
253
254 void
255 vk_draw(struct vk_ctx *ctx,
256         struct vk_semaphores *semaphores,
257         uint32_t num_buffers,
258         VkCommandBuffer *cmd_buf);
259
260 void
261 vk_clear_color(struct vk_ctx *ctx,
262                VkCommandBuffer cmd_buf,
263                struct vk_buf *vbo,
264                struct vk_renderer *renderer,
265                float *vk_fb_color,
266                uint32_t vk_fb_color_count,
267                struct vk_semaphores *semaphores,
268                bool has_wait, bool has_signal,
269                struct vk_attachment *attachments,
270                uint32_t n_attachments,
271                float x, float y, float w, float h);
272
273 /* swapchain */
274
275 bool
276 vk_create_swapchain(struct vk_ctx *ctx,
277                     int width, int height,
278                     bool vsync,
279                     VkSurfaceKHR surf,
280                     struct vk_swapchain *old_swapchain,
281                     struct vk_swapchain *swapchain);
282 void
283 vk_destroy_swapchain(struct vk_ctx *ctx,
284                      struct vk_swapchain *swapchain);
285
286 bool
287 vk_present_queue(struct vk_swapchain *swapchain,
288                  VkQueue queue,
289                  uint32_t image_idx,
290                  VkSemaphore wait_sema);
291
292 /* transitions */
293
294 void
295 vk_copy_image_to_buffer(struct vk_ctx *ctx,
296                         VkCommandBuffer cmd_buf,
297                         struct vk_attachment *src_img,
298                         struct vk_buf *dst_bo,
299                         float w, float h);
300
301 void
302 vk_transition_image_layout(struct vk_attachment *img_att,
303                            VkCommandBuffer cmd_buf,
304                            VkImageLayout old_layout,
305                            VkImageLayout new_layout,
306                            uint32_t src_queue_family_index,
307                            uint32_t dst_queue_family_index);
308
309 #endif /* VK_H */