Skip to content

Commit

Permalink
[naga wgsl-in] allow global vars without explicit type (#6199)
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev authored Sep 2, 2024
1 parent 105cb9d commit ace2e20
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 179 deletions.
43 changes: 29 additions & 14 deletions naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,26 +1034,41 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
ctx.globals.insert(f.name.name, lowered_decl);
}
ast::GlobalDeclKind::Var(ref v) => {
let ty = self.resolve_ast_type(v.ty, &mut ctx)?;

let init;
if let Some(init_ast) = v.init {
let mut ectx = ctx.as_override();
let lowered = self.expression_for_abstract(init_ast, &mut ectx)?;
let ty_res = crate::proc::TypeResolution::Handle(ty);
let converted = ectx
.try_automatic_conversions(lowered, &ty_res, v.name.span)
.map_err(|error| match error {
let explicit_ty =
v.ty.map(|ast| self.resolve_ast_type(ast, &mut ctx))
.transpose()?;

let mut ectx = ctx.as_override();

let ty;
let initializer;
match (v.init, explicit_ty) {
(Some(init), Some(explicit_ty)) => {
let init = self.expression_for_abstract(init, &mut ectx)?;
let ty_res = crate::proc::TypeResolution::Handle(explicit_ty);
let init = ectx
.try_automatic_conversions(init, &ty_res, v.name.span)
.map_err(|error| match error {
Error::AutoConversion(e) => Error::InitializationTypeMismatch {
name: v.name.span,
expected: e.dest_type,
got: e.source_type,
},
other => other,
})?;
init = Some(converted);
} else {
init = None;
ty = explicit_ty;
initializer = Some(init);
}
(Some(init), None) => {
let concretized = self.expression(init, &mut ectx)?;
ty = ectx.register_type(concretized)?;
initializer = Some(concretized);
}
(None, Some(explicit_ty)) => {
ty = explicit_ty;
initializer = None;
}
(None, None) => return Err(Error::DeclMissingTypeAndInit(v.name.span)),
}

let binding = if let Some(ref binding) = v.binding {
Expand All @@ -1071,7 +1086,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
space: v.space,
binding,
ty,
init,
init: initializer,
},
span,
);
Expand Down
2 changes: 1 addition & 1 deletion naga/src/front/wgsl/parse/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct GlobalVariable<'a> {
pub name: Ident<'a>,
pub space: crate::AddressSpace,
pub binding: Option<ResourceBinding<'a>>,
pub ty: Handle<Type<'a>>,
pub ty: Option<Handle<Type<'a>>>,
pub init: Option<Handle<Expression<'a>>>,
}

Expand Down
8 changes: 6 additions & 2 deletions naga/src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,12 @@ impl Parser {
lexer.expect(Token::Paren('>'))?;
}
let name = lexer.next_ident()?;
lexer.expect(Token::Separator(':'))?;
let ty = self.type_decl(lexer, ctx)?;

let ty = if lexer.skip(Token::Separator(':')) {
Some(self.type_decl(lexer, ctx)?)
} else {
None
};

let init = if lexer.skip(Token::Operation('=')) {
let handle = self.general_expression(lexer, ctx)?;
Expand Down
14 changes: 14 additions & 0 deletions naga/tests/in/abstract-types-var.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ var<private> xafpaiaf: array<f32, 2> = array(1, 2.0);
var<private> xafpafai: array<f32, 2> = array(1.0, 2);
var<private> xafpafaf: array<f32, 2> = array(1.0, 2.0);

var<private> ivispai = vec2(1);
var<private> ivfspaf = vec2(1.0);
var<private> ivis_ai = vec2<i32>(1);
var<private> ivus_ai = vec2<u32>(1);
var<private> ivfs_ai = vec2<f32>(1);
var<private> ivfs_af = vec2<f32>(1.0);

var<private> iafafaf = array<f32, 2>(1.0, 2.0);
var<private> iafaiai = array<f32, 2>(1, 2);

var<private> iafpafaf = array(1.0, 2.0);
var<private> iafpaiaf = array(1, 2.0);
var<private> iafpafai = array(1.0, 2);

fn all_constant_arguments() {
var xvipaiai: vec2<i32> = vec2(42, 43);
var xvupaiai: vec2<u32> = vec2(44, 45);
Expand Down
Loading

0 comments on commit ace2e20

Please sign in to comment.