Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 14 | |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 15 | #include <cassert> |
| 16 | #include <cstring> |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 17 | |
| 18 | #include "spirv-tools/libspirv.h" |
| 19 | #include "spirv_constant.h" |
| 20 | |
| 21 | const char* spvTargetEnvDescription(spv_target_env env) { |
| 22 | switch (env) { |
| 23 | case SPV_ENV_UNIVERSAL_1_0: |
| 24 | return "SPIR-V 1.0"; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 25 | case SPV_ENV_VULKAN_1_0: |
| 26 | return "SPIR-V 1.0 (under Vulkan 1.0 semantics)"; |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 27 | case SPV_ENV_UNIVERSAL_1_1: |
| 28 | return "SPIR-V 1.1"; |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 29 | case SPV_ENV_OPENCL_1_2: |
| 30 | return "SPIR-V 1.0 (under OpenCL 1.2 Full Profile semantics)"; |
| 31 | case SPV_ENV_OPENCL_EMBEDDED_1_2: |
| 32 | return "SPIR-V 1.0 (under OpenCL 1.2 Embedded Profile semantics)"; |
| 33 | case SPV_ENV_OPENCL_2_0: |
| 34 | return "SPIR-V 1.0 (under OpenCL 2.0 Full Profile semantics)"; |
| 35 | case SPV_ENV_OPENCL_EMBEDDED_2_0: |
| 36 | return "SPIR-V 1.0 (under OpenCL 2.0 Embedded Profile semantics)"; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 37 | case SPV_ENV_OPENCL_2_1: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 38 | return "SPIR-V 1.0 (under OpenCL 2.1 Full Profile semantics)"; |
| 39 | case SPV_ENV_OPENCL_EMBEDDED_2_1: |
| 40 | return "SPIR-V 1.0 (under OpenCL 2.1 Embedded Profile semantics)"; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 41 | case SPV_ENV_OPENCL_2_2: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 42 | return "SPIR-V 1.1 (under OpenCL 2.2 Full Profile semantics)"; |
| 43 | case SPV_ENV_OPENCL_EMBEDDED_2_2: |
| 44 | return "SPIR-V 1.1 (under OpenCL 2.2 Embedded Profile semantics)"; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 45 | case SPV_ENV_OPENGL_4_0: |
| 46 | return "SPIR-V 1.0 (under OpenCL 4.0 semantics)"; |
| 47 | case SPV_ENV_OPENGL_4_1: |
| 48 | return "SPIR-V 1.0 (under OpenCL 4.1 semantics)"; |
| 49 | case SPV_ENV_OPENGL_4_2: |
| 50 | return "SPIR-V 1.0 (under OpenCL 4.2 semantics)"; |
| 51 | case SPV_ENV_OPENGL_4_3: |
| 52 | return "SPIR-V 1.0 (under OpenCL 4.3 semantics)"; |
| 53 | case SPV_ENV_OPENGL_4_5: |
| 54 | return "SPIR-V 1.0 (under OpenCL 4.5 semantics)"; |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 55 | case SPV_ENV_UNIVERSAL_1_2: |
| 56 | return "SPIR-V 1.2"; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 57 | } |
| 58 | assert(0 && "Unhandled SPIR-V target environment"); |
| 59 | return ""; |
| 60 | } |
| 61 | |
| 62 | uint32_t spvVersionForTargetEnv(spv_target_env env) { |
| 63 | switch (env) { |
| 64 | case SPV_ENV_UNIVERSAL_1_0: |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 65 | case SPV_ENV_VULKAN_1_0: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 66 | case SPV_ENV_OPENCL_1_2: |
| 67 | case SPV_ENV_OPENCL_EMBEDDED_1_2: |
| 68 | case SPV_ENV_OPENCL_2_0: |
| 69 | case SPV_ENV_OPENCL_EMBEDDED_2_0: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 70 | case SPV_ENV_OPENCL_2_1: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 71 | case SPV_ENV_OPENCL_EMBEDDED_2_1: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 72 | case SPV_ENV_OPENGL_4_0: |
| 73 | case SPV_ENV_OPENGL_4_1: |
| 74 | case SPV_ENV_OPENGL_4_2: |
| 75 | case SPV_ENV_OPENGL_4_3: |
| 76 | case SPV_ENV_OPENGL_4_5: |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 77 | return SPV_SPIRV_VERSION_WORD(1, 0); |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 78 | case SPV_ENV_UNIVERSAL_1_1: |
| 79 | return SPV_SPIRV_VERSION_WORD(1, 1); |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 80 | case SPV_ENV_UNIVERSAL_1_2: |
| 81 | case SPV_ENV_OPENCL_2_2: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 82 | case SPV_ENV_OPENCL_EMBEDDED_2_2: |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 83 | return SPV_SPIRV_VERSION_WORD(1, 2); |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 84 | } |
| 85 | assert(0 && "Unhandled SPIR-V target environment"); |
| 86 | return SPV_SPIRV_VERSION_WORD(0, 0); |
| 87 | } |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 88 | |
| 89 | bool spvParseTargetEnv(const char* s, spv_target_env* env) { |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 90 | auto match = [s](const char* b) { |
| 91 | return s && (0 == strncmp(s, b, strlen(b))); |
| 92 | }; |
| 93 | if (match("vulkan1.0")) { |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 94 | if (env) *env = SPV_ENV_VULKAN_1_0; |
| 95 | return true; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 96 | } else if (match("spv1.0")) { |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 97 | if (env) *env = SPV_ENV_UNIVERSAL_1_0; |
| 98 | return true; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 99 | } else if (match("spv1.1")) { |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 100 | if (env) *env = SPV_ENV_UNIVERSAL_1_1; |
| 101 | return true; |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 102 | } else if (match("spv1.2")) { |
| 103 | if (env) *env = SPV_ENV_UNIVERSAL_1_2; |
| 104 | return true; |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 105 | } else if (match("opencl1.2embedded")) { |
| 106 | if (env) *env = SPV_ENV_OPENCL_EMBEDDED_1_2; |
| 107 | return true; |
| 108 | } else if (match("opencl1.2")) { |
| 109 | if (env) *env = SPV_ENV_OPENCL_1_2; |
| 110 | return true; |
| 111 | } else if (match("opencl2.0embedded")) { |
| 112 | if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_0; |
| 113 | return true; |
| 114 | } else if (match("opencl2.0")) { |
| 115 | if (env) *env = SPV_ENV_OPENCL_2_0; |
| 116 | return true; |
| 117 | } else if (match("opencl2.1embedded")) { |
| 118 | if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_1; |
| 119 | return true; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 120 | } else if (match("opencl2.1")) { |
| 121 | if (env) *env = SPV_ENV_OPENCL_2_1; |
| 122 | return true; |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 123 | } else if (match("opencl2.2embedded")) { |
| 124 | if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_2; |
| 125 | return true; |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 126 | } else if (match("opencl2.2")) { |
| 127 | if (env) *env = SPV_ENV_OPENCL_2_2; |
| 128 | return true; |
| 129 | } else if (match("opengl4.0")) { |
| 130 | if (env) *env = SPV_ENV_OPENGL_4_0; |
| 131 | return true; |
| 132 | } else if (match("opengl4.1")) { |
| 133 | if (env) *env = SPV_ENV_OPENGL_4_1; |
| 134 | return true; |
| 135 | } else if (match("opengl4.2")) { |
| 136 | if (env) *env = SPV_ENV_OPENGL_4_2; |
| 137 | return true; |
| 138 | } else if (match("opengl4.3")) { |
| 139 | if (env) *env = SPV_ENV_OPENGL_4_3; |
| 140 | return true; |
| 141 | } else if (match("opengl4.5")) { |
| 142 | if (env) *env = SPV_ENV_OPENGL_4_5; |
| 143 | return true; |
Dejan Mircevski | 2703039 | 2016-05-11 09:48:52 -0400 | [diff] [blame] | 144 | } else { |
| 145 | if (env) *env = SPV_ENV_UNIVERSAL_1_0; |
| 146 | return false; |
| 147 | } |
| 148 | } |
Andrey Tuganov | bdc7837 | 2018-01-23 15:02:27 -0500 | [diff] [blame^] | 149 | |
| 150 | bool spvIsVulkanEnv(spv_target_env env) { |
| 151 | switch (env) { |
| 152 | case SPV_ENV_UNIVERSAL_1_0: |
| 153 | case SPV_ENV_OPENCL_1_2: |
| 154 | case SPV_ENV_OPENCL_EMBEDDED_1_2: |
| 155 | case SPV_ENV_OPENCL_2_0: |
| 156 | case SPV_ENV_OPENCL_EMBEDDED_2_0: |
| 157 | case SPV_ENV_OPENCL_2_1: |
| 158 | case SPV_ENV_OPENCL_EMBEDDED_2_1: |
| 159 | case SPV_ENV_OPENGL_4_0: |
| 160 | case SPV_ENV_OPENGL_4_1: |
| 161 | case SPV_ENV_OPENGL_4_2: |
| 162 | case SPV_ENV_OPENGL_4_3: |
| 163 | case SPV_ENV_OPENGL_4_5: |
| 164 | case SPV_ENV_UNIVERSAL_1_1: |
| 165 | case SPV_ENV_UNIVERSAL_1_2: |
| 166 | case SPV_ENV_OPENCL_2_2: |
| 167 | case SPV_ENV_OPENCL_EMBEDDED_2_2: |
| 168 | return false; |
| 169 | case SPV_ENV_VULKAN_1_0: |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |