ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glsl / ir.h
blobb04222893cf661f3667ddbdd0e8e5d3ca25604d4
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
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:
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.
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.
25 #pragma once
26 #ifndef IR_H
27 #define IR_H
29 #include <cstdio>
30 #include <cstdlib>
32 extern "C" {
33 #include <talloc.h>
36 #include "list.h"
37 #include "ir_visitor.h"
38 #include "ir_hierarchical_visitor.h"
40 #ifndef ARRAY_SIZE
41 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
42 #endif
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 */
66 /**
67 * Base class of all IR instructions
69 class ir_instruction : public exec_node {
70 public:
71 enum ir_node_type ir_type;
72 const struct glsl_type *type;
74 /** ir_print_visitor helper for debugging. */
75 void print(void) const;
77 virtual void accept(ir_visitor *) = 0;
78 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
79 virtual ir_instruction *clone(void *mem_ctx,
80 struct hash_table *ht) const = 0;
82 /**
83 * \name IR instruction downcast functions
85 * These functions either cast the object to a derived class or return
86 * \c NULL if the object's type does not match the specified derived class.
87 * Additional downcast functions will be added as needed.
89 /*@{*/
90 virtual class ir_variable * as_variable() { return NULL; }
91 virtual class ir_function * as_function() { return NULL; }
92 virtual class ir_dereference * as_dereference() { return NULL; }
93 virtual class ir_dereference_array * as_dereference_array() { return NULL; }
94 virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
95 virtual class ir_expression * as_expression() { return NULL; }
96 virtual class ir_rvalue * as_rvalue() { return NULL; }
97 virtual class ir_loop * as_loop() { return NULL; }
98 virtual class ir_assignment * as_assignment() { return NULL; }
99 virtual class ir_call * as_call() { return NULL; }
100 virtual class ir_return * as_return() { return NULL; }
101 virtual class ir_if * as_if() { return NULL; }
102 virtual class ir_swizzle * as_swizzle() { return NULL; }
103 virtual class ir_constant * as_constant() { return NULL; }
104 /*@}*/
106 protected:
107 ir_instruction()
109 ir_type = ir_type_unset;
110 type = NULL;
115 class ir_rvalue : public ir_instruction {
116 public:
117 virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const = 0;
119 virtual ir_constant *constant_expression_value() = 0;
121 virtual ir_rvalue * as_rvalue()
123 return this;
126 virtual bool is_lvalue()
128 return false;
132 * Get the variable that is ultimately referenced by an r-value
134 virtual ir_variable *variable_referenced()
136 return NULL;
141 * If an r-value is a reference to a whole variable, get that variable
143 * \return
144 * Pointer to a variable that is completely dereferenced by the r-value. If
145 * the r-value is not a dereference or the dereference does not access the
146 * entire variable (i.e., it's just one array element, struct field), \c NULL
147 * is returned.
149 virtual ir_variable *whole_variable_referenced()
151 return NULL;
154 protected:
155 ir_rvalue();
159 enum ir_variable_mode {
160 ir_var_auto = 0,
161 ir_var_uniform,
162 ir_var_in,
163 ir_var_out,
164 ir_var_inout,
165 ir_var_temporary /**< Temporary variable generated during compilation. */
168 enum ir_variable_interpolation {
169 ir_var_smooth = 0,
170 ir_var_flat,
171 ir_var_noperspective
175 class ir_variable : public ir_instruction {
176 public:
177 ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
179 virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
181 virtual ir_variable *as_variable()
183 return this;
186 virtual void accept(ir_visitor *v)
188 v->visit(this);
191 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
195 * Get the string value for the interpolation qualifier
197 * \return The string that would be used in a shader to specify \c
198 * mode will be returned.
200 * This function should only be used on a shader input or output variable.
202 const char *interpolation_string() const;
205 * Calculate the number of slots required to hold this variable
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.
210 unsigned component_slots() const;
212 const char *name;
215 * Highest element accessed with a constant expression array index
217 * Not used for non-array variables.
219 unsigned max_array_access;
221 unsigned read_only:1;
222 unsigned centroid:1;
223 unsigned invariant:1;
225 unsigned mode:3;
226 unsigned interpolation:2;
229 * Flag that the whole array is assignable
231 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
232 * equality). This flag enables this behavior.
234 unsigned array_lvalue:1;
236 /* ARB_fragment_coord_conventions */
237 unsigned origin_upper_left:1;
238 unsigned pixel_center_integer:1;
241 * Storage location of the base of this variable
243 * The precise meaning of this field depends on the nature of the variable.
245 * - Vertex shader input: one of the values from \c gl_vert_attrib.
246 * - Vertex shader output: one of the values from \c gl_vert_result.
247 * - Fragment shader input: one of the values from \c gl_frag_attrib.
248 * - Fragment shader output: one of the values from \c gl_frag_result.
249 * - Uniforms: Per-stage uniform slot number.
250 * - Other: This field is not currently used.
252 * If the variable is a uniform, shader input, or shader output, and the
253 * slot has not been assigned, the value will be -1.
255 int location;
258 * Emit a warning if this variable is accessed.
260 const char *warn_extension;
263 * Value assigned in the initializer of a variable declared "const"
265 ir_constant *constant_value;
269 /*@{*/
271 * The representation of a function instance; may be the full definition or
272 * simply a prototype.
274 class ir_function_signature : public ir_instruction {
275 /* An ir_function_signature will be part of the list of signatures in
276 * an ir_function.
278 public:
279 ir_function_signature(const glsl_type *return_type);
281 virtual ir_function_signature *clone(void *mem_ctx,
282 struct hash_table *ht) const;
284 virtual void accept(ir_visitor *v)
286 v->visit(this);
289 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
292 * Get the name of the function for which this is a signature
294 const char *function_name() const;
297 * Get a handle to the function for which this is a signature
299 * There is no setter function, this function returns a \c const pointer,
300 * and \c ir_function_signature::_function is private for a reason. The
301 * only way to make a connection between a function and function signature
302 * is via \c ir_function::add_signature. This helps ensure that certain
303 * invariants (i.e., a function signature is in the list of signatures for
304 * its \c _function) are met.
306 * \sa ir_function::add_signature
308 inline const class ir_function *function() const
310 return this->_function;
314 * Check whether the qualifiers match between this signature's parameters
315 * and the supplied parameter list. If not, returns the name of the first
316 * parameter with mismatched qualifiers (for use in error messages).
318 const char *qualifiers_match(exec_list *params);
321 * Replace the current parameter list with the given one. This is useful
322 * if the current information came from a prototype, and either has invalid
323 * or missing parameter names.
325 void replace_parameters(exec_list *new_params);
328 * Function return type.
330 * \note This discards the optional precision qualifier.
332 const struct glsl_type *return_type;
335 * List of ir_variable of function parameters.
337 * This represents the storage. The paramaters passed in a particular
338 * call will be in ir_call::actual_paramaters.
340 struct exec_list parameters;
342 /** Whether or not this function has a body (which may be empty). */
343 unsigned is_defined:1;
345 /** Whether or not this function signature is a built-in. */
346 unsigned is_built_in:1;
348 /** Body of instructions in the function. */
349 struct exec_list body;
351 private:
352 /** Function of which this signature is one overload. */
353 class ir_function *_function;
355 friend class ir_function;
360 * Header for tracking multiple overloaded functions with the same name.
361 * Contains a list of ir_function_signatures representing each of the
362 * actual functions.
364 class ir_function : public ir_instruction {
365 public:
366 ir_function(const char *name);
368 virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
370 virtual ir_function *as_function()
372 return this;
375 virtual void accept(ir_visitor *v)
377 v->visit(this);
380 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
382 void add_signature(ir_function_signature *sig)
384 sig->_function = this;
385 this->signatures.push_tail(sig);
389 * Get an iterator for the set of function signatures
391 exec_list_iterator iterator()
393 return signatures.iterator();
397 * Find a signature that matches a set of actual parameters, taking implicit
398 * conversions into account.
400 ir_function_signature *matching_signature(const exec_list *actual_param);
403 * Find a signature that exactly matches a set of actual parameters without
404 * any implicit type conversions.
406 ir_function_signature *exact_matching_signature(const exec_list *actual_ps);
409 * Name of the function.
411 const char *name;
414 * List of ir_function_signature for each overloaded function with this name.
416 struct exec_list signatures;
419 inline const char *ir_function_signature::function_name() const
421 return this->_function->name;
423 /*@}*/
427 * IR instruction representing high-level if-statements
429 class ir_if : public ir_instruction {
430 public:
431 ir_if(ir_rvalue *condition)
432 : condition(condition)
434 ir_type = ir_type_if;
437 virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
439 virtual ir_if *as_if()
441 return this;
444 virtual void accept(ir_visitor *v)
446 v->visit(this);
449 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
451 ir_rvalue *condition;
452 /** List of ir_instruction for the body of the then branch */
453 exec_list then_instructions;
454 /** List of ir_instruction for the body of the else branch */
455 exec_list else_instructions;
460 * IR instruction representing a high-level loop structure.
462 class ir_loop : public ir_instruction {
463 public:
464 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
466 ir_type = ir_type_loop;
469 virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
471 virtual void accept(ir_visitor *v)
473 v->visit(this);
476 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
478 virtual ir_loop *as_loop()
480 return this;
484 * Get an iterator for the instructions of the loop body
486 exec_list_iterator iterator()
488 return body_instructions.iterator();
491 /** List of ir_instruction that make up the body of the loop. */
492 exec_list body_instructions;
495 * \name Loop counter and controls
497 /*@{*/
498 ir_rvalue *from;
499 ir_rvalue *to;
500 ir_rvalue *increment;
501 ir_variable *counter;
502 /*@}*/
506 class ir_assignment : public ir_instruction {
507 public:
508 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
511 * Construct an assignment with an explicit write mask
513 * \note
514 * Since a write mask is supplied, the LHS must already be a bare
515 * \c ir_dereference. The cannot be any swizzles in the LHS.
517 ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
518 unsigned write_mask);
520 virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
522 virtual ir_constant *constant_expression_value();
524 virtual void accept(ir_visitor *v)
526 v->visit(this);
529 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
531 virtual ir_assignment * as_assignment()
533 return this;
537 * Get a whole variable written by an assignment
539 * If the LHS of the assignment writes a whole variable, the variable is
540 * returned. Otherwise \c NULL is returned. Examples of whole-variable
541 * assignment are:
543 * - Assigning to a scalar
544 * - Assigning to all components of a vector
545 * - Whole array (or matrix) assignment
546 * - Whole structure assignment
548 ir_variable *whole_variable_written();
551 * Set the LHS of an assignment
553 void set_lhs(ir_rvalue *lhs);
556 * Left-hand side of the assignment.
558 * This should be treated as read only. If you need to set the LHS of an
559 * assignment, use \c ir_assignment::set_lhs.
561 ir_dereference *lhs;
564 * Value being assigned
566 ir_rvalue *rhs;
569 * Optional condition for the assignment.
571 ir_rvalue *condition;
575 * Component mask written
577 * For non-vector types in the LHS, this field will be zero. For vector
578 * types, a bit will be set for each component that is written. Note that
579 * for \c vec2 and \c vec3 types only the lower bits will ever be set.
581 unsigned write_mask:4;
584 /* Update ir_expression::num_operands() and operator_strs when
585 * updating this list.
587 enum ir_expression_operation {
588 ir_unop_bit_not,
589 ir_unop_logic_not,
590 ir_unop_neg,
591 ir_unop_abs,
592 ir_unop_sign,
593 ir_unop_rcp,
594 ir_unop_rsq,
595 ir_unop_sqrt,
596 ir_unop_exp, /**< Log base e on gentype */
597 ir_unop_log, /**< Natural log on gentype */
598 ir_unop_exp2,
599 ir_unop_log2,
600 ir_unop_f2i, /**< Float-to-integer conversion. */
601 ir_unop_i2f, /**< Integer-to-float conversion. */
602 ir_unop_f2b, /**< Float-to-boolean conversion */
603 ir_unop_b2f, /**< Boolean-to-float conversion */
604 ir_unop_i2b, /**< int-to-boolean conversion */
605 ir_unop_b2i, /**< Boolean-to-int conversion */
606 ir_unop_u2f, /**< Unsigned-to-float conversion. */
609 * \name Unary floating-point rounding operations.
611 /*@{*/
612 ir_unop_trunc,
613 ir_unop_ceil,
614 ir_unop_floor,
615 ir_unop_fract,
616 /*@}*/
619 * \name Trigonometric operations.
621 /*@{*/
622 ir_unop_sin,
623 ir_unop_cos,
624 /*@}*/
627 * \name Partial derivatives.
629 /*@{*/
630 ir_unop_dFdx,
631 ir_unop_dFdy,
632 /*@}*/
634 ir_binop_add,
635 ir_binop_sub,
636 ir_binop_mul,
637 ir_binop_div,
640 * Takes one of two combinations of arguments:
642 * - mod(vecN, vecN)
643 * - mod(vecN, float)
645 * Does not take integer types.
647 ir_binop_mod,
650 * \name Binary comparison operators
652 /*@{*/
653 ir_binop_less,
654 ir_binop_greater,
655 ir_binop_lequal,
656 ir_binop_gequal,
658 * Returns single boolean for whether all components of operands[0]
659 * equal the components of operands[1].
661 ir_binop_equal,
663 * Returns single boolean for whether any component of operands[0]
664 * is not equal to the corresponding component of operands[1].
666 ir_binop_nequal,
667 /*@}*/
670 * \name Bit-wise binary operations.
672 /*@{*/
673 ir_binop_lshift,
674 ir_binop_rshift,
675 ir_binop_bit_and,
676 ir_binop_bit_xor,
677 ir_binop_bit_or,
678 /*@}*/
680 ir_binop_logic_and,
681 ir_binop_logic_xor,
682 ir_binop_logic_or,
684 ir_binop_dot,
685 ir_binop_cross,
686 ir_binop_min,
687 ir_binop_max,
689 ir_binop_pow
692 class ir_expression : public ir_rvalue {
693 public:
694 ir_expression(int op, const struct glsl_type *type,
695 ir_rvalue *, ir_rvalue *);
697 virtual ir_expression *as_expression()
699 return this;
702 virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
704 virtual ir_constant *constant_expression_value();
706 static unsigned int get_num_operands(ir_expression_operation);
707 unsigned int get_num_operands() const
709 return get_num_operands(operation);
713 * Return a string representing this expression's operator.
715 const char *operator_string();
718 * Do a reverse-lookup to translate the given string into an operator.
720 static ir_expression_operation get_operator(const char *);
722 virtual void accept(ir_visitor *v)
724 v->visit(this);
727 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
729 ir_expression_operation operation;
730 ir_rvalue *operands[2];
735 * IR instruction representing a function call
737 class ir_call : public ir_rvalue {
738 public:
739 ir_call(ir_function_signature *callee, exec_list *actual_parameters)
740 : callee(callee)
742 ir_type = ir_type_call;
743 assert(callee->return_type != NULL);
744 type = callee->return_type;
745 actual_parameters->move_nodes_to(& this->actual_parameters);
748 virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
750 virtual ir_constant *constant_expression_value();
752 virtual ir_call *as_call()
754 return this;
757 virtual void accept(ir_visitor *v)
759 v->visit(this);
762 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
765 * Get a generic ir_call object when an error occurs
767 * Any allocation will be performed with 'ctx' as talloc owner.
769 static ir_call *get_error_instruction(void *ctx);
772 * Get an iterator for the set of acutal parameters
774 exec_list_iterator iterator()
776 return actual_parameters.iterator();
780 * Get the name of the function being called.
782 const char *callee_name() const
784 return callee->function_name();
787 ir_function_signature *get_callee()
789 return callee;
793 * Set the function call target
795 void set_callee(ir_function_signature *sig);
798 * Generates an inline version of the function before @ir,
799 * returning the return value of the function.
801 ir_rvalue *generate_inline(ir_instruction *ir);
803 /* List of ir_rvalue of paramaters passed in this call. */
804 exec_list actual_parameters;
806 private:
807 ir_call()
808 : callee(NULL)
810 this->ir_type = ir_type_call;
813 ir_function_signature *callee;
818 * \name Jump-like IR instructions.
820 * These include \c break, \c continue, \c return, and \c discard.
822 /*@{*/
823 class ir_jump : public ir_instruction {
824 protected:
825 ir_jump()
827 ir_type = ir_type_unset;
831 class ir_return : public ir_jump {
832 public:
833 ir_return()
834 : value(NULL)
836 this->ir_type = ir_type_return;
839 ir_return(ir_rvalue *value)
840 : value(value)
842 this->ir_type = ir_type_return;
845 virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
847 virtual ir_return *as_return()
849 return this;
852 ir_rvalue *get_value() const
854 return value;
857 virtual void accept(ir_visitor *v)
859 v->visit(this);
862 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
864 ir_rvalue *value;
869 * Jump instructions used inside loops
871 * These include \c break and \c continue. The \c break within a loop is
872 * different from the \c break within a switch-statement.
874 * \sa ir_switch_jump
876 class ir_loop_jump : public ir_jump {
877 public:
878 enum jump_mode {
879 jump_break,
880 jump_continue
883 ir_loop_jump(jump_mode mode)
885 this->ir_type = ir_type_loop_jump;
886 this->mode = mode;
887 this->loop = loop;
890 virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
892 virtual void accept(ir_visitor *v)
894 v->visit(this);
897 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
899 bool is_break() const
901 return mode == jump_break;
904 bool is_continue() const
906 return mode == jump_continue;
909 /** Mode selector for the jump instruction. */
910 enum jump_mode mode;
911 private:
912 /** Loop containing this break instruction. */
913 ir_loop *loop;
917 * IR instruction representing discard statements.
919 class ir_discard : public ir_jump {
920 public:
921 ir_discard()
923 this->ir_type = ir_type_discard;
924 this->condition = NULL;
927 ir_discard(ir_rvalue *cond)
929 this->ir_type = ir_type_discard;
930 this->condition = cond;
933 virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
935 virtual void accept(ir_visitor *v)
937 v->visit(this);
940 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
942 ir_rvalue *condition;
944 /*@}*/
948 * Texture sampling opcodes used in ir_texture
950 enum ir_texture_opcode {
951 ir_tex, /* Regular texture look-up */
952 ir_txb, /* Texture look-up with LOD bias */
953 ir_txl, /* Texture look-up with explicit LOD */
954 ir_txd, /* Texture look-up with partial derivatvies */
955 ir_txf /* Texel fetch with explicit LOD */
960 * IR instruction to sample a texture
962 * The specific form of the IR instruction depends on the \c mode value
963 * selected from \c ir_texture_opcodes. In the printed IR, these will
964 * appear as:
966 * Texel offset
967 * | Projection divisor
968 * | | Shadow comparitor
969 * | | |
970 * v v v
971 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
972 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
973 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
974 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
975 * (txf (sampler) (coordinate) (0 0 0) (lod))
977 class ir_texture : public ir_rvalue {
978 public:
979 ir_texture(enum ir_texture_opcode op)
980 : op(op), projector(NULL), shadow_comparitor(NULL)
982 this->ir_type = ir_type_texture;
985 virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
987 virtual ir_constant *constant_expression_value();
989 virtual void accept(ir_visitor *v)
991 v->visit(this);
994 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
997 * Return a string representing the ir_texture_opcode.
999 const char *opcode_string();
1001 /** Set the sampler and infer the type. */
1002 void set_sampler(ir_dereference *sampler);
1005 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1007 static ir_texture_opcode get_opcode(const char *);
1009 enum ir_texture_opcode op;
1011 /** Sampler to use for the texture access. */
1012 ir_dereference *sampler;
1014 /** Texture coordinate to sample */
1015 ir_rvalue *coordinate;
1018 * Value used for projective divide.
1020 * If there is no projective divide (the common case), this will be
1021 * \c NULL. Optimization passes should check for this to point to a constant
1022 * of 1.0 and replace that with \c NULL.
1024 ir_rvalue *projector;
1027 * Coordinate used for comparison on shadow look-ups.
1029 * If there is no shadow comparison, this will be \c NULL. For the
1030 * \c ir_txf opcode, this *must* be \c NULL.
1032 ir_rvalue *shadow_comparitor;
1034 /** Explicit texel offsets. */
1035 signed char offsets[3];
1037 union {
1038 ir_rvalue *lod; /**< Floating point LOD */
1039 ir_rvalue *bias; /**< Floating point LOD bias */
1040 struct {
1041 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
1042 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
1043 } grad;
1044 } lod_info;
1048 struct ir_swizzle_mask {
1049 unsigned x:2;
1050 unsigned y:2;
1051 unsigned z:2;
1052 unsigned w:2;
1055 * Number of components in the swizzle.
1057 unsigned num_components:3;
1060 * Does the swizzle contain duplicate components?
1062 * L-value swizzles cannot contain duplicate components.
1064 unsigned has_duplicates:1;
1068 class ir_swizzle : public ir_rvalue {
1069 public:
1070 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1071 unsigned count);
1073 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1075 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1077 virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1079 virtual ir_constant *constant_expression_value();
1081 virtual ir_swizzle *as_swizzle()
1083 return this;
1087 * Construct an ir_swizzle from the textual representation. Can fail.
1089 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1091 virtual void accept(ir_visitor *v)
1093 v->visit(this);
1096 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1098 bool is_lvalue()
1100 return val->is_lvalue() && !mask.has_duplicates;
1104 * Get the variable that is ultimately referenced by an r-value
1106 virtual ir_variable *variable_referenced();
1108 ir_rvalue *val;
1109 ir_swizzle_mask mask;
1111 private:
1113 * Initialize the mask component of a swizzle
1115 * This is used by the \c ir_swizzle constructors.
1117 void init_mask(const unsigned *components, unsigned count);
1121 class ir_dereference : public ir_rvalue {
1122 public:
1123 virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
1125 virtual ir_dereference *as_dereference()
1127 return this;
1130 bool is_lvalue();
1133 * Get the variable that is ultimately referenced by an r-value
1135 virtual ir_variable *variable_referenced() = 0;
1139 class ir_dereference_variable : public ir_dereference {
1140 public:
1141 ir_dereference_variable(ir_variable *var);
1143 virtual ir_dereference_variable *clone(void *mem_ctx,
1144 struct hash_table *) const;
1146 virtual ir_constant *constant_expression_value();
1148 virtual ir_dereference_variable *as_dereference_variable()
1150 return this;
1154 * Get the variable that is ultimately referenced by an r-value
1156 virtual ir_variable *variable_referenced()
1158 return this->var;
1161 virtual ir_variable *whole_variable_referenced()
1163 /* ir_dereference_variable objects always dereference the entire
1164 * variable. However, if this dereference is dereferenced by anything
1165 * else, the complete deferefernce chain is not a whole-variable
1166 * dereference. This method should only be called on the top most
1167 * ir_rvalue in a dereference chain.
1169 return this->var;
1172 virtual void accept(ir_visitor *v)
1174 v->visit(this);
1177 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1180 * Object being dereferenced.
1182 ir_variable *var;
1186 class ir_dereference_array : public ir_dereference {
1187 public:
1188 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1190 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1192 virtual ir_dereference_array *clone(void *mem_ctx,
1193 struct hash_table *) const;
1195 virtual ir_constant *constant_expression_value();
1197 virtual ir_dereference_array *as_dereference_array()
1199 return this;
1203 * Get the variable that is ultimately referenced by an r-value
1205 virtual ir_variable *variable_referenced()
1207 return this->array->variable_referenced();
1210 virtual void accept(ir_visitor *v)
1212 v->visit(this);
1215 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1217 ir_rvalue *array;
1218 ir_rvalue *array_index;
1220 private:
1221 void set_array(ir_rvalue *value);
1225 class ir_dereference_record : public ir_dereference {
1226 public:
1227 ir_dereference_record(ir_rvalue *value, const char *field);
1229 ir_dereference_record(ir_variable *var, const char *field);
1231 virtual ir_dereference_record *clone(void *mem_ctx,
1232 struct hash_table *) const;
1234 virtual ir_constant *constant_expression_value();
1237 * Get the variable that is ultimately referenced by an r-value
1239 virtual ir_variable *variable_referenced()
1241 return this->record->variable_referenced();
1244 virtual void accept(ir_visitor *v)
1246 v->visit(this);
1249 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1251 ir_rvalue *record;
1252 const char *field;
1257 * Data stored in an ir_constant
1259 union ir_constant_data {
1260 unsigned u[16];
1261 int i[16];
1262 float f[16];
1263 bool b[16];
1267 class ir_constant : public ir_rvalue {
1268 public:
1269 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1270 ir_constant(bool b);
1271 ir_constant(unsigned int u);
1272 ir_constant(int i);
1273 ir_constant(float f);
1276 * Construct an ir_constant from a list of ir_constant values
1278 ir_constant(const struct glsl_type *type, exec_list *values);
1281 * Construct an ir_constant from a scalar component of another ir_constant
1283 * The new \c ir_constant inherits the type of the component from the
1284 * source constant.
1286 * \note
1287 * In the case of a matrix constant, the new constant is a scalar, \b not
1288 * a vector.
1290 ir_constant(const ir_constant *c, unsigned i);
1293 * Return a new ir_constant of the specified type containing all zeros.
1295 static ir_constant *zero(void *mem_ctx, const glsl_type *type);
1297 virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
1299 virtual ir_constant *constant_expression_value();
1301 virtual ir_constant *as_constant()
1303 return this;
1306 virtual void accept(ir_visitor *v)
1308 v->visit(this);
1311 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1314 * Get a particular component of a constant as a specific type
1316 * This is useful, for example, to get a value from an integer constant
1317 * as a float or bool. This appears frequently when constructors are
1318 * called with all constant parameters.
1320 /*@{*/
1321 bool get_bool_component(unsigned i) const;
1322 float get_float_component(unsigned i) const;
1323 int get_int_component(unsigned i) const;
1324 unsigned get_uint_component(unsigned i) const;
1325 /*@}*/
1327 ir_constant *get_array_element(unsigned i) const;
1329 ir_constant *get_record_field(const char *name);
1332 * Determine whether a constant has the same value as another constant
1334 bool has_value(const ir_constant *) const;
1337 * Value of the constant.
1339 * The field used to back the values supplied by the constant is determined
1340 * by the type associated with the \c ir_instruction. Constants may be
1341 * scalars, vectors, or matrices.
1343 union ir_constant_data value;
1345 /* Array elements */
1346 ir_constant **array_elements;
1348 /* Structure fields */
1349 exec_list components;
1351 private:
1353 * Parameterless constructor only used by the clone method
1355 ir_constant(void);
1358 void
1359 visit_exec_list(exec_list *list, ir_visitor *visitor);
1361 void validate_ir_tree(exec_list *instructions);
1364 * Make a clone of each IR instruction in a list
1366 * \param in List of IR instructions that are to be cloned
1367 * \param out List to hold the cloned instructions
1369 void
1370 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
1372 extern void
1373 _mesa_glsl_initialize_variables(exec_list *instructions,
1374 struct _mesa_glsl_parse_state *state);
1376 extern void
1377 _mesa_glsl_initialize_functions(exec_list *instructions,
1378 struct _mesa_glsl_parse_state *state);
1380 extern void
1381 _mesa_glsl_release_functions(void);
1383 extern void
1384 reparent_ir(exec_list *list, void *mem_ctx);
1386 struct glsl_symbol_table;
1388 extern void
1389 import_prototypes(const exec_list *source, exec_list *dest,
1390 struct glsl_symbol_table *symbols, void *mem_ctx);
1392 extern bool
1393 ir_has_call(ir_instruction *ir);
1395 extern void
1396 do_set_program_inouts(exec_list *instructions, struct gl_program *prog);
1398 #endif /* IR_H */