glapi: add gl_dispatch_functions_start and end
[mesa.git] / src / mesa / glapi / gl_apitemp.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
4 # All Rights Reserved.
5 #
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:
12 #
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
15 # Software.
16 #
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
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 import gl_XML, glX_XML
29 import license
30 import sys, getopt
31
32 class PrintGlOffsets(gl_XML.gl_print_base):
33 def __init__(self):
34 gl_XML.gl_print_base.__init__(self)
35
36 self.name = "gl_apitemp.py (from Mesa)"
37 self.license = license.bsd_license_template % ( \
38 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
39 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
40
41 self.undef_list.append( "KEYWORD1" )
42 self.undef_list.append( "KEYWORD1_ALT" )
43 self.undef_list.append( "KEYWORD2" )
44 self.undef_list.append( "NAME" )
45 self.undef_list.append( "DISPATCH" )
46 self.undef_list.append( "RETURN_DISPATCH" )
47 self.undef_list.append( "DISPATCH_TABLE_NAME" )
48 self.undef_list.append( "UNUSED_TABLE_NAME" )
49 self.undef_list.append( "TABLE_ENTRY" )
50
51
52 def printFunction(self, f, name):
53 p_string = ""
54 o_string = ""
55 t_string = ""
56 comma = ""
57
58 if f.is_static_entry_point(name):
59 keyword = "KEYWORD1"
60 else:
61 keyword = "KEYWORD1_ALT"
62
63 n = f.static_name(name)
64
65 for p in f.parameterIterator():
66 if p.is_pointer():
67 cast = "(const void *) "
68 else:
69 cast = ""
70
71 t_string = t_string + comma + p.format_string()
72 p_string = p_string + comma + p.name
73 o_string = o_string + comma + cast + p.name
74 comma = ", "
75
76
77 if f.return_type != 'void':
78 dispatch = "RETURN_DISPATCH"
79 else:
80 dispatch = "DISPATCH"
81
82 if f.has_different_protocol(name):
83 print '#ifndef GLX_INDIRECT_RENDERING'
84
85 if not f.is_static_entry_point(name):
86 print '%s %s KEYWORD2 NAME(%s)(%s);' % (keyword, f.return_type, n, f.get_parameter_string(name))
87 print ''
88
89 print '%s %s KEYWORD2 NAME(%s)(%s)' % (keyword, f.return_type, n, f.get_parameter_string(name))
90 print '{'
91 if p_string == "":
92 print ' %s(%s, (), (F, "gl%s();\\n"));' \
93 % (dispatch, f.name, name)
94 else:
95 print ' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \
96 % (dispatch, f.name, p_string, name, t_string, o_string)
97 print '}'
98 if f.has_different_protocol(name):
99 print '#endif /* GLX_INDIRECT_RENDERING */'
100 print ''
101 return
102
103 def printRealHeader(self):
104 print ''
105 self.printVisibility( "HIDDEN", "hidden" )
106 print """
107 /*
108 * This file is a template which generates the OpenGL API entry point
109 * functions. It should be included by a .c file which first defines
110 * the following macros:
111 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
112 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
113 * NAME(n) - builds the final function name (usually add "gl" prefix)
114 * DISPATCH(func, args, msg) - code to do dispatch of named function.
115 * msg is a printf-style debug message.
116 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
117 *
118 * Here is an example which generates the usual OpenGL functions:
119 * #define KEYWORD1
120 * #define KEYWORD2
121 * #define NAME(func) gl##func
122 * #define DISPATCH(func, args, msg) \\
123 * struct _glapi_table *dispatch = CurrentDispatch; \\
124 * (*dispatch->func) args
125 * #define RETURN DISPATCH(func, args, msg) \\
126 * struct _glapi_table *dispatch = CurrentDispatch; \\
127 * return (*dispatch->func) args
128 *
129 */
130
131
132 #if defined( NAME )
133 #ifndef KEYWORD1
134 #define KEYWORD1
135 #endif
136
137 #ifndef KEYWORD1_ALT
138 #define KEYWORD1_ALT HIDDEN
139 #endif
140
141 #ifndef KEYWORD2
142 #define KEYWORD2
143 #endif
144
145 #ifndef DISPATCH
146 #error DISPATCH must be defined
147 #endif
148
149 #ifndef RETURN_DISPATCH
150 #error RETURN_DISPATCH must be defined
151 #endif
152
153 """
154 return
155
156
157
158 def printInitDispatch(self, api):
159 print """
160 #endif /* defined( NAME ) */
161
162 /*
163 * This is how a dispatch table can be initialized with all the functions
164 * we generated above.
165 */
166 #ifdef DISPATCH_TABLE_NAME
167
168 #ifndef TABLE_ENTRY
169 #error TABLE_ENTRY must be defined
170 #endif
171
172 static _glapi_proc DISPATCH_TABLE_NAME[] = {"""
173 for f in api.functionIterateByOffset():
174 print ' TABLE_ENTRY(%s),' % (f.dispatch_name())
175
176 print ' /* A whole bunch of no-op functions. These might be called'
177 print ' * when someone tries to call a dynamically-registered'
178 print ' * extension function without a current rendering context.'
179 print ' */'
180 for i in range(1, 100):
181 print ' TABLE_ENTRY(Unused),'
182
183 print '};'
184 print '#endif /* DISPATCH_TABLE_NAME */'
185 print ''
186 return
187
188
189 def printAliasedTable(self, api):
190 print """
191 /*
192 * This is just used to silence compiler warnings.
193 * We list the functions which are not otherwise used.
194 */
195 #ifdef UNUSED_TABLE_NAME
196 static _glapi_proc UNUSED_TABLE_NAME[] = {"""
197
198 for f in api.functionIterateByOffset():
199 for n in f.entry_points:
200 if n != f.name:
201 if f.is_static_entry_point(n):
202 text = ' TABLE_ENTRY(%s),' % (n)
203
204 if f.has_different_protocol(n):
205 print '#ifndef GLX_INDIRECT_RENDERING'
206 print text
207 print '#endif'
208 else:
209 print text
210 print '};'
211 print '#endif /*UNUSED_TABLE_NAME*/'
212 print ''
213 return
214
215
216 def printBody(self, api):
217 for func in api.functionIterateByOffset():
218 got_stub = 0
219 for n in func.entry_points:
220 if func.is_static_entry_point(n):
221 self.printFunction(func, n)
222 elif not got_stub:
223 self.printFunction(func, n)
224 got_stub = 1
225
226 self.printInitDispatch(api)
227 self.printAliasedTable(api)
228 return
229
230
231 def show_usage():
232 print "Usage: %s [-f input_file_name]" % sys.argv[0]
233 sys.exit(1)
234
235 if __name__ == '__main__':
236 file_name = "gl_API.xml"
237
238 try:
239 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
240 except Exception,e:
241 show_usage()
242
243 for (arg,val) in args:
244 if arg == "-f":
245 file_name = val
246
247 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
248
249 printer = PrintGlOffsets()
250 printer.Print(api)