Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mapi / glapi / gen / gl_enums.py
1 #!/usr/bin/python2
2 # -*- Mode: Python; py-indent-offset: 8 -*-
3
4 # (C) Copyright Zack Rusin 2005. All Rights Reserved.
5 # Copyright (C) 2015 Intel Corporation
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # Authors:
27 # Zack Rusin <zack@kde.org>
28
29 import argparse
30
31 import license
32 import gl_XML
33 import sys, getopt
34
35 class PrintGlEnums(gl_XML.gl_print_base):
36
37 def __init__(self):
38 gl_XML.gl_print_base.__init__(self)
39
40 self.name = "gl_enums.py (from Mesa)"
41 self.license = license.bsd_license_template % ( \
42 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
43 self.enum_table = {}
44
45
46 def printRealHeader(self):
47 print '#include "main/glheader.h"'
48 print '#include "main/enums.h"'
49 print '#include "main/imports.h"'
50 print '#include "main/mtypes.h"'
51 print ''
52 print 'typedef struct PACKED {'
53 print ' uint16_t offset;'
54 print ' int n;'
55 print '} enum_elt;'
56 print ''
57 return
58
59 def print_code(self):
60 print """
61 typedef int (*cfunc)(const void *, const void *);
62
63 /**
64 * Compare a key enum value to an element in the \c enum_string_table_offsets array.
65 *
66 * \c bsearch always passes the key as the first parameter and the pointer
67 * to the array element as the second parameter. We can elimiate some
68 * extra work by taking advantage of that fact.
69 *
70 * \param a Pointer to the desired enum name.
71 * \param b Pointer into the \c enum_string_table_offsets array.
72 */
73 static int compar_nr( const int *a, enum_elt *b )
74 {
75 return a[0] - b->n;
76 }
77
78
79 static char token_tmp[20];
80
81 const char *_mesa_enum_to_string( int nr )
82 {
83 enum_elt *elt;
84
85 STATIC_ASSERT(sizeof(enum_string_table) < (1 << 16));
86
87 elt = bsearch(& nr, enum_string_table_offsets,
88 ARRAY_SIZE(enum_string_table_offsets),
89 sizeof(enum_string_table_offsets[0]),
90 (cfunc) compar_nr);
91
92 if (elt != NULL) {
93 return &enum_string_table[elt->offset];
94 }
95 else {
96 /* this is not re-entrant safe, no big deal here */
97 _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
98 token_tmp[sizeof(token_tmp) - 1] = '\\0';
99 return token_tmp;
100 }
101 }
102
103 /**
104 * Primitive names
105 */
106 static const char *prim_names[PRIM_MAX+3] = {
107 "GL_POINTS",
108 "GL_LINES",
109 "GL_LINE_LOOP",
110 "GL_LINE_STRIP",
111 "GL_TRIANGLES",
112 "GL_TRIANGLE_STRIP",
113 "GL_TRIANGLE_FAN",
114 "GL_QUADS",
115 "GL_QUAD_STRIP",
116 "GL_POLYGON",
117 "GL_LINES_ADJACENCY",
118 "GL_LINE_STRIP_ADJACENCY",
119 "GL_TRIANGLES_ADJACENCY",
120 "GL_TRIANGLE_STRIP_ADJACENCY",
121 "GL_PATCHES",
122 "outside begin/end",
123 "unknown state"
124 };
125
126
127 /* Get the name of an enum given that it is a primitive type. Avoids
128 * GL_FALSE/GL_POINTS ambiguity and others.
129 */
130 const char *
131 _mesa_lookup_prim_by_nr(GLuint nr)
132 {
133 if (nr < ARRAY_SIZE(prim_names))
134 return prim_names[nr];
135 else
136 return "invalid mode";
137 }
138
139
140 """
141 return
142
143
144 def printBody(self, api_list):
145 self.enum_table = {}
146 for api in api_list:
147 self.process_enums( api )
148
149 enum_table = []
150
151 for enum in sorted(self.enum_table.keys()):
152 low_pri = 9
153 best_name = ''
154 for [name, pri] in self.enum_table[ enum ]:
155 if pri < low_pri:
156 low_pri = pri
157 best_name = name
158
159 enum_table.append((enum, best_name))
160
161 string_offsets = {}
162 i = 0;
163 print '#if defined(__GNUC__)'
164 print '# define LONGSTRING __extension__'
165 print '#else'
166 print '# define LONGSTRING'
167 print '#endif'
168 print ''
169 print 'LONGSTRING static const char enum_string_table[] = '
170 for enum, name in enum_table:
171 print ' "%s\\0"' % (name)
172 string_offsets[ enum ] = i
173 i += len(name) + 1
174
175 print ' ;'
176 print ''
177
178
179 print 'static const enum_elt enum_string_table_offsets[%u] =' % (len(enum_table))
180 print '{'
181 for enum, name in enum_table:
182 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets[enum], enum, name)
183 print '};'
184 print ''
185
186 self.print_code()
187 return
188
189
190 def process_enums(self, api):
191 for obj in api.enumIterateByName():
192 if obj.value not in self.enum_table:
193 self.enum_table[ obj.value ] = []
194
195
196 enum = self.enum_table[ obj.value ]
197 name = "GL_" + obj.name
198 priority = obj.priority()
199 already_in = False;
200 for n, p in enum:
201 if n == name:
202 already_in = True
203 if not already_in:
204 enum.append( [name, priority] )
205
206
207 def _parser():
208 parser = argparse.ArgumentParser()
209 parser.add_argument('-f', '--input_file',
210 required=True,
211 help="Choose an xml file to parse.")
212 return parser.parse_args()
213
214
215 def main():
216 args = _parser()
217 api_list = [gl_XML.parse_GL_API(args.input_file)]
218
219 printer = PrintGlEnums()
220 printer.Print(api_list)
221
222
223 if __name__ == '__main__':
224 main()