[msl-writer] Fixup matrix and array constructors.
This CL fixes up the array constructors to emit `{}` instead of `<type>[<size>]()`. The matrix example is also updated to have the correct data format for WGSL matrices which fixes the MSL output. Bug: tint:8 Change-Id: I3a08a8814d4b8b38a57b6324e2182a271c958ef3 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25060 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
4556cbe24f
commit
3f10421cee
|
@ -543,11 +543,14 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
|
bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
|
||||||
|
if (expr->type()->IsArray()) {
|
||||||
|
out_ << "{";
|
||||||
|
} else {
|
||||||
if (!EmitType(expr->type(), "")) {
|
if (!EmitType(expr->type(), "")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
out_ << "(";
|
out_ << "(";
|
||||||
|
}
|
||||||
|
|
||||||
// If the type constructor is empty then we need to construct with the zero
|
// If the type constructor is empty then we need to construct with the zero
|
||||||
// value for all components.
|
// value for all components.
|
||||||
|
@ -569,7 +572,11 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (expr->type()->IsArray()) {
|
||||||
|
out_ << "}";
|
||||||
|
} else {
|
||||||
out_ << ")";
|
out_ << ")";
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -171,23 +171,29 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) {
|
||||||
|
|
||||||
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) {
|
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) {
|
||||||
ast::type::F32Type f32;
|
ast::type::F32Type f32;
|
||||||
ast::type::MatrixType mat(&f32, 3, 2);
|
ast::type::MatrixType mat(&f32, 3, 2); // 3 ROWS, 2 COLUMNS
|
||||||
|
ast::type::VectorType vec(&f32, 3);
|
||||||
|
|
||||||
ast::type::VectorType vec(&f32, 2);
|
// WGSL matrix is mat2x3 (it flips for AST, sigh). With a type constructor
|
||||||
|
// of <vec3, vec3>
|
||||||
|
|
||||||
ast::ExpressionList mat_values;
|
ast::ExpressionList mat_values;
|
||||||
|
|
||||||
for (size_t i = 0; i < 3; i++) {
|
for (size_t i = 0; i < 2; i++) {
|
||||||
auto lit1 = std::make_unique<ast::FloatLiteral>(
|
auto lit1 = std::make_unique<ast::FloatLiteral>(
|
||||||
&f32, static_cast<float>(1 + (i * 2)));
|
&f32, static_cast<float>(1 + (i * 2)));
|
||||||
auto lit2 = std::make_unique<ast::FloatLiteral>(
|
auto lit2 = std::make_unique<ast::FloatLiteral>(
|
||||||
&f32, static_cast<float>(2 + (i * 2)));
|
&f32, static_cast<float>(2 + (i * 2)));
|
||||||
|
auto lit3 = std::make_unique<ast::FloatLiteral>(
|
||||||
|
&f32, static_cast<float>(3 + (i * 2)));
|
||||||
|
|
||||||
ast::ExpressionList values;
|
ast::ExpressionList values;
|
||||||
values.push_back(
|
values.push_back(
|
||||||
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
|
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
|
||||||
values.push_back(
|
values.push_back(
|
||||||
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
|
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
|
||||||
|
values.push_back(
|
||||||
|
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
|
||||||
|
|
||||||
mat_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
|
mat_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
|
||||||
&vec, std::move(values)));
|
&vec, std::move(values)));
|
||||||
|
@ -197,14 +203,16 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) {
|
||||||
|
|
||||||
GeneratorImpl g;
|
GeneratorImpl g;
|
||||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||||
EXPECT_EQ(g.result(),
|
|
||||||
std::string("float2x3(float2(1.00000000f, 2.00000000f), ") +
|
// A matrix of type T with n columns and m rows can also be constructed from
|
||||||
"float2(3.00000000f, 4.00000000f), " +
|
// n vectors of type T with m components.
|
||||||
"float2(5.00000000f, 6.00000000f))");
|
EXPECT_EQ(
|
||||||
|
g.result(),
|
||||||
|
std::string("float2x3(float3(1.00000000f, 2.00000000f, 3.00000000f), ") +
|
||||||
|
"float3(3.00000000f, 4.00000000f, 5.00000000f))");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(dsinclair): Verify
|
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) {
|
||||||
TEST_F(MslGeneratorImplTest, DISABLED_EmitConstructor_Type_Array) {
|
|
||||||
ast::type::F32Type f32;
|
ast::type::F32Type f32;
|
||||||
ast::type::VectorType vec(&f32, 3);
|
ast::type::VectorType vec(&f32, 3);
|
||||||
ast::type::ArrayType ary(&vec, 3);
|
ast::type::ArrayType ary(&vec, 3);
|
||||||
|
@ -235,10 +243,10 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitConstructor_Type_Array) {
|
||||||
|
|
||||||
GeneratorImpl g;
|
GeneratorImpl g;
|
||||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||||
EXPECT_EQ(g.result(), std::string("float3[3](") +
|
EXPECT_EQ(g.result(), std::string("{") +
|
||||||
"float3(1.00000000f, 2.00000000f, 3.00000000f), " +
|
"float3(1.00000000f, 2.00000000f, 3.00000000f), " +
|
||||||
"float3(4.00000000f, 5.00000000f, 6.00000000f), " +
|
"float3(4.00000000f, 5.00000000f, 6.00000000f), " +
|
||||||
"float3(7.00000000f, 8.00000000f, 9.00000000f))");
|
"float3(7.00000000f, 8.00000000f, 9.00000000f)}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(dsinclair): Add struct constructor test.
|
// TODO(dsinclair): Add struct constructor test.
|
||||||
|
|
Loading…
Reference in New Issue