From ea9c3a604bf98bad144715ad8aeaa103b0f1054f Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Thu, 19 Nov 2020 19:40:41 +0000 Subject: [PATCH] [wgsl-writer] Emit access control type. This CL updates the WGSL-Writer to emit the access control type. Bug: tint:287 Change-Id: Ifb42a5ab199f18014c33be62960d078e57df8dba Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33360 Reviewed-by: Ben Clayton Reviewed-by: Ryan Harrison Commit-Queue: dan sinclair Auto-Submit: dan sinclair --- src/writer/wgsl/generator_impl.cc | 23 +++++----- .../wgsl/generator_impl_function_test.cc | 3 +- src/writer/wgsl/generator_impl_type_test.cc | 42 +++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index d9df273b21..bd345bd5f6 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -404,18 +404,17 @@ bool GeneratorImpl::EmitImageFormat(const ast::type::ImageFormat fmt) { bool GeneratorImpl::EmitType(ast::type::Type* type) { if (type->IsAccessControl()) { auto* ac = type->AsAccessControl(); - // TODO(dsinclair): Access control isn't supported in WGSL yet, so this - // is disabled for now. - // - // out_ << "[[access("; - // if (ac->IsReadOnly()) { - // out_ << "read"; - // } else if (ac->IsWriteOnly()) { - // out_ << "write"; - // } else { - // out_ << "read_write"; - // } - // out_ << ")]]" << std::endl; + + out_ << "[[access("; + if (ac->IsReadOnly()) { + out_ << "read"; + } else if (ac->IsReadWrite()) { + out_ << "read_write"; + } else { + error_ = "invalid access control"; + return false; + } + out_ << ")]]" << std::endl; if (!EmitType(ac->type())) { return false; } diff --git a/src/writer/wgsl/generator_impl_function_test.cc b/src/writer/wgsl/generator_impl_function_test.cc index 6923099992..15815c1aab 100644 --- a/src/writer/wgsl/generator_impl_function_test.cc +++ b/src/writer/wgsl/generator_impl_function_test.cc @@ -240,7 +240,8 @@ struct Data { d : f32; }; -[[binding(0), set(0)]] var data : Data; +[[binding(0), set(0)]] var data : [[access(read_write)]] +Data; [[stage(compute)]] fn a() -> void { diff --git a/src/writer/wgsl/generator_impl_type_test.cc b/src/writer/wgsl/generator_impl_type_test.cc index 6cc49aaffc..fd4d81dcaf 100644 --- a/src/writer/wgsl/generator_impl_type_test.cc +++ b/src/writer/wgsl/generator_impl_type_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/access_control.h" #include "src/ast/stride_decoration.h" #include "src/ast/struct.h" #include "src/ast/struct_block_decoration.h" @@ -20,6 +21,7 @@ #include "src/ast/struct_member.h" #include "src/ast/struct_member_decoration.h" #include "src/ast/struct_member_offset_decoration.h" +#include "src/ast/type/access_control_type.h" #include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" #include "src/ast/type/depth_texture_type.h" @@ -61,6 +63,46 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array) { EXPECT_EQ(gen.result(), "array"); } +TEST_F(WgslGeneratorImplTest, EmitType_AccessControl_Read) { + ast::type::I32Type i32; + + ast::StructMember mem("a", &i32, ast::StructMemberDecorationList{}); + ast::StructMemberList members; + members.push_back(&mem); + + ast::StructBlockDecoration block_deco(Source{}); + ast::StructDecorationList decos; + decos.push_back(&block_deco); + + ast::Struct str(decos, members); + ast::type::StructType s("S", &str); + + ast::type::AccessControlType a(ast::AccessControl::kReadOnly, &s); + + ASSERT_TRUE(gen.EmitType(&a)) << gen.error(); + EXPECT_EQ(gen.result(), "[[access(read)]]\nS"); +} + +TEST_F(WgslGeneratorImplTest, EmitType_AccessControl_ReadWrite) { + ast::type::I32Type i32; + + ast::StructMember mem("a", &i32, ast::StructMemberDecorationList{}); + ast::StructMemberList members; + members.push_back(&mem); + + ast::StructBlockDecoration block_deco(Source{}); + ast::StructDecorationList decos; + decos.push_back(&block_deco); + + ast::Struct str(decos, members); + ast::type::StructType s("S", &str); + + ast::type::AccessControlType a(ast::AccessControl::kReadWrite, &s); + + ASSERT_TRUE(gen.EmitType(&a)) << gen.error(); + EXPECT_EQ(gen.result(), "[[access(read_write)]]\nS"); +} + TEST_F(WgslGeneratorImplTest, EmitType_Array_Decoration) { ast::type::BoolType b; ast::ArrayDecorationList decos;