blob: 43b80eab345412b5109a531850bda191e80d848a [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)},
David Netodbaf4072015-09-22 16:23:06 -040050 {GLSL450Inst1(InverseSqrt)},
Lei Zhang2d879b92015-08-27 11:22:14 -040051 {GLSL450Inst1(Determinant)},
David Netodbaf4072015-09-22 16:23:06 -040052 {GLSL450Inst1(MatrixInverse)},
Lei Zhang2d879b92015-08-27 11:22:14 -040053 {GLSL450Inst2(Modf)},
54 {GLSL450Inst1(ModfStruct)},
55 {GLSL450Inst2(FMin)},
56 {GLSL450Inst2(UMin)},
57 {GLSL450Inst2(SMin)},
58 {GLSL450Inst2(FMax)},
59 {GLSL450Inst2(UMax)},
60 {GLSL450Inst2(SMax)},
61 {GLSL450Inst3(FClamp)},
62 {GLSL450Inst3(UClamp)},
63 {GLSL450Inst3(SClamp)},
David Netodbaf4072015-09-22 16:23:06 -040064 {GLSL450Inst3(FMix)},
65 {GLSL450Inst3(IMix)},
Lei Zhang2d879b92015-08-27 11:22:14 -040066 {GLSL450Inst2(Step)},
David Netodbaf4072015-09-22 16:23:06 -040067 {GLSL450Inst3(SmoothStep)},
Lei Zhang2d879b92015-08-27 11:22:14 -040068 {GLSL450Inst3(Fma)},
69 {GLSL450Inst2(Frexp)},
70 {GLSL450Inst1(FrexpStruct)},
71 {GLSL450Inst2(Ldexp)},
Dejan Mircevskiafdbd2d2015-08-27 13:57:08 -040072 {GLSL450Inst1(PackSnorm4x8)},
73 {GLSL450Inst1(PackUnorm4x8)},
74 {GLSL450Inst1(PackSnorm2x16)},
75 {GLSL450Inst1(PackUnorm2x16)},
76 {GLSL450Inst1(PackHalf2x16)},
77 {GLSL450Inst1(PackDouble2x32)},
78 {GLSL450Inst1(UnpackSnorm2x16)},
79 {GLSL450Inst1(UnpackUnorm2x16)},
80 {GLSL450Inst1(UnpackHalf2x16)},
81 {GLSL450Inst1(UnpackSnorm4x8)},
82 {GLSL450Inst1(UnpackUnorm4x8)},
83 {GLSL450Inst1(UnpackDouble2x32)},
Lei Zhang3cb58962015-08-27 13:44:31 -040084 {GLSL450Inst1(Length)},
85 {GLSL450Inst2(Distance)},
86 {GLSL450Inst2(Cross)},
87 {GLSL450Inst1(Normalize)},
David Netodbaf4072015-09-22 16:23:06 -040088 {GLSL450Inst3(FaceForward)},
Lei Zhang3cb58962015-08-27 13:44:31 -040089 {GLSL450Inst2(Reflect)},
90 {GLSL450Inst3(Refract)},
David Netodbaf4072015-09-22 16:23:06 -040091 {GLSL450Inst1(FindILsb)},
92 {GLSL450Inst1(FindSMsb)},
93 {GLSL450Inst1(FindUMsb)},
Lei Zhang5b732142015-08-27 14:20:19 -040094 {GLSL450Inst1(InterpolateAtCentroid)},
95 {GLSL450Inst2(InterpolateAtSample)},
96 {GLSL450Inst2(InterpolateAtOffset)},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010097};
98
99static const spv_ext_inst_desc_t openclStd12Entries[] = {
100 {"placeholder", 0, {}},
101 // TODO: Add remaining OpenCL.std.12 instructions
102};
103
104static const spv_ext_inst_desc_t openclStd20Entries[] = {
105 {"placeholder", 0, {}},
106 // TODO: Add remaining OpenCL.std.20 instructions
107};
108
109static const spv_ext_inst_desc_t openclStd21Entries[] = {
110 {"placeholder", 0, {}},
111 // TODO: Add remaining OpenCL.std.21 instructions
112};
113
114spv_result_t spvExtInstTableGet(spv_ext_inst_table *pExtInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400115 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116
117 static const spv_ext_inst_group_t groups[] = {
118 {SPV_EXT_INST_TYPE_GLSL_STD_450,
119 sizeof(glslStd450Entries) / sizeof(spv_ext_inst_desc_t),
120 glslStd450Entries},
121 {SPV_EXT_INST_TYPE_OPENCL_STD_12,
122 sizeof(openclStd12Entries) / sizeof(spv_ext_inst_desc_t),
123 openclStd12Entries},
124 {SPV_EXT_INST_TYPE_OPENCL_STD_20,
125 sizeof(openclStd20Entries) / sizeof(spv_ext_inst_desc_t),
126 openclStd20Entries},
127 {SPV_EXT_INST_TYPE_OPENCL_STD_21,
128 sizeof(openclStd21Entries) / sizeof(spv_ext_inst_desc_t),
129 openclStd21Entries},
130 };
131
132 static const spv_ext_inst_table_t table = {
133 sizeof(groups) / sizeof(spv_ext_inst_group_t), groups};
134
135 *pExtInstTable = &table;
136
137 return SPV_SUCCESS;
138}
139
140spv_ext_inst_type_t spvExtInstImportTypeGet(const char *name) {
141 if (!strcmp("GLSL.std.450", name)) {
142 return SPV_EXT_INST_TYPE_GLSL_STD_450;
143 }
144 if (!strcmp("OpenCL.std.12", name)) {
145 return SPV_EXT_INST_TYPE_OPENCL_STD_12;
146 }
147 if (!strcmp("OpenCL.std.20", name)) {
148 return SPV_EXT_INST_TYPE_OPENCL_STD_20;
149 }
150 if (!strcmp("OpenCL.std.21", name)) {
151 return SPV_EXT_INST_TYPE_OPENCL_STD_21;
152 }
153 return SPV_EXT_INST_TYPE_NONE;
154}
155
156spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
157 const spv_ext_inst_type_t type,
158 const char *name,
159 spv_ext_inst_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400160 if (!table) return SPV_ERROR_INVALID_TABLE;
161 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100162
163 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
164 auto &group = table->groups[groupIndex];
165 if (type == group.type) {
166 for (uint32_t index = 0; index < group.count; index++) {
167 auto &entry = group.entries[index];
168 if (!strcmp(name, entry.name)) {
169 *pEntry = &table->groups[groupIndex].entries[index];
170 return SPV_SUCCESS;
171 }
172 }
173 }
174 }
175
176 return SPV_ERROR_INVALID_LOOKUP;
177}
178
179spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
180 const spv_ext_inst_type_t type,
181 const uint32_t value,
182 spv_ext_inst_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400183 if (!table) return SPV_ERROR_INVALID_TABLE;
184 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100185
186 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
187 auto &group = table->groups[groupIndex];
188 if (type == group.type) {
189 for (uint32_t index = 0; index < group.count; index++) {
190 auto &entry = group.entries[index];
191 if (value == entry.ext_inst) {
192 *pEntry = &table->groups[groupIndex].entries[index];
193 return SPV_SUCCESS;
194 }
195 }
196 }
197 }
198
199 return SPV_ERROR_INVALID_LOOKUP;
200}