Tint: Support ReadOnly and ReadWrite storage texture in inspector

This patch adds the support of ReadOnly and ReadWrite storage texture
in Tint inspector.

Bug: dawn:1972, tint:2007
Test: tint_unittests
Change-Id: I4f61575025934d1c1531259c5d996d657afff57f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/146940
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/tint/cmd/common/helper.cc b/src/tint/cmd/common/helper.cc
index 564246f..77b18f9 100644
--- a/src/tint/cmd/common/helper.cc
+++ b/src/tint/cmd/common/helper.cc
@@ -394,6 +394,10 @@
             return "MultisampledTexture";
         case tint::inspector::ResourceBinding::ResourceType::kWriteOnlyStorageTexture:
             return "WriteOnlyStorageTexture";
+        case tint::inspector::ResourceBinding::ResourceType::kReadOnlyStorageTexture:
+            return "ReadOnlyStorageTexture";
+        case tint::inspector::ResourceBinding::ResourceType::kReadWriteStorageTexture:
+            return "ReadWriteStorageTexture";
         case tint::inspector::ResourceBinding::ResourceType::kDepthTexture:
             return "DepthTexture";
         case tint::inspector::ResourceBinding::ResourceType::kDepthMultisampledTexture:
diff --git a/src/tint/fuzzers/tint_common_fuzzer.cc b/src/tint/fuzzers/tint_common_fuzzer.cc
index b8e74ed..aa5c65d 100644
--- a/src/tint/fuzzers/tint_common_fuzzer.cc
+++ b/src/tint/fuzzers/tint_common_fuzzer.cc
@@ -373,7 +373,7 @@
         inspector.GetMultisampledTextureResourceBindings(ep.name);
         CHECK_INSPECTOR(program, inspector);
 
-        inspector.GetWriteOnlyStorageTextureResourceBindings(ep.name);
+        inspector.GetStorageTextureResourceBindings(ep.name);
         CHECK_INSPECTOR(program, inspector);
 
         inspector.GetDepthTextureResourceBindings(ep.name);
diff --git a/src/tint/lang/wgsl/helpers/flatten_bindings.cc b/src/tint/lang/wgsl/helpers/flatten_bindings.cc
index 1647a1a..7e6d6c5 100644
--- a/src/tint/lang/wgsl/helpers/flatten_bindings.cc
+++ b/src/tint/lang/wgsl/helpers/flatten_bindings.cc
@@ -53,6 +53,8 @@
                 case tint::inspector::ResourceBinding::ResourceType::kSampledTexture:
                 case tint::inspector::ResourceBinding::ResourceType::kMultisampledTexture:
                 case tint::inspector::ResourceBinding::ResourceType::kWriteOnlyStorageTexture:
+                case tint::inspector::ResourceBinding::ResourceType::kReadOnlyStorageTexture:
+                case tint::inspector::ResourceBinding::ResourceType::kReadWriteStorageTexture:
                 case tint::inspector::ResourceBinding::ResourceType::kDepthTexture:
                 case tint::inspector::ResourceBinding::ResourceType::kDepthMultisampledTexture:
                 case tint::inspector::ResourceBinding::ResourceType::kExternalTexture:
diff --git a/src/tint/lang/wgsl/inspector/inspector.cc b/src/tint/lang/wgsl/inspector/inspector.cc
index e3a81b2..ee1b751 100644
--- a/src/tint/lang/wgsl/inspector/inspector.cc
+++ b/src/tint/lang/wgsl/inspector/inspector.cc
@@ -338,7 +338,7 @@
              &Inspector::GetComparisonSamplerResourceBindings,
              &Inspector::GetSampledTextureResourceBindings,
              &Inspector::GetMultisampledTextureResourceBindings,
-             &Inspector::GetWriteOnlyStorageTextureResourceBindings,
+             &Inspector::GetStorageTextureResourceBindings,
              &Inspector::GetDepthTextureResourceBindings,
              &Inspector::GetDepthMultisampledTextureResourceBindings,
              &Inspector::GetExternalTextureResourceBindings,
@@ -449,7 +449,7 @@
     return GetSampledTextureResourceBindingsImpl(entry_point, true);
 }
 
