Skip to content

Commit

Permalink
wgsl: declare attribtues with @
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 19, 2022
1 parent a1840be commit 1dcab75
Show file tree
Hide file tree
Showing 88 changed files with 609 additions and 702 deletions.
86 changes: 25 additions & 61 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<W: Write> Writer<W> {
],
};

self.write_attributes(&attributes, false)?;
self.write_attributes(&attributes)?;
// Add a newline after attribute
writeln!(self.out)?;

Expand Down Expand Up @@ -244,11 +244,10 @@ impl<W: Write> Writer<W> {
for (index, arg) in func.arguments.iter().enumerate() {
// Write argument attribute if a binding is present
if let Some(ref binding) = arg.binding {
self.write_attributes(
&map_binding_to_attribute(binding, module.types[arg.ty].inner.scalar_kind()),
false,
)?;
write!(self.out, " ")?;
self.write_attributes(&map_binding_to_attribute(
binding,
module.types[arg.ty].inner.scalar_kind(),
))?;
}
// Write argument name
let argument_name = match func_ctx.ty {
Expand All @@ -275,10 +274,10 @@ impl<W: Write> Writer<W> {
if let Some(ref result) = func.result {
write!(self.out, " -> ")?;
if let Some(ref binding) = result.binding {
self.write_attributes(
&map_binding_to_attribute(binding, module.types[result.ty].inner.scalar_kind()),
true,
)?;
self.write_attributes(&map_binding_to_attribute(
binding,
module.types[result.ty].inner.scalar_kind(),
))?;
}
self.write_type(module, result.ty)?;
}
Expand Down Expand Up @@ -331,30 +330,9 @@ impl<W: Write> Writer<W> {
}

/// Helper method to write a attribute
///
/// # Notes
/// Adds an extra space if required
fn write_attributes(&mut self, attributes: &[Attribute], extra_space: bool) -> BackendResult {
write!(self.out, "[[")?;

let mut need_last_comma = true;
if let Some(last_attrib) = attributes.last() {
// We duplicate the logic a little, but this will help to avoid extra heap allocation
match *last_attrib {
Attribute::BuiltIn(builtin_attrib) => {
need_last_comma = builtin_str(builtin_attrib).is_some();
}
Attribute::Interpolate(interpolation, sampling) => {
need_last_comma = (sampling.is_some()
&& sampling != Some(crate::Sampling::Center))
|| (interpolation.is_some()
&& interpolation != Some(crate::Interpolation::Perspective))
}
_ => {}
}
}

for (index, attribute) in attributes.iter().enumerate() {
fn write_attributes(&mut self, attributes: &[Attribute]) -> BackendResult {
for attribute in attributes {
write!(self.out, "@")?;
match *attribute {
Attribute::Location(id) => write!(self.out, "location({})", id)?,
Attribute::BuiltIn(builtin_attrib) => {
Expand All @@ -366,11 +344,11 @@ impl<W: Write> Writer<W> {
}
Attribute::Stage(shader_stage) => {
let stage_str = match shader_stage {
ShaderStage::Vertex => "stage(vertex)",
ShaderStage::Fragment => "stage(fragment)",
ShaderStage::Compute => "stage(compute)",
ShaderStage::Vertex => "vertex",
ShaderStage::Fragment => "fragment",
ShaderStage::Compute => "compute",
};
write!(self.out, "{}", stage_str)?;
write!(self.out, "stage({})", stage_str)?;
}
Attribute::Stride(stride) => write!(self.out, "stride({})", stride)?,
Attribute::WorkGroupSize(size) => {
Expand Down Expand Up @@ -405,19 +383,8 @@ impl<W: Write> Writer<W> {
}
}
};

// Only write a comma if isn't the last element
if index + 1 != attributes.len() && need_last_comma {
// The leading space is for readability only
write!(self.out, ", ")?;
}
}

write!(self.out, "]]")?;
if extra_space {
write!(self.out, " ")?;
}

Ok(())
}

Expand Down Expand Up @@ -447,10 +414,10 @@ impl<W: Write> Writer<W> {
// The indentation is only for readability
write!(self.out, "{}", back::INDENT)?;
if let Some(ref binding) = member.binding {
self.write_attributes(
&map_binding_to_attribute(binding, module.types[member.ty].inner.scalar_kind()),
true,
)?;
self.write_attributes(&map_binding_to_attribute(
binding,
module.types[member.ty].inner.scalar_kind(),
))?;
}
// Write struct member name and type
let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];
Expand All @@ -462,7 +429,7 @@ impl<W: Write> Writer<W> {
stride,
} = module.types[member.ty].inner
{
self.write_attributes(&[Attribute::Stride(stride)], true)?;
self.write_attributes(&[Attribute::Stride(stride)])?;
}
self.write_type(module, member.ty)?;
write!(self.out, ";")?;
Expand Down Expand Up @@ -1716,13 +1683,10 @@ impl<W: Write> Writer<W> {
) -> BackendResult {
// Write group and dinding attributes if present
if let Some(ref binding) = global.binding {
self.write_attributes(
&[
Attribute::Group(binding.group),
Attribute::Binding(binding.binding),
],
false,
)?;
self.write_attributes(&[
Attribute::Group(binding.group),
Attribute::Binding(binding.binding),
])?;
writeln!(self.out)?;
}

Expand Down
16 changes: 4 additions & 12 deletions src/front/wgsl/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ fn consume_token(mut input: &str, generic: bool) -> (Token<'_>, &str) {
_ => (Token::Separator(cur), og_chars),
}
}
'(' | ')' | '{' | '}' => (Token::Paren(cur), chars.as_str()),
'@' => (Token::Attribute, chars.as_str()),
'(' | ')' | '{' | '}' | '[' | ']' => (Token::Paren(cur), chars.as_str()),
'<' | '>' => {
input = chars.as_str();
let next = chars.next();
Expand All @@ -332,14 +333,6 @@ fn consume_token(mut input: &str, generic: bool) -> (Token<'_>, &str) {
(Token::Paren(cur), input)
}
}
'[' | ']' => {
input = chars.as_str();
if chars.next() == Some(cur) {
(Token::DoubleParen(cur), chars.as_str())
} else {
(Token::Paren(cur), input)
}
}
'0'..='9' => consume_number(input),
'_' | 'a'..='z' | 'A'..='Z' => {
let (word, rest) = consume_any(input, |c| c.is_ascii_alphanumeric() || c == '_');
Expand Down Expand Up @@ -678,9 +671,9 @@ fn test_tokens() {
#[test]
fn test_variable_decl() {
sub_test(
"[[ group(0 )]] var< uniform> texture: texture_multisampled_2d <f32 >;",
"@group(0 ) var< uniform> texture: texture_multisampled_2d <f32 >;",
&[
Token::DoubleParen('['),
Token::Attribute,
Token::Word("group"),
Token::Paren('('),
Token::Number {
Expand All @@ -689,7 +682,6 @@ fn test_variable_decl() {
width: None,
},
Token::Paren(')'),
Token::DoubleParen(']'),
Token::Word("var"),
Token::Paren('<'),
Token::Word("uniform"),
Expand Down
Loading

0 comments on commit 1dcab75

Please sign in to comment.