Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 1 | #include <libspirv/libspirv.h> |
| 2 | |
| 3 | #include <string.h> |
| 4 | |
Lei Zhang | dca65b3 | 2015-08-27 10:00:16 -0400 | [diff] [blame^] | 5 | #define GL450InstWithOneIdParam(name) \ |
| 6 | #name, GLSLstd450::GLSLstd450##name, { SPV_OPERAND_TYPE_ID } |
| 7 | #define GL450InstWithTwoIdParam(name) \ |
| 8 | #name, GLSLstd450::GLSLstd450##name, { \ |
| 9 | SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \ |
| 10 | } |
| 11 | #define GL450InstWithThreeIdParam(name) \ |
| 12 | #name, GLSLstd450::GLSLstd450##name, { \ |
| 13 | SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \ |
| 14 | } |
Andrew Woloszyn | 1d2a87e | 2015-08-21 13:39:34 -0400 | [diff] [blame] | 15 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 16 | static const spv_ext_inst_desc_t glslStd450Entries[] = { |
Lei Zhang | dca65b3 | 2015-08-27 10:00:16 -0400 | [diff] [blame^] | 17 | {GL450InstWithOneIdParam(Round)}, |
| 18 | {GL450InstWithOneIdParam(RoundEven)}, |
| 19 | {GL450InstWithOneIdParam(Trunc)}, |
| 20 | {GL450InstWithOneIdParam(FAbs)}, |
| 21 | {GL450InstWithOneIdParam(SAbs)}, |
| 22 | {GL450InstWithOneIdParam(FSign)}, |
| 23 | {GL450InstWithOneIdParam(SSign)}, |
| 24 | {GL450InstWithOneIdParam(Floor)}, |
| 25 | {GL450InstWithOneIdParam(Ceil)}, |
| 26 | {GL450InstWithOneIdParam(Fract)}, |
| 27 | {GL450InstWithOneIdParam(Radians)}, |
| 28 | {GL450InstWithOneIdParam(Degrees)}, |
| 29 | {GL450InstWithOneIdParam(Sin)}, |
| 30 | {GL450InstWithOneIdParam(Cos)}, |
| 31 | {GL450InstWithOneIdParam(Tan)}, |
| 32 | {GL450InstWithOneIdParam(Asin)}, |
| 33 | {GL450InstWithOneIdParam(Acos)}, |
| 34 | {GL450InstWithOneIdParam(Atan)}, |
| 35 | {GL450InstWithOneIdParam(Sinh)}, |
| 36 | {GL450InstWithOneIdParam(Cosh)}, |
| 37 | {GL450InstWithOneIdParam(Tanh)}, |
| 38 | {GL450InstWithOneIdParam(Asinh)}, |
| 39 | {GL450InstWithOneIdParam(Acosh)}, |
| 40 | {GL450InstWithOneIdParam(Atanh)}, |
| 41 | {GL450InstWithTwoIdParam(Atan2)}, |
| 42 | {GL450InstWithTwoIdParam(Pow)}, |
| 43 | {GL450InstWithOneIdParam(Exp)}, |
| 44 | {GL450InstWithOneIdParam(Log)}, |
| 45 | {GL450InstWithOneIdParam(Exp2)}, |
| 46 | {GL450InstWithOneIdParam(Log2)}, |
| 47 | {GL450InstWithOneIdParam(Sqrt)}, |
| 48 | // clang-format off |
| 49 | {"Inversesqrt", GLSLstd450::GLSLstd450InverseSqrt, {SPV_OPERAND_TYPE_ID}}, |
| 50 | {GL450InstWithOneIdParam(Determinant)}, |
| 51 | {"Inverse", GLSLstd450::GLSLstd450MatrixInverse, {SPV_OPERAND_TYPE_ID}}, |
| 52 | // clang-format on |
| 53 | {GL450InstWithTwoIdParam(Modf)}, |
| 54 | {GL450InstWithOneIdParam(ModfStruct)}, |
| 55 | {GL450InstWithTwoIdParam(FMin)}, |
| 56 | {GL450InstWithTwoIdParam(UMin)}, |
| 57 | {GL450InstWithTwoIdParam(SMin)}, |
| 58 | {GL450InstWithTwoIdParam(FMax)}, |
| 59 | {GL450InstWithTwoIdParam(UMax)}, |
| 60 | {GL450InstWithTwoIdParam(SMax)}, |
| 61 | {GL450InstWithThreeIdParam(FClamp)}, |
| 62 | {GL450InstWithThreeIdParam(UClamp)}, |
| 63 | {GL450InstWithThreeIdParam(SClamp)}, |
| 64 | {GL450InstWithThreeIdParam(Mix)}, |
| 65 | {GL450InstWithTwoIdParam(Step)}, |
| 66 | // clang-format off |
| 67 | {"Smoothstep", GLSLstd450::GLSLstd450SmoothStep, {SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID}}, |
| 68 | // clang-format on |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 69 | // TODO: Add remaining GLSL.std.450 instructions |
| 70 | }; |
| 71 | |
| 72 | static const spv_ext_inst_desc_t openclStd12Entries[] = { |
| 73 | {"placeholder", 0, {}}, |
| 74 | // TODO: Add remaining OpenCL.std.12 instructions |
| 75 | }; |
| 76 | |
| 77 | static const spv_ext_inst_desc_t openclStd20Entries[] = { |
| 78 | {"placeholder", 0, {}}, |
| 79 | // TODO: Add remaining OpenCL.std.20 instructions |
| 80 | }; |
| 81 | |
| 82 | static const spv_ext_inst_desc_t openclStd21Entries[] = { |
| 83 | {"placeholder", 0, {}}, |
| 84 | // TODO: Add remaining OpenCL.std.21 instructions |
| 85 | }; |
| 86 | |
| 87 | spv_result_t spvExtInstTableGet(spv_ext_inst_table *pExtInstTable) { |
| 88 | spvCheck(!pExtInstTable, return SPV_ERROR_INVALID_POINTER); |
| 89 | |
| 90 | static const spv_ext_inst_group_t groups[] = { |
| 91 | {SPV_EXT_INST_TYPE_GLSL_STD_450, |
| 92 | sizeof(glslStd450Entries) / sizeof(spv_ext_inst_desc_t), |
| 93 | glslStd450Entries}, |
| 94 | {SPV_EXT_INST_TYPE_OPENCL_STD_12, |
| 95 | sizeof(openclStd12Entries) / sizeof(spv_ext_inst_desc_t), |
| 96 | openclStd12Entries}, |
| 97 | {SPV_EXT_INST_TYPE_OPENCL_STD_20, |
| 98 | sizeof(openclStd20Entries) / sizeof(spv_ext_inst_desc_t), |
| 99 | openclStd20Entries}, |
| 100 | {SPV_EXT_INST_TYPE_OPENCL_STD_21, |
| 101 | sizeof(openclStd21Entries) / sizeof(spv_ext_inst_desc_t), |
| 102 | openclStd21Entries}, |
| 103 | }; |
| 104 | |
| 105 | static const spv_ext_inst_table_t table = { |
| 106 | sizeof(groups) / sizeof(spv_ext_inst_group_t), groups}; |
| 107 | |
| 108 | *pExtInstTable = &table; |
| 109 | |
| 110 | return SPV_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | spv_ext_inst_type_t spvExtInstImportTypeGet(const char *name) { |
| 114 | if (!strcmp("GLSL.std.450", name)) { |
| 115 | return SPV_EXT_INST_TYPE_GLSL_STD_450; |
| 116 | } |
| 117 | if (!strcmp("OpenCL.std.12", name)) { |
| 118 | return SPV_EXT_INST_TYPE_OPENCL_STD_12; |
| 119 | } |
| 120 | if (!strcmp("OpenCL.std.20", name)) { |
| 121 | return SPV_EXT_INST_TYPE_OPENCL_STD_20; |
| 122 | } |
| 123 | if (!strcmp("OpenCL.std.21", name)) { |
| 124 | return SPV_EXT_INST_TYPE_OPENCL_STD_21; |
| 125 | } |
| 126 | return SPV_EXT_INST_TYPE_NONE; |
| 127 | } |
| 128 | |
| 129 | spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table, |
| 130 | const spv_ext_inst_type_t type, |
| 131 | const char *name, |
| 132 | spv_ext_inst_desc *pEntry) { |
| 133 | spvCheck(!table, return SPV_ERROR_INVALID_TABLE); |
| 134 | spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER); |
| 135 | |
| 136 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
| 137 | auto &group = table->groups[groupIndex]; |
| 138 | if (type == group.type) { |
| 139 | for (uint32_t index = 0; index < group.count; index++) { |
| 140 | auto &entry = group.entries[index]; |
| 141 | if (!strcmp(name, entry.name)) { |
| 142 | *pEntry = &table->groups[groupIndex].entries[index]; |
| 143 | return SPV_SUCCESS; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return SPV_ERROR_INVALID_LOOKUP; |
| 150 | } |
| 151 | |
| 152 | spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table, |
| 153 | const spv_ext_inst_type_t type, |
| 154 | const uint32_t value, |
| 155 | spv_ext_inst_desc *pEntry) { |
| 156 | spvCheck(!table, return SPV_ERROR_INVALID_TABLE); |
| 157 | spvCheck(!pEntry, return SPV_ERROR_INVALID_POINTER); |
| 158 | |
| 159 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
| 160 | auto &group = table->groups[groupIndex]; |
| 161 | if (type == group.type) { |
| 162 | for (uint32_t index = 0; index < group.count; index++) { |
| 163 | auto &entry = group.entries[index]; |
| 164 | if (value == entry.ext_inst) { |
| 165 | *pEntry = &table->groups[groupIndex].entries[index]; |
| 166 | return SPV_SUCCESS; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return SPV_ERROR_INVALID_LOOKUP; |
| 173 | } |