Skip to content

Commit 188277a

Browse files
committed
wip
1 parent 9932236 commit 188277a

2 files changed

Lines changed: 121 additions & 106 deletions

File tree

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

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,26 +2622,37 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
26222622
let mut tl = TemplateListIter::new(ident_span, template_list);
26232623

26242624
if let Some(global) = ctx.globals.get(ident) {
2625-
match global {
2626-
&LoweredGlobalDecl::Type(handle) => {
2627-
tl.finish(ctx)?;
2628-
return Ok(handle);
2629-
}
2630-
_ => return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span))),
2631-
}
2625+
let &LoweredGlobalDecl::Type(handle) = global else {
2626+
return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span)));
2627+
};
2628+
2629+
tl.finish(ctx)?;
2630+
return Ok(handle);
26322631
}
26332632

2634-
let ty = conv::map_predeclared_type(&ctx.enable_extensions, ident_span, ident)?;
2635-
let Some(ty) = ty else {
2636-
return Err(Box::new(Error::UnknownIdent(ident_span, ident)));
2637-
};
2633+
let ty = conv::map_predeclared_type(&ctx.enable_extensions, ident_span, ident)?
2634+
.ok_or_else(|| Box::new(Error::UnknownIdent(ident_span, ident)))?;
26382635
let ty = self.finalize_type(ctx, ty, &mut tl, alias_name)?;
26392636

26402637
tl.finish(ctx)?;
26412638

26422639
Ok(ty)
26432640
}
26442641

2642+
/// Construct a Naga [`Type`] from a [`PredeclaredType`] and a list of
2643+
/// template parameters.
2644+
///
2645+
/// For example, when parsing `vec3<f32>`, the caller would pass:
2646+
///
2647+
/// - for `ty`, [`TypeGenerator::Vector`], and
2648+
///
2649+
/// - for `tl`, an iterator producing a single [`Expression::Ident`] representing `f32`.
2650+
///
2651+
/// From those arguments this function will return a handle for the
2652+
/// [`ir::Type`] representing `vec3<f32>`.
2653+
///
2654+
/// [`TypeGenerator::Vector`]: conv::TypeGenerator::Vector
2655+
/// [`Expression::Ident`]: crate::front::wgsl::parse::ast::Expression::Ident
26452656
fn finalize_type(
26462657
&mut self,
26472658
ctx: &mut ExpressionContext<'source, '_, '_>,
@@ -2651,29 +2662,28 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
26512662
) -> Result<'source, Handle<ir::Type>> {
26522663
let ty = match ty {
26532664
conv::PredeclaredType::TypeInner(ty_inner) => {
2654-
match ty_inner {
2655-
ir::TypeInner::Image {
2656-
class: ir::ImageClass::External,
2657-
..
2658-
} => {
2659-
// Other than the WGSL backend, every backend that supports
2660-
// external textures does so by lowering them to a set of
2661-
// ordinary textures and some parameters saying how to
2662-
// sample from them. We don't know which backend will
2663-
// consume the `Module` we're building, but in case it's not
2664-
// WGSL, populate `SpecialTypes::external_texture_params`
2665-
// and `SpecialTypes::external_texture_transfer_function`
2666-
// with the types the backend will use for the parameter
2667-
// buffer.
2668-
//
2669-
// Neither of these are the type we are lowering here:
2670-
// that's an ordinary `TypeInner::Image`. But the fact we
2671-
// are lowering a `texture_external` implies the backends
2672-
// may need these additional types too.
2673-
ctx.module.generate_external_texture_types();
2674-
}
2675-
_ => {}
2665+
if let ir::TypeInner::Image {
2666+
class: ir::ImageClass::External,
2667+
..
2668+
} = ty_inner
2669+
{
2670+
// Other than the WGSL backend, every backend that supports
2671+
// external textures does so by lowering them to a set of
2672+
// ordinary textures and some parameters saying how to
2673+
// sample from them. We don't know which backend will
2674+
// consume the `Module` we're building, but in case it's not
2675+
// WGSL, populate `SpecialTypes::external_texture_params`
2676+
// and `SpecialTypes::external_texture_transfer_function`
2677+
// with the types the backend will use for the parameter
2678+
// buffer.
2679+
//
2680+
// Neither of these are the type we are lowering here:
2681+
// that's an ordinary `TypeInner::Image`. But the fact we
2682+
// are lowering a `texture_external` implies the backends
2683+
// may need these additional types too.
2684+
ctx.module.generate_external_texture_types();
26762685
}
2686+
26772687
ctx.as_global().ensure_type_exists(alias_name, ty_inner)
26782688
}
26792689
conv::PredeclaredType::RayDesc => ctx.module.generate_ray_desc_type(),
@@ -4378,20 +4388,21 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
43784388
Ok(ir::ArraySize::Constant(size))
43794389
}
43804390
Err(err) => {
4381-
if let Error::ConstantEvaluatorError(ref ty, _) = *err {
4382-
match **ty {
4383-
proc::ConstantEvaluatorError::OverrideExpr => {
4384-
Ok(ir::ArraySize::Pending(self.array_size_override(
4385-
expr,
4386-
&mut ctx.as_global().as_override(),
4387-
span,
4388-
)?))
4389-
}
4390-
_ => Err(err),
4391-
}
4392-
} else {
4393-
Err(err)
4394-
}
4391+
// If the error is simply that `expr` was an override expression, then we
4392+
// can represent that as an array length.
4393+
let Error::ConstantEvaluatorError(ref ty, _) = *err else {
4394+
return Err(err);
4395+
};
4396+
4397+
let proc::ConstantEvaluatorError::OverrideExpr = **ty else {
4398+
return Err(err);
4399+
};
4400+
4401+
Ok(ir::ArraySize::Pending(self.array_size_override(
4402+
expr,
4403+
&mut ctx.as_global().as_override(),
4404+
span,
4405+
)?))
43954406
}
43964407
}
43974408
}

