diff --git a/BUILD.gn b/BUILD.gn index 690cb9bccb..6add641aeb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -926,6 +926,7 @@ source_set("tint_unittests_wgsl_reader_src") { "src/reader/wgsl/parser_impl_test.cc", "src/reader/wgsl/parser_impl_test_helper.cc", "src/reader/wgsl/parser_impl_test_helper.h", + "src/reader/wgsl/parser_impl_texture_sampler_types_test.cc", "src/reader/wgsl/parser_impl_type_alias_test.cc", "src/reader/wgsl/parser_impl_type_decl_test.cc", "src/reader/wgsl/parser_impl_unary_expression_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2f755c21bd..c735b0d440 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -457,6 +457,7 @@ if(${TINT_BUILD_WGSL_READER}) reader/wgsl/parser_impl_test.cc reader/wgsl/parser_impl_test_helper.cc reader/wgsl/parser_impl_test_helper.h + reader/wgsl/parser_impl_texture_sampler_types_test.cc reader/wgsl/parser_impl_type_alias_test.cc reader/wgsl/parser_impl_type_decl_test.cc reader/wgsl/parser_impl_unary_expression_test.cc diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 59dc553881..e7a748b94c 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -577,6 +577,26 @@ std::unique_ptr ParserImpl::variable_decl() { return std::make_unique(source, name, sc, type); } +// texture_sampler_types +// : sampler_type +// | depth_texture_type +// | TODO: sampled_texture_type LESS_THAN type_decl GREATER_THAN +// | TODO: multisampled_texture_type LESS_THAN type_decl GREATER_THAN +// | TODO: storage_texture_type LESS_THAN image_storage_type GREATER_THAN +ast::type::Type* ParserImpl::texture_sampler_types() { + auto* type = sampler_type(); + if (type != nullptr) { + return type; + } + + type = depth_texture_type(); + if (type != nullptr) { + return type; + } + + return nullptr; +} + // sampler_type // : SAMPLER // | SAMPLER_COMPARISON diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index b13f26448e..d4924384a6 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -180,6 +180,9 @@ class ParserImpl { /// Parses a `function_decl` grammar element /// @returns the parsed function, nullptr otherwise std::unique_ptr function_decl(); + /// Parses a `texture_sampler_types` grammar element + /// @returns the parsed Type or nullptr if none matched. + ast::type::Type* texture_sampler_types(); /// Parses a `sampler_type` grammar element /// @returns the parsed Type or nullptr if none matched. ast::type::Type* sampler_type(); diff --git a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc new file mode 100644 index 0000000000..a1580e6052 --- /dev/null +++ b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc @@ -0,0 +1,55 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "gtest/gtest.h" +#include "src/ast/type/sampled_texture_type.h" +#include "src/ast/type/sampler_type.h" +#include "src/reader/wgsl/parser_impl.h" +#include "src/reader/wgsl/parser_impl_test_helper.h" + +namespace tint { +namespace reader { +namespace wgsl { +namespace { + +TEST_F(ParserImplTest, TextureSamplerTypes_Invalid) { + auto* p = parser("1234"); + auto* t = p->texture_sampler_types(); + EXPECT_EQ(t, nullptr); + EXPECT_FALSE(p->has_error()); +} + +TEST_F(ParserImplTest, TextureSamplerTypes_Sampler) { + auto* p = parser("sampler"); + auto* t = p->texture_sampler_types(); + ASSERT_NE(t, nullptr); + ASSERT_TRUE(t->IsSampler()); + ASSERT_FALSE(t->AsSampler()->IsComparison()); + EXPECT_FALSE(p->has_error()); +} + +TEST_F(ParserImplTest, TextureSamplerTypes_DepthTexture) { + auto* p = parser("texture_depth_2d"); + auto* t = p->texture_sampler_types(); + ASSERT_NE(t, nullptr); + ASSERT_TRUE(t->IsTexture()); + ASSERT_TRUE(t->AsTexture()->IsDepth()); + EXPECT_EQ(t->AsTexture()->dim(), ast::type::TextureDimension::k2d); + EXPECT_FALSE(p->has_error()); +} + +} // namespace +} // namespace wgsl +} // namespace reader +} // namespace tint