Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 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 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 28 | import gl_XML |
| 29 | import license |
| 30 | import sys, getopt |
| 31 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 32 | class PrintGlTable(gl_XML.gl_print_base): |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 33 | def __init__(self): |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 34 | gl_XML.gl_print_base.__init__(self) |
Ian Romanick | 497dd3e | 2005-05-26 16:34:58 +0000 | [diff] [blame] | 35 | |
Ian Romanick | 16c3c74 | 2005-01-28 19:00:54 +0000 | [diff] [blame] | 36 | self.header_tag = '_GLAPI_TABLE_H_' |
Ian Romanick | 497dd3e | 2005-05-26 16:34:58 +0000 | [diff] [blame] | 37 | self.name = "gl_table.py (from Mesa)" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 38 | 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 Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 41 | return |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 42 | |
| 43 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 44 | 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 Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 48 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 49 | |
| 50 | def printRealHeader(self): |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 51 | print '#ifndef GLAPIENTRYP' |
| 52 | print '#define GLAPIENTRYP' |
| 53 | print '#endif' |
| 54 | print '' |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 55 | print 'typedef void (*_glapi_proc)(void); /* generic function pointer */' |
| 56 | print '' |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 57 | print 'struct _glapi_table' |
| 58 | print '{' |
| 59 | return |
| 60 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 61 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 62 | def printRealFooter(self): |
| 63 | print '};' |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 64 | return |
| 65 | |
| 66 | |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 67 | class PrintRemapTable(gl_XML.gl_print_base): |
| 68 | def __init__(self): |
| 69 | gl_XML.gl_print_base.__init__(self) |
| 70 | |
| 71 | self.header_tag = '_DISPATCH_H_' |
| 72 | self.name = "gl_table.py (from Mesa)" |
| 73 | self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM") |
| 74 | return |
| 75 | |
| 76 | |
| 77 | def printRealHeader(self): |
| 78 | print """/** |
| 79 | * \\file dispatch.h |
| 80 | * Macros for handling GL dispatch tables. |
| 81 | * |
| 82 | * For each known GL function, there are 3 macros in this file. The first |
| 83 | * macro is named CALL_FuncName and is used to call that GL function using |
| 84 | * the specified dispatch table. The other 2 macros, called GET_FuncName |
| 85 | * can SET_FuncName, are used to get and set the dispatch pointer for the |
| 86 | * named function in the specified dispatch table. |
| 87 | */ |
| 88 | """ |
| 89 | |
| 90 | return |
| 91 | |
| 92 | def printBody(self, api): |
| 93 | print '#define CALL_by_offset(disp, cast, offset, parameters) \\' |
Ian Romanick | 126c89e | 2005-08-05 18:13:37 +0000 | [diff] [blame^] | 94 | print ' (*(cast (GET_by_offset(disp, offset)))) parameters' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 95 | print '#define GET_by_offset(disp, offset) \\' |
Ian Romanick | 126c89e | 2005-08-05 18:13:37 +0000 | [diff] [blame^] | 96 | print ' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 97 | print '#define SET_by_offset(disp, offset, fn) \\' |
Ian Romanick | 126c89e | 2005-08-05 18:13:37 +0000 | [diff] [blame^] | 98 | print ' do { \\' |
| 99 | print ' if ( (offset) < 0 ) { \\' |
| 100 | print ' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\' |
| 101 | print ' /* __func__, __LINE__, disp, offset, # fn); */ \\' |
| 102 | print ' /* abort(); */ \\' |
| 103 | print ' } \\' |
| 104 | print ' else { \\' |
| 105 | print ' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\' |
| 106 | print ' } \\' |
| 107 | print ' } while(0)' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 108 | print '' |
| 109 | |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 110 | abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ] |
| 111 | |
| 112 | functions = [] |
| 113 | abi_functions = [] |
| 114 | count = 0 |
| 115 | for f in api.functionIterateByOffset(): |
| 116 | [category, num] = api.get_category_for_name( f.name ) |
| 117 | if category not in abi: |
| 118 | functions.append( [f, count] ) |
| 119 | count += 1 |
| 120 | else: |
| 121 | abi_functions.append( f ) |
| 122 | |
| 123 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 124 | for f in abi_functions: |
| 125 | print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name) |
| 126 | print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name) |
| 127 | print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name) |
| 128 | |
| 129 | |
| 130 | print '' |
| 131 | print '#if !defined(IN_DRI_DRIVER)' |
| 132 | print '' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 133 | |
| 134 | for [f, index] in functions: |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 135 | print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name) |
| 136 | print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name) |
| 137 | print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name) |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 138 | |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 139 | print '' |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 140 | print '#else' |
| 141 | print '' |
| 142 | print '#define driDispatchRemapTable_size %u' % (count) |
Ian Romanick | 126c89e | 2005-08-05 18:13:37 +0000 | [diff] [blame^] | 143 | print 'extern int driDispatchRemapTable[ driDispatchRemapTable_size ];' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 144 | print '' |
| 145 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 146 | for [f, index] in functions: |
| 147 | print '#define %s_remap_index %u' % (f.name, index) |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 148 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 149 | print '' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 150 | |
| 151 | for [f, index] in functions: |
| 152 | arg_string = gl_XML.create_parameter_string( f.parameters, 0 ) |
| 153 | cast = '%s (GLAPIENTRYP)(%s)' % (f.return_type, arg_string) |
| 154 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 155 | print '#define CALL_%s(disp, parameters) CALL_by_offset(disp, (%s), driDispatchRemapTable[%s_remap_index], parameters)' % (f.name, cast, f.name) |
| 156 | print '#define GET_%s(disp) GET_by_offset(disp, driDispatchRemapTable[%s_remap_index])' % (f.name, f.name) |
| 157 | print '#define SET_%s(disp, fn) SET_by_offset(disp, driDispatchRemapTable[%s_remap_index], fn)' % (f.name, f.name) |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 158 | |
Ian Romanick | 1585c23 | 2005-07-28 00:29:51 +0000 | [diff] [blame] | 159 | |
| 160 | print '' |
| 161 | print '#endif /* !defined(IN_DRI_DRIVER) */' |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 162 | return |
| 163 | |
| 164 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 165 | def show_usage(): |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 166 | print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0] |
| 167 | print " -m mode Mode can be 'table' or 'remap_table'." |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 168 | sys.exit(1) |
| 169 | |
| 170 | if __name__ == '__main__': |
| 171 | file_name = "gl_API.xml" |
| 172 | |
| 173 | try: |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 174 | (args, trail) = getopt.getopt(sys.argv[1:], "f:m:") |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 175 | except Exception,e: |
| 176 | show_usage() |
| 177 | |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 178 | mode = "table" |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 179 | for (arg,val) in args: |
| 180 | if arg == "-f": |
| 181 | file_name = val |
Ian Romanick | 9bdfee3 | 2005-07-18 12:31:24 +0000 | [diff] [blame] | 182 | elif arg == "-m": |
| 183 | mode = val |
| 184 | |
| 185 | if mode == "table": |
| 186 | printer = PrintGlTable() |
| 187 | elif mode == "remap_table": |
| 188 | printer = PrintRemapTable() |
| 189 | else: |
| 190 | show_usage() |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 191 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 192 | api = gl_XML.parse_GL_API( file_name ) |
| 193 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 194 | printer.Print( api ) |