blob: 3163377b0618bce822f85b239b41940895236451 [file] [log] [blame]
// 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 "src/tint/ast/test_helper.h"
#include "src/tint/reader/wgsl/parser_impl_test_helper.h"
#include "src/tint/type/depth_texture.h"
#include "src/tint/type/multisampled_texture.h"
#include "src/tint/type/sampled_texture.h"
#include "src/tint/type/texture_dimension.h"
namespace tint::reader::wgsl {
namespace {
TEST_F(ParserImplTest, TextureSamplerTypes_Invalid) {
auto p = parser("1234");
auto t = p->texture_and_sampler_types();
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_FALSE(t.errored);
EXPECT_FALSE(p->has_error());
}
TEST_F(ParserImplTest, TextureSamplerTypes_Sampler) {
auto p = parser("sampler");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value, "sampler");
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 8u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_SamplerComparison) {
auto p = parser("sampler_comparison");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ast::CheckIdentifier(p->builder().Symbols(), t.value, "sampler_comparison");
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 19u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_DepthTexture) {
auto p = parser("texture_depth_2d");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value, "texture_depth_2d");
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 17u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_F32) {
auto p = parser("texture_1d<f32>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value, ast::Template("texture_1d", "f32"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 16u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_I32) {
auto p = parser("texture_2d<i32>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value, ast::Template("texture_2d", "i32"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 16u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_U32) {
auto p = parser("texture_3d<u32>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value, ast::Template("texture_3d", "u32"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 16u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_MultisampledTexture_I32) {
auto p = parser("texture_multisampled_2d<i32>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value,
ast::Template("texture_multisampled_2d", "i32"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 29u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_MultisampledTexture_MissingType) {
auto p = parser("texture_multisampled_2d<>");
auto t = p->texture_and_sampler_types();
ASSERT_TRUE(p->has_error());
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_TRUE(t.errored);
EXPECT_EQ(p->error(), "1:25: invalid type for multisampled texture type");
}
TEST_F(ParserImplTest, TextureSamplerTypes_MultisampledTexture_MissingLessThan) {
auto p = parser("texture_multisampled_2d");
auto t = p->texture_and_sampler_types();
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_TRUE(t.errored);
EXPECT_EQ(p->error(), "1:24: expected '<' for multisampled texture type");
}
TEST_F(ParserImplTest, TextureSamplerTypes_MultisampledTexture_MissingGreaterThan) {
auto p = parser("texture_multisampled_2d<u32");
auto t = p->texture_and_sampler_types();
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_TRUE(t.errored);
EXPECT_EQ(p->error(), "1:24: missing closing '>' for multisampled texture type");
}
TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_Readonly1dRg32Float) {
auto p = parser("texture_storage_1d<rg32float, read>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value,
ast::Template("texture_storage_1d", "rg32float", "read"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 36u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_Writeonly2dR32Uint) {
auto p = parser("texture_storage_2d<r32uint, write>");
auto t = p->texture_and_sampler_types();
ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ast::CheckIdentifier(p->builder().Symbols(), t.value,
ast::Template("texture_storage_2d", "r32uint", "write"));
EXPECT_EQ(t->expr->source.range, (Source::Range{{1u, 1u}, {1u, 35u}}));
}
TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_MissingType) {
auto p = parser("texture_storage_1d<>");
auto t = p->texture_and_sampler_types();
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_TRUE(t.errored);
EXPECT_EQ(p->error(), R"(1:20: expected texel format for storage texture type
Possible values: 'bgra8unorm', 'r32float', 'r32sint', 'r32uint', 'rg32float', 'rg32sint', 'rg32uint', 'rgba16float', 'rgba16sint', 'rgba16uint', 'rgba32float', 'rgba32sint', 'rgba32uint', 'rgba8sint', 'rgba8snorm', 'rgba8uint', 'rgba8unorm')");
}
TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_MissingGreaterThan) {
auto p = parser("texture_storage_1d<r32uint, read");
auto t = p->texture_and_sampler_types();
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(t.matched);
EXPECT_TRUE(t.errored);
EXPECT_EQ(p->error(), "1:19: missing closing '>' for storage texture type");
}
} // namespace
} // namespace tint::reader::wgsl