8bab7a981f2c1718d65b801256df502c9cecf75d
[mesa.git] / src / glsl / glsl_parser_extras.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 <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <assert.h>
27
28 extern "C" {
29 #include "main/core.h" /* for struct gl_context */
30 #include "main/context.h"
31 #include "main/shaderobj.h"
32 }
33
34 #include "ralloc.h"
35 #include "ast.h"
36 #include "glsl_parser_extras.h"
37 #include "glsl_parser.h"
38 #include "ir_optimization.h"
39 #include "loop_analysis.h"
40
41 /**
42 * Format a short human-readable description of the given GLSL version.
43 */
44 const char *
45 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
46 {
47 return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
48 version / 100, version % 100);
49 }
50
51
52 static unsigned known_desktop_glsl_versions[] =
53 { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430, 440 };
54
55
56 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
57 gl_shader_stage stage,
58 void *mem_ctx)
59 : ctx(_ctx), cs_input_local_size_specified(false), cs_input_local_size(),
60 switch_state()
61 {
62 assert(stage < MESA_SHADER_STAGES);
63 this->stage = stage;
64
65 this->scanner = NULL;
66 this->translation_unit.make_empty();
67 this->symbols = new(mem_ctx) glsl_symbol_table;
68
69 this->num_uniform_blocks = 0;
70 this->uniform_block_array_size = 0;
71 this->uniform_blocks = NULL;
72
73 this->info_log = ralloc_strdup(mem_ctx, "");
74 this->error = false;
75 this->loop_nesting_ast = NULL;
76
77 this->struct_specifier_depth = 0;
78
79 this->uses_builtin_functions = false;
80
81 /* Set default language version and extensions */
82 this->language_version = ctx->Const.ForceGLSLVersion ?
83 ctx->Const.ForceGLSLVersion : 110;
84 this->es_shader = false;
85 this->ARB_texture_rectangle_enable = true;
86
87 /* OpenGL ES 2.0 has different defaults from desktop GL. */
88 if (ctx->API == API_OPENGLES2) {
89 this->language_version = 100;
90 this->es_shader = true;
91 this->ARB_texture_rectangle_enable = false;
92 }
93
94 this->extensions = &ctx->Extensions;
95
96 this->Const.MaxLights = ctx->Const.MaxLights;
97 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
98 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
99 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
100 this->Const.MaxVertexAttribs = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs;
101 this->Const.MaxVertexUniformComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents;
102 this->Const.MaxVertexTextureImageUnits = ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits;
103 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
104 this->Const.MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
105 this->Const.MaxFragmentUniformComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents;
106 this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
107 this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
108
109 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
110
111 /* 1.50 constants */
112 this->Const.MaxVertexOutputComponents = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
113 this->Const.MaxGeometryInputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents;
114 this->Const.MaxGeometryOutputComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
115 this->Const.MaxFragmentInputComponents = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents;
116 this->Const.MaxGeometryTextureImageUnits = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits;
117 this->Const.MaxGeometryOutputVertices = ctx->Const.MaxGeometryOutputVertices;
118 this->Const.MaxGeometryTotalOutputComponents = ctx->Const.MaxGeometryTotalOutputComponents;
119 this->Const.MaxGeometryUniformComponents = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents;
120
121 this->Const.MaxVertexAtomicCounters = ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicCounters;
122 this->Const.MaxGeometryAtomicCounters = ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxAtomicCounters;
123 this->Const.MaxFragmentAtomicCounters = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAtomicCounters;
124 this->Const.MaxCombinedAtomicCounters = ctx->Const.MaxCombinedAtomicCounters;
125 this->Const.MaxAtomicBufferBindings = ctx->Const.MaxAtomicBufferBindings;
126
127 /* Compute shader constants */
128 for (unsigned i = 0; i < Elements(this->Const.MaxComputeWorkGroupCount); i++)
129 this->Const.MaxComputeWorkGroupCount[i] = ctx->Const.MaxComputeWorkGroupCount[i];
130 for (unsigned i = 0; i < Elements(this->Const.MaxComputeWorkGroupSize); i++)
131 this->Const.MaxComputeWorkGroupSize[i] = ctx->Const.MaxComputeWorkGroupSize[i];
132
133 this->current_function = NULL;
134 this->toplevel_ir = NULL;
135 this->found_return = false;
136 this->all_invariant = false;
137 this->user_structures = NULL;
138 this->num_user_structures = 0;
139
140 /* Populate the list of supported GLSL versions */
141 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
142 * the OpenGL 3.2 Core context is supported, this logic will need
143 * change. Older versions of GLSL are no longer supported
144 * outside the compatibility contexts of 3.x.
145 */
146 this->num_supported_versions = 0;
147 if (_mesa_is_desktop_gl(ctx)) {
148 for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {
149 if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {
150 this->supported_versions[this->num_supported_versions].ver
151 = known_desktop_glsl_versions[i];
152 this->supported_versions[this->num_supported_versions].es = false;
153 this->num_supported_versions++;
154 }
155 }
156 }
157 if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility) {
158 this->supported_versions[this->num_supported_versions].ver = 100;
159 this->supported_versions[this->num_supported_versions].es = true;
160 this->num_supported_versions++;
161 }
162 if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
163 this->supported_versions[this->num_supported_versions].ver = 300;
164 this->supported_versions[this->num_supported_versions].es = true;
165 this->num_supported_versions++;
166 }
167 assert(this->num_supported_versions
168 <= ARRAY_SIZE(this->supported_versions));
169
170 /* Create a string for use in error messages to tell the user which GLSL
171 * versions are supported.
172 */
173 char *supported = ralloc_strdup(this, "");
174 for (unsigned i = 0; i < this->num_supported_versions; i++) {
175 unsigned ver = this->supported_versions[i].ver;
176 const char *const prefix = (i == 0)
177 ? ""
178 : ((i == this->num_supported_versions - 1) ? ", and " : ", ");
179 const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";
180
181 ralloc_asprintf_append(& supported, "%s%u.%02u%s",
182 prefix,
183 ver / 100, ver % 100,
184 suffix);
185 }
186
187 this->supported_version_string = supported;
188
189 if (ctx->Const.ForceGLSLExtensionsWarn)
190 _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
191
192 this->default_uniform_qualifier = new(this) ast_type_qualifier();
193 this->default_uniform_qualifier->flags.q.shared = 1;
194 this->default_uniform_qualifier->flags.q.column_major = 1;
195
196 this->gs_input_prim_type_specified = false;
197 this->gs_input_prim_type = GL_POINTS;
198 this->gs_input_size = 0;
199 this->out_qualifier = new(this) ast_type_qualifier();
200 this->early_fragment_tests = false;
201 memset(this->atomic_counter_offsets, 0,
202 sizeof(this->atomic_counter_offsets));
203 }
204
205 /**
206 * Determine whether the current GLSL version is sufficiently high to support
207 * a certain feature, and generate an error message if it isn't.
208 *
209 * \param required_glsl_version and \c required_glsl_es_version are
210 * interpreted as they are in _mesa_glsl_parse_state::is_version().
211 *
212 * \param locp is the parser location where the error should be reported.
213 *
214 * \param fmt (and additional arguments) constitute a printf-style error
215 * message to report if the version check fails. Information about the
216 * current and required GLSL versions will be appended. So, for example, if
217 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
218 * "foo unsupported") is called, the error message will be "foo unsupported in
219 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
220 */
221 bool
222 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
223 unsigned required_glsl_es_version,
224 YYLTYPE *locp, const char *fmt, ...)
225 {
226 if (this->is_version(required_glsl_version, required_glsl_es_version))
227 return true;
228
229 va_list args;
230 va_start(args, fmt);
231 char *problem = ralloc_vasprintf(this, fmt, args);
232 va_end(args);
233 const char *glsl_version_string
234 = glsl_compute_version_string(this, false, required_glsl_version);
235 const char *glsl_es_version_string
236 = glsl_compute_version_string(this, true, required_glsl_es_version);
237 const char *requirement_string = "";
238 if (required_glsl_version && required_glsl_es_version) {
239 requirement_string = ralloc_asprintf(this, " (%s or %s required)",
240 glsl_version_string,
241 glsl_es_version_string);
242 } else if (required_glsl_version) {
243 requirement_string = ralloc_asprintf(this, " (%s required)",
244 glsl_version_string);
245 } else if (required_glsl_es_version) {
246 requirement_string = ralloc_asprintf(this, " (%s required)",
247 glsl_es_version_string);
248 }
249 _mesa_glsl_error(locp, this, "%s in %s%s",
250 problem, this->get_version_string(),
251 requirement_string);
252
253 return false;
254 }
255
256 /**
257 * Process a GLSL #version directive.
258 *
259 * \param version is the integer that follows the #version token.
260 *
261 * \param ident is a string identifier that follows the integer, if any is
262 * present. Otherwise NULL.
263 */
264 void
265 _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
266 const char *ident)
267 {
268 bool es_token_present = false;
269 if (ident) {
270 if (strcmp(ident, "es") == 0) {
271 es_token_present = true;
272 } else if (version >= 150) {
273 if (strcmp(ident, "core") == 0) {
274 /* Accept the token. There's no need to record that this is
275 * a core profile shader since that's the only profile we support.
276 */
277 } else if (strcmp(ident, "compatibility") == 0) {
278 _mesa_glsl_error(locp, this,
279 "the compatibility profile is not supported");
280 } else {
281 _mesa_glsl_error(locp, this,
282 "\"%s\" is not a valid shading language profile; "
283 "if present, it must be \"core\"", ident);
284 }
285 } else {
286 _mesa_glsl_error(locp, this,
287 "illegal text following version number");
288 }
289 }
290
291 this->es_shader = es_token_present;
292 if (version == 100) {
293 if (es_token_present) {
294 _mesa_glsl_error(locp, this,
295 "GLSL 1.00 ES should be selected using "
296 "`#version 100'");
297 } else {
298 this->es_shader = true;
299 }
300 }
301
302 if (this->es_shader) {
303 this->ARB_texture_rectangle_enable = false;
304 }
305
306 this->language_version = version;
307
308 bool supported = false;
309 for (unsigned i = 0; i < this->num_supported_versions; i++) {
310 if (this->supported_versions[i].ver == (unsigned) version
311 && this->supported_versions[i].es == this->es_shader) {
312 supported = true;
313 break;
314 }
315 }
316
317 if (!supported) {
318 _mesa_glsl_error(locp, this, "%s is not supported. "
319 "Supported versions are: %s",
320 this->get_version_string(),
321 this->supported_version_string);
322
323 /* On exit, the language_version must be set to a valid value.
324 * Later calls to _mesa_glsl_initialize_types will misbehave if
325 * the version is invalid.
326 */
327 switch (this->ctx->API) {
328 case API_OPENGL_COMPAT:
329 case API_OPENGL_CORE:
330 this->language_version = this->ctx->Const.GLSLVersion;
331 break;
332
333 case API_OPENGLES:
334 assert(!"Should not get here.");
335 /* FALLTHROUGH */
336
337 case API_OPENGLES2:
338 this->language_version = 100;
339 break;
340 }
341 }
342 }
343
344
345 /**
346 * Translate a gl_shader_stage to a short shader stage name for debug
347 * printouts and error messages.
348 */
349 const char *
350 _mesa_shader_stage_to_string(unsigned stage)
351 {
352 switch (stage) {
353 case MESA_SHADER_VERTEX: return "vertex";
354 case MESA_SHADER_FRAGMENT: return "fragment";
355 case MESA_SHADER_GEOMETRY: return "geometry";
356 }
357
358 assert(!"Should not get here.");
359 return "unknown";
360 }
361
362 /* This helper function will append the given message to the shader's
363 info log and report it via GL_ARB_debug_output. Per that extension,
364 'type' is one of the enum values classifying the message, and
365 'id' is the implementation-defined ID of the given message. */
366 static void
367 _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
368 GLenum type, const char *fmt, va_list ap)
369 {
370 bool error = (type == MESA_DEBUG_TYPE_ERROR);
371 GLuint msg_id = 0;
372
373 assert(state->info_log != NULL);
374
375 /* Get the offset that the new message will be written to. */
376 int msg_offset = strlen(state->info_log);
377
378 ralloc_asprintf_append(&state->info_log, "%u:%u(%u): %s: ",
379 locp->source,
380 locp->first_line,
381 locp->first_column,
382 error ? "error" : "warning");
383 ralloc_vasprintf_append(&state->info_log, fmt, ap);
384
385 const char *const msg = &state->info_log[msg_offset];
386 struct gl_context *ctx = state->ctx;
387
388 /* Report the error via GL_ARB_debug_output. */
389 _mesa_shader_debug(ctx, type, &msg_id, msg, strlen(msg));
390
391 ralloc_strcat(&state->info_log, "\n");
392 }
393
394 void
395 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
396 const char *fmt, ...)
397 {
398 va_list ap;
399
400 state->error = true;
401
402 va_start(ap, fmt);
403 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);
404 va_end(ap);
405 }
406
407
408 void
409 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
410 const char *fmt, ...)
411 {
412 va_list ap;
413
414 va_start(ap, fmt);
415 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
416 va_end(ap);
417 }
418
419
420 /**
421 * Enum representing the possible behaviors that can be specified in
422 * an #extension directive.
423 */
424 enum ext_behavior {
425 extension_disable,
426 extension_enable,
427 extension_require,
428 extension_warn
429 };
430
431 /**
432 * Element type for _mesa_glsl_supported_extensions
433 */
434 struct _mesa_glsl_extension {
435 /**
436 * Name of the extension when referred to in a GLSL extension
437 * statement
438 */
439 const char *name;
440
441 /** True if this extension is available to desktop GL shaders */
442 bool avail_in_GL;
443
444 /** True if this extension is available to GLES shaders */
445 bool avail_in_ES;
446
447 /**
448 * Flag in the gl_extensions struct indicating whether this
449 * extension is supported by the driver, or
450 * &gl_extensions::dummy_true if supported by all drivers.
451 *
452 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
453 * member" type, the type-safe alternative to the "offsetof" macro.
454 * In a nutshell:
455 *
456 * - foo bar::* p declares p to be an "offset" to a field of type
457 * foo that exists within struct bar
458 * - &bar::baz computes the "offset" of field baz within struct bar
459 * - x.*p accesses the field of x that exists at "offset" p
460 * - x->*p is equivalent to (*x).*p
461 */
462 const GLboolean gl_extensions::* supported_flag;
463
464 /**
465 * Flag in the _mesa_glsl_parse_state struct that should be set
466 * when this extension is enabled.
467 *
468 * See note in _mesa_glsl_extension::supported_flag about "pointer
469 * to member" types.
470 */
471 bool _mesa_glsl_parse_state::* enable_flag;
472
473 /**
474 * Flag in the _mesa_glsl_parse_state struct that should be set
475 * when the shader requests "warn" behavior for this extension.
476 *
477 * See note in _mesa_glsl_extension::supported_flag about "pointer
478 * to member" types.
479 */
480 bool _mesa_glsl_parse_state::* warn_flag;
481
482
483 bool compatible_with_state(const _mesa_glsl_parse_state *state) const;
484 void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
485 };
486
487 #define EXT(NAME, GL, ES, SUPPORTED_FLAG) \
488 { "GL_" #NAME, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
489 &_mesa_glsl_parse_state::NAME##_enable, \
490 &_mesa_glsl_parse_state::NAME##_warn }
491
492 /**
493 * Table of extensions that can be enabled/disabled within a shader,
494 * and the conditions under which they are supported.
495 */
496 static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
497 /* API availability */
498 /* name GL ES supported flag */
499 EXT(ARB_arrays_of_arrays, true, false, ARB_arrays_of_arrays),
500 EXT(ARB_conservative_depth, true, false, ARB_conservative_depth),
501 EXT(ARB_draw_buffers, true, false, dummy_true),
502 EXT(ARB_draw_instanced, true, false, ARB_draw_instanced),
503 EXT(ARB_explicit_attrib_location, true, false, ARB_explicit_attrib_location),
504 EXT(ARB_fragment_coord_conventions, true, false, ARB_fragment_coord_conventions),
505 EXT(ARB_texture_rectangle, true, false, dummy_true),
506 EXT(EXT_texture_array, true, false, EXT_texture_array),
507 EXT(ARB_shader_texture_lod, true, false, ARB_shader_texture_lod),
508 EXT(ARB_shader_stencil_export, true, false, ARB_shader_stencil_export),
509 EXT(AMD_conservative_depth, true, false, ARB_conservative_depth),
510 EXT(AMD_shader_stencil_export, true, false, ARB_shader_stencil_export),
511 EXT(OES_texture_3D, false, true, EXT_texture3D),
512 EXT(OES_EGL_image_external, false, true, OES_EGL_image_external),
513 EXT(ARB_shader_bit_encoding, true, false, ARB_shader_bit_encoding),
514 EXT(ARB_uniform_buffer_object, true, false, ARB_uniform_buffer_object),
515 EXT(OES_standard_derivatives, false, true, OES_standard_derivatives),
516 EXT(ARB_texture_cube_map_array, true, false, ARB_texture_cube_map_array),
517 EXT(ARB_shading_language_packing, true, false, ARB_shading_language_packing),
518 EXT(ARB_shading_language_420pack, true, false, ARB_shading_language_420pack),
519 EXT(ARB_texture_multisample, true, false, ARB_texture_multisample),
520 EXT(ARB_texture_query_levels, true, false, ARB_texture_query_levels),
521 EXT(ARB_texture_query_lod, true, false, ARB_texture_query_lod),
522 EXT(ARB_gpu_shader5, true, false, ARB_gpu_shader5),
523 EXT(AMD_vertex_shader_layer, true, false, AMD_vertex_shader_layer),
524 EXT(EXT_shader_integer_mix, true, true, EXT_shader_integer_mix),
525 EXT(ARB_texture_gather, true, false, ARB_texture_gather),
526 EXT(ARB_shader_atomic_counters, true, false, ARB_shader_atomic_counters),
527 EXT(ARB_sample_shading, true, false, ARB_sample_shading),
528 EXT(AMD_shader_trinary_minmax, true, false, dummy_true),
529 EXT(ARB_viewport_array, true, false, ARB_viewport_array),
530 EXT(ARB_compute_shader, true, false, ARB_compute_shader),
531 EXT(ARB_shader_image_load_store, true, false, ARB_shader_image_load_store),
532 };
533
534 #undef EXT
535
536
537 /**
538 * Determine whether a given extension is compatible with the target,
539 * API, and extension information in the current parser state.
540 */
541 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state *
542 state) const
543 {
544 /* Check that this extension matches whether we are compiling
545 * for desktop GL or GLES.
546 */
547 if (state->es_shader) {
548 if (!this->avail_in_ES) return false;
549 } else {
550 if (!this->avail_in_GL) return false;
551 }
552
553 /* Check that this extension is supported by the OpenGL
554 * implementation.
555 *
556 * Note: the ->* operator indexes into state->extensions by the
557 * offset this->supported_flag. See
558 * _mesa_glsl_extension::supported_flag for more info.
559 */
560 return state->extensions->*(this->supported_flag);
561 }
562
563 /**
564 * Set the appropriate flags in the parser state to establish the
565 * given behavior for this extension.
566 */
567 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
568 ext_behavior behavior) const
569 {
570 /* Note: the ->* operator indexes into state by the
571 * offsets this->enable_flag and this->warn_flag. See
572 * _mesa_glsl_extension::supported_flag for more info.
573 */
574 state->*(this->enable_flag) = (behavior != extension_disable);
575 state->*(this->warn_flag) = (behavior == extension_warn);
576 }
577
578 /**
579 * Find an extension by name in _mesa_glsl_supported_extensions. If
580 * the name is not found, return NULL.
581 */
582 static const _mesa_glsl_extension *find_extension(const char *name)
583 {
584 for (unsigned i = 0; i < Elements(_mesa_glsl_supported_extensions); ++i) {
585 if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
586 return &_mesa_glsl_supported_extensions[i];
587 }
588 }
589 return NULL;
590 }
591
592
593 bool
594 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
595 const char *behavior_string, YYLTYPE *behavior_locp,
596 _mesa_glsl_parse_state *state)
597 {
598 ext_behavior behavior;
599 if (strcmp(behavior_string, "warn") == 0) {
600 behavior = extension_warn;
601 } else if (strcmp(behavior_string, "require") == 0) {
602 behavior = extension_require;
603 } else if (strcmp(behavior_string, "enable") == 0) {
604 behavior = extension_enable;
605 } else if (strcmp(behavior_string, "disable") == 0) {
606 behavior = extension_disable;
607 } else {
608 _mesa_glsl_error(behavior_locp, state,
609 "unknown extension behavior `%s'",
610 behavior_string);
611 return false;
612 }
613
614 if (strcmp(name, "all") == 0) {
615 if ((behavior == extension_enable) || (behavior == extension_require)) {
616 _mesa_glsl_error(name_locp, state, "cannot %s all extensions",
617 (behavior == extension_enable)
618 ? "enable" : "require");
619 return false;
620 } else {
621 for (unsigned i = 0;
622 i < Elements(_mesa_glsl_supported_extensions); ++i) {
623 const _mesa_glsl_extension *extension
624 = &_mesa_glsl_supported_extensions[i];
625 if (extension->compatible_with_state(state)) {
626 _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
627 }
628 }
629 }
630 } else {
631 const _mesa_glsl_extension *extension = find_extension(name);
632 if (extension && extension->compatible_with_state(state)) {
633 extension->set_flags(state, behavior);
634 } else {
635 static const char *const fmt = "extension `%s' unsupported in %s shader";
636
637 if (behavior == extension_require) {
638 _mesa_glsl_error(name_locp, state, fmt,
639 name, _mesa_shader_stage_to_string(state->stage));
640 return false;
641 } else {
642 _mesa_glsl_warning(name_locp, state, fmt,
643 name, _mesa_shader_stage_to_string(state->stage));
644 }
645 }
646 }
647
648 return true;
649 }
650
651
652 /**
653 * Recurses through <type> and <expr> if <expr> is an aggregate initializer
654 * and sets <expr>'s <constructor_type> field to <type>. Gives later functions
655 * (process_array_constructor, et al) sufficient information to do type
656 * checking.
657 *
658 * Operates on assignments involving an aggregate initializer. E.g.,
659 *
660 * vec4 pos = {1.0, -1.0, 0.0, 1.0};
661 *
662 * or more ridiculously,
663 *
664 * struct S {
665 * vec4 v[2];
666 * };
667 *
668 * struct {
669 * S a[2], b;
670 * int c;
671 * } aggregate = {
672 * {
673 * {
674 * {
675 * {1.0, 2.0, 3.0, 4.0}, // a[0].v[0]
676 * {5.0, 6.0, 7.0, 8.0} // a[0].v[1]
677 * } // a[0].v
678 * }, // a[0]
679 * {
680 * {
681 * {1.0, 2.0, 3.0, 4.0}, // a[1].v[0]
682 * {5.0, 6.0, 7.0, 8.0} // a[1].v[1]
683 * } // a[1].v
684 * } // a[1]
685 * }, // a
686 * {
687 * {
688 * {1.0, 2.0, 3.0, 4.0}, // b.v[0]
689 * {5.0, 6.0, 7.0, 8.0} // b.v[1]
690 * } // b.v
691 * }, // b
692 * 4 // c
693 * };
694 *
695 * This pass is necessary because the right-hand side of <type> e = { ... }
696 * doesn't contain sufficient information to determine if the types match.
697 */
698 void
699 _mesa_ast_set_aggregate_type(const glsl_type *type,
700 ast_expression *expr)
701 {
702 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)expr;
703 ai->constructor_type = type;
704
705 /* If the aggregate is an array, recursively set its elements' types. */
706 if (type->is_array()) {
707 /* Each array element has the type type->element_type().
708 *
709 * E.g., if <type> if struct S[2] we want to set each element's type to
710 * struct S.
711 */
712 for (exec_node *expr_node = ai->expressions.head;
713 !expr_node->is_tail_sentinel();
714 expr_node = expr_node->next) {
715 ast_expression *expr = exec_node_data(ast_expression, expr_node,
716 link);
717
718 if (expr->oper == ast_aggregate)
719 _mesa_ast_set_aggregate_type(type->element_type(), expr);
720 }
721
722 /* If the aggregate is a struct, recursively set its fields' types. */
723 } else if (type->is_record()) {
724 exec_node *expr_node = ai->expressions.head;
725
726 /* Iterate through the struct's fields. */
727 for (unsigned i = 0; !expr_node->is_tail_sentinel() && i < type->length;
728 i++, expr_node = expr_node->next) {
729 ast_expression *expr = exec_node_data(ast_expression, expr_node,
730 link);
731
732 if (expr->oper == ast_aggregate) {
733 _mesa_ast_set_aggregate_type(type->fields.structure[i].type, expr);
734 }
735 }
736 /* If the aggregate is a matrix, set its columns' types. */
737 } else if (type->is_matrix()) {
738 for (exec_node *expr_node = ai->expressions.head;
739 !expr_node->is_tail_sentinel();
740 expr_node = expr_node->next) {
741 ast_expression *expr = exec_node_data(ast_expression, expr_node,
742 link);
743
744 if (expr->oper == ast_aggregate)
745 _mesa_ast_set_aggregate_type(type->column_type(), expr);
746 }
747 }
748 }
749
750
751 void
752 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
753 {
754 if (q->flags.q.constant)
755 printf("const ");
756
757 if (q->flags.q.invariant)
758 printf("invariant ");
759
760 if (q->flags.q.attribute)
761 printf("attribute ");
762
763 if (q->flags.q.varying)
764 printf("varying ");
765
766 if (q->flags.q.in && q->flags.q.out)
767 printf("inout ");
768 else {
769 if (q->flags.q.in)
770 printf("in ");
771
772 if (q->flags.q.out)
773 printf("out ");
774 }
775
776 if (q->flags.q.centroid)
777 printf("centroid ");
778 if (q->flags.q.sample)
779 printf("sample ");
780 if (q->flags.q.uniform)
781 printf("uniform ");
782 if (q->flags.q.smooth)
783 printf("smooth ");
784 if (q->flags.q.flat)
785 printf("flat ");
786 if (q->flags.q.noperspective)
787 printf("noperspective ");
788 }
789
790
791 void
792 ast_node::print(void) const
793 {
794 printf("unhandled node ");
795 }
796
797
798 ast_node::ast_node(void)
799 {
800 this->location.source = 0;
801 this->location.line = 0;
802 this->location.column = 0;
803 }
804
805
806 static void
807 ast_opt_array_dimensions_print(const ast_array_specifier *array_specifier)
808 {
809 if (array_specifier)
810 array_specifier->print();
811 }
812
813
814 void
815 ast_compound_statement::print(void) const
816 {
817 printf("{\n");
818
819 foreach_list_const(n, &this->statements) {
820 ast_node *ast = exec_node_data(ast_node, n, link);
821 ast->print();
822 }
823
824 printf("}\n");
825 }
826
827
828 ast_compound_statement::ast_compound_statement(int new_scope,
829 ast_node *statements)
830 {
831 this->new_scope = new_scope;
832
833 if (statements != NULL) {
834 this->statements.push_degenerate_list_at_head(&statements->link);
835 }
836 }
837
838
839 void
840 ast_expression::print(void) const
841 {
842 switch (oper) {
843 case ast_assign:
844 case ast_mul_assign:
845 case ast_div_assign:
846 case ast_mod_assign:
847 case ast_add_assign:
848 case ast_sub_assign:
849 case ast_ls_assign:
850 case ast_rs_assign:
851 case ast_and_assign:
852 case ast_xor_assign:
853 case ast_or_assign:
854 subexpressions[0]->print();
855 printf("%s ", operator_string(oper));
856 subexpressions[1]->print();
857 break;
858
859 case ast_field_selection:
860 subexpressions[0]->print();
861 printf(". %s ", primary_expression.identifier);
862 break;
863
864 case ast_plus:
865 case ast_neg:
866 case ast_bit_not:
867 case ast_logic_not:
868 case ast_pre_inc:
869 case ast_pre_dec:
870 printf("%s ", operator_string(oper));
871 subexpressions[0]->print();
872 break;
873
874 case ast_post_inc:
875 case ast_post_dec:
876 subexpressions[0]->print();
877 printf("%s ", operator_string(oper));
878 break;
879
880 case ast_conditional:
881 subexpressions[0]->print();
882 printf("? ");
883 subexpressions[1]->print();
884 printf(": ");
885 subexpressions[2]->print();
886 break;
887
888 case ast_array_index:
889 subexpressions[0]->print();
890 printf("[ ");
891 subexpressions[1]->print();
892 printf("] ");
893 break;
894
895 case ast_function_call: {
896 subexpressions[0]->print();
897 printf("( ");
898
899 foreach_list_const (n, &this->expressions) {
900 if (n != this->expressions.get_head())
901 printf(", ");
902
903 ast_node *ast = exec_node_data(ast_node, n, link);
904 ast->print();
905 }
906
907 printf(") ");
908 break;
909 }
910
911 case ast_identifier:
912 printf("%s ", primary_expression.identifier);
913 break;
914
915 case ast_int_constant:
916 printf("%d ", primary_expression.int_constant);
917 break;
918
919 case ast_uint_constant:
920 printf("%u ", primary_expression.uint_constant);
921 break;
922
923 case ast_float_constant:
924 printf("%f ", primary_expression.float_constant);
925 break;
926
927 case ast_bool_constant:
928 printf("%s ",
929 primary_expression.bool_constant
930 ? "true" : "false");
931 break;
932
933 case ast_sequence: {
934 printf("( ");
935 foreach_list_const(n, & this->expressions) {
936 if (n != this->expressions.get_head())
937 printf(", ");
938
939 ast_node *ast = exec_node_data(ast_node, n, link);
940 ast->print();
941 }
942 printf(") ");
943 break;
944 }
945
946 case ast_aggregate: {
947 printf("{ ");
948 foreach_list_const(n, & this->expressions) {
949 if (n != this->expressions.get_head())
950 printf(", ");
951
952 ast_node *ast = exec_node_data(ast_node, n, link);
953 ast->print();
954 }
955 printf("} ");
956 break;
957 }
958
959 default:
960 assert(0);
961 break;
962 }
963 }
964
965 ast_expression::ast_expression(int oper,
966 ast_expression *ex0,
967 ast_expression *ex1,
968 ast_expression *ex2) :
969 primary_expression()
970 {
971 this->oper = ast_operators(oper);
972 this->subexpressions[0] = ex0;
973 this->subexpressions[1] = ex1;
974 this->subexpressions[2] = ex2;
975 this->non_lvalue_description = NULL;
976 }
977
978
979 void
980 ast_expression_statement::print(void) const
981 {
982 if (expression)
983 expression->print();
984
985 printf("; ");
986 }
987
988
989 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
990 expression(ex)
991 {
992 /* empty */
993 }
994
995
996 void
997 ast_function::print(void) const
998 {
999 return_type->print();
1000 printf(" %s (", identifier);
1001
1002 foreach_list_const(n, & this->parameters) {
1003 ast_node *ast = exec_node_data(ast_node, n, link);
1004 ast->print();
1005 }
1006
1007 printf(")");
1008 }
1009
1010
1011 ast_function::ast_function(void)
1012 : return_type(NULL), identifier(NULL), is_definition(false),
1013 signature(NULL)
1014 {
1015 /* empty */
1016 }
1017
1018
1019 void
1020 ast_fully_specified_type::print(void) const
1021 {
1022 _mesa_ast_type_qualifier_print(& qualifier);
1023 specifier->print();
1024 }
1025
1026
1027 void
1028 ast_parameter_declarator::print(void) const
1029 {
1030 type->print();
1031 if (identifier)
1032 printf("%s ", identifier);
1033 ast_opt_array_dimensions_print(array_specifier);
1034 }
1035
1036
1037 void
1038 ast_function_definition::print(void) const
1039 {
1040 prototype->print();
1041 body->print();
1042 }
1043
1044
1045 void
1046 ast_declaration::print(void) const
1047 {
1048 printf("%s ", identifier);
1049 ast_opt_array_dimensions_print(array_specifier);
1050
1051 if (initializer) {
1052 printf("= ");
1053 initializer->print();
1054 }
1055 }
1056
1057
1058 ast_declaration::ast_declaration(const char *identifier,
1059 ast_array_specifier *array_specifier,
1060 ast_expression *initializer)
1061 {
1062 this->identifier = identifier;
1063 this->array_specifier = array_specifier;
1064 this->initializer = initializer;
1065 }
1066
1067
1068 void
1069 ast_declarator_list::print(void) const
1070 {
1071 assert(type || invariant);
1072
1073 if (type)
1074 type->print();
1075 else
1076 printf("invariant ");
1077
1078 foreach_list_const (ptr, & this->declarations) {
1079 if (ptr != this->declarations.get_head())
1080 printf(", ");
1081
1082 ast_node *ast = exec_node_data(ast_node, ptr, link);
1083 ast->print();
1084 }
1085
1086 printf("; ");
1087 }
1088
1089
1090 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
1091 {
1092 this->type = type;
1093 this->invariant = false;
1094 }
1095
1096 void
1097 ast_jump_statement::print(void) const
1098 {
1099 switch (mode) {
1100 case ast_continue:
1101 printf("continue; ");
1102 break;
1103 case ast_break:
1104 printf("break; ");
1105 break;
1106 case ast_return:
1107 printf("return ");
1108 if (opt_return_value)
1109 opt_return_value->print();
1110
1111 printf("; ");
1112 break;
1113 case ast_discard:
1114 printf("discard; ");
1115 break;
1116 }
1117 }
1118
1119
1120 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
1121 : opt_return_value(NULL)
1122 {
1123 this->mode = ast_jump_modes(mode);
1124
1125 if (mode == ast_return)
1126 opt_return_value = return_value;
1127 }
1128
1129
1130 void
1131 ast_selection_statement::print(void) const
1132 {
1133 printf("if ( ");
1134 condition->print();
1135 printf(") ");
1136
1137 then_statement->print();
1138
1139 if (else_statement) {
1140 printf("else ");
1141 else_statement->print();
1142 }
1143
1144 }
1145
1146
1147 ast_selection_statement::ast_selection_statement(ast_expression *condition,
1148 ast_node *then_statement,
1149 ast_node *else_statement)
1150 {
1151 this->condition = condition;
1152 this->then_statement = then_statement;
1153 this->else_statement = else_statement;
1154 }
1155
1156
1157 void
1158 ast_switch_statement::print(void) const
1159 {
1160 printf("switch ( ");
1161 test_expression->print();
1162 printf(") ");
1163
1164 body->print();
1165 }
1166
1167
1168 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
1169 ast_node *body)
1170 {
1171 this->test_expression = test_expression;
1172 this->body = body;
1173 }
1174
1175
1176 void
1177 ast_switch_body::print(void) const
1178 {
1179 printf("{\n");
1180 if (stmts != NULL) {
1181 stmts->print();
1182 }
1183 printf("}\n");
1184 }
1185
1186
1187 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
1188 {
1189 this->stmts = stmts;
1190 }
1191
1192
1193 void ast_case_label::print(void) const
1194 {
1195 if (test_value != NULL) {
1196 printf("case ");
1197 test_value->print();
1198 printf(": ");
1199 } else {
1200 printf("default: ");
1201 }
1202 }
1203
1204
1205 ast_case_label::ast_case_label(ast_expression *test_value)
1206 {
1207 this->test_value = test_value;
1208 }
1209
1210
1211 void ast_case_label_list::print(void) const
1212 {
1213 foreach_list_const(n, & this->labels) {
1214 ast_node *ast = exec_node_data(ast_node, n, link);
1215 ast->print();
1216 }
1217 printf("\n");
1218 }
1219
1220
1221 ast_case_label_list::ast_case_label_list(void)
1222 {
1223 }
1224
1225
1226 void ast_case_statement::print(void) const
1227 {
1228 labels->print();
1229 foreach_list_const(n, & this->stmts) {
1230 ast_node *ast = exec_node_data(ast_node, n, link);
1231 ast->print();
1232 printf("\n");
1233 }
1234 }
1235
1236
1237 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
1238 {
1239 this->labels = labels;
1240 }
1241
1242
1243 void ast_case_statement_list::print(void) const
1244 {
1245 foreach_list_const(n, & this->cases) {
1246 ast_node *ast = exec_node_data(ast_node, n, link);
1247 ast->print();
1248 }
1249 }
1250
1251
1252 ast_case_statement_list::ast_case_statement_list(void)
1253 {
1254 }
1255
1256
1257 void
1258 ast_iteration_statement::print(void) const
1259 {
1260 switch (mode) {
1261 case ast_for:
1262 printf("for( ");
1263 if (init_statement)
1264 init_statement->print();
1265 printf("; ");
1266
1267 if (condition)
1268 condition->print();
1269 printf("; ");
1270
1271 if (rest_expression)
1272 rest_expression->print();
1273 printf(") ");
1274
1275 body->print();
1276 break;
1277
1278 case ast_while:
1279 printf("while ( ");
1280 if (condition)
1281 condition->print();
1282 printf(") ");
1283 body->print();
1284 break;
1285
1286 case ast_do_while:
1287 printf("do ");
1288 body->print();
1289 printf("while ( ");
1290 if (condition)
1291 condition->print();
1292 printf("); ");
1293 break;
1294 }
1295 }
1296
1297
1298 ast_iteration_statement::ast_iteration_statement(int mode,
1299 ast_node *init,
1300 ast_node *condition,
1301 ast_expression *rest_expression,
1302 ast_node *body)
1303 {
1304 this->mode = ast_iteration_modes(mode);
1305 this->init_statement = init;
1306 this->condition = condition;
1307 this->rest_expression = rest_expression;
1308 this->body = body;
1309 }
1310
1311
1312 void
1313 ast_struct_specifier::print(void) const
1314 {
1315 printf("struct %s { ", name);
1316 foreach_list_const(n, &this->declarations) {
1317 ast_node *ast = exec_node_data(ast_node, n, link);
1318 ast->print();
1319 }
1320 printf("} ");
1321 }
1322
1323
1324 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1325 ast_declarator_list *declarator_list)
1326 {
1327 if (identifier == NULL) {
1328 static unsigned anon_count = 1;
1329 identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
1330 anon_count++;
1331 }
1332 name = identifier;
1333 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1334 is_declaration = true;
1335 }
1336
1337 static void
1338 set_shader_inout_layout(struct gl_shader *shader,
1339 struct _mesa_glsl_parse_state *state)
1340 {
1341 if (shader->Stage != MESA_SHADER_GEOMETRY) {
1342 /* Should have been prevented by the parser. */
1343 assert(!state->gs_input_prim_type_specified);
1344 assert(!state->out_qualifier->flags.i);
1345 }
1346
1347 if (shader->Stage != MESA_SHADER_COMPUTE) {
1348 /* Should have been prevented by the parser. */
1349 assert(!state->cs_input_local_size_specified);
1350 }
1351
1352 switch (shader->Stage) {
1353 case MESA_SHADER_GEOMETRY:
1354 shader->Geom.VerticesOut = 0;
1355 if (state->out_qualifier->flags.q.max_vertices)
1356 shader->Geom.VerticesOut = state->out_qualifier->max_vertices;
1357
1358 if (state->gs_input_prim_type_specified) {
1359 shader->Geom.InputType = state->gs_input_prim_type;
1360 } else {
1361 shader->Geom.InputType = PRIM_UNKNOWN;
1362 }
1363
1364 if (state->out_qualifier->flags.q.prim_type) {
1365 shader->Geom.OutputType = state->out_qualifier->prim_type;
1366 } else {
1367 shader->Geom.OutputType = PRIM_UNKNOWN;
1368 }
1369 break;
1370
1371 case MESA_SHADER_COMPUTE:
1372 if (state->cs_input_local_size_specified) {
1373 for (int i = 0; i < 3; i++)
1374 shader->Comp.LocalSize[i] = state->cs_input_local_size[i];
1375 } else {
1376 for (int i = 0; i < 3; i++)
1377 shader->Comp.LocalSize[i] = 0;
1378 }
1379 break;
1380
1381 default:
1382 /* Nothing to do. */
1383 break;
1384 }
1385 }
1386
1387 extern "C" {
1388
1389 void
1390 _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
1391 bool dump_ast, bool dump_hir)
1392 {
1393 struct _mesa_glsl_parse_state *state =
1394 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
1395 const char *source = shader->Source;
1396
1397 state->error = glcpp_preprocess(state, &source, &state->info_log,
1398 &ctx->Extensions, ctx);
1399
1400 if (!state->error) {
1401 _mesa_glsl_lexer_ctor(state, source);
1402 _mesa_glsl_parse(state);
1403 _mesa_glsl_lexer_dtor(state);
1404 }
1405
1406 if (dump_ast) {
1407 foreach_list_const(n, &state->translation_unit) {
1408 ast_node *ast = exec_node_data(ast_node, n, link);
1409 ast->print();
1410 }
1411 printf("\n\n");
1412 }
1413
1414 ralloc_free(shader->ir);
1415 shader->ir = new(shader) exec_list;
1416 if (!state->error && !state->translation_unit.is_empty())
1417 _mesa_ast_to_hir(shader->ir, state);
1418
1419 if (!state->error) {
1420 validate_ir_tree(shader->ir);
1421
1422 /* Print out the unoptimized IR. */
1423 if (dump_hir) {
1424 _mesa_print_ir(shader->ir, state);
1425 }
1426 }
1427
1428
1429 if (!state->error && !shader->ir->is_empty()) {
1430 struct gl_shader_compiler_options *options =
1431 &ctx->ShaderCompilerOptions[shader->Stage];
1432
1433 /* Do some optimization at compile time to reduce shader IR size
1434 * and reduce later work if the same shader is linked multiple times
1435 */
1436 while (do_common_optimization(shader->ir, false, false, 32, options))
1437 ;
1438
1439 validate_ir_tree(shader->ir);
1440 }
1441
1442 if (shader->InfoLog)
1443 ralloc_free(shader->InfoLog);
1444
1445 shader->symbols = state->symbols;
1446 shader->CompileStatus = !state->error;
1447 shader->InfoLog = state->info_log;
1448 shader->Version = state->language_version;
1449 shader->IsES = state->es_shader;
1450 shader->uses_builtin_functions = state->uses_builtin_functions;
1451
1452 if (shader->UniformBlocks)
1453 ralloc_free(shader->UniformBlocks);
1454 shader->NumUniformBlocks = state->num_uniform_blocks;
1455 shader->UniformBlocks = state->uniform_blocks;
1456 ralloc_steal(shader, shader->UniformBlocks);
1457
1458 if (!state->error)
1459 set_shader_inout_layout(shader, state);
1460
1461 /* Retain any live IR, but trash the rest. */
1462 reparent_ir(shader->ir, shader->ir);
1463
1464 ralloc_free(state);
1465 }
1466
1467 } /* extern "C" */
1468 /**
1469 * Do the set of common optimizations passes
1470 *
1471 * \param ir List of instructions to be optimized
1472 * \param linked Is the shader linked? This enables
1473 * optimizations passes that remove code at
1474 * global scope and could cause linking to
1475 * fail.
1476 * \param uniform_locations_assigned Have locations already been assigned for
1477 * uniforms? This prevents the declarations
1478 * of unused uniforms from being removed.
1479 * The setting of this flag only matters if
1480 * \c linked is \c true.
1481 * \param max_unroll_iterations Maximum number of loop iterations to be
1482 * unrolled. Setting to 0 disables loop
1483 * unrolling.
1484 * \param options The driver's preferred shader options.
1485 */
1486 bool
1487 do_common_optimization(exec_list *ir, bool linked,
1488 bool uniform_locations_assigned,
1489 unsigned max_unroll_iterations,
1490 const struct gl_shader_compiler_options *options)
1491 {
1492 GLboolean progress = GL_FALSE;
1493
1494 progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
1495
1496 if (linked) {
1497 progress = do_function_inlining(ir) || progress;
1498 progress = do_dead_functions(ir) || progress;
1499 progress = do_structure_splitting(ir) || progress;
1500 }
1501 progress = do_if_simplification(ir) || progress;
1502 progress = opt_flatten_nested_if_blocks(ir) || progress;
1503 progress = do_copy_propagation(ir) || progress;
1504 progress = do_copy_propagation_elements(ir) || progress;
1505
1506 if (options->OptimizeForAOS && !linked)
1507 progress = opt_flip_matrices(ir) || progress;
1508
1509 if (linked && options->OptimizeForAOS) {
1510 progress = do_vectorize(ir) || progress;
1511 }
1512
1513 if (linked)
1514 progress = do_dead_code(ir, uniform_locations_assigned) || progress;
1515 else
1516 progress = do_dead_code_unlinked(ir) || progress;
1517 progress = do_dead_code_local(ir) || progress;
1518 progress = do_tree_grafting(ir) || progress;
1519 progress = do_constant_propagation(ir) || progress;
1520 if (linked)
1521 progress = do_constant_variable(ir) || progress;
1522 else
1523 progress = do_constant_variable_unlinked(ir) || progress;
1524 progress = do_constant_folding(ir) || progress;
1525 progress = do_cse(ir) || progress;
1526 progress = do_algebraic(ir) || progress;
1527 progress = do_lower_jumps(ir) || progress;
1528 progress = do_vec_index_to_swizzle(ir) || progress;
1529 progress = lower_vector_insert(ir, false) || progress;
1530 progress = do_swizzle_swizzle(ir) || progress;
1531 progress = do_noop_swizzle(ir) || progress;
1532
1533 progress = optimize_split_arrays(ir, linked) || progress;
1534 progress = optimize_redundant_jumps(ir) || progress;
1535
1536 loop_state *ls = analyze_loop_variables(ir);
1537 if (ls->loop_found) {
1538 progress = set_loop_controls(ir, ls) || progress;
1539 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
1540 }
1541 delete ls;
1542
1543 return progress;
1544 }
1545
1546 extern "C" {
1547
1548 /**
1549 * To be called at GL teardown time, this frees compiler datastructures.
1550 *
1551 * After calling this, any previously compiled shaders and shader
1552 * programs would be invalid. So this should happen at approximately
1553 * program exit.
1554 */
1555 void
1556 _mesa_destroy_shader_compiler(void)
1557 {
1558 _mesa_destroy_shader_compiler_caches();
1559
1560 _mesa_glsl_release_types();
1561 }
1562
1563 /**
1564 * Releases compiler caches to trade off performance for memory.
1565 *
1566 * Intended to be used with glReleaseShaderCompiler().
1567 */
1568 void
1569 _mesa_destroy_shader_compiler_caches(void)
1570 {
1571 _mesa_glsl_release_builtin_functions();
1572 }
1573
1574 }