-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvulkansystem.h
370 lines (278 loc) · 8.88 KB
/
vulkansystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef __VULKANSYSTEM_H__
#define __VULKANSYSTEM_H__
#include "lvulkan.h"
#include <vector>
#include <queue>
#include "buffer.h"
#include "shared/Matrices.h"
#include "flywheel.h"
#include "util.h"
inline Matrix4 glm_to_mat4(glm::mat4 m) {
//auto m = &mat[0];
return Matrix4(m[0][0], m[0][1], m[0][2], m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3],
m[3][0], m[3][1], m[3][2], m[3][3]);
}
enum PSO //shader files
{
PSO_SCENE = 0,
PSO_AXES,
PSO_RENDERMODEL,
PSO_COMPANION,
PSO_COUNT
};
struct FencedCommandBuffer {
VkCommandBuffer cmd_buffer;
VkFence fence;
FencedCommandBuffer();
void begin();
void end();
bool ready();
void reset();
void init();
};
/*
struct RenderModel {
Buffer mat_buffer;
Buffer vertex_buf, index_buf;
VkDescriptorSet desc_set;
void init();
};*/
struct Descriptor {
int idx = 0;
VkDescriptorSet desc;
VkImageView *view_ptr = 0;
VkSampler *sampler_ptr = 0;
VkBuffer *buffer_ptr = 0;
Descriptor();
~Descriptor();
void init();
void register_texture(VkImageView &view);
void register_model_texture(VkBuffer &buf, VkImageView &view, VkSampler &sampler);
void bind();
};
struct Swapchain {
~Swapchain();
VkSurfaceKHR surface = 0;
VkSwapchainKHR swapchain = 0;
VkRenderPass render_pass = 0;
std::vector< VkImage > vk_images;
std::vector< Image* > images;
//std::vector< VkImageView > views;
std::vector< VkFramebuffer > framebuffers;
std::vector< VkSemaphore > semaphores;
uint32_t n_swap = 0;
uint32_t frame_idx = 0;
uint32_t current_swapchain_image = 0;
//VkRenderPass renderpass, companion_renderpass;
void to_present();
void init();
void destroy();
//void to_present(int i);
//void to_colour_optimal(int i);
//void to_present_optimal(int i);
void acquire_image();
Image ¤t_img();
void begin_render_pass(uint32_t width, uint32_t height);
void end_render_pass();
void inc_frame();
};
struct VulkanSystem {
VkInstance inst;
VkDevice dev;
VkPhysicalDevice phys_dev;
VkPhysicalDeviceProperties prop;
VkPhysicalDeviceMemoryProperties mem_prop;
VkPhysicalDeviceFeatures features;
VkQueue queue;
int graphics_queue;
//uint32_t msaa = 1;
VkCommandPool cmd_pool = 0;
Swapchain swapchain;
VkDescriptorPool desc_pool = 0;
VkDescriptorSetLayout desc_set_layout = 0;
std::vector<VkDescriptorSet> desc_sets;
bool validation = true;
VkDebugReportCallbackEXT callback = 0;
//Buffer scene_constant_buffer[2]; //for both eyes
//VkImage scene_img;
//VkDeviceMemory scene_img_mem;
//VkImageView scene_img_view;
// Buffer scene_staging;
// VkBuffer scene_staging_buffer;
// VkDeviceMemory scene_staging_buffer_memory;
// VkSampler scene_sampler;
//Shader stuff
VkShaderModule shader_modules_vs[PSO_COUNT], shader_modules_ps[PSO_COUNT];
VkPipeline pipelines[PSO_COUNT];
VkPipelineLayout pipeline_layout = 0;
VkPipelineCache pipeline_cache = 0;
std::deque< FencedCommandBuffer* > cmd_buffers;
FencedCommandBuffer *cur_cmd_buffer = 0;
VkSampler sampler = 0;
VulkanSystem();
~VulkanSystem();
void init(); //general init
void init_instance();
void init_device();
void init_cmd_pool();
void init_descriptor_sets();
void init_swapchain();
void init_shaders();
void init_texture_maps();
void init_debug_callback();
void destroy_debug_callback();
void setup();
void submit(VkCommandBuffer cmd, VkFence fence, VkSemaphore semaphore = 0);
void wait_queue();
void wait_idle();
void flush_cmd();
void add_desc_set();
//void init_vulkan();
void end_cmd();
void submit_cmd();
void end_submit_cmd();
void submit_swapchain_cmd();
void end_submit_swapchain_cmd();
VkCommandBuffer cmd_buffer();
//void img_to_colour_optimal(VkImage &img);
//void swapchain_to_present(int i);
};
#include "scene.h"
struct GraphicsObject {
Descriptor desc_left, desc_right;
int n_vertex = 0, n_index = 0;
Buffer vertex_buf;
Buffer index_buf;
Matrix4 *mvp_left = 0, *mvp_right = 0;
Buffer mvp_buffer_left, mvp_buffer_right;
//Image texture;
std::vector<float> vertices;
std::vector<uint16_t> indices;
GraphicsObject();
virtual ~GraphicsObject();
virtual void render(Matrix4 &mvp, bool right);
void init_buffers();
void add_vertex(float x, float y, float z, float tx, float ty);
};
struct GraphicsCanvas : public GraphicsObject {
std::string texture; //flywheel is responsible for keeping image resources
GraphicsCanvas();
GraphicsCanvas(std::string texture_);
void init();
//void render(Matrix4 &mvp, bool right);
void change_texture(std::string texture_) {
if (texture == texture_) return;
texture = texture_;
auto *img = ImageFlywheel::image(texture);
desc_left.register_texture(img->view);
desc_right.register_texture(img->view);
}
};
struct GraphicsCube : public GraphicsObject {
std::string texture;
float width=1, height=1, depth=1;
bool balanced = true;
GraphicsCube();
void init();
//virtual void render(Matrix4 &mvp, bool right);
void change_texture(std::string texture_) {
if (texture == texture_) return;
texture = texture_;
auto *img = ImageFlywheel::image(texture);
desc_left.register_texture(img->view);
desc_right.register_texture(img->view);
}
void change_dim(float width_, float height_, float depth_);
void set_vertices();
};
struct DrawVisitor : public ObjectVisitor {
std::vector<GraphicsObject*> gobs;
Matrix4 mvp;
bool right = false;
~DrawVisitor() {
for (auto g : gobs)
delete g;
}
template <typename T>
void check_size_and_type(int i) {
if (i >= gobs.size()) {
gobs.resize(i+1);
gobs[i] = new T();
return;
}
if (dynamic_cast<T*>(gobs[i]) == NULL) {
delete gobs[i];
gobs[i] = new T();
}
}
template <typename T>
T& gob(int i) {
check_size_and_type<T>(i);
return *dynamic_cast<T*>(gobs[i]);
}
void visit(Canvas &canvas) {
auto &gcanvas = gob<GraphicsCanvas>(i);
gcanvas.change_texture(canvas.tex_name);
auto mat = mvp * glm_to_mat4(canvas.to_mat4());
//std::cout << "render canvas" << std::endl;
gcanvas.render(mat, right);
}
void visit(Box &box) {
auto &gbox = gob<GraphicsCube>(i);
gbox.change_texture(box.tex_name);
gbox.change_dim(box.width, box.height, box.depth);
//std::cout << "drawing box" << std::endl;
auto mat = mvp * glm_to_mat4(box.to_mat4());
gbox.render(mat, right);
}
void visit(Controller &controller) {
auto &gbox = gob<GraphicsCube>(i);
gbox.change_texture("red-checker.png");
gbox.change_dim(.005, .005, .005);
auto controller_mat = glm_to_mat4(controller.to_mat4());
auto mat = mvp * controller_mat;
//std::cout << "controller: " << controller.p << std::endl;
gbox.render(mat, right);
}
void visit(Point &point) {
}
void visit(HMD &hmd) {
}
};
int get_mem_type( uint32_t mem_bits, VkMemoryPropertyFlags mem_prop );
inline VkResult create_debug_report_callback_EXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugReportCallbackEXT* pCallback) {
auto func = (PFN_vkCreateDebugReportCallbackEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT");
if (func != nullptr) {
return func(instance, pCreateInfo, pAllocator, pCallback);
} else {
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
/*
typedef VkBool32 (VKAPI_PTR *PFN_vkDebugReportCallbackEXT)(
VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT objectType,
uint64_t object,
size_t location,
int32_t messageCode,
const char* pLayerPrefix,
const char* pMessage,
void* pUserData);
*/
static VkBool32 VKAPI_PTR debug_callback(
VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT objType,
uint64_t obj,
size_t location,
int32_t code,
const char* layerPrefix,
const char* msg,
void* userData) {
std::cout << "In Debug Callback: " << std::endl;
std::cerr << "validation layer: " << msg << std::endl;
// throw StringException(msg);
return VK_FALSE;
}
#endif