ast: Add InterpolateDecoration and related enums
Bug: tint:746
Change-Id: Ifd0de8b0e4400a153e67a8e301a7d0523e75898a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56240
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index eba4825..f838efb 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -368,6 +368,8 @@
"ast/int_literal.h",
"ast/internal_decoration.cc",
"ast/internal_decoration.h",
+ "ast/interpolate_decoration.cc",
+ "ast/interpolate_decoration.h",
"ast/literal.cc",
"ast/literal.h",
"ast/location_decoration.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8352f9e..14a78f9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -112,6 +112,8 @@
ast/int_literal.h
ast/internal_decoration.cc
ast/internal_decoration.h
+ ast/interpolate_decoration.cc
+ ast/interpolate_decoration.h
ast/literal.cc
ast/literal.h
ast/location_decoration.cc
@@ -554,6 +556,7 @@
ast/identifier_expression_test.cc
ast/if_statement_test.cc
ast/int_literal_test.cc
+ ast/interpolate_decoration_test.cc
ast/intrinsic_texture_helper_test.cc
ast/intrinsic_texture_helper_test.h
ast/location_decoration_test.cc
diff --git a/src/ast/interpolate_decoration.cc b/src/ast/interpolate_decoration.cc
new file mode 100644
index 0000000..333d7c2
--- /dev/null
+++ b/src/ast/interpolate_decoration.cc
@@ -0,0 +1,93 @@
+// 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/interpolate_decoration.h"
+
+#include <string>
+
+#include "src/program_builder.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::ast::InterpolateDecoration);
+
+namespace tint {
+namespace ast {
+
+InterpolateDecoration::InterpolateDecoration(ProgramID program_id,
+ const Source& source,
+ InterpolationType type,
+ InterpolationSampling sampling)
+ : Base(program_id, source), type_(type), sampling_(sampling) {}
+
+InterpolateDecoration::~InterpolateDecoration() = default;
+
+std::string InterpolateDecoration::name() const {
+ return "interpolate";
+}
+
+void InterpolateDecoration::to_str(const sem::Info&,
+ std::ostream& out,
+ size_t indent) const {
+ make_indent(out, indent);
+ out << "InterpolateDecoration{" << type_ << " " << sampling_ << "}"
+ << std::endl;
+}
+
+InterpolateDecoration* InterpolateDecoration::Clone(CloneContext* ctx) const {
+ // Clone arguments outside of create() call to have deterministic ordering
+ auto src = ctx->Clone(source());
+ return ctx->dst->create<InterpolateDecoration>(src, type_, sampling_);
+}
+
+std::ostream& operator<<(std::ostream& out, InterpolationType type) {
+ switch (type) {
+ case InterpolationType::kPerspective: {
+ out << "perspective";
+ break;
+ }
+ case InterpolationType::kLinear: {
+ out << "linear";
+ break;
+ }
+ case InterpolationType::kFlat: {
+ out << "flat";
+ break;
+ }
+ }
+ return out;
+}
+
+std::ostream& operator<<(std::ostream& out, InterpolationSampling sampling) {
+ switch (sampling) {
+ case InterpolationSampling::kNone: {
+ out << "none";
+ break;
+ }
+ case InterpolationSampling::kCenter: {
+ out << "center";
+ break;
+ }
+ case InterpolationSampling::kCentroid: {
+ out << "centroid";
+ break;
+ }
+ case InterpolationSampling::kSample: {
+ out << "sample";
+ break;
+ }
+ }
+ return out;
+}
+
+} // namespace ast
+} // namespace tint
diff --git a/src/ast/interpolate_decoration.h b/src/ast/interpolate_decoration.h
new file mode 100644
index 0000000..75e20b6
--- /dev/null
+++ b/src/ast/interpolate_decoration.h
@@ -0,0 +1,88 @@
+// 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_INTERPOLATE_DECORATION_H_
+#define SRC_AST_INTERPOLATE_DECORATION_H_
+
+#include <ostream>
+#include <string>
+
+#include "src/ast/decoration.h"
+
+namespace tint {
+namespace ast {
+
+/// The interpolation type.
+enum class InterpolationType { kPerspective, kLinear, kFlat };
+
+/// The interpolation sampling.
+enum class InterpolationSampling { kNone = -1, kCenter, kCentroid, kSample };
+
+/// An interpolate decoration
+class InterpolateDecoration
+ : public Castable<InterpolateDecoration, Decoration> {
+ public:
+ /// Create an interpolate decoration.
+ /// @param program_id the identifier of the program that owns this node
+ /// @param source the source of this decoration
+ /// @param type the interpolation type
+ /// @param sampling the interpolation sampling
+ InterpolateDecoration(ProgramID program_id,
+ const Source& source,
+ InterpolationType type,
+ InterpolationSampling sampling);
+ ~InterpolateDecoration() override;
+
+ /// @returns the interpolation type
+ InterpolationType type() const { return type_; }
+
+ /// @returns the interpolation sampling
+ InterpolationSampling sampling() const { return sampling_; }
+
+ /// @returns the WGSL name for the decoration
+ std::string name() const override;
+
+ /// Outputs the decoration to the given stream
+ /// @param sem the semantic info for the program
+ /// @param out the stream to write to
+ /// @param indent number of spaces to indent the node when writing
+ void to_str(const sem::Info& sem,
+ std::ostream& out,
+ size_t indent) const override;
+
+ /// Clones this node and all transitive child nodes using the `CloneContext`
+ /// `ctx`.
+ /// @param ctx the clone context
+ /// @return the newly cloned node
+ InterpolateDecoration* Clone(CloneContext* ctx) const override;
+
+ private:
+ InterpolationType const type_;
+ InterpolationSampling const sampling_;
+};
+
+/// @param out the std::ostream to write to
+/// @param type the interpolation type
+/// @return the std::ostream so calls can be chained
+std::ostream& operator<<(std::ostream& out, InterpolationType type);
+
+/// @param out the std::ostream to write to
+/// @param sampling the interpolation sampling
+/// @return the std::ostream so calls can be chained
+std::ostream& operator<<(std::ostream& out, InterpolationSampling sampling);
+
+} // namespace ast
+} // namespace tint
+
+#endif // SRC_AST_INTERPOLATE_DECORATION_H_
diff --git a/src/ast/interpolate_decoration_test.cc b/src/ast/interpolate_decoration_test.cc
new file mode 100644
index 0000000..e80bf21
--- /dev/null
+++ b/src/ast/interpolate_decoration_test.cc
@@ -0,0 +1,41 @@
+// 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/interpolate_decoration.h"
+
+#include "src/ast/test_helper.h"
+
+namespace tint {
+namespace ast {
+namespace {
+
+using InterpolateDecorationTest = TestHelper;
+
+TEST_F(InterpolateDecorationTest, Creation) {
+ auto* d = create<InterpolateDecoration>(InterpolationType::kLinear,
+ InterpolationSampling::kCenter);
+ EXPECT_EQ(InterpolationType::kLinear, d->type());
+ EXPECT_EQ(InterpolationSampling::kCenter, d->sampling());
+}
+
+TEST_F(InterpolateDecorationTest, ToStr) {
+ auto* d = create<InterpolateDecoration>(InterpolationType::kPerspective,
+ InterpolationSampling::kSample);
+ EXPECT_EQ(str(d), R"(InterpolateDecoration{perspective sample}
+)");
+}
+
+} // namespace
+} // namespace ast
+} // namespace tint
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 2871a80..e120d8b 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -175,6 +175,7 @@
"../src/ast/identifier_expression_test.cc",
"../src/ast/if_statement_test.cc",
"../src/ast/int_literal_test.cc",
+ "../src/ast/interpolate_decoration_test.cc",
"../src/ast/intrinsic_texture_helper_test.cc",
"../src/ast/intrinsic_texture_helper_test.h",
"../src/ast/location_decoration_test.cc",