2 # -*- Mode: Python; py-indent-offset: 8 -*-
4 # (C) Copyright Zack Rusin 2005. All Rights Reserved.
5 # Copyright (C) 2015 Intel Corporation
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:
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
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
27 # Zack Rusin <zack@kde.org>
35 class PrintGlEnums(gl_XML
.gl_print_base
):
38 gl_XML
.gl_print_base
.__init
__(self
)
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")
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"'
52 print 'typedef struct PACKED {'
53 print ' uint16_t offset;'
61 typedef int (*cfunc)(const void *, const void *);
64 * Compare a key enum value to an element in the \c enum_string_table_offsets array.
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.
70 * \param a Pointer to the desired enum name.
71 * \param b Pointer into the \c enum_string_table_offsets array.
73 static int compar_nr( const int *a, enum_elt *b )
79 static char token_tmp[20];
81 const char *_mesa_enum_to_string( int nr )
85 STATIC_ASSERT(sizeof(enum_string_table) < (1 << 16));
87 elt = bsearch(& nr, enum_string_table_offsets,
88 ARRAY_SIZE(enum_string_table_offsets),
89 sizeof(enum_string_table_offsets[0]),
93 return &enum_string_table[elt->offset];
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';
106 static const char *prim_names[PRIM_MAX+3] = {
117 "GL_LINES_ADJACENCY",
118 "GL_LINE_STRIP_ADJACENCY",
119 "GL_TRIANGLES_ADJACENCY",
120 "GL_TRIANGLE_STRIP_ADJACENCY",
127 /* Get the name of an enum given that it is a primitive type. Avoids
128 * GL_FALSE/GL_POINTS ambiguity and others.
131 _mesa_lookup_prim_by_nr(GLuint nr)
133 if (nr < ARRAY_SIZE(prim_names))
134 return prim_names[nr];
136 return "invalid mode";
144 def printBody(self
, api_list
):
147 self
.process_enums( api
)
151 for enum
in sorted(self
.enum_table
.keys()):
154 for [name
, pri
] in self
.enum_table
[ enum
]:
159 enum_table
.append((enum
, best_name
))
163 print '#if defined(__GNUC__)'
164 print '# define LONGSTRING __extension__'
166 print '# define LONGSTRING'
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
179 print 'static const enum_elt enum_string_table_offsets[%u] =' % (len(enum_table
))
181 for enum
, name
in enum_table
:
182 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets
[enum
], enum
, name
)
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
] = []
196 enum
= self
.enum_table
[ obj
.value
]
197 name
= "GL_" + obj
.name
198 priority
= obj
.priority()
204 enum
.append( [name
, priority
] )
208 parser
= argparse
.ArgumentParser()
209 parser
.add_argument('-f', '--input_file',
211 help="Choose an xml file to parse.")
212 return parser
.parse_args()
217 api_list
= [gl_XML
.parse_GL_API(args
.input_file
)]
219 printer
= PrintGlEnums()
220 printer
.Print(api_list
)
223 if __name__
== '__main__':