From cbabfa8ca1a3e7c2e91167e6aae3e2e22e8a6101 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 20 Mar 2020 19:01:29 +0000 Subject: [PATCH] Emit UnaryDerivative in WGSL writer. This CL updates the WGSL writer to emit the unary derivative expression. Bug: tint:4 Change-Id: I18c0fd56d88f98a6c5d37e176d98ab7b3077a50c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17120 Reviewed-by: David Neto --- src/CMakeLists.txt | 1 + src/writer/wgsl/generator_impl.cc | 31 +++++++ src/writer/wgsl/generator_impl.h | 4 + .../generator_impl_unary_derivative_test.cc | 83 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 src/writer/wgsl/generator_impl_unary_derivative_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 97c9a32d96..e8c005ce9d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -416,6 +416,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_member_accessor_test.cc writer/wgsl/generator_impl_relational_test.cc writer/wgsl/generator_impl_type_test.cc + writer/wgsl/generator_impl_unary_derivative_test.cc writer/wgsl/generator_impl_variable_test.cc ) endif() diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index f60126e323..9e6a1be386 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -44,6 +44,7 @@ #include "src/ast/type/vector_type.h" #include "src/ast/type_initializer_expression.h" #include "src/ast/uint_literal.h" +#include "src/ast/unary_derivative_expression.h" namespace tint { namespace writer { @@ -144,6 +145,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsRelational()) { return EmitRelational(expr->AsRelational()); } + if (expr->IsUnaryDerivative()) { + return EmitUnaryDerivative(expr->AsUnaryDerivative()); + } error_ = "unknown expression type"; return false; @@ -535,6 +539,33 @@ bool GeneratorImpl::EmitRelational(ast::RelationalExpression* expr) { return true; } +bool GeneratorImpl::EmitUnaryDerivative(ast::UnaryDerivativeExpression* expr) { + switch (expr->op()) { + case ast::UnaryDerivative::kDpdx: + out_ << "dpdx"; + break; + case ast::UnaryDerivative::kDpdy: + out_ << "dpdy"; + break; + case ast::UnaryDerivative::kFwidth: + out_ << "fwidth"; + break; + } + + if (expr->modifier() != ast::DerivativeModifier::kNone) { + out_ << "<" << expr->modifier() << ">"; + } + + out_ << "("; + + if (!EmitExpression(expr->param())) { + return false; + } + + out_ << ")"; + return true; +} + } // namespace wgsl } // namespace writer } // namespace tint diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 698de59c8b..c2836a23b9 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -126,6 +126,10 @@ class GeneratorImpl { /// @param expr the type initializer expression /// @returns true if the initializer is emitted bool EmitTypeInitializer(ast::TypeInitializerExpression* expr); + /// Handles a unary derivative expression + /// @param expr the expression to emit + /// @returns true if the expressionw as emitted + bool EmitUnaryDerivative(ast::UnaryDerivativeExpression* expr); /// Handles generating a variable /// @param var the variable to generate /// @returns true if the variable was emitted diff --git a/src/writer/wgsl/generator_impl_unary_derivative_test.cc b/src/writer/wgsl/generator_impl_unary_derivative_test.cc new file mode 100644 index 0000000000..b7af8dc1eb --- /dev/null +++ b/src/writer/wgsl/generator_impl_unary_derivative_test.cc @@ -0,0 +1,83 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/unary_derivative_expression.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +struct UnaryDerivativeData { + const char* name; + ast::UnaryDerivative derivative; +}; +inline std::ostream& operator<<(std::ostream& out, UnaryDerivativeData data) { + out << data.derivative; + return out; +} +using UnaryDerivativeTest = testing::TestWithParam; +TEST_P(UnaryDerivativeTest, Emit_ModifierNone) { + auto params = GetParam(); + + auto expr = std::make_unique("expr"); + + ast::UnaryDerivativeExpression derivative( + params.derivative, ast::DerivativeModifier::kNone, std::move(expr)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&derivative)) << g.error(); + EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); +} +TEST_P(UnaryDerivativeTest, Emit_ModifierFine) { + auto params = GetParam(); + + auto expr = std::make_unique("expr"); + + ast::UnaryDerivativeExpression derivative( + params.derivative, ast::DerivativeModifier::kFine, std::move(expr)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&derivative)) << g.error(); + EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); +} +TEST_P(UnaryDerivativeTest, Emit_ModifierCoarse) { + auto params = GetParam(); + + auto expr = std::make_unique("expr"); + + ast::UnaryDerivativeExpression derivative( + params.derivative, ast::DerivativeModifier::kCoarse, std::move(expr)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&derivative)) << g.error(); + EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); +} +INSTANTIATE_TEST_SUITE_P( + GeneratorImplTest, + UnaryDerivativeTest, + testing::Values(UnaryDerivativeData{"dpdx", ast::UnaryDerivative::kDpdx}, + UnaryDerivativeData{"dpdy", ast::UnaryDerivative::kDpdy}, + UnaryDerivativeData{"fwidth", + ast::UnaryDerivative::kFwidth})); + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint