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(ctx
, fmt
, args
);
156 const char *glsl_version_string
157 = glsl_compute_version_string(ctx
, false, required_glsl_version
);
158 const char *glsl_es_version_string
159 = glsl_compute_version_string(ctx
, true, required_glsl_es_version
);
160 const char *requirement_string
= "";
161 if (required_glsl_version
&& required_glsl_es_version
) {
162 requirement_string
= ralloc_asprintf(ctx
, " (%s or %s required)",
164 glsl_es_version_string
);
165 } else if (required_glsl_version
) {
166 requirement_string
= ralloc_asprintf(ctx
, " (%s required)",
167 glsl_version_string
);
168 } else if (required_glsl_es_version
) {
169 requirement_string
= ralloc_asprintf(ctx
, " (%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;
285 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
288 case vertex_shader
: return "vertex";
289 case fragment_shader
: return "fragment";
290 case geometry_shader
: return "geometry";
293 assert(!"Should not get here.");
297 /* This helper function will append the given message to the shader's
298 info log and report it via GL_ARB_debug_output. Per that extension,
299 'type' is one of the enum values classifying the message, and
300 'id' is the implementation-defined ID of the given message. */
302 _mesa_glsl_msg(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
303 GLenum type
, GLuint id
, const char *fmt
, va_list ap
)
305 bool error
= (type
== GL_DEBUG_TYPE_ERROR_ARB
);
307 assert(state
->info_log
!= NULL
);
309 /* Get the offset that the new message will be written to. */
310 int msg_offset
= strlen(state
->info_log
);
312 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): %s: ",
316 error
? "error" : "warning");
317 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
319 const char *const msg
= &state
->info_log
[msg_offset
];
320 struct gl_context
*ctx
= state
->ctx
;
321 /* Report the error via GL_ARB_debug_output. */
323 _mesa_shader_debug(ctx
, type
, id
, msg
, strlen(msg
));
325 ralloc_strcat(&state
->info_log
, "\n");
329 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
330 const char *fmt
, ...)
333 GLenum type
= GL_DEBUG_TYPE_ERROR_ARB
;
338 _mesa_glsl_msg(locp
, state
, type
, SHADER_ERROR_UNKNOWN
, fmt
, ap
);
344 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
345 const char *fmt
, ...)
348 GLenum type
= GL_DEBUG_TYPE_OTHER_ARB
;
351 _mesa_glsl_msg(locp
, state
, type
, 0, fmt
, ap
);
357 * Enum representing the possible behaviors that can be specified in
358 * an #extension directive.
368 * Element type for _mesa_glsl_supported_extensions
370 struct _mesa_glsl_extension
{
372 * Name of the extension when referred to in a GLSL extension
377 /** True if this extension is available to vertex shaders */
380 /** True if this extension is available to geometry shaders */
383 /** True if this extension is available to fragment shaders */
386 /** True if this extension is available to desktop GL shaders */
389 /** True if this extension is available to GLES shaders */
393 * Flag in the gl_extensions struct indicating whether this
394 * extension is supported by the driver, or
395 * &gl_extensions::dummy_true if supported by all drivers.
397 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
398 * member" type, the type-safe alternative to the "offsetof" macro.
401 * - foo bar::* p declares p to be an "offset" to a field of type
402 * foo that exists within struct bar
403 * - &bar::baz computes the "offset" of field baz within struct bar
404 * - x.*p accesses the field of x that exists at "offset" p
405 * - x->*p is equivalent to (*x).*p
407 const GLboolean
gl_extensions::* supported_flag
;
410 * Flag in the _mesa_glsl_parse_state struct that should be set
411 * when this extension is enabled.
413 * See note in _mesa_glsl_extension::supported_flag about "pointer
416 bool _mesa_glsl_parse_state::* enable_flag
;
419 * Flag in the _mesa_glsl_parse_state struct that should be set
420 * when the shader requests "warn" behavior for this extension.
422 * See note in _mesa_glsl_extension::supported_flag about "pointer
425 bool _mesa_glsl_parse_state::* warn_flag
;
428 bool compatible_with_state(const _mesa_glsl_parse_state
*state
) const;
429 void set_flags(_mesa_glsl_parse_state
*state
, ext_behavior behavior
) const;
432 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
433 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
434 &_mesa_glsl_parse_state::NAME##_enable, \
435 &_mesa_glsl_parse_state::NAME##_warn }
438 * Table of extensions that can be enabled/disabled within a shader,
439 * and the conditions under which they are supported.
441 static const _mesa_glsl_extension _mesa_glsl_supported_extensions
[] = {
442 /* target availability API availability */
443 /* name VS GS FS GL ES supported flag */
444 EXT(ARB_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
445 EXT(ARB_draw_buffers
, false, false, true, true, false, dummy_true
),
446 EXT(ARB_draw_instanced
, true, false, false, true, false, ARB_draw_instanced
),
447 EXT(ARB_explicit_attrib_location
, true, false, true, true, false, ARB_explicit_attrib_location
),
448 EXT(ARB_fragment_coord_conventions
, true, false, true, true, false, ARB_fragment_coord_conventions
),
449 EXT(ARB_texture_rectangle
, true, false, true, true, false, dummy_true
),
450 EXT(EXT_texture_array
, true, false, true, true, false, EXT_texture_array
),
451 EXT(ARB_shader_texture_lod
, true, false, true, true, false, ARB_shader_texture_lod
),
452 EXT(ARB_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
453 EXT(AMD_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
454 EXT(AMD_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
455 EXT(OES_texture_3D
, true, false, true, false, true, EXT_texture3D
),
456 EXT(OES_EGL_image_external
, true, false, true, false, true, OES_EGL_image_external
),
457 EXT(ARB_shader_bit_encoding
, true, true, true, true, false, ARB_shader_bit_encoding
),
458 EXT(ARB_uniform_buffer_object
, true, false, true, true, false, ARB_uniform_buffer_object
),
459 EXT(OES_standard_derivatives
, false, false, true, false, true, OES_standard_derivatives
),
460 EXT(ARB_texture_cube_map_array
, true, false, true, true, false, ARB_texture_cube_map_array
),
467 * Determine whether a given extension is compatible with the target,
468 * API, and extension information in the current parser state.
470 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
473 /* Check that this extension matches the type of shader we are
476 switch (state
->target
) {
478 if (!this->avail_in_VS
) {
482 case geometry_shader
:
483 if (!this->avail_in_GS
) {
487 case fragment_shader
:
488 if (!this->avail_in_FS
) {
493 assert (!"Unrecognized shader target");
497 /* Check that this extension matches whether we are compiling
498 * for desktop GL or GLES.
500 if (state
->es_shader
) {
501 if (!this->avail_in_ES
) return false;
503 if (!this->avail_in_GL
) return false;
506 /* Check that this extension is supported by the OpenGL
509 * Note: the ->* operator indexes into state->extensions by the
510 * offset this->supported_flag. See
511 * _mesa_glsl_extension::supported_flag for more info.
513 return state
->extensions
->*(this->supported_flag
);
517 * Set the appropriate flags in the parser state to establish the
518 * given behavior for this extension.
520 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
521 ext_behavior behavior
) const
523 /* Note: the ->* operator indexes into state by the
524 * offsets this->enable_flag and this->warn_flag. See
525 * _mesa_glsl_extension::supported_flag for more info.
527 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
528 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
532 * Find an extension by name in _mesa_glsl_supported_extensions. If
533 * the name is not found, return NULL.
535 static const _mesa_glsl_extension
*find_extension(const char *name
)
537 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
538 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
539 return &_mesa_glsl_supported_extensions
[i
];
547 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
548 const char *behavior_string
, YYLTYPE
*behavior_locp
,
549 _mesa_glsl_parse_state
*state
)
551 ext_behavior behavior
;
552 if (strcmp(behavior_string
, "warn") == 0) {
553 behavior
= extension_warn
;
554 } else if (strcmp(behavior_string
, "require") == 0) {
555 behavior
= extension_require
;
556 } else if (strcmp(behavior_string
, "enable") == 0) {
557 behavior
= extension_enable
;
558 } else if (strcmp(behavior_string
, "disable") == 0) {
559 behavior
= extension_disable
;
561 _mesa_glsl_error(behavior_locp
, state
,
562 "Unknown extension behavior `%s'",
567 if (strcmp(name
, "all") == 0) {
568 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
569 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
570 (behavior
== extension_enable
)
571 ? "enable" : "require");
575 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
576 const _mesa_glsl_extension
*extension
577 = &_mesa_glsl_supported_extensions
[i
];
578 if (extension
->compatible_with_state(state
)) {
579 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
584 const _mesa_glsl_extension
*extension
= find_extension(name
);
585 if (extension
&& extension
->compatible_with_state(state
)) {
586 extension
->set_flags(state
, behavior
);
588 static const char *const fmt
= "extension `%s' unsupported in %s shader";
590 if (behavior
== extension_require
) {
591 _mesa_glsl_error(name_locp
, state
, fmt
,
592 name
, _mesa_glsl_shader_target_name(state
->target
));
595 _mesa_glsl_warning(name_locp
, state
, fmt
,
596 name
, _mesa_glsl_shader_target_name(state
->target
));
605 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
607 if (q
->flags
.q
.constant
)
610 if (q
->flags
.q
.invariant
)
611 printf("invariant ");
613 if (q
->flags
.q
.attribute
)
614 printf("attribute ");
616 if (q
->flags
.q
.varying
)
619 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
629 if (q
->flags
.q
.centroid
)
631 if (q
->flags
.q
.uniform
)
633 if (q
->flags
.q
.smooth
)
637 if (q
->flags
.q
.noperspective
)
638 printf("noperspective ");
643 ast_node::print(void) const
645 printf("unhandled node ");
649 ast_node::ast_node(void)
651 this->location
.source
= 0;
652 this->location
.line
= 0;
653 this->location
.column
= 0;
658 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
672 ast_compound_statement::print(void) const
676 foreach_list_const(n
, &this->statements
) {
677 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
685 ast_compound_statement::ast_compound_statement(int new_scope
,
686 ast_node
*statements
)
688 this->new_scope
= new_scope
;
690 if (statements
!= NULL
) {
691 this->statements
.push_degenerate_list_at_head(&statements
->link
);
697 ast_expression::print(void) const
711 subexpressions
[0]->print();
712 printf("%s ", operator_string(oper
));
713 subexpressions
[1]->print();
716 case ast_field_selection
:
717 subexpressions
[0]->print();
718 printf(". %s ", primary_expression
.identifier
);
727 printf("%s ", operator_string(oper
));
728 subexpressions
[0]->print();
733 subexpressions
[0]->print();
734 printf("%s ", operator_string(oper
));
737 case ast_conditional
:
738 subexpressions
[0]->print();
740 subexpressions
[1]->print();
742 subexpressions
[2]->print();
745 case ast_array_index
:
746 subexpressions
[0]->print();
748 subexpressions
[1]->print();
752 case ast_function_call
: {
753 subexpressions
[0]->print();
756 foreach_list_const (n
, &this->expressions
) {
757 if (n
!= this->expressions
.get_head())
760 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
769 printf("%s ", primary_expression
.identifier
);
772 case ast_int_constant
:
773 printf("%d ", primary_expression
.int_constant
);
776 case ast_uint_constant
:
777 printf("%u ", primary_expression
.uint_constant
);
780 case ast_float_constant
:
781 printf("%f ", primary_expression
.float_constant
);
784 case ast_bool_constant
:
786 primary_expression
.bool_constant
792 foreach_list_const(n
, & this->expressions
) {
793 if (n
!= this->expressions
.get_head())
796 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
809 ast_expression::ast_expression(int oper
,
814 this->oper
= ast_operators(oper
);
815 this->subexpressions
[0] = ex0
;
816 this->subexpressions
[1] = ex1
;
817 this->subexpressions
[2] = ex2
;
818 this->non_lvalue_description
= NULL
;
823 ast_expression_statement::print(void) const
832 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
840 ast_function::print(void) const
842 return_type
->print();
843 printf(" %s (", identifier
);
845 foreach_list_const(n
, & this->parameters
) {
846 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
854 ast_function::ast_function(void)
855 : is_definition(false), signature(NULL
)
862 ast_fully_specified_type::print(void) const
864 _mesa_ast_type_qualifier_print(& qualifier
);
870 ast_parameter_declarator::print(void) const
874 printf("%s ", identifier
);
875 ast_opt_array_size_print(is_array
, array_size
);
880 ast_function_definition::print(void) const
888 ast_declaration::print(void) const
890 printf("%s ", identifier
);
891 ast_opt_array_size_print(is_array
, array_size
);
895 initializer
->print();
900 ast_declaration::ast_declaration(const char *identifier
, int is_array
,
901 ast_expression
*array_size
,
902 ast_expression
*initializer
)
904 this->identifier
= identifier
;
905 this->is_array
= is_array
;
906 this->array_size
= array_size
;
907 this->initializer
= initializer
;
912 ast_declarator_list::print(void) const
914 assert(type
|| invariant
);
919 printf("invariant ");
921 foreach_list_const (ptr
, & this->declarations
) {
922 if (ptr
!= this->declarations
.get_head())
925 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
933 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
936 this->invariant
= false;
937 this->ubo_qualifiers_valid
= false;
941 ast_jump_statement::print(void) const
945 printf("continue; ");
952 if (opt_return_value
)
953 opt_return_value
->print();
964 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
966 this->mode
= ast_jump_modes(mode
);
968 if (mode
== ast_return
)
969 opt_return_value
= return_value
;
974 ast_selection_statement::print(void) const
980 then_statement
->print();
982 if (else_statement
) {
984 else_statement
->print();
990 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
991 ast_node
*then_statement
,
992 ast_node
*else_statement
)
994 this->condition
= condition
;
995 this->then_statement
= then_statement
;
996 this->else_statement
= else_statement
;
1001 ast_switch_statement::print(void) const
1003 printf("switch ( ");
1004 test_expression
->print();
1011 ast_switch_statement::ast_switch_statement(ast_expression
*test_expression
,
1014 this->test_expression
= test_expression
;
1020 ast_switch_body::print(void) const
1023 if (stmts
!= NULL
) {
1030 ast_switch_body::ast_switch_body(ast_case_statement_list
*stmts
)
1032 this->stmts
= stmts
;
1036 void ast_case_label::print(void) const
1038 if (test_value
!= NULL
) {
1040 test_value
->print();
1043 printf("default: ");
1048 ast_case_label::ast_case_label(ast_expression
*test_value
)
1050 this->test_value
= test_value
;
1054 void ast_case_label_list::print(void) const
1056 foreach_list_const(n
, & this->labels
) {
1057 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1064 ast_case_label_list::ast_case_label_list(void)
1069 void ast_case_statement::print(void) const
1072 foreach_list_const(n
, & this->stmts
) {
1073 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1080 ast_case_statement::ast_case_statement(ast_case_label_list
*labels
)
1082 this->labels
= labels
;
1086 void ast_case_statement_list::print(void) const
1088 foreach_list_const(n
, & this->cases
) {
1089 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1095 ast_case_statement_list::ast_case_statement_list(void)
1101 ast_iteration_statement::print(void) const
1107 init_statement
->print();
1114 if (rest_expression
)
1115 rest_expression
->print();
1141 ast_iteration_statement::ast_iteration_statement(int mode
,
1143 ast_node
*condition
,
1144 ast_expression
*rest_expression
,
1147 this->mode
= ast_iteration_modes(mode
);
1148 this->init_statement
= init
;
1149 this->condition
= condition
;
1150 this->rest_expression
= rest_expression
;
1156 ast_struct_specifier::print(void) const
1158 printf("struct %s { ", name
);
1159 foreach_list_const(n
, &this->declarations
) {
1160 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
1167 ast_struct_specifier::ast_struct_specifier(const char *identifier
,
1168 ast_declarator_list
*declarator_list
)
1170 if (identifier
== NULL
) {
1171 static unsigned anon_count
= 1;
1172 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
1176 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
1180 * Do the set of common optimizations passes
1182 * \param ir List of instructions to be optimized
1183 * \param linked Is the shader linked? This enables
1184 * optimizations passes that remove code at
1185 * global scope and could cause linking to
1187 * \param uniform_locations_assigned Have locations already been assigned for
1188 * uniforms? This prevents the declarations
1189 * of unused uniforms from being removed.
1190 * The setting of this flag only matters if
1191 * \c linked is \c true.
1192 * \param max_unroll_iterations Maximum number of loop iterations to be
1193 * unrolled. Setting to 0 forces all loops
1197 do_common_optimization(exec_list
*ir
, bool linked
,
1198 bool uniform_locations_assigned
,
1199 unsigned max_unroll_iterations
)
1201 GLboolean progress
= GL_FALSE
;
1203 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
1206 progress
= do_function_inlining(ir
) || progress
;
1207 progress
= do_dead_functions(ir
) || progress
;
1208 progress
= do_structure_splitting(ir
) || progress
;
1210 progress
= do_if_simplification(ir
) || progress
;
1211 progress
= do_copy_propagation(ir
) || progress
;
1212 progress
= do_copy_propagation_elements(ir
) || progress
;
1214 progress
= do_dead_code(ir
, uniform_locations_assigned
) || progress
;
1216 progress
= do_dead_code_unlinked(ir
) || progress
;
1217 progress
= do_dead_code_local(ir
) || progress
;
1218 progress
= do_tree_grafting(ir
) || progress
;
1219 progress
= do_constant_propagation(ir
) || progress
;
1221 progress
= do_constant_variable(ir
) || progress
;
1223 progress
= do_constant_variable_unlinked(ir
) || progress
;
1224 progress
= do_constant_folding(ir
) || progress
;
1225 progress
= do_algebraic(ir
) || progress
;
1226 progress
= do_lower_jumps(ir
) || progress
;
1227 progress
= do_vec_index_to_swizzle(ir
) || progress
;
1228 progress
= do_swizzle_swizzle(ir
) || progress
;
1229 progress
= do_noop_swizzle(ir
) || progress
;
1231 progress
= optimize_split_arrays(ir
, linked
) || progress
;
1232 progress
= optimize_redundant_jumps(ir
) || progress
;
1234 loop_state
*ls
= analyze_loop_variables(ir
);
1235 if (ls
->loop_found
) {
1236 progress
= set_loop_controls(ir
, ls
) || progress
;
1237 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
1247 * To be called at GL teardown time, this frees compiler datastructures.
1249 * After calling this, any previously compiled shaders and shader
1250 * programs would be invalid. So this should happen at approximately
1254 _mesa_destroy_shader_compiler(void)
1256 _mesa_destroy_shader_compiler_caches();
1258 _mesa_glsl_release_types();
1262 * Releases compiler caches to trade off performance for memory.
1264 * Intended to be used with glReleaseShaderCompiler().
1267 _mesa_destroy_shader_compiler_caches(void)
1269 _mesa_glsl_release_functions();