Skip to content

Commit ea37172

Browse files
committed
[naga] Add TypeInner::scalar_for_conversions helper function.
Define a new helper function on `naga::ir::TypeInner`, `scalar_for_conversions`, that returns the leaf scalar of a type as relevant for WGSL-style automatic conversions. Specifically, this means that, unlike `TypeInner::scalar`, arrays are considered to have leaf scalars if their elements do.
1 parent 5e77539 commit ea37172

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

naga/src/proc/type_methods.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ impl crate::TypeInner {
102102
///
103103
/// If `inner` is a scalar, vector, or matrix type, return
104104
/// its scalar type. Otherwise, return `None`.
105+
///
106+
/// Note that this doesn't inspect [`Array`] types, as required
107+
/// for automatic conversions. For that, see [`scalar_for_conversions`].
108+
///
109+
/// [`Array`]: crate::TypeInner::Array
110+
/// [`scalar_for_conversions`]: crate::TypeInner::scalar_for_conversions
105111
pub const fn scalar(&self) -> Option<crate::Scalar> {
106112
use crate::TypeInner as Ti;
107113
match *self {
@@ -120,6 +126,31 @@ impl crate::TypeInner {
120126
self.scalar().map(|scalar| scalar.width)
121127
}
122128

129+
/// Return the leaf scalar type of `self`, as needed for automatic conversions.
130+
///
131+
/// Unlike the [`scalar`] method, which only retrieves scalars for
132+
/// [`Scalar`], [`Vector`], and [`Matrix`] this also looks into
133+
/// [`Array`] types to find the leaf scalar.
134+
///
135+
/// [`scalar`]: crate::TypeInner::scalar
136+
/// [`Scalar`]: crate::TypeInner::Scalar
137+
/// [`Vector`]: crate::TypeInner::Vector
138+
/// [`Matrix`]: crate::TypeInner::Matrix
139+
/// [`Array`]: crate::TypeInner::Array
140+
pub fn scalar_for_conversions(
141+
&self,
142+
types: &crate::UniqueArena<crate::Type>,
143+
) -> Option<crate::Scalar> {
144+
use crate::TypeInner as Ti;
145+
match *self {
146+
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } | Ti::Matrix { scalar, .. } => {
147+
Some(scalar)
148+
}
149+
Ti::Array { base, .. } => types[base].inner.scalar_for_conversions(types),
150+
_ => None,
151+
}
152+
}
153+
123154
pub const fn pointer_space(&self) -> Option<crate::AddressSpace> {
124155
match *self {
125156
Self::Pointer { space, .. } => Some(space),

0 commit comments

Comments
 (0)