Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glsl-in: Type qualifiers rework #1713

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 117 additions & 12 deletions src/front/glsl/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{borrow::Cow, fmt};

use super::{builtins::MacroCall, context::ExprPos, Span};
use crate::{
Expand Down Expand Up @@ -134,19 +134,118 @@ pub enum HirExprKind {
},
}

#[derive(Debug, Hash, PartialEq, Eq)]
pub enum QualifierKey<'a> {
String(Cow<'a, str>),
/// Used for `std140` and `std430` layout qualifiers
Layout,
kvark marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug)]
pub enum TypeQualifier {
StorageQualifier(StorageQualifier),
Interpolation(Interpolation),
Set(u32),
Binding(u32),
Location(u32),
WorkGroupSize(usize, u32),
Sampling(Sampling),
pub enum QualifierValue {
None,
Uint(u32),
Layout(StructLayout),
Precision(Precision),
EarlyFragmentTests,
StorageAccess(StorageAccess),
}

#[derive(Debug, Default)]
pub struct TypeQualifiers<'a> {
pub span: Span,
pub storage: (StorageQualifier, Span),
pub interpolation: Option<(Interpolation, Span)>,
pub precision: Option<(Precision, Span)>,
pub sampling: Option<(Sampling, Span)>,
pub storage_acess: Option<(StorageAccess, Span)>,
pub layout_qualifiers: crate::FastHashMap<QualifierKey<'a>, (QualifierValue, Span)>,
}

impl<'a> TypeQualifiers<'a> {
/// Appends `errors` with errors for all unused qualifiers
pub fn unused_errors(&self, errors: &mut Vec<super::Error>) {
if let Some((_, meta)) = self.interpolation {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError(
"Interpolation qualifiers can only be used in in/out variables".into(),
),
meta,
});
}

if let Some((_, meta)) = self.sampling {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError(
"Sampling qualifiers can only be used in in/out variables".into(),
),
meta,
});
}

if let Some((_, meta)) = self.storage_acess {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError(
"Memory qualifiers can only be used in storage variables".into(),
),
meta,
});
}

for &(_, meta) in self.layout_qualifiers.values() {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError("Unexpected qualifier".into()),
meta,
});
}
}

/// Removes the layout qualifier with `name`, if it exists and adds an error if it isn't
/// a [`QualifierValue::Uint`]
pub fn uint_layout_qualifier(
&mut self,
name: &'a str,
errors: &mut Vec<super::Error>,
) -> Option<u32> {
match self
.layout_qualifiers
.remove(&QualifierKey::String(name.into()))
{
Some((QualifierValue::Uint(v), _)) => Some(v),
Some((_, meta)) => {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError("Qualifier expects a uint value".into()),
meta,
});
// Return a dummy value instead of `None` to differentiate from
// the qualifier not existing, since some parts might require the
// qualifier to exist and throwing another error that it doesn't
// exist would be unhelpful
Some(0)
}
_ => None,
}
}

/// Removes the layout qualifier with `name`, if it exists and adds an error if it isn't
/// a [`QualifierValue::None`]
pub fn none_layout_qualifier(&mut self, name: &'a str, errors: &mut Vec<super::Error>) -> bool {
match self
.layout_qualifiers
.remove(&QualifierKey::String(name.into()))
{
Some((QualifierValue::None, _)) => true,
Some((_, meta)) => {
errors.push(super::Error {
kind: super::ErrorKind::SemanticError(
"Qualifier doesn't expect a value".into(),
),
meta,
});
// Return a `true` to since the qualifier is defined and adding
// another error for it not being defined would be unhelpful
true
}
_ => false,
}
}
}

#[derive(Debug, Clone)]
Expand All @@ -169,6 +268,12 @@ pub enum StorageQualifier {
Const,
}

