Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 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 | 2a0b773 | 2015-11-11 13:57:43 -0500 | [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 | 2a0b773 | 2015-11-11 13:57:43 -0500 | [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 | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 14 | |
Lei Zhang | 7a222e4 | 2015-11-11 12:40:25 -0500 | [diff] [blame] | 15 | #include "ext_inst.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 16 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 17 | #include <cassert> |
| 18 | #include <cstring> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 19 | |
Lei Zhang | 10dba91 | 2016-04-14 14:05:53 -0400 | [diff] [blame] | 20 | #include "spirv/1.0/GLSL.std.450.h" |
| 21 | #include "spirv/1.0/OpenCL.std.h" |
David Neto | d1bb51d | 2016-02-17 12:17:04 -0500 | [diff] [blame] | 22 | #include "spirv_definition.h" |
| 23 | |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 24 | #include "macro.h" |
| 25 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 26 | spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable, |
| 27 | spv_target_env env) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 28 | if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 29 | |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame^] | 30 | static const spv_ext_inst_desc_t glslStd450Entries_1_0[] = { |
| 31 | #include "glsl.std.450.insts-1.0.inc" |
| 32 | }; |
| 33 | |
| 34 | static const spv_ext_inst_desc_t openclEntries_1_0[] = { |
| 35 | #include "opencl.std.insts-1.0.inc" |
| 36 | }; |
| 37 | |
| 38 | static const spv_ext_inst_desc_t |
| 39 | spv_amd_shader_explicit_vertex_parameter_entries[] = { |
| 40 | #include "spv-amd-shader-explicit-vertex-parameter.insts.inc" |
| 41 | }; |
| 42 | |
| 43 | static const spv_ext_inst_desc_t spv_amd_shader_trinary_minmax_entries[] = { |
| 44 | #include "spv-amd-shader-trinary-minmax.insts.inc" |
| 45 | }; |
| 46 | |
| 47 | static const spv_ext_inst_desc_t spv_amd_gcn_shader_entries[] = { |
| 48 | #include "spv-amd-gcn-shader.insts.inc" |
| 49 | }; |
| 50 | |
| 51 | static const spv_ext_inst_desc_t spv_amd_shader_ballot_entries[] = { |
| 52 | #include "spv-amd-shader-ballot.insts.inc" |
| 53 | }; |
| 54 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 55 | static const spv_ext_inst_group_t groups_1_0[] = { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 56 | {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glslStd450Entries_1_0), |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 57 | glslStd450Entries_1_0}, |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 58 | {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(openclEntries_1_0), |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 59 | openclEntries_1_0}, |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 60 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER, |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame^] | 61 | ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries), |
| 62 | spv_amd_shader_explicit_vertex_parameter_entries}, |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 63 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX, |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame^] | 64 | ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries), |
| 65 | spv_amd_shader_trinary_minmax_entries}, |
David Neto | 4be6abe | 2017-04-03 14:59:18 -0400 | [diff] [blame] | 66 | {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER, |
| 67 | ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries}, |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 68 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT, |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame^] | 69 | ARRAY_SIZE(spv_amd_shader_ballot_entries), |
| 70 | spv_amd_shader_ballot_entries}, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 73 | static const spv_ext_inst_table_t table_1_0 = {ARRAY_SIZE(groups_1_0), |
| 74 | groups_1_0}; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 75 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 76 | switch (env) { |
| 77 | // The extended instruction sets are all version 1.0 so far. |
| 78 | case SPV_ENV_UNIVERSAL_1_0: |
| 79 | case SPV_ENV_VULKAN_1_0: |
| 80 | case SPV_ENV_UNIVERSAL_1_1: |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 81 | case SPV_ENV_UNIVERSAL_1_2: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 82 | case SPV_ENV_OPENCL_2_1: |
| 83 | case SPV_ENV_OPENCL_2_2: |
| 84 | case SPV_ENV_OPENGL_4_0: |
| 85 | case SPV_ENV_OPENGL_4_1: |
| 86 | case SPV_ENV_OPENGL_4_2: |
| 87 | case SPV_ENV_OPENGL_4_3: |
| 88 | case SPV_ENV_OPENGL_4_5: |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 89 | *pExtInstTable = &table_1_0; |
| 90 | return SPV_SUCCESS; |
| 91 | default: |
| 92 | assert(0 && "Unknown spv_target_env in spvExtInstTableGet()"); |
| 93 | return SPV_ERROR_INVALID_TABLE; |
| 94 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 97 | spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) { |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 98 | // The names are specified by the respective extension instruction |
| 99 | // specifications. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 100 | if (!strcmp("GLSL.std.450", name)) { |
| 101 | return SPV_EXT_INST_TYPE_GLSL_STD_450; |
| 102 | } |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 103 | if (!strcmp("OpenCL.std", name)) { |
| 104 | return SPV_EXT_INST_TYPE_OPENCL_STD; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 105 | } |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 106 | if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) { |
| 107 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER; |
| 108 | } |
| 109 | if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) { |
| 110 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX; |
| 111 | } |
David Neto | 7fe8a57 | 2017-03-21 12:43:26 -0400 | [diff] [blame] | 112 | if (!strcmp("SPV_AMD_gcn_shader", name)) { |
| 113 | return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER; |
| 114 | } |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 115 | if (!strcmp("SPV_AMD_shader_ballot", name)) { |
| 116 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT; |
| 117 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 118 | return SPV_EXT_INST_TYPE_NONE; |
| 119 | } |
| 120 | |
| 121 | spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table, |
| 122 | const spv_ext_inst_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 123 | const char* name, |
| 124 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 125 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 126 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 127 | |
| 128 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 129 | const auto& group = table->groups[groupIndex]; |
| 130 | if (type != group.type) continue; |
| 131 | for (uint32_t index = 0; index < group.count; index++) { |
| 132 | const auto& entry = group.entries[index]; |
| 133 | if (!strcmp(name, entry.name)) { |
| 134 | *pEntry = &entry; |
| 135 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return SPV_ERROR_INVALID_LOOKUP; |
| 141 | } |
| 142 | |
| 143 | spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table, |
| 144 | const spv_ext_inst_type_t type, |
| 145 | const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 146 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 147 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 148 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 149 | |
| 150 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 151 | const auto& group = table->groups[groupIndex]; |
| 152 | if (type != group.type) continue; |
| 153 | for (uint32_t index = 0; index < group.count; index++) { |
| 154 | const auto& entry = group.entries[index]; |
| 155 | if (value == entry.ext_inst) { |
| 156 | *pEntry = &entry; |
| 157 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return SPV_ERROR_INVALID_LOOKUP; |
| 163 | } |