blob: 69f7bd7c7b5c3a7b7ebfd3436d0747cd81a302e0 [file] [log] [blame]
Ian Romanick73f59b02004-05-18 18:33:40 +00001#!/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
Ian Romanick73f59b02004-05-18 18:33:40 +000028import gl_XML
29import license
30import sys, getopt
31
Ian Romanick66a55482005-06-21 23:42:43 +000032class PrintGlTable(gl_XML.gl_print_base):
Ian Romanick73f59b02004-05-18 18:33:40 +000033 def __init__(self):
Ian Romanick66a55482005-06-21 23:42:43 +000034 gl_XML.gl_print_base.__init__(self)
Ian Romanick497dd3e2005-05-26 16:34:58 +000035
Ian Romanick16c3c742005-01-28 19:00:54 +000036 self.header_tag = '_GLAPI_TABLE_H_'
Ian Romanick497dd3e2005-05-26 16:34:58 +000037 self.name = "gl_table.py (from Mesa)"
Ian Romanick73f59b02004-05-18 18:33:40 +000038 self.license = license.bsd_license_template % ( \
39"""Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
40(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
Ian Romanick66a55482005-06-21 23:42:43 +000041 return
Ian Romanick73f59b02004-05-18 18:33:40 +000042
43
Ian Romanick66a55482005-06-21 23:42:43 +000044 def printBody(self, api):
45 for f in api.functionIterateByOffset():
46 arg_string = f.get_parameter_string()
47 print ' %s (GLAPIENTRYP %s)(%s); /* %d */' % (f.return_type, f.name, arg_string, f.offset)
Ian Romanick73f59b02004-05-18 18:33:40 +000048
Ian Romanick73f59b02004-05-18 18:33:40 +000049
50 def printRealHeader(self):
Ian Romanick73f59b02004-05-18 18:33:40 +000051 print '#ifndef GLAPIENTRYP'
Ian Romanick3f8c5962007-02-23 15:15:50 -080052 print '# ifndef GLAPIENTRY'
53 print '# define GLAPIENTRY'
54 print '# endif'
55 print ''
56 print '# define GLAPIENTRYP GLAPIENTRY *'
Ian Romanick73f59b02004-05-18 18:33:40 +000057 print '#endif'
58 print ''
Ian Romanick1585c232005-07-28 00:29:51 +000059 print 'typedef void (*_glapi_proc)(void); /* generic function pointer */'
60 print ''
Ian Romanick73f59b02004-05-18 18:33:40 +000061 print 'struct _glapi_table'
62 print '{'
63 return
64
Ian Romanick66a55482005-06-21 23:42:43 +000065
Ian Romanick73f59b02004-05-18 18:33:40 +000066 def printRealFooter(self):
67 print '};'
Ian Romanick73f59b02004-05-18 18:33:40 +000068 return
69
70
Ian Romanick9bdfee32005-07-18 12:31:24 +000071class PrintRemapTable(gl_XML.gl_print_base):
72 def __init__(self):
73 gl_XML.gl_print_base.__init__(self)
74
75 self.header_tag = '_DISPATCH_H_'
76 self.name = "gl_table.py (from Mesa)"
77 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
78 return
79
80
81 def printRealHeader(self):
82 print """/**
83 * \\file dispatch.h
84 * Macros for handling GL dispatch tables.
85 *
86 * For each known GL function, there are 3 macros in this file. The first
87 * macro is named CALL_FuncName and is used to call that GL function using
88 * the specified dispatch table. The other 2 macros, called GET_FuncName
89 * can SET_FuncName, are used to get and set the dispatch pointer for the
90 * named function in the specified dispatch table.
91 */
92"""
93
94 return
95
96 def printBody(self, api):
97 print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
Ian Romanick126c89e2005-08-05 18:13:37 +000098 print ' (*(cast (GET_by_offset(disp, offset)))) parameters'
Ian Romanick9bdfee32005-07-18 12:31:24 +000099 print '#define GET_by_offset(disp, offset) \\'
Ian Romanick126c89e2005-08-05 18:13:37 +0000100 print ' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
Ian Romanick9bdfee32005-07-18 12:31:24 +0000101 print '#define SET_by_offset(disp, offset, fn) \\'
Ian Romanick126c89e2005-08-05 18:13:37 +0000102 print ' do { \\'
103 print ' if ( (offset) < 0 ) { \\'
104 print ' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
105 print ' /* __func__, __LINE__, disp, offset, # fn); */ \\'
106 print ' /* abort(); */ \\'
107 print ' } \\'
108 print ' else { \\'
109 print ' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
110 print ' } \\'
111 print ' } while(0)'
Ian Romanick9bdfee32005-07-18 12:31:24 +0000112 print ''
113
Ian Romanick9bdfee32005-07-18 12:31:24 +0000114 abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
115
116 functions = []
117 abi_functions = []
118 count = 0
119 for f in api.functionIterateByOffset():
120 [category, num] = api.get_category_for_name( f.name )
121 if category not in abi:
122 functions.append( [f, count] )
123 count += 1
124 else:
125 abi_functions.append( f )
126
127
Ian Romanick1585c232005-07-28 00:29:51 +0000128 for f in abi_functions:
129 print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name)
130 print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name)
131 print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name)
132
133
134 print ''
135 print '#if !defined(IN_DRI_DRIVER)'
136 print ''
Ian Romanick9bdfee32005-07-18 12:31:24 +0000137
138 for [f, index] in functions:
Ian Romanick1585c232005-07-28 00:29:51 +0000139 print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name)
140 print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name)
141 print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name)
Ian Romanick9bdfee32005-07-18 12:31:24 +0000142
Ian Romanick9bdfee32005-07-18 12:31:24 +0000143 print ''
Ian Romanick1585c232005-07-28 00:29:51 +0000144 print '#else'
145 print ''
146 print '#define driDispatchRemapTable_size %u' % (count)
Ian Romanick126c89e2005-08-05 18:13:37 +0000147 print 'extern int driDispatchRemapTable[ driDispatchRemapTable_size ];'
Ian Romanick9bdfee32005-07-18 12:31:24 +0000148 print ''
149
Ian Romanick1585c232005-07-28 00:29:51 +0000150 for [f, index] in functions:
151 print '#define %s_remap_index %u' % (f.name, index)
Ian Romanick9bdfee32005-07-18 12:31:24 +0000152
Ian Romanick1585c232005-07-28 00:29:51 +0000153 print ''
Ian Romanick9bdfee32005-07-18 12:31:24 +0000154
155 for [f, index] in functions:
156 arg_string = gl_XML.create_parameter_string( f.parameters, 0 )
157 cast = '%s (GLAPIENTRYP)(%s)' % (f.return_type, arg_string)
158
Ian Romanick1585c232005-07-28 00:29:51 +0000159 print '#define CALL_%s(disp, parameters) CALL_by_offset(disp, (%s), driDispatchRemapTable[%s_remap_index], parameters)' % (f.name, cast, f.name)
160 print '#define GET_%s(disp) GET_by_offset(disp, driDispatchRemapTable[%s_remap_index])' % (f.name, f.name)
161 print '#define SET_%s(disp, fn) SET_by_offset(disp, driDispatchRemapTable[%s_remap_index], fn)' % (f.name, f.name)
Ian Romanick9bdfee32005-07-18 12:31:24 +0000162
Ian Romanick1585c232005-07-28 00:29:51 +0000163
164 print ''
165 print '#endif /* !defined(IN_DRI_DRIVER) */'
Ian Romanick9bdfee32005-07-18 12:31:24 +0000166 return
167
168
Ian Romanick73f59b02004-05-18 18:33:40 +0000169def show_usage():
Ian Romanick9bdfee32005-07-18 12:31:24 +0000170 print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0]
171 print " -m mode Mode can be 'table' or 'remap_table'."
Ian Romanick73f59b02004-05-18 18:33:40 +0000172 sys.exit(1)
173
174if __name__ == '__main__':
175 file_name = "gl_API.xml"
176
177 try:
Ian Romanick9bdfee32005-07-18 12:31:24 +0000178 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
Ian Romanick73f59b02004-05-18 18:33:40 +0000179 except Exception,e:
180 show_usage()
181
Ian Romanick9bdfee32005-07-18 12:31:24 +0000182 mode = "table"
Ian Romanick73f59b02004-05-18 18:33:40 +0000183 for (arg,val) in args:
184 if arg == "-f":
185 file_name = val
Ian Romanick9bdfee32005-07-18 12:31:24 +0000186 elif arg == "-m":
187 mode = val
188
189 if mode == "table":
190 printer = PrintGlTable()
191 elif mode == "remap_table":
192 printer = PrintRemapTable()
193 else:
194 show_usage()
Ian Romanick73f59b02004-05-18 18:33:40 +0000195
Ian Romanick66a55482005-06-21 23:42:43 +0000196 api = gl_XML.parse_GL_API( file_name )
197
Ian Romanick66a55482005-06-21 23:42:43 +0000198 printer.Print( api )