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 committed Oct 20, 2023
1 parent 19209b6 commit c999a1b
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,15 @@ impl TypedExpression {
}
}

enum Composition {
enum Components {
Single(u32),
Multi(crate::VectorSize, [crate::SwizzleComponent; 4]),
Multi {
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 @@ -801,7 +804,7 @@ 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),
Expand All @@ -810,22 +813,20 @@ impl Composition {
}

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))?;
}
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::Multi { size, pattern })
}
}

Expand Down Expand Up @@ -1703,8 +1704,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::make(field.name, field.span)? {
Components::Multi { size, pattern } => {
let vector = ctx.apply_load_rule(TypedExpression {
handle,
is_reference,
Expand All @@ -1719,7 +1720,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 c999a1b

Please sign in to comment.