diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index f879befeaa..0ae7a5984b 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -1099,8 +1099,6 @@ uint32_t Builder::GenerateTypeConstructorExpression( result_type = result_type->AsVector()->type(); } else if (result_type->IsArray()) { result_type = result_type->AsArray()->type(); - } else if (result_type->IsMatrix()) { - result_type = result_type->AsMatrix()->type(); } for (const auto& e : values) { @@ -1116,7 +1114,10 @@ uint32_t Builder::GenerateTypeConstructorExpression( } auto* value_type = e->result_type()->UnwrapPtrIfNeeded(); - if (result_type == value_type) { + // If the result and value types are the same we can just use the object. + // If the result is a matrix then we should have validated that the value + // type is a correctly sized vector so we can just use it directly. + if (result_type == value_type || result_type->IsMatrix()) { out << "_" << id; ops.push_back(Operand::Int(id)); continue; diff --git a/src/writer/spirv/builder_constructor_expression_test.cc b/src/writer/spirv/builder_constructor_expression_test.cc index f1468db00e..6ca6cec05f 100644 --- a/src/writer/spirv/builder_constructor_expression_test.cc +++ b/src/writer/spirv/builder_constructor_expression_test.cc @@ -1097,85 +1097,517 @@ TEST_F(BuilderTest, Constructor_Type_ModuleScope_Vec4_With_Vec3_F32) { )"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat2x2_With_Vec2_Vec2) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat2x2_With_Vec2_Vec2) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 2); + ast::type::MatrixType mat(&f32, 2, 2); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 2 +%1 = OpTypeMatrix %2 2 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 +%6 = OpConstantComposite %1 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat3x2_With_Vec2_Vec2_Vec2) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat3x2_With_Vec2_Vec2_Vec2) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 2); + ast::type::MatrixType mat(&f32, 2, 3); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 2 +%1 = OpTypeMatrix %2 3 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 2); + ast::type::MatrixType mat(&f32, 2, 4); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec4_params; + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + params.push_back(std::make_unique( + &vec, std::move(vec4_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 2 +%1 = OpTypeMatrix %2 4 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat2x3_With_Vec3_Vec3) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat2x3_With_Vec3_Vec3) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + ast::type::MatrixType mat(&f32, 3, 2); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 3 +%1 = OpTypeMatrix %2 2 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat3x3_With_Vec3_Vec3_Vec3) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat3x3_With_Vec3_Vec3_Vec3) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + ast::type::MatrixType mat(&f32, 3, 3); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 3 +%1 = OpTypeMatrix %2 3 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + ast::type::MatrixType mat(&f32, 3, 4); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec4_params; + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + params.push_back(std::make_unique( + &vec, std::move(vec4_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 3 +%1 = OpTypeMatrix %2 4 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat2x4_With_Vec4_Vec4) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat2x4_With_Vec4_Vec4) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 4); + ast::type::MatrixType mat(&f32, 4, 2); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 4 +%1 = OpTypeMatrix %2 2 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat3x4_With_Vec4_Vec4_Vec4) { - FAIL(); +TEST_F(BuilderTest, Constructor_Type_Mat3x4_With_Vec4_Vec4_Vec4) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 4); + ast::type::MatrixType mat(&f32, 4, 3); + + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + + ast::TypeConstructorExpression cast(&mat, std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 4 +%1 = OpTypeMatrix %2 3 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 +)"); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) { - FAIL(); -} +TEST_F(BuilderTest, Constructor_Type_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 4); + ast::type::MatrixType mat(&f32, 4, 4); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat2x2_With_Vec2_Vec2) { - FAIL(); -} + ast::ExpressionList vec_params; + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat3x2_With_Vec2_Vec2_Vec2) { - FAIL(); -} + ast::ExpressionList vec2_params; + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec2_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) { - FAIL(); -} + ast::ExpressionList vec3_params; + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec3_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat2x3_With_Vec3_Vec3) { - FAIL(); -} + ast::ExpressionList vec4_params; + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); + vec4_params.push_back(std::make_unique( + std::make_unique(&f32, 2.0))); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat3x3_With_Vec3_Vec3_Vec3) { - FAIL(); -} + ast::ExpressionList params; + params.push_back(std::make_unique( + &vec, std::move(vec_params))); + params.push_back(std::make_unique( + &vec, std::move(vec2_params))); + params.push_back(std::make_unique( + &vec, std::move(vec3_params))); + params.push_back(std::make_unique( + &vec, std::move(vec4_params))); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) { - FAIL(); -} + ast::TypeConstructorExpression cast(&mat, std::move(params)); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat2x4_With_Vec4_Vec4) { - FAIL(); -} + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat3x4_With_Vec4_Vec4_Vec4) { - FAIL(); -} + Builder b(&mod); + b.push_function(Function{}); + EXPECT_EQ(b.GenerateExpression(&cast), 6u); -TEST_F(BuilderTest, - DISABLED_Constructor_Type_ModuleScope_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) { - FAIL(); + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 +%2 = OpTypeVector %3 4 +%1 = OpTypeMatrix %2 4 +%4 = OpConstant %3 2 +%5 = OpConstantComposite %2 %4 %4 %4 %4 +%6 = OpConstantComposite %1 %5 %5 %5 %5 +)"); } TEST_F(BuilderTest, DISABLED_Constructor_Type_Array_5_F32) { @@ -1186,10 +1618,6 @@ TEST_F(BuilderTest, DISABLED_Constructor_Type_Array_5_Vec3) { FAIL(); } -TEST_F(BuilderTest, DISABLED_Constructor_Type_ModuleScope_Array_5_Vec3) { - FAIL(); -} - TEST_F(BuilderTest, DISABLED_Constructor_Type_Struct) { FAIL(); }