2 # (C) Copyright IBM Corporation 2004, 2005
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 # on the rights to use, copy, modify, merge, publish, distribute, sub
9 # license, and/or sell copies of the Software, and to permit persons to whom
10 # the Software is furnished to do so, subject to the following conditions:
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
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 NON-INFRINGEMENT. IN NO EVENT SHALL
19 # IBM AND/OR ITS SUPPLIERS 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
25 # Ian Romanick <idr@us.ibm.com>
27 from __future__
import print_function
31 import gl_XML
, glX_XML
34 class PrintGlOffsets(gl_XML
.gl_print_base
):
35 def __init__(self
, es
=False):
36 gl_XML
.gl_print_base
.__init
__(self
)
38 self
.name
= "gl_apitemp.py (from Mesa)"
39 self
.license
= license
.bsd_license_template
% ( \
40 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
41 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
45 self
.undef_list
.append( "KEYWORD1" )
46 self
.undef_list
.append( "KEYWORD1_ALT" )
47 self
.undef_list
.append( "KEYWORD2" )
48 self
.undef_list
.append( "NAME" )
49 self
.undef_list
.append( "DISPATCH" )
50 self
.undef_list
.append( "RETURN_DISPATCH" )
51 self
.undef_list
.append( "DISPATCH_TABLE_NAME" )
52 self
.undef_list
.append( "UNUSED_TABLE_NAME" )
53 self
.undef_list
.append( "TABLE_ENTRY" )
56 def printFunction(self
, f
, name
):
62 if f
.is_static_entry_point(name
):
65 keyword
= "KEYWORD1_ALT"
67 n
= f
.static_name(name
)
71 for p
in f
.parameterIterator(name
):
76 cast
= "(const void *) "
80 t_string
= t_string
+ comma
+ p
.format_string()
81 p_string
= p_string
+ comma
+ p
.name
82 o_string
= o_string
+ comma
+ cast
+ p
.name
85 silence
+= "%s(void) %s;" % (space
, p
.name
);
89 if f
.return_type
!= 'void':
90 dispatch
= "RETURN_DISPATCH"
95 if not f
.is_static_entry_point(name
):
98 cat
, num
= api
.get_category_for_name(name
)
99 if (cat
.startswith("es") or cat
.startswith("GL_OES")):
102 print('%s %s KEYWORD2 NAME(%s)(%s);' % (keyword
, f
.return_type
, n
, f
.get_parameter_string(name
)))
105 print('%s %s KEYWORD2 NAME(%s)(%s)' % (keyword
, f
.return_type
, n
, f
.get_parameter_string(name
)))
108 print(' %s' % (silence
))
110 print(' %s(%s, (), (F, "gl%s();\\n"));' \
111 % (dispatch
, f
.name
, name
))
113 print(' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \
114 % (dispatch
, f
.name
, p_string
, name
, t_string
, o_string
))
119 def printRealHeader(self
):
121 self
.printVisibility( "HIDDEN", "hidden" )
124 * This file is a template which generates the OpenGL API entry point
125 * functions. It should be included by a .c file which first defines
126 * the following macros:
127 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
128 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
129 * NAME(n) - builds the final function name (usually add "gl" prefix)
130 * DISPATCH(func, args, msg) - code to do dispatch of named function.
131 * msg is a printf-style debug message.
132 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
134 * Here is an example which generates the usual OpenGL functions:
137 * #define NAME(func) gl##func
138 * #define DISPATCH(func, args, msg) \\
139 * struct _glapi_table *dispatch = CurrentClientDispatch; \\
140 * (*dispatch->func) args
141 * #define RETURN DISPATCH(func, args, msg) \\
142 * struct _glapi_table *dispatch = CurrentClientDispatch; \\
143 * return (*dispatch->func) args
154 #define KEYWORD1_ALT HIDDEN
162 #error DISPATCH must be defined
165 #ifndef RETURN_DISPATCH
166 #error RETURN_DISPATCH must be defined
174 def printInitDispatch(self
, api
):
176 #endif /* defined( NAME ) */
179 * This is how a dispatch table can be initialized with all the functions
180 * we generated above.
182 #ifdef DISPATCH_TABLE_NAME
185 #error TABLE_ENTRY must be defined
188 #ifdef _GLAPI_SKIP_NORMAL_ENTRY_POINTS
189 #error _GLAPI_SKIP_NORMAL_ENTRY_POINTS must not be defined
192 _glapi_proc DISPATCH_TABLE_NAME[] = {""")
193 for f
in api
.functionIterateByOffset():
194 print(' TABLE_ENTRY(%s),' % (f
.dispatch_name()))
196 print(' /* A whole bunch of no-op functions. These might be called')
197 print(' * when someone tries to call a dynamically-registered')
198 print(' * extension function without a current rendering context.')
200 for i
in range(1, 100):
201 print(' TABLE_ENTRY(Unused),')
204 print('#endif /* DISPATCH_TABLE_NAME */')
209 def printAliasedTable(self
, api
):
212 * This is just used to silence compiler warnings.
213 * We list the functions which are not otherwise used.
215 #ifdef UNUSED_TABLE_NAME
216 _glapi_proc UNUSED_TABLE_NAME[] = {""")
220 for f
in api
.functionIterateByOffset():
221 normal_ents
, proto_ents
= self
.classifyEntryPoints(f
)
224 if f
.name
in normal_ents
:
225 normal_ents
.remove(f
.name
)
226 elif f
.name
in proto_ents
:
227 proto_ents
.remove(f
.name
)
229 normal_ents
= [f
.static_name(ent
) for ent
in normal_ents
]
230 proto_ents
= [f
.static_name(ent
) for ent
in proto_ents
]
232 normal_entries
.extend(normal_ents
)
233 proto_entries
.extend(proto_ents
)
235 print('#ifndef _GLAPI_SKIP_NORMAL_ENTRY_POINTS')
236 for ent
in normal_entries
:
237 print(' TABLE_ENTRY(%s),' % (ent
))
238 print('#endif /* _GLAPI_SKIP_NORMAL_ENTRY_POINTS */')
239 print('#ifndef _GLAPI_SKIP_PROTO_ENTRY_POINTS')
240 for ent
in proto_entries
:
241 print(' TABLE_ENTRY(%s),' % (ent
))
242 print('#endif /* _GLAPI_SKIP_PROTO_ENTRY_POINTS */')
245 print('#endif /*UNUSED_TABLE_NAME*/')
250 def classifyEntryPoints(self
, func
):
255 # classify the entry points
256 for name
in func
.entry_points
:
257 if func
.has_different_protocol(name
):
258 if func
.is_static_entry_point(name
):
259 proto_names
.append(name
)
261 proto_stubs
.append(name
)
263 if func
.is_static_entry_point(name
):
264 normal_names
.append(name
)
266 normal_stubs
.append(name
)
267 # there can be at most one stub for a function
269 normal_names
.append(normal_stubs
[0])
271 proto_names
.append(proto_stubs
[0])
273 return (normal_names
, proto_names
)
275 def printBody(self
, api
):
276 normal_entry_points
= []
277 proto_entry_points
= []
278 for func
in api
.functionIterateByOffset():
279 normal_ents
, proto_ents
= self
.classifyEntryPoints(func
)
280 normal_entry_points
.append((func
, normal_ents
))
281 proto_entry_points
.append((func
, proto_ents
))
283 print('#ifndef _GLAPI_SKIP_NORMAL_ENTRY_POINTS')
285 for func
, ents
in normal_entry_points
:
287 self
.printFunction(func
, ent
)
289 print('#endif /* _GLAPI_SKIP_NORMAL_ENTRY_POINTS */')
291 print('/* these entry points might require different protocols */')
292 print('#ifndef _GLAPI_SKIP_PROTO_ENTRY_POINTS')
294 for func
, ents
in proto_entry_points
:
296 self
.printFunction(func
, ent
)
298 print('#endif /* _GLAPI_SKIP_PROTO_ENTRY_POINTS */')
301 self
.printInitDispatch(api
)
302 self
.printAliasedTable(api
)
307 """Parser arguments and return a namespace."""
308 parser
= argparse
.ArgumentParser()
309 parser
.add_argument('-f',
310 metavar
='<input file name>',
312 default
="gl_API.xml",
313 help="An XML file describing the API.")
314 parser
.add_argument('-c',
317 help="Enable OpenGL ES compatibility")
318 return parser
.parse_args()
325 api
= gl_XML
.parse_GL_API(args
.filename
, glX_XML
.glx_item_factory())
327 printer
= PrintGlOffsets(args
.es
)
331 if __name__
== '__main__':