impl Default for StorageQualifier {
fn default() -> Self {
StorageQualifier::AddressSpace(AddressSpace::Function)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StructLayout {
Std140,
Expand Down
10 changes: 5 additions & 5 deletions src/front/glsl/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
ast::{FunctionKind, Profile, TypeQualifier},
ast::{FunctionKind, Profile, TypeQualifiers},
context::{Context, ExprPos},
error::ExpectedToken,
error::{Error, ErrorKind},
Expand Down Expand Up @@ -366,15 +366,15 @@ impl Parser {
}
}

pub struct DeclarationContext<'ctx> {
qualifiers: Vec<(TypeQualifier, Span)>,
pub struct DeclarationContext<'ctx, 'qualifiers> {
qualifiers: TypeQualifiers<'qualifiers>,
external: bool,

ctx: &'ctx mut Context,
body: &'ctx mut Block,
}

impl<'ctx> DeclarationContext<'ctx> {
impl<'ctx, 'qualifiers> DeclarationContext<'ctx, 'qualifiers> {
fn add_var(
&mut self,
parser: &mut Parser,
Expand All @@ -384,7 +384,7 @@ impl<'ctx> DeclarationContext<'ctx> {
meta: Span,
) -> Result<Handle<Expression>> {
let decl = VarDeclaration {
qualifiers: &self.qualifiers,
qualifiers: &mut self.qualifiers,
ty,
name: Some(name),
init,
Expand Down
80 changes: 35 additions & 45 deletions src/front/glsl/parser/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
front::glsl::{
ast::{
GlobalLookup, GlobalLookupKind, Precision, StorageQualifier, StructLayout,
TypeQualifier,
GlobalLookup, GlobalLookupKind, Precision, QualifierKey, QualifierValue,
StorageQualifier, StructLayout, TypeQualifiers,
},
context::{Context, ExprPos},
error::ExpectedToken,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<'source> ParsingContext<'source> {
// type_qualifier IDENTIFIER identifier_list SEMICOLON

if self.peek_type_qualifier(parser) || self.peek_type_name(parser) {
let qualifiers = self.parse_type_qualifiers(parser)?;
let mut qualifiers = self.parse_type_qualifiers(parser)?;

if self.peek_type_name(parser) {
// This branch handles variables and function prototypes and if
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<'source> ParsingContext<'source> {
parser,
ctx,
body,
&qualifiers,
&mut qualifiers,
ty_name,
token.meta,
)
Expand All @@ -377,33 +377,28 @@ impl<'source> ParsingContext<'source> {
}
}
TokenValue::Semicolon => {
let mut meta_all = token.meta;
for &(ref qualifier, meta) in qualifiers.iter() {
meta_all.subsume(meta);
match *qualifier {
TypeQualifier::WorkGroupSize(i, value) => {
parser.meta.workgroup_size[i] = value
}
TypeQualifier::EarlyFragmentTests => {
parser.meta.early_fragment_tests = true;
}
TypeQualifier::StorageQualifier(_) => {
// TODO: Maybe add some checks here
// This is needed because of cases like
// layout(early_fragment_tests) in;
}
_ => {
parser.errors.push(Error {
kind: ErrorKind::SemanticError(
"Qualifier not supported as standalone".into(),
),
meta,
});
}
}
if let Some(value) =
qualifiers.uint_layout_qualifier("local_size_x", &mut parser.errors)
{
parser.meta.workgroup_size[0] = value;
}
if let Some(value) =
qualifiers.uint_layout_qualifier("local_size_y", &mut parser.errors)
{
parser.meta.workgroup_size[1] = value;
}
if let Some(value) =
qualifiers.uint_layout_qualifier("local_size_z", &mut parser.errors)
{
parser.meta.workgroup_size[2] = value;
}

parser.meta.early_fragment_tests |= qualifiers
.none_layout_qualifier("early_fragment_tests", &mut parser.errors);

Ok(Some(meta_all))
qualifiers.unused_errors(&mut parser.errors);

Ok(Some(qualifiers.span))
}
_ => Err(Error {
kind: ErrorKind::InvalidToken(
Expand Down Expand Up @@ -471,27 +466,22 @@ impl<'source> ParsingContext<'source> {
parser: &mut Parser,
ctx: &mut Context,
body: &mut Block,
qualifiers: &[(TypeQualifier, Span)],
qualifiers: &mut TypeQualifiers,
ty_name: String,
meta: Span,
) -> Result<Span> {
let mut storage = None;
let mut layout = None;

for &(ref qualifier, _) in qualifiers {
match *qualifier {
TypeQualifier::StorageQualifier(StorageQualifier::AddressSpace(c)) => {
storage = Some(c)
let layout = match qualifiers.layout_qualifiers.remove(&QualifierKey::Layout) {
Some((QualifierValue::Layout(l), _)) => l,
None => {
if let StorageQualifier::AddressSpace(AddressSpace::Storage { .. }) =
qualifiers.storage.0
{
StructLayout::Std430
} else {
StructLayout::Std140
}
TypeQualifier::Layout(l) => layout = Some(l),
_ => continue,
}
}

let layout = match (layout, storage) {
(Some(layout), _) => layout,
(None, Some(AddressSpace::Storage { .. })) => StructLayout::Std430,
_ => StructLayout::Std140,
_ => unreachable!(),
};

let mut members = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions src/front/glsl/parser/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl<'source> ParsingContext<'source> {
if self.bump_if(parser, TokenValue::Semicolon).is_none() {
let (expr, expr_meta) =
if self.peek_type_name(parser) || self.peek_type_qualifier(parser) {
let qualifiers = self.parse_type_qualifiers(parser)?;
let mut qualifiers = self.parse_type_qualifiers(parser)?;
let (ty, mut meta) = self.parse_type_non_void(parser)?;
let name = self.expect_ident(parser)?.0;

Expand All @@ -407,7 +407,7 @@ impl<'source> ParsingContext<'source> {
meta.subsume(end_meta);

let decl = VarDeclaration {
qualifiers: &qualifiers,
qualifiers: &mut qualifiers,
ty,
name: Some(name),
init: None,
Expand Down
Loading