writer/msl: Emit field designators for structures

This fixes constructors for structures that contain padding members
due to explicit layout attributes.

Also fix one test that was wrongly using an identity type constructor
for a structure.

Fixed: tint:853
Change-Id: I0a3e84fcd7c6a7f2ad92a4970ed11378e6ce2465
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53240
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price
2021-06-03 18:12:15 +00:00
committed by Tint LUCI CQ
parent 71786c99b3
commit 851b18b2f2
23 changed files with 102 additions and 71 deletions

View File

@@ -918,16 +918,24 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
out_ << "(";
}
bool first = true;
int i = 0;
for (auto* e : expr->values()) {
if (!first) {
if (i > 0) {
out_ << ", ";
}
first = false;
if (auto* struct_ty = type->As<sem::Struct>()) {
// Emit field designators for structures to account for padding members.
auto* member = struct_ty->Members()[i]->Declaration();
auto name = program_->Symbols().NameFor(member->symbol());
out_ << "." << name << "=";
}
if (!EmitExpression(e)) {
return false;
}
i++;
}
if (type->IsAnyOf<sem::Array, sem::Struct>()) {

View File

@@ -165,7 +165,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("{1, 2.0f, int3(3, 4, 5)}"));
EXPECT_THAT(gen.result(), HasSubstr("{.a=1, .b=2.0f, .c=int3(3, 4, 5)}"));
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct_Empty) {

View File

@@ -113,7 +113,7 @@ struct tint_symbol_2 {
fragment tint_symbol_2 frag_main(tint_symbol_1 tint_symbol [[stage_in]]) {
float const foo = tint_symbol.foo;
tint_symbol_2 const tint_symbol_3 = {foo};
tint_symbol_2 const tint_symbol_3 = {.value=foo};
return tint_symbol_3;
}
@@ -148,7 +148,7 @@ struct tint_symbol_2 {
fragment tint_symbol_2 frag_main(tint_symbol_1 tint_symbol [[stage_in]]) {
float4 const coord = tint_symbol.coord;
tint_symbol_2 const tint_symbol_3 = {coord.x};
tint_symbol_2 const tint_symbol_3 = {.value=coord.x};
return tint_symbol_3;
}
@@ -216,13 +216,13 @@ struct tint_symbol_3 {
};
vertex tint_symbol vert_main() {
Interface const tint_symbol_1 = {0.5f, 0.25f, float4()};
tint_symbol const tint_symbol_4 = {tint_symbol_1.col1, tint_symbol_1.col2, tint_symbol_1.pos};
Interface const tint_symbol_1 = {.col1=0.5f, .col2=0.25f, .pos=float4()};
tint_symbol const tint_symbol_4 = {.col1=tint_symbol_1.col1, .col2=tint_symbol_1.col2, .pos=tint_symbol_1.pos};
return tint_symbol_4;
}
fragment void frag_main(tint_symbol_3 tint_symbol_2 [[stage_in]]) {
Interface const colors = {tint_symbol_2.col1, tint_symbol_2.col2, tint_symbol_2.pos};
Interface const colors = {.col1=tint_symbol_2.col1, .col2=tint_symbol_2.col2, .pos=tint_symbol_2.pos};
float const r = colors.col1;
float const g = colors.col2;
return;
@@ -257,13 +257,12 @@ TEST_F(MslGeneratorImplTest,
{});
Func("vert_main1", {}, vertex_output_struct,
{Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.5f)))))},
{Return(Expr(Call("foo", Expr(0.5f))))},
{Stage(ast::PipelineStage::kVertex)});
Func(
"vert_main2", {}, vertex_output_struct,
{Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.25f)))))},
{Stage(ast::PipelineStage::kVertex)});
Func("vert_main2", {}, vertex_output_struct,
{Return(Expr(Call("foo", Expr(0.25f))))},
{Stage(ast::PipelineStage::kVertex)});
GeneratorImpl& gen = SanitizeAndBuild();
@@ -282,19 +281,19 @@ struct tint_symbol_2 {
};
VertexOutput foo(float x) {
VertexOutput const tint_symbol_4 = {float4(x, x, x, 1.0f)};
VertexOutput const tint_symbol_4 = {.pos=float4(x, x, x, 1.0f)};
return tint_symbol_4;
}
vertex tint_symbol vert_main1() {
VertexOutput const tint_symbol_1 = {foo(0.5f)};
tint_symbol const tint_symbol_5 = {tint_symbol_1.pos};
VertexOutput const tint_symbol_1 = foo(0.5f);
tint_symbol const tint_symbol_5 = {.pos=tint_symbol_1.pos};
return tint_symbol_5;
}
vertex tint_symbol_2 vert_main2() {
VertexOutput const tint_symbol_3 = {foo(0.25f)};
tint_symbol_2 const tint_symbol_6 = {tint_symbol_3.pos};
VertexOutput const tint_symbol_3 = foo(0.25f);
tint_symbol_2 const tint_symbol_6 = {.pos=tint_symbol_3.pos};
return tint_symbol_6;
}