glsl2: Add new ir_constant::zero static method.
[mesa.git] / src / glsl / ir.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #pragma once
26 #ifndef IR_H
27 #define IR_H
28
29 #include <cstdio>
30 #include <cstdlib>
31
32 extern "C" {
33 #include <talloc.h>
34 }
35
36 #include "list.h"
37 #include "ir_visitor.h"
38 #include "ir_hierarchical_visitor.h"
39
40 #ifndef ARRAY_SIZE
41 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
42 #endif
43
44 enum ir_node_type {
45 ir_type_unset,
46 ir_type_variable,
47 ir_type_assignment,
48 ir_type_call,
49 ir_type_constant,
50 ir_type_dereference_array,
51 ir_type_dereference_record,
52 ir_type_dereference_variable,
53 ir_type_discard,
54 ir_type_expression,
55 ir_type_function,
56 ir_type_function_signature,
57 ir_type_if,
58 ir_type_loop,
59 ir_type_loop_jump,
60 ir_type_return,
61 ir_type_swizzle,
62 ir_type_texture,
63 ir_type_max, /**< maximum ir_type enum number, for validation */
64 };
65
66 /**
67 * Base class of all IR instructions
68 */
69 class ir_instruction : public exec_node {
70 public:
71 enum ir_node_type ir_type;
72 const struct glsl_type *type;
73
74 /** ir_print_visitor helper for debugging. */
75 void print(void) const;
76
77 virtual void accept(ir_visitor *) = 0;
78 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
79 virtual ir_instruction *clone(struct hash_table *ht) const = 0;
80
81 /**
82 * \name IR instruction downcast functions
83 *
84 * These functions either cast the object to a derived class or return
85 * \c NULL if the object's type does not match the specified derived class.
86 * Additional downcast functions will be added as needed.
87 */
88 /*@{*/
89 virtual class ir_variable * as_variable() { return NULL; }
90 virtual class ir_function * as_function() { return NULL; }
91 virtual class ir_dereference * as_dereference() { return NULL; }
92 virtual class ir_dereference_array * as_dereference_array() { return NULL; }
93 virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
94 virtual class ir_expression * as_expression() { return NULL; }
95 virtual class ir_rvalue * as_rvalue() { return NULL; }
96 virtual class ir_loop * as_loop() { return NULL; }
97 virtual class ir_assignment * as_assignment() { return NULL; }
98 virtual class ir_call * as_call() { return NULL; }
99 virtual class ir_return * as_return() { return NULL; }
100 virtual class ir_if * as_if() { return NULL; }
101 virtual class ir_swizzle * as_swizzle() { return NULL; }
102 virtual class ir_constant * as_constant() { return NULL; }
103 /*@}*/
104
105 protected:
106 ir_instruction()
107 {
108 ir_type = ir_type_unset;
109 type = NULL;
110 }
111 };
112
113
114 class ir_rvalue : public ir_instruction {
115 public:
116 virtual ir_rvalue *clone(struct hash_table *) const = 0;
117
118 virtual ir_constant *constant_expression_value() = 0;
119
120 virtual ir_rvalue * as_rvalue()
121 {
122 return this;
123 }
124
125 virtual bool is_lvalue()
126 {
127 return false;
128 }
129
130 /**
131 * Get the variable that is ultimately referenced by an r-value
132 */
133 virtual ir_variable *variable_referenced()
134 {
135 return NULL;
136 }
137
138
139 /**
140 * If an r-value is a reference to a whole variable, get that variable
141 *
142 * \return
143 * Pointer to a variable that is completely dereferenced by the r-value. If
144 * the r-value is not a dereference or the dereference does not access the
145 * entire variable (i.e., it's just one array element, struct field), \c NULL
146 * is returned.
147 */
148 virtual ir_variable *whole_variable_referenced()
149 {
150 return NULL;
151 }
152
153 protected:
154 ir_rvalue();
155 };
156
157
158 enum ir_variable_mode {
159 ir_var_auto = 0,
160 ir_var_uniform,
161 ir_var_in,
162 ir_var_out,
163 ir_var_inout,
164 ir_var_temporary /**< Temporary variable generated during compilation. */
165 };
166
167 enum ir_variable_interpolation {
168 ir_var_smooth = 0,
169 ir_var_flat,
170 ir_var_noperspective
171 };
172
173
174 class ir_variable : public ir_instruction {
175 public:
176 ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
177
178 virtual ir_variable *clone(struct hash_table *ht) const;
179
180 virtual ir_variable *as_variable()
181 {
182 return this;
183 }
184
185 virtual void accept(ir_visitor *v)
186 {
187 v->visit(this);
188 }
189
190 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
191
192
193 /**
194 * Get the string value for the interpolation qualifier
195 *
196 * \return
197 * If none of \c shader_in or \c shader_out is set, an empty string will
198 * be returned. Otherwise the string that would be used in a shader to
199 * specify \c mode will be returned.
200 */
201 const char *interpolation_string() const;
202
203 /**
204 * Calculate the number of slots required to hold this variable
205 *
206 * This is used to determine how many uniform or varying locations a variable
207 * occupies. The count is in units of floating point components.
208 */
209 unsigned component_slots() const;
210
211 const char *name;
212
213 /**
214 * Highest element accessed with a constant expression array index
215 *
216 * Not used for non-array variables.
217 */
218 unsigned max_array_access;
219
220 unsigned read_only:1;
221 unsigned centroid:1;
222 unsigned invariant:1;
223 /** If the variable is initialized outside of the scope of the shader */
224 unsigned shader_in:1;
225 /**
226 * If the variable value is later used outside of the scope of the shader.
227 */
228 unsigned shader_out:1;
229
230 unsigned mode:3;
231 unsigned interpolation:2;
232
233 /**
234 * Flag that the whole array is assignable
235 *
236 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
237 * equality). This flag enables this behavior.
238 */
239 unsigned array_lvalue:1;
240
241 /* ARB_fragment_coord_conventions */
242 unsigned origin_upper_left:1;
243 unsigned pixel_center_integer:1;
244
245 /**
246 * Storage location of the base of this variable
247 *
248 * The precise meaning of this field depends on the nature of the variable.
249 *
250 * - Vertex shader input: one of the values from \c gl_vert_attrib.
251 * - Vertex shader output: one of the values from \c gl_vert_result.
252 * - Fragment shader input: one of the values from \c gl_frag_attrib.
253 * - Fragment shader output: one of the values from \c gl_frag_result.
254 * - Uniforms: Per-stage uniform slot number.
255 * - Other: This field is not currently used.
256 *
257 * If the variable is a uniform, shader input, or shader output, and the
258 * slot has not been assigned, the value will be -1.
259 */
260 int location;
261
262 /**
263 * Emit a warning if this variable is accessed.
264 */
265 const char *warn_extension;
266
267 /**
268 * Value assigned in the initializer of a variable declared "const"
269 */
270 ir_constant *constant_value;
271 };
272
273
274 /*@{*/
275 /**
276 * The representation of a function instance; may be the full definition or
277 * simply a prototype.
278 */
279 class ir_function_signature : public ir_instruction {
280 /* An ir_function_signature will be part of the list of signatures in
281 * an ir_function.
282 */
283 public:
284 ir_function_signature(const glsl_type *return_type);
285
286 virtual ir_function_signature *clone(struct hash_table *ht) const;
287
288 virtual void accept(ir_visitor *v)
289 {
290 v->visit(this);
291 }
292
293 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
294
295 /**
296 * Get the name of the function for which this is a signature
297 */
298 const char *function_name() const;
299
300 /**
301 * Get a handle to the function for which this is a signature
302 *
303 * There is no setter function, this function returns a \c const pointer,
304 * and \c ir_function_signature::_function is private for a reason. The
305 * only way to make a connection between a function and function signature
306 * is via \c ir_function::add_signature. This helps ensure that certain
307 * invariants (i.e., a function signature is in the list of signatures for
308 * its \c _function) are met.
309 *
310 * \sa ir_function::add_signature
311 */
312 inline const class ir_function *function() const
313 {
314 return this->_function;
315 }
316
317 /**
318 * Check whether the qualifiers match between this signature's parameters
319 * and the supplied parameter list. If not, returns the name of the first
320 * parameter with mismatched qualifiers (for use in error messages).
321 */
322 const char *qualifiers_match(exec_list *params);
323
324 /**
325 * Replace the current parameter list with the given one. This is useful
326 * if the current information came from a prototype, and either has invalid
327 * or missing parameter names.
328 */
329 void replace_parameters(exec_list *new_params);
330
331 /**
332 * Function return type.
333 *
334 * \note This discards the optional precision qualifier.
335 */
336 const struct glsl_type *return_type;
337
338 /**
339 * List of ir_variable of function parameters.
340 *
341 * This represents the storage. The paramaters passed in a particular
342 * call will be in ir_call::actual_paramaters.
343 */
344 struct exec_list parameters;
345
346 /** Whether or not this function has a body (which may be empty). */
347 unsigned is_defined:1;
348
349 /** Whether or not this function signature is a built-in. */
350 unsigned is_built_in:1;
351
352 /** Body of instructions in the function. */
353 struct exec_list body;
354
355 private:
356 /** Function of which this signature is one overload. */
357 class ir_function *_function;
358
359 friend class ir_function;
360 };
361
362
363 /**
364 * Header for tracking multiple overloaded functions with the same name.
365 * Contains a list of ir_function_signatures representing each of the
366 * actual functions.
367 */
368 class ir_function : public ir_instruction {
369 public:
370 ir_function(const char *name);
371
372 virtual ir_function *clone(struct hash_table *ht) const;
373
374 virtual ir_function *as_function()
375 {
376 return this;
377 }
378
379 virtual void accept(ir_visitor *v)
380 {
381 v->visit(this);
382 }
383
384 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
385
386 void add_signature(ir_function_signature *sig)
387 {
388 sig->_function = this;
389 this->signatures.push_tail(sig);
390 }
391
392 /**
393 * Get an iterator for the set of function signatures
394 */
395 exec_list_iterator iterator()
396 {
397 return signatures.iterator();
398 }
399
400 /**
401 * Find a signature that matches a set of actual parameters, taking implicit
402 * conversions into account.
403 */
404 ir_function_signature *matching_signature(const exec_list *actual_param);
405
406 /**
407 * Find a signature that exactly matches a set of actual parameters without
408 * any implicit type conversions.
409 */
410 ir_function_signature *exact_matching_signature(const exec_list *actual_ps);
411
412 /**
413 * Name of the function.
414 */
415 const char *name;
416
417 private:
418 /**
419 * List of ir_function_signature for each overloaded function with this name.
420 */
421 struct exec_list signatures;
422 };
423
424 inline const char *ir_function_signature::function_name() const
425 {
426 return this->_function->name;
427 }
428 /*@}*/
429
430
431 /**
432 * IR instruction representing high-level if-statements
433 */
434 class ir_if : public ir_instruction {
435 public:
436 ir_if(ir_rvalue *condition)
437 : condition(condition)
438 {
439 ir_type = ir_type_if;
440 }
441
442 virtual ir_if *clone(struct hash_table *ht) const;
443
444 virtual ir_if *as_if()
445 {
446 return this;
447 }
448
449 virtual void accept(ir_visitor *v)
450 {
451 v->visit(this);
452 }
453
454 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
455
456 ir_rvalue *condition;
457 /** List of ir_instruction for the body of the then branch */
458 exec_list then_instructions;
459 /** List of ir_instruction for the body of the else branch */
460 exec_list else_instructions;
461 };
462
463
464 /**
465 * IR instruction representing a high-level loop structure.
466 */
467 class ir_loop : public ir_instruction {
468 public:
469 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
470 {
471 ir_type = ir_type_loop;
472 }
473
474 virtual ir_loop *clone(struct hash_table *ht) const;
475
476 virtual void accept(ir_visitor *v)
477 {
478 v->visit(this);
479 }
480
481 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
482
483 virtual ir_loop *as_loop()
484 {
485 return this;
486 }
487
488 /**
489 * Get an iterator for the instructions of the loop body
490 */
491 exec_list_iterator iterator()
492 {
493 return body_instructions.iterator();
494 }
495
496 /** List of ir_instruction that make up the body of the loop. */
497 exec_list body_instructions;
498
499 /**
500 * \name Loop counter and controls
501 */
502 /*@{*/
503 ir_rvalue *from;
504 ir_rvalue *to;
505 ir_rvalue *increment;
506 ir_variable *counter;
507 /*@}*/
508 };
509
510
511 class ir_assignment : public ir_instruction {
512 public:
513 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
514
515 virtual ir_assignment *clone(struct hash_table *ht) const;
516
517 virtual ir_constant *constant_expression_value();
518
519 virtual void accept(ir_visitor *v)
520 {
521 v->visit(this);
522 }
523
524 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
525
526 virtual ir_assignment * as_assignment()
527 {
528 return this;
529 }
530
531 /**
532 * Left-hand side of the assignment.
533 */
534 ir_rvalue *lhs;
535
536 /**
537 * Value being assigned
538 */
539 ir_rvalue *rhs;
540
541 /**
542 * Optional condition for the assignment.
543 */
544 ir_rvalue *condition;
545 };
546
547 /* Update ir_expression::num_operands() and operator_strs when
548 * updating this list.
549 */
550 enum ir_expression_operation {
551 ir_unop_bit_not,
552 ir_unop_logic_not,
553 ir_unop_neg,
554 ir_unop_abs,
555 ir_unop_sign,
556 ir_unop_rcp,
557 ir_unop_rsq,
558 ir_unop_sqrt,
559 ir_unop_exp,
560 ir_unop_log,
561 ir_unop_exp2,
562 ir_unop_log2,
563 ir_unop_f2i, /**< Float-to-integer conversion. */
564 ir_unop_i2f, /**< Integer-to-float conversion. */
565 ir_unop_f2b, /**< Float-to-boolean conversion */
566 ir_unop_b2f, /**< Boolean-to-float conversion */
567 ir_unop_i2b, /**< int-to-boolean conversion */
568 ir_unop_b2i, /**< Boolean-to-int conversion */
569 ir_unop_u2f, /**< Unsigned-to-float conversion. */
570
571 /**
572 * \name Unary floating-point rounding operations.
573 */
574 /*@{*/
575 ir_unop_trunc,
576 ir_unop_ceil,
577 ir_unop_floor,
578 ir_unop_fract,
579 /*@}*/
580
581 /**
582 * \name Trigonometric operations.
583 */
584 /*@{*/
585 ir_unop_sin,
586 ir_unop_cos,
587 /*@}*/
588
589 /**
590 * \name Partial derivatives.
591 */
592 /*@{*/
593 ir_unop_dFdx,
594 ir_unop_dFdy,
595 /*@}*/
596
597 ir_binop_add,
598 ir_binop_sub,
599 ir_binop_mul,
600 ir_binop_div,
601
602 /**
603 * Takes one of two combinations of arguments:
604 *
605 * - mod(vecN, vecN)
606 * - mod(vecN, float)
607 *
608 * Does not take integer types.
609 */
610 ir_binop_mod,
611
612 /**
613 * \name Binary comparison operators
614 */
615 /*@{*/
616 ir_binop_less,
617 ir_binop_greater,
618 ir_binop_lequal,
619 ir_binop_gequal,
620 /**
621 * Returns single boolean for whether all components of operands[0]
622 * equal the components of operands[1].
623 */
624 ir_binop_equal,
625 /**
626 * Returns single boolean for whether any component of operands[0]
627 * is not equal to the corresponding component of operands[1].
628 */
629 ir_binop_nequal,
630 /*@}*/
631
632 /**
633 * \name Bit-wise binary operations.
634 */
635 /*@{*/
636 ir_binop_lshift,
637 ir_binop_rshift,
638 ir_binop_bit_and,
639 ir_binop_bit_xor,
640 ir_binop_bit_or,
641 /*@}*/
642
643 ir_binop_logic_and,
644 ir_binop_logic_xor,
645 ir_binop_logic_or,
646
647 ir_binop_dot,
648 ir_binop_cross,
649 ir_binop_min,
650 ir_binop_max,
651
652 ir_binop_pow
653 };
654
655 class ir_expression : public ir_rvalue {
656 public:
657 ir_expression(int op, const struct glsl_type *type,
658 ir_rvalue *, ir_rvalue *);
659
660 virtual ir_expression *as_expression()
661 {
662 return this;
663 }
664
665 virtual ir_expression *clone(struct hash_table *ht) const;
666
667 virtual ir_constant *constant_expression_value();
668
669 static unsigned int get_num_operands(ir_expression_operation);
670 unsigned int get_num_operands() const
671 {
672 return get_num_operands(operation);
673 }
674
675 /**
676 * Return a string representing this expression's operator.
677 */
678 const char *operator_string();
679
680 /**
681 * Do a reverse-lookup to translate the given string into an operator.
682 */
683 static ir_expression_operation get_operator(const char *);
684
685 virtual void accept(ir_visitor *v)
686 {
687 v->visit(this);
688 }
689
690 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
691
692 ir_expression_operation operation;
693 ir_rvalue *operands[2];
694 };
695
696
697 /**
698 * IR instruction representing a function call
699 */
700 class ir_call : public ir_rvalue {
701 public:
702 ir_call(ir_function_signature *callee, exec_list *actual_parameters)
703 : callee(callee)
704 {
705 ir_type = ir_type_call;
706 assert(callee->return_type != NULL);
707 type = callee->return_type;
708 actual_parameters->move_nodes_to(& this->actual_parameters);
709 }
710
711 virtual ir_call *clone(struct hash_table *ht) const;
712
713 virtual ir_constant *constant_expression_value();
714
715 virtual ir_call *as_call()
716 {
717 return this;
718 }
719
720 virtual void accept(ir_visitor *v)
721 {
722 v->visit(this);
723 }
724
725 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
726
727 /**
728 * Get a generic ir_call object when an error occurs
729 *
730 * Any allocation will be performed with 'ctx' as talloc owner.
731 */
732 static ir_call *get_error_instruction(void *ctx);
733
734 /**
735 * Get an iterator for the set of acutal parameters
736 */
737 exec_list_iterator iterator()
738 {
739 return actual_parameters.iterator();
740 }
741
742 /**
743 * Get the name of the function being called.
744 */
745 const char *callee_name() const
746 {
747 return callee->function_name();
748 }
749
750 ir_function_signature *get_callee()
751 {
752 return callee;
753 }
754
755 /**
756 * Set the function call target
757 */
758 void set_callee(ir_function_signature *sig);
759
760 /**
761 * Generates an inline version of the function before @ir,
762 * returning the return value of the function.
763 */
764 ir_rvalue *generate_inline(ir_instruction *ir);
765
766 /* List of ir_rvalue of paramaters passed in this call. */
767 exec_list actual_parameters;
768
769 private:
770 ir_call()
771 : callee(NULL)
772 {
773 this->ir_type = ir_type_call;
774 }
775
776 ir_function_signature *callee;
777 };
778
779
780 /**
781 * \name Jump-like IR instructions.
782 *
783 * These include \c break, \c continue, \c return, and \c discard.
784 */
785 /*@{*/
786 class ir_jump : public ir_instruction {
787 protected:
788 ir_jump()
789 {
790 ir_type = ir_type_unset;
791 }
792 };
793
794 class ir_return : public ir_jump {
795 public:
796 ir_return()
797 : value(NULL)
798 {
799 this->ir_type = ir_type_return;
800 }
801
802 ir_return(ir_rvalue *value)
803 : value(value)
804 {
805 this->ir_type = ir_type_return;
806 }
807
808 virtual ir_return *clone(struct hash_table *) const;
809
810 virtual ir_return *as_return()
811 {
812 return this;
813 }
814
815 ir_rvalue *get_value() const
816 {
817 return value;
818 }
819
820 virtual void accept(ir_visitor *v)
821 {
822 v->visit(this);
823 }
824
825 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
826
827 ir_rvalue *value;
828 };
829
830
831 /**
832 * Jump instructions used inside loops
833 *
834 * These include \c break and \c continue. The \c break within a loop is
835 * different from the \c break within a switch-statement.
836 *
837 * \sa ir_switch_jump
838 */
839 class ir_loop_jump : public ir_jump {
840 public:
841 enum jump_mode {
842 jump_break,
843 jump_continue
844 };
845
846 ir_loop_jump(jump_mode mode)
847 {
848 this->ir_type = ir_type_loop_jump;
849 this->mode = mode;
850 this->loop = loop;
851 }
852
853 virtual ir_loop_jump *clone(struct hash_table *) const;
854
855 virtual void accept(ir_visitor *v)
856 {
857 v->visit(this);
858 }
859
860 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
861
862 bool is_break() const
863 {
864 return mode == jump_break;
865 }
866
867 bool is_continue() const
868 {
869 return mode == jump_continue;
870 }
871
872 /** Mode selector for the jump instruction. */
873 enum jump_mode mode;
874 private:
875 /** Loop containing this break instruction. */
876 ir_loop *loop;
877 };
878
879 /**
880 * IR instruction representing discard statements.
881 */
882 class ir_discard : public ir_jump {
883 public:
884 ir_discard()
885 {
886 this->ir_type = ir_type_discard;
887 this->condition = NULL;
888 }
889
890 ir_discard(ir_rvalue *cond)
891 {
892 this->condition = cond;
893 }
894
895 virtual ir_discard *clone(struct hash_table *ht) const;
896
897 virtual void accept(ir_visitor *v)
898 {
899 v->visit(this);
900 }
901
902 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
903
904 ir_rvalue *condition;
905 };
906 /*@}*/
907
908
909 /**
910 * Texture sampling opcodes used in ir_texture
911 */
912 enum ir_texture_opcode {
913 ir_tex, /* Regular texture look-up */
914 ir_txb, /* Texture look-up with LOD bias */
915 ir_txl, /* Texture look-up with explicit LOD */
916 ir_txd, /* Texture look-up with partial derivatvies */
917 ir_txf /* Texel fetch with explicit LOD */
918 };
919
920
921 /**
922 * IR instruction to sample a texture
923 *
924 * The specific form of the IR instruction depends on the \c mode value
925 * selected from \c ir_texture_opcodes. In the printed IR, these will
926 * appear as:
927 *
928 * Texel offset
929 * | Projection divisor
930 * | | Shadow comparitor
931 * | | |
932 * v v v
933 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
934 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
935 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
936 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
937 * (txf (sampler) (coordinate) (0 0 0) (lod))
938 */
939 class ir_texture : public ir_rvalue {
940 public:
941 ir_texture(enum ir_texture_opcode op)
942 : op(op), projector(NULL), shadow_comparitor(NULL)
943 {
944 this->ir_type = ir_type_texture;
945 }
946
947 virtual ir_texture *clone(struct hash_table *) const;
948
949 virtual ir_constant *constant_expression_value();
950
951 virtual void accept(ir_visitor *v)
952 {
953 v->visit(this);
954 }
955
956 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
957
958 /**
959 * Return a string representing the ir_texture_opcode.
960 */
961 const char *opcode_string();
962
963 /** Set the sampler and infer the type. */
964 void set_sampler(ir_dereference *sampler);
965
966 /**
967 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
968 */
969 static ir_texture_opcode get_opcode(const char *);
970
971 enum ir_texture_opcode op;
972
973 /** Sampler to use for the texture access. */
974 ir_dereference *sampler;
975
976 /** Texture coordinate to sample */
977 ir_rvalue *coordinate;
978
979 /**
980 * Value used for projective divide.
981 *
982 * If there is no projective divide (the common case), this will be
983 * \c NULL. Optimization passes should check for this to point to a constant
984 * of 1.0 and replace that with \c NULL.
985 */
986 ir_rvalue *projector;
987
988 /**
989 * Coordinate used for comparison on shadow look-ups.
990 *
991 * If there is no shadow comparison, this will be \c NULL. For the
992 * \c ir_txf opcode, this *must* be \c NULL.
993 */
994 ir_rvalue *shadow_comparitor;
995
996 /** Explicit texel offsets. */
997 signed char offsets[3];
998
999 union {
1000 ir_rvalue *lod; /**< Floating point LOD */
1001 ir_rvalue *bias; /**< Floating point LOD bias */
1002 struct {
1003 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
1004 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
1005 } grad;
1006 } lod_info;
1007 };
1008
1009
1010 struct ir_swizzle_mask {
1011 unsigned x:2;
1012 unsigned y:2;
1013 unsigned z:2;
1014 unsigned w:2;
1015
1016 /**
1017 * Number of components in the swizzle.
1018 */
1019 unsigned num_components:3;
1020
1021 /**
1022 * Does the swizzle contain duplicate components?
1023 *
1024 * L-value swizzles cannot contain duplicate components.
1025 */
1026 unsigned has_duplicates:1;
1027 };
1028
1029
1030 class ir_swizzle : public ir_rvalue {
1031 public:
1032 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1033 unsigned count);
1034
1035 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1036
1037 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1038
1039 virtual ir_swizzle *clone(struct hash_table *) const;
1040
1041 virtual ir_constant *constant_expression_value();
1042
1043 virtual ir_swizzle *as_swizzle()
1044 {
1045 return this;
1046 }
1047
1048 /**
1049 * Construct an ir_swizzle from the textual representation. Can fail.
1050 */
1051 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1052
1053 virtual void accept(ir_visitor *v)
1054 {
1055 v->visit(this);
1056 }
1057
1058 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1059
1060 bool is_lvalue()
1061 {
1062 return val->is_lvalue() && !mask.has_duplicates;
1063 }
1064
1065 /**
1066 * Get the variable that is ultimately referenced by an r-value
1067 */
1068 virtual ir_variable *variable_referenced();
1069
1070 ir_rvalue *val;
1071 ir_swizzle_mask mask;
1072
1073 private:
1074 /**
1075 * Initialize the mask component of a swizzle
1076 *
1077 * This is used by the \c ir_swizzle constructors.
1078 */
1079 void init_mask(const unsigned *components, unsigned count);
1080 };
1081
1082
1083 class ir_dereference : public ir_rvalue {
1084 public:
1085 virtual ir_dereference *clone(struct hash_table *) const = 0;
1086
1087 virtual ir_dereference *as_dereference()
1088 {
1089 return this;
1090 }
1091
1092 bool is_lvalue();
1093
1094 /**
1095 * Get the variable that is ultimately referenced by an r-value
1096 */
1097 virtual ir_variable *variable_referenced() = 0;
1098 };
1099
1100
1101 class ir_dereference_variable : public ir_dereference {
1102 public:
1103 ir_dereference_variable(ir_variable *var);
1104
1105 virtual ir_dereference_variable *clone(struct hash_table *) const;
1106
1107 virtual ir_constant *constant_expression_value();
1108
1109 virtual ir_dereference_variable *as_dereference_variable()
1110 {
1111 return this;
1112 }
1113
1114 /**
1115 * Get the variable that is ultimately referenced by an r-value
1116 */
1117 virtual ir_variable *variable_referenced()
1118 {
1119 return this->var;
1120 }
1121
1122 virtual ir_variable *whole_variable_referenced()
1123 {
1124 /* ir_dereference_variable objects always dereference the entire
1125 * variable. However, if this dereference is dereferenced by anything
1126 * else, the complete deferefernce chain is not a whole-variable
1127 * dereference. This method should only be called on the top most
1128 * ir_rvalue in a dereference chain.
1129 */
1130 return this->var;
1131 }
1132
1133 virtual void accept(ir_visitor *v)
1134 {
1135 v->visit(this);
1136 }
1137
1138 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1139
1140 /**
1141 * Object being dereferenced.
1142 */
1143 ir_variable *var;
1144 };
1145
1146
1147 class ir_dereference_array : public ir_dereference {
1148 public:
1149 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1150
1151 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1152
1153 virtual ir_dereference_array *clone(struct hash_table *) const;
1154
1155 virtual ir_constant *constant_expression_value();
1156
1157 virtual ir_dereference_array *as_dereference_array()
1158 {
1159 return this;
1160 }
1161
1162 /**
1163 * Get the variable that is ultimately referenced by an r-value
1164 */
1165 virtual ir_variable *variable_referenced()
1166 {
1167 return this->array->variable_referenced();
1168 }
1169
1170 virtual void accept(ir_visitor *v)
1171 {
1172 v->visit(this);
1173 }
1174
1175 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1176
1177 ir_rvalue *array;
1178 ir_rvalue *array_index;
1179
1180 private:
1181 void set_array(ir_rvalue *value);
1182 };
1183
1184
1185 class ir_dereference_record : public ir_dereference {
1186 public:
1187 ir_dereference_record(ir_rvalue *value, const char *field);
1188
1189 ir_dereference_record(ir_variable *var, const char *field);
1190
1191 virtual ir_dereference_record *clone(struct hash_table *) const;
1192
1193 virtual ir_constant *constant_expression_value();
1194
1195 /**
1196 * Get the variable that is ultimately referenced by an r-value
1197 */
1198 virtual ir_variable *variable_referenced()
1199 {
1200 return this->record->variable_referenced();
1201 }
1202
1203 virtual void accept(ir_visitor *v)
1204 {
1205 v->visit(this);
1206 }
1207
1208 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1209
1210 ir_rvalue *record;
1211 const char *field;
1212 };
1213
1214
1215 /**
1216 * Data stored in an ir_constant
1217 */
1218 union ir_constant_data {
1219 unsigned u[16];
1220 int i[16];
1221 float f[16];
1222 bool b[16];
1223 };
1224
1225
1226 class ir_constant : public ir_rvalue {
1227 public:
1228 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1229 ir_constant(bool b);
1230 ir_constant(unsigned int u);
1231 ir_constant(int i);
1232 ir_constant(float f);
1233
1234 /**
1235 * Construct an ir_constant from a list of ir_constant values
1236 */
1237 ir_constant(const struct glsl_type *type, exec_list *values);
1238
1239 /**
1240 * Construct an ir_constant from a scalar component of another ir_constant
1241 *
1242 * The new \c ir_constant inherits the type of the component from the
1243 * source constant.
1244 *
1245 * \note
1246 * In the case of a matrix constant, the new constant is a scalar, \b not
1247 * a vector.
1248 */
1249 ir_constant(const ir_constant *c, unsigned i);
1250
1251 /**
1252 * Return a new ir_constant of the specified type containing all zeros.
1253 */
1254 static ir_constant *zero(void *mem_ctx, const glsl_type *type);
1255
1256 virtual ir_constant *clone(struct hash_table *) const;
1257
1258 virtual ir_constant *constant_expression_value();
1259
1260 virtual ir_constant *as_constant()
1261 {
1262 return this;
1263 }
1264
1265 virtual void accept(ir_visitor *v)
1266 {
1267 v->visit(this);
1268 }
1269
1270 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1271
1272 /**
1273 * Get a particular component of a constant as a specific type
1274 *
1275 * This is useful, for example, to get a value from an integer constant
1276 * as a float or bool. This appears frequently when constructors are
1277 * called with all constant parameters.
1278 */
1279 /*@{*/
1280 bool get_bool_component(unsigned i) const;
1281 float get_float_component(unsigned i) const;
1282 int get_int_component(unsigned i) const;
1283 unsigned get_uint_component(unsigned i) const;
1284 /*@}*/
1285
1286 ir_constant *get_array_element(unsigned i) const;
1287
1288 ir_constant *get_record_field(const char *name);
1289
1290 /**
1291 * Determine whether a constant has the same value as another constant
1292 */
1293 bool has_value(const ir_constant *) const;
1294
1295 /**
1296 * Value of the constant.
1297 *
1298 * The field used to back the values supplied by the constant is determined
1299 * by the type associated with the \c ir_instruction. Constants may be
1300 * scalars, vectors, or matrices.
1301 */
1302 union ir_constant_data value;
1303
1304 /* Array elements */
1305 ir_constant **array_elements;
1306
1307 /* Structure fields */
1308 exec_list components;
1309
1310 private:
1311 /**
1312 * Parameterless constructor only used by the clone method
1313 */
1314 ir_constant(void);
1315 };
1316
1317 void
1318 visit_exec_list(exec_list *list, ir_visitor *visitor);
1319
1320 void validate_ir_tree(exec_list *instructions);
1321
1322 /**
1323 * Make a clone of each IR instruction in a list
1324 *
1325 * \param in List of IR instructions that are to be cloned
1326 * \param out List to hold the cloned instructions
1327 */
1328 void
1329 clone_ir_list(exec_list *out, const exec_list *in);
1330
1331 extern void
1332 _mesa_glsl_initialize_variables(exec_list *instructions,
1333 struct _mesa_glsl_parse_state *state);
1334
1335 extern void
1336 _mesa_glsl_initialize_functions(exec_list *instructions,
1337 struct _mesa_glsl_parse_state *state);
1338
1339 extern void
1340 _mesa_glsl_release_functions(void);
1341
1342 extern void
1343 reparent_ir(exec_list *list, void *mem_ctx);
1344
1345 class glsl_symbol_table;
1346
1347 extern void
1348 import_prototypes(const exec_list *source, exec_list *dest,
1349 class glsl_symbol_table *symbols, void *mem_ctx);
1350
1351 #endif /* IR_H */