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