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 */
34 #include "glsl_parser_extras.h"
35 #include "glsl_parser.h"
36 #include "ir_optimization.h"
37 #include "loop_analysis.h"
39 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context
*ctx
,
40 GLenum target
, void *mem_ctx
)
43 case GL_VERTEX_SHADER
: this->target
= vertex_shader
; break;
44 case GL_FRAGMENT_SHADER
: this->target
= fragment_shader
; break;
48 this->translation_unit
.make_empty();
49 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
50 this->info_log
= ralloc_strdup(mem_ctx
, "");
52 this->loop_nesting_ast
= NULL
;
53 this->switch_state
.switch_nesting_ast
= NULL
;
55 this->num_builtins_to_link
= 0;
57 /* Set default language version and extensions */
58 this->language_version
= 110;
60 this->extensions
= &ctx
->Extensions
;
62 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
63 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
64 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
65 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
66 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
67 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
68 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
69 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
70 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
71 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
72 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
74 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
76 /* Note: Once the OpenGL 3.0 'forward compatible' context or the OpenGL 3.2
77 * Core context is supported, this logic will need change. Older versions of
78 * GLSL are no longer supported outside the compatibility contexts of 3.x.
80 this->Const
.GLSL_110
= true;
81 this->Const
.GLSL_120
= (ctx
->Const
.GLSLVersion
>= 120);
82 this->Const
.GLSL_130
= (ctx
->Const
.GLSLVersion
>= 130);
84 const unsigned lowest_version
= 110;
85 const unsigned highest_version
= ctx
->Const
.GLSLVersion
;
86 char *supported
= ralloc_strdup(this, "");
88 for (unsigned ver
= lowest_version
; ver
<= highest_version
; ver
+= 10) {
89 const char *const prefix
= (ver
== lowest_version
)
91 : ((ver
== highest_version
) ? ", and " : ", ");
93 ralloc_asprintf_append(& supported
, "%s%d.%02d%s",
96 (ver
== 100) ? " ES" : "");
99 this->supported_version_string
= supported
;
101 if (ctx
->Const
.ForceGLSLExtensionsWarn
)
102 _mesa_glsl_process_extension("all", NULL
, "warn", NULL
, this);
106 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
109 case vertex_shader
: return "vertex";
110 case fragment_shader
: return "fragment";
113 assert(!"Should not get here.");
119 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
120 const char *fmt
, ...)
126 assert(state
->info_log
!= NULL
);
127 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): error: ",
132 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
134 ralloc_strcat(&state
->info_log
, "\n");
139 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
140 const char *fmt
, ...)
144 assert(state
->info_log
!= NULL
);
145 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): warning: ",
150 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
152 ralloc_strcat(&state
->info_log
, "\n");
157 * Enum representing the possible behaviors that can be specified in
158 * an #extension directive.
168 * Element type for _mesa_glsl_supported_extensions
170 struct _mesa_glsl_extension
{
172 * Name of the extension when referred to in a GLSL extension
177 /** True if this extension is available to vertex shaders */
180 /** True if this extension is available to geometry shaders */
183 /** True if this extension is available to fragment shaders */
186 /** True if this extension is available to desktop GL shaders */
189 /** True if this extension is available to GLES shaders */
193 * Flag in the gl_extensions struct indicating whether this
194 * extension is supported by the driver, or
195 * &gl_extensions::dummy_true if supported by all drivers.
197 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
198 * member" type, the type-safe alternative to the "offsetof" macro.
201 * - foo bar::* p declares p to be an "offset" to a field of type
202 * foo that exists within struct bar
203 * - &bar::baz computes the "offset" of field baz within struct bar
204 * - x.*p accesses the field of x that exists at "offset" p
205 * - x->*p is equivalent to (*x).*p
207 const GLboolean
gl_extensions::* supported_flag
;
210 * Flag in the _mesa_glsl_parse_state struct that should be set
211 * when this extension is enabled.
213 * See note in _mesa_glsl_extension::supported_flag about "pointer
216 bool _mesa_glsl_parse_state::* enable_flag
;
219 * Flag in the _mesa_glsl_parse_state struct that should be set
220 * when the shader requests "warn" behavior for this extension.
222 * See note in _mesa_glsl_extension::supported_flag about "pointer
225 bool _mesa_glsl_parse_state::* warn_flag
;
228 bool compatible_with_state(const _mesa_glsl_parse_state
*state
) const;
229 void set_flags(_mesa_glsl_parse_state
*state
, ext_behavior behavior
) const;
232 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
233 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
234 &_mesa_glsl_parse_state::NAME##_enable, \
235 &_mesa_glsl_parse_state::NAME##_warn }
238 * Table of extensions that can be enabled/disabled within a shader,
239 * and the conditions under which they are supported.
241 static const _mesa_glsl_extension _mesa_glsl_supported_extensions
[] = {
242 /* target availability API availability */
243 /* name VS GS FS GL ES supported flag */
244 EXT(ARB_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
245 EXT(ARB_draw_buffers
, false, false, true, true, false, dummy_true
),
246 EXT(ARB_shader_texture_lod
, true, false, true, true, false, ARB_shader_texture_lod
),
247 EXT(ARB_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
248 EXT(AMD_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
249 EXT(AMD_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
250 EXT(OES_texture_3D
, true, false, true, false, true, EXT_texture3D
),
257 * Determine whether a given extension is compatible with the target,
258 * API, and extension information in the current parser state.
260 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
263 /* Check that this extension matches the type of shader we are
266 switch (state
->target
) {
268 if (!this->avail_in_VS
) {
272 case fragment_shader
:
273 if (!this->avail_in_FS
) {
278 assert (!"Unrecognized shader target");
282 /* Check that this extension matches whether we are compiling
283 * for desktop GL or GLES.
285 if (!this->avail_in_GL
) return false;
287 /* Check that this extension is supported by the OpenGL
290 * Note: the ->* operator indexes into state->extensions by the
291 * offset this->supported_flag. See
292 * _mesa_glsl_extension::supported_flag for more info.
294 return state
->extensions
->*(this->supported_flag
);
298 * Set the appropriate flags in the parser state to establish the
299 * given behavior for this extension.
301 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
302 ext_behavior behavior
) const
304 /* Note: the ->* operator indexes into state by the
305 * offsets this->enable_flag and this->warn_flag. See
306 * _mesa_glsl_extension::supported_flag for more info.
308 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
309 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
313 * Find an extension by name in _mesa_glsl_supported_extensions. If
314 * the name is not found, return NULL.
316 static const _mesa_glsl_extension
*find_extension(const char *name
)
318 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
319 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
320 return &_mesa_glsl_supported_extensions
[i
];
328 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
329 const char *behavior_string
, YYLTYPE
*behavior_locp
,
330 _mesa_glsl_parse_state
*state
)
332 ext_behavior behavior
;
333 if (strcmp(behavior_string
, "warn") == 0) {
334 behavior
= extension_warn
;
335 } else if (strcmp(behavior_string
, "require") == 0) {
336 behavior
= extension_require
;
337 } else if (strcmp(behavior_string
, "enable") == 0) {
338 behavior
= extension_enable
;
339 } else if (strcmp(behavior_string
, "disable") == 0) {
340 behavior
= extension_disable
;
342 _mesa_glsl_error(behavior_locp
, state
,
343 "Unknown extension behavior `%s'",
348 if (strcmp(name
, "all") == 0) {
349 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
350 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
351 (behavior
== extension_enable
)
352 ? "enable" : "require");
356 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
357 const _mesa_glsl_extension
*extension
358 = &_mesa_glsl_supported_extensions
[i
];
359 if (extension
->compatible_with_state(state
)) {
360 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
365 const _mesa_glsl_extension
*extension
= find_extension(name
);
366 if (extension
&& extension
->compatible_with_state(state
)) {
367 extension
->set_flags(state
, behavior
);
369 static const char *const fmt
= "extension `%s' unsupported in %s shader";
371 if (behavior
== extension_require
) {
372 _mesa_glsl_error(name_locp
, state
, fmt
,
373 name
, _mesa_glsl_shader_target_name(state
->target
));
376 _mesa_glsl_warning(name_locp
, state
, fmt
,
377 name
, _mesa_glsl_shader_target_name(state
->target
));
386 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
388 if (q
->flags
.q
.constant
)
391 if (q
->flags
.q
.invariant
)
392 printf("invariant ");
394 if (q
->flags
.q
.attribute
)
395 printf("attribute ");
397 if (q
->flags
.q
.varying
)
400 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
410 if (q
->flags
.q
.centroid
)
412 if (q
->flags
.q
.uniform
)
414 if (q
->flags
.q
.smooth
)
418 if (q
->flags
.q
.noperspective
)
419 printf("noperspective ");
424 ast_node::print(void) const
426 printf("unhandled node ");
430 ast_node::ast_node(void)
432 this->location
.source
= 0;
433 this->location
.line
= 0;
434 this->location
.column
= 0;
439 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
453 ast_compound_statement::print(void) const
457 foreach_list_const(n
, &this->statements
) {
458 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
466 ast_compound_statement::ast_compound_statement(int new_scope
,
467 ast_node
*statements
)
469 this->new_scope
= new_scope
;
471 if (statements
!= NULL
) {
472 this->statements
.push_degenerate_list_at_head(&statements
->link
);
478 ast_expression::print(void) const
492 subexpressions
[0]->print();
493 printf("%s ", operator_string(oper
));
494 subexpressions
[1]->print();
497 case ast_field_selection
:
498 subexpressions
[0]->print();
499 printf(". %s ", primary_expression
.identifier
);
508 printf("%s ", operator_string(oper
));
509 subexpressions
[0]->print();
514 subexpressions
[0]->print();
515 printf("%s ", operator_string(oper
));
518 case ast_conditional
:
519 subexpressions
[0]->print();
521 subexpressions
[1]->print();
523 subexpressions
[2]->print();
526 case ast_array_index
:
527 subexpressions
[0]->print();
529 subexpressions
[1]->print();
533 case ast_function_call
: {
534 subexpressions
[0]->print();
537 foreach_list_const (n
, &this->expressions
) {
538 if (n
!= this->expressions
.get_head())
541 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
550 printf("%s ", primary_expression
.identifier
);
553 case ast_int_constant
:
554 printf("%d ", primary_expression
.int_constant
);
557 case ast_uint_constant
:
558 printf("%u ", primary_expression
.uint_constant
);
561 case ast_float_constant
:
562 printf("%f ", primary_expression
.float_constant
);
565 case ast_bool_constant
:
567 primary_expression
.bool_constant
573 foreach_list_const(n
, & this->expressions
) {
574 if (n
!= this->expressions
.get_head())
577 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
590 ast_expression::ast_expression(int oper
,
595 this->oper
= ast_operators(oper
);
596 this->subexpressions
[0] = ex0
;
597 this->subexpressions
[1] = ex1
;
598 this->subexpressions
[2] = ex2
;
599 this->non_lvalue_description
= NULL
;
604 ast_expression_statement::print(void) const
613 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
621 ast_function::print(void) const
623 return_type
->print();
624 printf(" %s (", identifier
);
626 foreach_list_const(n
, & this->parameters
) {
627 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
635 ast_function::ast_function(void)
636 : is_definition(false), signature(NULL
)
643 ast_fully_specified_type::print(void) const
645 _mesa_ast_type_qualifier_print(& qualifier
);
651 ast_parameter_declarator::print(void) const
655 printf("%s ", identifier
);
656 ast_opt_array_size_print(is_array
, array_size
);
661 ast_function_definition::print(void) const
669 ast_declaration::print(void) const
671 printf("%s ", identifier
);
672 ast_opt_array_size_print(is_array
, array_size
);
676 initializer
->print();
681 ast_declaration::ast_declaration(char *identifier
, int is_array
,
682 ast_expression
*array_size
,
683 ast_expression
*initializer
)
685 this->identifier
= identifier
;
686 this->is_array
= is_array
;
687 this->array_size
= array_size
;
688 this->initializer
= initializer
;
693 ast_declarator_list::print(void) const
695 assert(type
|| invariant
);
700 printf("invariant ");
702 foreach_list_const (ptr
, & this->declarations
) {
703 if (ptr
!= this->declarations
.get_head())
706 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
714 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
717 this->invariant
= false;
721 ast_jump_statement::print(void) const
725 printf("continue; ");
732 if (opt_return_value
)
733 opt_return_value
->print();
744 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
746 this->mode
= ast_jump_modes(mode
);
748 if (mode
== ast_return
)
749 opt_return_value
= return_value
;
754 ast_selection_statement::print(void) const
760 then_statement
->print();
762 if (else_statement
) {
764 else_statement
->print();
770 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
771 ast_node
*then_statement
,
772 ast_node
*else_statement
)
774 this->condition
= condition
;
775 this->then_statement
= then_statement
;
776 this->else_statement
= else_statement
;
781 ast_switch_statement::print(void) const
784 test_expression
->print();
791 ast_switch_statement::ast_switch_statement(ast_expression
*test_expression
,
794 this->test_expression
= test_expression
;
800 ast_switch_body::print(void) const
810 ast_switch_body::ast_switch_body(ast_case_statement_list
*stmts
)
816 void ast_case_label::print(void) const
818 if (test_value
!= NULL
) {
828 ast_case_label::ast_case_label(ast_expression
*test_value
)
830 this->test_value
= test_value
;
834 void ast_case_label_list::print(void) const
836 foreach_list_const(n
, & this->labels
) {
837 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
844 ast_case_label_list::ast_case_label_list(void)
849 void ast_case_statement::print(void) const
852 foreach_list_const(n
, & this->stmts
) {
853 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
860 ast_case_statement::ast_case_statement(ast_case_label_list
*labels
)
862 this->labels
= labels
;
866 void ast_case_statement_list::print(void) const
868 foreach_list_const(n
, & this->cases
) {
869 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
875 ast_case_statement_list::ast_case_statement_list(void)
881 ast_iteration_statement::print(void) const
887 init_statement
->print();
895 rest_expression
->print();
921 ast_iteration_statement::ast_iteration_statement(int mode
,
924 ast_expression
*rest_expression
,
927 this->mode
= ast_iteration_modes(mode
);
928 this->init_statement
= init
;
929 this->condition
= condition
;
930 this->rest_expression
= rest_expression
;
936 ast_struct_specifier::print(void) const
938 printf("struct %s { ", name
);
939 foreach_list_const(n
, &this->declarations
) {
940 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
947 ast_struct_specifier::ast_struct_specifier(char *identifier
,
948 ast_node
*declarator_list
)
950 if (identifier
== NULL
) {
951 static unsigned anon_count
= 1;
952 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
956 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
960 * Do the set of common optimizations passes
962 * \param ir List of instructions to be optimized
963 * \param linked Is the shader linked? This enables
964 * optimizations passes that remove code at
965 * global scope and could cause linking to
967 * \param uniform_locations_assigned Have locations already been assigned for
968 * uniforms? This prevents the declarations
969 * of unused uniforms from being removed.
970 * The setting of this flag only matters if
971 * \c linked is \c true.
972 * \param max_unroll_iterations Maximum number of loop iterations to be
973 * unrolled. Setting to 0 forces all loops
977 do_common_optimization(exec_list
*ir
, bool linked
,
978 bool uniform_locations_assigned
,
979 unsigned max_unroll_iterations
)
981 GLboolean progress
= GL_FALSE
;
983 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
986 progress
= do_function_inlining(ir
) || progress
;
987 progress
= do_dead_functions(ir
) || progress
;
988 progress
= do_structure_splitting(ir
) || progress
;
990 progress
= do_if_simplification(ir
) || progress
;
991 progress
= do_discard_simplification(ir
) || progress
;
992 progress
= do_copy_propagation(ir
) || progress
;
993 progress
= do_copy_propagation_elements(ir
) || progress
;
995 progress
= do_dead_code(ir
, uniform_locations_assigned
) || progress
;
997 progress
= do_dead_code_unlinked(ir
) || progress
;
998 progress
= do_dead_code_local(ir
) || progress
;
999 progress
= do_tree_grafting(ir
) || progress
;
1000 progress
= do_constant_propagation(ir
) || progress
;
1002 progress
= do_constant_variable(ir
) || progress
;
1004 progress
= do_constant_variable_unlinked(ir
) || progress
;
1005 progress
= do_constant_folding(ir
) || progress
;
1006 progress
= do_algebraic(ir
) || progress
;
1007 progress
= do_lower_jumps(ir
) || progress
;
1008 progress
= do_vec_index_to_swizzle(ir
) || progress
;
1009 progress
= do_swizzle_swizzle(ir
) || progress
;
1010 progress
= do_noop_swizzle(ir
) || progress
;
1012 progress
= optimize_redundant_jumps(ir
) || progress
;
1014 loop_state
*ls
= analyze_loop_variables(ir
);
1015 if (ls
->loop_found
) {
1016 progress
= set_loop_controls(ir
, ls
) || progress
;
1017 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
1027 * To be called at GL teardown time, this frees compiler datastructures.
1029 * After calling this, any previously compiled shaders and shader
1030 * programs would be invalid. So this should happen at approximately
1034 _mesa_destroy_shader_compiler(void)
1036 _mesa_destroy_shader_compiler_caches();
1038 _mesa_glsl_release_types();
1042 * Releases compiler caches to trade off performance for memory.
1044 * Intended to be used with glReleaseShaderCompiler().
1047 _mesa_destroy_shader_compiler_caches(void)
1049 _mesa_glsl_release_functions();