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