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->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;
85
_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
88
case vertex_shader: return "vertex";
89
case fragment_shader: return "fragment";
90
case geometry_shader: return "geometry";
91
case ir_shader: break;
94
assert(!"Should not get here.");
100
_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
101
const char *fmt, ...)
107
assert(state->info_log != NULL);
108
state->info_log = talloc_asprintf_append(state->info_log,
109
"%u:%u(%u): error: ",
114
state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
116
state->info_log = talloc_strdup_append(state->info_log, "\n");
121
_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
122
const char *fmt, ...)
126
assert(state->info_log != NULL);
127
state->info_log = talloc_asprintf_append(state->info_log,
128
"%u:%u(%u): warning: ",
133
state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
135
state->info_log = talloc_strdup_append(state->info_log, "\n");
140
_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
141
const char *behavior, YYLTYPE *behavior_locp,
142
_mesa_glsl_parse_state *state)
151
if (strcmp(behavior, "warn") == 0) {
152
ext_mode = extension_warn;
153
} else if (strcmp(behavior, "require") == 0) {
154
ext_mode = extension_require;
155
} else if (strcmp(behavior, "enable") == 0) {
156
ext_mode = extension_enable;
157
} else if (strcmp(behavior, "disable") == 0) {
158
ext_mode = extension_disable;
160
_mesa_glsl_error(behavior_locp, state,
161
"Unknown extension behavior `%s'",
166
bool unsupported = false;
168
if (strcmp(name, "all") == 0) {
169
if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
170
_mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
171
(ext_mode == extension_enable)
172
? "enable" : "require");
175
} else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
176
/* This extension is only supported in fragment shaders.
178
if (state->target != fragment_shader) {
181
state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
182
state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
184
} else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
185
state->ARB_fragment_coord_conventions_enable =
186
(ext_mode != extension_disable);
187
state->ARB_fragment_coord_conventions_warn =
188
(ext_mode == extension_warn);
190
unsupported = !state->extensions->ARB_fragment_coord_conventions;
191
} else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
192
state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
193
state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
194
} else if (strcmp(name, "GL_EXT_texture_array") == 0) {
195
state->EXT_texture_array_enable = (ext_mode != extension_disable);
196
state->EXT_texture_array_warn = (ext_mode == extension_warn);
198
unsupported = !state->extensions->EXT_texture_array;
204
static const char *const fmt = "extension `%s' unsupported in %s shader";
206
if (ext_mode == extension_require) {
207
_mesa_glsl_error(name_locp, state, fmt,
208
name, _mesa_glsl_shader_target_name(state->target));
211
_mesa_glsl_warning(name_locp, state, fmt,
212
name, _mesa_glsl_shader_target_name(state->target));
220
_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
226
printf("invariant ");
229
printf("attribute ");
252
if (q->noperspective)
253
printf("noperspective ");
258
ast_node::print(void) const
260
printf("unhandled node ");
264
ast_node::ast_node(void)
266
this->location.source = 0;
267
this->location.line = 0;
268
this->location.column = 0;
273
ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
287
ast_compound_statement::print(void) const
291
foreach_list_const(n, &this->statements) {
292
ast_node *ast = exec_node_data(ast_node, n, link);
300
ast_compound_statement::ast_compound_statement(int new_scope,
301
ast_node *statements)
303
this->new_scope = new_scope;
305
if (statements != NULL) {
306
this->statements.push_degenerate_list_at_head(&statements->link);
312
ast_expression::print(void) const
326
subexpressions[0]->print();
327
printf("%s ", operator_string(oper));
328
subexpressions[1]->print();
331
case ast_field_selection:
332
subexpressions[0]->print();
333
printf(". %s ", primary_expression.identifier);
342
printf("%s ", operator_string(oper));
343
subexpressions[0]->print();
348
subexpressions[0]->print();
349
printf("%s ", operator_string(oper));
352
case ast_conditional:
353
subexpressions[0]->print();
355
subexpressions[1]->print();
357
subexpressions[1]->print();
360
case ast_array_index:
361
subexpressions[0]->print();
363
subexpressions[1]->print();
367
case ast_function_call: {
368
subexpressions[0]->print();
371
foreach_list_const (n, &this->expressions) {
372
if (n != this->expressions.get_head())
375
ast_node *ast = exec_node_data(ast_node, n, link);
384
printf("%s ", primary_expression.identifier);
387
case ast_int_constant:
388
printf("%d ", primary_expression.int_constant);
391
case ast_uint_constant:
392
printf("%u ", primary_expression.uint_constant);
395
case ast_float_constant:
396
printf("%f ", primary_expression.float_constant);
399
case ast_bool_constant:
401
primary_expression.bool_constant
407
foreach_list_const(n, & this->expressions) {
408
if (n != this->expressions.get_head())
411
ast_node *ast = exec_node_data(ast_node, n, link);
424
ast_expression::ast_expression(int oper,
429
this->oper = ast_operators(oper);
430
this->subexpressions[0] = ex0;
431
this->subexpressions[1] = ex1;
432
this->subexpressions[2] = ex2;
437
ast_expression_statement::print(void) const
446
ast_expression_statement::ast_expression_statement(ast_expression *ex) :
454
ast_function::print(void) const
456
return_type->print();
457
printf(" %s (", identifier);
459
foreach_list_const(n, & this->parameters) {
460
ast_node *ast = exec_node_data(ast_node, n, link);
468
ast_function::ast_function(void)
469
: is_definition(false), signature(NULL)
476
ast_fully_specified_type::print(void) const
478
_mesa_ast_type_qualifier_print(& qualifier);
484
ast_parameter_declarator::print(void) const
488
printf("%s ", identifier);
489
ast_opt_array_size_print(is_array, array_size);
494
ast_function_definition::print(void) const
502
ast_declaration::print(void) const
504
printf("%s ", identifier);
505
ast_opt_array_size_print(is_array, array_size);
509
initializer->print();
514
ast_declaration::ast_declaration(char *identifier, int is_array,
515
ast_expression *array_size,
516
ast_expression *initializer)
518
this->identifier = identifier;
519
this->is_array = is_array;
520
this->array_size = array_size;
521
this->initializer = initializer;
526
ast_declarator_list::print(void) const
528
assert(type || invariant);
533
printf("invariant ");
535
foreach_list_const (ptr, & this->declarations) {
536
if (ptr != this->declarations.get_head())
539
ast_node *ast = exec_node_data(ast_node, ptr, link);
547
ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
550
this->invariant = false;
554
ast_jump_statement::print(void) const
558
printf("continue; ");
565
if (opt_return_value)
566
opt_return_value->print();
577
ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
579
this->mode = ast_jump_modes(mode);
581
if (mode == ast_return)
582
opt_return_value = return_value;
587
ast_selection_statement::print(void) const
593
then_statement->print();
595
if (else_statement) {
597
else_statement->print();
603
ast_selection_statement::ast_selection_statement(ast_expression *condition,
604
ast_node *then_statement,
605
ast_node *else_statement)
607
this->condition = condition;
608
this->then_statement = then_statement;
609
this->else_statement = else_statement;
614
ast_iteration_statement::print(void) const
620
init_statement->print();
628
rest_expression->print();
654
ast_iteration_statement::ast_iteration_statement(int mode,
657
ast_expression *rest_expression,
660
this->mode = ast_iteration_modes(mode);
661
this->init_statement = init;
662
this->condition = condition;
663
this->rest_expression = rest_expression;
669
ast_struct_specifier::print(void) const
671
printf("struct %s { ", name);
672
foreach_list_const(n, &this->declarations) {
673
ast_node *ast = exec_node_data(ast_node, n, link);
680
ast_struct_specifier::ast_struct_specifier(char *identifier,
681
ast_node *declarator_list)
684
this->declarations.push_degenerate_list_at_head(&declarator_list->link);
688
do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations)
690
GLboolean progress = GL_FALSE;
692
progress = do_sub_to_add_neg(ir) || progress;
695
progress = do_function_inlining(ir) || progress;
696
progress = do_dead_functions(ir) || progress;
698
progress = do_structure_splitting(ir) || progress;
699
progress = do_if_simplification(ir) || progress;
700
progress = do_copy_propagation(ir) || progress;
702
progress = do_dead_code(ir) || progress;
704
progress = do_dead_code_unlinked(ir) || progress;
705
progress = do_dead_code_local(ir) || progress;
706
progress = do_tree_grafting(ir) || progress;
707
progress = do_constant_propagation(ir) || progress;
709
progress = do_constant_variable(ir) || progress;
711
progress = do_constant_variable_unlinked(ir) || progress;
712
progress = do_constant_folding(ir) || progress;
713
progress = do_algebraic(ir) || progress;
714
progress = do_if_return(ir) || progress;
715
progress = do_vec_index_to_swizzle(ir) || progress;
716
progress = do_swizzle_swizzle(ir) || progress;
717
progress = do_noop_swizzle(ir) || progress;
719
loop_state *ls = analyze_loop_variables(ir);
720
progress = set_loop_controls(ir, ls) || progress;
721
progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
730
* To be called at GL teardown time, this frees compiler datastructures.
732
* After calling this, any previously compiled shaders and shader
733
* programs would be invalid. So this should happen at approximately
737
_mesa_destroy_shader_compiler(void)
739
_mesa_destroy_shader_compiler_caches();
741
_mesa_glsl_release_types();
745
* Releases compiler caches to trade off performance for memory.
747
* Intended to be used with glReleaseShaderCompiler().
750
_mesa_destroy_shader_compiler_caches(void)
752
_mesa_glsl_release_functions();