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;
45 case GL_GEOMETRY_SHADER
: this->target
= geometry_shader
; break;
49 this->translation_unit
.make_empty();
50 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
51 this->info_log
= ralloc_strdup(mem_ctx
, "");
53 this->loop_or_switch_nesting
= NULL
;
55 /* Set default language version and extensions */
56 this->language_version
= 110;
57 this->es_shader
= false;
58 this->ARB_texture_rectangle_enable
= true;
60 /* OpenGL ES 2.0 has different defaults from desktop GL. */
61 if (ctx
->API
== API_OPENGLES2
) {
62 this->language_version
= 100;
63 this->es_shader
= true;
64 this->ARB_texture_rectangle_enable
= false;
67 this->extensions
= &ctx
->Extensions
;
69 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
70 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
71 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
72 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
73 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
74 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
75 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
76 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
77 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
78 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
79 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
81 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
83 /* Note: Once the OpenGL 3.0 'forward compatible' context or the OpenGL 3.2
84 * Core context is supported, this logic will need change. Older versions of
85 * GLSL are no longer supported outside the compatibility contexts of 3.x.
87 this->Const
.GLSL_100ES
= (ctx
->API
== API_OPENGLES2
)
88 || ctx
->Extensions
.ARB_ES2_compatibility
;
89 this->Const
.GLSL_110
= (ctx
->API
== API_OPENGL
);
90 this->Const
.GLSL_120
= (ctx
->API
== API_OPENGL
)
91 && (ctx
->Const
.GLSLVersion
>= 120);
92 this->Const
.GLSL_130
= (ctx
->API
== API_OPENGL
)
93 && (ctx
->Const
.GLSLVersion
>= 130);
95 const unsigned lowest_version
=
96 (ctx
->API
== API_OPENGLES2
) || ctx
->Extensions
.ARB_ES2_compatibility
98 const unsigned highest_version
=
99 (ctx
->API
== API_OPENGL
) ? ctx
->Const
.GLSLVersion
: 100;
100 char *supported
= ralloc_strdup(this, "");
102 for (unsigned ver
= lowest_version
; ver
<= highest_version
; ver
+= 10) {
103 const char *const prefix
= (ver
== lowest_version
)
105 : ((ver
== highest_version
) ? ", and " : ", ");
107 ralloc_asprintf_append(& supported
, "%s%d.%02d%s",
109 ver
/ 100, ver
% 100,
110 (ver
== 100) ? " ES" : "");
113 this->supported_version_string
= supported
;
117 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
120 case vertex_shader
: return "vertex";
121 case fragment_shader
: return "fragment";
122 case geometry_shader
: return "geometry";
125 assert(!"Should not get here.");
131 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
132 const char *fmt
, ...)
138 assert(state
->info_log
!= NULL
);
139 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): error: ",
144 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
146 ralloc_strcat(&state
->info_log
, "\n");
151 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
152 const char *fmt
, ...)
156 assert(state
->info_log
!= NULL
);
157 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): warning: ",
162 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
164 ralloc_strcat(&state
->info_log
, "\n");
169 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
170 const char *behavior
, YYLTYPE
*behavior_locp
,
171 _mesa_glsl_parse_state
*state
)
180 if (strcmp(behavior
, "warn") == 0) {
181 ext_mode
= extension_warn
;
182 } else if (strcmp(behavior
, "require") == 0) {
183 ext_mode
= extension_require
;
184 } else if (strcmp(behavior
, "enable") == 0) {
185 ext_mode
= extension_enable
;
186 } else if (strcmp(behavior
, "disable") == 0) {
187 ext_mode
= extension_disable
;
189 _mesa_glsl_error(behavior_locp
, state
,
190 "Unknown extension behavior `%s'",
195 bool unsupported
= false;
197 if (strcmp(name
, "all") == 0) {
198 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
199 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
200 (ext_mode
== extension_enable
)
201 ? "enable" : "require");
204 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
205 /* This extension is only supported in fragment shaders.
207 if (state
->target
!= fragment_shader
) {
210 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
211 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
213 } else if (strcmp(name
, "GL_ARB_draw_instanced") == 0) {
214 state
->ARB_draw_instanced_enable
= (ext_mode
!= extension_disable
);
215 state
->ARB_draw_instanced_warn
= (ext_mode
== extension_warn
);
217 /* This extension is only supported in vertex shaders.
219 unsupported
= (state
->target
!= vertex_shader
)
220 || !state
->extensions
->ARB_draw_instanced
;
221 } else if (strcmp(name
, "GL_ARB_explicit_attrib_location") == 0) {
222 state
->ARB_explicit_attrib_location_enable
=
223 (ext_mode
!= extension_disable
);
224 state
->ARB_explicit_attrib_location_warn
=
225 (ext_mode
== extension_warn
);
227 unsupported
= !state
->extensions
->ARB_explicit_attrib_location
;
228 } else if (strcmp(name
, "GL_ARB_fragment_coord_conventions") == 0) {
229 state
->ARB_fragment_coord_conventions_enable
=
230 (ext_mode
!= extension_disable
);
231 state
->ARB_fragment_coord_conventions_warn
=
232 (ext_mode
== extension_warn
);
234 unsupported
= !state
->extensions
->ARB_fragment_coord_conventions
;
235 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
236 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
237 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
238 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
239 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
240 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
242 unsupported
= !state
->extensions
->EXT_texture_array
;
243 } else if (strcmp(name
, "GL_ARB_shader_stencil_export") == 0) {
244 state
->ARB_shader_stencil_export_enable
= (ext_mode
!= extension_disable
);
245 state
->ARB_shader_stencil_export_warn
= (ext_mode
== extension_warn
);
247 /* This extension is only supported in fragment shaders.
249 unsupported
= (state
->target
!= fragment_shader
)
250 || !state
->extensions
->ARB_shader_stencil_export
;
251 } else if (strcmp(name
, "GL_AMD_conservative_depth") == 0) {
252 /* The AMD_conservative spec does not forbid requiring the extension in
255 state
->AMD_conservative_depth_enable
= (ext_mode
!= extension_disable
);
256 state
->AMD_conservative_depth_warn
= (ext_mode
== extension_warn
);
257 unsupported
= !state
->extensions
->AMD_conservative_depth
;
258 } else if (strcmp(name
, "GL_AMD_shader_stencil_export") == 0) {
259 state
->AMD_shader_stencil_export_enable
= (ext_mode
!= extension_disable
);
260 state
->AMD_shader_stencil_export_warn
= (ext_mode
== extension_warn
);
262 /* This extension is only supported in fragment shaders.
263 * Both the ARB and AMD variants share the same ARB flag
266 unsupported
= (state
->target
!= fragment_shader
)
267 || !state
->extensions
->ARB_shader_stencil_export
;
268 } else if (strcmp(name
, "GL_OES_texture_3D") == 0 && state
->es_shader
) {
269 state
->OES_texture_3D_enable
= (ext_mode
!= extension_disable
);
270 state
->OES_texture_3D_warn
= (ext_mode
== extension_warn
);
272 unsupported
= !state
->extensions
->EXT_texture3D
;
278 static const char *const fmt
= "extension `%s' unsupported in %s shader";
280 if (ext_mode
== extension_require
) {
281 _mesa_glsl_error(name_locp
, state
, fmt
,
282 name
, _mesa_glsl_shader_target_name(state
->target
));
285 _mesa_glsl_warning(name_locp
, state
, fmt
,
286 name
, _mesa_glsl_shader_target_name(state
->target
));
294 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
296 if (q
->flags
.q
.constant
)
299 if (q
->flags
.q
.invariant
)
300 printf("invariant ");
302 if (q
->flags
.q
.attribute
)
303 printf("attribute ");
305 if (q
->flags
.q
.varying
)
308 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
318 if (q
->flags
.q
.centroid
)
320 if (q
->flags
.q
.uniform
)
322 if (q
->flags
.q
.smooth
)
326 if (q
->flags
.q
.noperspective
)
327 printf("noperspective ");
332 ast_node::print(void) const
334 printf("unhandled node ");
338 ast_node::ast_node(void)
340 this->location
.source
= 0;
341 this->location
.line
= 0;
342 this->location
.column
= 0;
347 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
361 ast_compound_statement::print(void) const
365 foreach_list_const(n
, &this->statements
) {
366 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
374 ast_compound_statement::ast_compound_statement(int new_scope
,
375 ast_node
*statements
)
377 this->new_scope
= new_scope
;
379 if (statements
!= NULL
) {
380 this->statements
.push_degenerate_list_at_head(&statements
->link
);
386 ast_expression::print(void) const
400 subexpressions
[0]->print();
401 printf("%s ", operator_string(oper
));
402 subexpressions
[1]->print();
405 case ast_field_selection
:
406 subexpressions
[0]->print();
407 printf(". %s ", primary_expression
.identifier
);
416 printf("%s ", operator_string(oper
));
417 subexpressions
[0]->print();
422 subexpressions
[0]->print();
423 printf("%s ", operator_string(oper
));
426 case ast_conditional
:
427 subexpressions
[0]->print();
429 subexpressions
[1]->print();
431 subexpressions
[1]->print();
434 case ast_array_index
:
435 subexpressions
[0]->print();
437 subexpressions
[1]->print();
441 case ast_function_call
: {
442 subexpressions
[0]->print();
445 foreach_list_const (n
, &this->expressions
) {
446 if (n
!= this->expressions
.get_head())
449 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
458 printf("%s ", primary_expression
.identifier
);
461 case ast_int_constant
:
462 printf("%d ", primary_expression
.int_constant
);
465 case ast_uint_constant
:
466 printf("%u ", primary_expression
.uint_constant
);
469 case ast_float_constant
:
470 printf("%f ", primary_expression
.float_constant
);
473 case ast_bool_constant
:
475 primary_expression
.bool_constant
481 foreach_list_const(n
, & this->expressions
) {
482 if (n
!= this->expressions
.get_head())
485 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
498 ast_expression::ast_expression(int oper
,
503 this->oper
= ast_operators(oper
);
504 this->subexpressions
[0] = ex0
;
505 this->subexpressions
[1] = ex1
;
506 this->subexpressions
[2] = ex2
;
511 ast_expression_statement::print(void) const
520 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
528 ast_function::print(void) const
530 return_type
->print();
531 printf(" %s (", identifier
);
533 foreach_list_const(n
, & this->parameters
) {
534 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
542 ast_function::ast_function(void)
543 : is_definition(false), signature(NULL
)
550 ast_fully_specified_type::print(void) const
552 _mesa_ast_type_qualifier_print(& qualifier
);
558 ast_parameter_declarator::print(void) const
562 printf("%s ", identifier
);
563 ast_opt_array_size_print(is_array
, array_size
);
568 ast_function_definition::print(void) const
576 ast_declaration::print(void) const
578 printf("%s ", identifier
);
579 ast_opt_array_size_print(is_array
, array_size
);
583 initializer
->print();
588 ast_declaration::ast_declaration(char *identifier
, int is_array
,
589 ast_expression
*array_size
,
590 ast_expression
*initializer
)
592 this->identifier
= identifier
;
593 this->is_array
= is_array
;
594 this->array_size
= array_size
;
595 this->initializer
= initializer
;
600 ast_declarator_list::print(void) const
602 assert(type
|| invariant
);
607 printf("invariant ");
609 foreach_list_const (ptr
, & this->declarations
) {
610 if (ptr
!= this->declarations
.get_head())
613 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
621 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
624 this->invariant
= false;
628 ast_jump_statement::print(void) const
632 printf("continue; ");
639 if (opt_return_value
)
640 opt_return_value
->print();
651 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
653 this->mode
= ast_jump_modes(mode
);
655 if (mode
== ast_return
)
656 opt_return_value
= return_value
;
661 ast_selection_statement::print(void) const
667 then_statement
->print();
669 if (else_statement
) {
671 else_statement
->print();
677 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
678 ast_node
*then_statement
,
679 ast_node
*else_statement
)
681 this->condition
= condition
;
682 this->then_statement
= then_statement
;
683 this->else_statement
= else_statement
;
688 ast_iteration_statement::print(void) const
694 init_statement
->print();
702 rest_expression
->print();
728 ast_iteration_statement::ast_iteration_statement(int mode
,
731 ast_expression
*rest_expression
,
734 this->mode
= ast_iteration_modes(mode
);
735 this->init_statement
= init
;
736 this->condition
= condition
;
737 this->rest_expression
= rest_expression
;
743 ast_struct_specifier::print(void) const
745 printf("struct %s { ", name
);
746 foreach_list_const(n
, &this->declarations
) {
747 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
754 ast_struct_specifier::ast_struct_specifier(char *identifier
,
755 ast_node
*declarator_list
)
757 if (identifier
== NULL
) {
758 static unsigned anon_count
= 1;
759 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
763 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
767 do_common_optimization(exec_list
*ir
, bool linked
, unsigned max_unroll_iterations
)
769 GLboolean progress
= GL_FALSE
;
771 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
774 progress
= do_function_inlining(ir
) || progress
;
775 progress
= do_dead_functions(ir
) || progress
;
777 progress
= do_structure_splitting(ir
) || progress
;
778 progress
= do_if_simplification(ir
) || progress
;
779 progress
= do_discard_simplification(ir
) || progress
;
780 progress
= do_copy_propagation(ir
) || progress
;
781 progress
= do_copy_propagation_elements(ir
) || progress
;
783 progress
= do_dead_code(ir
) || progress
;
785 progress
= do_dead_code_unlinked(ir
) || progress
;
786 progress
= do_dead_code_local(ir
) || progress
;
787 progress
= do_tree_grafting(ir
) || progress
;
788 progress
= do_constant_propagation(ir
) || progress
;
790 progress
= do_constant_variable(ir
) || progress
;
792 progress
= do_constant_variable_unlinked(ir
) || progress
;
793 progress
= do_constant_folding(ir
) || progress
;
794 progress
= do_algebraic(ir
) || progress
;
795 progress
= do_lower_jumps(ir
) || progress
;
796 progress
= do_vec_index_to_swizzle(ir
) || progress
;
797 progress
= do_swizzle_swizzle(ir
) || progress
;
798 progress
= do_noop_swizzle(ir
) || progress
;
800 progress
= optimize_redundant_jumps(ir
) || progress
;
802 loop_state
*ls
= analyze_loop_variables(ir
);
803 if (ls
->loop_found
) {
804 progress
= set_loop_controls(ir
, ls
) || progress
;
805 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
815 * To be called at GL teardown time, this frees compiler datastructures.
817 * After calling this, any previously compiled shaders and shader
818 * programs would be invalid. So this should happen at approximately
822 _mesa_destroy_shader_compiler(void)
824 _mesa_destroy_shader_compiler_caches();
826 _mesa_glsl_release_types();
830 * Releases compiler caches to trade off performance for memory.
832 * Intended to be used with glReleaseShaderCompiler().
835 _mesa_destroy_shader_compiler_caches(void)
837 _mesa_glsl_release_functions();