844: Add mip level count and array layer count validation to texture_create_view r=kvark a=kunalmohan

**Connections**
_Link to the issues addressed by this PR, or dependent PRs in other repositories_

**Description**
_Describe what problem this is solving, and how it's solved._
Catches the error when `mip_level_count = 0` and `base_mip_level > texture.mip_level_count` (same with `array_layer_count`)

**Testing**
_Explain how this change is tested._
Haven't been tested yet
<!--
Non-trivial functional changes would need to be tested through:
  - [wgpu-rs](https://github.com/gfx-rs/wgpu-rs) - test the examples.
  - [wgpu-native](https://github.com/gfx-rs/wgpu-native/) - check the generated C header for sanity.

Ideally, a PR needs to link to the draft PRs in these projects with relevant modifications.
See https://github.com/gfx-rs/wgpu/pull/666 for an example.
If you can add a unit/integration test here in `wgpu`, that would be best.
-->


Co-authored-by: Kunal Mohan <kunalmohan99@gmail.com>
This commit is contained in:
bors[bot]
2020-07-29 17:35:40 +00:00
committed by GitHub

View File

@@ -998,13 +998,31 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let (format, view_kind, range) = match desc {
Some(desc) => {
let kind = conv::map_texture_view_dimension(desc.dimension);
let required_level_count =
desc.base_mip_level + desc.level_count.map_or(1, |count| count.get());
let required_layer_count =
desc.base_array_layer + desc.array_layer_count.map_or(1, |count| count.get());
let level_end = texture.full_range.levels.end;
let layer_end = texture.full_range.layers.end;
if required_level_count > level_end as u32 {
return Err(CreateTextureViewError::InvalidMipLevelCount {
requested: required_level_count,
total: level_end,
});
}
if required_layer_count > layer_end as u32 {
return Err(CreateTextureViewError::InvalidArrayLayerCount {
requested: required_layer_count,
total: layer_end,
});
};
let end_level = match desc.level_count {
Some(count) => (desc.base_mip_level + count.get()) as u8,
None => texture.full_range.levels.end,
None => level_end,
};
let end_layer = match desc.array_layer_count {
Some(count) => (desc.base_array_layer + count.get()) as u16,
None => texture.full_range.layers.end,
None => layer_end,
};
let range = hal::image::SubresourceRange {
aspects: texture.full_range.aspects,
@@ -3126,6 +3144,12 @@ pub enum CreateSwapChainError {
pub enum CreateTextureViewError {
#[error("not enough memory left")]
OutOfMemory,
#[error(
"TextureView mip level count + base mip level {requested} must be <= Texture mip level count {total}"
)]
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 },
}
#[derive(Clone, Debug, Error)]