f28930f77af8e73988f39f9513e078b6de65759c
[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
76 struct vk_renderer
77 {
78         VkPipeline pipeline;
79         VkPipelineLayout pipeline_layout;
80         VkRenderPass renderpass;
81         VkShaderModule vs;
82         VkShaderModule fs;
83         VkFramebuffer fb;
84
85         struct vk_vertex_info vertex_info;
86 };
87
88 struct vk_dims
89 {
90         float w;
91         float h;
92 };
93
94 struct vk_semaphores
95 {
96         VkSemaphore frame_ready;
97         VkSemaphore frame_done;
98 };
99
100 /* context */
101
102 bool vk_init_ctx(struct vk_ctx *ctx);
103 bool vk_init_ctx_for_rendering(struct vk_ctx *ctx);
104 void vk_cleanup_ctx(struct vk_ctx *ctx);
105
106 /* images */
107
108 bool
109 vk_create_image(struct vk_ctx *ctx,
110                                 struct vk_image_props *props,
111                                 struct vk_image_obj *img_obj);
112 void
113 vk_destroy_image(struct vk_ctx *ctx,
114                                  struct vk_image_obj *img_obj);
115
116
117 bool
118 vk_fill_ext_image_props(struct vk_ctx *ctx,
119                                                 uint32_t w, uint32_t h,
120                                                 uint32_t depth,
121                                                 uint32_t num_samples,
122                                                 uint32_t num_levels,
123                                                 uint32_t num_layers,
124                                                 VkFormat format,
125                                                 VkImageTiling tiling,
126                                                 VkImageLayout in_layout,
127                                                 VkImageLayout end_layout,
128                         bool need_export,
129                                                 struct vk_image_props *props);
130
131 /* buffers */
132
133 bool
134 vk_create_buffer(struct vk_ctx *ctx,
135                  bool is_external,
136                                  uint32_t sz,
137                                  VkBufferUsageFlagBits usage,
138                                  void *pnext,
139                                  struct vk_buf *bo);
140 void
141 vk_destroy_buffer(struct vk_ctx *ctx,
142                                   struct vk_buf *bo);
143 bool
144 vk_update_buffer_data(struct vk_ctx *ctx,
145                                           void *data,
146                                           uint32_t data_sz,
147                                           struct vk_buf *bo);
148
149 bool
150 vk_create_ext_buffer(struct vk_ctx *ctx,
151                      uint32_t sz,
152                      VkBufferUsageFlagBits usage,
153                      struct vk_buf *bo);
154
155
156 /* semaphores */
157
158 bool
159 vk_create_semaphores(struct vk_ctx *ctx,
160                                          struct vk_semaphores *semaphores);
161 void
162 vk_destroy_semaphores(struct vk_ctx *ctx,
163                                           struct vk_semaphores *semaphores);
164
165 /* renderer */
166
167 bool
168 vk_create_renderer(struct vk_ctx *ctx,
169                                    const char *vs_src,
170                                    unsigned int vs_size,
171                                    const char *fs_src,
172                                    unsigned int fs_size,
173                                    bool enable_depth,
174                                    bool enable_stencil,
175                                    struct vk_image_att *color_att,
176                                    struct vk_image_att *depth_att,
177                                    struct vk_vertex_info *vert_info,
178                                    struct vk_renderer *renderer);
179
180 void
181 vk_destroy_renderer(struct vk_ctx *ctx,
182                                         struct vk_renderer *pipeline);
183
184 /* draw */
185
186 void
187 vk_draw(struct vk_ctx *ctx,
188                 struct vk_buf *vbo,
189                 struct vk_renderer *renderer,
190                 float *vk_fb_color,
191                 uint32_t vk_fb_color_count,
192                 struct vk_semaphores *semaphores,
193                 struct vk_image_att *attachments,
194                 uint32_t n_attachments,
195                 float x, float y, float w, float h);
196
197 void
198 vk_clear_color(struct vk_ctx *ctx,
199                struct vk_buf *vbo,
200                struct vk_renderer *renderer,
201                float *vk_fb_color,
202                uint32_t vk_fb_color_count,
203                struct vk_semaphores *semaphores,
204                bool has_wait, bool has_signal,
205                struct vk_image_att *attachments,
206                uint32_t n_attachments,
207                float x, float y, float w, float h);
208
209 /* transitions */
210
211 void
212 vk_copy_image_to_buffer(struct vk_ctx *ctx,
213                                                 struct vk_image_att *src_img,
214                                                 struct vk_buf *dst_bo,
215                                                 float w, float h);
216
217 void
218 vk_transition_image_layout(struct vk_image_att *img_att,
219                            VkCommandBuffer cmd_buf,
220                            VkImageLayout old_layout,
221                            VkImageLayout new_layout,
222                            uint32_t src_queue_family_index,
223                            uint32_t dst_queue_family_index);
224
225 #endif /* VK_H */