Fixup single element swizzle type determination.

For a swizzle with one element (eg vec.x) the result type is just the
type of the vector, instead of a new vector.

Change-Id: I04ddb22da61db1c3553d465e4e5f9d6b32beae83
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20062
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-21 13:05:34 +00:00 committed by dan sinclair
parent 54444382a6
commit aac5865121
2 changed files with 31 additions and 6 deletions

View File

@ -400,12 +400,17 @@ bool TypeDeterminer::DetermineMemberAccessor(
if (data_type->IsVector()) { if (data_type->IsVector()) {
auto* vec = data_type->AsVector(); auto* vec = data_type->AsVector();
// The vector will have a number of components equal to the length of the auto size = expr->member()->name().size();
// swizzle. This assumes the validator will check that the swizzle if (size == 1) {
// is correct. // A single element swizzle is just the type of the vector.
expr->set_result_type( expr->set_result_type(vec->type());
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>( } else {
vec->type(), expr->member()->name().size()))); // The vector will have a number of components equal to the length of the
// swizzle. This assumes the validator will check that the swizzle
// is correct.
expr->set_result_type(ctx_.type_mgr().Get(
std::make_unique<ast::type::VectorType>(vec->type(), size)));
}
return true; return true;
} }

View File

@ -745,6 +745,26 @@ TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle) {
EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u); EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u);
} }
TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) {
ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3);
auto var = std::make_unique<ast::Variable>("my_vec", ast::StorageClass::kNone,
&vec3);
mod()->AddGlobalVariable(std::move(var));
// Register the global
EXPECT_TRUE(td()->Determine());
auto ident = std::make_unique<ast::IdentifierExpression>("my_vec");
auto swizzle = std::make_unique<ast::IdentifierExpression>("x");
ast::MemberAccessorExpression mem(std::move(ident), std::move(swizzle));
EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error();
ASSERT_NE(mem.result_type(), nullptr);
ASSERT_TRUE(mem.result_type()->IsF32());
}
TEST_F(TypeDeterminerTest, Expr_MultiLevel) { TEST_F(TypeDeterminerTest, Expr_MultiLevel) {
// struct b { // struct b {
// vec4<f32> foo // vec4<f32> foo