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