blob: 1589188fda2681a5c48b6a83c92034ac159383fe [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
David Netoabe51c42015-09-11 22:35:04 -04002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
David Netoabe51c42015-09-11 22:35:04 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
David Netoabe51c42015-09-11 22:35:04 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
David Netoabe51c42015-09-11 22:35:04 -040014
15// Assembler tests for instructions in the "Type-Declaration" section of the
16// SPIR-V spec.
17
dan sinclaireda2cfb2018-08-03 15:06:09 -040018#include <string>
19#include <vector>
David Netoabe51c42015-09-11 22:35:04 -040020
David Netod9ad0502015-11-24 18:37:24 -050021#include "gmock/gmock.h"
dan sinclaireda2cfb2018-08-03 15:06:09 -040022#include "test/test_fixture.h"
23#include "test/unit_spirv.h"
David Netoabe51c42015-09-11 22:35:04 -040024
dan sinclair2cce2c52018-07-11 09:24:49 -040025namespace spvtools {
David Netoabe51c42015-09-11 22:35:04 -040026namespace {
27
David Neto3d2bf532015-10-01 16:58:17 -040028using spvtest::EnumCase;
David Netoabe51c42015-09-11 22:35:04 -040029using spvtest::MakeInstruction;
30using ::testing::Eq;
31
David Netob30a0c52015-09-16 15:56:43 -040032// Test Dim enums via OpTypeImage
33
Dejan Mircevski52ff5342015-10-09 16:48:28 -040034using DimTest =
Lei Zhangb36e7042015-10-28 13:40:52 -040035 spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvDim>>>;
David Netob30a0c52015-09-16 15:56:43 -040036
37TEST_P(DimTest, AnyDim) {
David Netod9ad0502015-11-24 18:37:24 -050038 const std::string input =
39 "%1 = OpTypeImage %2 " + GetParam().name() + " 2 3 0 4 Rgba8\n";
David Netob30a0c52015-09-16 15:56:43 -040040 EXPECT_THAT(
41 CompiledInstructions(input),
Lei Zhang1a0334e2015-11-02 09:41:20 -050042 Eq(MakeInstruction(SpvOpTypeImage, {1, 2, GetParam().value(), 2, 3, 0, 4,
43 SpvImageFormatRgba8})));
David Netod9ad0502015-11-24 18:37:24 -050044
45 // Check the disassembler as well.
46 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
David Netob30a0c52015-09-16 15:56:43 -040047}
48
49// clang-format off
Lei Zhangb36e7042015-10-28 13:40:52 -040050#define CASE(NAME) {SpvDim##NAME, #NAME}
Steven Perron464111e2019-01-29 18:56:52 -050051INSTANTIATE_TEST_SUITE_P(
David Netob30a0c52015-09-16 15:56:43 -040052 TextToBinaryDim, DimTest,
Lei Zhangb36e7042015-10-28 13:40:52 -040053 ::testing::ValuesIn(std::vector<EnumCase<SpvDim>>{
David Netob30a0c52015-09-16 15:56:43 -040054 CASE(1D),
55 CASE(2D),
56 CASE(3D),
57 CASE(Cube),
58 CASE(Rect),
59 CASE(Buffer),
David Netod02f68a2015-11-11 12:32:21 -050060 CASE(SubpassData),
Steven Perron464111e2019-01-29 18:56:52 -050061 }));
David Netob30a0c52015-09-16 15:56:43 -040062#undef CASE
63// clang-format on
64
Dejan Mircevski2fd37922015-10-13 11:46:09 -040065TEST_F(DimTest, WrongDim) {
66 EXPECT_THAT(CompileFailure("%i = OpTypeImage %t xxyyzz 1 2 3 4 R8"),
67 Eq("Invalid dimensionality 'xxyyzz'."));
68}
69
David Netob30a0c52015-09-16 15:56:43 -040070// Test ImageFormat enums via OpTypeImage
71
David Neto1b5fd492015-09-21 11:36:44 -040072using ImageFormatTest = spvtest::TextToBinaryTestBase<
Lei Zhangb36e7042015-10-28 13:40:52 -040073 ::testing::TestWithParam<EnumCase<SpvImageFormat>>>;
David Netob30a0c52015-09-16 15:56:43 -040074
David Neto2889a0c2016-02-15 13:50:00 -050075TEST_P(ImageFormatTest, AnyImageFormatAndNoAccessQualifier) {
Dejan Mircevskid062f8b2015-10-13 12:46:13 -040076 const std::string input =
David Netod9ad0502015-11-24 18:37:24 -050077 "%1 = OpTypeImage %2 1D 2 3 0 4 " + GetParam().name() + "\n";
David Netob30a0c52015-09-16 15:56:43 -040078 EXPECT_THAT(CompiledInstructions(input),
Lei Zhang1a0334e2015-11-02 09:41:20 -050079 Eq(MakeInstruction(SpvOpTypeImage, {1, 2, SpvDim1D, 2, 3, 0, 4,
80 GetParam().value()})));
David Netod9ad0502015-11-24 18:37:24 -050081 // Check the disassembler as well.
82 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
David Netob30a0c52015-09-16 15:56:43 -040083}
84
85// clang-format off
Lei Zhangb36e7042015-10-28 13:40:52 -040086#define CASE(NAME) {SpvImageFormat##NAME, #NAME}
Steven Perron464111e2019-01-29 18:56:52 -050087INSTANTIATE_TEST_SUITE_P(
David Netob30a0c52015-09-16 15:56:43 -040088 TextToBinaryImageFormat, ImageFormatTest,
Lei Zhangb36e7042015-10-28 13:40:52 -040089 ::testing::ValuesIn(std::vector<EnumCase<SpvImageFormat>>{
David Netob30a0c52015-09-16 15:56:43 -040090 CASE(Unknown),
91 CASE(Rgba32f),
92 CASE(Rgba16f),
93 CASE(R32f),
94 CASE(Rgba8),
95 CASE(Rgba8Snorm),
96 CASE(Rg32f),
97 CASE(Rg16f),
98 CASE(R11fG11fB10f),
99 CASE(R16f),
100 CASE(Rgba16),
101 CASE(Rgb10A2),
102 CASE(Rg16),
103 CASE(Rg8),
104 CASE(R16),
105 CASE(R8),
106 CASE(Rgba16Snorm),
107 CASE(Rg16Snorm),
108 CASE(Rg8Snorm),
109 CASE(R16Snorm),
110 CASE(R8Snorm),
111 CASE(Rgba32i),
112 CASE(Rgba16i),
113 CASE(Rgba8i),
114 CASE(R32i),
115 CASE(Rg32i),
116 CASE(Rg16i),
117 CASE(Rg8i),
118 CASE(R16i),
119 CASE(R8i),
120 CASE(Rgba32ui),
121 CASE(Rgba16ui),
122 CASE(Rgba8ui),
123 CASE(R32ui),
124 CASE(Rgb10a2ui),
125 CASE(Rg32ui),
126 CASE(Rg16ui),
127 CASE(Rg8ui),
128 CASE(R16ui),
129 CASE(R8ui),
Steven Perron464111e2019-01-29 18:56:52 -0500130 }));
David Netob30a0c52015-09-16 15:56:43 -0400131#undef CASE
132// clang-format on
133
Dejan Mircevski971b3442015-10-13 12:54:47 -0400134TEST_F(ImageFormatTest, WrongFormat) {
135 EXPECT_THAT(CompileFailure("%r = OpTypeImage %t 1D 2 3 0 4 xxyyzz"),
136 Eq("Invalid image format 'xxyyzz'."));
137}
138
David Neto2889a0c2016-02-15 13:50:00 -0500139// Test AccessQualifier enums via OpTypeImage.
140using ImageAccessQualifierTest = spvtest::TextToBinaryTestBase<
141 ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
142
143TEST_P(ImageAccessQualifierTest, AnyAccessQualifier) {
144 const std::string input =
145 "%1 = OpTypeImage %2 1D 2 3 0 4 Rgba8 " + GetParam().name() + "\n";
146 EXPECT_THAT(CompiledInstructions(input),
147 Eq(MakeInstruction(SpvOpTypeImage,
148 {1, 2, SpvDim1D, 2, 3, 0, 4,
149 SpvImageFormatRgba8, GetParam().value()})));
150 // Check the disassembler as well.
151 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
152}
153
154// clang-format off
155#define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
Steven Perron464111e2019-01-29 18:56:52 -0500156INSTANTIATE_TEST_SUITE_P(
David Neto2889a0c2016-02-15 13:50:00 -0500157 AccessQualifier, ImageAccessQualifierTest,
158 ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
159 CASE(ReadOnly),
160 CASE(WriteOnly),
161 CASE(ReadWrite),
Steven Perron464111e2019-01-29 18:56:52 -0500162 }));
David Neto2889a0c2016-02-15 13:50:00 -0500163// clang-format on
164#undef CASE
165
David Netob30a0c52015-09-16 15:56:43 -0400166// Test AccessQualifier enums via OpTypePipe.
David Netof7ee0ca2015-09-16 15:29:02 -0400167
David Neto1b5fd492015-09-21 11:36:44 -0400168using OpTypePipeTest = spvtest::TextToBinaryTestBase<
Lei Zhangb36e7042015-10-28 13:40:52 -0400169 ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
David Netoabe51c42015-09-11 22:35:04 -0400170
171TEST_P(OpTypePipeTest, AnyAccessQualifier) {
David Neto2889a0c2016-02-15 13:50:00 -0500172 const std::string input = "%1 = OpTypePipe " + GetParam().name() + "\n";
David Netoe0890da2015-09-24 15:45:59 -0400173 EXPECT_THAT(CompiledInstructions(input),
Lei Zhangb36e7042015-10-28 13:40:52 -0400174 Eq(MakeInstruction(SpvOpTypePipe, {1, GetParam().value()})));
David Neto2889a0c2016-02-15 13:50:00 -0500175 // Check the disassembler as well.
176 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
David Netoabe51c42015-09-11 22:35:04 -0400177}
178
179// clang-format off
Lei Zhangb36e7042015-10-28 13:40:52 -0400180#define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
Steven Perron464111e2019-01-29 18:56:52 -0500181INSTANTIATE_TEST_SUITE_P(
David Netof7ee0ca2015-09-16 15:29:02 -0400182 TextToBinaryTypePipe, OpTypePipeTest,
Lei Zhangb36e7042015-10-28 13:40:52 -0400183 ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
Andrew Woloszynf2d07752015-09-18 11:50:54 -0400184 CASE(ReadOnly),
185 CASE(WriteOnly),
186 CASE(ReadWrite),
Steven Perron464111e2019-01-29 18:56:52 -0500187 }));
David Netoabe51c42015-09-11 22:35:04 -0400188#undef CASE
189// clang-format on
190
Dejan Mircevski10fa49c2015-10-13 15:25:31 -0400191TEST_F(OpTypePipeTest, WrongAccessQualifier) {
192 EXPECT_THAT(CompileFailure("%1 = OpTypePipe xxyyzz"),
193 Eq("Invalid access qualifier 'xxyyzz'."));
194}
195
Dejan Mircevski52ff5342015-10-09 16:48:28 -0400196using OpTypeForwardPointerTest = spvtest::TextToBinaryTest;
197
198#define CASE(storage_class) \
199 do { \
200 EXPECT_THAT( \
201 CompiledInstructions("OpTypeForwardPointer %pt " #storage_class), \
Lei Zhangb36e7042015-10-28 13:40:52 -0400202 Eq(MakeInstruction(SpvOpTypeForwardPointer, \
203 {1, SpvStorageClass##storage_class}))); \
Dejan Mircevski52ff5342015-10-09 16:48:28 -0400204 } while (0)
205
206TEST_F(OpTypeForwardPointerTest, ValidStorageClass) {
207 CASE(UniformConstant);
208 CASE(Input);
209 CASE(Uniform);
210 CASE(Output);
David Netod02f68a2015-11-11 12:32:21 -0500211 CASE(Workgroup);
212 CASE(CrossWorkgroup);
213 CASE(Private);
Dejan Mircevski52ff5342015-10-09 16:48:28 -0400214 CASE(Function);
215 CASE(Generic);
216 CASE(PushConstant);
217 CASE(AtomicCounter);
218 CASE(Image);
David Netobf68c812017-03-25 21:12:22 -0400219 CASE(StorageBuffer);
Dejan Mircevski52ff5342015-10-09 16:48:28 -0400220}
221
222#undef CASE
223
224TEST_F(OpTypeForwardPointerTest, MissingType) {
225 EXPECT_THAT(CompileFailure("OpTypeForwardPointer"),
226 Eq("Expected operand, found end of stream."));
227}
228
229TEST_F(OpTypeForwardPointerTest, MissingClass) {
230 EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt"),
231 Eq("Expected operand, found end of stream."));
232}
233
234TEST_F(OpTypeForwardPointerTest, WrongClass) {
235 EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt xxyyzz"),
236 Eq("Invalid storage class 'xxyyzz'."));
237}
238
Dejan Mircevski45d391b2016-04-27 15:41:58 -0400239using OpSizeOfTest = spvtest::TextToBinaryTest;
240
David Neto8d65c892018-06-19 09:54:33 -0400241// We should be able to assemble it. Validation checks are in another test
242// file.
243TEST_F(OpSizeOfTest, OpcodeAssemblesInV10) {
244 EXPECT_THAT(
245 CompiledInstructions("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_0),
246 Eq(MakeInstruction(SpvOpSizeOf, {1, 2, 3})));
Dejan Mircevski45d391b2016-04-27 15:41:58 -0400247}
248
249TEST_F(OpSizeOfTest, ArgumentCount) {
250 EXPECT_THAT(
251 CompileFailure("OpSizeOf", SPV_ENV_UNIVERSAL_1_1),
252 Eq("Expected <result-id> at the beginning of an instruction, found "
253 "'OpSizeOf'."));
254 EXPECT_THAT(CompileFailure("%res = OpSizeOf OpNop", SPV_ENV_UNIVERSAL_1_1),
255 Eq("Expected operand, found next instruction instead."));
256 EXPECT_THAT(
257 CompiledInstructions("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_1),
258 Eq(MakeInstruction(SpvOpSizeOf, {1, 2, 3})));
259 EXPECT_THAT(
260 CompileFailure("%1 = OpSizeOf %2 %3 44 55 ", SPV_ENV_UNIVERSAL_1_1),
261 Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
262 "found '44'."));
263}
264
265TEST_F(OpSizeOfTest, ArgumentTypes) {
266 EXPECT_THAT(CompileFailure("%1 = OpSizeOf 2 %3", SPV_ENV_UNIVERSAL_1_1),
267 Eq("Expected id to start with %."));
268 EXPECT_THAT(CompileFailure("%1 = OpSizeOf %2 \"abc\"", SPV_ENV_UNIVERSAL_1_1),
269 Eq("Expected id to start with %."));
270}
271
David Netoabe51c42015-09-11 22:35:04 -0400272// TODO(dneto): OpTypeVoid
273// TODO(dneto): OpTypeBool
274// TODO(dneto): OpTypeInt
275// TODO(dneto): OpTypeFloat
276// TODO(dneto): OpTypeVector
277// TODO(dneto): OpTypeMatrix
278// TODO(dneto): OpTypeImage
279// TODO(dneto): OpTypeSampler
280// TODO(dneto): OpTypeSampledImage
281// TODO(dneto): OpTypeArray
282// TODO(dneto): OpTypeRuntimeArray
283// TODO(dneto): OpTypeStruct
284// TODO(dneto): OpTypeOpaque
285// TODO(dneto): OpTypePointer
286// TODO(dneto): OpTypeFunction
287// TODO(dneto): OpTypeEvent
288// TODO(dneto): OpTypeDeviceEvent
289// TODO(dneto): OpTypeReserveId
290// TODO(dneto): OpTypeQueue
David Netoabe51c42015-09-11 22:35:04 -0400291
dan sinclair2cce2c52018-07-11 09:24:49 -0400292} // namespace
293} // namespace spvtools