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
;
54 this->ARB_texture_rectangle_enable
= true;
57 this->extensions
= &ctx
->Extensions
;
59 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
60 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
61 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
62 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
63 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
64 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
65 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
66 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
67 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
68 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
69 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
71 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
73 /* If there is no GL context (standalone compiler), fill in constants
74 * with the minimum required values.
76 static struct gl_extensions null_extensions
;
78 memset(&null_extensions
, 0, sizeof(null_extensions
));
79 null_extensions
.ARB_draw_buffers
= GL_TRUE
;
80 null_extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
81 null_extensions
.EXT_texture_array
= GL_TRUE
;
82 null_extensions
.NV_texture_rectangle
= GL_TRUE
;
84 this->extensions
= &null_extensions
;
87 this->Const
.MaxLights
= 8;
88 this->Const
.MaxClipPlanes
= 8;
89 this->Const
.MaxTextureUnits
= 2;
91 /* More than the 1.10 minimum to appease parser tests taken from
92 * apps that (hopefully) already checked the number of coords.
94 this->Const
.MaxTextureCoords
= 4;
96 this->Const
.MaxVertexAttribs
= 16;
97 this->Const
.MaxVertexUniformComponents
= 512;
98 this->Const
.MaxVaryingFloats
= 32;
99 this->Const
.MaxVertexTextureImageUnits
= 0;
100 this->Const
.MaxCombinedTextureImageUnits
= 2;
101 this->Const
.MaxTextureImageUnits
= 2;
102 this->Const
.MaxFragmentUniformComponents
= 64;
104 this->Const
.MaxDrawBuffers
= 2;
109 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
112 case vertex_shader
: return "vertex";
113 case fragment_shader
: return "fragment";
114 case geometry_shader
: return "geometry";
115 case ir_shader
: break;
118 assert(!"Should not get here.");
124 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
125 const char *fmt
, ...)
131 assert(state
->info_log
!= NULL
);
132 state
->info_log
= talloc_asprintf_append(state
->info_log
,
133 "%u:%u(%u): error: ",
138 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
140 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
145 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
146 const char *fmt
, ...)
150 assert(state
->info_log
!= NULL
);
151 state
->info_log
= talloc_asprintf_append(state
->info_log
,
152 "%u:%u(%u): warning: ",
157 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
159 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
164 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
165 const char *behavior
, YYLTYPE
*behavior_locp
,
166 _mesa_glsl_parse_state
*state
)
175 if (strcmp(behavior
, "warn") == 0) {
176 ext_mode
= extension_warn
;
177 } else if (strcmp(behavior
, "require") == 0) {
178 ext_mode
= extension_require
;
179 } else if (strcmp(behavior
, "enable") == 0) {
180 ext_mode
= extension_enable
;
181 } else if (strcmp(behavior
, "disable") == 0) {
182 ext_mode
= extension_disable
;
184 _mesa_glsl_error(behavior_locp
, state
,
185 "Unknown extension behavior `%s'",
190 bool unsupported
= false;
192 if (strcmp(name
, "all") == 0) {
193 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
194 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
195 (ext_mode
== extension_enable
)
196 ? "enable" : "require");
199 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
200 /* This extension is only supported in fragment shaders.
202 if (state
->target
!= fragment_shader
) {
205 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
206 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
208 } else if (strcmp(name
, "GL_ARB_fragment_coord_conventions") == 0) {
209 state
->ARB_fragment_coord_conventions_enable
=
210 (ext_mode
!= extension_disable
);
211 state
->ARB_fragment_coord_conventions_warn
=
212 (ext_mode
== extension_warn
);
214 unsupported
= !state
->extensions
->ARB_fragment_coord_conventions
;
215 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
216 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
217 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
218 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
219 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
220 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
222 unsupported
= !state
->extensions
->EXT_texture_array
;
228 static const char *const fmt
= "extension `%s' unsupported in %s shader";
230 if (ext_mode
== extension_require
) {
231 _mesa_glsl_error(name_locp
, state
, fmt
,
232 name
, _mesa_glsl_shader_target_name(state
->target
));
235 _mesa_glsl_warning(name_locp
, state
, fmt
,
236 name
, _mesa_glsl_shader_target_name(state
->target
));
244 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
250 printf("invariant ");
253 printf("attribute ");
276 if (q
->noperspective
)
277 printf("noperspective ");
282 ast_node::print(void) const
284 printf("unhandled node ");
288 ast_node::ast_node(void)
290 this->location
.source
= 0;
291 this->location
.line
= 0;
292 this->location
.column
= 0;
297 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
311 ast_compound_statement::print(void) const
315 foreach_list_const(n
, &this->statements
) {
316 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
324 ast_compound_statement::ast_compound_statement(int new_scope
,
325 ast_node
*statements
)
327 this->new_scope
= new_scope
;
329 if (statements
!= NULL
) {
330 this->statements
.push_degenerate_list_at_head(&statements
->link
);
336 ast_expression::print(void) const
350 subexpressions
[0]->print();
351 printf("%s ", operator_string(oper
));
352 subexpressions
[1]->print();
355 case ast_field_selection
:
356 subexpressions
[0]->print();
357 printf(". %s ", primary_expression
.identifier
);
366 printf("%s ", operator_string(oper
));
367 subexpressions
[0]->print();
372 subexpressions
[0]->print();
373 printf("%s ", operator_string(oper
));
376 case ast_conditional
:
377 subexpressions
[0]->print();
379 subexpressions
[1]->print();
381 subexpressions
[1]->print();
384 case ast_array_index
:
385 subexpressions
[0]->print();
387 subexpressions
[1]->print();
391 case ast_function_call
: {
392 subexpressions
[0]->print();
395 foreach_list_const (n
, &this->expressions
) {
396 if (n
!= this->expressions
.get_head())
399 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
408 printf("%s ", primary_expression
.identifier
);
411 case ast_int_constant
:
412 printf("%d ", primary_expression
.int_constant
);
415 case ast_uint_constant
:
416 printf("%u ", primary_expression
.uint_constant
);
419 case ast_float_constant
:
420 printf("%f ", primary_expression
.float_constant
);
423 case ast_bool_constant
:
425 primary_expression
.bool_constant
431 foreach_list_const(n
, & this->expressions
) {
432 if (n
!= this->expressions
.get_head())
435 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
448 ast_expression::ast_expression(int oper
,
453 this->oper
= ast_operators(oper
);
454 this->subexpressions
[0] = ex0
;
455 this->subexpressions
[1] = ex1
;
456 this->subexpressions
[2] = ex2
;
461 ast_expression_statement::print(void) const
470 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
478 ast_function::print(void) const
480 return_type
->print();
481 printf(" %s (", identifier
);
483 foreach_list_const(n
, & this->parameters
) {
484 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
492 ast_function::ast_function(void)
493 : is_definition(false), signature(NULL
)
500 ast_fully_specified_type::print(void) const
502 _mesa_ast_type_qualifier_print(& qualifier
);
508 ast_parameter_declarator::print(void) const
512 printf("%s ", identifier
);
513 ast_opt_array_size_print(is_array
, array_size
);
518 ast_function_definition::print(void) const
526 ast_declaration::print(void) const
528 printf("%s ", identifier
);
529 ast_opt_array_size_print(is_array
, array_size
);
533 initializer
->print();
538 ast_declaration::ast_declaration(char *identifier
, int is_array
,
539 ast_expression
*array_size
,
540 ast_expression
*initializer
)
542 this->identifier
= identifier
;
543 this->is_array
= is_array
;
544 this->array_size
= array_size
;
545 this->initializer
= initializer
;
550 ast_declarator_list::print(void) const
552 assert(type
|| invariant
);
557 printf("invariant ");
559 foreach_list_const (ptr
, & this->declarations
) {
560 if (ptr
!= this->declarations
.get_head())
563 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
571 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
574 this->invariant
= false;
578 ast_jump_statement::print(void) const
582 printf("continue; ");
589 if (opt_return_value
)
590 opt_return_value
->print();
601 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
603 this->mode
= ast_jump_modes(mode
);
605 if (mode
== ast_return
)
606 opt_return_value
= return_value
;
611 ast_selection_statement::print(void) const
617 then_statement
->print();
619 if (else_statement
) {
621 else_statement
->print();
627 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
628 ast_node
*then_statement
,
629 ast_node
*else_statement
)
631 this->condition
= condition
;
632 this->then_statement
= then_statement
;
633 this->else_statement
= else_statement
;
638 ast_iteration_statement::print(void) const
644 init_statement
->print();
652 rest_expression
->print();
678 ast_iteration_statement::ast_iteration_statement(int mode
,
681 ast_expression
*rest_expression
,
684 this->mode
= ast_iteration_modes(mode
);
685 this->init_statement
= init
;
686 this->condition
= condition
;
687 this->rest_expression
= rest_expression
;
693 ast_struct_specifier::print(void) const
695 printf("struct %s { ", name
);
696 foreach_list_const(n
, &this->declarations
) {
697 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
704 ast_struct_specifier::ast_struct_specifier(char *identifier
,
705 ast_node
*declarator_list
)
708 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
712 do_common_optimization(exec_list
*ir
, bool linked
)
714 GLboolean progress
= GL_FALSE
;
716 progress
= do_sub_to_add_neg(ir
) || progress
;
719 progress
= do_function_inlining(ir
) || progress
;
720 progress
= do_dead_functions(ir
) || progress
;
722 progress
= do_structure_splitting(ir
) || progress
;
723 progress
= do_if_simplification(ir
) || progress
;
724 progress
= do_copy_propagation(ir
) || progress
;
726 progress
= do_dead_code(ir
) || progress
;
728 progress
= do_dead_code_unlinked(ir
) || progress
;
729 progress
= do_dead_code_local(ir
) || progress
;
730 progress
= do_tree_grafting(ir
) || progress
;
731 progress
= do_constant_propagation(ir
) || progress
;
733 progress
= do_constant_variable(ir
) || progress
;
735 progress
= do_constant_variable_unlinked(ir
) || progress
;
736 progress
= do_constant_folding(ir
) || progress
;
737 progress
= do_algebraic(ir
) || progress
;
738 progress
= do_if_return(ir
) || progress
;
739 progress
= do_vec_index_to_swizzle(ir
) || progress
;
740 progress
= do_swizzle_swizzle(ir
) || progress
;
741 progress
= do_noop_swizzle(ir
) || progress
;
743 loop_state
*ls
= analyze_loop_variables(ir
);
744 progress
= set_loop_controls(ir
, ls
) || progress
;
753 * To be called at GL teardown time, this frees compiler datastructures.
755 * After calling this, any previously compiled shaders and shader
756 * programs would be invalid. So this should happen at approximately
760 _mesa_destroy_shader_compiler(void)
762 _mesa_destroy_shader_compiler_caches();
764 _mesa_glsl_release_types();
768 * Releases compiler caches to trade off performance for memory.
770 * Intended to be used with glReleaseShaderCompiler().
773 _mesa_destroy_shader_compiler_caches(void)
775 _mesa_glsl_release_functions();