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 #include <cassert> 16 #include <cstring> 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"; 25 case SPV_ENV_VULKAN_1_0: 26 return "SPIR-V 1.0 (under Vulkan 1.0 semantics)"; 27 case SPV_ENV_UNIVERSAL_1_1: 28 return "SPIR-V 1.1"; 29 case SPV_ENV_OPENCL_2_1: 30 return "SPIR-V 1.0 (under OpenCL 2.1 semantics)"; 31 case SPV_ENV_OPENCL_2_2: 32 return "SPIR-V 1.1 (under OpenCL 2.2 semantics)"; 33 case SPV_ENV_OPENGL_4_0: 34 return "SPIR-V 1.0 (under OpenCL 4.0 semantics)"; 35 case SPV_ENV_OPENGL_4_1: 36 return "SPIR-V 1.0 (under OpenCL 4.1 semantics)"; 37 case SPV_ENV_OPENGL_4_2: 38 return "SPIR-V 1.0 (under OpenCL 4.2 semantics)"; 39 case SPV_ENV_OPENGL_4_3: 40 return "SPIR-V 1.0 (under OpenCL 4.3 semantics)"; 41 case SPV_ENV_OPENGL_4_5: 42 return "SPIR-V 1.0 (under OpenCL 4.5 semantics)"; 43 case SPV_ENV_UNIVERSAL_1_2: 44 return "SPIR-V 1.2"; 45 } 46 assert(0 && "Unhandled SPIR-V target environment"); 47 return ""; 48 } 49 50 uint32_t spvVersionForTargetEnv(spv_target_env env) { 51 switch (env) { 52 case SPV_ENV_UNIVERSAL_1_0: 53 case SPV_ENV_VULKAN_1_0: 54 case SPV_ENV_OPENCL_2_1: 55 case SPV_ENV_OPENGL_4_0: 56 case SPV_ENV_OPENGL_4_1: 57 case SPV_ENV_OPENGL_4_2: 58 case SPV_ENV_OPENGL_4_3: 59 case SPV_ENV_OPENGL_4_5: 60 return SPV_SPIRV_VERSION_WORD(1, 0); 61 case SPV_ENV_UNIVERSAL_1_1: 62 return SPV_SPIRV_VERSION_WORD(1, 1); 63 case SPV_ENV_UNIVERSAL_1_2: 64 case SPV_ENV_OPENCL_2_2: 65 return SPV_SPIRV_VERSION_WORD(1, 2); 66 } 67 assert(0 && "Unhandled SPIR-V target environment"); 68 return SPV_SPIRV_VERSION_WORD(0, 0); 69 } 70 71 bool spvParseTargetEnv(const char* s, spv_target_env* env) { 72 auto match = [s](const char* b) { 73 return s && (0 == strncmp(s, b, strlen(b))); 74 }; 75 if (match("vulkan1.0")) { 76 if (env) *env = SPV_ENV_VULKAN_1_0; 77 return true; 78 } else if (match("spv1.0")) { 79 if (env) *env = SPV_ENV_UNIVERSAL_1_0; 80 return true; 81 } else if (match("spv1.1")) { 82 if (env) *env = SPV_ENV_UNIVERSAL_1_1; 83 return true; 84 } else if (match("spv1.2")) { 85 if (env) *env = SPV_ENV_UNIVERSAL_1_2; 86 return true; 87 } else if (match("opencl2.1")) { 88 if (env) *env = SPV_ENV_OPENCL_2_1; 89 return true; 90 } else if (match("opencl2.2")) { 91 if (env) *env = SPV_ENV_OPENCL_2_2; 92 return true; 93 } else if (match("opengl4.0")) { 94 if (env) *env = SPV_ENV_OPENGL_4_0; 95 return true; 96 } else if (match("opengl4.1")) { 97 if (env) *env = SPV_ENV_OPENGL_4_1; 98 return true; 99 } else if (match("opengl4.2")) { 100 if (env) *env = SPV_ENV_OPENGL_4_2; 101 return true; 102 } else if (match("opengl4.3")) { 103 if (env) *env = SPV_ENV_OPENGL_4_3; 104 return true; 105 } else if (match("opengl4.5")) { 106 if (env) *env = SPV_ENV_OPENGL_4_5; 107 return true; 108 } else { 109 if (env) *env = SPV_ENV_UNIVERSAL_1_0; 110 return false; 111 } 112 } 113