blob: ca2a197522c2c3ceb1b0ab7f5cf4cd34ba30f76d [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001#include <libspirv/libspirv.h>
2
3#include <string.h>
4
Lei Zhang2d879b92015-08-27 11:22:14 -04005/// Generate a spv_ext_inst_desc_t literal for a GLSL std450 extended
6/// instruction with one/two/three <id> parameter(s).
7#define GLSL450Inst1(name) \
Lei Zhangdca65b32015-08-27 10:00:16 -04008 #name, GLSLstd450::GLSLstd450##name, { SPV_OPERAND_TYPE_ID }
Lei Zhang2d879b92015-08-27 11:22:14 -04009#define GLSL450Inst2(name) \
Lei Zhangdca65b32015-08-27 10:00:16 -040010 #name, GLSLstd450::GLSLstd450##name, { \
11 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
12 }
Lei Zhang2d879b92015-08-27 11:22:14 -040013#define GLSL450Inst3(name) \
Lei Zhangdca65b32015-08-27 10:00:16 -040014 #name, GLSLstd450::GLSLstd450##name, { \
15 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
16 }
Andrew Woloszyn1d2a87e2015-08-21 13:39:34 -040017
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010018static const spv_ext_inst_desc_t glslStd450Entries[] = {
Lei Zhang2d879b92015-08-27 11:22:14 -040019 {GLSL450Inst1(Round)},
20 {GLSL450Inst1(RoundEven)},
21 {GLSL450Inst1(Trunc)},
22 {GLSL450Inst1(FAbs)},
23 {GLSL450Inst1(SAbs)},
24 {GLSL450Inst1(FSign)},
25 {GLSL450Inst1(SSign)},
26 {GLSL450Inst1(Floor)},
27 {GLSL450Inst1(Ceil)},
28 {GLSL450Inst1(Fract)},
29 {GLSL450Inst1(Radians)},
30 {GLSL450Inst1(Degrees)},
31 {GLSL450Inst1(Sin)},
32 {GLSL450Inst1(Cos)},
33 {GLSL450Inst1(Tan)},
34 {GLSL450Inst1(Asin)},
35 {GLSL450Inst1(Acos)},
36 {GLSL450Inst1(Atan)},
37 {GLSL450Inst1(Sinh)},
38 {GLSL450Inst1(Cosh)},
39 {GLSL450Inst1(Tanh)},
40 {GLSL450Inst1(Asinh)},
41 {GLSL450Inst1(Acosh)},
42 {GLSL450Inst1(Atanh)},
43 {GLSL450Inst2(Atan2)},
44 {GLSL450Inst2(Pow)},
45 {GLSL450Inst1(Exp)},
46 {GLSL450Inst1(Log)},
47 {GLSL450Inst1(Exp2)},
48 {GLSL450Inst1(Log2)},
49 {GLSL450Inst1(Sqrt)},
Lei Zhangdca65b32015-08-27 10:00:16 -040050 // clang-format off
51 {"Inversesqrt", GLSLstd450::GLSLstd450InverseSqrt, {SPV_OPERAND_TYPE_ID}},
Lei Zhang2d879b92015-08-27 11:22:14 -040052 {GLSL450Inst1(Determinant)},
Lei Zhangdca65b32015-08-27 10:00:16 -040053 {"Inverse", GLSLstd450::GLSLstd450MatrixInverse, {SPV_OPERAND_TYPE_ID}},
54 // clang-format on
Lei Zhang2d879b92015-08-27 11:22:14 -040055 {GLSL450Inst2(Modf)},
56 {GLSL450Inst1(ModfStruct)},
57 {GLSL450Inst2(FMin)},
58 {GLSL450Inst2(UMin)},
59 {GLSL450Inst2(SMin)},
60 {GLSL450Inst2(FMax)},
61 {GLSL450Inst2(UMax)},
62 {GLSL450Inst2(SMax)},
63 {GLSL450Inst3(FClamp)},
64 {GLSL450Inst3(UClamp)},
65 {GLSL450Inst3(SClamp)},
66 {GLSL450Inst3(Mix)},
67 {GLSL450Inst2(Step)},
Lei Zhangdca65b32015-08-27 10:00:16 -040068 // clang-format off
69 {"Smoothstep", GLSLstd450::GLSLstd450SmoothStep, {SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}},
70 // clang-format on
Lei Zhang2d879b92015-08-27 11:22:14 -040071 {GLSL450Inst3(Fma)},
72 {GLSL450Inst2(Frexp)},
73 {GLSL450Inst1(FrexpStruct)},
74 {GLSL450Inst2(Ldexp)},
Dejan Mircevskiafdbd2d2015-08-27 13:57:08 -040075 {GLSL450Inst1(PackSnorm4x8)},
76 {GLSL450Inst1(PackUnorm4x8)},
77 {GLSL450Inst1(PackSnorm2x16)},
78 {GLSL450Inst1(PackUnorm2x16)},
79 {GLSL450Inst1(PackHalf2x16)},
80 {GLSL450Inst1(PackDouble2x32)},
81 {GLSL450Inst1(UnpackSnorm2x16)},
82 {GLSL450Inst1(UnpackUnorm2x16)},
83 {GLSL450Inst1(UnpackHalf2x16)},
84 {GLSL450Inst1(UnpackSnorm4x8)},
85 {GLSL450Inst1(UnpackUnorm4x8)},
86 {GLSL450Inst1(UnpackDouble2x32)},
Lei Zhang3cb58962015-08-27 13:44:31 -040087 {GLSL450Inst1(Length)},
88 {GLSL450Inst2(Distance)},
89 {GLSL450Inst2(Cross)},
90 {GLSL450Inst1(Normalize)},
91 // clang-format off
92 {"Faceforward", GLSLstd450::GLSLstd450FaceForward, {SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}},
93 // clang-format on
94 {GLSL450Inst2(Reflect)},
95 {GLSL450Inst3(Refract)},
Lei Zhang5b732142015-08-27 14:20:19 -040096 // clang-format off
97 {"FindILsb", GLSLstd450::GLSLstd450FindILSB, {SPV_OPERAND_TYPE_ID}},
98 {"FindSMsb", GLSLstd450::GLSLstd450FindSMSB, {SPV_OPERAND_TYPE_ID}},
99 {"FindUMsb", GLSLstd450::GLSLstd450FindUMSB, {SPV_OPERAND_TYPE_ID}},
100 // clang-format on
101 {GLSL450Inst1(InterpolateAtCentroid)},
102 {GLSL450Inst2(InterpolateAtSample)},
103 {GLSL450Inst2(InterpolateAtOffset)},
104 // clang-format off
105 {"UaddCarry", GLSLstd450::GLSLstd450AddCarry, {SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}},
106 {"UsubBorrow", GLSLstd450::GLSLstd450SubBorrow, {SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}},
107 {"UmulExtended", GLSLstd450::GLSLstd450MulExtended, { SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}},
108 // clang-format on
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100109};
110
111static const spv_ext_inst_desc_t openclStd12Entries[] = {
112 {"placeholder", 0, {}},
113 // TODO: Add remaining OpenCL.std.12 instructions
114};
115
116static const spv_ext_inst_desc_t openclStd20Entries[] = {
117 {"placeholder", 0, {}},
118 // TODO: Add remaining OpenCL.std.20 instructions
119};
120
121static const spv_ext_inst_desc_t openclStd21Entries[] = {
122 {"placeholder", 0, {}},
123 // TODO: Add remaining OpenCL.std.21 instructions
124};
125
126spv_result_t spvExtInstTableGet(spv_ext_inst_table *pExtInstTable) {
127 spvCheck(!pExtInstTable, return SPV_ERROR_INVALID_POINTER);
128
129 static const spv_ext_inst_group_t groups[] = {
130 {SPV_EXT_INST_TYPE_GLSL_STD_450,
131 sizeof(glslStd450Entries) / sizeof(spv_ext_inst_desc_t),
132 glslStd450Entries},
133 {SPV_EXT_INST_TYPE_OPENCL_STD_12,
134 sizeof(openclStd12Entries) / sizeof(spv_ext_inst_desc_t),
135 openclStd12Entries},
136 {SPV_EXT_INST_TYPE_OPENCL_STD_20,
137 sizeof(openclStd20Entries) / sizeof(spv_ext_inst_desc_t),
138 openclStd20Entries},
139 {SPV_EXT_INST_TYPE_OPENCL_STD_21,
140 sizeof(openclStd21Entries) / sizeof(spv_ext_inst_desc_t),
141 openclStd21Entries},
142 };
143
144 static const spv_ext_inst_table_t table = {
145 sizeof(groups) / sizeof(spv_ext_inst_group_t), groups};
146
147 *pExtInstTable = &table;
148
149 return SPV_SUCCESS;
150}
151
152spv_ext_inst_type_t spvExtInstImportTypeGet(const char *name) {
153 if (!strcmp("GLSL.std.450", name)) {
154 return SPV_EXT_INST_TYPE_GLSL_STD_450;
155 }
156 if (!strcmp("OpenCL.std.12", name)) {
157 return SPV_EXT_INST_TYPE_OPENCL_STD_12;
158 }
159 if (!strcmp("OpenCL.std.20", name)) {
160 return SPV_EXT_INST_TYPE_OPENCL_STD_20;
161 }
162 if (!strcmp("OpenCL.std.21", name)) {
163 return SPV_EXT_INST_TYPE_OPENCL_STD_21;
164 }
165 return SPV_EXT_INST_TYPE_NONE;
166}
167
168spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
169 const spv_ext_inst_type_t type,
170 const char *name,
171 spv_ext_inst_desc *pEntry) {
172 spvCheck(!table, return SPV_ERROR_INVALID_TABLE);
173 spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER);
174
175 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
176 auto &group = table->groups[groupIndex];
177 if (type == group.type) {
178 for (uint32_t index = 0; index < group.count; index++) {
179 auto &entry = group.entries[index];
180 if (!strcmp(name, entry.name)) {
181 *pEntry = &table->groups[groupIndex].entries[index];
182 return SPV_SUCCESS;
183 }
184 }
185 }
186 }
187
188 return SPV_ERROR_INVALID_LOOKUP;
189}
190
191spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
192 const spv_ext_inst_type_t type,
193 const uint32_t value,
194 spv_ext_inst_desc *pEntry) {
195 spvCheck(!table, return SPV_ERROR_INVALID_TABLE);
196 spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER);
197
198 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
199 auto &group = table->groups[groupIndex];
200 if (type == group.type) {
201 for (uint32_t index = 0; index < group.count; index++) {
202 auto &entry = group.entries[index];
203 if (value == entry.ext_inst) {
204 *pEntry = &table->groups[groupIndex].entries[index];
205 return SPV_SUCCESS;
206 }
207 }
208 }
209 }
210
211 return SPV_ERROR_INVALID_LOOKUP;
212}