spirv_target_env.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "source/spirv_target_env.h"
  15. #include <cassert>
  16. #include <cstring>
  17. #include "source/spirv_constant.h"
  18. #include "spirv-tools/libspirv.h"
  19. const char* spvTargetEnvDescription(spv_target_env env) {
  20. switch (env) {
  21. case SPV_ENV_UNIVERSAL_1_0:
  22. return "SPIR-V 1.0";
  23. case SPV_ENV_VULKAN_1_0:
  24. return "SPIR-V 1.0 (under Vulkan 1.0 semantics)";
  25. case SPV_ENV_UNIVERSAL_1_1:
  26. return "SPIR-V 1.1";
  27. case SPV_ENV_OPENCL_1_2:
  28. return "SPIR-V 1.0 (under OpenCL 1.2 Full Profile semantics)";
  29. case SPV_ENV_OPENCL_EMBEDDED_1_2:
  30. return "SPIR-V 1.0 (under OpenCL 1.2 Embedded Profile semantics)";
  31. case SPV_ENV_OPENCL_2_0:
  32. return "SPIR-V 1.0 (under OpenCL 2.0 Full Profile semantics)";
  33. case SPV_ENV_OPENCL_EMBEDDED_2_0:
  34. return "SPIR-V 1.0 (under OpenCL 2.0 Embedded Profile semantics)";
  35. case SPV_ENV_OPENCL_2_1:
  36. return "SPIR-V 1.0 (under OpenCL 2.1 Full Profile semantics)";
  37. case SPV_ENV_OPENCL_EMBEDDED_2_1:
  38. return "SPIR-V 1.0 (under OpenCL 2.1 Embedded Profile semantics)";
  39. case SPV_ENV_OPENCL_2_2:
  40. return "SPIR-V 1.2 (under OpenCL 2.2 Full Profile semantics)";
  41. case SPV_ENV_OPENCL_EMBEDDED_2_2:
  42. return "SPIR-V 1.2 (under OpenCL 2.2 Embedded Profile semantics)";
  43. case SPV_ENV_OPENGL_4_0:
  44. return "SPIR-V 1.0 (under OpenGL 4.0 semantics)";
  45. case SPV_ENV_OPENGL_4_1:
  46. return "SPIR-V 1.0 (under OpenGL 4.1 semantics)";
  47. case SPV_ENV_OPENGL_4_2:
  48. return "SPIR-V 1.0 (under OpenGL 4.2 semantics)";
  49. case SPV_ENV_OPENGL_4_3:
  50. return "SPIR-V 1.0 (under OpenGL 4.3 semantics)";
  51. case SPV_ENV_OPENGL_4_5:
  52. return "SPIR-V 1.0 (under OpenGL 4.5 semantics)";
  53. case SPV_ENV_UNIVERSAL_1_2:
  54. return "SPIR-V 1.2";
  55. case SPV_ENV_UNIVERSAL_1_3:
  56. return "SPIR-V 1.3";
  57. case SPV_ENV_VULKAN_1_1:
  58. return "SPIR-V 1.3 (under Vulkan 1.1 semantics)";
  59. case SPV_ENV_WEBGPU_0:
  60. return "SPIR-V 1.3 (under WIP WebGPU semantics)";
  61. }
  62. assert(0 && "Unhandled SPIR-V target environment");
  63. return "";
  64. }
  65. uint32_t spvVersionForTargetEnv(spv_target_env env) {
  66. switch (env) {
  67. case SPV_ENV_UNIVERSAL_1_0:
  68. case SPV_ENV_VULKAN_1_0:
  69. case SPV_ENV_OPENCL_1_2:
  70. case SPV_ENV_OPENCL_EMBEDDED_1_2:
  71. case SPV_ENV_OPENCL_2_0:
  72. case SPV_ENV_OPENCL_EMBEDDED_2_0:
  73. case SPV_ENV_OPENCL_2_1:
  74. case SPV_ENV_OPENCL_EMBEDDED_2_1:
  75. case SPV_ENV_OPENGL_4_0:
  76. case SPV_ENV_OPENGL_4_1:
  77. case SPV_ENV_OPENGL_4_2:
  78. case SPV_ENV_OPENGL_4_3:
  79. case SPV_ENV_OPENGL_4_5:
  80. return SPV_SPIRV_VERSION_WORD(1, 0);
  81. case SPV_ENV_UNIVERSAL_1_1:
  82. return SPV_SPIRV_VERSION_WORD(1, 1);
  83. case SPV_ENV_UNIVERSAL_1_2:
  84. case SPV_ENV_OPENCL_2_2:
  85. case SPV_ENV_OPENCL_EMBEDDED_2_2:
  86. return SPV_SPIRV_VERSION_WORD(1, 2);
  87. case SPV_ENV_UNIVERSAL_1_3:
  88. case SPV_ENV_VULKAN_1_1:
  89. case SPV_ENV_WEBGPU_0:
  90. return SPV_SPIRV_VERSION_WORD(1, 3);
  91. }
  92. assert(0 && "Unhandled SPIR-V target environment");
  93. return SPV_SPIRV_VERSION_WORD(0, 0);
  94. }
  95. bool spvParseTargetEnv(const char* s, spv_target_env* env) {
  96. auto match = [s](const char* b) {
  97. return s && (0 == strncmp(s, b, strlen(b)));
  98. };
  99. if (match("vulkan1.0")) {
  100. if (env) *env = SPV_ENV_VULKAN_1_0;
  101. return true;
  102. } else if (match("vulkan1.1")) {
  103. if (env) *env = SPV_ENV_VULKAN_1_1;
  104. return true;
  105. } else if (match("spv1.0")) {
  106. if (env) *env = SPV_ENV_UNIVERSAL_1_0;
  107. return true;
  108. } else if (match("spv1.1")) {
  109. if (env) *env = SPV_ENV_UNIVERSAL_1_1;
  110. return true;
  111. } else if (match("spv1.2")) {
  112. if (env) *env = SPV_ENV_UNIVERSAL_1_2;
  113. return true;
  114. } else if (match("spv1.3")) {
  115. if (env) *env = SPV_ENV_UNIVERSAL_1_3;
  116. return true;
  117. } else if (match("opencl1.2embedded")) {
  118. if (env) *env = SPV_ENV_OPENCL_EMBEDDED_1_2;
  119. return true;
  120. } else if (match("opencl1.2")) {
  121. if (env) *env = SPV_ENV_OPENCL_1_2;
  122. return true;
  123. } else if (match("opencl2.0embedded")) {
  124. if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_0;
  125. return true;
  126. } else if (match("opencl2.0")) {
  127. if (env) *env = SPV_ENV_OPENCL_2_0;
  128. return true;
  129. } else if (match("opencl2.1embedded")) {
  130. if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_1;
  131. return true;
  132. } else if (match("opencl2.1")) {
  133. if (env) *env = SPV_ENV_OPENCL_2_1;
  134. return true;
  135. } else if (match("opencl2.2embedded")) {
  136. if (env) *env = SPV_ENV_OPENCL_EMBEDDED_2_2;
  137. return true;
  138. } else if (match("opencl2.2")) {
  139. if (env) *env = SPV_ENV_OPENCL_2_2;
  140. return true;
  141. } else if (match("opengl4.0")) {
  142. if (env) *env = SPV_ENV_OPENGL_4_0;
  143. return true;
  144. } else if (match("opengl4.1")) {
  145. if (env) *env = SPV_ENV_OPENGL_4_1;
  146. return true;
  147. } else if (match("opengl4.2")) {
  148. if (env) *env = SPV_ENV_OPENGL_4_2;
  149. return true;
  150. } else if (match("opengl4.3")) {
  151. if (env) *env = SPV_ENV_OPENGL_4_3;
  152. return true;
  153. } else if (match("opengl4.5")) {
  154. if (env) *env = SPV_ENV_OPENGL_4_5;
  155. return true;
  156. } else if (match("webgpu0")) {
  157. if (env) *env = SPV_ENV_WEBGPU_0;
  158. return true;
  159. } else {
  160. if (env) *env = SPV_ENV_UNIVERSAL_1_0;
  161. return false;
  162. }
  163. }
  164. bool spvIsVulkanEnv(spv_target_env env) {
  165. switch (env) {
  166. case SPV_ENV_UNIVERSAL_1_0:
  167. case SPV_ENV_OPENCL_1_2:
  168. case SPV_ENV_OPENCL_EMBEDDED_1_2:
  169. case SPV_ENV_OPENCL_2_0:
  170. case SPV_ENV_OPENCL_EMBEDDED_2_0:
  171. case SPV_ENV_OPENCL_2_1:
  172. case SPV_ENV_OPENCL_EMBEDDED_2_1:
  173. case SPV_ENV_OPENGL_4_0:
  174. case SPV_ENV_OPENGL_4_1:
  175. case SPV_ENV_OPENGL_4_2:
  176. case SPV_ENV_OPENGL_4_3:
  177. case SPV_ENV_OPENGL_4_5:
  178. case SPV_ENV_UNIVERSAL_1_1:
  179. case SPV_ENV_UNIVERSAL_1_2:
  180. case SPV_ENV_OPENCL_2_2:
  181. case SPV_ENV_OPENCL_EMBEDDED_2_2:
  182. case SPV_ENV_UNIVERSAL_1_3:
  183. case SPV_ENV_WEBGPU_0:
  184. return false;
  185. case SPV_ENV_VULKAN_1_0:
  186. case SPV_ENV_VULKAN_1_1:
  187. return true;
  188. }
  189. return false;
  190. }