blob: 9bb6a4ad94735534a9fff06116c8c50d745980fd [file] [log] [blame]
// Copyright 2021 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/tint/lang/wgsl/resolver/resolver.h"
#include "gmock/gmock.h"
#include "src/tint/lang/core/type/helper_test.h"
#include "src/tint/lang/core/type/sampled_texture.h"
#include "src/tint/lang/core/type/texture_dimension.h"
#include "src/tint/lang/wgsl/ast/assignment_statement.h"
#include "src/tint/lang/wgsl/ast/break_statement.h"
#include "src/tint/lang/wgsl/ast/builtin_texture_helper_test.h"
#include "src/tint/lang/wgsl/ast/call_statement.h"
#include "src/tint/lang/wgsl/ast/continue_statement.h"
#include "src/tint/lang/wgsl/ast/if_statement.h"
#include "src/tint/lang/wgsl/ast/loop_statement.h"
#include "src/tint/lang/wgsl/ast/return_statement.h"
#include "src/tint/lang/wgsl/ast/stage_attribute.h"
#include "src/tint/lang/wgsl/ast/switch_statement.h"
#include "src/tint/lang/wgsl/ast/unary_op_expression.h"
#include "src/tint/lang/wgsl/ast/variable_decl_statement.h"
#include "src/tint/lang/wgsl/resolver/resolver_helper_test.h"
#include "src/tint/lang/wgsl/sem/call.h"
#include "src/tint/lang/wgsl/sem/function.h"
#include "src/tint/lang/wgsl/sem/member_accessor_expression.h"
#include "src/tint/lang/wgsl/sem/statement.h"
#include "src/tint/lang/wgsl/sem/variable.h"
#include "src/tint/utils/text/string.h"
#include "src/tint/utils/text/string_stream.h"
using ::testing::ElementsAre;
using ::testing::HasSubstr;
namespace tint::resolver {
namespace {
using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT
using ExpressionList = Vector<const ast::Expression*, 8>;
using ResolverBuiltinTest = ResolverTest;
struct BuiltinData {
const char* name;
wgsl::BuiltinFn builtin;
};
inline std::ostream& operator<<(std::ostream& out, BuiltinData data) {
out << data.name;
return out;
}
TEST_F(ResolverBuiltinTest, ModuleScopeUsage) {
GlobalConst("c", ty.f32(), Call(Source{{12, 34}}, "dpdy", 1._f));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: const initializer requires a const-expression, but expression is a runtime-expression)");
}
TEST_F(ResolverBuiltinTest, SameOverloadReturnsSameCallTarget) {
// let i = 42i;
// let a = select(1_i, 2_i, true);
// let b = select(3_i, 4_i, false);
// let c = select(5_u, 6_u, true);
auto* select_a = Call(wgsl::BuiltinFn::kSelect, 1_i, 2_i, true);
auto* select_b = Call(wgsl::BuiltinFn::kSelect, 3_i, 4_i, false);
auto* select_c = Call(wgsl::BuiltinFn::kSelect, 5_u, 6_u, true);
WrapInFunction(Decl(Let("i", Expr(42_i))), //
Decl(Let("a", select_a)), //
Decl(Let("b", select_b)), //
Decl(Let("c", select_c)));
EXPECT_TRUE(r()->Resolve()) << r()->error();
EXPECT_EQ(Sem().Get<sem::Call>(select_a)->Target(), Sem().Get<sem::Call>(select_b)->Target());
EXPECT_NE(Sem().Get<sem::Call>(select_a)->Target(), Sem().Get<sem::Call>(select_c)->Target());
EXPECT_NE(Sem().Get<sem::Call>(select_b)->Target(), Sem().Get<sem::Call>(select_c)->Target());
}
// Tests for Logical builtins
namespace logical_builtin_tests {
using ResolverBuiltinTest_BoolMethod = ResolverTestWithParam<std::string>;
TEST_P(ResolverBuiltinTest_BoolMethod, Scalar) {
auto name = GetParam();
GlobalVar("my_var", ty.bool_(), core::AddressSpace::kPrivate);
auto* expr = Call(name, "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::Bool>());
}
TEST_P(ResolverBuiltinTest_BoolMethod, Vector) {
auto name = GetParam();
GlobalVar("my_var", ty.vec3<bool>(), core::AddressSpace::kPrivate);
auto* expr = Call(name, "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::Bool>());
}
INSTANTIATE_TEST_SUITE_P(ResolverTest,
ResolverBuiltinTest_BoolMethod,
testing::Values("any", "all"));
TEST_F(ResolverBuiltinTest, Select) {
GlobalVar("my_var", ty.vec3<f32>(), core::AddressSpace::kPrivate);
GlobalVar("bool_var", ty.vec3<bool>(), core::AddressSpace::kPrivate);
auto* expr = Call("select", "my_var", "my_var", "bool_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::Vector>());
EXPECT_EQ(TypeOf(expr)->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(TypeOf(expr)->As<core::type::Vector>()->type()->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinTest, Select_Error_NoParams) {
auto* expr = Call("select");
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'select()'
3 candidate functions:
'select(T ✗ , T ✗ , bool ✗ ) -> T' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , bool ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , vecN<bool> ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
)");
}
TEST_F(ResolverBuiltinTest, Select_Error_SelectorInt) {
auto* expr = Call("select", 1_i, 1_i, 1_i);
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'select(i32, i32, i32)'
3 candidate functions:
'select(T ✓ , T ✓ , bool ✗ ) -> T' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , bool ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , vecN<bool> ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
)");
}
TEST_F(ResolverBuiltinTest, Select_Error_Matrix) {
auto* expr =
Call("select", Call<mat2x2<f32>>(Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f)),
Call<mat2x2<f32>>(Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f)), Expr(true));
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'select(mat2x2<f32>, mat2x2<f32>, bool)'
3 candidate functions:
'select(T ✗ , T ✗ , bool ✓ ) -> T' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , bool ✓ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✗ , vecN<bool> ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
)");
}
TEST_F(ResolverBuiltinTest, Select_Error_MismatchTypes) {
auto* expr = Call("select", 1_f, Call<vec2<f32>>(2_f, 3_f), Expr(true));
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'select(f32, vec2<f32>, bool)'
3 candidate functions:
'select(T ✓ , T ✗ , bool ✓ ) -> T' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✓ , bool ✓ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✗ , vecN<T> ✓ , vecN<bool> ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
)");
}
TEST_F(ResolverBuiltinTest, Select_Error_MismatchVectorSize) {
auto* expr = Call("select", Call<vec2<f32>>(1_f, 2_f), Call<vec3<f32>>(3_f, 4_f, 5_f), true);
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'select(vec2<f32>, vec3<f32>, bool)'
3 candidate functions:
'select(vecN<T> ✓ , vecN<T> ✗ , bool ✓ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(T ✗ , T ✗ , bool ✓ ) -> T' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
'select(vecN<T> ✓ , vecN<T> ✗ , vecN<bool> ✗ ) -> vecN<T>' where:
'T' is 'abstract-int', 'abstract-float', 'f32', 'f16', 'i32', 'u32' or 'bool'
)");
}
} // namespace logical_builtin_tests
// Tests for Array builtins
namespace array_builtin_tests {
using ResolverBuiltinArrayTest = ResolverTest;
TEST_F(ResolverBuiltinArrayTest, ArrayLength_Vector) {
auto ary = ty.array<i32>();
auto* str = Structure("S", Vector{Member("x", ary)});
GlobalVar("a", ty.Of(str), core::AddressSpace::kStorage, core::Access::kRead, Binding(0_a),
Group(0_a));
auto* call = Call("arrayLength", AddressOf(MemberAccessor("a", "x")));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
}
TEST_F(ResolverBuiltinArrayTest, ArrayLength_Error_ArraySized) {
GlobalVar("arr", ty.array<i32, 4>(), core::AddressSpace::kPrivate);
auto* call = Call("arrayLength", AddressOf("arr"));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'arrayLength(ptr<private, array<i32, 4>, read_write>)'
2 candidate functions:
'arrayLength(ptr<storage, array<T>, R> ✗ ) -> u32' where:
'R' is 'read'
'arrayLength(ptr<storage, array<T>, W> ✗ ) -> u32' where:
'W' is 'write' or 'read_write'
)");
}
} // namespace array_builtin_tests
// Tests for Numeric builtins with float parameter
namespace float_builtin_tests {
// Testcase parameters for float built-in having signature of (T, ...) -> T and (vecN<T>, ...) ->
// vecN<T>
struct BuiltinDataWithParamNum {
uint32_t args_number;
const char* name;
wgsl::BuiltinFn builtin;
};
inline std::ostream& operator<<(std::ostream& out, BuiltinDataWithParamNum data) {
out << data.name;
return out;
}
// Tests for float built-ins that has signiture (T, ...) -> T and (vecN<T>, ...) -> vecN<T>
using ResolverBuiltinTest_FloatBuiltin_IdenticalType =
ResolverTestWithParam<BuiltinDataWithParamNum>;
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, Error_NoParams) {
auto param = GetParam();
auto* call = Call(param.name);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "()'"));
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, OneParam_Scalar_f32) {
auto param = GetParam();
auto val = 0.5_f;
if (param.name == std::string("acosh")) {
val = 1.0_f;
}
auto* call = Call(param.name, val);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "(f32)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, OneParam_Vector_f32) {
auto param = GetParam();
auto val = param.name == std::string("acosh") ? Call<vec3<f32>>(1.0_f, 2.0_f, 3.0_f)
: Call<vec3<f32>>(0.5_f, 0.5_f, 0.8_f);
auto* call = Call(param.name, val);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<f32>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, TwoParams_Scalar_f32) {
auto param = GetParam();
auto* call = Call(param.name, 1_f, 1_f);
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f32, f32)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, TwoParams_Vector_f32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<f32>, vec3<f32>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, ThreeParams_Scalar_f32) {
auto param = GetParam();
auto* call = Call(param.name, 0_f, 1_f, 2_f);
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f32, f32, f32)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, ThreeParams_Vector_f32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<f32>>(0_f, 0_f, 0_f), Call<vec3<f32>>(1_f, 1_f, 1_f),
Call<vec3<f32>>(2_f, 2_f, 2_f));
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<f32>, vec3<f32>, vec3<f32>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, FourParams_Scalar_f32) {
auto param = GetParam();
auto* call = Call(param.name, 1_f, 1_f, 1_f, 1_f);
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f32, f32, f32, f32)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, FourParams_Vector_f32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f),
Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<f32>, vec3<f32>, vec3<f32>, vec3<f32>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, OneParam_Scalar_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto val = 0.5_h;
if (param.name == std::string("acosh")) {
val = 1.0_h;
}
auto* call = Call(param.name, val);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "(f16)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, OneParam_Vector_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto val = param.name == std::string("acosh") ? Call<vec3<f16>>(1.0_h, 2.0_h, 3.0_h)
: Call<vec3<f16>>(0.5_h, 0.5_h, 0.8_h);
auto* call = Call(param.name, val);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<f16>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, TwoParams_Scalar_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, 1_h, 1_h);
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f16, f16)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, TwoParams_Vector_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, Call<vec3<f16>>(1_h, 1_h, 3_h), Call<vec3<f16>>(1_h, 1_h, 3_h));
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<f16>, vec3<f16>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, ThreeParams_Scalar_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, 0_h, 1_h, 2_h);
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f16, f16, f16)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, ThreeParams_Vector_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, Call<vec3<f16>>(0_h, 0_h, 0_h), Call<vec3<f16>>(1_h, 1_h, 1_h),
Call<vec3<f16>>(2_h, 2_h, 2_h));
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<f16>, vec3<f16>, vec3<f16>)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, FourParams_Scalar_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, 1_h, 1_h, 1_h, 1_h);
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(f16, f16, f16, f16)'"));
}
}
TEST_P(ResolverBuiltinTest_FloatBuiltin_IdenticalType, FourParams_Vector_f16) {
auto param = GetParam();
Enable(wgsl::Extension::kF16);
auto* call = Call(param.name, Call<vec3<f16>>(1_h, 1_h, 3_h), Call<vec3<f16>>(1_h, 1_h, 3_h),
Call<vec3<f16>>(1_h, 1_h, 3_h), Call<vec3<f16>>(1_h, 1_h, 3_h));
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<f16>, vec3<f16>, vec3<f16>, vec3<f16>)'"));
}
}
INSTANTIATE_TEST_SUITE_P(
ResolverTest,
ResolverBuiltinTest_FloatBuiltin_IdenticalType,
testing::Values(BuiltinDataWithParamNum{1, "abs", wgsl::BuiltinFn::kAbs},
BuiltinDataWithParamNum{1, "acos", wgsl::BuiltinFn::kAcos},
BuiltinDataWithParamNum{1, "acosh", wgsl::BuiltinFn::kAcos},
BuiltinDataWithParamNum{1, "asin", wgsl::BuiltinFn::kAsin},
BuiltinDataWithParamNum{1, "asinh", wgsl::BuiltinFn::kAsin},
BuiltinDataWithParamNum{1, "atan", wgsl::BuiltinFn::kAtan},
BuiltinDataWithParamNum{1, "atanh", wgsl::BuiltinFn::kAtan},
BuiltinDataWithParamNum{2, "atan2", wgsl::BuiltinFn::kAtan2},
BuiltinDataWithParamNum{1, "ceil", wgsl::BuiltinFn::kCeil},
BuiltinDataWithParamNum{3, "clamp", wgsl::BuiltinFn::kClamp},
BuiltinDataWithParamNum{1, "cos", wgsl::BuiltinFn::kCos},
BuiltinDataWithParamNum{1, "cosh", wgsl::BuiltinFn::kCosh},
// cross: (vec3<T>, vec3<T>) -> vec3<T>
BuiltinDataWithParamNum{1, "degrees", wgsl::BuiltinFn::kDegrees},
// distance: (T, T) -> T, (vecN<T>, vecN<T>) -> T
BuiltinDataWithParamNum{1, "exp", wgsl::BuiltinFn::kExp},
BuiltinDataWithParamNum{1, "exp2", wgsl::BuiltinFn::kExp2},
// faceForward: (vecN<T>, vecN<T>, vecN<T>) -> vecN<T>
BuiltinDataWithParamNum{1, "floor", wgsl::BuiltinFn::kFloor},
BuiltinDataWithParamNum{3, "fma", wgsl::BuiltinFn::kFma},
BuiltinDataWithParamNum{1, "fract", wgsl::BuiltinFn::kFract},
// frexp
BuiltinDataWithParamNum{1, "inverseSqrt", wgsl::BuiltinFn::kInverseSqrt},
// ldexp: (T, i32) -> T, (vecN<T>, vecN<i32>) -> vecN<T>
// length: (vecN<T>) -> T
BuiltinDataWithParamNum{1, "log", wgsl::BuiltinFn::kLog},
BuiltinDataWithParamNum{1, "log2", wgsl::BuiltinFn::kLog2},
BuiltinDataWithParamNum{2, "max", wgsl::BuiltinFn::kMax},
BuiltinDataWithParamNum{2, "min", wgsl::BuiltinFn::kMin},
// Note that `mix(vecN<f32>, vecN<f32>, f32) -> vecN<f32>` is not tested here.
BuiltinDataWithParamNum{3, "mix", wgsl::BuiltinFn::kMix},
// modf
// normalize: (vecN<T>) -> vecN<T>
BuiltinDataWithParamNum{2, "pow", wgsl::BuiltinFn::kPow},
// quantizeToF16 is not implemented yet.
BuiltinDataWithParamNum{1, "radians", wgsl::BuiltinFn::kRadians},
// reflect: (vecN<T>, vecN<T>) -> vecN<T>
// refract: (vecN<T>, vecN<T>, T) -> vecN<T>
BuiltinDataWithParamNum{1, "round", wgsl::BuiltinFn::kRound},
// saturate not implemented yet.
BuiltinDataWithParamNum{1, "sign", wgsl::BuiltinFn::kSign},
BuiltinDataWithParamNum{1, "sin", wgsl::BuiltinFn::kSin},
BuiltinDataWithParamNum{1, "sinh", wgsl::BuiltinFn::kSinh},
BuiltinDataWithParamNum{3, "smoothstep", wgsl::BuiltinFn::kSmoothstep},
BuiltinDataWithParamNum{1, "sqrt", wgsl::BuiltinFn::kSqrt},
BuiltinDataWithParamNum{2, "step", wgsl::BuiltinFn::kStep},
BuiltinDataWithParamNum{1, "tan", wgsl::BuiltinFn::kTan},
BuiltinDataWithParamNum{1, "tanh", wgsl::BuiltinFn::kTanh},
BuiltinDataWithParamNum{1, "trunc", wgsl::BuiltinFn::kTrunc}));
using ResolverBuiltinFloatTest = ResolverTest;
// cross: (vec3<T>, vec3<T>) -> vec3<T>
TEST_F(ResolverBuiltinFloatTest, Cross_f32) {
auto* call = Call("cross", Call<vec3<f32>>(1_f, 2_f, 3_f), Call<vec3<f32>>(1_f, 2_f, 3_f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Cross_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("cross", Call<vec3<f16>>(1_h, 2_h, 3_h), Call<vec3<f16>>(1_h, 2_h, 3_h));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Cross_Error_NoArgs) {
auto* call = Call("cross");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'cross()'
1 candidate function:
'cross(vec3<T> ✗ , vec3<T> ✗ ) -> vec3<T>' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Cross_Error_Scalar) {
auto* call = Call("cross", 1_f, 1_f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'cross(f32, f32)'
1 candidate function:
'cross(vec3<T> ✗ , vec3<T> ✗ ) -> vec3<T>' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Cross_Error_Vec3Int) {
auto* call = Call("cross", Call<vec3<i32>>(1_i, 2_i, 3_i), Call<vec3<i32>>(1_i, 2_i, 3_i));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'cross(vec3<i32>, vec3<i32>)'
1 candidate function:
'cross(vec3<T> ✗ , vec3<T> ✗ ) -> vec3<T>' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Cross_Error_Vec4) {
auto* call =
Call("cross", Call<vec4<f32>>(1_f, 2_f, 3_f, 4_f), Call<vec4<f32>>(1_f, 2_f, 3_f, 4_f));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'cross(vec4<f32>, vec4<f32>)'
1 candidate function:
'cross(vec3<T> ✗ , vec3<T> ✗ ) -> vec3<T>' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Cross_Error_TooManyParams) {
auto* call = Call("cross", Call<vec3<f32>>(1_f, 2_f, 3_f), Call<vec3<f32>>(1_f, 2_f, 3_f),
Call<vec3<f32>>(1_f, 2_f, 3_f));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'cross(vec3<f32>, vec3<f32>, vec3<f32>)'
1 candidate function:
'cross(vec3<T> ✓ , vec3<T> ✓ ) -> vec3<T>' where:
overload expects 2 arguments, call passed 3 arguments
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
// distance: (T, T) -> T, (vecN<T>, vecN<T>) -> T
TEST_F(ResolverBuiltinFloatTest, Distance_Scalar_f32) {
auto* call = Call("distance", 1_f, 1_f);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Distance_Scalar_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("distance", 1_h, 1_h);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Distance_Vector_f32) {
auto* call = Call("distance", Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Distance_Vector_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("distance", Call<vec3<f16>>(1_h, 1_h, 3_h), Call<vec3<f16>>(1_h, 1_h, 3_h));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Distance_TooManyParams) {
auto* call = Call("distance", Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f),
Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'distance(vec3<f32>, vec3<f32>, vec3<f32>)'
2 candidate functions:
'distance(vecN<T> ✓ , vecN<T> ✓ ) -> T' where:
overload expects 2 arguments, call passed 3 arguments
'T' is 'abstract-float', 'f32' or 'f16'
'distance(T ✗ , T ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Distance_TooFewParams) {
auto* call = Call("distance", Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'distance(vec3<f32>)'
2 candidate functions:
'distance(vecN<T> ✓ , vecN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
'distance(T ✗ , T ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Distance_NoParams) {
auto* call = Call("distance");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'distance()'
2 candidate functions:
'distance(T ✗ , T ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
'distance(vecN<T> ✗ , vecN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
// frexp: (f32) -> __frexp_result, (vecN<f32>) -> __frexp_result_vecN, (f16) -> __frexp_result_16,
// (vecN<f16>) -> __frexp_result_vecN_f16
TEST_F(ResolverBuiltinFloatTest, FrexpScalar_f32) {
auto* call = Call("frexp", 1_f);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
EXPECT_TRUE(fract->Type()->Is<core::type::F32>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 4u);
EXPECT_EQ(fract->Align(), 4u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* exp = ty->Members()[1];
EXPECT_TRUE(exp->Type()->Is<core::type::I32>());
EXPECT_EQ(exp->Offset(), 4u);
EXPECT_EQ(exp->Size(), 4u);
EXPECT_EQ(exp->Align(), 4u);
EXPECT_EQ(exp->Name(), Sym("exp"));
EXPECT_EQ(ty->Size(), 8u);
EXPECT_EQ(ty->SizeNoPadding(), 8u);
}
TEST_F(ResolverBuiltinFloatTest, FrexpScalar_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("frexp", 1_h);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
EXPECT_TRUE(fract->Type()->Is<core::type::F16>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 2u);
EXPECT_EQ(fract->Align(), 2u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* exp = ty->Members()[1];
EXPECT_TRUE(exp->Type()->Is<core::type::I32>());
EXPECT_EQ(exp->Offset(), 4u);
EXPECT_EQ(exp->Size(), 4u);
EXPECT_EQ(exp->Align(), 4u);
EXPECT_EQ(exp->Name(), Sym("exp"));
EXPECT_EQ(ty->Size(), 8u);
EXPECT_EQ(ty->SizeNoPadding(), 8u);
}
TEST_F(ResolverBuiltinFloatTest, FrexpVector_f32) {
auto* call = Call("frexp", Call<vec3<f32>>());
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
ASSERT_TRUE(fract->Type()->Is<core::type::Vector>());
EXPECT_EQ(fract->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(fract->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 12u);
EXPECT_EQ(fract->Align(), 16u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* exp = ty->Members()[1];
ASSERT_TRUE(exp->Type()->Is<core::type::Vector>());
EXPECT_EQ(exp->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(exp->Type()->As<core::type::Vector>()->type()->Is<core::type::I32>());
EXPECT_EQ(exp->Offset(), 16u);
EXPECT_EQ(exp->Size(), 12u);
EXPECT_EQ(exp->Align(), 16u);
EXPECT_EQ(exp->Name(), Sym("exp"));
EXPECT_EQ(ty->Size(), 32u);
EXPECT_EQ(ty->SizeNoPadding(), 28u);
}
TEST_F(ResolverBuiltinFloatTest, FrexpVector_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("frexp", Call<vec3<f16>>());
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
ASSERT_TRUE(fract->Type()->Is<core::type::Vector>());
EXPECT_EQ(fract->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(fract->Type()->As<core::type::Vector>()->type()->Is<core::type::F16>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 6u);
EXPECT_EQ(fract->Align(), 8u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* exp = ty->Members()[1];
ASSERT_TRUE(exp->Type()->Is<core::type::Vector>());
EXPECT_EQ(exp->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(exp->Type()->As<core::type::Vector>()->type()->Is<core::type::I32>());
EXPECT_EQ(exp->Offset(), 16u);
EXPECT_EQ(exp->Size(), 12u);
EXPECT_EQ(exp->Align(), 16u);
EXPECT_EQ(exp->Name(), Sym("exp"));
EXPECT_EQ(ty->Size(), 32u);
EXPECT_EQ(ty->SizeNoPadding(), 28u);
}
TEST_F(ResolverBuiltinFloatTest, Frexp_Error_FirstParamInt) {
GlobalVar("v", ty.i32(), core::AddressSpace::kWorkgroup);
auto* call = Call("frexp", 1_i, AddressOf("v"));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'frexp(i32, ptr<workgroup, i32, read_write>)'
2 candidate functions:
'frexp(T ✗ ) -> __frexp_result_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
'frexp(vecN<T> ✗ ) -> __frexp_result_vecN_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
// length: (T) -> T, (vecN<T>) -> T
TEST_F(ResolverBuiltinFloatTest, Length_Scalar_f32) {
auto* call = Call("length", 1_f);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Length_Scalar_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("length", 1_h);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Length_FloatVector_f32) {
auto* call = Call("length", Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Length_FloatVector_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("length", Call<vec3<f16>>(1_h, 1_h, 3_h));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Length_NoParams) {
auto* call = Call("length");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'length()'
2 candidate functions:
'length(T ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
'length(vecN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Length_TooManyParams) {
auto* call = Call("length", 1_f, 2_f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'length(f32, f32)'
2 candidate functions:
'length(T ✓ ) -> T' where:
overload expects 1 argument, call passed 2 arguments
'T' is 'abstract-float', 'f32' or 'f16'
'length(vecN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
// mix(vecN<T>, vecN<T>, T) -> vecN<T>. Other overloads are tested in
// ResolverBuiltinTest_FloatBuiltin_IdenticalType above.
TEST_F(ResolverBuiltinFloatTest, Mix_VectorScalar_f32) {
auto* call = Call("mix", Call<vec3<f32>>(1_f, 1_f, 3_f), Call<vec3<f32>>(1_f, 1_f, 3_f), 4_f);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Mix_VectorScalar_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("mix", Call<vec3<f16>>(1_h, 1_h, 1_h), Call<vec3<f16>>(1_h, 1_h, 1_h), 4_h);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
}
// modf: (f32) -> __modf_result, (vecN<f32>) -> __modf_result_vecN, (f16) -> __modf_result_f16,
// (vecN<f16>) -> __modf_result_vecN_f16
TEST_F(ResolverBuiltinFloatTest, ModfScalar_f32) {
auto* call = Call("modf", 1_f);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
EXPECT_TRUE(fract->Type()->Is<core::type::F32>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 4u);
EXPECT_EQ(fract->Align(), 4u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* whole = ty->Members()[1];
EXPECT_TRUE(whole->Type()->Is<core::type::F32>());
EXPECT_EQ(whole->Offset(), 4u);
EXPECT_EQ(whole->Size(), 4u);
EXPECT_EQ(whole->Align(), 4u);
EXPECT_EQ(whole->Name(), Sym("whole"));
EXPECT_EQ(ty->Size(), 8u);
EXPECT_EQ(ty->SizeNoPadding(), 8u);
}
TEST_F(ResolverBuiltinFloatTest, ModfScalar_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("modf", 1_h);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
EXPECT_TRUE(fract->Type()->Is<core::type::F16>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 2u);
EXPECT_EQ(fract->Align(), 2u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* whole = ty->Members()[1];
EXPECT_TRUE(whole->Type()->Is<core::type::F16>());
EXPECT_EQ(whole->Offset(), 2u);
EXPECT_EQ(whole->Size(), 2u);
EXPECT_EQ(whole->Align(), 2u);
EXPECT_EQ(whole->Name(), Sym("whole"));
EXPECT_EQ(ty->Size(), 4u);
EXPECT_EQ(ty->SizeNoPadding(), 4u);
}
TEST_F(ResolverBuiltinFloatTest, ModfVector_f32) {
auto* call = Call("modf", Call<vec3<f32>>());
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
ASSERT_TRUE(fract->Type()->Is<core::type::Vector>());
EXPECT_EQ(fract->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(fract->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 12u);
EXPECT_EQ(fract->Align(), 16u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* whole = ty->Members()[1];
ASSERT_TRUE(whole->Type()->Is<core::type::Vector>());
EXPECT_EQ(whole->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(whole->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>());
EXPECT_EQ(whole->Offset(), 16u);
EXPECT_EQ(whole->Size(), 12u);
EXPECT_EQ(whole->Align(), 16u);
EXPECT_EQ(whole->Name(), Sym("whole"));
EXPECT_EQ(ty->Size(), 32u);
EXPECT_EQ(ty->SizeNoPadding(), 28u);
}
TEST_F(ResolverBuiltinFloatTest, ModfVector_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("modf", Call<vec3<f16>>());
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
auto* ty = TypeOf(call)->As<core::type::Struct>();
ASSERT_NE(ty, nullptr);
ASSERT_EQ(ty->Members().Length(), 2u);
auto* fract = ty->Members()[0];
ASSERT_TRUE(fract->Type()->Is<core::type::Vector>());
EXPECT_EQ(fract->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(fract->Type()->As<core::type::Vector>()->type()->Is<core::type::F16>());
EXPECT_EQ(fract->Offset(), 0u);
EXPECT_EQ(fract->Size(), 6u);
EXPECT_EQ(fract->Align(), 8u);
EXPECT_EQ(fract->Name(), Sym("fract"));
auto* whole = ty->Members()[1];
ASSERT_TRUE(whole->Type()->Is<core::type::Vector>());
EXPECT_EQ(whole->Type()->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(whole->Type()->As<core::type::Vector>()->type()->Is<core::type::F16>());
EXPECT_EQ(whole->Offset(), 8u);
EXPECT_EQ(whole->Size(), 6u);
EXPECT_EQ(whole->Align(), 8u);
EXPECT_EQ(whole->Name(), Sym("whole"));
EXPECT_EQ(ty->Size(), 16u);
EXPECT_EQ(ty->SizeNoPadding(), 14u);
}
TEST_F(ResolverBuiltinFloatTest, Modf_Error_FirstParamInt) {
GlobalVar("whole", ty.f32(), core::AddressSpace::kWorkgroup);
auto* call = Call("modf", 1_i, AddressOf("whole"));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'modf(i32, ptr<workgroup, f32, read_write>)'
2 candidate functions:
'modf(T ✗ ) -> __modf_result_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
'modf(vecN<T> ✗ ) -> __modf_result_vecN_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Modf_Error_SecondParamIntPtr) {
GlobalVar("whole", ty.i32(), core::AddressSpace::kWorkgroup);
auto* call = Call("modf", 1_f, AddressOf("whole"));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'modf(f32, ptr<workgroup, i32, read_write>)'
2 candidate functions:
'modf(T ✓ ) -> __modf_result_T' where:
overload expects 1 argument, call passed 2 arguments
'T' is 'abstract-float', 'f32' or 'f16'
'modf(vecN<T> ✗ ) -> __modf_result_vecN_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Modf_Error_SecondParamNotAPointer) {
auto* call = Call("modf", 1_f, 1_f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'modf(f32, f32)'
2 candidate functions:
'modf(T ✓ ) -> __modf_result_T' where:
overload expects 1 argument, call passed 2 arguments
'T' is 'abstract-float', 'f32' or 'f16'
'modf(vecN<T> ✗ ) -> __modf_result_vecN_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinFloatTest, Modf_Error_VectorSizesDontMatch) {
GlobalVar("whole", ty.vec4<f32>(), core::AddressSpace::kWorkgroup);
auto* call = Call("modf", Call<vec2<f32>>(1_f, 2_f), AddressOf("whole"));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'modf(vec2<f32>, ptr<workgroup, vec4<f32>, read_write>)'
2 candidate functions:
'modf(vecN<T> ✓ ) -> __modf_result_vecN_T' where:
overload expects 1 argument, call passed 2 arguments
'T' is 'abstract-float', 'f32' or 'f16'
'modf(T ✗ ) -> __modf_result_T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
// normalize: (vecN<T>) -> vecN<T>
TEST_F(ResolverBuiltinFloatTest, Normalize_Vector_f32) {
auto* call = Call("normalize", Call<vec3<f32>>(1_f, 1_f, 3_f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinFloatTest, Normalize_Vector_f16) {
Enable(wgsl::Extension::kF16);
auto* call = Call("normalize", Call<vec3<f16>>(1_h, 1_h, 3_h));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinFloatTest, Normalize_Error_NoParams) {
auto* call = Call("normalize");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'normalize()'
1 candidate function:
'normalize(vecN<T> ✗ ) -> vecN<T>' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
} // namespace float_builtin_tests
// Tests for Numeric builtins with all integer parameter
namespace integer_builtin_tests {
// Testcase parameters for integer built-in having signature of (T, ...) -> T and (vecN<T>, ...) ->
// vecN<T>, where T is i32 and u32
struct BuiltinDataWithParamNum {
uint32_t args_number;
const char* name;
wgsl::BuiltinFn builtin;
};
inline std::ostream& operator<<(std::ostream& out, BuiltinDataWithParamNum data) {
out << data.name;
return out;
}
// Tests for integer built-ins that has signiture (T, ...) -> T and (vecN<T>, ...) -> vecN<T>
using ResolverBuiltinTest_IntegerBuiltin_IdenticalType =
ResolverTestWithParam<BuiltinDataWithParamNum>;
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, Error_NoParams) {
auto param = GetParam();
auto* call = Call(param.name);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "()'"));
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, OneParams_Scalar_i32) {
auto param = GetParam();
auto* call = Call(param.name, 1_i);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "(i32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, OneParams_Vector_i32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<i32>>(1_i, 1_i, 3_i));
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_signed_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<i32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, OneParams_Scalar_u32) {
auto param = GetParam();
auto* call = Call(param.name, 1_u);
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) + "(u32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, OneParams_Vector_u32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<u32>>(1_u, 1_u, 3_u));
WrapInFunction(call);
if (param.args_number == 1u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_unsigned_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<u32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, TwoParams_Scalar_i32) {
auto param = GetParam();
auto* call = Call(param.name, 1_i, 1_i);
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(i32, i32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, TwoParams_Vector_i32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<i32>>(1_i, 1_i, 3_i), Call<vec3<i32>>(1_i, 1_i, 3_i));
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_signed_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<i32>, vec3<i32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, TwoParams_Scalar_u32) {
auto param = GetParam();
auto* call = Call(param.name, 1_u, 1_u);
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(u32, u32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, TwoParams_Vector_u32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<u32>>(1_u, 1_u, 3_u), Call<vec3<u32>>(1_u, 1_u, 3_u));
WrapInFunction(call);
if (param.args_number == 2u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_unsigned_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(vec3<u32>, vec3<u32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, ThreeParams_Scalar_i32) {
auto param = GetParam();
auto* call = Call(param.name, 1_i, 1_i, 1_i);
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(i32, i32, i32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, ThreeParams_Vector_i32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<i32>>(1_i, 1_i, 3_i), Call<vec3<i32>>(1_i, 1_i, 3_i),
Call<vec3<i32>>(1_i, 1_i, 3_i));
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_signed_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<i32>, vec3<i32>, vec3<i32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, ThreeParams_Scalar_u32) {
auto param = GetParam();
auto* call = Call(param.name, 1_u, 1_u, 1_u);
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(u32, u32, u32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, ThreeParams_Vector_u32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<u32>>(1_u, 1_u, 3_u), Call<vec3<u32>>(1_u, 1_u, 3_u),
Call<vec3<u32>>(1_u, 1_u, 3_u));
WrapInFunction(call);
if (param.args_number == 3u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_unsigned_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<u32>, vec3<u32>, vec3<u32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, FourParams_Scalar_i32) {
auto param = GetParam();
auto* call = Call(param.name, 1_i, 1_i, 1_i, 1_i);
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(i32, i32, i32, i32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, FourParams_Vector_i32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<i32>>(1_i, 1_i, 3_i), Call<vec3<i32>>(1_i, 1_i, 3_i),
Call<vec3<i32>>(1_i, 1_i, 3_i), Call<vec3<i32>>(1_i, 1_i, 3_i));
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_signed_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::I32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<i32>, vec3<i32>, vec3<i32>, vec3<i32>)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, FourParams_Scalar_u32) {
auto param = GetParam();
auto* call = Call(param.name, 1_u, 1_u, 1_u, 1_u);
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" +
std::string(param.name) + "(u32, u32, u32, u32)'"));
}
}
TEST_P(ResolverBuiltinTest_IntegerBuiltin_IdenticalType, FourParams_Vector_u32) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec3<u32>>(1_u, 1_u, 3_u), Call<vec3<u32>>(1_u, 1_u, 3_u),
Call<vec3<u32>>(1_u, 1_u, 3_u), Call<vec3<u32>>(1_u, 1_u, 3_u));
WrapInFunction(call);
if (param.args_number == 4u) {
// Parameter count matched.
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_unsigned_integer_vector());
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 3u);
ASSERT_NE(TypeOf(call)->As<core::type::Vector>()->type(), nullptr);
EXPECT_TRUE(TypeOf(call)->As<core::type::Vector>()->type()->Is<core::type::U32>());
} else {
// Invalid parameter count.
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(),
HasSubstr("error: no matching call to '" + std::string(param.name) +
"(vec3<u32>, vec3<u32>, vec3<u32>, vec3<u32>)'"));
}
}
INSTANTIATE_TEST_SUITE_P(
ResolverTest,
ResolverBuiltinTest_IntegerBuiltin_IdenticalType,
testing::Values(
BuiltinDataWithParamNum{1, "abs", wgsl::BuiltinFn::kAbs},
BuiltinDataWithParamNum{3, "clamp", wgsl::BuiltinFn::kClamp},
BuiltinDataWithParamNum{1, "countLeadingZeros", wgsl::BuiltinFn::kCountLeadingZeros},
BuiltinDataWithParamNum{1, "countOneBits", wgsl::BuiltinFn::kCountOneBits},
BuiltinDataWithParamNum{1, "countTrailingZeros", wgsl::BuiltinFn::kCountTrailingZeros},
// extractBits: (T, u32, u32) -> T
BuiltinDataWithParamNum{1, "firstLeadingBit", wgsl::BuiltinFn::kFirstLeadingBit},
BuiltinDataWithParamNum{1, "firstTrailingBit", wgsl::BuiltinFn::kFirstTrailingBit},
// insertBits: (T, T, u32, u32) -> T
BuiltinDataWithParamNum{2, "max", wgsl::BuiltinFn::kMax},
BuiltinDataWithParamNum{2, "min", wgsl::BuiltinFn::kMin},
BuiltinDataWithParamNum{1, "reverseBits", wgsl::BuiltinFn::kReverseBits}));
} // namespace integer_builtin_tests
// Tests for Numeric builtins with matrix parameter, i.e. "determinant" and "transpose"
namespace matrix_builtin_tests {
TEST_F(ResolverBuiltinTest, Determinant_2x2_f32) {
GlobalVar("var", ty.mat2x2<f32>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinTest, Determinant_2x2_f16) {
Enable(wgsl::Extension::kF16);
GlobalVar("var", ty.mat2x2<f16>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinTest, Determinant_3x3_f32) {
GlobalVar("var", ty.mat3x3<f32>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinTest, Determinant_3x3_f16) {
Enable(wgsl::Extension::kF16);
GlobalVar("var", ty.mat3x3<f16>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinTest, Determinant_4x4_f32) {
GlobalVar("var", ty.mat4x4<f32>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinTest, Determinant_4x4_f16) {
Enable(wgsl::Extension::kF16);
GlobalVar("var", ty.mat4x4<f16>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinTest, Determinant_NotSquare) {
GlobalVar("var", ty.mat2x3<f32>(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'determinant(mat2x3<f32>)'
1 candidate function:
'determinant(matNxN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
TEST_F(ResolverBuiltinTest, Determinant_NotMatrix) {
GlobalVar("var", ty.f32(), core::AddressSpace::kPrivate);
auto* call = Call("determinant", "var");
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(error: no matching call to 'determinant(f32)'
1 candidate function:
'determinant(matNxN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'f32' or 'f16'
)");
}
} // namespace matrix_builtin_tests
// Tests for Numeric builtins with float and integer vector parameter, i.e. "dot"
namespace vector_builtin_tests {
TEST_F(ResolverBuiltinTest, Dot_Vec2_f32) {
GlobalVar("my_var", ty.vec2<f32>(), core::AddressSpace::kPrivate);
auto* expr = Call("dot", "my_var", "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::F32>());
}
TEST_F(ResolverBuiltinTest, Dot_Vec2_f16) {
Enable(wgsl::Extension::kF16);
GlobalVar("my_var", ty.vec2<f16>(), core::AddressSpace::kPrivate);
auto* expr = Call("dot", "my_var", "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::F16>());
}
TEST_F(ResolverBuiltinTest, Dot_Vec3_i32) {
GlobalVar("my_var", ty.vec3<i32>(), core::AddressSpace::kPrivate);
auto* expr = Call("dot", "my_var", "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::I32>());
}
TEST_F(ResolverBuiltinTest, Dot_Vec4_u32) {
GlobalVar("my_var", ty.vec4<u32>(), core::AddressSpace::kPrivate);
auto* expr = Call("dot", "my_var", "my_var");
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
EXPECT_TRUE(TypeOf(expr)->Is<core::type::U32>());
}
TEST_F(ResolverBuiltinTest, Dot_Error_Scalar) {
auto* expr = Call("dot", 1_f, 1_f);
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(error: no matching call to 'dot(f32, f32)'
1 candidate function:
'dot(vecN<T> ✗ , vecN<T> ✗ ) -> T' where:
'T' is 'abstract-float', 'abstract-int', 'f32', 'i32', 'u32' or 'f16'
)");
}
} // namespace vector_builtin_tests
// Tests for Derivative builtins
namespace derivative_builtin_tests {
using ResolverBuiltinDerivativeTest = ResolverTestWithParam<std::string>;
TEST_P(ResolverBuiltinDerivativeTest, Scalar) {
auto name = GetParam();
GlobalVar("ident", ty.f32(), core::AddressSpace::kPrivate);
auto* expr = Call(name, "ident");
Func("func", tint::Empty, ty.void_(), Vector{Ignore(expr)},
Vector{create<ast::StageAttribute>(ast::PipelineStage::kFragment)});
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
ASSERT_TRUE(TypeOf(expr)->Is<core::type::F32>());
}
TEST_P(ResolverBuiltinDerivativeTest, Vector) {
auto name = GetParam();
GlobalVar("ident", ty.vec4<f32>(), core::AddressSpace::kPrivate);
auto* expr = Call(name, "ident");
Func("func", tint::Empty, ty.void_(), Vector{Ignore(expr)},
Vector{create<ast::StageAttribute>(ast::PipelineStage::kFragment)});
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
ASSERT_TRUE(TypeOf(expr)->Is<core::type::Vector>());
EXPECT_TRUE(TypeOf(expr)->As<core::type::Vector>()->type()->Is<core::type::F32>());
EXPECT_EQ(TypeOf(expr)->As<core::type::Vector>()->Width(), 4u);
}
TEST_P(ResolverBuiltinDerivativeTest, MissingParam) {
auto name = GetParam();
auto* expr = Call(name);
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "error: no matching call to '" + name +
"()'\n\n"
"2 candidate functions:\n"
" • '" +
name +
"(f32 ✗ ) -> f32'\n"
" • '" +
name + "(vecN<f32> ✗ ) -> vecN<f32>'\n");
}
INSTANTIATE_TEST_SUITE_P(ResolverTest,
ResolverBuiltinDerivativeTest,
testing::Values("dpdx",
"dpdxCoarse",
"dpdxFine",
"dpdy",
"dpdyCoarse",
"dpdyFine",
"fwidth",
"fwidthCoarse",
"fwidthFine"));
} // namespace derivative_builtin_tests
// Tests for Texture builtins
namespace texture_builtin_tests {
enum class Texture { kF32, kI32, kU32 };
template <typename STREAM, typename = traits::EnableIfIsOStream<STREAM>>
auto& operator<<(STREAM& out, Texture data) {
if (data == Texture::kF32) {
out << "f32";
} else if (data == Texture::kI32) {
out << "i32";
} else {
out << "u32";
}
return out;
}
struct TextureTestParams {
core::type::TextureDimension dim;
Texture type = Texture::kF32;
core::TexelFormat format = core::TexelFormat::kR32Float;
};
inline std::ostream& operator<<(std::ostream& out, TextureTestParams data) {
StringStream str;
str << data.dim << "_" << data.type;
out << str.str();
return out;
}
class ResolverBuiltinTest_TextureOperation : public ResolverTestWithParam<TextureTestParams> {
public:
/// Gets an appropriate type for the coords parameter depending the the
/// dimensionality of the texture being sampled.
/// @param dim dimensionality of the texture being sampled
/// @param scalar the scalar type
/// @returns a pointer to a type appropriate for the coord param
ast::Type GetCoordsType(core::type::TextureDimension dim, ast::Type scalar) {
switch (dim) {
case core::type::TextureDimension::k1d:
return ty(scalar);
case core::type::TextureDimension::k2d:
case core::type::TextureDimension::k2dArray:
return ty.vec2(scalar);
case core::type::TextureDimension::k3d:
case core::type::TextureDimension::kCube:
case core::type::TextureDimension::kCubeArray:
return ty.vec3(scalar);
default:
[=] {
StringStream str;
str << dim;
FAIL() << "Unsupported texture dimension: " << str.str();
}();
}
return ast::Type{};
}
void add_call_param(std::string name, ast::Type type, ExpressionList* call_params) {
std::string type_name = type->identifier->symbol.Name();
if (tint::HasPrefix(type_name, "texture") || tint::HasPrefix(type_name, "sampler")) {
GlobalVar(name, type, Binding(0_a), Group(0_a));
} else {
GlobalVar(name, type, core::AddressSpace::kPrivate);
}
call_params->Push(Expr(name));
}
ast::Type subtype(Texture type) {
if (type == Texture::kF32) {
return ty.f32();
}
if (type == Texture::kI32) {
return ty.i32();
}
return ty.u32();
}
};
using ResolverBuiltinTest_SampledTextureOperation = ResolverBuiltinTest_TextureOperation;
TEST_P(ResolverBuiltinTest_SampledTextureOperation, TextureLoadSampled) {
auto dim = GetParam().dim;
auto type = GetParam().type;
ast::Type s = subtype(type);
ast::Type coords_type = GetCoordsType(dim, ty.i32());
auto texture_type = ty.sampled_texture(dim, s);
ExpressionList call_params;
add_call_param("texture", texture_type, &call_params);
add_call_param("coords", coords_type, &call_params);
if (dim == core::type::TextureDimension::k2dArray) {
add_call_param("array_index", ty.i32(), &call_params);
}
add_call_param("level", ty.i32(), &call_params);
auto* expr = Call("textureLoad", call_params);
WrapInFunction(expr);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(expr), nullptr);
ASSERT_TRUE(TypeOf(expr)->Is<core::type::Vector>());
if (type == Texture::kF32) {
EXPECT_TRUE(TypeOf(expr)->As<core::type::Vector>()->type()->Is<core::type::F32>());
} else if (type == Texture::kI32) {
EXPECT_TRUE(TypeOf(expr)->As<core::type::Vector>()->type()->Is<core::type::I32>());
} else {
EXPECT_TRUE(TypeOf(expr)->As<core::type::Vector>()->type()->Is<core::type::U32>());
}
EXPECT_EQ(TypeOf(expr)->As<core::type::Vector>()->Width(), 4u);
}
INSTANTIATE_TEST_SUITE_P(ResolverTest,
ResolverBuiltinTest_SampledTextureOperation,
testing::Values(TextureTestParams{core::type::TextureDimension::k1d},
TextureTestParams{core::type::TextureDimension::k2d},
TextureTestParams{core::type::TextureDimension::k2dArray},
TextureTestParams{core::type::TextureDimension::k3d}));
using ResolverBuiltinTest_Texture = ResolverTestWithParam<ast::test::TextureOverloadCase>;
INSTANTIATE_TEST_SUITE_P(ResolverTest,
ResolverBuiltinTest_Texture,
testing::ValuesIn(ast::test::TextureOverloadCase::ValidCases()));
static std::string to_str(const std::string& func, VectorRef<const sem::Parameter*> params) {
StringStream out;
out << func << "(";
bool first = true;
for (auto* param : params) {
if (!first) {
out << ", ";
}
out << param->Usage();
first = false;
}
out << ")";
return out.str();
}
static const char* expected_texture_overload(ast::test::ValidTextureOverload overload) {
using ValidTextureOverload = ast::test::ValidTextureOverload;
switch (overload) {
case ValidTextureOverload::kDimensions1d:
case ValidTextureOverload::kDimensions2d:
case ValidTextureOverload::kDimensions2dArray:
case ValidTextureOverload::kDimensions3d:
case ValidTextureOverload::kDimensionsCube:
case ValidTextureOverload::kDimensionsCubeArray:
case ValidTextureOverload::kDimensionsMultisampled2d:
case ValidTextureOverload::kDimensionsDepth2d:
case ValidTextureOverload::kDimensionsDepth2dArray:
case ValidTextureOverload::kDimensionsDepthCube:
case ValidTextureOverload::kDimensionsDepthCubeArray:
case ValidTextureOverload::kDimensionsDepthMultisampled2d:
case ValidTextureOverload::kDimensionsStorageWO1d:
case ValidTextureOverload::kDimensionsStorageWO2d:
case ValidTextureOverload::kDimensionsStorageWO2dArray:
case ValidTextureOverload::kDimensionsStorageWO3d:
return R"(textureDimensions(texture))";
case ValidTextureOverload::kGather2dF32:
return R"(textureGather(component, texture, sampler, coords))";
case ValidTextureOverload::kGather2dOffsetF32:
return R"(textureGather(component, texture, sampler, coords, offset))";
case ValidTextureOverload::kGather2dArrayF32:
return R"(textureGather(component, texture, sampler, coords, array_index))";
case ValidTextureOverload::kGather2dArrayOffsetF32:
return R"(textureGather(component, texture, sampler, coords, array_index, offset))";
case ValidTextureOverload::kGatherCubeF32:
return R"(textureGather(component, texture, sampler, coords))";
case ValidTextureOverload::kGatherCubeArrayF32:
return R"(textureGather(component, texture, sampler, coords, array_index))";
case ValidTextureOverload::kGatherDepth2dF32:
return R"(textureGather(texture, sampler, coords))";
case ValidTextureOverload::kGatherDepth2dOffsetF32:
return R"(textureGather(texture, sampler, coords, offset))";
case ValidTextureOverload::kGatherDepth2dArrayF32:
return R"(textureGather(texture, sampler, coords, array_index))";
case ValidTextureOverload::kGatherDepth2dArrayOffsetF32:
return R"(textureGather(texture, sampler, coords, array_index, offset))";
case ValidTextureOverload::kGatherDepthCubeF32:
return R"(textureGather(texture, sampler, coords))";
case ValidTextureOverload::kGatherDepthCubeArrayF32:
return R"(textureGather(texture, sampler, coords, array_index))";
case ValidTextureOverload::kGatherCompareDepth2dF32:
return R"(textureGatherCompare(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kGatherCompareDepth2dOffsetF32:
return R"(textureGatherCompare(texture, sampler, coords, depth_ref, offset))";
case ValidTextureOverload::kGatherCompareDepth2dArrayF32:
return R"(textureGatherCompare(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kGatherCompareDepth2dArrayOffsetF32:
return R"(textureGatherCompare(texture, sampler, coords, array_index, depth_ref, offset))";
case ValidTextureOverload::kGatherCompareDepthCubeF32:
return R"(textureGatherCompare(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kGatherCompareDepthCubeArrayF32:
return R"(textureGatherCompare(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kNumLayers2dArray:
case ValidTextureOverload::kNumLayersCubeArray:
case ValidTextureOverload::kNumLayersDepth2dArray:
case ValidTextureOverload::kNumLayersDepthCubeArray:
case ValidTextureOverload::kNumLayersStorageWO2dArray:
return R"(textureNumLayers(texture))";
case ValidTextureOverload::kNumLevels2d:
case ValidTextureOverload::kNumLevels2dArray:
case ValidTextureOverload::kNumLevels3d:
case ValidTextureOverload::kNumLevelsCube:
case ValidTextureOverload::kNumLevelsCubeArray:
case ValidTextureOverload::kNumLevelsDepth2d:
case ValidTextureOverload::kNumLevelsDepth2dArray:
case ValidTextureOverload::kNumLevelsDepthCube:
case ValidTextureOverload::kNumLevelsDepthCubeArray:
return R"(textureNumLevels(texture))";
case ValidTextureOverload::kNumSamplesDepthMultisampled2d:
case ValidTextureOverload::kNumSamplesMultisampled2d:
return R"(textureNumSamples(texture))";
case ValidTextureOverload::kDimensions2dLevel:
case ValidTextureOverload::kDimensions2dArrayLevel:
case ValidTextureOverload::kDimensions3dLevel:
case ValidTextureOverload::kDimensionsCubeLevel:
case ValidTextureOverload::kDimensionsCubeArrayLevel:
case ValidTextureOverload::kDimensionsDepth2dLevel:
case ValidTextureOverload::kDimensionsDepth2dArrayLevel:
case ValidTextureOverload::kDimensionsDepthCubeLevel:
case ValidTextureOverload::kDimensionsDepthCubeArrayLevel:
return R"(textureDimensions(texture, level))";
case ValidTextureOverload::kSample1dF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSample2dF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSample2dOffsetF32:
return R"(textureSample(texture, sampler, coords, offset))";
case ValidTextureOverload::kSample2dArrayF32:
return R"(textureSample(texture, sampler, coords, array_index))";
case ValidTextureOverload::kSample2dArrayOffsetF32:
return R"(textureSample(texture, sampler, coords, array_index, offset))";
case ValidTextureOverload::kSample3dF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSample3dOffsetF32:
return R"(textureSample(texture, sampler, coords, offset))";
case ValidTextureOverload::kSampleCubeF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSampleCubeArrayF32:
return R"(textureSample(texture, sampler, coords, array_index))";
case ValidTextureOverload::kSampleDepth2dF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSampleDepth2dOffsetF32:
return R"(textureSample(texture, sampler, coords, offset))";
case ValidTextureOverload::kSampleDepth2dArrayF32:
return R"(textureSample(texture, sampler, coords, array_index))";
case ValidTextureOverload::kSampleDepth2dArrayOffsetF32:
return R"(textureSample(texture, sampler, coords, array_index, offset))";
case ValidTextureOverload::kSampleDepthCubeF32:
return R"(textureSample(texture, sampler, coords))";
case ValidTextureOverload::kSampleDepthCubeArrayF32:
return R"(textureSample(texture, sampler, coords, array_index))";
case ValidTextureOverload::kSampleBias2dF32:
return R"(textureSampleBias(texture, sampler, coords, bias))";
case ValidTextureOverload::kSampleBias2dOffsetF32:
return R"(textureSampleBias(texture, sampler, coords, bias, offset))";
case ValidTextureOverload::kSampleBias2dArrayF32:
return R"(textureSampleBias(texture, sampler, coords, array_index, bias))";
case ValidTextureOverload::kSampleBias2dArrayOffsetF32:
return R"(textureSampleBias(texture, sampler, coords, array_index, bias, offset))";
case ValidTextureOverload::kSampleBias3dF32:
return R"(textureSampleBias(texture, sampler, coords, bias))";
case ValidTextureOverload::kSampleBias3dOffsetF32:
return R"(textureSampleBias(texture, sampler, coords, bias, offset))";
case ValidTextureOverload::kSampleBiasCubeF32:
return R"(textureSampleBias(texture, sampler, coords, bias))";
case ValidTextureOverload::kSampleBiasCubeArrayF32:
return R"(textureSampleBias(texture, sampler, coords, array_index, bias))";
case ValidTextureOverload::kSampleLevel2dF32:
return R"(textureSampleLevel(texture, sampler, coords, level))";
case ValidTextureOverload::kSampleLevel2dOffsetF32:
return R"(textureSampleLevel(texture, sampler, coords, level, offset))";
case ValidTextureOverload::kSampleLevel2dArrayF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level))";
case ValidTextureOverload::kSampleLevel2dArrayOffsetF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level, offset))";
case ValidTextureOverload::kSampleLevel3dF32:
return R"(textureSampleLevel(texture, sampler, coords, level))";
case ValidTextureOverload::kSampleLevel3dOffsetF32:
return R"(textureSampleLevel(texture, sampler, coords, level, offset))";
case ValidTextureOverload::kSampleLevelCubeF32:
return R"(textureSampleLevel(texture, sampler, coords, level))";
case ValidTextureOverload::kSampleLevelCubeArrayF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level))";
case ValidTextureOverload::kSampleLevelDepth2dF32:
return R"(textureSampleLevel(texture, sampler, coords, level))";
case ValidTextureOverload::kSampleLevelDepth2dOffsetF32:
return R"(textureSampleLevel(texture, sampler, coords, level, offset))";
case ValidTextureOverload::kSampleLevelDepth2dArrayF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level))";
case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level, offset))";
case ValidTextureOverload::kSampleLevelDepthCubeF32:
return R"(textureSampleLevel(texture, sampler, coords, level))";
case ValidTextureOverload::kSampleLevelDepthCubeArrayF32:
return R"(textureSampleLevel(texture, sampler, coords, array_index, level))";
case ValidTextureOverload::kSampleGrad2dF32:
return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))";
case ValidTextureOverload::kSampleGrad2dOffsetF32:
return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy, offset))";
case ValidTextureOverload::kSampleGrad2dArrayF32:
return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy))";
case ValidTextureOverload::kSampleGrad2dArrayOffsetF32:
return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy, offset))";
case ValidTextureOverload::kSampleGrad3dF32:
return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))";
case ValidTextureOverload::kSampleGrad3dOffsetF32:
return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy, offset))";
case ValidTextureOverload::kSampleGradCubeF32:
return R"(textureSampleGrad(texture, sampler, coords, ddx, ddy))";
case ValidTextureOverload::kSampleGradCubeArrayF32:
return R"(textureSampleGrad(texture, sampler, coords, array_index, ddx, ddy))";
case ValidTextureOverload::kSampleCompareDepth2dF32:
return R"(textureSampleCompare(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kSampleCompareDepth2dOffsetF32:
return R"(textureSampleCompare(texture, sampler, coords, depth_ref, offset))";
case ValidTextureOverload::kSampleCompareDepth2dArrayF32:
return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kSampleCompareDepth2dArrayOffsetF32:
return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref, offset))";
case ValidTextureOverload::kSampleCompareDepthCubeF32:
return R"(textureSampleCompare(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kSampleCompareDepthCubeArrayF32:
return R"(textureSampleCompare(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kSampleCompareLevelDepth2dF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kSampleCompareLevelDepth2dOffsetF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, depth_ref, offset))";
case ValidTextureOverload::kSampleCompareLevelDepth2dArrayF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kSampleCompareLevelDepth2dArrayOffsetF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, array_index, depth_ref, offset))";
case ValidTextureOverload::kSampleCompareLevelDepthCubeF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, depth_ref))";
case ValidTextureOverload::kSampleCompareLevelDepthCubeArrayF32:
return R"(textureSampleCompareLevel(texture, sampler, coords, array_index, depth_ref))";
case ValidTextureOverload::kLoad1dLevelF32:
case ValidTextureOverload::kLoad1dLevelU32:
case ValidTextureOverload::kLoad1dLevelI32:
case ValidTextureOverload::kLoad2dLevelF32:
case ValidTextureOverload::kLoad2dLevelU32:
case ValidTextureOverload::kLoad2dLevelI32:
return R"(textureLoad(texture, coords, level))";
case ValidTextureOverload::kLoad2dArrayLevelF32:
case ValidTextureOverload::kLoad2dArrayLevelU32:
case ValidTextureOverload::kLoad2dArrayLevelI32:
return R"(textureLoad(texture, coords, array_index, level))";
case ValidTextureOverload::kLoad3dLevelF32:
case ValidTextureOverload::kLoad3dLevelU32:
case ValidTextureOverload::kLoad3dLevelI32:
case ValidTextureOverload::kLoadDepth2dLevelF32:
return R"(textureLoad(texture, coords, level))";
case ValidTextureOverload::kLoadDepthMultisampled2dF32:
case ValidTextureOverload::kLoadMultisampled2dF32:
case ValidTextureOverload::kLoadMultisampled2dU32:
case ValidTextureOverload::kLoadMultisampled2dI32:
return R"(textureLoad(texture, coords, sample_index))";
case ValidTextureOverload::kLoadDepth2dArrayLevelF32:
return R"(textureLoad(texture, coords, array_index, level))";
case ValidTextureOverload::kStoreWO1dRgba32float:
case ValidTextureOverload::kStoreWO2dRgba32float:
case ValidTextureOverload::kStoreWO3dRgba32float:
return R"(textureStore(texture, coords, value))";
case ValidTextureOverload::kStoreWO2dArrayRgba32float:
return R"(textureStore(texture, coords, array_index, value))";
}
return "<unmatched texture overload>";
}
TEST_P(ResolverBuiltinTest_Texture, Call) {
auto param = GetParam();
param.BuildTextureVariable(this);
param.BuildSamplerVariable(this);
auto* call = Call(param.function, param.args(this));
auto* stmt = param.returns_value ? static_cast<const ast::Statement*>(Assign(Phony(), call))
: static_cast<const ast::Statement*>(CallStmt(call));
Func("func", tint::Empty, ty.void_(), Vector{stmt},
Vector{Stage(ast::PipelineStage::kFragment)});
ASSERT_TRUE(r()->Resolve()) << r()->error();
if (std::string(param.function) == "textureDimensions") {
switch (param.texture_dimension) {
default: {
StringStream str;
str << param.texture_dimension;
FAIL() << "invalid texture dimensions: " << str.str();
}
case core::type::TextureDimension::k1d:
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
break;
case core::type::TextureDimension::k2d:
case core::type::TextureDimension::k2dArray:
case core::type::TextureDimension::kCube:
case core::type::TextureDimension::kCubeArray: {
auto* vec = As<core::type::Vector>(TypeOf(call));
ASSERT_NE(vec, nullptr);
EXPECT_EQ(vec->Width(), 2u);
EXPECT_TRUE(vec->type()->Is<core::type::U32>());
break;
}
case core::type::TextureDimension::k3d: {
auto* vec = As<core::type::Vector>(TypeOf(call));
ASSERT_NE(vec, nullptr);
EXPECT_EQ(vec->Width(), 3u);
EXPECT_TRUE(vec->type()->Is<core::type::U32>());
break;
}
}
} else if (std::string(param.function) == "textureNumLayers") {
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else if (std::string(param.function) == "textureNumLevels") {
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else if (std::string(param.function) == "textureNumSamples") {
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
} else if (std::string(param.function) == "textureStore") {
EXPECT_TRUE(TypeOf(call)->Is<core::type::Void>());
} else if (std::string(param.function) == "textureGather") {
auto* vec = As<core::type::Vector>(TypeOf(call));
ASSERT_NE(vec, nullptr);
EXPECT_EQ(vec->Width(), 4u);
switch (param.texture_data_type) {
case ast::test::TextureDataType::kF32:
EXPECT_TRUE(vec->type()->Is<core::type::F32>());
break;
case ast::test::TextureDataType::kU32:
EXPECT_TRUE(vec->type()->Is<core::type::U32>());
break;
case ast::test::TextureDataType::kI32:
EXPECT_TRUE(vec->type()->Is<core::type::I32>());
break;
}
} else if (std::string(param.function) == "textureGatherCompare") {
auto* vec = As<core::type::Vector>(TypeOf(call));
ASSERT_NE(vec, nullptr);
EXPECT_EQ(vec->Width(), 4u);
EXPECT_TRUE(vec->type()->Is<core::type::F32>());
} else {
switch (param.texture_kind) {
case ast::test::TextureKind::kRegular:
case ast::test::TextureKind::kMultisampled:
case ast::test::TextureKind::kStorage: {
auto* vec = TypeOf(call)->As<core::type::Vector>();
ASSERT_NE(vec, nullptr);
switch (param.texture_data_type) {
case ast::test::TextureDataType::kF32:
EXPECT_TRUE(vec->type()->Is<core::type::F32>());
break;
case ast::test::TextureDataType::kU32:
EXPECT_TRUE(vec->type()->Is<core::type::U32>());
break;
case ast::test::TextureDataType::kI32:
EXPECT_TRUE(vec->type()->Is<core::type::I32>());
break;
}
break;
}
case ast::test::TextureKind::kDepth:
case ast::test::TextureKind::kDepthMultisampled: {
EXPECT_TRUE(TypeOf(call)->Is<core::type::F32>());
break;
}
}
}
auto* call_sem = Sem().Get<sem::Call>(call);
ASSERT_NE(call_sem, nullptr);
auto* target = call_sem->Target();
ASSERT_NE(target, nullptr);
auto got = texture_builtin_tests::to_str(param.function, target->Parameters());
auto* expected = expected_texture_overload(param.overload);
EXPECT_EQ(got, expected);
}
} // namespace texture_builtin_tests
// Tests for Data Packing builtins
namespace data_packing_builtin_tests {
using ResolverBuiltinTest_DataPacking = ResolverTestWithParam<BuiltinData>;
TEST_P(ResolverBuiltinTest_DataPacking, InferType) {
auto param = GetParam();
bool pack4 = param.builtin == wgsl::BuiltinFn::kPack4X8Snorm ||
param.builtin == wgsl::BuiltinFn::kPack4X8Unorm;
auto* call = pack4 ? Call(param.name, Call<vec4<f32>>(1_f, 2_f, 3_f, 4_f))
: Call(param.name, Call<vec2<f32>>(1_f, 2_f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::U32>());
}
TEST_P(ResolverBuiltinTest_DataPacking, Error_IncorrectParamType) {
auto param = GetParam();
bool pack4 = param.builtin == wgsl::BuiltinFn::kPack4X8Snorm ||
param.builtin == wgsl::BuiltinFn::kPack4X8Unorm;
auto* call = pack4 ? Call(param.name, Call<vec4<i32>>(1_i, 2_i, 3_i, 4_i))
: Call(param.name, Call<vec2<i32>>(1_i, 2_i));
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" + std::string(param.name)));
}
TEST_P(ResolverBuiltinTest_DataPacking, Error_NoParams) {
auto param = GetParam();
auto* call = Call(param.name);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" + std::string(param.name)));
}
TEST_P(ResolverBuiltinTest_DataPacking, Error_TooManyParams) {
auto param = GetParam();
bool pack4 = param.builtin == wgsl::BuiltinFn::kPack4X8Snorm ||
param.builtin == wgsl::BuiltinFn::kPack4X8Unorm;
auto* call = pack4 ? Call(param.name, Call<vec4<f32>>(1_f, 2_f, 3_f, 4_f), 1_f)
: Call(param.name, Call<vec2<f32>>(1_f, 2_f), 1_f);
WrapInFunction(call);
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" + std::string(param.name)));
}
INSTANTIATE_TEST_SUITE_P(
ResolverTest,
ResolverBuiltinTest_DataPacking,
testing::Values(BuiltinData{"pack4x8snorm", wgsl::BuiltinFn::kPack4X8Snorm},
BuiltinData{"pack4x8unorm", wgsl::BuiltinFn::kPack4X8Unorm},
BuiltinData{"pack2x16snorm", wgsl::BuiltinFn::kPack2X16Snorm},
BuiltinData{"pack2x16unorm", wgsl::BuiltinFn::kPack2X16Unorm},
BuiltinData{"pack2x16float", wgsl::BuiltinFn::kPack2X16Float}));
} // namespace data_packing_builtin_tests
// Tests for Data Unpacking builtins
namespace data_unpacking_builtin_tests {
using ResolverBuiltinTest_DataUnpacking = ResolverTestWithParam<BuiltinData>;
TEST_P(ResolverBuiltinTest_DataUnpacking, InferType) {
auto param = GetParam();
bool pack4 = param.builtin == wgsl::BuiltinFn::kUnpack4X8Snorm ||
param.builtin == wgsl::BuiltinFn::kUnpack4X8Unorm;
auto* call = Call(param.name, 1_u);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->is_float_vector());
if (pack4) {
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 4u);
} else {
EXPECT_EQ(TypeOf(call)->As<core::type::Vector>()->Width(), 2u);
}
}
INSTANTIATE_TEST_SUITE_P(
ResolverTest,
ResolverBuiltinTest_DataUnpacking,
testing::Values(BuiltinData{"unpack4x8snorm", wgsl::BuiltinFn::kUnpack4X8Snorm},
BuiltinData{"unpack4x8unorm", wgsl::BuiltinFn::kUnpack4X8Unorm},
BuiltinData{"unpack2x16snorm", wgsl::BuiltinFn::kUnpack2X16Snorm},
BuiltinData{"unpack2x16unorm", wgsl::BuiltinFn::kUnpack2X16Unorm},
BuiltinData{"unpack2x16float", wgsl::BuiltinFn::kUnpack2X16Float}));
} // namespace data_unpacking_builtin_tests
// Tests for Synchronization builtins
namespace synchronization_builtin_tests {
using ResolverBuiltinTest_Barrier = ResolverTestWithParam<BuiltinData>;
TEST_P(ResolverBuiltinTest_Barrier, InferType) {
auto param = GetParam();
auto* call = Call(param.name);
WrapInFunction(CallStmt(call));
EXPECT_TRUE(r()->Resolve()) << r()->error();
ASSERT_NE(TypeOf(call), nullptr);
EXPECT_TRUE(TypeOf(call)->Is<core::type::Void>());
}
TEST_P(ResolverBuiltinTest_Barrier, Error_TooManyParams) {
auto param = GetParam();
auto* call = Call(param.name, Call<vec4<f32>>(1_f, 2_f, 3_f, 4_f), 1_f);
WrapInFunction(CallStmt(call));
EXPECT_FALSE(r()->Resolve());
EXPECT_THAT(r()->error(), HasSubstr("error: no matching call to '" + std::string(param.name)));
}
INSTANTIATE_TEST_SUITE_P(
ResolverTest,
ResolverBuiltinTest_Barrier,
testing::Values(BuiltinData{"storageBarrier", wgsl::BuiltinFn::kStorageBarrier},
BuiltinData{"workgroupBarrier", wgsl::BuiltinFn::kWorkgroupBarrier}));
} // namespace synchronization_builtin_tests
} // namespace
} // namespace tint::resolver