053b6fecd1d5feb3a9d6a70d114d8dadd0c5f1cb
[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 * Get a handle to the function for which this is a signature
280 *
281 * There is no setter function, this function returns a \c const pointer,
282 * and \c ir_function_signature::_function is private for a reason. The
283 * only way to make a connection between a function and function signature
284 * is via \c ir_function::add_signature. This helps ensure that certain
285 * invariants (i.e., a function signature is in the list of signatures for
286 * its \c _function) are met.
287 *
288 * \sa ir_function::add_signature
289 */
290 inline const class ir_function *function() const
291 {
292 return this->_function;
293 }
294
295 /**
296 * Check whether the qualifiers match between this signature's parameters
297 * and the supplied parameter list. If not, returns the name of the first
298 * parameter with mismatched qualifiers (for use in error messages).
299 */
300 const char *qualifiers_match(exec_list *params);
301
302 /**
303 * Replace the current parameter list with the given one. This is useful
304 * if the current information came from a prototype, and either has invalid
305 * or missing parameter names.
306 */
307 void replace_parameters(exec_list *new_params);
308
309 /**
310 * Function return type.
311 *
312 * \note This discards the optional precision qualifier.
313 */
314 const struct glsl_type *return_type;
315
316 /**
317 * List of ir_variable of function parameters.
318 *
319 * This represents the storage. The paramaters passed in a particular
320 * call will be in ir_call::actual_paramaters.
321 */
322 struct exec_list parameters;
323
324 /** Whether or not this function has a body (which may be empty). */
325 unsigned is_defined:1;
326
327 /** Whether or not this function signature is a built-in. */
328 unsigned is_built_in:1;
329
330 /** Body of instructions in the function. */
331 struct exec_list body;
332
333 private:
334 /** Function of which this signature is one overload. */
335 class ir_function *_function;
336
337 friend class ir_function;
338 };
339
340
341 /**
342 * Header for tracking multiple overloaded functions with the same name.
343 * Contains a list of ir_function_signatures representing each of the
344 * actual functions.
345 */
346 class ir_function : public ir_instruction {
347 public:
348 ir_function(const char *name);
349
350 virtual ir_function *clone(struct hash_table *ht) const;
351
352 virtual ir_function *as_function()
353 {
354 return this;
355 }
356
357 virtual void accept(ir_visitor *v)
358 {
359 v->visit(this);
360 }
361
362 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
363
364 void add_signature(ir_function_signature *sig)
365 {
366 sig->_function = this;
367 this->signatures.push_tail(sig);
368 }
369
370 /**
371 * Get an iterator for the set of function signatures
372 */
373 exec_list_iterator iterator()
374 {
375 return signatures.iterator();
376 }
377
378 /**
379 * Find a signature that matches a set of actual parameters, taking implicit
380 * conversions into account.
381 */
382 ir_function_signature *matching_signature(exec_list *actual_param);
383
384 /**
385 * Find a signature that exactly matches a set of actual parameters without
386 * any implicit type conversions.
387 */
388 ir_function_signature *exact_matching_signature(exec_list *actual_ps);
389
390 /**
391 * Name of the function.
392 */
393 const char *name;
394
395 private:
396 /**
397 * List of ir_function_signature for each overloaded function with this name.
398 */
399 struct exec_list signatures;
400 };
401
402 inline const char *ir_function_signature::function_name() const
403 {
404 return this->_function->name;
405 }
406 /*@}*/
407
408
409 /**
410 * IR instruction representing high-level if-statements
411 */
412 class ir_if : public ir_instruction {
413 public:
414 ir_if(ir_rvalue *condition)
415 : condition(condition)
416 {
417 /* empty */
418 }
419
420 virtual ir_if *clone(struct hash_table *ht) const;
421
422 virtual ir_if *as_if()
423 {
424 return this;
425 }
426
427 virtual void accept(ir_visitor *v)
428 {
429 v->visit(this);
430 }
431
432 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
433
434 ir_rvalue *condition;
435 /** List of ir_instruction for the body of the then branch */
436 exec_list then_instructions;
437 /** List of ir_instruction for the body of the else branch */
438 exec_list else_instructions;
439 };
440
441
442 /**
443 * IR instruction representing a high-level loop structure.
444 */
445 class ir_loop : public ir_instruction {
446 public:
447 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
448 {
449 /* empty */
450 }
451
452 virtual ir_loop *clone(struct hash_table *ht) const;
453
454 virtual void accept(ir_visitor *v)
455 {
456 v->visit(this);
457 }
458
459 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
460
461 virtual ir_loop *as_loop()
462 {
463 return this;
464 }
465
466 /**
467 * Get an iterator for the instructions of the loop body
468 */
469 exec_list_iterator iterator()
470 {
471 return body_instructions.iterator();
472 }
473
474 /** List of ir_instruction that make up the body of the loop. */
475 exec_list body_instructions;
476
477 /**
478 * \name Loop counter and controls
479 */
480 /*@{*/
481 ir_rvalue *from;
482 ir_rvalue *to;
483 ir_rvalue *increment;
484 ir_variable *counter;
485 /*@}*/
486 };
487
488
489 class ir_assignment : public ir_rvalue {
490 public:
491 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
492
493 virtual ir_assignment *clone(struct hash_table *ht) const;
494
495 virtual void accept(ir_visitor *v)
496 {
497 v->visit(this);
498 }
499
500 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
501
502 virtual ir_assignment * as_assignment()
503 {
504 return this;
505 }
506
507 /**
508 * Left-hand side of the assignment.
509 */
510 ir_rvalue *lhs;
511
512 /**
513 * Value being assigned
514 */
515 ir_rvalue *rhs;
516
517 /**
518 * Optional condition for the assignment.
519 */
520 ir_rvalue *condition;
521 };
522
523 /* Update ir_expression::num_operands() and operator_strs when
524 * updating this list.
525 */
526 enum ir_expression_operation {
527 ir_unop_bit_not,
528 ir_unop_logic_not,
529 ir_unop_neg,
530 ir_unop_abs,
531 ir_unop_sign,
532 ir_unop_rcp,
533 ir_unop_rsq,
534 ir_unop_sqrt,
535 ir_unop_exp,
536 ir_unop_log,
537 ir_unop_exp2,
538 ir_unop_log2,
539 ir_unop_f2i, /**< Float-to-integer conversion. */
540 ir_unop_i2f, /**< Integer-to-float conversion. */
541 ir_unop_f2b, /**< Float-to-boolean conversion */
542 ir_unop_b2f, /**< Boolean-to-float conversion */
543 ir_unop_i2b, /**< int-to-boolean conversion */
544 ir_unop_b2i, /**< Boolean-to-int conversion */
545 ir_unop_u2f, /**< Unsigned-to-float conversion. */
546
547 /**
548 * \name Unary floating-point rounding operations.
549 */
550 /*@{*/
551 ir_unop_trunc,
552 ir_unop_ceil,
553 ir_unop_floor,
554 ir_unop_fract,
555 /*@}*/
556
557 /**
558 * \name Trigonometric operations.
559 */
560 /*@{*/
561 ir_unop_sin,
562 ir_unop_cos,
563 /*@}*/
564
565 /**
566 * \name Partial derivatives.
567 */
568 /*@{*/
569 ir_unop_dFdx,
570 ir_unop_dFdy,
571 /*@}*/
572
573 ir_binop_add,
574 ir_binop_sub,
575 ir_binop_mul,
576 ir_binop_div,
577
578 /**
579 * Takes one of two combinations of arguments:
580 *
581 * - mod(vecN, vecN)
582 * - mod(vecN, float)
583 *
584 * Does not take integer types.
585 */
586 ir_binop_mod,
587
588 /**
589 * \name Binary comparison operators
590 */
591 /*@{*/
592 ir_binop_less,
593 ir_binop_greater,
594 ir_binop_lequal,
595 ir_binop_gequal,
596 ir_binop_equal,
597 ir_binop_nequal,
598 /*@}*/
599
600 /**
601 * \name Bit-wise binary operations.
602 */
603 /*@{*/
604 ir_binop_lshift,
605 ir_binop_rshift,
606 ir_binop_bit_and,
607 ir_binop_bit_xor,
608 ir_binop_bit_or,
609 /*@}*/
610
611 ir_binop_logic_and,
612 ir_binop_logic_xor,
613 ir_binop_logic_or,
614
615 ir_binop_dot,
616 ir_binop_min,
617 ir_binop_max,
618
619 ir_binop_pow
620 };
621
622 class ir_expression : public ir_rvalue {
623 public:
624 ir_expression(int op, const struct glsl_type *type,
625 ir_rvalue *, ir_rvalue *);
626
627 virtual ir_expression *as_expression()
628 {
629 return this;
630 }
631
632 virtual ir_expression *clone(struct hash_table *ht) const;
633
634 static unsigned int get_num_operands(ir_expression_operation);
635 unsigned int get_num_operands() const
636 {
637 return get_num_operands(operation);
638 }
639
640 /**
641 * Return a string representing this expression's operator.
642 */
643 const char *operator_string();
644
645 /**
646 * Do a reverse-lookup to translate the given string into an operator.
647 */
648 static ir_expression_operation get_operator(const char *);
649
650 virtual void accept(ir_visitor *v)
651 {
652 v->visit(this);
653 }
654
655 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
656
657 ir_expression_operation operation;
658 ir_rvalue *operands[2];
659 };
660
661
662 /**
663 * IR instruction representing a function call
664 */
665 class ir_call : public ir_rvalue {
666 public:
667 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
668 : callee(callee)
669 {
670 assert(callee->return_type != NULL);
671 type = callee->return_type;
672 actual_parameters->move_nodes_to(& this->actual_parameters);
673 }
674
675 virtual ir_call *clone(struct hash_table *ht) const;
676
677 virtual ir_call *as_call()
678 {
679 return this;
680 }
681
682 virtual void accept(ir_visitor *v)
683 {
684 v->visit(this);
685 }
686
687 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
688
689 /**
690 * Get a generic ir_call object when an error occurs
691 *
692 * Any allocation will be performed with 'ctx' as talloc owner.
693 */
694 static ir_call *get_error_instruction(void *ctx);
695
696 /**
697 * Get an iterator for the set of acutal parameters
698 */
699 exec_list_iterator iterator()
700 {
701 return actual_parameters.iterator();
702 }
703
704 /**
705 * Get the name of the function being called.
706 */
707 const char *callee_name() const
708 {
709 return callee->function_name();
710 }
711
712 const ir_function_signature *get_callee()
713 {
714 return callee;
715 }
716
717 /**
718 * Set the function call target
719 */
720 void set_callee(const ir_function_signature *sig);
721
722 /**
723 * Generates an inline version of the function before @ir,
724 * returning the return value of the function.
725 */
726 ir_rvalue *generate_inline(ir_instruction *ir);
727
728 private:
729 ir_call()
730 : callee(NULL)
731 {
732 /* empty */
733 }
734
735 const ir_function_signature *callee;
736
737 /* List of ir_rvalue of paramaters passed in this call. */
738 exec_list actual_parameters;
739 };
740
741
742 /**
743 * \name Jump-like IR instructions.
744 *
745 * These include \c break, \c continue, \c return, and \c discard.
746 */
747 /*@{*/
748 class ir_jump : public ir_instruction {
749 protected:
750 ir_jump()
751 {
752 /* empty */
753 }
754 };
755
756 class ir_return : public ir_jump {
757 public:
758 ir_return()
759 : value(NULL)
760 {
761 /* empty */
762 }
763
764 ir_return(ir_rvalue *value)
765 : value(value)
766 {
767 /* empty */
768 }
769
770 virtual ir_return *clone(struct hash_table *) const;
771
772 virtual ir_return *as_return()
773 {
774 return this;
775 }
776
777 ir_rvalue *get_value() const
778 {
779 return value;
780 }
781
782 virtual void accept(ir_visitor *v)
783 {
784 v->visit(this);
785 }
786
787 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
788
789 ir_rvalue *value;
790 };
791
792
793 /**
794 * Jump instructions used inside loops
795 *
796 * These include \c break and \c continue. The \c break within a loop is
797 * different from the \c break within a switch-statement.
798 *
799 * \sa ir_switch_jump
800 */
801 class ir_loop_jump : public ir_jump {
802 public:
803 enum jump_mode {
804 jump_break,
805 jump_continue
806 };
807
808 ir_loop_jump(jump_mode mode)
809 {
810 this->mode = mode;
811 this->loop = loop;
812 }
813
814 virtual ir_loop_jump *clone(struct hash_table *) const;
815
816 virtual void accept(ir_visitor *v)
817 {
818 v->visit(this);
819 }
820
821 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
822
823 bool is_break() const
824 {
825 return mode == jump_break;
826 }
827
828 bool is_continue() const
829 {
830 return mode == jump_continue;
831 }
832
833 /** Mode selector for the jump instruction. */
834 enum jump_mode mode;
835 private:
836 /** Loop containing this break instruction. */
837 ir_loop *loop;
838 };
839
840 /**
841 * IR instruction representing discard statements.
842 */
843 class ir_discard : public ir_jump {
844 public:
845 ir_discard()
846 {
847 this->condition = NULL;
848 }
849
850 ir_discard(ir_rvalue *cond)
851 {
852 this->condition = cond;
853 }
854
855 virtual ir_discard *clone(struct hash_table *ht) const;
856
857 virtual void accept(ir_visitor *v)
858 {
859 v->visit(this);
860 }
861
862 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
863
864 ir_rvalue *condition;
865 };
866 /*@}*/
867
868
869 /**
870 * Texture sampling opcodes used in ir_texture
871 */
872 enum ir_texture_opcode {
873 ir_tex, /* Regular texture look-up */
874 ir_txb, /* Texture look-up with LOD bias */
875 ir_txl, /* Texture look-up with explicit LOD */
876 ir_txd, /* Texture look-up with partial derivatvies */
877 ir_txf /* Texel fetch with explicit LOD */
878 };
879
880
881 /**
882 * IR instruction to sample a texture
883 *
884 * The specific form of the IR instruction depends on the \c mode value
885 * selected from \c ir_texture_opcodes. In the printed IR, these will
886 * appear as:
887 *
888 * Texel offset
889 * | Projection divisor
890 * | | Shadow comparitor
891 * | | |
892 * v v v
893 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
894 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
895 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
896 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
897 * (txf (sampler) (coordinate) (0 0 0) (lod))
898 */
899 class ir_texture : public ir_rvalue {
900 public:
901 ir_texture(enum ir_texture_opcode op)
902 : op(op), projector(NULL), shadow_comparitor(NULL)
903 {
904 /* empty */
905 }
906
907 virtual ir_texture *clone(struct hash_table *) const;
908
909 virtual void accept(ir_visitor *v)
910 {
911 v->visit(this);
912 }
913
914 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
915
916 /**
917 * Return a string representing the ir_texture_opcode.
918 */
919 const char *opcode_string();
920
921 /** Set the sampler and infer the type. */
922 void set_sampler(ir_dereference *sampler);
923
924 /**
925 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
926 */
927 static ir_texture_opcode get_opcode(const char *);
928
929 enum ir_texture_opcode op;
930
931 /** Sampler to use for the texture access. */
932 ir_dereference *sampler;
933
934 /** Texture coordinate to sample */
935 ir_rvalue *coordinate;
936
937 /**
938 * Value used for projective divide.
939 *
940 * If there is no projective divide (the common case), this will be
941 * \c NULL. Optimization passes should check for this to point to a constant
942 * of 1.0 and replace that with \c NULL.
943 */
944 ir_rvalue *projector;
945
946 /**
947 * Coordinate used for comparison on shadow look-ups.
948 *
949 * If there is no shadow comparison, this will be \c NULL. For the
950 * \c ir_txf opcode, this *must* be \c NULL.
951 */
952 ir_rvalue *shadow_comparitor;
953
954 /** Explicit texel offsets. */
955 signed char offsets[3];
956
957 union {
958 ir_rvalue *lod; /**< Floating point LOD */
959 ir_rvalue *bias; /**< Floating point LOD bias */
960 struct {
961 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
962 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
963 } grad;
964 } lod_info;
965 };
966
967
968 struct ir_swizzle_mask {
969 unsigned x:2;
970 unsigned y:2;
971 unsigned z:2;
972 unsigned w:2;
973
974 /**
975 * Number of components in the swizzle.
976 */
977 unsigned num_components:3;
978
979 /**
980 * Does the swizzle contain duplicate components?
981 *
982 * L-value swizzles cannot contain duplicate components.
983 */
984 unsigned has_duplicates:1;
985 };
986
987
988 class ir_swizzle : public ir_rvalue {
989 public:
990 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
991 unsigned count);
992
993 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
994
995 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
996
997 virtual ir_swizzle *clone(struct hash_table *) const;
998
999 virtual ir_swizzle *as_swizzle()
1000 {
1001 return this;
1002 }
1003
1004 /**
1005 * Construct an ir_swizzle from the textual representation. Can fail.
1006 */
1007 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1008
1009 virtual void accept(ir_visitor *v)
1010 {
1011 v->visit(this);
1012 }
1013
1014 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1015
1016 bool is_lvalue()
1017 {
1018 return val->is_lvalue() && !mask.has_duplicates;
1019 }
1020
1021 /**
1022 * Get the variable that is ultimately referenced by an r-value
1023 */
1024 virtual ir_variable *variable_referenced();
1025
1026 ir_rvalue *val;
1027 ir_swizzle_mask mask;
1028
1029 private:
1030 /**
1031 * Initialize the mask component of a swizzle
1032 *
1033 * This is used by the \c ir_swizzle constructors.
1034 */
1035 void init_mask(const unsigned *components, unsigned count);
1036 };
1037
1038
1039 class ir_dereference : public ir_rvalue {
1040 public:
1041 virtual ir_dereference *clone(struct hash_table *) const = 0;
1042
1043 virtual ir_dereference *as_dereference()
1044 {
1045 return this;
1046 }
1047
1048 bool is_lvalue();
1049
1050 /**
1051 * Get the variable that is ultimately referenced by an r-value
1052 */
1053 virtual ir_variable *variable_referenced() = 0;
1054 };
1055
1056
1057 class ir_dereference_variable : public ir_dereference {
1058 public:
1059 ir_dereference_variable(ir_variable *var);
1060
1061 virtual ir_dereference_variable *clone(struct hash_table *) const;
1062
1063 virtual ir_dereference_variable *as_dereference_variable()
1064 {
1065 return this;
1066 }
1067
1068 /**
1069 * Get the variable that is ultimately referenced by an r-value
1070 */
1071 virtual ir_variable *variable_referenced()
1072 {
1073 return this->var;
1074 }
1075
1076 virtual ir_variable *whole_variable_referenced()
1077 {
1078 /* ir_dereference_variable objects always dereference the entire
1079 * variable. However, if this dereference is dereferenced by anything
1080 * else, the complete deferefernce chain is not a whole-variable
1081 * dereference. This method should only be called on the top most
1082 * ir_rvalue in a dereference chain.
1083 */
1084 return this->var;
1085 }
1086
1087 virtual void accept(ir_visitor *v)
1088 {
1089 v->visit(this);
1090 }
1091
1092 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1093
1094 /**
1095 * Object being dereferenced.
1096 */
1097 ir_variable *var;
1098 };
1099
1100
1101 class ir_dereference_array : public ir_dereference {
1102 public:
1103 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1104
1105 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1106
1107 virtual ir_dereference_array *clone(struct hash_table *) const;
1108
1109 virtual ir_dereference_array *as_dereference_array()
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->array->variable_referenced();
1120 }
1121
1122 virtual void accept(ir_visitor *v)
1123 {
1124 v->visit(this);
1125 }
1126
1127 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1128
1129 ir_rvalue *array;
1130 ir_rvalue *array_index;
1131
1132 private:
1133 void set_array(ir_rvalue *value);
1134 };
1135
1136
1137 class ir_dereference_record : public ir_dereference {
1138 public:
1139 ir_dereference_record(ir_rvalue *value, const char *field);
1140
1141 ir_dereference_record(ir_variable *var, const char *field);
1142
1143 virtual ir_dereference_record *clone(struct hash_table *) const;
1144
1145 /**
1146 * Get the variable that is ultimately referenced by an r-value
1147 */
1148 virtual ir_variable *variable_referenced()
1149 {
1150 return this->record->variable_referenced();
1151 }
1152
1153 virtual void accept(ir_visitor *v)
1154 {
1155 v->visit(this);
1156 }
1157
1158 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1159
1160 ir_rvalue *record;
1161 const char *field;
1162 };
1163
1164
1165 /**
1166 * Data stored in an ir_constant
1167 */
1168 union ir_constant_data {
1169 unsigned u[16];
1170 int i[16];
1171 float f[16];
1172 bool b[16];
1173 };
1174
1175
1176 class ir_constant : public ir_rvalue {
1177 public:
1178 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1179 ir_constant(bool b);
1180 ir_constant(unsigned int u);
1181 ir_constant(int i);
1182 ir_constant(float f);
1183
1184 /**
1185 * Construct an ir_constant from a list of ir_constant values
1186 */
1187 ir_constant(const struct glsl_type *type, exec_list *values);
1188
1189 /**
1190 * Construct an ir_constant from a scalar component of another ir_constant
1191 *
1192 * The new \c ir_constant inherits the type of the component from the
1193 * source constant.
1194 *
1195 * \note
1196 * In the case of a matrix constant, the new constant is a scalar, \b not
1197 * a vector.
1198 */
1199 ir_constant(const ir_constant *c, unsigned i);
1200
1201 virtual ir_constant *clone(struct hash_table *) const;
1202
1203 virtual ir_constant *as_constant()
1204 {
1205 return this;
1206 }
1207
1208 virtual void accept(ir_visitor *v)
1209 {
1210 v->visit(this);
1211 }
1212
1213 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1214
1215 /**
1216 * Get a particular component of a constant as a specific type
1217 *
1218 * This is useful, for example, to get a value from an integer constant
1219 * as a float or bool. This appears frequently when constructors are
1220 * called with all constant parameters.
1221 */
1222 /*@{*/
1223 bool get_bool_component(unsigned i) const;
1224 float get_float_component(unsigned i) const;
1225 int get_int_component(unsigned i) const;
1226 unsigned get_uint_component(unsigned i) const;
1227 /*@}*/
1228
1229 ir_constant *get_record_field(const char *name);
1230
1231 /**
1232 * Determine whether a constant has the same value as another constant
1233 */
1234 bool has_value(const ir_constant *) const;
1235
1236 /**
1237 * Value of the constant.
1238 *
1239 * The field used to back the values supplied by the constant is determined
1240 * by the type associated with the \c ir_instruction. Constants may be
1241 * scalars, vectors, or matrices.
1242 */
1243 union ir_constant_data value;
1244
1245 exec_list components;
1246
1247 private:
1248 /**
1249 * Parameterless constructor only used by the clone method
1250 */
1251 ir_constant(void);
1252 };
1253
1254 void
1255 visit_exec_list(exec_list *list, ir_visitor *visitor);
1256
1257 void validate_ir_tree(exec_list *instructions);
1258
1259 /**
1260 * Make a clone of each IR instruction in a list
1261 *
1262 * \param in List of IR instructions that are to be cloned
1263 * \param out List to hold the cloned instructions
1264 */
1265 void
1266 clone_ir_list(exec_list *out, const exec_list *in);
1267
1268 extern void
1269 _mesa_glsl_initialize_variables(exec_list *instructions,
1270 struct _mesa_glsl_parse_state *state);
1271
1272 extern void
1273 _mesa_glsl_initialize_functions(exec_list *instructions,
1274 struct _mesa_glsl_parse_state *state);
1275
1276 #endif /* IR_H */