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