glsl: Add an ir_variable::reinit_interface_type() function.
[mesa.git] / src / glsl / main.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <getopt.h>
24
25 /** @file main.cpp
26 *
27 * This file is the main() routine and scaffolding for producing
28 * builtin_compiler (which doesn't include builtins itself and is used
29 * to generate the profile information for builtin_function.cpp), and
30 * for glsl_compiler (which does include builtins and can be used to
31 * offline compile GLSL code and examine the resulting GLSL IR.
32 */
33
34 #include "ast.h"
35 #include "glsl_parser_extras.h"
36 #include "ir_optimization.h"
37 #include "program.h"
38 #include "loop_analysis.h"
39 #include "standalone_scaffolding.h"
40
41 static int glsl_version = 330;
42
43 static void
44 initialize_context(struct gl_context *ctx, gl_api api)
45 {
46 initialize_context_to_defaults(ctx, api);
47
48 /* The standalone compiler needs to claim support for almost
49 * everything in order to compile the built-in functions.
50 */
51 ctx->Const.GLSLVersion = glsl_version;
52 ctx->Extensions.ARB_ES3_compatibility = true;
53
54 switch (ctx->Const.GLSLVersion) {
55 case 100:
56 ctx->Const.MaxClipPlanes = 0;
57 ctx->Const.MaxCombinedTextureImageUnits = 8;
58 ctx->Const.MaxDrawBuffers = 2;
59 ctx->Const.MinProgramTexelOffset = 0;
60 ctx->Const.MaxProgramTexelOffset = 0;
61 ctx->Const.MaxLights = 0;
62 ctx->Const.MaxTextureCoordUnits = 0;
63 ctx->Const.MaxTextureUnits = 8;
64
65 ctx->Const.VertexProgram.MaxAttribs = 8;
66 ctx->Const.VertexProgram.MaxTextureImageUnits = 0;
67 ctx->Const.VertexProgram.MaxUniformComponents = 128 * 4;
68 ctx->Const.VertexProgram.MaxInputComponents = 0; /* not used */
69 ctx->Const.VertexProgram.MaxOutputComponents = 32;
70
71 ctx->Const.FragmentProgram.MaxTextureImageUnits =
72 ctx->Const.MaxCombinedTextureImageUnits;
73 ctx->Const.FragmentProgram.MaxUniformComponents = 16 * 4;
74 ctx->Const.FragmentProgram.MaxInputComponents =
75 ctx->Const.VertexProgram.MaxOutputComponents;
76 ctx->Const.FragmentProgram.MaxOutputComponents = 0; /* not used */
77
78 ctx->Const.MaxVarying = ctx->Const.VertexProgram.MaxOutputComponents / 4;
79 break;
80 case 110:
81 case 120:
82 ctx->Const.MaxClipPlanes = 6;
83 ctx->Const.MaxCombinedTextureImageUnits = 2;
84 ctx->Const.MaxDrawBuffers = 1;
85 ctx->Const.MinProgramTexelOffset = 0;
86 ctx->Const.MaxProgramTexelOffset = 0;
87 ctx->Const.MaxLights = 8;
88 ctx->Const.MaxTextureCoordUnits = 2;
89 ctx->Const.MaxTextureUnits = 2;
90
91 ctx->Const.VertexProgram.MaxAttribs = 16;
92 ctx->Const.VertexProgram.MaxTextureImageUnits = 0;
93 ctx->Const.VertexProgram.MaxUniformComponents = 512;
94 ctx->Const.VertexProgram.MaxInputComponents = 0; /* not used */
95 ctx->Const.VertexProgram.MaxOutputComponents = 32;
96
97 ctx->Const.FragmentProgram.MaxTextureImageUnits =
98 ctx->Const.MaxCombinedTextureImageUnits;
99 ctx->Const.FragmentProgram.MaxUniformComponents = 64;
100 ctx->Const.FragmentProgram.MaxInputComponents =
101 ctx->Const.VertexProgram.MaxOutputComponents;
102 ctx->Const.FragmentProgram.MaxOutputComponents = 0; /* not used */
103
104 ctx->Const.MaxVarying = ctx->Const.VertexProgram.MaxOutputComponents / 4;
105 break;
106 case 130:
107 case 140:
108 ctx->Const.MaxClipPlanes = 8;
109 ctx->Const.MaxCombinedTextureImageUnits = 16;
110 ctx->Const.MaxDrawBuffers = 8;
111 ctx->Const.MinProgramTexelOffset = -8;
112 ctx->Const.MaxProgramTexelOffset = 7;
113 ctx->Const.MaxLights = 8;
114 ctx->Const.MaxTextureCoordUnits = 8;
115 ctx->Const.MaxTextureUnits = 2;
116
117 ctx->Const.VertexProgram.MaxAttribs = 16;
118 ctx->Const.VertexProgram.MaxTextureImageUnits = 16;
119 ctx->Const.VertexProgram.MaxUniformComponents = 1024;
120 ctx->Const.VertexProgram.MaxInputComponents = 0; /* not used */
121 ctx->Const.VertexProgram.MaxOutputComponents = 64;
122
123 ctx->Const.FragmentProgram.MaxTextureImageUnits = 16;
124 ctx->Const.FragmentProgram.MaxUniformComponents = 1024;
125 ctx->Const.FragmentProgram.MaxInputComponents =
126 ctx->Const.VertexProgram.MaxOutputComponents;
127 ctx->Const.FragmentProgram.MaxOutputComponents = 0; /* not used */
128
129 ctx->Const.MaxVarying = ctx->Const.VertexProgram.MaxOutputComponents / 4;
130 break;
131 case 150:
132 case 330:
133 ctx->Const.MaxClipPlanes = 8;
134 ctx->Const.MaxDrawBuffers = 8;
135 ctx->Const.MinProgramTexelOffset = -8;
136 ctx->Const.MaxProgramTexelOffset = 7;
137 ctx->Const.MaxLights = 8;
138 ctx->Const.MaxTextureCoordUnits = 8;
139 ctx->Const.MaxTextureUnits = 2;
140
141 ctx->Const.VertexProgram.MaxAttribs = 16;
142 ctx->Const.VertexProgram.MaxTextureImageUnits = 16;
143 ctx->Const.VertexProgram.MaxUniformComponents = 1024;
144 ctx->Const.VertexProgram.MaxInputComponents = 0; /* not used */
145 ctx->Const.VertexProgram.MaxOutputComponents = 64;
146
147 ctx->Const.GeometryProgram.MaxTextureImageUnits = 16;
148 ctx->Const.GeometryProgram.MaxUniformComponents = 1024;
149 ctx->Const.GeometryProgram.MaxInputComponents =
150 ctx->Const.VertexProgram.MaxOutputComponents;
151 ctx->Const.GeometryProgram.MaxOutputComponents = 128;
152
153 ctx->Const.FragmentProgram.MaxTextureImageUnits = 16;
154 ctx->Const.FragmentProgram.MaxUniformComponents = 1024;
155 ctx->Const.FragmentProgram.MaxInputComponents =
156 ctx->Const.GeometryProgram.MaxOutputComponents;
157 ctx->Const.FragmentProgram.MaxOutputComponents = 0; /* not used */
158
159 ctx->Const.MaxCombinedTextureImageUnits =
160 ctx->Const.VertexProgram.MaxTextureImageUnits
161 + ctx->Const.GeometryProgram.MaxTextureImageUnits
162 + ctx->Const.FragmentProgram.MaxTextureImageUnits;
163
164 ctx->Const.MaxGeometryOutputVertices = 256;
165 ctx->Const.MaxGeometryTotalOutputComponents = 1024;
166
167 // ctx->Const.MaxGeometryVaryingComponents = 64;
168
169 ctx->Const.MaxVarying = 60 / 4;
170 break;
171 case 300:
172 ctx->Const.MaxClipPlanes = 8;
173 ctx->Const.MaxCombinedTextureImageUnits = 32;
174 ctx->Const.MaxDrawBuffers = 4;
175 ctx->Const.MinProgramTexelOffset = -8;
176 ctx->Const.MaxProgramTexelOffset = 7;
177 ctx->Const.MaxLights = 0;
178 ctx->Const.MaxTextureCoordUnits = 0;
179 ctx->Const.MaxTextureUnits = 0;
180
181 ctx->Const.VertexProgram.MaxAttribs = 16;
182 ctx->Const.VertexProgram.MaxTextureImageUnits = 16;
183 ctx->Const.VertexProgram.MaxUniformComponents = 1024;
184 ctx->Const.VertexProgram.MaxInputComponents = 0; /* not used */
185 ctx->Const.VertexProgram.MaxOutputComponents = 16 * 4;
186
187 ctx->Const.FragmentProgram.MaxTextureImageUnits = 16;
188 ctx->Const.FragmentProgram.MaxUniformComponents = 224;
189 ctx->Const.FragmentProgram.MaxInputComponents = 15 * 4;
190 ctx->Const.FragmentProgram.MaxOutputComponents = 0; /* not used */
191
192 ctx->Const.MaxVarying = ctx->Const.FragmentProgram.MaxInputComponents / 4;
193 break;
194 }
195
196 ctx->Driver.NewShader = _mesa_new_shader;
197 }
198
199 /* Returned string will have 'ctx' as its ralloc owner. */
200 static char *
201 load_text_file(void *ctx, const char *file_name)
202 {
203 char *text = NULL;
204 size_t size;
205 size_t total_read = 0;
206 FILE *fp = fopen(file_name, "rb");
207
208 if (!fp) {
209 return NULL;
210 }
211
212 fseek(fp, 0L, SEEK_END);
213 size = ftell(fp);
214 fseek(fp, 0L, SEEK_SET);
215
216 text = (char *) ralloc_size(ctx, size + 1);
217 if (text != NULL) {
218 do {
219 size_t bytes = fread(text + total_read,
220 1, size - total_read, fp);
221 if (bytes < size - total_read) {
222 free(text);
223 text = NULL;
224 break;
225 }
226
227 if (bytes == 0) {
228 break;
229 }
230
231 total_read += bytes;
232 } while (total_read < size);
233
234 text[total_read] = '\0';
235 }
236
237 fclose(fp);
238
239 return text;
240 }
241
242 int dump_ast = 0;
243 int dump_hir = 0;
244 int dump_lir = 0;
245 int do_link = 0;
246
247 const struct option compiler_opts[] = {
248 { "dump-ast", no_argument, &dump_ast, 1 },
249 { "dump-hir", no_argument, &dump_hir, 1 },
250 { "dump-lir", no_argument, &dump_lir, 1 },
251 { "link", no_argument, &do_link, 1 },
252 { "version", required_argument, NULL, 'v' },
253 { NULL, 0, NULL, 0 }
254 };
255
256 /**
257 * \brief Print proper usage and exit with failure.
258 */
259 void
260 usage_fail(const char *name)
261 {
262
263 const char *header =
264 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
265 "\n"
266 "Possible options are:\n";
267 printf(header, name, name);
268 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
269 printf(" --%s\n", o->name);
270 }
271 exit(EXIT_FAILURE);
272 }
273
274
275 void
276 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
277 {
278 struct _mesa_glsl_parse_state *state =
279 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
280
281 _mesa_glsl_compile_shader(ctx, shader, dump_ast, dump_hir);
282
283 /* Print out the resulting IR */
284 if (!state->error && dump_lir) {
285 _mesa_print_ir(shader->ir, state);
286 }
287
288 return;
289 }
290
291 int
292 main(int argc, char **argv)
293 {
294 int status = EXIT_SUCCESS;
295 struct gl_context local_ctx;
296 struct gl_context *ctx = &local_ctx;
297 bool glsl_es = false;
298
299 int c;
300 int idx = 0;
301 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
302 switch (c) {
303 case 'v':
304 glsl_version = strtol(optarg, NULL, 10);
305 switch (glsl_version) {
306 case 100:
307 case 300:
308 glsl_es = true;
309 break;
310 case 110:
311 case 120:
312 case 130:
313 case 140:
314 case 150:
315 case 330:
316 glsl_es = false;
317 break;
318 default:
319 fprintf(stderr, "Unrecognized GLSL version `%s'\n", optarg);
320 usage_fail(argv[0]);
321 break;
322 }
323 break;
324 default:
325 break;
326 }
327 }
328
329
330 if (argc <= optind)
331 usage_fail(argv[0]);
332
333 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
334
335 struct gl_shader_program *whole_program;
336
337 whole_program = rzalloc (NULL, struct gl_shader_program);
338 assert(whole_program != NULL);
339 whole_program->InfoLog = ralloc_strdup(whole_program, "");
340
341 for (/* empty */; argc > optind; optind++) {
342 whole_program->Shaders =
343 reralloc(whole_program, whole_program->Shaders,
344 struct gl_shader *, whole_program->NumShaders + 1);
345 assert(whole_program->Shaders != NULL);
346
347 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
348
349 whole_program->Shaders[whole_program->NumShaders] = shader;
350 whole_program->NumShaders++;
351
352 const unsigned len = strlen(argv[optind]);
353 if (len < 6)
354 usage_fail(argv[0]);
355
356 const char *const ext = & argv[optind][len - 5];
357 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
358 shader->Type = GL_VERTEX_SHADER;
359 else if (strncmp(".geom", ext, 5) == 0)
360 shader->Type = GL_GEOMETRY_SHADER;
361 else if (strncmp(".frag", ext, 5) == 0)
362 shader->Type = GL_FRAGMENT_SHADER;
363 else
364 usage_fail(argv[0]);
365
366 shader->Source = load_text_file(whole_program, argv[optind]);
367 if (shader->Source == NULL) {
368 printf("File \"%s\" does not exist.\n", argv[optind]);
369 exit(EXIT_FAILURE);
370 }
371
372 compile_shader(ctx, shader);
373
374 if (strlen(shader->InfoLog) > 0)
375 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
376
377 if (!shader->CompileStatus) {
378 status = EXIT_FAILURE;
379 break;
380 }
381 }
382
383 if ((status == EXIT_SUCCESS) && do_link) {
384 link_shaders(ctx, whole_program);
385 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
386
387 if (strlen(whole_program->InfoLog) > 0)
388 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
389 }
390
391 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
392 ralloc_free(whole_program->_LinkedShaders[i]);
393
394 ralloc_free(whole_program);
395 _mesa_glsl_release_types();
396 _mesa_glsl_release_builtin_functions();
397
398 return status;
399 }