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