fixes in image structs and functions
[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_swapchain
25 {
26     VkSwapchainKHR swapchain;
27     VkSurfaceKHR surface;
28     VkSurfaceFormatKHR surf_fmt;
29
30     /* image properties */
31     VkFormat image_fmt;
32     VkExtent2D extent2d;
33
34     uint32_t num_images;
35     struct vk_image_obj *images;
36 };
37
38 struct vk_image_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 need_export;
56 };
57
58 struct vk_mem_obj {
59     VkDeviceMemory mem;
60     VkDeviceSize mem_sz;
61
62     bool dedicated;
63 };
64
65 struct vk_image_obj {
66     VkImage img;
67     VkImageView img_view;
68
69     struct vk_mem_obj mobj;
70 };
71
72 struct vk_image_attachment {
73     struct vk_image_obj obj;
74     struct vk_image_props props;
75 };
76
77 struct vk_vertex_info
78 {
79     int num_verts;
80     int num_components;
81
82     VkPrimitiveTopology topology;
83 };
84
85 struct vk_buf
86 {
87     VkBuffer buf;
88     struct vk_mem_obj mobj;
89 };
90
91 struct vk_renderer
92 {
93     VkPipeline pipeline;
94     VkPipelineLayout pipeline_layout;
95     VkRenderPass renderpass;
96     VkShaderModule vs;
97     VkShaderModule fs;
98     VkFramebuffer fb;
99
100     struct vk_vertex_info vertex_info;
101 };
102
103 struct vk_dims
104 {
105     float w;
106     float h;
107 };
108
109 struct vk_semaphores
110 {
111     VkSemaphore frame_ready;
112     VkSemaphore frame_done;
113 };
114
115 /* context */
116
117 bool vk_init_ctx(struct vk_ctx *ctx);
118 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx);
119 void vk_cleanup_ctx(struct vk_ctx *ctx);
120
121 /* images */
122
123 bool
124 vk_create_image(struct vk_ctx *ctx,
125                 struct vk_image_props *props,
126                 struct vk_image_obj *img_obj);
127 void
128 vk_destroy_image(struct vk_ctx *ctx,
129                  struct vk_image_obj *img_obj);
130
131 bool
132 vk_fill_image_props(struct vk_ctx *ctx,
133                     uint32_t w, uint32_t h,
134                     uint32_t depth,
135                     uint32_t num_samples,
136                     uint32_t num_levels,
137                     uint32_t num_layers,
138                     VkFormat format,
139                     VkImageTiling tiling,
140                     VkImageLayout in_layout,
141                     VkImageLayout end_layout,
142                     bool need_export,
143                     struct vk_image_props *props);
144
145 bool
146 vk_create_attachment_from_swapchain_image(struct vk_ctx *ctx,
147                                           VkImage *swapchain_img,
148                                           VkImageView *swapchain_view,
149                                           struct vk_image_props *swapchain_props,
150                                           struct vk_image_attachment *color_att);
151
152 /* buffers */
153
154 bool
155 vk_create_buffer(struct vk_ctx *ctx,
156                  bool is_external,
157                  uint32_t sz,
158                  VkBufferUsageFlagBits usage,
159                  void *pnext,
160                  struct vk_buf *bo);
161 void
162 vk_destroy_buffer(struct vk_ctx *ctx,
163                   struct vk_buf *bo);
164 bool
165 vk_update_buffer_data(struct vk_ctx *ctx,
166                       void *data,
167                       uint32_t data_sz,
168                       struct vk_buf *bo);
169
170 bool
171 vk_create_ext_buffer(struct vk_ctx *ctx,
172                      uint32_t sz,
173                      VkBufferUsageFlagBits usage,
174                      struct vk_buf *bo);
175
176
177 /* semaphores and fences */
178
179 bool
180 vk_create_semaphores(struct vk_ctx *ctx,
181                      bool is_external,
182                      struct vk_semaphores *semaphores);
183 void
184 vk_destroy_semaphores(struct vk_ctx *ctx,
185                       struct vk_semaphores *semaphores);
186
187 bool
188 vk_create_fences(struct vk_ctx *ctx,
189                  int num_cmd_buf,
190                  VkFenceCreateFlagBits flags,
191                  VkFence *fences);
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                    bool enable_depth,
207                    bool enable_stencil,
208                    struct vk_image_attachment *color_att,
209                    struct vk_image_attachment *depth_att,
210                    struct vk_vertex_info *vert_info,
211                    struct vk_renderer *renderer);
212
213 void
214 vk_destroy_renderer(struct vk_ctx *ctx,
215                     struct vk_renderer *pipeline);
216
217 /* draw */
218
219 void
220 vk_draw(struct vk_ctx *ctx,
221         struct vk_buf *vbo,
222         struct vk_renderer *renderer,
223         float *vk_fb_color,
224         uint32_t vk_fb_color_count,
225         struct vk_semaphores *semaphores,
226         struct vk_image_attachment *attachments,
227         uint32_t n_attachments,
228         float x, float y, float w, float h);
229
230 void
231 vk_clear_color(struct vk_ctx *ctx,
232                struct vk_buf *vbo,
233                struct vk_renderer *renderer,
234                float *vk_fb_color,
235                uint32_t vk_fb_color_count,
236                struct vk_semaphores *semaphores,
237                bool has_wait, bool has_signal,
238                struct vk_image_attachment *attachments,
239                uint32_t n_attachments,
240                float x, float y, float w, float h);
241
242 /* swapchain */
243
244 bool
245 vk_create_swapchain(struct vk_ctx *ctx,
246                     int width, int height,
247                     bool vsync,
248                     VkSurfaceKHR surf,
249                     struct vk_swapchain *old_swapchain,
250                     struct vk_swapchain *swapchain);
251 void
252 vk_destroy_swapchain(struct vk_ctx *ctx,
253                      struct vk_swapchain *swapchain);
254
255 /* transitions */
256
257 void
258 vk_copy_image_to_buffer(struct vk_ctx *ctx,
259                         struct vk_image_attachment *src_img,
260                         struct vk_buf *dst_bo,
261                         float w, float h);
262
263 void
264 vk_transition_image_layout(struct vk_image_attachment *img_att,
265                            VkCommandBuffer cmd_buf,
266                            VkImageLayout old_layout,
267                            VkImageLayout new_layout,
268                            uint32_t src_queue_family_index,
269                            uint32_t dst_queue_family_index);
270
271 #endif /* VK_H */