blob: 4020b9e599a4b80bb5103949e6c79c673c20624d [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//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17// https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
Lei Zhang7a222e42015-11-11 12:40:25 -050027#include "ext_inst.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040029#include <cassert>
30#include <cstring>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010031
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040032#include "spirv/GLSL.std.450.h"
33#include "spirv/OpenCL.std.h"
David Netod1bb51d2016-02-17 12:17:04 -050034#include "spirv_definition.h"
35
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040036static const spv_ext_inst_desc_t glslStd450Entries_1_0[] = {
Dejan Mircevskie26fdc62016-04-07 14:09:48 -040037#include "glsl.std.450.insts-1-0.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010038};
39
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040040static const spv_ext_inst_desc_t openclEntries_1_0[] = {
Dejan Mircevskie26fdc62016-04-07 14:09:48 -040041#include "opencl.std.insts-1-0.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042};
43
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040044spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
45 spv_target_env env) {
Lei Zhang40056702015-09-11 14:31:27 -040046 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040048 static const spv_ext_inst_group_t groups_1_0[] = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049 {SPV_EXT_INST_TYPE_GLSL_STD_450,
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040050 static_cast<uint32_t>(sizeof(glslStd450Entries_1_0) /
David Netoba73a7c2016-01-06 13:08:39 -050051 sizeof(spv_ext_inst_desc_t)),
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040052 glslStd450Entries_1_0},
David Neto21c4ad42015-10-14 17:02:11 -040053 {SPV_EXT_INST_TYPE_OPENCL_STD,
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040054 static_cast<uint32_t>(sizeof(openclEntries_1_0) /
David Netoba73a7c2016-01-06 13:08:39 -050055 sizeof(spv_ext_inst_desc_t)),
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040056 openclEntries_1_0},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010057 };
58
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040059 static const spv_ext_inst_table_t table_1_0 = {
60 static_cast<uint32_t>(sizeof(groups_1_0) / sizeof(spv_ext_inst_group_t)),
61 groups_1_0};
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:
68 *pExtInstTable = &table_1_0;
69 return SPV_SUCCESS;
70 default:
71 assert(0 && "Unknown spv_target_env in spvExtInstTableGet()");
72 return SPV_ERROR_INVALID_TABLE;
73 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010074}
75
Lei Zhang1a0334e2015-11-02 09:41:20 -050076spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -040077 // The names are specified by the respective extension instruction
78 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079 if (!strcmp("GLSL.std.450", name)) {
80 return SPV_EXT_INST_TYPE_GLSL_STD_450;
81 }
David Neto21c4ad42015-10-14 17:02:11 -040082 if (!strcmp("OpenCL.std", name)) {
83 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010084 }
85 return SPV_EXT_INST_TYPE_NONE;
86}
87
88spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
89 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -050090 const char* name,
91 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -040092 if (!table) return SPV_ERROR_INVALID_TABLE;
93 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010094
95 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -050096 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010097 if (type == group.type) {
98 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -050099 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100100 if (!strcmp(name, entry.name)) {
101 *pEntry = &table->groups[groupIndex].entries[index];
102 return SPV_SUCCESS;
103 }
104 }
105 }
106 }
107
108 return SPV_ERROR_INVALID_LOOKUP;
109}
110
111spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
112 const spv_ext_inst_type_t type,
113 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500114 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400115 if (!table) return SPV_ERROR_INVALID_TABLE;
116 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100117
118 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500119 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100120 if (type == group.type) {
121 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500122 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100123 if (value == entry.ext_inst) {
124 *pEntry = &table->groups[groupIndex].entries[index];
125 return SPV_SUCCESS;
126 }
127 }
128 }
129 }
130
131 return SPV_ERROR_INVALID_LOOKUP;
132}