From be6fb2a8d2f3c574ef0b3ced7b19c3a6541ece35 Mon Sep 17 00:00:00 2001 From: Zhaoming Jiang Date: Fri, 17 Jun 2022 12:48:46 +0000 Subject: [PATCH] tint: Fix emitting vector constant in HLSL writer This patch fix the issue that HLSL generator emit wrong constant element index for vectors with all elements being the same, which may cause error when emitting a matrix constant with sub columns having identical elements. Corresponding unit test is implemented. Bug: tint:1588 Change-Id: Ia40b3f1a676d84aadaa5ce900677547fb15abe7f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94041 Commit-Queue: Zhaoming Jiang Kokoro: Kokoro Reviewed-by: Ben Clayton --- src/tint/writer/hlsl/generator_impl.cc | 10 +++---- .../hlsl/generator_impl_constructor_test.cc | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index b76b9e6cf6..0906940325 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -3162,11 +3162,11 @@ bool GeneratorImpl::EmitConstant(std::ostream& out, const sem::Constant& constan { ScopedParen sp(out); bool ok = Switch( - vec_ty->type(), // - [&](const sem::Bool*) { return emit_bool(0); }, // - [&](const sem::F32*) { return emit_f32(0); }, // - [&](const sem::I32*) { return emit_i32(0); }, // - [&](const sem::U32*) { return emit_u32(0); } // + vec_ty->type(), // + [&](const sem::Bool*) { return emit_bool(start); }, // + [&](const sem::F32*) { return emit_f32(start); }, // + [&](const sem::I32*) { return emit_i32(start); }, // + [&](const sem::U32*) { return emit_u32(start); } // ); if (!ok) { return false; diff --git a/src/tint/writer/hlsl/generator_impl_constructor_test.cc b/src/tint/writer/hlsl/generator_impl_constructor_test.cc index fbcc79b693..700d94bada 100644 --- a/src/tint/writer/hlsl/generator_impl_constructor_test.cc +++ b/src/tint/writer/hlsl/generator_impl_constructor_test.cc @@ -186,6 +186,33 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) { HasSubstr("float2x3(float3(1.0f, 2.0f, 3.0f), float3(3.0f, 4.0f, 5.0f))")); } +TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat_Complex) { + // mat4x4( + // vec4(2.0f, 3.0f, 4.0f, 8.0f), + // vec4(), + // vec4(7.0f), + // vec4(vec4(42.0f, 21.0f, 6.0f, -5.0f)), + // ); + auto* vector_literal = + vec4(Expr(f32(2.0)), Expr(f32(3.0)), Expr(f32(4.0)), Expr(f32(8.0))); + auto* vector_zero_ctor = vec4(); + auto* vector_single_scalar_ctor = vec4(Expr(f32(7.0))); + auto* vector_identical_ctor = + vec4(vec4(Expr(f32(42.0)), Expr(f32(21.0)), Expr(f32(6.0)), Expr(f32(-5.0)))); + + auto* constructor = mat4x4(vector_literal, vector_zero_ctor, vector_single_scalar_ctor, + vector_identical_ctor); + + WrapInFunction(constructor); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.Generate()) << gen.error(); + + EXPECT_THAT(gen.result(), HasSubstr("float4x4(float4(2.0f, 3.0f, 4.0f, 8.0f), (0.0f).xxxx, " + "(7.0f).xxxx, float4(42.0f, 21.0f, 6.0f, -5.0f))")); +} + TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat_Empty) { WrapInFunction(mat2x3());