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