glsl: initialise const force glsl extension warning in fake ctx
[mesa.git] / src / glsl / builtins / tools / generate_builtins.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import with_statement
5
6 import re
7 import sys
8 from glob import glob
9 from os import path
10 from subprocess import Popen, PIPE
11 from sys import argv
12
13 # Local module: generator for texture lookup builtins
14 from texture_builtins import generate_texture_functions
15
16 builtins_dir = path.join(path.dirname(path.abspath(__file__)), "..")
17
18 # Get the path to the standalone GLSL compiler
19 if len(argv) != 2:
20 print "Usage:", argv[0], "<path to compiler>"
21 sys.exit(1)
22
23 compiler = argv[1]
24
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()
31
32 # Return a dictionary containing all builtin definitions (even generated)
33 def get_builtin_definitions():
34 fs = {}
35 generate_texture_functions(fs)
36 read_ir_files(fs)
37 return fs
38
39 def stringify(s):
40 # Work around MSVC's 65535 byte limit by outputting an array of characters
41 # rather than actual string literals.
42 if len(s) >= 65535:
43 #t = "/* Warning: length " + repr(len(s)) + " too large */\n"
44 t = ""
45 for c in re.sub('\s\s+', ' ', s):
46 if c == '\n':
47 t += '\n'
48 else:
49 t += "'" + c + "',"
50 return '{' + t[:-1] + '}'
51
52 t = s.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n"\n "')
53 return ' "' + t + '"\n'
54
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), ';'
60
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]
65
66 if (p.returncode):
67 sys.stderr.write("Failed to compile builtins with command:\n")
68 for arg in command:
69 sys.stderr.write(arg + " ")
70 sys.stderr.write("\n")
71 sys.stderr.write("Result:\n")
72 sys.stderr.write(output)
73
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)
77
78 # Also toss any duplicate newlines
79 output = output.replace('\n\n', '\n')
80
81 return (output, p.returncode)
82
83 def write_profile(filename, profile):
84 (proto_ir, returncode) = run_compiler([filename])
85
86 if returncode != 0:
87 print '#error builtins profile', profile, 'failed to compile'
88 return
89
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)
93
94 print 'static const char prototypes_for_' + profile + '[] ='
95 print stringify(proto_ir), ';'
96
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.
99
100 function_names = set()
101 for func in re.finditer(r'\(function (.+)\n', proto_ir):
102 function_names.add(func.group(1))
103
104 print 'static const char *functions_for_' + profile + ' [] = {'
105 for func in sorted(function_names):
106 print ' builtin_' + func + ','
107 print '};'
108
109 def write_profiles():
110 profiles = get_profile_list()
111 for (filename, profile) in profiles:
112 write_profile(filename, profile)
113
114 def get_profile_list():
115 profile_files = []
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))
120 profiles = []
121 for pfile in sorted(profile_files):
122 profiles.append((pfile, path.basename(pfile).replace('.', '_')))
123 return profiles
124
125 if __name__ == "__main__":
126 print """/* DO NOT MODIFY - automatically generated by generate_builtins.py */
127 /*
128 * Copyright © 2010 Intel Corporation
129 *
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:
136 *
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
139 * Software.
140 *
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.
148 */
149
150 #include <stdio.h>
151 #include "main/core.h" /* for struct gl_shader */
152 #include "glsl_parser_extras.h"
153 #include "ir_reader.h"
154 #include "program.h"
155 #include "ast.h"
156
157 extern "C" struct gl_shader *
158 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
159
160 gl_shader *
161 read_builtins(GLenum target, const char *protos, const char **functions, unsigned count)
162 {
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);
171
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);
178
179 sh->ir = new(sh) exec_list;
180 sh->symbols = st->symbols;
181
182 /* Read the IR containing the prototypes */
183 _mesa_glsl_read_ir(st, sh->ir, protos, true);
184
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.
188 */
189 for (unsigned i = 0; i < count; i++) {
190 _mesa_glsl_read_ir(st, sh->ir, functions[i], false);
191
192 if (st->error) {
193 printf("error reading builtin: %.35s ...\\n", functions[i]);
194 printf("Info log:\\n%s\\n", st->info_log);
195 ralloc_free(sh);
196 return NULL;
197 }
198 }
199
200 reparent_ir(sh->ir, sh);
201 delete st;
202
203 return sh;
204 }
205 """
206
207 write_function_definitions()
208 write_profiles()
209
210 profiles = get_profile_list()
211
212 print 'static gl_shader *builtin_profiles[%d];' % len(profiles)
213
214 print """
215 void *builtin_mem_ctx = NULL;
216
217 void
218 _mesa_glsl_release_functions(void)
219 {
220 ralloc_free(builtin_mem_ctx);
221 builtin_mem_ctx = NULL;
222 memset(builtin_profiles, 0, sizeof(builtin_profiles));
223 }
224
225 static void
226 _mesa_read_profile(struct _mesa_glsl_parse_state *state,
227 int profile_index,
228 const char *prototypes,
229 const char **functions,
230 int count)
231 {
232 gl_shader *sh = builtin_profiles[profile_index];
233
234 if (sh == NULL) {
235 sh = read_builtins(GL_VERTEX_SHADER, prototypes, functions, count);
236 ralloc_steal(builtin_mem_ctx, sh);
237 builtin_profiles[profile_index] = sh;
238 }
239
240 state->builtins_to_link[state->num_builtins_to_link] = sh;
241 state->num_builtins_to_link++;
242 }
243
244 void
245 _mesa_glsl_initialize_functions(struct _mesa_glsl_parse_state *state)
246 {
247 /* If we've already initialized the built-ins, bail early. */
248 if (state->num_builtins_to_link > 0)
249 return;
250
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));
254 }
255 """
256
257 i = 0
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 && '
263
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'
269
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 + '));'
275 print ' }'
276 print
277 i = i + 1
278 print '}'
279