gl_genexec.py revision 848b8605
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
28import collections
29import license
30import gl_XML
31import sys, getopt
32
33
34exec_flavor_map = {
35    'dynamic': None,
36    'mesa': '_mesa_',
37    'skip': None,
38    }
39
40
41header = """/**
42 * \\file api_exec.c
43 * Initialize dispatch table.
44 */
45
46
47#include "main/accum.h"
48#include "main/api_loopback.h"
49#include "main/api_exec.h"
50#include "main/arbprogram.h"
51#include "main/atifragshader.h"
52#include "main/attrib.h"
53#include "main/blend.h"
54#include "main/blit.h"
55#include "main/bufferobj.h"
56#include "main/arrayobj.h"
57#include "main/buffers.h"
58#include "main/clear.h"
59#include "main/clip.h"
60#include "main/colortab.h"
61#include "main/compute.h"
62#include "main/condrender.h"
63#include "main/context.h"
64#include "main/convolve.h"
65#include "main/copyimage.h"
66#include "main/depth.h"
67#include "main/dlist.h"
68#include "main/drawpix.h"
69#include "main/drawtex.h"
70#include "main/rastpos.h"
71#include "main/enable.h"
72#include "main/errors.h"
73#include "main/es1_conversion.h"
74#include "main/eval.h"
75#include "main/get.h"
76#include "main/feedback.h"
77#include "main/fog.h"
78#include "main/fbobject.h"
79#include "main/framebuffer.h"
80#include "main/genmipmap.h"
81#include "main/hint.h"
82#include "main/histogram.h"
83#include "main/imports.h"
84#include "main/light.h"
85#include "main/lines.h"
86#include "main/matrix.h"
87#include "main/multisample.h"
88#include "main/objectlabel.h"
89#include "main/performance_monitor.h"
90#include "main/pipelineobj.h"
91#include "main/pixel.h"
92#include "main/pixelstore.h"
93#include "main/points.h"
94#include "main/polygon.h"
95#include "main/querymatrix.h"
96#include "main/queryobj.h"
97#include "main/readpix.h"
98#include "main/samplerobj.h"
99#include "main/scissor.h"
100#include "main/stencil.h"
101#include "main/texenv.h"
102#include "main/texgetimage.h"
103#include "main/teximage.h"
104#include "main/texgen.h"
105#include "main/texobj.h"
106#include "main/texparam.h"
107#include "main/texstate.h"
108#include "main/texstorage.h"
109#include "main/texturebarrier.h"
110#include "main/textureview.h"
111#include "main/transformfeedback.h"
112#include "main/mtypes.h"
113#include "main/varray.h"
114#include "main/viewport.h"
115#include "main/shaderapi.h"
116#include "main/shaderimage.h"
117#include "main/uniforms.h"
118#include "main/syncobj.h"
119#include "main/formatquery.h"
120#include "main/dispatch.h"
121#include "main/vdpau.h"
122#include "vbo/vbo.h"
123
124
125/**
126 * Initialize a context's exec table with pointers to Mesa's supported
127 * GL functions.
128 *
129 * This function depends on ctx->Version.
130 *
131 * \param ctx  GL context to which \c exec belongs.
132 */
133void
134_mesa_initialize_exec_table(struct gl_context *ctx)
135{
136   struct _glapi_table *exec;
137
138   exec = ctx->Exec;
139   assert(exec != NULL);
140
141   assert(ctx->Version > 0);
142
143   vbo_initialize_exec_dispatch(ctx, exec);
144"""
145
146
147footer = """
148}
149"""
150
151
152class PrintCode(gl_XML.gl_print_base):
153
154    def __init__(self):
155        gl_XML.gl_print_base.__init__(self)
156
157        self.name = 'gl_genexec.py'
158        self.license = license.bsd_license_template % (
159            'Copyright (C) 2012 Intel Corporation',
160            'Intel Corporation')
161
162    def printRealHeader(self):
163        print header
164
165    def printRealFooter(self):
166        print footer
167
168    def printBody(self, api):
169        # Collect SET_* calls by the condition under which they should
170        # be called.
171        settings_by_condition = collections.defaultdict(lambda: [])
172        for f in api.functionIterateAll():
173            if f.exec_flavor not in exec_flavor_map:
174                raise Exception(
175                    'Unrecognized exec flavor {0!r}'.format(f.exec_flavor))
176            condition_parts = []
177            if f.desktop:
178                if f.deprecated:
179                    condition_parts.append('ctx->API == API_OPENGL_COMPAT')
180                else:
181                    condition_parts.append('_mesa_is_desktop_gl(ctx)')
182            if 'es1' in f.api_map:
183                condition_parts.append('ctx->API == API_OPENGLES')
184            if 'es2' in f.api_map:
185                if f.api_map['es2'] == 3:
186                    condition_parts.append('_mesa_is_gles3(ctx)')
187                else:
188                    condition_parts.append('ctx->API == API_OPENGLES2')
189            if not condition_parts:
190                # This function does not exist in any API.
191                continue
192            condition = ' || '.join(condition_parts)
193            prefix = exec_flavor_map[f.exec_flavor]
194            if prefix is None:
195                # This function is not implemented, or is dispatched
196                # dynamically.
197                continue
198            settings_by_condition[condition].append(
199                'SET_{0}(exec, {1}{0});'.format(f.name, prefix, f.name))
200        # Print out an if statement for each unique condition, with
201        # the SET_* calls nested inside it.
202        for condition in sorted(settings_by_condition.keys()):
203            print '   if ({0}) {{'.format(condition)
204            for setting in sorted(settings_by_condition[condition]):
205                print '      {0}'.format(setting)
206            print '   }'
207
208
209def show_usage():
210    print "Usage: %s [-f input_file_name]" % sys.argv[0]
211    sys.exit(1)
212
213
214if __name__ == '__main__':
215    file_name = "gl_and_es_API.xml"
216
217    try:
218        (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
219    except Exception,e:
220        show_usage()
221
222    for (arg,val) in args:
223        if arg == "-f":
224            file_name = val
225
226    printer = PrintCode()
227
228    api = gl_XML.parse_GL_API(file_name)
229    printer.Print(api)
230