diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-07-09 10:16:26 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-07-09 10:16:26 -0700 |
| commit | cd833e3d2dd46ba3d8b8e773b97ae8e850809d0a (patch) | |
| tree | a57eb934e07528bf1c6d03a6c652cd41f3c5e46d /src | |
| parent | 1397f6fdd1af7878695c40d292041d179390513a (diff) | |
removed all the bundled-up return-value structs
they are replaced with tuples and single-purpose bool wrappers Force-Push: yes Change-Id: Iabf9c1d3f9dddee160d556004b2a72b15cd6e574
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 123 |
1 files changed, 47 insertions, 76 deletions
diff --git a/src/main.rs b/src/main.rs index d7791d9..e181e46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,32 +176,12 @@ struct Concurrency { 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, -} +// These structs exist for use in function calling, to remove the potential +// for accidentally passing or returning one boolean as if it's another. +struct EnablePortability(bool); +struct EnableValidation(bool); +struct EnableSwapchain(bool); -// This struct exists for a single use, returning from -// find_device_swapchain_features(). -#[derive(Debug)] -struct SwapchainFeatures { - capabilities: vk::SurfaceCapabilitiesKHR, - formats: Vec<vk::SurfaceFormatKHR>, - presentation_modes: Vec<vk::PresentModeKHR>, -} impl Surreality { fn new() -> Self { @@ -223,29 +203,27 @@ impl Surreality { let window = Self::init_window(event_loop)?; let entry = Self::init_vulkan_entry()?; - let InstanceCreation { - instance, - debug_messager, + let (instance, + debug_messager, - // There are a few Vulkan features (in the informal sense of - // "feature") that we want to be able to run both with and without. - // Here, we have booleans describing which we're doing. - // - // These are only used to communicate between initialization phases; - // we don't keep them around after that. - enable_portability, - enable_validation - } = Self::init_vulkan_instance(&window, &entry)?; + // There are a few Vulkan features (in the informal sense of + // "feature") that we want to be able to run both with and without. + // Here, we have booleans describing which we're doing. + // + // These are only used to communicate between initialization + // phases; we don't keep them around after that. + enable_portability, enable_validation) + = Self::init_vulkan_instance(&window, &entry)?; let surface = Self::init_surface(&window, &instance)?; - let DeviceCreation { - physical_device, device, indices, graphics_queue, presentation_queue, - enable_swapchain - } = Self::init_vulkan_device(&instance, &surface, - enable_validation, enable_portability)?; + let (physical_device, device, + indices, graphics_queue, presentation_queue, + enable_swapchain) + = Self::init_vulkan_device(&instance, &surface, + enable_validation, enable_portability)?; - if enable_swapchain { + if enable_swapchain.0 { let swapchain = Self::init_swapchain( &window, &instance, &surface, &physical_device, &device, &indices)?; @@ -323,7 +301,8 @@ impl Surreality { #[allow(unsafe_code)] fn init_vulkan_instance(window: &Window, entry: &Entry) - -> Result<InstanceCreation> + -> Result<(Instance, Option<vk::DebugUtilsMessengerEXT>, + EnablePortability, EnableValidation)> { let enable_validation = cfg!(feature = "vulkan-validation") || cfg!(debug_assertions); @@ -375,12 +354,12 @@ impl Surreality { vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name.as_ptr()); flags.insert(vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR); - true + EnablePortability(true) } else { - false + EnablePortability(false) } } else { - false + EnablePortability(false) }; // Request the LunarG validation layer, when appropriate. @@ -390,15 +369,15 @@ impl Surreality { if available_layers.contains(&validation_layer_name) { layers.push(validation_layer_name.as_ptr()); - true + EnableValidation(true) } else { eprintln!("Vulkan validation requested at build time, \ but no validation layer available."); - false + EnableValidation(false) } } else { - false + EnableValidation(false) }; // Request the debug extension. This is the first of three bits of code @@ -471,12 +450,7 @@ impl Surreality { None }; - Ok(InstanceCreation { - instance, - debug_messager, - enable_portability, - enable_validation, - }) + Ok((instance, debug_messager, enable_portability, enable_validation)) } // TODO this is so short that it can likely be eliminated @@ -501,8 +475,10 @@ impl Surreality { #[allow(unsafe_code)] fn init_vulkan_device(instance: &Instance, surface: &vk::SurfaceKHR, - enable_validation: bool, enable_portability: bool) - -> Result<DeviceCreation> + enable_validation: EnableValidation, + enable_portability: EnablePortability) + -> Result<(vk::PhysicalDevice, Device, QueueFamilyIndices, vk::Queue, + vk::Queue, EnableSwapchain)> { let physical_device = Self::pick_vulkan_device(instance, surface)?; @@ -533,7 +509,7 @@ impl Surreality { let validation_layer_name = vk::ExtensionName::from_bytes( b"VK_LAYER_KHRONOS_validation"); - if enable_validation { + if enable_validation.0 { // It's not concerning if this isn't supported, because device // layers are ignored on recent versions, they're purely historical. if available_extensions.contains(&validation_layer_name) { @@ -543,7 +519,7 @@ impl Surreality { let portability_extension_name = vk::ExtensionName::from_bytes( b"VK_KHR_portability_subset"); - if enable_portability { + if enable_portability.0 { // This is untested, since the only scenario where it would come up // is on a Mac, which we don't actually support. Sorry, and good luck. if available_extensions.contains(&portability_extension_name) { @@ -567,12 +543,12 @@ impl Surreality { { extensions.push(swapchain_extension_name.as_ptr()); - true + EnableSwapchain(true) } else { - false + EnableSwapchain(false) } } else { - false + EnableSwapchain(false) }; // We have one or more queue family indices; we don't know a priori @@ -617,14 +593,9 @@ impl Surreality { device.get_device_queue(indices.presentation, 0) }; - Ok(DeviceCreation { - physical_device, - device, - indices, - graphics_queue, - presentation_queue, - enable_swapchain - }) + Ok((physical_device, device, + indices, graphics_queue, presentation_queue, + enable_swapchain)) } #[allow(unsafe_code)] @@ -634,7 +605,7 @@ impl Surreality { indices: &QueueFamilyIndices) -> Result<Swapchain> { - let SwapchainFeatures { capabilities, formats, presentation_modes } + let (capabilities, formats, presentation_modes) = Self::find_device_swapchain_features( instance, surface, physical_device)?.require()?; @@ -1226,7 +1197,9 @@ impl Surreality { fn find_device_swapchain_features(instance: &Instance, surface: &vk::SurfaceKHR, physical_device: &vk::PhysicalDevice) - -> Result<Acceptable<SwapchainFeatures>> + -> Result<Acceptable<(vk::SurfaceCapabilitiesKHR, + Vec<vk::SurfaceFormatKHR>, + Vec<vk::PresentModeKHR>)>> { let capabilities = unsafe { instance.get_physical_device_surface_capabilities_khr( @@ -1246,9 +1219,7 @@ impl Surreality { } else if presentation_modes.is_empty() { Ok(Acceptable::Rejected("No matching presentation modes.".to_string())) } else { - Ok(Acceptable::Accepted(SwapchainFeatures { - capabilities, formats, presentation_modes - })) + Ok(Acceptable::Accepted((capabilities, formats, presentation_modes))) } } |