Implement vector initialization from single scalar (aka "splat")

Updated spir-v and HSLS backends to emit valid constructors.

Bug: tint:656
Change-Id: I53356945563b633239b12c0f4e207f160dbc23d8
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53780
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@chromium.org>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano
2021-06-10 13:23:31 +00:00
committed by Tint LUCI CQ
parent 5b2f49b5df
commit b5508fdcb5
13 changed files with 988 additions and 21 deletions

View File

@@ -1895,10 +1895,10 @@ bool Resolver::ValidateVectorConstructor(
}
}
// A correct vector constructor must either be a zero-value expression
// or the number of components of all constructor arguments must add up
// to the vector cardinality.
if (value_cardinality_sum > 0 && value_cardinality_sum != vec_type->size()) {
// A correct vector constructor must either be a zero-value expression,
// a single-value initializer (splat) expression, or the number of components
// of all constructor arguments must add up to the vector cardinality.
if (value_cardinality_sum > 1 && value_cardinality_sum != vec_type->size()) {
if (values.empty()) {
TINT_ICE(diagnostics_)
<< "constructor arguments expected to be non-empty!";

View File

@@ -776,18 +776,6 @@ TEST_F(ResolverValidationTest,
"12:34 error: attempted to construct 'vec2<f32>' with 4 component(s)");
}
TEST_F(ResolverValidationTest,
Expr_Constructor_Vec2_Error_TooFewArgumentsScalar) {
auto* tc = vec2<f32>(create<ast::ScalarConstructorExpression>(
Source{{12, 34}}, Literal(1.0f)));
WrapInFunction(tc);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
"12:34 error: attempted to construct 'vec2<f32>' with 1 component(s)");
}
TEST_F(ResolverValidationTest,
Expr_Constructor_Vec2_Error_TooManyArgumentsScalar) {
auto* tc = vec2<f32>(
@@ -1632,16 +1620,16 @@ TEST_F(ResolverValidationTest,
TEST_F(ResolverValidationTest,
Expr_Constructor_NestedVectorConstructors_InnerError) {
auto* tc = vec4<f32>(
vec3<f32>(1.0f, vec2<f32>(create<ast::ScalarConstructorExpression>(
Source{{12, 34}}, Literal(1.0f)))),
1.0f);
auto* tc = vec4<f32>(vec4<f32>(1.0f, 1.0f,
vec3<f32>(Expr(Source{{12, 34}}, 1.0f),
Expr(Source{{12, 34}}, 1.0f))),
1.0f);
WrapInFunction(tc);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
"12:34 error: attempted to construct 'vec2<f32>' with 1 component(s)");
"12:34 error: attempted to construct 'vec3<f32>' with 2 component(s)");
}
TEST_F(ResolverValidationTest,