Skip to content

Commit

Permalink
cast packed vec3's to vec3 for expressions that require it
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy authored and kvark committed Apr 12, 2022
1 parent 32cdb27 commit 0e77d26
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
45 changes: 42 additions & 3 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ impl<W: Write> Writer<W> {
vector,
pattern,
} => {
self.put_expression(vector, context, false)?;
self.put_wrapped_expression_for_packed_vec3_access(vector, context, false)?;
write!(self.out, ".")?;
for &sc in pattern[..size as usize].iter() {
write!(self.out, "{}", back::COMPONENTS[sc as usize])?;
Expand Down Expand Up @@ -1447,9 +1447,31 @@ impl<W: Write> Writer<W> {
if !is_scoped {
write!(self.out, "(")?;
}
self.put_expression(left, context, false)?;

// Cast packed vector if necessary
// Packed vector - matrix multiplications are not supported in MSL
if op == crate::BinaryOperator::Multiply
&& matches!(
context.resolve_type(right),
&crate::TypeInner::Matrix { .. }
)
{
self.put_wrapped_expression_for_packed_vec3_access(left, context, false)?;
} else {
self.put_expression(left, context, false)?;
}

write!(self.out, " {} ", op_str)?;
self.put_expression(right, context, false)?;

// See comment above
if op == crate::BinaryOperator::Multiply
&& matches!(context.resolve_type(left), &crate::TypeInner::Matrix { .. })
{
self.put_wrapped_expression_for_packed_vec3_access(right, context, false)?;
} else {
self.put_expression(right, context, false)?;
}

if !is_scoped {
write!(self.out, ")")?;
}
Expand Down Expand Up @@ -1738,6 +1760,23 @@ impl<W: Write> Writer<W> {
Ok(())
}

/// Used by expressions like Swizzle and Binary since they need packed_vec3's to be casted to a vec3
fn put_wrapped_expression_for_packed_vec3_access(
&mut self,
expr_handle: Handle<crate::Expression>,
context: &ExpressionContext,
is_scoped: bool,
) -> BackendResult {
if let Some(scalar_kind) = context.get_packed_vec_kind(expr_handle) {
write!(self.out, "{}::{}3(", NAMESPACE, scalar_kind.to_msl_name())?;
self.put_expression(expr_handle, context, is_scoped)?;
write!(self.out, ")")?;
} else {
self.put_expression(expr_handle, context, is_scoped)?;
}
Ok(())
}

/// Write a `GuardedIndex` as a Metal expression.
fn put_index(
&mut self,
Expand Down
6 changes: 3 additions & 3 deletions tests/out/msl/globals.msl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ void test_msl_packed_vec3_(
alignment.v3_[_e19] = 3.0;
Foo data = alignment;
metal::float3 unnamed = data.v3_;
metal::float2 unnamed_1 = data.v3_.zx;
metal::float2 unnamed_1 = metal::float3(data.v3_).zx;
test_msl_packed_vec3_as_arg(data.v3_);
metal::float3 unnamed_2 = data.v3_ * const_type_10_;
metal::float3 unnamed_3 = const_type_10_ * data.v3_;
metal::float3 unnamed_2 = metal::float3(data.v3_) * const_type_10_;
metal::float3 unnamed_3 = const_type_10_ * metal::float3(data.v3_);
metal::float3 unnamed_4 = data.v3_ * 2.0;
metal::float3 unnamed_5 = 2.0 * data.v3_;
}
Expand Down

0 comments on commit 0e77d26

Please sign in to comment.