Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 2 | // |
| 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 Zhang | 7a222e4 | 2015-11-11 12:40:25 -0500 | [diff] [blame] | 27 | #include "ext_inst.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 31 | /// Generate a spv_ext_inst_desc_t literal for a GLSL std450 extended |
| 32 | /// instruction with one/two/three <id> parameter(s). |
| 33 | #define GLSL450Inst1(name) \ |
Lei Zhang | dca65b3 | 2015-08-27 10:00:16 -0400 | [diff] [blame] | 34 | #name, GLSLstd450::GLSLstd450##name, { SPV_OPERAND_TYPE_ID } |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 35 | #define GLSL450Inst2(name) \ |
Lei Zhang | dca65b3 | 2015-08-27 10:00:16 -0400 | [diff] [blame] | 36 | #name, GLSLstd450::GLSLstd450##name, { \ |
| 37 | SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \ |
| 38 | } |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 39 | #define GLSL450Inst3(name) \ |
Lei Zhang | dca65b3 | 2015-08-27 10:00:16 -0400 | [diff] [blame] | 40 | #name, GLSLstd450::GLSLstd450##name, { \ |
| 41 | SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \ |
| 42 | } |
Andrew Woloszyn | 1d2a87e | 2015-08-21 13:39:34 -0400 | [diff] [blame] | 43 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 44 | static const spv_ext_inst_desc_t glslStd450Entries[] = { |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 45 | {GLSL450Inst1(Round)}, |
| 46 | {GLSL450Inst1(RoundEven)}, |
| 47 | {GLSL450Inst1(Trunc)}, |
| 48 | {GLSL450Inst1(FAbs)}, |
| 49 | {GLSL450Inst1(SAbs)}, |
| 50 | {GLSL450Inst1(FSign)}, |
| 51 | {GLSL450Inst1(SSign)}, |
| 52 | {GLSL450Inst1(Floor)}, |
| 53 | {GLSL450Inst1(Ceil)}, |
| 54 | {GLSL450Inst1(Fract)}, |
| 55 | {GLSL450Inst1(Radians)}, |
| 56 | {GLSL450Inst1(Degrees)}, |
| 57 | {GLSL450Inst1(Sin)}, |
| 58 | {GLSL450Inst1(Cos)}, |
| 59 | {GLSL450Inst1(Tan)}, |
| 60 | {GLSL450Inst1(Asin)}, |
| 61 | {GLSL450Inst1(Acos)}, |
| 62 | {GLSL450Inst1(Atan)}, |
| 63 | {GLSL450Inst1(Sinh)}, |
| 64 | {GLSL450Inst1(Cosh)}, |
| 65 | {GLSL450Inst1(Tanh)}, |
| 66 | {GLSL450Inst1(Asinh)}, |
| 67 | {GLSL450Inst1(Acosh)}, |
| 68 | {GLSL450Inst1(Atanh)}, |
| 69 | {GLSL450Inst2(Atan2)}, |
| 70 | {GLSL450Inst2(Pow)}, |
| 71 | {GLSL450Inst1(Exp)}, |
| 72 | {GLSL450Inst1(Log)}, |
| 73 | {GLSL450Inst1(Exp2)}, |
| 74 | {GLSL450Inst1(Log2)}, |
| 75 | {GLSL450Inst1(Sqrt)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 76 | {GLSL450Inst1(InverseSqrt)}, |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 77 | {GLSL450Inst1(Determinant)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 78 | {GLSL450Inst1(MatrixInverse)}, |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 79 | {GLSL450Inst2(Modf)}, |
| 80 | {GLSL450Inst1(ModfStruct)}, |
| 81 | {GLSL450Inst2(FMin)}, |
| 82 | {GLSL450Inst2(UMin)}, |
| 83 | {GLSL450Inst2(SMin)}, |
| 84 | {GLSL450Inst2(FMax)}, |
| 85 | {GLSL450Inst2(UMax)}, |
| 86 | {GLSL450Inst2(SMax)}, |
| 87 | {GLSL450Inst3(FClamp)}, |
| 88 | {GLSL450Inst3(UClamp)}, |
| 89 | {GLSL450Inst3(SClamp)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 90 | {GLSL450Inst3(FMix)}, |
| 91 | {GLSL450Inst3(IMix)}, |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 92 | {GLSL450Inst2(Step)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 93 | {GLSL450Inst3(SmoothStep)}, |
Lei Zhang | 2d879b9 | 2015-08-27 11:22:14 -0400 | [diff] [blame] | 94 | {GLSL450Inst3(Fma)}, |
| 95 | {GLSL450Inst2(Frexp)}, |
| 96 | {GLSL450Inst1(FrexpStruct)}, |
| 97 | {GLSL450Inst2(Ldexp)}, |
Dejan Mircevski | afdbd2d | 2015-08-27 13:57:08 -0400 | [diff] [blame] | 98 | {GLSL450Inst1(PackSnorm4x8)}, |
| 99 | {GLSL450Inst1(PackUnorm4x8)}, |
| 100 | {GLSL450Inst1(PackSnorm2x16)}, |
| 101 | {GLSL450Inst1(PackUnorm2x16)}, |
| 102 | {GLSL450Inst1(PackHalf2x16)}, |
| 103 | {GLSL450Inst1(PackDouble2x32)}, |
| 104 | {GLSL450Inst1(UnpackSnorm2x16)}, |
| 105 | {GLSL450Inst1(UnpackUnorm2x16)}, |
| 106 | {GLSL450Inst1(UnpackHalf2x16)}, |
| 107 | {GLSL450Inst1(UnpackSnorm4x8)}, |
| 108 | {GLSL450Inst1(UnpackUnorm4x8)}, |
| 109 | {GLSL450Inst1(UnpackDouble2x32)}, |
Lei Zhang | 3cb5896 | 2015-08-27 13:44:31 -0400 | [diff] [blame] | 110 | {GLSL450Inst1(Length)}, |
| 111 | {GLSL450Inst2(Distance)}, |
| 112 | {GLSL450Inst2(Cross)}, |
| 113 | {GLSL450Inst1(Normalize)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 114 | {GLSL450Inst3(FaceForward)}, |
Lei Zhang | 3cb5896 | 2015-08-27 13:44:31 -0400 | [diff] [blame] | 115 | {GLSL450Inst2(Reflect)}, |
| 116 | {GLSL450Inst3(Refract)}, |
David Neto | dbaf407 | 2015-09-22 16:23:06 -0400 | [diff] [blame] | 117 | {GLSL450Inst1(FindILsb)}, |
| 118 | {GLSL450Inst1(FindSMsb)}, |
| 119 | {GLSL450Inst1(FindUMsb)}, |
Lei Zhang | 5b73214 | 2015-08-27 14:20:19 -0400 | [diff] [blame] | 120 | {GLSL450Inst1(InterpolateAtCentroid)}, |
| 121 | {GLSL450Inst2(InterpolateAtSample)}, |
| 122 | {GLSL450Inst2(InterpolateAtOffset)}, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 125 | static const spv_ext_inst_desc_t openclEntries[] = { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 126 | #define ExtInst(Name, Opcode, OperandList) \ |
| 127 | { #Name, Opcode, OperandList } \ |
| 128 | , |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 129 | #define EmptyList \ |
| 130 | {} |
| 131 | #define List(...) \ |
| 132 | { __VA_ARGS__ } |
| 133 | #define OperandId SPV_OPERAND_TYPE_ID |
David Neto | 1bb8b70 | 2015-10-16 14:16:00 -0400 | [diff] [blame] | 134 | #define OperandLiteralNumber SPV_OPERAND_TYPE_LITERAL_INTEGER |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 135 | #define OperandFPRoundingMode SPV_OPERAND_TYPE_FP_ROUNDING_MODE |
| 136 | #define OperandVariableIds SPV_OPERAND_TYPE_VARIABLE_ID |
| 137 | #include "opencl_std_ext_inst.inc" |
| 138 | #undef ExtList |
| 139 | #undef EmptyList |
| 140 | #undef List |
| 141 | #undef OperandId |
| 142 | #undef OperandLiteralNumber |
| 143 | #undef OperandFPRoundingMode |
| 144 | #undef OperandVariableIds |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 147 | spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 148 | if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 149 | |
| 150 | static const spv_ext_inst_group_t groups[] = { |
| 151 | {SPV_EXT_INST_TYPE_GLSL_STD_450, |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 152 | static_cast<uint32_t>(sizeof(glslStd450Entries) / |
| 153 | sizeof(spv_ext_inst_desc_t)), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 154 | glslStd450Entries}, |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 155 | {SPV_EXT_INST_TYPE_OPENCL_STD, |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 156 | static_cast<uint32_t>(sizeof(openclEntries) / |
| 157 | sizeof(spv_ext_inst_desc_t)), |
| 158 | openclEntries}, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | static const spv_ext_inst_table_t table = { |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 162 | static_cast<uint32_t>(sizeof(groups) / sizeof(spv_ext_inst_group_t)), |
| 163 | groups}; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 164 | |
| 165 | *pExtInstTable = &table; |
| 166 | |
| 167 | return SPV_SUCCESS; |
| 168 | } |
| 169 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 170 | spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) { |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 171 | // The names are specified by the respective extension instruction |
| 172 | // specifications. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 173 | if (!strcmp("GLSL.std.450", name)) { |
| 174 | return SPV_EXT_INST_TYPE_GLSL_STD_450; |
| 175 | } |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 176 | if (!strcmp("OpenCL.std", name)) { |
| 177 | return SPV_EXT_INST_TYPE_OPENCL_STD; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 178 | } |
| 179 | return SPV_EXT_INST_TYPE_NONE; |
| 180 | } |
| 181 | |
| 182 | spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table, |
| 183 | const spv_ext_inst_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 184 | const char* name, |
| 185 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 186 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 187 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 188 | |
| 189 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 190 | auto& group = table->groups[groupIndex]; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 191 | if (type == group.type) { |
| 192 | for (uint32_t index = 0; index < group.count; index++) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 193 | auto& entry = group.entries[index]; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 194 | if (!strcmp(name, entry.name)) { |
| 195 | *pEntry = &table->groups[groupIndex].entries[index]; |
| 196 | return SPV_SUCCESS; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return SPV_ERROR_INVALID_LOOKUP; |
| 203 | } |
| 204 | |
| 205 | spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table, |
| 206 | const spv_ext_inst_type_t type, |
| 207 | const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 208 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 209 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 210 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 211 | |
| 212 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 213 | auto& group = table->groups[groupIndex]; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 214 | if (type == group.type) { |
| 215 | for (uint32_t index = 0; index < group.count; index++) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 216 | auto& entry = group.entries[index]; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 217 | if (value == entry.ext_inst) { |
| 218 | *pEntry = &table->groups[groupIndex].entries[index]; |
| 219 | return SPV_SUCCESS; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return SPV_ERROR_INVALID_LOOKUP; |
| 226 | } |