many changes: Makefile, util, fixed indent, fixes in swapchain
[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_buf;
16
17     VkQueue queue;
18     int qfam_idx;
19
20     uint8_t deviceUUID[VK_UUID_SIZE];
21     uint8_t driverUUID[VK_UUID_SIZE];
22 };
23
24 struct vk_swap_image_obj
25 {
26     VkImage image;
27     VkImageView image_view;
28 };
29
30 struct vk_swapchain
31 {
32     VkSwapchainKHR swapchain;
33     VkSurfaceKHR surface;
34     VkSurfaceFormatKHR surf_fmt;
35
36     /* image properties */
37     VkFormat image_fmt;
38     VkExtent2D extent2d;
39
40     uint32_t num_images;
41     struct vk_swap_image_obj *images;
42 };
43
44 struct vk_image_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 need_export;
62 };
63
64 struct vk_mem_obj {
65     VkDeviceMemory mem;
66     VkDeviceSize mem_sz;
67
68     bool dedicated;
69 };
70
71 struct vk_image_obj {
72     VkImage img;
73     VkImageView img_view;
74
75     struct vk_mem_obj mobj;
76 };
77
78 struct vk_image_att {
79     struct vk_image_obj obj;
80     struct vk_image_props props;
81 };
82
83 struct vk_vertex_info
84 {
85     int num_verts;
86     int num_components;
87
88     VkPrimitiveTopology topology;
89 };
90
91 struct vk_buf
92 {
93     VkBuffer buf;
94     struct vk_mem_obj mobj;
95 };
96
97 struct vk_renderer
98 {
99     VkPipeline pipeline;
100     VkPipelineLayout pipeline_layout;
101     VkRenderPass renderpass;
102     VkShaderModule vs;
103     VkShaderModule fs;
104     VkFramebuffer fb;
105
106     struct vk_vertex_info vertex_info;
107 };
108
109 struct vk_dims
110 {
111     float w;
112     float h;
113 };
114
115 struct vk_semaphores
116 {
117     VkSemaphore frame_ready;
118     VkSemaphore frame_done;
119 };
120
121 /* context */
122
123 bool vk_init_ctx(struct vk_ctx *ctx);
124 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx);
125 void vk_cleanup_ctx(struct vk_ctx *ctx);
126
127 /* images */
128
129 bool
130 vk_create_image(struct vk_ctx *ctx,
131                 struct vk_image_props *props,
132                 struct vk_image_obj *img_obj);
133 void
134 vk_destroy_image(struct vk_ctx *ctx,
135                  struct vk_image_obj *img_obj);
136
137 bool
138 vk_fill_ext_image_props(struct vk_ctx *ctx,
139                         uint32_t w, uint32_t h,
140                         uint32_t depth,
141                         uint32_t num_samples,
142                         uint32_t num_levels,
143                         uint32_t num_layers,
144                         VkFormat format,
145                         VkImageTiling tiling,
146                         VkImageLayout in_layout,
147                         VkImageLayout end_layout,
148                         bool need_export,
149                         struct vk_image_props *props);
150
151 bool
152 vk_create_attachment_from_swapchain_image(struct vk_ctx *ctx,
153                                           VkImage *swapchain_img,
154                                           VkImageView *swapchain_view,
155                                           struct vk_image_props *swapchain_props,
156                                           struct vk_image_att *color_att);
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 */
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 /* renderer */
194
195 bool
196 vk_create_renderer(struct vk_ctx *ctx,
197                    const char *vs_src,
198                    unsigned int vs_size,
199                    const char *fs_src,
200                    unsigned int fs_size,
201                    bool enable_depth,
202                    bool enable_stencil,
203                    struct vk_image_att *color_att,
204                    struct vk_image_att *depth_att,
205                    struct vk_vertex_info *vert_info,
206                    struct vk_renderer *renderer);
207
208 void
209 vk_destroy_renderer(struct vk_ctx *ctx,
210                     struct vk_renderer *pipeline);
211
212 /* draw */
213
214 void
215 vk_draw(struct vk_ctx *ctx,
216         struct vk_buf *vbo,
217         struct vk_renderer *renderer,
218         float *vk_fb_color,
219         uint32_t vk_fb_color_count,
220         struct vk_semaphores *semaphores,
221         struct vk_image_att *attachments,
222         uint32_t n_attachments,
223         float x, float y, float w, float h);
224
225 void
226 vk_clear_color(struct vk_ctx *ctx,
227                struct vk_buf *vbo,
228                struct vk_renderer *renderer,
229                float *vk_fb_color,
230                uint32_t vk_fb_color_count,
231                struct vk_semaphores *semaphores,
232                bool has_wait, bool has_signal,
233                struct vk_image_att *attachments,
234                uint32_t n_attachments,
235                float x, float y, float w, float h);
236
237 /* swapchain */
238
239 bool
240 vk_create_swapchain(struct vk_ctx *ctx,
241                     int width, int height,
242                     bool vsync,
243                     VkSurfaceKHR surf,
244                     struct vk_swapchain *old_swapchain,
245                     struct vk_swapchain *swapchain);
246 void
247 vk_destroy_swapchain(struct vk_ctx *ctx,
248                      struct vk_swapchain *swapchain);
249
250 /* transitions */
251
252 void
253 vk_copy_image_to_buffer(struct vk_ctx *ctx,
254                         struct vk_image_att *src_img,
255                         struct vk_buf *dst_bo,
256                         float w, float h);
257
258 void
259 vk_transition_image_layout(struct vk_image_att *img_att,
260                            VkCommandBuffer cmd_buf,
261                            VkImageLayout old_layout,
262                            VkImageLayout new_layout,
263                            uint32_t src_queue_family_index,
264                            uint32_t dst_queue_family_index);
265
266 #endif /* VK_H */