blob: 6b25bf39e8ac7228ec4dbe8b3f3c200aa112b0b4 [file] [log] [blame]
dan sinclair5cd08fd2020-03-30 20:01:38 +00001// 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
dan sinclair5cd08fd2020-03-30 20:01:38 +000015#include "src/writer/spirv/spv_dump.h"
dan sinclair196e0972020-11-13 18:13:24 +000016#include "src/writer/spirv/test_helper.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000017
18namespace tint {
19namespace writer {
20namespace spirv {
21namespace {
22
Ben Clayton0ae939b2020-11-17 17:53:38 +000023using SpvBuilderConstructorTest = TestHelper;
dan sinclair5cd08fd2020-03-30 20:01:38 +000024
Ben Clayton0ae939b2020-11-17 17:53:38 +000025TEST_F(SpvBuilderConstructorTest, Const) {
26 auto* c = Expr(42.2f);
Ben Clayton238de882021-04-19 14:26:00 +000027 WrapInFunction(c);
dan sinclair5cd08fd2020-03-30 20:01:38 +000028
Ben Claytonf12054e2021-01-21 16:15:00 +000029 spirv::Builder& b = Build();
30
Ben Clayton0ae939b2020-11-17 17:53:38 +000031 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, c, true), 2u);
dan sinclair5cd08fd2020-03-30 20:01:38 +000032 ASSERT_FALSE(b.has_error()) << b.error();
33
34 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
35%2 = OpConstant %1 42.2000008
36)");
37}
38
David Neto035c5242020-12-15 20:29:08 +000039TEST_F(SpvBuilderConstructorTest, Type_WithCasts_OutsideFunction_IsError) {
40 auto* t = Construct<f32>(Construct<u32>(1));
Ben Clayton401b96b2021-02-03 17:19:59 +000041 WrapInFunction(t);
David Neto035c5242020-12-15 20:29:08 +000042
Ben Claytonf12054e2021-01-21 16:15:00 +000043 spirv::Builder& b = Build();
44
David Neto035c5242020-12-15 20:29:08 +000045 EXPECT_EQ(b.GenerateExpression(t), 0u);
46 EXPECT_TRUE(b.has_error()) << b.error();
47 EXPECT_EQ(b.error(),
48 "Internal error: trying to add SPIR-V instruction 124 outside a "
49 "function");
50}
51
Ben Clayton0ae939b2020-11-17 17:53:38 +000052TEST_F(SpvBuilderConstructorTest, Type) {
53 auto* t = vec3<f32>(1.0f, 1.0f, 3.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +000054 WrapInFunction(t);
dan sinclair5cd08fd2020-03-30 20:01:38 +000055
Ben Claytonf12054e2021-01-21 16:15:00 +000056 spirv::Builder& b = Build();
57
Ben Clayton0ae939b2020-11-17 17:53:38 +000058 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 5u);
dan sinclair5cd08fd2020-03-30 20:01:38 +000059 ASSERT_FALSE(b.has_error()) << b.error();
60
61 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
62%1 = OpTypeVector %2 3
63%3 = OpConstant %2 1
64%4 = OpConstant %2 3
dan sinclair73e1ef82020-03-31 21:15:51 +000065%5 = OpConstantComposite %1 %3 %3 %4
dan sinclair5cd08fd2020-03-30 20:01:38 +000066)");
67}
68
Ben Clayton0ae939b2020-11-17 17:53:38 +000069TEST_F(SpvBuilderConstructorTest, Type_WithCasts) {
70 auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(1));
Ben Clayton401b96b2021-02-03 17:19:59 +000071 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +000072
Ben Claytonf12054e2021-01-21 16:15:00 +000073 spirv::Builder& b = Build();
74
dan sinclair435916e2020-10-14 15:14:11 +000075 b.push_function(Function{});
76
Ben Clayton0ae939b2020-11-17 17:53:38 +000077 EXPECT_EQ(b.GenerateExpression(t), 7u);
dan sinclair435916e2020-10-14 15:14:11 +000078 ASSERT_FALSE(b.has_error()) << b.error();
79
80 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
81%1 = OpTypeVector %2 2
82%4 = OpTypeInt 32 1
83%5 = OpConstant %4 1
84)");
85 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
86 R"(%3 = OpConvertSToF %2 %5
87%6 = OpConvertSToF %2 %5
88%7 = OpCompositeConstruct %1 %3 %6
89)");
90}
91
Ben Clayton0ae939b2020-11-17 17:53:38 +000092TEST_F(SpvBuilderConstructorTest, Type_WithAlias) {
dan sinclair3c025922020-09-24 14:38:44 +000093 // type Int = i32
Ben Clayton0ae939b2020-11-17 17:53:38 +000094 // cast<Int>(2.3f)
dan sinclair3c025922020-09-24 14:38:44 +000095
Ben Claytone204f272021-04-22 14:40:23 +000096 auto alias = ty.alias("Int", ty.i32());
Ben Clayton1637cbb2021-01-05 15:44:39 +000097 auto* cast = Construct(alias, 2.3f);
Ben Clayton401b96b2021-02-03 17:19:59 +000098 WrapInFunction(cast);
dan sinclair5b853ee2020-06-22 20:44:27 +000099
Ben Claytonf12054e2021-01-21 16:15:00 +0000100 spirv::Builder& b = Build();
101
dan sinclair5b853ee2020-06-22 20:44:27 +0000102 b.push_function(Function{});
Ben Claytone6e70412021-01-05 15:29:29 +0000103 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair5b853ee2020-06-22 20:44:27 +0000104
dan sinclair3c025922020-09-24 14:38:44 +0000105 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
106%3 = OpTypeFloat 32
107%4 = OpConstant %3 2.29999995
108)");
109 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
110 R"(%1 = OpConvertFToS %2 %4
dan sinclair5b853ee2020-06-22 20:44:27 +0000111)");
112}
113
Ben Clayton0ae939b2020-11-17 17:53:38 +0000114TEST_F(SpvBuilderConstructorTest, Type_IdentifierExpression_Param) {
Ben Clayton37571bc2021-02-16 23:57:01 +0000115 auto* var = Global("ident", ty.f32(), ast::StorageClass::kFunction);
dan sinclair361e4572020-04-24 00:41:12 +0000116
Ben Clayton0ae939b2020-11-17 17:53:38 +0000117 auto* t = vec2<f32>(1.0f, "ident");
Ben Clayton401b96b2021-02-03 17:19:59 +0000118 WrapInFunction(t);
dan sinclair361e4572020-04-24 00:41:12 +0000119
Ben Claytonf12054e2021-01-21 16:15:00 +0000120 spirv::Builder& b = Build();
121
dan sinclair361e4572020-04-24 00:41:12 +0000122 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +0000123 ASSERT_TRUE(b.GenerateFunctionVariable(var)) << b.error();
dan sinclair361e4572020-04-24 00:41:12 +0000124
Ben Clayton0ae939b2020-11-17 17:53:38 +0000125 EXPECT_EQ(b.GenerateExpression(t), 8u);
dan sinclair361e4572020-04-24 00:41:12 +0000126 ASSERT_FALSE(b.has_error()) << b.error();
127
128 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
129%2 = OpTypePointer Function %3
dan sinclair2287f332020-05-05 14:21:19 +0000130%4 = OpConstantNull %3
131%5 = OpTypeVector %3 2
132%6 = OpConstant %3 1
dan sinclair361e4572020-04-24 00:41:12 +0000133)");
134 EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
dan sinclair2287f332020-05-05 14:21:19 +0000135 R"(%1 = OpVariable %2 Function %4
dan sinclair361e4572020-04-24 00:41:12 +0000136)");
137
138 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
dan sinclair2287f332020-05-05 14:21:19 +0000139 R"(%7 = OpLoad %3 %1
140%8 = OpCompositeConstruct %5 %6 %7
dan sinclair361e4572020-04-24 00:41:12 +0000141)");
142}
143
Ben Clayton0ae939b2020-11-17 17:53:38 +0000144TEST_F(SpvBuilderConstructorTest, Vector_Bitcast_Params) {
Arman Uguray3549e2e2021-03-15 21:21:33 +0000145 auto* t = vec2<u32>(Construct<u32>(1), Construct<u32>(1));
Ben Clayton401b96b2021-02-03 17:19:59 +0000146 WrapInFunction(t);
dan sinclaira388d562020-10-07 14:06:28 +0000147
Ben Claytonf12054e2021-01-21 16:15:00 +0000148 spirv::Builder& b = Build();
149
dan sinclaira388d562020-10-07 14:06:28 +0000150 b.push_function(Function{});
151
Ben Clayton0ae939b2020-11-17 17:53:38 +0000152 EXPECT_EQ(b.GenerateExpression(t), 7u);
dan sinclaira388d562020-10-07 14:06:28 +0000153 ASSERT_FALSE(b.has_error()) << b.error();
154
155 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
156%1 = OpTypeVector %2 2
Arman Uguray3549e2e2021-03-15 21:21:33 +0000157%4 = OpTypeInt 32 1
158%5 = OpConstant %4 1
dan sinclaira388d562020-10-07 14:06:28 +0000159)");
160
161 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
Arman Uguray3549e2e2021-03-15 21:21:33 +0000162 R"(%3 = OpBitcast %2 %5
163%6 = OpBitcast %2 %5
164%7 = OpCompositeConstruct %1 %3 %6
dan sinclaira388d562020-10-07 14:06:28 +0000165)");
166}
167
Ben Clayton0ae939b2020-11-17 17:53:38 +0000168TEST_F(SpvBuilderConstructorTest, Type_NonConst_Value_Fails) {
Ben Claytone6e70412021-01-05 15:29:29 +0000169 auto* rel = create<ast::BinaryExpression>(ast::BinaryOp::kAdd, Expr(3.0f),
170 Expr(3.0f));
dan sinclair5cd08fd2020-03-30 20:01:38 +0000171
Ben Clayton0ae939b2020-11-17 17:53:38 +0000172 auto* t = vec2<f32>(1.0f, rel);
Ben Clayton401b96b2021-02-03 17:19:59 +0000173 WrapInFunction(t);
dan sinclair5cd08fd2020-03-30 20:01:38 +0000174
Ben Claytonf12054e2021-01-21 16:15:00 +0000175 spirv::Builder& b = Build();
176
Ben Clayton0ae939b2020-11-17 17:53:38 +0000177 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 0u);
dan sinclair5cd08fd2020-03-30 20:01:38 +0000178 EXPECT_TRUE(b.has_error());
dan sinclaira322f5d2020-03-30 22:46:06 +0000179 EXPECT_EQ(b.error(), R"(constructor must be a constant expression)");
dan sinclair5cd08fd2020-03-30 20:01:38 +0000180}
181
Ben Clayton0ae939b2020-11-17 17:53:38 +0000182TEST_F(SpvBuilderConstructorTest, Type_Bool_With_Bool) {
Ben Clayton1637cbb2021-01-05 15:44:39 +0000183 auto* cast = Construct<bool>(true);
Ben Clayton401b96b2021-02-03 17:19:59 +0000184 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000185
Ben Claytonf12054e2021-01-21 16:15:00 +0000186 spirv::Builder& b = Build();
187
dan sinclair3c025922020-09-24 14:38:44 +0000188 b.push_function(Function{});
189
Ben Claytone6e70412021-01-05 15:29:29 +0000190 EXPECT_EQ(b.GenerateExpression(cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000191 ASSERT_FALSE(b.has_error()) << b.error();
192
193 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
194%3 = OpConstantTrue %2
195)");
David Neto6cd6f742020-11-23 16:34:35 +0000196 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000197}
198
Ben Clayton0ae939b2020-11-17 17:53:38 +0000199TEST_F(SpvBuilderConstructorTest, Type_I32_With_I32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +0000200 auto* cast = Construct<i32>(2);
Ben Clayton401b96b2021-02-03 17:19:59 +0000201 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000202
Ben Claytonf12054e2021-01-21 16:15:00 +0000203 spirv::Builder& b = Build();
204
dan sinclair3c025922020-09-24 14:38:44 +0000205 b.push_function(Function{});
Ben Claytone6e70412021-01-05 15:29:29 +0000206 EXPECT_EQ(b.GenerateExpression(cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000207
208 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
209%3 = OpConstant %2 2
210)");
David Neto6cd6f742020-11-23 16:34:35 +0000211 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000212}
213
Ben Clayton0ae939b2020-11-17 17:53:38 +0000214TEST_F(SpvBuilderConstructorTest, Type_U32_With_U32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +0000215 auto* cast = Construct<u32>(2u);
Ben Clayton401b96b2021-02-03 17:19:59 +0000216 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000217
Ben Claytonf12054e2021-01-21 16:15:00 +0000218 spirv::Builder& b = Build();
219
dan sinclair3c025922020-09-24 14:38:44 +0000220 b.push_function(Function{});
Ben Claytone6e70412021-01-05 15:29:29 +0000221 EXPECT_EQ(b.GenerateExpression(cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000222
223 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
224%3 = OpConstant %2 2
225)");
David Neto6cd6f742020-11-23 16:34:35 +0000226 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000227}
228
Ben Clayton0ae939b2020-11-17 17:53:38 +0000229TEST_F(SpvBuilderConstructorTest, Type_F32_With_F32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +0000230 auto* cast = Construct<f32>(2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000231 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000232
Ben Claytonf12054e2021-01-21 16:15:00 +0000233 spirv::Builder& b = Build();
234
dan sinclair3c025922020-09-24 14:38:44 +0000235 b.push_function(Function{});
Ben Claytone6e70412021-01-05 15:29:29 +0000236 EXPECT_EQ(b.GenerateExpression(cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000237
238 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
239%3 = OpConstant %2 2
240)");
David Neto6cd6f742020-11-23 16:34:35 +0000241 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000242}
243
Ben Clayton0ae939b2020-11-17 17:53:38 +0000244TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_F32_F32) {
245 auto* cast = vec2<f32>(2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000246 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000247
Ben Claytonf12054e2021-01-21 16:15:00 +0000248 spirv::Builder& b = Build();
249
dan sinclair3c025922020-09-24 14:38:44 +0000250 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000251 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000252
253 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
254%1 = OpTypeVector %2 2
255%3 = OpConstant %2 2
256%4 = OpConstantComposite %1 %3 %3
257)");
258}
259
David Neto6cd6f742020-11-23 16:34:35 +0000260TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_Vec2) {
261 auto* value = vec2<f32>(2.0f, 2.0f);
262 auto* cast = vec2<f32>(value);
Ben Clayton401b96b2021-02-03 17:19:59 +0000263 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000264
Ben Claytonf12054e2021-01-21 16:15:00 +0000265 spirv::Builder& b = Build();
266
David Neto6cd6f742020-11-23 16:34:35 +0000267 b.push_function(Function{});
268 EXPECT_EQ(b.GenerateExpression(cast), 5u);
269
270 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
271%2 = OpTypeVector %3 2
272%4 = OpConstant %3 2
273%5 = OpConstantComposite %2 %4 %4
274)");
275 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
276}
277
Ben Clayton0ae939b2020-11-17 17:53:38 +0000278TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_F32_F32) {
279 auto* cast = vec3<f32>(2.0f, 2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000280 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000281
Ben Claytonf12054e2021-01-21 16:15:00 +0000282 spirv::Builder& b = Build();
283
dan sinclair3c025922020-09-24 14:38:44 +0000284 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000285 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000286
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 Clayton0ae939b2020-11-17 17:53:38 +0000294TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_Vec2) {
295 auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000296 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000297
Ben Claytonf12054e2021-01-21 16:15:00 +0000298 spirv::Builder& b = Build();
299
dan sinclair3c025922020-09-24 14:38:44 +0000300 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000301 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000302
303 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
304%1 = OpTypeVector %2 3
305%3 = OpConstant %2 2
306%4 = OpTypeVector %2 2
307%5 = OpConstantComposite %4 %3 %3
308)");
309 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
310 R"(%6 = OpCompositeExtract %2 %5 0
311%7 = OpCompositeExtract %2 %5 1
312%8 = OpCompositeConstruct %1 %3 %6 %7
313)");
314}
315
Ben Clayton0ae939b2020-11-17 17:53:38 +0000316TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec2_F32) {
317 auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000318 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000319
Ben Claytonf12054e2021-01-21 16:15:00 +0000320 spirv::Builder& b = Build();
321
dan sinclair3c025922020-09-24 14:38:44 +0000322 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000323 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000324
325 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
326%1 = OpTypeVector %2 3
327%3 = OpTypeVector %2 2
328%4 = OpConstant %2 2
329%5 = OpConstantComposite %3 %4 %4
330)");
331 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
332 R"(%6 = OpCompositeExtract %2 %5 0
333%7 = OpCompositeExtract %2 %5 1
334%8 = OpCompositeConstruct %1 %6 %7 %4
335)");
336}
337
David Neto6cd6f742020-11-23 16:34:35 +0000338TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec3) {
339 auto* value = vec3<f32>(2.0f, 2.0f, 2.0f);
340 auto* cast = vec3<f32>(value);
Ben Clayton401b96b2021-02-03 17:19:59 +0000341 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000342
Ben Claytonf12054e2021-01-21 16:15:00 +0000343 spirv::Builder& b = Build();
344
David Neto6cd6f742020-11-23 16:34:35 +0000345 b.push_function(Function{});
346 EXPECT_EQ(b.GenerateExpression(cast), 5u);
347
348 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
349%2 = OpTypeVector %3 3
350%4 = OpConstant %3 2
351%5 = OpConstantComposite %2 %4 %4 %4
352)");
353 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
354}
355
Ben Clayton0ae939b2020-11-17 17:53:38 +0000356TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_F32_F32) {
357 auto* cast = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000358 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000359
Ben Claytonf12054e2021-01-21 16:15:00 +0000360 spirv::Builder& b = Build();
361
dan sinclair3c025922020-09-24 14:38:44 +0000362 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000363 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000364
365 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
366%1 = OpTypeVector %2 4
367%3 = OpConstant %2 2
368%4 = OpConstantComposite %1 %3 %3 %3 %3
369)");
370}
371
Ben Clayton0ae939b2020-11-17 17:53:38 +0000372TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_Vec2) {
373 auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000374 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000375
Ben Claytonf12054e2021-01-21 16:15:00 +0000376 spirv::Builder& b = Build();
377
dan sinclair3c025922020-09-24 14:38:44 +0000378 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000379 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000380
381 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
382%1 = OpTypeVector %2 4
383%3 = OpConstant %2 2
384%4 = OpTypeVector %2 2
385%5 = OpConstantComposite %4 %3 %3
386)");
387 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
388 R"(%6 = OpCompositeExtract %2 %5 0
389%7 = OpCompositeExtract %2 %5 1
390%8 = OpCompositeConstruct %1 %3 %3 %6 %7
391)");
392}
393
Ben Clayton0ae939b2020-11-17 17:53:38 +0000394TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec2_F32) {
395 auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000396 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000397
Ben Claytonf12054e2021-01-21 16:15:00 +0000398 spirv::Builder& b = Build();
399
dan sinclair3c025922020-09-24 14:38:44 +0000400 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000401 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000402
403 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
404%1 = OpTypeVector %2 4
405%3 = OpConstant %2 2
406%4 = OpTypeVector %2 2
407%5 = OpConstantComposite %4 %3 %3
408)");
409 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
410 R"(%6 = OpCompositeExtract %2 %5 0
411%7 = OpCompositeExtract %2 %5 1
412%8 = OpCompositeConstruct %1 %3 %6 %7 %3
413)");
414}
415
Ben Clayton0ae939b2020-11-17 17:53:38 +0000416TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_F32_F32) {
417 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000418 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000419
Ben Claytonf12054e2021-01-21 16:15:00 +0000420 spirv::Builder& b = Build();
421
dan sinclair3c025922020-09-24 14:38:44 +0000422 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000423 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000424
425 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
426%1 = OpTypeVector %2 4
427%3 = OpTypeVector %2 2
428%4 = OpConstant %2 2
429%5 = OpConstantComposite %3 %4 %4
430)");
431 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
432 R"(%6 = OpCompositeExtract %2 %5 0
433%7 = OpCompositeExtract %2 %5 1
434%8 = OpCompositeConstruct %1 %6 %7 %4 %4
435)");
436}
437
Ben Clayton0ae939b2020-11-17 17:53:38 +0000438TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_Vec2) {
439 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000440 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000441
Ben Claytonf12054e2021-01-21 16:15:00 +0000442 spirv::Builder& b = Build();
443
dan sinclair3c025922020-09-24 14:38:44 +0000444 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000445 EXPECT_EQ(b.GenerateExpression(cast), 10u);
dan sinclair3c025922020-09-24 14:38:44 +0000446
447 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
448%1 = OpTypeVector %2 4
449%3 = OpTypeVector %2 2
450%4 = OpConstant %2 2
451%5 = OpConstantComposite %3 %4 %4
452)");
453 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
454 R"(%6 = OpCompositeExtract %2 %5 0
455%7 = OpCompositeExtract %2 %5 1
456%8 = OpCompositeExtract %2 %5 0
457%9 = OpCompositeExtract %2 %5 1
458%10 = OpCompositeConstruct %1 %6 %7 %8 %9
459)");
460}
461
Ben Clayton0ae939b2020-11-17 17:53:38 +0000462TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec3) {
463 auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000464 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000465
Ben Claytonf12054e2021-01-21 16:15:00 +0000466 spirv::Builder& b = Build();
467
dan sinclair3c025922020-09-24 14:38:44 +0000468 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000469 EXPECT_EQ(b.GenerateExpression(cast), 9u);
dan sinclair3c025922020-09-24 14:38:44 +0000470
471 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
472%1 = OpTypeVector %2 4
473%3 = OpConstant %2 2
474%4 = OpTypeVector %2 3
475%5 = OpConstantComposite %4 %3 %3 %3
476)");
477 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
478 R"(%6 = OpCompositeExtract %2 %5 0
479%7 = OpCompositeExtract %2 %5 1
480%8 = OpCompositeExtract %2 %5 2
481%9 = OpCompositeConstruct %1 %3 %6 %7 %8
482)");
483}
484
Ben Clayton0ae939b2020-11-17 17:53:38 +0000485TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec3_F32) {
486 auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000487 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000488
Ben Claytonf12054e2021-01-21 16:15:00 +0000489 spirv::Builder& b = Build();
490
dan sinclair3c025922020-09-24 14:38:44 +0000491 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000492 EXPECT_EQ(b.GenerateExpression(cast), 9u);
dan sinclair3c025922020-09-24 14:38:44 +0000493
494 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
495%1 = OpTypeVector %2 4
496%3 = OpTypeVector %2 3
497%4 = OpConstant %2 2
498%5 = OpConstantComposite %3 %4 %4 %4
499)");
500 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
501 R"(%6 = OpCompositeExtract %2 %5 0
502%7 = OpCompositeExtract %2 %5 1
503%8 = OpCompositeExtract %2 %5 2
504%9 = OpCompositeConstruct %1 %6 %7 %8 %4
505)");
506}
507
David Neto6cd6f742020-11-23 16:34:35 +0000508TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec4) {
509 auto* value = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f);
510 auto* cast = vec4<f32>(value);
Ben Clayton401b96b2021-02-03 17:19:59 +0000511 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000512
Ben Claytonf12054e2021-01-21 16:15:00 +0000513 spirv::Builder& b = Build();
514
David Neto6cd6f742020-11-23 16:34:35 +0000515 b.push_function(Function{});
516 EXPECT_EQ(b.GenerateExpression(cast), 5u);
517
518 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
519%2 = OpTypeVector %3 4
520%4 = OpConstant %3 2
521%5 = OpConstantComposite %2 %4 %4 %4 %4
522)");
523 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
524}
525
526TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec2_With_Vec2) {
527 auto* cast = vec2<f32>(vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000528 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000529
Ben Claytonf12054e2021-01-21 16:15:00 +0000530 spirv::Builder& b = Build();
531
David Neto6cd6f742020-11-23 16:34:35 +0000532 b.push_function(Function{});
533 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
534
535 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
536%2 = OpTypeVector %3 2
537%4 = OpConstant %3 2
538%5 = OpConstantComposite %2 %4 %4
539)");
540}
541
542TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec3) {
543 auto* cast = vec3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000544 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000545
Ben Claytonf12054e2021-01-21 16:15:00 +0000546 spirv::Builder& b = Build();
547
David Neto6cd6f742020-11-23 16:34:35 +0000548 b.push_function(Function{});
549 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
550
551 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
552%2 = OpTypeVector %3 3
553%4 = OpConstant %3 2
554%5 = OpConstantComposite %2 %4 %4 %4
555)");
556}
557
558TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec4) {
559 auto* cast = vec4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000560 WrapInFunction(cast);
David Neto6cd6f742020-11-23 16:34:35 +0000561
Ben Claytonf12054e2021-01-21 16:15:00 +0000562 spirv::Builder& b = Build();
563
David Neto6cd6f742020-11-23 16:34:35 +0000564 b.push_function(Function{});
565 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
566
567 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
568%2 = OpTypeVector %3 4
569%4 = OpConstant %3 2
570%5 = OpConstantComposite %2 %4 %4 %4 %4
571)");
572}
573
Ben Clayton0ae939b2020-11-17 17:53:38 +0000574TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_F32_Vec2) {
575 auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000576 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000577
Ben Claytonf12054e2021-01-21 16:15:00 +0000578 spirv::Builder& b = Build();
579
dan sinclair3c025922020-09-24 14:38:44 +0000580 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000581 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000582
583 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
584%1 = OpTypeVector %2 3
585%3 = OpConstant %2 2
586%4 = OpTypeVector %2 2
587%5 = OpConstantComposite %4 %3 %3
588%7 = OpTypeInt 32 0
589%8 = OpConstant %7 0
590%6 = OpSpecConstantOp %2 CompositeExtract %5 8
591%10 = OpConstant %7 1
592%9 = OpSpecConstantOp %2 CompositeExtract %5 10
593%11 = OpSpecConstantComposite %1 %3 %6 %9
594)");
595}
596
Ben Clayton0ae939b2020-11-17 17:53:38 +0000597TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec2_F32) {
598 auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000599 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000600
Ben Claytonf12054e2021-01-21 16:15:00 +0000601 spirv::Builder& b = Build();
602
dan sinclair3c025922020-09-24 14:38:44 +0000603 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000604 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000605
606 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
607%1 = OpTypeVector %2 3
608%3 = OpTypeVector %2 2
609%4 = OpConstant %2 2
610%5 = OpConstantComposite %3 %4 %4
611%7 = OpTypeInt 32 0
612%8 = OpConstant %7 0
613%6 = OpSpecConstantOp %2 CompositeExtract %5 8
614%10 = OpConstant %7 1
615%9 = OpSpecConstantOp %2 CompositeExtract %5 10
616%11 = OpSpecConstantComposite %1 %6 %9 %4
617)");
618}
619
Ben Clayton0ae939b2020-11-17 17:53:38 +0000620TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_F32_Vec2) {
621 auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000622 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000623
Ben Claytonf12054e2021-01-21 16:15:00 +0000624 spirv::Builder& b = Build();
625
dan sinclair3c025922020-09-24 14:38:44 +0000626 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000627 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000628
629 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
630%1 = OpTypeVector %2 4
631%3 = OpConstant %2 2
632%4 = OpTypeVector %2 2
633%5 = OpConstantComposite %4 %3 %3
634%7 = OpTypeInt 32 0
635%8 = OpConstant %7 0
636%6 = OpSpecConstantOp %2 CompositeExtract %5 8
637%10 = OpConstant %7 1
638%9 = OpSpecConstantOp %2 CompositeExtract %5 10
639%11 = OpSpecConstantComposite %1 %3 %3 %6 %9
640)");
641}
642
Ben Clayton0ae939b2020-11-17 17:53:38 +0000643TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec2_F32) {
644 auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000645 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000646
Ben Claytonf12054e2021-01-21 16:15:00 +0000647 spirv::Builder& b = Build();
648
dan sinclair3c025922020-09-24 14:38:44 +0000649 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000650 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000651
652 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
653%1 = OpTypeVector %2 4
654%3 = OpConstant %2 2
655%4 = OpTypeVector %2 2
656%5 = OpConstantComposite %4 %3 %3
657%7 = OpTypeInt 32 0
658%8 = OpConstant %7 0
659%6 = OpSpecConstantOp %2 CompositeExtract %5 8
660%10 = OpConstant %7 1
661%9 = OpSpecConstantOp %2 CompositeExtract %5 10
662%11 = OpSpecConstantComposite %1 %3 %6 %9 %3
663)");
664}
665
Ben Clayton0ae939b2020-11-17 17:53:38 +0000666TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_F32_F32) {
667 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000668 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000669
Ben Claytonf12054e2021-01-21 16:15:00 +0000670 spirv::Builder& b = Build();
671
dan sinclair3c025922020-09-24 14:38:44 +0000672 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000673 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000674
675 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
676%1 = OpTypeVector %2 4
677%3 = OpTypeVector %2 2
678%4 = OpConstant %2 2
679%5 = OpConstantComposite %3 %4 %4
680%7 = OpTypeInt 32 0
681%8 = OpConstant %7 0
682%6 = OpSpecConstantOp %2 CompositeExtract %5 8
683%10 = OpConstant %7 1
684%9 = OpSpecConstantOp %2 CompositeExtract %5 10
685%11 = OpSpecConstantComposite %1 %6 %9 %4 %4
686)");
687}
688
Ben Clayton0ae939b2020-11-17 17:53:38 +0000689TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_Vec2) {
690 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000691 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000692
Ben Claytonf12054e2021-01-21 16:15:00 +0000693 spirv::Builder& b = Build();
694
dan sinclair3c025922020-09-24 14:38:44 +0000695 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000696 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000697
698 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
699%1 = OpTypeVector %2 4
700%3 = OpTypeVector %2 2
701%4 = OpConstant %2 2
702%5 = OpConstantComposite %3 %4 %4
703%7 = OpTypeInt 32 0
704%8 = OpConstant %7 0
705%6 = OpSpecConstantOp %2 CompositeExtract %5 8
706%10 = OpConstant %7 1
707%9 = OpSpecConstantOp %2 CompositeExtract %5 10
708%11 = OpSpecConstantOp %2 CompositeExtract %5 8
709%12 = OpSpecConstantOp %2 CompositeExtract %5 10
710%13 = OpSpecConstantComposite %1 %6 %9 %11 %12
711)");
712}
713
Ben Clayton0ae939b2020-11-17 17:53:38 +0000714TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec3) {
715 auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000716 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000717
Ben Claytonf12054e2021-01-21 16:15:00 +0000718 spirv::Builder& b = Build();
719
dan sinclair3c025922020-09-24 14:38:44 +0000720 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000721 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000722
723 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
724%1 = OpTypeVector %2 4
725%3 = OpConstant %2 2
726%4 = OpTypeVector %2 3
727%5 = OpConstantComposite %4 %3 %3 %3
728%7 = OpTypeInt 32 0
729%8 = OpConstant %7 0
730%6 = OpSpecConstantOp %2 CompositeExtract %5 8
731%10 = OpConstant %7 1
732%9 = OpSpecConstantOp %2 CompositeExtract %5 10
733%12 = OpConstant %7 2
734%11 = OpSpecConstantOp %2 CompositeExtract %5 12
735%13 = OpSpecConstantComposite %1 %3 %6 %9 %11
736)");
737}
738
Ben Clayton0ae939b2020-11-17 17:53:38 +0000739TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec3_F32) {
740 auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000741 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000742
Ben Claytonf12054e2021-01-21 16:15:00 +0000743 spirv::Builder& b = Build();
744
dan sinclair3c025922020-09-24 14:38:44 +0000745 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000746 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000747
748 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
749%1 = OpTypeVector %2 4
750%3 = OpTypeVector %2 3
751%4 = OpConstant %2 2
752%5 = OpConstantComposite %3 %4 %4 %4
753%7 = OpTypeInt 32 0
754%8 = OpConstant %7 0
755%6 = OpSpecConstantOp %2 CompositeExtract %5 8
756%10 = OpConstant %7 1
757%9 = OpSpecConstantOp %2 CompositeExtract %5 10
758%12 = OpConstant %7 2
759%11 = OpSpecConstantOp %2 CompositeExtract %5 12
760%13 = OpSpecConstantComposite %1 %6 %9 %11 %4
761)");
762}
763
Ben Clayton0ae939b2020-11-17 17:53:38 +0000764TEST_F(SpvBuilderConstructorTest, Type_Mat2x2_With_Vec2_Vec2) {
765 auto* cast = mat2x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000766 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000767
Ben Claytonf12054e2021-01-21 16:15:00 +0000768 spirv::Builder& b = Build();
769
dan sinclaird6e063d2020-09-24 16:38:35 +0000770 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000771 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000772
773 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
774%2 = OpTypeVector %3 2
775%1 = OpTypeMatrix %2 2
776%4 = OpConstant %3 2
777%5 = OpConstantComposite %2 %4 %4
778%6 = OpConstantComposite %1 %5 %5
779)");
dan sinclair3c025922020-09-24 14:38:44 +0000780}
781
Ben Clayton0ae939b2020-11-17 17:53:38 +0000782TEST_F(SpvBuilderConstructorTest, Type_Mat3x2_With_Vec2_Vec2_Vec2) {
783 auto* cast = mat3x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f),
784 vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000785 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000786
Ben Claytonf12054e2021-01-21 16:15:00 +0000787 spirv::Builder& b = Build();
788
dan sinclaird6e063d2020-09-24 16:38:35 +0000789 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000790 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000791
792 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
793%2 = OpTypeVector %3 2
794%1 = OpTypeMatrix %2 3
795%4 = OpConstant %3 2
796%5 = OpConstantComposite %2 %4 %4
797%6 = OpConstantComposite %1 %5 %5 %5
798)");
dan sinclair3c025922020-09-24 14:38:44 +0000799}
800
Ben Clayton0ae939b2020-11-17 17:53:38 +0000801TEST_F(SpvBuilderConstructorTest, Type_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) {
802 auto* cast = mat4x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f),
803 vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000804 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000805
Ben Claytonf12054e2021-01-21 16:15:00 +0000806 spirv::Builder& b = Build();
807
dan sinclaird6e063d2020-09-24 16:38:35 +0000808 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000809 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000810
811 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
812%2 = OpTypeVector %3 2
813%1 = OpTypeMatrix %2 4
814%4 = OpConstant %3 2
815%5 = OpConstantComposite %2 %4 %4
816%6 = OpConstantComposite %1 %5 %5 %5 %5
817)");
dan sinclair3c025922020-09-24 14:38:44 +0000818}
819
Ben Clayton0ae939b2020-11-17 17:53:38 +0000820TEST_F(SpvBuilderConstructorTest, Type_Mat2x3_With_Vec3_Vec3) {
821 auto* cast =
822 mat2x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000823 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000824
Ben Claytonf12054e2021-01-21 16:15:00 +0000825 spirv::Builder& b = Build();
826
dan sinclaird6e063d2020-09-24 16:38:35 +0000827 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000828 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000829
830 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
831%2 = OpTypeVector %3 3
832%1 = OpTypeMatrix %2 2
833%4 = OpConstant %3 2
834%5 = OpConstantComposite %2 %4 %4 %4
835%6 = OpConstantComposite %1 %5 %5
836)");
dan sinclair3c025922020-09-24 14:38:44 +0000837}
838
Ben Clayton0ae939b2020-11-17 17:53:38 +0000839TEST_F(SpvBuilderConstructorTest, Type_Mat3x3_With_Vec3_Vec3_Vec3) {
840 auto* cast =
841 mat3x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f),
842 vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000843 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000844
Ben Claytonf12054e2021-01-21 16:15:00 +0000845 spirv::Builder& b = Build();
846
dan sinclaird6e063d2020-09-24 16:38:35 +0000847 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000848 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000849
850 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
851%2 = OpTypeVector %3 3
852%1 = OpTypeMatrix %2 3
853%4 = OpConstant %3 2
854%5 = OpConstantComposite %2 %4 %4 %4
855%6 = OpConstantComposite %1 %5 %5 %5
856)");
dan sinclair3c025922020-09-24 14:38:44 +0000857}
858
Ben Clayton0ae939b2020-11-17 17:53:38 +0000859TEST_F(SpvBuilderConstructorTest, Type_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) {
860 auto* cast =
861 mat4x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f),
862 vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000863 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000864
Ben Claytonf12054e2021-01-21 16:15:00 +0000865 spirv::Builder& b = Build();
866
dan sinclaird6e063d2020-09-24 16:38:35 +0000867 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000868 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000869
870 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
871%2 = OpTypeVector %3 3
872%1 = OpTypeMatrix %2 4
873%4 = OpConstant %3 2
874%5 = OpConstantComposite %2 %4 %4 %4
875%6 = OpConstantComposite %1 %5 %5 %5 %5
876)");
dan sinclair3c025922020-09-24 14:38:44 +0000877}
878
Ben Clayton0ae939b2020-11-17 17:53:38 +0000879TEST_F(SpvBuilderConstructorTest, Type_Mat2x4_With_Vec4_Vec4) {
880 auto* cast = mat2x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
881 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000882 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000883
Ben Claytonf12054e2021-01-21 16:15:00 +0000884 spirv::Builder& b = Build();
885
dan sinclaird6e063d2020-09-24 16:38:35 +0000886 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000887 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000888
889 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
890%2 = OpTypeVector %3 4
891%1 = OpTypeMatrix %2 2
892%4 = OpConstant %3 2
893%5 = OpConstantComposite %2 %4 %4 %4 %4
894%6 = OpConstantComposite %1 %5 %5
895)");
dan sinclair3c025922020-09-24 14:38:44 +0000896}
897
Ben Clayton0ae939b2020-11-17 17:53:38 +0000898TEST_F(SpvBuilderConstructorTest, Type_Mat3x4_With_Vec4_Vec4_Vec4) {
899 auto* cast = mat3x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
900 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
901 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000902 WrapInFunction(cast);
dan sinclaird6e063d2020-09-24 16:38:35 +0000903
Ben Claytonf12054e2021-01-21 16:15:00 +0000904 spirv::Builder& b = Build();
905
dan sinclaird6e063d2020-09-24 16:38:35 +0000906 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000907 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000908
909 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
910%2 = OpTypeVector %3 4
911%1 = OpTypeMatrix %2 3
912%4 = OpConstant %3 2
913%5 = OpConstantComposite %2 %4 %4 %4 %4
914%6 = OpConstantComposite %1 %5 %5 %5
915)");
dan sinclair3c025922020-09-24 14:38:44 +0000916}
917
Ben Clayton0ae939b2020-11-17 17:53:38 +0000918TEST_F(SpvBuilderConstructorTest, Type_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) {
919 auto* cast = mat4x4<f32>(
920 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
921 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000922 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +0000923
Ben Claytonf12054e2021-01-21 16:15:00 +0000924 spirv::Builder& b = Build();
925
dan sinclaird6e063d2020-09-24 16:38:35 +0000926 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000927 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclair3c025922020-09-24 14:38:44 +0000928
dan sinclaird6e063d2020-09-24 16:38:35 +0000929 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
930%2 = OpTypeVector %3 4
931%1 = OpTypeMatrix %2 4
932%4 = OpConstant %3 2
933%5 = OpConstantComposite %2 %4 %4 %4 %4
934%6 = OpConstantComposite %1 %5 %5 %5 %5
935)");
dan sinclair3c025922020-09-24 14:38:44 +0000936}
937
Ben Clayton0ae939b2020-11-17 17:53:38 +0000938TEST_F(SpvBuilderConstructorTest, Type_Array_5_F32) {
939 auto* cast = array<f32, 5>(2.0f, 2.0f, 2.0f, 2.0f, 2.0f);
Ben Clayton401b96b2021-02-03 17:19:59 +0000940 WrapInFunction(cast);
dan sinclair8a220a62020-09-24 16:48:35 +0000941
Ben Claytonf12054e2021-01-21 16:15:00 +0000942 spirv::Builder& b = Build();
943
dan sinclair8a220a62020-09-24 16:48:35 +0000944 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000945 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclair8a220a62020-09-24 16:48:35 +0000946
947 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
948%3 = OpTypeInt 32 0
949%4 = OpConstant %3 5
950%1 = OpTypeArray %2 %4
951%5 = OpConstant %2 2
952%6 = OpConstantComposite %1 %5 %5 %5 %5 %5
953)");
dan sinclair3c025922020-09-24 14:38:44 +0000954}
955
Ben Clayton0ae939b2020-11-17 17:53:38 +0000956TEST_F(SpvBuilderConstructorTest, Type_Array_2_Vec3) {
957 auto* cast =
958 array<f32, 2>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000959 WrapInFunction(cast);
dan sinclair8a220a62020-09-24 16:48:35 +0000960
Ben Claytonf12054e2021-01-21 16:15:00 +0000961 spirv::Builder& b = Build();
962
dan sinclair8a220a62020-09-24 16:48:35 +0000963 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000964 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair8a220a62020-09-24 16:48:35 +0000965
Ben Clayton0ae939b2020-11-17 17:53:38 +0000966 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
967%3 = OpTypeInt 32 0
968%4 = OpConstant %3 2
969%1 = OpTypeArray %2 %4
970%5 = OpTypeVector %2 3
971%6 = OpConstant %2 2
972%7 = OpConstantComposite %5 %6 %6 %6
dan sinclair8a220a62020-09-24 16:48:35 +0000973%8 = OpConstantComposite %1 %7 %7
974)");
dan sinclair3c025922020-09-24 14:38:44 +0000975}
976
Ben Clayton0ae939b2020-11-17 17:53:38 +0000977TEST_F(SpvBuilderConstructorTest, Type_Struct) {
Ben Claytone204f272021-04-22 14:40:23 +0000978 auto s = Structure("my_struct", {
979 Member("a", ty.f32()),
980 Member("b", ty.vec3<f32>()),
981 });
dan sinclair571bce62020-09-24 18:18:05 +0000982
Ben Claytond614dd52021-03-15 10:43:11 +0000983 auto* t = Construct(s, 2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
Ben Clayton401b96b2021-02-03 17:19:59 +0000984 WrapInFunction(t);
dan sinclair571bce62020-09-24 18:18:05 +0000985
Ben Claytonf12054e2021-01-21 16:15:00 +0000986 spirv::Builder& b = Build();
987
dan sinclair571bce62020-09-24 18:18:05 +0000988 b.push_function(Function{});
989
Ben Clayton0ae939b2020-11-17 17:53:38 +0000990 EXPECT_EQ(b.GenerateExpression(t), 6u);
dan sinclair571bce62020-09-24 18:18:05 +0000991 ASSERT_FALSE(b.has_error()) << b.error();
992
993 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
994%3 = OpTypeVector %2 3
995%1 = OpTypeStruct %2 %3
996%4 = OpConstant %2 2
997%5 = OpConstantComposite %3 %4 %4 %4
998%6 = OpConstantComposite %1 %4 %5
999)");
dan sinclair3c025922020-09-24 14:38:44 +00001000}
1001
Ben Clayton0ae939b2020-11-17 17:53:38 +00001002TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_F32) {
Ben Clayton8d391f72021-01-26 16:57:10 +00001003 auto* t = Construct(ty.f32());
dan sinclair3c025922020-09-24 14:38:44 +00001004
Ben Clayton401b96b2021-02-03 17:19:59 +00001005 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001006
Ben Claytonf12054e2021-01-21 16:15:00 +00001007 spirv::Builder& b = Build();
1008
dan sinclair3c025922020-09-24 14:38:44 +00001009 b.push_function(Function{});
1010
Ben Clayton0ae939b2020-11-17 17:53:38 +00001011 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001012 ASSERT_FALSE(b.has_error()) << b.error();
1013
1014 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
1015%2 = OpConstantNull %1
1016)");
1017}
1018
Ben Clayton0ae939b2020-11-17 17:53:38 +00001019TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_I32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001020 auto* t = Construct<i32>();
dan sinclair3c025922020-09-24 14:38:44 +00001021
Ben Clayton401b96b2021-02-03 17:19:59 +00001022 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001023
Ben Claytonf12054e2021-01-21 16:15:00 +00001024 spirv::Builder& b = Build();
1025
dan sinclair3c025922020-09-24 14:38:44 +00001026 b.push_function(Function{});
1027
Ben Clayton0ae939b2020-11-17 17:53:38 +00001028 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001029 ASSERT_FALSE(b.has_error()) << b.error();
1030
1031 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 1
1032%2 = OpConstantNull %1
1033)");
1034}
1035
Ben Clayton0ae939b2020-11-17 17:53:38 +00001036TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_U32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001037 auto* t = Construct<u32>();
dan sinclair3c025922020-09-24 14:38:44 +00001038
Ben Clayton401b96b2021-02-03 17:19:59 +00001039 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001040
Ben Claytonf12054e2021-01-21 16:15:00 +00001041 spirv::Builder& b = Build();
1042
dan sinclair3c025922020-09-24 14:38:44 +00001043 b.push_function(Function{});
1044
Ben Clayton0ae939b2020-11-17 17:53:38 +00001045 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001046 ASSERT_FALSE(b.has_error()) << b.error();
1047
1048 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 0
1049%2 = OpConstantNull %1
1050)");
1051}
1052
Ben Clayton0ae939b2020-11-17 17:53:38 +00001053TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Bool) {
Ben Clayton8d391f72021-01-26 16:57:10 +00001054 auto* t = Construct(ty.bool_());
dan sinclair3c025922020-09-24 14:38:44 +00001055
Ben Clayton401b96b2021-02-03 17:19:59 +00001056 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001057
Ben Claytonf12054e2021-01-21 16:15:00 +00001058 spirv::Builder& b = Build();
1059
dan sinclair3c025922020-09-24 14:38:44 +00001060 b.push_function(Function{});
1061
Ben Clayton0ae939b2020-11-17 17:53:38 +00001062 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001063 ASSERT_FALSE(b.has_error()) << b.error();
1064
1065 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool
1066%2 = OpConstantNull %1
1067)");
1068}
1069
Ben Clayton0ae939b2020-11-17 17:53:38 +00001070TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Vector) {
1071 auto* t = vec2<i32>();
dan sinclair3c025922020-09-24 14:38:44 +00001072
Ben Clayton401b96b2021-02-03 17:19:59 +00001073 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001074
Ben Claytonf12054e2021-01-21 16:15:00 +00001075 spirv::Builder& b = Build();
1076
dan sinclair3c025922020-09-24 14:38:44 +00001077 b.push_function(Function{});
1078
Ben Clayton0ae939b2020-11-17 17:53:38 +00001079 EXPECT_EQ(b.GenerateExpression(t), 3u);
dan sinclair3c025922020-09-24 14:38:44 +00001080 ASSERT_FALSE(b.has_error()) << b.error();
1081
1082 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1083%1 = OpTypeVector %2 2
1084%3 = OpConstantNull %1
1085)");
1086}
1087
Ben Clayton0ae939b2020-11-17 17:53:38 +00001088TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Matrix) {
1089 auto* t = mat4x2<f32>();
dan sinclair3c025922020-09-24 14:38:44 +00001090
Ben Clayton401b96b2021-02-03 17:19:59 +00001091 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001092
Ben Claytonf12054e2021-01-21 16:15:00 +00001093 spirv::Builder& b = Build();
1094
dan sinclair3c025922020-09-24 14:38:44 +00001095 b.push_function(Function{});
1096
Ben Clayton0ae939b2020-11-17 17:53:38 +00001097 EXPECT_EQ(b.GenerateExpression(t), 4u);
dan sinclair3c025922020-09-24 14:38:44 +00001098 ASSERT_FALSE(b.has_error()) << b.error();
1099
1100 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
1101%2 = OpTypeVector %3 2
1102%1 = OpTypeMatrix %2 4
1103%4 = OpConstantNull %1
1104)");
1105}
1106
Ben Clayton0ae939b2020-11-17 17:53:38 +00001107TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Array) {
1108 auto* t = array<i32, 2>();
dan sinclair3c025922020-09-24 14:38:44 +00001109
Ben Clayton401b96b2021-02-03 17:19:59 +00001110 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001111
Ben Claytonf12054e2021-01-21 16:15:00 +00001112 spirv::Builder& b = Build();
1113
dan sinclair3c025922020-09-24 14:38:44 +00001114 b.push_function(Function{});
1115
Ben Clayton0ae939b2020-11-17 17:53:38 +00001116 EXPECT_EQ(b.GenerateExpression(t), 5u);
dan sinclair3c025922020-09-24 14:38:44 +00001117 ASSERT_FALSE(b.has_error()) << b.error();
1118
1119 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1120%3 = OpTypeInt 32 0
1121%4 = OpConstant %3 2
1122%1 = OpTypeArray %2 %4
1123%5 = OpConstantNull %1
1124)");
1125}
1126
Ben Clayton0ae939b2020-11-17 17:53:38 +00001127TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Struct) {
Ben Claytone204f272021-04-22 14:40:23 +00001128 auto s = Structure("my_struct", {Member("a", ty.f32())});
Ben Claytond614dd52021-03-15 10:43:11 +00001129 auto* t = Construct(s);
Ben Clayton401b96b2021-02-03 17:19:59 +00001130 WrapInFunction(t);
dan sinclair3c025922020-09-24 14:38:44 +00001131
Ben Claytonf12054e2021-01-21 16:15:00 +00001132 spirv::Builder& b = Build();
1133
dan sinclair3c025922020-09-24 14:38:44 +00001134 b.push_function(Function{});
1135
Ben Clayton0ae939b2020-11-17 17:53:38 +00001136 EXPECT_EQ(b.GenerateExpression(t), 3u);
dan sinclair3c025922020-09-24 14:38:44 +00001137 ASSERT_FALSE(b.has_error()) << b.error();
1138
1139 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1140%1 = OpTypeStruct %2
1141%3 = OpConstantNull %1
1142)");
1143}
1144
Ben Clayton0ae939b2020-11-17 17:53:38 +00001145TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_I32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001146 auto* cast = Construct<i32>(2u);
Ben Clayton401b96b2021-02-03 17:19:59 +00001147 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001148
Ben Claytonf12054e2021-01-21 16:15:00 +00001149 spirv::Builder& b = Build();
1150
dan sinclair3c025922020-09-24 14:38:44 +00001151 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001152 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001153
1154 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1155%3 = OpTypeInt 32 0
1156%4 = OpConstant %3 2
1157)");
1158 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1159 R"(%1 = OpBitcast %2 %4
1160)");
1161}
1162
Ben Clayton0ae939b2020-11-17 17:53:38 +00001163TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_U32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001164 auto* cast = Construct<u32>(2);
Ben Clayton401b96b2021-02-03 17:19:59 +00001165 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001166
Ben Claytonf12054e2021-01-21 16:15:00 +00001167 spirv::Builder& b = Build();
1168
dan sinclair3c025922020-09-24 14:38:44 +00001169 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001170 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001171
1172 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
1173%3 = OpTypeInt 32 1
1174%4 = OpConstant %3 2
1175)");
1176 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1177 R"(%1 = OpBitcast %2 %4
1178)");
1179}
1180
Ben Clayton0ae939b2020-11-17 17:53:38 +00001181TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_I32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001182 auto* cast = Construct<i32>(2.4f);
Ben Clayton401b96b2021-02-03 17:19:59 +00001183 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001184
Ben Claytonf12054e2021-01-21 16:15:00 +00001185 spirv::Builder& b = Build();
1186
dan sinclair3c025922020-09-24 14:38:44 +00001187 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001188 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001189
1190 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1191%3 = OpTypeFloat 32
1192%4 = OpConstant %3 2.4000001
1193)");
1194 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1195 R"(%1 = OpConvertFToS %2 %4
1196)");
1197}
1198
Ben Clayton0ae939b2020-11-17 17:53:38 +00001199TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_U32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001200 auto* cast = Construct<u32>(2.4f);
Ben Clayton401b96b2021-02-03 17:19:59 +00001201 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001202
Ben Claytonf12054e2021-01-21 16:15:00 +00001203 spirv::Builder& b = Build();
1204
dan sinclair3c025922020-09-24 14:38:44 +00001205 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001206 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001207
1208 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
1209%3 = OpTypeFloat 32
1210%4 = OpConstant %3 2.4000001
1211)");
1212 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1213 R"(%1 = OpConvertFToU %2 %4
1214)");
1215}
1216
Ben Clayton0ae939b2020-11-17 17:53:38 +00001217TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001218 auto* cast = Construct<f32>(2);
Ben Clayton401b96b2021-02-03 17:19:59 +00001219 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001220
Ben Claytonf12054e2021-01-21 16:15:00 +00001221 spirv::Builder& b = Build();
1222
dan sinclair3c025922020-09-24 14:38:44 +00001223 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001224 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001225
1226 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1227%3 = OpTypeInt 32 1
1228%4 = OpConstant %3 2
1229)");
1230 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1231 R"(%1 = OpConvertSToF %2 %4
1232)");
1233}
1234
Ben Clayton0ae939b2020-11-17 17:53:38 +00001235TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F32) {
Ben Clayton1637cbb2021-01-05 15:44:39 +00001236 auto* cast = Construct<f32>(2u);
Ben Clayton401b96b2021-02-03 17:19:59 +00001237 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001238
Ben Claytonf12054e2021-01-21 16:15:00 +00001239 spirv::Builder& b = Build();
1240
dan sinclair3c025922020-09-24 14:38:44 +00001241 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001242 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001243
1244 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1245%3 = OpTypeInt 32 0
1246%4 = OpConstant %3 2
1247)");
1248 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1249 R"(%1 = OpConvertUToF %2 %4
1250)");
1251}
1252
Ben Clayton0ae939b2020-11-17 17:53:38 +00001253TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_I32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001254 auto* var = Global("i", ty.vec3<u32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001255
Ben Clayton1637cbb2021-01-05 15:44:39 +00001256 auto* cast = vec3<i32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001257 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001258
Ben Claytonf12054e2021-01-21 16:15:00 +00001259 spirv::Builder& b = Build();
1260
dan sinclair3c025922020-09-24 14:38:44 +00001261 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001262 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001263 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001264
1265 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
1266%3 = OpTypeVector %4 3
1267%2 = OpTypePointer Private %3
1268%5 = OpConstantNull %3
1269%1 = OpVariable %2 Private %5
1270%8 = OpTypeInt 32 1
1271%7 = OpTypeVector %8 3
1272)");
1273 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1274 R"(%9 = OpLoad %3 %1
1275%6 = OpBitcast %7 %9
1276)");
1277}
1278
Ben Clayton0ae939b2020-11-17 17:53:38 +00001279TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_I32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001280 auto* var = Global("i", ty.vec3<f32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001281
Ben Clayton1637cbb2021-01-05 15:44:39 +00001282 auto* cast = vec3<i32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001283 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001284
Ben Claytonf12054e2021-01-21 16:15:00 +00001285 spirv::Builder& b = Build();
1286
dan sinclair3c025922020-09-24 14:38:44 +00001287 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001288 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001289 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001290
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 1
1297%7 = OpTypeVector %8 3
1298)");
1299 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1300 R"(%9 = OpLoad %3 %1
1301%6 = OpConvertFToS %7 %9
1302)");
1303}
1304
Ben Clayton0ae939b2020-11-17 17:53:38 +00001305TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_U32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001306 auto* var = Global("i", ty.vec3<i32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001307
Ben Clayton1637cbb2021-01-05 15:44:39 +00001308 auto* cast = vec3<u32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001309 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001310
Ben Claytonf12054e2021-01-21 16:15:00 +00001311 spirv::Builder& b = Build();
1312
dan sinclair3c025922020-09-24 14:38:44 +00001313 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001314 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001315 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001316
1317 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
1318%3 = OpTypeVector %4 3
1319%2 = OpTypePointer Private %3
1320%5 = OpConstantNull %3
1321%1 = OpVariable %2 Private %5
1322%8 = OpTypeInt 32 0
1323%7 = OpTypeVector %8 3
1324)");
1325 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1326 R"(%9 = OpLoad %3 %1
1327%6 = OpBitcast %7 %9
1328)");
1329}
1330
Ben Clayton0ae939b2020-11-17 17:53:38 +00001331TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_U32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001332 auto* var = Global("i", ty.vec3<f32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001333
Ben Clayton1637cbb2021-01-05 15:44:39 +00001334 auto* cast = vec3<u32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001335 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001336
Ben Claytonf12054e2021-01-21 16:15:00 +00001337 spirv::Builder& b = Build();
1338
dan sinclair3c025922020-09-24 14:38:44 +00001339 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001340 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001341 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001342
1343 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
1344%3 = OpTypeVector %4 3
1345%2 = OpTypePointer Private %3
1346%5 = OpConstantNull %3
1347%1 = OpVariable %2 Private %5
1348%8 = OpTypeInt 32 0
1349%7 = OpTypeVector %8 3
1350)");
1351 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1352 R"(%9 = OpLoad %3 %1
1353%6 = OpConvertFToU %7 %9
1354)");
1355}
1356
Ben Clayton0ae939b2020-11-17 17:53:38 +00001357TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001358 auto* var = Global("i", ty.vec3<i32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001359
Ben Clayton1637cbb2021-01-05 15:44:39 +00001360 auto* cast = vec3<f32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001361 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001362
Ben Claytonf12054e2021-01-21 16:15:00 +00001363 spirv::Builder& b = Build();
1364
dan sinclair3c025922020-09-24 14:38:44 +00001365 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001366 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001367 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001368
1369 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
1370%3 = OpTypeVector %4 3
1371%2 = OpTypePointer Private %3
1372%5 = OpConstantNull %3
1373%1 = OpVariable %2 Private %5
1374%8 = OpTypeFloat 32
1375%7 = OpTypeVector %8 3
1376)");
1377 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1378 R"(%9 = OpLoad %3 %1
1379%6 = OpConvertSToF %7 %9
1380)");
1381}
1382
Ben Clayton0ae939b2020-11-17 17:53:38 +00001383TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F32) {
Ben Clayton37571bc2021-02-16 23:57:01 +00001384 auto* var = Global("i", ty.vec3<u32>(), ast::StorageClass::kPrivate);
dan sinclair3c025922020-09-24 14:38:44 +00001385
Ben Clayton1637cbb2021-01-05 15:44:39 +00001386 auto* cast = vec3<f32>("i");
Ben Clayton401b96b2021-02-03 17:19:59 +00001387 WrapInFunction(cast);
dan sinclair3c025922020-09-24 14:38:44 +00001388
Ben Claytonf12054e2021-01-21 16:15:00 +00001389 spirv::Builder& b = Build();
1390
dan sinclair3c025922020-09-24 14:38:44 +00001391 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001392 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001393 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001394
1395 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
1396%3 = OpTypeVector %4 3
1397%2 = OpTypePointer Private %3
1398%5 = OpConstantNull %3
1399%1 = OpVariable %2 Private %5
1400%8 = OpTypeFloat 32
1401%7 = OpTypeVector %8 3
1402)");
1403 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1404 R"(%9 = OpLoad %3 %1
1405%6 = OpConvertUToF %7 %9
1406)");
1407}
1408
Ben Clayton0ae939b2020-11-17 17:53:38 +00001409TEST_F(SpvBuilderConstructorTest,
1410 IsConstructorConst_GlobalVectorWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001411 // vec3<f32>(1.0, 2.0, 3.0) -> true
Ben Clayton0ae939b2020-11-17 17:53:38 +00001412 auto* t = vec3<f32>(1.f, 2.f, 3.f);
Ben Clayton401b96b2021-02-03 17:19:59 +00001413 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001414
Ben Claytonf12054e2021-01-21 16:15:00 +00001415 spirv::Builder& b = Build();
1416
Ben Clayton0ae939b2020-11-17 17:53:38 +00001417 EXPECT_TRUE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001418 EXPECT_FALSE(b.has_error());
1419}
1420
Ben Clayton0ae939b2020-11-17 17:53:38 +00001421TEST_F(SpvBuilderConstructorTest, IsConstructorConst_GlobalVector_WithIdent) {
dan sinclair435916e2020-10-14 15:14:11 +00001422 // vec3<f32>(a, b, c) -> false -- ERROR
Ben Clayton401b96b2021-02-03 17:19:59 +00001423
Ben Clayton37571bc2021-02-16 23:57:01 +00001424 Global("a", ty.f32(), ast::StorageClass::kPrivate);
1425 Global("b", ty.f32(), ast::StorageClass::kPrivate);
1426 Global("c", ty.f32(), ast::StorageClass::kPrivate);
Ben Clayton401b96b2021-02-03 17:19:59 +00001427
Ben Clayton0ae939b2020-11-17 17:53:38 +00001428 auto* t = vec3<f32>("a", "b", "c");
Ben Clayton401b96b2021-02-03 17:19:59 +00001429 WrapInFunction(t);
dan sinclairff267ca2020-10-14 18:26:31 +00001430
Ben Claytonf12054e2021-01-21 16:15:00 +00001431 spirv::Builder& b = Build();
1432
Ben Clayton0ae939b2020-11-17 17:53:38 +00001433 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001434 EXPECT_TRUE(b.has_error());
1435 EXPECT_EQ(b.error(), "constructor must be a constant expression");
1436}
1437
Ben Clayton0ae939b2020-11-17 17:53:38 +00001438TEST_F(SpvBuilderConstructorTest,
1439 IsConstructorConst_GlobalArrayWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001440 // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
1441 // -> true
Ben Clayton0ae939b2020-11-17 17:53:38 +00001442 auto* t = Construct(ty.array(ty.vec2<f32>(), 2), vec3<f32>(1.f, 2.f, 3.f),
1443 vec3<f32>(1.f, 2.f, 3.f));
Ben Clayton401b96b2021-02-03 17:19:59 +00001444 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001445
Ben Claytonf12054e2021-01-21 16:15:00 +00001446 spirv::Builder& b = Build();
1447
Ben Clayton0ae939b2020-11-17 17:53:38 +00001448 EXPECT_TRUE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001449 EXPECT_FALSE(b.has_error());
1450}
1451
Ben Clayton0ae939b2020-11-17 17:53:38 +00001452TEST_F(SpvBuilderConstructorTest,
dan sinclair435916e2020-10-14 15:14:11 +00001453 IsConstructorConst_GlobalVectorWithMatchingTypeConstructors) {
Arman Uguray3549e2e2021-03-15 21:21:33 +00001454 // vec2<f32>(f32(1.0), f32(2.0)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001455
Ben Clayton0ae939b2020-11-17 17:53:38 +00001456 auto* t = vec2<f32>(Construct<f32>(1.f), Construct<f32>(2.f));
Ben Clayton401b96b2021-02-03 17:19:59 +00001457 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001458
Ben Claytonf12054e2021-01-21 16:15:00 +00001459 spirv::Builder& b = Build();
1460
Ben Clayton0ae939b2020-11-17 17:53:38 +00001461 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001462 EXPECT_FALSE(b.has_error());
1463}
1464
Ben Clayton0ae939b2020-11-17 17:53:38 +00001465TEST_F(SpvBuilderConstructorTest,
1466 IsConstructorConst_GlobalWithTypeCastConstructor) {
Arman Uguray3549e2e2021-03-15 21:21:33 +00001467 // vec2<f32>(f32(1), f32(2)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001468
Ben Clayton0ae939b2020-11-17 17:53:38 +00001469 auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(2));
Ben Clayton401b96b2021-02-03 17:19:59 +00001470 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001471
Ben Claytonf12054e2021-01-21 16:15:00 +00001472 spirv::Builder& b = Build();
1473
Ben Clayton0ae939b2020-11-17 17:53:38 +00001474 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001475 EXPECT_FALSE(b.has_error());
1476}
1477
Ben Clayton0ae939b2020-11-17 17:53:38 +00001478TEST_F(SpvBuilderConstructorTest,
1479 IsConstructorConst_VectorWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001480 // vec3<f32>(1.0, 2.0, 3.0) -> true
dan sinclair435916e2020-10-14 15:14:11 +00001481
Ben Clayton0ae939b2020-11-17 17:53:38 +00001482 auto* t = vec3<f32>(1.f, 2.f, 3.f);
Ben Clayton401b96b2021-02-03 17:19:59 +00001483 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001484
Ben Claytonf12054e2021-01-21 16:15:00 +00001485 spirv::Builder& b = Build();
1486
Ben Clayton0ae939b2020-11-17 17:53:38 +00001487 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001488 EXPECT_FALSE(b.has_error());
1489}
1490
Ben Clayton0ae939b2020-11-17 17:53:38 +00001491TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Vector_WithIdent) {
dan sinclair435916e2020-10-14 15:14:11 +00001492 // vec3<f32>(a, b, c) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001493
Ben Clayton37571bc2021-02-16 23:57:01 +00001494 Global("a", ty.f32(), ast::StorageClass::kPrivate);
1495 Global("b", ty.f32(), ast::StorageClass::kPrivate);
1496 Global("c", ty.f32(), ast::StorageClass::kPrivate);
dan sinclair435916e2020-10-14 15:14:11 +00001497
Antonio Maioranobb5617f2021-03-19 18:45:30 +00001498 auto* t = vec3<f32>("a", "b", "c");
1499 WrapInFunction(t);
1500
Ben Claytonf12054e2021-01-21 16:15:00 +00001501 spirv::Builder& b = Build();
1502
Ben Clayton0ae939b2020-11-17 17:53:38 +00001503 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001504 EXPECT_FALSE(b.has_error());
1505}
1506
Ben Clayton0ae939b2020-11-17 17:53:38 +00001507TEST_F(SpvBuilderConstructorTest,
1508 IsConstructorConst_ArrayWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001509 // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
1510 // -> true
dan sinclair435916e2020-10-14 15:14:11 +00001511
Ben Clayton0ae939b2020-11-17 17:53:38 +00001512 auto* first = vec3<f32>(1.f, 2.f, 3.f);
1513 auto* second = vec3<f32>(1.f, 2.f, 3.f);
dan sinclair435916e2020-10-14 15:14:11 +00001514
Ben Clayton0ae939b2020-11-17 17:53:38 +00001515 auto* t = Construct(ty.array(ty.vec3<f32>(), 2), first, second);
Ben Clayton401b96b2021-02-03 17:19:59 +00001516 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001517
Ben Claytonf12054e2021-01-21 16:15:00 +00001518 spirv::Builder& b = Build();
1519
Ben Clayton0ae939b2020-11-17 17:53:38 +00001520 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001521 EXPECT_FALSE(b.has_error());
1522}
1523
Ben Clayton0ae939b2020-11-17 17:53:38 +00001524TEST_F(SpvBuilderConstructorTest,
Arman Uguray3549e2e2021-03-15 21:21:33 +00001525 IsConstructorConst_VectorWithTypeCastConstConstructors) {
Ben Clayton0ae939b2020-11-17 17:53:38 +00001526 // vec2<f32>(f32(1), f32(2)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001527
Arman Uguray3549e2e2021-03-15 21:21:33 +00001528 auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(2));
Ben Clayton401b96b2021-02-03 17:19:59 +00001529 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001530
Ben Claytonf12054e2021-01-21 16:15:00 +00001531 spirv::Builder& b = Build();
1532
Ben Clayton0ae939b2020-11-17 17:53:38 +00001533 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001534 EXPECT_FALSE(b.has_error());
1535}
1536
Ben Clayton0ae939b2020-11-17 17:53:38 +00001537TEST_F(SpvBuilderConstructorTest, IsConstructorConst_BitCastScalars) {
Arman Uguray3549e2e2021-03-15 21:21:33 +00001538 auto* t = vec2<u32>(Construct<u32>(1), Construct<u32>(1));
Ben Clayton401b96b2021-02-03 17:19:59 +00001539 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001540
Ben Claytonf12054e2021-01-21 16:15:00 +00001541 spirv::Builder& b = Build();
1542
Ben Clayton0ae939b2020-11-17 17:53:38 +00001543 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001544 EXPECT_FALSE(b.has_error());
1545}
1546
Ben Clayton0ae939b2020-11-17 17:53:38 +00001547TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Struct) {
Ben Claytone204f272021-04-22 14:40:23 +00001548 auto s = Structure("my_struct", {
1549 Member("a", ty.f32()),
1550 Member("b", ty.vec3<f32>()),
1551 });
Ben Claytond614dd52021-03-15 10:43:11 +00001552
1553 auto* t = Construct(s, 2.f, vec3<f32>(2.f, 2.f, 2.f));
Ben Clayton401b96b2021-02-03 17:19:59 +00001554 WrapInFunction(t);
dan sinclair435916e2020-10-14 15:14:11 +00001555
Ben Claytonf12054e2021-01-21 16:15:00 +00001556 spirv::Builder& b = Build();
1557
Ben Clayton0ae939b2020-11-17 17:53:38 +00001558 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001559 EXPECT_FALSE(b.has_error());
1560}
1561
Ben Clayton0ae939b2020-11-17 17:53:38 +00001562TEST_F(SpvBuilderConstructorTest,
1563 IsConstructorConst_Struct_WithIdentSubExpression) {
Ben Claytone204f272021-04-22 14:40:23 +00001564 auto s = Structure("my_struct", {
1565 Member("a", ty.f32()),
1566 Member("b", ty.vec3<f32>()),
1567 });
dan sinclair435916e2020-10-14 15:14:11 +00001568
Ben Clayton37571bc2021-02-16 23:57:01 +00001569 Global("a", ty.f32(), ast::StorageClass::kPrivate);
1570 Global("b", ty.f32(), ast::StorageClass::kPrivate);
dan sinclair435916e2020-10-14 15:14:11 +00001571
Antonio Maioranobb5617f2021-03-19 18:45:30 +00001572 auto* t = Construct(s, 2.f, "a", 2.f);
1573 WrapInFunction(t);
1574
Ben Claytonf12054e2021-01-21 16:15:00 +00001575 spirv::Builder& b = Build();
1576
Ben Clayton0ae939b2020-11-17 17:53:38 +00001577 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001578 EXPECT_FALSE(b.has_error());
1579}
1580
dan sinclair5cd08fd2020-03-30 20:01:38 +00001581} // namespace
1582} // namespace spirv
1583} // namespace writer
1584} // namespace tint