-std::vector<ResourceBinding> Inspector::GetWriteOnlyStorageTextureResourceBindings(
+std::vector<ResourceBinding> Inspector::GetStorageTextureResourceBindings(
     const std::string& entry_point) {
     return GetStorageTextureResourceBindingsImpl(entry_point);
 }
@@ -762,7 +762,19 @@
         auto* texture_type = var->Type()->UnwrapRef()->As<core::type::StorageTexture>();
 
         ResourceBinding entry;
-        entry.resource_type = ResourceBinding::ResourceType::kWriteOnlyStorageTexture;
+        switch (texture_type->access()) {
+            case core::Access::kWrite:
+                entry.resource_type = ResourceBinding::ResourceType::kWriteOnlyStorageTexture;
+                break;
+            case core::Access::kReadWrite:
+                entry.resource_type = ResourceBinding::ResourceType::kReadWriteStorageTexture;
+                break;
+            case core::Access::kRead:
+                entry.resource_type = ResourceBinding::ResourceType::kReadOnlyStorageTexture;
+                break;
+            case core::Access::kUndefined:
+                TINT_UNREACHABLE() << "unhandled storage texture access";
+        }
         entry.bind_group = binding_info.group;
         entry.binding = binding_info.binding;
 
diff --git a/src/tint/lang/wgsl/inspector/inspector.h b/src/tint/lang/wgsl/inspector/inspector.h
index afa9b02..a615b40 100644
--- a/src/tint/lang/wgsl/inspector/inspector.h
+++ b/src/tint/lang/wgsl/inspector/inspector.h
@@ -108,8 +108,7 @@
 
     /// @param entry_point name of the entry point to get information about.
     /// @returns vector of all of the bindings for write-only storage textures.
-    std::vector<ResourceBinding> GetWriteOnlyStorageTextureResourceBindings(
-        const std::string& entry_point);
+    std::vector<ResourceBinding> GetStorageTextureResourceBindings(const std::string& entry_point);
 
     /// @param entry_point name of the entry point to get information about.
     /// @returns vector of all of the bindings for depth textures.
diff --git a/src/tint/lang/wgsl/inspector/inspector_builder_test.cc b/src/tint/lang/wgsl/inspector/inspector_builder_test.cc
index b09f9fe..6e960a4 100644
--- a/src/tint/lang/wgsl/inspector/inspector_builder_test.cc
+++ b/src/tint/lang/wgsl/inspector/inspector_builder_test.cc
@@ -285,8 +285,9 @@
 }
 
 ast::Type InspectorBuilder::MakeStorageTextureTypes(core::type::TextureDimension dim,
-                                                    core::TexelFormat format) {
-    return ty.storage_texture(dim, format, core::Access::kWrite);
+                                                    core::TexelFormat format,
+                                                    core::Access access) {
+    return ty.storage_texture(dim, format, access);
 }
 
 void InspectorBuilder::AddStorageTexture(const std::string& name,
diff --git a/src/tint/lang/wgsl/inspector/inspector_builder_test.h b/src/tint/lang/wgsl/inspector/inspector_builder_test.h
index 21a962b..e0579bb 100644
--- a/src/tint/lang/wgsl/inspector/inspector_builder_test.h
+++ b/src/tint/lang/wgsl/inspector/inspector_builder_test.h
@@ -284,11 +284,14 @@
     /// @returns a pointer to a type appropriate for the coord param
     ast::Type GetCoordsType(core::type::TextureDimension dim, ast::Type scalar);
 
-    /// Generates appropriate types for a Read-Only StorageTexture
+    /// Generates appropriate types for a StorageTexture
     /// @param dim the texture dimension of the storage texture
     /// @param format the texel format of the storage texture
+    /// @param access the storage texture access
     /// @returns the storage texture type
-    ast::Type MakeStorageTextureTypes(core::type::TextureDimension dim, core::TexelFormat format);
+    ast::Type MakeStorageTextureTypes(core::type::TextureDimension dim,
+                                      core::TexelFormat format,
+                                      core::Access access);
 
     /// Adds a storage texture variable to the program
     /// @param name the name of the variable
diff --git a/src/tint/lang/wgsl/inspector/inspector_test.cc b/src/tint/lang/wgsl/inspector/inspector_test.cc
index bce7e23..2721c9f 100644
--- a/src/tint/lang/wgsl/inspector/inspector_test.cc
+++ b/src/tint/lang/wgsl/inspector/inspector_test.cc
@@ -117,7 +117,7 @@
 typedef std::tuple<core::type::TextureDimension, ResourceBinding::TextureDimension> DimensionParams;
 typedef std::tuple<core::TexelFormat, ResourceBinding::TexelFormat, ResourceBinding::SampledKind>
     TexelFormatParams;
-typedef std::tuple<DimensionParams, TexelFormatParams> GetStorageTextureTestParams;
+typedef std::tuple<DimensionParams, TexelFormatParams, core::Access> GetStorageTextureTestParams;
 class InspectorGetStorageTextureResourceBindingsTestWithParam
     : public InspectorBuilder,
       public testing::TestWithParam<GetStorageTextureTestParams> {};
@@ -1855,8 +1855,8 @@
              Ignore("depth_ms_texture"),
          });
 
