• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 #include "main/mtypes.h"
47 #include "program/program.h"
48 
49 class dead_variable_visitor : public ir_hierarchical_visitor {
50 public:
dead_variable_visitor()51    dead_variable_visitor()
52    {
53       variables = _mesa_pointer_set_create(NULL);
54    }
55 
~dead_variable_visitor()56    virtual ~dead_variable_visitor()
57    {
58       _mesa_set_destroy(variables, NULL);
59    }
60 
visit(ir_variable * ir)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 
visit(ir_dereference_variable * ir)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 
remove_dead_variables()87    void remove_dead_variables()
88    {
89       set_foreach(variables, entry) {
90          ir_variable *ir = (ir_variable *) entry->key;
91 
92          assert(ir->ir_type == ir_type_variable);
93          ir->remove();
94       }
95    }
96 
97 private:
98    set *variables;
99 };
100 
101 static void
init_gl_program(struct gl_program * prog,bool is_arb_asm,gl_shader_stage stage)102 init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage)
103 {
104    prog->RefCount = 1;
105    prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
106    prog->is_arb_asm = is_arb_asm;
107    prog->info.stage = stage;
108 }
109 
110 static struct gl_program *
new_program(UNUSED struct gl_context * ctx,gl_shader_stage stage,UNUSED GLuint id,bool is_arb_asm)111 new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage,
112             UNUSED GLuint id, bool is_arb_asm)
113 {
114    struct gl_program *prog = rzalloc(NULL, struct gl_program);
115    init_gl_program(prog, is_arb_asm, stage);
116    return prog;
117 }
118 
119 static const struct standalone_options *options;
120 
121 static void
initialize_context(struct gl_context * ctx,gl_api api)122 initialize_context(struct gl_context *ctx, gl_api api)
123 {
124    initialize_context_to_defaults(ctx, api);
125    _mesa_glsl_builtin_functions_init_or_ref();
126 
127    /* The standalone compiler needs to claim support for almost
128     * everything in order to compile the built-in functions.
129     */
130    ctx->Const.GLSLVersion = options->glsl_version;
131    ctx->Extensions.ARB_ES3_compatibility = true;
132    ctx->Extensions.ARB_ES3_1_compatibility = true;
133    ctx->Extensions.ARB_ES3_2_compatibility = true;
134    ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
135    ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
136    ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
137    ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
138    ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
139    ctx->Const.MaxComputeWorkGroupSize[2] = 64;
140    ctx->Const.MaxComputeWorkGroupInvocations = 1024;
141    ctx->Const.MaxComputeSharedMemorySize = 32768;
142    ctx->Const.MaxComputeVariableGroupSize[0] = 512;
143    ctx->Const.MaxComputeVariableGroupSize[1] = 512;
144    ctx->Const.MaxComputeVariableGroupSize[2] = 64;
145    ctx->Const.MaxComputeVariableGroupInvocations = 512;
146    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
147    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
148    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
149    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
150    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
151    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
152    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
153    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
154    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
155 
156    switch (ctx->Const.GLSLVersion) {
157    case 100:
158       ctx->Const.MaxClipPlanes = 0;
159       ctx->Const.MaxCombinedTextureImageUnits = 8;
160       ctx->Const.MaxDrawBuffers = 2;
161       ctx->Const.MinProgramTexelOffset = 0;
162       ctx->Const.MaxProgramTexelOffset = 0;
163       ctx->Const.MaxLights = 0;
164       ctx->Const.MaxTextureCoordUnits = 0;
165       ctx->Const.MaxTextureUnits = 8;
166 
167       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
168       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
169       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
170       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
171       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
172       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
173 
174       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
175          ctx->Const.MaxCombinedTextureImageUnits;
176       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
177       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
178       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
179          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
180       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
181 
182       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
183       break;
184    case 110:
185    case 120:
186       ctx->Const.MaxClipPlanes = 6;
187       ctx->Const.MaxCombinedTextureImageUnits = 2;
188       ctx->Const.MaxDrawBuffers = 1;
189       ctx->Const.MinProgramTexelOffset = 0;
190       ctx->Const.MaxProgramTexelOffset = 0;
191       ctx->Const.MaxLights = 8;
192       ctx->Const.MaxTextureCoordUnits = 2;
193       ctx->Const.MaxTextureUnits = 2;
194 
195       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
196       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
197       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
198       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
199       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
200       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
201 
202       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
203          ctx->Const.MaxCombinedTextureImageUnits;
204       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
205       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
206       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
207          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
208       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
209 
210       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
211       break;
212    case 130:
213    case 140:
214       ctx->Const.MaxClipPlanes = 8;
215       ctx->Const.MaxCombinedTextureImageUnits = 16;
216       ctx->Const.MaxDrawBuffers = 8;
217       ctx->Const.MinProgramTexelOffset = -8;
218       ctx->Const.MaxProgramTexelOffset = 7;
219       ctx->Const.MaxLights = 8;
220       ctx->Const.MaxTextureCoordUnits = 8;
221       ctx->Const.MaxTextureUnits = 2;
222       ctx->Const.MaxUniformBufferBindings = 84;
223       ctx->Const.MaxVertexStreams = 4;
224       ctx->Const.MaxTransformFeedbackBuffers = 4;
225 
226       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
227       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
228       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
229       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
230       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
231       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
232 
233       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
234       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
235       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
236       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
237          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
238       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
239 
240       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
241       break;
242    case 150:
243    case 330:
244    case 400:
245    case 410:
246    case 420:
247    case 430:
248    case 440:
249    case 450:
250    case 460:
251       ctx->Const.MaxClipPlanes = 8;
252       ctx->Const.MaxDrawBuffers = 8;
253       ctx->Const.MinProgramTexelOffset = -8;
254       ctx->Const.MaxProgramTexelOffset = 7;
255       ctx->Const.MaxLights = 8;
256       ctx->Const.MaxTextureCoordUnits = 8;
257       ctx->Const.MaxTextureUnits = 2;
258       ctx->Const.MaxUniformBufferBindings = 84;
259       ctx->Const.MaxVertexStreams = 4;
260       ctx->Const.MaxTransformFeedbackBuffers = 4;
261       ctx->Const.MaxShaderStorageBufferBindings = 4;
262       ctx->Const.MaxShaderStorageBlockSize = 4096;
263       ctx->Const.MaxAtomicBufferBindings = 4;
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       ctx->Const.MaxUniformBufferBindings = 84;
306       ctx->Const.MaxVertexStreams = 4;
307       ctx->Const.MaxTransformFeedbackBuffers = 4;
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 *
load_text_file(void * ctx,const char * file_name)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 static void
compile_shader(struct gl_context * ctx,struct gl_shader * shader)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,
387                              options->dump_hir, true);
388 
389    /* Print out the resulting IR */
390    if (!state->error && options->dump_lir) {
391       _mesa_print_ir(stdout, shader->ir, state);
392    }
393 
394    return;
395 }
396 
397 extern "C" struct gl_shader_program *
standalone_compile_shader(const struct standalone_options * _options,unsigned num_files,char * const * files,struct gl_context * ctx)398 standalone_compile_shader(const struct standalone_options *_options,
399       unsigned num_files, char* const* files, struct gl_context *ctx)
400 {
401    int status = EXIT_SUCCESS;
402    bool glsl_es = false;
403 
404    options = _options;
405 
406    switch (options->glsl_version) {
407    case 100:
408    case 300:
409       glsl_es = true;
410       break;
411    case 110:
412    case 120:
413    case 130:
414    case 140:
415    case 150:
416    case 330:
417    case 400:
418    case 410:
419    case 420:
420    case 430:
421    case 440:
422    case 450:
423    case 460:
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    if (options->lower_precision) {
438       for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) {
439          struct gl_shader_compiler_options *options =
440             &ctx->Const.ShaderCompilerOptions[i];
441          options->LowerPrecisionFloat16 = true;
442          options->LowerPrecisionInt16 = true;
443          options->LowerPrecisionDerivatives = true;
444          options->LowerPrecisionConstants = true;
445       }
446    }
447 
448    struct gl_shader_program *whole_program;
449 
450    whole_program = rzalloc (NULL, struct gl_shader_program);
451    assert(whole_program != NULL);
452    whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
453    assert(whole_program->data != NULL);
454    whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
455 
456    /* Created just to avoid segmentation faults */
457    whole_program->AttributeBindings = new string_to_uint_map;
458    whole_program->FragDataBindings = new string_to_uint_map;
459    whole_program->FragDataIndexBindings = new string_to_uint_map;
460 
461    for (unsigned i = 0; i < num_files; i++) {
462       whole_program->Shaders =
463             reralloc(whole_program, whole_program->Shaders,
464                   struct gl_shader *, whole_program->NumShaders + 1);
465       assert(whole_program->Shaders != NULL);
466 
467       struct gl_shader *shader = rzalloc(whole_program, gl_shader);
468 
469       whole_program->Shaders[whole_program->NumShaders] = shader;
470       whole_program->NumShaders++;
471 
472       const unsigned len = strlen(files[i]);
473       if (len < 6)
474          goto fail;
475 
476       const char *const ext = & files[i][len - 5];
477       /* TODO add support to read a .shader_test */
478       if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
479 	 shader->Type = GL_VERTEX_SHADER;
480       else if (strncmp(".tesc", ext, 5) == 0)
481 	 shader->Type = GL_TESS_CONTROL_SHADER;
482       else if (strncmp(".tese", ext, 5) == 0)
483 	 shader->Type = GL_TESS_EVALUATION_SHADER;
484       else if (strncmp(".geom", ext, 5) == 0)
485 	 shader->Type = GL_GEOMETRY_SHADER;
486       else if (strncmp(".frag", ext, 5) == 0)
487 	 shader->Type = GL_FRAGMENT_SHADER;
488       else if (strncmp(".comp", ext, 5) == 0)
489          shader->Type = GL_COMPUTE_SHADER;
490       else
491          goto fail;
492       shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
493 
494       shader->Source = load_text_file(whole_program, files[i]);
495       if (shader->Source == NULL) {
496          printf("File \"%s\" does not exist.\n", files[i]);
497          exit(EXIT_FAILURE);
498       }
499 
500       compile_shader(ctx, shader);
501 
502       if (strlen(shader->InfoLog) > 0) {
503          if (!options->just_log)
504             printf("Info log for %s:\n", files[i]);
505 
506          printf("%s", shader->InfoLog);
507          if (!options->just_log)
508             printf("\n");
509       }
510 
511       if (!shader->CompileStatus) {
512          status = EXIT_FAILURE;
513          break;
514       }
515    }
516 
517    if (status == EXIT_SUCCESS) {
518       _mesa_clear_shader_program_data(ctx, whole_program);
519 
520       if (options->do_link)  {
521          link_shaders(ctx, whole_program);
522       } else {
523          const gl_shader_stage stage = whole_program->Shaders[0]->Stage;
524 
525          whole_program->data->LinkStatus = LINKING_SUCCESS;
526          whole_program->_LinkedShaders[stage] =
527             link_intrastage_shaders(whole_program /* mem_ctx */,
528                                     ctx,
529                                     whole_program,
530                                     whole_program->Shaders,
531                                     1,
532                                     true);
533 
534          /* Par-linking can fail, for example, if there are undefined external
535           * references.
536           */
537          if (whole_program->_LinkedShaders[stage] != NULL) {
538             assert(whole_program->data->LinkStatus);
539 
540             struct gl_shader_compiler_options *const compiler_options =
541                &ctx->Const.ShaderCompilerOptions[stage];
542 
543             exec_list *const ir =
544                whole_program->_LinkedShaders[stage]->ir;
545 
546             bool progress;
547             do {
548                progress = do_function_inlining(ir);
549 
550                progress = do_common_optimization(ir,
551                                                  false,
552                                                  false,
553                                                  compiler_options,
554                                                  true)
555                   && progress;
556             } while(progress);
557          }
558       }
559 
560       status = (whole_program->data->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
561 
562       if (strlen(whole_program->data->InfoLog) > 0) {
563          printf("\n");
564          if (!options->just_log)
565             printf("Info log for linking:\n");
566          printf("%s", whole_program->data->InfoLog);
567          if (!options->just_log)
568             printf("\n");
569       }
570 
571       for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
572          struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
573 
574          if (!shader)
575             continue;
576 
577          add_neg_to_sub_visitor v;
578          visit_list_elements(&v, shader->ir);
579 
580          dead_variable_visitor dv;
581          visit_list_elements(&dv, shader->ir);
582          dv.remove_dead_variables();
583       }
584 
585       if (options->dump_builder) {
586          for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
587             struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
588 
589             if (!shader)
590                continue;
591 
592             _mesa_print_builder_for_ir(stdout, shader->ir);
593          }
594       }
595    }
596 
597    return whole_program;
598 
599 fail:
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    ralloc_free(whole_program);
606    return NULL;
607 }
608 
609 extern "C" void
standalone_compiler_cleanup(struct gl_shader_program * whole_program)610 standalone_compiler_cleanup(struct gl_shader_program *whole_program)
611 {
612    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
613       if (whole_program->_LinkedShaders[i])
614          ralloc_free(whole_program->_LinkedShaders[i]->Program);
615    }
616 
617    delete whole_program->AttributeBindings;
618    delete whole_program->FragDataBindings;
619    delete whole_program->FragDataIndexBindings;
620 
621    ralloc_free(whole_program);
622    _mesa_glsl_builtin_functions_decref();
623 }
624