diff --git a/BUILD.gn b/BUILD.gn index ccff5dd703..4c1a7b2979 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -892,6 +892,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_if_test.cc", + "src/writer/msl/generator_impl_kill_test.cc", "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_type_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 27d1bfeee1..75136f2c46 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -500,6 +500,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_if_test.cc + writer/msl/generator_impl_kill_test.cc writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc writer/msl/generator_impl_type_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 79f5afb6b0..23171dcfc0 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -347,6 +347,14 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { return true; } +bool GeneratorImpl::EmitKill(ast::KillStatement*) { + make_indent(); + // TODO(dsinclair): Verify this is correct when the kill semantics are defined + // for WGSL (https://github.com/gpuweb/gpuweb/issues/361) + out_ << "discard_fragment();" << std::endl; + return true; +} + bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { if (stmt->HasCondition()) { out_ << " else if ("; @@ -431,6 +439,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsIf()) { return EmitIf(stmt->AsIf()); } + if (stmt->IsKill()) { + return EmitKill(stmt->AsKill()); + } if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 9459089549..0a3fc76cfd 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -80,6 +80,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted bool EmitIf(ast::IfStatement* stmt); + /// Handles generating a kill statement + /// @param stmt the kill statement + /// @returns true if the statement was successfully emitted + bool EmitKill(ast::KillStatement* stmt); /// Handles a literal /// @param lit the literal to emit /// @returns true if the literal was successfully emitted diff --git a/src/writer/msl/generator_impl_kill_test.cc b/src/writer/msl/generator_impl_kill_test.cc new file mode 100644 index 0000000000..7eb7f02e9f --- /dev/null +++ b/src/writer/msl/generator_impl_kill_test.cc @@ -0,0 +1,39 @@ +// 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/kill_statement.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Kill) { + ast::KillStatement k; + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&k)) << g.error(); + EXPECT_EQ(g.result(), " discard_fragment();\n"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint