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);
50 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context
*_ctx
,
51 GLenum target
, void *mem_ctx
)
55 case GL_VERTEX_SHADER
: this->target
= vertex_shader
; break;
56 case GL_FRAGMENT_SHADER
: this->target
= fragment_shader
; break;
57 case GL_GEOMETRY_SHADER
: this->target
= geometry_shader
; break;
61 this->translation_unit
.make_empty();
62 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
63 this->info_log
= ralloc_strdup(mem_ctx
, "");
65 this->loop_nesting_ast
= NULL
;
66 this->switch_state
.switch_nesting_ast
= NULL
;
68 this->num_builtins_to_link
= 0;
70 /* Set default language version and extensions */
71 this->language_version
= 110;
72 this->es_shader
= false;
73 this->ARB_texture_rectangle_enable
= true;
75 /* OpenGL ES 2.0 has different defaults from desktop GL. */
76 if (ctx
->API
== API_OPENGLES2
) {
77 this->language_version
= 100;
78 this->es_shader
= true;
79 this->ARB_texture_rectangle_enable
= false;
82 this->extensions
= &ctx
->Extensions
;
84 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
85 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
86 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
87 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
88 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
89 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
90 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
91 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
92 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
93 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
94 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
95 this->Const
.MinProgramTexelOffset
= ctx
->Const
.MinProgramTexelOffset
;
96 this->Const
.MaxProgramTexelOffset
= ctx
->Const
.MaxProgramTexelOffset
;
98 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
100 const unsigned lowest_version
=
101 (ctx
->API
== API_OPENGLES2
) || ctx
->Extensions
.ARB_ES2_compatibility
103 const unsigned highest_version
=
104 _mesa_is_desktop_gl(ctx
) ? ctx
->Const
.GLSLVersion
: 100;
105 char *supported
= ralloc_strdup(this, "");
107 for (unsigned ver
= lowest_version
; ver
<= highest_version
; ver
+= 10) {
108 const char *const prefix
= (ver
== lowest_version
)
110 : ((ver
== highest_version
) ? ", and " : ", ");
112 ralloc_asprintf_append(& supported
, "%s%d.%02d%s",
114 ver
/ 100, ver
% 100,
115 (ver
== 100) ? " ES" : "");
118 this->supported_version_string
= supported
;
120 if (ctx
->Const
.ForceGLSLExtensionsWarn
)
121 _mesa_glsl_process_extension("all", NULL
, "warn", NULL
, this);
123 this->default_uniform_qualifier
= new(this) ast_type_qualifier();
124 this->default_uniform_qualifier
->flags
.q
.shared
= 1;
125 this->default_uniform_qualifier
->flags
.q
.column_major
= 1;
129 * Determine whether the current GLSL version is sufficiently high to support
130 * a certain feature, and generate an error message if it isn't.
132 * \param required_glsl_version and \c required_glsl_es_version are
133 * interpreted as they are in _mesa_glsl_parse_state::is_version().
135 * \param locp is the parser location where the error should be reported.
137 * \param fmt (and additional arguments) constitute a printf-style error
138 * message to report if the version check fails. Information about the
139 * current and required GLSL versions will be appended. So, for example, if
140 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
141 * "foo unsupported") is called, the error message will be "foo unsupported in
142 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
145 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version
,
146 unsigned required_glsl_es_version
,
147 YYLTYPE
*locp
, const char *fmt
, ...)
149 if (this->is_version(required_glsl_version
, required_glsl_es_version
))
154 char *problem
= ralloc_vasprintf(this, fmt
, args
);
156 const char *glsl_version_string
157 = glsl_compute_version_string(this, false, required_glsl_version
);
158 const char *glsl_es_version_string
159 = glsl_compute_version_string(this, true, required_glsl_es_version
);
160 const char *requirement_string
= "";
161 if (required_glsl_version
&& required_glsl_es_version
) {
162 requirement_string
= ralloc_asprintf(this, " (%s or %s required)",
164 glsl_es_version_string
);
165 } else if (required_glsl_version
) {
166 requirement_string
= ralloc_asprintf(this, " (%s required)",
167 glsl_version_string
);
168 } else if (required_glsl_es_version
) {
169 requirement_string
= ralloc_asprintf(this, " (%s required)",
170 glsl_es_version_string
);
172 _mesa_glsl_error(locp
, this, "%s in %s%s.",
173 problem
, this->get_version_string(),
180 * Process a GLSL #version directive.
182 * \param version is the integer that follows the #version token.
184 * \param ident is a string identifier that follows the integer, if any is
185 * present. Otherwise NULL.
188 _mesa_glsl_parse_state::process_version_directive(YYLTYPE
*locp
, int version
,
191 bool es_token_present
= false;
193 if (strcmp(ident
, "es") == 0) {
194 es_token_present
= true;
196 _mesa_glsl_error(locp
, this,
197 "Illegal text following version number\n");
201 bool supported
= false;
203 if (es_token_present
) {
204 this->es_shader
= true;
207 _mesa_glsl_error(locp
, this,
208 "GLSL 1.00 ES should be selected using "
210 supported
= this->ctx
->API
== API_OPENGLES2
||
211 this->ctx
->Extensions
.ARB_ES2_compatibility
;
214 supported
= _mesa_is_gles3(this->ctx
) ||
215 this->ctx
->Extensions
.ARB_ES3_compatibility
;
224 this->es_shader
= true;
225 supported
= this->ctx
->API
== API_OPENGLES2
||
226 this->ctx
->Extensions
.ARB_ES2_compatibility
;
230 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
231 * the OpenGL 3.2 Core context is supported, this logic will need
232 * change. Older versions of GLSL are no longer supported
233 * outside the compatibility contexts of 3.x.
242 supported
= _mesa_is_desktop_gl(this->ctx
) &&
243 ((unsigned) version
) <= this->ctx
->Const
.GLSLVersion
;
251 this->language_version
= version
;
254 _mesa_glsl_error(locp
, this, "%s is not supported. "
255 "Supported versions are: %s\n",
256 this->get_version_string(),
257 this->supported_version_string
);
259 /* On exit, the language_version must be set to a valid value.
260 * Later calls to _mesa_glsl_initialize_types will misbehave if
261 * the version is invalid.
263 switch (this->ctx
->API
) {
264 case API_OPENGL_COMPAT
:
265 case API_OPENGL_CORE
:
266 this->language_version
= this->ctx
->Const
.GLSLVersion
;
270 assert(!"Should not get here.");
274 this->language_version
= 100;
279 if (this->language_version
>= 140) {
280 this->ARB_uniform_buffer_object_enable
= true;
283 if (this->language_version
== 300 && this->es_shader
) {
284 this->ARB_explicit_attrib_location_enable
= true;
289 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
292 case vertex_shader
: return "vertex";
293 case fragment_shader
: return "fragment";
294 case geometry_shader
: return "geometry";
297 assert(!"Should not get here.");
301 /* This helper function will append the given message to the shader's
302 info log and report it via GL_ARB_debug_output. Per that extension,
303 'type' is one of the enum values classifying the message, and
304 'id' is the implementation-defined ID of the given message. */
306 _mesa_glsl_msg(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
307 GLenum type
, GLuint id
, const char *fmt
, va_list ap
)
309 bool error
= (type
== GL_DEBUG_TYPE_ERROR_ARB
);
311 assert(state
->info_log
!= NULL
);
313 /* Get the offset that the new message will be written to. */
314 int msg_offset
= strlen(state
->info_log
);
316 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): %s: ",
320 error
? "error" : "warning");
321 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
323 const char *const msg
= &state
->info_log
[msg_offset
];
324 struct gl_context
*ctx
= state
->ctx
;
325 /* Report the error via GL_ARB_debug_output. */
327 _mesa_shader_debug(ctx
, type
, id
, msg
, strlen(msg
));
329 ralloc_strcat(&state
->info_log
, "\n");
333 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
334 const char *fmt
, ...)
337 GLenum type
= GL_DEBUG_TYPE_ERROR_ARB
;
342 _mesa_glsl_msg(locp
, state
, type
, SHADER_ERROR_UNKNOWN
, fmt
, ap
);
348 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
349 const char *fmt
, ...)
352 GLenum type
= GL_DEBUG_TYPE_OTHER_ARB
;
355 _mesa_glsl_msg(locp
, state
, type
, 0, fmt
, ap
);
361 * Enum representing the possible behaviors that can be specified in
362 * an #extension directive.
372 * Element type for _mesa_glsl_supported_extensions
374 struct _mesa_glsl_extension
{
376 * Name of the extension when referred to in a GLSL extension
381 /** True if this extension is available to vertex shaders */
384 /** True if this extension is available to geometry shaders */
387 /** True if this extension is available to fragment shaders */
390 /** True if this extension is available to desktop GL shaders */
393 /** True if this extension is available to GLES shaders */
397 * Flag in the gl_extensions struct indicating whether this
398 * extension is supported by the driver, or
399 * &gl_extensions::dummy_true if supported by all drivers.
401 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
402 * member" type, the type-safe alternative to the "offsetof" macro.
405 * - foo bar::* p declares p to be an "offset" to a field of type
406 * foo that exists within struct bar
407 * - &bar::baz computes the "offset" of field baz within struct bar
408 * - x.*p accesses the field of x that exists at "offset" p
409 * - x->*p is equivalent to (*x).*p
411 const GLboolean
gl_extensions::* supported_flag
;
414 * Flag in the _mesa_glsl_parse_state struct that should be set
415 * when this extension is enabled.
417 * See note in _mesa_glsl_extension::supported_flag about "pointer
420 bool _mesa_glsl_parse_state::* enable_flag
;
423 * Flag in the _mesa_glsl_parse_state struct that should be set
424 * when the shader requests "warn" behavior for this extension.
426 * See note in _mesa_glsl_extension::supported_flag about "pointer
429 bool _mesa_glsl_parse_state::* warn_flag
;
432 bool compatible_with_state(const _mesa_glsl_parse_state
*state
) const;
433 void set_flags(_mesa_glsl_parse_state
*state
, ext_behavior behavior
) const;
436 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
437 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
438 &_mesa_glsl_parse_state::NAME##_enable, \
439 &_mesa_glsl_parse_state::NAME##_warn }
442 * Table of extensions that can be enabled/disabled within a shader,
443 * and the conditions under which they are supported.
445 static const _mesa_glsl_extension _mesa_glsl_supported_extensions
[] = {
446 /* target availability API availability */
447 /* name VS GS FS GL ES supported flag */
448 EXT(ARB_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
449 EXT(ARB_draw_buffers
, false, false, true, true, false, dummy_true
),
450 EXT(ARB_draw_instanced
, true, false, false, true, false, ARB_draw_instanced
),
451 EXT(ARB_explicit_attrib_location
, true, false, true, true, false, ARB_explicit_attrib_location
),
452 EXT(ARB_fragment_coord_conventions
, true, false, true, true, false, ARB_fragment_coord_conventions
),
453 EXT(ARB_texture_rectangle
, true, false, true, true, false, dummy_true
),
454 EXT(EXT_texture_array
, true, false, true, true, false, EXT_texture_array
),
455 EXT(ARB_shader_texture_lod
, true, false, true, true, false, ARB_shader_texture_lod
),
456 EXT(ARB_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
457 EXT(AMD_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
458 EXT(AMD_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
459 EXT(OES_texture_3D
, true, false, true, false, true, EXT_texture3D
),
460 EXT(OES_EGL_image_external
, true, false, true, false, true, OES_EGL_image_external
),
461 EXT(ARB_shader_bit_encoding
, true, true, true, true, false, ARB_shader_bit_encoding
),
462 EXT(ARB_uniform_buffer_object
, true, false, true, true, false, ARB_uniform_buffer_object
),
463 EXT(OES_standard_derivatives
, false, false, true, false, true, OES_standard_derivatives
),
464 EXT(ARB_texture_cube_map_array
, true, false, true, true, false, ARB_texture_cube_map_array
),
471 * Determine whether a given extension is compatible with the target,
472 * API, and extension information in the current parser state.
474 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
477 /* Check that this extension matches the type of shader we are
480 switch (state
->target
) {
482 if (!this->avail_in_VS
) {
486 case geometry_shader
:
487 if (!this->avail_in_GS
) {
491 case fragment_shader
:
492 if (!this->avail_in_FS
) {
497 assert (!"Unrecognized shader target");
501 /* Check that this extension matches whether we are compiling
502 * for desktop GL or GLES.
504 if (state
->es_shader
) {
505 if (!this->avail_in_ES
) return false;
507 if (!this->avail_in_GL
) return false;
510 /* Check that this extension is supported by the OpenGL
513 * Note: the ->* operator indexes into state->extensions by the
514 * offset this->supported_flag. See
515 * _mesa_glsl_extension::supported_flag for more info.
517 return state
->extensions
->*(this->supported_flag
);
521 * Set the appropriate flags in the parser state to establish the
522 * given behavior for this extension.
524 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
525 ext_behavior behavior
) const
527 /* Note: the ->* operator indexes into state by the
528 * offsets this->enable_flag and this->warn_flag. See
529 * _mesa_glsl_extension::supported_flag for more info.
531 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
532 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
536 * Find an extension by name in _mesa_glsl_supported_extensions. If
537 * the name is not found, return NULL.
539 static const _mesa_glsl_extension
*find_extension(const char *name
)
541 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
542 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
543 return &_mesa_glsl_supported_extensions
[i
];
551 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
552 const char *behavior_string
, YYLTYPE
*behavior_locp
,
553 _mesa_glsl_parse_state
*state
)
555 ext_behavior behavior
;
556 if (strcmp(behavior_string
, "warn") == 0) {
557 behavior
= extension_warn
;
558 } else if (strcmp(behavior_string
, "require") == 0) {
559 behavior
= extension_require
;
560 } else if (strcmp(behavior_string
, "enable") == 0) {
561 behavior
= extension_enable
;
562 } else if (strcmp(behavior_string
, "disable") == 0) {
563 behavior
= extension_disable
;
565 _mesa_glsl_error(behavior_locp
, state
,
566 "Unknown extension behavior `%s'",
571 if (strcmp(name
, "all") == 0) {
572 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
573 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
574 (behavior
== extension_enable
)
575 ? "enable" : "require");
579 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
580 const _mesa_glsl_extension
*extension
581 = &_mesa_glsl_supported_extensions
[i
];
582 if (extension
->compatible_with_state(state
)) {
583 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
588 const _mesa_glsl_extension
*extension
= find_extension(name
);
589 if (extension
&& extension
->compatible_with_state(state
)) {
590 extension
->set_flags(state
, behavior
);
592 static const char *const fmt
= "extension `%s' unsupported in %s shader";
594 if (behavior
== extension_require
) {
595 _mesa_glsl_error(name_locp
, state
, fmt
,
596 name
, _mesa_glsl_shader_target_name(state
->target
));
599 _mesa_glsl_warning(name_locp
, state
, fmt
,
600 name
, _mesa_glsl_shader_target_name(state
->target
));
609 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
611 if (q
->flags
.q
.constant
)
614 if (q
->flags
.q
.invariant
)
615 printf("invariant ");
617 if (q
->flags
.q
.attribute
)
618 printf("attribute ");
620 if (q
->flags
.q
.varying
)
623 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
633 if (q
->flags
.q
.centroid
)
635 if (q
->flags
.q
.uniform
)
637 if (q
->flags
.q
.smooth
)
641 if (q
->flags
.q
.noperspective
)
642 printf("noperspective ");
647 ast_node::print(void) const
649 printf("unhandled node ");
653 ast_node::ast_node(void)
655 this->location
.source
= 0;
656 this->location
.line
= 0;
657 this->location
.column
= 0;
662 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
676 ast_compound_statement::print(void) const
680 foreach_list_const(n
, &this->statements
) {
681 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
689 ast_compound_statement::ast_compound_statement(int new_scope
,
690 ast_node
*statements
)
692 this->new_scope
= new_scope
;
694 if (statements
!= NULL
) {
695 this->statements
.push_degenerate_list_at_head(&statements
->link
);
701 ast_expression::print(void) const
715 subexpressions
[0]->print();
716 printf("%s ", operator_string(oper
));
717 subexpressions
[1]->print();
720 case ast_field_selection
:
721 subexpressions
[0]->print();
722 printf(". %s ", primary_expression
.identifier
);
731 printf("%s ", operator_string(oper
));
732 subexpressions
[0]->print();
737 subexpressions
[0]->print();
738 printf("%s ", operator_string(oper
));
741 case ast_conditional
:
742 subexpressions
[0]->print();
744 subexpressions
[1]->print();
746 subexpressions
[2]->print();
749 case ast_array_index
:
750 subexpressions
[0]->print();
752 subexpressions
[1]->print();
756 case ast_function_call
: {
757 subexpressions
[0]->print();
760 foreach_list_const (n
, &this->expressions
) {
761 if (n
!= this->expressions
.get_head())
764 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
773 printf("%s ", primary_expression
.identifier
);
776 case ast_int_constant
:
777 printf("%d ", primary_expression
.int_constant
);
780 case ast_uint_constant
:
781 printf("%u ", primary_expression
.uint_constant
);
784 case ast_float_constant
:
785 printf("%f ", primary_expression
.float_constant
);
788 case ast_bool_constant
:
790 primary_expression
.bool_constant
796 foreach_list_const(n
, & this->expressions
) {
797 if (n
!= this->expressions
.get_head())
800 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
813 ast_expression::ast_expression(int oper
,
818 this->oper
= ast_operators(oper
);
819 this->subexpressions
[0] = ex0
;
820 this->subexpressions
[1] = ex1
;
821 this->subexpressions
[2] = ex2
;
822 this->non_lvalue_description
= NULL
;
827 ast_expression_statement::print(void) const
836 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
844 ast_function::print(void) const
846 return_type
->print();
847 printf(" %s (", identifier
);
849 foreach_list_const(n
, & this->parameters
) {
850 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
858 ast_function::ast_function(void)
859 : is_definition(false), signature(NULL
)
866 ast_fully_specified_type::print(void) const
868 _mesa_ast_type_qualifier_print(& qualifier
);
874 ast_parameter_declarator::print(void) const
878 printf("%s ", identifier
);
879 ast_opt_array_size_print(is_array
, array_size
);
884 ast_function_definition::print(void) const
892 ast_declaration::print(void) const
894 printf("%s ", identifier
);
895 ast_opt_array_size_print(is_array
, array_size
);
899 initializer
->print();
904 ast_declaration::ast_declaration(const char *identifier
, int is_array
,
905 ast_expression
*array_size
,
906 ast_expression
*initializer
)
908 this->identifier
= identifier
;
909 this->is_array
= is_array
;
910 this->array_size
= array_size
;
911 this->initializer
= initializer
;
916 ast_declarator_list::print(void) const
918 assert(type
|| invariant
);
923 printf("invariant ");
925 foreach_list_const (ptr
, & this->declarations
) {
926 if (ptr
!= this->declarations
.get_head())
929 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
937 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
940 this->invariant
= false;
941 this->ubo_qualifiers_valid
= false;
945 ast_jump_statement::print(void) const
949 printf("continue; ");
956 if (opt_return_value
)
957 opt_return_value
->print();
968 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
970 this->mode
= ast_jump_modes(mode
);
972 if (mode
== ast_return
)
973 opt_return_value
= return_value
;
978 ast_selection_statement::print(void) const
984 then_statement
->print();
986 if (else_statement
) {
988 else_statement
->print();
994 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
995 ast_node
*then_statement
,
996 ast_node
*else_statement
)
998 this->condition
= condition
;
999 this->then_statement
= then_statement
;
1000 this->else_statement
= else_statement
;
1005 ast_switch_statement::print(void) const
1007 printf("switch ( ");
1008 test_expression
->print();
1015 ast_switch_statement::ast_switch_statement(ast_expression
*test_expression
,
1018 this->test_expression
= test_expression
;
1024 ast_switch_body::print(void) const
1027 if (stmts
!= NULL
) {
1034 ast_switch_body::ast_switch_body(ast_case_statement_list
*stmts
)
1036 this->stmts
= stmts
;
1040 void ast_case_label::print(void) const
1042 if (test_value
!= NULL
) {
1044 test_value
->print();
1047 printf("default: ");
1052 ast_case_label::ast_case_label(ast_expression
*test_value
)
1054 this->test_value
= test_value
;
1058 void ast_case_label_list::print(void) const
1060 foreach_list_const(n
, & this->labels
) {
1061 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1068 ast_case_label_list::ast_case_label_list(void)
1073 void ast_case_statement::print(void) const
1076 foreach_list_const(n
, & this->stmts
) {
1077 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1084 ast_case_statement::ast_case_statement(ast_case_label_list
*labels
)
1086 this->labels
= labels
;
1090 void ast_case_statement_list::print(void) const
1092 foreach_list_const(n
, & this->cases
) {
1093 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1099 ast_case_statement_list::ast_case_statement_list(void)
1105 ast_iteration_statement::print(void) const
1111 init_statement
->print();
1118 if (rest_expression
)
1119 rest_expression
->print();
1145 ast_iteration_statement::ast_iteration_statement(int mode
,
1147 ast_node
*condition
,
1148 ast_expression
*rest_expression
,
1151 this->mode
= ast_iteration_modes(mode
);
1152 this->init_statement
= init
;
1153 this->condition
= condition
;
1154 this->rest_expression
= rest_expression
;
1160 ast_struct_specifier::print(void) const
1162 printf("struct %s { ", name
);
1163 foreach_list_const(n
, &this->declarations
) {
1164 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1171 ast_struct_specifier::ast_struct_specifier(const char *identifier
,
1172 ast_declarator_list
*declarator_list
)
1174 if (identifier
== NULL
) {
1175 static unsigned anon_count
= 1;
1176 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
1180 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
1184 * Do the set of common optimizations passes
1186 * \param ir List of instructions to be optimized
1187 * \param linked Is the shader linked? This enables
1188 * optimizations passes that remove code at
1189 * global scope and could cause linking to
1191 * \param uniform_locations_assigned Have locations already been assigned for
1192 * uniforms? This prevents the declarations
1193 * of unused uniforms from being removed.
1194 * The setting of this flag only matters if
1195 * \c linked is \c true.
1196 * \param max_unroll_iterations Maximum number of loop iterations to be
1197 * unrolled. Setting to 0 forces all loops
1201 do_common_optimization(exec_list
*ir
, bool linked
,
1202 bool uniform_locations_assigned
,
1203 unsigned max_unroll_iterations
)
1205 GLboolean progress
= GL_FALSE
;
1207 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
1210 progress
= do_function_inlining(ir
) || progress
;
1211 progress
= do_dead_functions(ir
) || progress
;
1212 progress
= do_structure_splitting(ir
) || progress
;
1214 progress
= do_if_simplification(ir
) || progress
;
1215 progress
= do_copy_propagation(ir
) || progress
;
1216 progress
= do_copy_propagation_elements(ir
) || progress
;
1218 progress
= do_dead_code(ir
, uniform_locations_assigned
) || progress
;
1220 progress
= do_dead_code_unlinked(ir
) || progress
;
1221 progress
= do_dead_code_local(ir
) || progress
;
1222 progress
= do_tree_grafting(ir
) || progress
;
1223 progress
= do_constant_propagation(ir
) || progress
;
1225 progress
= do_constant_variable(ir
) || progress
;
1227 progress
= do_constant_variable_unlinked(ir
) || progress
;
1228 progress
= do_constant_folding(ir
) || progress
;
1229 progress
= do_algebraic(ir
) || progress
;
1230 progress
= do_lower_jumps(ir
) || progress
;
1231 progress
= do_vec_index_to_swizzle(ir
) || progress
;
1232 progress
= do_swizzle_swizzle(ir
) || progress
;
1233 progress
= do_noop_swizzle(ir
) || progress
;
1235 progress
= optimize_split_arrays(ir
, linked
) || progress
;
1236 progress
= optimize_redundant_jumps(ir
) || progress
;
1238 loop_state
*ls
= analyze_loop_variables(ir
);
1239 if (ls
->loop_found
) {
1240 progress
= set_loop_controls(ir
, ls
) || progress
;
1241 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
1251 * To be called at GL teardown time, this frees compiler datastructures.
1253 * After calling this, any previously compiled shaders and shader
1254 * programs would be invalid. So this should happen at approximately
1258 _mesa_destroy_shader_compiler(void)
1260 _mesa_destroy_shader_compiler_caches();
1262 _mesa_glsl_release_types();
1266 * Releases compiler caches to trade off performance for memory.
1268 * Intended to be used with glReleaseShaderCompiler().
1271 _mesa_destroy_shader_compiler_caches(void)
1273 _mesa_glsl_release_functions();