From 7316a1678fa5faac3b61cf4c6c44c0a6a803af39 Mon Sep 17 00:00:00 2001 From: Zakarum Date: Wed, 27 Oct 2021 14:40:32 +0300 Subject: [PATCH] fix: clippy --- proc/src/descriptors/buffer.rs | 10 ++-- .../src/descriptors/combined_image_sampler.rs | 4 +- proc/src/descriptors/instance.rs | 4 +- proc/src/descriptors/layout.rs | 1 - proc/src/pass/parse.rs | 27 ++++----- proc/src/pipeline/layout.rs | 3 +- proc/src/repr/{repr.rs => generate.rs} | 0 proc/src/repr/mod.rs | 4 +- src/backend/vulkan/device.rs | 54 +++++++++--------- src/backend/vulkan/encode.rs | 45 ++++++++------- src/backend/vulkan/graphics.rs | 8 ++- src/backend/vulkan/physical.rs | 39 ++++++------- src/backend/vulkan/queue.rs | 6 +- src/backend/vulkan/resources.rs | 43 ++++++-------- src/backend/vulkan/surface.rs | 2 +- src/backend/vulkan/swapchain.rs | 12 +--- src/descriptor/mod.rs | 22 ++++---- src/encode.rs | 2 +- src/format.rs | 30 +++++----- src/pipeline/graphics.rs | 5 +- src/repr/array.rs | 9 +-- src/repr/mat.rs | 31 +++++----- src/repr/mod.rs | 55 +++++++++++++++++- src/repr/repr.rs | 56 ------------------- src/shader.rs | 2 +- src/view.rs | 6 +- 26 files changed, 226 insertions(+), 254 deletions(-) rename proc/src/repr/{repr.rs => generate.rs} (100%) delete mode 100644 src/repr/repr.rs diff --git a/proc/src/descriptors/buffer.rs b/proc/src/descriptors/buffer.rs index eda4b1f..6bfe6de 100644 --- a/proc/src/descriptors/buffer.rs +++ b/proc/src/descriptors/buffer.rs @@ -3,7 +3,7 @@ use crate::{find_unique, get_unique}; #[derive(Clone)] pub struct Buffer { pub kind: Kind, - pub ty: syn::Type, + pub ty: Box, } impl Buffer { @@ -21,7 +21,7 @@ pub enum Kind { enum Argument { Kind(Kind), - Type(syn::Type), + Type(Box), } pub(super) fn parse_buffer_attr(attr: &syn::Attribute) -> syn::Result> { @@ -43,11 +43,9 @@ pub(super) fn parse_buffer_attr(attr: &syn::Attribute) -> syn::Result()?; let ty = stream.parse::()?; - Ok(Argument::Type(ty)) - } - _ => { - return Err(stream.error("Unrecognized argument")); + Ok(Argument::Type(Box::new(ty))) } + _ => Err(stream.error("Unrecognized argument")), } })?; diff --git a/proc/src/descriptors/combined_image_sampler.rs b/proc/src/descriptors/combined_image_sampler.rs index 54228e9..b4d98da 100644 --- a/proc/src/descriptors/combined_image_sampler.rs +++ b/proc/src/descriptors/combined_image_sampler.rs @@ -40,8 +40,8 @@ pub(super) fn parse_combined_image_sampler_attr( })?; let sampler = get_unique( - args.iter().filter_map(|arg| match arg { - AttributeArgument::SeparateSampler { member } => Some(member.clone()), + args.iter().map(|arg| match arg { + AttributeArgument::SeparateSampler { member } => member.clone(), }), attr, "Argument with sampler member must be specified", diff --git a/proc/src/descriptors/instance.rs b/proc/src/descriptors/instance.rs index 3fc54d5..7250525 100644 --- a/proc/src/descriptors/instance.rs +++ b/proc/src/descriptors/instance.rs @@ -33,7 +33,7 @@ pub(super) fn generate(input: &Input) -> TokenStream { let update_descriptor_statements: TokenStream = input .descriptors .iter() - .filter_map(|input| { + .map(|input| { let field = &input.member; let descriptor_field = quote::format_ident!("descriptor_{}", input.member); @@ -52,7 +52,7 @@ pub(super) fn generate(input: &Input) -> TokenStream { } ); - Some(stream) + stream }) .collect(); diff --git a/proc/src/descriptors/layout.rs b/proc/src/descriptors/layout.rs index fe34ded..1c572f1 100644 --- a/proc/src/descriptors/layout.rs +++ b/proc/src/descriptors/layout.rs @@ -101,7 +101,6 @@ pub(super) fn generate(input: &Input) -> TokenStream { } } ) - .into() } fn generate_layout_binding(descriptor: &Descriptor, binding: u32) -> TokenStream { diff --git a/proc/src/pass/parse.rs b/proc/src/pass/parse.rs index f6317ef..1a69053 100644 --- a/proc/src/pass/parse.rs +++ b/proc/src/pass/parse.rs @@ -12,14 +12,14 @@ pub struct Input { #[derive(Clone)] pub enum Layout { - Expr(syn::Expr), - Member(syn::Member), + Expr(Box), + Member(Box), } #[derive(Clone)] pub enum ClearValue { - Expr(syn::Expr), - Member(syn::Member), + Expr(Box), + Member(Box), } #[derive(Clone)] @@ -53,11 +53,8 @@ impl Attachment { } _ => {} } - match &self.store_op { - StoreOp::Store(Layout::Member(layout)) => { - validate_member(layout, item_struct)?; - } - _ => {} + if let StoreOp::Store(Layout::Member(layout)) = &self.store_op { + validate_member(layout, item_struct)?; } Ok(()) } @@ -313,10 +310,10 @@ fn parse_attachment_attr(attr: &syn::Attribute) -> syn::Result()?; let expr = value.parse::()?; - ClearValue::Expr(expr) + ClearValue::Expr(Box::new(expr)) } else { let member = value.parse::()?; - ClearValue::Member(member) + ClearValue::Member(Box::new(member)) }; Ok(AttachmentAttributeArgument::LoadOp(LoadOp::Clear(value))) @@ -328,10 +325,10 @@ fn parse_attachment_attr(attr: &syn::Attribute) -> syn::Result()?; let expr = value.parse::()?; - Layout::Expr(expr) + Layout::Expr(Box::new(expr)) } else { let member = value.parse::()?; - Layout::Member(member) + Layout::Member(Box::new(member)) }; Ok(AttachmentAttributeArgument::LoadOp(LoadOp::Load(layout))) @@ -343,10 +340,10 @@ fn parse_attachment_attr(attr: &syn::Attribute) -> syn::Result()?; let expr = value.parse::()?; - Layout::Expr(expr) + Layout::Expr(Box::new(expr)) } else { let member = value.parse::()?; - Layout::Member(member) + Layout::Member(Box::new(member)) }; Ok(AttachmentAttributeArgument::StoreOp(StoreOp::Store(layout))) diff --git a/proc/src/pipeline/layout.rs b/proc/src/pipeline/layout.rs index b499496..4680121 100644 --- a/proc/src/pipeline/layout.rs +++ b/proc/src/pipeline/layout.rs @@ -142,7 +142,7 @@ pub(super) fn generate(input: &Input) -> TokenStream { D: ::sierra::UpdatedPipelineDescriptors, { let raw: &_ = encoder.scope().to_scope(::std::clone::Clone::clone(::sierra::UpdatedDescriptors::raw(updated_descriptors))); - + encoder.bind_ray_tracing_descriptor_sets( &self.pipeline_layout, D::N, @@ -186,5 +186,4 @@ pub(super) fn generate(input: &Input) -> TokenStream { #pipeline_descriptors ) - .into() } diff --git a/proc/src/repr/repr.rs b/proc/src/repr/generate.rs similarity index 100% rename from proc/src/repr/repr.rs rename to proc/src/repr/generate.rs diff --git a/proc/src/repr/mod.rs b/proc/src/repr/mod.rs index 0ef6291..c87613b 100644 --- a/proc/src/repr/mod.rs +++ b/proc/src/repr/mod.rs @@ -1,5 +1,5 @@ +mod generate; mod parse; -mod repr; use proc_macro2::TokenStream; @@ -8,7 +8,7 @@ pub fn shader_repr(attr: proc_macro::TokenStream, item: proc_macro::TokenStream) Ok(input) => { let struct_item = &input.item_struct; std::iter::once(quote::quote!(#struct_item)) - .chain(Some(repr::generate_repr(&input))) + .chain(Some(generate::generate_repr(&input))) // .chain(Some(generate_glsl_type(&input))) .collect::() } diff --git a/src/backend/vulkan/device.rs b/src/backend/vulkan/device.rs index 09d31b7..874e711 100644 --- a/src/backend/vulkan/device.rs +++ b/src/backend/vulkan/device.rs @@ -1,3 +1,5 @@ +use ordered_float::OrderedFloat; + use { super::{ access::supported_access, @@ -401,7 +403,7 @@ impl Device { address, buffer_index, block, - memory_usage.unwrap_or(MemoryUsage::empty()), + memory_usage.unwrap_or_else(MemoryUsage::empty), )) } @@ -878,7 +880,7 @@ impl Device { .create_graphics_pipelines(None, &[builder], None) } .result() - .map_err(|err| oom_error_from_erupt(err))?; + .map_err(oom_error_from_erupt)?; debug_assert_eq!(pipelines.len(), 1); @@ -923,7 +925,7 @@ impl Device { ) } .result() - .map_err(|err| oom_error_from_erupt(err))?; + .map_err(oom_error_from_erupt)?; debug_assert_eq!(pipelines.len(), 1); @@ -1027,7 +1029,7 @@ impl Device { self.inner .allocator .lock() - .dealloc(EruptMemoryDevice::wrap(&self.logical()), block); + .dealloc(EruptMemoryDevice::wrap(self.logical()), block); let handle = self.inner.images.lock().remove(index); self.inner.logical.destroy_image(Some(handle), None); @@ -1280,14 +1282,13 @@ impl Device { } else { None } - .and_then(|c| c.try_into().ok()) - .ok_or_else(|| { + .ok_or( CreateRenderPassError::ColorAttachmentReferenceOutOfBound { subpass: si, index: ci, attachment: c, } - })?) + )?) .layout(cl.to_erupt()) ) }) @@ -1304,13 +1305,12 @@ impl Device { } else { None } - .and_then(|d| d.try_into().ok()) - .ok_or_else(|| { + .ok_or( CreateRenderPassError::DepthAttachmentReferenceOutOfBound { subpass: si, attachment: d, - } - })?, + }, + )?, ) .layout(dl.to_erupt()), ); @@ -1354,16 +1354,8 @@ impl Device { .iter() .map(|d| { vk1_0::SubpassDependencyBuilder::new() - .src_subpass( - d.src - .map(|s| s.try_into().expect("Subpass index out of bound")) - .unwrap_or(vk1_0::SUBPASS_EXTERNAL), - ) - .dst_subpass( - d.dst - .map(|s| s.try_into().expect("Subpass index out of bound")) - .unwrap_or(vk1_0::SUBPASS_EXTERNAL), - ) + .src_subpass(d.src.unwrap_or(vk1_0::SUBPASS_EXTERNAL)) + .dst_subpass(d.dst.unwrap_or(vk1_0::SUBPASS_EXTERNAL)) .src_stage_mask(d.src_stages.to_erupt()) .dst_stage_mask(d.dst_stages.to_erupt()) .src_access_mask(supported_access(d.src_stages.to_erupt())) @@ -1499,7 +1491,7 @@ impl Device { } }; - if code.len() == 0 { + if code.is_empty() { return Err(CreateShaderModuleError::InvalidShader { source: InvalidShader::EmptySource, }); @@ -1582,7 +1574,7 @@ impl Device { /// Only one swapchain may be associated with one surface. #[tracing::instrument] pub fn create_swapchain(&self, surface: &mut Surface) -> Result { - Ok(Swapchain::new(surface, self)?) + Swapchain::new(surface, self) } pub(super) fn insert_swapchain(&self, swapchain: vksw::SwapchainKHR) -> usize { @@ -2166,10 +2158,10 @@ impl Device { .bindings(&bindings) .flags(info.flags.to_erupt()); - let mut flags = vk1_2::DescriptorSetLayoutBindingFlagsCreateInfoBuilder::new() + let flags = vk1_2::DescriptorSetLayoutBindingFlagsCreateInfoBuilder::new() .binding_flags(&flags); - create_info = create_info.extend_from(&mut flags); + create_info = create_info.extend_from(&flags); self.inner .logical @@ -2228,7 +2220,7 @@ impl Device { EruptDescriptorDevice::wrap(&self.inner.logical), &info.layout.handle(), flags, - &info.layout.total_count(), + info.layout.total_count(), 1, ) } @@ -2510,8 +2502,8 @@ impl Device { *acc_structure_write = vkacc::WriteDescriptorSetAccelerationStructureKHRBuilder::new() - .acceleration_structures(&acceleration_structures[range.clone()]); - write.extend_from(&mut *acc_structure_write) + .acceleration_structures(&acceleration_structures[range]); + write.extend_from(&*acc_structure_write) } } }) @@ -2536,7 +2528,11 @@ impl Device { .address_mode_w(info.address_mode_w.to_erupt()) .mip_lod_bias(info.mip_lod_bias.into_inner()) .anisotropy_enable(info.max_anisotropy.is_some()) - .max_anisotropy(info.max_anisotropy.unwrap_or(0.0.into()).into_inner()) + .max_anisotropy( + info.max_anisotropy + .unwrap_or(OrderedFloat(0.0)) + .into_inner(), + ) .compare_enable(info.compare_op.is_some()) .compare_op(match info.compare_op { Some(compare_op) => compare_op.to_erupt(), diff --git a/src/backend/vulkan/encode.rs b/src/backend/vulkan/encode.rs index 04d4692..de5bc60 100644 --- a/src/backend/vulkan/encode.rs +++ b/src/backend/vulkan/encode.rs @@ -126,7 +126,7 @@ impl CommandBuffer { let pass = &framebuffer.info().render_pass; - let mut clears = clears.into_iter(); + let mut clears = clears.iter(); let clear_values = scope.to_scope_from_iter( pass .info() @@ -137,25 +137,26 @@ impl CommandBuffer { if attachment.load_op == LoadOp::Clear { let clear = clears.next().expect("Not enough clear values"); - match clear { - &ClearValue::Color(r, g, b, a) => vk1_0::ClearValue { - color: match attachment.format.description() { - R(repr)|RG(repr)|RGB(repr)|RGBA(repr)|BGR(repr)|BGRA(repr) => colors_f32_to_value(r, g, b, a, repr), - _ => panic!("Attempt to clear depth-stencil attachment with color value"), + match *clear { + ClearValue::Color(r, g, b, a) => vk1_0::ClearValue { + color: match attachment.format.description() { + R(repr)|RG(repr)|RGB(repr)|RGBA(repr)|BGR(repr)|BGRA(repr) => colors_f32_to_value(r, g, b, a, repr), + _ => panic!("Attempt to clear depth-stencil attachment with color value"), + } + }, + ClearValue::DepthStencil(depth, stencil) => { + assert!( + attachment.format.is_depth() + || attachment.format.is_stencil() + ); + vk1_0::ClearValue { + depth_stencil: vk1_0::ClearDepthStencilValue { + depth, + stencil, + }, + } } - }, - &ClearValue::DepthStencil(depth, stencil) => { - assert!( - attachment.format.is_depth() - || attachment.format.is_stencil() - ); - vk1_0::ClearValue { - depth_stencil: vk1_0::ClearDepthStencilValue { - depth, - stencil, - }, - } - }} + } } else { vk1_0::ClearValue { color: vk1_0::ClearColorValue { @@ -425,15 +426,13 @@ impl CommandBuffer { )); let build_offsets = &*scope.to_scope_from_iter( - offsets_per_info - .into_iter() - .map(|&offsets| offsets.as_ptr()), + offsets_per_info.iter().map(|&offsets| offsets.as_ptr()), ); unsafe { device.logical().cmd_build_acceleration_structures_khr( self.handle, - &build_infos, + &*build_infos, build_offsets, ) } diff --git a/src/backend/vulkan/graphics.rs b/src/backend/vulkan/graphics.rs index b088b27..0fe9a6b 100644 --- a/src/backend/vulkan/graphics.rs +++ b/src/backend/vulkan/graphics.rs @@ -476,9 +476,11 @@ impl Graphics { } unsafe { - let mut info = Win32SurfaceCreateInfoKHR::default(); - info.hinstance = handle.hinstance; - info.hwnd = handle.hwnd; + let info = Win32SurfaceCreateInfoKHR { + hinstance: handle.hinstance, + hwnd: handle.hwnd, + ..Default::default() + }; self.instance.create_win32_surface_khr(&info, None) } .result() diff --git a/src/backend/vulkan/physical.rs b/src/backend/vulkan/physical.rs index 02332e2..baa5028 100644 --- a/src/backend/vulkan/physical.rs +++ b/src/backend/vulkan/physical.rs @@ -253,10 +253,8 @@ impl PhysicalDevice { if self.features.sbl.scalar_block_layout > 0 { features.push(Feature::ScalarBlockLayout); } - } else if graphics.instance.enabled().vk1_2 { - if self.features.v12.scalar_block_layout > 0 { - features.push(Feature::ScalarBlockLayout); - } + } else if graphics.instance.enabled().vk1_2 && self.features.v12.scalar_block_layout > 0 { + features.push(Feature::ScalarBlockLayout); } if self @@ -399,10 +397,8 @@ impl PhysicalDevice { if self.features.bda.buffer_device_address > 0 { features.push(Feature::BufferDeviceAddress); } - } else if graphics.instance.enabled().vk1_2 { - if self.features.v12.buffer_device_address > 0 { - features.push(Feature::BufferDeviceAddress); - } + } else if graphics.instance.enabled().vk1_2 && self.features.v12.buffer_device_address > 0 { + features.push(Feature::BufferDeviceAddress); } if self @@ -436,7 +432,12 @@ impl PhysicalDevice { vk1_0::PhysicalDeviceType::INTEGRATED_GPU => Some(DeviceKind::Integrated), vk1_0::PhysicalDeviceType::DISCRETE_GPU => Some(DeviceKind::Discrete), vk1_0::PhysicalDeviceType::CPU => Some(DeviceKind::Software), - vk1_0::PhysicalDeviceType::OTHER | vk1_0::PhysicalDeviceType::VIRTUAL_GPU | _ => { + vk1_0::PhysicalDeviceType::OTHER | vk1_0::PhysicalDeviceType::VIRTUAL_GPU => None, + _ => { + tracing::error!( + "Unexpected device type value: {:?}", + self.properties.v10.device_type + ); None } }, @@ -522,7 +523,7 @@ impl PhysicalDevice { return Err(CreateDeviceError::BadFamiliesRequested); } - let priorities = families_requested.entry(family).or_insert(Vec::new()); + let priorities = families_requested.entry(family).or_insert_with(Vec::new); if arith_gt( priorities.len() + count, @@ -551,7 +552,7 @@ impl PhysicalDevice { // Collect requested features. let mut features2 = vk1_1::PhysicalDeviceFeatures2Builder::new(); - let mut features11 = vk1_2::PhysicalDeviceVulkan11FeaturesBuilder::new(); + let features11 = vk1_2::PhysicalDeviceVulkan11FeaturesBuilder::new(); let mut features12 = vk1_2::PhysicalDeviceVulkan12FeaturesBuilder::new(); let mut features_sbl = sbl::PhysicalDeviceScalarBlockLayoutFeaturesEXTBuilder::new(); let mut features_edi = edi::PhysicalDeviceDescriptorIndexingFeaturesEXTBuilder::new(); @@ -977,39 +978,39 @@ impl PhysicalDevice { // Push structure to the list if at least one feature is enabled. if include_features_sbl { push_ext(EXT_SCALAR_BLOCK_LAYOUT_EXTENSION_NAME); - device_create_info = device_create_info.extend_from(&mut features_sbl); + device_create_info = device_create_info.extend_from(&features_sbl); } if include_features_edi { push_ext(EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME); - device_create_info = device_create_info.extend_from(&mut features_edi); + device_create_info = device_create_info.extend_from(&features_edi); } if include_features_bda { push_ext(KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME); - device_create_info = device_create_info.extend_from(&mut features_bda); + device_create_info = device_create_info.extend_from(&features_bda); } if include_features_acc { push_ext(KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME); push_ext(KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME); - device_create_info = device_create_info.extend_from(&mut features_acc); + device_create_info = device_create_info.extend_from(&features_acc); } if include_features_rt { push_ext(KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME); - device_create_info = device_create_info.extend_from(&mut features_rt); + device_create_info = device_create_info.extend_from(&features_rt); } if include_features12 { - device_create_info = device_create_info.extend_from(&mut features12); + device_create_info = device_create_info.extend_from(&features12); } if include_features11 { - device_create_info = device_create_info.extend_from(&mut features11); + device_create_info = device_create_info.extend_from(&features11); } - device_create_info = device_create_info.extend_from(&mut features2); + device_create_info = device_create_info.extend_from(&features2); } device_create_info = device_create_info.enabled_extension_names(&enable_exts); diff --git a/src/backend/vulkan/queue.rs b/src/backend/vulkan/queue.rs index c50ce1b..294f8cf 100644 --- a/src/backend/vulkan/queue.rs +++ b/src/backend/vulkan/queue.rs @@ -151,9 +151,9 @@ impl Queue { .queue_submit( self.handle, &[vk1_0::SubmitInfoBuilder::new() - .wait_semaphores(&wait_semaphores) - .wait_dst_stage_mask(&wait_stages) - .signal_semaphores(&signal_semaphores) + .wait_semaphores(&*wait_semaphores) + .wait_dst_stage_mask(&*wait_stages) + .signal_semaphores(&*signal_semaphores) .command_buffers(&*handles)], fence.map(|f| f.handle()), ) diff --git a/src/backend/vulkan/resources.rs b/src/backend/vulkan/resources.rs index 9e768cf..cdfe466 100644 --- a/src/backend/vulkan/resources.rs +++ b/src/backend/vulkan/resources.rs @@ -295,19 +295,17 @@ impl Drop for ImageInner { fn drop(&mut self) { resource_freed(); - match &mut self.flavor { - ImageFlavor::DeviceImage { - memory_block, - index, - } => { - if let Some(device) = self.owner.upgrade() { - unsafe { - let block = ManuallyDrop::take(memory_block); - device.destroy_image(*index, block); - } + if let ImageFlavor::DeviceImage { + memory_block, + index, + } = &mut self.flavor + { + if let Some(device) = self.owner.upgrade() { + unsafe { + let block = ManuallyDrop::take(memory_block); + device.destroy_image(*index, block); } } - _ => {} } } } @@ -337,15 +335,13 @@ impl Debug for Image { .field("owner", &self.inner.owner) .field("handle", &self.handle); - match &self.inner.flavor { - ImageFlavor::DeviceImage { - memory_block, - index, - } => { - fmt.field("memory_block", &**memory_block) - .field("index", index); - } - _ => {} + if let ImageFlavor::DeviceImage { + memory_block, + index, + } = &self.inner.flavor + { + fmt.field("memory_block", &**memory_block) + .field("index", index); } fmt.finish() @@ -534,11 +530,8 @@ impl Drop for Fence { resource_freed(); if let Some(device) = self.owner.upgrade() { - match self.state { - FenceState::Armed { .. } => { - device.wait_fences(&mut [self], true); - } - _ => {} + if let FenceState::Armed { .. } = self.state { + device.wait_fences(&mut [self], true); } unsafe { device.destroy_fence(self.index) } } diff --git a/src/backend/vulkan/surface.rs b/src/backend/vulkan/surface.rs index 6aa1a17..08ec80e 100644 --- a/src/backend/vulkan/surface.rs +++ b/src/backend/vulkan/surface.rs @@ -52,7 +52,7 @@ impl Surface { pub(crate) fn mark_used(&self) -> Result<(), SurfaceError> { if self.inner.used.fetch_or(true, Ordering::SeqCst) { - return Err(SurfaceError::AlreadyUsed); + Err(SurfaceError::AlreadyUsed) } else { Ok(()) } diff --git a/src/backend/vulkan/swapchain.rs b/src/backend/vulkan/swapchain.rs index 1a5a777..7761653 100644 --- a/src/backend/vulkan/swapchain.rs +++ b/src/backend/vulkan/swapchain.rs @@ -177,10 +177,7 @@ impl Swapchain { format: Format, mode: PresentMode, ) -> Result<(), SurfaceError> { - let device = self - .device - .upgrade() - .ok_or_else(|| SurfaceError::SurfaceLost)?; + let device = self.device.upgrade().ok_or(SurfaceError::SurfaceLost)?; // TODO: Configurable count if self.retired.len() > 16 { @@ -244,7 +241,7 @@ impl Swapchain { let sf = formats .iter() .find(|sf| sf.format == erupt_format) - .ok_or_else(|| SurfaceError::FormatUnsupported { format })?; + .ok_or(SurfaceError::FormatUnsupported { format })?; let composite_alpha = { let raw = caps.supported_composite_alpha.to_erupt().bits(); @@ -372,10 +369,7 @@ impl Swapchain { } pub fn acquire_image(&mut self, optimal: bool) -> Result, SurfaceError> { - let device = self - .device - .upgrade() - .ok_or_else(|| SurfaceError::SurfaceLost)?; + let device = self.device.upgrade().ok_or(SurfaceError::SurfaceLost)?; assert!( device.logical().enabled().khr_swapchain, diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index 26aaa59..a88af6d 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -175,14 +175,14 @@ pub trait TypedDescriptor { const TYPE: DescriptorType; type Descriptor: std::hash::Hash + Eq; - fn descriptors<'a>(slice: &'a [Self::Descriptor]) -> Descriptors<'a>; + fn descriptors(slice: &[Self::Descriptor]) -> Descriptors<'_>; } impl TypedDescriptor for SamplerDescriptor { const TYPE: DescriptorType = DescriptorType::Sampler; type Descriptor = Sampler; - fn descriptors<'a>(slice: &'a [Sampler]) -> Descriptors<'a> { + fn descriptors(slice: &[Sampler]) -> Descriptors<'_> { Descriptors::Sampler(slice) } } @@ -191,7 +191,7 @@ impl TypedDescriptor for CombinedImageSamplerDescriptor { const TYPE: DescriptorType = DescriptorType::CombinedImageSampler; type Descriptor = CombinedImageSampler; - fn descriptors<'a>(slice: &'a [CombinedImageSampler]) -> Descriptors<'a> { + fn descriptors(slice: &[CombinedImageSampler]) -> Descriptors<'_> { Descriptors::CombinedImageSampler(slice) } } @@ -200,7 +200,7 @@ impl TypedDescriptor for SampledImageDescriptor { const TYPE: DescriptorType = DescriptorType::SampledImage; type Descriptor = ImageViewDescriptor; - fn descriptors<'a>(slice: &'a [ImageViewDescriptor]) -> Descriptors<'a> { + fn descriptors(slice: &[ImageViewDescriptor]) -> Descriptors<'_> { Descriptors::SampledImage(slice) } } @@ -209,7 +209,7 @@ impl TypedDescriptor for StorageImageDescriptor { const TYPE: DescriptorType = DescriptorType::StorageImage; type Descriptor = ImageViewDescriptor; - fn descriptors<'a>(slice: &'a [ImageViewDescriptor]) -> Descriptors<'a> { + fn descriptors(slice: &[ImageViewDescriptor]) -> Descriptors<'_> { Descriptors::StorageImage(slice) } } @@ -218,7 +218,7 @@ impl TypedDescriptor for UniformBufferDescriptor { const TYPE: DescriptorType = DescriptorType::UniformBuffer; type Descriptor = BufferRange; - fn descriptors<'a>(slice: &'a [BufferRange]) -> Descriptors<'a> { + fn descriptors(slice: &[BufferRange]) -> Descriptors<'_> { Descriptors::UniformBuffer(slice) } } @@ -227,7 +227,7 @@ impl TypedDescriptor for StorageBufferDescriptor { const TYPE: DescriptorType = DescriptorType::StorageBuffer; type Descriptor = BufferRange; - fn descriptors<'a>(slice: &'a [BufferRange]) -> Descriptors<'a> { + fn descriptors(slice: &[BufferRange]) -> Descriptors<'_> { Descriptors::StorageBuffer(slice) } } @@ -236,7 +236,7 @@ impl TypedDescriptor for UniformBufferDynamicDescriptor { const TYPE: DescriptorType = DescriptorType::UniformBufferDynamic; type Descriptor = BufferRange; - fn descriptors<'a>(slice: &'a [BufferRange]) -> Descriptors<'a> { + fn descriptors(slice: &[BufferRange]) -> Descriptors<'_> { Descriptors::UniformBufferDynamic(slice) } } @@ -245,7 +245,7 @@ impl TypedDescriptor for StorageBufferDynamicDescriptor { const TYPE: DescriptorType = DescriptorType::StorageBufferDynamic; type Descriptor = BufferRange; - fn descriptors<'a>(slice: &'a [BufferRange]) -> Descriptors<'a> { + fn descriptors(slice: &[BufferRange]) -> Descriptors<'_> { Descriptors::StorageBufferDynamic(slice) } } @@ -254,7 +254,7 @@ impl TypedDescriptor for InputAttachmentDescriptor { const TYPE: DescriptorType = DescriptorType::InputAttachment; type Descriptor = ImageViewDescriptor; - fn descriptors<'a>(slice: &'a [ImageViewDescriptor]) -> Descriptors<'a> { + fn descriptors(slice: &[ImageViewDescriptor]) -> Descriptors<'_> { Descriptors::InputAttachment(slice) } } @@ -263,7 +263,7 @@ impl TypedDescriptor for AccelerationStructureDescriptor { const TYPE: DescriptorType = DescriptorType::AccelerationStructure; type Descriptor = AccelerationStructure; - fn descriptors<'a>(slice: &'a [AccelerationStructure]) -> Descriptors<'a> { + fn descriptors(slice: &[AccelerationStructure]) -> Descriptors<'_> { Descriptors::AccelerationStructure(slice) } } diff --git a/src/encode.rs b/src/encode.rs index 011945b..23d7804 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -798,7 +798,7 @@ impl<'a, 'b> RenderPassEncoder<'a, 'b> { .set_viewport(self.framebuffer.info().extent.into()); } - let gp = pipeline.get(&self.render_pass, self.subpass, device)?; + let gp = pipeline.get(self.render_pass, self.subpass, device)?; self.inner.bind_graphics_pipeline(gp); Ok(()) } diff --git a/src/format.rs b/src/format.rs index 097ba9f..dde99fa 100644 --- a/src/format.rs +++ b/src/format.rs @@ -174,15 +174,15 @@ impl Format { } pub fn is_color(&self) -> bool { - match self { + !matches!( + self, Self::D16Unorm - | Self::D32Sfloat - | Self::S8Uint - | Self::D16UnormS8Uint - | Self::D24UnormS8Uint - | Self::D32SfloatS8Uint => false, - _ => true, - } + | Self::D32Sfloat + | Self::S8Uint + | Self::D16UnormS8Uint + | Self::D24UnormS8Uint + | Self::D32SfloatS8Uint + ) } pub fn color_type(&self) -> Option { @@ -198,14 +198,14 @@ impl Format { } pub fn is_depth(&self) -> bool { - match self { + matches!( + self, Self::D16Unorm - | Self::D32Sfloat - | Self::D16UnormS8Uint - | Self::D24UnormS8Uint - | Self::D32SfloatS8Uint => true, - _ => false, - } + | Self::D32Sfloat + | Self::D16UnormS8Uint + | Self::D24UnormS8Uint + | Self::D32SfloatS8Uint + ) } pub fn is_stencil(&self) -> bool { diff --git a/src/pipeline/graphics.rs b/src/pipeline/graphics.rs index f085408..b09f238 100644 --- a/src/pipeline/graphics.rs +++ b/src/pipeline/graphics.rs @@ -30,10 +30,7 @@ pub enum State { impl State { pub fn is_dynamic(&self) -> bool { - match self { - Self::Dynamic => true, - _ => false, - } + matches!(self, Self::Dynamic) } pub fn dynamic() -> Self { diff --git a/src/repr/array.rs b/src/repr/array.rs index dda7d20..00ae11e 100644 --- a/src/repr/array.rs +++ b/src/repr/array.rs @@ -1,7 +1,4 @@ -use super::{ - pad::Padded, - repr::{ShaderRepr, Std140, Std430}, -}; +use super::{pad::Padded, ShaderRepr, Std140, Std430}; macro_rules! impl_unsafe_marker_for_array { ($( $n:expr ),*) => { @@ -17,6 +14,8 @@ macro_rules! impl_unsafe_marker_for_array { type ArrayPadding = [u8; 0]; fn copy_to_repr(&self, repr: &mut [Padded; $n]) { + #![allow(clippy::reversed_empty_ranges)] + for i in 0..$n { self[i].copy_to_repr(&mut repr[i].value); } @@ -34,6 +33,8 @@ macro_rules! impl_unsafe_marker_for_array { type ArrayPadding = [u8; 0]; fn copy_to_repr(&self, repr: &mut [Padded; $n]) { + #![allow(clippy::reversed_empty_ranges)] + for i in 0..$n { self[i].copy_to_repr(&mut repr[i].value); } diff --git a/src/repr/mat.rs b/src/repr/mat.rs index 92c74fd..186ed14 100644 --- a/src/repr/mat.rs +++ b/src/repr/mat.rs @@ -1,15 +1,11 @@ use { - super::{ - native::ShaderNative, - pad::Padded, - repr::{ShaderRepr, Std140, Std430}, - vec::vec, - }, + super::{native::ShaderNative, pad::Padded, vec::vec, ShaderRepr, Std140, Std430}, bytemuck::{Pod, Zeroable}, core::{ convert::TryFrom, mem::{align_of, align_of_val, size_of, size_of_val}, }, + std::cmp::Ordering, }; /// Generic matrix type. @@ -42,15 +38,20 @@ impl TryFrom<&[T]> for mat { debug_assert_eq!(align_of_val(v), align_of::()); let len = v.len(); - if len < N * M { - debug_assert!(size_of_val(v) < size_of::()); - Err(WrongNumberOfElements::NotEnoughElements) - } else if len > N * M { - debug_assert!(size_of_val(v) > size_of::()); - Err(WrongNumberOfElements::TooManyElements) - } else { - debug_assert_eq!(size_of_val(v), size_of::()); - Ok(bytemuck::cast_slice(v)[0]) + + match len.cmp(&(N * M)) { + Ordering::Less => { + debug_assert!(size_of_val(v) < size_of::()); + Err(WrongNumberOfElements::NotEnoughElements) + } + Ordering::Greater => { + debug_assert!(size_of_val(v) > size_of::()); + Err(WrongNumberOfElements::TooManyElements) + } + Ordering::Equal => { + debug_assert_eq!(size_of_val(v), size_of::()); + Ok(bytemuck::cast_slice(v)[0]) + } } } } diff --git a/src/repr/mod.rs b/src/repr/mod.rs index 289ea97..570c655 100644 --- a/src/repr/mod.rs +++ b/src/repr/mod.rs @@ -4,12 +4,11 @@ mod array; mod mat; mod native; mod pad; -mod repr; mod scalar; mod vec; pub use { - self::{mat::*, native::*, pad::*, repr::*, scalar::*, vec::*}, + self::{mat::*, native::*, pad::*, scalar::*, vec::*}, bytemuck::{Pod, Zeroable}, }; @@ -20,3 +19,55 @@ pub const fn pad_size(align_mask: usize, offset: usize) -> usize { pub const fn next_offset(align_mask: usize, offset: usize, size: usize) -> usize { size + offset + pad_size(align_mask, offset) } + +/// Default layout for [`Repr`]. +/// Can be used for both uniforms storage buffers. +#[derive(Clone, Copy, Debug)] +pub enum Std140 {} + +/// Can be used only for storage buffers. +#[derive(Clone, Copy, Debug)] +pub enum Std430 {} + +/// Type that can be represented in shader. +pub trait ShaderRepr { + const ALIGN_MASK: usize; + const ARRAY_PADDING: usize; + + /// Type with matching layout. + type Type: Pod; + + /// Padding required after field of `Self::Type` for arrays. + type ArrayPadding: Padding; + + /// Copy data in this type into its representation. + fn copy_to_repr(&self, repr: &mut Self::Type); +} + +impl ShaderRepr for T +where + T: ShaderNative, +{ + const ALIGN_MASK: usize = ::ALIGN_MASK; + const ARRAY_PADDING: usize = ::ARRAY_PADDING_140; + type Type = T; + type ArrayPadding = ::ArrayPadding140; + + fn copy_to_repr(&self, repr: &mut T) { + *repr = *self + } +} + +impl ShaderRepr for T +where + T: ShaderNative, +{ + const ALIGN_MASK: usize = ::ALIGN_MASK; + const ARRAY_PADDING: usize = ::ARRAY_PADDING_430; + type Type = T; + type ArrayPadding = ::ArrayPadding430; + + fn copy_to_repr(&self, repr: &mut T) { + *repr = *self + } +} diff --git a/src/repr/repr.rs b/src/repr/repr.rs deleted file mode 100644 index 7fb97e1..0000000 --- a/src/repr/repr.rs +++ /dev/null @@ -1,56 +0,0 @@ -use { - super::{native::ShaderNative, pad::Padding}, - bytemuck::Pod, -}; - -/// Default layout for [`Repr`]. -/// Can be used for both uniforms storage buffers. -#[derive(Clone, Copy, Debug)] -pub enum Std140 {} - -/// Can be used only for storage buffers. -#[derive(Clone, Copy, Debug)] -pub enum Std430 {} - -/// Type that can be represented in shader. -pub trait ShaderRepr { - const ALIGN_MASK: usize; - const ARRAY_PADDING: usize; - - /// Type with matching layout. - type Type: Pod; - - /// Padding required after field of `Self::Type` for arrays. - type ArrayPadding: Padding; - - /// Copy data in this type into its representation. - fn copy_to_repr(&self, repr: &mut Self::Type); -} - -impl ShaderRepr for T -where - T: ShaderNative, -{ - const ALIGN_MASK: usize = ::ALIGN_MASK; - const ARRAY_PADDING: usize = ::ARRAY_PADDING_140; - type Type = T; - type ArrayPadding = ::ArrayPadding140; - - fn copy_to_repr(&self, repr: &mut T) { - *repr = *self - } -} - -impl ShaderRepr for T -where - T: ShaderNative, -{ - const ALIGN_MASK: usize = ::ALIGN_MASK; - const ARRAY_PADDING: usize = ::ARRAY_PADDING_430; - type Type = T; - type ArrayPadding = ::ArrayPadding430; - - fn copy_to_repr(&self, repr: &mut T) { - *repr = *self - } -} diff --git a/src/shader.rs b/src/shader.rs index 66e86ec..119655c 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -196,7 +196,7 @@ impl Spirv { impl From for ShaderModuleInfo { fn from(shader: Spirv) -> Self { ShaderModuleInfo { - code: shader.code.into(), + code: shader.code, language: ShaderLanguage::SPIRV, } } diff --git a/src/view.rs b/src/view.rs index d982367..45dda2e 100644 --- a/src/view.rs +++ b/src/view.rs @@ -65,17 +65,17 @@ impl ImageViewInfo { #[doc(hidden)] pub trait MakeImageView { - fn make_view<'a>(&'a self, device: &Device) -> Result; + fn make_view(&self, device: &Device) -> Result; } impl MakeImageView for ImageView { - fn make_view<'a>(&'a self, _device: &Device) -> Result { + fn make_view(&self, _device: &Device) -> Result { Ok(self.clone()) } } impl MakeImageView for Image { - fn make_view<'a>(&'a self, device: &Device) -> Result { + fn make_view(&self, device: &Device) -> Result { let view = device.create_image_view(ImageViewInfo::new(self.clone()))?; Ok(view) }