15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/usr/bin/env python
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# (C) Copyright IBM Corporation 2005
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# All Rights Reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Permission is hereby granted, free of charge, to any person obtaining a
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# copy of this software and associated documentation files (the "Software"),
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# to deal in the Software without restriction, including without limitation
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# on the rights to use, copy, modify, merge, publish, distribute, sub
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# license, and/or sell copies of the Software, and to permit persons to whom
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# the Software is furnished to do so, subject to the following conditions:
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# The above copyright notice and this permission notice (including the next
145e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)# paragraph) shall be included in all copies or substantial portions of the
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)# Software.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# IN THE SOFTWARE.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Authors:
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#    Ian Romanick <idr@us.ibm.com>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import gl_XML
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import license
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import sys, getopt, string
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)vtxfmt = [
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "ArrayElement", \
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Color3f", \
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Color3fv", \
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Color4f", \
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Color4fv", \
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EdgeFlag", \
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EdgeFlagv", \
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalCoord1f", \
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalCoord1fv", \
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalCoord2f", \
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalCoord2fv", \
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalPoint1", \
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "EvalPoint2", \
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "FogCoordfEXT", \
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "FogCoordfvEXT", \
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Indexf", \
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Indexfv", \
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Materialfv", \
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord1fARB", \
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord1fvARB", \
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord2fARB", \
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord2fvARB", \
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord3fARB", \
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord3fvARB", \
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord4fARB", \
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "MultiTexCoord4fvARB", \
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Normal3f", \
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Normal3fv", \
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "SecondaryColor3fEXT", \
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "SecondaryColor3fvEXT", \
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord1f", \
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord1fv", \
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord2f", \
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord2fv", \
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord3f", \
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord3fv", \
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord4f", \
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "TexCoord4fv", \
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex2f", \
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex2fv", \
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex3f", \
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex3fv", \
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex4f", \
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Vertex4fv", \
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "CallList", \
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "CallLists", \
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "Begin", \
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "End", \
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib1fNV", \
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib1fvNV", \
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib2fNV", \
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib2fvNV", \
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib3fNV", \
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib3fvNV", \
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib4fNV", \
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib4fvNV", \
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib1fARB", \
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib1fvARB", \
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib2fARB", \
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib2fvARB", \
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib3fARB", \
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib3fvARB", \
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib4fARB", \
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "VertexAttrib4fvARB", \
97    "Rectf", \
98    "DrawArrays", \
99    "DrawElements", \
100    "DrawRangeElements", \
101    "EvalMesh1", \
102    "EvalMesh2", \
103]
104
105def all_entrypoints_in_abi(f, abi, api):
106	for n in f.entry_points:
107		[category, num] = api.get_category_for_name( n )
108		if category not in abi:
109			return 0
110
111	return 1
112
113
114def any_entrypoints_in_abi(f, abi, api):
115	for n in f.entry_points:
116		[category, num] = api.get_category_for_name( n )
117		if category in abi:
118			return 1
119
120	return 0
121
122
123def condition_for_function(f, abi, all_not_in_ABI):
124	"""Create a C-preprocessor condition for the function.
125
126	There are two modes of operation.  If all_not_in_ABI is set, a
127	condition is only created is all of the entry-point names for f are
128	not in the selected ABI.  If all_not_in_ABI is not set, a condition
129	is created if any entryp-point name is not in the selected ABI.
130	"""
131
132	condition = []
133	for n in f.entry_points:
134		[category, num] = api.get_category_for_name( n )
135		if category not in abi:
136			condition.append( 'defined(need_%s)' % (gl_XML.real_category_name( category )) )
137		elif all_not_in_ABI:
138			return []
139
140	return condition
141
142
143class PrintGlExtensionGlue(gl_XML.gl_print_base):
144	def __init__(self):
145		gl_XML.gl_print_base.__init__(self)
146
147		self.name = "extension_helper.py (from Mesa)"
148		self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
149		return
150
151
152	def printRealHeader(self):
153		print '#include "utils.h"'
154		print '#include "main/dispatch.h"'
155		print ''
156		return
157
158
159	def printBody(self, api):
160		abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
161
162		category_list = {}
163
164		print '#ifndef NULL'
165		print '# define NULL 0'
166		print '#endif'
167		print ''
168
169		for f in api.functionIterateAll():
170			condition = condition_for_function(f, abi, 0)
171			if len(condition):
172				print '#if %s' % (string.join(condition, " || "))
173				print 'static const char %s_names[] =' % (f.name)
174
175				parameter_signature = ''
176				for p in f.parameterIterator():
177					if p.is_padding:
178						continue
179
180					# FIXME: This is a *really* ugly hack. :(
181
182					tn = p.type_expr.get_base_type_node()
183					if p.is_pointer():
184						parameter_signature += 'p'
185					elif tn.integer:
186						parameter_signature += 'i'
187					elif tn.size == 4:
188						parameter_signature += 'f'
189					else:
190						parameter_signature += 'd'
191
192				print '    "%s\\0" /* Parameter signature */' % (parameter_signature)
193
194				for n in f.entry_points:
195					print '    "gl%s\\0"' % (n)
196
197					[category, num] = api.get_category_for_name( n )
198					if category not in abi:
199						c = gl_XML.real_category_name(category)
200						if not category_list.has_key(c):
201							category_list[ c ] = []
202
203						category_list[ c ].append( f )
204
205				print '    "";'
206				print '#endif'
207				print ''
208
209		keys = category_list.keys()
210		keys.sort()
211
212		for category in keys:
213			print '#if defined(need_%s)' % (category)
214			print 'static const struct dri_extension_function %s_functions[] = {' % (category)
215
216			for f in category_list[ category ]:
217				# A function either has an offset that is
218				# assigned by the ABI, or it has a remap
219				# index.
220				if any_entrypoints_in_abi(f, abi, api):
221					index_name = "-1"
222					offset = f.offset
223				else:
224					index_name = "%s_remap_index" % (f.name)
225					offset = -1
226
227				print '    { %s_names, %s, %d },' % (f.name, index_name, offset)
228
229
230			print '    { NULL, 0, 0 }'
231			print '};'
232			print '#endif'
233			print ''
234
235		return
236
237
238class PrintInitDispatch(gl_XML.gl_print_base):
239	def __init__(self):
240		gl_XML.gl_print_base.__init__(self)
241
242		self.name = "extension_helper.py (from Mesa)"
243		self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
244		return
245
246
247	def do_function_body(self, api, abi, vtxfmt_only):
248		last_condition_string = None
249		for f in api.functionIterateByOffset():
250			if (f.name in vtxfmt) and not vtxfmt_only:
251				continue
252
253			if (f.name not in vtxfmt) and vtxfmt_only:
254				continue
255
256			condition = condition_for_function(f, abi, 1)
257			condition_string = string.join(condition, " || ")
258
259			if condition_string != last_condition_string:
260				if last_condition_string:
261					print '#endif /* %s */' % (last_condition_string)
262
263				if condition_string:
264					print '#if %s' % (condition_string)
265
266			if vtxfmt_only:
267				print '   disp->%s = vfmt->%s;' % (f.name, f.name)
268			else:
269				print '   disp->%s = _mesa_%s;' % (f.name, f.name)
270
271			last_condition_string = condition_string
272
273		if last_condition_string:
274			print '#endif /* %s */' % (last_condition_string)
275
276
277
278	def printBody(self, api):
279		abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
280
281		print 'void driver_init_exec_table(struct _glapi_table *disp)'
282		print '{'
283		self.do_function_body(api, abi, 0)
284		print '}'
285		print ''
286		print 'void driver_install_vtxfmt(struct _glapi_table *disp, const GLvertexformat *vfmt)'
287		print '{'
288		self.do_function_body(api, abi, 1)
289		print '}'
290
291		return
292
293
294def show_usage():
295	print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
296	print "    -m output_mode   Output mode can be one of 'extensions' or 'exec_init'."
297	sys.exit(1)
298
299if __name__ == '__main__':
300	file_name = "gl_API.xml"
301
302	try:
303		(args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
304	except Exception,e:
305		show_usage()
306
307	mode = "extensions"
308	for (arg,val) in args:
309		if arg == "-f":
310			file_name = val
311		if arg == '-m':
312			mode = val
313
314
315	api = gl_XML.parse_GL_API( file_name )
316
317	if mode == "extensions":
318		printer = PrintGlExtensionGlue()
319	elif mode == "exec_init":
320		printer = PrintInitDispatch()
321	else:
322		show_usage()
323
324	printer.Print( api )
325