[msl-writer] Add cast conversion.

This CL adds conversion of casts to MSL.

Bug: tint:8
Change-Id: Iecfb9a5b413b1d10372b4d2fec31c0956b1475a0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23822
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-26 21:27:59 +00:00 committed by David Neto
parent 3dbc8378fa
commit b1de84316c
5 changed files with 81 additions and 1 deletions

View File

@ -888,6 +888,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_as_test.cc",
"src/writer/msl/generator_impl_assign_test.cc",
"src/writer/msl/generator_impl_binary_test.cc",
"src/writer/msl/generator_impl_cast_test.cc",
"src/writer/msl/generator_impl_constructor_test.cc",
"src/writer/msl/generator_impl_function_test.cc",
"src/writer/msl/generator_impl_identifier_test.cc",

View File

@ -496,6 +496,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_as_test.cc
writer/msl/generator_impl_assign_test.cc
writer/msl/generator_impl_binary_test.cc
writer/msl/generator_impl_cast_test.cc
writer/msl/generator_impl_constructor_test.cc
writer/msl/generator_impl_function_test.cc
writer/msl/generator_impl_identifier_test.cc

View File

@ -18,6 +18,7 @@
#include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h"
#include "src/ast/cast_expression.h"
#include "src/ast/float_literal.h"
#include "src/ast/function.h"
#include "src/ast/identifier_expression.h"
@ -141,8 +142,9 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
// implementation-defined behaviour for negative LHS. We may have to
// generate extra code to implement WGSL-specified behaviour for negative
// LHS.
out_ << ">>";
out_ << R"(>>)";
break;
case ast::BinaryOp::kAdd:
out_ << "+";
break;
@ -172,6 +174,19 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
return true;
}
bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
if (!EmitType(expr->type(), "")) {
return false;
}
out_ << "(";
if (!EmitExpression(expr->expr())) {
return false;
}
out_ << ")";
return true;
}
bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
if (expr->IsScalarConstructor()) {
return EmitScalarConstructor(expr->AsScalarConstructor());
@ -239,6 +254,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsBinary()) {
return EmitBinary(expr->AsBinary());
}
if (expr->IsCast()) {
return EmitCast(expr->AsCast());
}
if (expr->IsConstructor()) {
return EmitConstructor(expr->AsConstructor());
}

View File

@ -52,6 +52,10 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
bool EmitBinary(ast::BinaryExpression* expr);
/// Handles generating a cast expression
/// @param expr the cast expression
/// @returns true if the cast was emitted
bool EmitCast(ast::CastExpression* expr);
/// Handles generating constructor expressions
/// @param expr the constructor expression
/// @returns true if the expression was emitted

View File

@ -0,0 +1,56 @@
// 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/cast_expression.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/vector_type.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Scalar) {
ast::type::F32Type f32;
auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::CastExpression cast(&f32, std::move(id));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
EXPECT_EQ(g.result(), "float(id)");
}
TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) {
ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3);
auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::CastExpression cast(&vec3, std::move(id));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
EXPECT_EQ(g.result(), "float3(id)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint