blob: 21f5b56fbbd9a56e0d996af775b9923281c88d89 [file] [log] [blame]
Ian Romanickc212abf2005-06-30 16:00:48 +00001#!/usr/bin/env python
2
3# (C) Copyright IBM Corporation 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
28import gl_XML
29import license
30import sys, getopt, string
31
32vtxfmt = [
33 "ArrayElement", \
34 "Color3f", \
35 "Color3fv", \
36 "Color4f", \
37 "Color4fv", \
38 "EdgeFlag", \
39 "EdgeFlagv", \
40 "EvalCoord1f", \
41 "EvalCoord1fv", \
42 "EvalCoord2f", \
43 "EvalCoord2fv", \
44 "EvalPoint1", \
45 "EvalPoint2", \
46 "FogCoordfEXT", \
47 "FogCoordfvEXT", \
48 "Indexf", \
49 "Indexfv", \
50 "Materialfv", \
51 "MultiTexCoord1fARB", \
52 "MultiTexCoord1fvARB", \
53 "MultiTexCoord2fARB", \
54 "MultiTexCoord2fvARB", \
55 "MultiTexCoord3fARB", \
56 "MultiTexCoord3fvARB", \
57 "MultiTexCoord4fARB", \
58 "MultiTexCoord4fvARB", \
59 "Normal3f", \
60 "Normal3fv", \
61 "SecondaryColor3fEXT", \
62 "SecondaryColor3fvEXT", \
63 "TexCoord1f", \
64 "TexCoord1fv", \
65 "TexCoord2f", \
66 "TexCoord2fv", \
67 "TexCoord3f", \
68 "TexCoord3fv", \
69 "TexCoord4f", \
70 "TexCoord4fv", \
71 "Vertex2f", \
72 "Vertex2fv", \
73 "Vertex3f", \
74 "Vertex3fv", \
75 "Vertex4f", \
76 "Vertex4fv", \
77 "CallList", \
78 "CallLists", \
79 "Begin", \
80 "End", \
81 "VertexAttrib1fNV", \
82 "VertexAttrib1fvNV", \
83 "VertexAttrib2fNV", \
84 "VertexAttrib2fvNV", \
85 "VertexAttrib3fNV", \
86 "VertexAttrib3fvNV", \
87 "VertexAttrib4fNV", \
88 "VertexAttrib4fvNV", \
89 "VertexAttrib1fARB", \
90 "VertexAttrib1fvARB", \
91 "VertexAttrib2fARB", \
92 "VertexAttrib2fvARB", \
93 "VertexAttrib3fARB", \
94 "VertexAttrib3fvARB", \
95 "VertexAttrib4fARB", \
96 "VertexAttrib4fvARB", \
97 "Rectf", \
98 "DrawArrays", \
99 "DrawElements", \
100 "DrawRangeElements", \
101 "EvalMesh1", \
102 "EvalMesh2", \
103]
104
105def condition_for_function(f, abi, all_not_in_ABI):
106 """Create a C-preprocessor condition for the function.
107
108 There are two modes of operation. If all_not_in_ABI is set, a
109 condition is only created is all of the entry-point names for f are
110 not in the selected ABI. If all_not_in_ABI is not set, a condition
111 is created if any entryp-point name is not in the selected ABI.
112 """
113
114 condition = []
115 for n in f.entry_points:
116 [category, num] = api.get_category_for_name( n )
117 if category not in abi:
118 condition.append( 'defined(need_%s)' % (gl_XML.real_category_name( category )) )
119 elif all_not_in_ABI:
120 return []
121
122 return condition
123
124
125class PrintGlExtensionGlue(gl_XML.gl_print_base):
126 def __init__(self):
127 gl_XML.gl_print_base.__init__(self)
128
129 self.name = "extension_helper.py (from Mesa)"
130 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
131 return
132
133
134 def printRealHeader(self):
135 print '#include "utils.h"'
136 print ''
137 return
138
139
140 def printBody(self, api):
141 abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
142
143 category_list = {}
144
145 print '#ifndef NULL'
146 print '# define NULL 0'
147 print '#endif'
148 print ''
149
150 for f in api.functionIterateAll():
151 condition = condition_for_function(f, abi, 0)
152 if len(condition):
153 print '#if %s' % (string.join(condition, " || "))
154 print 'static const char %s_names[] = ' % (f.name)
155
156 parameter_signature = ''
157 for p in f.parameterIterator():
158 # FIXME: This is a *really* ugly hack. :(
159
160 tn = p.type_expr.get_base_type_node()
161 if p.is_pointer():
162 parameter_signature += 'p'
163 elif tn.integer:
164 parameter_signature += 'i'
165 elif tn.size == 4:
166 parameter_signature += 'f'
167 else:
168 parameter_signature += 'd'
169
170 print ' "%s\\0" /* Parameter signature */' % (parameter_signature)
171
172 for n in f.entry_points:
173 print ' "gl%s\\0"' % (n)
174
175 [category, num] = api.get_category_for_name( n )
176 if category not in abi:
177 c = gl_XML.real_category_name(category)
178 if not category_list.has_key(c):
179 category_list[ c ] = []
180
181 category_list[ c ].append( [f.name, f.offset] )
182
183 print ' "";'
184 print '#endif'
185 print ''
186
187 keys = category_list.keys()
188 keys.sort()
189
190 for category in keys:
191 print '#if defined(need_%s)' % (category)
192 print 'static const struct dri_extension_function %s_functions[] = {' % (category)
193 for [function, offset] in category_list[ category ]:
194 print ' { %s_names, %d },' % (function, offset)
195 print ' { NULL, 0 }'
196 print '};'
197 print '#endif'
198 print ''
199
200 return
201
202
203class PrintInitDispatch(gl_XML.gl_print_base):
204 def __init__(self):
205 gl_XML.gl_print_base.__init__(self)
206
207 self.name = "extension_helper.py (from Mesa)"
208 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
209 return
210
211
212 def do_function_body(self, api, abi, vtxfmt_only):
213 last_condition_string = None
214 for f in api.functionIterateByOffset():
215 if (f.name in vtxfmt) and not vtxfmt_only:
216 continue
217
218 if (f.name not in vtxfmt) and vtxfmt_only:
219 continue
220
221 condition = condition_for_function(f, abi, 1)
222 condition_string = string.join(condition, " || ")
223
224 if condition_string != last_condition_string:
225 if last_condition_string:
226 print '#endif /* %s */' % (last_condition_string)
227
228 if condition_string:
229 print '#if %s' % (condition_string)
230
231 if vtxfmt_only:
232 print ' disp->%s = vfmt->%s;' % (f.name, f.name)
233 else:
234 print ' disp->%s = _mesa_%s;' % (f.name, f.name)
235
236 last_condition_string = condition_string
237
238 if last_condition_string:
239 print '#endif /* %s */' % (last_condition_string)
240
241
242
243 def printBody(self, api):
244 abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
245
246 print 'void driver_init_exec_table(struct _glapi_table *disp)'
247 print '{'
248 self.do_function_body(api, abi, 0)
249 print '}'
250 print ''
251 print 'void driver_install_vtxfmt(struct _glapi_table *disp, const GLvertexformat *vfmt)'
252 print '{'
253 self.do_function_body(api, abi, 1)
254 print '}'
255
256 return
257
258
259def show_usage():
260 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
261 print " -m output_mode Output mode can be one of 'extensions' or 'exec_init'."
262 sys.exit(1)
263
264if __name__ == '__main__':
265 file_name = "gl_API.xml"
266
267 try:
268 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
269 except Exception,e:
270 show_usage()
271
272 mode = "extensions"
273 for (arg,val) in args:
274 if arg == "-f":
275 file_name = val
276 if arg == '-m':
277 mode = val
278
279
280 api = gl_XML.parse_GL_API( file_name )
281
282 if mode == "extensions":
283 printer = PrintGlExtensionGlue()
284 elif mode == "exec_init":
285 printer = PrintInitDispatch()
286 else:
287 show_usage()
288
289 printer.Print( api )