Skip to content

Commit

Permalink
[glsl-in] Support anonymous global uniforms
Browse files Browse the repository at this point in the history
Partly fixes gfx-rs#210
  • Loading branch information
pjoe committed Sep 21, 2020
1 parent 0651eb8 commit 7bdcac8
Show file tree
Hide file tree
Showing 3 changed files with 49 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
19 changes: 11 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 @@ -871,15 +872,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
37 changes: 37 additions & 0 deletions src/front/glsl/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ use super::ast::*;
use super::error::ErrorKind;

impl Program {
fn lookup_global_struct_member(&mut self, name: &str) -> Option<Handle<Expression>> {
let global_struct_member = self
.module
.global_variables
.iter()
// find globals without name
.filter(|(_, var)| var.name.is_none())
// find those of struct type and map to their members
.filter_map(|(h, var)| {
let ty = &self.module.types[var.ty];
if let TypeInner::Struct { members } = &ty.inner {
Some((h, members))
} else {
None
}
})
// find member matching name
.find_map(|(h, members)| {
members
.iter()
.position(|m| m.name == Some(name.into()))
.map(|idx| (h, idx))
});
global_struct_member.map(|(h, idx)| {
let base = self
.context
.expressions
.append(Expression::GlobalVariable(h));
self.context.expressions.append(Expression::AccessIndex {
base,
index: idx as u32,
})
})
}

pub fn lookup_variable(&mut self, name: &str) -> Result<Option<Handle<Expression>>, ErrorKind> {
let mut expression: Option<Handle<Expression>> = None;
match name {
Expand Down Expand Up @@ -92,6 +127,8 @@ impl Program {
Ok(Some(local_var))
} else if let Some(global_var) = self.context.lookup_global_var_exps.get(name) {
Ok(Some(*global_var))
} else if let Some(global_member) = self.lookup_global_struct_member(name) {
Ok(Some(global_member))
} else {
Ok(None)
}
Expand Down

0 comments on commit 7bdcac8

Please sign in to comment.