glsl: Generate ir_expression_operation.h from Python
[mesa.git] / src / compiler / glsl / ir_expression_operation.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2015 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23
24 import mako.template
25
26 ir_expression_operation = [
27 # Name operands string comment
28 ("bit_not", 1, "~", None),
29 ("logic_not", 1, "!", None),
30 ("neg", 1, None, None),
31 ("abs", 1, None, None),
32 ("sign", 1, None, None),
33 ("rcp", 1, None, None),
34 ("rsq", 1, None, None),
35 ("sqrt", 1, None, None),
36 ("exp", 1, None, "Log base e on gentype"),
37 ("log", 1, None, "Natural log on gentype"),
38 ("exp2", 1, None, None),
39 ("log2", 1, None, None),
40 ("f2i", 1, None, "Float-to-integer conversion."),
41 ("f2u", 1, None, "Float-to-unsigned conversion."),
42 ("i2f", 1, None, "Integer-to-float conversion."),
43 ("f2b", 1, None, "Float-to-boolean conversion"),
44 ("b2f", 1, None, "Boolean-to-float conversion"),
45 ("i2b", 1, None, "int-to-boolean conversion"),
46 ("b2i", 1, None, "Boolean-to-int conversion"),
47 ("u2f", 1, None, "Unsigned-to-float conversion."),
48 ("i2u", 1, None, "Integer-to-unsigned conversion."),
49 ("u2i", 1, None, "Unsigned-to-integer conversion."),
50 ("d2f", 1, None, "Double-to-float conversion."),
51 ("f2d", 1, None, "Float-to-double conversion."),
52 ("d2i", 1, None, "Double-to-integer conversion."),
53 ("i2d", 1, None, "Integer-to-double conversion."),
54 ("d2u", 1, None, "Double-to-unsigned conversion."),
55 ("u2d", 1, None, "Unsigned-to-double conversion."),
56 ("d2b", 1, None, "Double-to-boolean conversion."),
57 ("bitcast_i2f", 1, None, 'Bit-identical int-to-float "conversion"'),
58 ("bitcast_f2i", 1, None, 'Bit-identical float-to-int "conversion"'),
59 ("bitcast_u2f", 1, None, 'Bit-identical uint-to-float "conversion"'),
60 ("bitcast_f2u", 1, None, 'Bit-identical float-to-uint "conversion"'),
61 """
62 /**
63 * \\name Unary floating-point rounding operations.
64 */
65 /*@{*/""",
66 ("trunc", 1, None, None),
67 ("ceil", 1, None, None),
68 ("floor", 1, None, None),
69 ("fract", 1, None, None),
70 ("round_even", 1, None, None),
71 """ /*@}*/
72
73 /**
74 * \\name Trigonometric operations.
75 */
76 /*@{*/""",
77 ("sin", 1, None, None),
78 ("cos", 1, None, None),
79 """ /*@}*/
80
81 /**
82 * \\name Partial derivatives.
83 */
84 /*@{*/""",
85 ("dFdx", 1, None, None),
86 ("dFdx_coarse", 1, "dFdxCoarse", None),
87 ("dFdx_fine", 1, "dFdxFine", None),
88 ("dFdy", 1, None, None),
89 ("dFdy_coarse", 1, "dFdyCoarse", None),
90 ("dFdy_fine", 1, "dFdyFine", None),
91 """ /*@}*/
92
93 /**
94 * \\name Floating point pack and unpack operations.
95 */
96 /*@{*/""",
97 ("pack_snorm_2x16", 1, "packSnorm2x16", None),
98 ("pack_snorm_4x8", 1, "packSnorm4x8", None),
99 ("pack_unorm_2x16", 1, "packUnorm2x16", None),
100 ("pack_unorm_4x8", 1, "packUnorm4x8", None),
101 ("pack_half_2x16", 1, "packHalf2x16", None),
102 ("unpack_snorm_2x16", 1, "unpackSnorm2x16", None),
103 ("unpack_snorm_4x8", 1, "unpackSnorm4x8", None),
104 ("unpack_unorm_2x16", 1, "unpackUnorm2x16", None),
105 ("unpack_unorm_4x8", 1, "unpackUnorm4x8", None),
106 ("unpack_half_2x16", 1, "unpackHalf2x16", None),
107 """ /*@}*/
108
109 /**
110 * \\name Bit operations, part of ARB_gpu_shader5.
111 */
112 /*@{*/""",
113 ("bitfield_reverse", 1, None, None),
114 ("bit_count", 1, None, None),
115 ("find_msb", 1, None, None),
116 ("find_lsb", 1, None, None),
117 """ /*@}*/
118 """,
119 ("saturate", 1, "sat", None),
120 """
121 /**
122 * \\name Double packing, part of ARB_gpu_shader_fp64.
123 */
124 /*@{*/""",
125 ("pack_double_2x32", 1, "packDouble2x32", None),
126 ("unpack_double_2x32", 1, "unpackDouble2x32", None),
127 """ /*@}*/
128 """,
129 ("frexp_sig", 1, None, None),
130 ("frexp_exp", 1, None, None),
131 "",
132 ("noise", 1, None, None),
133 "",
134 ("subroutine_to_int", 1, None, None),
135 """ /**
136 * Interpolate fs input at centroid
137 *
138 * operand0 is the fs input.
139 */""",
140 ("interpolate_at_centroid", 1, None, None),
141 """
142 /**
143 * Ask the driver for the total size of a buffer block.
144 *
145 * operand0 is the ir_constant buffer block index in the linked shader.
146 */""",
147 ("get_buffer_size", 1, None, None),
148 """
149 /**
150 * Calculate length of an unsized array inside a buffer block.
151 * This opcode is going to be replaced in a lowering pass inside
152 * the linker.
153 *
154 * operand0 is the unsized array's ir_value for the calculation
155 * of its length.
156 */""",
157 ("ssbo_unsized_array_length", 1, None, None),
158 """
159 /**
160 * Vote among threads on the value of the boolean argument.
161 */""",
162 ("vote_any", 1, None, None),
163 ("vote_all", 1, None, None),
164 ("vote_eq", 1, None, None),
165
166 """
167 /**
168 * A sentinel marking the last of the unary operations.
169 */
170 ir_last_unop = ir_unop_vote_eq,
171 """,
172 ("add", 2, "+", None),
173 ("sub", 2, "-", None),
174 ("mul", 2, "*", "Floating-point or low 32-bit integer multiply."),
175 ("imul_high", 2, None, "Calculates the high 32-bits of a 64-bit multiply."),
176 ("div", 2, "/", None),
177 """
178 /**
179 * Returns the carry resulting from the addition of the two arguments.
180 */
181 /*@{*/""",
182 ("carry", 2, None, None),
183 """ /*@}*/
184
185 /**
186 * Returns the borrow resulting from the subtraction of the second argument
187 * from the first argument.
188 */
189 /*@{*/""",
190 ("borrow", 2, None, None),
191 """ /*@}*/
192
193 /**
194 * Takes one of two combinations of arguments:
195 *
196 * - mod(vecN, vecN)
197 * - mod(vecN, float)
198 *
199 * Does not take integer types.
200 */""",
201 ("mod", 2, "%", None),
202 """
203 /**
204 * \\name Binary comparison operators which return a boolean vector.
205 * The type of both operands must be equal.
206 */
207 /*@{*/""",
208 ("less", 2, "<", None),
209 ("greater", 2, ">", None),
210 ("lequal", 2, "<=", None),
211 ("gequal", 2, ">=", None),
212 ("equal", 2, "==", None),
213 ("nequal", 2, "!=", None),
214 """ /**
215 * Returns single boolean for whether all components of operands[0]
216 * equal the components of operands[1].
217 */""",
218 ("all_equal", 2, None, None),
219 """ /**
220 * Returns single boolean for whether any component of operands[0]
221 * is not equal to the corresponding component of operands[1].
222 */""",
223 ("any_nequal", 2, None, None),
224 """ /*@}*/
225
226 /**
227 * \\name Bit-wise binary operations.
228 */
229 /*@{*/""",
230 ("lshift", 2, "<<", None),
231 ("rshift", 2, ">>", None),
232 ("bit_and", 2, "&", None),
233 ("bit_xor", 2, "^", None),
234 ("bit_or", 2, "|", None),
235 """ /*@}*/
236 """,
237 ("logic_and", 2, "&&", None),
238 ("logic_xor", 2, "^^", None),
239 ("logic_or", 2, "||", None),
240 "",
241 ("dot", 2, None, None),
242 ("min", 2, None, None),
243 ("max", 2, None, None),
244 "",
245 ("pow", 2, None, None),
246 """
247 /**
248 * Load a value the size of a given GLSL type from a uniform block.
249 *
250 * operand0 is the ir_constant uniform block index in the linked shader.
251 * operand1 is a byte offset within the uniform block.
252 */""",
253 ("ubo_load", 2, None, None),
254 """
255 /**
256 * \\name Multiplies a number by two to a power, part of ARB_gpu_shader5.
257 */
258 /*@{*/""",
259 ("ldexp", 2, None, None),
260 """ /*@}*/
261
262 /**
263 * Extract a scalar from a vector
264 *
265 * operand0 is the vector
266 * operand1 is the index of the field to read from operand0
267 */""",
268 ("vector_extract", 2, None, None),
269 """
270 /**
271 * Interpolate fs input at offset
272 *
273 * operand0 is the fs input
274 * operand1 is the offset from the pixel center
275 */""",
276 ("interpolate_at_offset", 2, None, None),
277 """
278 /**
279 * Interpolate fs input at sample position
280 *
281 * operand0 is the fs input
282 * operand1 is the sample ID
283 */""",
284 ("interpolate_at_sample", 2, None, None),
285 """
286 /**
287 * A sentinel marking the last of the binary operations.
288 */
289 ir_last_binop = ir_binop_interpolate_at_sample,
290
291 /**
292 * \\name Fused floating-point multiply-add, part of ARB_gpu_shader5.
293 */
294 /*@{*/""",
295 ("fma", 3, None, None),
296 """ /*@}*/
297 """,
298 ("lrp", 3, None, None),
299 """
300 /**
301 * \\name Conditional Select
302 *
303 * A vector conditional select instruction (like ?:, but operating per-
304 * component on vectors).
305 *
306 * \\see lower_instructions_visitor::ldexp_to_arith
307 */
308 /*@{*/""",
309 ("csel", 3, None, None),
310 """ /*@}*/
311 """,
312 ("bitfield_extract", 3, None, None),
313 """
314 /**
315 * Generate a value with one field of a vector changed
316 *
317 * operand0 is the vector
318 * operand1 is the value to write into the vector result
319 * operand2 is the index in operand0 to be modified
320 */""",
321 ("vector_insert", 3, None, None),
322 """
323 /**
324 * A sentinel marking the last of the ternary operations.
325 */
326 ir_last_triop = ir_triop_vector_insert,
327 """,
328 ("bitfield_insert", 4, None, None),
329 "",
330 ("vector", 4, None, None),
331 """
332 /**
333 * A sentinel marking the last of the ternary operations.
334 */
335 ir_last_quadop = ir_quadop_vector,
336
337 /**
338 * A sentinel marking the last of all operations.
339 */
340 ir_last_opcode = ir_quadop_vector""",
341 ]
342
343 def name_from_item(item):
344 return "ir_{}op_{}".format(("un", "bin", "tri", "quad")[item[1]-1], item[0])
345
346 if __name__ == "__main__":
347 enum_template = mako.template.Template("""/*
348 * Copyright (C) 2010 Intel Corporation
349 *
350 * Permission is hereby granted, free of charge, to any person obtaining a
351 * copy of this software and associated documentation files (the "Software"),
352 * to deal in the Software without restriction, including without limitation
353 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
354 * and/or sell copies of the Software, and to permit persons to whom the
355 * Software is furnished to do so, subject to the following conditions:
356 *
357 * The above copyright notice and this permission notice (including the next
358 * paragraph) shall be included in all copies or substantial portions of the
359 * Software.
360 *
361 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
362 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
363 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
364 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
365 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
366 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
367 * DEALINGS IN THE SOFTWARE.
368 */
369
370 /* Update ir_expression::get_num_operands() and operator_strs when
371 * updating this list.
372 */
373 enum ir_expression_operation {
374 % for item in values:
375 % if isinstance(item, str):
376 ${item}
377 % else:
378 ${name_from_item(item)},${"" if item[3] is None else " /**< {} */".format(item[3])}
379 % endif
380 % endfor
381 };""")
382
383 print(enum_template.render(values=ir_expression_operation,
384 name_from_item=name_from_item))