blob: 9d777bc4f6547535b38c7292c83f5735c988f452 [file] [log] [blame]
Lei Zhang6fa3f8a2016-03-31 17:26:31 -04001// Copyright (c) 2015-2016 The Khronos Group Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// 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 Zhang6fa3f8a2016-03-31 17:26:31 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhang6fa3f8a2016-03-31 17:26:31 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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 Zhang6fa3f8a2016-03-31 17:26:31 -040014
Dejan Mircevski27030392016-05-11 09:48:52 -040015#include <cassert>
16#include <cstring>
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040017
18#include "spirv-tools/libspirv.h"
19#include "spirv_constant.h"
20
21const char* spvTargetEnvDescription(spv_target_env env) {
22 switch (env) {
23 case SPV_ENV_UNIVERSAL_1_0:
24 return "SPIR-V 1.0";
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040025 case SPV_ENV_VULKAN_1_0:
26 return "SPIR-V 1.0 (under Vulkan 1.0 semantics)";
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040027 case SPV_ENV_UNIVERSAL_1_1:
28 return "SPIR-V 1.1";
Pierre Moreau12447d82017-11-30 00:49:23 +010029 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 Netoc2967012016-08-05 18:19:30 -040037 case SPV_ENV_OPENCL_2_1:
Pierre Moreau12447d82017-11-30 00:49:23 +010038 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 Netoc2967012016-08-05 18:19:30 -040041 case SPV_ENV_OPENCL_2_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010042 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 Netoc2967012016-08-05 18:19:30 -040045 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 Netodbc20492017-03-14 12:43:41 -040055 case SPV_ENV_UNIVERSAL_1_2:
56 return "SPIR-V 1.2";
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040057 }
58 assert(0 && "Unhandled SPIR-V target environment");
59 return "";
60}
61
62uint32_t spvVersionForTargetEnv(spv_target_env env) {
63 switch (env) {
64 case SPV_ENV_UNIVERSAL_1_0:
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040065 case SPV_ENV_VULKAN_1_0:
Pierre Moreau12447d82017-11-30 00:49:23 +010066 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 Netoc2967012016-08-05 18:19:30 -040070 case SPV_ENV_OPENCL_2_1:
Pierre Moreau12447d82017-11-30 00:49:23 +010071 case SPV_ENV_OPENCL_EMBEDDED_2_1:
David Netoc2967012016-08-05 18:19:30 -040072 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 Zhang6fa3f8a2016-03-31 17:26:31 -040077 return SPV_SPIRV_VERSION_WORD(1, 0);
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040078 case SPV_ENV_UNIVERSAL_1_1:
79 return SPV_SPIRV_VERSION_WORD(1, 1);
David Netodbc20492017-03-14 12:43:41 -040080 case SPV_ENV_UNIVERSAL_1_2:
81 case SPV_ENV_OPENCL_2_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010082 case SPV_ENV_OPENCL_EMBEDDED_2_2:
David Netodbc20492017-03-14 12:43:41 -040083 return SPV_SPIRV_VERSION_WORD(1, 2);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040084 }
85 assert(0 && "Unhandled SPIR-V target environment");
86 return SPV_SPIRV_VERSION_WORD(0, 0);
87}
Dejan Mircevski27030392016-05-11 09:48:52 -040088
89bool spvParseTargetEnv(const char* s, spv_target_env* env) {
David Netoc2967012016-08-05 18:19:30 -040090 auto match = [s](const char* b) {
91 return s && (0 == strncmp(s, b, strlen(b)));
92 };
93 if (match("vulkan1.0")) {
Dejan Mircevski27030392016-05-11 09:48:52 -040094 if (env) *env = SPV_ENV_VULKAN_1_0;
95 return true;
David Netoc2967012016-08-05 18:19:30 -040096 } else if (match("spv1.0")) {
Dejan Mircevski27030392016-05-11 09:48:52 -040097 if (env) *env = SPV_ENV_UNIVERSAL_1_0;
98 return true;
David Netoc2967012016-08-05 18:19:30 -040099 } else if (match("spv1.1")) {
Dejan Mircevski27030392016-05-11 09:48:52 -0400100 if (env) *env = SPV_ENV_UNIVERSAL_1_1;
101 return true;
David Netodbc20492017-03-14 12:43:41 -0400102 } else if (match("spv1.2")) {
103 if (env) *env = SPV_ENV_UNIVERSAL_1_2;
104 return true;
Pierre Moreau12447d82017-11-30 00:49:23 +0100105 } 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 Netoc2967012016-08-05 18:19:30 -0400120 } else if (match("opencl2.1")) {
121 if (env) *env = SPV_ENV_OPENCL_2_1;
122 return true;
Pierre Moreau12447d82017-11-30 00:49:23 +0100123 } else if (match("opencl2.2embedded")) {
124 if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_2;
125 return true;
David Netoc2967012016-08-05 18:19:30 -0400126 } 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 Mircevski27030392016-05-11 09:48:52 -0400144 } else {
145 if (env) *env = SPV_ENV_UNIVERSAL_1_0;
146 return false;
147 }
148}
Andrey Tuganovbdc78372018-01-23 15:02:27 -0500149
150bool 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}