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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:01:29 +00:00 committed by dan sinclair
parent 49568e162d
commit cbabfa8ca1
4 changed files with 119 additions and 0 deletions

View File

@ -416,6 +416,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_member_accessor_test.cc writer/wgsl/generator_impl_member_accessor_test.cc
writer/wgsl/generator_impl_relational_test.cc writer/wgsl/generator_impl_relational_test.cc
writer/wgsl/generator_impl_type_test.cc writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_unary_derivative_test.cc
writer/wgsl/generator_impl_variable_test.cc writer/wgsl/generator_impl_variable_test.cc
) )
endif() endif()

View File

@ -44,6 +44,7 @@
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/type_initializer_expression.h" #include "src/ast/type_initializer_expression.h"
#include "src/ast/uint_literal.h" #include "src/ast/uint_literal.h"
#include "src/ast/unary_derivative_expression.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -144,6 +145,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsRelational()) { if (expr->IsRelational()) {
return EmitRelational(expr->AsRelational()); return EmitRelational(expr->AsRelational());
} }
if (expr->IsUnaryDerivative()) {
return EmitUnaryDerivative(expr->AsUnaryDerivative());
}
error_ = "unknown expression type"; error_ = "unknown expression type";
return false; return false;
@ -535,6 +539,33 @@ bool GeneratorImpl::EmitRelational(ast::RelationalExpression* expr) {
return true; 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 wgsl
} // namespace writer } // namespace writer
} // namespace tint } // namespace tint

View File

@ -126,6 +126,10 @@ class GeneratorImpl {
/// @param expr the type initializer expression /// @param expr the type initializer expression
/// @returns true if the initializer is emitted /// @returns true if the initializer is emitted
bool EmitTypeInitializer(ast::TypeInitializerExpression* expr); 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 /// Handles generating a variable
/// @param var the variable to generate /// @param var the variable to generate
/// @returns true if the variable was emitted /// @returns true if the variable was emitted

View File

@ -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 <memory>
#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<UnaryDerivativeData>;
TEST_P(UnaryDerivativeTest, Emit_ModifierNone) {
auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("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<ast::IdentifierExpression>("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) + "<fine>(expr)");
}
TEST_P(UnaryDerivativeTest, Emit_ModifierCoarse) {
auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("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) + "<coarse>(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