allow builtin functions to require parameters to be shader inputs
[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 <stdio.h>
30 #include <stdlib.h>
31
32 #include "ralloc.h"
33 #include "glsl_types.h"
34 #include "list.h"
35 #include "ir_visitor.h"
36 #include "ir_hierarchical_visitor.h"
37 #include "main/mtypes.h"
38
39 #ifdef __cplusplus
40
41 /**
42 * \defgroup IR Intermediate representation nodes
43 *
44 * @{
45 */
46
47 /**
48 * Class tags
49 *
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.
55 *
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.
60 */
61 enum ir_node_type {
62 ir_type_dereference_array,
63 ir_type_dereference_record,
64 ir_type_dereference_variable,
65 ir_type_constant,
66 ir_type_expression,
67 ir_type_swizzle,
68 ir_type_texture,
69 ir_type_variable,
70 ir_type_assignment,
71 ir_type_call,
72 ir_type_function,
73 ir_type_function_signature,
74 ir_type_if,
75 ir_type_loop,
76 ir_type_loop_jump,
77 ir_type_return,
78 ir_type_discard,
79 ir_type_emit_vertex,
80 ir_type_end_primitive,
81 ir_type_max, /**< maximum ir_type enum number, for validation */
82 ir_type_unset = ir_type_max
83 };
84
85
86 /**
87 * Base class of all IR instructions
88 */
89 class ir_instruction : public exec_node {
90 public:
91 enum ir_node_type ir_type;
92
93 /**
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.
98 */
99 virtual ~ir_instruction()
100 {
101 }
102
103 /** ir_print_visitor helper for debugging. */
104 void print(void) const;
105 void fprint(FILE *f) const;
106
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;
111
112 /**
113 * \name IR instruction downcast functions
114 *
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.
118 */
119 /*@{*/
120 class ir_rvalue *as_rvalue()
121 {
122 if (ir_type == ir_type_dereference_array ||
123 ir_type == ir_type_dereference_record ||
124 ir_type == ir_type_dereference_variable ||
125 ir_type == ir_type_constant ||
126 ir_type == ir_type_expression ||
127 ir_type == ir_type_swizzle ||
128 ir_type == ir_type_texture)
129 return (class ir_rvalue *) this;
130 return NULL;
131 }
132
133 class ir_dereference *as_dereference()
134 {
135 if (ir_type == ir_type_dereference_array ||
136 ir_type == ir_type_dereference_record ||
137 ir_type == ir_type_dereference_variable)
138 return (class ir_dereference *) this;
139 return NULL;
140 }
141
142 class ir_jump *as_jump()
143 {
144 if (ir_type == ir_type_loop_jump ||
145 ir_type == ir_type_return ||
146 ir_type == ir_type_discard)
147 return (class ir_jump *) this;
148 return NULL;
149 }
150
151 #define AS_CHILD(TYPE) \
152 class ir_##TYPE * as_##TYPE() \
153 { \
154 return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \
155 }
156 AS_CHILD(variable)
157 AS_CHILD(function)
158 AS_CHILD(dereference_array)
159 AS_CHILD(dereference_variable)
160 AS_CHILD(dereference_record)
161 AS_CHILD(expression)
162 AS_CHILD(loop)
163 AS_CHILD(assignment)
164 AS_CHILD(call)
165 AS_CHILD(return)
166 AS_CHILD(if)
167 AS_CHILD(swizzle)
168 AS_CHILD(texture)
169 AS_CHILD(constant)
170 AS_CHILD(discard)
171 #undef AS_CHILD
172 /*@}*/
173
174 /**
175 * IR equality method: Return true if the referenced instruction would
176 * return the same value as this one.
177 *
178 * This intended to be used for CSE and algebraic optimizations, on rvalues
179 * in particular. No support for other instruction types (assignments,
180 * jumps, calls, etc.) is planned.
181 */
182 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
183
184 protected:
185 ir_instruction(enum ir_node_type t)
186 : ir_type(t)
187 {
188 }
189
190 private:
191 ir_instruction()
192 {
193 assert(!"Should not get here.");
194 }
195 };
196
197
198 /**
199 * The base class for all "values"/expression trees.
200 */
201 class ir_rvalue : public ir_instruction {
202 public:
203 const struct glsl_type *type;
204
205 virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;
206
207 virtual void accept(ir_visitor *v)
208 {
209 v->visit(this);
210 }
211
212 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
213
214 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
215
216 ir_rvalue *as_rvalue_to_saturate();
217
218 virtual bool is_lvalue() const
219 {
220 return false;
221 }
222
223 /**
224 * Get the variable that is ultimately referenced by an r-value
225 */
226 virtual ir_variable *variable_referenced() const
227 {
228 return NULL;
229 }
230
231
232 /**
233 * If an r-value is a reference to a whole variable, get that variable
234 *
235 * \return
236 * Pointer to a variable that is completely dereferenced by the r-value. If
237 * the r-value is not a dereference or the dereference does not access the
238 * entire variable (i.e., it's just one array element, struct field), \c NULL
239 * is returned.
240 */
241 virtual ir_variable *whole_variable_referenced()
242 {
243 return NULL;
244 }
245
246 /**
247 * Determine if an r-value has the value zero
248 *
249 * The base implementation of this function always returns \c false. The
250 * \c ir_constant class over-rides this function to return \c true \b only
251 * for vector and scalar types that have all elements set to the value
252 * zero (or \c false for booleans).
253 *
254 * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one,
255 * ir_constant::is_basis
256 */
257 virtual bool is_zero() const;
258
259 /**
260 * Determine if an r-value has the value one
261 *
262 * The base implementation of this function always returns \c false. The
263 * \c ir_constant class over-rides this function to return \c true \b only
264 * for vector and scalar types that have all elements set to the value
265 * one (or \c true for booleans).
266 *
267 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one,
268 * ir_constant::is_basis
269 */
270 virtual bool is_one() const;
271
272 /**
273 * Determine if an r-value has the value negative one
274 *
275 * The base implementation of this function always returns \c false. The
276 * \c ir_constant class over-rides this function to return \c true \b only
277 * for vector and scalar types that have all elements set to the value
278 * negative one. For boolean types, the result is always \c false.
279 *
280 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
281 * ir_constant::is_basis
282 */
283 virtual bool is_negative_one() const;
284
285 /**
286 * Determine if an r-value is a basis vector
287 *
288 * The base implementation of this function always returns \c false. The
289 * \c ir_constant class over-rides this function to return \c true \b only
290 * for vector and scalar types that have one element set to the value one,
291 * and the other elements set to the value zero. For boolean types, the
292 * result is always \c false.
293 *
294 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one,
295 * is_constant::is_negative_one
296 */
297 virtual bool is_basis() const;
298
299 /**
300 * Determine if an r-value is an unsigned integer constant which can be
301 * stored in 16 bits.
302 *
303 * \sa ir_constant::is_uint16_constant.
304 */
305 virtual bool is_uint16_constant() const { return false; }
306
307 /**
308 * Return a generic value of error_type.
309 *
310 * Allocation will be performed with 'mem_ctx' as ralloc owner.
311 */
312 static ir_rvalue *error_value(void *mem_ctx);
313
314 protected:
315 ir_rvalue(enum ir_node_type t);
316 };
317
318
319 /**
320 * Variable storage classes
321 */
322 enum ir_variable_mode {
323 ir_var_auto = 0, /**< Function local variables and globals. */
324 ir_var_uniform, /**< Variable declared as a uniform. */
325 ir_var_shader_in,
326 ir_var_shader_out,
327 ir_var_function_in,
328 ir_var_function_out,
329 ir_var_function_inout,
330 ir_var_const_in, /**< "in" param that must be a constant expression */
331 ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
332 ir_var_temporary, /**< Temporary variable generated during compilation. */
333 ir_var_mode_count /**< Number of variable modes */
334 };
335
336 /**
337 * Enum keeping track of how a variable was declared. For error checking of
338 * the gl_PerVertex redeclaration rules.
339 */
340 enum ir_var_declaration_type {
341 /**
342 * Normal declaration (for most variables, this means an explicit
343 * declaration. Exception: temporaries are always implicitly declared, but
344 * they still use ir_var_declared_normally).
345 *
346 * Note: an ir_variable that represents a named interface block uses
347 * ir_var_declared_normally.
348 */
349 ir_var_declared_normally = 0,
350
351 /**
352 * Variable was explicitly declared (or re-declared) in an unnamed
353 * interface block.
354 */
355 ir_var_declared_in_block,
356
357 /**
358 * Variable is an implicitly declared built-in that has not been explicitly
359 * re-declared by the shader.
360 */
361 ir_var_declared_implicitly,
362 };
363
364 /**
365 * \brief Layout qualifiers for gl_FragDepth.
366 *
367 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
368 * with a layout qualifier.
369 */
370 enum ir_depth_layout {
371 ir_depth_layout_none, /**< No depth layout is specified. */
372 ir_depth_layout_any,
373 ir_depth_layout_greater,
374 ir_depth_layout_less,
375 ir_depth_layout_unchanged
376 };
377
378 /**
379 * \brief Convert depth layout qualifier to string.
380 */
381 const char*
382 depth_layout_string(ir_depth_layout layout);
383
384 /**
385 * Description of built-in state associated with a uniform
386 *
387 * \sa ir_variable::state_slots
388 */
389 struct ir_state_slot {
390 int tokens[5];
391 int swizzle;
392 };
393
394
395 /**
396 * Get the string value for an interpolation qualifier
397 *
398 * \return The string that would be used in a shader to specify \c
399 * mode will be returned.
400 *
401 * This function is used to generate error messages of the form "shader
402 * uses %s interpolation qualifier", so in the case where there is no
403 * interpolation qualifier, it returns "no".
404 *
405 * This function should only be used on a shader input or output variable.
406 */
407 const char *interpolation_string(unsigned interpolation);
408
409
410 class ir_variable : public ir_instruction {
411 public:
412 ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
413
414 virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
415
416 virtual void accept(ir_visitor *v)
417 {
418 v->visit(this);
419 }
420
421 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
422
423
424 /**
425 * Determine how this variable should be interpolated based on its
426 * interpolation qualifier (if present), whether it is gl_Color or
427 * gl_SecondaryColor, and whether flatshading is enabled in the current GL
428 * state.
429 *
430 * The return value will always be either INTERP_QUALIFIER_SMOOTH,
431 * INTERP_QUALIFIER_NOPERSPECTIVE, or INTERP_QUALIFIER_FLAT.
432 */
433 glsl_interp_qualifier determine_interpolation_mode(bool flat_shade);
434
435 /**
436 * Determine whether or not a variable is part of a uniform block.
437 */
438 inline bool is_in_uniform_block() const
439 {
440 return this->data.mode == ir_var_uniform && this->interface_type != NULL;
441 }
442
443 /**
444 * Determine whether or not a variable is the declaration of an interface
445 * block
446 *
447 * For the first declaration below, there will be an \c ir_variable named
448 * "instance" whose type and whose instance_type will be the same
449 * \cglsl_type. For the second declaration, there will be an \c ir_variable
450 * named "f" whose type is float and whose instance_type is B2.
451 *
452 * "instance" is an interface instance variable, but "f" is not.
453 *
454 * uniform B1 {
455 * float f;
456 * } instance;
457 *
458 * uniform B2 {
459 * float f;
460 * };
461 */
462 inline bool is_interface_instance() const
463 {
464 const glsl_type *const t = this->type;
465
466 return (t == this->interface_type)
467 || (t->is_array() && t->fields.array == this->interface_type);
468 }
469
470 /**
471 * Set this->interface_type on a newly created variable.
472 */
473 void init_interface_type(const struct glsl_type *type)
474 {
475 assert(this->interface_type == NULL);
476 this->interface_type = type;
477 if (this->is_interface_instance()) {
478 this->max_ifc_array_access =
479 rzalloc_array(this, unsigned, type->length);
480 }
481 }
482
483 /**
484 * Change this->interface_type on a variable that previously had a
485 * different, but compatible, interface_type. This is used during linking
486 * to set the size of arrays in interface blocks.
487 */
488 void change_interface_type(const struct glsl_type *type)
489 {
490 if (this->max_ifc_array_access != NULL) {
491 /* max_ifc_array_access has already been allocated, so make sure the
492 * new interface has the same number of fields as the old one.
493 */
494 assert(this->interface_type->length == type->length);
495 }
496 this->interface_type = type;
497 }
498
499 /**
500 * Change this->interface_type on a variable that previously had a
501 * different, and incompatible, interface_type. This is used during
502 * compilation to handle redeclaration of the built-in gl_PerVertex
503 * interface block.
504 */
505 void reinit_interface_type(const struct glsl_type *type)
506 {
507 if (this->max_ifc_array_access != NULL) {
508 #ifndef NDEBUG
509 /* Redeclaring gl_PerVertex is only allowed if none of the built-ins
510 * it defines have been accessed yet; so it's safe to throw away the
511 * old max_ifc_array_access pointer, since all of its values are
512 * zero.
513 */
514 for (unsigned i = 0; i < this->interface_type->length; i++)
515 assert(this->max_ifc_array_access[i] == 0);
516 #endif
517 ralloc_free(this->max_ifc_array_access);
518 this->max_ifc_array_access = NULL;
519 }
520 this->interface_type = NULL;
521 init_interface_type(type);
522 }
523
524 const glsl_type *get_interface_type() const
525 {
526 return this->interface_type;
527 }
528
529 /**
530 * Declared type of the variable
531 */
532 const struct glsl_type *type;
533
534 /**
535 * Declared name of the variable
536 */
537 const char *name;
538
539 /**
540 * For variables which satisfy the is_interface_instance() predicate, this
541 * points to an array of integers such that if the ith member of the
542 * interface block is an array, max_ifc_array_access[i] is the maximum
543 * array element of that member that has been accessed. If the ith member
544 * of the interface block is not an array, max_ifc_array_access[i] is
545 * unused.
546 *
547 * For variables whose type is not an interface block, this pointer is
548 * NULL.
549 */
550 unsigned *max_ifc_array_access;
551
552 struct ir_variable_data {
553
554 /**
555 * Is the variable read-only?
556 *
557 * This is set for variables declared as \c const, shader inputs,
558 * and uniforms.
559 */
560 unsigned read_only:1;
561 unsigned centroid:1;
562 unsigned sample:1;
563 unsigned invariant:1;
564 unsigned precise:1;
565
566 /**
567 * Has this variable been used for reading or writing?
568 *
569 * Several GLSL semantic checks require knowledge of whether or not a
570 * variable has been used. For example, it is an error to redeclare a
571 * variable as invariant after it has been used.
572 *
573 * This is only maintained in the ast_to_hir.cpp path, not in
574 * Mesa's fixed function or ARB program paths.
575 */
576 unsigned used:1;
577
578 /**
579 * Has this variable been statically assigned?
580 *
581 * This answers whether the variable was assigned in any path of
582 * the shader during ast_to_hir. This doesn't answer whether it is
583 * still written after dead code removal, nor is it maintained in
584 * non-ast_to_hir.cpp (GLSL parsing) paths.
585 */
586 unsigned assigned:1;
587
588 /**
589 * Enum indicating how the variable was declared. See
590 * ir_var_declaration_type.
591 *
592 * This is used to detect certain kinds of illegal variable redeclarations.
593 */
594 unsigned how_declared:2;
595
596 /**
597 * Storage class of the variable.
598 *
599 * \sa ir_variable_mode
600 */
601 unsigned mode:4;
602
603 /**
604 * Interpolation mode for shader inputs / outputs
605 *
606 * \sa ir_variable_interpolation
607 */
608 unsigned interpolation:2;
609
610 /**
611 * \name ARB_fragment_coord_conventions
612 * @{
613 */
614 unsigned origin_upper_left:1;
615 unsigned pixel_center_integer:1;
616 /*@}*/
617
618 /**
619 * Was the location explicitly set in the shader?
620 *
621 * If the location is explicitly set in the shader, it \b cannot be changed
622 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
623 * no effect).
624 */
625 unsigned explicit_location:1;
626 unsigned explicit_index:1;
627
628 /**
629 * Was an initial binding explicitly set in the shader?
630 *
631 * If so, constant_value contains an integer ir_constant representing the
632 * initial binding point.
633 */
634 unsigned explicit_binding:1;
635
636 /**
637 * Does this variable have an initializer?
638 *
639 * This is used by the linker to cross-validiate initializers of global
640 * variables.
641 */
642 unsigned has_initializer:1;
643
644 /**
645 * Is this variable a generic output or input that has not yet been matched
646 * up to a variable in another stage of the pipeline?
647 *
648 * This is used by the linker as scratch storage while assigning locations
649 * to generic inputs and outputs.
650 */
651 unsigned is_unmatched_generic_inout:1;
652
653 /**
654 * If non-zero, then this variable may be packed along with other variables
655 * into a single varying slot, so this offset should be applied when
656 * accessing components. For example, an offset of 1 means that the x
657 * component of this variable is actually stored in component y of the
658 * location specified by \c location.
659 */
660 unsigned location_frac:2;
661
662 /**
663 * Non-zero if this variable was created by lowering a named interface
664 * block which was not an array.
665 *
666 * Note that this variable and \c from_named_ifc_block_array will never
667 * both be non-zero.
668 */
669 unsigned from_named_ifc_block_nonarray:1;
670
671 /**
672 * Non-zero if this variable was created by lowering a named interface
673 * block which was an array.
674 *
675 * Note that this variable and \c from_named_ifc_block_nonarray will never
676 * both be non-zero.
677 */
678 unsigned from_named_ifc_block_array:1;
679
680 /**
681 * Non-zero if the variable must be a shader input. This is useful for
682 * constraints on function parameters.
683 */
684 unsigned must_be_shader_input:1;
685
686 /**
687 * \brief Layout qualifier for gl_FragDepth.
688 *
689 * This is not equal to \c ir_depth_layout_none if and only if this
690 * variable is \c gl_FragDepth and a layout qualifier is specified.
691 */
692 ir_depth_layout depth_layout;
693
694 /**
695 * Storage location of the base of this variable
696 *
697 * The precise meaning of this field depends on the nature of the variable.
698 *
699 * - Vertex shader input: one of the values from \c gl_vert_attrib.
700 * - Vertex shader output: one of the values from \c gl_varying_slot.
701 * - Geometry shader input: one of the values from \c gl_varying_slot.
702 * - Geometry shader output: one of the values from \c gl_varying_slot.
703 * - Fragment shader input: one of the values from \c gl_varying_slot.
704 * - Fragment shader output: one of the values from \c gl_frag_result.
705 * - Uniforms: Per-stage uniform slot number for default uniform block.
706 * - Uniforms: Index within the uniform block definition for UBO members.
707 * - Other: This field is not currently used.
708 *
709 * If the variable is a uniform, shader input, or shader output, and the
710 * slot has not been assigned, the value will be -1.
711 */
712 int location;
713
714 /**
715 * Vertex stream output identifier.
716 */
717 unsigned stream;
718
719 /**
720 * output index for dual source blending.
721 */
722 int index;
723
724 /**
725 * Initial binding point for a sampler or UBO.
726 *
727 * For array types, this represents the binding point for the first element.
728 */
729 int binding;
730
731 /**
732 * Location an atomic counter is stored at.
733 */
734 struct {
735 unsigned buffer_index;
736 unsigned offset;
737 } atomic;
738
739 /**
740 * ARB_shader_image_load_store qualifiers.
741 */
742 struct {
743 bool read_only; /**< "readonly" qualifier. */
744 bool write_only; /**< "writeonly" qualifier. */
745 bool coherent;
746 bool _volatile;
747 bool restrict_flag;
748
749 /** Image internal format if specified explicitly, otherwise GL_NONE. */
750 GLenum format;
751 } image;
752
753 /**
754 * Highest element accessed with a constant expression array index
755 *
756 * Not used for non-array variables.
757 */
758 unsigned max_array_access;
759
760 } data;
761
762 /**
763 * Built-in state that backs this uniform
764 *
765 * Once set at variable creation, \c state_slots must remain invariant.
766 * This is because, ideally, this array would be shared by all clones of
767 * this variable in the IR tree. In other words, we'd really like for it
768 * to be a fly-weight.
769 *
770 * If the variable is not a uniform, \c num_state_slots will be zero and
771 * \c state_slots will be \c NULL.
772 */
773 /*@{*/
774 unsigned num_state_slots; /**< Number of state slots used */
775 ir_state_slot *state_slots; /**< State descriptors. */
776 /*@}*/
777
778 /**
779 * Emit a warning if this variable is accessed.
780 */
781 const char *warn_extension;
782
783 /**
784 * Value assigned in the initializer of a variable declared "const"
785 */
786 ir_constant *constant_value;
787
788 /**
789 * Constant expression assigned in the initializer of the variable
790 *
791 * \warning
792 * This field and \c ::constant_value are distinct. Even if the two fields
793 * refer to constants with the same value, they must point to separate
794 * objects.
795 */
796 ir_constant *constant_initializer;
797
798 private:
799 /**
800 * For variables that are in an interface block or are an instance of an
801 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
802 *
803 * \sa ir_variable::location
804 */
805 const glsl_type *interface_type;
806 };
807
808 /**
809 * A function that returns whether a built-in function is available in the
810 * current shading language (based on version, ES or desktop, and extensions).
811 */
812 typedef bool (*builtin_available_predicate)(const _mesa_glsl_parse_state *);
813
814 /*@{*/
815 /**
816 * The representation of a function instance; may be the full definition or
817 * simply a prototype.
818 */
819 class ir_function_signature : public ir_instruction {
820 /* An ir_function_signature will be part of the list of signatures in
821 * an ir_function.
822 */
823 public:
824 ir_function_signature(const glsl_type *return_type,
825 builtin_available_predicate builtin_avail = NULL);
826
827 virtual ir_function_signature *clone(void *mem_ctx,
828 struct hash_table *ht) const;
829 ir_function_signature *clone_prototype(void *mem_ctx,
830 struct hash_table *ht) const;
831
832 virtual void accept(ir_visitor *v)
833 {
834 v->visit(this);
835 }
836
837 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
838
839 /**
840 * Attempt to evaluate this function as a constant expression,
841 * given a list of the actual parameters and the variable context.
842 * Returns NULL for non-built-ins.
843 */
844 ir_constant *constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context);
845
846 /**
847 * Get the name of the function for which this is a signature
848 */
849 const char *function_name() const;
850
851 /**
852 * Get a handle to the function for which this is a signature
853 *
854 * There is no setter function, this function returns a \c const pointer,
855 * and \c ir_function_signature::_function is private for a reason. The
856 * only way to make a connection between a function and function signature
857 * is via \c ir_function::add_signature. This helps ensure that certain
858 * invariants (i.e., a function signature is in the list of signatures for
859 * its \c _function) are met.
860 *
861 * \sa ir_function::add_signature
862 */
863 inline const class ir_function *function() const
864 {
865 return this->_function;
866 }
867
868 /**
869 * Check whether the qualifiers match between this signature's parameters
870 * and the supplied parameter list. If not, returns the name of the first
871 * parameter with mismatched qualifiers (for use in error messages).
872 */
873 const char *qualifiers_match(exec_list *params);
874
875 /**
876 * Replace the current parameter list with the given one. This is useful
877 * if the current information came from a prototype, and either has invalid
878 * or missing parameter names.
879 */
880 void replace_parameters(exec_list *new_params);
881
882 /**
883 * Function return type.
884 *
885 * \note This discards the optional precision qualifier.
886 */
887 const struct glsl_type *return_type;
888
889 /**
890 * List of ir_variable of function parameters.
891 *
892 * This represents the storage. The paramaters passed in a particular
893 * call will be in ir_call::actual_paramaters.
894 */
895 struct exec_list parameters;
896
897 /** Whether or not this function has a body (which may be empty). */
898 unsigned is_defined:1;
899
900 /** Whether or not this function signature is a built-in. */
901 bool is_builtin() const;
902
903 /**
904 * Whether or not this function is an intrinsic to be implemented
905 * by the driver.
906 */
907 bool is_intrinsic;
908
909 /** Whether or not a built-in is available for this shader. */
910 bool is_builtin_available(const _mesa_glsl_parse_state *state) const;
911
912 /** Body of instructions in the function. */
913 struct exec_list body;
914
915 private:
916 /**
917 * A function pointer to a predicate that answers whether a built-in
918 * function is available in the current shader. NULL if not a built-in.
919 */
920 builtin_available_predicate builtin_avail;
921
922 /** Function of which this signature is one overload. */
923 class ir_function *_function;
924
925 /** Function signature of which this one is a prototype clone */
926 const ir_function_signature *origin;
927
928 friend class ir_function;
929
930 /**
931 * Helper function to run a list of instructions for constant
932 * expression evaluation.
933 *
934 * The hash table represents the values of the visible variables.
935 * There are no scoping issues because the table is indexed on
936 * ir_variable pointers, not variable names.
937 *
938 * Returns false if the expression is not constant, true otherwise,
939 * and the value in *result if result is non-NULL.
940 */
941 bool constant_expression_evaluate_expression_list(const struct exec_list &body,
942 struct hash_table *variable_context,
943 ir_constant **result);
944 };
945
946
947 /**
948 * Header for tracking multiple overloaded functions with the same name.
949 * Contains a list of ir_function_signatures representing each of the
950 * actual functions.
951 */
952 class ir_function : public ir_instruction {
953 public:
954 ir_function(const char *name);
955
956 virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
957
958 virtual void accept(ir_visitor *v)
959 {
960 v->visit(this);
961 }
962
963 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
964
965 void add_signature(ir_function_signature *sig)
966 {
967 sig->_function = this;
968 this->signatures.push_tail(sig);
969 }
970
971 /**
972 * Find a signature that matches a set of actual parameters, taking implicit
973 * conversions into account. Also flags whether the match was exact.
974 */
975 ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
976 const exec_list *actual_param,
977 bool *match_is_exact);
978
979 /**
980 * Find a signature that matches a set of actual parameters, taking implicit
981 * conversions into account.
982 */
983 ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
984 const exec_list *actual_param);
985
986 /**
987 * Find a signature that exactly matches a set of actual parameters without
988 * any implicit type conversions.
989 */
990 ir_function_signature *exact_matching_signature(_mesa_glsl_parse_state *state,
991 const exec_list *actual_ps);
992
993 /**
994 * Name of the function.
995 */
996 const char *name;
997
998 /** Whether or not this function has a signature that isn't a built-in. */
999 bool has_user_signature();
1000
1001 /**
1002 * List of ir_function_signature for each overloaded function with this name.
1003 */
1004 struct exec_list signatures;
1005 };
1006
1007 inline const char *ir_function_signature::function_name() const
1008 {
1009 return this->_function->name;
1010 }
1011 /*@}*/
1012
1013
1014 /**
1015 * IR instruction representing high-level if-statements
1016 */
1017 class ir_if : public ir_instruction {
1018 public:
1019 ir_if(ir_rvalue *condition)
1020 : ir_instruction(ir_type_if), condition(condition)
1021 {
1022 }
1023
1024 virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
1025
1026 virtual void accept(ir_visitor *v)
1027 {
1028 v->visit(this);
1029 }
1030
1031 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1032
1033 ir_rvalue *condition;
1034 /** List of ir_instruction for the body of the then branch */
1035 exec_list then_instructions;
1036 /** List of ir_instruction for the body of the else branch */
1037 exec_list else_instructions;
1038 };
1039
1040
1041 /**
1042 * IR instruction representing a high-level loop structure.
1043 */
1044 class ir_loop : public ir_instruction {
1045 public:
1046 ir_loop();
1047
1048 virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
1049
1050 virtual void accept(ir_visitor *v)
1051 {
1052 v->visit(this);
1053 }
1054
1055 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1056
1057 /** List of ir_instruction that make up the body of the loop. */
1058 exec_list body_instructions;
1059 };
1060
1061
1062 class ir_assignment : public ir_instruction {
1063 public:
1064 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition = NULL);
1065
1066 /**
1067 * Construct an assignment with an explicit write mask
1068 *
1069 * \note
1070 * Since a write mask is supplied, the LHS must already be a bare
1071 * \c ir_dereference. The cannot be any swizzles in the LHS.
1072 */
1073 ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
1074 unsigned write_mask);
1075
1076 virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
1077
1078 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1079
1080 virtual void accept(ir_visitor *v)
1081 {
1082 v->visit(this);
1083 }
1084
1085 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1086
1087 /**
1088 * Get a whole variable written by an assignment
1089 *
1090 * If the LHS of the assignment writes a whole variable, the variable is
1091 * returned. Otherwise \c NULL is returned. Examples of whole-variable
1092 * assignment are:
1093 *
1094 * - Assigning to a scalar
1095 * - Assigning to all components of a vector
1096 * - Whole array (or matrix) assignment
1097 * - Whole structure assignment
1098 */
1099 ir_variable *whole_variable_written();
1100
1101 /**
1102 * Set the LHS of an assignment
1103 */
1104 void set_lhs(ir_rvalue *lhs);
1105
1106 /**
1107 * Left-hand side of the assignment.
1108 *
1109 * This should be treated as read only. If you need to set the LHS of an
1110 * assignment, use \c ir_assignment::set_lhs.
1111 */
1112 ir_dereference *lhs;
1113
1114 /**
1115 * Value being assigned
1116 */
1117 ir_rvalue *rhs;
1118
1119 /**
1120 * Optional condition for the assignment.
1121 */
1122 ir_rvalue *condition;
1123
1124
1125 /**
1126 * Component mask written
1127 *
1128 * For non-vector types in the LHS, this field will be zero. For vector
1129 * types, a bit will be set for each component that is written. Note that
1130 * for \c vec2 and \c vec3 types only the lower bits will ever be set.
1131 *
1132 * A partially-set write mask means that each enabled channel gets
1133 * the value from a consecutive channel of the rhs. For example,
1134 * to write just .xyw of gl_FrontColor with color:
1135 *
1136 * (assign (constant bool (1)) (xyw)
1137 * (var_ref gl_FragColor)
1138 * (swiz xyw (var_ref color)))
1139 */
1140 unsigned write_mask:4;
1141 };
1142
1143 /* Update ir_expression::get_num_operands() and operator_strs when
1144 * updating this list.
1145 */
1146 enum ir_expression_operation {
1147 ir_unop_bit_not,
1148 ir_unop_logic_not,
1149 ir_unop_neg,
1150 ir_unop_abs,
1151 ir_unop_sign,
1152 ir_unop_rcp,
1153 ir_unop_rsq,
1154 ir_unop_sqrt,
1155 ir_unop_exp, /**< Log base e on gentype */
1156 ir_unop_log, /**< Natural log on gentype */
1157 ir_unop_exp2,
1158 ir_unop_log2,
1159 ir_unop_f2i, /**< Float-to-integer conversion. */
1160 ir_unop_f2u, /**< Float-to-unsigned conversion. */
1161 ir_unop_i2f, /**< Integer-to-float conversion. */
1162 ir_unop_f2b, /**< Float-to-boolean conversion */
1163 ir_unop_b2f, /**< Boolean-to-float conversion */
1164 ir_unop_i2b, /**< int-to-boolean conversion */
1165 ir_unop_b2i, /**< Boolean-to-int conversion */
1166 ir_unop_u2f, /**< Unsigned-to-float conversion. */
1167 ir_unop_i2u, /**< Integer-to-unsigned conversion. */
1168 ir_unop_u2i, /**< Unsigned-to-integer conversion. */
1169 ir_unop_bitcast_i2f, /**< Bit-identical int-to-float "conversion" */
1170 ir_unop_bitcast_f2i, /**< Bit-identical float-to-int "conversion" */
1171 ir_unop_bitcast_u2f, /**< Bit-identical uint-to-float "conversion" */
1172 ir_unop_bitcast_f2u, /**< Bit-identical float-to-uint "conversion" */
1173 ir_unop_any,
1174
1175 /**
1176 * \name Unary floating-point rounding operations.
1177 */
1178 /*@{*/
1179 ir_unop_trunc,
1180 ir_unop_ceil,
1181 ir_unop_floor,
1182 ir_unop_fract,
1183 ir_unop_round_even,
1184 /*@}*/
1185
1186 /**
1187 * \name Trigonometric operations.
1188 */
1189 /*@{*/
1190 ir_unop_sin,
1191 ir_unop_cos,
1192 ir_unop_sin_reduced, /**< Reduced range sin. [-pi, pi] */
1193 ir_unop_cos_reduced, /**< Reduced range cos. [-pi, pi] */
1194 /*@}*/
1195
1196 /**
1197 * \name Partial derivatives.
1198 */
1199 /*@{*/
1200 ir_unop_dFdx,
1201 ir_unop_dFdy,
1202 /*@}*/
1203
1204 /**
1205 * \name Floating point pack and unpack operations.
1206 */
1207 /*@{*/
1208 ir_unop_pack_snorm_2x16,
1209 ir_unop_pack_snorm_4x8,
1210 ir_unop_pack_unorm_2x16,
1211 ir_unop_pack_unorm_4x8,
1212 ir_unop_pack_half_2x16,
1213 ir_unop_unpack_snorm_2x16,
1214 ir_unop_unpack_snorm_4x8,
1215 ir_unop_unpack_unorm_2x16,
1216 ir_unop_unpack_unorm_4x8,
1217 ir_unop_unpack_half_2x16,
1218 /*@}*/
1219
1220 /**
1221 * \name Lowered floating point unpacking operations.
1222 *
1223 * \see lower_packing_builtins_visitor::split_unpack_half_2x16
1224 */
1225 /*@{*/
1226 ir_unop_unpack_half_2x16_split_x,
1227 ir_unop_unpack_half_2x16_split_y,
1228 /*@}*/
1229
1230 /**
1231 * \name Bit operations, part of ARB_gpu_shader5.
1232 */
1233 /*@{*/
1234 ir_unop_bitfield_reverse,
1235 ir_unop_bit_count,
1236 ir_unop_find_msb,
1237 ir_unop_find_lsb,
1238 /*@}*/
1239
1240 ir_unop_noise,
1241
1242 /**
1243 * A sentinel marking the last of the unary operations.
1244 */
1245 ir_last_unop = ir_unop_noise,
1246
1247 ir_binop_add,
1248 ir_binop_sub,
1249 ir_binop_mul, /**< Floating-point or low 32-bit integer multiply. */
1250 ir_binop_imul_high, /**< Calculates the high 32-bits of a 64-bit multiply. */
1251 ir_binop_div,
1252
1253 /**
1254 * Returns the carry resulting from the addition of the two arguments.
1255 */
1256 /*@{*/
1257 ir_binop_carry,
1258 /*@}*/
1259
1260 /**
1261 * Returns the borrow resulting from the subtraction of the second argument
1262 * from the first argument.
1263 */
1264 /*@{*/
1265 ir_binop_borrow,
1266 /*@}*/
1267
1268 /**
1269 * Takes one of two combinations of arguments:
1270 *
1271 * - mod(vecN, vecN)
1272 * - mod(vecN, float)
1273 *
1274 * Does not take integer types.
1275 */
1276 ir_binop_mod,
1277
1278 /**
1279 * \name Binary comparison operators which return a boolean vector.
1280 * The type of both operands must be equal.
1281 */
1282 /*@{*/
1283 ir_binop_less,
1284 ir_binop_greater,
1285 ir_binop_lequal,
1286 ir_binop_gequal,
1287 ir_binop_equal,
1288 ir_binop_nequal,
1289 /**
1290 * Returns single boolean for whether all components of operands[0]
1291 * equal the components of operands[1].
1292 */
1293 ir_binop_all_equal,
1294 /**
1295 * Returns single boolean for whether any component of operands[0]
1296 * is not equal to the corresponding component of operands[1].
1297 */
1298 ir_binop_any_nequal,
1299 /*@}*/
1300
1301 /**
1302 * \name Bit-wise binary operations.
1303 */
1304 /*@{*/
1305 ir_binop_lshift,
1306 ir_binop_rshift,
1307 ir_binop_bit_and,
1308 ir_binop_bit_xor,
1309 ir_binop_bit_or,
1310 /*@}*/
1311
1312 ir_binop_logic_and,
1313 ir_binop_logic_xor,
1314 ir_binop_logic_or,
1315
1316 ir_binop_dot,
1317 ir_binop_min,
1318 ir_binop_max,
1319
1320 ir_binop_pow,
1321
1322 /**
1323 * \name Lowered floating point packing operations.
1324 *
1325 * \see lower_packing_builtins_visitor::split_pack_half_2x16
1326 */
1327 /*@{*/
1328 ir_binop_pack_half_2x16_split,
1329 /*@}*/
1330
1331 /**
1332 * \name First half of a lowered bitfieldInsert() operation.
1333 *
1334 * \see lower_instructions::bitfield_insert_to_bfm_bfi
1335 */
1336 /*@{*/
1337 ir_binop_bfm,
1338 /*@}*/
1339
1340 /**
1341 * Load a value the size of a given GLSL type from a uniform block.
1342 *
1343 * operand0 is the ir_constant uniform block index in the linked shader.
1344 * operand1 is a byte offset within the uniform block.
1345 */
1346 ir_binop_ubo_load,
1347
1348 /**
1349 * \name Multiplies a number by two to a power, part of ARB_gpu_shader5.
1350 */
1351 /*@{*/
1352 ir_binop_ldexp,
1353 /*@}*/
1354
1355 /**
1356 * Extract a scalar from a vector
1357 *
1358 * operand0 is the vector
1359 * operand1 is the index of the field to read from operand0
1360 */
1361 ir_binop_vector_extract,
1362
1363 /**
1364 * A sentinel marking the last of the binary operations.
1365 */
1366 ir_last_binop = ir_binop_vector_extract,
1367
1368 /**
1369 * \name Fused floating-point multiply-add, part of ARB_gpu_shader5.
1370 */
1371 /*@{*/
1372 ir_triop_fma,
1373 /*@}*/
1374
1375 ir_triop_lrp,
1376
1377 /**
1378 * \name Conditional Select
1379 *
1380 * A vector conditional select instruction (like ?:, but operating per-
1381 * component on vectors).
1382 *
1383 * \see lower_instructions_visitor::ldexp_to_arith
1384 */
1385 /*@{*/
1386 ir_triop_csel,
1387 /*@}*/
1388
1389 /**
1390 * \name Second half of a lowered bitfieldInsert() operation.
1391 *
1392 * \see lower_instructions::bitfield_insert_to_bfm_bfi
1393 */
1394 /*@{*/
1395 ir_triop_bfi,
1396 /*@}*/
1397
1398 ir_triop_bitfield_extract,
1399
1400 /**
1401 * Generate a value with one field of a vector changed
1402 *
1403 * operand0 is the vector
1404 * operand1 is the value to write into the vector result
1405 * operand2 is the index in operand0 to be modified
1406 */
1407 ir_triop_vector_insert,
1408
1409 /**
1410 * A sentinel marking the last of the ternary operations.
1411 */
1412 ir_last_triop = ir_triop_vector_insert,
1413
1414 ir_quadop_bitfield_insert,
1415
1416 ir_quadop_vector,
1417
1418 /**
1419 * A sentinel marking the last of the ternary operations.
1420 */
1421 ir_last_quadop = ir_quadop_vector,
1422
1423 /**
1424 * A sentinel marking the last of all operations.
1425 */
1426 ir_last_opcode = ir_quadop_vector
1427 };
1428
1429 class ir_expression : public ir_rvalue {
1430 public:
1431 ir_expression(int op, const struct glsl_type *type,
1432 ir_rvalue *op0, ir_rvalue *op1 = NULL,
1433 ir_rvalue *op2 = NULL, ir_rvalue *op3 = NULL);
1434
1435 /**
1436 * Constructor for unary operation expressions
1437 */
1438 ir_expression(int op, ir_rvalue *);
1439
1440 /**
1441 * Constructor for binary operation expressions
1442 */
1443 ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
1444
1445 /**
1446 * Constructor for ternary operation expressions
1447 */
1448 ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1, ir_rvalue *op2);
1449
1450 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
1451
1452 virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
1453
1454 /**
1455 * Attempt to constant-fold the expression
1456 *
1457 * The "variable_context" hash table links ir_variable * to ir_constant *
1458 * that represent the variables' values. \c NULL represents an empty
1459 * context.
1460 *
1461 * If the expression cannot be constant folded, this method will return
1462 * \c NULL.
1463 */
1464 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1465
1466 /**
1467 * Determine the number of operands used by an expression
1468 */
1469 static unsigned int get_num_operands(ir_expression_operation);
1470
1471 /**
1472 * Determine the number of operands used by an expression
1473 */
1474 unsigned int get_num_operands() const
1475 {
1476 return (this->operation == ir_quadop_vector)
1477 ? this->type->vector_elements : get_num_operands(operation);
1478 }
1479
1480 /**
1481 * Return whether the expression operates on vectors horizontally.
1482 */
1483 bool is_horizontal() const
1484 {
1485 return operation == ir_binop_all_equal ||
1486 operation == ir_binop_any_nequal ||
1487 operation == ir_unop_any ||
1488 operation == ir_binop_dot ||
1489 operation == ir_quadop_vector;
1490 }
1491
1492 /**
1493 * Return a string representing this expression's operator.
1494 */
1495 const char *operator_string();
1496
1497 /**
1498 * Return a string representing this expression's operator.
1499 */
1500 static const char *operator_string(ir_expression_operation);
1501
1502
1503 /**
1504 * Do a reverse-lookup to translate the given string into an operator.
1505 */
1506 static ir_expression_operation get_operator(const char *);
1507
1508 virtual void accept(ir_visitor *v)
1509 {
1510 v->visit(this);
1511 }
1512
1513 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1514
1515 ir_expression_operation operation;
1516 ir_rvalue *operands[4];
1517 };
1518
1519
1520 /**
1521 * HIR instruction representing a high-level function call, containing a list
1522 * of parameters and returning a value in the supplied temporary.
1523 */
1524 class ir_call : public ir_instruction {
1525 public:
1526 ir_call(ir_function_signature *callee,
1527 ir_dereference_variable *return_deref,
1528 exec_list *actual_parameters)
1529 : ir_instruction(ir_type_call), return_deref(return_deref), callee(callee)
1530 {
1531 assert(callee->return_type != NULL);
1532 actual_parameters->move_nodes_to(& this->actual_parameters);
1533 this->use_builtin = callee->is_builtin();
1534 }
1535
1536 virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
1537
1538 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1539
1540 virtual void accept(ir_visitor *v)
1541 {
1542 v->visit(this);
1543 }
1544
1545 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1546
1547 /**
1548 * Get the name of the function being called.
1549 */
1550 const char *callee_name() const
1551 {
1552 return callee->function_name();
1553 }
1554
1555 /**
1556 * Generates an inline version of the function before @ir,
1557 * storing the return value in return_deref.
1558 */
1559 void generate_inline(ir_instruction *ir);
1560
1561 /**
1562 * Storage for the function's return value.
1563 * This must be NULL if the return type is void.
1564 */
1565 ir_dereference_variable *return_deref;
1566
1567 /**
1568 * The specific function signature being called.
1569 */
1570 ir_function_signature *callee;
1571
1572 /* List of ir_rvalue of paramaters passed in this call. */
1573 exec_list actual_parameters;
1574
1575 /** Should this call only bind to a built-in function? */
1576 bool use_builtin;
1577 };
1578
1579
1580 /**
1581 * \name Jump-like IR instructions.
1582 *
1583 * These include \c break, \c continue, \c return, and \c discard.
1584 */
1585 /*@{*/
1586 class ir_jump : public ir_instruction {
1587 protected:
1588 ir_jump(enum ir_node_type t)
1589 : ir_instruction(t)
1590 {
1591 }
1592 };
1593
1594 class ir_return : public ir_jump {
1595 public:
1596 ir_return()
1597 : ir_jump(ir_type_return), value(NULL)
1598 {
1599 }
1600
1601 ir_return(ir_rvalue *value)
1602 : ir_jump(ir_type_return), value(value)
1603 {
1604 }
1605
1606 virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
1607
1608 ir_rvalue *get_value() const
1609 {
1610 return value;
1611 }
1612
1613 virtual void accept(ir_visitor *v)
1614 {
1615 v->visit(this);
1616 }
1617
1618 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1619
1620 ir_rvalue *value;
1621 };
1622
1623
1624 /**
1625 * Jump instructions used inside loops
1626 *
1627 * These include \c break and \c continue. The \c break within a loop is
1628 * different from the \c break within a switch-statement.
1629 *
1630 * \sa ir_switch_jump
1631 */
1632 class ir_loop_jump : public ir_jump {
1633 public:
1634 enum jump_mode {
1635 jump_break,
1636 jump_continue
1637 };
1638
1639 ir_loop_jump(jump_mode mode)
1640 : ir_jump(ir_type_loop_jump)
1641 {
1642 this->mode = mode;
1643 }
1644
1645 virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
1646
1647 virtual void accept(ir_visitor *v)
1648 {
1649 v->visit(this);
1650 }
1651
1652 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1653
1654 bool is_break() const
1655 {
1656 return mode == jump_break;
1657 }
1658
1659 bool is_continue() const
1660 {
1661 return mode == jump_continue;
1662 }
1663
1664 /** Mode selector for the jump instruction. */
1665 enum jump_mode mode;
1666 };
1667
1668 /**
1669 * IR instruction representing discard statements.
1670 */
1671 class ir_discard : public ir_jump {
1672 public:
1673 ir_discard()
1674 : ir_jump(ir_type_discard)
1675 {
1676 this->condition = NULL;
1677 }
1678
1679 ir_discard(ir_rvalue *cond)
1680 : ir_jump(ir_type_discard)
1681 {
1682 this->condition = cond;
1683 }
1684
1685 virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
1686
1687 virtual void accept(ir_visitor *v)
1688 {
1689 v->visit(this);
1690 }
1691
1692 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1693
1694 ir_rvalue *condition;
1695 };
1696 /*@}*/
1697
1698
1699 /**
1700 * Texture sampling opcodes used in ir_texture
1701 */
1702 enum ir_texture_opcode {
1703 ir_tex, /**< Regular texture look-up */
1704 ir_txb, /**< Texture look-up with LOD bias */
1705 ir_txl, /**< Texture look-up with explicit LOD */
1706 ir_txd, /**< Texture look-up with partial derivatvies */
1707 ir_txf, /**< Texel fetch with explicit LOD */
1708 ir_txf_ms, /**< Multisample texture fetch */
1709 ir_txs, /**< Texture size */
1710 ir_lod, /**< Texture lod query */
1711 ir_tg4, /**< Texture gather */
1712 ir_query_levels /**< Texture levels query */
1713 };
1714
1715
1716 /**
1717 * IR instruction to sample a texture
1718 *
1719 * The specific form of the IR instruction depends on the \c mode value
1720 * selected from \c ir_texture_opcodes. In the printed IR, these will
1721 * appear as:
1722 *
1723 * Texel offset (0 or an expression)
1724 * | Projection divisor
1725 * | | Shadow comparitor
1726 * | | |
1727 * v v v
1728 * (tex <type> <sampler> <coordinate> 0 1 ( ))
1729 * (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)
1730 * (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)
1731 * (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))
1732 * (txf <type> <sampler> <coordinate> 0 <lod>)
1733 * (txf_ms
1734 * <type> <sampler> <coordinate> <sample_index>)
1735 * (txs <type> <sampler> <lod>)
1736 * (lod <type> <sampler> <coordinate>)
1737 * (tg4 <type> <sampler> <coordinate> <offset> <component>)
1738 * (query_levels <type> <sampler>)
1739 */
1740 class ir_texture : public ir_rvalue {
1741 public:
1742 ir_texture(enum ir_texture_opcode op)
1743 : ir_rvalue(ir_type_texture),
1744 op(op), sampler(NULL), coordinate(NULL), projector(NULL),
1745 shadow_comparitor(NULL), offset(NULL)
1746 {
1747 memset(&lod_info, 0, sizeof(lod_info));
1748 }
1749
1750 virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
1751
1752 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1753
1754 virtual void accept(ir_visitor *v)
1755 {
1756 v->visit(this);
1757 }
1758
1759 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1760
1761 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
1762
1763 /**
1764 * Return a string representing the ir_texture_opcode.
1765 */
1766 const char *opcode_string();
1767
1768 /** Set the sampler and type. */
1769 void set_sampler(ir_dereference *sampler, const glsl_type *type);
1770
1771 /**
1772 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1773 */
1774 static ir_texture_opcode get_opcode(const char *);
1775
1776 enum ir_texture_opcode op;
1777
1778 /** Sampler to use for the texture access. */
1779 ir_dereference *sampler;
1780
1781 /** Texture coordinate to sample */
1782 ir_rvalue *coordinate;
1783
1784 /**
1785 * Value used for projective divide.
1786 *
1787 * If there is no projective divide (the common case), this will be
1788 * \c NULL. Optimization passes should check for this to point to a constant
1789 * of 1.0 and replace that with \c NULL.
1790 */
1791 ir_rvalue *projector;
1792
1793 /**
1794 * Coordinate used for comparison on shadow look-ups.
1795 *
1796 * If there is no shadow comparison, this will be \c NULL. For the
1797 * \c ir_txf opcode, this *must* be \c NULL.
1798 */
1799 ir_rvalue *shadow_comparitor;
1800
1801 /** Texel offset. */
1802 ir_rvalue *offset;
1803
1804 union {
1805 ir_rvalue *lod; /**< Floating point LOD */
1806 ir_rvalue *bias; /**< Floating point LOD bias */
1807 ir_rvalue *sample_index; /**< MSAA sample index */
1808 ir_rvalue *component; /**< Gather component selector */
1809 struct {
1810 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
1811 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
1812 } grad;
1813 } lod_info;
1814 };
1815
1816
1817 struct ir_swizzle_mask {
1818 unsigned x:2;
1819 unsigned y:2;
1820 unsigned z:2;
1821 unsigned w:2;
1822
1823 /**
1824 * Number of components in the swizzle.
1825 */
1826 unsigned num_components:3;
1827
1828 /**
1829 * Does the swizzle contain duplicate components?
1830 *
1831 * L-value swizzles cannot contain duplicate components.
1832 */
1833 unsigned has_duplicates:1;
1834 };
1835
1836
1837 class ir_swizzle : public ir_rvalue {
1838 public:
1839 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1840 unsigned count);
1841
1842 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1843
1844 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1845
1846 virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1847
1848 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1849
1850 /**
1851 * Construct an ir_swizzle from the textual representation. Can fail.
1852 */
1853 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1854
1855 virtual void accept(ir_visitor *v)
1856 {
1857 v->visit(this);
1858 }
1859
1860 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1861
1862 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
1863
1864 bool is_lvalue() const
1865 {
1866 return val->is_lvalue() && !mask.has_duplicates;
1867 }
1868
1869 /**
1870 * Get the variable that is ultimately referenced by an r-value
1871 */
1872 virtual ir_variable *variable_referenced() const;
1873
1874 ir_rvalue *val;
1875 ir_swizzle_mask mask;
1876
1877 private:
1878 /**
1879 * Initialize the mask component of a swizzle
1880 *
1881 * This is used by the \c ir_swizzle constructors.
1882 */
1883 void init_mask(const unsigned *components, unsigned count);
1884 };
1885
1886
1887 class ir_dereference : public ir_rvalue {
1888 public:
1889 virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
1890
1891 bool is_lvalue() const;
1892
1893 /**
1894 * Get the variable that is ultimately referenced by an r-value
1895 */
1896 virtual ir_variable *variable_referenced() const = 0;
1897
1898 protected:
1899 ir_dereference(enum ir_node_type t)
1900 : ir_rvalue(t)
1901 {
1902 }
1903 };
1904
1905
1906 class ir_dereference_variable : public ir_dereference {
1907 public:
1908 ir_dereference_variable(ir_variable *var);
1909
1910 virtual ir_dereference_variable *clone(void *mem_ctx,
1911 struct hash_table *) const;
1912
1913 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1914
1915 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
1916
1917 /**
1918 * Get the variable that is ultimately referenced by an r-value
1919 */
1920 virtual ir_variable *variable_referenced() const
1921 {
1922 return this->var;
1923 }
1924
1925 virtual ir_variable *whole_variable_referenced()
1926 {
1927 /* ir_dereference_variable objects always dereference the entire
1928 * variable. However, if this dereference is dereferenced by anything
1929 * else, the complete deferefernce chain is not a whole-variable
1930 * dereference. This method should only be called on the top most
1931 * ir_rvalue in a dereference chain.
1932 */
1933 return this->var;
1934 }
1935
1936 virtual void accept(ir_visitor *v)
1937 {
1938 v->visit(this);
1939 }
1940
1941 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1942
1943 /**
1944 * Object being dereferenced.
1945 */
1946 ir_variable *var;
1947 };
1948
1949
1950 class ir_dereference_array : public ir_dereference {
1951 public:
1952 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1953
1954 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1955
1956 virtual ir_dereference_array *clone(void *mem_ctx,
1957 struct hash_table *) const;
1958
1959 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1960
1961 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
1962
1963 /**
1964 * Get the variable that is ultimately referenced by an r-value
1965 */
1966 virtual ir_variable *variable_referenced() const
1967 {
1968 return this->array->variable_referenced();
1969 }
1970
1971 virtual void accept(ir_visitor *v)
1972 {
1973 v->visit(this);
1974 }
1975
1976 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1977
1978 ir_rvalue *array;
1979 ir_rvalue *array_index;
1980
1981 private:
1982 void set_array(ir_rvalue *value);
1983 };
1984
1985
1986 class ir_dereference_record : public ir_dereference {
1987 public:
1988 ir_dereference_record(ir_rvalue *value, const char *field);
1989
1990 ir_dereference_record(ir_variable *var, const char *field);
1991
1992 virtual ir_dereference_record *clone(void *mem_ctx,
1993 struct hash_table *) const;
1994
1995 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
1996
1997 /**
1998 * Get the variable that is ultimately referenced by an r-value
1999 */
2000 virtual ir_variable *variable_referenced() const
2001 {
2002 return this->record->variable_referenced();
2003 }
2004
2005 virtual void accept(ir_visitor *v)
2006 {
2007 v->visit(this);
2008 }
2009
2010 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2011
2012 ir_rvalue *record;
2013 const char *field;
2014 };
2015
2016
2017 /**
2018 * Data stored in an ir_constant
2019 */
2020 union ir_constant_data {
2021 unsigned u[16];
2022 int i[16];
2023 float f[16];
2024 bool b[16];
2025 };
2026
2027
2028 class ir_constant : public ir_rvalue {
2029 public:
2030 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
2031 ir_constant(bool b, unsigned vector_elements=1);
2032 ir_constant(unsigned int u, unsigned vector_elements=1);
2033 ir_constant(int i, unsigned vector_elements=1);
2034 ir_constant(float f, unsigned vector_elements=1);
2035
2036 /**
2037 * Construct an ir_constant from a list of ir_constant values
2038 */
2039 ir_constant(const struct glsl_type *type, exec_list *values);
2040
2041 /**
2042 * Construct an ir_constant from a scalar component of another ir_constant
2043 *
2044 * The new \c ir_constant inherits the type of the component from the
2045 * source constant.
2046 *
2047 * \note
2048 * In the case of a matrix constant, the new constant is a scalar, \b not
2049 * a vector.
2050 */
2051 ir_constant(const ir_constant *c, unsigned i);
2052
2053 /**
2054 * Return a new ir_constant of the specified type containing all zeros.
2055 */
2056 static ir_constant *zero(void *mem_ctx, const glsl_type *type);
2057
2058 virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
2059
2060 virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
2061
2062 virtual void accept(ir_visitor *v)
2063 {
2064 v->visit(this);
2065 }
2066
2067 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2068
2069 virtual bool equals(ir_instruction *ir, enum ir_node_type ignore = ir_type_unset);
2070
2071 /**
2072 * Get a particular component of a constant as a specific type
2073 *
2074 * This is useful, for example, to get a value from an integer constant
2075 * as a float or bool. This appears frequently when constructors are
2076 * called with all constant parameters.
2077 */
2078 /*@{*/
2079 bool get_bool_component(unsigned i) const;
2080 float get_float_component(unsigned i) const;
2081 int get_int_component(unsigned i) const;
2082 unsigned get_uint_component(unsigned i) const;
2083 /*@}*/
2084
2085 ir_constant *get_array_element(unsigned i) const;
2086
2087 ir_constant *get_record_field(const char *name);
2088
2089 /**
2090 * Copy the values on another constant at a given offset.
2091 *
2092 * The offset is ignored for array or struct copies, it's only for
2093 * scalars or vectors into vectors or matrices.
2094 *
2095 * With identical types on both sides and zero offset it's clone()
2096 * without creating a new object.
2097 */
2098
2099 void copy_offset(ir_constant *src, int offset);
2100
2101 /**
2102 * Copy the values on another constant at a given offset and
2103 * following an assign-like mask.
2104 *
2105 * The mask is ignored for scalars.
2106 *
2107 * Note that this function only handles what assign can handle,
2108 * i.e. at most a vector as source and a column of a matrix as
2109 * destination.
2110 */
2111
2112 void copy_masked_offset(ir_constant *src, int offset, unsigned int mask);
2113
2114 /**
2115 * Determine whether a constant has the same value as another constant
2116 *
2117 * \sa ir_constant::is_zero, ir_constant::is_one,
2118 * ir_constant::is_negative_one, ir_constant::is_basis
2119 */
2120 bool has_value(const ir_constant *) const;
2121
2122 /**
2123 * Return true if this ir_constant represents the given value.
2124 *
2125 * For vectors, this checks that each component is the given value.
2126 */
2127 virtual bool is_value(float f, int i) const;
2128 virtual bool is_zero() const;
2129 virtual bool is_one() const;
2130 virtual bool is_negative_one() const;
2131 virtual bool is_basis() const;
2132
2133 /**
2134 * Return true for constants that could be stored as 16-bit unsigned values.
2135 *
2136 * Note that this will return true even for signed integer ir_constants, as
2137 * long as the value is non-negative and fits in 16-bits.
2138 */
2139 virtual bool is_uint16_constant() const;
2140
2141 /**
2142 * Value of the constant.
2143 *
2144 * The field used to back the values supplied by the constant is determined
2145 * by the type associated with the \c ir_instruction. Constants may be
2146 * scalars, vectors, or matrices.
2147 */
2148 union ir_constant_data value;
2149
2150 /* Array elements */
2151 ir_constant **array_elements;
2152
2153 /* Structure fields */
2154 exec_list components;
2155
2156 private:
2157 /**
2158 * Parameterless constructor only used by the clone method
2159 */
2160 ir_constant(void);
2161 };
2162
2163 /**
2164 * IR instruction to emit a vertex in a geometry shader.
2165 */
2166 class ir_emit_vertex : public ir_instruction {
2167 public:
2168 ir_emit_vertex(ir_rvalue *stream)
2169 : ir_instruction(ir_type_emit_vertex),
2170 stream(stream)
2171 {
2172 assert(stream);
2173 }
2174
2175 virtual void accept(ir_visitor *v)
2176 {
2177 v->visit(this);
2178 }
2179
2180 virtual ir_emit_vertex *clone(void *mem_ctx, struct hash_table *ht) const
2181 {
2182 return new(mem_ctx) ir_emit_vertex(this->stream->clone(mem_ctx, ht));
2183 }
2184
2185 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2186
2187 int stream_id() const
2188 {
2189 return stream->as_constant()->value.i[0];
2190 }
2191
2192 ir_rvalue *stream;
2193 };
2194
2195 /**
2196 * IR instruction to complete the current primitive and start a new one in a
2197 * geometry shader.
2198 */
2199 class ir_end_primitive : public ir_instruction {
2200 public:
2201 ir_end_primitive(ir_rvalue *stream)
2202 : ir_instruction(ir_type_end_primitive),
2203 stream(stream)
2204 {
2205 assert(stream);
2206 }
2207
2208 virtual void accept(ir_visitor *v)
2209 {
2210 v->visit(this);
2211 }
2212
2213 virtual ir_end_primitive *clone(void *mem_ctx, struct hash_table *ht) const
2214 {
2215 return new(mem_ctx) ir_end_primitive(this->stream->clone(mem_ctx, ht));
2216 }
2217
2218 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2219
2220 int stream_id() const
2221 {
2222 return stream->as_constant()->value.i[0];
2223 }
2224
2225 ir_rvalue *stream;
2226 };
2227
2228 /*@}*/
2229
2230 /**
2231 * Apply a visitor to each IR node in a list
2232 */
2233 void
2234 visit_exec_list(exec_list *list, ir_visitor *visitor);
2235
2236 /**
2237 * Validate invariants on each IR node in a list
2238 */
2239 void validate_ir_tree(exec_list *instructions);
2240
2241 struct _mesa_glsl_parse_state;
2242 struct gl_shader_program;
2243
2244 /**
2245 * Detect whether an unlinked shader contains static recursion
2246 *
2247 * If the list of instructions is determined to contain static recursion,
2248 * \c _mesa_glsl_error will be called to emit error messages for each function
2249 * that is in the recursion cycle.
2250 */
2251 void
2252 detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
2253 exec_list *instructions);
2254
2255 /**
2256 * Detect whether a linked shader contains static recursion
2257 *
2258 * If the list of instructions is determined to contain static recursion,
2259 * \c link_error_printf will be called to emit error messages for each function
2260 * that is in the recursion cycle. In addition,
2261 * \c gl_shader_program::LinkStatus will be set to false.
2262 */
2263 void
2264 detect_recursion_linked(struct gl_shader_program *prog,
2265 exec_list *instructions);
2266
2267 /**
2268 * Make a clone of each IR instruction in a list
2269 *
2270 * \param in List of IR instructions that are to be cloned
2271 * \param out List to hold the cloned instructions
2272 */
2273 void
2274 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
2275
2276 extern void
2277 _mesa_glsl_initialize_variables(exec_list *instructions,
2278 struct _mesa_glsl_parse_state *state);
2279
2280 extern void
2281 _mesa_glsl_initialize_functions(_mesa_glsl_parse_state *state);
2282
2283 extern void
2284 _mesa_glsl_initialize_builtin_functions();
2285
2286 extern ir_function_signature *
2287 _mesa_glsl_find_builtin_function(_mesa_glsl_parse_state *state,
2288 const char *name, exec_list *actual_parameters);
2289
2290 extern gl_shader *
2291 _mesa_glsl_get_builtin_function_shader(void);
2292
2293 extern void
2294 _mesa_glsl_release_functions(void);
2295
2296 extern void
2297 _mesa_glsl_release_builtin_functions(void);
2298
2299 extern void
2300 reparent_ir(exec_list *list, void *mem_ctx);
2301
2302 struct glsl_symbol_table;
2303
2304 extern void
2305 import_prototypes(const exec_list *source, exec_list *dest,
2306 struct glsl_symbol_table *symbols, void *mem_ctx);
2307
2308 extern bool
2309 ir_has_call(ir_instruction *ir);
2310
2311 extern void
2312 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
2313 gl_shader_stage shader_stage);
2314
2315 extern char *
2316 prototype_string(const glsl_type *return_type, const char *name,
2317 exec_list *parameters);
2318
2319 const char *
2320 mode_string(const ir_variable *var);
2321
2322 /**
2323 * Built-in / reserved GL variables names start with "gl_"
2324 */
2325 static inline bool
2326 is_gl_identifier(const char *s)
2327 {
2328 return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';
2329 }
2330
2331 extern "C" {
2332 #endif /* __cplusplus */
2333
2334 extern void _mesa_print_ir(FILE *f, struct exec_list *instructions,
2335 struct _mesa_glsl_parse_state *state);
2336
2337 extern void
2338 fprint_ir(FILE *f, const void *instruction);
2339
2340 #ifdef __cplusplus
2341 } /* extern "C" */
2342 #endif
2343
2344 unsigned
2345 vertices_per_prim(GLenum prim);
2346
2347 #endif /* IR_H */