From 6e99cd3a3ed4a8758d8da5544980318f23ee61e8 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Sun, 14 Aug 2022 02:01:19 +0200 Subject: [PATCH] Fix calculation/validation of layer/mip ranges in create_texture_view (#2955) Co-authored-by: Connor Fitzgerald --- CHANGELOG.md | 1 + wgpu-core/src/device/mod.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d530904f8..89c9c4e098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ the same every time it is rendered, we now warn if it is missing. - Validate the number of color attachments in `create_render_pipeline` by @nical in [#2913](https://github.com/gfx-rs/wgpu/pull/2913) - Validate against the maximum binding index in `create_bind_group_layout` by @nical in [#2892](https://github.com/gfx-rs/wgpu/pull/2892) - Validate that map_async's range is not negative by @nical in [#2938](https://github.com/gfx-rs/wgpu/pull/2938) +- Fix calculation/validation of layer/mip ranges in create_texture_view by @nical in [#2955](https://github.com/gfx-rs/wgpu/pull/2955) - Validate the sample count and mip level in `copy_texture_to_buffer` by @nical in [#2958](https://github.com/gfx-rs/wgpu/pull/2958) #### DX12 diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index b3d8247ee4..afe32801d7 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -948,18 +948,21 @@ impl Device { }, }; - let required_level_count = - desc.range.base_mip_level + desc.range.mip_level_count.map_or(1, |count| count.get()); + let mip_count = desc.range.mip_level_count.map_or(1, |count| count.get()); + let required_level_count = desc.range.base_mip_level.saturating_add(mip_count); + let required_layer_count = match desc.range.array_layer_count { - Some(count) => desc.range.base_array_layer + count.get(), + Some(count) => desc.range.base_array_layer.saturating_add(count.get()), None => match view_dim { wgt::TextureViewDimension::D1 | wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D3 => 1, wgt::TextureViewDimension::Cube => 6, _ => texture.desc.array_layer_count(), - }, + } + .max(desc.range.base_array_layer.saturating_add(1)), }; + let level_end = texture.full_range.mips.end; let layer_end = texture.full_range.layers.end; if required_level_count > level_end {