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