1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <algorithm>
16 #include <vector>
17
18 #include "gmock/gmock.h"
19 #include "src/reader/spirv/parser_impl_test_helper.h"
20
21 namespace tint {
22 namespace reader {
23 namespace spirv {
24 namespace {
25
26 using ::testing::Eq;
27
TEST_F(SpvParserTest,Usage_Trivial_Properties)28 TEST_F(SpvParserTest, Usage_Trivial_Properties) {
29 Usage u;
30 EXPECT_TRUE(u.IsValid());
31 EXPECT_FALSE(u.IsComplete());
32 EXPECT_FALSE(u.IsSampler());
33 EXPECT_FALSE(u.IsComparisonSampler());
34 EXPECT_FALSE(u.IsTexture());
35 EXPECT_FALSE(u.IsSampledTexture());
36 EXPECT_FALSE(u.IsMultisampledTexture());
37 EXPECT_FALSE(u.IsDepthTexture());
38 EXPECT_FALSE(u.IsStorageReadTexture());
39 EXPECT_FALSE(u.IsStorageWriteTexture());
40 }
41
TEST_F(SpvParserTest,Usage_Trivial_Output)42 TEST_F(SpvParserTest, Usage_Trivial_Output) {
43 std::ostringstream ss;
44 Usage u;
45 ss << u;
46 EXPECT_THAT(ss.str(), Eq("Usage()"));
47 }
48
TEST_F(SpvParserTest,Usage_Equality_OneDifference)49 TEST_F(SpvParserTest, Usage_Equality_OneDifference) {
50 const int num_usages = 9;
51 std::vector<Usage> usages(num_usages);
52 usages[1].AddSampler();
53 usages[2].AddComparisonSampler();
54 usages[3].AddTexture();
55 usages[4].AddSampledTexture();
56 usages[5].AddMultisampledTexture();
57 usages[6].AddDepthTexture();
58 usages[7].AddStorageReadTexture();
59 usages[8].AddStorageWriteTexture();
60 for (int i = 0; i < num_usages; ++i) {
61 for (int j = 0; j < num_usages; ++j) {
62 const auto& lhs = usages[i];
63 const auto& rhs = usages[j];
64 if (i == j) {
65 EXPECT_TRUE(lhs == rhs);
66 } else {
67 EXPECT_FALSE(lhs == rhs);
68 }
69 }
70 }
71 }
72
TEST_F(SpvParserTest,Usage_Add)73 TEST_F(SpvParserTest, Usage_Add) {
74 // Mix two nontrivial usages.
75 Usage a;
76 a.AddStorageReadTexture();
77
78 Usage b;
79 b.AddComparisonSampler();
80
81 a.Add(b);
82
83 EXPECT_FALSE(a.IsValid());
84 EXPECT_FALSE(a.IsComplete());
85 EXPECT_TRUE(a.IsSampler());
86 EXPECT_TRUE(a.IsComparisonSampler());
87 EXPECT_TRUE(a.IsTexture());
88 EXPECT_FALSE(a.IsSampledTexture());
89 EXPECT_FALSE(a.IsMultisampledTexture());
90 EXPECT_FALSE(a.IsDepthTexture());
91 EXPECT_TRUE(a.IsStorageReadTexture());
92 EXPECT_FALSE(a.IsStorageWriteTexture());
93
94 std::ostringstream ss;
95 ss << a;
96 EXPECT_THAT(ss.str(), Eq("Usage(Sampler( comparison )Texture( read ))"));
97 }
98
TEST_F(SpvParserTest,Usage_AddSampler)99 TEST_F(SpvParserTest, Usage_AddSampler) {
100 std::ostringstream ss;
101 Usage u;
102 u.AddSampler();
103
104 EXPECT_TRUE(u.IsValid());
105 EXPECT_TRUE(u.IsComplete());
106 EXPECT_TRUE(u.IsSampler());
107 EXPECT_FALSE(u.IsComparisonSampler());
108 EXPECT_FALSE(u.IsTexture());
109 EXPECT_FALSE(u.IsSampledTexture());
110 EXPECT_FALSE(u.IsMultisampledTexture());
111 EXPECT_FALSE(u.IsDepthTexture());
112 EXPECT_FALSE(u.IsStorageReadTexture());
113 EXPECT_FALSE(u.IsStorageWriteTexture());
114
115 ss << u;
116 EXPECT_THAT(ss.str(), Eq("Usage(Sampler( ))"));
117
118 // Check idempotency
119 auto copy(u);
120 u.AddSampler();
121 EXPECT_TRUE(u == copy);
122 }
123
TEST_F(SpvParserTest,Usage_AddComparisonSampler)124 TEST_F(SpvParserTest, Usage_AddComparisonSampler) {
125 std::ostringstream ss;
126 Usage u;
127 u.AddComparisonSampler();
128
129 EXPECT_TRUE(u.IsValid());
130 EXPECT_TRUE(u.IsComplete());
131 EXPECT_TRUE(u.IsSampler());
132 EXPECT_TRUE(u.IsComparisonSampler());
133 EXPECT_FALSE(u.IsTexture());
134 EXPECT_FALSE(u.IsSampledTexture());
135 EXPECT_FALSE(u.IsMultisampledTexture());
136 EXPECT_FALSE(u.IsDepthTexture());
137 EXPECT_FALSE(u.IsStorageReadTexture());
138 EXPECT_FALSE(u.IsStorageWriteTexture());
139
140 ss << u;
141 EXPECT_THAT(ss.str(), Eq("Usage(Sampler( comparison ))"));
142
143 auto copy(u);
144 u.AddComparisonSampler();
145 EXPECT_TRUE(u == copy);
146 }
147
TEST_F(SpvParserTest,Usage_AddTexture)148 TEST_F(SpvParserTest, Usage_AddTexture) {
149 std::ostringstream ss;
150 Usage u;
151 u.AddTexture();
152
153 EXPECT_TRUE(u.IsValid());
154 EXPECT_FALSE(u.IsComplete()); // Don't know if it's sampled or storage
155 EXPECT_FALSE(u.IsSampler());
156 EXPECT_FALSE(u.IsComparisonSampler());
157 EXPECT_TRUE(u.IsTexture());
158 EXPECT_FALSE(u.IsSampledTexture());
159 EXPECT_FALSE(u.IsMultisampledTexture());
160 EXPECT_FALSE(u.IsDepthTexture());
161 EXPECT_FALSE(u.IsStorageReadTexture());
162 EXPECT_FALSE(u.IsStorageWriteTexture());
163
164 ss << u;
165 EXPECT_THAT(ss.str(), Eq("Usage(Texture( ))"));
166
167 auto copy(u);
168 u.AddTexture();
169 EXPECT_TRUE(u == copy);
170 }
171
TEST_F(SpvParserTest,Usage_AddSampledTexture)172 TEST_F(SpvParserTest, Usage_AddSampledTexture) {
173 std::ostringstream ss;
174 Usage u;
175 u.AddSampledTexture();
176
177 EXPECT_TRUE(u.IsValid());
178 EXPECT_TRUE(u.IsComplete());
179 EXPECT_FALSE(u.IsSampler());
180 EXPECT_FALSE(u.IsComparisonSampler());
181 EXPECT_TRUE(u.IsTexture());
182 EXPECT_TRUE(u.IsSampledTexture());
183 EXPECT_FALSE(u.IsMultisampledTexture());
184 EXPECT_FALSE(u.IsDepthTexture());
185 EXPECT_FALSE(u.IsStorageReadTexture());
186 EXPECT_FALSE(u.IsStorageWriteTexture());
187
188 ss << u;
189 EXPECT_THAT(ss.str(), Eq("Usage(Texture( is_sampled ))"));
190
191 auto copy(u);
192 u.AddSampledTexture();
193 EXPECT_TRUE(u == copy);
194 }
195
TEST_F(SpvParserTest,Usage_AddMultisampledTexture)196 TEST_F(SpvParserTest, Usage_AddMultisampledTexture) {
197 std::ostringstream ss;
198 Usage u;
199 u.AddMultisampledTexture();
200
201 EXPECT_TRUE(u.IsValid());
202 EXPECT_TRUE(u.IsComplete());
203 EXPECT_FALSE(u.IsSampler());
204 EXPECT_FALSE(u.IsComparisonSampler());
205 EXPECT_TRUE(u.IsTexture());
206 EXPECT_TRUE(u.IsSampledTexture());
207 EXPECT_TRUE(u.IsMultisampledTexture());
208 EXPECT_FALSE(u.IsDepthTexture());
209 EXPECT_FALSE(u.IsStorageReadTexture());
210 EXPECT_FALSE(u.IsStorageWriteTexture());
211
212 ss << u;
213 EXPECT_THAT(ss.str(), Eq("Usage(Texture( is_sampled ms ))"));
214
215 auto copy(u);
216 u.AddMultisampledTexture();
217 EXPECT_TRUE(u == copy);
218 }
219
TEST_F(SpvParserTest,Usage_AddDepthTexture)220 TEST_F(SpvParserTest, Usage_AddDepthTexture) {
221 std::ostringstream ss;
222 Usage u;
223 u.AddDepthTexture();
224
225 EXPECT_TRUE(u.IsValid());
226 EXPECT_TRUE(u.IsComplete());
227 EXPECT_FALSE(u.IsSampler());
228 EXPECT_FALSE(u.IsComparisonSampler());
229 EXPECT_TRUE(u.IsTexture());
230 EXPECT_TRUE(u.IsSampledTexture());
231 EXPECT_FALSE(u.IsMultisampledTexture());
232 EXPECT_TRUE(u.IsDepthTexture());
233 EXPECT_FALSE(u.IsStorageReadTexture());
234 EXPECT_FALSE(u.IsStorageWriteTexture());
235
236 ss << u;
237 EXPECT_THAT(ss.str(), Eq("Usage(Texture( is_sampled depth ))"));
238
239 auto copy(u);
240 u.AddDepthTexture();
241 EXPECT_TRUE(u == copy);
242 }
243
TEST_F(SpvParserTest,Usage_AddStorageReadTexture)244 TEST_F(SpvParserTest, Usage_AddStorageReadTexture) {
245 std::ostringstream ss;
246 Usage u;
247 u.AddStorageReadTexture();
248
249 EXPECT_TRUE(u.IsValid());
250 EXPECT_TRUE(u.IsComplete());
251 EXPECT_FALSE(u.IsSampler());
252 EXPECT_FALSE(u.IsComparisonSampler());
253 EXPECT_TRUE(u.IsTexture());
254 EXPECT_FALSE(u.IsSampledTexture());
255 EXPECT_FALSE(u.IsMultisampledTexture());
256 EXPECT_FALSE(u.IsDepthTexture());
257 EXPECT_TRUE(u.IsStorageReadTexture());
258 EXPECT_FALSE(u.IsStorageWriteTexture());
259
260 ss << u;
261 EXPECT_THAT(ss.str(), Eq("Usage(Texture( read ))"));
262
263 auto copy(u);
264 u.AddStorageReadTexture();
265 EXPECT_TRUE(u == copy);
266 }
267
TEST_F(SpvParserTest,Usage_AddStorageWriteTexture)268 TEST_F(SpvParserTest, Usage_AddStorageWriteTexture) {
269 std::ostringstream ss;
270 Usage u;
271 u.AddStorageWriteTexture();
272
273 EXPECT_TRUE(u.IsValid());
274 EXPECT_TRUE(u.IsComplete());
275 EXPECT_FALSE(u.IsSampler());
276 EXPECT_FALSE(u.IsComparisonSampler());
277 EXPECT_TRUE(u.IsTexture());
278 EXPECT_FALSE(u.IsSampledTexture());
279 EXPECT_FALSE(u.IsMultisampledTexture());
280 EXPECT_FALSE(u.IsDepthTexture());
281 EXPECT_FALSE(u.IsStorageReadTexture());
282 EXPECT_TRUE(u.IsStorageWriteTexture());
283
284 ss << u;
285 EXPECT_THAT(ss.str(), Eq("Usage(Texture( write ))"));
286
287 auto copy(u);
288 u.AddStorageWriteTexture();
289 EXPECT_TRUE(u == copy);
290 }
291
292 } // namespace
293 } // namespace spirv
294 } // namespace reader
295 } // namespace tint
296