3 # (C) Copyright IBM Corporation 2004, 2005
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 # Ian Romanick <idr@us.ibm.com>
30 import gl_XML
, glX_XML
33 class PrintGlOffsets(gl_XML
.gl_print_base
):
34 def __init__(self
, es
=False):
35 gl_XML
.gl_print_base
.__init
__(self
)
37 self
.name
= "gl_apitemp.py (from Mesa)"
38 self
.license
= license
.bsd_license_template
% ( \
39 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
40 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
44 self
.undef_list
.append( "KEYWORD1" )
45 self
.undef_list
.append( "KEYWORD1_ALT" )
46 self
.undef_list
.append( "KEYWORD2" )
47 self
.undef_list
.append( "NAME" )
48 self
.undef_list
.append( "DISPATCH" )
49 self
.undef_list
.append( "RETURN_DISPATCH" )
50 self
.undef_list
.append( "DISPATCH_TABLE_NAME" )
51 self
.undef_list
.append( "UNUSED_TABLE_NAME" )
52 self
.undef_list
.append( "TABLE_ENTRY" )
55 def printFunction(self
, f
, name
):
61 if f
.is_static_entry_point(name
):
64 keyword
= "KEYWORD1_ALT"
66 n
= f
.static_name(name
)
70 for p
in f
.parameterIterator(name
):
75 cast
= "(const void *) "
79 t_string
= t_string
+ comma
+ p
.format_string()
80 p_string
= p_string
+ comma
+ p
.name
81 o_string
= o_string
+ comma
+ cast
+ p
.name
84 silence
+= "%s(void) %s;" % (space
, p
.name
);
88 if f
.return_type
!= 'void':
89 dispatch
= "RETURN_DISPATCH"
94 if not f
.is_static_entry_point(name
):
97 cat
, num
= api
.get_category_for_name(name
)
98 if (cat
.startswith("es") or cat
.startswith("GL_OES")):
101 print '%s %s KEYWORD2 NAME(%s)(%s);' % (keyword
, f
.return_type
, n
, f
.get_parameter_string(name
))
104 print '%s %s KEYWORD2 NAME(%s)(%s)' % (keyword
, f
.return_type
, n
, f
.get_parameter_string(name
))
107 print ' %s' % (silence
)
109 print ' %s(%s, (), (F, "gl%s();\\n"));' \
110 % (dispatch
, f
.name
, name
)
112 print ' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \
113 % (dispatch
, f
.name
, p_string
, name
, t_string
, o_string
)
118 def printRealHeader(self
):
120 self
.printVisibility( "HIDDEN", "hidden" )
123 * This file is a template which generates the OpenGL API entry point
124 * functions. It should be included by a .c file which first defines
125 * the following macros:
126 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
127 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
128 * NAME(n) - builds the final function name (usually add "gl" prefix)
129 * DISPATCH(func, args, msg) - code to do dispatch of named function.
130 * msg is a printf-style debug message.
131 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
133 * Here is an example which generates the usual OpenGL functions:
136 * #define NAME(func) gl##func
137 * #define DISPATCH(func, args, msg) \\
138 * struct _glapi_table *dispatch = CurrentDispatch; \\
139 * (*dispatch->func) args
140 * #define RETURN DISPATCH(func, args, msg) \\
141 * struct _glapi_table *dispatch = CurrentDispatch; \\
142 * return (*dispatch->func) args
153 #define KEYWORD1_ALT HIDDEN
161 #error DISPATCH must be defined
164 #ifndef RETURN_DISPATCH
165 #error RETURN_DISPATCH must be defined
173 def printInitDispatch(self
, api
):
175 #endif /* defined( NAME ) */
178 * This is how a dispatch table can be initialized with all the functions
179 * we generated above.
181 #ifdef DISPATCH_TABLE_NAME
184 #error TABLE_ENTRY must be defined
187 #ifdef _GLAPI_SKIP_NORMAL_ENTRY_POINTS
188 #error _GLAPI_SKIP_NORMAL_ENTRY_POINTS must not be defined
191 _glapi_proc DISPATCH_TABLE_NAME[] = {"""
192 for f
in api
.functionIterateByOffset():
193 print ' TABLE_ENTRY(%s),' % (f
.dispatch_name())
195 print ' /* A whole bunch of no-op functions. These might be called'
196 print ' * when someone tries to call a dynamically-registered'
197 print ' * extension function without a current rendering context.'
199 for i
in range(1, 100):
200 print ' TABLE_ENTRY(Unused),'
203 print '#endif /* DISPATCH_TABLE_NAME */'
208 def printAliasedTable(self
, api
):
211 * This is just used to silence compiler warnings.
212 * We list the functions which are not otherwise used.
214 #ifdef UNUSED_TABLE_NAME
215 _glapi_proc UNUSED_TABLE_NAME[] = {"""
219 for f
in api
.functionIterateByOffset():
220 normal_ents
, proto_ents
= self
.classifyEntryPoints(f
)
223 if f
.name
in normal_ents
:
224 normal_ents
.remove(f
.name
)
225 elif f
.name
in proto_ents
:
226 proto_ents
.remove(f
.name
)
228 normal_ents
= [f
.static_name(ent
) for ent
in normal_ents
]
229 proto_ents
= [f
.static_name(ent
) for ent
in proto_ents
]
231 normal_entries
.extend(normal_ents
)
232 proto_entries
.extend(proto_ents
)
234 print '#ifndef _GLAPI_SKIP_NORMAL_ENTRY_POINTS'
235 for ent
in normal_entries
:
236 print ' TABLE_ENTRY(%s),' % (ent
)
237 print '#endif /* _GLAPI_SKIP_NORMAL_ENTRY_POINTS */'
238 print '#ifndef _GLAPI_SKIP_PROTO_ENTRY_POINTS'
239 for ent
in proto_entries
:
240 print ' TABLE_ENTRY(%s),' % (ent
)
241 print '#endif /* _GLAPI_SKIP_PROTO_ENTRY_POINTS */'
244 print '#endif /*UNUSED_TABLE_NAME*/'
249 def classifyEntryPoints(self
, func
):
254 # classify the entry points
255 for name
in func
.entry_points
:
256 if func
.has_different_protocol(name
):
257 if func
.is_static_entry_point(name
):
258 proto_names
.append(name
)
260 proto_stubs
.append(name
)
262 if func
.is_static_entry_point(name
):
263 normal_names
.append(name
)
265 normal_stubs
.append(name
)
266 # there can be at most one stub for a function
268 normal_names
.append(normal_stubs
[0])
270 proto_names
.append(proto_stubs
[0])
272 return (normal_names
, proto_names
)
274 def printBody(self
, api
):
275 normal_entry_points
= []
276 proto_entry_points
= []
277 for func
in api
.functionIterateByOffset():
278 normal_ents
, proto_ents
= self
.classifyEntryPoints(func
)
279 normal_entry_points
.append((func
, normal_ents
))
280 proto_entry_points
.append((func
, proto_ents
))
282 print '#ifndef _GLAPI_SKIP_NORMAL_ENTRY_POINTS'
284 for func
, ents
in normal_entry_points
:
286 self
.printFunction(func
, ent
)
288 print '#endif /* _GLAPI_SKIP_NORMAL_ENTRY_POINTS */'
290 print '/* these entry points might require different protocols */'
291 print '#ifndef _GLAPI_SKIP_PROTO_ENTRY_POINTS'
293 for func
, ents
in proto_entry_points
:
295 self
.printFunction(func
, ent
)
297 print '#endif /* _GLAPI_SKIP_PROTO_ENTRY_POINTS */'
300 self
.printInitDispatch(api
)
301 self
.printAliasedTable(api
)
306 """Parser arguments and return a namespace."""
307 parser
= argparse
.ArgumentParser()
308 parser
.add_argument('-f',
309 metavar
='<input file name>',
311 default
="gl_API.xml",
312 help="An XML file describing the API.")
313 parser
.add_argument('-c',
316 help="Enable OpenGL ES compatibility")
317 return parser
.parse_args()
324 api
= gl_XML
.parse_GL_API(args
.filename
, glX_XML
.glx_item_factory())
326 printer
= PrintGlOffsets(args
.es
)
330 if __name__
== '__main__':