Add scalar_kind() accessor to IR Type (#251)

This commit is contained in:
Dzmitry Malyshau
2020-10-30 21:25:55 -04:00
committed by GitHub
parent 871139431a
commit 80bdd23fd4
4 changed files with 27 additions and 24 deletions

View File

@@ -1,11 +1,8 @@
use std::{cmp::Ordering, fmt, hash, marker::PhantomData, num::NonZeroU32};
/// An unique index in the arena array that a handle points to.
///
/// This type is independent of `spv::Word`. `spv::Word` is used in data
/// representation. It holds a SPIR-V and refers to that instruction. In
/// structured representation, we use Handle to refer to an SPIR-V instruction.
/// `Index` is an implementation detail to `Handle`.
/// The "non-zero" part ensures that an `Option<Handle<T>>` has
/// the same size and representation as `Handle<T>`.
type Index = NonZeroU32;
/// A strongly typed reference to a SPIR-V element.

View File

@@ -1131,11 +1131,10 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let value_lexp = self.lookup_expression.lookup(value_id)?;
let ty_lookup = self.lookup_type.lookup(result_type_id)?;
let kind = match type_arena[ty_lookup.handle].inner {
crate::TypeInner::Scalar { kind, .. }
| crate::TypeInner::Vector { kind, .. } => kind,
_ => return Err(Error::InvalidAsType(ty_lookup.handle)),
};
let kind = type_arena[ty_lookup.handle]
.inner
.scalar_kind()
.ok_or(Error::InvalidAsType(ty_lookup.handle))?;
let expr = crate::Expression::As {
expr: value_lexp.handle,
@@ -2071,10 +2070,10 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let format = self.next()?;
let base_handle = self.lookup_type.lookup(sample_type_id)?.handle;
let kind = match module.types[base_handle].inner {
crate::TypeInner::Scalar { kind, .. } | crate::TypeInner::Vector { kind, .. } => kind,
_ => return Err(Error::InvalidImageBaseType(base_handle)),
};
let kind = module.types[base_handle]
.inner
.scalar_kind()
.ok_or(Error::InvalidImageBaseType(base_handle))?;
let class = if format != 0 {
crate::ImageClass::Storage(map_image_format(format)?)

View File

@@ -47,3 +47,15 @@ impl From<super::StorageFormat> for super::ScalarKind {
}
}
}
impl crate::TypeInner {
pub fn scalar_kind(&self) -> Option<super::ScalarKind> {
match *self {
super::TypeInner::Scalar { kind, .. } | super::TypeInner::Vector { kind, .. } => {
Some(kind)
}
super::TypeInner::Matrix { .. } => Some(super::ScalarKind::Float),
_ => None,
}
}
}

View File

@@ -263,17 +263,12 @@ impl Validator {
match (stage, var.class) {
(crate::ShaderStage::Vertex, crate::StorageClass::Output)
| (crate::ShaderStage::Fragment, crate::StorageClass::Input) => {
match module.types[var.ty].inner {
crate::TypeInner::Scalar { kind, .. }
| crate::TypeInner::Vector { kind, .. } => {
if kind != crate::ScalarKind::Float
&& var.interpolation != Some(crate::Interpolation::Flat)
{
return Err(EntryPointError::InvalidIntegerInterpolation);
}
match module.types[var.ty].inner.scalar_kind() {
Some(crate::ScalarKind::Float) => {}
Some(_) if var.interpolation != Some(crate::Interpolation::Flat) => {
return Err(EntryPointError::InvalidIntegerInterpolation);
}
crate::TypeInner::Matrix { .. } => {}
_ => unreachable!(),
_ => {}
}
}
_ => {}