diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 5db61eb74..acafe3903 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -91,6 +91,8 @@ pub enum CreateBindGroupError { }, #[error("the given sampler is/is not a comparison sampler, while the layout type indicates otherwise")] WrongSamplerComparison, + #[error("bound texture views can not have both depth and stencil aspects enabled")] + DepthStencilAspect, } #[derive(Clone, Debug, Error)] diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 707c65475..3302c79a9 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1348,8 +1348,20 @@ impl Global { let end_layer = desc .array_layer_count .map_or(layer_end, |_| required_layer_count as u16); + let aspects = match desc.aspect { + wgt::TextureAspect::All => texture.full_range.aspects, + wgt::TextureAspect::DepthOnly => hal::format::Aspects::DEPTH, + wgt::TextureAspect::StencilOnly => hal::format::Aspects::STENCIL, + }; + if !texture.full_range.aspects.contains(aspects) { + return Err(resource::CreateTextureViewError::InvalidAspect { + requested: aspects, + total: texture.full_range.aspects, + }); + } + let range = hal::image::SubresourceRange { - aspects: texture.full_range.aspects, + aspects, levels: desc.base_mip_level as u8..end_level, layers: desc.base_array_layer as u16..end_layer, }; @@ -2061,6 +2073,13 @@ impl Global { expected: "SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture" }) }; + if view + .range + .aspects + .contains(hal::format::Aspects::DEPTH | hal::format::Aspects::STENCIL) + { + return Err(CreateBindGroupError::DepthStencilAspect); + } match view.inner { resource::TextureViewInner::Native { ref raw, diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 80f0fa5e7..392c2f6a3 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -276,6 +276,11 @@ pub enum CreateTextureViewError { InvalidMipLevelCount { requested: u32, total: u8 }, #[error("TextureView array layer count + base array layer {requested} must be <= Texture depth/array layer count {total}")] InvalidArrayLayerCount { requested: u32, total: u16 }, + #[error("Aspect {requested:?} is not in the source texture ({total:?})")] + InvalidAspect { + requested: hal::format::Aspects, + total: hal::format::Aspects, + }, } #[derive(Clone, Debug, Error)] diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index e6e991bb4..aab171ae0 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -1780,6 +1780,11 @@ impl<'a> TextureViewDescriptor>> { } impl TextureViewDescriptor { + pub fn aspect(&mut self, aspect: TextureAspect) -> &mut Self { + self.aspect = aspect; + self + } + pub fn base_mip_level(&mut self, base_mip_level: u32) -> &mut Self { self.base_mip_level = base_mip_level; self