From 0ef76a76438b89d8930a1b412cca7e559c2e1807 Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Tue, 28 Jul 2020 23:18:11 +0530 Subject: [PATCH] Add mip level count and array layer count validation to texture_create_view --- wgpu-core/src/device/mod.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index b6ac1a05b..3a70b158b 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -998,13 +998,31 @@ impl Global { 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)]