Improve texture creation validation

This commit is contained in:
Dzmitry Malyshau 2020-08-23 23:54:58 -04:00
parent 0bb6bb8647
commit b8b7561796
4 changed files with 26 additions and 13 deletions

View File

@ -10,7 +10,7 @@ use crate::{
device::{all_buffer_stages, all_image_stages},
hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Storage, Token},
id::{BufferId, CommandEncoderId, TextureId},
resource::{BufferUse, Texture, TextureUse},
resource::{BufferUse, Texture, TextureErrorDimension, TextureUse},
span,
track::TextureSelector,
};
@ -32,13 +32,6 @@ pub enum CopySide {
Destination,
}
#[derive(Clone, Debug)]
pub enum TextureErrorDimension {
X,
Y,
Z,
}
/// Error encountered while attempting a data transfer.
#[derive(Clone, Debug, Error)]
pub enum TransferError {

View File

@ -575,6 +575,19 @@ pub fn map_texture_dimension_size(
use resource::TextureDimensionError as Tde;
use wgt::TextureDimension::*;
let zero_dim = if width == 0 {
Some(resource::TextureErrorDimension::X)
} else if height == 0 {
Some(resource::TextureErrorDimension::Y)
} else if depth == 0 {
Some(resource::TextureErrorDimension::Z)
} else {
None
};
if let Some(dim) = zero_dim {
return Err(resource::TextureDimensionError::Zero(dim));
}
Ok(match dimension {
D1 => {
if height != 1 {
@ -583,14 +596,14 @@ pub fn map_texture_dimension_size(
if sample_size != 1 {
return Err(Tde::InvalidSampleCount(sample_size));
}
let layers = depth.try_into().or(Err(Tde::TooManyLayers(depth)))?;
let layers = depth.try_into().unwrap_or(!0);
H::D1(width, layers)
}
D2 => {
if sample_size > 32 || !is_power_of_two(sample_size) {
return Err(Tde::InvalidSampleCount(sample_size));
}
let layers = depth.try_into().or(Err(Tde::TooManyLayers(depth)))?;
let layers = depth.try_into().unwrap_or(!0);
H::D2(width, height, layers, sample_size as u8)
}
D3 => {

View File

@ -482,7 +482,7 @@ impl<B: GfxBackend> Device<B> {
let usage = conv::map_texture_usage(desc.usage, aspects);
let mip_level_count = desc.mip_level_count;
if mip_level_count >= MAX_MIP_LEVELS {
if mip_level_count == 0 && mip_level_count > kind.compute_num_levels() as u32 {
return Err(resource::CreateTextureError::InvalidMipLevelCount(
mip_level_count,
));

View File

@ -213,10 +213,17 @@ pub struct Texture<B: hal::Backend> {
pub(crate) life_guard: LifeGuard,
}
#[derive(Clone, Debug)]
pub enum TextureErrorDimension {
X,
Y,
Z,
}
#[derive(Clone, Debug, Error)]
pub enum TextureDimensionError {
#[error("too many layers ({0}) for texture array")]
TooManyLayers(u32),
#[error("Dimension {0:?} is zero")]
Zero(TextureErrorDimension),
#[error("1D textures must have height set to 1")]
InvalidHeight,
#[error("sample count {0} is invalid")]