Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[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_padding:
67 continue
68
69 if p.is_pointer():
70 cast = "(const void *) "
71 else:
72 cast = ""
73
74 t_string = t_string + comma + p.format_string()
75 p_string = p_string + comma + p.name
76 o_string = o_string + comma + cast + p.name
77 comma = ", "
78
79
80 if f.return_type != 'void':
81 dispatch = "RETURN_DISPATCH"
82 else:
83 dispatch = "DISPATCH"
84
85 if f.has_different_protocol(name):
86 print '#ifndef GLX_INDIRECT_RENDERING'
87
88 if not f.is_static_entry_point(name):
89 print '%s %s KEYWORD2 NAME(%s)(%s);' % (keyword, f.return_type, n, f.get_parameter_string(name))
90 print ''
91
92 print '%s %s KEYWORD2 NAME(%s)(%s)' % (keyword, f.return_type, n, f.get_parameter_string(name))
93 print '{'
94 if p_string == "":
95 print ' %s(%s, (), (F, "gl%s();\\n"));' \
96 % (dispatch, f.name, name)
97 else:
98 print ' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \
99 % (dispatch, f.name, p_string, name, t_string, o_string)
100 print '}'
101 if f.has_different_protocol(name):
102 print '#endif /* GLX_INDIRECT_RENDERING */'
103 print ''
104 return
105
106 def printRealHeader(self):
107 print ''
108 self.printVisibility( "HIDDEN", "hidden" )
109 print """
110 /*
111 * This file is a template which generates the OpenGL API entry point
112 * functions. It should be included by a .c file which first defines
113 * the following macros:
114 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
115 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
116 * NAME(n) - builds the final function name (usually add "gl" prefix)
117 * DISPATCH(func, args, msg) - code to do dispatch of named function.
118 * msg is a printf-style debug message.
119 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
120 *
121 * Here is an example which generates the usual OpenGL functions:
122 * #define KEYWORD1
123 * #define KEYWORD2
124 * #define NAME(func) gl##func
125 * #define DISPATCH(func, args, msg) \\
126 * struct _glapi_table *dispatch = CurrentDispatch; \\
127 * (*dispatch->func) args
128 * #define RETURN DISPATCH(func, args, msg) \\
129 * struct _glapi_table *dispatch = CurrentDispatch; \\
130 * return (*dispatch->func) args
131 *
132 */
133
134
135 #if defined( NAME )
136 #ifndef KEYWORD1
137 #define KEYWORD1
138 #endif
139
140 #ifndef KEYWORD1_ALT
141 #define KEYWORD1_ALT HIDDEN
142 #endif
143
144 #ifndef KEYWORD2
145 #define KEYWORD2
146 #endif
147
148 #ifndef DISPATCH
149 #error DISPATCH must be defined
150 #endif
151
152 #ifndef RETURN_DISPATCH
153 #error RETURN_DISPATCH must be defined
154 #endif
155
156 """
157 return
158
159
160
161 def printInitDispatch(self, api):
162 print """
163 #endif /* defined( NAME ) */
164
165 /*
166 * This is how a dispatch table can be initialized with all the functions
167 * we generated above.
168 */
169 #ifdef DISPATCH_TABLE_NAME
170
171 #ifndef TABLE_ENTRY
172 #error TABLE_ENTRY must be defined
173 #endif
174
175 static _glapi_proc DISPATCH_TABLE_NAME[] = {"""
176 for f in api.functionIterateByOffset():
177 print ' TABLE_ENTRY(%s),' % (f.dispatch_name())
178
179 print ' /* A whole bunch of no-op functions. These might be called'
180 print ' * when someone tries to call a dynamically-registered'
181 print ' * extension function without a current rendering context.'
182 print ' */'
183 for i in range(1, 100):
184 print ' TABLE_ENTRY(Unused),'
185
186 print '};'
187 print '#endif /* DISPATCH_TABLE_NAME */'
188 print ''
189 return
190
191
192 def printAliasedTable(self, api):
193 print """
194 /*
195 * This is just used to silence compiler warnings.
196 * We list the functions which are not otherwise used.
197 */
198 #ifdef UNUSED_TABLE_NAME
199 static _glapi_proc UNUSED_TABLE_NAME[] = {"""
200
201 for f in api.functionIterateByOffset():
202 for n in f.entry_points:
203 if n != f.name:
204 if f.is_static_entry_point(n):
205 text = ' TABLE_ENTRY(%s),' % (n)
206
207 if f.has_different_protocol(n):
208 print '#ifndef GLX_INDIRECT_RENDERING'
209 print text
210 print '#endif'
211 else:
212 print text
213 print '};'
214 print '#endif /*UNUSED_TABLE_NAME*/'
215 print ''
216 return
217
218
219 def printBody(self, api):
220 for func in api.functionIterateByOffset():
221 got_stub = 0
222 for n in func.entry_points:
223 if func.is_static_entry_point(n):
224 self.printFunction(func, n)
225 elif not got_stub:
226 self.printFunction(func, n)
227 got_stub = 1
228
229 self.printInitDispatch(api)
230 self.printAliasedTable(api)
231 return
232
233
234 def show_usage():
235 print "Usage: %s [-f input_file_name]" % sys.argv[0]
236 sys.exit(1)
237
238 if __name__ == '__main__':
239 file_name = "gl_API.xml"
240
241 try:
242 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
243 except Exception,e:
244 show_usage()
245
246 for (arg,val) in args:
247 if arg == "-f":
248 file_name = val
249
250 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
251
252 printer = PrintGlOffsets()
253 printer.Print(api)