blob: 41f1723805962a61a91de779bbf26f4b3153f37a [file] [log] [blame]
Lei Zhang2a0b7732015-11-11 13:57:43 -05001// Copyright (c) 2015 The Khronos Group Inc.
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 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
Lei Zhang2d879b92015-08-27 11:22:14 -040031/// 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 Zhangdca65b32015-08-27 10:00:16 -040034 #name, GLSLstd450::GLSLstd450##name, { SPV_OPERAND_TYPE_ID }
Lei Zhang2d879b92015-08-27 11:22:14 -040035#define GLSL450Inst2(name) \
Lei Zhangdca65b32015-08-27 10:00:16 -040036 #name, GLSLstd450::GLSLstd450##name, { \
37 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
38 }
Lei Zhang2d879b92015-08-27 11:22:14 -040039#define GLSL450Inst3(name) \
Lei Zhangdca65b32015-08-27 10:00:16 -040040 #name, GLSLstd450::GLSLstd450##name, { \
41 SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID, SPV_OPERAND_TYPE_ID \
42 }
Andrew Woloszyn1d2a87e2015-08-21 13:39:34 -040043
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010044static const spv_ext_inst_desc_t glslStd450Entries[] = {
Lei Zhang2d879b92015-08-27 11:22:14 -040045 {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 Netodbaf4072015-09-22 16:23:06 -040076 {GLSL450Inst1(InverseSqrt)},
Lei Zhang2d879b92015-08-27 11:22:14 -040077 {GLSL450Inst1(Determinant)},
David Netodbaf4072015-09-22 16:23:06 -040078 {GLSL450Inst1(MatrixInverse)},
Lei Zhang2d879b92015-08-27 11:22:14 -040079 {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 Netodbaf4072015-09-22 16:23:06 -040090 {GLSL450Inst3(FMix)},
91 {GLSL450Inst3(IMix)},
Lei Zhang2d879b92015-08-27 11:22:14 -040092 {GLSL450Inst2(Step)},
David Netodbaf4072015-09-22 16:23:06 -040093 {GLSL450Inst3(SmoothStep)},
Lei Zhang2d879b92015-08-27 11:22:14 -040094 {GLSL450Inst3(Fma)},
95 {GLSL450Inst2(Frexp)},
96 {GLSL450Inst1(FrexpStruct)},
97 {GLSL450Inst2(Ldexp)},
Dejan Mircevskiafdbd2d2015-08-27 13:57:08 -040098 {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 Zhang3cb58962015-08-27 13:44:31 -0400110 {GLSL450Inst1(Length)},
111 {GLSL450Inst2(Distance)},
112 {GLSL450Inst2(Cross)},
113 {GLSL450Inst1(Normalize)},
David Netodbaf4072015-09-22 16:23:06 -0400114 {GLSL450Inst3(FaceForward)},
Lei Zhang3cb58962015-08-27 13:44:31 -0400115 {GLSL450Inst2(Reflect)},
116 {GLSL450Inst3(Refract)},
David Netodbaf4072015-09-22 16:23:06 -0400117 {GLSL450Inst1(FindILsb)},
118 {GLSL450Inst1(FindSMsb)},
119 {GLSL450Inst1(FindUMsb)},
Lei Zhang5b732142015-08-27 14:20:19 -0400120 {GLSL450Inst1(InterpolateAtCentroid)},
121 {GLSL450Inst2(InterpolateAtSample)},
122 {GLSL450Inst2(InterpolateAtOffset)},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100123};
124
David Neto21c4ad42015-10-14 17:02:11 -0400125static const spv_ext_inst_desc_t openclEntries[] = {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500126#define ExtInst(Name, Opcode, OperandList) \
127 { #Name, Opcode, OperandList } \
128 ,
David Neto21c4ad42015-10-14 17:02:11 -0400129#define EmptyList \
130 {}
131#define List(...) \
132 { __VA_ARGS__ }
133#define OperandId SPV_OPERAND_TYPE_ID
David Neto1bb8b702015-10-16 14:16:00 -0400134#define OperandLiteralNumber SPV_OPERAND_TYPE_LITERAL_INTEGER
David Neto21c4ad42015-10-14 17:02:11 -0400135#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)83e5a292015-05-22 18:26:19 +0100145};
146
Lei Zhang1a0334e2015-11-02 09:41:20 -0500147spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400148 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100149
150 static const spv_ext_inst_group_t groups[] = {
151 {SPV_EXT_INST_TYPE_GLSL_STD_450,
David Netoba73a7c2016-01-06 13:08:39 -0500152 static_cast<uint32_t>(sizeof(glslStd450Entries) /
153 sizeof(spv_ext_inst_desc_t)),
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100154 glslStd450Entries},
David Neto21c4ad42015-10-14 17:02:11 -0400155 {SPV_EXT_INST_TYPE_OPENCL_STD,
David Netoba73a7c2016-01-06 13:08:39 -0500156 static_cast<uint32_t>(sizeof(openclEntries) /
157 sizeof(spv_ext_inst_desc_t)),
158 openclEntries},
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100159 };
160
161 static const spv_ext_inst_table_t table = {
David Netoba73a7c2016-01-06 13:08:39 -0500162 static_cast<uint32_t>(sizeof(groups) / sizeof(spv_ext_inst_group_t)),
163 groups};
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100164
165 *pExtInstTable = &table;
166
167 return SPV_SUCCESS;
168}
169
Lei Zhang1a0334e2015-11-02 09:41:20 -0500170spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -0400171 // The names are specified by the respective extension instruction
172 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100173 if (!strcmp("GLSL.std.450", name)) {
174 return SPV_EXT_INST_TYPE_GLSL_STD_450;
175 }
David Neto21c4ad42015-10-14 17:02:11 -0400176 if (!strcmp("OpenCL.std", name)) {
177 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100178 }
179 return SPV_EXT_INST_TYPE_NONE;
180}
181
182spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
183 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500184 const char* name,
185 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400186 if (!table) return SPV_ERROR_INVALID_TABLE;
187 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100188
189 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500190 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100191 if (type == group.type) {
192 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500193 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100194 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
205spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
206 const spv_ext_inst_type_t type,
207 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500208 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400209 if (!table) return SPV_ERROR_INVALID_TABLE;
210 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100211
212 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500213 auto& group = table->groups[groupIndex];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100214 if (type == group.type) {
215 for (uint32_t index = 0; index < group.count; index++) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500216 auto& entry = group.entries[index];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 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}