1 // Copyright 2021 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 "src/writer/glsl/test_helper.h"
16
17 namespace tint {
18 namespace writer {
19 namespace glsl {
20 namespace {
21
22 using GlslGeneratorImplTest_Import = TestHelper;
23
24 struct GlslImportData {
25 const char* name;
26 const char* glsl_name;
27 };
operator <<(std::ostream & out,GlslImportData data)28 inline std::ostream& operator<<(std::ostream& out, GlslImportData data) {
29 out << data.name;
30 return out;
31 }
32
33 using GlslImportData_SingleParamTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_SingleParamTest,FloatScalar)34 TEST_P(GlslImportData_SingleParamTest, FloatScalar) {
35 auto param = GetParam();
36
37 auto* ident = Expr(param.name);
38 auto* expr = Call(ident, 1.f);
39 WrapInFunction(expr);
40
41 GeneratorImpl& gen = Build();
42
43 std::stringstream out;
44 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
45 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1.0f)");
46 }
47 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
48 GlslImportData_SingleParamTest,
49 testing::Values(GlslImportData{"abs", "abs"},
50 GlslImportData{"acos", "acos"},
51 GlslImportData{"asin", "asin"},
52 GlslImportData{"atan", "atan"},
53 GlslImportData{"cos", "cos"},
54 GlslImportData{"cosh", "cosh"},
55 GlslImportData{"ceil", "ceil"},
56 GlslImportData{"exp", "exp"},
57 GlslImportData{"exp2", "exp2"},
58 GlslImportData{"floor", "floor"},
59 GlslImportData{"fract", "frac"},
60 GlslImportData{"inverseSqrt", "rsqrt"},
61 GlslImportData{"length", "length"},
62 GlslImportData{"log", "log"},
63 GlslImportData{"log2", "log2"},
64 GlslImportData{"round", "round"},
65 GlslImportData{"sign", "sign"},
66 GlslImportData{"sin", "sin"},
67 GlslImportData{"sinh", "sinh"},
68 GlslImportData{"sqrt", "sqrt"},
69 GlslImportData{"tan", "tan"},
70 GlslImportData{"tanh", "tanh"},
71 GlslImportData{"trunc", "trunc"}));
72
73 using GlslImportData_SingleIntParamTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_SingleIntParamTest,IntScalar)74 TEST_P(GlslImportData_SingleIntParamTest, IntScalar) {
75 auto param = GetParam();
76
77 auto* expr = Call(param.name, Expr(1));
78 WrapInFunction(expr);
79
80 GeneratorImpl& gen = Build();
81
82 std::stringstream out;
83 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
84 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1)");
85 }
86 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
87 GlslImportData_SingleIntParamTest,
88 testing::Values(GlslImportData{"abs", "abs"}));
89
90 using GlslImportData_SingleVectorParamTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_SingleVectorParamTest,FloatVector)91 TEST_P(GlslImportData_SingleVectorParamTest, FloatVector) {
92 auto param = GetParam();
93
94 auto* ident = Expr(param.name);
95 auto* expr = Call(ident, vec3<f32>(1.f, 2.f, 3.f));
96 WrapInFunction(expr);
97
98 GeneratorImpl& gen = Build();
99
100 std::stringstream out;
101 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
102 EXPECT_EQ(out.str(),
103 std::string(param.glsl_name) + "(vec3(1.0f, 2.0f, 3.0f))");
104 }
105 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
106 GlslImportData_SingleVectorParamTest,
107 testing::Values(GlslImportData{"abs", "abs"},
108 GlslImportData{"acos", "acos"},
109 GlslImportData{"asin", "asin"},
110 GlslImportData{"atan", "atan"},
111 GlslImportData{"cos", "cos"},
112 GlslImportData{"cosh", "cosh"},
113 GlslImportData{"ceil", "ceil"},
114 GlslImportData{"exp", "exp"},
115 GlslImportData{"exp2", "exp2"},
116 GlslImportData{"floor", "floor"},
117 GlslImportData{"fract", "frac"},
118 GlslImportData{"inverseSqrt", "rsqrt"},
119 GlslImportData{"length", "length"},
120 GlslImportData{"log", "log"},
121 GlslImportData{"log2", "log2"},
122 GlslImportData{"normalize",
123 "normalize"},
124 GlslImportData{"round", "round"},
125 GlslImportData{"sign", "sign"},
126 GlslImportData{"sin", "sin"},
127 GlslImportData{"sinh", "sinh"},
128 GlslImportData{"sqrt", "sqrt"},
129 GlslImportData{"tan", "tan"},
130 GlslImportData{"tanh", "tanh"},
131 GlslImportData{"trunc", "trunc"}));
132
133 using GlslImportData_DualParam_ScalarTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_DualParam_ScalarTest,Float)134 TEST_P(GlslImportData_DualParam_ScalarTest, Float) {
135 auto param = GetParam();
136
137 auto* expr = Call(param.name, 1.f, 2.f);
138 WrapInFunction(expr);
139
140 GeneratorImpl& gen = Build();
141
142 std::stringstream out;
143 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
144 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1.0f, 2.0f)");
145 }
146 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
147 GlslImportData_DualParam_ScalarTest,
148 testing::Values(GlslImportData{"atan2", "atan"},
149 GlslImportData{"distance", "distance"},
150 GlslImportData{"max", "max"},
151 GlslImportData{"min", "min"},
152 GlslImportData{"pow", "pow"},
153 GlslImportData{"step", "step"}));
154
155 using GlslImportData_DualParam_VectorTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_DualParam_VectorTest,Float)156 TEST_P(GlslImportData_DualParam_VectorTest, Float) {
157 auto param = GetParam();
158
159 auto* expr =
160 Call(param.name, vec3<f32>(1.f, 2.f, 3.f), vec3<f32>(4.f, 5.f, 6.f));
161 WrapInFunction(expr);
162
163 GeneratorImpl& gen = Build();
164
165 std::stringstream out;
166 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
167 EXPECT_EQ(out.str(), std::string(param.glsl_name) +
168 "(vec3(1.0f, 2.0f, 3.0f), vec3(4.0f, 5.0f, 6.0f))");
169 }
170 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
171 GlslImportData_DualParam_VectorTest,
172 testing::Values(GlslImportData{"atan2", "atan"},
173 GlslImportData{"cross", "cross"},
174 GlslImportData{"distance", "distance"},
175 GlslImportData{"max", "max"},
176 GlslImportData{"min", "min"},
177 GlslImportData{"pow", "pow"},
178 GlslImportData{"reflect", "reflect"},
179 GlslImportData{"step", "step"}));
180
181 using GlslImportData_DualParam_Int_Test = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_DualParam_Int_Test,IntScalar)182 TEST_P(GlslImportData_DualParam_Int_Test, IntScalar) {
183 auto param = GetParam();
184
185 auto* expr = Call(param.name, 1, 2);
186 WrapInFunction(expr);
187
188 GeneratorImpl& gen = Build();
189
190 std::stringstream out;
191 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
192 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1, 2)");
193 }
194 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
195 GlslImportData_DualParam_Int_Test,
196 testing::Values(GlslImportData{"max", "max"},
197 GlslImportData{"min", "min"}));
198
199 using GlslImportData_TripleParam_ScalarTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_TripleParam_ScalarTest,Float)200 TEST_P(GlslImportData_TripleParam_ScalarTest, Float) {
201 auto param = GetParam();
202
203 auto* expr = Call(param.name, 1.f, 2.f, 3.f);
204 WrapInFunction(expr);
205
206 GeneratorImpl& gen = Build();
207
208 std::stringstream out;
209 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
210 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1.0f, 2.0f, 3.0f)");
211 }
212 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
213 GlslImportData_TripleParam_ScalarTest,
214 testing::Values(GlslImportData{"fma", "mad"},
215 GlslImportData{"mix", "mix"},
216 GlslImportData{"clamp", "clamp"},
217 GlslImportData{"smoothStep",
218 "smoothstep"}));
219
220 using GlslImportData_TripleParam_VectorTest = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_TripleParam_VectorTest,Float)221 TEST_P(GlslImportData_TripleParam_VectorTest, Float) {
222 auto param = GetParam();
223
224 auto* expr = Call(param.name, vec3<f32>(1.f, 2.f, 3.f),
225 vec3<f32>(4.f, 5.f, 6.f), vec3<f32>(7.f, 8.f, 9.f));
226 WrapInFunction(expr);
227
228 GeneratorImpl& gen = Build();
229
230 std::stringstream out;
231 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
232 EXPECT_EQ(
233 out.str(),
234 std::string(param.glsl_name) +
235 R"((vec3(1.0f, 2.0f, 3.0f), vec3(4.0f, 5.0f, 6.0f), vec3(7.0f, 8.0f, 9.0f)))");
236 }
237 INSTANTIATE_TEST_SUITE_P(
238 GlslGeneratorImplTest_Import,
239 GlslImportData_TripleParam_VectorTest,
240 testing::Values(GlslImportData{"faceForward", "faceforward"},
241 GlslImportData{"fma", "mad"},
242 GlslImportData{"clamp", "clamp"},
243 GlslImportData{"smoothStep", "smoothstep"}));
244
TEST_F(GlslGeneratorImplTest_Import,DISABLED_GlslImportData_FMix)245 TEST_F(GlslGeneratorImplTest_Import, DISABLED_GlslImportData_FMix) {
246 FAIL();
247 }
248
249 using GlslImportData_TripleParam_Int_Test = TestParamHelper<GlslImportData>;
TEST_P(GlslImportData_TripleParam_Int_Test,IntScalar)250 TEST_P(GlslImportData_TripleParam_Int_Test, IntScalar) {
251 auto param = GetParam();
252
253 auto* expr = Call(param.name, 1, 2, 3);
254 WrapInFunction(expr);
255
256 GeneratorImpl& gen = Build();
257
258 std::stringstream out;
259 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
260 EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(1, 2, 3)");
261 }
262 INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
263 GlslImportData_TripleParam_Int_Test,
264 testing::Values(GlslImportData{"clamp", "clamp"}));
265
TEST_F(GlslGeneratorImplTest_Import,GlslImportData_Determinant)266 TEST_F(GlslGeneratorImplTest_Import, GlslImportData_Determinant) {
267 Global("var", ty.mat3x3<f32>(), ast::StorageClass::kPrivate);
268
269 auto* expr = Call("determinant", "var");
270 WrapInFunction(expr);
271
272 GeneratorImpl& gen = Build();
273
274 std::stringstream out;
275 ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
276 EXPECT_EQ(out.str(), std::string("determinant(var)"));
277 }
278
279 } // namespace
280 } // namespace glsl
281 } // namespace writer
282 } // namespace tint
283