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