glsl: Parse "#version 150 core" directives.
[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 }
32
33 #include "ralloc.h"
34 #include "ast.h"
35 #include "glsl_parser_extras.h"
36 #include "glsl_parser.h"
37 #include "ir_optimization.h"
38 #include "loop_analysis.h"
39
40 /**
41 * Format a short human-readable description of the given GLSL version.
42 */
43 const char *
44 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
45 {
46 return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
47 version / 100, version % 100);
48 }
49
50
51 static unsigned known_desktop_glsl_versions[] =
52 { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430 };
53
54
55 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
56 GLenum target, void *mem_ctx)
57 : ctx(_ctx)
58 {
59 switch (target) {
60 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
61 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
62 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
63 }
64
65 this->scanner = NULL;
66 this->translation_unit.make_empty();
67 this->symbols = new(mem_ctx) glsl_symbol_table;
68 this->info_log = ralloc_strdup(mem_ctx, "");
69 this->error = false;
70 this->loop_nesting_ast = NULL;
71 this->switch_state.switch_nesting_ast = NULL;
72
73 this->num_builtins_to_link = 0;
74
75 /* Set default language version and extensions */
76 this->language_version = 110;
77 this->es_shader = false;
78 this->ARB_texture_rectangle_enable = true;
79
80 /* OpenGL ES 2.0 has different defaults from desktop GL. */
81 if (ctx->API == API_OPENGLES2) {
82 this->language_version = 100;
83 this->es_shader = true;
84 this->ARB_texture_rectangle_enable = false;
85 }
86
87 this->extensions = &ctx->Extensions;
88
89 this->Const.MaxLights = ctx->Const.MaxLights;
90 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
91 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
92 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
93 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
94 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
95 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
96 this->Const.MaxVertexTextureImageUnits = ctx->Const.VertexProgram.MaxTextureImageUnits;
97 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
98 this->Const.MaxTextureImageUnits = ctx->Const.FragmentProgram.MaxTextureImageUnits;
99 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
100 this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
101 this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
102
103 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
104
105 /* Populate the list of supported GLSL versions */
106 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
107 * the OpenGL 3.2 Core context is supported, this logic will need
108 * change. Older versions of GLSL are no longer supported
109 * outside the compatibility contexts of 3.x.
110 */
111 this->num_supported_versions = 0;
112 if (_mesa_is_desktop_gl(ctx)) {
113 for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {
114 if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {
115 this->supported_versions[this->num_supported_versions].ver
116 = known_desktop_glsl_versions[i];
117 this->supported_versions[this->num_supported_versions].es = false;
118 this->num_supported_versions++;
119 }
120 }
121 }
122 if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility) {
123 this->supported_versions[this->num_supported_versions].ver = 100;
124 this->supported_versions[this->num_supported_versions].es = true;
125 this->num_supported_versions++;
126 }
127 if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
128 this->supported_versions[this->num_supported_versions].ver = 300;
129 this->supported_versions[this->num_supported_versions].es = true;
130 this->num_supported_versions++;
131 }
132 assert(this->num_supported_versions
133 <= ARRAY_SIZE(this->supported_versions));
134
135 /* Create a string for use in error messages to tell the user which GLSL
136 * versions are supported.
137 */
138 char *supported = ralloc_strdup(this, "");
139 for (unsigned i = 0; i < this->num_supported_versions; i++) {
140 unsigned ver = this->supported_versions[i].ver;
141 const char *const prefix = (i == 0)
142 ? ""
143 : ((i == this->num_supported_versions - 1) ? ", and " : ", ");
144 const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";
145
146 ralloc_asprintf_append(& supported, "%s%u.%02u%s",
147 prefix,
148 ver / 100, ver % 100,
149 suffix);
150 }
151
152 this->supported_version_string = supported;
153
154 if (ctx->Const.ForceGLSLExtensionsWarn)
155 _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
156
157 this->default_uniform_qualifier = new(this) ast_type_qualifier();
158 this->default_uniform_qualifier->flags.q.shared = 1;
159 this->default_uniform_qualifier->flags.q.column_major = 1;
160 }
161
162 /**
163 * Determine whether the current GLSL version is sufficiently high to support
164 * a certain feature, and generate an error message if it isn't.
165 *
166 * \param required_glsl_version and \c required_glsl_es_version are
167 * interpreted as they are in _mesa_glsl_parse_state::is_version().
168 *
169 * \param locp is the parser location where the error should be reported.
170 *
171 * \param fmt (and additional arguments) constitute a printf-style error
172 * message to report if the version check fails. Information about the
173 * current and required GLSL versions will be appended. So, for example, if
174 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
175 * "foo unsupported") is called, the error message will be "foo unsupported in
176 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
177 */
178 bool
179 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
180 unsigned required_glsl_es_version,
181 YYLTYPE *locp, const char *fmt, ...)
182 {
183 if (this->is_version(required_glsl_version, required_glsl_es_version))
184 return true;
185
186 va_list args;
187 va_start(args, fmt);
188 char *problem = ralloc_vasprintf(this, fmt, args);
189 va_end(args);
190 const char *glsl_version_string
191 = glsl_compute_version_string(this, false, required_glsl_version);
192 const char *glsl_es_version_string
193 = glsl_compute_version_string(this, true, required_glsl_es_version);
194 const char *requirement_string = "";
195 if (required_glsl_version && required_glsl_es_version) {
196 requirement_string = ralloc_asprintf(this, " (%s or %s required)",
197 glsl_version_string,
198 glsl_es_version_string);
199 } else if (required_glsl_version) {
200 requirement_string = ralloc_asprintf(this, " (%s required)",
201 glsl_version_string);
202 } else if (required_glsl_es_version) {
203 requirement_string = ralloc_asprintf(this, " (%s required)",
204 glsl_es_version_string);
205 }
206 _mesa_glsl_error(locp, this, "%s in %s%s.",
207 problem, this->get_version_string(),
208 requirement_string);
209
210 return false;
211 }
212
213 /**
214 * Process a GLSL #version directive.
215 *
216 * \param version is the integer that follows the #version token.
217 *
218 * \param ident is a string identifier that follows the integer, if any is
219 * present. Otherwise NULL.
220 */
221 void
222 _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
223 const char *ident)
224 {
225 bool es_token_present = false;
226 if (ident) {
227 if (strcmp(ident, "es") == 0) {
228 es_token_present = true;
229 } else if (version >= 150) {
230 if (strcmp(ident, "core") == 0) {
231 /* Accept the token. There's no need to record that this is
232 * a core profile shader since that's the only profile we support.
233 */
234 } else if (strcmp(ident, "compatibility") == 0) {
235 _mesa_glsl_error(locp, this,
236 "The compatibility profile is not supported.\n");
237 } else {
238 _mesa_glsl_error(locp, this,
239 "\"%s\" is not a valid shading language profile; "
240 "if present, it must be \"core\".\n", ident);
241 }
242 } else {
243 _mesa_glsl_error(locp, this,
244 "Illegal text following version number\n");
245 }
246 }
247
248 this->es_shader = es_token_present;
249 if (version == 100) {
250 if (es_token_present) {
251 _mesa_glsl_error(locp, this,
252 "GLSL 1.00 ES should be selected using "
253 "`#version 100'\n");
254 } else {
255 this->es_shader = true;
256 }
257 }
258
259 this->language_version = version;
260
261 bool supported = false;
262 for (unsigned i = 0; i < this->num_supported_versions; i++) {
263 if (this->supported_versions[i].ver == (unsigned) version
264 && this->supported_versions[i].es == this->es_shader) {
265 supported = true;
266 break;
267 }
268 }
269
270 if (!supported) {
271 _mesa_glsl_error(locp, this, "%s is not supported. "
272 "Supported versions are: %s\n",
273 this->get_version_string(),
274 this->supported_version_string);
275
276 /* On exit, the language_version must be set to a valid value.
277 * Later calls to _mesa_glsl_initialize_types will misbehave if
278 * the version is invalid.
279 */
280 switch (this->ctx->API) {
281 case API_OPENGL_COMPAT:
282 case API_OPENGL_CORE:
283 this->language_version = this->ctx->Const.GLSLVersion;
284 break;
285
286 case API_OPENGLES:
287 assert(!"Should not get here.");
288 /* FALLTHROUGH */
289
290 case API_OPENGLES2:
291 this->language_version = 100;
292 break;
293 }
294 }
295
296 if (this->language_version >= 140) {
297 this->ARB_uniform_buffer_object_enable = true;
298 }
299
300 if (this->language_version == 300 && this->es_shader) {
301 this->ARB_explicit_attrib_location_enable = true;
302 }
303 }
304
305 const char *
306 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
307 {
308 switch (target) {
309 case vertex_shader: return "vertex";
310 case fragment_shader: return "fragment";
311 case geometry_shader: return "geometry";
312 }
313
314 assert(!"Should not get here.");
315 return "unknown";
316 }
317
318 /* This helper function will append the given message to the shader's
319 info log and report it via GL_ARB_debug_output. Per that extension,
320 'type' is one of the enum values classifying the message, and
321 'id' is the implementation-defined ID of the given message. */
322 static void
323 _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
324 GLenum type, const char *fmt, va_list ap)
325 {
326 bool error = (type == MESA_DEBUG_TYPE_ERROR);
327 GLuint msg_id = 0;
328
329 assert(state->info_log != NULL);
330
331 /* Get the offset that the new message will be written to. */
332 int msg_offset = strlen(state->info_log);
333
334 ralloc_asprintf_append(&state->info_log, "%u:%u(%u): %s: ",
335 locp->source,
336 locp->first_line,
337 locp->first_column,
338 error ? "error" : "warning");
339 ralloc_vasprintf_append(&state->info_log, fmt, ap);
340
341 const char *const msg = &state->info_log[msg_offset];
342 struct gl_context *ctx = state->ctx;
343
344 /* Report the error via GL_ARB_debug_output. */
345 _mesa_shader_debug(ctx, type, &msg_id, msg, strlen(msg));
346
347 ralloc_strcat(&state->info_log, "\n");
348 }
349
350 void
351 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
352 const char *fmt, ...)
353 {
354 va_list ap;
355
356 state->error = true;
357
358 va_start(ap, fmt);
359 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);
360 va_end(ap);
361 }
362
363
364 void
365 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
366 const char *fmt, ...)
367 {
368 va_list ap;
369
370 va_start(ap, fmt);
371 _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
372 va_end(ap);
373 }
374
375
376 /**
377 * Enum representing the possible behaviors that can be specified in
378 * an #extension directive.
379 */
380 enum ext_behavior {
381 extension_disable,
382 extension_enable,
383 extension_require,
384 extension_warn
385 };
386
387 /**
388 * Element type for _mesa_glsl_supported_extensions
389 */
390 struct _mesa_glsl_extension {
391 /**
392 * Name of the extension when referred to in a GLSL extension
393 * statement
394 */
395 const char *name;
396
397 /** True if this extension is available to vertex shaders */
398 bool avail_in_VS;
399
400 /** True if this extension is available to geometry shaders */
401 bool avail_in_GS;
402
403 /** True if this extension is available to fragment shaders */
404 bool avail_in_FS;
405
406 /** True if this extension is available to desktop GL shaders */
407 bool avail_in_GL;
408
409 /** True if this extension is available to GLES shaders */
410 bool avail_in_ES;
411
412 /**
413 * Flag in the gl_extensions struct indicating whether this
414 * extension is supported by the driver, or
415 * &gl_extensions::dummy_true if supported by all drivers.
416 *
417 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
418 * member" type, the type-safe alternative to the "offsetof" macro.
419 * In a nutshell:
420 *
421 * - foo bar::* p declares p to be an "offset" to a field of type
422 * foo that exists within struct bar
423 * - &bar::baz computes the "offset" of field baz within struct bar
424 * - x.*p accesses the field of x that exists at "offset" p
425 * - x->*p is equivalent to (*x).*p
426 */
427 const GLboolean gl_extensions::* supported_flag;
428
429 /**
430 * Flag in the _mesa_glsl_parse_state struct that should be set
431 * when this extension is enabled.
432 *
433 * See note in _mesa_glsl_extension::supported_flag about "pointer
434 * to member" types.
435 */
436 bool _mesa_glsl_parse_state::* enable_flag;
437
438 /**
439 * Flag in the _mesa_glsl_parse_state struct that should be set
440 * when the shader requests "warn" behavior for this extension.
441 *
442 * See note in _mesa_glsl_extension::supported_flag about "pointer
443 * to member" types.
444 */
445 bool _mesa_glsl_parse_state::* warn_flag;
446
447
448 bool compatible_with_state(const _mesa_glsl_parse_state *state) const;
449 void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
450 };
451
452 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
453 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
454 &_mesa_glsl_parse_state::NAME##_enable, \
455 &_mesa_glsl_parse_state::NAME##_warn }
456
457 /**
458 * Table of extensions that can be enabled/disabled within a shader,
459 * and the conditions under which they are supported.
460 */
461 static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
462 /* target availability API availability */
463 /* name VS GS FS GL ES supported flag */
464 EXT(ARB_conservative_depth, false, false, true, true, false, ARB_conservative_depth),
465 EXT(ARB_draw_buffers, false, false, true, true, false, dummy_true),
466 EXT(ARB_draw_instanced, true, false, false, true, false, ARB_draw_instanced),
467 EXT(ARB_explicit_attrib_location, true, false, true, true, false, ARB_explicit_attrib_location),
468 EXT(ARB_fragment_coord_conventions, true, false, true, true, false, ARB_fragment_coord_conventions),
469 EXT(ARB_texture_rectangle, true, false, true, true, false, dummy_true),
470 EXT(EXT_texture_array, true, false, true, true, false, EXT_texture_array),
471 EXT(ARB_shader_texture_lod, true, false, true, true, false, ARB_shader_texture_lod),
472 EXT(ARB_shader_stencil_export, false, false, true, true, false, ARB_shader_stencil_export),
473 EXT(AMD_conservative_depth, false, false, true, true, false, ARB_conservative_depth),
474 EXT(AMD_shader_stencil_export, false, false, true, true, false, ARB_shader_stencil_export),
475 EXT(OES_texture_3D, true, false, true, false, true, EXT_texture3D),
476 EXT(OES_EGL_image_external, true, false, true, false, true, OES_EGL_image_external),
477 EXT(ARB_shader_bit_encoding, true, true, true, true, false, ARB_shader_bit_encoding),
478 EXT(ARB_uniform_buffer_object, true, false, true, true, false, ARB_uniform_buffer_object),
479 EXT(OES_standard_derivatives, false, false, true, false, true, OES_standard_derivatives),
480 EXT(ARB_texture_cube_map_array, true, false, true, true, false, ARB_texture_cube_map_array),
481 EXT(ARB_shading_language_packing, true, false, true, true, false, ARB_shading_language_packing),
482 EXT(ARB_texture_multisample, true, false, true, true, false, ARB_texture_multisample),
483 EXT(ARB_texture_query_lod, false, false, true, true, false, ARB_texture_query_lod),
484 EXT(ARB_gpu_shader5, true, true, true, true, false, ARB_gpu_shader5),
485 EXT(AMD_vertex_shader_layer, true, false, false, true, false, AMD_vertex_shader_layer),
486 };
487
488 #undef EXT
489
490
491 /**
492 * Determine whether a given extension is compatible with the target,
493 * API, and extension information in the current parser state.
494 */
495 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state *
496 state) const
497 {
498 /* Check that this extension matches the type of shader we are
499 * compiling to.
500 */
501 switch (state->target) {
502 case vertex_shader:
503 if (!this->avail_in_VS) {
504 return false;
505 }
506 break;
507 case geometry_shader:
508 if (!this->avail_in_GS) {
509 return false;
510 }
511 break;
512 case fragment_shader:
513 if (!this->avail_in_FS) {
514 return false;
515 }
516 break;
517 default:
518 assert (!"Unrecognized shader target");
519 return false;
520 }
521
522 /* Check that this extension matches whether we are compiling
523 * for desktop GL or GLES.
524 */
525 if (state->es_shader) {
526 if (!this->avail_in_ES) return false;
527 } else {
528 if (!this->avail_in_GL) return false;
529 }
530
531 /* Check that this extension is supported by the OpenGL
532 * implementation.
533 *
534 * Note: the ->* operator indexes into state->extensions by the
535 * offset this->supported_flag. See
536 * _mesa_glsl_extension::supported_flag for more info.
537 */
538 return state->extensions->*(this->supported_flag);
539 }
540
541 /**
542 * Set the appropriate flags in the parser state to establish the
543 * given behavior for this extension.
544 */
545 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
546 ext_behavior behavior) const
547 {
548 /* Note: the ->* operator indexes into state by the
549 * offsets this->enable_flag and this->warn_flag. See
550 * _mesa_glsl_extension::supported_flag for more info.
551 */
552 state->*(this->enable_flag) = (behavior != extension_disable);
553 state->*(this->warn_flag) = (behavior == extension_warn);
554 }
555
556 /**
557 * Find an extension by name in _mesa_glsl_supported_extensions. If
558 * the name is not found, return NULL.
559 */
560 static const _mesa_glsl_extension *find_extension(const char *name)
561 {
562 for (unsigned i = 0; i < Elements(_mesa_glsl_supported_extensions); ++i) {
563 if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
564 return &_mesa_glsl_supported_extensions[i];
565 }
566 }
567 return NULL;
568 }
569
570
571 bool
572 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
573 const char *behavior_string, YYLTYPE *behavior_locp,
574 _mesa_glsl_parse_state *state)
575 {
576 ext_behavior behavior;
577 if (strcmp(behavior_string, "warn") == 0) {
578 behavior = extension_warn;
579 } else if (strcmp(behavior_string, "require") == 0) {
580 behavior = extension_require;
581 } else if (strcmp(behavior_string, "enable") == 0) {
582 behavior = extension_enable;
583 } else if (strcmp(behavior_string, "disable") == 0) {
584 behavior = extension_disable;
585 } else {
586 _mesa_glsl_error(behavior_locp, state,
587 "Unknown extension behavior `%s'",
588 behavior_string);
589 return false;
590 }
591
592 if (strcmp(name, "all") == 0) {
593 if ((behavior == extension_enable) || (behavior == extension_require)) {
594 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
595 (behavior == extension_enable)
596 ? "enable" : "require");
597 return false;
598 } else {
599 for (unsigned i = 0;
600 i < Elements(_mesa_glsl_supported_extensions); ++i) {
601 const _mesa_glsl_extension *extension
602 = &_mesa_glsl_supported_extensions[i];
603 if (extension->compatible_with_state(state)) {
604 _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
605 }
606 }
607 }
608 } else {
609 const _mesa_glsl_extension *extension = find_extension(name);
610 if (extension && extension->compatible_with_state(state)) {
611 extension->set_flags(state, behavior);
612 } else {
613 static const char *const fmt = "extension `%s' unsupported in %s shader";
614
615 if (behavior == extension_require) {
616 _mesa_glsl_error(name_locp, state, fmt,
617 name, _mesa_glsl_shader_target_name(state->target));
618 return false;
619 } else {
620 _mesa_glsl_warning(name_locp, state, fmt,
621 name, _mesa_glsl_shader_target_name(state->target));
622 }
623 }
624 }
625
626 return true;
627 }
628
629 void
630 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
631 {
632 if (q->flags.q.constant)
633 printf("const ");
634
635 if (q->flags.q.invariant)
636 printf("invariant ");
637
638 if (q->flags.q.attribute)
639 printf("attribute ");
640
641 if (q->flags.q.varying)
642 printf("varying ");
643
644 if (q->flags.q.in && q->flags.q.out)
645 printf("inout ");
646 else {
647 if (q->flags.q.in)
648 printf("in ");
649
650 if (q->flags.q.out)
651 printf("out ");
652 }
653
654 if (q->flags.q.centroid)
655 printf("centroid ");
656 if (q->flags.q.uniform)
657 printf("uniform ");
658 if (q->flags.q.smooth)
659 printf("smooth ");
660 if (q->flags.q.flat)
661 printf("flat ");
662 if (q->flags.q.noperspective)
663 printf("noperspective ");
664 }
665
666
667 void
668 ast_node::print(void) const
669 {
670 printf("unhandled node ");
671 }
672
673
674 ast_node::ast_node(void)
675 {
676 this->location.source = 0;
677 this->location.line = 0;
678 this->location.column = 0;
679 }
680
681
682 static void
683 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
684 {
685 if (is_array) {
686 printf("[ ");
687
688 if (array_size)
689 array_size->print();
690
691 printf("] ");
692 }
693 }
694
695
696 void
697 ast_compound_statement::print(void) const
698 {
699 printf("{\n");
700
701 foreach_list_const(n, &this->statements) {
702 ast_node *ast = exec_node_data(ast_node, n, link);
703 ast->print();
704 }
705
706 printf("}\n");
707 }
708
709
710 ast_compound_statement::ast_compound_statement(int new_scope,
711 ast_node *statements)
712 {
713 this->new_scope = new_scope;
714
715 if (statements != NULL) {
716 this->statements.push_degenerate_list_at_head(&statements->link);
717 }
718 }
719
720
721 void
722 ast_expression::print(void) const
723 {
724 switch (oper) {
725 case ast_assign:
726 case ast_mul_assign:
727 case ast_div_assign:
728 case ast_mod_assign:
729 case ast_add_assign:
730 case ast_sub_assign:
731 case ast_ls_assign:
732 case ast_rs_assign:
733 case ast_and_assign:
734 case ast_xor_assign:
735 case ast_or_assign:
736 subexpressions[0]->print();
737 printf("%s ", operator_string(oper));
738 subexpressions[1]->print();
739 break;
740
741 case ast_field_selection:
742 subexpressions[0]->print();
743 printf(". %s ", primary_expression.identifier);
744 break;
745
746 case ast_plus:
747 case ast_neg:
748 case ast_bit_not:
749 case ast_logic_not:
750 case ast_pre_inc:
751 case ast_pre_dec:
752 printf("%s ", operator_string(oper));
753 subexpressions[0]->print();
754 break;
755
756 case ast_post_inc:
757 case ast_post_dec:
758 subexpressions[0]->print();
759 printf("%s ", operator_string(oper));
760 break;
761
762 case ast_conditional:
763 subexpressions[0]->print();
764 printf("? ");
765 subexpressions[1]->print();
766 printf(": ");
767 subexpressions[2]->print();
768 break;
769
770 case ast_array_index:
771 subexpressions[0]->print();
772 printf("[ ");
773 subexpressions[1]->print();
774 printf("] ");
775 break;
776
777 case ast_function_call: {
778 subexpressions[0]->print();
779 printf("( ");
780
781 foreach_list_const (n, &this->expressions) {
782 if (n != this->expressions.get_head())
783 printf(", ");
784
785 ast_node *ast = exec_node_data(ast_node, n, link);
786 ast->print();
787 }
788
789 printf(") ");
790 break;
791 }
792
793 case ast_identifier:
794 printf("%s ", primary_expression.identifier);
795 break;
796
797 case ast_int_constant:
798 printf("%d ", primary_expression.int_constant);
799 break;
800
801 case ast_uint_constant:
802 printf("%u ", primary_expression.uint_constant);
803 break;
804
805 case ast_float_constant:
806 printf("%f ", primary_expression.float_constant);
807 break;
808
809 case ast_bool_constant:
810 printf("%s ",
811 primary_expression.bool_constant
812 ? "true" : "false");
813 break;
814
815 case ast_sequence: {
816 printf("( ");
817 foreach_list_const(n, & this->expressions) {
818 if (n != this->expressions.get_head())
819 printf(", ");
820
821 ast_node *ast = exec_node_data(ast_node, n, link);
822 ast->print();
823 }
824 printf(") ");
825 break;
826 }
827
828 default:
829 assert(0);
830 break;
831 }
832 }
833
834 ast_expression::ast_expression(int oper,
835 ast_expression *ex0,
836 ast_expression *ex1,
837 ast_expression *ex2)
838 {
839 this->oper = ast_operators(oper);
840 this->subexpressions[0] = ex0;
841 this->subexpressions[1] = ex1;
842 this->subexpressions[2] = ex2;
843 this->non_lvalue_description = NULL;
844 }
845
846
847 void
848 ast_expression_statement::print(void) const
849 {
850 if (expression)
851 expression->print();
852
853 printf("; ");
854 }
855
856
857 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
858 expression(ex)
859 {
860 /* empty */
861 }
862
863
864 void
865 ast_function::print(void) const
866 {
867 return_type->print();
868 printf(" %s (", identifier);
869
870 foreach_list_const(n, & this->parameters) {
871 ast_node *ast = exec_node_data(ast_node, n, link);
872 ast->print();
873 }
874
875 printf(")");
876 }
877
878
879 ast_function::ast_function(void)
880 : is_definition(false), signature(NULL)
881 {
882 /* empty */
883 }
884
885
886 void
887 ast_fully_specified_type::print(void) const
888 {
889 _mesa_ast_type_qualifier_print(& qualifier);
890 specifier->print();
891 }
892
893
894 void
895 ast_parameter_declarator::print(void) const
896 {
897 type->print();
898 if (identifier)
899 printf("%s ", identifier);
900 ast_opt_array_size_print(is_array, array_size);
901 }
902
903
904 void
905 ast_function_definition::print(void) const
906 {
907 prototype->print();
908 body->print();
909 }
910
911
912 void
913 ast_declaration::print(void) const
914 {
915 printf("%s ", identifier);
916 ast_opt_array_size_print(is_array, array_size);
917
918 if (initializer) {
919 printf("= ");
920 initializer->print();
921 }
922 }
923
924
925 ast_declaration::ast_declaration(const char *identifier, int is_array,
926 ast_expression *array_size,
927 ast_expression *initializer)
928 {
929 this->identifier = identifier;
930 this->is_array = is_array;
931 this->array_size = array_size;
932 this->initializer = initializer;
933 }
934
935
936 void
937 ast_declarator_list::print(void) const
938 {
939 assert(type || invariant);
940
941 if (type)
942 type->print();
943 else
944 printf("invariant ");
945
946 foreach_list_const (ptr, & this->declarations) {
947 if (ptr != this->declarations.get_head())
948 printf(", ");
949
950 ast_node *ast = exec_node_data(ast_node, ptr, link);
951 ast->print();
952 }
953
954 printf("; ");
955 }
956
957
958 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
959 {
960 this->type = type;
961 this->invariant = false;
962 this->ubo_qualifiers_valid = false;
963 }
964
965 void
966 ast_jump_statement::print(void) const
967 {
968 switch (mode) {
969 case ast_continue:
970 printf("continue; ");
971 break;
972 case ast_break:
973 printf("break; ");
974 break;
975 case ast_return:
976 printf("return ");
977 if (opt_return_value)
978 opt_return_value->print();
979
980 printf("; ");
981 break;
982 case ast_discard:
983 printf("discard; ");
984 break;
985 }
986 }
987
988
989 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
990 {
991 this->mode = ast_jump_modes(mode);
992
993 if (mode == ast_return)
994 opt_return_value = return_value;
995 }
996
997
998 void
999 ast_selection_statement::print(void) const
1000 {
1001 printf("if ( ");
1002 condition->print();
1003 printf(") ");
1004
1005 then_statement->print();
1006
1007 if (else_statement) {
1008 printf("else ");
1009 else_statement->print();
1010 }
1011
1012 }
1013
1014
1015 ast_selection_statement::ast_selection_statement(ast_expression *condition,
1016 ast_node *then_statement,
1017 ast_node *else_statement)
1018 {
1019 this->condition = condition;
1020 this->then_statement = then_statement;
1021 this->else_statement = else_statement;
1022 }
1023
1024
1025 void
1026 ast_switch_statement::print(void) const
1027 {
1028 printf("switch ( ");
1029 test_expression->print();
1030 printf(") ");
1031
1032 body->print();
1033 }
1034
1035
1036 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
1037 ast_node *body)
1038 {
1039 this->test_expression = test_expression;
1040 this->body = body;
1041 }
1042
1043
1044 void
1045 ast_switch_body::print(void) const
1046 {
1047 printf("{\n");
1048 if (stmts != NULL) {
1049 stmts->print();
1050 }
1051 printf("}\n");
1052 }
1053
1054
1055 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
1056 {
1057 this->stmts = stmts;
1058 }
1059
1060
1061 void ast_case_label::print(void) const
1062 {
1063 if (test_value != NULL) {
1064 printf("case ");
1065 test_value->print();
1066 printf(": ");
1067 } else {
1068 printf("default: ");
1069 }
1070 }
1071
1072
1073 ast_case_label::ast_case_label(ast_expression *test_value)
1074 {
1075 this->test_value = test_value;
1076 }
1077
1078
1079 void ast_case_label_list::print(void) const
1080 {
1081 foreach_list_const(n, & this->labels) {
1082 ast_node *ast = exec_node_data(ast_node, n, link);
1083 ast->print();
1084 }
1085 printf("\n");
1086 }
1087
1088
1089 ast_case_label_list::ast_case_label_list(void)
1090 {
1091 }
1092
1093
1094 void ast_case_statement::print(void) const
1095 {
1096 labels->print();
1097 foreach_list_const(n, & this->stmts) {
1098 ast_node *ast = exec_node_data(ast_node, n, link);
1099 ast->print();
1100 printf("\n");
1101 }
1102 }
1103
1104
1105 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
1106 {
1107 this->labels = labels;
1108 }
1109
1110
1111 void ast_case_statement_list::print(void) const
1112 {
1113 foreach_list_const(n, & this->cases) {
1114 ast_node *ast = exec_node_data(ast_node, n, link);
1115 ast->print();
1116 }
1117 }
1118
1119
1120 ast_case_statement_list::ast_case_statement_list(void)
1121 {
1122 }
1123
1124
1125 void
1126 ast_iteration_statement::print(void) const
1127 {
1128 switch (mode) {
1129 case ast_for:
1130 printf("for( ");
1131 if (init_statement)
1132 init_statement->print();
1133 printf("; ");
1134
1135 if (condition)
1136 condition->print();
1137 printf("; ");
1138
1139 if (rest_expression)
1140 rest_expression->print();
1141 printf(") ");
1142
1143 body->print();
1144 break;
1145
1146 case ast_while:
1147 printf("while ( ");
1148 if (condition)
1149 condition->print();
1150 printf(") ");
1151 body->print();
1152 break;
1153
1154 case ast_do_while:
1155 printf("do ");
1156 body->print();
1157 printf("while ( ");
1158 if (condition)
1159 condition->print();
1160 printf("); ");
1161 break;
1162 }
1163 }
1164
1165
1166 ast_iteration_statement::ast_iteration_statement(int mode,
1167 ast_node *init,
1168 ast_node *condition,
1169 ast_expression *rest_expression,
1170 ast_node *body)
1171 {
1172 this->mode = ast_iteration_modes(mode);
1173 this->init_statement = init;
1174 this->condition = condition;
1175 this->rest_expression = rest_expression;
1176 this->body = body;
1177 }
1178
1179
1180 void
1181 ast_struct_specifier::print(void) const
1182 {
1183 printf("struct %s { ", name);
1184 foreach_list_const(n, &this->declarations) {
1185 ast_node *ast = exec_node_data(ast_node, n, link);
1186 ast->print();
1187 }
1188 printf("} ");
1189 }
1190
1191
1192 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1193 ast_declarator_list *declarator_list)
1194 {
1195 if (identifier == NULL) {
1196 static unsigned anon_count = 1;
1197 identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
1198 anon_count++;
1199 }
1200 name = identifier;
1201 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1202 }
1203
1204 /**
1205 * Do the set of common optimizations passes
1206 *
1207 * \param ir List of instructions to be optimized
1208 * \param linked Is the shader linked? This enables
1209 * optimizations passes that remove code at
1210 * global scope and could cause linking to
1211 * fail.
1212 * \param uniform_locations_assigned Have locations already been assigned for
1213 * uniforms? This prevents the declarations
1214 * of unused uniforms from being removed.
1215 * The setting of this flag only matters if
1216 * \c linked is \c true.
1217 * \param max_unroll_iterations Maximum number of loop iterations to be
1218 * unrolled. Setting to 0 disables loop
1219 * unrolling.
1220 * \param options The driver's preferred shader options.
1221 */
1222 bool
1223 do_common_optimization(exec_list *ir, bool linked,
1224 bool uniform_locations_assigned,
1225 unsigned max_unroll_iterations,
1226 const struct gl_shader_compiler_options *options)
1227 {
1228 GLboolean progress = GL_FALSE;
1229
1230 progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
1231
1232 if (linked) {
1233 progress = do_function_inlining(ir) || progress;
1234 progress = do_dead_functions(ir) || progress;
1235 progress = do_structure_splitting(ir) || progress;
1236 }
1237 progress = do_if_simplification(ir) || progress;
1238 progress = opt_flatten_nested_if_blocks(ir) || progress;
1239 progress = do_copy_propagation(ir) || progress;
1240 progress = do_copy_propagation_elements(ir) || progress;
1241
1242 if (options->PreferDP4 && !linked)
1243 progress = opt_flip_matrices(ir) || progress;
1244
1245 if (linked)
1246 progress = do_dead_code(ir, uniform_locations_assigned) || progress;
1247 else
1248 progress = do_dead_code_unlinked(ir) || progress;
1249 progress = do_dead_code_local(ir) || progress;
1250 progress = do_tree_grafting(ir) || progress;
1251 progress = do_constant_propagation(ir) || progress;
1252 if (linked)
1253 progress = do_constant_variable(ir) || progress;
1254 else
1255 progress = do_constant_variable_unlinked(ir) || progress;
1256 progress = do_constant_folding(ir) || progress;
1257 progress = do_algebraic(ir) || progress;
1258 progress = do_lower_jumps(ir) || progress;
1259 progress = do_vec_index_to_swizzle(ir) || progress;
1260 progress = lower_vector_insert(ir, false) || progress;
1261 progress = do_swizzle_swizzle(ir) || progress;
1262 progress = do_noop_swizzle(ir) || progress;
1263
1264 progress = optimize_split_arrays(ir, linked) || progress;
1265 progress = optimize_redundant_jumps(ir) || progress;
1266
1267 loop_state *ls = analyze_loop_variables(ir);
1268 if (ls->loop_found) {
1269 progress = set_loop_controls(ir, ls) || progress;
1270 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
1271 }
1272 delete ls;
1273
1274 return progress;
1275 }
1276
1277 extern "C" {
1278
1279 /**
1280 * To be called at GL teardown time, this frees compiler datastructures.
1281 *
1282 * After calling this, any previously compiled shaders and shader
1283 * programs would be invalid. So this should happen at approximately
1284 * program exit.
1285 */
1286 void
1287 _mesa_destroy_shader_compiler(void)
1288 {
1289 _mesa_destroy_shader_compiler_caches();
1290
1291 _mesa_glsl_release_types();
1292 }
1293
1294 /**
1295 * Releases compiler caches to trade off performance for memory.
1296 *
1297 * Intended to be used with glReleaseShaderCompiler().
1298 */
1299 void
1300 _mesa_destroy_shader_compiler_caches(void)
1301 {
1302 _mesa_glsl_release_functions();
1303 }
1304
1305 }