| // Copyright 2023 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/core/fluent_types.h" |
| #include "src/tint/lang/core/type/array.h" |
| #include "src/tint/lang/core/type/matrix.h" |
| #include "src/tint/lang/msl/writer/helper_test.h" |
| #include "src/tint/utils/text/string.h" |
| |
| using namespace tint::core::number_suffixes; // NOLINT |
| using namespace tint::core::fluent_types; // NOLINT |
| |
| namespace tint::msl::writer { |
| namespace { |
| |
| TEST_F(MslWriterTest, Constant_Bool_True) { |
| auto* c = b.Constant(true); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| bool const a = true; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Bool_False) { |
| auto* c = b.Constant(false); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| bool const a = false; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_i32) { |
| auto* c = b.Constant(-12345_i); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| int const a = -12345; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_u32) { |
| auto* c = b.Constant(12345_u); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| uint const a = 12345u; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_F32) { |
| auto* c = b.Constant(f32((1 << 30) - 4)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float const a = 1073741824.0f; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_F16) { |
| auto* c = b.Constant(f16((1 << 15) - 8)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| half const a = 32752.0h; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Vector_Splat) { |
| auto* c = b.Splat<vec3<f32>>(1.5_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3 const a = float3(1.5f); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Vector_Composite) { |
| auto* c = b.Composite<vec3<f32>>(1.5_f, 1.0_f, 1.5_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3 const a = float3(1.5f, 1.0f, 1.5f); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Vector_Composite_AnyZero) { |
| auto* c = b.Composite<vec3<f32>>(1.0_f, 0.0_f, 1.5_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3 const a = float3(1.0f, 0.0f, 1.5f); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Vector_Composite_AllZero) { |
| auto* c = b.Composite<vec3<f32>>(0.0_f, 0.0_f, 0.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3 const a = float3(0.0f); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Matrix_Splat) { |
| auto* c = b.Splat<mat3x2<f32>>(b.Splat<vec2<f32>>(1.5_f)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3x2 const a = float3x2(float2(1.5f), float2(1.5f), float2(1.5f)); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Matrix_Composite) { |
| auto* c = b.Composite<mat3x2<f32>>( // |
| b.Composite<vec2<f32>>(1.5_f, 1.0_f), // |
| b.Composite<vec2<f32>>(1.5_f, 2.0_f), // |
| b.Composite<vec2<f32>>(2.5_f, 3.5_f)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3x2 const a = float3x2(float2(1.5f, 1.0f), float2(1.5f, 2.0f), float2(2.5f, 3.5f)); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Matrix_Composite_AnyZero) { |
| auto* c = b.Composite<mat2x2<f32>>( // |
| b.Composite<vec2<f32>>(1.0_f, 0.0_f), // |
| b.Composite<vec2<f32>>(1.5_f, 2.5_f)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float2x2 const a = float2x2(float2(1.0f, 0.0f), float2(1.5f, 2.5f)); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Matrix_Composite_AllZero) { |
| auto* c = b.Composite<mat3x2<f32>>( // |
| b.Composite<vec2<f32>>(0.0_f, 0.0_f), // |
| b.Composite<vec2<f32>>(0.0_f, 0.0_f), // |
| b.Composite<vec2<f32>>(0.0_f, 0.0_f)); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| void foo() { |
| float3x2 const a = float3x2(float2(0.0f), float2(0.0f), float2(0.0f)); |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Array_Splat) { |
| auto* c = b.Splat<array<f32, 3>>(1.5_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + MetalArray() + R"( |
| void foo() { |
| tint_array<float, 3> const a = tint_array<float, 3>{1.5f, 1.5f, 1.5f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Array_Composite) { |
| auto* c = b.Composite<array<f32, 3>>(1.5_f, 1.0_f, 2.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + MetalArray() + R"( |
| void foo() { |
| tint_array<float, 3> const a = tint_array<float, 3>{1.5f, 1.0f, 2.0f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Array_Composite_AnyZero) { |
| auto* c = b.Composite<array<f32, 2>>(1.0_f, 0.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + MetalArray() + R"( |
| void foo() { |
| tint_array<float, 2> const a = tint_array<float, 2>{1.0f, 0.0f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Array_Composite_AllZero) { |
| auto* c = b.Composite<array<f32, 3>>(0.0_f, 0.0_f, 0.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + MetalArray() + R"( |
| void foo() { |
| tint_array<float, 3> const a = tint_array<float, 3>{}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Struct_Splat) { |
| auto* s = ty.Struct(mod.symbols.New("S"), { |
| {mod.symbols.Register("a"), ty.f32()}, |
| {mod.symbols.Register("b"), ty.f32()}, |
| }); |
| auto* c = b.Splat(s, 1.5_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| struct S { |
| float a; |
| float b; |
| }; |
| |
| void foo() { |
| S const a = S{.a=1.5f, .b=1.5f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Struct_Composite) { |
| auto* s = ty.Struct(mod.symbols.New("S"), { |
| {mod.symbols.Register("a"), ty.f32()}, |
| {mod.symbols.Register("b"), ty.f32()}, |
| }); |
| auto* c = b.Composite(s, 1.5_f, 1.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| struct S { |
| float a; |
| float b; |
| }; |
| |
| void foo() { |
| S const a = S{.a=1.5f, .b=1.0f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Struct_Composite_AnyZero) { |
| auto* s = ty.Struct(mod.symbols.New("S"), { |
| {mod.symbols.Register("a"), ty.f32()}, |
| {mod.symbols.Register("b"), ty.f32()}, |
| }); |
| auto* c = b.Composite(s, 1.0_f, 0.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| struct S { |
| float a; |
| float b; |
| }; |
| |
| void foo() { |
| S const a = S{.a=1.0f, .b=0.0f}; |
| } |
| )"); |
| } |
| |
| TEST_F(MslWriterTest, Constant_Struct_Composite_AllZero) { |
| auto* s = ty.Struct(mod.symbols.New("S"), { |
| {mod.symbols.Register("a"), ty.f32()}, |
| {mod.symbols.Register("b"), ty.f32()}, |
| }); |
| auto* c = b.Composite(s, 0.0_f, 0.0_f); |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Let("a", c); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << err_ << output_.msl; |
| EXPECT_EQ(output_.msl, MetalHeader() + R"( |
| struct S { |
| float a; |
| float b; |
| }; |
| |
| void foo() { |
| S const a = S{}; |
| } |
| )"); |
| } |
| |
| } // namespace |
| } // namespace tint::msl::writer |