Setup dependent external sources
[platform/upstream/VK-GL-CTS.git] / external / spirv-tools / src / test / text_to_binary.type_declaration_test.cpp
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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 // Assembler tests for instructions in the "Type-Declaration" section of the
16 // SPIR-V spec.
17
18 #include "unit_spirv.h"
19
20 #include "test_fixture.h"
21 #include "gmock/gmock.h"
22
23 namespace {
24
25 using spvtest::EnumCase;
26 using spvtest::MakeInstruction;
27 using ::testing::Eq;
28
29 // Test Dim enums via OpTypeImage
30
31 using DimTest =
32     spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvDim>>>;
33
34 TEST_P(DimTest, AnyDim) {
35   const std::string input =
36       "%1 = OpTypeImage %2 " + GetParam().name() + " 2 3 0 4 Rgba8\n";
37   EXPECT_THAT(
38       CompiledInstructions(input),
39       Eq(MakeInstruction(SpvOpTypeImage, {1, 2, GetParam().value(), 2, 3, 0, 4,
40                                           SpvImageFormatRgba8})));
41
42   // Check the disassembler as well.
43   EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
44 }
45
46 // clang-format off
47 #define CASE(NAME) {SpvDim##NAME, #NAME}
48 INSTANTIATE_TEST_CASE_P(
49     TextToBinaryDim, DimTest,
50     ::testing::ValuesIn(std::vector<EnumCase<SpvDim>>{
51         CASE(1D),
52         CASE(2D),
53         CASE(3D),
54         CASE(Cube),
55         CASE(Rect),
56         CASE(Buffer),
57         CASE(SubpassData),
58     }),);
59 #undef CASE
60 // clang-format on
61
62 TEST_F(DimTest, WrongDim) {
63   EXPECT_THAT(CompileFailure("%i = OpTypeImage %t xxyyzz 1 2 3 4 R8"),
64               Eq("Invalid dimensionality 'xxyyzz'."));
65 }
66
67 // Test ImageFormat enums via OpTypeImage
68
69 using ImageFormatTest = spvtest::TextToBinaryTestBase<
70     ::testing::TestWithParam<EnumCase<SpvImageFormat>>>;
71
72 TEST_P(ImageFormatTest, AnyImageFormatAndNoAccessQualifier) {
73   const std::string input =
74       "%1 = OpTypeImage %2 1D 2 3 0 4 " + GetParam().name() + "\n";
75   EXPECT_THAT(CompiledInstructions(input),
76               Eq(MakeInstruction(SpvOpTypeImage, {1, 2, SpvDim1D, 2, 3, 0, 4,
77                                                   GetParam().value()})));
78   // Check the disassembler as well.
79   EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
80 }
81
82 // clang-format off
83 #define CASE(NAME) {SpvImageFormat##NAME, #NAME}
84 INSTANTIATE_TEST_CASE_P(
85     TextToBinaryImageFormat, ImageFormatTest,
86     ::testing::ValuesIn(std::vector<EnumCase<SpvImageFormat>>{
87         CASE(Unknown),
88         CASE(Rgba32f),
89         CASE(Rgba16f),
90         CASE(R32f),
91         CASE(Rgba8),
92         CASE(Rgba8Snorm),
93         CASE(Rg32f),
94         CASE(Rg16f),
95         CASE(R11fG11fB10f),
96         CASE(R16f),
97         CASE(Rgba16),
98         CASE(Rgb10A2),
99         CASE(Rg16),
100         CASE(Rg8),
101         CASE(R16),
102         CASE(R8),
103         CASE(Rgba16Snorm),
104         CASE(Rg16Snorm),
105         CASE(Rg8Snorm),
106         CASE(R16Snorm),
107         CASE(R8Snorm),
108         CASE(Rgba32i),
109         CASE(Rgba16i),
110         CASE(Rgba8i),
111         CASE(R32i),
112         CASE(Rg32i),
113         CASE(Rg16i),
114         CASE(Rg8i),
115         CASE(R16i),
116         CASE(R8i),
117         CASE(Rgba32ui),
118         CASE(Rgba16ui),
119         CASE(Rgba8ui),
120         CASE(R32ui),
121         CASE(Rgb10a2ui),
122         CASE(Rg32ui),
123         CASE(Rg16ui),
124         CASE(Rg8ui),
125         CASE(R16ui),
126         CASE(R8ui),
127     }),);
128 #undef CASE
129 // clang-format on
130
131 TEST_F(ImageFormatTest, WrongFormat) {
132   EXPECT_THAT(CompileFailure("%r = OpTypeImage %t 1D  2 3 0 4 xxyyzz"),
133               Eq("Invalid image format 'xxyyzz'."));
134 }
135
136 // Test AccessQualifier enums via OpTypeImage.
137 using ImageAccessQualifierTest = spvtest::TextToBinaryTestBase<
138     ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
139
140 TEST_P(ImageAccessQualifierTest, AnyAccessQualifier) {
141   const std::string input =
142       "%1 = OpTypeImage %2 1D 2 3 0 4 Rgba8 " + GetParam().name() + "\n";
143   EXPECT_THAT(CompiledInstructions(input),
144               Eq(MakeInstruction(SpvOpTypeImage,
145                                  {1, 2, SpvDim1D, 2, 3, 0, 4,
146                                   SpvImageFormatRgba8, GetParam().value()})));
147   // Check the disassembler as well.
148   EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
149 }
150
151 // clang-format off
152 #define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
153 INSTANTIATE_TEST_CASE_P(
154     AccessQualifier, ImageAccessQualifierTest,
155     ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
156       CASE(ReadOnly),
157       CASE(WriteOnly),
158       CASE(ReadWrite),
159     }),);
160 // clang-format on
161 #undef CASE
162
163 // Test AccessQualifier enums via OpTypePipe.
164
165 using OpTypePipeTest = spvtest::TextToBinaryTestBase<
166     ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
167
168 TEST_P(OpTypePipeTest, AnyAccessQualifier) {
169   const std::string input = "%1 = OpTypePipe " + GetParam().name() + "\n";
170   EXPECT_THAT(CompiledInstructions(input),
171               Eq(MakeInstruction(SpvOpTypePipe, {1, GetParam().value()})));
172   // Check the disassembler as well.
173   EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
174 }
175
176 // clang-format off
177 #define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
178 INSTANTIATE_TEST_CASE_P(
179     TextToBinaryTypePipe, OpTypePipeTest,
180     ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
181                             CASE(ReadOnly),
182                             CASE(WriteOnly),
183                             CASE(ReadWrite),
184     }),);
185 #undef CASE
186 // clang-format on
187
188 TEST_F(OpTypePipeTest, WrongAccessQualifier) {
189   EXPECT_THAT(CompileFailure("%1 = OpTypePipe xxyyzz"),
190               Eq("Invalid access qualifier 'xxyyzz'."));
191 }
192
193 using OpTypeForwardPointerTest = spvtest::TextToBinaryTest;
194
195 #define CASE(storage_class)                                               \
196   do {                                                                    \
197     EXPECT_THAT(                                                          \
198         CompiledInstructions("OpTypeForwardPointer %pt " #storage_class), \
199         Eq(MakeInstruction(SpvOpTypeForwardPointer,                       \
200                            {1, SpvStorageClass##storage_class})));        \
201   } while (0)
202
203 TEST_F(OpTypeForwardPointerTest, ValidStorageClass) {
204   CASE(UniformConstant);
205   CASE(Input);
206   CASE(Uniform);
207   CASE(Output);
208   CASE(Workgroup);
209   CASE(CrossWorkgroup);
210   CASE(Private);
211   CASE(Function);
212   CASE(Generic);
213   CASE(PushConstant);
214   CASE(AtomicCounter);
215   CASE(Image);
216   CASE(StorageBuffer);
217 }
218
219 #undef CASE
220
221 TEST_F(OpTypeForwardPointerTest, MissingType) {
222   EXPECT_THAT(CompileFailure("OpTypeForwardPointer"),
223               Eq("Expected operand, found end of stream."));
224 }
225
226 TEST_F(OpTypeForwardPointerTest, MissingClass) {
227   EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt"),
228               Eq("Expected operand, found end of stream."));
229 }
230
231 TEST_F(OpTypeForwardPointerTest, WrongClass) {
232   EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt xxyyzz"),
233               Eq("Invalid storage class 'xxyyzz'."));
234 }
235
236 using OpSizeOfTest = spvtest::TextToBinaryTest;
237
238 TEST_F(OpSizeOfTest, OpcodeUnrecognizedInV10) {
239   EXPECT_THAT(CompileFailure("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_0),
240               Eq("Invalid Opcode name 'OpSizeOf'"));
241 }
242
243 TEST_F(OpSizeOfTest, ArgumentCount) {
244   EXPECT_THAT(
245       CompileFailure("OpSizeOf", SPV_ENV_UNIVERSAL_1_1),
246       Eq("Expected <result-id> at the beginning of an instruction, found "
247          "'OpSizeOf'."));
248   EXPECT_THAT(CompileFailure("%res = OpSizeOf OpNop", SPV_ENV_UNIVERSAL_1_1),
249               Eq("Expected operand, found next instruction instead."));
250   EXPECT_THAT(
251       CompiledInstructions("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_1),
252       Eq(MakeInstruction(SpvOpSizeOf, {1, 2, 3})));
253   EXPECT_THAT(
254       CompileFailure("%1 = OpSizeOf %2 %3 44 55 ", SPV_ENV_UNIVERSAL_1_1),
255       Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
256          "found '44'."));
257 }
258
259 TEST_F(OpSizeOfTest, ArgumentTypes) {
260   EXPECT_THAT(CompileFailure("%1 = OpSizeOf 2 %3", SPV_ENV_UNIVERSAL_1_1),
261               Eq("Expected id to start with %."));
262   EXPECT_THAT(CompileFailure("%1 = OpSizeOf %2 \"abc\"", SPV_ENV_UNIVERSAL_1_1),
263               Eq("Expected id to start with %."));
264 }
265
266 // TODO(dneto): OpTypeVoid
267 // TODO(dneto): OpTypeBool
268 // TODO(dneto): OpTypeInt
269 // TODO(dneto): OpTypeFloat
270 // TODO(dneto): OpTypeVector
271 // TODO(dneto): OpTypeMatrix
272 // TODO(dneto): OpTypeImage
273 // TODO(dneto): OpTypeSampler
274 // TODO(dneto): OpTypeSampledImage
275 // TODO(dneto): OpTypeArray
276 // TODO(dneto): OpTypeRuntimeArray
277 // TODO(dneto): OpTypeStruct
278 // TODO(dneto): OpTypeOpaque
279 // TODO(dneto): OpTypePointer
280 // TODO(dneto): OpTypeFunction
281 // TODO(dneto): OpTypeEvent
282 // TODO(dneto): OpTypeDeviceEvent
283 // TODO(dneto): OpTypeReserveId
284 // TODO(dneto): OpTypeQueue
285
286 }  // anonymous namespace