glapy: gl_genexec.py: use a main function
[mesa.git] / src / mapi / glapi / gen / gl_genexec.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2012 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23
24 # This script generates the file api_exec.c, which contains
25 # _mesa_initialize_exec_table(). It is responsible for populating all
26 # entries in the "exec" dispatch table that aren't dynamic.
27
28 import argparse
29 import collections
30 import license
31 import gl_XML
32 import sys
33
34
35 exec_flavor_map = {
36 'dynamic': None,
37 'mesa': '_mesa_',
38 'skip': None,
39 }
40
41
42 header = """/**
43 * \\file api_exec.c
44 * Initialize dispatch table.
45 */
46
47
48 #include "main/accum.h"
49 #include "main/api_loopback.h"
50 #include "main/api_exec.h"
51 #include "main/arbprogram.h"
52 #include "main/atifragshader.h"
53 #include "main/attrib.h"
54 #include "main/blend.h"
55 #include "main/blit.h"
56 #include "main/bufferobj.h"
57 #include "main/arrayobj.h"
58 #include "main/buffers.h"
59 #include "main/clear.h"
60 #include "main/clip.h"
61 #include "main/colortab.h"
62 #include "main/compute.h"
63 #include "main/condrender.h"
64 #include "main/context.h"
65 #include "main/convolve.h"
66 #include "main/copyimage.h"
67 #include "main/depth.h"
68 #include "main/dlist.h"
69 #include "main/drawpix.h"
70 #include "main/drawtex.h"
71 #include "main/rastpos.h"
72 #include "main/enable.h"
73 #include "main/errors.h"
74 #include "main/es1_conversion.h"
75 #include "main/eval.h"
76 #include "main/get.h"
77 #include "main/feedback.h"
78 #include "main/fog.h"
79 #include "main/fbobject.h"
80 #include "main/framebuffer.h"
81 #include "main/genmipmap.h"
82 #include "main/hint.h"
83 #include "main/histogram.h"
84 #include "main/imports.h"
85 #include "main/light.h"
86 #include "main/lines.h"
87 #include "main/matrix.h"
88 #include "main/multisample.h"
89 #include "main/objectlabel.h"
90 #include "main/performance_monitor.h"
91 #include "main/pipelineobj.h"
92 #include "main/pixel.h"
93 #include "main/pixelstore.h"
94 #include "main/points.h"
95 #include "main/polygon.h"
96 #include "main/program_resource.h"
97 #include "main/querymatrix.h"
98 #include "main/queryobj.h"
99 #include "main/readpix.h"
100 #include "main/samplerobj.h"
101 #include "main/scissor.h"
102 #include "main/stencil.h"
103 #include "main/texenv.h"
104 #include "main/texgetimage.h"
105 #include "main/teximage.h"
106 #include "main/texgen.h"
107 #include "main/texobj.h"
108 #include "main/texparam.h"
109 #include "main/texstate.h"
110 #include "main/texstorage.h"
111 #include "main/texturebarrier.h"
112 #include "main/textureview.h"
113 #include "main/transformfeedback.h"
114 #include "main/mtypes.h"
115 #include "main/varray.h"
116 #include "main/viewport.h"
117 #include "main/shaderapi.h"
118 #include "main/shaderimage.h"
119 #include "main/uniforms.h"
120 #include "main/syncobj.h"
121 #include "main/formatquery.h"
122 #include "main/dispatch.h"
123 #include "main/vdpau.h"
124 #include "vbo/vbo.h"
125
126
127 /**
128 * Initialize a context's exec table with pointers to Mesa's supported
129 * GL functions.
130 *
131 * This function depends on ctx->Version.
132 *
133 * \param ctx GL context to which \c exec belongs.
134 */
135 void
136 _mesa_initialize_exec_table(struct gl_context *ctx)
137 {
138 struct _glapi_table *exec;
139
140 exec = ctx->Exec;
141 assert(exec != NULL);
142
143 assert(ctx->Version > 0);
144
145 vbo_initialize_exec_dispatch(ctx, exec);
146 """
147
148
149 footer = """
150 }
151 """
152
153
154 class PrintCode(gl_XML.gl_print_base):
155
156 def __init__(self):
157 gl_XML.gl_print_base.__init__(self)
158
159 self.name = 'gl_genexec.py'
160 self.license = license.bsd_license_template % (
161 'Copyright (C) 2012 Intel Corporation',
162 'Intel Corporation')
163
164 def printRealHeader(self):
165 print header
166
167 def printRealFooter(self):
168 print footer
169
170 def printBody(self, api):
171 # Collect SET_* calls by the condition under which they should
172 # be called.
173 settings_by_condition = collections.defaultdict(lambda: [])
174 for f in api.functionIterateAll():
175 if f.exec_flavor not in exec_flavor_map:
176 raise Exception(
177 'Unrecognized exec flavor {0!r}'.format(f.exec_flavor))
178 condition_parts = []
179 if f.desktop:
180 if f.deprecated:
181 condition_parts.append('ctx->API == API_OPENGL_COMPAT')
182 else:
183 condition_parts.append('_mesa_is_desktop_gl(ctx)')
184 if 'es1' in f.api_map:
185 condition_parts.append('ctx->API == API_OPENGLES')
186 if 'es2' in f.api_map:
187 if f.api_map['es2'] > 2.0:
188 condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(int(f.api_map['es2'] * 10)))
189 else:
190 condition_parts.append('ctx->API == API_OPENGLES2')
191 if not condition_parts:
192 # This function does not exist in any API.
193 continue
194 condition = ' || '.join(condition_parts)
195 prefix = exec_flavor_map[f.exec_flavor]
196 if prefix is None:
197 # This function is not implemented, or is dispatched
198 # dynamically.
199 continue
200 settings_by_condition[condition].append(
201 'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
202 # Print out an if statement for each unique condition, with
203 # the SET_* calls nested inside it.
204 for condition in sorted(settings_by_condition.keys()):
205 print ' if ({0}) {{'.format(condition)
206 for setting in sorted(settings_by_condition[condition]):
207 print ' {0}'.format(setting)
208 print ' }'
209
210
211 def _parser():
212 """Parse arguments and return namespace."""
213 parser = argparse.ArgumentParser()
214 parser.add_argument('-f',
215 dest='filename',
216 default='gl_and_es_API.xml',
217 help='an xml file describing an API')
218 return parser.parse_args()
219
220
221 def main():
222 """Main function."""
223 args = _parser()
224 printer = PrintCode()
225 api = gl_XML.parse_GL_API(args.filename)
226 printer.Print(api)
227
228
229 if __name__ == '__main__':
230 main()