diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 169 |
1 files changed, 85 insertions, 84 deletions
diff --git a/src/main.rs b/src/main.rs index d23ff01..61a770a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,37 +124,29 @@ struct Surreality { graphics_queue: RefCell<Option<vk::Queue>>, presentation_queue: RefCell<Option<vk::Queue>>, - swapchain: RefCell<Option<Swapchain>>, + window_dressing: RefCell<Option<WindowDressing>>, - render_pass: RefCell<Option<vk::RenderPass>>, - - pipeline: RefCell<Option<vk::Pipeline>>, - pipeline_layout: RefCell<Option<vk::PipelineLayout>>, + frame_index: usize, +} - framebuffers: RefCell<Option<Vec<vk::Framebuffer>>>, +// Of the various Vulkan graphics objects, the WindowDressing consists of +// the ones which need to be regenerated or modified when the window changes +// in certain ways, such as resizing. +#[derive(Debug)] +struct WindowDressing { + swapchain: Swapchain, - command_pool: RefCell<Option<vk::CommandPool>>, - command_buffers: RefCell<Option<Vec<vk::CommandBuffer>>>, + render_pass: vk::RenderPass, - concurrency: RefCell<Option<Concurrency>>, + pipeline: vk::Pipeline, + pipeline_layout: vk::PipelineLayout, - frame_index: usize, -} + framebuffers: Vec<vk::Framebuffer>, -struct InstanceCreation { - instance: Instance, - debug_messager: Option<vk::DebugUtilsMessengerEXT>, - enable_portability: bool, - enable_validation: bool, -} + command_pool: vk::CommandPool, + command_buffers: Vec<vk::CommandBuffer>, -struct DeviceCreation { - physical_device: vk::PhysicalDevice, - device: Device, - indices: QueueFamilyIndices, - graphics_queue: vk::Queue, - presentation_queue: vk::Queue, - enable_swapchain: bool, + concurrency: Concurrency, } // A swapchain is the generalized facility that is used to implement @@ -188,7 +180,25 @@ struct Concurrency { // frame fence is in the "signaled" state. It will be reset right before // submitting the queue, then signaled again when the submission completes. frame_fences: Vec<vk::Fence>, - image_fences: RefCell<Vec<vk::Fence>>, + image_fences: Vec<vk::Fence>, +} + +// This struct exists for a single use, returning from init_vulkan_instance(). +struct InstanceCreation { + instance: Instance, + debug_messager: Option<vk::DebugUtilsMessengerEXT>, + enable_portability: bool, + enable_validation: bool, +} + +// This struct exists for a single use, returning from init_vulkan_device(). +struct DeviceCreation { + physical_device: vk::PhysicalDevice, + device: Device, + indices: QueueFamilyIndices, + graphics_queue: vk::Queue, + presentation_queue: vk::Queue, + enable_swapchain: bool, } impl Surreality { @@ -202,14 +212,7 @@ impl Surreality { device: RefCell::new(None), graphics_queue: RefCell::new(None), presentation_queue: RefCell::new(None), - swapchain: RefCell::new(None), - render_pass: RefCell::new(None), - pipeline: RefCell::new(None), - pipeline_layout: RefCell::new(None), - framebuffers: RefCell::new(None), - command_pool: RefCell::new(None), - command_buffers: RefCell::new(None), - concurrency: RefCell::new(None), + window_dressing: RefCell::new(None), frame_index: 0, } } @@ -262,14 +265,16 @@ impl Surreality { let concurrency = Self::init_concurrency(&device, &swapchain.images)?; - *self.swapchain.get_mut() = Some(swapchain); - *self.render_pass.get_mut() = Some(render_pass); - *self.pipeline.get_mut() = Some(pipeline); - *self.pipeline_layout.get_mut() = Some(pipeline_layout); - *self.framebuffers.get_mut() = Some(framebuffers); - *self.command_pool.borrow_mut() = Some(command_pool); - *self.command_buffers.borrow_mut() = Some(command_buffers); - *self.concurrency.get_mut() = Some(concurrency); + *self.window_dressing.get_mut() = Some(WindowDressing { + swapchain, + render_pass, + pipeline, + pipeline_layout, + framebuffers, + command_pool, + command_buffers, + concurrency, + }); } *self.window.get_mut() = Some(window); @@ -1030,7 +1035,7 @@ impl Surreality { image_available_semaphores, rendering_finished_semaphores, frame_fences, - image_fences: RefCell::new(image_fences), + image_fences: image_fences, }) } @@ -1315,55 +1320,51 @@ impl Surreality { let graphics_queue = graphics_queue.as_ref().unwrap(); let presentation_queue = self.presentation_queue.borrow(); let presentation_queue = presentation_queue.as_ref().unwrap(); - let swapchain = self.swapchain.borrow(); - let swapchain = swapchain.as_ref().unwrap().swapchain; - let command_buffers = self.command_buffers.borrow(); + let mut window_dressing = self.window_dressing.borrow_mut(); + let window_dressing = window_dressing.as_mut().unwrap(); + let swapchain = &window_dressing.swapchain.swapchain; + let command_buffers = &window_dressing.command_buffers; let frame_index = self.frame_index; + let concurrency = &mut window_dressing.concurrency; let image_available_semaphore - = self.concurrency.borrow().as_ref().unwrap() - .image_available_semaphores[frame_index]; + = &concurrency.image_available_semaphores[frame_index]; let rendering_finished_semaphore - = self.concurrency.borrow().as_ref().unwrap() - .rendering_finished_semaphores[frame_index]; - let frame_fence = self.concurrency.borrow().as_ref().unwrap() - .frame_fences[frame_index]; + = &concurrency.rendering_finished_semaphores[frame_index]; + let frame_fence = &concurrency.frame_fences[frame_index]; - unsafe { device.wait_for_fences(&[frame_fence], true, u64::MAX) }?; + unsafe { device.wait_for_fences(&[*frame_fence], true, u64::MAX) }?; let image_index = unsafe { - device.acquire_next_image_khr(swapchain, u64::MAX, - image_available_semaphore, + device.acquire_next_image_khr(*swapchain, u64::MAX, + *image_available_semaphore, vk::Fence::null()) }?.0 as usize; - let image_fence = self.concurrency.borrow().as_ref().unwrap() - .image_fences.borrow()[image_index]; + let image_fence = &concurrency.image_fences[image_index]; if !image_fence.is_null() { - unsafe { device.wait_for_fences(&[image_fence], true, u64::MAX) }?; + unsafe { device.wait_for_fences(&[*image_fence], true, u64::MAX) }?; } - if let Some(concurrency) = self.concurrency.borrow().as_ref() { - concurrency.image_fences.borrow_mut()[image_index] = frame_fence; - } + concurrency.image_fences[image_index] = *frame_fence; - let first_semaphores = [image_available_semaphore]; - let second_semaphores = [rendering_finished_semaphore]; + let first_semaphores = [*image_available_semaphore]; + let second_semaphores = [*rendering_finished_semaphore]; let wait_stages = [vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT]; - let command_buffers = [command_buffers.as_ref().unwrap()[image_index]]; + let command_buffers = [command_buffers[image_index]]; let submit_info = vk::SubmitInfo::builder() .wait_semaphores(&first_semaphores) .wait_dst_stage_mask(&wait_stages) .command_buffers(&command_buffers) .signal_semaphores(&second_semaphores); - unsafe { device.reset_fences(&[frame_fence]) }?; + unsafe { device.reset_fences(&[*frame_fence]) }?; unsafe { device.queue_submit(*graphics_queue, &[submit_info], - frame_fence) + *frame_fence) }?; - let swapchains = [swapchain]; + let swapchains = [*swapchain]; let image_indices = [image_index as u32]; let present_info = vk::PresentInfoKHR::builder() .wait_semaphores(&second_semaphores) @@ -1387,7 +1388,10 @@ impl Drop for Surreality { if let Some(device) = self.device.replace(None) { unsafe { device.device_wait_idle() }.unwrap(); - if let Some(concurrency) = self.concurrency.replace(None) { + if let Some(window_dressing) = self.window_dressing.replace(None) { + let concurrency = window_dressing.concurrency; + let swapchain = window_dressing.swapchain; + for semaphore in concurrency.image_available_semaphores { unsafe { device.destroy_semaphore(semaphore, None) }; } @@ -1399,31 +1403,28 @@ impl Drop for Surreality { for fence in concurrency.frame_fences { unsafe { device.destroy_fence(fence, None) }; } - } - if let Some(command_pool) = self.command_pool.replace(None) { - unsafe { device.destroy_command_pool(command_pool, None) }; - } + unsafe { + device.destroy_command_pool(window_dressing.command_pool, None) + }; - if let Some(framebuffers) = self.framebuffers.replace(None) { - for framebuffer in framebuffers { + for framebuffer in window_dressing.framebuffers { unsafe { device.destroy_framebuffer(framebuffer, None) }; } - } - if let Some(pipeline) = self.pipeline.replace(None) { - unsafe { device.destroy_pipeline(pipeline, None) }; - } + unsafe { + device.destroy_pipeline(window_dressing.pipeline, None) + }; - if let Some(render_pass) = self.render_pass.replace(None) { - unsafe { device.destroy_render_pass(render_pass, None) }; - } + unsafe { + device.destroy_render_pass(window_dressing.render_pass, None) + }; - if let Some(pipeline_layout) = self.pipeline_layout.replace(None) { - unsafe { device.destroy_pipeline_layout(pipeline_layout, None) }; - } + unsafe { + device.destroy_pipeline_layout( + window_dressing.pipeline_layout, None) + }; - if let Some(swapchain) = self.swapchain.replace(None) { for view in swapchain.image_views { unsafe { device.destroy_image_view(view, None) }; } |