-    auto st_type =
-        MakeStorageTextureTypes(core::type::TextureDimension::k2d, core::TexelFormat::kR32Uint);
+    auto st_type = MakeStorageTextureTypes(core::type::TextureDimension::k2d,
+                                           core::TexelFormat::kR32Uint, core::Access::kWrite);
     AddStorageTexture("st_var", st_type, 4, 0);
     MakeStorageTextureBodyFunction("st_func", "st_var", ty.vec2<u32>(), tint::Empty);
 
@@ -3002,7 +3002,7 @@
 
     Inspector& inspector = Build();
 
-    auto result = inspector.GetWriteOnlyStorageTextureResourceBindings("ep");
+    auto result = inspector.GetStorageTextureResourceBindings("ep");
     ASSERT_FALSE(inspector.has_error()) << inspector.error();
     EXPECT_EQ(0u, result.size());
 }
@@ -3010,7 +3010,8 @@
 TEST_P(InspectorGetStorageTextureResourceBindingsTestWithParam, Simple) {
     DimensionParams dim_params;
     TexelFormatParams format_params;
-    std::tie(dim_params, format_params) = GetParam();
+    core::Access access;
+    std::tie(dim_params, format_params, access) = GetParam();
 
     core::type::TextureDimension dim;
     ResourceBinding::TextureDimension expected_dim;
@@ -3021,7 +3022,25 @@
     ResourceBinding::SampledKind expected_kind;
     std::tie(format, expected_format, expected_kind) = format_params;
 
-    ast::Type st_type = MakeStorageTextureTypes(dim, format);
+    ResourceBinding::ResourceType expectedResourceType;
+    switch (access) {
+        case core::Access::kWrite:
+            expectedResourceType = ResourceBinding::ResourceType::kWriteOnlyStorageTexture;
+            break;
+        case core::Access::kRead:
+            Enable(core::Extension::kChromiumExperimentalReadWriteStorageTexture);
+            expectedResourceType = ResourceBinding::ResourceType::kReadOnlyStorageTexture;
+            break;
+        case core::Access::kReadWrite:
+            Enable(core::Extension::kChromiumExperimentalReadWriteStorageTexture);
+            expectedResourceType = ResourceBinding::ResourceType::kReadWriteStorageTexture;
+            break;
+        case core::Access::kUndefined:
+            ASSERT_TRUE(false);
+            break;
+    }
+
+    ast::Type st_type = MakeStorageTextureTypes(dim, format, access);
     AddStorageTexture("st_var", st_type, 0, 0);
 
     ast::Type dim_type;
@@ -3049,11 +3068,11 @@
 
     Inspector& inspector = Build();
 
-    auto result = inspector.GetWriteOnlyStorageTextureResourceBindings("ep");
+    auto result = inspector.GetStorageTextureResourceBindings("ep");
     ASSERT_FALSE(inspector.has_error()) << inspector.error();
     ASSERT_EQ(1u, result.size());
 
-    EXPECT_EQ(ResourceBinding::ResourceType::kWriteOnlyStorageTexture, result[0].resource_type);
+    EXPECT_EQ(expectedResourceType, result[0].resource_type);
     EXPECT_EQ(0u, result[0].bind_group);
     EXPECT_EQ(0u, result[0].binding);
     EXPECT_EQ(expected_dim, result[0].dim);
@@ -3064,62 +3083,64 @@
 INSTANTIATE_TEST_SUITE_P(
     InspectorGetStorageTextureResourceBindingsTest,
     InspectorGetStorageTextureResourceBindingsTestWithParam,
-    testing::Combine(testing::Values(std::make_tuple(core::type::TextureDimension::k1d,
-                                                     ResourceBinding::TextureDimension::k1d),
-                                     std::make_tuple(core::type::TextureDimension::k2d,
-                                                     ResourceBinding::TextureDimension::k2d),
-                                     std::make_tuple(core::type::TextureDimension::k2dArray,
-                                                     ResourceBinding::TextureDimension::k2dArray),
-                                     std::make_tuple(core::type::TextureDimension::k3d,
-                                                     ResourceBinding::TextureDimension::k3d)),
-                     testing::Values(std::make_tuple(core::TexelFormat::kR32Float,
-                                                     ResourceBinding::TexelFormat::kR32Float,
-                                                     ResourceBinding::SampledKind::kFloat),
-                                     std::make_tuple(core::TexelFormat::kR32Sint,
-                                                     ResourceBinding::TexelFormat::kR32Sint,
-                                                     ResourceBinding::SampledKind::kSInt),
-                                     std::make_tuple(core::TexelFormat::kR32Uint,
-                                                     ResourceBinding::TexelFormat::kR32Uint,
-                                                     ResourceBinding::SampledKind::kUInt),
-                                     std::make_tuple(core::TexelFormat::kRg32Float,
-                                                     ResourceBinding::TexelFormat::kRg32Float,
-                                                     ResourceBinding::SampledKind::kFloat),
-                                     std::make_tuple(core::TexelFormat::kRg32Sint,
-                                                     ResourceBinding::TexelFormat::kRg32Sint,
-                                                     ResourceBinding::SampledKind::kSInt),
-                                     std::make_tuple(core::TexelFormat::kRg32Uint,
-                                                     ResourceBinding::TexelFormat::kRg32Uint,
-                                                     ResourceBinding::SampledKind::kUInt),
-                                     std::make_tuple(core::TexelFormat::kRgba16Float,
-                                                     ResourceBinding::TexelFormat::kRgba16Float,
-                                                     ResourceBinding::SampledKind::kFloat),
-                                     std::make_tuple(core::TexelFormat::kRgba16Sint,
-                                                     ResourceBinding::TexelFormat::kRgba16Sint,
-                                                     ResourceBinding::SampledKind::kSInt),
-                                     std::make_tuple(core::TexelFormat::kRgba16Uint,
-                                                     ResourceBinding::TexelFormat::kRgba16Uint,
-                                                     ResourceBinding::SampledKind::kUInt),
-                                     std::make_tuple(core::TexelFormat::kRgba32Float,
-                                                     ResourceBinding::TexelFormat::kRgba32Float,
-                                                     ResourceBinding::SampledKind::kFloat),
-                                     std::make_tuple(core::TexelFormat::kRgba32Sint,
-                                                     ResourceBinding::TexelFormat::kRgba32Sint,
-                                                     ResourceBinding::SampledKind::kSInt),
-                                     std::make_tuple(core::TexelFormat::kRgba32Uint,
-                                                     ResourceBinding::TexelFormat::kRgba32Uint,
-                                                     ResourceBinding::SampledKind::kUInt),
-                                     std::make_tuple(core::TexelFormat::kRgba8Sint,
-                                                     ResourceBinding::TexelFormat::kRgba8Sint,
-                                                     ResourceBinding::SampledKind::kSInt),
-                                     std::make_tuple(core::TexelFormat::kRgba8Snorm,
-                                                     ResourceBinding::TexelFormat::kRgba8Snorm,
-                                                     ResourceBinding::SampledKind::kFloat),
-                                     std::make_tuple(core::TexelFormat::kRgba8Uint,
-                                                     ResourceBinding::TexelFormat::kRgba8Uint,
-                                                     ResourceBinding::SampledKind::kUInt),
-                                     std::make_tuple(core::TexelFormat::kRgba8Unorm,
-                                                     ResourceBinding::TexelFormat::kRgba8Unorm,
-                                                     ResourceBinding::SampledKind::kFloat))));
+    testing::Combine(
+        testing::Values(std::make_tuple(core::type::TextureDimension::k1d,
+                                        ResourceBinding::TextureDimension::k1d),
+                        std::make_tuple(core::type::TextureDimension::k2d,
+                                        ResourceBinding::TextureDimension::k2d),
+                        std::make_tuple(core::type::TextureDimension::k2dArray,
+                                        ResourceBinding::TextureDimension::k2dArray),
+                        std::make_tuple(core::type::TextureDimension::k3d,
+                                        ResourceBinding::TextureDimension::k3d)),
+        testing::Values(std::make_tuple(core::TexelFormat::kR32Float,
+                                        ResourceBinding::TexelFormat::kR32Float,
+                                        ResourceBinding::SampledKind::kFloat),
+                        std::make_tuple(core::TexelFormat::kR32Sint,
+                                        ResourceBinding::TexelFormat::kR32Sint,
+                                        ResourceBinding::SampledKind::kSInt),
+                        std::make_tuple(core::TexelFormat::kR32Uint,
+                                        ResourceBinding::TexelFormat::kR32Uint,
+                                        ResourceBinding::SampledKind::kUInt),
+                        std::make_tuple(core::TexelFormat::kRg32Float,
+                                        ResourceBinding::TexelFormat::kRg32Float,
+                                        ResourceBinding::SampledKind::kFloat),
+                        std::make_tuple(core::TexelFormat::kRg32Sint,
+                                        ResourceBinding::TexelFormat::kRg32Sint,
+                                        ResourceBinding::SampledKind::kSInt),
+                        std::make_tuple(core::TexelFormat::kRg32Uint,
+                                        ResourceBinding::TexelFormat::kRg32Uint,
+                                        ResourceBinding::SampledKind::kUInt),
+                        std::make_tuple(core::TexelFormat::kRgba16Float,
+                                        ResourceBinding::TexelFormat::kRgba16Float,
+                                        ResourceBinding::SampledKind::kFloat),
+                        std::make_tuple(core::TexelFormat::kRgba16Sint,
+                                        ResourceBinding::TexelFormat::kRgba16Sint,
+                                        ResourceBinding::SampledKind::kSInt),
+                        std::make_tuple(core::TexelFormat::kRgba16Uint,
+                                        ResourceBinding::TexelFormat::kRgba16Uint,
+                                        ResourceBinding::SampledKind::kUInt),
+                        std::make_tuple(core::TexelFormat::kRgba32Float,
+                                        ResourceBinding::TexelFormat::kRgba32Float,
+                                        ResourceBinding::SampledKind::kFloat),
+                        std::make_tuple(core::TexelFormat::kRgba32Sint,
+                                        ResourceBinding::TexelFormat::kRgba32Sint,
+                                        ResourceBinding::SampledKind::kSInt),
+                        std::make_tuple(core::TexelFormat::kRgba32Uint,
+                                        ResourceBinding::TexelFormat::kRgba32Uint,
+                                        ResourceBinding::SampledKind::kUInt),
+                        std::make_tuple(core::TexelFormat::kRgba8Sint,
+                                        ResourceBinding::TexelFormat::kRgba8Sint,
+                                        ResourceBinding::SampledKind::kSInt),
+                        std::make_tuple(core::TexelFormat::kRgba8Snorm,
+                                        ResourceBinding::TexelFormat::kRgba8Snorm,
+                                        ResourceBinding::SampledKind::kFloat),
+                        std::make_tuple(core::TexelFormat::kRgba8Uint,
+                                        ResourceBinding::TexelFormat::kRgba8Uint,
+                                        ResourceBinding::SampledKind::kUInt),
+                        std::make_tuple(core::TexelFormat::kRgba8Unorm,
+                                        ResourceBinding::TexelFormat::kRgba8Unorm,
+                                        ResourceBinding::SampledKind::kFloat)),
+        testing::Values(core::Access::kRead, core::Access::kWrite, core::Access::kReadWrite)));
 
 TEST_P(InspectorGetDepthTextureResourceBindingsTestWithParam, textureDimensions) {
     auto depth_texture_type = ty.depth_texture(GetParam().type_dim);
diff --git a/src/tint/lang/wgsl/inspector/resource_binding.h b/src/tint/lang/wgsl/inspector/resource_binding.h
index ed7e70d..03983e1 100644
--- a/src/tint/lang/wgsl/inspector/resource_binding.h
+++ b/src/tint/lang/wgsl/inspector/resource_binding.h
@@ -79,6 +79,8 @@
         kSampledTexture,
         kMultisampledTexture,
         kWriteOnlyStorageTexture,
+        kReadOnlyStorageTexture,
+        kReadWriteStorageTexture,
         kDepthTexture,
         kDepthMultisampledTexture,
         kExternalTexture