Add SamplerType to AST.

This CL adds the sampler type into the AST.

Bug: tint:141
Change-Id: Id2f7678a2df677cb7dae47f05543c1e0a1999eed
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26080
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-30 22:27:25 +00:00
parent 8278f2c6f0
commit fe8c59ace9
17 changed files with 219 additions and 0 deletions

View File

@ -331,6 +331,8 @@ source_set("libtint_core_src") {
"src/ast/type/matrix_type.h", "src/ast/type/matrix_type.h",
"src/ast/type/pointer_type.cc", "src/ast/type/pointer_type.cc",
"src/ast/type/pointer_type.h", "src/ast/type/pointer_type.h",
"src/ast/type/sampler_type.cc",
"src/ast/type/sampler_type.h",
"src/ast/type/struct_type.cc", "src/ast/type/struct_type.cc",
"src/ast/type/struct_type.h", "src/ast/type/struct_type.h",
"src/ast/type/type.cc", "src/ast/type/type.cc",
@ -714,6 +716,7 @@ source_set("tint_unittests_core_src") {
"src/ast/type/i32_type_test.cc", "src/ast/type/i32_type_test.cc",
"src/ast/type/matrix_type_test.cc", "src/ast/type/matrix_type_test.cc",
"src/ast/type/pointer_type_test.cc", "src/ast/type/pointer_type_test.cc",
"src/ast/type/sampler_type_test.cc",
"src/ast/type/struct_type_test.cc", "src/ast/type/struct_type_test.cc",
"src/ast/type/u32_type_test.cc", "src/ast/type/u32_type_test.cc",
"src/ast/type/vector_type_test.cc", "src/ast/type/vector_type_test.cc",

View File

@ -152,6 +152,8 @@ set(TINT_LIB_SRCS
ast/type/matrix_type.h ast/type/matrix_type.h
ast/type/pointer_type.cc ast/type/pointer_type.cc
ast/type/pointer_type.h ast/type/pointer_type.h
ast/type/sampler_type.cc
ast/type/sampler_type.h
ast/type/struct_type.cc ast/type/struct_type.cc
ast/type/struct_type.h ast/type/struct_type.h
ast/type/type.cc ast/type/type.cc
@ -322,6 +324,7 @@ set(TINT_TEST_SRCS
ast/type/i32_type_test.cc ast/type/i32_type_test.cc
ast/type/matrix_type_test.cc ast/type/matrix_type_test.cc
ast/type/pointer_type_test.cc ast/type/pointer_type_test.cc
ast/type/sampler_type_test.cc
ast/type/struct_type_test.cc ast/type/struct_type_test.cc
ast/type/u32_type_test.cc ast/type/u32_type_test.cc
ast/type/vector_type_test.cc ast/type/vector_type_test.cc

View File

@ -45,6 +45,7 @@ TEST_F(AliasTypeTest, Is) {
EXPECT_FALSE(at.IsI32()); EXPECT_FALSE(at.IsI32());
EXPECT_FALSE(at.IsMatrix()); EXPECT_FALSE(at.IsMatrix());
EXPECT_FALSE(at.IsPointer()); EXPECT_FALSE(at.IsPointer());
EXPECT_FALSE(at.IsSampler());
EXPECT_FALSE(at.IsStruct()); EXPECT_FALSE(at.IsStruct());
EXPECT_FALSE(at.IsU32()); EXPECT_FALSE(at.IsU32());
EXPECT_FALSE(at.IsVector()); EXPECT_FALSE(at.IsVector());

View File

@ -54,6 +54,7 @@ TEST_F(ArrayTypeTest, Is) {
EXPECT_FALSE(arr.IsI32()); EXPECT_FALSE(arr.IsI32());
EXPECT_FALSE(arr.IsMatrix()); EXPECT_FALSE(arr.IsMatrix());
EXPECT_FALSE(arr.IsPointer()); EXPECT_FALSE(arr.IsPointer());
EXPECT_FALSE(arr.IsSampler());
EXPECT_FALSE(arr.IsStruct()); EXPECT_FALSE(arr.IsStruct());
EXPECT_FALSE(arr.IsU32()); EXPECT_FALSE(arr.IsU32());
EXPECT_FALSE(arr.IsVector()); EXPECT_FALSE(arr.IsVector());

View File

@ -32,6 +32,7 @@ TEST_F(BoolTypeTest, Is) {
EXPECT_FALSE(b.IsI32()); EXPECT_FALSE(b.IsI32());
EXPECT_FALSE(b.IsMatrix()); EXPECT_FALSE(b.IsMatrix());
EXPECT_FALSE(b.IsPointer()); EXPECT_FALSE(b.IsPointer());
EXPECT_FALSE(b.IsSampler());
EXPECT_FALSE(b.IsStruct()); EXPECT_FALSE(b.IsStruct());
EXPECT_FALSE(b.IsU32()); EXPECT_FALSE(b.IsU32());
EXPECT_FALSE(b.IsVector()); EXPECT_FALSE(b.IsVector());

View File

@ -32,6 +32,7 @@ TEST_F(F32TypeTest, Is) {
EXPECT_FALSE(f.IsI32()); EXPECT_FALSE(f.IsI32());
EXPECT_FALSE(f.IsMatrix()); EXPECT_FALSE(f.IsMatrix());
EXPECT_FALSE(f.IsPointer()); EXPECT_FALSE(f.IsPointer());
EXPECT_FALSE(f.IsSampler());
EXPECT_FALSE(f.IsStruct()); EXPECT_FALSE(f.IsStruct());
EXPECT_FALSE(f.IsU32()); EXPECT_FALSE(f.IsU32());
EXPECT_FALSE(f.IsVector()); EXPECT_FALSE(f.IsVector());

View File

@ -32,6 +32,7 @@ TEST_F(I32TypeTest, Is) {
EXPECT_TRUE(i.IsI32()); EXPECT_TRUE(i.IsI32());
EXPECT_FALSE(i.IsMatrix()); EXPECT_FALSE(i.IsMatrix());
EXPECT_FALSE(i.IsPointer()); EXPECT_FALSE(i.IsPointer());
EXPECT_FALSE(i.IsSampler());
EXPECT_FALSE(i.IsStruct()); EXPECT_FALSE(i.IsStruct());
EXPECT_FALSE(i.IsU32()); EXPECT_FALSE(i.IsU32());
EXPECT_FALSE(i.IsVector()); EXPECT_FALSE(i.IsVector());

View File

@ -42,6 +42,7 @@ TEST_F(MatrixTypeTest, Is) {
EXPECT_FALSE(m.IsI32()); EXPECT_FALSE(m.IsI32());
EXPECT_TRUE(m.IsMatrix()); EXPECT_TRUE(m.IsMatrix());
EXPECT_FALSE(m.IsPointer()); EXPECT_FALSE(m.IsPointer());
EXPECT_FALSE(m.IsSampler());
EXPECT_FALSE(m.IsStruct()); EXPECT_FALSE(m.IsStruct());
EXPECT_FALSE(m.IsU32()); EXPECT_FALSE(m.IsU32());
EXPECT_FALSE(m.IsVector()); EXPECT_FALSE(m.IsVector());

View File

@ -41,6 +41,7 @@ TEST_F(PointerTypeTest, Is) {
EXPECT_FALSE(p.IsI32()); EXPECT_FALSE(p.IsI32());
EXPECT_FALSE(p.IsMatrix()); EXPECT_FALSE(p.IsMatrix());
EXPECT_TRUE(p.IsPointer()); EXPECT_TRUE(p.IsPointer());
EXPECT_FALSE(p.IsSampler());
EXPECT_FALSE(p.IsStruct()); EXPECT_FALSE(p.IsStruct());
EXPECT_FALSE(p.IsU32()); EXPECT_FALSE(p.IsU32());
EXPECT_FALSE(p.IsVector()); EXPECT_FALSE(p.IsVector());

View File

@ -0,0 +1,50 @@
// 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/ast/type/sampler_type.h"
namespace tint {
namespace ast {
namespace type {
std::ostream& operator<<(std::ostream& out, SamplerKind kind) {
switch (kind) {
case SamplerKind::kSampler:
out << "sampler";
break;
case SamplerKind::kComparisonSampler:
out << "comparison_sampler";
break;
}
return out;
}
SamplerType::SamplerType(SamplerKind kind) : kind_(kind) {}
SamplerType::SamplerType(SamplerType&&) = default;
SamplerType::~SamplerType() = default;
bool SamplerType::IsSampler() const {
return true;
}
std::string SamplerType::type_name() const {
return std::string("__sampler_") +
(kind_ == SamplerKind::kSampler ? "sampler" : "comparison");
}
} // namespace type
} // namespace ast
} // namespace tint

View File

@ -0,0 +1,66 @@
// 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.
#ifndef SRC_AST_TYPE_SAMPLER_TYPE_H_
#define SRC_AST_TYPE_SAMPLER_TYPE_H_
#include <ostream>
#include <string>
#include "src/ast/type/type.h"
namespace tint {
namespace ast {
namespace type {
/// The different kinds of samplers
enum class SamplerKind {
/// A regular sampler
kSampler,
/// A comparison sampler
kComparisonSampler
};
std::ostream& operator<<(std::ostream& out, SamplerKind kind);
/// A sampler type.
class SamplerType : public Type {
public:
/// Constructor
/// @param kind the kind of sampler
explicit SamplerType(SamplerKind kind);
/// Move constructor
SamplerType(SamplerType&&);
~SamplerType() override;
/// @returns true if the type is a sampler type
bool IsSampler() const override;
/// @returns the sampler type
SamplerKind kind() const { return kind_; }
/// @returns true if this is a comparison sampler
bool IsComparison() const { return kind_ == SamplerKind::kComparisonSampler; }
/// @returns the name for this type
std::string type_name() const override;
private:
SamplerKind kind_ = SamplerKind::kSampler;
};
} // namespace type
} // namespace ast
} // namespace tint
#endif // SRC_AST_TYPE_SAMPLER_TYPE_H_

View File

@ -0,0 +1,65 @@
// 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/ast/type/sampler_type.h"
#include "gtest/gtest.h"
namespace tint {
namespace ast {
namespace type {
namespace {
using SamplerTypeTest = testing::Test;
TEST_F(SamplerTypeTest, Creation) {
SamplerType s{SamplerKind::kSampler};
EXPECT_EQ(s.kind(), SamplerKind::kSampler);
}
TEST_F(SamplerTypeTest, Creation_ComparisonSampler) {
SamplerType s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.kind(), SamplerKind::kComparisonSampler);
EXPECT_TRUE(s.IsComparison());
}
TEST_F(SamplerTypeTest, Is) {
SamplerType s{SamplerKind::kSampler};
EXPECT_FALSE(s.IsAlias());
EXPECT_FALSE(s.IsArray());
EXPECT_FALSE(s.IsBool());
EXPECT_FALSE(s.IsF32());
EXPECT_FALSE(s.IsI32());
EXPECT_FALSE(s.IsMatrix());
EXPECT_FALSE(s.IsPointer());
EXPECT_TRUE(s.IsSampler());
EXPECT_FALSE(s.IsStruct());
EXPECT_FALSE(s.IsU32());
EXPECT_FALSE(s.IsVector());
}
TEST_F(SamplerTypeTest, TypeName_Sampler) {
SamplerType s{SamplerKind::kSampler};
EXPECT_EQ(s.type_name(), "__sampler_sampler");
}
TEST_F(SamplerTypeTest, TypeName_Comparison) {
SamplerType s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.type_name(), "__sampler_comparison");
}
} // namespace
} // namespace type
} // namespace ast
} // namespace tint

View File

@ -43,6 +43,7 @@ TEST_F(StructTypeTest, Is) {
EXPECT_FALSE(s.IsI32()); EXPECT_FALSE(s.IsI32());
EXPECT_FALSE(s.IsMatrix()); EXPECT_FALSE(s.IsMatrix());
EXPECT_FALSE(s.IsPointer()); EXPECT_FALSE(s.IsPointer());
EXPECT_FALSE(s.IsSampler());
EXPECT_TRUE(s.IsStruct()); EXPECT_TRUE(s.IsStruct());
EXPECT_FALSE(s.IsU32()); EXPECT_FALSE(s.IsU32());
EXPECT_FALSE(s.IsVector()); EXPECT_FALSE(s.IsVector());

View File

@ -23,6 +23,7 @@
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h" #include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h" #include "src/ast/type/pointer_type.h"
#include "src/ast/type/sampler_type.h"
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
@ -83,6 +84,10 @@ bool Type::IsPointer() const {
return false; return false;
} }
bool Type::IsSampler() const {
return false;
}
bool Type::IsStruct() const { bool Type::IsStruct() const {
return false; return false;
} }
@ -174,6 +179,11 @@ const PointerType* Type::AsPointer() const {
return static_cast<const PointerType*>(this); return static_cast<const PointerType*>(this);
} }
const SamplerType* Type::AsSampler() const {
assert(IsSampler());
return static_cast<const SamplerType*>(this);
}
const StructType* Type::AsStruct() const { const StructType* Type::AsStruct() const {
assert(IsStruct()); assert(IsStruct());
return static_cast<const StructType*>(this); return static_cast<const StructType*>(this);
@ -229,6 +239,11 @@ PointerType* Type::AsPointer() {
return static_cast<PointerType*>(this); return static_cast<PointerType*>(this);
} }
SamplerType* Type::AsSampler() {
assert(IsSampler());
return static_cast<SamplerType*>(this);
}
StructType* Type::AsStruct() { StructType* Type::AsStruct() {
assert(IsStruct()); assert(IsStruct());
return static_cast<StructType*>(this); return static_cast<StructType*>(this);

View File

@ -28,6 +28,7 @@ class F32Type;
class I32Type; class I32Type;
class MatrixType; class MatrixType;
class PointerType; class PointerType;
class SamplerType;
class StructType; class StructType;
class U32Type; class U32Type;
class VectorType; class VectorType;
@ -54,6 +55,8 @@ class Type {
virtual bool IsMatrix() const; virtual bool IsMatrix() const;
/// @returns true if the type is a ptr type /// @returns true if the type is a ptr type
virtual bool IsPointer() const; virtual bool IsPointer() const;
/// @returns true if the type is a sampler
virtual bool IsSampler() const;
/// @returns true if the type is a struct type /// @returns true if the type is a struct type
virtual bool IsStruct() const; virtual bool IsStruct() const;
/// @returns true if the type is a u32 type /// @returns true if the type is a u32 type
@ -119,6 +122,8 @@ class Type {
const MatrixType* AsMatrix() const; const MatrixType* AsMatrix() const;
/// @returns the type as a pointer type /// @returns the type as a pointer type
const PointerType* AsPointer() const; const PointerType* AsPointer() const;
/// @returns the type as a sampler type
const SamplerType* AsSampler() const;
/// @returns the type as a struct type /// @returns the type as a struct type
const StructType* AsStruct() const; const StructType* AsStruct() const;
/// @returns the type as a u32 type /// @returns the type as a u32 type
@ -142,6 +147,8 @@ class Type {
MatrixType* AsMatrix(); MatrixType* AsMatrix();
/// @returns the type as a pointer type /// @returns the type as a pointer type
PointerType* AsPointer(); PointerType* AsPointer();
/// @returns the type as a sampler type
SamplerType* AsSampler();
/// @returns the type as a struct type /// @returns the type as a struct type
StructType* AsStruct(); StructType* AsStruct();
/// @returns the type as a u32 type /// @returns the type as a u32 type

View File

@ -32,6 +32,7 @@ TEST_F(U32TypeTest, Is) {
EXPECT_FALSE(u.IsI32()); EXPECT_FALSE(u.IsI32());
EXPECT_FALSE(u.IsMatrix()); EXPECT_FALSE(u.IsMatrix());
EXPECT_FALSE(u.IsPointer()); EXPECT_FALSE(u.IsPointer());
EXPECT_FALSE(u.IsSampler());
EXPECT_FALSE(u.IsStruct()); EXPECT_FALSE(u.IsStruct());
EXPECT_TRUE(u.IsU32()); EXPECT_TRUE(u.IsU32());
EXPECT_FALSE(u.IsVector()); EXPECT_FALSE(u.IsVector());

View File

@ -41,6 +41,7 @@ TEST_F(VectorTypeTest, Is) {
EXPECT_FALSE(v.IsI32()); EXPECT_FALSE(v.IsI32());
EXPECT_FALSE(v.IsMatrix()); EXPECT_FALSE(v.IsMatrix());
EXPECT_FALSE(v.IsPointer()); EXPECT_FALSE(v.IsPointer());
EXPECT_FALSE(v.IsSampler());
EXPECT_FALSE(v.IsStruct()); EXPECT_FALSE(v.IsStruct());
EXPECT_FALSE(v.IsU32()); EXPECT_FALSE(v.IsU32());
EXPECT_TRUE(v.IsVector()); EXPECT_TRUE(v.IsVector());