Skip to content

Commit

Permalink
[wgsl-in] Clean up swizzle/component access type.
Browse files Browse the repository at this point in the history
Rename `front::wgsl::lower::Composition` to `Components`. It doesn't
represent a composition, it represents either a single component being
selected from a vector or a swizzle.

Clean up code and helper function names.
  • Loading branch information
jimblandy authored and teoxoy committed Oct 23, 2023
1 parent d038506 commit dd8f814
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,20 @@ impl TypedExpression {
}
}

enum Composition {
/// A single vector component or swizzle.
///
/// This represents the things that can appear after the `.` in a vector access
/// expression: either a single component name, or a series of them,
/// representing a swizzle.
enum Components {
Single(u32),
Multi(crate::VectorSize, [crate::SwizzleComponent; 4]),
Swizzle {
size: crate::VectorSize,
pattern: [crate::SwizzleComponent; 4],
},
}

impl Composition {
impl Components {
const fn letter_component(letter: char) -> Option<crate::SwizzleComponent> {
use crate::SwizzleComponent as Sc;
match letter {
Expand All @@ -740,31 +748,32 @@ impl Composition {
}
}

fn extract_impl(name: &str, name_span: Span) -> Result<u32, Error> {
fn single_component(name: &str, name_span: Span) -> Result<u32, Error> {
let ch = name.chars().next().ok_or(Error::BadAccessor(name_span))?;
match Self::letter_component(ch) {
Some(sc) => Ok(sc as u32),
None => Err(Error::BadAccessor(name_span)),
}
}

fn make(name: &str, name_span: Span) -> Result<Self, Error> {
if name.len() > 1 {
let mut components = [crate::SwizzleComponent::X; 4];
for (comp, ch) in components.iter_mut().zip(name.chars()) {
*comp = Self::letter_component(ch).ok_or(Error::BadAccessor(name_span))?;
}
/// Construct a `Components` value from a 'member' name, like `"wzy"` or `"x"`.
///
/// Use `name_span` for reporting errors in parsing the component string.
fn new(name: &str, name_span: Span) -> Result<Self, Error> {
let size = match name.len() {
1 => return Ok(Components::Single(Self::single_component(name, name_span)?)),
2 => crate::VectorSize::Bi,
3 => crate::VectorSize::Tri,
4 => crate::VectorSize::Quad,
_ => return Err(Error::BadAccessor(name_span)),
};

let size = match name.len() {
2 => crate::VectorSize::Bi,
3 => crate::VectorSize::Tri,
4 => crate::VectorSize::Quad,
_ => return Err(Error::BadAccessor(name_span)),
};
Ok(Composition::Multi(size, components))
} else {
Self::extract_impl(name, name_span).map(Composition::Single)
let mut pattern = [crate::SwizzleComponent::X; 4];
for (comp, ch) in pattern.iter_mut().zip(name.chars()) {
*comp = Self::letter_component(ch).ok_or(Error::BadAccessor(name_span))?;
}

Ok(Components::Swizzle { size, pattern })
}
}

Expand Down Expand Up @@ -1644,8 +1653,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
)
}
crate::TypeInner::Vector { .. } | crate::TypeInner::Matrix { .. } => {
match Composition::make(field.name, field.span)? {
Composition::Multi(size, pattern) => {
match Components::new(field.name, field.span)? {
Components::Swizzle { size, pattern } => {
let vector = ctx.apply_load_rule(TypedExpression {
handle,
is_reference,
Expand All @@ -1660,7 +1669,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
false,
)
}
Composition::Single(index) => (
Components::Single(index) => (
crate::Expression::AccessIndex {
base: handle,
index,
Expand Down

0 comments on commit dd8f814

Please sign in to comment.