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/ast/call_statement.h"
16 #include "src/ast/stage_decoration.h"
17 #include "src/ast/struct_block_decoration.h"
18 #include "src/ast/variable_decl_statement.h"
19 #include "src/writer/glsl/test_helper.h"
20
21 namespace tint {
22 namespace writer {
23 namespace glsl {
24 namespace {
25
26 using GlslSanitizerTest = TestHelper;
27
TEST_F(GlslSanitizerTest,Call_ArrayLength)28 TEST_F(GlslSanitizerTest, Call_ArrayLength) {
29 auto* s = Structure("my_struct", {Member(0, "a", ty.array<f32>(4))},
30 {create<ast::StructBlockDecoration>()});
31 Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead,
32 ast::DecorationList{
33 create<ast::BindingDecoration>(1),
34 create<ast::GroupDecoration>(2),
35 });
36
37 Func("a_func", ast::VariableList{}, ty.void_(),
38 ast::StatementList{
39 Decl(Var("len", ty.u32(), ast::StorageClass::kNone,
40 Call("arrayLength", AddressOf(MemberAccessor("b", "a"))))),
41 },
42 ast::DecorationList{
43 Stage(ast::PipelineStage::kFragment),
44 });
45
46 GeneratorImpl& gen = SanitizeAndBuild();
47
48 ASSERT_TRUE(gen.Generate()) << gen.error();
49
50 auto got = gen.result();
51 auto* expect = R"(#version 310 es
52 precision mediump float;
53
54
55 layout (binding = 1) buffer my_struct_1 {
56 float a[];
57 } b;
58
59 void a_func() {
60 uint tint_symbol_1 = 0u;
61 b.GetDimensions(tint_symbol_1);
62 uint tint_symbol_2 = ((tint_symbol_1 - 0u) / 4u);
63 uint len = tint_symbol_2;
64 return;
65 }
66 void main() {
67 a_func();
68 }
69
70
71 )";
72 EXPECT_EQ(expect, got);
73 }
74
TEST_F(GlslSanitizerTest,Call_ArrayLength_OtherMembersInStruct)75 TEST_F(GlslSanitizerTest, Call_ArrayLength_OtherMembersInStruct) {
76 auto* s = Structure("my_struct",
77 {
78 Member(0, "z", ty.f32()),
79 Member(4, "a", ty.array<f32>(4)),
80 },
81 {create<ast::StructBlockDecoration>()});
82 Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead,
83 ast::DecorationList{
84 create<ast::BindingDecoration>(1),
85 create<ast::GroupDecoration>(2),
86 });
87
88 Func("a_func", ast::VariableList{}, ty.void_(),
89 ast::StatementList{
90 Decl(Var("len", ty.u32(), ast::StorageClass::kNone,
91 Call("arrayLength", AddressOf(MemberAccessor("b", "a"))))),
92 },
93 ast::DecorationList{
94 Stage(ast::PipelineStage::kFragment),
95 });
96
97 GeneratorImpl& gen = SanitizeAndBuild();
98
99 ASSERT_TRUE(gen.Generate()) << gen.error();
100
101 auto got = gen.result();
102 auto* expect = R"(#version 310 es
103 precision mediump float;
104
105
106 layout (binding = 1) buffer my_struct_1 {
107 float z;
108 float a[];
109 } b;
110
111 void a_func() {
112 uint tint_symbol_1 = 0u;
113 b.GetDimensions(tint_symbol_1);
114 uint tint_symbol_2 = ((tint_symbol_1 - 4u) / 4u);
115 uint len = tint_symbol_2;
116 return;
117 }
118 void main() {
119 a_func();
120 }
121
122
123 )";
124
125 EXPECT_EQ(expect, got);
126 }
127
TEST_F(GlslSanitizerTest,Call_ArrayLength_ViaLets)128 TEST_F(GlslSanitizerTest, Call_ArrayLength_ViaLets) {
129 auto* s = Structure("my_struct", {Member(0, "a", ty.array<f32>(4))},
130 {create<ast::StructBlockDecoration>()});
131 Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead,
132 ast::DecorationList{
133 create<ast::BindingDecoration>(1),
134 create<ast::GroupDecoration>(2),
135 });
136
137 auto* p = Const("p", nullptr, AddressOf("b"));
138 auto* p2 = Const("p2", nullptr, AddressOf(MemberAccessor(Deref(p), "a")));
139
140 Func("a_func", ast::VariableList{}, ty.void_(),
141 ast::StatementList{
142 Decl(p),
143 Decl(p2),
144 Decl(Var("len", ty.u32(), ast::StorageClass::kNone,
145 Call("arrayLength", p2))),
146 },
147 ast::DecorationList{
148 Stage(ast::PipelineStage::kFragment),
149 });
150
151 GeneratorImpl& gen = SanitizeAndBuild();
152
153 ASSERT_TRUE(gen.Generate()) << gen.error();
154
155 auto got = gen.result();
156 auto* expect = R"(#version 310 es
157 precision mediump float;
158
159
160 layout (binding = 1) buffer my_struct_1 {
161 float a[];
162 } b;
163
164 void a_func() {
165 uint tint_symbol_1 = 0u;
166 b.GetDimensions(tint_symbol_1);
167 uint tint_symbol_2 = ((tint_symbol_1 - 0u) / 4u);
168 uint len = tint_symbol_2;
169 return;
170 }
171 void main() {
172 a_func();
173 }
174
175
176 )";
177
178 EXPECT_EQ(expect, got);
179 }
180
TEST_F(GlslSanitizerTest,PromoteArrayInitializerToConstVar)181 TEST_F(GlslSanitizerTest, PromoteArrayInitializerToConstVar) {
182 auto* array_init = array<i32, 4>(1, 2, 3, 4);
183 auto* array_index = IndexAccessor(array_init, 3);
184 auto* pos = Var("pos", ty.i32(), ast::StorageClass::kNone, array_index);
185
186 Func("main", ast::VariableList{}, ty.void_(),
187 {
188 Decl(pos),
189 },
190 {
191 Stage(ast::PipelineStage::kFragment),
192 });
193
194 GeneratorImpl& gen = SanitizeAndBuild();
195
196 ASSERT_TRUE(gen.Generate()) << gen.error();
197
198 auto got = gen.result();
199 auto* expect = R"(#version 310 es
200 precision mediump float;
201
202 void tint_symbol() {
203 int tint_symbol_1[4] = int[4](1, 2, 3, 4);
204 int pos = tint_symbol_1[3];
205 return;
206 }
207 void main() {
208 tint_symbol();
209 }
210
211
212 )";
213 EXPECT_EQ(expect, got);
214 }
215
TEST_F(GlslSanitizerTest,PromoteStructInitializerToConstVar)216 TEST_F(GlslSanitizerTest, PromoteStructInitializerToConstVar) {
217 auto* str = Structure("S", {
218 Member("a", ty.i32()),
219 Member("b", ty.vec3<f32>()),
220 Member("c", ty.i32()),
221 });
222 auto* struct_init = Construct(ty.Of(str), 1, vec3<f32>(2.f, 3.f, 4.f), 4);
223 auto* struct_access = MemberAccessor(struct_init, "b");
224 auto* pos =
225 Var("pos", ty.vec3<f32>(), ast::StorageClass::kNone, struct_access);
226
227 Func("main", ast::VariableList{}, ty.void_(),
228 {
229 Decl(pos),
230 },
231 {
232 Stage(ast::PipelineStage::kFragment),
233 });
234
235 GeneratorImpl& gen = SanitizeAndBuild();
236
237 ASSERT_TRUE(gen.Generate()) << gen.error();
238
239 auto got = gen.result();
240 auto* expect = R"(#version 310 es
241 precision mediump float;
242
243 struct S {
244 int a;
245 vec3 b;
246 int c;
247 };
248
249 void tint_symbol() {
250 S tint_symbol_1 = S(1, vec3(2.0f, 3.0f, 4.0f), 4);
251 vec3 pos = tint_symbol_1.b;
252 return;
253 }
254 void main() {
255 tint_symbol();
256 }
257
258
259 )";
260 EXPECT_EQ(expect, got);
261 }
262
TEST_F(GlslSanitizerTest,InlinePtrLetsBasic)263 TEST_F(GlslSanitizerTest, InlinePtrLetsBasic) {
264 // var v : i32;
265 // let p : ptr<function, i32> = &v;
266 // let x : i32 = *p;
267 auto* v = Var("v", ty.i32());
268 auto* p =
269 Const("p", ty.pointer<i32>(ast::StorageClass::kFunction), AddressOf(v));
270 auto* x = Var("x", ty.i32(), ast::StorageClass::kNone, Deref(p));
271
272 Func("main", ast::VariableList{}, ty.void_(),
273 {
274 Decl(v),
275 Decl(p),
276 Decl(x),
277 },
278 {
279 Stage(ast::PipelineStage::kFragment),
280 });
281
282 GeneratorImpl& gen = SanitizeAndBuild();
283
284 ASSERT_TRUE(gen.Generate()) << gen.error();
285
286 auto got = gen.result();
287 auto* expect = R"(#version 310 es
288 precision mediump float;
289
290 void tint_symbol() {
291 int v = 0;
292 int x = v;
293 return;
294 }
295 void main() {
296 tint_symbol();
297 }
298
299
300 )";
301 EXPECT_EQ(expect, got);
302 }
303
TEST_F(GlslSanitizerTest,InlinePtrLetsComplexChain)304 TEST_F(GlslSanitizerTest, InlinePtrLetsComplexChain) {
305 // var a : array<mat4x4<f32>, 4>;
306 // let ap : ptr<function, array<mat4x4<f32>, 4>> = &a;
307 // let mp : ptr<function, mat4x4<f32>> = &(*ap)[3];
308 // let vp : ptr<function, vec4<f32>> = &(*mp)[2];
309 // let v : vec4<f32> = *vp;
310 auto* a = Var("a", ty.array(ty.mat4x4<f32>(), 4));
311 auto* ap = Const(
312 "ap",
313 ty.pointer(ty.array(ty.mat4x4<f32>(), 4), ast::StorageClass::kFunction),
314 AddressOf(a));
315 auto* mp =
316 Const("mp", ty.pointer(ty.mat4x4<f32>(), ast::StorageClass::kFunction),
317 AddressOf(IndexAccessor(Deref(ap), 3)));
318 auto* vp =
319 Const("vp", ty.pointer(ty.vec4<f32>(), ast::StorageClass::kFunction),
320 AddressOf(IndexAccessor(Deref(mp), 2)));
321 auto* v = Var("v", ty.vec4<f32>(), ast::StorageClass::kNone, Deref(vp));
322
323 Func("main", ast::VariableList{}, ty.void_(),
324 {
325 Decl(a),
326 Decl(ap),
327 Decl(mp),
328 Decl(vp),
329 Decl(v),
330 },
331 {
332 Stage(ast::PipelineStage::kFragment),
333 });
334
335 GeneratorImpl& gen = SanitizeAndBuild();
336
337 ASSERT_TRUE(gen.Generate()) << gen.error();
338
339 auto got = gen.result();
340 auto* expect = R"(#version 310 es
341 precision mediump float;
342
343 void tint_symbol() {
344 mat4 a[4] = mat4[4](mat4(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f), mat4(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f), mat4(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f), mat4(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
345 vec4 v = a[3][2];
346 return;
347 }
348 void main() {
349 tint_symbol();
350 }
351
352
353 )";
354 EXPECT_EQ(expect, got);
355 }
356
357 } // namespace
358 } // namespace glsl
359 } // namespace writer
360 } // namespace tint
361