-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.cpp
More file actions
450 lines (377 loc) · 16.3 KB
/
Copy pathapplication.cpp
File metadata and controls
450 lines (377 loc) · 16.3 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#define GLFW_INCLUDE_VULKAN
#if _WIN32
#define VK_USE_PLATFORM_WIN32_KHR
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include <vulkan/vulkan.h>
#else
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>
#endif
#include <array>
#include <print>
#include <span>
import vk;
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(
[[maybe_unused]] VkDebugUtilsMessageSeverityFlagBitsEXT p_message_severity,
[[maybe_unused]] VkDebugUtilsMessageTypeFlagsEXT p_message_type,
const VkDebugUtilsMessengerCallbackDataEXT* p_callback_data,
[[maybe_unused]] void* p_user_data) {
std::print("validation layer:\t\t{}\n\n", p_callback_data->pMessage);
return false;
}
std::vector<const char*>
initialize_instance_extensions() {
std::vector<const char*> extension_names;
extension_names.emplace_back(VK_KHR_SURFACE_EXTENSION_NAME);
extension_names.emplace_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
// An additional surface extension needs to be loaded. This extension is
// platform-specific so needs to be selected based on the platform the
// example is going to be deployed to. Preprocessor directives are used
// here to select the correct platform.
#ifdef VK_USE_PLATFORM_WIN32_KHR
extension_names.emplace_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_XLIB_KHR
extensionNames.emplace_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_XCB_KHR
extensionNames.emplace_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_ANDROID_KHR
extensionNames.emplace_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
extensionNames.emplace_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_MACOS_MVK
extensionNames.emplace_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
#endif
#ifdef USE_PLATFORM_NULLWS
extensionNames.emplace_back(VK_KHR_DISPLAY_EXTENSION_NAME);
#endif
return extension_names;
}
// int main(){
// //! @note Just added the some test code to test the conan-starter setup code
// if(!glfwInit()){
// fmt::print("glfwInit could not be initialized!\n");
// return -1;
// }
// int width = 800;
// int height = 600;
// std::string title = "Hello Window";
// GLFWwindow* window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
// while(!glfwWindowShouldClose(window)){
// glfwPollEvents();
// glfwSwapBuffers(window);
// }
// return 0;
// }
int
main() {
//! @note Just added the some test code to test the conan-starter setup code
if (!glfwInit()) {
std::println("glfwInit could not be initialized!");
return -1;
}
if (!glfwVulkanSupported()) {
std::println("GLFW: Vulkan is not supported!");
return -1;
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
int width = 800;
int height = 600;
std::string title = "Hello Window";
GLFWwindow* window =
glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(window);
std::array<const char*, 1> validation_layers = {
"VK_LAYER_KHRONOS_validation",
};
// setting up extensions
std::vector<const char*> global_extensions =
initialize_instance_extensions();
vk::debug_message_utility debug_callback_info = {
// .severity essentially takes in vk::message::verbose,
// vk::message::warning, vk::message::error
.severity =
vk::message::verbose | vk::message::warning | vk::message::error,
// .message_type essentially takes in vk::debug. Like:
// vk::debug::general, vk::debug::validation, vk::debug::performance
.message_type =
vk::debug::general | vk::debug::validation | vk::debug::performance,
.callback = debug_callback
};
vk::application_params config = {
.name = "vulkan instance",
.version = vk::api_version::vk_1_3, // specify to using vulkan 1.3
.validations =
validation_layers, // .validation takes in a std::span<const char*>
.extensions =
global_extensions // .extensions also takes in std::span<const char*>
};
// 1. Setting up vk instance
vk::instance api_instance(config, debug_callback_info);
if (api_instance.alive()) {
std::println("\napi_instance alive and initiated!!!");
}
// TODO: Implement this as a way to setup physical devices
// vk::enumerate_physical_devices(vk::instance) -> returns
// std::span<vk::physical_device>
// setting up physical device
// TODO: Probably enforce the use of
// vk::enumerate_physical_device({.device_type = vk::physical::discrete})
vk::physical_enumeration enumerate_devices{
.device_type = vk::physical_gpu::discrete,
};
vk::physical_device physical_device(api_instance, enumerate_devices);
// selecting depth format
std::array<vk::format, 3> format_support = {
// VK_FORMAT_D32_SFLOAT,
// VK_FORMAT_D32_SFLOAT_S8_UINT,
// VK_FORMAT_D24_UNORM_S8_UINT,
vk::format::d32_sfloat,
vk::format::d32_sfloat_s8_uint,
vk::format::d24_unorm_s8_uint
};
// We provide a selection of format support that we want to check is
// supported on current hardware device.
VkFormat depth_format =
vk::select_depth_format(physical_device, format_support);
vk::queue_indices queue_indices = physical_device.family_indices();
std::println("Graphics Queue Family Index = {}", queue_indices.graphics);
std::println("Compute Queue Family Index = {}", queue_indices.compute);
std::println("Transfer Queue Family Index = {}", queue_indices.transfer);
// setting up logical device
std::array<float, 1> priorities = { 0.f };
std::array<const char*, 1> extensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
vk::device_params logical_device_params = {
.queue_priorities = priorities,
.extensions = extensions,
.queue_family_index = 0,
};
vk::device logical_device(physical_device, logical_device_params);
vk::surface window_surface(api_instance, window);
std::println("Starting implementation of the swapchain!!!");
vk::surface_params surface_properties =
vk::enumerate_surface(physical_device, window_surface);
if (surface_properties.format.format != VK_FORMAT_UNDEFINED) {
std::println("Surface Format.format is not undefined!!!");
}
vk::swapchain_params enumerate_swapchain_settings = {
.width = (uint32_t)width,
.height = (uint32_t)height,
.present_index =
physical_device.family_indices()
.graphics, // presentation index just uses the graphics index
};
vk::swapchain main_swapchain(logical_device,
window_surface,
enumerate_swapchain_settings,
surface_properties);
// querying swapchain images
// TODO: Make the images and framebuffers contained within the vk::swapchain
// Considering if you have two display they will prob have their own set of
// images to display to the two separate screens
uint32_t image_count = 0;
vkGetSwapchainImagesKHR(logical_device,
main_swapchain,
&image_count,
nullptr); // used to get the amount of images
std::vector<VkImage> images(image_count);
vkGetSwapchainImagesKHR(logical_device,
main_swapchain,
&image_count,
images.data()); // used to store in the images
// Creating Images
std::vector<vk::sample_image> swapchain_images(image_count);
std::vector<vk::sample_image> swapchain_depth_images(image_count);
VkExtent2D swapchain_extent = surface_properties.capabilities.currentExtent;
// Setting up the images
for (uint32_t i = 0; i < swapchain_images.size(); i++) {
vk::image_params swapchain_image_config = {
.extent = { .width=swapchain_extent.width, .height=swapchain_extent.width },
.format = surface_properties.format.format,
.aspect = vk::image_aspect_flags::color_bit,
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.mip_levels = 1,
.layer_count = 1,
.phsyical_memory_properties = physical_device.memory_properties(),
};
swapchain_images[i] =
vk::sample_image(logical_device, images[i], swapchain_image_config);
// Creating Depth Images for depth buffering
vk::image_params image_config = {
.extent = { .width=swapchain_extent.width, .height=swapchain_extent.width },
.format = depth_format,
.aspect = vk::image_aspect_flags::depth_bit,
// .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
.usage = vk::image_usage::depth_stencil_bit,
.mip_levels = 1,
.layer_count = 1,
.phsyical_memory_properties = physical_device.memory_properties(),
};
swapchain_depth_images[i] =
vk::sample_image(logical_device, image_config);
}
// setting up command buffers
std::vector<vk::command_buffer> swapchain_command_buffers(image_count);
for (size_t i = 0; i < swapchain_command_buffers.size(); i++) {
vk::command_params settings = {
.levels = vk::command_levels::primary,
.queue_index = enumerate_swapchain_settings.present_index,
.flags = vk::command_pool_flags::reset,
};
swapchain_command_buffers[i] =
vk::command_buffer(logical_device, settings);
}
// setting up attachments for the renderpass
std::array<vk::attachment, 2> renderpass_attachments = {
vk::attachment{
.format = surface_properties.format.format,
.layout = vk::image_layout::color_optimal,
.samples = vk::sample_bit::count_1,
.load = vk::attachment_load::clear,
.store = vk::attachment_store::dont_care,
.stencil_load = vk::attachment_load::clear,
.stencil_store = vk::attachment_store::dont_care,
.initial_layout = vk::image_layout::undefined,
.final_layout = vk::image_layout::present_src_khr,
},
vk::attachment{
.format = depth_format,
.layout = vk::image_layout::depth_stencil_optimal,
.samples = vk::sample_bit::count_1,
.load = vk::attachment_load::clear,
.store = vk::attachment_store::dont_care,
.stencil_load = vk::attachment_load::clear,
.stencil_store = vk::attachment_store::dont_care,
.initial_layout = vk::image_layout::undefined,
.final_layout = vk::image_layout::present_src_khr,
},
};
vk::renderpass main_renderpass(logical_device, renderpass_attachments);
std::println("renderpass created!!!");
// Setting up swapchain framebuffers
std::vector<vk::framebuffer> swapchain_framebuffers(image_count);
for (uint32_t i = 0; i < swapchain_framebuffers.size(); i++) {
// NOTE: This must match the amount of attachments the renderpass also
// has to match the image_view attachment for per-framebuffers as well
// I just set the size to whatever the renderpass attachment size are to
// ensure this is the case Since you have an image for color attachment
// and another image for the depth atttachment to specify
std::array<VkImageView, renderpass_attachments.size()>
image_view_attachments = { swapchain_images[i].image_view(),
swapchain_depth_images[i].image_view() };
vk::framebuffer_params framebuffer_info = {
.renderpass = main_renderpass,
.views = image_view_attachments,
.extent = swapchain_extent
};
swapchain_framebuffers[i] =
vk::framebuffer(logical_device, framebuffer_info);
}
std::println("Created VkFramebuffer's with size = {}",
swapchain_framebuffers.size());
// setting up presentation queue to display commands to the screen
vk::queue_params enumerate_present_queue{
.family = 0,
.index = 0,
};
vk::device_present_queue presentation_queue(
logical_device, main_swapchain, enumerate_present_queue);
// gets set with the renderpass
std::array<float, 4> color = { 0.f, 0.5f, 0.5f, 1.f };
std::println("Start implementing graphics pipeline!!!");
// Now creating a vulkan graphics pipeline for the shader loading
std::array<vk::shader_source, 2> shader_sources = {
vk::shader_source{ .filename = "sample-shaders/test.vert.spv",
.stage = vk::shader_stage::vertex },
vk::shader_source{ .filename = "sample-shaders/test.frag.spv",
.stage = vk::shader_stage::fragment },
};
// To render triangle, we do not need to set any vertex attributes
vk::shader_resource_info shader_info = {
.sources = shader_sources,
.vertex_attributes = {} // this is to explicitly set to none, but also
// dont need to set this at all regardless
};
vk::shader_resource geometry_resource(logical_device, shader_info);
if (geometry_resource.is_valid()) {
std::println("geometry resource is valid!");
}
std::array<vk::color_blend_attachment_state, 1> color_blend_attachments = {
vk::color_blend_attachment_state{},
};
std::array<vk::dynamic_state, 2> dynamic_states = { vk::dynamic_state::viewport, vk::dynamic_state::scissor };
vk::pipeline_params pipeline_configuration = {
.renderpass = main_renderpass,
.shader_modules = geometry_resource.handles(),
.vertex_attributes = geometry_resource.vertex_attributes(),
.vertex_bind_attributes = geometry_resource.vertex_bind_attributes(),
.color_blend = {
.attachments = color_blend_attachments,
},
.depth_stencil_enabled = true,
.dynamic_states = dynamic_states,
};
vk::pipeline main_graphics_pipeline(logical_device, pipeline_configuration);
if (main_graphics_pipeline.alive()) {
std::println("Main graphics pipeline alive() = {}",
main_graphics_pipeline.alive());
}
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
uint32_t current_frame = presentation_queue.acquire_next_image();
vk::command_buffer current = swapchain_command_buffers[current_frame];
current.begin(vk::command_usage::simulatneous_use_bit);
// renderpass begin/end must be within a recording command buffer
vk::renderpass_begin_params begin_renderpass = {
.current_command = current,
.extent = swapchain_extent,
.current_framebuffer = swapchain_framebuffers[current_frame],
.color = color,
.subpass = vk::subpass_contents::inline_bit
};
main_renderpass.begin(begin_renderpass);
// Binding a graphics pipeline -- before drawing stuff
// Inside of this graphics pipeline bind, is where you want to do the
// drawing stuff to
main_graphics_pipeline.bind(current);
// Drawing-call to render actual triangle to the screen
vkCmdDraw(current, 3, 1, 0, 0);
main_renderpass.end(current);
current.end();
// Submit workload and then presents the information to the screen
std::array<const VkCommandBuffer, 1> commands = { current };
presentation_queue.submit_async(commands);
presentation_queue.present_frame(current_frame);
}
// Must be called after the application exits
logical_device.wait();
main_swapchain.destroy();
for (auto& command : swapchain_command_buffers) {
command.destroy();
}
for (auto& fb : swapchain_framebuffers) {
fb.destroy();
}
for (auto& image : swapchain_images) {
image.destroy();
}
for (auto& depth_image : swapchain_depth_images) {
depth_image.destroy();
}
main_graphics_pipeline.destroy();
geometry_resource.destroy();
main_renderpass.destroy();
presentation_queue.destroy();
logical_device.destroy();
window_surface.destroy();
glfwDestroyWindow(window);
api_instance.destroy();
return 0;
}