[hlsl-writer] Emit cast expressions.
This CL adds support for cast expressions to the HLSL backend. Bug: tint:7 Change-Id: Ie7e180dc89abf137ab7d9b8790cc4206b3d5a672 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26926 Commit-Queue: dan sinclair <dsinclair@chromium.org> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
10585f0ac3
commit
3739a23f24
1
BUILD.gn
1
BUILD.gn
|
@ -1065,6 +1065,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
|||
"src/writer/hlsl/generator_impl_break_test.cc",
|
||||
"src/writer/hlsl/generator_impl_call_test.cc",
|
||||
"src/writer/hlsl/generator_impl_case_test.cc",
|
||||
"src/writer/hlsl/generator_impl_cast_test.cc",
|
||||
"src/writer/hlsl/generator_impl_constructor_test.cc",
|
||||
"src/writer/hlsl/generator_impl_continue_test.cc",
|
||||
"src/writer/hlsl/generator_impl_discard_test.cc",
|
||||
|
|
|
@ -576,6 +576,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
|||
writer/hlsl/generator_impl_break_test.cc
|
||||
writer/hlsl/generator_impl_call_test.cc
|
||||
writer/hlsl/generator_impl_case_test.cc
|
||||
writer/hlsl/generator_impl_cast_test.cc
|
||||
writer/hlsl/generator_impl_constructor_test.cc
|
||||
writer/hlsl/generator_impl_continue_test.cc
|
||||
writer/hlsl/generator_impl_discard_test.cc
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "src/ast/call_expression.h"
|
||||
#include "src/ast/call_statement.h"
|
||||
#include "src/ast/case_statement.h"
|
||||
#include "src/ast/cast_expression.h"
|
||||
#include "src/ast/decorated_variable.h"
|
||||
#include "src/ast/else_statement.h"
|
||||
#include "src/ast/float_literal.h"
|
||||
|
@ -404,6 +405,19 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* 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::EmitCase(ast::CaseStatement* stmt) {
|
||||
make_indent();
|
||||
|
||||
|
@ -525,6 +539,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
|||
if (expr->IsCall()) {
|
||||
return EmitCall(expr->AsCall());
|
||||
}
|
||||
if (expr->IsCast()) {
|
||||
return EmitCast(expr->AsCast());
|
||||
}
|
||||
if (expr->IsConstructor()) {
|
||||
return EmitConstructor(expr->AsConstructor());
|
||||
}
|
||||
|
|
|
@ -82,6 +82,10 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// @param stmt the statement
|
||||
/// @returns true if the statment was emitted successfully
|
||||
bool EmitCase(ast::CaseStatement* stmt);
|
||||
/// 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
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
// 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/module.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_Cast_Scalar) {
|
||||
ast::type::F32Type f32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::CastExpression cast(&f32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
|
||||
EXPECT_EQ(g.result(), "float(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, 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));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
|
||||
EXPECT_EQ(g.result(), "vector<float, 3>(id)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
Loading…
Reference in New Issue