Merge branch 'master' of ../mesa 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/objectpurge.h"
92 #include "main/performance_monitor.h"
93 #include "main/pipelineobj.h"
94 #include "main/pixel.h"
95 #include "main/pixelstore.h"
96 #include "main/points.h"
97 #include "main/polygon.h"
98 #include "main/program_resource.h"
99 #include "main/querymatrix.h"
100 #include "main/queryobj.h"
101 #include "main/readpix.h"
102 #include "main/samplerobj.h"
103 #include "main/scissor.h"
104 #include "main/stencil.h"
105 #include "main/texenv.h"
106 #include "main/texgetimage.h"
107 #include "main/teximage.h"
108 #include "main/texgen.h"
109 #include "main/texobj.h"
110 #include "main/texparam.h"
111 #include "main/texstate.h"
112 #include "main/texstorage.h"
113 #include "main/texturebarrier.h"
114 #include "main/textureview.h"
115 #include "main/transformfeedback.h"
116 #include "main/mtypes.h"
117 #include "main/varray.h"
118 #include "main/viewport.h"
119 #include "main/shaderapi.h"
120 #include "main/shaderimage.h"
121 #include "main/uniforms.h"
122 #include "main/syncobj.h"
123 #include "main/formatquery.h"
124 #include "main/dispatch.h"
125 #include "main/vdpau.h"
126 #include "vbo/vbo.h"
127
128
129 /**
130 * Initialize a context's exec table with pointers to Mesa's supported
131 * GL functions.
132 *
133 * This function depends on ctx->Version.
134 *
135 * \param ctx GL context to which \c exec belongs.
136 */
137 void
138 _mesa_initialize_exec_table(struct gl_context *ctx)
139 {
140 struct _glapi_table *exec;
141
142 exec = ctx->Exec;
143 assert(exec != NULL);
144
145 assert(ctx->Version > 0);
146
147 vbo_initialize_exec_dispatch(ctx, exec);
148 """
149
150
151 footer = """
152 }
153 """
154
155
156 class PrintCode(gl_XML.gl_print_base):
157
158 def __init__(self):
159 gl_XML.gl_print_base.__init__(self)
160
161 self.name = 'gl_genexec.py'
162 self.license = license.bsd_license_template % (
163 'Copyright (C) 2012 Intel Corporation',
164 'Intel Corporation')
165
166 def printRealHeader(self):
167 print header
168
169 def printRealFooter(self):
170 print footer
171
172 def printBody(self, api):
173 # Collect SET_* calls by the condition under which they should
174 # be called.
175 settings_by_condition = collections.defaultdict(lambda: [])
176 for f in api.functionIterateAll():
177 if f.exec_flavor not in exec_flavor_map:
178 raise Exception(
179 'Unrecognized exec flavor {0!r}'.format(f.exec_flavor))
180 condition_parts = []
181 if f.name in apiexec.functions:
182 ex = apiexec.functions[f.name]
183 unconditional_count = 0
184
185 if ex.compatibility is not None:
186 condition_parts.append('ctx->API == API_OPENGL_COMPAT')
187 unconditional_count += 1
188
189 if ex.core is not None:
190 condition_parts.append('ctx->API == API_OPENGL_CORE')
191 unconditional_count += 1
192
193 if ex.es1 is not None:
194 condition_parts.append('ctx->API == API_OPENGLES')
195 unconditional_count += 1
196
197 if ex.es2 is not None:
198 if ex.es2 > 20:
199 condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(ex.es2))
200 else:
201 condition_parts.append('ctx->API == API_OPENGLES2')
202 unconditional_count += 1
203
204 # If the function is unconditionally available in all four
205 # APIs, then it is always available. Replace the complex
206 # tautology condition with "true" and let GCC do the right
207 # thing.
208 if unconditional_count == 4:
209 condition_parts = ['true']
210 else:
211 if f.desktop:
212 if f.deprecated:
213 condition_parts.append('ctx->API == API_OPENGL_COMPAT')
214 else:
215 condition_parts.append('_mesa_is_desktop_gl(ctx)')
216 if 'es1' in f.api_map:
217 condition_parts.append('ctx->API == API_OPENGLES')
218 if 'es2' in f.api_map:
219 if f.api_map['es2'] > 2.0:
220 condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(int(f.api_map['es2'] * 10)))
221 else:
222 condition_parts.append('ctx->API == API_OPENGLES2')
223
224 if not condition_parts:
225 # This function does not exist in any API.
226 continue
227 condition = ' || '.join(condition_parts)
228 prefix = exec_flavor_map[f.exec_flavor]
229 if prefix is None:
230 # This function is not implemented, or is dispatched
231 # dynamically.
232 continue
233 settings_by_condition[condition].append(
234 'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
235 # Print out an if statement for each unique condition, with
236 # the SET_* calls nested inside it.
237 for condition in sorted(settings_by_condition.keys()):
238 print ' if ({0}) {{'.format(condition)
239 for setting in sorted(settings_by_condition[condition]):
240 print ' {0}'.format(setting)
241 print ' }'
242
243
244 def _parser():
245 """Parse arguments and return namespace."""
246 parser = argparse.ArgumentParser()
247 parser.add_argument('-f',
248 dest='filename',
249 default='gl_and_es_API.xml',
250 help='an xml file describing an API')
251 return parser.parse_args()
252
253
254 def main():
255 """Main function."""
256 args = _parser()
257 printer = PrintCode()
258 api = gl_XML.parse_GL_API(args.filename)
259 printer.Print(api)
260
261
262 if __name__ == '__main__':
263 main()