2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
29 #include "main/core.h" /* for struct gl_context */
30 #include "main/context.h"
35 #include "glsl_parser_extras.h"
36 #include "glsl_parser.h"
37 #include "ir_optimization.h"
38 #include "loop_analysis.h"
41 * Format a short human-readable description of the given GLSL version.
44 glsl_compute_version_string(void *mem_ctx
, bool is_es
, unsigned version
)
46 return ralloc_asprintf(mem_ctx
, "GLSL%s %d.%02d", is_es
? " ES" : "",
47 version
/ 100, version
% 100);
51 static unsigned known_desktop_glsl_versions
[] =
52 { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430 };
55 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context
*_ctx
,
56 GLenum target
, void *mem_ctx
)
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;
66 this->translation_unit
.make_empty();
67 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
68 this->info_log
= ralloc_strdup(mem_ctx
, "");
70 this->loop_nesting_ast
= NULL
;
71 this->switch_state
.switch_nesting_ast
= NULL
;
73 this->num_builtins_to_link
= 0;
75 /* Set default language version and extensions */
76 this->language_version
= 110;
77 this->es_shader
= false;
78 this->ARB_texture_rectangle_enable
= true;
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;
87 this->extensions
= &ctx
->Extensions
;
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
;
103 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
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.
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
++;
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
++;
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
++;
132 assert(this->num_supported_versions
133 <= ARRAY_SIZE(this->supported_versions
));
135 /* Create a string for use in error messages to tell the user which GLSL
136 * versions are supported.
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)
143 : ((i
== this->num_supported_versions
- 1) ? ", and " : ", ");
144 const char *const suffix
= (this->supported_versions
[i
].es
) ? " ES" : "";
146 ralloc_asprintf_append(& supported
, "%s%u.%02u%s",
148 ver
/ 100, ver
% 100,
152 this->supported_version_string
= supported
;
154 if (ctx
->Const
.ForceGLSLExtensionsWarn
)
155 _mesa_glsl_process_extension("all", NULL
, "warn", NULL
, this);
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;
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.
166 * \param required_glsl_version and \c required_glsl_es_version are
167 * interpreted as they are in _mesa_glsl_parse_state::is_version().
169 * \param locp is the parser location where the error should be reported.
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)".
179 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version
,
180 unsigned required_glsl_es_version
,
181 YYLTYPE
*locp
, const char *fmt
, ...)
183 if (this->is_version(required_glsl_version
, required_glsl_es_version
))
188 char *problem
= ralloc_vasprintf(this, fmt
, 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)",
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
);
206 _mesa_glsl_error(locp
, this, "%s in %s%s.",
207 problem
, this->get_version_string(),
214 * Process a GLSL #version directive.
216 * \param version is the integer that follows the #version token.
218 * \param ident is a string identifier that follows the integer, if any is
219 * present. Otherwise NULL.
222 _mesa_glsl_parse_state::process_version_directive(YYLTYPE
*locp
, int version
,
225 bool es_token_present
= false;
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.
234 } else if (strcmp(ident
, "compatibility") == 0) {
235 _mesa_glsl_error(locp
, this,
236 "The compatibility profile is not supported.\n");
238 _mesa_glsl_error(locp
, this,
239 "\"%s\" is not a valid shading language profile; "
240 "if present, it must be \"core\".\n", ident
);
243 _mesa_glsl_error(locp
, this,
244 "Illegal text following version number\n");
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 "
255 this->es_shader
= true;
259 this->language_version
= version
;
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
) {
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
);
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.
280 switch (this->ctx
->API
) {
281 case API_OPENGL_COMPAT
:
282 case API_OPENGL_CORE
:
283 this->language_version
= this->ctx
->Const
.GLSLVersion
;
287 assert(!"Should not get here.");
291 this->language_version
= 100;
296 if (this->language_version
>= 140) {
297 this->ARB_uniform_buffer_object_enable
= true;
300 if (this->language_version
== 300 && this->es_shader
) {
301 this->ARB_explicit_attrib_location_enable
= true;
306 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
309 case vertex_shader
: return "vertex";
310 case fragment_shader
: return "fragment";
311 case geometry_shader
: return "geometry";
314 assert(!"Should not get here.");
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. */
323 _mesa_glsl_msg(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
324 GLenum type
, const char *fmt
, va_list ap
)
326 bool error
= (type
== MESA_DEBUG_TYPE_ERROR
);
329 assert(state
->info_log
!= NULL
);
331 /* Get the offset that the new message will be written to. */
332 int msg_offset
= strlen(state
->info_log
);
334 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): %s: ",
338 error
? "error" : "warning");
339 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
341 const char *const msg
= &state
->info_log
[msg_offset
];
342 struct gl_context
*ctx
= state
->ctx
;
344 /* Report the error via GL_ARB_debug_output. */
345 _mesa_shader_debug(ctx
, type
, &msg_id
, msg
, strlen(msg
));
347 ralloc_strcat(&state
->info_log
, "\n");
351 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
352 const char *fmt
, ...)
359 _mesa_glsl_msg(locp
, state
, MESA_DEBUG_TYPE_ERROR
, fmt
, ap
);
365 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
366 const char *fmt
, ...)
371 _mesa_glsl_msg(locp
, state
, MESA_DEBUG_TYPE_OTHER
, fmt
, ap
);
377 * Enum representing the possible behaviors that can be specified in
378 * an #extension directive.
388 * Element type for _mesa_glsl_supported_extensions
390 struct _mesa_glsl_extension
{
392 * Name of the extension when referred to in a GLSL extension
397 /** True if this extension is available to vertex shaders */
400 /** True if this extension is available to geometry shaders */
403 /** True if this extension is available to fragment shaders */
406 /** True if this extension is available to desktop GL shaders */
409 /** True if this extension is available to GLES shaders */
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.
417 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
418 * member" type, the type-safe alternative to the "offsetof" macro.
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
427 const GLboolean
gl_extensions::* supported_flag
;
430 * Flag in the _mesa_glsl_parse_state struct that should be set
431 * when this extension is enabled.
433 * See note in _mesa_glsl_extension::supported_flag about "pointer
436 bool _mesa_glsl_parse_state::* enable_flag
;
439 * Flag in the _mesa_glsl_parse_state struct that should be set
440 * when the shader requests "warn" behavior for this extension.
442 * See note in _mesa_glsl_extension::supported_flag about "pointer
445 bool _mesa_glsl_parse_state::* warn_flag
;
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;
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 }
458 * Table of extensions that can be enabled/disabled within a shader,
459 * and the conditions under which they are supported.
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_shading_language_420pack
, true, true, true, true, false, ARB_shading_language_420pack
),
483 EXT(ARB_texture_multisample
, true, false, true, true, false, ARB_texture_multisample
),
484 EXT(ARB_texture_query_lod
, false, false, true, true, false, ARB_texture_query_lod
),
485 EXT(ARB_gpu_shader5
, true, true, true, true, false, ARB_gpu_shader5
),
486 EXT(AMD_vertex_shader_layer
, true, false, false, true, false, AMD_vertex_shader_layer
),
493 * Determine whether a given extension is compatible with the target,
494 * API, and extension information in the current parser state.
496 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
499 /* Check that this extension matches the type of shader we are
502 switch (state
->target
) {
504 if (!this->avail_in_VS
) {
508 case geometry_shader
:
509 if (!this->avail_in_GS
) {
513 case fragment_shader
:
514 if (!this->avail_in_FS
) {
519 assert (!"Unrecognized shader target");
523 /* Check that this extension matches whether we are compiling
524 * for desktop GL or GLES.
526 if (state
->es_shader
) {
527 if (!this->avail_in_ES
) return false;
529 if (!this->avail_in_GL
) return false;
532 /* Check that this extension is supported by the OpenGL
535 * Note: the ->* operator indexes into state->extensions by the
536 * offset this->supported_flag. See
537 * _mesa_glsl_extension::supported_flag for more info.
539 return state
->extensions
->*(this->supported_flag
);
543 * Set the appropriate flags in the parser state to establish the
544 * given behavior for this extension.
546 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
547 ext_behavior behavior
) const
549 /* Note: the ->* operator indexes into state by the
550 * offsets this->enable_flag and this->warn_flag. See
551 * _mesa_glsl_extension::supported_flag for more info.
553 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
554 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
558 * Find an extension by name in _mesa_glsl_supported_extensions. If
559 * the name is not found, return NULL.
561 static const _mesa_glsl_extension
*find_extension(const char *name
)
563 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
564 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
565 return &_mesa_glsl_supported_extensions
[i
];
573 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
574 const char *behavior_string
, YYLTYPE
*behavior_locp
,
575 _mesa_glsl_parse_state
*state
)
577 ext_behavior behavior
;
578 if (strcmp(behavior_string
, "warn") == 0) {
579 behavior
= extension_warn
;
580 } else if (strcmp(behavior_string
, "require") == 0) {
581 behavior
= extension_require
;
582 } else if (strcmp(behavior_string
, "enable") == 0) {
583 behavior
= extension_enable
;
584 } else if (strcmp(behavior_string
, "disable") == 0) {
585 behavior
= extension_disable
;
587 _mesa_glsl_error(behavior_locp
, state
,
588 "Unknown extension behavior `%s'",
593 if (strcmp(name
, "all") == 0) {
594 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
595 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
596 (behavior
== extension_enable
)
597 ? "enable" : "require");
601 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
602 const _mesa_glsl_extension
*extension
603 = &_mesa_glsl_supported_extensions
[i
];
604 if (extension
->compatible_with_state(state
)) {
605 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
610 const _mesa_glsl_extension
*extension
= find_extension(name
);
611 if (extension
&& extension
->compatible_with_state(state
)) {
612 extension
->set_flags(state
, behavior
);
614 static const char *const fmt
= "extension `%s' unsupported in %s shader";
616 if (behavior
== extension_require
) {
617 _mesa_glsl_error(name_locp
, state
, fmt
,
618 name
, _mesa_glsl_shader_target_name(state
->target
));
621 _mesa_glsl_warning(name_locp
, state
, fmt
,
622 name
, _mesa_glsl_shader_target_name(state
->target
));
631 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
633 if (q
->flags
.q
.constant
)
636 if (q
->flags
.q
.invariant
)
637 printf("invariant ");
639 if (q
->flags
.q
.attribute
)
640 printf("attribute ");
642 if (q
->flags
.q
.varying
)
645 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
655 if (q
->flags
.q
.centroid
)
657 if (q
->flags
.q
.uniform
)
659 if (q
->flags
.q
.smooth
)
663 if (q
->flags
.q
.noperspective
)
664 printf("noperspective ");
669 ast_node::print(void) const
671 printf("unhandled node ");
675 ast_node::ast_node(void)
677 this->location
.source
= 0;
678 this->location
.line
= 0;
679 this->location
.column
= 0;
684 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
698 ast_compound_statement::print(void) const
702 foreach_list_const(n
, &this->statements
) {
703 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
711 ast_compound_statement::ast_compound_statement(int new_scope
,
712 ast_node
*statements
)
714 this->new_scope
= new_scope
;
716 if (statements
!= NULL
) {
717 this->statements
.push_degenerate_list_at_head(&statements
->link
);
723 ast_expression::print(void) const
737 subexpressions
[0]->print();
738 printf("%s ", operator_string(oper
));
739 subexpressions
[1]->print();
742 case ast_field_selection
:
743 subexpressions
[0]->print();
744 printf(". %s ", primary_expression
.identifier
);
753 printf("%s ", operator_string(oper
));
754 subexpressions
[0]->print();
759 subexpressions
[0]->print();
760 printf("%s ", operator_string(oper
));
763 case ast_conditional
:
764 subexpressions
[0]->print();
766 subexpressions
[1]->print();
768 subexpressions
[2]->print();
771 case ast_array_index
:
772 subexpressions
[0]->print();
774 subexpressions
[1]->print();
778 case ast_function_call
: {
779 subexpressions
[0]->print();
782 foreach_list_const (n
, &this->expressions
) {
783 if (n
!= this->expressions
.get_head())
786 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
795 printf("%s ", primary_expression
.identifier
);
798 case ast_int_constant
:
799 printf("%d ", primary_expression
.int_constant
);
802 case ast_uint_constant
:
803 printf("%u ", primary_expression
.uint_constant
);
806 case ast_float_constant
:
807 printf("%f ", primary_expression
.float_constant
);
810 case ast_bool_constant
:
812 primary_expression
.bool_constant
818 foreach_list_const(n
, & this->expressions
) {
819 if (n
!= this->expressions
.get_head())
822 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
835 ast_expression::ast_expression(int oper
,
840 this->oper
= ast_operators(oper
);
841 this->subexpressions
[0] = ex0
;
842 this->subexpressions
[1] = ex1
;
843 this->subexpressions
[2] = ex2
;
844 this->non_lvalue_description
= NULL
;
849 ast_expression_statement::print(void) const
858 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
866 ast_function::print(void) const
868 return_type
->print();
869 printf(" %s (", identifier
);
871 foreach_list_const(n
, & this->parameters
) {
872 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
880 ast_function::ast_function(void)
881 : is_definition(false), signature(NULL
)
888 ast_fully_specified_type::print(void) const
890 _mesa_ast_type_qualifier_print(& qualifier
);
896 ast_parameter_declarator::print(void) const
900 printf("%s ", identifier
);
901 ast_opt_array_size_print(is_array
, array_size
);
906 ast_function_definition::print(void) const
914 ast_declaration::print(void) const
916 printf("%s ", identifier
);
917 ast_opt_array_size_print(is_array
, array_size
);
921 initializer
->print();
926 ast_declaration::ast_declaration(const char *identifier
, int is_array
,
927 ast_expression
*array_size
,
928 ast_expression
*initializer
)
930 this->identifier
= identifier
;
931 this->is_array
= is_array
;
932 this->array_size
= array_size
;
933 this->initializer
= initializer
;
938 ast_declarator_list::print(void) const
940 assert(type
|| invariant
);
945 printf("invariant ");
947 foreach_list_const (ptr
, & this->declarations
) {
948 if (ptr
!= this->declarations
.get_head())
951 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
959 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
962 this->invariant
= false;
963 this->ubo_qualifiers_valid
= false;
967 ast_jump_statement::print(void) const
971 printf("continue; ");
978 if (opt_return_value
)
979 opt_return_value
->print();
990 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
992 this->mode
= ast_jump_modes(mode
);
994 if (mode
== ast_return
)
995 opt_return_value
= return_value
;
1000 ast_selection_statement::print(void) const
1006 then_statement
->print();
1008 if (else_statement
) {
1010 else_statement
->print();
1016 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
1017 ast_node
*then_statement
,
1018 ast_node
*else_statement
)
1020 this->condition
= condition
;
1021 this->then_statement
= then_statement
;
1022 this->else_statement
= else_statement
;
1027 ast_switch_statement::print(void) const
1029 printf("switch ( ");
1030 test_expression
->print();
1037 ast_switch_statement::ast_switch_statement(ast_expression
*test_expression
,
1040 this->test_expression
= test_expression
;
1046 ast_switch_body::print(void) const
1049 if (stmts
!= NULL
) {
1056 ast_switch_body::ast_switch_body(ast_case_statement_list
*stmts
)
1058 this->stmts
= stmts
;
1062 void ast_case_label::print(void) const
1064 if (test_value
!= NULL
) {
1066 test_value
->print();
1069 printf("default: ");
1074 ast_case_label::ast_case_label(ast_expression
*test_value
)
1076 this->test_value
= test_value
;
1080 void ast_case_label_list::print(void) const
1082 foreach_list_const(n
, & this->labels
) {
1083 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1090 ast_case_label_list::ast_case_label_list(void)
1095 void ast_case_statement::print(void) const
1098 foreach_list_const(n
, & this->stmts
) {
1099 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1106 ast_case_statement::ast_case_statement(ast_case_label_list
*labels
)
1108 this->labels
= labels
;
1112 void ast_case_statement_list::print(void) const
1114 foreach_list_const(n
, & this->cases
) {
1115 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1121 ast_case_statement_list::ast_case_statement_list(void)
1127 ast_iteration_statement::print(void) const
1133 init_statement
->print();
1140 if (rest_expression
)
1141 rest_expression
->print();
1167 ast_iteration_statement::ast_iteration_statement(int mode
,
1169 ast_node
*condition
,
1170 ast_expression
*rest_expression
,
1173 this->mode
= ast_iteration_modes(mode
);
1174 this->init_statement
= init
;
1175 this->condition
= condition
;
1176 this->rest_expression
= rest_expression
;
1182 ast_struct_specifier::print(void) const
1184 printf("struct %s { ", name
);
1185 foreach_list_const(n
, &this->declarations
) {
1186 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1193 ast_struct_specifier::ast_struct_specifier(const char *identifier
,
1194 ast_declarator_list
*declarator_list
)
1196 if (identifier
== NULL
) {
1197 static unsigned anon_count
= 1;
1198 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
1202 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
1206 * Do the set of common optimizations passes
1208 * \param ir List of instructions to be optimized
1209 * \param linked Is the shader linked? This enables
1210 * optimizations passes that remove code at
1211 * global scope and could cause linking to
1213 * \param uniform_locations_assigned Have locations already been assigned for
1214 * uniforms? This prevents the declarations
1215 * of unused uniforms from being removed.
1216 * The setting of this flag only matters if
1217 * \c linked is \c true.
1218 * \param max_unroll_iterations Maximum number of loop iterations to be
1219 * unrolled. Setting to 0 disables loop
1221 * \param options The driver's preferred shader options.
1224 do_common_optimization(exec_list
*ir
, bool linked
,
1225 bool uniform_locations_assigned
,
1226 unsigned max_unroll_iterations
,
1227 const struct gl_shader_compiler_options
*options
)
1229 GLboolean progress
= GL_FALSE
;
1231 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
1234 progress
= do_function_inlining(ir
) || progress
;
1235 progress
= do_dead_functions(ir
) || progress
;
1236 progress
= do_structure_splitting(ir
) || progress
;
1238 progress
= do_if_simplification(ir
) || progress
;
1239 progress
= opt_flatten_nested_if_blocks(ir
) || progress
;
1240 progress
= do_copy_propagation(ir
) || progress
;
1241 progress
= do_copy_propagation_elements(ir
) || progress
;
1243 if (options
->PreferDP4
&& !linked
)
1244 progress
= opt_flip_matrices(ir
) || progress
;
1247 progress
= do_dead_code(ir
, uniform_locations_assigned
) || progress
;
1249 progress
= do_dead_code_unlinked(ir
) || progress
;
1250 progress
= do_dead_code_local(ir
) || progress
;
1251 progress
= do_tree_grafting(ir
) || progress
;
1252 progress
= do_constant_propagation(ir
) || progress
;
1254 progress
= do_constant_variable(ir
) || progress
;
1256 progress
= do_constant_variable_unlinked(ir
) || progress
;
1257 progress
= do_constant_folding(ir
) || progress
;
1258 progress
= do_algebraic(ir
) || progress
;
1259 progress
= do_lower_jumps(ir
) || progress
;
1260 progress
= do_vec_index_to_swizzle(ir
) || progress
;
1261 progress
= lower_vector_insert(ir
, false) || progress
;
1262 progress
= do_swizzle_swizzle(ir
) || progress
;
1263 progress
= do_noop_swizzle(ir
) || progress
;
1265 progress
= optimize_split_arrays(ir
, linked
) || progress
;
1266 progress
= optimize_redundant_jumps(ir
) || progress
;
1268 loop_state
*ls
= analyze_loop_variables(ir
);
1269 if (ls
->loop_found
) {
1270 progress
= set_loop_controls(ir
, ls
) || progress
;
1271 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
1281 * To be called at GL teardown time, this frees compiler datastructures.
1283 * After calling this, any previously compiled shaders and shader
1284 * programs would be invalid. So this should happen at approximately
1288 _mesa_destroy_shader_compiler(void)
1290 _mesa_destroy_shader_compiler_caches();
1292 _mesa_glsl_release_types();
1296 * Releases compiler caches to trade off performance for memory.
1298 * Intended to be used with glReleaseShaderCompiler().
1301 _mesa_destroy_shader_compiler_caches(void)
1303 _mesa_glsl_release_functions();