aboutsummaryrefslogtreecommitdiff
path: root/test/text_to_binary.image_test.cpp
blob: 3e7a560adee3e60e0adce8e06de37d9874ec58af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2015-2016 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Assembler tests for instructions in the "Image Instructions" section of
// the SPIR-V spec.

#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"

namespace spvtools {
namespace {

using spvtest::MakeInstruction;
using spvtest::TextToBinaryTest;
using ::testing::Eq;

// An example case for a mask value with operands.
struct ImageOperandsCase {
  std::string image_operands;
  // The expected mask, followed by its operands.
  std::vector<uint32_t> expected_mask_and_operands;
};

// Test all kinds of image operands.

using ImageOperandsTest =
    spvtest::TextToBinaryTestBase<::testing::TestWithParam<ImageOperandsCase>>;

TEST_P(ImageOperandsTest, Sample) {
  const std::string input =
      "%2 = OpImageFetch %1 %3 %4" + GetParam().image_operands + "\n";
  EXPECT_THAT(CompiledInstructions(input),
              Eq(MakeInstruction(spv::Op::OpImageFetch, {1, 2, 3, 4},
                                 GetParam().expected_mask_and_operands)));
}

#define MASK(NAME) uint32_t(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
    TextToBinaryImageOperandsAny, ImageOperandsTest,
    ::testing::ValuesIn(std::vector<ImageOperandsCase>{
        // TODO(dneto): Rev32 adds many more values, and rearranges their
        // values.
        // Image operands are optional.
        {"", {}},
        // Test each kind, alone.
        {" Bias %5", {MASK(Bias), 5}},
        {" Lod %5", {MASK(Lod), 5}},
        {" Grad %5 %6", {MASK(Grad), 5, 6}},
        {" ConstOffset %5", {MASK(ConstOffset), 5}},
        {" Offset %5", {MASK(Offset), 5}},
        {" ConstOffsets %5", {MASK(ConstOffsets), 5}},
        {" Sample %5", {MASK(Sample), 5}},
        {" MinLod %5", {MASK(MinLod), 5}},
    }));
#undef MASK
#define MASK(NAME) static_cast<uint32_t>(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
    TextToBinaryImageOperandsCombination, ImageOperandsTest,
    ::testing::ValuesIn(std::vector<ImageOperandsCase>{
        // TODO(dneto): Rev32 adds many more values, and rearranges their
        // values.
        // Test adjacent pairs, so we can easily debug the values when it fails.
        {" Bias|Lod %5 %6", {MASK(Bias) | MASK(Lod), 5, 6}},
        {" Lod|Grad %5 %6 %7", {MASK(Lod) | MASK(Grad), 5, 6, 7}},
        {" Grad|ConstOffset %5 %6 %7",
         {MASK(Grad) | MASK(ConstOffset), 5, 6, 7}},
        {" ConstOffset|Offset %5 %6", {MASK(ConstOffset) | MASK(Offset), 5, 6}},
        {" Offset|ConstOffsets %5 %6",
         {MASK(Offset) | MASK(ConstOffsets), 5, 6}},
        {" ConstOffsets|Sample %5 %6",
         {MASK(ConstOffsets) | MASK(Sample), 5, 6}},
        // Test all masks together.
        {" Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample"
         " %5 %6 %7 %8 %9 %10 %11 %12",
         {MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
              MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
          5, 6, 7, 8, 9, 10, 11, 12}},
        // The same, but with mask value names reversed.
        {" Sample|ConstOffsets|Offset|ConstOffset|Grad|Lod|Bias"
         " %5 %6 %7 %8 %9 %10 %11 %12",
         {MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
              MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
          5, 6, 7, 8, 9, 10, 11, 12}}}));
#undef MASK

TEST_F(ImageOperandsTest, WrongOperand) {
  EXPECT_THAT(CompileFailure("%r = OpImageFetch %t %i %c xxyyzz"),
              Eq("Invalid image operand 'xxyyzz'."));
}

// Test OpImage

using OpImageTest = TextToBinaryTest;

TEST_F(OpImageTest, Valid) {
  const std::string input = "%2 = OpImage %1 %3\n";
  EXPECT_THAT(CompiledInstructions(input),
              Eq(MakeInstruction(spv::Op::OpImage, {1, 2, 3})));

  // Test the disassembler.
  EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}

TEST_F(OpImageTest, InvalidTypeOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImage 42"),
              Eq("Expected id to start with %."));
}

TEST_F(OpImageTest, MissingSampledImageOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImage %1"),
              Eq("Expected operand for OpImage instruction, but found the end "
                 "of the stream."));
}

TEST_F(OpImageTest, InvalidSampledImageOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImage %1 1000"),
              Eq("Expected id to start with %."));
}

TEST_F(OpImageTest, TooManyOperands) {
  // We should improve this message, to say what instruction we're trying to
  // parse.
  EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 %4"),  // an Id
              Eq("Expected '=', found end of stream."));

  EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 99"),  // a number
              Eq("Expected <opcode> or <result-id> at the beginning of an "
                 "instruction, found '99'."));
  EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 \"abc\""),  // a string
              Eq("Expected <opcode> or <result-id> at the beginning of an "
                 "instruction, found '\"abc\"'."));
}

// Test OpImageSparseRead

using OpImageSparseReadTest = TextToBinaryTest;

TEST_F(OpImageSparseReadTest, OnlyRequiredOperands) {
  const std::string input = "%2 = OpImageSparseRead %1 %3 %4\n";
  EXPECT_THAT(CompiledInstructions(input),
              Eq(MakeInstruction(spv::Op::OpImageSparseRead, {1, 2, 3, 4})));
  // Test the disassembler.
  EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}

