bgfx/3rdparty/spirv-tools/source/spirv_target_env.cpp

426 lines
13 KiB
C++
Raw Normal View History

2018-04-11 05:44:28 +03:00
// 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.
2018-09-03 07:14:20 +03:00
#include "source/spirv_target_env.h"
2021-01-16 07:33:45 +03:00
#include <cassert>
2018-04-11 05:44:28 +03:00
#include <cstring>
2019-05-18 22:27:37 +03:00
#include <string>
2023-03-17 06:27:10 +03:00
#include <utility>
2018-04-11 05:44:28 +03:00
2018-09-03 07:14:20 +03:00
#include "source/spirv_constant.h"
2018-04-11 05:44:28 +03:00
#include "spirv-tools/libspirv.h"
const char* spvTargetEnvDescription(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
return "SPIR-V 1.0";
case SPV_ENV_VULKAN_1_0:
return "SPIR-V 1.0 (under Vulkan 1.0 semantics)";
case SPV_ENV_UNIVERSAL_1_1:
return "SPIR-V 1.1";
case SPV_ENV_OPENCL_1_2:
return "SPIR-V 1.0 (under OpenCL 1.2 Full Profile semantics)";
case SPV_ENV_OPENCL_EMBEDDED_1_2:
return "SPIR-V 1.0 (under OpenCL 1.2 Embedded Profile semantics)";
case SPV_ENV_OPENCL_2_0:
return "SPIR-V 1.0 (under OpenCL 2.0 Full Profile semantics)";
case SPV_ENV_OPENCL_EMBEDDED_2_0:
return "SPIR-V 1.0 (under OpenCL 2.0 Embedded Profile semantics)";
case SPV_ENV_OPENCL_2_1:
return "SPIR-V 1.0 (under OpenCL 2.1 Full Profile semantics)";
case SPV_ENV_OPENCL_EMBEDDED_2_1:
return "SPIR-V 1.0 (under OpenCL 2.1 Embedded Profile semantics)";
case SPV_ENV_OPENCL_2_2:
return "SPIR-V 1.2 (under OpenCL 2.2 Full Profile semantics)";
case SPV_ENV_OPENCL_EMBEDDED_2_2:
return "SPIR-V 1.2 (under OpenCL 2.2 Embedded Profile semantics)";
case SPV_ENV_OPENGL_4_0:
return "SPIR-V 1.0 (under OpenGL 4.0 semantics)";
case SPV_ENV_OPENGL_4_1:
return "SPIR-V 1.0 (under OpenGL 4.1 semantics)";
case SPV_ENV_OPENGL_4_2:
return "SPIR-V 1.0 (under OpenGL 4.2 semantics)";
case SPV_ENV_OPENGL_4_3:
return "SPIR-V 1.0 (under OpenGL 4.3 semantics)";
case SPV_ENV_OPENGL_4_5:
return "SPIR-V 1.0 (under OpenGL 4.5 semantics)";
case SPV_ENV_UNIVERSAL_1_2:
return "SPIR-V 1.2";
case SPV_ENV_UNIVERSAL_1_3:
return "SPIR-V 1.3";
case SPV_ENV_VULKAN_1_1:
return "SPIR-V 1.3 (under Vulkan 1.1 semantics)";
2018-09-03 07:14:20 +03:00
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
2021-01-16 07:33:45 +03:00
break;
2019-05-18 22:27:37 +03:00
case SPV_ENV_UNIVERSAL_1_4:
return "SPIR-V 1.4";
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
return "SPIR-V 1.4 (under Vulkan 1.1 semantics)";
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_5:
return "SPIR-V 1.5";
2020-02-05 08:33:12 +03:00
case SPV_ENV_VULKAN_1_2:
return "SPIR-V 1.5 (under Vulkan 1.2 semantics)";
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
return "SPIR-V 1.6";
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
return "SPIR-V 1.6 (under Vulkan 1.3 semantics)";
2021-11-01 06:37:36 +03:00
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
break;
2018-04-11 05:44:28 +03:00
}
return "";
}
uint32_t spvVersionForTargetEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5:
return SPV_SPIRV_VERSION_WORD(1, 0);
case SPV_ENV_UNIVERSAL_1_1:
return SPV_SPIRV_VERSION_WORD(1, 1);
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_OPENCL_2_2:
case SPV_ENV_OPENCL_EMBEDDED_2_2:
return SPV_SPIRV_VERSION_WORD(1, 2);
case SPV_ENV_UNIVERSAL_1_3:
case SPV_ENV_VULKAN_1_1:
return SPV_SPIRV_VERSION_WORD(1, 3);
2021-01-16 07:33:45 +03:00
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
2021-01-16 07:33:45 +03:00
break;
2019-05-18 22:27:37 +03:00
case SPV_ENV_UNIVERSAL_1_4:
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
return SPV_SPIRV_VERSION_WORD(1, 4);
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_5:
2020-02-05 08:33:12 +03:00
case SPV_ENV_VULKAN_1_2:
2019-09-14 17:09:33 +03:00
return SPV_SPIRV_VERSION_WORD(1, 5);
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
2021-12-24 06:15:20 +03:00
return SPV_SPIRV_VERSION_WORD(1, 6);
2021-11-01 06:37:36 +03:00
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
break;
2018-04-11 05:44:28 +03:00
}
return SPV_SPIRV_VERSION_WORD(0, 0);
}
2019-06-22 17:30:33 +03:00
static const std::pair<const char*, spv_target_env> spvTargetEnvNameMap[] = {
{"vulkan1.1spv1.4", SPV_ENV_VULKAN_1_1_SPIRV_1_4},
{"vulkan1.0", SPV_ENV_VULKAN_1_0},
{"vulkan1.1", SPV_ENV_VULKAN_1_1},
2020-02-05 08:33:12 +03:00
{"vulkan1.2", SPV_ENV_VULKAN_1_2},
2022-01-29 07:07:21 +03:00
{"vulkan1.3", SPV_ENV_VULKAN_1_3},
2019-06-22 17:30:33 +03:00
{"spv1.0", SPV_ENV_UNIVERSAL_1_0},
{"spv1.1", SPV_ENV_UNIVERSAL_1_1},
{"spv1.2", SPV_ENV_UNIVERSAL_1_2},
{"spv1.3", SPV_ENV_UNIVERSAL_1_3},
{"spv1.4", SPV_ENV_UNIVERSAL_1_4},
2019-09-14 17:09:33 +03:00
{"spv1.5", SPV_ENV_UNIVERSAL_1_5},
2021-12-24 06:15:20 +03:00
{"spv1.6", SPV_ENV_UNIVERSAL_1_6},
2019-06-22 17:30:33 +03:00
{"opencl1.2embedded", SPV_ENV_OPENCL_EMBEDDED_1_2},
{"opencl1.2", SPV_ENV_OPENCL_1_2},
{"opencl2.0embedded", SPV_ENV_OPENCL_EMBEDDED_2_0},
{"opencl2.0", SPV_ENV_OPENCL_2_0},
{"opencl2.1embedded", SPV_ENV_OPENCL_EMBEDDED_2_1},
{"opencl2.1", SPV_ENV_OPENCL_2_1},
{"opencl2.2embedded", SPV_ENV_OPENCL_EMBEDDED_2_2},
{"opencl2.2", SPV_ENV_OPENCL_2_2},
{"opengl4.0", SPV_ENV_OPENGL_4_0},
{"opengl4.1", SPV_ENV_OPENGL_4_1},
{"opengl4.2", SPV_ENV_OPENGL_4_2},
{"opengl4.3", SPV_ENV_OPENGL_4_3},
{"opengl4.5", SPV_ENV_OPENGL_4_5},
};
2018-04-11 05:44:28 +03:00
bool spvParseTargetEnv(const char* s, spv_target_env* env) {
auto match = [s](const char* b) {
return s && (0 == strncmp(s, b, strlen(b)));
};
2019-06-22 17:30:33 +03:00
for (auto& name_env : spvTargetEnvNameMap) {
if (match(name_env.first)) {
if (env) {
*env = name_env.second;
}
return true;
}
2018-04-11 05:44:28 +03:00
}
2019-06-22 17:30:33 +03:00
if (env) *env = SPV_ENV_UNIVERSAL_1_0;
return false;
2018-04-11 05:44:28 +03:00
}
2020-02-05 08:33:12 +03:00
#define VULKAN_VER(MAJOR, MINOR) ((MAJOR << 22) | (MINOR << 12))
#define SPIRV_VER(MAJOR, MINOR) ((MAJOR << 16) | (MINOR << 8))
struct VulkanEnv {
spv_target_env vulkan_env;
uint32_t vulkan_ver;
uint32_t spirv_ver;
};
// Maps each Vulkan target environment enum to the Vulkan version, and the
// maximum supported SPIR-V version for that Vulkan environment.
// Keep this ordered from least capable to most capable.
static const VulkanEnv ordered_vulkan_envs[] = {
{SPV_ENV_VULKAN_1_0, VULKAN_VER(1, 0), SPIRV_VER(1, 0)},
{SPV_ENV_VULKAN_1_1, VULKAN_VER(1, 1), SPIRV_VER(1, 3)},
{SPV_ENV_VULKAN_1_1_SPIRV_1_4, VULKAN_VER(1, 1), SPIRV_VER(1, 4)},
2022-01-29 07:07:21 +03:00
{SPV_ENV_VULKAN_1_2, VULKAN_VER(1, 2), SPIRV_VER(1, 5)},
{SPV_ENV_VULKAN_1_3, VULKAN_VER(1, 3), SPIRV_VER(1, 6)}};
2020-02-05 08:33:12 +03:00
bool spvParseVulkanEnv(uint32_t vulkan_ver, uint32_t spirv_ver,
spv_target_env* env) {
for (auto triple : ordered_vulkan_envs) {
if (triple.vulkan_ver >= vulkan_ver && triple.spirv_ver >= spirv_ver) {
*env = triple.vulkan_env;
return true;
}
}
return false;
}
2018-04-11 05:44:28 +03:00
bool spvIsVulkanEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_OPENCL_2_2:
case SPV_ENV_OPENCL_EMBEDDED_2_2:
case SPV_ENV_UNIVERSAL_1_3:
2019-05-18 22:27:37 +03:00
case SPV_ENV_UNIVERSAL_1_4:
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_5:
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
2018-04-11 05:44:28 +03:00
return false;
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_VULKAN_1_1:
2019-05-18 22:27:37 +03:00
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
2020-02-05 08:33:12 +03:00
case SPV_ENV_VULKAN_1_2:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
2018-04-11 05:44:28 +03:00
return true;
2021-01-16 07:33:45 +03:00
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
break;
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
2021-01-16 07:33:45 +03:00
break;
2018-04-11 05:44:28 +03:00
}
return false;
}
2019-01-05 19:15:11 +03:00
bool spvIsOpenCLEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_UNIVERSAL_1_3:
case SPV_ENV_VULKAN_1_1:
2019-05-18 22:27:37 +03:00
case SPV_ENV_UNIVERSAL_1_4:
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_5:
2020-02-05 08:33:12 +03:00
case SPV_ENV_VULKAN_1_2:
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
2019-01-05 19:15:11 +03:00
return false;
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_2:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_2_2:
return true;
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
break;
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
2021-01-16 07:33:45 +03:00
break;
2019-01-05 19:15:11 +03:00
}
return false;
}
2019-01-26 09:09:33 +03:00
2019-07-03 18:08:41 +03:00
bool spvIsOpenGLEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_UNIVERSAL_1_3:
case SPV_ENV_VULKAN_1_1:
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_2:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_2_2:
case SPV_ENV_UNIVERSAL_1_4:
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_5:
2020-02-05 08:33:12 +03:00
case SPV_ENV_VULKAN_1_2:
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
2019-07-03 18:08:41 +03:00
return false;
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5:
return true;
2021-01-16 07:33:45 +03:00
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
break;
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
break;
}
return false;
}
bool spvIsValidEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_UNIVERSAL_1_3:
case SPV_ENV_VULKAN_1_1:
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_2:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_2_2:
case SPV_ENV_UNIVERSAL_1_4:
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
case SPV_ENV_UNIVERSAL_1_5:
case SPV_ENV_VULKAN_1_2:
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_6:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_3:
2021-11-01 06:37:36 +03:00
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5:
return true;
case SPV_ENV_WEBGPU_0:
case SPV_ENV_MAX:
2021-01-16 07:33:45 +03:00
break;
2019-07-03 18:08:41 +03:00
}
return false;
}
2019-01-26 09:09:33 +03:00
std::string spvLogStringForEnv(spv_target_env env) {
switch (env) {
case SPV_ENV_OPENCL_1_2:
case SPV_ENV_OPENCL_2_0:
case SPV_ENV_OPENCL_2_1:
case SPV_ENV_OPENCL_2_2:
case SPV_ENV_OPENCL_EMBEDDED_1_2:
case SPV_ENV_OPENCL_EMBEDDED_2_0:
case SPV_ENV_OPENCL_EMBEDDED_2_1:
case SPV_ENV_OPENCL_EMBEDDED_2_2: {
return "OpenCL";
}
case SPV_ENV_OPENGL_4_0:
case SPV_ENV_OPENGL_4_1:
case SPV_ENV_OPENGL_4_2:
case SPV_ENV_OPENGL_4_3:
case SPV_ENV_OPENGL_4_5: {
return "OpenGL";
}
case SPV_ENV_VULKAN_1_0:
2019-05-18 22:27:37 +03:00
case SPV_ENV_VULKAN_1_1:
2021-12-24 06:15:20 +03:00
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
2022-01-29 07:07:21 +03:00
case SPV_ENV_VULKAN_1_2:
case SPV_ENV_VULKAN_1_3: {
2021-12-24 06:15:20 +03:00
return "Vulkan";
2019-01-26 09:09:33 +03:00
}
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
2019-05-18 22:27:37 +03:00
case SPV_ENV_UNIVERSAL_1_3:
2019-09-14 17:09:33 +03:00
case SPV_ENV_UNIVERSAL_1_4:
2021-12-24 06:15:20 +03:00
case SPV_ENV_UNIVERSAL_1_5:
case SPV_ENV_UNIVERSAL_1_6: {
2019-01-26 09:09:33 +03:00
return "Universal";
}
2021-01-16 07:33:45 +03:00
case SPV_ENV_WEBGPU_0:
2021-11-01 06:37:36 +03:00
assert(false && "Deprecated target environment value.");
break;
case SPV_ENV_MAX:
assert(false && "Invalid target environment value.");
2021-01-16 07:33:45 +03:00
break;
2019-01-26 09:09:33 +03:00
}
return "Unknown";
}
2019-06-22 17:30:33 +03:00
std::string spvTargetEnvList(const int pad, const int wrap) {
std::string ret;
size_t max_line_len = wrap - pad; // The first line isn't padded
std::string line;
std::string sep = "";
for (auto& name_env : spvTargetEnvNameMap) {
std::string word = sep + name_env.first;
if (line.length() + word.length() > max_line_len) {
// Adding one word wouldn't fit, commit the line in progress and
// start a new one.
ret += line + "\n";
line.assign(pad, ' ');
// The first line is done. The max length now comprises the
// padding.
max_line_len = wrap;
}
line += word;
sep = "|";
}
ret += line;
return ret;
}