writer/hlsl: Clean up vector type emission

For HLSL emission instead of:
  `vector<float, N>` emit `floatN`
  `vector<int, N>` emit `intN`
  `vector<uint, N>` emit `uintN`

These are significantly shorter, more idiomatic, and is far easier to read.

Change-Id: Idef8cc550e0b49cc919087e281b72a7a0a0f11bc
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33424
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2020-11-19 16:49:12 +00:00 committed by Commit Bot service account
parent 36d9a061c8
commit 31df1137d4
9 changed files with 39 additions and 34 deletions

View File

@ -2036,11 +2036,20 @@ bool GeneratorImpl::EmitType(std::ostream& out,
out << "uint"; out << "uint";
} else if (type->IsVector()) { } else if (type->IsVector()) {
auto* vec = type->AsVector(); auto* vec = type->AsVector();
auto size = vec->size();
if (vec->type()->IsF32() && size >= 1 && size <= 4) {
out << "float" << size;
} else if (vec->type()->IsI32() && size >= 1 && size <= 4) {
out << "int" << size;
} else if (vec->type()->IsU32() && size >= 1 && size <= 4) {
out << "uint" << size;
} else {
out << "vector<"; out << "vector<";
if (!EmitType(out, vec->type(), "")) { if (!EmitType(out, vec->type(), "")) {
return false; return false;
} }
out << ", " << vec->size() << ">"; out << ", " << size << ">";
}
} else if (type->IsVoid()) { } else if (type->IsVoid()) {
out << "void"; out << "void";
} else { } else {

View File

@ -50,7 +50,7 @@ TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Vector) {
ast::TypeConstructorExpression cast(&vec3, params); ast::TypeConstructorExpression cast(&vec3, params);
ASSERT_TRUE(gen.EmitExpression(pre, out, &cast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, &cast)) << gen.error();
EXPECT_EQ(result(), "vector<float, 3>(id)"); EXPECT_EQ(result(), "float3(id)");
} }
} // namespace } // namespace

View File

@ -140,8 +140,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec) {
ast::TypeConstructorExpression expr(&vec, values); ast::TypeConstructorExpression expr(&vec, values);
ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error(); ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(), "float3(1.00000000f, 2.00000000f, 3.00000000f)");
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)");
} }
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec_Empty) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec_Empty) {
@ -152,7 +151,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec_Empty) {
ast::TypeConstructorExpression expr(&vec, values); ast::TypeConstructorExpression expr(&vec, values);
ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error(); ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error();
EXPECT_EQ(result(), "vector<float, 3>(0.0f)"); EXPECT_EQ(result(), "float3(0.0f)");
} }
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) {
@ -187,10 +186,9 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) {
// A matrix of type T with n columns and m rows can also be constructed from // A matrix of type T with n columns and m rows can also be constructed from
// n vectors of type T with m components. // n vectors of type T with m components.
EXPECT_EQ(result(), EXPECT_EQ(result(), std::string("matrix<float, 3, 2>(float3(1.00000000f, "
std::string("matrix<float, 3, 2>(vector<float, 3>(1.00000000f, "
"2.00000000f, 3.00000000f), ") + "2.00000000f, 3.00000000f), ") +
"vector<float, 3>(3.00000000f, 4.00000000f, 5.00000000f))"); "float3(3.00000000f, 4.00000000f, 5.00000000f))");
} }
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) {
@ -219,11 +217,10 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) {
ast::TypeConstructorExpression expr(&ary, ary_values); ast::TypeConstructorExpression expr(&ary, ary_values);
ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error(); ASSERT_TRUE(gen.EmitConstructor(pre, out, &expr)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(), std::string("{") +
std::string("{") + "float3(1.00000000f, 2.00000000f, 3.00000000f), " +
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " + "float3(4.00000000f, 5.00000000f, 6.00000000f), " +
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f), " + "float3(7.00000000f, 8.00000000f, 9.00000000f)}");
"vector<float, 3>(7.00000000f, 8.00000000f, 9.00000000f)}");
} }
// TODO(dsinclair): Add struct constructor test. // TODO(dsinclair): Add struct constructor test.

View File

