2 # -*- coding: utf-8 -*-
4 from __future__
import with_statement
10 from subprocess
import Popen
, PIPE
13 # Local module: generator for texture lookup builtins
14 from texture_builtins
import generate_texture_functions
16 builtins_dir
= path
.join(path
.dirname(path
.abspath(__file__
)), "..")
18 # Get the path to the standalone GLSL compiler
20 print "Usage:", argv
[0], "<path to compiler>"
25 # Read the files in builtins/ir/*...add them to the supplied dictionary.
26 def read_ir_files(fs
):
27 for filename
in glob(path
.join(path
.join(builtins_dir
, 'ir'), '*.ir')):
28 function_name
= path
.basename(filename
).split('.')[0]
29 with
open(filename
) as f
:
30 fs
[function_name
] = f
.read()
32 # Return a dictionary containing all builtin definitions (even generated)
33 def get_builtin_definitions():
35 generate_texture_functions(fs
)
40 # Work around MSVC's 65535 byte limit by outputting an array of characters
41 # rather than actual string literals.
43 #t = "/* Warning: length " + repr(len(s)) + " too large */\n"
45 for c
in re
.sub('\s\s+', ' ', s
):
50 return '{' + t
[:-1] + '}'
52 t
= s
.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n"\n "')
53 return ' "' + t
+ '"\n'
55 def write_function_definitions():
56 fs
= get_builtin_definitions()
57 for k
, v
in sorted(fs
.iteritems()):
58 print 'static const char builtin_' + k
+ '[] ='
59 print stringify(v
), ';'
61 def run_compiler(args
):
62 command
= [compiler
, '--dump-lir'] + args
63 p
= Popen(command
, 1, stdout
=PIPE
, shell
=False)
64 output
= p
.communicate()[0]
67 sys
.stderr
.write("Failed to compile builtins with command:\n")
69 sys
.stderr
.write(arg
+ " ")
70 sys
.stderr
.write("\n")
71 sys
.stderr
.write("Result:\n")
72 sys
.stderr
.write(output
)
74 # Clean up output a bit by killing whitespace before a closing paren.
75 kill_paren_whitespace
= re
.compile(r
'[ \n]*\)', re
.MULTILINE
)
76 output
= kill_paren_whitespace
.sub(')', output
)
78 # Also toss any duplicate newlines
79 output
= output
.replace('\n\n', '\n')
81 return (output
, p
.returncode
)
83 def write_profile(filename
, profile
):
84 (proto_ir
, returncode
) = run_compiler([filename
])
87 print '#error builtins profile', profile
, 'failed to compile'
90 # Kill any global variable declarations. We don't want them.
91 kill_globals
= re
.compile(r
'^\(declare.*\n', re
.MULTILINE
)
92 proto_ir
= kill_globals
.sub('', proto_ir
)
94 print 'static const char prototypes_for_' + profile
+ '[] ='
95 print stringify(proto_ir
), ';'
97 # Print a table of all the functions (not signatures) referenced.
98 # This is done so we can avoid bothering with a hash table in the C++ code.
100 function_names
= set()
101 for func
in re
.finditer(r
'\(function (.+)\n', proto_ir
):
102 function_names
.add(func
.group(1))
104 print 'static const char *functions_for_' + profile
+ ' [] = {'
105 for func
in sorted(function_names
):
106 print ' builtin_' + func
+ ','
109 def write_profiles():
110 profiles
= get_profile_list()
111 for (filename
, profile
) in profiles
:
112 write_profile(filename
, profile
)
114 def get_profile_list():
116 for extension
in ['frag', 'vert']:
117 path_glob
= path
.join(
118 path
.join(builtins_dir
, 'profiles'), '*.' + extension
)
119 profile_files
.extend(glob(path_glob
))
121 for pfile
in sorted(profile_files
):
122 profiles
.append((pfile
, path
.basename(pfile
).replace('.', '_')))
125 if __name__
== "__main__":
126 print """/* DO NOT MODIFY - automatically generated by generate_builtins.py */
128 * Copyright © 2010 Intel Corporation
130 * Permission is hereby granted, free of charge, to any person obtaining a
131 * copy of this software and associated documentation files (the "Software"),
132 * to deal in the Software without restriction, including without limitation
133 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
134 * and/or sell copies of the Software, and to permit persons to whom the
135 * Software is furnished to do so, subject to the following conditions:
137 * The above copyright notice and this permission notice (including the next
138 * paragraph) shall be included in all copies or substantial portions of the
141 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
142 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
143 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
144 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
145 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
146 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
147 * DEALINGS IN THE SOFTWARE.
151 #include "main/core.h" /* for struct gl_shader */
152 #include "glsl_parser_extras.h"
153 #include "ir_reader.h"
157 extern "C" struct gl_shader *
158 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
161 read_builtins(GLenum target, const char *protos, const char **functions, unsigned count)
163 struct gl_context fakeCtx;
164 fakeCtx.API = API_OPENGL;
165 fakeCtx.Const.GLSLVersion = 140;
166 fakeCtx.Extensions.ARB_ES2_compatibility = true;
167 fakeCtx.Const.ForceGLSLExtensionsWarn = false;
168 gl_shader *sh = _mesa_new_shader(NULL, 0, target);
169 struct _mesa_glsl_parse_state *st =
170 new(sh) _mesa_glsl_parse_state(&fakeCtx, target, sh);
172 st->language_version = 140;
173 st->symbols->language_version = 140;
174 st->ARB_texture_rectangle_enable = true;
175 st->EXT_texture_array_enable = true;
176 st->OES_EGL_image_external_enable = true;
177 _mesa_glsl_initialize_types(st);
179 sh->ir = new(sh) exec_list;
180 sh->symbols = st->symbols;
182 /* Read the IR containing the prototypes */
183 _mesa_glsl_read_ir(st, sh->ir, protos, true);
185 /* Read ALL the function bodies, telling the IR reader not to scan for
186 * prototypes (we've already created them). The IR reader will skip any
187 * signature that does not already exist as a prototype.
189 for (unsigned i = 0; i < count; i++) {
190 _mesa_glsl_read_ir(st, sh->ir, functions[i], false);
193 printf("error reading builtin: %.35s ...\\n", functions[i]);
194 printf("Info log:\\n%s\\n", st->info_log);
200 reparent_ir(sh->ir, sh);
207 write_function_definitions()
210 profiles
= get_profile_list()
212 print 'static gl_shader *builtin_profiles[%d];' % len(profiles
)
215 void *builtin_mem_ctx = NULL;
218 _mesa_glsl_release_functions(void)
220 ralloc_free(builtin_mem_ctx);
221 builtin_mem_ctx = NULL;
222 memset(builtin_profiles, 0, sizeof(builtin_profiles));
226 _mesa_read_profile(struct _mesa_glsl_parse_state *state,
228 const char *prototypes,
229 const char **functions,
232 gl_shader *sh = builtin_profiles[profile_index];
235 sh = read_builtins(GL_VERTEX_SHADER, prototypes, functions, count);
236 ralloc_steal(builtin_mem_ctx, sh);
237 builtin_profiles[profile_index] = sh;
240 state->builtins_to_link[state->num_builtins_to_link] = sh;
241 state->num_builtins_to_link++;
245 _mesa_glsl_initialize_functions(struct _mesa_glsl_parse_state *state)
247 /* If we've already initialized the built-ins, bail early. */
248 if (state->num_builtins_to_link > 0)
251 if (builtin_mem_ctx == NULL) {
252 builtin_mem_ctx = ralloc_context(NULL); // "GLSL built-in functions"
253 memset(&builtin_profiles, 0, sizeof(builtin_profiles));
258 for (filename
, profile
) in profiles
:
259 if profile
.endswith('_vert'):
260 check
= 'state->target == vertex_shader && '
261 elif profile
.endswith('_frag'):
262 check
= 'state->target == fragment_shader && '
264 version
= re
.sub(r
'_(vert|frag)$', '', profile
)
265 if version
.isdigit():
266 check
+= 'state->language_version == ' + version
267 else: # an extension name
268 check
+= 'state->' + version
+ '_enable'
270 print ' if (' + check
+ ') {'
271 print ' _mesa_read_profile(state, %d,' % i
272 print ' prototypes_for_' + profile
+ ','
273 print ' functions_for_' + profile
+ ','
274 print ' Elements(functions_for_' + profile
+ '));'