mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
fix(naga): Coerce matrix initializers to concrete floats (#8817)
This commit is contained in:
1 parent
70a7f83ea8
commit
a24965f8b7
4 files changed
+43
-35
No files matched your search
@@ -245,6 +245,7 @@ webgpu:shader,validation,expression,call,builtin,trunc:*
|
||||
// FAIL: others in `value_constructor` due to https://github.com/gfx-rs/wgpu/issues/4720, possibly more
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:array_value:*
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:matrix_zero_value:*
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:must_use:ctor="mat4x4";use=true
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:scalar_value:*
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:scalar_zero_value:*
|
||||
webgpu:shader,validation,expression,call,builtin,value_constructor:struct_value:*
|
||||
|
||||
@@ -345,11 +345,11 @@ impl<'source> Lowerer<'source, '_> {
|
||||
},
|
||||
Constructor::PartialVector { size },
|
||||
) => {
|
||||
let consensus_scalar =
|
||||
ctx.automatic_conversion_consensus(&components)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
let consensus_scalar = ctx
|
||||
.automatic_conversion_consensus(None, &components)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
ctx.convert_slice_to_common_leaf_scalar(&mut components, consensus_scalar)?;
|
||||
let inner = consensus_scalar.to_inner_vector(size);
|
||||
let ty = ctx.ensure_type_exists(inner);
|
||||
@@ -373,15 +373,14 @@ impl<'source> Lowerer<'source, '_> {
|
||||
},
|
||||
Constructor::PartialMatrix { columns, rows },
|
||||
) if components.len() == columns as usize * rows as usize => {
|
||||
let consensus_scalar =
|
||||
ctx.automatic_conversion_consensus(&components)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
// We actually only accept floating-point elements.
|
||||
let consensus_scalar = consensus_scalar
|
||||
.automatic_conversion_combine(crate::Scalar::ABSTRACT_FLOAT)
|
||||
.unwrap_or(consensus_scalar);
|
||||
let consensus_scalar = ctx
|
||||
.automatic_conversion_consensus(
|
||||
Some(crate::Scalar::ABSTRACT_FLOAT),
|
||||
&components,
|
||||
)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
ctx.convert_slice_to_common_leaf_scalar(&mut components, consensus_scalar)?;
|
||||
let vec_ty = ctx.ensure_type_exists(consensus_scalar.to_inner_vector(rows));
|
||||
|
||||
@@ -451,11 +450,14 @@ impl<'source> Lowerer<'source, '_> {
|
||||
},
|
||||
Constructor::PartialMatrix { columns, rows },
|
||||
) => {
|
||||
let consensus_scalar =
|
||||
ctx.automatic_conversion_consensus(&components)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
let consensus_scalar = ctx
|
||||
.automatic_conversion_consensus(
|
||||
Some(crate::Scalar::ABSTRACT_FLOAT),
|
||||
&components,
|
||||
)
|
||||
.map_err(|index| {
|
||||
Error::InvalidConstructorComponentType(spans[index], index as i32)
|
||||
})?;
|
||||
ctx.convert_slice_to_common_leaf_scalar(&mut components, consensus_scalar)?;
|
||||
let ty = ctx.ensure_type_exists(crate::TypeInner::Matrix {
|
||||
columns,
|
||||
@@ -489,7 +491,8 @@ impl<'source> Lowerer<'source, '_> {
|
||||
// Array constructor - infer type
|
||||
(components, Constructor::PartialArray) => {
|
||||
let mut components = components.into_components_vec();
|
||||
if let Ok(consensus_scalar) = ctx.automatic_conversion_consensus(&components) {
|
||||
if let Ok(consensus_scalar) = ctx.automatic_conversion_consensus(None, &components)
|
||||
{
|
||||
// Note that this will *not* necessarily convert all the
|
||||
// components to the same type! The `automatic_conversion_consensus`
|
||||
// method only considers the parameters' leaf scalar
|
||||
|
||||
@@ -298,12 +298,17 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
|
||||
/// constructors, return `Err(i)`, where `i` is the index in
|
||||
/// `components` of some problematic argument.
|
||||
///
|
||||
/// If `base` is `Some(scalar)`, the consensus scalar must also be
|
||||
/// compatible with that `scalar`. This is used to restrict matrix
|
||||
/// initializers to floating-point types.
|
||||
///
|
||||
/// This function doesn't fully type-check the arguments - it only
|
||||
/// considers their leaf scalar types. This means it may return `Ok`
|
||||
/// even when the Naga validator will reject the resulting
|
||||
/// construction expression later.
|
||||
pub fn automatic_conversion_consensus<'handle, I>(
|
||||
&self,
|
||||
base: Option<crate::Scalar>,
|
||||
components: I,
|
||||
) -> core::result::Result<crate::Scalar, usize>
|
||||
where
|
||||
@@ -323,18 +328,17 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
);
|
||||
let mut inners = components_iter.map(|&c| self.typifier()[c].inner_with(types));
|
||||
let mut best = inners.next().unwrap().scalar().ok_or(0_usize)?;
|
||||
for (inner, i) in inners.zip(1..) {
|
||||
let scalar = inner.scalar().ok_or(i)?;
|
||||
match best.automatic_conversion_combine(scalar) {
|
||||
Some(new_best) => {
|
||||
best = new_best;
|
||||
}
|
||||
None => return Err(i),
|
||||
}
|
||||
}
|
||||
|
||||
let mut components_iter = components_iter
|
||||
.map(|&c| self.typifier()[c].inner_with(types).scalar())
|
||||
.enumerate();
|
||||
let base = base
|
||||
.or_else(|| components_iter.next().unwrap().1)
|
||||
.ok_or(0usize)?;
|
||||
let best = components_iter.try_fold(base, |best, (i, scalar)| {
|
||||
scalar
|
||||
.and_then(|scalar| best.automatic_conversion_combine(scalar))
|
||||
.ok_or(i)
|
||||
})?;
|
||||
log::debug!(" consensus: {}", best.to_wgsl_for_diagnostics());
|
||||
Ok(best)
|
||||
}
|
||||
|
||||
@@ -1987,7 +1987,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
.collect::<Result<(Vec<_>, Vec<_>)>>()?;
|
||||
|
||||
let mut consensus =
|
||||
ectx.automatic_conversion_consensus(&exprs)
|
||||
ectx.automatic_conversion_consensus(None, &exprs)
|
||||
.map_err(|span_idx| Error::SwitchCaseTypeMismatch {
|
||||
span: spans[span_idx],
|
||||
})?;
|
||||
@@ -2928,7 +2928,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
ctx.grow_types(left)?;
|
||||
ctx.grow_types(right)?;
|
||||
if let Ok(consensus_scalar) =
|
||||
ctx.automatic_conversion_consensus([left, right].iter())
|
||||
ctx.automatic_conversion_consensus(None, [left, right].iter())
|
||||
{
|
||||
ctx.convert_to_leaf_scalar(&mut left, consensus_scalar)?;
|
||||
ctx.convert_to_leaf_scalar(&mut right, consensus_scalar)?;
|
||||
@@ -3123,7 +3123,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
}
|
||||
}
|
||||
let mut consensus_scalar = ctx
|
||||
.automatic_conversion_consensus(&values)
|
||||
.automatic_conversion_consensus(None, &values)
|
||||
.map_err(|_idx| {
|
||||
let [reject, accept] = values;
|
||||
let [(reject_span, reject_type), (accept_span, accept_type)] =
|
||||
|
||||
Reference in new issue
Block a user