glsl: add support for shader stencil export
[mesa.git] / src / glsl / glsl_parser_extras.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <assert.h>
27
28 extern "C" {
29 #include <talloc.h>
30 #include "main/core.h" /* for struct __GLcontextRec */
31 }
32
33 #include "ast.h"
34 #include "glsl_parser_extras.h"
35 #include "glsl_parser.h"
36 #include "ir_optimization.h"
37 #include "loop_analysis.h"
38
39 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
40 GLenum target, void *mem_ctx)
41 {
42 switch (target) {
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;
46 }
47
48 this->scanner = NULL;
49 this->translation_unit.make_empty();
50 this->symbols = new(mem_ctx) glsl_symbol_table;
51 this->info_log = talloc_strdup(mem_ctx, "");
52 this->error = false;
53 this->loop_or_switch_nesting = NULL;
54
55 /* Set default language version and extensions */
56 this->language_version = 110;
57 this->es_shader = false;
58 this->ARB_texture_rectangle_enable = true;
59
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;
65 }
66
67 this->extensions = &ctx->Extensions;
68
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;
80
81 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
82 }
83
84 const char *
85 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
86 {
87 switch (target) {
88 case vertex_shader: return "vertex";
89 case fragment_shader: return "fragment";
90 case geometry_shader: return "geometry";
91 case ir_shader: break;
92 }
93
94 assert(!"Should not get here.");
95 return "unknown";
96 }
97
98
99 void
100 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
101 const char *fmt, ...)
102 {
103 va_list ap;
104
105 state->error = true;
106
107 assert(state->info_log != NULL);
108 state->info_log = talloc_asprintf_append(state->info_log,
109 "%u:%u(%u): error: ",
110 locp->source,
111 locp->first_line,
112 locp->first_column);
113 va_start(ap, fmt);
114 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
115 va_end(ap);
116 state->info_log = talloc_strdup_append(state->info_log, "\n");
117 }
118
119
120 void
121 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
122 const char *fmt, ...)
123 {
124 va_list ap;
125
126 assert(state->info_log != NULL);
127 state->info_log = talloc_asprintf_append(state->info_log,
128 "%u:%u(%u): warning: ",
129 locp->source,
130 locp->first_line,
131 locp->first_column);
132 va_start(ap, fmt);
133 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
134 va_end(ap);
135 state->info_log = talloc_strdup_append(state->info_log, "\n");
136 }
137
138
139 bool
140 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
141 const char *behavior, YYLTYPE *behavior_locp,
142 _mesa_glsl_parse_state *state)
143 {
144 enum {
145 extension_disable,
146 extension_enable,
147 extension_require,
148 extension_warn
149 } ext_mode;
150
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;
159 } else {
160 _mesa_glsl_error(behavior_locp, state,
161 "Unknown extension behavior `%s'",
162 behavior);
163 return false;
164 }
165
166 bool unsupported = false;
167
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");
173 return false;
174 }
175 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
176 /* This extension is only supported in fragment shaders.
177 */
178 if (state->target != fragment_shader) {
179 unsupported = true;
180 } else {
181 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
182 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
183 }
184 } else if (strcmp(name, "GL_ARB_explicit_attrib_location") == 0) {
185 state->ARB_explicit_attrib_location_enable =
186 (ext_mode != extension_disable);
187 state->ARB_explicit_attrib_location_warn =
188 (ext_mode == extension_warn);
189
190 unsupported = !state->extensions->ARB_explicit_attrib_location;
191 } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
192 state->ARB_fragment_coord_conventions_enable =
193 (ext_mode != extension_disable);
194 state->ARB_fragment_coord_conventions_warn =
195 (ext_mode == extension_warn);
196
197 unsupported = !state->extensions->ARB_fragment_coord_conventions;
198 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
199 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
200 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
201 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
202 state->EXT_texture_array_enable = (ext_mode != extension_disable);
203 state->EXT_texture_array_warn = (ext_mode == extension_warn);
204
205 unsupported = !state->extensions->EXT_texture_array;
206 } else if (strcmp(name, "GL_ARB_shader_stencil_export") == 0) {
207 if (state->target != fragment_shader) {
208 unsupported = true;
209 } else {
210 state->ARB_shader_stencil_export_enable = (ext_mode != extension_disable);
211 state->ARB_shader_stencil_export_warn = (ext_mode == extension_warn);
212 unsupported = !state->extensions->ARB_shader_stencil_export;
213 }
214 } else {
215 unsupported = true;
216 }
217
218 if (unsupported) {
219 static const char *const fmt = "extension `%s' unsupported in %s shader";
220
221 if (ext_mode == extension_require) {
222 _mesa_glsl_error(name_locp, state, fmt,
223 name, _mesa_glsl_shader_target_name(state->target));
224 return false;
225 } else {
226 _mesa_glsl_warning(name_locp, state, fmt,
227 name, _mesa_glsl_shader_target_name(state->target));
228 }
229 }
230
231 return true;
232 }
233
234 void
235 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
236 {
237 if (q->flags.q.constant)
238 printf("const ");
239
240 if (q->flags.q.invariant)
241 printf("invariant ");
242
243 if (q->flags.q.attribute)
244 printf("attribute ");
245
246 if (q->flags.q.varying)
247 printf("varying ");
248
249 if (q->flags.q.in && q->flags.q.out)
250 printf("inout ");
251 else {
252 if (q->flags.q.in)
253 printf("in ");
254
255 if (q->flags.q.out)
256 printf("out ");
257 }
258
259 if (q->flags.q.centroid)
260 printf("centroid ");
261 if (q->flags.q.uniform)
262 printf("uniform ");
263 if (q->flags.q.smooth)
264 printf("smooth ");
265 if (q->flags.q.flat)
266 printf("flat ");
267 if (q->flags.q.noperspective)
268 printf("noperspective ");
269 }
270
271
272 void
273 ast_node::print(void) const
274 {
275 printf("unhandled node ");
276 }
277
278
279 ast_node::ast_node(void)
280 {
281 this->location.source = 0;
282 this->location.line = 0;
283 this->location.column = 0;
284 }
285
286
287 static void
288 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
289 {
290 if (is_array) {
291 printf("[ ");
292
293 if (array_size)
294 array_size->print();
295
296 printf("] ");
297 }
298 }
299
300
301 void
302 ast_compound_statement::print(void) const
303 {
304 printf("{\n");
305
306 foreach_list_const(n, &this->statements) {
307 ast_node *ast = exec_node_data(ast_node, n, link);
308 ast->print();
309 }
310
311 printf("}\n");
312 }
313
314
315 ast_compound_statement::ast_compound_statement(int new_scope,
316 ast_node *statements)
317 {
318 this->new_scope = new_scope;
319
320 if (statements != NULL) {
321 this->statements.push_degenerate_list_at_head(&statements->link);
322 }
323 }
324
325
326 void
327 ast_expression::print(void) const
328 {
329 switch (oper) {
330 case ast_assign:
331 case ast_mul_assign:
332 case ast_div_assign:
333 case ast_mod_assign:
334 case ast_add_assign:
335 case ast_sub_assign:
336 case ast_ls_assign:
337 case ast_rs_assign:
338 case ast_and_assign:
339 case ast_xor_assign:
340 case ast_or_assign:
341 subexpressions[0]->print();
342 printf("%s ", operator_string(oper));
343 subexpressions[1]->print();
344 break;
345
346 case ast_field_selection:
347 subexpressions[0]->print();
348 printf(". %s ", primary_expression.identifier);
349 break;
350
351 case ast_plus:
352 case ast_neg:
353 case ast_bit_not:
354 case ast_logic_not:
355 case ast_pre_inc:
356 case ast_pre_dec:
357 printf("%s ", operator_string(oper));
358 subexpressions[0]->print();
359 break;
360
361 case ast_post_inc:
362 case ast_post_dec:
363 subexpressions[0]->print();
364 printf("%s ", operator_string(oper));
365 break;
366
367 case ast_conditional:
368 subexpressions[0]->print();
369 printf("? ");
370 subexpressions[1]->print();
371 printf(": ");
372 subexpressions[1]->print();
373 break;
374
375 case ast_array_index:
376 subexpressions[0]->print();
377 printf("[ ");
378 subexpressions[1]->print();
379 printf("] ");
380 break;
381
382 case ast_function_call: {
383 subexpressions[0]->print();
384 printf("( ");
385
386 foreach_list_const (n, &this->expressions) {
387 if (n != this->expressions.get_head())
388 printf(", ");
389
390 ast_node *ast = exec_node_data(ast_node, n, link);
391 ast->print();
392 }
393
394 printf(") ");
395 break;
396 }
397
398 case ast_identifier:
399 printf("%s ", primary_expression.identifier);
400 break;
401
402 case ast_int_constant:
403 printf("%d ", primary_expression.int_constant);
404 break;
405
406 case ast_uint_constant:
407 printf("%u ", primary_expression.uint_constant);
408 break;
409
410 case ast_float_constant:
411 printf("%f ", primary_expression.float_constant);
412 break;
413
414 case ast_bool_constant:
415 printf("%s ",
416 primary_expression.bool_constant
417 ? "true" : "false");
418 break;
419
420 case ast_sequence: {
421 printf("( ");
422 foreach_list_const(n, & this->expressions) {
423 if (n != this->expressions.get_head())
424 printf(", ");
425
426 ast_node *ast = exec_node_data(ast_node, n, link);
427 ast->print();
428 }
429 printf(") ");
430 break;
431 }
432
433 default:
434 assert(0);
435 break;
436 }
437 }
438
439 ast_expression::ast_expression(int oper,
440 ast_expression *ex0,
441 ast_expression *ex1,
442 ast_expression *ex2)
443 {
444 this->oper = ast_operators(oper);
445 this->subexpressions[0] = ex0;
446 this->subexpressions[1] = ex1;
447 this->subexpressions[2] = ex2;
448 }
449
450
451 void
452 ast_expression_statement::print(void) const
453 {
454 if (expression)
455 expression->print();
456
457 printf("; ");
458 }
459
460
461 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
462 expression(ex)
463 {
464 /* empty */
465 }
466
467
468 void
469 ast_function::print(void) const
470 {
471 return_type->print();
472 printf(" %s (", identifier);
473
474 foreach_list_const(n, & this->parameters) {
475 ast_node *ast = exec_node_data(ast_node, n, link);
476 ast->print();
477 }
478
479 printf(")");
480 }
481
482
483 ast_function::ast_function(void)
484 : is_definition(false), signature(NULL)
485 {
486 /* empty */
487 }
488
489
490 void
491 ast_fully_specified_type::print(void) const
492 {
493 _mesa_ast_type_qualifier_print(& qualifier);
494 specifier->print();
495 }
496
497
498 void
499 ast_parameter_declarator::print(void) const
500 {
501 type->print();
502 if (identifier)
503 printf("%s ", identifier);
504 ast_opt_array_size_print(is_array, array_size);
505 }
506
507
508 void
509 ast_function_definition::print(void) const
510 {
511 prototype->print();
512 body->print();
513 }
514
515
516 void
517 ast_declaration::print(void) const
518 {
519 printf("%s ", identifier);
520 ast_opt_array_size_print(is_array, array_size);
521
522 if (initializer) {
523 printf("= ");
524 initializer->print();
525 }
526 }
527
528
529 ast_declaration::ast_declaration(char *identifier, int is_array,
530 ast_expression *array_size,
531 ast_expression *initializer)
532 {
533 this->identifier = identifier;
534 this->is_array = is_array;
535 this->array_size = array_size;
536 this->initializer = initializer;
537 }
538
539
540 void
541 ast_declarator_list::print(void) const
542 {
543 assert(type || invariant);
544
545 if (type)
546 type->print();
547 else
548 printf("invariant ");
549
550 foreach_list_const (ptr, & this->declarations) {
551 if (ptr != this->declarations.get_head())
552 printf(", ");
553
554 ast_node *ast = exec_node_data(ast_node, ptr, link);
555 ast->print();
556 }
557
558 printf("; ");
559 }
560
561
562 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
563 {
564 this->type = type;
565 this->invariant = false;
566 }
567
568 void
569 ast_jump_statement::print(void) const
570 {
571 switch (mode) {
572 case ast_continue:
573 printf("continue; ");
574 break;
575 case ast_break:
576 printf("break; ");
577 break;
578 case ast_return:
579 printf("return ");
580 if (opt_return_value)
581 opt_return_value->print();
582
583 printf("; ");
584 break;
585 case ast_discard:
586 printf("discard; ");
587 break;
588 }
589 }
590
591
592 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
593 {
594 this->mode = ast_jump_modes(mode);
595
596 if (mode == ast_return)
597 opt_return_value = return_value;
598 }
599
600
601 void
602 ast_selection_statement::print(void) const
603 {
604 printf("if ( ");
605 condition->print();
606 printf(") ");
607
608 then_statement->print();
609
610 if (else_statement) {
611 printf("else ");
612 else_statement->print();
613 }
614
615 }
616
617
618 ast_selection_statement::ast_selection_statement(ast_expression *condition,
619 ast_node *then_statement,
620 ast_node *else_statement)
621 {
622 this->condition = condition;
623 this->then_statement = then_statement;
624 this->else_statement = else_statement;
625 }
626
627
628 void
629 ast_iteration_statement::print(void) const
630 {
631 switch (mode) {
632 case ast_for:
633 printf("for( ");
634 if (init_statement)
635 init_statement->print();
636 printf("; ");
637
638 if (condition)
639 condition->print();
640 printf("; ");
641
642 if (rest_expression)
643 rest_expression->print();
644 printf(") ");
645
646 body->print();
647 break;
648
649 case ast_while:
650 printf("while ( ");
651 if (condition)
652 condition->print();
653 printf(") ");
654 body->print();
655 break;
656
657 case ast_do_while:
658 printf("do ");
659 body->print();
660 printf("while ( ");
661 if (condition)
662 condition->print();
663 printf("); ");
664 break;
665 }
666 }
667
668
669 ast_iteration_statement::ast_iteration_statement(int mode,
670 ast_node *init,
671 ast_node *condition,
672 ast_expression *rest_expression,
673 ast_node *body)
674 {
675 this->mode = ast_iteration_modes(mode);
676 this->init_statement = init;
677 this->condition = condition;
678 this->rest_expression = rest_expression;
679 this->body = body;
680 }
681
682
683 void
684 ast_struct_specifier::print(void) const
685 {
686 printf("struct %s { ", name);
687 foreach_list_const(n, &this->declarations) {
688 ast_node *ast = exec_node_data(ast_node, n, link);
689 ast->print();
690 }
691 printf("} ");
692 }
693
694
695 ast_struct_specifier::ast_struct_specifier(char *identifier,
696 ast_node *declarator_list)
697 {
698 if (identifier == NULL) {
699 static unsigned anon_count = 1;
700 identifier = talloc_asprintf(this, "#anon_struct_%04x", anon_count);
701 anon_count++;
702 }
703 name = identifier;
704 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
705 }
706
707 bool
708 do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations)
709 {
710 GLboolean progress = GL_FALSE;
711
712 progress = do_sub_to_add_neg(ir) || progress;
713
714 if (linked) {
715 progress = do_function_inlining(ir) || progress;
716 progress = do_dead_functions(ir) || progress;
717 }
718 progress = do_structure_splitting(ir) || progress;
719 progress = do_if_simplification(ir) || progress;
720 progress = do_copy_propagation(ir) || progress;
721 if (linked)
722 progress = do_dead_code(ir) || progress;
723 else
724 progress = do_dead_code_unlinked(ir) || progress;
725 progress = do_dead_code_local(ir) || progress;
726 progress = do_tree_grafting(ir) || progress;
727 progress = do_constant_propagation(ir) || progress;
728 if (linked)
729 progress = do_constant_variable(ir) || progress;
730 else
731 progress = do_constant_variable_unlinked(ir) || progress;
732 progress = do_constant_folding(ir) || progress;
733 progress = do_algebraic(ir) || progress;
734 progress = do_lower_jumps(ir) || progress;
735 progress = do_vec_index_to_swizzle(ir) || progress;
736 progress = do_swizzle_swizzle(ir) || progress;
737 progress = do_noop_swizzle(ir) || progress;
738
739 progress = optimize_redundant_jumps(ir) || progress;
740
741 loop_state *ls = analyze_loop_variables(ir);
742 progress = set_loop_controls(ir, ls) || progress;
743 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
744 delete ls;
745
746 return progress;
747 }
748
749 extern "C" {
750
751 /**
752 * To be called at GL teardown time, this frees compiler datastructures.
753 *
754 * After calling this, any previously compiled shaders and shader
755 * programs would be invalid. So this should happen at approximately
756 * program exit.
757 */
758 void
759 _mesa_destroy_shader_compiler(void)
760 {
761 _mesa_destroy_shader_compiler_caches();
762
763 _mesa_glsl_release_types();
764 }
765
766 /**
767 * Releases compiler caches to trade off performance for memory.
768 *
769 * Intended to be used with glReleaseShaderCompiler().
770 */
771 void
772 _mesa_destroy_shader_compiler_caches(void)
773 {
774 _mesa_glsl_release_functions();
775 }
776
777 }