Skip to content

Commit

Permalink
Change type of mip_level_count and array_layer_count (members of …
Browse files Browse the repository at this point in the history
…`TextureViewDescriptor` and `ImageSubresourceRange`) from `Option<NonZeroU32>` to `Option<u32>` (#3445)

Clean up duplicated code related to texture layers/mips.
  • Loading branch information
teoxoy authored Feb 3, 2023
1 parent 6399dd4 commit 41de797
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 269 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Bottom level categories:

## Unreleased

### Major changes

#### General

- Change type of `mip_level_count` and `array_layer_count` (members of `TextureViewDescriptor` and `ImageSubresourceRange`) from `Option<NonZeroU32>` to `Option<u32>`. By @teoxoy in [#3445](https://github.com/gfx-rs/wgpu/pull/3445)

### Changes

#### WebGPU
Expand Down
15 changes: 3 additions & 12 deletions deno_webgpu/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ pub struct CreateTextureViewArgs {
label: Option<String>,
format: Option<wgpu_types::TextureFormat>,
dimension: Option<wgpu_types::TextureViewDimension>,
aspect: wgpu_types::TextureAspect,
base_mip_level: u32,
mip_level_count: Option<u32>,
base_array_layer: u32,
array_layer_count: Option<u32>,
#[serde(flatten)]
range: wgpu_types::ImageSubresourceRange,
}

#[op]
Expand All @@ -95,13 +92,7 @@ pub fn op_webgpu_create_texture_view(
label: args.label.map(Cow::from),
format: args.format,
dimension: args.dimension,
range: wgpu_types::ImageSubresourceRange {
aspect: args.aspect,
base_mip_level: args.base_mip_level,
mip_level_count: std::num::NonZeroU32::new(args.mip_level_count.unwrap_or(0)),
base_array_layer: args.base_array_layer,
array_layer_count: std::num::NonZeroU32::new(args.array_layer_count.unwrap_or(0)),
},
range: args.range,
};

gfx_put!(texture => instance.texture_create_view(
Expand Down
27 changes: 11 additions & 16 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ whereas subesource range specified start {subresource_base_mip_level} and count
InvalidTextureLevelRange {
texture_level_range: Range<u32>,
subresource_base_mip_level: u32,
subresource_mip_level_count: Option<NonZeroU32>,
subresource_mip_level_count: Option<u32>,
},
#[error("image subresource layer range is outside of the texture's layer range. texture range is {texture_layer_range:?}, \
whereas subesource range specified start {subresource_base_array_layer} and count {subresource_array_layer_count:?}")]
InvalidTextureLayerRange {
texture_layer_range: Range<u32>,
subresource_base_array_layer: u32,
subresource_array_layer_count: Option<NonZeroU32>,
subresource_array_layer_count: Option<u32>,
},
}

Expand Down Expand Up @@ -188,12 +188,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
};

// Check if subresource level range is valid
let subresource_level_end = match subresource_range.mip_level_count {
Some(count) => subresource_range.base_mip_level + count.get(),
None => dst_texture.full_range.mips.end,
};
if dst_texture.full_range.mips.start > subresource_range.base_mip_level
|| dst_texture.full_range.mips.end < subresource_level_end
let subresource_mip_range = subresource_range.mip_range(dst_texture.full_range.mips.end);
if dst_texture.full_range.mips.start > subresource_mip_range.start
|| dst_texture.full_range.mips.end < subresource_mip_range.end
{
return Err(ClearError::InvalidTextureLevelRange {
texture_level_range: dst_texture.full_range.mips.clone(),
Expand All @@ -202,12 +199,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
});
}
// Check if subresource layer range is valid
let subresource_layer_end = match subresource_range.array_layer_count {
Some(count) => subresource_range.base_array_layer + count.get(),
None => dst_texture.full_range.layers.end,
};
if dst_texture.full_range.layers.start > subresource_range.base_array_layer
|| dst_texture.full_range.layers.end < subresource_layer_end
let subresource_layer_range =
subresource_range.layer_range(dst_texture.full_range.layers.end);
if dst_texture.full_range.layers.start > subresource_layer_range.start
|| dst_texture.full_range.layers.end < subresource_layer_range.end
{
return Err(ClearError::InvalidTextureLayerRange {
texture_layer_range: dst_texture.full_range.layers.clone(),
Expand All @@ -222,8 +217,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&*texture_guard,
Valid(dst),
TextureInitRange {
mip_range: subresource_range.base_mip_level..subresource_level_end,
layer_range: subresource_range.base_array_layer..subresource_layer_end,
mip_range: subresource_mip_range,
layer_range: subresource_layer_range,
},
cmd_buf.encoder.open(),
&mut cmd_buf.trackers.textures,
Expand Down
10 changes: 5 additions & 5 deletions wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ pub(crate) fn extract_texture_selector<A: hal::Api>(
}

let (layers, origin_z) = match texture.desc.dimension {
wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => (
wgt::TextureDimension::D1 => (0..1, 0),
wgt::TextureDimension::D2 => (
copy_texture.origin.z..copy_texture.origin.z + copy_size.depth_or_array_layers,
0,
),
Expand Down Expand Up @@ -323,7 +324,7 @@ pub(crate) fn validate_linear_texture_data(
///
/// Returns the HAL copy extent and the layer count.
///
/// [vtcr]: https://gpuweb.github.io/gpuweb/#valid-texture-copy-range
/// [vtcr]: https://gpuweb.github.io/gpuweb/#validating-texture-copy-range
pub(crate) fn validate_texture_copy_range(
texture_copy_view: &ImageCopyTexture,
desc: &wgt::TextureDescriptor<(), Vec<wgt::TextureFormat>>,
Expand Down Expand Up @@ -417,9 +418,8 @@ pub(crate) fn validate_texture_copy_range(
}

let (depth, array_layer_count) = match desc.dimension {
wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => {
(1, copy_size.depth_or_array_layers)
}
wgt::TextureDimension::D1 => (1, 1),
wgt::TextureDimension::D2 => (1, copy_size.depth_or_array_layers),
wgt::TextureDimension::D3 => (copy_size.depth_or_array_layers, 1),
};

Expand Down
62 changes: 30 additions & 32 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,9 @@ impl<A: HalApi> Device<A> {
range: wgt::ImageSubresourceRange {
aspect: wgt::TextureAspect::All,
base_mip_level: mip_level,
mip_level_count: NonZeroU32::new(1),
mip_level_count: Some(1),
base_array_layer: array_layer,
array_layer_count: NonZeroU32::new(1),
array_layer_count: Some(1),
},
};
clear_views.push(
Expand Down Expand Up @@ -992,33 +992,28 @@ impl<A: HalApi> Device<A> {
wgt::TextureDimension::D3 => wgt::TextureViewDimension::D3,
});

let resolved_mip_level_count = desc
.range
.mip_level_count
.map(NonZeroU32::get)
.unwrap_or_else(|| {
texture
.desc
.mip_level_count
.saturating_sub(desc.range.base_mip_level)
});
let resolved_mip_level_count = desc.range.mip_level_count.unwrap_or_else(|| {
texture
.desc
.mip_level_count
.saturating_sub(desc.range.base_mip_level)
});

let resolved_array_layer_count = desc
.range
.array_layer_count
.map(NonZeroU32::get)
.unwrap_or_else(|| match resolved_dimension {
wgt::TextureViewDimension::D1
| wgt::TextureViewDimension::D2
| wgt::TextureViewDimension::D3 => 1,
wgt::TextureViewDimension::Cube => 6,
wgt::TextureViewDimension::D2Array | wgt::TextureViewDimension::CubeArray => {
texture
.desc
.array_layer_count()
.saturating_sub(desc.range.base_array_layer)
}
});
let resolved_array_layer_count =
desc.range
.array_layer_count
.unwrap_or_else(|| match resolved_dimension {
wgt::TextureViewDimension::D1
| wgt::TextureViewDimension::D2
| wgt::TextureViewDimension::D3 => 1,
wgt::TextureViewDimension::Cube => 6,
wgt::TextureViewDimension::D2Array | wgt::TextureViewDimension::CubeArray => {
texture
.desc
.array_layer_count()
.saturating_sub(desc.range.base_array_layer)
}
});

// validate TextureViewDescriptor

Expand Down Expand Up @@ -1179,9 +1174,9 @@ impl<A: HalApi> Device<A> {
let resolved_range = wgt::ImageSubresourceRange {
aspect: desc.range.aspect,
base_mip_level: desc.range.base_mip_level,
mip_level_count: NonZeroU32::new(resolved_mip_level_count),
mip_level_count: Some(resolved_mip_level_count),
base_array_layer: desc.range.base_array_layer,
array_layer_count: NonZeroU32::new(resolved_array_layer_count),
array_layer_count: Some(resolved_array_layer_count),
};

let hal_desc = hal::TextureViewDescriptor {
Expand Down Expand Up @@ -1878,8 +1873,11 @@ impl<A: HalApi> Device<A> {
used_texture_ranges.push(TextureInitTrackerAction {
id: view.parent_id.value.0,
range: TextureInitRange {
mip_range: view.desc.range.mip_range(&texture.desc),
layer_range: view.desc.range.layer_range(&texture.desc),
mip_range: view.desc.range.mip_range(texture.desc.mip_level_count),
layer_range: view
.desc
.range
.layer_range(texture.desc.array_layer_count()),
},
kind: MemoryInitKind::NeedsInitializedMemory,
});
Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ use crate::{
pipeline, resource,
};

use std::{fmt, num::NonZeroU32, ops};
use std::{fmt, ops};
use thiserror::Error;

pub(crate) use buffer::{BufferBindGroupState, BufferTracker, BufferUsageScope};
Expand Down Expand Up @@ -162,9 +162,9 @@ impl PendingTransition<hal::TextureUses> {
range: wgt::ImageSubresourceRange {
aspect: wgt::TextureAspect::All,
base_mip_level: self.selector.mips.start,
mip_level_count: unsafe { Some(NonZeroU32::new_unchecked(mip_count)) },
mip_level_count: Some(mip_count),
base_array_layer: self.selector.layers.start,
array_layer_count: unsafe { Some(NonZeroU32::new_unchecked(layer_count)) },
array_layer_count: Some(layer_count),
},
usage: self.usage,
}
Expand Down
31 changes: 10 additions & 21 deletions wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,12 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
}
};

let mip_level_count = match barrier.range.mip_level_count {
Some(count) => count.get(),
None => barrier.texture.mip_level_count - barrier.range.base_mip_level,
};
let array_layer_count = match barrier.range.array_layer_count {
Some(count) => count.get(),
None => barrier.texture.array_layer_count() - barrier.range.base_array_layer,
};
let tex_mip_level_count = barrier.texture.mip_level_count;
let tex_array_layer_count = barrier.texture.array_layer_count();

if barrier.range.aspect == wgt::TextureAspect::All
&& barrier.range.base_mip_level == 0
&& mip_level_count == barrier.texture.mip_level_count
&& barrier.range.base_array_layer == 0
&& array_layer_count == barrier.texture.array_layer_count()
if barrier
.range
.is_full_resource(tex_mip_level_count, tex_array_layer_count)
{
// Only one barrier if it affects the whole image.
self.temp.barriers.push(raw);
Expand All @@ -415,16 +407,13 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
0..1
};

for rel_mip_level in 0..mip_level_count {
for rel_array_layer in 0..array_layer_count {
for mip_level in barrier.range.mip_range(tex_mip_level_count) {
for array_layer in barrier.range.layer_range(tex_array_layer_count) {
for plane in planes.clone() {
unsafe {
raw.u.Transition_mut().Subresource =
barrier.texture.calc_subresource(
barrier.range.base_mip_level + rel_mip_level,
barrier.range.base_array_layer + rel_array_layer,
plane,
);
raw.u.Transition_mut().Subresource = barrier
.texture
.calc_subresource(mip_level, array_layer, plane);
};
self.temp.barriers.push(raw);
}
Expand Down
6 changes: 2 additions & 4 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,8 @@ unsafe impl Sync for Texture {}
impl Texture {
fn array_layer_count(&self) -> u32 {
match self.dimension {
wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => {
self.size.depth_or_array_layers
}
wgt::TextureDimension::D3 => 1,
wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1,
wgt::TextureDimension::D2 => self.size.depth_or_array_layers,
}
}

Expand Down
10 changes: 2 additions & 8 deletions wgpu-hal/src/dx12/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ impl crate::TextureViewDescriptor<'_> {
format_nodepth: auxil::dxgi::conv::map_texture_format_nodepth(self.format),
multisampled: texture.sample_count > 1,
mip_level_base: self.range.base_mip_level,
mip_level_count: match self.range.mip_level_count {
Some(count) => count.get(),
None => !0,
},
mip_level_count: self.range.mip_level_count.unwrap_or(!0),
array_layer_base: self.range.base_array_layer,
array_layer_count: match self.range.array_layer_count {
Some(count) => count.get(),
None => !0,
},
array_layer_count: self.range.array_layer_count.unwrap_or(!0),
}
}
}
Expand Down
Loading

0 comments on commit 41de797

Please sign in to comment.