From 42b0e2d5aff588a84a918482d296dd01b56ad4cd Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 26 Aug 2020 19:24:16 +0000 Subject: [PATCH] [hlsl-writer] Emit zero matrix values. This CL extends the zero emission to support matrices. Bug: tint:7 Change-Id: I7e39a68c83edb096fc4b365c66539e8a8b7cb1db Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27443 Commit-Queue: dan sinclair Reviewed-by: Sarah Mashayekhi Reviewed-by: David Neto --- src/writer/hlsl/generator_impl.cc | 10 +++++++++ ...rator_impl_variable_decl_statement_test.cc | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 1ffda9dbf3..4ed643d050 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -1430,6 +1430,16 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, ast::type::Type* type) { out << "0u"; } else if (type->IsVector()) { return EmitZeroValue(out, type->AsVector()->type()); + } else if (type->IsMatrix()) { + auto* mat = type->AsMatrix(); + for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { + if (i != 0) { + out << ", "; + } + if (!EmitZeroValue(out, mat->type())) { + return false; + } + } } else { error_ = "Invalid type for zero emission: " + type->type_name(); return false; diff --git a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc index 1a85ec9c1f..bef692f880 100644 --- a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc @@ -19,6 +19,7 @@ #include "src/ast/module.h" #include "src/ast/type/array_type.h" #include "src/ast/type/f32_type.h" +#include "src/ast/type/matrix_type.h" #include "src/ast/type/vector_type.h" #include "src/ast/variable.h" #include "src/ast/variable_decl_statement.h" @@ -130,6 +131,27 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, )"); } +TEST_F(HlslGeneratorImplTest_VariableDecl, + Emit_VariableDeclStatement_Initializer_ZeroMat) { + ast::type::F32Type f32; + ast::type::MatrixType mat(&f32, 3, 2); + + ast::ExpressionList values; + auto zero_mat = + std::make_unique(&mat, std::move(values)); + + auto var = + std::make_unique("a", ast::StorageClass::kNone, &mat); + var->set_constructor(std::move(zero_mat)); + + ast::VariableDeclStatement stmt(std::move(var)); + ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error(); + EXPECT_EQ( + result(), + R"(matrix a = matrix(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); +)"); +} + } // namespace } // namespace hlsl } // namespace writer