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;
59 this->es_shader
= false;
60 this->ARB_texture_rectangle_enable
= true;
62 this->extensions
= &ctx
->Extensions
;
64 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
65 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
66 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
67 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
68 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
69 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
70 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
71 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
72 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
73 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
74 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
76 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
78 /* Note: Once the OpenGL 3.0 'forward compatible' context or the OpenGL 3.2
79 * Core context is supported, this logic will need change. Older versions of
80 * GLSL are no longer supported outside the compatibility contexts of 3.x.
82 this->Const
.GLSL_100ES
= ctx
->Extensions
.ARB_ES2_compatibility
;
83 this->Const
.GLSL_110
= true;
84 this->Const
.GLSL_120
= (ctx
->Const
.GLSLVersion
>= 120);
85 this->Const
.GLSL_130
= (ctx
->Const
.GLSLVersion
>= 130);
87 const unsigned lowest_version
= ctx
->Extensions
.ARB_ES2_compatibility
? 100 : 110;
88 const unsigned highest_version
= ctx
->Const
.GLSLVersion
;
89 char *supported
= ralloc_strdup(this, "");
91 for (unsigned ver
= lowest_version
; ver
<= highest_version
; ver
+= 10) {
92 const char *const prefix
= (ver
== lowest_version
)
94 : ((ver
== highest_version
) ? ", and " : ", ");
96 ralloc_asprintf_append(& supported
, "%s%d.%02d%s",
99 (ver
== 100) ? " ES" : "");
102 this->supported_version_string
= supported
;
104 if (ctx
->Const
.ForceGLSLExtensionsWarn
)
105 _mesa_glsl_process_extension("all", NULL
, "warn", NULL
, this);
109 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
112 case vertex_shader
: return "vertex";
113 case fragment_shader
: return "fragment";
116 assert(!"Should not get here.");
122 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
123 const char *fmt
, ...)
129 assert(state
->info_log
!= NULL
);
130 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): error: ",
135 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
137 ralloc_strcat(&state
->info_log
, "\n");
142 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
143 const char *fmt
, ...)
147 assert(state
->info_log
!= NULL
);
148 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): warning: ",
153 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
155 ralloc_strcat(&state
->info_log
, "\n");
160 * Enum representing the possible behaviors that can be specified in
161 * an #extension directive.
171 * Element type for _mesa_glsl_supported_extensions
173 struct _mesa_glsl_extension
{
175 * Name of the extension when referred to in a GLSL extension
180 /** True if this extension is available to vertex shaders */
183 /** True if this extension is available to geometry shaders */
186 /** True if this extension is available to fragment shaders */
189 /** True if this extension is available to desktop GL shaders */
192 /** True if this extension is available to GLES shaders */
196 * Flag in the gl_extensions struct indicating whether this
197 * extension is supported by the driver, or
198 * &gl_extensions::dummy_true if supported by all drivers.
200 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
201 * member" type, the type-safe alternative to the "offsetof" macro.
204 * - foo bar::* p declares p to be an "offset" to a field of type
205 * foo that exists within struct bar
206 * - &bar::baz computes the "offset" of field baz within struct bar
207 * - x.*p accesses the field of x that exists at "offset" p
208 * - x->*p is equivalent to (*x).*p
210 const GLboolean
gl_extensions::* supported_flag
;
213 * Flag in the _mesa_glsl_parse_state struct that should be set
214 * when this extension is enabled.
216 * See note in _mesa_glsl_extension::supported_flag about "pointer
219 bool _mesa_glsl_parse_state::* enable_flag
;
222 * Flag in the _mesa_glsl_parse_state struct that should be set
223 * when the shader requests "warn" behavior for this extension.
225 * See note in _mesa_glsl_extension::supported_flag about "pointer
228 bool _mesa_glsl_parse_state::* warn_flag
;
231 bool compatible_with_state(const _mesa_glsl_parse_state
*state
) const;
232 void set_flags(_mesa_glsl_parse_state
*state
, ext_behavior behavior
) const;
235 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
236 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
237 &_mesa_glsl_parse_state::NAME##_enable, \
238 &_mesa_glsl_parse_state::NAME##_warn }
241 * Table of extensions that can be enabled/disabled within a shader,
242 * and the conditions under which they are supported.
244 static const _mesa_glsl_extension _mesa_glsl_supported_extensions
[] = {
245 /* target availability API availability */
246 /* name VS GS FS GL ES supported flag */
247 EXT(ARB_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
248 EXT(ARB_draw_buffers
, false, false, true, true, false, dummy_true
),
249 EXT(ARB_draw_instanced
, true, false, false, true, false, ARB_draw_instanced
),
250 EXT(ARB_texture_rectangle
, true, false, true, true, false, dummy_true
),
251 EXT(EXT_texture_array
, true, false, true, true, false, EXT_texture_array
),
252 EXT(ARB_shader_texture_lod
, true, false, true, true, false, ARB_shader_texture_lod
),
253 EXT(ARB_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
254 EXT(AMD_conservative_depth
, false, false, true, true, false, ARB_conservative_depth
),
255 EXT(AMD_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
256 EXT(OES_texture_3D
, true, false, true, false, true, EXT_texture3D
),
263 * Determine whether a given extension is compatible with the target,
264 * API, and extension information in the current parser state.
266 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
269 /* Check that this extension matches the type of shader we are
272 switch (state
->target
) {
274 if (!this->avail_in_VS
) {
278 case fragment_shader
:
279 if (!this->avail_in_FS
) {
284 assert (!"Unrecognized shader target");
288 /* Check that this extension matches whether we are compiling
289 * for desktop GL or GLES.
291 if (state
->es_shader
) {
292 if (!this->avail_in_ES
) return false;
294 if (!this->avail_in_GL
) return false;
297 /* Check that this extension is supported by the OpenGL
300 * Note: the ->* operator indexes into state->extensions by the
301 * offset this->supported_flag. See
302 * _mesa_glsl_extension::supported_flag for more info.
304 return state
->extensions
->*(this->supported_flag
);
308 * Set the appropriate flags in the parser state to establish the
309 * given behavior for this extension.
311 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
312 ext_behavior behavior
) const
314 /* Note: the ->* operator indexes into state by the
315 * offsets this->enable_flag and this->warn_flag. See
316 * _mesa_glsl_extension::supported_flag for more info.
318 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
319 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
323 * Find an extension by name in _mesa_glsl_supported_extensions. If
324 * the name is not found, return NULL.
326 static const _mesa_glsl_extension
*find_extension(const char *name
)
328 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
329 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
330 return &_mesa_glsl_supported_extensions
[i
];
338 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
339 const char *behavior_string
, YYLTYPE
*behavior_locp
,
340 _mesa_glsl_parse_state
*state
)
342 ext_behavior behavior
;
343 if (strcmp(behavior_string
, "warn") == 0) {
344 behavior
= extension_warn
;
345 } else if (strcmp(behavior_string
, "require") == 0) {
346 behavior
= extension_require
;
347 } else if (strcmp(behavior_string
, "enable") == 0) {
348 behavior
= extension_enable
;
349 } else if (strcmp(behavior_string
, "disable") == 0) {
350 behavior
= extension_disable
;
352 _mesa_glsl_error(behavior_locp
, state
,
353 "Unknown extension behavior `%s'",
358 if (strcmp(name
, "all") == 0) {
359 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
360 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
361 (behavior
== extension_enable
)
362 ? "enable" : "require");
366 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
367 const _mesa_glsl_extension
*extension
368 = &_mesa_glsl_supported_extensions
[i
];
369 if (extension
->compatible_with_state(state
)) {
370 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
375 const _mesa_glsl_extension
*extension
= find_extension(name
);
376 if (extension
&& extension
->compatible_with_state(state
)) {
377 extension
->set_flags(state
, behavior
);
379 static const char *const fmt
= "extension `%s' unsupported in %s shader";
381 if (behavior
== extension_require
) {
382 _mesa_glsl_error(name_locp
, state
, fmt
,
383 name
, _mesa_glsl_shader_target_name(state
->target
));
386 _mesa_glsl_warning(name_locp
, state
, fmt
,
387 name
, _mesa_glsl_shader_target_name(state
->target
));
396 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
398 if (q
->flags
.q
.constant
)
401 if (q
->flags
.q
.invariant
)
402 printf("invariant ");
404 if (q
->flags
.q
.attribute
)
405 printf("attribute ");
407 if (q
->flags
.q
.varying
)
410 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
420 if (q
->flags
.q
.centroid
)
422 if (q
->flags
.q
.uniform
)
424 if (q
->flags
.q
.smooth
)
428 if (q
->flags
.q
.noperspective
)
429 printf("noperspective ");
434 ast_node::print(void) const
436 printf("unhandled node ");
440 ast_node::ast_node(void)
442 this->location
.source
= 0;
443 this->location
.line
= 0;
444 this->location
.column
= 0;
449 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
463 ast_compound_statement::print(void) const
467 foreach_list_const(n
, &this->statements
) {
468 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
476 ast_compound_statement::ast_compound_statement(int new_scope
,
477 ast_node
*statements
)
479 this->new_scope
= new_scope
;
481 if (statements
!= NULL
) {
482 this->statements
.push_degenerate_list_at_head(&statements
->link
);
488 ast_expression::print(void) const
502 subexpressions
[0]->print();
503 printf("%s ", operator_string(oper
));
504 subexpressions
[1]->print();
507 case ast_field_selection
:
508 subexpressions
[0]->print();
509 printf(". %s ", primary_expression
.identifier
);
518 printf("%s ", operator_string(oper
));
519 subexpressions
[0]->print();
524 subexpressions
[0]->print();
525 printf("%s ", operator_string(oper
));
528 case ast_conditional
:
529 subexpressions
[0]->print();
531 subexpressions
[1]->print();
533 subexpressions
[2]->print();
536 case ast_array_index
:
537 subexpressions
[0]->print();
539 subexpressions
[1]->print();
543 case ast_function_call
: {
544 subexpressions
[0]->print();
547 foreach_list_const (n
, &this->expressions
) {
548 if (n
!= this->expressions
.get_head())
551 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
560 printf("%s ", primary_expression
.identifier
);
563 case ast_int_constant
:
564 printf("%d ", primary_expression
.int_constant
);
567 case ast_uint_constant
:
568 printf("%u ", primary_expression
.uint_constant
);
571 case ast_float_constant
:
572 printf("%f ", primary_expression
.float_constant
);
575 case ast_bool_constant
:
577 primary_expression
.bool_constant
583 foreach_list_const(n
, & this->expressions
) {
584 if (n
!= this->expressions
.get_head())
587 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
600 ast_expression::ast_expression(int oper
,
605 this->oper
= ast_operators(oper
);
606 this->subexpressions
[0] = ex0
;
607 this->subexpressions
[1] = ex1
;
608 this->subexpressions
[2] = ex2
;
609 this->non_lvalue_description
= NULL
;
614 ast_expression_statement::print(void) const
623 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
631 ast_function::print(void) const
633 return_type
->print();
634 printf(" %s (", identifier
);
636 foreach_list_const(n
, & this->parameters
) {
637 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
645 ast_function::ast_function(void)
646 : is_definition(false), signature(NULL
)
653 ast_fully_specified_type::print(void) const
655 _mesa_ast_type_qualifier_print(& qualifier
);
661 ast_parameter_declarator::print(void) const
665 printf("%s ", identifier
);
666 ast_opt_array_size_print(is_array
, array_size
);
671 ast_function_definition::print(void) const
679 ast_declaration::print(void) const
681 printf("%s ", identifier
);
682 ast_opt_array_size_print(is_array
, array_size
);
686 initializer
->print();
691 ast_declaration::ast_declaration(char *identifier
, int is_array
,
692 ast_expression
*array_size
,
693 ast_expression
*initializer
)
695 this->identifier
= identifier
;
696 this->is_array
= is_array
;
697 this->array_size
= array_size
;
698 this->initializer
= initializer
;
703 ast_declarator_list::print(void) const
705 assert(type
|| invariant
);
710 printf("invariant ");
712 foreach_list_const (ptr
, & this->declarations
) {
713 if (ptr
!= this->declarations
.get_head())
716 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
724 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
727 this->invariant
= false;
731 ast_jump_statement::print(void) const
735 printf("continue; ");
742 if (opt_return_value
)
743 opt_return_value
->print();
754 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
756 this->mode
= ast_jump_modes(mode
);
758 if (mode
== ast_return
)
759 opt_return_value
= return_value
;
764 ast_selection_statement::print(void) const
770 then_statement
->print();
772 if (else_statement
) {
774 else_statement
->print();
780 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
781 ast_node
*then_statement
,
782 ast_node
*else_statement
)
784 this->condition
= condition
;
785 this->then_statement
= then_statement
;
786 this->else_statement
= else_statement
;
791 ast_switch_statement::print(void) const
794 test_expression
->print();
801 ast_switch_statement::ast_switch_statement(ast_expression
*test_expression
,
804 this->test_expression
= test_expression
;
810 ast_switch_body::print(void) const
820 ast_switch_body::ast_switch_body(ast_case_statement_list
*stmts
)
826 void ast_case_label::print(void) const
828 if (test_value
!= NULL
) {
838 ast_case_label::ast_case_label(ast_expression
*test_value
)
840 this->test_value
= test_value
;
844 void ast_case_label_list::print(void) const
846 foreach_list_const(n
, & this->labels
) {
847 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
854 ast_case_label_list::ast_case_label_list(void)
859 void ast_case_statement::print(void) const
862 foreach_list_const(n
, & this->stmts
) {
863 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
870 ast_case_statement::ast_case_statement(ast_case_label_list
*labels
)
872 this->labels
= labels
;
876 void ast_case_statement_list::print(void) const
878 foreach_list_const(n
, & this->cases
) {
879 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
885 ast_case_statement_list::ast_case_statement_list(void)
891 ast_iteration_statement::print(void) const
897 init_statement
->print();
905 rest_expression
->print();
931 ast_iteration_statement::ast_iteration_statement(int mode
,
934 ast_expression
*rest_expression
,
937 this->mode
= ast_iteration_modes(mode
);
938 this->init_statement
= init
;
939 this->condition
= condition
;
940 this->rest_expression
= rest_expression
;
946 ast_struct_specifier::print(void) const
948 printf("struct %s { ", name
);
949 foreach_list_const(n
, &this->declarations
) {
950 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
957 ast_struct_specifier::ast_struct_specifier(char *identifier
,
958 ast_node
*declarator_list
)
960 if (identifier
== NULL
) {
961 static unsigned anon_count
= 1;
962 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
966 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
970 * Do the set of common optimizations passes
972 * \param ir List of instructions to be optimized
973 * \param linked Is the shader linked? This enables
974 * optimizations passes that remove code at
975 * global scope and could cause linking to
977 * \param uniform_locations_assigned Have locations already been assigned for
978 * uniforms? This prevents the declarations
979 * of unused uniforms from being removed.
980 * The setting of this flag only matters if
981 * \c linked is \c true.
982 * \param max_unroll_iterations Maximum number of loop iterations to be
983 * unrolled. Setting to 0 forces all loops
987 do_common_optimization(exec_list
*ir
, bool linked
,
988 bool uniform_locations_assigned
,
989 unsigned max_unroll_iterations
)
991 GLboolean progress
= GL_FALSE
;
993 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
996 progress
= do_function_inlining(ir
) || progress
;
997 progress
= do_dead_functions(ir
) || progress
;
998 progress
= do_structure_splitting(ir
) || progress
;
1000 progress
= do_if_simplification(ir
) || progress
;
1001 progress
= do_discard_simplification(ir
) || progress
;
1002 progress
= do_copy_propagation(ir
) || progress
;
1003 progress
= do_copy_propagation_elements(ir
) || progress
;
1005 progress
= do_dead_code(ir
, uniform_locations_assigned
) || progress
;
1007 progress
= do_dead_code_unlinked(ir
) || progress
;
1008 progress
= do_dead_code_local(ir
) || progress
;
1009 progress
= do_tree_grafting(ir
) || progress
;
1010 progress
= do_constant_propagation(ir
) || progress
;
1012 progress
= do_constant_variable(ir
) || progress
;
1014 progress
= do_constant_variable_unlinked(ir
) || progress
;
1015 progress
= do_constant_folding(ir
) || progress
;
1016 progress
= do_algebraic(ir
) || progress
;
1017 progress
= do_lower_jumps(ir
) || progress
;
1018 progress
= do_vec_index_to_swizzle(ir
) || progress
;
1019 progress
= do_swizzle_swizzle(ir
) || progress
;
1020 progress
= do_noop_swizzle(ir
) || progress
;
1022 progress
= optimize_redundant_jumps(ir
) || progress
;
1024 loop_state
*ls
= analyze_loop_variables(ir
);
1025 if (ls
->loop_found
) {
1026 progress
= set_loop_controls(ir
, ls
) || progress
;
1027 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
1037 * To be called at GL teardown time, this frees compiler datastructures.
1039 * After calling this, any previously compiled shaders and shader
1040 * programs would be invalid. So this should happen at approximately
1044 _mesa_destroy_shader_compiler(void)
1046 _mesa_destroy_shader_compiler_caches();
1048 _mesa_glsl_release_types();
1052 * Releases compiler caches to trade off performance for memory.
1054 * Intended to be used with glReleaseShaderCompiler().
1057 _mesa_destroy_shader_compiler_caches(void)
1059 _mesa_glsl_release_functions();