naga/src/front/wgsl/parse/conv.rs

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -402,62 +402,66 @@ pub fn map_predeclared_type(
402402
span: Span,
403403
word: &str,
404404
) -> Result<'static, Option<PredeclaredType>> {
405+
use Scalar as Sc;
406+
use TypeInner as Ti;
407+
use VectorSize as Vs;
408+
405409
#[rustfmt::skip]
406410
let ty = match word {
407411
// predeclared types
408412

409413
// scalars
410-
"bool" => TypeInner::Scalar(Scalar::BOOL).into(),
411-
"i32" => TypeInner::Scalar(Scalar::I32).into(),
412-
"u32" => TypeInner::Scalar(Scalar::U32).into(),
413-
"f32" => TypeInner::Scalar(Scalar::F32).into(),
414-
"f16" => TypeInner::Scalar(Scalar::F16).into(),
415-
"i64" => TypeInner::Scalar(Scalar::I64).into(),
416-
"u64" => TypeInner::Scalar(Scalar::U64).into(),
417-
"f64" => TypeInner::Scalar(Scalar::F64).into(),
414+
"bool" => Ti::Scalar(Sc::BOOL).into(),
415+
"i32" => Ti::Scalar(Sc::I32).into(),
416+
"u32" => Ti::Scalar(Sc::U32).into(),
417+
"f32" => Ti::Scalar(Sc::F32).into(),
418+
"f16" => Ti::Scalar(Sc::F16).into(),
419+
"i64" => Ti::Scalar(Sc::I64).into(),
420+
"u64" => Ti::Scalar(Sc::U64).into(),
421+
"f64" => Ti::Scalar(Sc::F64).into(),
418422
// vector aliases
419-
"vec2i" => TypeInner::Vector { size: VectorSize::Bi, scalar: Scalar::I32 }.into(),
420-
"vec3i" => TypeInner::Vector { size: VectorSize::Tri, scalar: Scalar::I32 }.into(),
421-
"vec4i" => TypeInner::Vector { size: VectorSize::Quad, scalar: Scalar::I32 }.into(),
422-
"vec2u" => TypeInner::Vector { size: VectorSize::Bi, scalar: Scalar::U32 }.into(),
423-
"vec3u" => TypeInner::Vector { size: VectorSize::Tri, scalar: Scalar::U32 }.into(),
424-
"vec4u" => TypeInner::Vector { size: VectorSize::Quad, scalar: Scalar::U32 }.into(),
425-
"vec2f" => TypeInner::Vector { size: VectorSize::Bi, scalar: Scalar::F32 }.into(),
426-
"vec3f" => TypeInner::Vector { size: VectorSize::Tri, scalar: Scalar::F32 }.into(),
427-
"vec4f" => TypeInner::Vector { size: VectorSize::Quad, scalar: Scalar::F32 }.into(),
428-
"vec2h" => TypeInner::Vector { size: VectorSize::Bi, scalar: Scalar::F16 }.into(),
429-
"vec3h" => TypeInner::Vector { size: VectorSize::Tri, scalar: Scalar::F16 }.into(),
430-
"vec4h" => TypeInner::Vector { size: VectorSize::Quad, scalar: Scalar::F16 }.into(),
423+
"vec2i" => Ti::Vector { size: Vs::Bi, scalar: Sc::I32 }.into(),
424+
"vec3i" => Ti::Vector { size: Vs::Tri, scalar: Sc::I32 }.into(),
425+
"vec4i" => Ti::Vector { size: Vs::Quad, scalar: Sc::I32 }.into(),
426+
"vec2u" => Ti::Vector { size: Vs::Bi, scalar: Sc::U32 }.into(),
427+
"vec3u" => Ti::Vector { size: Vs::Tri, scalar: Sc::U32 }.into(),
428+
"vec4u" => Ti::Vector { size: Vs::Quad, scalar: Sc::U32 }.into(),
429+
"vec2f" => Ti::Vector { size: Vs::Bi, scalar: Sc::F32 }.into(),
430+
"vec3f" => Ti::Vector { size: Vs::Tri, scalar: Sc::F32 }.into(),
431+
"vec4f" => Ti::Vector { size: Vs::Quad, scalar: Sc::F32 }.into(),
432+
"vec2h" => Ti::Vector { size: Vs::Bi, scalar: Sc::F16 }.into(),
433+
"vec3h" => Ti::Vector { size: Vs::Tri, scalar: Sc::F16 }.into(),
434+
"vec4h" => Ti::Vector { size: Vs::Quad, scalar: Sc::F16 }.into(),
431435
// matrix aliases
432-
"mat2x2f" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Bi, scalar: Scalar::F32 }.into(),
433-
"mat2x3f" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Tri, scalar: Scalar::F32 }.into(),
434-
"mat2x4f" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Quad, scalar: Scalar::F32 }.into(),
435-
"mat3x2f" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Bi, scalar: Scalar::F32 }.into(),
436-
"mat3x3f" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Tri, scalar: Scalar::F32 }.into(),
437-
"mat3x4f" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Quad, scalar: Scalar::F32 }.into(),
438-
"mat4x2f" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Bi, scalar: Scalar::F32 }.into(),
439-
"mat4x3f" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Tri, scalar: Scalar::F32 }.into(),
440-
"mat4x4f" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Quad, scalar: Scalar::F32 }.into(),
441-
"mat2x2h" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Bi, scalar: Scalar::F16 }.into(),
442-
"mat2x3h" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Tri, scalar: Scalar::F16 }.into(),
443-
"mat2x4h" => TypeInner::Matrix { columns: VectorSize::Bi, rows: VectorSize::Quad, scalar: Scalar::F16 }.into(),
444-
"mat3x2h" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Bi, scalar: Scalar::F16 }.into(),
445-
"mat3x3h" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Tri, scalar: Scalar::F16 }.into(),
446-
"mat3x4h" => TypeInner::Matrix { columns: VectorSize::Tri, rows: VectorSize::Quad, scalar: Scalar::F16 }.into(),
447-
"mat4x2h" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Bi, scalar: Scalar::F16 }.into(),
448-
"mat4x3h" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Tri, scalar: Scalar::F16 }.into(),
449-
"mat4x4h" => TypeInner::Matrix { columns: VectorSize::Quad, rows: VectorSize::Quad, scalar: Scalar::F16 }.into(),
436+
"mat2x2f" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Bi, scalar: Sc::F32 }.into(),
437+
"mat2x3f" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Tri, scalar: Sc::F32 }.into(),
438+
"mat2x4f" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Quad, scalar: Sc::F32 }.into(),
439+
"mat3x2f" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Bi, scalar: Sc::F32 }.into(),
440+
"mat3x3f" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Tri, scalar: Sc::F32 }.into(),
441+
"mat3x4f" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Quad, scalar: Sc::F32 }.into(),
442+
"mat4x2f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi, scalar: Sc::F32 }.into(),
443+
"mat4x3f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri, scalar: Sc::F32 }.into(),
444+
"mat4x4f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F32 }.into(),
445+
"mat2x2h" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Bi, scalar: Sc::F16 }.into(),
446+
"mat2x3h" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Tri, scalar: Sc::F16 }.into(),
447+
"mat2x4h" => Ti::Matrix { columns: Vs::Bi, rows: Vs::Quad, scalar: Sc::F16 }.into(),
448+
"mat3x2h" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Bi, scalar: Sc::F16 }.into(),
449+
"mat3x3h" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Tri, scalar: Sc::F16 }.into(),
450+
"mat3x4h" => Ti::Matrix { columns: Vs::Tri, rows: Vs::Quad, scalar: Sc::F16 }.into(),
451+
"mat4x2h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi, scalar: Sc::F16 }.into(),
452+
"mat4x3h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri, scalar: Sc::F16 }.into(),
453+
"mat4x4h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F16 }.into(),
450454
// samplers
451-
"sampler" => TypeInner::Sampler { comparison: false }.into(),
452-
"sampler_comparison" => TypeInner::Sampler { comparison: true }.into(),
455+
"sampler" => Ti::Sampler { comparison: false }.into(),
456+
"sampler_comparison" => Ti::Sampler { comparison: true }.into(),
453457
// depth textures
454-
"texture_depth_2d" => TypeInner::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
455-
"texture_depth_2d_array" => TypeInner::Image { dim: ImageDimension::D2, arrayed: true, class: ImageClass::Depth { multi: false } }.into(),
456-
"texture_depth_cube" => TypeInner::Image { dim: ImageDimension::Cube, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
457-
"texture_depth_cube_array" => TypeInner::Image { dim: ImageDimension::Cube, arrayed: true, class: ImageClass::Depth { multi: false } }.into(),
458-
"texture_depth_multisampled_2d" => TypeInner::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::Depth { multi: true } }.into(),
458+
"texture_depth_2d" => Ti::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
459+
"texture_depth_2d_array" => Ti::Image { dim: ImageDimension::D2, arrayed: true, class: ImageClass::Depth { multi: false } }.into(),
460+
"texture_depth_cube" => Ti::Image { dim: ImageDimension::Cube, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
461+
"texture_depth_cube_array" => Ti::Image { dim: ImageDimension::Cube, arrayed: true, class: ImageClass::Depth { multi: false } }.into(),
462+
"texture_depth_multisampled_2d" => Ti::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::Depth { multi: true } }.into(),
459463
// external texture
460-
"texture_external" => TypeInner::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::External }.into(),
464+
"texture_external" => Ti::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::External }.into(),
461465
// ray desc
462466
"RayDesc" => PredeclaredType::RayDesc,
463467
// ray intersection
@@ -466,19 +470,19 @@ pub fn map_predeclared_type(
466470
// predeclared type generators
467471

468472
// vector
469-
"vec2" => TypeGenerator::Vector { size: VectorSize::Bi }.into(),
470-
"vec3" => TypeGenerator::Vector { size: VectorSize::Tri }.into(),
471-
"vec4" => TypeGenerator::Vector { size: VectorSize::Quad }.into(),
473+
"vec2" => TypeGenerator::Vector { size: Vs::Bi }.into(),
474+
"vec3" => TypeGenerator::Vector { size: Vs::Tri }.into(),
475+
"vec4" => TypeGenerator::Vector { size: Vs::Quad }.into(),
472476
// matrix
473-
"mat2x2" => TypeGenerator::Matrix { columns: VectorSize::Bi, rows: VectorSize::Bi }.into(),
474-
"mat2x3" => TypeGenerator::Matrix { columns: VectorSize::Bi, rows: VectorSize::Tri }.into(),
475-
"mat2x4" => TypeGenerator::Matrix { columns: VectorSize::Bi, rows: VectorSize::Quad }.into(),
476-
"mat3x2" => TypeGenerator::Matrix { columns: VectorSize::Tri, rows: VectorSize::Bi }.into(),
477-
"mat3x3" => TypeGenerator::Matrix { columns: VectorSize::Tri, rows: VectorSize::Tri }.into(),
478-
"mat3x4" => TypeGenerator::Matrix { columns: VectorSize::Tri, rows: VectorSize::Quad }.into(),
479-
"mat4x2" => TypeGenerator::Matrix { columns: VectorSize::Quad, rows: VectorSize::Bi }.into(),
480-
"mat4x3" => TypeGenerator::Matrix { columns: VectorSize::Quad, rows: VectorSize::Tri }.into(),
481-
"mat4x4" => TypeGenerator::Matrix { columns: VectorSize::Quad, rows: VectorSize::Quad }.into(),
477+
"mat2x2" => TypeGenerator::Matrix { columns: Vs::Bi, rows: Vs::Bi }.into(),
478+
"mat2x3" => TypeGenerator::Matrix { columns: Vs::Bi, rows: Vs::Tri }.into(),
479+
"mat2x4" => TypeGenerator::Matrix { columns: Vs::Bi, rows: Vs::Quad }.into(),
480+
"mat3x2" => TypeGenerator::Matrix { columns: Vs::Tri, rows: Vs::Bi }.into(),
481+
"mat3x3" => TypeGenerator::Matrix { columns: Vs::Tri, rows: Vs::Tri }.into(),
482+
"mat3x4" => TypeGenerator::Matrix { columns: Vs::Tri, rows: Vs::Quad }.into(),
483+
"mat4x2" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Bi }.into(),
484+
"mat4x3" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Tri }.into(),
485+
"mat4x4" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Quad }.into(),
482486
// array
483487
"array" => TypeGenerator::Array.into(),
484488
// atomic
@@ -507,7 +511,7 @@ pub fn map_predeclared_type(
507511
_ => return Ok(None),
508512
};
509513

510-
if matches!(ty, PredeclaredType::TypeInner(ref ty) if ty.scalar() == Some(Scalar::F16))
514+
if matches!(ty, PredeclaredType::TypeInner(ref ty) if ty.scalar() == Some(Sc::F16))
511515
&& !enable_extensions.contains(ImplementedEnableExtension::F16)
512516
{
513517
return Err(Box::new(Error::EnableExtensionNotEnabled {

0 commit comments

Comments
 (0)