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