blob: 6218eb1153ff6ead9b15508e390fb29f9b6dbd9d [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Lei Zhang2a0b7732015-11-11 13:57:43 -05002//
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 Zhang2a0b7732015-11-11 13:57:43 -05006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhang2a0b7732015-11-11 13:57:43 -05008//
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 Zhang2a0b7732015-11-11 13:57:43 -050014
Lei Zhang7a222e42015-11-11 12:40:25 -050015#include "ext_inst.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010016
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040017#include <cassert>
18#include <cstring>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010019
David Neto59de6102017-12-03 12:30:08 -050020// DebugInfo extended instruction set.
21// See https://www.khronos.org/registry/spir-v/specs/1.0/DebugInfo.html
22// TODO(dneto): DebugInfo.h should probably move to SPIRV-Headers.
23#include "DebugInfo.h"
24
Andrey Tuganovaf7d5792017-12-13 17:55:00 -050025#include "latest_version_glsl_std_450_header.h"
26#include "latest_version_opencl_std_header.h"
David Neto59de6102017-12-03 12:30:08 -050027#include "macro.h"
David Netod1bb51d2016-02-17 12:17:04 -050028#include "spirv_definition.h"
29
David Neto59de6102017-12-03 12:30:08 -050030#include "debuginfo.insts.inc" // defines opencl_entries
Andrey Tuganovaf7d5792017-12-13 17:55:00 -050031#include "glsl.std.450.insts.inc" // defines glsl_entries
32#include "opencl.std.insts.inc" // defines opencl_entries
Lei Zhang063dbea2017-10-25 12:15:51 -040033
34#include "spv-amd-gcn-shader.insts.inc"
35#include "spv-amd-shader-ballot.insts.inc"
36#include "spv-amd-shader-explicit-vertex-parameter.insts.inc"
37#include "spv-amd-shader-trinary-minmax.insts.inc"
38
39static const spv_ext_inst_group_t kGroups_1_0[] = {
40 {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries},
41 {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries},
42 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
43 ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries),
44 spv_amd_shader_explicit_vertex_parameter_entries},
45 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
46 ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries),
47 spv_amd_shader_trinary_minmax_entries},
48 {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
49 ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
50 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
51 ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries},
David Neto59de6102017-12-03 12:30:08 -050052 {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries),
53 debuginfo_entries},
Lei Zhang063dbea2017-10-25 12:15:51 -040054};
55
56static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0),
57 kGroups_1_0};
58
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040059spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
60 spv_target_env env) {
Lei Zhang40056702015-09-11 14:31:27 -040061 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010062
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040063 switch (env) {
64 // The extended instruction sets are all version 1.0 so far.
65 case SPV_ENV_UNIVERSAL_1_0:
66 case SPV_ENV_VULKAN_1_0:
67 case SPV_ENV_UNIVERSAL_1_1:
David Netodbc20492017-03-14 12:43:41 -040068 case SPV_ENV_UNIVERSAL_1_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010069 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:
David Netoc2967012016-08-05 18:19:30 -040073 case SPV_ENV_OPENCL_2_1:
Pierre Moreau12447d82017-11-30 00:49:23 +010074 case SPV_ENV_OPENCL_EMBEDDED_2_1:
David Netoc2967012016-08-05 18:19:30 -040075 case SPV_ENV_OPENCL_2_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010076 case SPV_ENV_OPENCL_EMBEDDED_2_2:
David Netoc2967012016-08-05 18:19:30 -040077 case SPV_ENV_OPENGL_4_0:
78 case SPV_ENV_OPENGL_4_1:
79 case SPV_ENV_OPENGL_4_2:
80 case SPV_ENV_OPENGL_4_3:
81 case SPV_ENV_OPENGL_4_5:
David Neto00fa3932018-02-09 14:29:02 -050082 case SPV_ENV_UNIVERSAL_1_3:
83 case SPV_ENV_VULKAN_1_1:
Lei Zhang063dbea2017-10-25 12:15:51 -040084 *pExtInstTable = &kTable_1_0;
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040085 return SPV_SUCCESS;
86 default:
87 assert(0 && "Unknown spv_target_env in spvExtInstTableGet()");
88 return SPV_ERROR_INVALID_TABLE;
89 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010090}
91
Lei Zhang1a0334e2015-11-02 09:41:20 -050092spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -040093 // The names are specified by the respective extension instruction
94 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010095 if (!strcmp("GLSL.std.450", name)) {
96 return SPV_EXT_INST_TYPE_GLSL_STD_450;
97 }
David Neto21c4ad42015-10-14 17:02:11 -040098 if (!strcmp("OpenCL.std", name)) {
99 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100100 }
Rex Xu5fbbadc2017-06-21 15:58:00 +0800101 if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) {
102 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER;
103 }
104 if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) {
105 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX;
106 }
David Neto7fe8a572017-03-21 12:43:26 -0400107 if (!strcmp("SPV_AMD_gcn_shader", name)) {
108 return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
109 }
Rex Xu5fbbadc2017-06-21 15:58:00 +0800110 if (!strcmp("SPV_AMD_shader_ballot", name)) {
111 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT;
112 }
David Neto59de6102017-12-03 12:30:08 -0500113 if (!strcmp("DebugInfo", name)) {
114 return SPV_EXT_INST_TYPE_DEBUGINFO;
115 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116 return SPV_EXT_INST_TYPE_NONE;
117}
118
119spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
120 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500121 const char* name,
122 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400123 if (!table) return SPV_ERROR_INVALID_TABLE;
124 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100125
126 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -0400127 const auto& group = table->groups[groupIndex];
128 if (type != group.type) continue;
129 for (uint32_t index = 0; index < group.count; index++) {
130 const auto& entry = group.entries[index];
131 if (!strcmp(name, entry.name)) {
132 *pEntry = &entry;
133 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100134 }
135 }
136 }
137
138 return SPV_ERROR_INVALID_LOOKUP;
139}
140
141spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
142 const spv_ext_inst_type_t type,
143 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500144 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400145 if (!table) return SPV_ERROR_INVALID_TABLE;
146 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100147
148 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -0400149 const auto& group = table->groups[groupIndex];
150 if (type != group.type) continue;
151 for (uint32_t index = 0; index < group.count; index++) {
152 const auto& entry = group.entries[index];
153 if (value == entry.ext_inst) {
154 *pEntry = &entry;
155 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100156 }
157 }
158 }
159
160 return SPV_ERROR_INVALID_LOOKUP;
161}