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.
30 #include "main/core.h" /* for struct __GLcontextRec */
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 __GLcontextRec
*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
= talloc_strdup(mem_ctx
, "");
53 this->loop_or_switch_nesting
= NULL
;
55 /* Set default language version and extensions */
56 this->language_version
= 110;
57 this->ARB_texture_rectangle_enable
= true;
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 /* If there is no GL context (standalone compiler), fill in constants
77 * with the minimum required values.
79 static struct gl_extensions null_extensions
;
81 memset(&null_extensions
, 0, sizeof(null_extensions
));
82 null_extensions
.ARB_draw_buffers
= GL_TRUE
;
83 null_extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
84 null_extensions
.EXT_texture_array
= GL_TRUE
;
85 null_extensions
.NV_texture_rectangle
= GL_TRUE
;
87 this->extensions
= &null_extensions
;
90 this->Const
.MaxLights
= 8;
91 this->Const
.MaxClipPlanes
= 8;
92 this->Const
.MaxTextureUnits
= 2;
94 /* More than the 1.10 minimum to appease parser tests taken from
95 * apps that (hopefully) already checked the number of coords.
97 this->Const
.MaxTextureCoords
= 4;
99 this->Const
.MaxVertexAttribs
= 16;
100 this->Const
.MaxVertexUniformComponents
= 512;
101 this->Const
.MaxVaryingFloats
= 32;
102 this->Const
.MaxVertexTextureImageUnits
= 0;
103 this->Const
.MaxCombinedTextureImageUnits
= 2;
104 this->Const
.MaxTextureImageUnits
= 2;
105 this->Const
.MaxFragmentUniformComponents
= 64;
107 this->Const
.MaxDrawBuffers
= 2;
112 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
115 case vertex_shader
: return "vertex";
116 case fragment_shader
: return "fragment";
117 case geometry_shader
: return "geometry";
118 case ir_shader
: break;
121 assert(!"Should not get here.");
127 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
128 const char *fmt
, ...)
134 assert(state
->info_log
!= NULL
);
135 state
->info_log
= talloc_asprintf_append(state
->info_log
,
136 "%u:%u(%u): error: ",
141 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
143 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
148 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
149 const char *fmt
, ...)
153 assert(state
->info_log
!= NULL
);
154 state
->info_log
= talloc_asprintf_append(state
->info_log
,
155 "%u:%u(%u): warning: ",
160 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
162 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
167 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
168 const char *behavior
, YYLTYPE
*behavior_locp
,
169 _mesa_glsl_parse_state
*state
)
178 if (strcmp(behavior
, "warn") == 0) {
179 ext_mode
= extension_warn
;
180 } else if (strcmp(behavior
, "require") == 0) {
181 ext_mode
= extension_require
;
182 } else if (strcmp(behavior
, "enable") == 0) {
183 ext_mode
= extension_enable
;
184 } else if (strcmp(behavior
, "disable") == 0) {
185 ext_mode
= extension_disable
;
187 _mesa_glsl_error(behavior_locp
, state
,
188 "Unknown extension behavior `%s'",
193 bool unsupported
= false;
195 if (strcmp(name
, "all") == 0) {
196 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
197 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
198 (ext_mode
== extension_enable
)
199 ? "enable" : "require");
202 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
203 /* This extension is only supported in fragment shaders.
205 if (state
->target
!= fragment_shader
) {
208 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
209 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
211 } else if (strcmp(name
, "GL_ARB_fragment_coord_conventions") == 0) {
212 state
->ARB_fragment_coord_conventions_enable
=
213 (ext_mode
!= extension_disable
);
214 state
->ARB_fragment_coord_conventions_warn
=
215 (ext_mode
== extension_warn
);
217 unsupported
= !state
->extensions
->ARB_fragment_coord_conventions
;
218 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
219 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
220 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
221 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
222 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
223 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
225 unsupported
= !state
->extensions
->EXT_texture_array
;
231 static const char *const fmt
= "extension `%s' unsupported in %s shader";
233 if (ext_mode
== extension_require
) {
234 _mesa_glsl_error(name_locp
, state
, fmt
,
235 name
, _mesa_glsl_shader_target_name(state
->target
));
238 _mesa_glsl_warning(name_locp
, state
, fmt
,
239 name
, _mesa_glsl_shader_target_name(state
->target
));
247 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
253 printf("invariant ");
256 printf("attribute ");
279 if (q
->noperspective
)
280 printf("noperspective ");
285 ast_node::print(void) const
287 printf("unhandled node ");
291 ast_node::ast_node(void)
293 this->location
.source
= 0;
294 this->location
.line
= 0;
295 this->location
.column
= 0;
300 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
314 ast_compound_statement::print(void) const
318 foreach_list_const(n
, &this->statements
) {
319 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
327 ast_compound_statement::ast_compound_statement(int new_scope
,
328 ast_node
*statements
)
330 this->new_scope
= new_scope
;
332 if (statements
!= NULL
) {
333 this->statements
.push_degenerate_list_at_head(&statements
->link
);
339 ast_expression::print(void) const
353 subexpressions
[0]->print();
354 printf("%s ", operator_string(oper
));
355 subexpressions
[1]->print();
358 case ast_field_selection
:
359 subexpressions
[0]->print();
360 printf(". %s ", primary_expression
.identifier
);
369 printf("%s ", operator_string(oper
));
370 subexpressions
[0]->print();
375 subexpressions
[0]->print();
376 printf("%s ", operator_string(oper
));
379 case ast_conditional
:
380 subexpressions
[0]->print();
382 subexpressions
[1]->print();
384 subexpressions
[1]->print();
387 case ast_array_index
:
388 subexpressions
[0]->print();
390 subexpressions
[1]->print();
394 case ast_function_call
: {
395 subexpressions
[0]->print();
398 foreach_list_const (n
, &this->expressions
) {
399 if (n
!= this->expressions
.get_head())
402 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
411 printf("%s ", primary_expression
.identifier
);
414 case ast_int_constant
:
415 printf("%d ", primary_expression
.int_constant
);
418 case ast_uint_constant
:
419 printf("%u ", primary_expression
.uint_constant
);
422 case ast_float_constant
:
423 printf("%f ", primary_expression
.float_constant
);
426 case ast_bool_constant
:
428 primary_expression
.bool_constant
434 foreach_list_const(n
, & this->expressions
) {
435 if (n
!= this->expressions
.get_head())
438 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
451 ast_expression::ast_expression(int oper
,
456 this->oper
= ast_operators(oper
);
457 this->subexpressions
[0] = ex0
;
458 this->subexpressions
[1] = ex1
;
459 this->subexpressions
[2] = ex2
;
464 ast_expression_statement::print(void) const
473 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
481 ast_function::print(void) const
483 return_type
->print();
484 printf(" %s (", identifier
);
486 foreach_list_const(n
, & this->parameters
) {
487 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
495 ast_function::ast_function(void)
496 : is_definition(false), signature(NULL
)
503 ast_fully_specified_type::print(void) const
505 _mesa_ast_type_qualifier_print(& qualifier
);
511 ast_parameter_declarator::print(void) const
515 printf("%s ", identifier
);
516 ast_opt_array_size_print(is_array
, array_size
);
521 ast_function_definition::print(void) const
529 ast_declaration::print(void) const
531 printf("%s ", identifier
);
532 ast_opt_array_size_print(is_array
, array_size
);
536 initializer
->print();
541 ast_declaration::ast_declaration(char *identifier
, int is_array
,
542 ast_expression
*array_size
,
543 ast_expression
*initializer
)
545 this->identifier
= identifier
;
546 this->is_array
= is_array
;
547 this->array_size
= array_size
;
548 this->initializer
= initializer
;
553 ast_declarator_list::print(void) const
555 assert(type
|| invariant
);
560 printf("invariant ");
562 foreach_list_const (ptr
, & this->declarations
) {
563 if (ptr
!= this->declarations
.get_head())
566 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
574 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
577 this->invariant
= false;
581 ast_jump_statement::print(void) const
585 printf("continue; ");
592 if (opt_return_value
)
593 opt_return_value
->print();
604 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
606 this->mode
= ast_jump_modes(mode
);
608 if (mode
== ast_return
)
609 opt_return_value
= return_value
;
614 ast_selection_statement::print(void) const
620 then_statement
->print();
622 if (else_statement
) {
624 else_statement
->print();
630 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
631 ast_node
*then_statement
,
632 ast_node
*else_statement
)
634 this->condition
= condition
;
635 this->then_statement
= then_statement
;
636 this->else_statement
= else_statement
;
641 ast_iteration_statement::print(void) const
647 init_statement
->print();
655 rest_expression
->print();
681 ast_iteration_statement::ast_iteration_statement(int mode
,
684 ast_expression
*rest_expression
,
687 this->mode
= ast_iteration_modes(mode
);
688 this->init_statement
= init
;
689 this->condition
= condition
;
690 this->rest_expression
= rest_expression
;
696 ast_struct_specifier::print(void) const
698 printf("struct %s { ", name
);
699 foreach_list_const(n
, &this->declarations
) {
700 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
707 ast_struct_specifier::ast_struct_specifier(char *identifier
,
708 ast_node
*declarator_list
)
711 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
715 do_common_optimization(exec_list
*ir
, bool linked
)
717 GLboolean progress
= GL_FALSE
;
719 progress
= do_sub_to_add_neg(ir
) || progress
;
722 progress
= do_function_inlining(ir
) || progress
;
723 progress
= do_dead_functions(ir
) || progress
;
725 progress
= do_structure_splitting(ir
) || progress
;
726 progress
= do_if_simplification(ir
) || progress
;
727 progress
= do_copy_propagation(ir
) || progress
;
729 progress
= do_dead_code(ir
) || progress
;
731 progress
= do_dead_code_unlinked(ir
) || progress
;
732 progress
= do_dead_code_local(ir
) || progress
;
733 progress
= do_tree_grafting(ir
) || progress
;
734 progress
= do_constant_propagation(ir
) || progress
;
736 progress
= do_constant_variable(ir
) || progress
;
738 progress
= do_constant_variable_unlinked(ir
) || progress
;
739 progress
= do_constant_folding(ir
) || progress
;
740 progress
= do_algebraic(ir
) || progress
;
741 progress
= do_if_return(ir
) || progress
;
742 progress
= do_vec_index_to_swizzle(ir
) || progress
;
743 progress
= do_swizzle_swizzle(ir
) || progress
;
744 progress
= do_noop_swizzle(ir
) || progress
;
746 loop_state
*ls
= analyze_loop_variables(ir
);
747 progress
= set_loop_controls(ir
, ls
) || progress
;
748 progress
= unroll_loops(ir
, ls
) || progress
;
757 * To be called at GL teardown time, this frees compiler datastructures.
759 * After calling this, any previously compiled shaders and shader
760 * programs would be invalid. So this should happen at approximately
764 _mesa_destroy_shader_compiler(void)
766 _mesa_destroy_shader_compiler_caches();
768 _mesa_glsl_release_types();
772 * Releases compiler caches to trade off performance for memory.
774 * Intended to be used with glReleaseShaderCompiler().
777 _mesa_destroy_shader_compiler_caches(void)
779 _mesa_glsl_release_functions();