1 2# (C) Copyright IBM Corporation 2004 3# All Rights Reserved. 4# Copyright (c) 2014 Intel Corporation 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 <[email protected]> 27 28import argparse 29 30import gl_XML 31import license 32 33 34class PrintGlTable(gl_XML.gl_print_base): 35 def __init__(self): 36 gl_XML.gl_print_base.__init__(self) 37 38 self.header_tag = '_GLAPI_TABLE_H_' 39 self.name = "gl_table.py (from Mesa)" 40 self.license = license.bsd_license_template % ( \ 41"""Copyright (C) 1999-2003 Brian Paul All Rights Reserved. 42(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM") 43 return 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 */' % ( 49 f.return_type, f.name, arg_string, f.offset)) 50 51 def printRealHeader(self): 52 print('#include "util/glheader.h"') 53 print('') 54 print('#ifdef __cplusplus') 55 print('extern "C" {') 56 print('#endif') 57 print('') 58 print('#if defined(_WIN32) && defined(_WINDOWS_)') 59 print('#error "Should not include <windows.h> here"') 60 print('#endif') 61 print('') 62 print('struct _glapi_table') 63 print('{') 64 return 65 66 def printRealFooter(self): 67 print('};') 68 print('') 69 print('#ifdef __cplusplus') 70 print('}') 71 print('#endif') 72 return 73 74 75class PrintRemapTable(gl_XML.gl_print_base): 76 def __init__(self): 77 gl_XML.gl_print_base.__init__(self) 78 79 self.header_tag = '_DISPATCH_H_' 80 self.name = "gl_table.py (from Mesa)" 81 self.license = license.bsd_license_template % ( 82 "(C) Copyright IBM Corporation 2005", "IBM") 83 return 84 85 86 def printRealHeader(self): 87 print(""" 88/** 89 * \\file main/dispatch.h 90 * Macros for handling GL dispatch tables. 91 * 92 * For each known GL function, there are 3 macros in this file. The first 93 * macro is named CALL_FuncName and is used to call that GL function using 94 * the specified dispatch table. The other 2 macros, called GET_FuncName 95 * can SET_FuncName, are used to get and set the dispatch pointer for the 96 * named function in the specified dispatch table. 97 */ 98 99#include "util/glheader.h" 100""") 101 return 102 103 104 def printBody(self, api): 105 print('#define CALL_by_offset(disp, cast, offset, parameters) \\') 106 print(' (*(cast (GET_by_offset(disp, offset)))) parameters') 107 print('#define GET_by_offset(disp, offset) \\') 108 print(' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL') 109 print('#define SET_by_offset(disp, offset, fn) \\') 110 print(' do { \\') 111 print(' if ( (offset) < 0 ) { \\') 112 print(' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\') 113 print(' /* __func__, __LINE__, disp, offset, # fn); */ \\') 114 print(' /* abort(); */ \\') 115 print(' } \\') 116 print(' else { \\') 117 print(' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\') 118 print(' } \\') 119 print(' } while(0)') 120 print('') 121 122 functions = [] 123 abi_functions = [] 124 count = 0 125 for f in api.functionIterateByOffset(): 126 if not f.is_abi(): 127 functions.append([f, count]) 128 count += 1 129 else: 130 abi_functions.append([f, -1]) 131 132 print('/* total number of offsets below */') 133 print('#define _gloffset_COUNT %d' % (len(abi_functions + functions))) 134 print('') 135 136 for f, index in abi_functions: 137 print('#define _gloffset_%s %d' % (f.name, f.offset)) 138 139 remap_table = "driDispatchRemapTable" 140 141 print('#define %s_size %u' % (remap_table, count)) 142 print('extern int %s[ %s_size ];' % (remap_table, remap_table)) 143 print('') 144 145 for f, index in functions: 146 print('#define %s_remap_index %u' % (f.name, index)) 147 148 print('') 149 150 for f, index in functions: 151 print('#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name)) 152 153 print('') 154 155 for f, index in abi_functions + functions: 156 arg_string = gl_XML.create_parameter_string(f.parameters, 0) 157 158 print('typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string)) 159 print('#define CALL_{0}(disp, parameters) (* GET_{0}(disp)) parameters'.format(f.name)) 160 print('#define GET_{0}(disp) ((_glptr_{0})(GET_by_offset((disp), _gloffset_{0})))'.format(f.name)) 161 print("""#define SET_{0}(disp, func) do {{ \\ 162 {1} (GLAPIENTRYP fn)({2}) = func; \\ 163 SET_by_offset(disp, _gloffset_{0}, fn); \\ 164}} while (0) 165""".format(f.name, f.return_type, arg_string)) 166 167 return 168 169 170def _parser(): 171 """Parse arguments and return a namespace.""" 172 parser = argparse.ArgumentParser() 173 parser.add_argument('-f', '--filename', 174 default='gl_API.xml', 175 metavar="input_file_name", 176 dest='file_name', 177 help="Path to an XML description of OpenGL API.") 178 parser.add_argument('-m', '--mode', 179 choices=['table', 'remap_table'], 180 default='table', 181 metavar="mode", 182 help="Generate either a table or a remap_table") 183 return parser.parse_args() 184 185 186def main(): 187 """Main function.""" 188 args = _parser() 189 190 api = gl_XML.parse_GL_API(args.file_name) 191 192 if args.mode == "table": 193 printer = PrintGlTable() 194 elif args.mode == "remap_table": 195 printer = PrintRemapTable() 196 197 printer.Print(api) 198 199 200if __name__ == '__main__': 201 main() 202