dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "spirv/unified1/spirv.h" |
| 19 | #include "spirv/unified1/spirv.hpp11" |
dan sinclair | 1c9b486 | 2020-04-07 19:27:41 +0000 | [diff] [blame] | 20 | #include "src/ast/binary_expression.h" |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 21 | #include "src/ast/bool_literal.h" |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 22 | #include "src/ast/float_literal.h" |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 23 | #include "src/ast/identifier_expression.h" |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 24 | #include "src/ast/member_accessor_expression.h" |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame] | 25 | #include "src/ast/scalar_constructor_expression.h" |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 26 | #include "src/ast/sint_literal.h" |
| 27 | #include "src/ast/struct.h" |
| 28 | #include "src/ast/struct_decoration.h" |
| 29 | #include "src/ast/struct_member.h" |
| 30 | #include "src/ast/type/array_type.h" |
| 31 | #include "src/ast/type/bool_type.h" |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 32 | #include "src/ast/type/f32_type.h" |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 33 | #include "src/ast/type/i32_type.h" |
| 34 | #include "src/ast/type/matrix_type.h" |
| 35 | #include "src/ast/type/struct_type.h" |
| 36 | #include "src/ast/type/u32_type.h" |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 37 | #include "src/ast/type/vector_type.h" |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame] | 38 | #include "src/ast/type_constructor_expression.h" |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 39 | #include "src/ast/uint_literal.h" |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 40 | #include "src/context.h" |
| 41 | #include "src/type_determiner.h" |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 42 | #include "src/writer/spirv/builder.h" |
| 43 | #include "src/writer/spirv/spv_dump.h" |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 44 | #include "src/writer/spirv/test_helper.h" |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 45 | |
| 46 | namespace tint { |
| 47 | namespace writer { |
| 48 | namespace spirv { |
| 49 | namespace { |
| 50 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 51 | using SpvBuilderConstructorTest = TestHelper; |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 52 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 53 | TEST_F(SpvBuilderConstructorTest, Const) { |
| 54 | auto* c = Expr(42.2f); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 55 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 56 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, c, true), 2u); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 57 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 58 | |
| 59 | EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32 |
| 60 | %2 = OpConstant %1 42.2000008 |
| 61 | )"); |
| 62 | } |
| 63 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 64 | TEST_F(SpvBuilderConstructorTest, Type) { |
| 65 | auto* t = vec3<f32>(1.0f, 1.0f, 3.0f); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 66 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 67 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 68 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 69 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 5u); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 70 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 71 | |
| 72 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 73 | %1 = OpTypeVector %2 3 |
| 74 | %3 = OpConstant %2 1 |
| 75 | %4 = OpConstant %2 3 |
dan sinclair | 73e1ef8 | 2020-03-31 21:15:51 +0000 | [diff] [blame] | 76 | %5 = OpConstantComposite %1 %3 %3 %4 |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 77 | )"); |
| 78 | } |
| 79 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 80 | TEST_F(SpvBuilderConstructorTest, Type_WithCasts) { |
| 81 | auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(1)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 82 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 83 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 84 | |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 85 | b.push_function(Function{}); |
| 86 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 87 | EXPECT_EQ(b.GenerateExpression(t), 7u); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 88 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 89 | |
| 90 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 91 | %1 = OpTypeVector %2 2 |
| 92 | %4 = OpTypeInt 32 1 |
| 93 | %5 = OpConstant %4 1 |
| 94 | )"); |
| 95 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 96 | R"(%3 = OpConvertSToF %2 %5 |
| 97 | %6 = OpConvertSToF %2 %5 |
| 98 | %7 = OpCompositeConstruct %1 %3 %6 |
| 99 | )"); |
| 100 | } |
| 101 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 102 | TEST_F(SpvBuilderConstructorTest, Type_WithAlias) { |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 103 | // type Int = i32 |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 104 | // cast<Int>(2.3f) |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 105 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 106 | ast::type::AliasType alias("Int", ty.i32); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 107 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 108 | ast::TypeConstructorExpression cast(&alias, ExprList(2.3f)); |
dan sinclair | 5b853ee | 2020-06-22 20:44:27 +0000 | [diff] [blame] | 109 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 110 | ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); |
dan sinclair | 5b853ee | 2020-06-22 20:44:27 +0000 | [diff] [blame] | 111 | |
dan sinclair | 5b853ee | 2020-06-22 20:44:27 +0000 | [diff] [blame] | 112 | b.push_function(Function{}); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 113 | EXPECT_EQ(b.GenerateExpression(&cast), 1u); |
dan sinclair | 5b853ee | 2020-06-22 20:44:27 +0000 | [diff] [blame] | 114 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 115 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 116 | %3 = OpTypeFloat 32 |
| 117 | %4 = OpConstant %3 2.29999995 |
| 118 | )"); |
| 119 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 120 | R"(%1 = OpConvertFToS %2 %4 |
dan sinclair | 5b853ee | 2020-06-22 20:44:27 +0000 | [diff] [blame] | 121 | )"); |
| 122 | } |
| 123 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 124 | TEST_F(SpvBuilderConstructorTest, Type_IdentifierExpression_Param) { |
| 125 | auto* var = Var("ident", ast::StorageClass::kFunction, ty.f32); |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 126 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 127 | auto* t = vec2<f32>(1.0f, "ident"); |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 128 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 129 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 130 | |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 131 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 132 | ASSERT_TRUE(b.GenerateFunctionVariable(var)) << b.error(); |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 133 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 134 | EXPECT_EQ(b.GenerateExpression(t), 8u); |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 135 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 136 | |
| 137 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 138 | %2 = OpTypePointer Function %3 |
dan sinclair | 2287f33 | 2020-05-05 14:21:19 +0000 | [diff] [blame] | 139 | %4 = OpConstantNull %3 |
| 140 | %5 = OpTypeVector %3 2 |
| 141 | %6 = OpConstant %3 1 |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 142 | )"); |
| 143 | EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), |
dan sinclair | 2287f33 | 2020-05-05 14:21:19 +0000 | [diff] [blame] | 144 | R"(%1 = OpVariable %2 Function %4 |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 145 | )"); |
| 146 | |
| 147 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
dan sinclair | 2287f33 | 2020-05-05 14:21:19 +0000 | [diff] [blame] | 148 | R"(%7 = OpLoad %3 %1 |
| 149 | %8 = OpCompositeConstruct %5 %6 %7 |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 150 | )"); |
| 151 | } |
| 152 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 153 | TEST_F(SpvBuilderConstructorTest, Vector_Bitcast_Params) { |
| 154 | auto* t = vec2<u32>(1, 1); |
dan sinclair | a388d56 | 2020-10-07 14:06:28 +0000 | [diff] [blame] | 155 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 156 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | a388d56 | 2020-10-07 14:06:28 +0000 | [diff] [blame] | 157 | |
dan sinclair | a388d56 | 2020-10-07 14:06:28 +0000 | [diff] [blame] | 158 | b.push_function(Function{}); |
| 159 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 160 | EXPECT_EQ(b.GenerateExpression(t), 7u); |
dan sinclair | a388d56 | 2020-10-07 14:06:28 +0000 | [diff] [blame] | 161 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 162 | |
| 163 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0 |
| 164 | %1 = OpTypeVector %2 2 |
| 165 | %3 = OpTypeInt 32 1 |
| 166 | %4 = OpConstant %3 1 |
| 167 | )"); |
| 168 | |
| 169 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 170 | R"(%5 = OpBitcast %2 %4 |
| 171 | %6 = OpBitcast %2 %4 |
| 172 | %7 = OpCompositeConstruct %1 %5 %6 |
| 173 | )"); |
| 174 | } |
| 175 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 176 | TEST_F(SpvBuilderConstructorTest, Type_NonConst_Value_Fails) { |
| 177 | auto* rel = create<ast::BinaryExpression>(ast::BinaryOp::kAdd, Expr(3.0f), |
| 178 | Expr(3.0f)); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 179 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 180 | auto* t = vec2<f32>(1.0f, rel); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 181 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 182 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 183 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 184 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 0u); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 185 | EXPECT_TRUE(b.has_error()); |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame] | 186 | EXPECT_EQ(b.error(), R"(constructor must be a constant expression)"); |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 189 | TEST_F(SpvBuilderConstructorTest, Type_Bool_With_Bool) { |
| 190 | ast::TypeConstructorExpression cast(ty.bool_, ExprList(true)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 191 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 192 | ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 193 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 194 | b.push_function(Function{}); |
| 195 | |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 196 | EXPECT_EQ(b.GenerateExpression(&cast), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 197 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 198 | |
| 199 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool |
| 200 | %3 = OpConstantTrue %2 |
| 201 | )"); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 202 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 205 | TEST_F(SpvBuilderConstructorTest, Type_I32_With_I32) { |
| 206 | ast::TypeConstructorExpression cast(ty.i32, ExprList(2)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 207 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 208 | ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); |
| 209 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 210 | b.push_function(Function{}); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 211 | EXPECT_EQ(b.GenerateExpression(&cast), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 212 | |
| 213 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 214 | %3 = OpConstant %2 2 |
| 215 | )"); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 216 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 219 | TEST_F(SpvBuilderConstructorTest, Type_U32_With_U32) { |
| 220 | ast::TypeConstructorExpression cast(ty.u32, ExprList(2u)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 221 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 222 | ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); |
| 223 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 224 | b.push_function(Function{}); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 225 | EXPECT_EQ(b.GenerateExpression(&cast), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 226 | |
| 227 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0 |
| 228 | %3 = OpConstant %2 2 |
| 229 | )"); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 230 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 233 | TEST_F(SpvBuilderConstructorTest, Type_F32_With_F32) { |
| 234 | ast::TypeConstructorExpression cast(ty.f32, ExprList(2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 235 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 236 | ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error(); |
| 237 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 238 | b.push_function(Function{}); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 239 | EXPECT_EQ(b.GenerateExpression(&cast), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 240 | |
| 241 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 242 | %3 = OpConstant %2 2 |
| 243 | )"); |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 244 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 247 | TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_F32_F32) { |
| 248 | auto* cast = vec2<f32>(2.0f, 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 249 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 250 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 251 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 252 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 253 | EXPECT_EQ(b.GenerateExpression(cast), 4u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 254 | |
| 255 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 256 | %1 = OpTypeVector %2 2 |
| 257 | %3 = OpConstant %2 2 |
| 258 | %4 = OpConstantComposite %1 %3 %3 |
| 259 | )"); |
| 260 | } |
| 261 | |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 262 | TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_Vec2) { |
| 263 | auto* value = vec2<f32>(2.0f, 2.0f); |
| 264 | auto* cast = vec2<f32>(value); |
| 265 | |
| 266 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 267 | |
| 268 | b.push_function(Function{}); |
| 269 | EXPECT_EQ(b.GenerateExpression(cast), 5u); |
| 270 | |
| 271 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 272 | %2 = OpTypeVector %3 2 |
| 273 | %4 = OpConstant %3 2 |
| 274 | %5 = OpConstantComposite %2 %4 %4 |
| 275 | )"); |
| 276 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
| 277 | } |
| 278 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 279 | TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_F32_F32) { |
| 280 | auto* cast = vec3<f32>(2.0f, 2.0f, 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 281 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 282 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 283 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 284 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 285 | EXPECT_EQ(b.GenerateExpression(cast), 4u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 286 | |
| 287 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 288 | %1 = OpTypeVector %2 3 |
| 289 | %3 = OpConstant %2 2 |
| 290 | %4 = OpConstantComposite %1 %3 %3 %3 |
| 291 | )"); |
| 292 | } |
| 293 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 294 | TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_Vec2) { |
| 295 | auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 296 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 297 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 298 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 299 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 300 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 301 | |
| 302 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 303 | %1 = OpTypeVector %2 3 |
| 304 | %3 = OpConstant %2 2 |
| 305 | %4 = OpTypeVector %2 2 |
| 306 | %5 = OpConstantComposite %4 %3 %3 |
| 307 | )"); |
| 308 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 309 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 310 | %7 = OpCompositeExtract %2 %5 1 |
| 311 | %8 = OpCompositeConstruct %1 %3 %6 %7 |
| 312 | )"); |
| 313 | } |
| 314 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 315 | TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec2_F32) { |
| 316 | auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 317 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 318 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 319 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 320 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 321 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 322 | |
| 323 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 324 | %1 = OpTypeVector %2 3 |
| 325 | %3 = OpTypeVector %2 2 |
| 326 | %4 = OpConstant %2 2 |
| 327 | %5 = OpConstantComposite %3 %4 %4 |
| 328 | )"); |
| 329 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 330 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 331 | %7 = OpCompositeExtract %2 %5 1 |
| 332 | %8 = OpCompositeConstruct %1 %6 %7 %4 |
| 333 | )"); |
| 334 | } |
| 335 | |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 336 | TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec3) { |
| 337 | auto* value = vec3<f32>(2.0f, 2.0f, 2.0f); |
| 338 | auto* cast = vec3<f32>(value); |
| 339 | |
| 340 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 341 | |
| 342 | b.push_function(Function{}); |
| 343 | EXPECT_EQ(b.GenerateExpression(cast), 5u); |
| 344 | |
| 345 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 346 | %2 = OpTypeVector %3 3 |
| 347 | %4 = OpConstant %3 2 |
| 348 | %5 = OpConstantComposite %2 %4 %4 %4 |
| 349 | )"); |
| 350 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
| 351 | } |
| 352 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 353 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_F32_F32) { |
| 354 | auto* cast = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 355 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 356 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 357 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 358 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 359 | EXPECT_EQ(b.GenerateExpression(cast), 4u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 360 | |
| 361 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 362 | %1 = OpTypeVector %2 4 |
| 363 | %3 = OpConstant %2 2 |
| 364 | %4 = OpConstantComposite %1 %3 %3 %3 %3 |
| 365 | )"); |
| 366 | } |
| 367 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 368 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_Vec2) { |
| 369 | auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 370 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 371 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 372 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 373 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 374 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 375 | |
| 376 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 377 | %1 = OpTypeVector %2 4 |
| 378 | %3 = OpConstant %2 2 |
| 379 | %4 = OpTypeVector %2 2 |
| 380 | %5 = OpConstantComposite %4 %3 %3 |
| 381 | )"); |
| 382 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 383 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 384 | %7 = OpCompositeExtract %2 %5 1 |
| 385 | %8 = OpCompositeConstruct %1 %3 %3 %6 %7 |
| 386 | )"); |
| 387 | } |
| 388 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 389 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec2_F32) { |
| 390 | auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 391 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 392 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 393 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 394 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 395 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 396 | |
| 397 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 398 | %1 = OpTypeVector %2 4 |
| 399 | %3 = OpConstant %2 2 |
| 400 | %4 = OpTypeVector %2 2 |
| 401 | %5 = OpConstantComposite %4 %3 %3 |
| 402 | )"); |
| 403 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 404 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 405 | %7 = OpCompositeExtract %2 %5 1 |
| 406 | %8 = OpCompositeConstruct %1 %3 %6 %7 %3 |
| 407 | )"); |
| 408 | } |
| 409 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 410 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_F32_F32) { |
| 411 | auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 412 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 413 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 414 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 415 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 416 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 417 | |
| 418 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 419 | %1 = OpTypeVector %2 4 |
| 420 | %3 = OpTypeVector %2 2 |
| 421 | %4 = OpConstant %2 2 |
| 422 | %5 = OpConstantComposite %3 %4 %4 |
| 423 | )"); |
| 424 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 425 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 426 | %7 = OpCompositeExtract %2 %5 1 |
| 427 | %8 = OpCompositeConstruct %1 %6 %7 %4 %4 |
| 428 | )"); |
| 429 | } |
| 430 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 431 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_Vec2) { |
| 432 | auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 433 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 434 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 435 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 436 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 437 | EXPECT_EQ(b.GenerateExpression(cast), 10u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 438 | |
| 439 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 440 | %1 = OpTypeVector %2 4 |
| 441 | %3 = OpTypeVector %2 2 |
| 442 | %4 = OpConstant %2 2 |
| 443 | %5 = OpConstantComposite %3 %4 %4 |
| 444 | )"); |
| 445 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 446 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 447 | %7 = OpCompositeExtract %2 %5 1 |
| 448 | %8 = OpCompositeExtract %2 %5 0 |
| 449 | %9 = OpCompositeExtract %2 %5 1 |
| 450 | %10 = OpCompositeConstruct %1 %6 %7 %8 %9 |
| 451 | )"); |
| 452 | } |
| 453 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 454 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec3) { |
| 455 | auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 456 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 457 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 458 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 459 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 460 | EXPECT_EQ(b.GenerateExpression(cast), 9u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 461 | |
| 462 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 463 | %1 = OpTypeVector %2 4 |
| 464 | %3 = OpConstant %2 2 |
| 465 | %4 = OpTypeVector %2 3 |
| 466 | %5 = OpConstantComposite %4 %3 %3 %3 |
| 467 | )"); |
| 468 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 469 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 470 | %7 = OpCompositeExtract %2 %5 1 |
| 471 | %8 = OpCompositeExtract %2 %5 2 |
| 472 | %9 = OpCompositeConstruct %1 %3 %6 %7 %8 |
| 473 | )"); |
| 474 | } |
| 475 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 476 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec3_F32) { |
| 477 | auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 478 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 479 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 480 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 481 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 482 | EXPECT_EQ(b.GenerateExpression(cast), 9u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 483 | |
| 484 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 485 | %1 = OpTypeVector %2 4 |
| 486 | %3 = OpTypeVector %2 3 |
| 487 | %4 = OpConstant %2 2 |
| 488 | %5 = OpConstantComposite %3 %4 %4 %4 |
| 489 | )"); |
| 490 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 491 | R"(%6 = OpCompositeExtract %2 %5 0 |
| 492 | %7 = OpCompositeExtract %2 %5 1 |
| 493 | %8 = OpCompositeExtract %2 %5 2 |
| 494 | %9 = OpCompositeConstruct %1 %6 %7 %8 %4 |
| 495 | )"); |
| 496 | } |
| 497 | |
David Neto | 6cd6f74 | 2020-11-23 16:34:35 +0000 | [diff] [blame^] | 498 | TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec4) { |
| 499 | auto* value = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f); |
| 500 | auto* cast = vec4<f32>(value); |
| 501 | |
| 502 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 503 | |
| 504 | b.push_function(Function{}); |
| 505 | EXPECT_EQ(b.GenerateExpression(cast), 5u); |
| 506 | |
| 507 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 508 | %2 = OpTypeVector %3 4 |
| 509 | %4 = OpConstant %3 2 |
| 510 | %5 = OpConstantComposite %2 %4 %4 %4 %4 |
| 511 | )"); |
| 512 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()"); |
| 513 | } |
| 514 | |
| 515 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec2_With_Vec2) { |
| 516 | auto* cast = vec2<f32>(vec2<f32>(2.0f, 2.0f)); |
| 517 | |
| 518 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 519 | |
| 520 | b.push_function(Function{}); |
| 521 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u); |
| 522 | |
| 523 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 524 | %2 = OpTypeVector %3 2 |
| 525 | %4 = OpConstant %3 2 |
| 526 | %5 = OpConstantComposite %2 %4 %4 |
| 527 | )"); |
| 528 | } |
| 529 | |
| 530 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec3) { |
| 531 | auto* cast = vec3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f)); |
| 532 | |
| 533 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 534 | |
| 535 | b.push_function(Function{}); |
| 536 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u); |
| 537 | |
| 538 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 539 | %2 = OpTypeVector %3 3 |
| 540 | %4 = OpConstant %3 2 |
| 541 | %5 = OpConstantComposite %2 %4 %4 %4 |
| 542 | )"); |
| 543 | } |
| 544 | |
| 545 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec4) { |
| 546 | auto* cast = vec4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f)); |
| 547 | |
| 548 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
| 549 | |
| 550 | b.push_function(Function{}); |
| 551 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u); |
| 552 | |
| 553 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 554 | %2 = OpTypeVector %3 4 |
| 555 | %4 = OpConstant %3 2 |
| 556 | %5 = OpConstantComposite %2 %4 %4 %4 %4 |
| 557 | )"); |
| 558 | } |
| 559 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 560 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_F32_Vec2) { |
| 561 | auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 562 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 563 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 564 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 565 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 566 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 567 | |
| 568 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 569 | %1 = OpTypeVector %2 3 |
| 570 | %3 = OpConstant %2 2 |
| 571 | %4 = OpTypeVector %2 2 |
| 572 | %5 = OpConstantComposite %4 %3 %3 |
| 573 | %7 = OpTypeInt 32 0 |
| 574 | %8 = OpConstant %7 0 |
| 575 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 576 | %10 = OpConstant %7 1 |
| 577 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 578 | %11 = OpSpecConstantComposite %1 %3 %6 %9 |
| 579 | )"); |
| 580 | } |
| 581 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 582 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec2_F32) { |
| 583 | auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 584 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 585 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 586 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 587 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 588 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 589 | |
| 590 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 591 | %1 = OpTypeVector %2 3 |
| 592 | %3 = OpTypeVector %2 2 |
| 593 | %4 = OpConstant %2 2 |
| 594 | %5 = OpConstantComposite %3 %4 %4 |
| 595 | %7 = OpTypeInt 32 0 |
| 596 | %8 = OpConstant %7 0 |
| 597 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 598 | %10 = OpConstant %7 1 |
| 599 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 600 | %11 = OpSpecConstantComposite %1 %6 %9 %4 |
| 601 | )"); |
| 602 | } |
| 603 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 604 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_F32_Vec2) { |
| 605 | auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 606 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 607 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 608 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 609 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 610 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 611 | |
| 612 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 613 | %1 = OpTypeVector %2 4 |
| 614 | %3 = OpConstant %2 2 |
| 615 | %4 = OpTypeVector %2 2 |
| 616 | %5 = OpConstantComposite %4 %3 %3 |
| 617 | %7 = OpTypeInt 32 0 |
| 618 | %8 = OpConstant %7 0 |
| 619 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 620 | %10 = OpConstant %7 1 |
| 621 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 622 | %11 = OpSpecConstantComposite %1 %3 %3 %6 %9 |
| 623 | )"); |
| 624 | } |
| 625 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 626 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec2_F32) { |
| 627 | auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 628 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 629 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 630 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 631 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 632 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 633 | |
| 634 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 635 | %1 = OpTypeVector %2 4 |
| 636 | %3 = OpConstant %2 2 |
| 637 | %4 = OpTypeVector %2 2 |
| 638 | %5 = OpConstantComposite %4 %3 %3 |
| 639 | %7 = OpTypeInt 32 0 |
| 640 | %8 = OpConstant %7 0 |
| 641 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 642 | %10 = OpConstant %7 1 |
| 643 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 644 | %11 = OpSpecConstantComposite %1 %3 %6 %9 %3 |
| 645 | )"); |
| 646 | } |
| 647 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 648 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_F32_F32) { |
| 649 | auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 650 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 651 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 652 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 653 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 654 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 655 | |
| 656 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 657 | %1 = OpTypeVector %2 4 |
| 658 | %3 = OpTypeVector %2 2 |
| 659 | %4 = OpConstant %2 2 |
| 660 | %5 = OpConstantComposite %3 %4 %4 |
| 661 | %7 = OpTypeInt 32 0 |
| 662 | %8 = OpConstant %7 0 |
| 663 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 664 | %10 = OpConstant %7 1 |
| 665 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 666 | %11 = OpSpecConstantComposite %1 %6 %9 %4 %4 |
| 667 | )"); |
| 668 | } |
| 669 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 670 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_Vec2) { |
| 671 | auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 672 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 673 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 674 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 675 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 676 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 677 | |
| 678 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 679 | %1 = OpTypeVector %2 4 |
| 680 | %3 = OpTypeVector %2 2 |
| 681 | %4 = OpConstant %2 2 |
| 682 | %5 = OpConstantComposite %3 %4 %4 |
| 683 | %7 = OpTypeInt 32 0 |
| 684 | %8 = OpConstant %7 0 |
| 685 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 686 | %10 = OpConstant %7 1 |
| 687 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 688 | %11 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 689 | %12 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 690 | %13 = OpSpecConstantComposite %1 %6 %9 %11 %12 |
| 691 | )"); |
| 692 | } |
| 693 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 694 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec3) { |
| 695 | auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 696 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 697 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 698 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 699 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 700 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 701 | |
| 702 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 703 | %1 = OpTypeVector %2 4 |
| 704 | %3 = OpConstant %2 2 |
| 705 | %4 = OpTypeVector %2 3 |
| 706 | %5 = OpConstantComposite %4 %3 %3 %3 |
| 707 | %7 = OpTypeInt 32 0 |
| 708 | %8 = OpConstant %7 0 |
| 709 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 710 | %10 = OpConstant %7 1 |
| 711 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 712 | %12 = OpConstant %7 2 |
| 713 | %11 = OpSpecConstantOp %2 CompositeExtract %5 12 |
| 714 | %13 = OpSpecConstantComposite %1 %3 %6 %9 %11 |
| 715 | )"); |
| 716 | } |
| 717 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 718 | TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec3_F32) { |
| 719 | auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 720 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 721 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 722 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 723 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 724 | EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 725 | |
| 726 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 727 | %1 = OpTypeVector %2 4 |
| 728 | %3 = OpTypeVector %2 3 |
| 729 | %4 = OpConstant %2 2 |
| 730 | %5 = OpConstantComposite %3 %4 %4 %4 |
| 731 | %7 = OpTypeInt 32 0 |
| 732 | %8 = OpConstant %7 0 |
| 733 | %6 = OpSpecConstantOp %2 CompositeExtract %5 8 |
| 734 | %10 = OpConstant %7 1 |
| 735 | %9 = OpSpecConstantOp %2 CompositeExtract %5 10 |
| 736 | %12 = OpConstant %7 2 |
| 737 | %11 = OpSpecConstantOp %2 CompositeExtract %5 12 |
| 738 | %13 = OpSpecConstantComposite %1 %6 %9 %11 %4 |
| 739 | )"); |
| 740 | } |
| 741 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 742 | TEST_F(SpvBuilderConstructorTest, Type_Mat2x2_With_Vec2_Vec2) { |
| 743 | auto* cast = mat2x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 744 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 745 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 746 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 747 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 748 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 749 | |
| 750 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 751 | %2 = OpTypeVector %3 2 |
| 752 | %1 = OpTypeMatrix %2 2 |
| 753 | %4 = OpConstant %3 2 |
| 754 | %5 = OpConstantComposite %2 %4 %4 |
| 755 | %6 = OpConstantComposite %1 %5 %5 |
| 756 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 759 | TEST_F(SpvBuilderConstructorTest, Type_Mat3x2_With_Vec2_Vec2_Vec2) { |
| 760 | auto* cast = mat3x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f), |
| 761 | vec2<f32>(2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 762 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 763 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 764 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 765 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 766 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 767 | |
| 768 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 769 | %2 = OpTypeVector %3 2 |
| 770 | %1 = OpTypeMatrix %2 3 |
| 771 | %4 = OpConstant %3 2 |
| 772 | %5 = OpConstantComposite %2 %4 %4 |
| 773 | %6 = OpConstantComposite %1 %5 %5 %5 |
| 774 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 777 | TEST_F(SpvBuilderConstructorTest, Type_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) { |
| 778 | auto* cast = mat4x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f), |
| 779 | vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 780 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 781 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 782 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 783 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 784 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 785 | |
| 786 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 787 | %2 = OpTypeVector %3 2 |
| 788 | %1 = OpTypeMatrix %2 4 |
| 789 | %4 = OpConstant %3 2 |
| 790 | %5 = OpConstantComposite %2 %4 %4 |
| 791 | %6 = OpConstantComposite %1 %5 %5 %5 %5 |
| 792 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 795 | TEST_F(SpvBuilderConstructorTest, Type_Mat2x3_With_Vec3_Vec3) { |
| 796 | auto* cast = |
| 797 | mat2x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 798 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 799 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 800 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 801 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 802 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 803 | |
| 804 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 805 | %2 = OpTypeVector %3 3 |
| 806 | %1 = OpTypeMatrix %2 2 |
| 807 | %4 = OpConstant %3 2 |
| 808 | %5 = OpConstantComposite %2 %4 %4 %4 |
| 809 | %6 = OpConstantComposite %1 %5 %5 |
| 810 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 813 | TEST_F(SpvBuilderConstructorTest, Type_Mat3x3_With_Vec3_Vec3_Vec3) { |
| 814 | auto* cast = |
| 815 | mat3x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f), |
| 816 | vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 817 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 818 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 819 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 820 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 821 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 822 | |
| 823 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 824 | %2 = OpTypeVector %3 3 |
| 825 | %1 = OpTypeMatrix %2 3 |
| 826 | %4 = OpConstant %3 2 |
| 827 | %5 = OpConstantComposite %2 %4 %4 %4 |
| 828 | %6 = OpConstantComposite %1 %5 %5 %5 |
| 829 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 832 | TEST_F(SpvBuilderConstructorTest, Type_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) { |
| 833 | auto* cast = |
| 834 | mat4x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f), |
| 835 | vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 836 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 837 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 838 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 839 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 840 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 841 | |
| 842 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 843 | %2 = OpTypeVector %3 3 |
| 844 | %1 = OpTypeMatrix %2 4 |
| 845 | %4 = OpConstant %3 2 |
| 846 | %5 = OpConstantComposite %2 %4 %4 %4 |
| 847 | %6 = OpConstantComposite %1 %5 %5 %5 %5 |
| 848 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 851 | TEST_F(SpvBuilderConstructorTest, Type_Mat2x4_With_Vec4_Vec4) { |
| 852 | auto* cast = mat2x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), |
| 853 | vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 854 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 855 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 856 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 857 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 858 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 859 | |
| 860 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 861 | %2 = OpTypeVector %3 4 |
| 862 | %1 = OpTypeMatrix %2 2 |
| 863 | %4 = OpConstant %3 2 |
| 864 | %5 = OpConstantComposite %2 %4 %4 %4 %4 |
| 865 | %6 = OpConstantComposite %1 %5 %5 |
| 866 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 869 | TEST_F(SpvBuilderConstructorTest, Type_Mat3x4_With_Vec4_Vec4_Vec4) { |
| 870 | auto* cast = mat3x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), |
| 871 | vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), |
| 872 | vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f)); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 873 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 874 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 875 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 876 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 877 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 878 | |
| 879 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 880 | %2 = OpTypeVector %3 4 |
| 881 | %1 = OpTypeMatrix %2 3 |
| 882 | %4 = OpConstant %3 2 |
| 883 | %5 = OpConstantComposite %2 %4 %4 %4 %4 |
| 884 | %6 = OpConstantComposite %1 %5 %5 %5 |
| 885 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 888 | TEST_F(SpvBuilderConstructorTest, Type_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) { |
| 889 | auto* cast = mat4x4<f32>( |
| 890 | vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), |
| 891 | vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f)); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 892 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 893 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 894 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 895 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 896 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 897 | |
dan sinclair | d6e063d | 2020-09-24 16:38:35 +0000 | [diff] [blame] | 898 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 899 | %2 = OpTypeVector %3 4 |
| 900 | %1 = OpTypeMatrix %2 4 |
| 901 | %4 = OpConstant %3 2 |
| 902 | %5 = OpConstantComposite %2 %4 %4 %4 %4 |
| 903 | %6 = OpConstantComposite %1 %5 %5 %5 %5 |
| 904 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 907 | TEST_F(SpvBuilderConstructorTest, Type_Array_5_F32) { |
| 908 | auto* cast = array<f32, 5>(2.0f, 2.0f, 2.0f, 2.0f, 2.0f); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 909 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 910 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 911 | |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 912 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 913 | EXPECT_EQ(b.GenerateExpression(cast), 6u); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 914 | |
| 915 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 916 | %3 = OpTypeInt 32 0 |
| 917 | %4 = OpConstant %3 5 |
| 918 | %1 = OpTypeArray %2 %4 |
| 919 | %5 = OpConstant %2 2 |
| 920 | %6 = OpConstantComposite %1 %5 %5 %5 %5 %5 |
| 921 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 924 | TEST_F(SpvBuilderConstructorTest, Type_Array_2_Vec3) { |
| 925 | auto* cast = |
| 926 | array<f32, 2>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 927 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 928 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 929 | |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 930 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 931 | EXPECT_EQ(b.GenerateExpression(cast), 8u); |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 932 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 933 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 934 | %3 = OpTypeInt 32 0 |
| 935 | %4 = OpConstant %3 2 |
| 936 | %1 = OpTypeArray %2 %4 |
| 937 | %5 = OpTypeVector %2 3 |
| 938 | %6 = OpConstant %2 2 |
| 939 | %7 = OpConstantComposite %5 %6 %6 %6 |
dan sinclair | 8a220a6 | 2020-09-24 16:48:35 +0000 | [diff] [blame] | 940 | %8 = OpConstantComposite %1 %7 %7 |
| 941 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 944 | TEST_F(SpvBuilderConstructorTest, Type_Struct) { |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 945 | ast::StructMemberDecorationList decos; |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 946 | auto* s = create<ast::Struct>(ast::StructMemberList{ |
| 947 | create<ast::StructMember>("a", ty.f32, decos), |
| 948 | create<ast::StructMember>("b", ty.vec3<f32>(), decos), |
| 949 | }); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 950 | ast::type::StructType s_type("my_struct", s); |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 951 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 952 | auto* t = Construct(&s_type, 2.0f, vec3<f32>(2.0f, 2.0f, 2.0f)); |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 953 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 954 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 955 | |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 956 | b.push_function(Function{}); |
| 957 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 958 | EXPECT_EQ(b.GenerateExpression(t), 6u); |
dan sinclair | 571bce6 | 2020-09-24 18:18:05 +0000 | [diff] [blame] | 959 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 960 | |
| 961 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 962 | %3 = OpTypeVector %2 3 |
| 963 | %1 = OpTypeStruct %2 %3 |
| 964 | %4 = OpConstant %2 2 |
| 965 | %5 = OpConstantComposite %3 %4 %4 %4 |
| 966 | %6 = OpConstantComposite %1 %4 %5 |
| 967 | )"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 970 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_F32) { |
| 971 | auto* t = Construct(ty.f32); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 972 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 973 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 974 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 975 | b.push_function(Function{}); |
| 976 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 977 | EXPECT_EQ(b.GenerateExpression(t), 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 978 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 979 | |
| 980 | EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32 |
| 981 | %2 = OpConstantNull %1 |
| 982 | )"); |
| 983 | } |
| 984 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 985 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_I32) { |
| 986 | auto* t = Construct(ty.i32); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 987 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 988 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 989 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 990 | b.push_function(Function{}); |
| 991 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 992 | EXPECT_EQ(b.GenerateExpression(t), 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 993 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 994 | |
| 995 | EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 1 |
| 996 | %2 = OpConstantNull %1 |
| 997 | )"); |
| 998 | } |
| 999 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1000 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_U32) { |
| 1001 | auto* t = Construct(ty.u32); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1002 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1003 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1004 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1005 | b.push_function(Function{}); |
| 1006 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1007 | EXPECT_EQ(b.GenerateExpression(t), 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1008 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1009 | |
| 1010 | EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 0 |
| 1011 | %2 = OpConstantNull %1 |
| 1012 | )"); |
| 1013 | } |
| 1014 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1015 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Bool) { |
| 1016 | auto* t = Construct(ty.bool_); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1017 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1018 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1019 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1020 | b.push_function(Function{}); |
| 1021 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1022 | EXPECT_EQ(b.GenerateExpression(t), 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1023 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1024 | |
| 1025 | EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool |
| 1026 | %2 = OpConstantNull %1 |
| 1027 | )"); |
| 1028 | } |
| 1029 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1030 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Vector) { |
| 1031 | auto* t = vec2<i32>(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1032 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1033 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1034 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1035 | b.push_function(Function{}); |
| 1036 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1037 | EXPECT_EQ(b.GenerateExpression(t), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1038 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1039 | |
| 1040 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 1041 | %1 = OpTypeVector %2 2 |
| 1042 | %3 = OpConstantNull %1 |
| 1043 | )"); |
| 1044 | } |
| 1045 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1046 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Matrix) { |
| 1047 | auto* t = mat4x2<f32>(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1048 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1049 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1050 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1051 | b.push_function(Function{}); |
| 1052 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1053 | EXPECT_EQ(b.GenerateExpression(t), 4u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1054 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1055 | |
| 1056 | EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 |
| 1057 | %2 = OpTypeVector %3 2 |
| 1058 | %1 = OpTypeMatrix %2 4 |
| 1059 | %4 = OpConstantNull %1 |
| 1060 | )"); |
| 1061 | } |
| 1062 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1063 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Array) { |
| 1064 | auto* t = array<i32, 2>(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1065 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1066 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1067 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1068 | b.push_function(Function{}); |
| 1069 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1070 | EXPECT_EQ(b.GenerateExpression(t), 5u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1071 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1072 | |
| 1073 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 1074 | %3 = OpTypeInt 32 0 |
| 1075 | %4 = OpConstant %3 2 |
| 1076 | %1 = OpTypeArray %2 %4 |
| 1077 | %5 = OpConstantNull %1 |
| 1078 | )"); |
| 1079 | } |
| 1080 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1081 | TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Struct) { |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1082 | ast::StructMemberDecorationList decos; |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1083 | auto* s = create<ast::Struct>(ast::StructMemberList{ |
| 1084 | create<ast::StructMember>("a", ty.f32, decos), |
| 1085 | }); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1086 | ast::type::StructType s_type("my_struct", s); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1087 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1088 | auto* t = Construct(&s_type); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1089 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1090 | EXPECT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1091 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1092 | b.push_function(Function{}); |
| 1093 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1094 | EXPECT_EQ(b.GenerateExpression(t), 3u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1095 | ASSERT_FALSE(b.has_error()) << b.error(); |
| 1096 | |
| 1097 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 1098 | %1 = OpTypeStruct %2 |
| 1099 | %3 = OpConstantNull %1 |
| 1100 | )"); |
| 1101 | } |
| 1102 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1103 | TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_I32) { |
| 1104 | auto* cast = Construct(ty.i32, 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1105 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1106 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1107 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1108 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1109 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1110 | |
| 1111 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 1112 | %3 = OpTypeInt 32 0 |
| 1113 | %4 = OpConstant %3 2 |
| 1114 | )"); |
| 1115 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1116 | R"(%1 = OpBitcast %2 %4 |
| 1117 | )"); |
| 1118 | } |
| 1119 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1120 | TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_U32) { |
| 1121 | auto* cast = Construct(ty.u32, 2); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1122 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1123 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1124 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1125 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1126 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1127 | |
| 1128 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0 |
| 1129 | %3 = OpTypeInt 32 1 |
| 1130 | %4 = OpConstant %3 2 |
| 1131 | )"); |
| 1132 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1133 | R"(%1 = OpBitcast %2 %4 |
| 1134 | )"); |
| 1135 | } |
| 1136 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1137 | TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_I32) { |
| 1138 | auto* cast = Construct(ty.i32, 2.4f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1139 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1140 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1141 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1142 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1143 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1144 | |
| 1145 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1 |
| 1146 | %3 = OpTypeFloat 32 |
| 1147 | %4 = OpConstant %3 2.4000001 |
| 1148 | )"); |
| 1149 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1150 | R"(%1 = OpConvertFToS %2 %4 |
| 1151 | )"); |
| 1152 | } |
| 1153 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1154 | TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_U32) { |
| 1155 | auto* cast = Construct(ty.u32, 2.4f); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1156 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1157 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1158 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1159 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1160 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1161 | |
| 1162 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0 |
| 1163 | %3 = OpTypeFloat 32 |
| 1164 | %4 = OpConstant %3 2.4000001 |
| 1165 | )"); |
| 1166 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1167 | R"(%1 = OpConvertFToU %2 %4 |
| 1168 | )"); |
| 1169 | } |
| 1170 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1171 | TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F32) { |
| 1172 | auto* cast = Construct(ty.f32, 2); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1173 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1174 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1175 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1176 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1177 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1178 | |
| 1179 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 1180 | %3 = OpTypeInt 32 1 |
| 1181 | %4 = OpConstant %3 2 |
| 1182 | )"); |
| 1183 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1184 | R"(%1 = OpConvertSToF %2 %4 |
| 1185 | )"); |
| 1186 | } |
| 1187 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1188 | TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F32) { |
| 1189 | auto* cast = Construct(ty.f32, 2u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1190 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1191 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1192 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1193 | b.push_function(Function{}); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1194 | EXPECT_EQ(b.GenerateExpression(cast), 1u); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1195 | |
| 1196 | EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 |
| 1197 | %3 = OpTypeInt 32 0 |
| 1198 | %4 = OpConstant %3 2 |
| 1199 | )"); |
| 1200 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1201 | R"(%1 = OpConvertUToF %2 %4 |
| 1202 | )"); |
| 1203 | } |
| 1204 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1205 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_I32) { |
| 1206 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<u32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1207 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1208 | auto* cast = Construct(ty.vec3<i32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1209 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1210 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1211 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1212 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1213 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1214 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1215 | |
| 1216 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0 |
| 1217 | %3 = OpTypeVector %4 3 |
| 1218 | %2 = OpTypePointer Private %3 |
| 1219 | %5 = OpConstantNull %3 |
| 1220 | %1 = OpVariable %2 Private %5 |
| 1221 | %8 = OpTypeInt 32 1 |
| 1222 | %7 = OpTypeVector %8 3 |
| 1223 | )"); |
| 1224 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1225 | R"(%9 = OpLoad %3 %1 |
| 1226 | %6 = OpBitcast %7 %9 |
| 1227 | )"); |
| 1228 | } |
| 1229 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1230 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_I32) { |
| 1231 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<f32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1232 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1233 | auto* cast = Construct(ty.vec3<i32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1234 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1235 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1236 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1237 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1238 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1239 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1240 | |
| 1241 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32 |
| 1242 | %3 = OpTypeVector %4 3 |
| 1243 | %2 = OpTypePointer Private %3 |
| 1244 | %5 = OpConstantNull %3 |
| 1245 | %1 = OpVariable %2 Private %5 |
| 1246 | %8 = OpTypeInt 32 1 |
| 1247 | %7 = OpTypeVector %8 3 |
| 1248 | )"); |
| 1249 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1250 | R"(%9 = OpLoad %3 %1 |
| 1251 | %6 = OpConvertFToS %7 %9 |
| 1252 | )"); |
| 1253 | } |
| 1254 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1255 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_U32) { |
| 1256 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<i32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1257 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1258 | auto* cast = Construct(ty.vec3<u32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1259 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1260 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1261 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1262 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1263 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1264 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1265 | |
| 1266 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 |
| 1267 | %3 = OpTypeVector %4 3 |
| 1268 | %2 = OpTypePointer Private %3 |
| 1269 | %5 = OpConstantNull %3 |
| 1270 | %1 = OpVariable %2 Private %5 |
| 1271 | %8 = OpTypeInt 32 0 |
| 1272 | %7 = OpTypeVector %8 3 |
| 1273 | )"); |
| 1274 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1275 | R"(%9 = OpLoad %3 %1 |
| 1276 | %6 = OpBitcast %7 %9 |
| 1277 | )"); |
| 1278 | } |
| 1279 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1280 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_U32) { |
| 1281 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<f32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1282 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1283 | auto* cast = Construct(ty.vec3<u32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1284 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1285 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1286 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1287 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1288 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1289 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1290 | |
| 1291 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32 |
| 1292 | %3 = OpTypeVector %4 3 |
| 1293 | %2 = OpTypePointer Private %3 |
| 1294 | %5 = OpConstantNull %3 |
| 1295 | %1 = OpVariable %2 Private %5 |
| 1296 | %8 = OpTypeInt 32 0 |
| 1297 | %7 = OpTypeVector %8 3 |
| 1298 | )"); |
| 1299 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1300 | R"(%9 = OpLoad %3 %1 |
| 1301 | %6 = OpConvertFToU %7 %9 |
| 1302 | )"); |
| 1303 | } |
| 1304 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1305 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F32) { |
| 1306 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<i32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1307 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1308 | auto* cast = Construct(ty.vec3<f32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1309 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1310 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1311 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1312 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1313 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1314 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1315 | |
| 1316 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 |
| 1317 | %3 = OpTypeVector %4 3 |
| 1318 | %2 = OpTypePointer Private %3 |
| 1319 | %5 = OpConstantNull %3 |
| 1320 | %1 = OpVariable %2 Private %5 |
| 1321 | %8 = OpTypeFloat 32 |
| 1322 | %7 = OpTypeVector %8 3 |
| 1323 | )"); |
| 1324 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1325 | R"(%9 = OpLoad %3 %1 |
| 1326 | %6 = OpConvertSToF %7 %9 |
| 1327 | )"); |
| 1328 | } |
| 1329 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1330 | TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F32) { |
| 1331 | auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<u32>()); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1332 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1333 | auto* cast = Construct(ty.vec3<f32>(), "i"); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1334 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1335 | ASSERT_TRUE(td.DetermineResultType(cast)) << td.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1336 | |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1337 | b.push_function(Function{}); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1338 | ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error(); |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1339 | EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error(); |
dan sinclair | 3c02592 | 2020-09-24 14:38:44 +0000 | [diff] [blame] | 1340 | |
| 1341 | EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0 |
| 1342 | %3 = OpTypeVector %4 3 |
| 1343 | %2 = OpTypePointer Private %3 |
| 1344 | %5 = OpConstantNull %3 |
| 1345 | %1 = OpVariable %2 Private %5 |
| 1346 | %8 = OpTypeFloat 32 |
| 1347 | %7 = OpTypeVector %8 3 |
| 1348 | )"); |
| 1349 | EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), |
| 1350 | R"(%9 = OpLoad %3 %1 |
| 1351 | %6 = OpConvertUToF %7 %9 |
| 1352 | )"); |
| 1353 | } |
| 1354 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1355 | TEST_F(SpvBuilderConstructorTest, |
| 1356 | IsConstructorConst_GlobalVectorWithAllConstConstructors) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1357 | // vec3<f32>(1.0, 2.0, 3.0) -> true |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1358 | auto* t = vec3<f32>(1.f, 2.f, 3.f); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1359 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1360 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1361 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1362 | EXPECT_TRUE(b.is_constructor_const(t, true)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1363 | EXPECT_FALSE(b.has_error()); |
| 1364 | } |
| 1365 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1366 | TEST_F(SpvBuilderConstructorTest, IsConstructorConst_GlobalVector_WithIdent) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1367 | // vec3<f32>(a, b, c) -> false -- ERROR |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1368 | auto* t = vec3<f32>("a", "b", "c"); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1369 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1370 | Var("a", ast::StorageClass::kPrivate, ty.f32); |
| 1371 | Var("b", ast::StorageClass::kPrivate, ty.f32); |
| 1372 | Var("c", ast::StorageClass::kPrivate, ty.f32); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1373 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1374 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 1375 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1376 | EXPECT_FALSE(b.is_constructor_const(t, true)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1377 | EXPECT_TRUE(b.has_error()); |
| 1378 | EXPECT_EQ(b.error(), "constructor must be a constant expression"); |
| 1379 | } |
| 1380 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1381 | TEST_F(SpvBuilderConstructorTest, |
| 1382 | IsConstructorConst_GlobalArrayWithAllConstConstructors) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1383 | // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0)) |
| 1384 | // -> true |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1385 | auto* t = Construct(ty.array(ty.vec2<f32>(), 2), vec3<f32>(1.f, 2.f, 3.f), |
| 1386 | vec3<f32>(1.f, 2.f, 3.f)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1387 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1388 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1389 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1390 | EXPECT_TRUE(b.is_constructor_const(t, true)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1391 | EXPECT_FALSE(b.has_error()); |
| 1392 | } |
| 1393 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1394 | TEST_F(SpvBuilderConstructorTest, |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1395 | IsConstructorConst_GlobalVectorWithMatchingTypeConstructors) { |
| 1396 | // vec3<f32>(f32(1.0), f32(2.0)) -> false |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1397 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1398 | auto* t = vec2<f32>(Construct<f32>(1.f), Construct<f32>(2.f)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1399 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1400 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1401 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1402 | EXPECT_FALSE(b.is_constructor_const(t, true)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1403 | EXPECT_FALSE(b.has_error()); |
| 1404 | } |
| 1405 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1406 | TEST_F(SpvBuilderConstructorTest, |
| 1407 | IsConstructorConst_GlobalWithTypeCastConstructor) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1408 | // vec3<f32>(f32(1), f32(2)) -> false |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1409 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1410 | auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(2)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1411 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1412 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1413 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1414 | EXPECT_FALSE(b.is_constructor_const(t, true)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1415 | EXPECT_FALSE(b.has_error()); |
| 1416 | } |
| 1417 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1418 | TEST_F(SpvBuilderConstructorTest, |
| 1419 | IsConstructorConst_VectorWithAllConstConstructors) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1420 | // vec3<f32>(1.0, 2.0, 3.0) -> true |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1421 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1422 | auto* t = vec3<f32>(1.f, 2.f, 3.f); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1423 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1424 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1425 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1426 | EXPECT_TRUE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1427 | EXPECT_FALSE(b.has_error()); |
| 1428 | } |
| 1429 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1430 | TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Vector_WithIdent) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1431 | // vec3<f32>(a, b, c) -> false |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1432 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1433 | auto* t = vec3<f32>("a", "b", "c"); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1434 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1435 | Var("a", ast::StorageClass::kPrivate, ty.f32); |
| 1436 | Var("b", ast::StorageClass::kPrivate, ty.f32); |
| 1437 | Var("c", ast::StorageClass::kPrivate, ty.f32); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 1438 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1439 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1440 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1441 | EXPECT_FALSE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1442 | EXPECT_FALSE(b.has_error()); |
| 1443 | } |
| 1444 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1445 | TEST_F(SpvBuilderConstructorTest, |
| 1446 | IsConstructorConst_ArrayWithAllConstConstructors) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1447 | // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0)) |
| 1448 | // -> true |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1449 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1450 | auto* first = vec3<f32>(1.f, 2.f, 3.f); |
| 1451 | auto* second = vec3<f32>(1.f, 2.f, 3.f); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1452 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1453 | auto* t = Construct(ty.array(ty.vec3<f32>(), 2), first, second); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1454 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1455 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1456 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1457 | EXPECT_TRUE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1458 | EXPECT_FALSE(b.has_error()); |
| 1459 | } |
| 1460 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1461 | TEST_F(SpvBuilderConstructorTest, |
| 1462 | IsConstructorConst_VectorWith_TypeCastConstConstructors) { |
| 1463 | // vec2<f32>(f32(1), f32(2)) -> false |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1464 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1465 | auto* t = vec2<f32>(1, 2); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1466 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1467 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1468 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1469 | EXPECT_FALSE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1470 | EXPECT_FALSE(b.has_error()); |
| 1471 | } |
| 1472 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1473 | TEST_F(SpvBuilderConstructorTest, IsConstructorConst_WithTypeCastConstructor) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1474 | // vec3<f32>(f32(1), f32(2)) -> false |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1475 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1476 | auto* t = vec3<f32>(1, 2); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1477 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1478 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1479 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1480 | EXPECT_FALSE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1481 | EXPECT_FALSE(b.has_error()); |
| 1482 | } |
| 1483 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1484 | TEST_F(SpvBuilderConstructorTest, IsConstructorConst_BitCastScalars) { |
| 1485 | auto* t = vec2<u32>(1, 1); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1486 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1487 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1488 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1489 | EXPECT_FALSE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1490 | EXPECT_FALSE(b.has_error()); |
| 1491 | } |
| 1492 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1493 | TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Struct) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1494 | ast::StructMemberDecorationList decos; |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1495 | auto* s = create<ast::Struct>(ast::StructMemberList{ |
| 1496 | create<ast::StructMember>("a", ty.f32, decos), |
| 1497 | create<ast::StructMember>("b", ty.vec3<f32>(), decos), |
| 1498 | }); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1499 | ast::type::StructType s_type("my_struct", s); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1500 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1501 | auto* t = Construct(&s_type, 2.f, vec3<f32>(2.f, 2.f, 2.f)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1502 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1503 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1504 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1505 | EXPECT_TRUE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1506 | EXPECT_FALSE(b.has_error()); |
| 1507 | } |
| 1508 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1509 | TEST_F(SpvBuilderConstructorTest, |
| 1510 | IsConstructorConst_Struct_WithIdentSubExpression) { |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1511 | ast::StructMemberDecorationList decos; |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1512 | auto* s = create<ast::Struct>(ast::StructMemberList{ |
| 1513 | create<ast::StructMember>("a", ty.f32, decos), |
| 1514 | create<ast::StructMember>("b", ty.vec3<f32>(), decos), |
| 1515 | }); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1516 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 1517 | ast::type::StructType s_type("my_struct", s); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1518 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1519 | auto* t = Construct(&s_type, 2.f, "a", 2.f); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1520 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1521 | Var("a", ast::StorageClass::kPrivate, ty.f32); |
| 1522 | Var("b", ast::StorageClass::kPrivate, ty.f32); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1523 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1524 | ASSERT_TRUE(td.DetermineResultType(t)) << td.error(); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1525 | |
Ben Clayton | 0ae939b | 2020-11-17 17:53:38 +0000 | [diff] [blame] | 1526 | EXPECT_FALSE(b.is_constructor_const(t, false)); |
dan sinclair | 435916e | 2020-10-14 15:14:11 +0000 | [diff] [blame] | 1527 | EXPECT_FALSE(b.has_error()); |
| 1528 | } |
| 1529 | |
dan sinclair | 5cd08fd | 2020-03-30 20:01:38 +0000 | [diff] [blame] | 1530 | } // namespace |
| 1531 | } // namespace spirv |
| 1532 | } // namespace writer |
| 1533 | } // namespace tint |