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
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.
32 #include "util/ralloc.h"
33 #include "glsl_types.h"
35 #include "ir_visitor.h"
36 #include "ir_hierarchical_visitor.h"
37 #include "main/mtypes.h"
42 * \defgroup IR Intermediate representation nodes
50 * Each concrete class derived from \c ir_instruction has a value in this
51 * enumerant. The value for the type is stored in \c ir_instruction::ir_type
52 * by the constructor. While using type tags is not very C++, it is extremely
53 * convenient. For example, during debugging you can simply inspect
54 * \c ir_instruction::ir_type to find out the actual type of the object.
56 * In addition, it is possible to use a switch-statement based on \c
57 * \c ir_instruction::ir_type to select different behavior for different object
58 * types. For functions that have only slight differences for several object
59 * types, this allows writing very straightforward, readable code.
62 ir_type_dereference_array
,
63 ir_type_dereference_record
,
64 ir_type_dereference_variable
,
73 ir_type_function_signature
,
80 ir_type_end_primitive
,
81 ir_type_max
, /**< maximum ir_type enum number, for validation */
82 ir_type_unset
= ir_type_max
87 * Base class of all IR instructions
89 class ir_instruction
: public exec_node
{
91 enum ir_node_type ir_type
;
94 * GCC 4.7+ and clang warn when deleting an ir_instruction unless
95 * there's a virtual destructor present. Because we almost
96 * universally use ralloc for our memory management of
97 * ir_instructions, the destructor doesn't need to do any work.
99 virtual ~ir_instruction()
103 /** ir_print_visitor helper for debugging. */
104 void print(void) const;
105 void fprint(FILE *f
) const;
107 virtual void accept(ir_visitor
*) = 0;
108 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
109 virtual ir_instruction
*clone(void *mem_ctx
,
110 struct hash_table
*ht
) const = 0;
113 * \name IR instruction downcast functions
115 * These functions either cast the object to a derived class or return
116 * \c NULL if the object's type does not match the specified derived class.
117 * Additional downcast functions will be added as needed.
120 class ir_rvalue
*as_rvalue()
122 assume(this != NULL
);
123 if (ir_type
== ir_type_dereference_array
||
124 ir_type
== ir_type_dereference_record
||
125 ir_type
== ir_type_dereference_variable
||
126 ir_type
== ir_type_constant
||
127 ir_type
== ir_type_expression
||
128 ir_type
== ir_type_swizzle
||
129 ir_type
== ir_type_texture
)
130 return (class ir_rvalue
*) this;
134 class ir_dereference
*as_dereference()
136 assume(this != NULL
);
137 if (ir_type
== ir_type_dereference_array
||
138 ir_type
== ir_type_dereference_record
||
139 ir_type
== ir_type_dereference_variable
)
140 return (class ir_dereference
*) this;
144 class ir_jump
*as_jump()
146 assume(this != NULL
);
147 if (ir_type
== ir_type_loop_jump
||
148 ir_type
== ir_type_return
||
149 ir_type
== ir_type_discard
)
150 return (class ir_jump
*) this;
154 #define AS_CHILD(TYPE) \
155 class ir_##TYPE * as_##TYPE() \
157 assume(this != NULL); \
158 return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \
162 AS_CHILD(dereference_array
)
163 AS_CHILD(dereference_variable
)
164 AS_CHILD(dereference_record
)
179 * IR equality method: Return true if the referenced instruction would
180 * return the same value as this one.
182 * This intended to be used for CSE and algebraic optimizations, on rvalues
183 * in particular. No support for other instruction types (assignments,
184 * jumps, calls, etc.) is planned.
186 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
189 ir_instruction(enum ir_node_type t
)
197 assert(!"Should not get here.");
203 * The base class for all "values"/expression trees.
205 class ir_rvalue
: public ir_instruction
{
207 const struct glsl_type
*type
;
209 virtual ir_rvalue
*clone(void *mem_ctx
, struct hash_table
*) const;
211 virtual void accept(ir_visitor
*v
)
216 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
218 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
220 ir_rvalue
*as_rvalue_to_saturate();
222 virtual bool is_lvalue() const
228 * Get the variable that is ultimately referenced by an r-value
230 virtual ir_variable
*variable_referenced() const
237 * If an r-value is a reference to a whole variable, get that variable
240 * Pointer to a variable that is completely dereferenced by the r-value. If
241 * the r-value is not a dereference or the dereference does not access the
242 * entire variable (i.e., it's just one array element, struct field), \c NULL
245 virtual ir_variable
*whole_variable_referenced()
251 * Determine if an r-value has the value zero
253 * The base implementation of this function always returns \c false. The
254 * \c ir_constant class over-rides this function to return \c true \b only
255 * for vector and scalar types that have all elements set to the value
256 * zero (or \c false for booleans).
258 * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one
260 virtual bool is_zero() const;
263 * Determine if an r-value has the value one
265 * The base implementation of this function always returns \c false. The
266 * \c ir_constant class over-rides this function to return \c true \b only
267 * for vector and scalar types that have all elements set to the value
268 * one (or \c true for booleans).
270 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one
272 virtual bool is_one() const;
275 * Determine if an r-value has the value negative one
277 * The base implementation of this function always returns \c false. The
278 * \c ir_constant class over-rides this function to return \c true \b only
279 * for vector and scalar types that have all elements set to the value
280 * negative one. For boolean types, the result is always \c false.
282 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
284 virtual bool is_negative_one() const;
287 * Determine if an r-value is an unsigned integer constant which can be
290 * \sa ir_constant::is_uint16_constant.
292 virtual bool is_uint16_constant() const { return false; }
295 * Return a generic value of error_type.
297 * Allocation will be performed with 'mem_ctx' as ralloc owner.
299 static ir_rvalue
*error_value(void *mem_ctx
);
302 ir_rvalue(enum ir_node_type t
);
307 * Variable storage classes
309 enum ir_variable_mode
{
310 ir_var_auto
= 0, /**< Function local variables and globals. */
311 ir_var_uniform
, /**< Variable declared as a uniform. */
316 ir_var_function_inout
,
317 ir_var_const_in
, /**< "in" param that must be a constant expression */
318 ir_var_system_value
, /**< Ex: front-face, instance-id, etc. */
319 ir_var_temporary
, /**< Temporary variable generated during compilation. */
320 ir_var_mode_count
/**< Number of variable modes */
324 * Enum keeping track of how a variable was declared. For error checking of
325 * the gl_PerVertex redeclaration rules.
327 enum ir_var_declaration_type
{
329 * Normal declaration (for most variables, this means an explicit
330 * declaration. Exception: temporaries are always implicitly declared, but
331 * they still use ir_var_declared_normally).
333 * Note: an ir_variable that represents a named interface block uses
334 * ir_var_declared_normally.
336 ir_var_declared_normally
= 0,
339 * Variable was explicitly declared (or re-declared) in an unnamed
342 ir_var_declared_in_block
,
345 * Variable is an implicitly declared built-in that has not been explicitly
346 * re-declared by the shader.
348 ir_var_declared_implicitly
,
351 * Variable is implicitly generated by the compiler and should not be
352 * visible via the API.
358 * \brief Layout qualifiers for gl_FragDepth.
360 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
361 * with a layout qualifier.
363 enum ir_depth_layout
{
364 ir_depth_layout_none
, /**< No depth layout is specified. */
366 ir_depth_layout_greater
,
367 ir_depth_layout_less
,
368 ir_depth_layout_unchanged
372 * \brief Convert depth layout qualifier to string.
375 depth_layout_string(ir_depth_layout layout
);
378 * Description of built-in state associated with a uniform
380 * \sa ir_variable::state_slots
382 struct ir_state_slot
{
389 * Get the string value for an interpolation qualifier
391 * \return The string that would be used in a shader to specify \c
392 * mode will be returned.
394 * This function is used to generate error messages of the form "shader
395 * uses %s interpolation qualifier", so in the case where there is no
396 * interpolation qualifier, it returns "no".
398 * This function should only be used on a shader input or output variable.
400 const char *interpolation_string(unsigned interpolation
);
403 class ir_variable
: public ir_instruction
{
405 ir_variable(const struct glsl_type
*, const char *, ir_variable_mode
);
407 virtual ir_variable
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
409 virtual void accept(ir_visitor
*v
)
414 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
418 * Determine how this variable should be interpolated based on its
419 * interpolation qualifier (if present), whether it is gl_Color or
420 * gl_SecondaryColor, and whether flatshading is enabled in the current GL
423 * The return value will always be either INTERP_QUALIFIER_SMOOTH,
424 * INTERP_QUALIFIER_NOPERSPECTIVE, or INTERP_QUALIFIER_FLAT.
426 glsl_interp_qualifier
determine_interpolation_mode(bool flat_shade
);
429 * Determine whether or not a variable is part of a uniform block.
431 inline bool is_in_uniform_block() const
433 return this->data
.mode
== ir_var_uniform
&& this->interface_type
!= NULL
;
437 * Determine whether or not a variable is the declaration of an interface
440 * For the first declaration below, there will be an \c ir_variable named
441 * "instance" whose type and whose instance_type will be the same
442 * \cglsl_type. For the second declaration, there will be an \c ir_variable
443 * named "f" whose type is float and whose instance_type is B2.
445 * "instance" is an interface instance variable, but "f" is not.
455 inline bool is_interface_instance() const
457 return this->type
->without_array() == this->interface_type
;
461 * Set this->interface_type on a newly created variable.
463 void init_interface_type(const struct glsl_type
*type
)
465 assert(this->interface_type
== NULL
);
466 this->interface_type
= type
;
467 if (this->is_interface_instance()) {
468 this->u
.max_ifc_array_access
=
469 rzalloc_array(this, unsigned, type
->length
);
474 * Change this->interface_type on a variable that previously had a
475 * different, but compatible, interface_type. This is used during linking
476 * to set the size of arrays in interface blocks.
478 void change_interface_type(const struct glsl_type
*type
)
480 if (this->u
.max_ifc_array_access
!= NULL
) {
481 /* max_ifc_array_access has already been allocated, so make sure the
482 * new interface has the same number of fields as the old one.
484 assert(this->interface_type
->length
== type
->length
);
486 this->interface_type
= type
;
490 * Change this->interface_type on a variable that previously had a
491 * different, and incompatible, interface_type. This is used during
492 * compilation to handle redeclaration of the built-in gl_PerVertex
495 void reinit_interface_type(const struct glsl_type
*type
)
497 if (this->u
.max_ifc_array_access
!= NULL
) {
499 /* Redeclaring gl_PerVertex is only allowed if none of the built-ins
500 * it defines have been accessed yet; so it's safe to throw away the
501 * old max_ifc_array_access pointer, since all of its values are
504 for (unsigned i
= 0; i
< this->interface_type
->length
; i
++)
505 assert(this->u
.max_ifc_array_access
[i
] == 0);
507 ralloc_free(this->u
.max_ifc_array_access
);
508 this->u
.max_ifc_array_access
= NULL
;
510 this->interface_type
= NULL
;
511 init_interface_type(type
);
514 const glsl_type
*get_interface_type() const
516 return this->interface_type
;
520 * Get the max_ifc_array_access pointer
522 * A "set" function is not needed because the array is dynmically allocated
525 inline unsigned *get_max_ifc_array_access()
527 assert(this->data
._num_state_slots
== 0);
528 return this->u
.max_ifc_array_access
;
531 inline unsigned get_num_state_slots() const
533 assert(!this->is_interface_instance()
534 || this->data
._num_state_slots
== 0);
535 return this->data
._num_state_slots
;
538 inline void set_num_state_slots(unsigned n
)
540 assert(!this->is_interface_instance()
542 this->data
._num_state_slots
= n
;
545 inline ir_state_slot
*get_state_slots()
547 return this->is_interface_instance() ? NULL
: this->u
.state_slots
;
550 inline const ir_state_slot
*get_state_slots() const
552 return this->is_interface_instance() ? NULL
: this->u
.state_slots
;
555 inline ir_state_slot
*allocate_state_slots(unsigned n
)
557 assert(!this->is_interface_instance());
559 this->u
.state_slots
= ralloc_array(this, ir_state_slot
, n
);
560 this->data
._num_state_slots
= 0;
562 if (this->u
.state_slots
!= NULL
)
563 this->data
._num_state_slots
= n
;
565 return this->u
.state_slots
;
568 inline bool is_name_ralloced() const
570 return this->name
!= ir_variable::tmp_name
;
574 * Enable emitting extension warnings for this variable
576 void enable_extension_warning(const char *extension
);
579 * Get the extension warning string for this variable
581 * If warnings are not enabled, \c NULL is returned.
583 const char *get_extension_warning() const;
586 * Declared type of the variable
588 const struct glsl_type
*type
;
591 * Declared name of the variable
595 struct ir_variable_data
{
598 * Is the variable read-only?
600 * This is set for variables declared as \c const, shader inputs,
603 unsigned read_only
:1;
606 unsigned invariant
:1;
610 * Has this variable been used for reading or writing?
612 * Several GLSL semantic checks require knowledge of whether or not a
613 * variable has been used. For example, it is an error to redeclare a
614 * variable as invariant after it has been used.
616 * This is only maintained in the ast_to_hir.cpp path, not in
617 * Mesa's fixed function or ARB program paths.
622 * Has this variable been statically assigned?
624 * This answers whether the variable was assigned in any path of
625 * the shader during ast_to_hir. This doesn't answer whether it is
626 * still written after dead code removal, nor is it maintained in
627 * non-ast_to_hir.cpp (GLSL parsing) paths.
632 * Enum indicating how the variable was declared. See
633 * ir_var_declaration_type.
635 * This is used to detect certain kinds of illegal variable redeclarations.
637 unsigned how_declared
:2;
640 * Storage class of the variable.
642 * \sa ir_variable_mode
647 * Interpolation mode for shader inputs / outputs
649 * \sa ir_variable_interpolation
651 unsigned interpolation
:2;
654 * \name ARB_fragment_coord_conventions
657 unsigned origin_upper_left
:1;
658 unsigned pixel_center_integer
:1;
662 * Was the location explicitly set in the shader?
664 * If the location is explicitly set in the shader, it \b cannot be changed
665 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
668 unsigned explicit_location
:1;
669 unsigned explicit_index
:1;
672 * Was an initial binding explicitly set in the shader?
674 * If so, constant_value contains an integer ir_constant representing the
675 * initial binding point.
677 unsigned explicit_binding
:1;
680 * Does this variable have an initializer?
682 * This is used by the linker to cross-validiate initializers of global
685 unsigned has_initializer
:1;
688 * Is this variable a generic output or input that has not yet been matched
689 * up to a variable in another stage of the pipeline?
691 * This is used by the linker as scratch storage while assigning locations
692 * to generic inputs and outputs.
694 unsigned is_unmatched_generic_inout
:1;
697 * If non-zero, then this variable may be packed along with other variables
698 * into a single varying slot, so this offset should be applied when
699 * accessing components. For example, an offset of 1 means that the x
700 * component of this variable is actually stored in component y of the
701 * location specified by \c location.
703 unsigned location_frac
:2;
706 * Layout of the matrix. Uses glsl_matrix_layout values.
708 unsigned matrix_layout
:2;
711 * Non-zero if this variable was created by lowering a named interface
712 * block which was not an array.
714 * Note that this variable and \c from_named_ifc_block_array will never
717 unsigned from_named_ifc_block_nonarray
:1;
720 * Non-zero if this variable was created by lowering a named interface
721 * block which was an array.
723 * Note that this variable and \c from_named_ifc_block_nonarray will never
726 unsigned from_named_ifc_block_array
:1;
729 * Non-zero if the variable must be a shader input. This is useful for
730 * constraints on function parameters.
732 unsigned must_be_shader_input
:1;
735 * Output index for dual source blending.
738 * The GLSL spec only allows the values 0 or 1 for the index in \b dual
744 * \brief Layout qualifier for gl_FragDepth.
746 * This is not equal to \c ir_depth_layout_none if and only if this
747 * variable is \c gl_FragDepth and a layout qualifier is specified.
749 ir_depth_layout depth_layout
:3;
752 * ARB_shader_image_load_store qualifiers.
754 unsigned image_read_only
:1; /**< "readonly" qualifier. */
755 unsigned image_write_only
:1; /**< "writeonly" qualifier. */
756 unsigned image_coherent
:1;
757 unsigned image_volatile
:1;
758 unsigned image_restrict
:1;
761 * Emit a warning if this variable is accessed.
764 uint8_t warn_extension_index
;
767 /** Image internal format if specified explicitly, otherwise GL_NONE. */
768 uint16_t image_format
;
772 * Number of state slots used
775 * This could be stored in as few as 7-bits, if necessary. If it is made
776 * smaller, add an assertion to \c ir_variable::allocate_state_slots to
779 uint16_t _num_state_slots
;
783 * Initial binding point for a sampler, atomic, or UBO.
785 * For array types, this represents the binding point for the first element.
790 * Storage location of the base of this variable
792 * The precise meaning of this field depends on the nature of the variable.
794 * - Vertex shader input: one of the values from \c gl_vert_attrib.
795 * - Vertex shader output: one of the values from \c gl_varying_slot.
796 * - Geometry shader input: one of the values from \c gl_varying_slot.
797 * - Geometry shader output: one of the values from \c gl_varying_slot.
798 * - Fragment shader input: one of the values from \c gl_varying_slot.
799 * - Fragment shader output: one of the values from \c gl_frag_result.
800 * - Uniforms: Per-stage uniform slot number for default uniform block.
801 * - Uniforms: Index within the uniform block definition for UBO members.
802 * - Other: This field is not currently used.
804 * If the variable is a uniform, shader input, or shader output, and the
805 * slot has not been assigned, the value will be -1.
810 * Vertex stream output identifier.
815 * Location an atomic counter is stored at.
822 * Highest element accessed with a constant expression array index
824 * Not used for non-array variables.
826 unsigned max_array_access
;
829 * Allow (only) ir_variable direct access private members.
831 friend class ir_variable
;
835 * Value assigned in the initializer of a variable declared "const"
837 ir_constant
*constant_value
;
840 * Constant expression assigned in the initializer of the variable
843 * This field and \c ::constant_value are distinct. Even if the two fields
844 * refer to constants with the same value, they must point to separate
847 ir_constant
*constant_initializer
;
850 static const char *const warn_extension_table
[];
854 * For variables which satisfy the is_interface_instance() predicate,
855 * this points to an array of integers such that if the ith member of
856 * the interface block is an array, max_ifc_array_access[i] is the
857 * maximum array element of that member that has been accessed. If the
858 * ith member of the interface block is not an array,
859 * max_ifc_array_access[i] is unused.
861 * For variables whose type is not an interface block, this pointer is
864 unsigned *max_ifc_array_access
;
867 * Built-in state that backs this uniform
869 * Once set at variable creation, \c state_slots must remain invariant.
871 * If the variable is not a uniform, \c _num_state_slots will be zero
872 * and \c state_slots will be \c NULL.
874 ir_state_slot
*state_slots
;
878 * For variables that are in an interface block or are an instance of an
879 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
881 * \sa ir_variable::location
883 const glsl_type
*interface_type
;
886 * Name used for anonymous compiler temporaries
888 static const char tmp_name
[];
892 * Should the construct keep names for ir_var_temporary variables?
894 * When this global is false, names passed to the constructor for
895 * \c ir_var_temporary variables will be dropped. Instead, the variable will
896 * be named "compiler_temp". This name will be in static storage.
899 * \b NEVER change the mode of an \c ir_var_temporary.
902 * This variable is \b not thread-safe. It is global, \b not
903 * per-context. It begins life false. A context can, at some point, make
904 * it true. From that point on, it will be true forever. This should be
905 * okay since it will only be set true while debugging.
907 static bool temporaries_allocate_names
;
911 * A function that returns whether a built-in function is available in the
912 * current shading language (based on version, ES or desktop, and extensions).
914 typedef bool (*builtin_available_predicate
)(const _mesa_glsl_parse_state
*);
918 * The representation of a function instance; may be the full definition or
919 * simply a prototype.
921 class ir_function_signature
: public ir_instruction
{
922 /* An ir_function_signature will be part of the list of signatures in
926 ir_function_signature(const glsl_type
*return_type
,
927 builtin_available_predicate builtin_avail
= NULL
);
929 virtual ir_function_signature
*clone(void *mem_ctx
,
930 struct hash_table
*ht
) const;
931 ir_function_signature
*clone_prototype(void *mem_ctx
,
932 struct hash_table
*ht
) const;
934 virtual void accept(ir_visitor
*v
)
939 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
942 * Attempt to evaluate this function as a constant expression,
943 * given a list of the actual parameters and the variable context.
944 * Returns NULL for non-built-ins.
946 ir_constant
*constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
);
949 * Get the name of the function for which this is a signature
951 const char *function_name() const;
954 * Get a handle to the function for which this is a signature
956 * There is no setter function, this function returns a \c const pointer,
957 * and \c ir_function_signature::_function is private for a reason. The
958 * only way to make a connection between a function and function signature
959 * is via \c ir_function::add_signature. This helps ensure that certain
960 * invariants (i.e., a function signature is in the list of signatures for
961 * its \c _function) are met.
963 * \sa ir_function::add_signature
965 inline const class ir_function
*function() const
967 return this->_function
;
971 * Check whether the qualifiers match between this signature's parameters
972 * and the supplied parameter list. If not, returns the name of the first
973 * parameter with mismatched qualifiers (for use in error messages).
975 const char *qualifiers_match(exec_list
*params
);
978 * Replace the current parameter list with the given one. This is useful
979 * if the current information came from a prototype, and either has invalid
980 * or missing parameter names.
982 void replace_parameters(exec_list
*new_params
);
985 * Function return type.
987 * \note This discards the optional precision qualifier.
989 const struct glsl_type
*return_type
;
992 * List of ir_variable of function parameters.
994 * This represents the storage. The paramaters passed in a particular
995 * call will be in ir_call::actual_paramaters.
997 struct exec_list parameters
;
999 /** Whether or not this function has a body (which may be empty). */
1000 unsigned is_defined
:1;
1002 /** Whether or not this function signature is a built-in. */
1003 bool is_builtin() const;
1006 * Whether or not this function is an intrinsic to be implemented
1011 /** Whether or not a built-in is available for this shader. */
1012 bool is_builtin_available(const _mesa_glsl_parse_state
*state
) const;
1014 /** Body of instructions in the function. */
1015 struct exec_list body
;
1019 * A function pointer to a predicate that answers whether a built-in
1020 * function is available in the current shader. NULL if not a built-in.
1022 builtin_available_predicate builtin_avail
;
1024 /** Function of which this signature is one overload. */
1025 class ir_function
*_function
;
1027 /** Function signature of which this one is a prototype clone */
1028 const ir_function_signature
*origin
;
1030 friend class ir_function
;
1033 * Helper function to run a list of instructions for constant
1034 * expression evaluation.
1036 * The hash table represents the values of the visible variables.
1037 * There are no scoping issues because the table is indexed on
1038 * ir_variable pointers, not variable names.
1040 * Returns false if the expression is not constant, true otherwise,
1041 * and the value in *result if result is non-NULL.
1043 bool constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1044 struct hash_table
*variable_context
,
1045 ir_constant
**result
);
1050 * Header for tracking multiple overloaded functions with the same name.
1051 * Contains a list of ir_function_signatures representing each of the
1054 class ir_function
: public ir_instruction
{
1056 ir_function(const char *name
);
1058 virtual ir_function
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1060 virtual void accept(ir_visitor
*v
)
1065 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1067 void add_signature(ir_function_signature
*sig
)
1069 sig
->_function
= this;
1070 this->signatures
.push_tail(sig
);
1074 * Find a signature that matches a set of actual parameters, taking implicit
1075 * conversions into account. Also flags whether the match was exact.
1077 ir_function_signature
*matching_signature(_mesa_glsl_parse_state
*state
,
1078 const exec_list
*actual_param
,
1079 bool allow_builtins
,
1080 bool *match_is_exact
);
1083 * Find a signature that matches a set of actual parameters, taking implicit
1084 * conversions into account.
1086 ir_function_signature
*matching_signature(_mesa_glsl_parse_state
*state
,
1087 const exec_list
*actual_param
,
1088 bool allow_builtins
);
1091 * Find a signature that exactly matches a set of actual parameters without
1092 * any implicit type conversions.
1094 ir_function_signature
*exact_matching_signature(_mesa_glsl_parse_state
*state
,
1095 const exec_list
*actual_ps
);
1098 * Name of the function.
1102 /** Whether or not this function has a signature that isn't a built-in. */
1103 bool has_user_signature();
1106 * List of ir_function_signature for each overloaded function with this name.
1108 struct exec_list signatures
;
1111 inline const char *ir_function_signature::function_name() const
1113 return this->_function
->name
;
1119 * IR instruction representing high-level if-statements
1121 class ir_if
: public ir_instruction
{
1123 ir_if(ir_rvalue
*condition
)
1124 : ir_instruction(ir_type_if
), condition(condition
)
1128 virtual ir_if
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1130 virtual void accept(ir_visitor
*v
)
1135 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1137 ir_rvalue
*condition
;
1138 /** List of ir_instruction for the body of the then branch */
1139 exec_list then_instructions
;
1140 /** List of ir_instruction for the body of the else branch */
1141 exec_list else_instructions
;
1146 * IR instruction representing a high-level loop structure.
1148 class ir_loop
: public ir_instruction
{
1152 virtual ir_loop
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1154 virtual void accept(ir_visitor
*v
)
1159 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1161 /** List of ir_instruction that make up the body of the loop. */
1162 exec_list body_instructions
;
1166 class ir_assignment
: public ir_instruction
{
1168 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
= NULL
);
1171 * Construct an assignment with an explicit write mask
1174 * Since a write mask is supplied, the LHS must already be a bare
1175 * \c ir_dereference. The cannot be any swizzles in the LHS.
1177 ir_assignment(ir_dereference
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
,
1178 unsigned write_mask
);
1180 virtual ir_assignment
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1182 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
1184 virtual void accept(ir_visitor
*v
)
1189 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1192 * Get a whole variable written by an assignment
1194 * If the LHS of the assignment writes a whole variable, the variable is
1195 * returned. Otherwise \c NULL is returned. Examples of whole-variable
1198 * - Assigning to a scalar
1199 * - Assigning to all components of a vector
1200 * - Whole array (or matrix) assignment
1201 * - Whole structure assignment
1203 ir_variable
*whole_variable_written();
1206 * Set the LHS of an assignment
1208 void set_lhs(ir_rvalue
*lhs
);
1211 * Left-hand side of the assignment.
1213 * This should be treated as read only. If you need to set the LHS of an
1214 * assignment, use \c ir_assignment::set_lhs.
1216 ir_dereference
*lhs
;
1219 * Value being assigned
1224 * Optional condition for the assignment.
1226 ir_rvalue
*condition
;
1230 * Component mask written
1232 * For non-vector types in the LHS, this field will be zero. For vector
1233 * types, a bit will be set for each component that is written. Note that
1234 * for \c vec2 and \c vec3 types only the lower bits will ever be set.
1236 * A partially-set write mask means that each enabled channel gets
1237 * the value from a consecutive channel of the rhs. For example,
1238 * to write just .xyw of gl_FrontColor with color:
1240 * (assign (constant bool (1)) (xyw)
1241 * (var_ref gl_FragColor)
1242 * (swiz xyw (var_ref color)))
1244 unsigned write_mask
:4;
1247 /* Update ir_expression::get_num_operands() and operator_strs when
1248 * updating this list.
1250 enum ir_expression_operation
{
1259 ir_unop_exp
, /**< Log base e on gentype */
1260 ir_unop_log
, /**< Natural log on gentype */
1263 ir_unop_f2i
, /**< Float-to-integer conversion. */
1264 ir_unop_f2u
, /**< Float-to-unsigned conversion. */
1265 ir_unop_i2f
, /**< Integer-to-float conversion. */
1266 ir_unop_f2b
, /**< Float-to-boolean conversion */
1267 ir_unop_b2f
, /**< Boolean-to-float conversion */
1268 ir_unop_i2b
, /**< int-to-boolean conversion */
1269 ir_unop_b2i
, /**< Boolean-to-int conversion */
1270 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
1271 ir_unop_i2u
, /**< Integer-to-unsigned conversion. */
1272 ir_unop_u2i
, /**< Unsigned-to-integer conversion. */
1273 ir_unop_d2f
, /**< Double-to-float conversion. */
1274 ir_unop_f2d
, /**< Float-to-double conversion. */
1275 ir_unop_d2i
, /**< Double-to-integer conversion. */
1276 ir_unop_i2d
, /**< Integer-to-double conversion. */
1277 ir_unop_d2u
, /**< Double-to-unsigned conversion. */
1278 ir_unop_u2d
, /**< Unsigned-to-double conversion. */
1279 ir_unop_d2b
, /**< Double-to-boolean conversion. */
1280 ir_unop_bitcast_i2f
, /**< Bit-identical int-to-float "conversion" */
1281 ir_unop_bitcast_f2i
, /**< Bit-identical float-to-int "conversion" */
1282 ir_unop_bitcast_u2f
, /**< Bit-identical uint-to-float "conversion" */
1283 ir_unop_bitcast_f2u
, /**< Bit-identical float-to-uint "conversion" */
1287 * \name Unary floating-point rounding operations.
1298 * \name Trigonometric operations.
1303 ir_unop_sin_reduced
, /**< Reduced range sin. [-pi, pi] */
1304 ir_unop_cos_reduced
, /**< Reduced range cos. [-pi, pi] */
1308 * \name Partial derivatives.
1312 ir_unop_dFdx_coarse
,
1315 ir_unop_dFdy_coarse
,
1320 * \name Floating point pack and unpack operations.
1323 ir_unop_pack_snorm_2x16
,
1324 ir_unop_pack_snorm_4x8
,
1325 ir_unop_pack_unorm_2x16
,
1326 ir_unop_pack_unorm_4x8
,
1327 ir_unop_pack_half_2x16
,
1328 ir_unop_unpack_snorm_2x16
,
1329 ir_unop_unpack_snorm_4x8
,
1330 ir_unop_unpack_unorm_2x16
,
1331 ir_unop_unpack_unorm_4x8
,
1332 ir_unop_unpack_half_2x16
,
1336 * \name Lowered floating point unpacking operations.
1338 * \see lower_packing_builtins_visitor::split_unpack_half_2x16
1341 ir_unop_unpack_half_2x16_split_x
,
1342 ir_unop_unpack_half_2x16_split_y
,
1346 * \name Bit operations, part of ARB_gpu_shader5.
1349 ir_unop_bitfield_reverse
,
1358 * \name Double packing, part of ARB_gpu_shader_fp64.
1361 ir_unop_pack_double_2x32
,
1362 ir_unop_unpack_double_2x32
,
1371 * Interpolate fs input at centroid
1373 * operand0 is the fs input.
1375 ir_unop_interpolate_at_centroid
,
1378 * A sentinel marking the last of the unary operations.
1380 ir_last_unop
= ir_unop_interpolate_at_centroid
,
1384 ir_binop_mul
, /**< Floating-point or low 32-bit integer multiply. */
1385 ir_binop_imul_high
, /**< Calculates the high 32-bits of a 64-bit multiply. */
1389 * Returns the carry resulting from the addition of the two arguments.
1396 * Returns the borrow resulting from the subtraction of the second argument
1397 * from the first argument.
1404 * Takes one of two combinations of arguments:
1407 * - mod(vecN, float)
1409 * Does not take integer types.
1414 * \name Binary comparison operators which return a boolean vector.
1415 * The type of both operands must be equal.
1425 * Returns single boolean for whether all components of operands[0]
1426 * equal the components of operands[1].
1430 * Returns single boolean for whether any component of operands[0]
1431 * is not equal to the corresponding component of operands[1].
1433 ir_binop_any_nequal
,
1437 * \name Bit-wise binary operations.
1458 * \name Lowered floating point packing operations.
1460 * \see lower_packing_builtins_visitor::split_pack_half_2x16
1463 ir_binop_pack_half_2x16_split
,
1467 * \name First half of a lowered bitfieldInsert() operation.
1469 * \see lower_instructions::bitfield_insert_to_bfm_bfi
1476 * Load a value the size of a given GLSL type from a uniform block.
1478 * operand0 is the ir_constant uniform block index in the linked shader.
1479 * operand1 is a byte offset within the uniform block.
1484 * \name Multiplies a number by two to a power, part of ARB_gpu_shader5.
1491 * Extract a scalar from a vector
1493 * operand0 is the vector
1494 * operand1 is the index of the field to read from operand0
1496 ir_binop_vector_extract
,
1499 * Interpolate fs input at offset
1501 * operand0 is the fs input
1502 * operand1 is the offset from the pixel center
1504 ir_binop_interpolate_at_offset
,
1507 * Interpolate fs input at sample position
1509 * operand0 is the fs input
1510 * operand1 is the sample ID
1512 ir_binop_interpolate_at_sample
,
1515 * A sentinel marking the last of the binary operations.
1517 ir_last_binop
= ir_binop_interpolate_at_sample
,
1520 * \name Fused floating-point multiply-add, part of ARB_gpu_shader5.
1529 * \name Conditional Select
1531 * A vector conditional select instruction (like ?:, but operating per-
1532 * component on vectors).
1534 * \see lower_instructions_visitor::ldexp_to_arith
1541 * \name Second half of a lowered bitfieldInsert() operation.
1543 * \see lower_instructions::bitfield_insert_to_bfm_bfi
1549 ir_triop_bitfield_extract
,
1552 * Generate a value with one field of a vector changed
1554 * operand0 is the vector
1555 * operand1 is the value to write into the vector result
1556 * operand2 is the index in operand0 to be modified
1558 ir_triop_vector_insert
,
1561 * A sentinel marking the last of the ternary operations.
1563 ir_last_triop
= ir_triop_vector_insert
,
1565 ir_quadop_bitfield_insert
,
1570 * A sentinel marking the last of the ternary operations.
1572 ir_last_quadop
= ir_quadop_vector
,
1575 * A sentinel marking the last of all operations.
1577 ir_last_opcode
= ir_quadop_vector
1580 class ir_expression
: public ir_rvalue
{
1582 ir_expression(int op
, const struct glsl_type
*type
,
1583 ir_rvalue
*op0
, ir_rvalue
*op1
= NULL
,
1584 ir_rvalue
*op2
= NULL
, ir_rvalue
*op3
= NULL
);
1587 * Constructor for unary operation expressions
1589 ir_expression(int op
, ir_rvalue
*);
1592 * Constructor for binary operation expressions
1594 ir_expression(int op
, ir_rvalue
*op0
, ir_rvalue
*op1
);
1597 * Constructor for ternary operation expressions
1599 ir_expression(int op
, ir_rvalue
*op0
, ir_rvalue
*op1
, ir_rvalue
*op2
);
1601 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
1603 virtual ir_expression
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1606 * Attempt to constant-fold the expression
1608 * The "variable_context" hash table links ir_variable * to ir_constant *
1609 * that represent the variables' values. \c NULL represents an empty
1612 * If the expression cannot be constant folded, this method will return
1615 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
1618 * Determine the number of operands used by an expression
1620 static unsigned int get_num_operands(ir_expression_operation
);
1623 * Determine the number of operands used by an expression
1625 unsigned int get_num_operands() const
1627 return (this->operation
== ir_quadop_vector
)
1628 ? this->type
->vector_elements
: get_num_operands(operation
);
1632 * Return whether the expression operates on vectors horizontally.
1634 bool is_horizontal() const
1636 return operation
== ir_binop_all_equal
||
1637 operation
== ir_binop_any_nequal
||
1638 operation
== ir_unop_any
||
1639 operation
== ir_binop_dot
||
1640 operation
== ir_quadop_vector
;
1644 * Return a string representing this expression's operator.
1646 const char *operator_string();
1649 * Return a string representing this expression's operator.
1651 static const char *operator_string(ir_expression_operation
);
1655 * Do a reverse-lookup to translate the given string into an operator.
1657 static ir_expression_operation
get_operator(const char *);
1659 virtual void accept(ir_visitor
*v
)
1664 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1666 ir_expression_operation operation
;
1667 ir_rvalue
*operands
[4];
1672 * HIR instruction representing a high-level function call, containing a list
1673 * of parameters and returning a value in the supplied temporary.
1675 class ir_call
: public ir_instruction
{
1677 ir_call(ir_function_signature
*callee
,
1678 ir_dereference_variable
*return_deref
,
1679 exec_list
*actual_parameters
)
1680 : ir_instruction(ir_type_call
), return_deref(return_deref
), callee(callee
)
1682 assert(callee
->return_type
!= NULL
);
1683 actual_parameters
->move_nodes_to(& this->actual_parameters
);
1684 this->use_builtin
= callee
->is_builtin();
1687 virtual ir_call
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1689 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
1691 virtual void accept(ir_visitor
*v
)
1696 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1699 * Get the name of the function being called.
1701 const char *callee_name() const
1703 return callee
->function_name();
1707 * Generates an inline version of the function before @ir,
1708 * storing the return value in return_deref.
1710 void generate_inline(ir_instruction
*ir
);
1713 * Storage for the function's return value.
1714 * This must be NULL if the return type is void.
1716 ir_dereference_variable
*return_deref
;
1719 * The specific function signature being called.
1721 ir_function_signature
*callee
;
1723 /* List of ir_rvalue of paramaters passed in this call. */
1724 exec_list actual_parameters
;
1726 /** Should this call only bind to a built-in function? */
1732 * \name Jump-like IR instructions.
1734 * These include \c break, \c continue, \c return, and \c discard.
1737 class ir_jump
: public ir_instruction
{
1739 ir_jump(enum ir_node_type t
)
1745 class ir_return
: public ir_jump
{
1748 : ir_jump(ir_type_return
), value(NULL
)
1752 ir_return(ir_rvalue
*value
)
1753 : ir_jump(ir_type_return
), value(value
)
1757 virtual ir_return
*clone(void *mem_ctx
, struct hash_table
*) const;
1759 ir_rvalue
*get_value() const
1764 virtual void accept(ir_visitor
*v
)
1769 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1776 * Jump instructions used inside loops
1778 * These include \c break and \c continue. The \c break within a loop is
1779 * different from the \c break within a switch-statement.
1781 * \sa ir_switch_jump
1783 class ir_loop_jump
: public ir_jump
{
1790 ir_loop_jump(jump_mode mode
)
1791 : ir_jump(ir_type_loop_jump
)
1796 virtual ir_loop_jump
*clone(void *mem_ctx
, struct hash_table
*) const;
1798 virtual void accept(ir_visitor
*v
)
1803 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1805 bool is_break() const
1807 return mode
== jump_break
;
1810 bool is_continue() const
1812 return mode
== jump_continue
;
1815 /** Mode selector for the jump instruction. */
1816 enum jump_mode mode
;
1820 * IR instruction representing discard statements.
1822 class ir_discard
: public ir_jump
{
1825 : ir_jump(ir_type_discard
)
1827 this->condition
= NULL
;
1830 ir_discard(ir_rvalue
*cond
)
1831 : ir_jump(ir_type_discard
)
1833 this->condition
= cond
;
1836 virtual ir_discard
*clone(void *mem_ctx
, struct hash_table
*ht
) const;
1838 virtual void accept(ir_visitor
*v
)
1843 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1845 ir_rvalue
*condition
;
1851 * Texture sampling opcodes used in ir_texture
1853 enum ir_texture_opcode
{
1854 ir_tex
, /**< Regular texture look-up */
1855 ir_txb
, /**< Texture look-up with LOD bias */
1856 ir_txl
, /**< Texture look-up with explicit LOD */
1857 ir_txd
, /**< Texture look-up with partial derivatvies */
1858 ir_txf
, /**< Texel fetch with explicit LOD */
1859 ir_txf_ms
, /**< Multisample texture fetch */
1860 ir_txs
, /**< Texture size */
1861 ir_lod
, /**< Texture lod query */
1862 ir_tg4
, /**< Texture gather */
1863 ir_query_levels
/**< Texture levels query */
1868 * IR instruction to sample a texture
1870 * The specific form of the IR instruction depends on the \c mode value
1871 * selected from \c ir_texture_opcodes. In the printed IR, these will
1874 * Texel offset (0 or an expression)
1875 * | Projection divisor
1876 * | | Shadow comparitor
1879 * (tex <type> <sampler> <coordinate> 0 1 ( ))
1880 * (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)
1881 * (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)
1882 * (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))
1883 * (txf <type> <sampler> <coordinate> 0 <lod>)
1885 * <type> <sampler> <coordinate> <sample_index>)
1886 * (txs <type> <sampler> <lod>)
1887 * (lod <type> <sampler> <coordinate>)
1888 * (tg4 <type> <sampler> <coordinate> <offset> <component>)
1889 * (query_levels <type> <sampler>)
1891 class ir_texture
: public ir_rvalue
{
1893 ir_texture(enum ir_texture_opcode op
)
1894 : ir_rvalue(ir_type_texture
),
1895 op(op
), sampler(NULL
), coordinate(NULL
), projector(NULL
),
1896 shadow_comparitor(NULL
), offset(NULL
)
1898 memset(&lod_info
, 0, sizeof(lod_info
));
1901 virtual ir_texture
*clone(void *mem_ctx
, struct hash_table
*) const;
1903 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
1905 virtual void accept(ir_visitor
*v
)
1910 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1912 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
1915 * Return a string representing the ir_texture_opcode.
1917 const char *opcode_string();
1919 /** Set the sampler and type. */
1920 void set_sampler(ir_dereference
*sampler
, const glsl_type
*type
);
1923 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1925 static ir_texture_opcode
get_opcode(const char *);
1927 enum ir_texture_opcode op
;
1929 /** Sampler to use for the texture access. */
1930 ir_dereference
*sampler
;
1932 /** Texture coordinate to sample */
1933 ir_rvalue
*coordinate
;
1936 * Value used for projective divide.
1938 * If there is no projective divide (the common case), this will be
1939 * \c NULL. Optimization passes should check for this to point to a constant
1940 * of 1.0 and replace that with \c NULL.
1942 ir_rvalue
*projector
;
1945 * Coordinate used for comparison on shadow look-ups.
1947 * If there is no shadow comparison, this will be \c NULL. For the
1948 * \c ir_txf opcode, this *must* be \c NULL.
1950 ir_rvalue
*shadow_comparitor
;
1952 /** Texel offset. */
1956 ir_rvalue
*lod
; /**< Floating point LOD */
1957 ir_rvalue
*bias
; /**< Floating point LOD bias */
1958 ir_rvalue
*sample_index
; /**< MSAA sample index */
1959 ir_rvalue
*component
; /**< Gather component selector */
1961 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
1962 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
1968 struct ir_swizzle_mask
{
1975 * Number of components in the swizzle.
1977 unsigned num_components
:3;
1980 * Does the swizzle contain duplicate components?
1982 * L-value swizzles cannot contain duplicate components.
1984 unsigned has_duplicates
:1;
1988 class ir_swizzle
: public ir_rvalue
{
1990 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
1993 ir_swizzle(ir_rvalue
*val
, const unsigned *components
, unsigned count
);
1995 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
1997 virtual ir_swizzle
*clone(void *mem_ctx
, struct hash_table
*) const;
1999 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
2002 * Construct an ir_swizzle from the textual representation. Can fail.
2004 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
2006 virtual void accept(ir_visitor
*v
)
2011 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2013 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
2015 bool is_lvalue() const
2017 return val
->is_lvalue() && !mask
.has_duplicates
;
2021 * Get the variable that is ultimately referenced by an r-value
2023 virtual ir_variable
*variable_referenced() const;
2026 ir_swizzle_mask mask
;
2030 * Initialize the mask component of a swizzle
2032 * This is used by the \c ir_swizzle constructors.
2034 void init_mask(const unsigned *components
, unsigned count
);
2038 class ir_dereference
: public ir_rvalue
{
2040 virtual ir_dereference
*clone(void *mem_ctx
, struct hash_table
*) const = 0;
2042 bool is_lvalue() const;
2045 * Get the variable that is ultimately referenced by an r-value
2047 virtual ir_variable
*variable_referenced() const = 0;
2050 ir_dereference(enum ir_node_type t
)
2057 class ir_dereference_variable
: public ir_dereference
{
2059 ir_dereference_variable(ir_variable
*var
);
2061 virtual ir_dereference_variable
*clone(void *mem_ctx
,
2062 struct hash_table
*) const;
2064 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
2066 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
2069 * Get the variable that is ultimately referenced by an r-value
2071 virtual ir_variable
*variable_referenced() const
2076 virtual ir_variable
*whole_variable_referenced()
2078 /* ir_dereference_variable objects always dereference the entire
2079 * variable. However, if this dereference is dereferenced by anything
2080 * else, the complete deferefernce chain is not a whole-variable
2081 * dereference. This method should only be called on the top most
2082 * ir_rvalue in a dereference chain.
2087 virtual void accept(ir_visitor
*v
)
2092 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2095 * Object being dereferenced.
2101 class ir_dereference_array
: public ir_dereference
{
2103 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
2105 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
2107 virtual ir_dereference_array
*clone(void *mem_ctx
,
2108 struct hash_table
*) const;
2110 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
2112 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
2115 * Get the variable that is ultimately referenced by an r-value
2117 virtual ir_variable
*variable_referenced() const
2119 return this->array
->variable_referenced();
2122 virtual void accept(ir_visitor
*v
)
2127 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2130 ir_rvalue
*array_index
;
2133 void set_array(ir_rvalue
*value
);
2137 class ir_dereference_record
: public ir_dereference
{
2139 ir_dereference_record(ir_rvalue
*value
, const char *field
);
2141 ir_dereference_record(ir_variable
*var
, const char *field
);
2143 virtual ir_dereference_record
*clone(void *mem_ctx
,
2144 struct hash_table
*) const;
2146 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
2149 * Get the variable that is ultimately referenced by an r-value
2151 virtual ir_variable
*variable_referenced() const
2153 return this->record
->variable_referenced();
2156 virtual void accept(ir_visitor
*v
)
2161 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2169 * Data stored in an ir_constant
2171 union ir_constant_data
{
2180 class ir_constant
: public ir_rvalue
{
2182 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
2183 ir_constant(bool b
, unsigned vector_elements
=1);
2184 ir_constant(unsigned int u
, unsigned vector_elements
=1);
2185 ir_constant(int i
, unsigned vector_elements
=1);
2186 ir_constant(float f
, unsigned vector_elements
=1);
2187 ir_constant(double d
, unsigned vector_elements
=1);
2190 * Construct an ir_constant from a list of ir_constant values
2192 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
2195 * Construct an ir_constant from a scalar component of another ir_constant
2197 * The new \c ir_constant inherits the type of the component from the
2201 * In the case of a matrix constant, the new constant is a scalar, \b not
2204 ir_constant(const ir_constant
*c
, unsigned i
);
2207 * Return a new ir_constant of the specified type containing all zeros.
2209 static ir_constant
*zero(void *mem_ctx
, const glsl_type
*type
);
2211 virtual ir_constant
*clone(void *mem_ctx
, struct hash_table
*) const;
2213 virtual ir_constant
*constant_expression_value(struct hash_table
*variable_context
= NULL
);
2215 virtual void accept(ir_visitor
*v
)
2220 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2222 virtual bool equals(ir_instruction
*ir
, enum ir_node_type ignore
= ir_type_unset
);
2225 * Get a particular component of a constant as a specific type
2227 * This is useful, for example, to get a value from an integer constant
2228 * as a float or bool. This appears frequently when constructors are
2229 * called with all constant parameters.
2232 bool get_bool_component(unsigned i
) const;
2233 float get_float_component(unsigned i
) const;
2234 double get_double_component(unsigned i
) const;
2235 int get_int_component(unsigned i
) const;
2236 unsigned get_uint_component(unsigned i
) const;
2239 ir_constant
*get_array_element(unsigned i
) const;
2241 ir_constant
*get_record_field(const char *name
);
2244 * Copy the values on another constant at a given offset.
2246 * The offset is ignored for array or struct copies, it's only for
2247 * scalars or vectors into vectors or matrices.
2249 * With identical types on both sides and zero offset it's clone()
2250 * without creating a new object.
2253 void copy_offset(ir_constant
*src
, int offset
);
2256 * Copy the values on another constant at a given offset and
2257 * following an assign-like mask.
2259 * The mask is ignored for scalars.
2261 * Note that this function only handles what assign can handle,
2262 * i.e. at most a vector as source and a column of a matrix as
2266 void copy_masked_offset(ir_constant
*src
, int offset
, unsigned int mask
);
2269 * Determine whether a constant has the same value as another constant
2271 * \sa ir_constant::is_zero, ir_constant::is_one,
2272 * ir_constant::is_negative_one
2274 bool has_value(const ir_constant
*) const;
2277 * Return true if this ir_constant represents the given value.
2279 * For vectors, this checks that each component is the given value.
2281 virtual bool is_value(float f
, int i
) const;
2282 virtual bool is_zero() const;
2283 virtual bool is_one() const;
2284 virtual bool is_negative_one() const;
2287 * Return true for constants that could be stored as 16-bit unsigned values.
2289 * Note that this will return true even for signed integer ir_constants, as
2290 * long as the value is non-negative and fits in 16-bits.
2292 virtual bool is_uint16_constant() const;
2295 * Value of the constant.
2297 * The field used to back the values supplied by the constant is determined
2298 * by the type associated with the \c ir_instruction. Constants may be
2299 * scalars, vectors, or matrices.
2301 union ir_constant_data value
;
2303 /* Array elements */
2304 ir_constant
**array_elements
;
2306 /* Structure fields */
2307 exec_list components
;
2311 * Parameterless constructor only used by the clone method
2317 * IR instruction to emit a vertex in a geometry shader.
2319 class ir_emit_vertex
: public ir_instruction
{
2321 ir_emit_vertex(ir_rvalue
*stream
)
2322 : ir_instruction(ir_type_emit_vertex
),
2328 virtual void accept(ir_visitor
*v
)
2333 virtual ir_emit_vertex
*clone(void *mem_ctx
, struct hash_table
*ht
) const
2335 return new(mem_ctx
) ir_emit_vertex(this->stream
->clone(mem_ctx
, ht
));
2338 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2340 int stream_id() const
2342 return stream
->as_constant()->value
.i
[0];
2349 * IR instruction to complete the current primitive and start a new one in a
2352 class ir_end_primitive
: public ir_instruction
{
2354 ir_end_primitive(ir_rvalue
*stream
)
2355 : ir_instruction(ir_type_end_primitive
),
2361 virtual void accept(ir_visitor
*v
)
2366 virtual ir_end_primitive
*clone(void *mem_ctx
, struct hash_table
*ht
) const
2368 return new(mem_ctx
) ir_end_primitive(this->stream
->clone(mem_ctx
, ht
));
2371 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
2373 int stream_id() const
2375 return stream
->as_constant()->value
.i
[0];
2384 * Apply a visitor to each IR node in a list
2387 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
2390 * Validate invariants on each IR node in a list
2392 void validate_ir_tree(exec_list
*instructions
);
2394 struct _mesa_glsl_parse_state
;
2395 struct gl_shader_program
;
2398 * Detect whether an unlinked shader contains static recursion
2400 * If the list of instructions is determined to contain static recursion,
2401 * \c _mesa_glsl_error will be called to emit error messages for each function
2402 * that is in the recursion cycle.
2405 detect_recursion_unlinked(struct _mesa_glsl_parse_state
*state
,
2406 exec_list
*instructions
);
2409 * Detect whether a linked shader contains static recursion
2411 * If the list of instructions is determined to contain static recursion,
2412 * \c link_error_printf will be called to emit error messages for each function
2413 * that is in the recursion cycle. In addition,
2414 * \c gl_shader_program::LinkStatus will be set to false.
2417 detect_recursion_linked(struct gl_shader_program
*prog
,
2418 exec_list
*instructions
);
2421 * Make a clone of each IR instruction in a list
2423 * \param in List of IR instructions that are to be cloned
2424 * \param out List to hold the cloned instructions
2427 clone_ir_list(void *mem_ctx
, exec_list
*out
, const exec_list
*in
);
2430 _mesa_glsl_initialize_variables(exec_list
*instructions
,
2431 struct _mesa_glsl_parse_state
*state
);
2434 _mesa_glsl_initialize_functions(_mesa_glsl_parse_state
*state
);
2437 _mesa_glsl_initialize_builtin_functions();
2439 extern ir_function_signature
*
2440 _mesa_glsl_find_builtin_function(_mesa_glsl_parse_state
*state
,
2441 const char *name
, exec_list
*actual_parameters
);
2443 extern ir_function
*
2444 _mesa_glsl_find_builtin_function_by_name(_mesa_glsl_parse_state
*state
,
2448 _mesa_glsl_get_builtin_function_shader(void);
2451 _mesa_glsl_release_functions(void);
2454 _mesa_glsl_release_builtin_functions(void);
2457 reparent_ir(exec_list
*list
, void *mem_ctx
);
2459 struct glsl_symbol_table
;
2462 import_prototypes(const exec_list
*source
, exec_list
*dest
,
2463 struct glsl_symbol_table
*symbols
, void *mem_ctx
);
2466 ir_has_call(ir_instruction
*ir
);
2469 do_set_program_inouts(exec_list
*instructions
, struct gl_program
*prog
,
2470 gl_shader_stage shader_stage
);
2473 prototype_string(const glsl_type
*return_type
, const char *name
,
2474 exec_list
*parameters
);
2477 mode_string(const ir_variable
*var
);
2480 * Built-in / reserved GL variables names start with "gl_"
2483 is_gl_identifier(const char *s
)
2485 return s
&& s
[0] == 'g' && s
[1] == 'l' && s
[2] == '_';
2489 #endif /* __cplusplus */
2491 extern void _mesa_print_ir(FILE *f
, struct exec_list
*instructions
,
2492 struct _mesa_glsl_parse_state
*state
);
2495 fprint_ir(FILE *f
, const void *instruction
);
2502 vertices_per_prim(GLenum prim
);