Skip to content

Commit 7c0c43b

Browse files
committed
[naga] Move TypeInner::automatically_converts_to into proc.
Make the associated function `TypeInner::automatically_converts_to` available outside the WGSL front end by moving it into the `proc::type_methods` module.
1 parent 57a3bb3 commit 7c0c43b

File tree

2 files changed

+105
-90
lines changed

2 files changed

+105
-90
lines changed

naga/src/front/wgsl/lower/conversion.rs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -338,96 +338,6 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
338338
}
339339

340340
impl crate::TypeInner {
341-
/// Determine whether `self` automatically converts to `goal`.
342-
///
343-
/// If WGSL's automatic conversions (excluding the Load Rule) will
344-
/// convert `self` to `goal`, then return a pair `(from, to)`,
345-
/// where `from` and `to` are the scalar types of the leaf values
346-
/// of `self` and `goal`.
347-
///
348-
/// This function assumes that `self` and `goal` are different
349-
/// types. Callers should first check whether any conversion is
350-
/// needed at all.
351-
///
352-
/// If the automatic conversions cannot convert `self` to `goal`,
353-
/// return `None`.
354-
fn automatically_converts_to(
355-
&self,
356-
goal: &Self,
357-
types: &crate::UniqueArena<crate::Type>,
358-
) -> Option<(crate::Scalar, crate::Scalar)> {
359-
use crate::ScalarKind as Sk;
360-
use crate::TypeInner as Ti;
361-
362-
// Automatic conversions only change the scalar type of a value's leaves
363-
// (e.g., `vec4<AbstractFloat>` to `vec4<f32>`), never the type
364-
// constructors applied to those scalar types (e.g., never scalar to
365-
// `vec4`, or `vec2` to `vec3`). So first we check that the type
366-
// constructors match, extracting the leaf scalar types in the process.
367-
let expr_scalar;
368-
let goal_scalar;
369-
match (self, goal) {
370-
(&Ti::Scalar(expr), &Ti::Scalar(goal)) => {
371-
expr_scalar = expr;
372-
goal_scalar = goal;
373-
}
374-
(
375-
&Ti::Vector {
376-
size: expr_size,
377-
scalar: expr,
378-
},
379-
&Ti::Vector {
380-
size: goal_size,
381-
scalar: goal,
382-
},
383-
) if expr_size == goal_size => {
384-
expr_scalar = expr;
385-
goal_scalar = goal;
386-
}
387-
(
388-
&Ti::Matrix {
389-
rows: expr_rows,
390-
columns: expr_columns,
391-
scalar: expr,
392-
},
393-
&Ti::Matrix {
394-
rows: goal_rows,
395-
columns: goal_columns,
396-
scalar: goal,
397-
},
398-
) if expr_rows == goal_rows && expr_columns == goal_columns => {
399-
expr_scalar = expr;
400-
goal_scalar = goal;
401-
}
402-
(
403-
&Ti::Array {
404-
base: expr_base,
405-
size: expr_size,
406-
stride: _,
407-
},
408-
&Ti::Array {
409-
base: goal_base,
410-
size: goal_size,
411-
stride: _,
412-
},
413-
) if expr_size == goal_size => {
414-
return types[expr_base]
415-
.inner
416-
.automatically_converts_to(&types[goal_base].inner, types);
417-
}
418-
_ => return None,
419-
}
420-
421-
match (expr_scalar.kind, goal_scalar.kind) {
422-
(Sk::AbstractFloat, Sk::Float) => {}
423-
(Sk::AbstractInt, Sk::Sint | Sk::Uint | Sk::AbstractFloat | Sk::Float) => {}
424-
_ => return None,
425-
}
426-
427-
log::trace!(" okay: expr {expr_scalar:?}, goal {goal_scalar:?}");
428-
Some((expr_scalar, goal_scalar))
429-
}
430-
431341
fn automatically_convertible_scalar(
432342
&self,
433343
types: &crate::UniqueArena<crate::Type>,

naga/src/proc/type_methods.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,109 @@ impl crate::TypeInner {
303303
| crate::TypeInner::BindingArray { .. } => false,
304304
}
305305
}
306+
307+
/// Determine whether `self` automatically converts to `goal`.
308+
///
309+
/// If Naga IR's automatic conversions will convert `self` to
310+
/// `goal`, then return a pair `(from, to)`, where `from` and `to`
311+
/// are the scalar types of the leaf values of `self` and `goal`.
312+
///
313+
/// If `self` and `goal` are the same type, this will simply return
314+
/// a pair `(S, S)`.
315+
///
316+
/// If the automatic conversions cannot convert `self` to `goal`,
317+
/// return `None`.
318+
///
319+
/// Naga IR's automatic conversions will convert:
320+
///
321+
/// - [`AbstractInt`] scalars to [`AbstractFloat`] or any numeric scalar type
322+
///
323+
/// - [`AbstractFloat`] scalars to any floating-point scalar type
324+
///
325+
/// - A [`Vector`] `{ size, scalar: S }` to `{ size, scalar: T }`
326+
/// if they would convert `S` to `T`
327+
///
328+
/// - An [`Array`] `{ base: S, size, stride }` to `{ base: T, size, stride }`
329+
/// if they would convert `S` to `T`
330+
///
331+
/// [`AbstractInt`]: crate::ScalarKind::AbstractInt
332+
/// [`AbstractFloat`]: crate::ScalarKind::AbstractFloat
333+
/// [`Vector`]: crate::TypeInner::Vector
334+
/// [`Array`]: crate::TypeInner::Array
335+
pub fn automatically_converts_to(
336+
&self,
337+
goal: &Self,
338+
types: &crate::UniqueArena<crate::Type>,
339+
) -> Option<(crate::Scalar, crate::Scalar)> {
340+
use crate::ScalarKind as Sk;
341+
use crate::TypeInner as Ti;
342+
343+
// Automatic conversions only change the scalar type of a value's leaves
344+
// (e.g., `vec4<AbstractFloat>` to `vec4<f32>`), never the type
345+
// constructors applied to those scalar types (e.g., never scalar to
346+
// `vec4`, or `vec2` to `vec3`). So first we check that the type
347+
// constructors match, extracting the leaf scalar types in the process.
348+
let expr_scalar;
349+
let goal_scalar;
350+
match (self, goal) {
351+
(&Ti::Scalar(expr), &Ti::Scalar(goal)) => {
352+
expr_scalar = expr;
353+
goal_scalar = goal;
354+
}
355+
(
356+
&Ti::Vector {
357+
size: expr_size,
358+
scalar: expr,
359+
},
360+
&Ti::Vector {
361+
size: goal_size,
362+
scalar: goal,
363+
},
364+
) if expr_size == goal_size => {
365+
expr_scalar = expr;
366+
goal_scalar = goal;
367+
}
368+
(
369+
&Ti::Matrix {
370+
rows: expr_rows,
371+
columns: expr_columns,
372+
scalar: expr,
373+
},
374+
&Ti::Matrix {
375+
rows: goal_rows,
376+
columns: goal_columns,
377+
scalar: goal,
378+
},
379+
) if expr_rows == goal_rows && expr_columns == goal_columns => {
380+
expr_scalar = expr;
381+
goal_scalar = goal;
382+
}
383+
(
384+
&Ti::Array {
385+
base: expr_base,
386+
size: expr_size,
387+
stride: _,
388+
},
389+
&Ti::Array {
390+
base: goal_base,
391+
size: goal_size,
392+
stride: _,
393+
},
394+
) if expr_size == goal_size => {
395+
return types[expr_base]
396+
.inner
397+
.automatically_converts_to(&types[goal_base].inner, types);
398+
}
399+
_ => return None,
400+
}
401+
402+
match (expr_scalar.kind, goal_scalar.kind) {
403+
(Sk::AbstractFloat, Sk::Float) => {}
404+
(Sk::AbstractInt, Sk::Sint | Sk::Uint | Sk::AbstractFloat | Sk::Float) => {}
405+
_ => return None,
406+
}
407+
408+
log::trace!(" okay: expr {expr_scalar:?}, goal {goal_scalar:?}");
409+
Some((expr_scalar, goal_scalar))
410+
}
306411
}

0 commit comments

Comments
 (0)