gl_table.py revision 3464ebd5
1#!/usr/bin/python2
2
3# (C) Copyright IBM Corporation 2004
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
31
32class PrintGlTable(gl_XML.gl_print_base):
33	def __init__(self, es=False):
34		gl_XML.gl_print_base.__init__(self)
35
36		self.es = es
37		self.header_tag = '_GLAPI_TABLE_H_'
38		self.name = "gl_table.py (from Mesa)"
39		self.license = license.bsd_license_template % ( \
40"""Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
41(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
42		return
43
44
45	def printBody(self, api):
46		for f in api.functionIterateByOffset():
47			arg_string = f.get_parameter_string()
48			print '   %s (GLAPIENTRYP %s)(%s); /* %d */' % (f.return_type, f.name, arg_string, f.offset)
49
50
51	def printRealHeader(self):
52		print '#ifndef GLAPIENTRYP'
53		print '# ifndef GLAPIENTRY'
54		print '#  define GLAPIENTRY'
55		print '# endif'
56		print ''
57		print '# define GLAPIENTRYP GLAPIENTRY *'
58		print '#endif'
59		print ''
60		print ''
61		print 'struct _glapi_table'
62		print '{'
63		return
64
65
66	def printRealFooter(self):
67		print '};'
68		return
69
70
71class PrintRemapTable(gl_XML.gl_print_base):
72	def __init__(self, es=False):
73		gl_XML.gl_print_base.__init__(self)
74
75		self.es = es
76		self.header_tag = '_DISPATCH_H_'
77		self.name = "gl_table.py (from Mesa)"
78		self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
79		return
80
81
82	def printRealHeader(self):
83		print """
84/**
85 * \\file main/dispatch.h
86 * Macros for handling GL dispatch tables.
87 *
88 * For each known GL function, there are 3 macros in this file.  The first
89 * macro is named CALL_FuncName and is used to call that GL function using
90 * the specified dispatch table.  The other 2 macros, called GET_FuncName
91 * can SET_FuncName, are used to get and set the dispatch pointer for the
92 * named function in the specified dispatch table.
93 */
94
95#include "main/mfeatures.h"
96"""
97		return
98
99	def printBody(self, api):
100		print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
101		print '    (*(cast (GET_by_offset(disp, offset)))) parameters'
102		print '#define GET_by_offset(disp, offset) \\'
103		print '    (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
104		print '#define SET_by_offset(disp, offset, fn) \\'
105		print '    do { \\'
106		print '        if ( (offset) < 0 ) { \\'
107		print '            /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
108		print '            /*         __func__, __LINE__, disp, offset, # fn); */ \\'
109		print '            /* abort(); */ \\'
110		print '        } \\'
111		print '        else { \\'
112		print '            ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
113		print '        } \\'
114		print '    } while(0)'
115		print ''
116
117		functions = []
118		abi_functions = []
119		alias_functions = []
120		count = 0
121		for f in api.functionIterateByOffset():
122			if not f.is_abi():
123				functions.append( [f, count] )
124				count += 1
125			else:
126				abi_functions.append( [f, -1] )
127
128			if self.es:
129				# remember functions with aliases
130				if len(f.entry_points) > 1:
131					alias_functions.append(f)
132
133		print '/* total number of offsets below */'
134		print '#define _gloffset_COUNT %d' % (len(abi_functions + functions))
135		print ''
136
137		for f, index in abi_functions:
138			print '#define _gloffset_%s %d' % (f.name, f.offset)
139
140		print ''
141		print '#if !FEATURE_remap_table'
142		print ''
143
144		for f, index in functions:
145			print '#define _gloffset_%s %d' % (f.name, f.offset)
146
147		print ''
148		print '#else /* !FEATURE_remap_table */'
149		print ''
150
151		if self.es:
152			remap_table = "esLocalRemapTable"
153
154			print '#define %s_size %u' % (remap_table, count)
155			print 'static int %s[ %s_size ];' % (remap_table, remap_table)
156			print ''
157		else:
158			remap_table = "driDispatchRemapTable"
159
160			print '#define %s_size %u' % (remap_table, count)
161			print 'extern int %s[ %s_size ];' % (remap_table, remap_table)
162			print ''
163
164		for f, index in functions:
165			print '#define %s_remap_index %u' % (f.name, index)
166
167		print ''
168
169		for f, index in functions:
170			print '#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name)
171
172		print ''
173		print '#endif /* !FEATURE_remap_table */'
174		print ''
175
176		for f, index in abi_functions + functions:
177			arg_string = gl_XML.create_parameter_string( f.parameters, 0 )
178
179			print 'typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string)
180			print '#define CALL_%s(disp, parameters) \\' % (f.name)
181			print '    (* GET_%s(disp)) parameters' % (f.name)
182			print 'static INLINE _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name)
183			print '   return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name)
184			print '}'
185			print
186			print 'static INLINE void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string)
187			print '   SET_by_offset(disp, _gloffset_%s, fn);' % (f.name)
188			print '}'
189			print
190
191		if alias_functions:
192			print ''
193			print '/* define aliases for compatibility */'
194			for f in alias_functions:
195				for name in f.entry_points:
196					if name != f.name:
197						print '#define CALL_%s(disp, parameters) CALL_%s(disp, parameters)' % (name, f.name)
198						print '#define GET_%s(disp) GET_%s(disp)' % (name, f.name)
199						print '#define SET_%s(disp, fn) SET_%s(disp, fn)' % (name, f.name)
200			print ''
201
202			print '#if FEATURE_remap_table'
203			for f in alias_functions:
204				for name in f.entry_points:
205					if name != f.name:
206						print '#define %s_remap_index %s_remap_index' % (name, f.name)
207			print '#endif /* FEATURE_remap_table */'
208			print ''
209
210		return
211
212
213def show_usage():
214	print "Usage: %s [-f input_file_name] [-m mode] [-c]" % sys.argv[0]
215	print "    -m mode   Mode can be 'table' or 'remap_table'."
216	print "    -c        Enable compatibility with OpenGL ES."
217	sys.exit(1)
218
219if __name__ == '__main__':
220	file_name = "gl_API.xml"
221
222	try:
223		(args, trail) = getopt.getopt(sys.argv[1:], "f:m:c")
224	except Exception,e:
225		show_usage()
226
227	mode = "table"
228	es = False
229	for (arg,val) in args:
230		if arg == "-f":
231			file_name = val
232		elif arg == "-m":
233			mode = val
234		elif arg == "-c":
235			es = True
236
237	if mode == "table":
238		printer = PrintGlTable(es)
239	elif mode == "remap_table":
240		printer = PrintRemapTable(es)
241	else:
242		show_usage()
243
244	api = gl_XML.parse_GL_API( file_name )
245
246	printer.Print( api )
247