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 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 /* OpenGL ES 2.0 has different defaults from desktop GL. */
63 if (ctx
->API
== API_OPENGLES2
) {
64 this->language_version
= 100;
65 this->es_shader
= true;
66 this->ARB_texture_rectangle_enable
= false;
69 this->extensions
= &ctx
->Extensions
;
71 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
72 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
73 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
74 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
75 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
76 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
77 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
78 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
79 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
80 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
81 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
83 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
85 /* Note: Once the OpenGL 3.0 'forward compatible' context or the OpenGL 3.2
86 * Core context is supported, this logic will need change. Older versions of
87 * GLSL are no longer supported outside the compatibility contexts of 3.x.
89 this->Const
.GLSL_100ES
= (ctx
->API
== API_OPENGLES2
)
90 || ctx
->Extensions
.ARB_ES2_compatibility
;
91 this->Const
.GLSL_110
= (ctx
->API
== API_OPENGL
);
92 this->Const
.GLSL_120
= (ctx
->API
== API_OPENGL
)
93 && (ctx
->Const
.GLSLVersion
>= 120);
94 this->Const
.GLSL_130
= (ctx
->API
== API_OPENGL
)
95 && (ctx
->Const
.GLSLVersion
>= 130);
97 const unsigned lowest_version
=
98 (ctx
->API
== API_OPENGLES2
) || ctx
->Extensions
.ARB_ES2_compatibility
100 const unsigned highest_version
=
101 (ctx
->API
== API_OPENGL
) ? ctx
->Const
.GLSLVersion
: 100;
102 char *supported
= ralloc_strdup(this, "");
104 for (unsigned ver
= lowest_version
; ver
<= highest_version
; ver
+= 10) {
105 const char *const prefix
= (ver
== lowest_version
)
107 : ((ver
== highest_version
) ? ", and " : ", ");
109 ralloc_asprintf_append(& supported
, "%s%d.%02d%s",
111 ver
/ 100, ver
% 100,
112 (ver
== 100) ? " ES" : "");
115 this->supported_version_string
= supported
;
119 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
122 case vertex_shader
: return "vertex";
123 case fragment_shader
: return "fragment";
124 case geometry_shader
: return "geometry";
127 assert(!"Should not get here.");
133 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
134 const char *fmt
, ...)
140 assert(state
->info_log
!= NULL
);
141 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): error: ",
146 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
148 ralloc_strcat(&state
->info_log
, "\n");
153 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
154 const char *fmt
, ...)
158 assert(state
->info_log
!= NULL
);
159 ralloc_asprintf_append(&state
->info_log
, "%u:%u(%u): warning: ",
164 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
166 ralloc_strcat(&state
->info_log
, "\n");
171 * Enum representing the possible behaviors that can be specified in
172 * an #extension directive.
182 * Element type for _mesa_glsl_supported_extensions
184 struct _mesa_glsl_extension
{
186 * Name of the extension when referred to in a GLSL extension
191 /** True if this extension is available to vertex shaders */
194 /** True if this extension is available to geometry shaders */
197 /** True if this extension is available to fragment shaders */
200 /** True if this extension is available to desktop GL shaders */
203 /** True if this extension is available to GLES shaders */
207 * Flag in the gl_extensions struct indicating whether this
208 * extension is supported by the driver, or
209 * &gl_extensions::dummy_true if supported by all drivers.
211 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
212 * member" type, the type-safe alternative to the "offsetof" macro.
215 * - foo bar::* p declares p to be an "offset" to a field of type
216 * foo that exists within struct bar
217 * - &bar::baz computes the "offset" of field baz within struct bar
218 * - x.*p accesses the field of x that exists at "offset" p
219 * - x->*p is equivalent to (*x).*p
221 const GLboolean
gl_extensions::* supported_flag
;
224 * Flag in the _mesa_glsl_parse_state struct that should be set
225 * when this extension is enabled.
227 * See note in _mesa_glsl_extension::supported_flag about "pointer
230 bool _mesa_glsl_parse_state::* enable_flag
;
233 * Flag in the _mesa_glsl_parse_state struct that should be set
234 * when the shader requests "warn" behavior for this extension.
236 * See note in _mesa_glsl_extension::supported_flag about "pointer
239 bool _mesa_glsl_parse_state::* warn_flag
;
242 bool compatible_with_state(const _mesa_glsl_parse_state
*state
) const;
243 void set_flags(_mesa_glsl_parse_state
*state
, ext_behavior behavior
) const;
246 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
247 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
248 &_mesa_glsl_parse_state::NAME##_enable, \
249 &_mesa_glsl_parse_state::NAME##_warn }
252 * Table of extensions that can be enabled/disabled within a shader,
253 * and the conditions under which they are supported.
255 static const _mesa_glsl_extension _mesa_glsl_supported_extensions
[] = {
256 /* target availability API availability */
257 /* name VS GS FS GL ES supported flag */
258 EXT(ARB_conservative_depth
, true, false, true, true, false, AMD_conservative_depth
),
259 EXT(ARB_draw_buffers
, false, false, true, true, false, dummy_true
),
260 EXT(ARB_draw_instanced
, true, false, false, true, false, ARB_draw_instanced
),
261 EXT(ARB_explicit_attrib_location
, true, false, true, true, false, ARB_explicit_attrib_location
),
262 EXT(ARB_fragment_coord_conventions
, true, false, true, true, false, ARB_fragment_coord_conventions
),
263 EXT(ARB_texture_rectangle
, true, false, true, true, false, dummy_true
),
264 EXT(EXT_texture_array
, true, false, true, true, false, EXT_texture_array
),
265 EXT(ARB_shader_texture_lod
, true, false, true, true, false, ARB_shader_texture_lod
),
266 EXT(ARB_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
267 EXT(AMD_conservative_depth
, true, false, true, true, false, AMD_conservative_depth
),
268 EXT(AMD_shader_stencil_export
, false, false, true, true, false, ARB_shader_stencil_export
),
269 EXT(OES_texture_3D
, true, false, true, false, true, EXT_texture3D
),
276 * Determine whether a given extension is compatible with the target,
277 * API, and extension information in the current parser state.
279 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state
*
282 /* Check that this extension matches the type of shader we are
285 switch (state
->target
) {
287 if (!this->avail_in_VS
) {
291 case geometry_shader
:
292 if (!this->avail_in_GS
) {
296 case fragment_shader
:
297 if (!this->avail_in_FS
) {
302 assert (!"Unrecognized shader target");
306 /* Check that this extension matches whether we are compiling
307 * for desktop GL or GLES.
309 if (state
->es_shader
) {
310 if (!this->avail_in_ES
) return false;
312 if (!this->avail_in_GL
) return false;
315 /* Check that this extension is supported by the OpenGL
318 * Note: the ->* operator indexes into state->extensions by the
319 * offset this->supported_flag. See
320 * _mesa_glsl_extension::supported_flag for more info.
322 return state
->extensions
->*(this->supported_flag
);
326 * Set the appropriate flags in the parser state to establish the
327 * given behavior for this extension.
329 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state
*state
,
330 ext_behavior behavior
) const
332 /* Note: the ->* operator indexes into state by the
333 * offsets this->enable_flag and this->warn_flag. See
334 * _mesa_glsl_extension::supported_flag for more info.
336 state
->*(this->enable_flag
) = (behavior
!= extension_disable
);
337 state
->*(this->warn_flag
) = (behavior
== extension_warn
);
341 * Find an extension by name in _mesa_glsl_supported_extensions. If
342 * the name is not found, return NULL.
344 static const _mesa_glsl_extension
*find_extension(const char *name
)
346 for (unsigned i
= 0; i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
347 if (strcmp(name
, _mesa_glsl_supported_extensions
[i
].name
) == 0) {
348 return &_mesa_glsl_supported_extensions
[i
];
356 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
357 const char *behavior_string
, YYLTYPE
*behavior_locp
,
358 _mesa_glsl_parse_state
*state
)
360 ext_behavior behavior
;
361 if (strcmp(behavior_string
, "warn") == 0) {
362 behavior
= extension_warn
;
363 } else if (strcmp(behavior_string
, "require") == 0) {
364 behavior
= extension_require
;
365 } else if (strcmp(behavior_string
, "enable") == 0) {
366 behavior
= extension_enable
;
367 } else if (strcmp(behavior_string
, "disable") == 0) {
368 behavior
= extension_disable
;
370 _mesa_glsl_error(behavior_locp
, state
,
371 "Unknown extension behavior `%s'",
376 if (strcmp(name
, "all") == 0) {
377 if ((behavior
== extension_enable
) || (behavior
== extension_require
)) {
378 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
379 (behavior
== extension_enable
)
380 ? "enable" : "require");
384 i
< Elements(_mesa_glsl_supported_extensions
); ++i
) {
385 const _mesa_glsl_extension
*extension
386 = &_mesa_glsl_supported_extensions
[i
];
387 if (extension
->compatible_with_state(state
)) {
388 _mesa_glsl_supported_extensions
[i
].set_flags(state
, behavior
);
393 const _mesa_glsl_extension
*extension
= find_extension(name
);
394 if (extension
&& extension
->compatible_with_state(state
)) {
395 extension
->set_flags(state
, behavior
);
397 static const char *const fmt
= "extension `%s' unsupported in %s shader";
399 if (behavior
== extension_require
) {
400 _mesa_glsl_error(name_locp
, state
, fmt
,
401 name
, _mesa_glsl_shader_target_name(state
->target
));
404 _mesa_glsl_warning(name_locp
, state
, fmt
,
405 name
, _mesa_glsl_shader_target_name(state
->target
));
414 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
416 if (q
->flags
.q
.constant
)
419 if (q
->flags
.q
.invariant
)
420 printf("invariant ");
422 if (q
->flags
.q
.attribute
)
423 printf("attribute ");
425 if (q
->flags
.q
.varying
)
428 if (q
->flags
.q
.in
&& q
->flags
.q
.out
)
438 if (q
->flags
.q
.centroid
)
440 if (q
->flags
.q
.uniform
)
442 if (q
->flags
.q
.smooth
)
446 if (q
->flags
.q
.noperspective
)
447 printf("noperspective ");
452 ast_node::print(void) const
454 printf("unhandled node ");
458 ast_node::ast_node(void)
460 this->location
.source
= 0;
461 this->location
.line
= 0;
462 this->location
.column
= 0;
467 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
481 ast_compound_statement::print(void) const
485 foreach_list_const(n
, &this->statements
) {
486 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
494 ast_compound_statement::ast_compound_statement(int new_scope
,
495 ast_node
*statements
)
497 this->new_scope
= new_scope
;
499 if (statements
!= NULL
) {
500 this->statements
.push_degenerate_list_at_head(&statements
->link
);
506 ast_expression::print(void) const
520 subexpressions
[0]->print();
521 printf("%s ", operator_string(oper
));
522 subexpressions
[1]->print();
525 case ast_field_selection
:
526 subexpressions
[0]->print();
527 printf(". %s ", primary_expression
.identifier
);
536 printf("%s ", operator_string(oper
));
537 subexpressions
[0]->print();
542 subexpressions
[0]->print();
543 printf("%s ", operator_string(oper
));
546 case ast_conditional
:
547 subexpressions
[0]->print();
549 subexpressions
[1]->print();
551 subexpressions
[2]->print();
554 case ast_array_index
:
555 subexpressions
[0]->print();
557 subexpressions
[1]->print();
561 case ast_function_call
: {
562 subexpressions
[0]->print();
565 foreach_list_const (n
, &this->expressions
) {
566 if (n
!= this->expressions
.get_head())
569 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
578 printf("%s ", primary_expression
.identifier
);
581 case ast_int_constant
:
582 printf("%d ", primary_expression
.int_constant
);
585 case ast_uint_constant
:
586 printf("%u ", primary_expression
.uint_constant
);
589 case ast_float_constant
:
590 printf("%f ", primary_expression
.float_constant
);
593 case ast_bool_constant
:
595 primary_expression
.bool_constant
601 foreach_list_const(n
, & this->expressions
) {
602 if (n
!= this->expressions
.get_head())
605 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
618 ast_expression::ast_expression(int oper
,
623 this->oper
= ast_operators(oper
);
624 this->subexpressions
[0] = ex0
;
625 this->subexpressions
[1] = ex1
;
626 this->subexpressions
[2] = ex2
;
631 ast_expression_statement::print(void) const
640 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
648 ast_function::print(void) const
650 return_type
->print();
651 printf(" %s (", identifier
);
653 foreach_list_const(n
, & this->parameters
) {
654 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
662 ast_function::ast_function(void)
663 : is_definition(false), signature(NULL
)
670 ast_fully_specified_type::print(void) const
672 _mesa_ast_type_qualifier_print(& qualifier
);
678 ast_parameter_declarator::print(void) const
682 printf("%s ", identifier
);
683 ast_opt_array_size_print(is_array
, array_size
);
688 ast_function_definition::print(void) const
696 ast_declaration::print(void) const
698 printf("%s ", identifier
);
699 ast_opt_array_size_print(is_array
, array_size
);
703 initializer
->print();
708 ast_declaration::ast_declaration(char *identifier
, int is_array
,
709 ast_expression
*array_size
,
710 ast_expression
*initializer
)
712 this->identifier
= identifier
;
713 this->is_array
= is_array
;
714 this->array_size
= array_size
;
715 this->initializer
= initializer
;
720 ast_declarator_list::print(void) const
722 assert(type
|| invariant
);
727 printf("invariant ");
729 foreach_list_const (ptr
, & this->declarations
) {
730 if (ptr
!= this->declarations
.get_head())
733 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
741 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
744 this->invariant
= false;
748 ast_jump_statement::print(void) const
752 printf("continue; ");
759 if (opt_return_value
)
760 opt_return_value
->print();
771 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
773 this->mode
= ast_jump_modes(mode
);
775 if (mode
== ast_return
)
776 opt_return_value
= return_value
;
781 ast_selection_statement::print(void) const
787 then_statement
->print();
789 if (else_statement
) {
791 else_statement
->print();
797 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
798 ast_node
*then_statement
,
799 ast_node
*else_statement
)
801 this->condition
= condition
;
802 this->then_statement
= then_statement
;
803 this->else_statement
= else_statement
;
808 ast_iteration_statement::print(void) const
814 init_statement
->print();
822 rest_expression
->print();
848 ast_iteration_statement::ast_iteration_statement(int mode
,
851 ast_expression
*rest_expression
,
854 this->mode
= ast_iteration_modes(mode
);
855 this->init_statement
= init
;
856 this->condition
= condition
;
857 this->rest_expression
= rest_expression
;
863 ast_struct_specifier::print(void) const
865 printf("struct %s { ", name
);
866 foreach_list_const(n
, &this->declarations
) {
867 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
874 ast_struct_specifier::ast_struct_specifier(char *identifier
,
875 ast_node
*declarator_list
)
877 if (identifier
== NULL
) {
878 static unsigned anon_count
= 1;
879 identifier
= ralloc_asprintf(this, "#anon_struct_%04x", anon_count
);
883 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
887 do_common_optimization(exec_list
*ir
, bool linked
, unsigned max_unroll_iterations
)
889 GLboolean progress
= GL_FALSE
;
891 progress
= lower_instructions(ir
, SUB_TO_ADD_NEG
) || progress
;
894 progress
= do_function_inlining(ir
) || progress
;
895 progress
= do_dead_functions(ir
) || progress
;
896 progress
= do_structure_splitting(ir
) || progress
;
898 progress
= do_if_simplification(ir
) || progress
;
899 progress
= do_discard_simplification(ir
) || progress
;
900 progress
= do_copy_propagation(ir
) || progress
;
901 progress
= do_copy_propagation_elements(ir
) || progress
;
903 progress
= do_dead_code(ir
) || progress
;
905 progress
= do_dead_code_unlinked(ir
) || progress
;
906 progress
= do_dead_code_local(ir
) || progress
;
907 progress
= do_tree_grafting(ir
) || progress
;
908 progress
= do_constant_propagation(ir
) || progress
;
910 progress
= do_constant_variable(ir
) || progress
;
912 progress
= do_constant_variable_unlinked(ir
) || progress
;
913 progress
= do_constant_folding(ir
) || progress
;
914 progress
= do_algebraic(ir
) || progress
;
915 progress
= do_lower_jumps(ir
) || progress
;
916 progress
= do_vec_index_to_swizzle(ir
) || progress
;
917 progress
= do_swizzle_swizzle(ir
) || progress
;
918 progress
= do_noop_swizzle(ir
) || progress
;
920 progress
= optimize_redundant_jumps(ir
) || progress
;
922 loop_state
*ls
= analyze_loop_variables(ir
);
923 if (ls
->loop_found
) {
924 progress
= set_loop_controls(ir
, ls
) || progress
;
925 progress
= unroll_loops(ir
, ls
, max_unroll_iterations
) || progress
;
935 * To be called at GL teardown time, this frees compiler datastructures.
937 * After calling this, any previously compiled shaders and shader
938 * programs would be invalid. So this should happen at approximately
942 _mesa_destroy_shader_compiler(void)
944 _mesa_destroy_shader_compiler_caches();
946 _mesa_glsl_release_types();
950 * Releases compiler caches to trade off performance for memory.
952 * Intended to be used with glReleaseShaderCompiler().
955 _mesa_destroy_shader_compiler_caches(void)
957 _mesa_glsl_release_functions();