[hlsl-writer] Add AsExpression to the HLSL backend.
This CL adds as casts to the HLSL backend. Bug: tint:7 Change-Id: I599527d665a3ec1ab6cf80b4f550f7aee8fdf294 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25843 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
d450a920d5
commit
7cf1979667
1
BUILD.gn
1
BUILD.gn
|
@ -1043,6 +1043,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
|||
sources = [
|
||||
"src/writer/hlsl/generator_impl_alias_type_test.cc",
|
||||
"src/writer/hlsl/generator_impl_array_accessor_test.cc",
|
||||
"src/writer/hlsl/generator_impl_as_test.cc",
|
||||
"src/writer/hlsl/generator_impl_assign_test.cc",
|
||||
"src/writer/hlsl/generator_impl_binary_test.cc",
|
||||
"src/writer/hlsl/generator_impl_block_test.cc",
|
||||
|
|
|
@ -551,6 +551,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
|||
list(APPEND TINT_TEST_SRCS
|
||||
writer/hlsl/generator_impl_alias_type_test.cc
|
||||
writer/hlsl/generator_impl_array_accessor_test.cc
|
||||
writer/hlsl/generator_impl_as_test.cc
|
||||
writer/hlsl/generator_impl_assign_test.cc
|
||||
writer/hlsl/generator_impl_binary_test.cc
|
||||
writer/hlsl/generator_impl_block_test.cc
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
#include "src/ast/array_accessor_expression.h"
|
||||
#include "src/ast/as_expression.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/binary_expression.h"
|
||||
#include "src/ast/bool_literal.h"
|
||||
|
@ -117,6 +118,25 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitAs(ast::AsExpression* expr) {
|
||||
if (!expr->type()->IsF32() && !expr->type()->IsI32() &&
|
||||
!expr->type()->IsU32()) {
|
||||
error_ = "Unable to do as cast to type " + expr->type()->type_name();
|
||||
return false;
|
||||
}
|
||||
|
||||
out_ << "as";
|
||||
if (!EmitType(expr->type(), "")) {
|
||||
return false;
|
||||
}
|
||||
out_ << "(";
|
||||
if (!EmitExpression(expr->expr())) {
|
||||
return false;
|
||||
}
|
||||
out_ << ")";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
|
||||
make_indent();
|
||||
|
||||
|
@ -368,6 +388,9 @@ bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) {
|
|||
}
|
||||
|
||||
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
||||
if (expr->IsAs()) {
|
||||
return EmitAs(expr->AsAs());
|
||||
}
|
||||
if (expr->IsArrayAccessor()) {
|
||||
return EmitArrayAccessor(expr->AsArrayAccessor());
|
||||
}
|
||||
|
|
|
@ -46,6 +46,10 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// @param expr the expression to emit
|
||||
/// @returns true if the array accessor was emitted
|
||||
bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr);
|
||||
/// Handles generating an as expression
|
||||
/// @param expr the as expression
|
||||
/// @returns true if the as was emitted
|
||||
bool EmitAs(ast::AsExpression* expr);
|
||||
/// Handles an assignment statement
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
// 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/as_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/u32_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Float) {
|
||||
ast::type::F32Type f32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&f32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Int) {
|
||||
ast::type::I32Type i32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&i32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asint(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Uint) {
|
||||
ast::type::U32Type u32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&u32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asuint(id)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
Loading…
Reference in New Issue