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