WIP create and destroy 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_image_props
25 {
26         uint32_t w;
27         uint32_t h;
28         uint32_t depth;
29
30         uint32_t num_samples;
31         uint32_t num_levels;
32         uint32_t num_layers;
33
34         VkFormat format;
35         VkImageUsageFlagBits usage;
36         VkImageTiling tiling;
37
38         VkImageLayout in_layout;
39         VkImageLayout end_layout;
40
41     bool need_export;
42 };
43
44 struct vk_mem_obj {
45         VkDeviceMemory mem;
46         VkDeviceSize mem_sz;
47         bool dedicated;
48 };
49
50 struct vk_image_obj {
51         VkImage img;
52     VkImageView img_view;
53         struct vk_mem_obj mobj;
54 };
55
56 struct vk_image_att {
57         struct vk_image_obj obj;
58         struct vk_image_props props;
59 };
60
61 struct vk_vertex_info
62 {
63         int num_verts;
64         int num_components;
65
66         VkPrimitiveTopology topology;
67 };
68
69 struct vk_buf
70 {
71         VkBuffer buf;
72         struct vk_mem_obj mobj;
73 };
74
75 struct vk_renderer
76 {
77         VkPipeline pipeline;
78         VkPipelineLayout pipeline_layout;
79         VkRenderPass renderpass;
80         VkShaderModule vs;
81         VkShaderModule fs;
82         VkFramebuffer fb;
83
84         struct vk_vertex_info vertex_info;
85 };
86
87 struct vk_dims
88 {
89         float w;
90         float h;
91 };
92
93 struct vk_semaphores
94 {
95         VkSemaphore frame_ready;
96         VkSemaphore frame_done;
97 };
98
99 /* context */
100
101 bool vk_init_ctx(struct vk_ctx *ctx);
102 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx);
103 void vk_cleanup_ctx(struct vk_ctx *ctx);
104
105 /* images */
106
107 bool
108 vk_create_image(struct vk_ctx *ctx,
109                                 struct vk_image_props *props,
110                                 struct vk_image_obj *img_obj);
111 void
112 vk_destroy_image(struct vk_ctx *ctx,
113                                  struct vk_image_obj *img_obj);
114
115
116 bool
117 vk_fill_ext_image_props(struct vk_ctx *ctx,
118                                                 uint32_t w, uint32_t h,
119                                                 uint32_t depth,
120                                                 uint32_t num_samples,
121                                                 uint32_t num_levels,
122                                                 uint32_t num_layers,
123                                                 VkFormat format,
124                                                 VkImageTiling tiling,
125                                                 VkImageLayout in_layout,
126                                                 VkImageLayout end_layout,
127                         bool need_export,
128                                                 struct vk_image_props *props);
129
130 /* buffers */
131
132 bool
133 vk_create_buffer(struct vk_ctx *ctx,
134                  bool is_external,
135                                  uint32_t sz,
136                                  VkBufferUsageFlagBits usage,
137                                  void *pnext,
138                                  struct vk_buf *bo);
139 void
140 vk_destroy_buffer(struct vk_ctx *ctx,
141                                   struct vk_buf *bo);
142 bool
143 vk_update_buffer_data(struct vk_ctx *ctx,
144                                           void *data,
145                                           uint32_t data_sz,
146                                           struct vk_buf *bo);
147
148 bool
149 vk_create_ext_buffer(struct vk_ctx *ctx,
150                      uint32_t sz,
151                      VkBufferUsageFlagBits usage,
152                      struct vk_buf *bo);
153
154
155 /* semaphores */
156
157 bool
158 vk_create_semaphores(struct vk_ctx *ctx,
159                                          struct vk_semaphores *semaphores);
160 void
161 vk_destroy_semaphores(struct vk_ctx *ctx,
162                                           struct vk_semaphores *semaphores);
163
164 /* renderer */
165
166 bool
167 vk_create_renderer(struct vk_ctx *ctx,
168                                    const char *vs_src,
169                                    unsigned int vs_size,
170                                    const char *fs_src,
171                                    unsigned int fs_size,
172                                    bool enable_depth,
173                                    bool enable_stencil,
174                                    struct vk_image_att *color_att,
175                                    struct vk_image_att *depth_att,
176                                    struct vk_vertex_info *vert_info,
177                                    struct vk_renderer *renderer);
178
179 void
180 vk_destroy_renderer(struct vk_ctx *ctx,
181                                         struct vk_renderer *pipeline);
182
183 /* draw */
184
185 void
186 vk_draw(struct vk_ctx *ctx,
187                 struct vk_buf *vbo,
188                 struct vk_renderer *renderer,
189                 float *vk_fb_color,
190                 uint32_t vk_fb_color_count,
191                 struct vk_semaphores *semaphores,
192                 struct vk_image_att *attachments,
193                 uint32_t n_attachments,
194                 float x, float y, float w, float h);
195
196 void
197 vk_clear_color(struct vk_ctx *ctx,
198                struct vk_buf *vbo,
199                struct vk_renderer *renderer,
200                float *vk_fb_color,
201                uint32_t vk_fb_color_count,
202                struct vk_semaphores *semaphores,
203                bool has_wait, bool has_signal,
204                struct vk_image_att *attachments,
205                uint32_t n_attachments,
206                float x, float y, float w, float h);
207
208 /* transitions */
209
210 void
211 vk_copy_image_to_buffer(struct vk_ctx *ctx,
212                                                 struct vk_image_att *src_img,
213                                                 struct vk_buf *dst_bo,
214                                                 float w, float h);
215
216 void
217 vk_transition_image_layout(struct vk_image_att *img_att,
218                            VkCommandBuffer cmd_buf,
219                            VkImageLayout old_layout,
220                            VkImageLayout new_layout,
221                            uint32_t src_queue_family_index,
222                            uint32_t dst_queue_family_index);
223
224 #endif /* VK_H */