From 247dc5c233df71432e1f4e166cfcc043c6600579 Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Fri, 13 Jan 2023 10:27:48 +0800 Subject: [PATCH] impl `map_label_and_view_formats` --- wgpu-types/src/lib.rs | 18 ++++++++++++++++++ wgpu/src/backend/direct.rs | 22 ++-------------------- wgpu/src/util/device.rs | 15 ++------------- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index a1c4dc6018..6b0dc0f3bc 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -4387,6 +4387,24 @@ impl TextureDescriptor { } } + /// Maps the label and view_formats of the texture descriptor into another. + pub fn map_label_and_view_formats( + &self, + l_fun: impl FnOnce(&L) -> K, + v_fun: impl FnOnce(V) -> M, + ) -> TextureDescriptor { + TextureDescriptor { + label: l_fun(&self.label), + size: self.size, + mip_level_count: self.mip_level_count, + sample_count: self.sample_count, + dimension: self.dimension, + format: self.format, + usage: self.usage, + view_formats: v_fun(self.view_formats.clone()), + } + } + /// Calculates the extent at a given mip level. /// /// If the given mip level is larger than possible, returns None. diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 1221dcd878..6d5a30ecfe 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -135,16 +135,7 @@ impl Context { device: &Device, desc: &TextureDescriptor, ) -> Texture { - let descriptor = wgt::TextureDescriptor { - label: desc.label.map(Borrowed), - size: desc.size, - mip_level_count: desc.mip_level_count, - sample_count: desc.sample_count, - format: desc.format, - dimension: desc.dimension, - usage: desc.usage, - view_formats: desc.view_formats.to_vec(), - }; + let descriptor = desc.map_label_and_view_formats(|l| l.map(Borrowed), |v| v.to_vec()); let global = &self.0; let (id, error) = unsafe { global.create_texture_from_hal::(hal_texture, device.id, &descriptor, ()) }; @@ -1253,16 +1244,7 @@ impl crate::Context for Context { device_data: &Self::DeviceData, desc: &TextureDescriptor, ) -> (Self::TextureId, Self::TextureData) { - let wgt_desc = wgt::TextureDescriptor { - label: desc.label.map(Borrowed), - size: desc.size, - mip_level_count: desc.mip_level_count, - sample_count: desc.sample_count, - format: desc.format, - dimension: desc.dimension, - usage: desc.usage, - view_formats: desc.view_formats.to_vec(), - }; + let wgt_desc = desc.map_label_and_view_formats(|l| l.map(Borrowed), |v| v.to_vec()); let global = &self.0; let (id, error) = wgc::gfx_select!(device => global.device_create_texture( *device, diff --git a/wgpu/src/util/device.rs b/wgpu/src/util/device.rs index cb801d9d81..b441041a71 100644 --- a/wgpu/src/util/device.rs +++ b/wgpu/src/util/device.rs @@ -82,29 +82,18 @@ impl DeviceExt for crate::Device { desc: &crate::TextureDescriptor, data: &[u8], ) -> crate::Texture { - let wgt_desc: wgt::TextureDescriptor, Vec> = - wgt::TextureDescriptor { - label: Some(""), - size: desc.size, - mip_level_count: desc.mip_level_count, - sample_count: desc.sample_count, - format: desc.format, - dimension: desc.dimension, - usage: desc.usage, - view_formats: desc.view_formats.to_vec(), - }; // Implicitly add the COPY_DST usage let mut desc = desc.to_owned(); desc.usage |= crate::TextureUsages::COPY_DST; let texture = self.create_texture(&desc); let format_info = desc.format.describe(); - let layer_iterations = wgt_desc.array_layer_count(); + let layer_iterations = desc.array_layer_count(); let mut binary_offset = 0; for layer in 0..layer_iterations { for mip in 0..desc.mip_level_count { - let mut mip_size = wgt_desc.mip_level_size(mip).unwrap(); + let mut mip_size = desc.mip_level_size(mip).unwrap(); // copying layers separately if desc.dimension != wgt::TextureDimension::D3 { mip_size.depth_or_array_layers = 1;