diff --git a/src/BUILD.gn b/src/BUILD.gn index 9d033c6957..c808dee8a2 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -308,6 +308,8 @@ libtint_source_set("libtint_core_all_src") { "ast/else_statement.h", "ast/expression.cc", "ast/expression.h", + "ast/external_texture.cc", + "ast/external_texture.h", "ast/f32.cc", "ast/f32.h", "ast/fallthrough_statement.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b8b1ca7b6..4ed9729658 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,6 +90,8 @@ set(TINT_LIB_SRCS ast/else_statement.h ast/expression.cc ast/expression.h + ast/external_texture.cc + ast/external_texture.h ast/f32.cc ast/f32.h ast/fallthrough_statement.cc @@ -479,6 +481,7 @@ if(${TINT_BUILD_TESTS}) ast/depth_texture_test.cc ast/discard_statement_test.cc ast/else_statement_test.cc + ast/external_texture_test.cc ast/f32_test.cc ast/fallthrough_statement_test.cc ast/float_literal_test.cc diff --git a/src/ast/external_texture.cc b/src/ast/external_texture.cc new file mode 100644 index 0000000000..bc50f5335a --- /dev/null +++ b/src/ast/external_texture.cc @@ -0,0 +1,45 @@ +// Copyright 2021 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 "src/ast/external_texture.h" + +#include "src/program_builder.h" + +TINT_INSTANTIATE_TYPEINFO(tint::ast::ExternalTexture); + +namespace tint { +namespace ast { + +// ExternalTexture::ExternalTexture() : Base(ast::TextureDimension::k2d) {} +ExternalTexture::ExternalTexture(ProgramID program_id, const Source& source) + : Base(program_id, source, ast::TextureDimension::k2d) {} + +ExternalTexture::ExternalTexture(ExternalTexture&&) = default; + +ExternalTexture::~ExternalTexture() = default; + +std::string ExternalTexture::type_name() const { + return "__external_texture"; +} + +std::string ExternalTexture::FriendlyName(const SymbolTable&) const { + return "texture_external"; +} + +ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const { + return ctx->dst->create(); +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/external_texture.h b/src/ast/external_texture.h new file mode 100644 index 0000000000..a7bbea44af --- /dev/null +++ b/src/ast/external_texture.h @@ -0,0 +1,54 @@ +// Copyright 2021 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. + +#ifndef SRC_AST_EXTERNAL_TEXTURE_H_ +#define SRC_AST_EXTERNAL_TEXTURE_H_ + +#include + +#include "src/ast/texture.h" + +namespace tint { +namespace ast { + +/// An external texture type +class ExternalTexture : public Castable { + public: + /// Constructor + /// @param program_id the identifier of the program that owns this node + /// @param source the source of this node + ExternalTexture(ProgramID program_id, const Source& source); + + /// Move constructor + ExternalTexture(ExternalTexture&&); + ~ExternalTexture() override; + + /// @returns the name for this type + std::string type_name() const override; + + /// @param symbols the program's symbol table + /// @returns the name for this type that closely resembles how it would be + /// declared in WGSL. + std::string FriendlyName(const SymbolTable& symbols) const override; + + /// Clones this type and all transitive types using the `CloneContext` `ctx`. + /// @param ctx the clone context + /// @return the newly cloned type + ExternalTexture* Clone(CloneContext* ctx) const override; +}; + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_EXTERNAL_TEXTURE_H_ diff --git a/src/ast/external_texture_test.cc b/src/ast/external_texture_test.cc new file mode 100644 index 0000000000..803e626a20 --- /dev/null +++ b/src/ast/external_texture_test.cc @@ -0,0 +1,83 @@ +// Copyright 2021 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 "src/ast/external_texture.h" + +#include "src/ast/access_control.h" +#include "src/ast/alias.h" +#include "src/ast/array.h" +#include "src/ast/bool.h" +#include "src/ast/depth_texture.h" +#include "src/ast/f32.h" +#include "src/ast/i32.h" +#include "src/ast/matrix.h" +#include "src/ast/pointer.h" +#include "src/ast/sampler.h" +#include "src/ast/storage_texture.h" +#include "src/ast/struct.h" +#include "src/ast/test_helper.h" +#include "src/ast/texture.h" +#include "src/ast/u32.h" +#include "src/ast/vector.h" + +namespace tint { +namespace ast { +namespace { + +using AstExternalTextureTest = TestHelper; + +TEST_F(AstExternalTextureTest, Is) { + Type* ty = create(); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_TRUE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); +} + +TEST_F(AstExternalTextureTest, IsTexture) { + Texture* ty = create(); + EXPECT_FALSE(ty->Is()); + EXPECT_TRUE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); + EXPECT_FALSE(ty->Is()); +} + +TEST_F(AstExternalTextureTest, Dim) { + auto* ty = create(); + EXPECT_EQ(ty->dim(), ast::TextureDimension::k2d); +} + +TEST_F(AstExternalTextureTest, TypeName) { + auto* ty = create(); + EXPECT_EQ(ty->type_name(), "__external_texture"); +} + +TEST_F(AstExternalTextureTest, FriendlyName) { + auto* ty = create(); + EXPECT_EQ(ty->FriendlyName(Symbols()), "texture_external"); +} + +} // namespace +} // namespace ast +} // namespace tint diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 3c07f7bc1d..7536cc4202 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -23,6 +23,7 @@ #include "src/ast/constant_id_decoration.h" #include "src/ast/continue_statement.h" #include "src/ast/discard_statement.h" +#include "src/ast/external_texture.h" #include "src/ast/fallthrough_statement.h" #include "src/ast/if_statement.h" #include "src/ast/loop_statement.h" @@ -657,8 +658,8 @@ Maybe ParserImpl::sampled_texture_type() { // : TEXTURE_EXTERNAL Maybe ParserImpl::external_texture_type() { if (match(Token::Type::kTextureExternal)) { - // TODO(crbug.com/tint/724): builder_.create() - return typ::Type{nullptr, builder_.create()}; + return typ::Type{builder_.create(), + builder_.create()}; } return Failure::kNoMatch; diff --git a/src/sem/external_texture_type.cc b/src/sem/external_texture_type.cc index 7f475e2935..c101245890 100644 --- a/src/sem/external_texture_type.cc +++ b/src/sem/external_texture_type.cc @@ -28,15 +28,11 @@ ExternalTexture::ExternalTexture(ExternalTexture&&) = default; ExternalTexture::~ExternalTexture() = default; std::string ExternalTexture::type_name() const { - std::ostringstream out; - out << "__external_texture"; - return out.str(); + return "__external_texture"; } std::string ExternalTexture::FriendlyName(const SymbolTable&) const { - std::ostringstream out; - out << "texture_external"; - return out.str(); + return "texture_external"; } ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const { diff --git a/test/BUILD.gn b/test/BUILD.gn index ce361ddd27..ab2ae1a7bc 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -192,6 +192,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/ast/depth_texture_test.cc", "../src/ast/discard_statement_test.cc", "../src/ast/else_statement_test.cc", + "../src/ast/external_texture_test.cc", "../src/ast/f32_test.cc", "../src/ast/fallthrough_statement_test.cc", "../src/ast/float_literal_test.cc",