From 5b5c98cdf49ad8ddedec0b6a572b1ea5aaf4b59d Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 20 Nov 2020 09:37:14 +0000 Subject: [PATCH] writer/hlsl: Clean up matrix type emission For HLSL emission instead of: `matrix` emit `typeNxM` These are significantly shorter, more idiomatic, and is far easier to read. Change-Id: I78d3256aa36f4a23f5aece817ac48c255462991c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33460 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- src/writer/hlsl/generator_impl.cc | 5 ++-- .../hlsl/generator_impl_constructor_test.cc | 7 +++--- .../generator_impl_member_accessor_test.cc | 23 +++++++++---------- src/writer/hlsl/generator_impl_type_test.cc | 2 +- ...rator_impl_variable_decl_statement_test.cc | 5 ++-- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 039217d583..91d1930051 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -1762,7 +1762,7 @@ bool GeneratorImpl::EmitStorageBufferAccessor(std::ostream& pre, return true; } - out << "matrixrows() << ", " << mat->columns() << ">("; + out << "uint" << mat->rows() << "x" << mat->columns() << "("; for (uint32_t i = 0; i < mat->columns(); i++) { if (i != 0) { @@ -2000,11 +2000,10 @@ bool GeneratorImpl::EmitType(std::ostream& out, out << "int"; } else if (type->IsMatrix()) { auto* mat = type->AsMatrix(); - out << "matrix<"; if (!EmitType(out, mat->type(), "")) { return false; } - out << ", " << mat->rows() << ", " << mat->columns() << ">"; + out << mat->rows() << "x" << mat->columns(); } else if (type->IsPointer()) { // TODO(dsinclair): What do we do with pointers in HLSL? // https://bugs.chromium.org/p/tint/issues/detail?id=183 diff --git a/src/writer/hlsl/generator_impl_constructor_test.cc b/src/writer/hlsl/generator_impl_constructor_test.cc index c17d547ee4..6590765a6f 100644 --- a/src/writer/hlsl/generator_impl_constructor_test.cc +++ b/src/writer/hlsl/generator_impl_constructor_test.cc @@ -186,9 +186,10 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) { // A matrix of type T with n columns and m rows can also be constructed from // n vectors of type T with m components. - EXPECT_EQ(result(), std::string("matrix(float3(1.00000000f, " - "2.00000000f, 3.00000000f), ") + - "float3(3.00000000f, 4.00000000f, 5.00000000f))"); + EXPECT_EQ( + result(), + std::string("float3x2(float3(1.00000000f, 2.00000000f, 3.00000000f), ") + + "float3(3.00000000f, 4.00000000f, 5.00000000f))"); } TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) { diff --git a/src/writer/hlsl/generator_impl_member_accessor_test.cc b/src/writer/hlsl/generator_impl_member_accessor_test.cc index f0c4592e32..e7e13308f4 100644 --- a/src/writer/hlsl/generator_impl_member_accessor_test.cc +++ b/src/writer/hlsl/generator_impl_member_accessor_test.cc @@ -173,7 +173,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, // mat2x3 b; // data.a = b; // - // -> matrix _tint_tmp = b; + // -> float3x2 _tint_tmp = b; // data.Store3(4 + 0, asuint(_tint_tmp[0])); // data.Store3(4 + 16, asuint(_tint_tmp[1])); ast::type::F32Type f32; @@ -217,7 +217,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error(); - EXPECT_EQ(result(), R"(matrix _tint_tmp = b; + EXPECT_EQ(result(), R"(float3x2 _tint_tmp = b; data.Store3(4 + 0, asuint(_tint_tmp[0])); data.Store3(4 + 16, asuint(_tint_tmp[1])); )"); @@ -232,7 +232,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, // var data : Data; // data.a = mat2x3(); // - // -> matrix _tint_tmp = matrix(0.0f, 0.0f, 0.0f, + // -> float3x2 _tint_tmp = float3x2(0.0f, 0.0f, 0.0f, // 0.0f, 0.0f, 0.0f); // data.Store3(4 + 0, asuint(_tint_tmp[0]); // data.Store3(4 + 16, asuint(_tint_tmp[1])); @@ -275,7 +275,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error(); EXPECT_EQ( result(), - R"(matrix _tint_tmp = matrix(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); + R"(float3x2 _tint_tmp = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); data.Store3(4 + 0, asuint(_tint_tmp[0])); data.Store3(4 + 16, asuint(_tint_tmp[1])); )"); @@ -290,7 +290,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, // var data : Data; // data.a; // - // -> asfloat(matrix(data.Load2(4 + 0), data.Load2(4 + 8), + // -> asfloat(uint2x3(data.Load2(4 + 0), data.Load2(4 + 8), // data.Load2(4 + 16))); ast::type::F32Type f32; ast::type::I32Type i32; @@ -325,7 +325,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, ASSERT_TRUE(gen.EmitExpression(pre, out, &expr)) << gen.error(); EXPECT_EQ(result(), - "asfloat(matrix(data.Load2(4 + 0), data.Load2(4 + 8), " + "asfloat(uint2x3(data.Load2(4 + 0), data.Load2(4 + 8), " "data.Load2(4 + 16)))"); } @@ -342,7 +342,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, // var data : Outer; // data.b.a; // - // -> asfloat(matrix(data.Load3(4 + 0), data.Load3(4 + 16))); + // -> asfloat(uint3x2(data.Load3(4 + 0), data.Load3(4 + 16))); ast::type::F32Type f32; ast::type::I32Type i32; ast::type::MatrixType mat(&f32, 3, 2); @@ -375,9 +375,8 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(gen.EmitExpression(pre, out, &expr)) << gen.error(); - EXPECT_EQ( - result(), - "asfloat(matrix(data.Load3(4 + 0), data.Load3(4 + 16)))"); + EXPECT_EQ(result(), + "asfloat(uint3x2(data.Load3(4 + 0), data.Load3(4 + 16)))"); } TEST_F( @@ -389,7 +388,7 @@ TEST_F( // var data : Data; // data.a; // - // -> asfloat(matrix(data.Load3(0), data.Load3(16), + // -> asfloat(uint3x3(data.Load3(0), data.Load3(16), // data.Load3(32))); ast::type::F32Type f32; ast::type::I32Type i32; @@ -420,7 +419,7 @@ TEST_F( ASSERT_TRUE(gen.EmitExpression(pre, out, &expr)) << gen.error(); EXPECT_EQ(result(), - "asfloat(matrix(data.Load3(0 + 0), data.Load3(0 + 16), " + "asfloat(uint3x3(data.Load3(0 + 0), data.Load3(0 + 16), " "data.Load3(0 + 32)))"); } diff --git a/src/writer/hlsl/generator_impl_type_test.cc b/src/writer/hlsl/generator_impl_type_test.cc index 9c5937dd55..592a600cec 100644 --- a/src/writer/hlsl/generator_impl_type_test.cc +++ b/src/writer/hlsl/generator_impl_type_test.cc @@ -157,7 +157,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) { ast::type::MatrixType m(&f32, 3, 2); ASSERT_TRUE(gen.EmitType(out, &m, "")) << gen.error(); - EXPECT_EQ(result(), "matrix"); + EXPECT_EQ(result(), "float3x2"); } // TODO(dsinclair): How to annotate as workgroup? 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 f4bc9628bc..b7da41dd1e 100644 --- a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc @@ -135,9 +135,8 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, ast::VariableDeclStatement stmt(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); + EXPECT_EQ(result(), + R"(float3x2 a = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); )"); }