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