ext_inst.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/ext_inst.h"
  15. #include <cstring>
  16. // DebugInfo extended instruction set.
  17. // See https://www.khronos.org/registry/spir-v/specs/1.0/DebugInfo.html
  18. // TODO(dneto): DebugInfo.h should probably move to SPIRV-Headers.
  19. #include "DebugInfo.h"
  20. #include "source/latest_version_glsl_std_450_header.h"
  21. #include "source/latest_version_opencl_std_header.h"
  22. #include "source/macro.h"
  23. #include "source/spirv_definition.h"
  24. #include "debuginfo.insts.inc"
  25. #include "glsl.std.450.insts.inc"
  26. #include "nonsemantic.clspvreflection.insts.inc"
  27. #include "nonsemantic.shader.debuginfo.100.insts.inc"
  28. #include "nonsemantic.vkspreflection.insts.inc"
  29. #include "opencl.debuginfo.100.insts.inc"
  30. #include "opencl.std.insts.inc"
  31. #include "spirv-tools/libspirv.h"
  32. #include "spv-amd-gcn-shader.insts.inc"
  33. #include "spv-amd-shader-ballot.insts.inc"
  34. #include "spv-amd-shader-explicit-vertex-parameter.insts.inc"
  35. #include "spv-amd-shader-trinary-minmax.insts.inc"
  36. static const spv_ext_inst_group_t kGroups_1_0[] = {
  37. {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries},
  38. {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries},
  39. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
  40. ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries),
  41. spv_amd_shader_explicit_vertex_parameter_entries},
  42. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
  43. ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries),
  44. spv_amd_shader_trinary_minmax_entries},
  45. {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
  46. ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
  47. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
  48. ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries},
  49. {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries),
  50. debuginfo_entries},
  51. {SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100,
  52. ARRAY_SIZE(opencl_debuginfo_100_entries), opencl_debuginfo_100_entries},
  53. {SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100,
  54. ARRAY_SIZE(nonsemantic_shader_debuginfo_100_entries),
  55. nonsemantic_shader_debuginfo_100_entries},
  56. {SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION,
  57. ARRAY_SIZE(nonsemantic_clspvreflection_entries),
  58. nonsemantic_clspvreflection_entries},
  59. {SPV_EXT_INST_TYPE_NONSEMANTIC_VKSPREFLECTION,
  60. ARRAY_SIZE(nonsemantic_vkspreflection_entries),
  61. nonsemantic_vkspreflection_entries},
  62. };
  63. static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0),
  64. kGroups_1_0};
  65. spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
  66. spv_target_env env) {
  67. if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
  68. switch (env) {
  69. // The extended instruction sets are all version 1.0 so far.
  70. case SPV_ENV_UNIVERSAL_1_0:
  71. case SPV_ENV_VULKAN_1_0:
  72. case SPV_ENV_UNIVERSAL_1_1:
  73. case SPV_ENV_UNIVERSAL_1_2:
  74. case SPV_ENV_OPENCL_1_2:
  75. case SPV_ENV_OPENCL_EMBEDDED_1_2:
  76. case SPV_ENV_OPENCL_2_0:
  77. case SPV_ENV_OPENCL_EMBEDDED_2_0:
  78. case SPV_ENV_OPENCL_2_1:
  79. case SPV_ENV_OPENCL_EMBEDDED_2_1:
  80. case SPV_ENV_OPENCL_2_2:
  81. case SPV_ENV_OPENCL_EMBEDDED_2_2:
  82. case SPV_ENV_OPENGL_4_0:
  83. case SPV_ENV_OPENGL_4_1:
  84. case SPV_ENV_OPENGL_4_2:
  85. case SPV_ENV_OPENGL_4_3:
  86. case SPV_ENV_OPENGL_4_5:
  87. case SPV_ENV_UNIVERSAL_1_3:
  88. case SPV_ENV_VULKAN_1_1:
  89. case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
  90. case SPV_ENV_UNIVERSAL_1_4:
  91. case SPV_ENV_UNIVERSAL_1_5:
  92. case SPV_ENV_VULKAN_1_2:
  93. case SPV_ENV_UNIVERSAL_1_6:
  94. case SPV_ENV_VULKAN_1_3:
  95. case SPV_ENV_VULKAN_1_4:
  96. *pExtInstTable = &kTable_1_0;
  97. return SPV_SUCCESS;
  98. default:
  99. return SPV_ERROR_INVALID_TABLE;
  100. }
  101. }
  102. spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
  103. // The names are specified by the respective extension instruction
  104. // specifications.
  105. if (!strcmp("GLSL.std.450", name)) {
  106. return SPV_EXT_INST_TYPE_GLSL_STD_450;
  107. }
  108. if (!strcmp("OpenCL.std", name)) {
  109. return SPV_EXT_INST_TYPE_OPENCL_STD;
  110. }
  111. if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) {
  112. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER;
  113. }
  114. if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) {
  115. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX;
  116. }
  117. if (!strcmp("SPV_AMD_gcn_shader", name)) {
  118. return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
  119. }
  120. if (!strcmp("SPV_AMD_shader_ballot", name)) {
  121. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT;
  122. }
  123. if (!strcmp("DebugInfo", name)) {
  124. return SPV_EXT_INST_TYPE_DEBUGINFO;
  125. }
  126. if (!strcmp("OpenCL.DebugInfo.100", name)) {
  127. return SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100;
  128. }
  129. if (!strcmp("NonSemantic.Shader.DebugInfo.100", name)) {
  130. return SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100;
  131. }
  132. if (!strncmp("NonSemantic.ClspvReflection.", name, 28)) {
  133. return SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION;
  134. }
  135. if (!strncmp("NonSemantic.VkspReflection.", name, 27)) {
  136. return SPV_EXT_INST_TYPE_NONSEMANTIC_VKSPREFLECTION;
  137. }
  138. // ensure to add any known non-semantic extended instruction sets
  139. // above this point, and update spvExtInstIsNonSemantic()
  140. if (!strncmp("NonSemantic.", name, 12)) {
  141. return SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN;
  142. }
  143. return SPV_EXT_INST_TYPE_NONE;
  144. }
  145. bool spvExtInstIsNonSemantic(const spv_ext_inst_type_t type) {
  146. if (type == SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN ||
  147. type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100 ||
  148. type == SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION ||
  149. type == SPV_EXT_INST_TYPE_NONSEMANTIC_VKSPREFLECTION) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. bool spvExtInstIsDebugInfo(const spv_ext_inst_type_t type) {
  155. if (type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  156. type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100 ||
  157. type == SPV_EXT_INST_TYPE_DEBUGINFO) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
  163. const spv_ext_inst_type_t type,
  164. const char* name,
  165. spv_ext_inst_desc* pEntry) {
  166. if (!table) return SPV_ERROR_INVALID_TABLE;
  167. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  168. for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
  169. const auto& group = table->groups[groupIndex];
  170. if (type != group.type) continue;
  171. for (uint32_t index = 0; index < group.count; index++) {
  172. const auto& entry = group.entries[index];
  173. if (!strcmp(name, entry.name)) {
  174. *pEntry = &entry;
  175. return SPV_SUCCESS;
  176. }
  177. }
  178. }
  179. return SPV_ERROR_INVALID_LOOKUP;
  180. }
  181. spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
  182. const spv_ext_inst_type_t type,
  183. const uint32_t value,
  184. spv_ext_inst_desc* pEntry) {
  185. if (!table) return SPV_ERROR_INVALID_TABLE;
  186. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  187. for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
  188. const auto& group = table->groups[groupIndex];
  189. if (type != group.type) continue;
  190. for (uint32_t index = 0; index < group.count; index++) {
  191. const auto& entry = group.entries[index];
  192. if (value == entry.ext_inst) {
  193. *pEntry = &entry;
  194. return SPV_SUCCESS;
  195. }
  196. }
  197. }
  198. return SPV_ERROR_INVALID_LOOKUP;
  199. }