mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 08:21:25 +01:00
msl: fix packed vec access (#1634)
This commit is contained in:
1 parent
7fb21f7b99
commit
845354d90b
9 files changed
+130
-96
No files matched your search
+65
-46
@@ -47,20 +47,20 @@ impl<'a> Display for TypeContext<'a> {
|
||||
// work around Metal toolchain bug with `uint` typedef
|
||||
crate::ScalarKind::Uint => write!(out, "{}::uint", NAMESPACE),
|
||||
_ => {
|
||||
let kind_str = scalar_kind_string(kind);
|
||||
let kind_str = kind.to_msl_name();
|
||||
write!(out, "{}", kind_str)
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::TypeInner::Atomic { kind, .. } => {
|
||||
write!(out, "{}::atomic_{}", NAMESPACE, scalar_kind_string(kind))
|
||||
write!(out, "{}::atomic_{}", NAMESPACE, kind.to_msl_name())
|
||||
}
|
||||
crate::TypeInner::Vector { size, kind, .. } => {
|
||||
write!(
|
||||
out,
|
||||
"{}::{}{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
back::vector_size_str(size),
|
||||
)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ impl<'a> Display for TypeContext<'a> {
|
||||
out,
|
||||
"{}::{}{}x{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(crate::ScalarKind::Float),
|
||||
crate::ScalarKind::Float.to_msl_name(),
|
||||
back::vector_size_str(columns),
|
||||
back::vector_size_str(rows),
|
||||
)
|
||||
@@ -96,7 +96,7 @@ impl<'a> Display for TypeContext<'a> {
|
||||
Some(name) => name,
|
||||
None => return Ok(()),
|
||||
};
|
||||
write!(out, "{} {}&", class_name, scalar_kind_string(kind),)
|
||||
write!(out, "{} {}&", class_name, kind.to_msl_name(),)
|
||||
}
|
||||
crate::TypeInner::ValuePointer {
|
||||
size: Some(size),
|
||||
@@ -113,7 +113,7 @@ impl<'a> Display for TypeContext<'a> {
|
||||
"{} {}::{}{}&",
|
||||
class_name,
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
back::vector_size_str(size),
|
||||
)
|
||||
}
|
||||
@@ -178,7 +178,7 @@ impl<'a> Display for TypeContext<'a> {
|
||||
("texture", "", format.into(), access)
|
||||
}
|
||||
};
|
||||
let base_name = scalar_kind_string(kind);
|
||||
let base_name = kind.to_msl_name();
|
||||
let array_str = if arrayed { "_array" } else { "" };
|
||||
write!(
|
||||
out,
|
||||
@@ -316,12 +316,14 @@ pub struct Writer<W> {
|
||||
struct_member_pads: FastHashSet<(Handle<crate::Type>, u32)>,
|
||||
}
|
||||
|
||||
fn scalar_kind_string(kind: crate::ScalarKind) -> &'static str {
|
||||
match kind {
|
||||
crate::ScalarKind::Float => "float",
|
||||
crate::ScalarKind::Sint => "int",
|
||||
crate::ScalarKind::Uint => "uint",
|
||||
crate::ScalarKind::Bool => "bool",
|
||||
impl crate::ScalarKind {
|
||||
fn to_msl_name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Float => "float",
|
||||
Self::Sint => "int",
|
||||
Self::Uint => "uint",
|
||||
Self::Bool => "bool",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,6 +483,29 @@ impl<'a> ExpressionContext<'a> {
|
||||
) -> Option<index::IndexableLength> {
|
||||
index::access_needs_check(base, index, self.module, self.function, self.info)
|
||||
}
|
||||
|
||||
// Because packed vectors such as `packed_float3` cannot be directly loaded,
|
||||
// we convert them to unpacked vectors like `float3` on load.
|
||||
fn get_packed_vec_kind(
|
||||
&self,
|
||||
expr_handle: Handle<crate::Expression>,
|
||||
) -> Option<crate::ScalarKind> {
|
||||
match self.function.expressions[expr_handle] {
|
||||
crate::Expression::AccessIndex { base, index } => {
|
||||
let ty = match *self.resolve_type(base) {
|
||||
crate::TypeInner::Pointer { base, .. } => &self.module.types[base].inner,
|
||||
ref ty => ty,
|
||||
};
|
||||
match *ty {
|
||||
crate::TypeInner::Struct {
|
||||
ref members, span, ..
|
||||
} => should_pack_struct_member(members, span, index as usize, self.module),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct StatementContext<'a> {
|
||||
@@ -652,7 +677,7 @@ impl<W: Write> Writer<W> {
|
||||
) -> BackendResult {
|
||||
match context.module.types[ty].inner {
|
||||
crate::TypeInner::Scalar { width: 4, kind } if components.len() == 1 => {
|
||||
write!(self.out, "{}", scalar_kind_string(kind))?;
|
||||
write!(self.out, "{}", kind.to_msl_name())?;
|
||||
self.put_call_parameters(components.iter().cloned(), context)?;
|
||||
}
|
||||
crate::TypeInner::Vector { size, kind, .. } => {
|
||||
@@ -660,7 +685,7 @@ impl<W: Write> Writer<W> {
|
||||
self.out,
|
||||
"{}::{}{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
back::vector_size_str(size)
|
||||
)?;
|
||||
self.put_call_parameters(components.iter().cloned(), context)?;
|
||||
@@ -671,7 +696,7 @@ impl<W: Write> Writer<W> {
|
||||
self.out,
|
||||
"{}::{}{}x{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
back::vector_size_str(columns),
|
||||
back::vector_size_str(rows)
|
||||
)?;
|
||||
@@ -845,7 +870,7 @@ impl<W: Write> Writer<W> {
|
||||
crate::TypeInner::Scalar { kind, .. } => kind,
|
||||
_ => return Err(Error::Validation),
|
||||
};
|
||||
let scalar = scalar_kind_string(scalar_kind);
|
||||
let scalar = scalar_kind.to_msl_name();
|
||||
let size = back::vector_size_str(size);
|
||||
|
||||
write!(self.out, "{}::{}{}(", NAMESPACE, scalar, size)?;
|
||||
@@ -1203,7 +1228,7 @@ impl<W: Write> Writer<W> {
|
||||
kind,
|
||||
convert,
|
||||
} => {
|
||||
let scalar = scalar_kind_string(kind);
|
||||
let scalar = kind.to_msl_name();
|
||||
let (src_kind, src_width) = match *context.resolve_type(expr) {
|
||||
crate::TypeInner::Scalar { kind, width }
|
||||
| crate::TypeInner::Vector { kind, width, .. } => (kind, width),
|
||||
@@ -1444,7 +1469,15 @@ impl<W: Write> Writer<W> {
|
||||
write!(self.out, ".{}", name)?;
|
||||
}
|
||||
crate::TypeInner::ValuePointer { .. } | crate::TypeInner::Vector { .. } => {
|
||||
self.put_access_chain(base, policy, context)?;
|
||||
let wrap_packed_vec_scalar_kind = context.get_packed_vec_kind(base);
|
||||
//Note: this doesn't work for left-hand side
|
||||
if let Some(scalar_kind) = wrap_packed_vec_scalar_kind {
|
||||
write!(self.out, "{}::{}3(", NAMESPACE, scalar_kind.to_msl_name())?;
|
||||
self.put_access_chain(base, policy, context)?;
|
||||
write!(self.out, ")")?;
|
||||
} else {
|
||||
self.put_access_chain(base, policy, context)?;
|
||||
}
|
||||
write!(self.out, ".{}", back::COMPONENTS[index as usize])?;
|
||||
}
|
||||
_ => {
|
||||
@@ -1571,23 +1604,7 @@ impl<W: Write> Writer<W> {
|
||||
policy: index::BoundsCheckPolicy,
|
||||
context: &ExpressionContext,
|
||||
) -> BackendResult {
|
||||
// Because packed vectors such as `packed_float3` cannot be directly multipied by
|
||||
// matrices, we convert them to unpacked vectors like `float3` on load.
|
||||
let wrap_packed_vec_scalar_kind = match context.function.expressions[pointer] {
|
||||
crate::Expression::AccessIndex { base, index } => {
|
||||
let ty = match *context.resolve_type(base) {
|
||||
crate::TypeInner::Pointer { base, .. } => &context.module.types[base].inner,
|
||||
ref ty => ty,
|
||||
};
|
||||
match *ty {
|
||||
crate::TypeInner::Struct {
|
||||
ref members, span, ..
|
||||
} => should_pack_struct_member(members, span, index as usize, context.module),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let wrap_packed_vec_scalar_kind = context.get_packed_vec_kind(pointer);
|
||||
let is_atomic = match *context.resolve_type(pointer) {
|
||||
crate::TypeInner::Pointer { base, .. } => match context.module.types[base].inner {
|
||||
crate::TypeInner::Atomic { .. } => true,
|
||||
@@ -1597,12 +1614,7 @@ impl<W: Write> Writer<W> {
|
||||
};
|
||||
|
||||
if let Some(scalar_kind) = wrap_packed_vec_scalar_kind {
|
||||
write!(
|
||||
self.out,
|
||||
"{}::{}3(",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(scalar_kind)
|
||||
)?;
|
||||
write!(self.out, "{}::{}3(", NAMESPACE, scalar_kind.to_msl_name())?;
|
||||
self.put_access_chain(pointer, policy, context)?;
|
||||
write!(self.out, ")")?;
|
||||
} else if is_atomic {
|
||||
@@ -1718,15 +1730,22 @@ impl<W: Write> Writer<W> {
|
||||
};
|
||||
write!(self.out, "{}", ty_name)?;
|
||||
}
|
||||
TypeResolution::Value(crate::TypeInner::Scalar {
|
||||
kind: crate::ScalarKind::Uint,
|
||||
..
|
||||
}) => {
|
||||
// work around Metal toolchain bug with `uint` typedef
|
||||
write!(self.out, "{}::uint", NAMESPACE)?;
|
||||
}
|
||||
TypeResolution::Value(crate::TypeInner::Scalar { kind, .. }) => {
|
||||
write!(self.out, "{}", scalar_kind_string(kind))?;
|
||||
write!(self.out, "{}", kind.to_msl_name())?;
|
||||
}
|
||||
TypeResolution::Value(crate::TypeInner::Vector { size, kind, .. }) => {
|
||||
write!(
|
||||
self.out,
|
||||
"{}::{}{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
back::vector_size_str(size)
|
||||
)?;
|
||||
}
|
||||
@@ -1735,7 +1754,7 @@ impl<W: Write> Writer<W> {
|
||||
self.out,
|
||||
"{}::{}{}x{}",
|
||||
NAMESPACE,
|
||||
scalar_kind_string(crate::ScalarKind::Float),
|
||||
crate::ScalarKind::Float.to_msl_name(),
|
||||
back::vector_size_str(columns),
|
||||
back::vector_size_str(rows),
|
||||
)?;
|
||||
@@ -2317,7 +2336,7 @@ impl<W: Write> Writer<W> {
|
||||
"{}{}::packed_{}3 {};",
|
||||
back::INDENT,
|
||||
NAMESPACE,
|
||||
scalar_kind_string(kind),
|
||||
kind.to_msl_name(),
|
||||
member_name
|
||||
)?;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user