blob: 123d2575fa848bd7150f7c9792644916bdf4b25a [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001#include <libspirv/libspirv.h>
2
3#include <string.h>
4
Andrew Woloszyn1d2a87e2015-08-21 13:39:34 -04005#define GL450Inst(name) #name, GLSLstd450::GLSLstd450##name
6
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01007static const spv_ext_inst_desc_t glslStd450Entries[] = {
Andrew Woloszyn1d2a87e2015-08-21 13:39:34 -04008 { GL450Inst(Round), {SPV_OPERAND_TYPE_ID}, },
9 { GL450Inst(RoundEven), {SPV_OPERAND_TYPE_ID}, },
10 { GL450Inst(Trunc), {SPV_OPERAND_TYPE_ID}, },
11 { GL450Inst(FAbs), {SPV_OPERAND_TYPE_ID}, },
12 { GL450Inst(SAbs), {SPV_OPERAND_TYPE_ID}, },
13 { GL450Inst(FSign), {SPV_OPERAND_TYPE_ID}, },
14 { GL450Inst(SSign), {SPV_OPERAND_TYPE_ID}, },
15 { GL450Inst(Floor), {SPV_OPERAND_TYPE_ID}, },
16 { GL450Inst(Ceil), {SPV_OPERAND_TYPE_ID}, },
17 { GL450Inst(Fract), {SPV_OPERAND_TYPE_ID}, },
18 { GL450Inst(Radians), {SPV_OPERAND_TYPE_ID}, },
19 { GL450Inst(Degrees), {SPV_OPERAND_TYPE_ID}, },
20 { GL450Inst(Sin), {SPV_OPERAND_TYPE_ID}, },
21 { GL450Inst(Cos), {SPV_OPERAND_TYPE_ID}, },
22 { GL450Inst(Tan), {SPV_OPERAND_TYPE_ID}, },
23 { GL450Inst(Asin), {SPV_OPERAND_TYPE_ID}, },
24 { GL450Inst(Acos), {SPV_OPERAND_TYPE_ID}, },
25 { GL450Inst(Atan), {SPV_OPERAND_TYPE_ID}, },
26 { GL450Inst(Sinh), {SPV_OPERAND_TYPE_ID}, },
27 { GL450Inst(Cosh), {SPV_OPERAND_TYPE_ID}, },
28 { GL450Inst(Tanh), {SPV_OPERAND_TYPE_ID}, },
29 { GL450Inst(Asinh), {SPV_OPERAND_TYPE_ID}, },
30 { GL450Inst(Acosh), {SPV_OPERAND_TYPE_ID}, },
31 { GL450Inst(Atanh), {SPV_OPERAND_TYPE_ID}, },
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010032 // TODO: Add remaining GLSL.std.450 instructions
33};
34
35static const spv_ext_inst_desc_t openclStd12Entries[] = {
36 {"placeholder", 0, {}},
37 // TODO: Add remaining OpenCL.std.12 instructions
38};
39
40static const spv_ext_inst_desc_t openclStd20Entries[] = {
41 {"placeholder", 0, {}},
42 // TODO: Add remaining OpenCL.std.20 instructions
43};
44
45static const spv_ext_inst_desc_t openclStd21Entries[] = {
46 {"placeholder", 0, {}},
47 // TODO: Add remaining OpenCL.std.21 instructions
48};
49
50spv_result_t spvExtInstTableGet(spv_ext_inst_table *pExtInstTable) {
51 spvCheck(!pExtInstTable, return SPV_ERROR_INVALID_POINTER);
52
53 static const spv_ext_inst_group_t groups[] = {
54 {SPV_EXT_INST_TYPE_GLSL_STD_450,
55 sizeof(glslStd450Entries) / sizeof(spv_ext_inst_desc_t),
56 glslStd450Entries},
57 {SPV_EXT_INST_TYPE_OPENCL_STD_12,
58 sizeof(openclStd12Entries) / sizeof(spv_ext_inst_desc_t),
59 openclStd12Entries},
60 {SPV_EXT_INST_TYPE_OPENCL_STD_20,
61 sizeof(openclStd20Entries) / sizeof(spv_ext_inst_desc_t),
62 openclStd20Entries},
63 {SPV_EXT_INST_TYPE_OPENCL_STD_21,
64 sizeof(openclStd21Entries) / sizeof(spv_ext_inst_desc_t),
65 openclStd21Entries},
66 };
67
68 static const spv_ext_inst_table_t table = {
69 sizeof(groups) / sizeof(spv_ext_inst_group_t), groups};
70
71 *pExtInstTable = &table;
72
73 return SPV_SUCCESS;
74}
75
76spv_ext_inst_type_t spvExtInstImportTypeGet(const char *name) {
77 if (!strcmp("GLSL.std.450", name)) {
78 return SPV_EXT_INST_TYPE_GLSL_STD_450;
79 }
80 if (!strcmp("OpenCL.std.12", name)) {
81 return SPV_EXT_INST_TYPE_OPENCL_STD_12;
82 }
83 if (!strcmp("OpenCL.std.20", name)) {
84 return SPV_EXT_INST_TYPE_OPENCL_STD_20;
85 }
86 if (!strcmp("OpenCL.std.21", name)) {
87 return SPV_EXT_INST_TYPE_OPENCL_STD_21;
88 }
89 return SPV_EXT_INST_TYPE_NONE;
90}
91
92spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
93 const spv_ext_inst_type_t type,
94 const char *name,
95 spv_ext_inst_desc *pEntry) {
96 spvCheck(!table, return SPV_ERROR_INVALID_TABLE);
97 spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER);
98
99 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
100 auto &group = table->groups[groupIndex];
101 if (type == group.type) {
102 for (uint32_t index = 0; index < group.count; index++) {
103 auto &entry = group.entries[index];
104 if (!strcmp(name, entry.name)) {
105 *pEntry = &table->groups[groupIndex].entries[index];
106 return SPV_SUCCESS;
107 }
108 }
109 }
110 }
111
112 return SPV_ERROR_INVALID_LOOKUP;
113}
114
115spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
116 const spv_ext_inst_type_t type,
117 const uint32_t value,
118 spv_ext_inst_desc *pEntry) {
119 spvCheck(!table, return SPV_ERROR_INVALID_TABLE);
120 spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER);
121
122 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
123 auto &group = table->groups[groupIndex];
124 if (type == group.type) {
125 for (uint32_t index = 0; index < group.count; index++) {
126 auto &entry = group.entries[index];
127 if (value == entry.ext_inst) {
128 *pEntry = &table->groups[groupIndex].entries[index];
129 return SPV_SUCCESS;
130 }
131 }
132 }
133 }
134
135 return SPV_ERROR_INVALID_LOOKUP;
136}