@ -364,7 +364,7 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
// [[builtin frag_depth]] var<out> depth : f32; // [[builtin frag_depth]] var<out> depth : f32;
// //
// struct main_in { // struct main_in {
// vector<float, 4> coord : SV_Position; // float4 coord : SV_Position;
// }; // };
// //
// struct main_out { // struct main_out {
@ -410,7 +410,7 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct main_in { EXPECT_EQ(result(), R"(struct main_in {
vector<float, 4> coord : SV_Position; float4 coord : SV_Position;
}; };
struct main_out { struct main_out {

View File

@ -207,7 +207,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct frag_main_in { EXPECT_EQ(result(), R"(struct frag_main_in {
vector<float, 4> coord : SV_Position; float4 coord : SV_Position;
}; };
struct frag_main_out { struct frag_main_out {
@ -258,7 +258,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(cbuffer : register(b0) { EXPECT_EQ(result(), R"(cbuffer : register(b0) {
vector<float, 4> coord; float4 coord;
}; };
void frag_main() { void frag_main() {
@ -317,7 +317,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct Uniforms { EXPECT_EQ(result(), R"(struct Uniforms {
vector<float, 4> coord; float4 coord;
}; };
ConstantBuffer<Uniforms> uniforms : register(b0); ConstantBuffer<Uniforms> uniforms : register(b0);
@ -717,7 +717,7 @@ TEST_F(
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct ep_1_in { EXPECT_EQ(result(), R"(struct ep_1_in {
vector<float, 4> coord : SV_Position; float4 coord : SV_Position;
}; };
struct ep_1_out { struct ep_1_out {
@ -789,7 +789,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(cbuffer : register(b0) { EXPECT_EQ(result(), R"(cbuffer : register(b0) {
vector<float, 4> coord; float4 coord;
}; };
float sub_func(float param) { float sub_func(float param) {

View File

@ -176,10 +176,9 @@ TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
ASSERT_TRUE(gen.EmitCall(pre, out, &expr)) << gen.error(); ASSERT_TRUE(gen.EmitCall(pre, out, &expr)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(), std::string(param.hlsl_name) +
std::string(param.hlsl_name) + "(float3(1.00000000f, 2.00000000f, 3.00000000f), "
"(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " "float3(4.00000000f, 5.00000000f, 6.00000000f))");
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f))");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_DualParam_VectorTest, HlslImportData_DualParam_VectorTest,

View File

@ -780,7 +780,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
// var<storage_buffer> data : Data; // var<storage_buffer> data : Data;
// data.b = vec3<f32>(2.3f, 1.2f, 0.2f); // data.b = vec3<f32>(2.3f, 1.2f, 0.2f);
// //
// -> data.Store(16, asuint(vector<float, 3>(2.3f, 1.2f, 0.2f))); // -> data.Store(16, asuint(float3(2.3f, 1.2f, 0.2f)));
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::I32Type i32; ast::type::I32Type i32;
@ -829,7 +829,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error();
EXPECT_EQ( EXPECT_EQ(
result(), result(),
R"(data.Store3(16, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f))); R"(data.Store3(16, asuint(float3(1.00000000f, 2.00000000f, 3.00000000f)));
)"); )");
} }
@ -1131,7 +1131,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
// var<storage_buffer> data : Pre; // var<storage_buffer> data : Pre;
// data.c[2].b = vec3<f32>(1.f, 2.f, 3.f); // data.c[2].b = vec3<f32>(1.f, 2.f, 3.f);
// //
// -> data.Store3(16 + (2 * 32), asuint(vector<float, 3>(1.0f, 2.0f, 3.0f))); // -> data.Store3(16 + (2 * 32), asuint(float3(1.0f, 2.0f, 3.0f)));
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::I32Type i32; ast::type::I32Type i32;
@ -1197,7 +1197,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, &assign)) << gen.error();
EXPECT_EQ( EXPECT_EQ(
result(), result(),
R"(data.Store3(16 + (32 * 2) + 0, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f))); R"(data.Store3(16 + (32 * 2) + 0, asuint(float3(1.00000000f, 2.00000000f, 3.00000000f)));
)"); )");
} }

View File

@ -310,7 +310,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Vector) {
ast::type::VectorType v(&f32, 3); ast::type::VectorType v(&f32, 3);
ASSERT_TRUE(gen.EmitType(out, &v, "")) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &v, "")) << gen.error();
EXPECT_EQ(result(), "vector<float, 3>"); EXPECT_EQ(result(), "float3");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) {

View File

@ -118,7 +118,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
ast::VariableDeclStatement stmt(var); ast::VariableDeclStatement stmt(var);
ASSERT_TRUE(gen.EmitStatement(out, &stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, &stmt)) << gen.error();
EXPECT_EQ(result(), R"(vector<float, 3> a = vector<float, 3>(0.0f); EXPECT_EQ(result(), R"(float3 a = float3(0.0f);
)"); )");
} }