writer/hlsl: Clean up matrix type emission

For HLSL emission instead of:
    `matrix<type, N, M>` 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 <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-20 09:37:14 +00:00 committed by Commit Bot service account
parent ea9c3a604b
commit 5b5c98cdf4
5 changed files with 20 additions and 22 deletions

View File

@ -1762,7 +1762,7 @@ bool GeneratorImpl::EmitStorageBufferAccessor(std::ostream& pre,
return true;
}
out << "matrix<uint, " << mat->rows() << ", " << 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

View File

@ -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<float, 3, 2>(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) {

View File

@ -173,7 +173,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
// mat2x3<f32> b;
// data.a = b;
//
// -> matrix<float, 3, 2> _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<float, 3, 2> _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<storage_buffer> data : Data;
// data.a = mat2x3<f32>();
//
// -> matrix<float, 3, 2> _tint_tmp = matrix<float, 3, 2>(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<float, 3, 2> _tint_tmp = matrix<float, 3, 2>(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<storage_buffer> data : Data;
// data.a;
//
// -> asfloat(matrix<uint, 2, 3>(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<uint, 2, 3>(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<storage_buffer> data : Outer;
// data.b.a;
//
// -> asfloat(matrix<uint, 3, 2>(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<uint, 3, 2>(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<storage_buffer> data : Data;
// data.a;
//
// -> asfloat(matrix<uint, 3, 3>(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<uint, 3, 3>(data.Load3(0 + 0), data.Load3(0 + 16), "
"asfloat(uint3x3(data.Load3(0 + 0), data.Load3(0 + 16), "
"data.Load3(0 + 32)))");
}

View File

@ -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<float, 3, 2>");
EXPECT_EQ(result(), "float3x2");
}
// TODO(dsinclair): How to annotate as workgroup?

View File

@ -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<float, 3, 2> a = matrix<float, 3, 2>(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);
)");
}