[hlsl-writer] Emit variable declarations.
This CL adds variable declarations to the HLSL backend. Bug: tint:7 Change-Id: I5c1e42ca26029f1595bf4f23b3b867a492ddacc1 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25846 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
b07cb63896
commit
cbe0668f9b
1
BUILD.gn
1
BUILD.gn
|
@ -1059,6 +1059,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
|||
"src/writer/hlsl/generator_impl_test.cc",
|
||||
"src/writer/hlsl/generator_impl_type_test.cc",
|
||||
"src/writer/hlsl/generator_impl_unary_op_test.cc",
|
||||
"src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
|
||||
"src/writer/hlsl/namer_test.cc",
|
||||
]
|
||||
|
||||
|
|
|
@ -567,6 +567,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
|||
writer/hlsl/generator_impl_test.cc
|
||||
writer/hlsl/generator_impl_type_test.cc
|
||||
writer/hlsl/generator_impl_unary_op_test.cc
|
||||
writer/hlsl/generator_impl_variable_decl_statement_test.cc
|
||||
writer/hlsl/namer_test.cc
|
||||
)
|
||||
endif()
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/uint_literal.h"
|
||||
#include "src/ast/unary_op_expression.h"
|
||||
#include "src/ast/variable_decl_statement.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
|
@ -544,6 +545,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
|||
if (stmt->IsSwitch()) {
|
||||
return EmitSwitch(stmt->AsSwitch());
|
||||
}
|
||||
if (stmt->IsVariableDecl()) {
|
||||
return EmitVariable(stmt->AsVariableDecl()->variable());
|
||||
}
|
||||
|
||||
error_ = "unknown statement type: " + stmt->str();
|
||||
return false;
|
||||
|
@ -685,6 +689,36 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitVariable(ast::Variable* var) {
|
||||
make_indent();
|
||||
|
||||
// TODO(dsinclair): Handle variable decorations
|
||||
if (var->IsDecorated()) {
|
||||
error_ = "Variable decorations are not handled yet";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (var->is_const()) {
|
||||
out_ << "const ";
|
||||
}
|
||||
if (!EmitType(var->type(), var->name())) {
|
||||
return false;
|
||||
}
|
||||
if (!var->type()->IsArray()) {
|
||||
out_ << " " << var->name();
|
||||
}
|
||||
|
||||
if (var->constructor() != nullptr) {
|
||||
out_ << " = ";
|
||||
if (!EmitExpression(var->constructor())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out_ << ";" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
||||
|
|
|
@ -139,6 +139,10 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// @param type the type to emit the value for
|
||||
/// @returns true if the zero value was successfully emitted.
|
||||
bool EmitZeroValue(ast::type::Type* type);
|
||||
/// Handles generating a variable
|
||||
/// @param var the variable to generate
|
||||
/// @returns true if the variable was emitted
|
||||
bool EmitVariable(ast::Variable* var);
|
||||
|
||||
/// Checks if the global variable is in an input or output struct
|
||||
/// @param var the variable to check
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
// 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 <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/array_type.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/variable.h"
|
||||
#include "src/ast/variable_decl_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
var->set_is_const(true);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " const float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::ArrayType ary(&f32, 5);
|
||||
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a[5];\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
|
||||
auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
|
||||
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
var->set_constructor(std::move(ident));
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(float a = initializer;
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
|
||||
ast::ExpressionList values;
|
||||
auto zero_vec =
|
||||
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(values));
|
||||
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &vec);
|
||||
var->set_constructor(std::move(zero_vec));
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
|
@ -47,6 +47,22 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement) {
|
|||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
var->set_is_const(true);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " const float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::ArrayType ary(&f32, 5);
|
||||
|
|
Loading…
Reference in New Issue