Skip to content

Commit

Permalink
[glsl-in] Support anonymous global uniforms (#211)
Browse files Browse the repository at this point in the history
* [glsl-in] Support anonymous global uniforms

Partly fixes #210

* [glsl-in] Handle anon global structs in fn prelude
  • Loading branch information
pjoe authored Sep 22, 2020
1 parent 8c48ae5 commit e5d17e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/front/glsl/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub enum TypeQualifier {
#[derive(Debug)]
pub struct VarDeclaration {
pub type_qualifiers: Vec<TypeQualifier>,
pub ids_initializers: Vec<(String, Option<ExpressionRule>)>,
pub ids_initializers: Vec<(Option<String>, Option<ExpressionRule>)>,
pub ty: Handle<Type>,
}

Expand Down
38 changes: 30 additions & 8 deletions src/front/glsl/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pomelo! {
struct_declaration_list(sdl) RightBrace Semicolon {
VarDeclaration{
type_qualifiers: t,
ids_initializers: vec![],
ids_initializers: vec![(None, None)],
ty: extra.module.types.fetch_or_append(Type{
name: Some(i.1),
inner: TypeInner::Struct {
Expand All @@ -489,7 +489,7 @@ pomelo! {
struct_declaration_list(sdl) RightBrace Identifier(i2) Semicolon {
VarDeclaration{
type_qualifiers: t,
ids_initializers: vec![(i2.1, None)],
ids_initializers: vec![(Some(i2.1), None)],
ty: extra.module.types.fetch_or_append(Type{
name: Some(i1.1),
inner: TypeInner::Struct {
Expand All @@ -504,13 +504,13 @@ pomelo! {

init_declarator_list ::= single_declaration;
init_declarator_list ::= init_declarator_list(mut idl) Comma Identifier(i) {
idl.ids_initializers.push((i.1, None));
idl.ids_initializers.push((Some(i.1), None));
idl
}
// init_declarator_list ::= init_declarator_list Comma Identifier array_specifier;
// init_declarator_list ::= init_declarator_list Comma Identifier array_specifier Equal initializer;
init_declarator_list ::= init_declarator_list(mut idl) Comma Identifier(i) Equal initializer(init) {
idl.ids_initializers.push((i.1, Some(init)));
idl.ids_initializers.push((Some(i.1), Some(init)));
idl
}

Expand All @@ -528,7 +528,7 @@ pomelo! {

VarDeclaration{
type_qualifiers: t.0,
ids_initializers: vec![(i.1, None)],
ids_initializers: vec![(Some(i.1), None)],
ty,
}
}
Expand All @@ -539,7 +539,7 @@ pomelo! {

VarDeclaration{
type_qualifiers: t.0,
ids_initializers: vec![(i.1, Some(init))],
ids_initializers: vec![(Some(i.1), Some(init))],
ty,
}
}
Expand Down Expand Up @@ -705,6 +705,7 @@ pomelo! {
let mut statements = Vec::<Statement>::new();
// local variables
for (id, initializer) in d.ids_initializers {
let id = id.ok_or(ErrorKind::SemanticError("local var must be named"))?;
// check if already declared in current scope
#[cfg(feature = "glsl-validate")]
{
Expand Down Expand Up @@ -793,6 +794,25 @@ pomelo! {
Expression::GlobalVariable(var_handle)
);
extra.context.lookup_global_var_exps.insert(name.clone(), exp);
} else {
let ty = &extra.module.types[var.ty];
// anonymous structs
if let TypeInner::Struct { members } = &ty.inner {
let base = extra.context.expressions.append(
Expression::GlobalVariable(var_handle)
);
for (idx, member) in members.iter().enumerate() {
if let Some(name) = member.name.as_ref() {
let exp = extra.context.expressions.append(
Expression::AccessIndex{
base,
index: idx as u32,
}
);
extra.context.lookup_global_var_exps.insert(name.clone(), exp);
}
}
}
}
}
f
Expand Down Expand Up @@ -871,15 +891,17 @@ pomelo! {
for (id, initializer) in d.ids_initializers {
let h = extra.module.global_variables.fetch_or_append(
GlobalVariable {
name: Some(id.clone()),
name: id.clone(),
class,
binding: binding.clone(),
ty: d.ty,
interpolation,
storage_access: StorageAccess::empty(), //TODO
},
);
extra.lookup_global_variables.insert(id, h);
if let Some(id) = id {
extra.lookup_global_variables.insert(id, h);
}
}
}

Expand Down

0 comments on commit e5d17e8

Please sign in to comment.