1848b8605Smrg
2848b8605Smrg# Copyright (C) 2012 Intel Corporation
3848b8605Smrg#
4848b8605Smrg# Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg# copy of this software and associated documentation files (the "Software"),
6848b8605Smrg# to deal in the Software without restriction, including without limitation
7848b8605Smrg# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg# and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg# Software is furnished to do so, subject to the following conditions:
10848b8605Smrg#
11848b8605Smrg# The above copyright notice and this permission notice (including the next
12848b8605Smrg# paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg# Software.
14848b8605Smrg#
15848b8605Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21848b8605Smrg# IN THE SOFTWARE.
22848b8605Smrg
23848b8605Smrg# This script generates the file api_exec.c, which contains
24848b8605Smrg# _mesa_initialize_exec_table().  It is responsible for populating all
25848b8605Smrg# entries in the "exec" dispatch table that aren't dynamic.
26848b8605Smrg
27b8e80941Smrgfrom __future__ import print_function
28b8e80941Smrg
29b8e80941Smrgimport argparse
30848b8605Smrgimport collections
31848b8605Smrgimport license
32848b8605Smrgimport gl_XML
33b8e80941Smrgimport sys
34b8e80941Smrgimport apiexec
35848b8605Smrg
36848b8605Smrg
37848b8605Smrgexec_flavor_map = {
38848b8605Smrg    'dynamic': None,
39848b8605Smrg    'mesa': '_mesa_',
40848b8605Smrg    'skip': None,
41848b8605Smrg    }
42848b8605Smrg
43848b8605Smrg
44848b8605Smrgheader = """/**
45848b8605Smrg * \\file api_exec.c
46848b8605Smrg * Initialize dispatch table.
47848b8605Smrg */
48848b8605Smrg
49848b8605Smrg
50848b8605Smrg#include "main/accum.h"
51848b8605Smrg#include "main/api_loopback.h"
52848b8605Smrg#include "main/api_exec.h"
53848b8605Smrg#include "main/arbprogram.h"
54848b8605Smrg#include "main/atifragshader.h"
55848b8605Smrg#include "main/attrib.h"
56848b8605Smrg#include "main/blend.h"
57848b8605Smrg#include "main/blit.h"
58848b8605Smrg#include "main/bufferobj.h"
59848b8605Smrg#include "main/arrayobj.h"
60b8e80941Smrg#include "main/bbox.h"
61848b8605Smrg#include "main/buffers.h"
62848b8605Smrg#include "main/clear.h"
63848b8605Smrg#include "main/clip.h"
64848b8605Smrg#include "main/colortab.h"
65848b8605Smrg#include "main/compute.h"
66848b8605Smrg#include "main/condrender.h"
67b8e80941Smrg#include "main/conservativeraster.h"
68848b8605Smrg#include "main/context.h"
69848b8605Smrg#include "main/convolve.h"
70848b8605Smrg#include "main/copyimage.h"
71848b8605Smrg#include "main/depth.h"
72b8e80941Smrg#include "main/debug_output.h"
73848b8605Smrg#include "main/dlist.h"
74b8e80941Smrg#include "main/draw.h"
75848b8605Smrg#include "main/drawpix.h"
76848b8605Smrg#include "main/drawtex.h"
77848b8605Smrg#include "main/rastpos.h"
78848b8605Smrg#include "main/enable.h"
79848b8605Smrg#include "main/errors.h"
80848b8605Smrg#include "main/es1_conversion.h"
81848b8605Smrg#include "main/eval.h"
82b8e80941Smrg#include "main/externalobjects.h"
83848b8605Smrg#include "main/get.h"
84b8e80941Smrg#include "main/glspirv.h"
85848b8605Smrg#include "main/feedback.h"
86848b8605Smrg#include "main/fog.h"
87848b8605Smrg#include "main/fbobject.h"
88848b8605Smrg#include "main/framebuffer.h"
89848b8605Smrg#include "main/genmipmap.h"
90848b8605Smrg#include "main/hint.h"
91848b8605Smrg#include "main/histogram.h"
92848b8605Smrg#include "main/imports.h"
93848b8605Smrg#include "main/light.h"
94848b8605Smrg#include "main/lines.h"
95848b8605Smrg#include "main/matrix.h"
96848b8605Smrg#include "main/multisample.h"
97848b8605Smrg#include "main/objectlabel.h"
98b8e80941Smrg#include "main/objectpurge.h"
99848b8605Smrg#include "main/performance_monitor.h"
100b8e80941Smrg#include "main/performance_query.h"
101848b8605Smrg#include "main/pipelineobj.h"
102848b8605Smrg#include "main/pixel.h"
103848b8605Smrg#include "main/pixelstore.h"
104848b8605Smrg#include "main/points.h"
105848b8605Smrg#include "main/polygon.h"
106b8e80941Smrg#include "main/program_resource.h"
107848b8605Smrg#include "main/querymatrix.h"
108848b8605Smrg#include "main/queryobj.h"
109848b8605Smrg#include "main/readpix.h"
110848b8605Smrg#include "main/samplerobj.h"
111848b8605Smrg#include "main/scissor.h"
112848b8605Smrg#include "main/stencil.h"
113848b8605Smrg#include "main/texenv.h"
114848b8605Smrg#include "main/texgetimage.h"
115848b8605Smrg#include "main/teximage.h"
116848b8605Smrg#include "main/texgen.h"
117848b8605Smrg#include "main/texobj.h"
118848b8605Smrg#include "main/texparam.h"
119848b8605Smrg#include "main/texstate.h"
120848b8605Smrg#include "main/texstorage.h"
121b8e80941Smrg#include "main/barrier.h"
122b8e80941Smrg#include "main/texturebindless.h"
123848b8605Smrg#include "main/textureview.h"
124848b8605Smrg#include "main/transformfeedback.h"
125848b8605Smrg#include "main/mtypes.h"
126848b8605Smrg#include "main/varray.h"
127848b8605Smrg#include "main/viewport.h"
128848b8605Smrg#include "main/shaderapi.h"
129848b8605Smrg#include "main/shaderimage.h"
130848b8605Smrg#include "main/uniforms.h"
131848b8605Smrg#include "main/syncobj.h"
132848b8605Smrg#include "main/formatquery.h"
133848b8605Smrg#include "main/dispatch.h"
134848b8605Smrg#include "main/vdpau.h"
135848b8605Smrg
136848b8605Smrg
137848b8605Smrg/**
138848b8605Smrg * Initialize a context's exec table with pointers to Mesa's supported
139848b8605Smrg * GL functions.
140848b8605Smrg *
141848b8605Smrg * This function depends on ctx->Version.
142848b8605Smrg *
143848b8605Smrg * \param ctx  GL context to which \c exec belongs.
144848b8605Smrg */
145848b8605Smrgvoid
146848b8605Smrg_mesa_initialize_exec_table(struct gl_context *ctx)
147848b8605Smrg{
148848b8605Smrg   struct _glapi_table *exec;
149848b8605Smrg
150848b8605Smrg   exec = ctx->Exec;
151848b8605Smrg   assert(exec != NULL);
152848b8605Smrg
153848b8605Smrg   assert(ctx->Version > 0);
154848b8605Smrg
155b8e80941Smrg   _mesa_initialize_exec_dispatch(ctx, exec);
156848b8605Smrg"""
157848b8605Smrg
158848b8605Smrg
159848b8605Smrgfooter = """
160848b8605Smrg}
161848b8605Smrg"""
162848b8605Smrg
163848b8605Smrg
164848b8605Smrgclass PrintCode(gl_XML.gl_print_base):
165848b8605Smrg
166848b8605Smrg    def __init__(self):
167848b8605Smrg        gl_XML.gl_print_base.__init__(self)
168848b8605Smrg
169848b8605Smrg        self.name = 'gl_genexec.py'
170848b8605Smrg        self.license = license.bsd_license_template % (
171848b8605Smrg            'Copyright (C) 2012 Intel Corporation',
172848b8605Smrg            'Intel Corporation')
173848b8605Smrg
174848b8605Smrg    def printRealHeader(self):
175b8e80941Smrg        print(header)
176848b8605Smrg
177848b8605Smrg    def printRealFooter(self):
178b8e80941Smrg        print(footer)
179848b8605Smrg
180848b8605Smrg    def printBody(self, api):
181848b8605Smrg        # Collect SET_* calls by the condition under which they should
182848b8605Smrg        # be called.
183848b8605Smrg        settings_by_condition = collections.defaultdict(lambda: [])
184848b8605Smrg        for f in api.functionIterateAll():
185848b8605Smrg            if f.exec_flavor not in exec_flavor_map:
186848b8605Smrg                raise Exception(
187848b8605Smrg                    'Unrecognized exec flavor {0!r}'.format(f.exec_flavor))
188848b8605Smrg            condition_parts = []
189b8e80941Smrg            if f.name in apiexec.functions:
190b8e80941Smrg                ex = apiexec.functions[f.name]
191b8e80941Smrg                unconditional_count = 0
192b8e80941Smrg
193b8e80941Smrg                if ex.compatibility is not None:
194848b8605Smrg                    condition_parts.append('ctx->API == API_OPENGL_COMPAT')
195b8e80941Smrg                    unconditional_count += 1
196b8e80941Smrg
197b8e80941Smrg                if ex.core is not None:
198b8e80941Smrg                    condition_parts.append('ctx->API == API_OPENGL_CORE')
199b8e80941Smrg                    unconditional_count += 1
200b8e80941Smrg
201b8e80941Smrg                if ex.es1 is not None:
202b8e80941Smrg                    condition_parts.append('ctx->API == API_OPENGLES')
203b8e80941Smrg                    unconditional_count += 1
204b8e80941Smrg
205b8e80941Smrg                if ex.es2 is not None:
206b8e80941Smrg                    if ex.es2 > 20:
207b8e80941Smrg                        condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(ex.es2))
208b8e80941Smrg                    else:
209b8e80941Smrg                        condition_parts.append('ctx->API == API_OPENGLES2')
210b8e80941Smrg                        unconditional_count += 1
211b8e80941Smrg
212b8e80941Smrg                # If the function is unconditionally available in all four
213b8e80941Smrg                # APIs, then it is always available.  Replace the complex
214b8e80941Smrg                # tautology condition with "true" and let GCC do the right
215b8e80941Smrg                # thing.
216b8e80941Smrg                if unconditional_count == 4:
217b8e80941Smrg                    condition_parts = ['true']
218b8e80941Smrg            else:
219b8e80941Smrg                if f.desktop:
220b8e80941Smrg                    if f.deprecated:
221b8e80941Smrg                        condition_parts.append('ctx->API == API_OPENGL_COMPAT')
222b8e80941Smrg                    else:
223b8e80941Smrg                        condition_parts.append('_mesa_is_desktop_gl(ctx)')
224b8e80941Smrg                if 'es1' in f.api_map:
225b8e80941Smrg                    condition_parts.append('ctx->API == API_OPENGLES')
226b8e80941Smrg                if 'es2' in f.api_map:
227b8e80941Smrg                    if f.api_map['es2'] > 2.0:
228b8e80941Smrg                        condition_parts.append('(ctx->API == API_OPENGLES2 && ctx->Version >= {0})'.format(int(f.api_map['es2'] * 10)))
229b8e80941Smrg                    else:
230b8e80941Smrg                        condition_parts.append('ctx->API == API_OPENGLES2')
231b8e80941Smrg
232848b8605Smrg            if not condition_parts:
233848b8605Smrg                # This function does not exist in any API.
234848b8605Smrg                continue
235848b8605Smrg            condition = ' || '.join(condition_parts)
236848b8605Smrg            prefix = exec_flavor_map[f.exec_flavor]
237848b8605Smrg            if prefix is None:
238848b8605Smrg                # This function is not implemented, or is dispatched
239848b8605Smrg                # dynamically.
240848b8605Smrg                continue
241b8e80941Smrg            if f.has_no_error_variant:
242b8e80941Smrg                no_error_condition = '_mesa_is_no_error_enabled(ctx) && ({0})'.format(condition)
243b8e80941Smrg                error_condition = '!_mesa_is_no_error_enabled(ctx) && ({0})'.format(condition)
244b8e80941Smrg                settings_by_condition[no_error_condition].append(
245b8e80941Smrg                    'SET_{0}(exec, {1}{0}_no_error);'.format(f.name, prefix, f.name))
246b8e80941Smrg                settings_by_condition[error_condition].append(
247b8e80941Smrg                    'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
248b8e80941Smrg            else:
249b8e80941Smrg                settings_by_condition[condition].append(
250b8e80941Smrg                    'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
251848b8605Smrg        # Print out an if statement for each unique condition, with
252848b8605Smrg        # the SET_* calls nested inside it.
253848b8605Smrg        for condition in sorted(settings_by_condition.keys()):
254b8e80941Smrg            print('   if ({0}) {{'.format(condition))
255848b8605Smrg            for setting in sorted(settings_by_condition[condition]):
256b8e80941Smrg                print('      {0}'.format(setting))
257b8e80941Smrg            print('   }')
258848b8605Smrg
259848b8605Smrg
260b8e80941Smrgdef _parser():
261b8e80941Smrg    """Parse arguments and return namespace."""
262b8e80941Smrg    parser = argparse.ArgumentParser()
263b8e80941Smrg    parser.add_argument('-f',
264b8e80941Smrg                        dest='filename',
265b8e80941Smrg                        default='gl_and_es_API.xml',
266b8e80941Smrg                        help='an xml file describing an API')
267b8e80941Smrg    return parser.parse_args()
268848b8605Smrg
269848b8605Smrg
270b8e80941Smrgdef main():
271b8e80941Smrg    """Main function."""
272b8e80941Smrg    args = _parser()
273848b8605Smrg    printer = PrintCode()
274b8e80941Smrg    api = gl_XML.parse_GL_API(args.filename)
275848b8605Smrg    printer.Print(api)
276b8e80941Smrg
277b8e80941Smrg
278b8e80941Smrgif __name__ == '__main__':
279b8e80941Smrg    main()
280