WIP impl. create glfw window surface

Minor `cargo fmt` fix.

TODO: "Querying for presentation support"
https://vulkan-tutorial.com/Drawing_a_triangle/Presentation/Window_surface
This commit is contained in:
Stephen Seo 2024-03-05 15:58:33 +09:00
parent ac88544d2f
commit 62c01acc7a

View file

@ -184,6 +184,7 @@ struct VulkanApp {
window: *mut ffi::GLFWwindow, window: *mut ffi::GLFWwindow,
vk_instance: ffi::VkInstance, vk_instance: ffi::VkInstance,
debug_messenger: ffi::VkDebugUtilsMessengerEXT, debug_messenger: ffi::VkDebugUtilsMessengerEXT,
surface: ffi::VkSurfaceKHR,
physical_device: ffi::VkPhysicalDevice, physical_device: ffi::VkPhysicalDevice,
device: ffi::VkDevice, device: ffi::VkDevice,
graphics_queue: ffi::VkQueue, graphics_queue: ffi::VkQueue,
@ -195,6 +196,7 @@ impl VulkanApp {
window: std::ptr::null_mut(), window: std::ptr::null_mut(),
vk_instance: std::ptr::null_mut(), vk_instance: std::ptr::null_mut(),
debug_messenger: std::ptr::null_mut(), debug_messenger: std::ptr::null_mut(),
surface: std::ptr::null_mut(),
physical_device: std::ptr::null_mut(), physical_device: std::ptr::null_mut(),
device: std::ptr::null_mut(), device: std::ptr::null_mut(),
graphics_queue: std::ptr::null_mut(), graphics_queue: std::ptr::null_mut(),
@ -228,6 +230,7 @@ impl VulkanApp {
self.create_instance(); self.create_instance();
self.setup_debug_messenger(); self.setup_debug_messenger();
self.create_surface();
self.pick_physical_device(); self.pick_physical_device();
self.create_logical_device(); self.create_logical_device();
} }
@ -376,7 +379,8 @@ impl VulkanApp {
let indices = find_queue_families(self.physical_device); let indices = find_queue_families(self.physical_device);
let mut dev_queue_create_info: ffi::VkDeviceQueueCreateInfo = unsafe { std::mem::zeroed() }; let mut dev_queue_create_info: ffi::VkDeviceQueueCreateInfo = unsafe { std::mem::zeroed() };
dev_queue_create_info.sType = ffi::VkStructureType_VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; dev_queue_create_info.sType =
ffi::VkStructureType_VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
dev_queue_create_info.queueFamilyIndex = indices dev_queue_create_info.queueFamilyIndex = indices
.graphics_family .graphics_family
.expect("\"physical_device\" must support graphics!"); .expect("\"physical_device\" must support graphics!");
@ -423,6 +427,20 @@ impl VulkanApp {
} }
} }
fn create_surface(&mut self) {
let result = unsafe {
ffi::glfwCreateWindowSurface(
self.vk_instance,
self.window,
std::ptr::null(),
std::ptr::addr_of_mut!(self.surface),
)
};
if result != ffi::VkResult_VK_SUCCESS {
panic!("Failed to create window surface!");
}
}
fn main_loop(&mut self) { fn main_loop(&mut self) {
if self.window.is_null() { if self.window.is_null() {
panic!("ERROR: Cannot execute main loop if window is null!"); panic!("ERROR: Cannot execute main loop if window is null!");
@ -468,6 +486,12 @@ impl Drop for VulkanApp {
} }
} }
if !self.surface.is_null() {
unsafe {
ffi::vkDestroySurfaceKHR(self.vk_instance, self.surface, std::ptr::null());
}
}
if !self.vk_instance.is_null() { if !self.vk_instance.is_null() {
unsafe { unsafe {
ffi::vkDestroyInstance(self.vk_instance, std::ptr::null()); ffi::vkDestroyInstance(self.vk_instance, std::ptr::null());