Skip to content

Commit

Permalink
impl map_label_and_view_formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 13, 2023
1 parent b00e4cd commit 247dc5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
18 changes: 18 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4387,6 +4387,24 @@ impl<L, V: Clone> TextureDescriptor<L, V> {
}
}

/// Maps the label and view_formats of the texture descriptor into another.
pub fn map_label_and_view_formats<K, M>(
&self,
l_fun: impl FnOnce(&L) -> K,
v_fun: impl FnOnce(V) -> M,
) -> TextureDescriptor<K, M> {
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.
Expand Down
22 changes: 2 additions & 20 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<A>(hal_texture, device.id, &descriptor, ()) };
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 2 additions & 13 deletions wgpu/src/util/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,18 @@ impl DeviceExt for crate::Device {
desc: &crate::TextureDescriptor,
data: &[u8],
) -> crate::Texture {
let wgt_desc: wgt::TextureDescriptor<Option<&str>, Vec<wgt::TextureFormat>> =
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;
Expand Down

0 comments on commit 247dc5c

Please sign in to comment.