blob: 3f2b6ce692d7508785a78523ce255beddaa9837a [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
Lei Zhang10dba912016-04-14 14:05:53 -040020#include "spirv/1.0/GLSL.std.450.h"
21#include "spirv/1.0/OpenCL.std.h"
David Netod1bb51d2016-02-17 12:17:04 -050022#include "spirv_definition.h"
23
Lei Zhangca1bf942016-04-27 16:47:13 -040024#include "macro.h"
25
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040026static const spv_ext_inst_desc_t glslStd450Entries_1_0[] = {
Lei Zhang10dba912016-04-14 14:05:53 -040027#include "glsl.std.450.insts-1.0.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028};
29
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040030static const spv_ext_inst_desc_t openclEntries_1_0[] = {
Lei Zhang10dba912016-04-14 14:05:53 -040031#include "opencl.std.insts-1.0.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010032};
33
David Neto4be6abe2017-04-03 14:59:18 -040034static const spv_ext_inst_desc_t spv_amd_gcn_shader_entries[] = {
35#include "spv-amd-gcn-shader.insts.inc"
David Neto7fe8a572017-03-21 12:43:26 -040036};
37
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040038spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
39 spv_target_env env) {
Lei Zhang40056702015-09-11 14:31:27 -040040 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040042 static const spv_ext_inst_group_t groups_1_0[] = {
Lei Zhangca1bf942016-04-27 16:47:13 -040043 {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glslStd450Entries_1_0),
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040044 glslStd450Entries_1_0},
Lei Zhangca1bf942016-04-27 16:47:13 -040045 {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(openclEntries_1_0),
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040046 openclEntries_1_0},
David Neto4be6abe2017-04-03 14:59:18 -040047 {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
48 ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049 };
50
Lei Zhangca1bf942016-04-27 16:47:13 -040051 static const spv_ext_inst_table_t table_1_0 = {ARRAY_SIZE(groups_1_0),
52 groups_1_0};
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010053
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040054 switch (env) {
55 // The extended instruction sets are all version 1.0 so far.
56 case SPV_ENV_UNIVERSAL_1_0:
57 case SPV_ENV_VULKAN_1_0:
58 case SPV_ENV_UNIVERSAL_1_1:
David Netodbc20492017-03-14 12:43:41 -040059 case SPV_ENV_UNIVERSAL_1_2:
David Netoc2967012016-08-05 18:19:30 -040060 case SPV_ENV_OPENCL_2_1:
61 case SPV_ENV_OPENCL_2_2:
62 case SPV_ENV_OPENGL_4_0:
63 case SPV_ENV_OPENGL_4_1:
64 case SPV_ENV_OPENGL_4_2:
65 case SPV_ENV_OPENGL_4_3:
66 case SPV_ENV_OPENGL_4_5:
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040067 *pExtInstTable = &table_1_0;
68 return SPV_SUCCESS;
69 default:
70 assert(0 && "Unknown spv_target_env in spvExtInstTableGet()");
71 return SPV_ERROR_INVALID_TABLE;
72 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073}
74
Lei Zhang1a0334e2015-11-02 09:41:20 -050075spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -040076 // The names are specified by the respective extension instruction
77 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 if (!strcmp("GLSL.std.450", name)) {
79 return SPV_EXT_INST_TYPE_GLSL_STD_450;
80 }
David Neto21c4ad42015-10-14 17:02:11 -040081 if (!strcmp("OpenCL.std", name)) {
82 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010083 }
David Neto7fe8a572017-03-21 12:43:26 -040084 if (!strcmp("SPV_AMD_gcn_shader", name)) {
85 return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
86 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087 return SPV_EXT_INST_TYPE_NONE;
88}
89
90spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
91 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -050092 const char* name,
93 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -040094 if (!table) return SPV_ERROR_INVALID_TABLE;
95 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010096
97 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -040098 const auto& group = table->groups[groupIndex];
99 if (type != group.type) continue;
100 for (uint32_t index = 0; index < group.count; index++) {
101 const auto& entry = group.entries[index];
102 if (!strcmp(name, entry.name)) {
103 *pEntry = &entry;
104 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100105 }
106 }
107 }
108
109 return SPV_ERROR_INVALID_LOOKUP;
110}
111
112spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
113 const spv_ext_inst_type_t type,
114 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500115 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400116 if (!table) return SPV_ERROR_INVALID_TABLE;
117 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100118
119 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -0400120 const auto& group = table->groups[groupIndex];
121 if (type != group.type) continue;
122 for (uint32_t index = 0; index < group.count; index++) {
123 const auto& entry = group.entries[index];
124 if (value == entry.ext_inst) {
125 *pEntry = &entry;
126 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100127 }
128 }
129 }
130
131 return SPV_ERROR_INVALID_LOOKUP;
132}