From 4c68d74905f84cfde7c07e5912beedfc8ede00eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Thu, 17 Mar 2022 23:01:00 +0000 Subject: [PATCH] glsl-in: Fix memory qualifiers being inverted Adds some documentation to better explain how the memory qualifier works troughout the parser and some storage textures tests. --- src/front/glsl/ast.rs | 2 ++ src/front/glsl/lex.rs | 4 ++-- src/front/glsl/parser/types.rs | 4 ++-- src/front/glsl/token.rs | 6 +++++- tests/in/glsl/images.frag | 18 ++++++++++++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/front/glsl/ast.rs b/src/front/glsl/ast.rs index 9f28b2bdd7..c04425d069 100644 --- a/src/front/glsl/ast.rs +++ b/src/front/glsl/ast.rs @@ -175,6 +175,8 @@ pub struct TypeQualifiers<'a> { pub interpolation: Option<(Interpolation, Span)>, pub precision: Option<(Precision, Span)>, pub sampling: Option<(Sampling, Span)>, + /// Memory qualifiers used in the declaration to reduce storage access to be used + /// in declarations that support it (storage images and buffers) pub storage_acess: Option<(StorageAccess, Span)>, pub layout_qualifiers: crate::FastHashMap, (QualifierValue, Span)>, } diff --git a/src/front/glsl/lex.rs b/src/front/glsl/lex.rs index a96f6a701f..bbf400f51f 100644 --- a/src/front/glsl/lex.rs +++ b/src/front/glsl/lex.rs @@ -84,8 +84,8 @@ impl<'a> Iterator for Lexer<'a> { "mediump" => TokenValue::PrecisionQualifier(Precision::Medium), "lowp" => TokenValue::PrecisionQualifier(Precision::Low), "restrict" => TokenValue::Restrict, - "readonly" => TokenValue::StorageAccess(StorageAccess::LOAD), - "writeonly" => TokenValue::StorageAccess(StorageAccess::STORE), + "readonly" => TokenValue::MemoryQualifier(StorageAccess::STORE), + "writeonly" => TokenValue::MemoryQualifier(StorageAccess::LOAD), // values "true" => TokenValue::BoolConstant(true), "false" => TokenValue::BoolConstant(false), diff --git a/src/front/glsl/parser/types.rs b/src/front/glsl/parser/types.rs index 47ad230d39..af0a9181ff 100644 --- a/src/front/glsl/parser/types.rs +++ b/src/front/glsl/parser/types.rs @@ -122,7 +122,7 @@ impl<'source> ParsingContext<'source> { | TokenValue::Shared | TokenValue::Buffer | TokenValue::Restrict - | TokenValue::StorageAccess(_) + | TokenValue::MemoryQualifier(_) | TokenValue::Layout => true, _ => false, }) @@ -219,7 +219,7 @@ impl<'source> ParsingContext<'source> { qualifiers.precision = Some((p, token.meta)); } - TokenValue::StorageAccess(access) => { + TokenValue::MemoryQualifier(access) => { let storage_access = qualifiers .storage_acess .get_or_insert((crate::StorageAccess::empty(), Span::default())); diff --git a/src/front/glsl/token.rs b/src/front/glsl/token.rs index e522f3f16b..5053830cd2 100644 --- a/src/front/glsl/token.rs +++ b/src/front/glsl/token.rs @@ -38,7 +38,11 @@ pub enum TokenValue { Shared, Restrict, - StorageAccess(crate::StorageAccess), + /// A `glsl` memory qualifier such as `writeonly` + /// + /// The associated [`crate::StorageAccess`] is the access being forbidden + /// (for example `writeonly` has an associated value of [`crate::StorageAccess::LOAD`]) + MemoryQualifier(crate::StorageAccess), Interpolation(Interpolation), Sampling(Sampling), diff --git a/tests/in/glsl/images.frag b/tests/in/glsl/images.frag index 41679b9218..f2aeea23dd 100644 --- a/tests/in/glsl/images.frag +++ b/tests/in/glsl/images.frag @@ -8,6 +8,10 @@ layout(rgba8, binding = 4) uniform image1DArray img1DArray; layout(rgba8, binding = 5) uniform image2DArray img2DArray; // layout(rgba8, binding = 6) uniform imageCubeArray imgCubeArray; +layout(rgba8, binding = 7) readonly uniform image2D imgReadOnly; +layout(rgba8, binding = 8) writeonly uniform image2D imgWriteOnly; +layout(rgba8, binding = 9) writeonly readonly uniform image2D imgWriteReadOnly; + void testImg1D(in int coord) { int size = imageSize(img1D); vec4 c = imageLoad(img1D, coord); @@ -52,4 +56,18 @@ void testImg3D(in ivec3 coord) { // imageStore(imgCubeArray, coord, vec4(2)); // } +void testImgReadOnly(in ivec2 coord) { + vec2 size = imageSize(img2D); + vec4 c = imageLoad(imgReadOnly, coord); +} + +void testImgWriteOnly(in ivec2 coord) { + vec2 size = imageSize(img2D); + imageStore(imgWriteOnly, coord, vec4(2)); +} + +void testImgWriteReadOnly(in ivec2 coord) { + vec2 size = imageSize(imgWriteReadOnly); +} + void main() {}