Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 import apiexec
34
35
36 exec_flavor_map = {
37 'dynamic': None,
38 'mesa': '_mesa_',
39 'skip': None,
40 }
41
42
43 header = """/**
44 * \\file api_exec.c
45 * Initialize dispatch table.
46 */
47
48
49 #include "main/accum.h"
50 #include "main/api_loopback.h"
51 #include "main/api_exec.h"
52 #include "main/arbprogram.h"
53 #include "main/atifragshader.h"
54 #include "main/attrib.h"
55 #include "main/blend.h"
56 #include "main/blit.h"
57 #include "main/bufferobj.h"
58 #include "main/arrayobj.h"
59 #include "main/buffers.h"
60 #include "main/clear.h"
61 #include "main/clip.h"
62 #include "main/colortab.h"
63 #include "main/compute.h"
64 #include "main/condrender.h"
65 #include "main/context.h"
66 #include "main/convolve.h"
67 #include "main/copyimage.h"
68 #include "main/depth.h"
69 #include "main/dlist.h"
70 #include "main/drawpix.h"
71 #include "main/drawtex.h"
72 #include "main/rastpos.h"
73 #include "main/enable.h"
74 #include "main/errors.h"
75 #include "main/es1_conversion.h"
76 #include "main/eval.h"
77 #include "main/get.h"
78 #include "main/feedback.h"
79 #include "main/fog.h"
80 #include "main/fbobject.h"
81 #include "main/framebuffer.h"
82 #include "main/genmipmap.h"
83 #include "main/hint.h"
84 #include "main/histogram.h"
85 #include "main/imports.h"
86 #include "main/light.h"
87 #include "main/lines.h"
88 #include "main/matrix.h"
89 #include "main/multisample.h"
90 #include "main/objectlabel.h"
91 #include "main/performance_monitor.h"
92 #include "main/pipelineobj.h"
93 #include "main/pixel.h"
94 #include "main/pixelstore.h"
95 #include "main/points.h"
96 #include "main/polygon.h"
97 #include "main/program_resource.h"
98 #include "main/querymatrix.h"
99 #include "main/queryobj.h"
100 #include "main/readpix.h"
101 #include "main/samplerobj.h"
102 #include "main/scissor.h"
103 #include "main/stencil.h"
104 #include "main/texenv.h"
105 #include "main/texgetimage.h"
106 #include "main/teximage.h"
107 #include "main/texgen.h"
108 #include "main/texobj.h"
109 #include "main/texparam.h"
110 #include "main/texstate.h"
111 #include "main/texstorage.h"
112 #include "main/texturebarrier.h"
113 #include "main/textureview.h"
114 #include "main/transformfeedback.h"
115 #include "main/mtypes.h"
116 #include "main/varray.h"
117 #include "main/viewport.h"
118 #include "main/shaderapi.h"
119 #include "main/shaderimage.h"
120 #include "main/uniforms.h"
121 #include "main/syncobj.h"
122 #include "main/formatquery.h"
123 #include "main/dispatch.h"
124 #include "main/vdpau.h"
125 #include "vbo/vbo.h"
126
127
128 /**
129 * Initialize a context's exec table with pointers to Mesa's supported
130 * GL functions.
131 *
132 * This function depends on ctx->Version.
133 *
134 * \param ctx GL context to which \c exec belongs.
135 */
136 void
137 _mesa_initialize_exec_table(struct gl_context *ctx)
138 {
139 struct _glapi_table *exec;
140
141 exec = ctx->Exec;
142 assert(exec != NULL);
143
144 assert(ctx->Version > 0);
145
146 vbo_initialize_exec_dispatch(ctx, exec);
147 """
148
149
150 footer = """
151 }
152 """
153
154
155 class PrintCode(gl_XML.gl_print_base):
156
157 def __init__(self):
158 gl_XML.gl_print_base.__init__(self)
159
160 self.name = 'gl_genexec.py'
161 self.license = license.bsd_license_template % (
162 'Copyright (C) 2012 Intel Corporation',
163 'Intel Corporation')
164
165 def printRealHeader(self):
166 print header
167
168 def printRealFooter(self):
169 print footer
170
171 def printBody(self, api):
172 # Collect SET_* calls by the condition under which they should
173 # be called.
174 settings_by_condition = collections.defaultdict(lambda: [])
175 for f in api.functionIterateAll():
176 if f.exec_flavor not in exec_flavor_map:
177 raise Exception(
178 'Unrecognized exec flavor {0!r}'.format(f.exec_flavor))
179 condition_parts = []
180 if f.name in apiexec.functions:
181 ex = apiexec.functions[f.name]
182 unconditional_count = 0
183
184 if ex.compatibility is not None:
185 condition_parts.append('ctx->API == API_OPENGL_COMPAT')
186 unconditional_count += 1
187
188 if ex.core is not None:
189 condition_parts.append('ctx->API == API_OPENGL_CORE')
190 unconditional_count += 1
191
192 if ex.es1 is not None:
193 condition_parts.append('ctx->API == API_OPENGLES')
194 unconditional_count += 1
195
196 if ex.es2 is not None:
197 if ex.es2 > 20:
198 condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(ex.es2))
199 else:
200 condition_parts.append('ctx->API == API_OPENGLES2')
201 unconditional_count += 1
202
203 # If the function is unconditionally available in all four
204 # APIs, then it is always available. Replace the complex
205 # tautology condition with "true" and let GCC do the right
206 # thing.
207 if unconditional_count == 4:
208 condition_parts = ['true']
209 else:
210 if f.desktop:
211 if f.deprecated:
212 condition_parts.append('ctx->API == API_OPENGL_COMPAT')
213 else:
214 condition_parts.append('_mesa_is_desktop_gl(ctx)')
215 if 'es1' in f.api_map:
216 condition_parts.append('ctx->API == API_OPENGLES')
217 if 'es2' in f.api_map:
218 if f.api_map['es2'] > 2.0:
219 condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(int(f.api_map['es2'] * 10)))
220 else:
221 condition_parts.append('ctx->API == API_OPENGLES2')
222
223 if not condition_parts:
224 # This function does not exist in any API.
225 continue
226 condition = ' || '.join(condition_parts)
227 prefix = exec_flavor_map[f.exec_flavor]
228 if prefix is None:
229 # This function is not implemented, or is dispatched
230 # dynamically.
231 continue
232 settings_by_condition[condition].append(
233 'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
234 # Print out an if statement for each unique condition, with
235 # the SET_* calls nested inside it.
236 for condition in sorted(settings_by_condition.keys()):
237 print ' if ({0}) {{'.format(condition)
238 for setting in sorted(settings_by_condition[condition]):
239 print ' {0}'.format(setting)
240 print ' }'
241
242
243 def _parser():
244 """Parse arguments and return namespace."""
245 parser = argparse.ArgumentParser()
246 parser.add_argument('-f',
247 dest='filename',
248 default='gl_and_es_API.xml',
249 help='an xml file describing an API')
250 return parser.parse_args()
251
252
253 def main():
254 """Main function."""
255 args = _parser()
256 printer = PrintCode()
257 api = gl_XML.parse_GL_API(args.filename)
258 printer.Print(api)
259
260
261 if __name__ == '__main__':
262 main()