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