blob: 7ae384e87f3aae0db371b33eb2de3362c21f86e4 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Lei Zhang2a0b7732015-11-11 13:57:43 -05002//
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 Zhang7a222e42015-11-11 12:40:25 -050027#include "ext_inst.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028
29#include <string.h>
30
David Netod1bb51d2016-02-17 12:17:04 -050031#include "spirv_definition.h"
32
Lei Zhang2d879b92015-08-27 11:22:14 -040033/// 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 Netod1bb51d2016-02-17 12:17:04 -050036 #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 Zhang2d879b92015-08-27 11:22:14 -040042#define GLSL450Inst2(name) \
David Netod1bb51d2016-02-17 12:17:04 -050043 #name, GLSLstd450::GLSLstd450##name, 0, { \
Lei Zhangdca65b32015-08-27 10:00:16 -040044 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
45 }
David Netod1bb51d2016-02-17 12:17:04 -050046#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 Zhang2d879b92015-08-27 11:22:14 -040051#define GLSL450Inst3(name) \
David Netod1bb51d2016-02-17 12:17:04 -050052 #name, GLSLstd450::GLSLstd450##name, 0, { \
Lei Zhangdca65b32015-08-27 10:00:16 -040053 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
54 }
Andrew Woloszyn1d2a87e2015-08-21 13:39:34 -040055
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010056static const spv_ext_inst_desc_t glslStd450Entries[] = {
Lei Zhang2d879b92015-08-27 11:22:14 -040057 {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 Netodbaf4072015-09-22 16:23:06 -040088 {GLSL450Inst1(InverseSqrt)},
Lei Zhang2d879b92015-08-27 11:22:14 -040089 {GLSL450Inst1(Determinant)},
David Netodbaf4072015-09-22 16:23:06 -040090 {GLSL450Inst1(MatrixInverse)},
Lei Zhang2d879b92015-08-27 11:22:14 -040091 {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 Netodbaf4072015-09-22 16:23:06 -0400102 {GLSL450Inst3(FMix)},
103 {GLSL450Inst3(IMix)},
Lei Zhang2d879b92015-08-27 11:22:14 -0400104 {GLSL450Inst2(Step)},
David Netodbaf4072015-09-22 16:23:06 -0400105 {GLSL450Inst3(SmoothStep)},
Lei Zhang2d879b92015-08-27 11:22:14 -0400106 {GLSL450Inst3(Fma)},
107 {GLSL450Inst2(Frexp)},
108 {GLSL450Inst1(FrexpStruct)},
109 {GLSL450Inst2(Ldexp)},
Dejan Mircevskiafdbd2d2015-08-27 13:57:08 -0400110 {GLSL450Inst1(PackSnorm4x8)},
111 {GLSL450Inst1(PackUnorm4x8)},
112 {GLSL450Inst1(PackSnorm2x16)},
113 {GLSL450Inst1(PackUnorm2x16)},
114 {GLSL450Inst1(PackHalf2x16)},
David Netod1bb51d2016-02-17 12:17:04 -0500115 {GLSL450Inst1Cap(PackDouble2x32, Float64)},
Dejan Mircevskiafdbd2d2015-08-27 13:57:08 -0400116 {GLSL450Inst1(UnpackSnorm2x16)},
117 {GLSL450Inst1(UnpackUnorm2x16)},
118 {GLSL450Inst1(UnpackHalf2x16)},
119 {GLSL450Inst1(UnpackSnorm4x8)},
120 {GLSL450Inst1(UnpackUnorm4x8)},
121 {GLSL450Inst1(UnpackDouble2x32)},
Lei Zhang3cb58962015-08-27 13:44:31 -0400122 {GLSL450Inst1(Length)},
123 {GLSL450Inst2(Distance)},
124 {GLSL450Inst2(Cross)},
125 {GLSL450Inst1(Normalize)},
David Netodbaf4072015-09-22 16:23:06 -0400126 {GLSL450Inst3(FaceForward)},
Lei Zhang3cb58962015-08-27 13:44:31 -0400127 {GLSL450Inst2(Reflect)},
128 {GLSL450Inst3(Refract)},
David Netodbaf4072015-09-22 16:23:06 -0400129 {GLSL450Inst1(FindILsb)},
130 {GLSL450Inst1(FindSMsb)},
131 {GLSL450Inst1(FindUMsb)},
David Netod1bb51d2016-02-17 12:17:04 -0500132 {GLSL450Inst1Cap(InterpolateAtCentroid, InterpolationFunction)},
133 {GLSL450Inst2Cap(InterpolateAtSample, InterpolationFunction)},
134 {GLSL450Inst2Cap(InterpolateAtOffset, InterpolationFunction)},
David Neto58c4e1d2016-02-16 19:26:13 -0500135 {GLSL450Inst2(NMin)},
136 {GLSL450Inst2(NMax)},
137 {GLSL450Inst2(NClamp)},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100138};
139
David Neto21c4ad42015-10-14 17:02:11 -0400140static const spv_ext_inst_desc_t openclEntries[] = {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500141#define ExtInst(Name, Opcode, OperandList) \
David Netod1bb51d2016-02-17 12:17:04 -0500142 { #Name, Opcode, 0, OperandList } \
Lei Zhang1a0334e2015-11-02 09:41:20 -0500143 ,
David Neto21c4ad42015-10-14 17:02:11 -0400144#define EmptyList \
145 {}
146#define List(...) \
147 { __VA_ARGS__ }
148#define OperandId SPV_OPERAND_TYPE_ID
David Neto1bb8b702015-10-16 14:16:00 -0400149#define OperandLiteralNumber SPV_OPERAND_TYPE_LITERAL_INTEGER
David Neto21c4ad42015-10-14 17:02:11 -0400150#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)83e5a292015-05-22 18:26:19 +0100160};
161
Lei Zhang1a0334e2015-11-02 09:41:20 -0500162spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400163 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100164
165 static const spv_ext_inst_group_t groups[] = {
166 {SPV_EXT_INST_TYPE_GLSL_STD_450,
David Netoba73a7c2016-01-06 13:08:39 -0500167 static_cast<uint32_t>(sizeof(glslStd450Entries) /
168 sizeof(spv_ext_inst_desc_t)),
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100169 glslStd450Entries},
David Neto21c4ad42015-10-14 17:02:11 -0400170 {SPV_EXT_INST_TYPE_OPENCL_STD,
David Netoba73a7c2016-01-06 13:08:39 -0500171 static_cast<uint32_t>(sizeof(openclEntries) /
172 sizeof(spv_ext_inst_desc_t)),
173 openclEntries},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100174 };
175
176 static const spv_ext_inst_table_t table = {
David Netoba73a7c2016-01-06 13:08:39 -0500177 static_cast<uint32_t>(sizeof(groups) / sizeof(spv_ext_inst_group_t)),
178 groups};
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100179
180 *pExtInstTable = &table;
181
182 return SPV_SUCCESS;
183}
184
Lei Zhang1a0334e2015-11-02 09:41:20 -0500185spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -0400186 // The names are specified by the respective extension instruction
187 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100188 if (!strcmp("GLSL.std.450", name)) {
189 return SPV_EXT_INST_TYPE_GLSL_STD_450;
190 }
David Neto21c4ad42015-10-14 17:02:11 -0400191 if (!strcmp("OpenCL.std", name)) {
192 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100193 }
194 return SPV_EXT_INST_TYPE_NONE;
195}
196
197spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
198 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500199 const char* name,
200 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400201 if (!table) return SPV_ERROR_INVALID_TABLE;
202 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100203
204 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500205 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100206 if (type == group.type) {
207 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500208 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 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
220spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
221 const spv_ext_inst_type_t type,
222 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500223 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400224 if (!table) return SPV_ERROR_INVALID_TABLE;
225 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226
227 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500228 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100229 if (type == group.type) {
230 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500231 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100232 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}