glsl: Mark functions static
[mesa.git] / src / compiler / glsl / standalone.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 standalone.cpp
26 *
27 * Standalone compiler helper lib. Used by standalone glsl_compiler and
28 * also available to drivers to implement their own standalone compiler
29 * with driver backend.
30 */
31
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "ir_optimization.h"
35 #include "program.h"
36 #include "loop_analysis.h"
37 #include "standalone_scaffolding.h"
38 #include "standalone.h"
39 #include "util/string_to_uint_map.h"
40 #include "util/set.h"
41 #include "linker.h"
42 #include "glsl_parser_extras.h"
43 #include "ir_builder_print_visitor.h"
44 #include "builtin_functions.h"
45 #include "opt_add_neg_to_sub.h"
46
47 class dead_variable_visitor : public ir_hierarchical_visitor {
48 public:
49 dead_variable_visitor()
50 {
51 variables = _mesa_set_create(NULL,
52 _mesa_hash_pointer,
53 _mesa_key_pointer_equal);
54 }
55
56 virtual ~dead_variable_visitor()
57 {
58 _mesa_set_destroy(variables, NULL);
59 }
60
61 virtual ir_visitor_status visit(ir_variable *ir)
62 {
63 /* If the variable is auto or temp, add it to the set of variables that
64 * are candidates for removal.
65 */
66 if (ir->data.mode != ir_var_auto && ir->data.mode != ir_var_temporary)
67 return visit_continue;
68
69 _mesa_set_add(variables, ir);
70
71 return visit_continue;
72 }
73
74 virtual ir_visitor_status visit(ir_dereference_variable *ir)
75 {
76 struct set_entry *entry = _mesa_set_search(variables, ir->var);
77
78 /* If a variable is dereferenced at all, remove it from the set of
79 * variables that are candidates for removal.
80 */
81 if (entry != NULL)
82 _mesa_set_remove(variables, entry);
83
84 return visit_continue;
85 }
86
87 void remove_dead_variables()
88 {
89 struct set_entry *entry;
90
91 set_foreach(variables, entry) {
92 ir_variable *ir = (ir_variable *) entry->key;
93
94 assert(ir->ir_type == ir_type_variable);
95 ir->remove();
96 }
97 }
98
99 private:
100 set *variables;
101 };
102
103 static void
104 init_gl_program(struct gl_program *prog, GLenum target, bool is_arb_asm)
105 {
106 prog->RefCount = 1;
107 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
108 prog->is_arb_asm = is_arb_asm;
109 }
110
111 static struct gl_program *
112 new_program(struct gl_context *ctx, GLenum target, GLuint id, bool is_arb_asm)
113 {
114 switch (target) {
115 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
116 case GL_GEOMETRY_PROGRAM_NV:
117 case GL_TESS_CONTROL_PROGRAM_NV:
118 case GL_TESS_EVALUATION_PROGRAM_NV:
119 case GL_FRAGMENT_PROGRAM_ARB:
120 case GL_COMPUTE_PROGRAM_NV: {
121 struct gl_program *prog = rzalloc(NULL, struct gl_program);
122 init_gl_program(prog, target, is_arb_asm);
123 return prog;
124 }
125 default:
126 printf("bad target in new_program\n");
127 return NULL;
128 }
129 }
130
131 static const struct standalone_options *options;
132
133 static void
134 initialize_context(struct gl_context *ctx, gl_api api)
135 {
136 initialize_context_to_defaults(ctx, api);
137
138 /* The standalone compiler needs to claim support for almost
139 * everything in order to compile the built-in functions.
140 */
141 ctx->Const.GLSLVersion = options->glsl_version;
142 ctx->Extensions.ARB_ES3_compatibility = true;
143 ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
144 ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
145 ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
146 ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
147 ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
148 ctx->Const.MaxComputeWorkGroupSize[2] = 64;
149 ctx->Const.MaxComputeWorkGroupInvocations = 1024;
150 ctx->Const.MaxComputeSharedMemorySize = 32768;
151 ctx->Const.MaxComputeVariableGroupSize[0] = 512;
152 ctx->Const.MaxComputeVariableGroupSize[1] = 512;
153 ctx->Const.MaxComputeVariableGroupSize[2] = 64;
154 ctx->Const.MaxComputeVariableGroupInvocations = 512;
155 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
156 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
157 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
158 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
159 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
160 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
161 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
162 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
163 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
164
165 switch (ctx->Const.GLSLVersion) {
166 case 100:
167 ctx->Const.MaxClipPlanes = 0;
168 ctx->Const.MaxCombinedTextureImageUnits = 8;
169 ctx->Const.MaxDrawBuffers = 2;
170 ctx->Const.MinProgramTexelOffset = 0;
171 ctx->Const.MaxProgramTexelOffset = 0;
172 ctx->Const.MaxLights = 0;
173 ctx->Const.MaxTextureCoordUnits = 0;
174 ctx->Const.MaxTextureUnits = 8;
175
176 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
177 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
178 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
179 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
180 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
181 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
182
183 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
184 ctx->Const.MaxCombinedTextureImageUnits;
185 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
186 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
187 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
188 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
189 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
190
191 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
192 break;
193 case 110:
194 case 120:
195 ctx->Const.MaxClipPlanes = 6;
196 ctx->Const.MaxCombinedTextureImageUnits = 2;
197 ctx->Const.MaxDrawBuffers = 1;
198 ctx->Const.MinProgramTexelOffset = 0;
199 ctx->Const.MaxProgramTexelOffset = 0;
200 ctx->Const.MaxLights = 8;
201 ctx->Const.MaxTextureCoordUnits = 2;
202 ctx->Const.MaxTextureUnits = 2;
203
204 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
205 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
206 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
207 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
208 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
209 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
210
211 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
212 ctx->Const.MaxCombinedTextureImageUnits;
213 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
214 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
215 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
216 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
217 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
218
219 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
220 break;
221 case 130:
222 case 140:
223 ctx->Const.MaxClipPlanes = 8;
224 ctx->Const.MaxCombinedTextureImageUnits = 16;
225 ctx->Const.MaxDrawBuffers = 8;
226 ctx->Const.MinProgramTexelOffset = -8;
227 ctx->Const.MaxProgramTexelOffset = 7;
228 ctx->Const.MaxLights = 8;
229 ctx->Const.MaxTextureCoordUnits = 8;
230 ctx->Const.MaxTextureUnits = 2;
231
232 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
233 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
234 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
235 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
236 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
237 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
238
239 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
240 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
241 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
242 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
243 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
244 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
245
246 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
247 break;
248 case 150:
249 case 330:
250 case 400:
251 case 410:
252 case 420:
253 case 430:
254 case 440:
255 case 450:
256 case 460:
257 ctx->Const.MaxClipPlanes = 8;
258 ctx->Const.MaxDrawBuffers = 8;
259 ctx->Const.MinProgramTexelOffset = -8;
260 ctx->Const.MaxProgramTexelOffset = 7;
261 ctx->Const.MaxLights = 8;
262 ctx->Const.MaxTextureCoordUnits = 8;
263 ctx->Const.MaxTextureUnits = 2;
264
265 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
266 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
267 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
268 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
269 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
270 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
271
272 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
273 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
274 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024;
275 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
276 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
277 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
278
279 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
280 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
281 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
282 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
283 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
284 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
285
286 ctx->Const.MaxCombinedTextureImageUnits =
287 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
288 + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
289 + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
290
291 ctx->Const.MaxGeometryOutputVertices = 256;
292 ctx->Const.MaxGeometryTotalOutputComponents = 1024;
293
294 ctx->Const.MaxVarying = 60 / 4;
295 break;
296 case 300:
297 ctx->Const.MaxClipPlanes = 8;
298 ctx->Const.MaxCombinedTextureImageUnits = 32;
299 ctx->Const.MaxDrawBuffers = 4;
300 ctx->Const.MinProgramTexelOffset = -8;
301 ctx->Const.MaxProgramTexelOffset = 7;
302 ctx->Const.MaxLights = 0;
303 ctx->Const.MaxTextureCoordUnits = 0;
304 ctx->Const.MaxTextureUnits = 0;
305
306 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
307 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
308 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
309 ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
310 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
311 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
312
313 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
314 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
315 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224;
316 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
317 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
318
319 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
320 break;
321 }
322
323 ctx->Const.GenerateTemporaryNames = true;
324 ctx->Const.MaxPatchVertices = 32;
325
326 /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
327 ctx->Const.MaxUserAssignableUniformLocations =
328 4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
329
330 ctx->Driver.NewProgram = new_program;
331 }
332
333 /* Returned string will have 'ctx' as its ralloc owner. */
334 static char *
335 load_text_file(void *ctx, const char *file_name)
336 {
337 char *text = NULL;
338 size_t size;
339 size_t total_read = 0;
340 FILE *fp = fopen(file_name, "rb");
341
342 if (!fp) {
343 return NULL;
344 }
345
346 fseek(fp, 0L, SEEK_END);
347 size = ftell(fp);
348 fseek(fp, 0L, SEEK_SET);
349
350 text = (char *) ralloc_size(ctx, size + 1);
351 if (text != NULL) {
352 do {
353 size_t bytes = fread(text + total_read,
354 1, size - total_read, fp);
355 if (bytes < size - total_read) {
356 free(text);
357 text = NULL;
358 goto error;
359 }
360
361 if (bytes == 0) {
362 break;
363 }
364
365 total_read += bytes;
366 } while (total_read < size);
367
368 text[total_read] = '\0';
369 error:;
370 }
371
372 fclose(fp);
373
374 return text;
375 }
376
377 static void
378 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
379 {
380 struct _mesa_glsl_parse_state *state =
381 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
382
383 _mesa_glsl_compile_shader(ctx, shader, options->dump_ast,
384 options->dump_hir, true);
385
386 /* Print out the resulting IR */
387 if (!state->error && options->dump_lir) {
388 _mesa_print_ir(stdout, shader->ir, state);
389 }
390
391 return;
392 }
393
394 extern "C" struct gl_shader_program *
395 standalone_compile_shader(const struct standalone_options *_options,
396 unsigned num_files, char* const* files)
397 {
398 int status = EXIT_SUCCESS;
399 static struct gl_context local_ctx;
400 struct gl_context *ctx = &local_ctx;
401 bool glsl_es = false;
402
403 options = _options;
404
405 switch (options->glsl_version) {
406 case 100:
407 case 300:
408 glsl_es = true;
409 break;
410 case 110:
411 case 120:
412 case 130:
413 case 140:
414 case 150:
415 case 330:
416 case 400:
417 case 410:
418 case 420:
419 case 430:
420 case 440:
421 case 450:
422 case 460:
423 glsl_es = false;
424 break;
425 default:
426 fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version);
427 return NULL;
428 }
429
430 if (glsl_es) {
431 initialize_context(ctx, API_OPENGLES2);
432 } else {
433 initialize_context(ctx, options->glsl_version > 130 ? API_OPENGL_CORE : API_OPENGL_COMPAT);
434 }
435
436 struct gl_shader_program *whole_program;
437
438 whole_program = rzalloc (NULL, struct gl_shader_program);
439 assert(whole_program != NULL);
440 whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
441 assert(whole_program->data != NULL);
442 whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
443
444 /* Created just to avoid segmentation faults */
445 whole_program->AttributeBindings = new string_to_uint_map;
446 whole_program->FragDataBindings = new string_to_uint_map;
447 whole_program->FragDataIndexBindings = new string_to_uint_map;
448
449 for (unsigned i = 0; i < num_files; i++) {
450 whole_program->Shaders =
451 reralloc(whole_program, whole_program->Shaders,
452 struct gl_shader *, whole_program->NumShaders + 1);
453 assert(whole_program->Shaders != NULL);
454
455 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
456
457 whole_program->Shaders[whole_program->NumShaders] = shader;
458 whole_program->NumShaders++;
459
460 const unsigned len = strlen(files[i]);
461 if (len < 6)
462 goto fail;
463
464 const char *const ext = & files[i][len - 5];
465 /* TODO add support to read a .shader_test */
466 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
467 shader->Type = GL_VERTEX_SHADER;
468 else if (strncmp(".tesc", ext, 5) == 0)
469 shader->Type = GL_TESS_CONTROL_SHADER;
470 else if (strncmp(".tese", ext, 5) == 0)
471 shader->Type = GL_TESS_EVALUATION_SHADER;
472 else if (strncmp(".geom", ext, 5) == 0)
473 shader->Type = GL_GEOMETRY_SHADER;
474 else if (strncmp(".frag", ext, 5) == 0)
475 shader->Type = GL_FRAGMENT_SHADER;
476 else if (strncmp(".comp", ext, 5) == 0)
477 shader->Type = GL_COMPUTE_SHADER;
478 else
479 goto fail;
480 shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
481
482 shader->Source = load_text_file(whole_program, files[i]);
483 if (shader->Source == NULL) {
484 printf("File \"%s\" does not exist.\n", files[i]);
485 exit(EXIT_FAILURE);
486 }
487
488 compile_shader(ctx, shader);
489
490 if (strlen(shader->InfoLog) > 0) {
491 if (!options->just_log)
492 printf("Info log for %s:\n", files[i]);
493
494 printf("%s", shader->InfoLog);
495 if (!options->just_log)
496 printf("\n");
497 }
498
499 if (!shader->CompileStatus) {
500 status = EXIT_FAILURE;
501 break;
502 }
503 }
504
505 if (status == EXIT_SUCCESS) {
506 _mesa_clear_shader_program_data(ctx, whole_program);
507
508 if (options->do_link) {
509 link_shaders(ctx, whole_program);
510 } else {
511 const gl_shader_stage stage = whole_program->Shaders[0]->Stage;
512
513 whole_program->data->LinkStatus = linking_success;
514 whole_program->_LinkedShaders[stage] =
515 link_intrastage_shaders(whole_program /* mem_ctx */,
516 ctx,
517 whole_program,
518 whole_program->Shaders,
519 1,
520 true);
521
522 /* Par-linking can fail, for example, if there are undefined external
523 * references.
524 */
525 if (whole_program->_LinkedShaders[stage] != NULL) {
526 assert(whole_program->data->LinkStatus);
527
528 struct gl_shader_compiler_options *const compiler_options =
529 &ctx->Const.ShaderCompilerOptions[stage];
530
531 exec_list *const ir =
532 whole_program->_LinkedShaders[stage]->ir;
533
534 bool progress;
535 do {
536 progress = do_function_inlining(ir);
537
538 progress = do_common_optimization(ir,
539 false,
540 false,
541 compiler_options,
542 true)
543 && progress;
544 } while(progress);
545 }
546 }
547
548 status = (whole_program->data->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
549
550 if (strlen(whole_program->data->InfoLog) > 0) {
551 printf("\n");
552 if (!options->just_log)
553 printf("Info log for linking:\n");
554 printf("%s", whole_program->data->InfoLog);
555 if (!options->just_log)
556 printf("\n");
557 }
558
559 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
560 struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
561
562 if (!shader)
563 continue;
564
565 add_neg_to_sub_visitor v;
566 visit_list_elements(&v, shader->ir);
567
568 dead_variable_visitor dv;
569 visit_list_elements(&dv, shader->ir);
570 dv.remove_dead_variables();
571 }
572
573 if (options->dump_builder) {
574 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
575 struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
576
577 if (!shader)
578 continue;
579
580 _mesa_print_builder_for_ir(stdout, shader->ir);
581 }
582 }
583 }
584
585 return whole_program;
586
587 fail:
588 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
589 if (whole_program->_LinkedShaders[i])
590 ralloc_free(whole_program->_LinkedShaders[i]->Program);
591 }
592
593 ralloc_free(whole_program);
594 return NULL;
595 }
596
597 extern "C" void
598 standalone_compiler_cleanup(struct gl_shader_program *whole_program)
599 {
600 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
601 if (whole_program->_LinkedShaders[i])
602 ralloc_free(whole_program->_LinkedShaders[i]->Program);
603 }
604
605 delete whole_program->AttributeBindings;
606 delete whole_program->FragDataBindings;
607 delete whole_program->FragDataIndexBindings;
608
609 ralloc_free(whole_program);
610 _mesa_glsl_release_types();
611 _mesa_glsl_release_builtin_functions();
612 }