2016-08-05 22:19:30 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-08-05 22:19:30 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-05 22:19:30 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-08-05 22:19:30 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <vector>
|
2016-08-05 22:19:30 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "gmock/gmock.h"
|
2016-08-05 22:19:30 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "test/unit_spirv.h"
|
2016-08-05 22:19:30 +00:00
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
namespace spvtools {
|
2016-08-05 22:19:30 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ::testing::AnyOf;
|
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::StartsWith;
|
2017-11-27 15:16:41 +00:00
|
|
|
using ::testing::ValuesIn;
|
2016-08-05 22:19:30 +00:00
|
|
|
|
|
|
|
using TargetEnvTest = ::testing::TestWithParam<spv_target_env>;
|
|
|
|
TEST_P(TargetEnvTest, CreateContext) {
|
|
|
|
spv_target_env env = GetParam();
|
|
|
|
spv_context context = spvContextCreate(env);
|
|
|
|
ASSERT_NE(nullptr, context);
|
2017-11-27 15:16:41 +00:00
|
|
|
spvContextDestroy(context); // Avoid leaking
|
2016-08-05 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TargetEnvTest, ValidDescription) {
|
|
|
|
const char* description = spvTargetEnvDescription(GetParam());
|
|
|
|
ASSERT_NE(nullptr, description);
|
|
|
|
ASSERT_THAT(description, StartsWith("SPIR-V "));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TargetEnvTest, ValidSpirvVersion) {
|
|
|
|
auto spirv_version = spvVersionForTargetEnv(GetParam());
|
2018-02-09 19:29:02 +00:00
|
|
|
ASSERT_THAT(spirv_version, AnyOf(0x10000, 0x10100, 0x10200, 0x10300));
|
2016-08-05 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(AllTargetEnvs, TargetEnvTest,
|
|
|
|
ValuesIn(spvtest::AllTargetEnvironments()));
|
2016-08-05 22:19:30 +00:00
|
|
|
|
|
|
|
TEST(GetContextTest, InvalidTargetEnvProducesNull) {
|
2017-03-14 16:43:41 +00:00
|
|
|
// Use a value beyond the last valid enum value.
|
2018-02-09 19:29:02 +00:00
|
|
|
spv_context context = spvContextCreate(static_cast<spv_target_env>(30));
|
2016-08-05 22:19:30 +00:00
|
|
|
EXPECT_EQ(context, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A test case for parsing an environment string.
|
|
|
|
struct ParseCase {
|
|
|
|
const char* input;
|
|
|
|
bool success; // Expect to successfully parse?
|
|
|
|
spv_target_env env; // The parsed environment, if successful.
|
|
|
|
};
|
|
|
|
|
|
|
|
using TargetParseTest = ::testing::TestWithParam<ParseCase>;
|
|
|
|
|
2020-01-23 22:20:32 +00:00
|
|
|
TEST_P(TargetParseTest, Samples) {
|
2016-08-05 22:19:30 +00:00
|
|
|
spv_target_env env;
|
|
|
|
bool parsed = spvParseTargetEnv(GetParam().input, &env);
|
|
|
|
EXPECT_THAT(parsed, Eq(GetParam().success));
|
2020-01-23 22:20:32 +00:00
|
|
|
if (parsed) {
|
|
|
|
EXPECT_THAT(env, Eq(GetParam().env));
|
|
|
|
}
|
2016-08-05 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2017-11-29 23:49:23 +00:00
|
|
|
TargetParsing, TargetParseTest,
|
|
|
|
ValuesIn(std::vector<ParseCase>{
|
|
|
|
{"spv1.0", true, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
{"spv1.1", true, SPV_ENV_UNIVERSAL_1_1},
|
|
|
|
{"spv1.2", true, SPV_ENV_UNIVERSAL_1_2},
|
2018-02-09 19:29:02 +00:00
|
|
|
{"spv1.3", true, SPV_ENV_UNIVERSAL_1_3},
|
2017-11-29 23:49:23 +00:00
|
|
|
{"vulkan1.0", true, SPV_ENV_VULKAN_1_0},
|
2018-02-09 19:29:02 +00:00
|
|
|
{"vulkan1.1", true, SPV_ENV_VULKAN_1_1},
|
2019-08-28 22:49:33 +00:00
|
|
|
{"vulkan1.2", true, SPV_ENV_VULKAN_1_2},
|
2017-11-29 23:49:23 +00:00
|
|
|
{"opencl2.1", true, SPV_ENV_OPENCL_2_1},
|
|
|
|
{"opencl2.2", true, SPV_ENV_OPENCL_2_2},
|
|
|
|
{"opengl4.0", true, SPV_ENV_OPENGL_4_0},
|
|
|
|
{"opengl4.1", true, SPV_ENV_OPENGL_4_1},
|
|
|
|
{"opengl4.2", true, SPV_ENV_OPENGL_4_2},
|
|
|
|
{"opengl4.3", true, SPV_ENV_OPENGL_4_3},
|
|
|
|
{"opengl4.5", true, SPV_ENV_OPENGL_4_5},
|
|
|
|
{"opencl1.2", true, SPV_ENV_OPENCL_1_2},
|
|
|
|
{"opencl1.2embedded", true, SPV_ENV_OPENCL_EMBEDDED_1_2},
|
|
|
|
{"opencl2.0", true, SPV_ENV_OPENCL_2_0},
|
|
|
|
{"opencl2.0embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_0},
|
|
|
|
{"opencl2.1embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_1},
|
|
|
|
{"opencl2.2embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_2},
|
2018-06-20 17:29:38 +00:00
|
|
|
{"webgpu0", true, SPV_ENV_WEBGPU_0},
|
2018-02-09 19:29:02 +00:00
|
|
|
{"opencl2.3", false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
{"opencl3.0", false, SPV_ENV_UNIVERSAL_1_0},
|
2019-08-28 22:49:33 +00:00
|
|
|
{"vulkan1.9", false, SPV_ENV_UNIVERSAL_1_0},
|
2018-02-09 19:29:02 +00:00
|
|
|
{"vulkan2.0", false, SPV_ENV_UNIVERSAL_1_0},
|
2017-11-29 23:49:23 +00:00
|
|
|
{nullptr, false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
{"", false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
{"abc", false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
}));
|
2016-08-05 22:19:30 +00:00
|
|
|
|
2020-01-23 22:20:32 +00:00
|
|
|
// A test case for parsing an environment string.
|
|
|
|
struct ParseVulkanCase {
|
|
|
|
uint32_t vulkan;
|
|
|
|
uint32_t spirv;
|
|
|
|
bool success; // Expect to successfully parse?
|
|
|
|
spv_target_env env; // The parsed environment, if successful.
|
|
|
|
};
|
|
|
|
|
|
|
|
using TargetParseVulkanTest = ::testing::TestWithParam<ParseVulkanCase>;
|
|
|
|
|
|
|
|
TEST_P(TargetParseVulkanTest, Samples) {
|
|
|
|
spv_target_env env;
|
|
|
|
bool parsed = spvParseVulkanEnv(GetParam().vulkan, GetParam().spirv, &env);
|
|
|
|
EXPECT_THAT(parsed, Eq(GetParam().success));
|
|
|
|
if (parsed) {
|
|
|
|
EXPECT_THAT(env, Eq(GetParam().env));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VK(MAJ, MIN) ((MAJ << 22) | (MIN << 12))
|
|
|
|
#define SPV(MAJ, MIN) ((MAJ << 16) | (MIN << 8))
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
TargetVulkanParsing, TargetParseVulkanTest,
|
|
|
|
ValuesIn(std::vector<ParseVulkanCase>{
|
|
|
|
// Vulkan 1.0 cases
|
|
|
|
{VK(1, 0), SPV(1, 0), true, SPV_ENV_VULKAN_1_0},
|
|
|
|
{VK(1, 0), SPV(1, 1), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 0), SPV(1, 2), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 0), SPV(1, 3), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 0), SPV(1, 4), true, SPV_ENV_VULKAN_1_1_SPIRV_1_4},
|
|
|
|
{VK(1, 0), SPV(1, 5), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 0), SPV(1, 6), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
// Vulkan 1.1 cases
|
|
|
|
{VK(1, 1), SPV(1, 0), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 1), SPV(1, 1), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 1), SPV(1, 2), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 1), SPV(1, 3), true, SPV_ENV_VULKAN_1_1},
|
|
|
|
{VK(1, 1), SPV(1, 4), true, SPV_ENV_VULKAN_1_1_SPIRV_1_4},
|
|
|
|
{VK(1, 1), SPV(1, 5), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 1), SPV(1, 6), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
// Vulkan 1.2 cases
|
|
|
|
{VK(1, 2), SPV(1, 0), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 1), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 2), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 3), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 4), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 5), true, SPV_ENV_VULKAN_1_2},
|
|
|
|
{VK(1, 2), SPV(1, 6), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
// Vulkan 1.3 cases
|
|
|
|
{VK(1, 3), SPV(1, 0), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
// Vulkan 2.0 cases
|
|
|
|
{VK(2, 0), SPV(1, 0), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
// Vulkan 99.0 cases
|
|
|
|
{VK(99, 0), SPV(1, 0), false, SPV_ENV_UNIVERSAL_1_0},
|
|
|
|
}));
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|