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