// Test all kinds of image operands on OpImageSparseRead

using ImageSparseReadImageOperandsTest =
    spvtest::TextToBinaryTestBase<::testing::TestWithParam<ImageOperandsCase>>;

TEST_P(ImageSparseReadImageOperandsTest, Sample) {
  const std::string input =
      "%2 = OpImageSparseRead %1 %3 %4" + GetParam().image_operands + "\n";
  EXPECT_THAT(CompiledInstructions(input),
              Eq(MakeInstruction(spv::Op::OpImageSparseRead, {1, 2, 3, 4},
                                 GetParam().expected_mask_and_operands)));
  // Test the disassembler.
  EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}

#define MASK(NAME) uint32_t(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(ImageSparseReadImageOperandsAny,
                         ImageSparseReadImageOperandsTest,
                         ::testing::ValuesIn(std::vector<ImageOperandsCase>{
                             // Image operands are optional.
                             {"", {}},
                             // Test each kind, alone.
                             {" Bias %5", {MASK(Bias), 5}},
                             {" Lod %5", {MASK(Lod), 5}},
                             {" Grad %5 %6", {MASK(Grad), 5, 6}},
                             {" ConstOffset %5", {MASK(ConstOffset), 5}},
                             {" Offset %5", {MASK(Offset), 5}},
                             {" ConstOffsets %5", {MASK(ConstOffsets), 5}},
                             {" Sample %5", {MASK(Sample), 5}},
                             {" MinLod %5", {MASK(MinLod), 5}},
                         }));
#undef MASK
#define MASK(NAME) static_cast<uint32_t>(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
    ImageSparseReadImageOperandsCombination, ImageSparseReadImageOperandsTest,
    ::testing::ValuesIn(std::vector<ImageOperandsCase>{
        // values.
        // Test adjacent pairs, so we can easily debug the values when it fails.
        {" Bias|Lod %5 %6", {MASK(Bias) | MASK(Lod), 5, 6}},
        {" Lod|Grad %5 %6 %7", {MASK(Lod) | MASK(Grad), 5, 6, 7}},
        {" Grad|ConstOffset %5 %6 %7",
         {MASK(Grad) | MASK(ConstOffset), 5, 6, 7}},
        {" ConstOffset|Offset %5 %6", {MASK(ConstOffset) | MASK(Offset), 5, 6}},
        {" Offset|ConstOffsets %5 %6",
         {MASK(Offset) | MASK(ConstOffsets), 5, 6}},
        {" ConstOffsets|Sample %5 %6",
         {MASK(ConstOffsets) | MASK(Sample), 5, 6}},
        // Test all masks together.
        {" Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample"
         " %5 %6 %7 %8 %9 %10 %11 %12",
         {MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
              MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
          5, 6, 7, 8, 9, 10, 11, 12}},
        // Don't try the masks reversed, since this is a round trip test,
        // and the disassembler will sort them.
    }));
#undef MASK

TEST_F(OpImageSparseReadTest, InvalidTypeOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead 42"),
              Eq("Expected id to start with %."));
}

TEST_F(OpImageSparseReadTest, MissingImageOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1"),
              Eq("Expected operand for OpImageSparseRead instruction, but "
                 "found the end of the stream."));
}

TEST_F(OpImageSparseReadTest, InvalidImageOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 1000"),
              Eq("Expected id to start with %."));
}

TEST_F(OpImageSparseReadTest, MissingCoordinateOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 %2"),
              Eq("Expected operand for OpImageSparseRead instruction, but "
                 "found the end of the stream."));
}

TEST_F(OpImageSparseReadTest, InvalidCoordinateOperand) {
  EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 %2 1000"),
              Eq("Expected id to start with %."));
}

// TODO(dneto): OpSampledImage
// TODO(dneto): OpImageSampleImplicitLod
// TODO(dneto): OpImageSampleExplicitLod
// TODO(dneto): OpImageSampleDrefImplicitLod
// TODO(dneto): OpImageSampleDrefExplicitLod
// TODO(dneto): OpImageSampleProjImplicitLod
// TODO(dneto): OpImageSampleProjExplicitLod
// TODO(dneto): OpImageSampleProjDrefImplicitLod
// TODO(dneto): OpImageSampleProjDrefExplicitLod
// TODO(dneto): OpImageGather
// TODO(dneto): OpImageDrefGather
// TODO(dneto): OpImageRead
// TODO(dneto): OpImageWrite
// TODO(dneto): OpImageQueryFormat
// TODO(dneto): OpImageQueryOrder
// TODO(dneto): OpImageQuerySizeLod
// TODO(dneto): OpImageQuerySize
// TODO(dneto): OpImageQueryLod
// TODO(dneto): OpImageQueryLevels
// TODO(dneto): OpImageQuerySamples
// TODO(dneto): OpImageSparseSampleImplicitLod
// TODO(dneto): OpImageSparseSampleExplicitLod
// TODO(dneto): OpImageSparseSampleDrefImplicitLod
// TODO(dneto): OpImageSparseSampleDrefExplicitLod
// TODO(dneto): OpImageSparseSampleProjImplicitLod
// TODO(dneto): OpImageSparseSampleProjExplicitLod
// TODO(dneto): OpImageSparseSampleProjDrefImplicitLod
// TODO(dneto): OpImageSparseSampleProjDrefExplicitLod
// TODO(dneto): OpImageSparseFetch
// TODO(dneto): OpImageSparseDrefGather
// TODO(dneto): OpImageSparseTexelsResident

}  // namespace
}  // namespace spvtools