1bf215546Sopenharmony_ci
2bf215546Sopenharmony_ci# (C) Copyright IBM Corporation 2004
3bf215546Sopenharmony_ci# All Rights Reserved.
4bf215546Sopenharmony_ci# Copyright (c) 2014 Intel Corporation
5bf215546Sopenharmony_ci#
6bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci# on the rights to use, copy, modify, merge, publish, distribute, sub
10bf215546Sopenharmony_ci# license, and/or sell copies of the Software, and to permit persons to whom
11bf215546Sopenharmony_ci# the Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci#
13bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci# Software.
16bf215546Sopenharmony_ci#
17bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23bf215546Sopenharmony_ci# IN THE SOFTWARE.
24bf215546Sopenharmony_ci#
25bf215546Sopenharmony_ci# Authors:
26bf215546Sopenharmony_ci#    Ian Romanick <idr@us.ibm.com>
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ciimport argparse
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ciimport gl_XML
31bf215546Sopenharmony_ciimport license
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ciclass PrintGlTable(gl_XML.gl_print_base):
35bf215546Sopenharmony_ci    def __init__(self):
36bf215546Sopenharmony_ci        gl_XML.gl_print_base.__init__(self)
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci        self.header_tag = '_GLAPI_TABLE_H_'
39bf215546Sopenharmony_ci        self.name = "gl_table.py (from Mesa)"
40bf215546Sopenharmony_ci        self.license = license.bsd_license_template % ( \
41bf215546Sopenharmony_ci"""Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
42bf215546Sopenharmony_ci(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
43bf215546Sopenharmony_ci        return
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci    def printBody(self, api):
46bf215546Sopenharmony_ci        for f in api.functionIterateByOffset():
47bf215546Sopenharmony_ci            arg_string = f.get_parameter_string()
48bf215546Sopenharmony_ci            print('   %s (GLAPIENTRYP %s)(%s); /* %d */' % (
49bf215546Sopenharmony_ci                f.return_type, f.name, arg_string, f.offset))
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci    def printRealHeader(self):
52bf215546Sopenharmony_ci        print('#ifndef GLAPIENTRYP')
53bf215546Sopenharmony_ci        print('# ifndef GLAPIENTRY')
54bf215546Sopenharmony_ci        print('#  define GLAPIENTRY')
55bf215546Sopenharmony_ci        print('# endif')
56bf215546Sopenharmony_ci        print('')
57bf215546Sopenharmony_ci        print('# define GLAPIENTRYP GLAPIENTRY *')
58bf215546Sopenharmony_ci        print('#endif')
59bf215546Sopenharmony_ci        print('')
60bf215546Sopenharmony_ci        print('')
61bf215546Sopenharmony_ci        print('#ifdef __cplusplus')
62bf215546Sopenharmony_ci        print('extern "C" {')
63bf215546Sopenharmony_ci        print('#endif')
64bf215546Sopenharmony_ci        print('')
65bf215546Sopenharmony_ci        print('#ifdef MemoryBarrier')
66bf215546Sopenharmony_ci        print('#undef MemoryBarrier')
67bf215546Sopenharmony_ci        print('#endif')
68bf215546Sopenharmony_ci        print('')
69bf215546Sopenharmony_ci        print('struct _glapi_table')
70bf215546Sopenharmony_ci        print('{')
71bf215546Sopenharmony_ci        return
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci    def printRealFooter(self):
74bf215546Sopenharmony_ci        print('};')
75bf215546Sopenharmony_ci        print('')
76bf215546Sopenharmony_ci        print('#ifdef __cplusplus')
77bf215546Sopenharmony_ci        print('}')
78bf215546Sopenharmony_ci        print('#endif')
79bf215546Sopenharmony_ci        return
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ciclass PrintRemapTable(gl_XML.gl_print_base):
83bf215546Sopenharmony_ci    def __init__(self):
84bf215546Sopenharmony_ci        gl_XML.gl_print_base.__init__(self)
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        self.header_tag = '_DISPATCH_H_'
87bf215546Sopenharmony_ci        self.name = "gl_table.py (from Mesa)"
88bf215546Sopenharmony_ci        self.license = license.bsd_license_template % (
89bf215546Sopenharmony_ci            "(C) Copyright IBM Corporation 2005", "IBM")
90bf215546Sopenharmony_ci        return
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci    def printRealHeader(self):
94bf215546Sopenharmony_ci        print("""
95bf215546Sopenharmony_ci/**
96bf215546Sopenharmony_ci * \\file main/dispatch.h
97bf215546Sopenharmony_ci * Macros for handling GL dispatch tables.
98bf215546Sopenharmony_ci *
99bf215546Sopenharmony_ci * For each known GL function, there are 3 macros in this file.  The first
100bf215546Sopenharmony_ci * macro is named CALL_FuncName and is used to call that GL function using
101bf215546Sopenharmony_ci * the specified dispatch table.  The other 2 macros, called GET_FuncName
102bf215546Sopenharmony_ci * can SET_FuncName, are used to get and set the dispatch pointer for the
103bf215546Sopenharmony_ci * named function in the specified dispatch table.
104bf215546Sopenharmony_ci */
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci#include "main/glheader.h"
107bf215546Sopenharmony_ci""")
108bf215546Sopenharmony_ci        print('#include "main/glheader.h"')
109bf215546Sopenharmony_ci        return
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci    def printBody(self, api):
113bf215546Sopenharmony_ci        print('#define CALL_by_offset(disp, cast, offset, parameters) \\')
114bf215546Sopenharmony_ci        print('    (*(cast (GET_by_offset(disp, offset)))) parameters')
115bf215546Sopenharmony_ci        print('#define GET_by_offset(disp, offset) \\')
116bf215546Sopenharmony_ci        print('    (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL')
117bf215546Sopenharmony_ci        print('#define SET_by_offset(disp, offset, fn) \\')
118bf215546Sopenharmony_ci        print('    do { \\')
119bf215546Sopenharmony_ci        print('        if ( (offset) < 0 ) { \\')
120bf215546Sopenharmony_ci        print('            /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\')
121bf215546Sopenharmony_ci        print('            /*         __func__, __LINE__, disp, offset, # fn); */ \\')
122bf215546Sopenharmony_ci        print('            /* abort(); */ \\')
123bf215546Sopenharmony_ci        print('        } \\')
124bf215546Sopenharmony_ci        print('        else { \\')
125bf215546Sopenharmony_ci        print('            ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\')
126bf215546Sopenharmony_ci        print('        } \\')
127bf215546Sopenharmony_ci        print('    } while(0)')
128bf215546Sopenharmony_ci        print('')
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci        functions = []
131bf215546Sopenharmony_ci        abi_functions = []
132bf215546Sopenharmony_ci        count = 0
133bf215546Sopenharmony_ci        for f in api.functionIterateByOffset():
134bf215546Sopenharmony_ci            if not f.is_abi():
135bf215546Sopenharmony_ci                functions.append([f, count])
136bf215546Sopenharmony_ci                count += 1
137bf215546Sopenharmony_ci            else:
138bf215546Sopenharmony_ci                abi_functions.append([f, -1])
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci        print('/* total number of offsets below */')
141bf215546Sopenharmony_ci        print('#define _gloffset_COUNT %d' % (len(abi_functions + functions)))
142bf215546Sopenharmony_ci        print('')
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci        for f, index in abi_functions:
145bf215546Sopenharmony_ci            print('#define _gloffset_%s %d' % (f.name, f.offset))
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci        remap_table = "driDispatchRemapTable"
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci        print('#define %s_size %u' % (remap_table, count))
150bf215546Sopenharmony_ci        print('extern int %s[ %s_size ];' % (remap_table, remap_table))
151bf215546Sopenharmony_ci        print('')
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci        for f, index in functions:
154bf215546Sopenharmony_ci            print('#define %s_remap_index %u' % (f.name, index))
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci        print('')
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci        for f, index in functions:
159bf215546Sopenharmony_ci            print('#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name))
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci        print('')
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci        for f, index in abi_functions + functions:
164bf215546Sopenharmony_ci            arg_string = gl_XML.create_parameter_string(f.parameters, 0)
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci            print('typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string))
167bf215546Sopenharmony_ci            print('#define CALL_{0}(disp, parameters) (* GET_{0}(disp)) parameters'.format(f.name))
168bf215546Sopenharmony_ci            print('#define GET_{0}(disp) ((_glptr_{0})(GET_by_offset((disp), _gloffset_{0})))'.format(f.name))
169bf215546Sopenharmony_ci            print("""#define SET_{0}(disp, func) do {{ \\
170bf215546Sopenharmony_ci   {1} (GLAPIENTRYP fn)({2}) = func; \\
171bf215546Sopenharmony_ci   SET_by_offset(disp, _gloffset_{0}, fn); \\
172bf215546Sopenharmony_ci}} while (0)
173bf215546Sopenharmony_ci""".format(f.name, f.return_type, arg_string))
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci        return
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cidef _parser():
179bf215546Sopenharmony_ci    """Parse arguments and return a namespace."""
180bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
181bf215546Sopenharmony_ci    parser.add_argument('-f', '--filename',
182bf215546Sopenharmony_ci                        default='gl_API.xml',
183bf215546Sopenharmony_ci                        metavar="input_file_name",
184bf215546Sopenharmony_ci                        dest='file_name',
185bf215546Sopenharmony_ci                        help="Path to an XML description of OpenGL API.")
186bf215546Sopenharmony_ci    parser.add_argument('-m', '--mode',
187bf215546Sopenharmony_ci                        choices=['table', 'remap_table'],
188bf215546Sopenharmony_ci                        default='table',
189bf215546Sopenharmony_ci                        metavar="mode",
190bf215546Sopenharmony_ci                        help="Generate either a table or a remap_table")
191bf215546Sopenharmony_ci    return parser.parse_args()
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_cidef main():
195bf215546Sopenharmony_ci    """Main function."""
196bf215546Sopenharmony_ci    args = _parser()
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci    api = gl_XML.parse_GL_API(args.file_name)
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci    if args.mode == "table":
201bf215546Sopenharmony_ci        printer = PrintGlTable()
202bf215546Sopenharmony_ci    elif args.mode == "remap_table":
203bf215546Sopenharmony_ci        printer = PrintRemapTable()
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci    printer.Print(api)
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ciif __name__ == '__main__':
209bf215546Sopenharmony_ci    main()
210