9d207bd425527eadc913f105ad7834696a5baa19
[mesa.git] / src / glsl / nir / nir.h
1 /*
2 * Copyright © 2014 Connor Abbott
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #pragma once
29
30 #include "util/hash_table.h"
31 #include "main/set.h"
32 #include "../list.h"
33 #include "GL/gl.h" /* GLenum */
34 #include "util/ralloc.h"
35 #include "main/mtypes.h"
36 #include "main/bitset.h"
37 #include "nir_types.h"
38 #include <stdio.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #define NIR_FALSE 0u
45 #define NIR_TRUE (~0u)
46
47 struct nir_function_overload;
48 struct nir_function;
49 struct nir_shader;
50
51
52 /**
53 * Description of built-in state associated with a uniform
54 *
55 * \sa nir_variable::state_slots
56 */
57 typedef struct {
58 int tokens[5];
59 int swizzle;
60 } nir_state_slot;
61
62 typedef enum {
63 nir_var_shader_in,
64 nir_var_shader_out,
65 nir_var_global,
66 nir_var_local,
67 nir_var_uniform,
68 nir_var_system_value
69 } nir_variable_mode;
70
71 /**
72 * Data stored in an nir_constant
73 */
74 union nir_constant_data {
75 unsigned u[16];
76 int i[16];
77 float f[16];
78 bool b[16];
79 };
80
81 typedef struct nir_constant {
82 /**
83 * Value of the constant.
84 *
85 * The field used to back the values supplied by the constant is determined
86 * by the type associated with the \c ir_instruction. Constants may be
87 * scalars, vectors, or matrices.
88 */
89 union nir_constant_data value;
90
91 /* Array elements / Structure Fields */
92 struct nir_constant **elements;
93 } nir_constant;
94
95 /**
96 * \brief Layout qualifiers for gl_FragDepth.
97 *
98 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
99 * with a layout qualifier.
100 */
101 typedef enum {
102 nir_depth_layout_none, /**< No depth layout is specified. */
103 nir_depth_layout_any,
104 nir_depth_layout_greater,
105 nir_depth_layout_less,
106 nir_depth_layout_unchanged
107 } nir_depth_layout;
108
109 /**
110 * Either a uniform, global variable, shader input, or shader output. Based on
111 * ir_variable - it should be easy to translate between the two.
112 */
113
114 typedef struct {
115 struct exec_node node;
116
117 /**
118 * Declared type of the variable
119 */
120 const struct glsl_type *type;
121
122 /**
123 * Declared name of the variable
124 */
125 char *name;
126
127 /**
128 * For variables which satisfy the is_interface_instance() predicate, this
129 * points to an array of integers such that if the ith member of the
130 * interface block is an array, max_ifc_array_access[i] is the maximum
131 * array element of that member that has been accessed. If the ith member
132 * of the interface block is not an array, max_ifc_array_access[i] is
133 * unused.
134 *
135 * For variables whose type is not an interface block, this pointer is
136 * NULL.
137 */
138 unsigned *max_ifc_array_access;
139
140 struct nir_variable_data {
141
142 /**
143 * Is the variable read-only?
144 *
145 * This is set for variables declared as \c const, shader inputs,
146 * and uniforms.
147 */
148 unsigned read_only:1;
149 unsigned centroid:1;
150 unsigned sample:1;
151 unsigned invariant:1;
152
153 /**
154 * Storage class of the variable.
155 *
156 * \sa nir_variable_mode
157 */
158 nir_variable_mode mode:4;
159
160 /**
161 * Interpolation mode for shader inputs / outputs
162 *
163 * \sa ir_variable_interpolation
164 */
165 unsigned interpolation:2;
166
167 /**
168 * \name ARB_fragment_coord_conventions
169 * @{
170 */
171 unsigned origin_upper_left:1;
172 unsigned pixel_center_integer:1;
173 /*@}*/
174
175 /**
176 * Was the location explicitly set in the shader?
177 *
178 * If the location is explicitly set in the shader, it \b cannot be changed
179 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
180 * no effect).
181 */
182 unsigned explicit_location:1;
183 unsigned explicit_index:1;
184
185 /**
186 * Was an initial binding explicitly set in the shader?
187 *
188 * If so, constant_value contains an integer ir_constant representing the
189 * initial binding point.
190 */
191 unsigned explicit_binding:1;
192
193 /**
194 * Does this variable have an initializer?
195 *
196 * This is used by the linker to cross-validiate initializers of global
197 * variables.
198 */
199 unsigned has_initializer:1;
200
201 /**
202 * Is this variable a generic output or input that has not yet been matched
203 * up to a variable in another stage of the pipeline?
204 *
205 * This is used by the linker as scratch storage while assigning locations
206 * to generic inputs and outputs.
207 */
208 unsigned is_unmatched_generic_inout:1;
209
210 /**
211 * If non-zero, then this variable may be packed along with other variables
212 * into a single varying slot, so this offset should be applied when
213 * accessing components. For example, an offset of 1 means that the x
214 * component of this variable is actually stored in component y of the
215 * location specified by \c location.
216 */
217 unsigned location_frac:2;
218
219 /**
220 * Non-zero if this variable was created by lowering a named interface
221 * block which was not an array.
222 *
223 * Note that this variable and \c from_named_ifc_block_array will never
224 * both be non-zero.
225 */
226 unsigned from_named_ifc_block_nonarray:1;
227
228 /**
229 * Non-zero if this variable was created by lowering a named interface
230 * block which was an array.
231 *
232 * Note that this variable and \c from_named_ifc_block_nonarray will never
233 * both be non-zero.
234 */
235 unsigned from_named_ifc_block_array:1;
236
237 /**
238 * \brief Layout qualifier for gl_FragDepth.
239 *
240 * This is not equal to \c ir_depth_layout_none if and only if this
241 * variable is \c gl_FragDepth and a layout qualifier is specified.
242 */
243 nir_depth_layout depth_layout;
244
245 /**
246 * Storage location of the base of this variable
247 *
248 * The precise meaning of this field depends on the nature of the variable.
249 *
250 * - Vertex shader input: one of the values from \c gl_vert_attrib.
251 * - Vertex shader output: one of the values from \c gl_varying_slot.
252 * - Geometry shader input: one of the values from \c gl_varying_slot.
253 * - Geometry shader output: one of the values from \c gl_varying_slot.
254 * - Fragment shader input: one of the values from \c gl_varying_slot.
255 * - Fragment shader output: one of the values from \c gl_frag_result.
256 * - Uniforms: Per-stage uniform slot number for default uniform block.
257 * - Uniforms: Index within the uniform block definition for UBO members.
258 * - Other: This field is not currently used.
259 *
260 * If the variable is a uniform, shader input, or shader output, and the
261 * slot has not been assigned, the value will be -1.
262 */
263 int location;
264
265 /**
266 * The actual location of the variable in the IR. Only valid for inputs
267 * and outputs.
268 */
269 unsigned int driver_location;
270
271 /**
272 * output index for dual source blending.
273 */
274 int index;
275
276 /**
277 * Initial binding point for a sampler or UBO.
278 *
279 * For array types, this represents the binding point for the first element.
280 */
281 int binding;
282
283 /**
284 * Location an atomic counter is stored at.
285 */
286 struct {
287 unsigned buffer_index;
288 unsigned offset;
289 } atomic;
290
291 /**
292 * ARB_shader_image_load_store qualifiers.
293 */
294 struct {
295 bool read_only; /**< "readonly" qualifier. */
296 bool write_only; /**< "writeonly" qualifier. */
297 bool coherent;
298 bool _volatile;
299 bool restrict_flag;
300
301 /** Image internal format if specified explicitly, otherwise GL_NONE. */
302 GLenum format;
303 } image;
304
305 /**
306 * Highest element accessed with a constant expression array index
307 *
308 * Not used for non-array variables.
309 */
310 unsigned max_array_access;
311
312 } data;
313
314 /**
315 * Built-in state that backs this uniform
316 *
317 * Once set at variable creation, \c state_slots must remain invariant.
318 * This is because, ideally, this array would be shared by all clones of
319 * this variable in the IR tree. In other words, we'd really like for it
320 * to be a fly-weight.
321 *
322 * If the variable is not a uniform, \c num_state_slots will be zero and
323 * \c state_slots will be \c NULL.
324 */
325 /*@{*/
326 unsigned num_state_slots; /**< Number of state slots used */
327 nir_state_slot *state_slots; /**< State descriptors. */
328 /*@}*/
329
330 /**
331 * Value assigned in the initializer of a variable declared "const"
332 */
333 nir_constant *constant_value;
334
335 /**
336 * Constant expression assigned in the initializer of the variable
337 *
338 * \warning
339 * This field and \c ::constant_value are distinct. Even if the two fields
340 * refer to constants with the same value, they must point to separate
341 * objects.
342 */
343 nir_constant *constant_initializer;
344
345 /**
346 * For variables that are in an interface block or are an instance of an
347 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
348 *
349 * \sa ir_variable::location
350 */
351 const struct glsl_type *interface_type;
352 } nir_variable;
353
354 typedef struct {
355 struct exec_node node;
356
357 unsigned num_components; /** < number of vector components */
358 unsigned num_array_elems; /** < size of array (0 for no array) */
359
360 /** generic register index. */
361 unsigned index;
362
363 /** only for debug purposes, can be NULL */
364 const char *name;
365
366 /** whether this register is local (per-function) or global (per-shader) */
367 bool is_global;
368
369 /**
370 * If this flag is set to true, then accessing channels >= num_components
371 * is well-defined, and simply spills over to the next array element. This
372 * is useful for backends that can do per-component accessing, in
373 * particular scalar backends. By setting this flag and making
374 * num_components equal to 1, structures can be packed tightly into
375 * registers and then registers can be accessed per-component to get to
376 * each structure member, even if it crosses vec4 boundaries.
377 */
378 bool is_packed;
379
380 /** set of nir_instr's where this register is used (read from) */
381 struct set *uses;
382
383 /** set of nir_instr's where this register is defined (written to) */
384 struct set *defs;
385
386 /** set of ifs where this register is used as a condition */
387 struct set *if_uses;
388 } nir_register;
389
390 typedef enum {
391 nir_instr_type_alu,
392 nir_instr_type_call,
393 nir_instr_type_texture,
394 nir_instr_type_intrinsic,
395 nir_instr_type_load_const,
396 nir_instr_type_jump,
397 nir_instr_type_ssa_undef,
398 nir_instr_type_phi,
399 nir_instr_type_parallel_copy,
400 } nir_instr_type;
401
402 typedef struct {
403 struct exec_node node;
404 nir_instr_type type;
405 struct nir_block *block;
406
407 /* flag for dead code elimination (see nir_opt_dce.c) */
408 bool live;
409 } nir_instr;
410
411 #define nir_instr_next(instr) \
412 exec_node_data(nir_instr, (instr)->node.next, node)
413
414 #define nir_instr_prev(instr) \
415 exec_node_data(nir_instr, (instr)->node.prev, node)
416
417 typedef struct {
418 /** for debugging only, can be NULL */
419 const char* name;
420
421 /** generic SSA definition index. */
422 unsigned index;
423
424 /** Index into the live_in and live_out bitfields */
425 unsigned live_index;
426
427 nir_instr *parent_instr;
428
429 struct set *uses;
430 struct set *if_uses;
431
432 uint8_t num_components;
433 } nir_ssa_def;
434
435 struct nir_src;
436
437 typedef struct {
438 nir_register *reg;
439 struct nir_src *indirect; /** < NULL for no indirect offset */
440 unsigned base_offset;
441
442 /* TODO use-def chain goes here */
443 } nir_reg_src;
444
445 typedef struct {
446 nir_register *reg;
447 struct nir_src *indirect; /** < NULL for no indirect offset */
448 unsigned base_offset;
449
450 /* TODO def-use chain goes here */
451 } nir_reg_dest;
452
453 typedef struct nir_src {
454 union {
455 nir_reg_src reg;
456 nir_ssa_def *ssa;
457 };
458
459 bool is_ssa;
460 } nir_src;
461
462 typedef struct {
463 union {
464 nir_reg_dest reg;
465 nir_ssa_def ssa;
466 };
467
468 bool is_ssa;
469 } nir_dest;
470
471 nir_src nir_src_copy(nir_src src, void *mem_ctx);
472 nir_dest nir_dest_copy(nir_dest dest, void *mem_ctx);
473
474 typedef struct {
475 nir_src src;
476
477 /**
478 * \name input modifiers
479 */
480 /*@{*/
481 /**
482 * For inputs interpreted as a floating point, flips the sign bit. For inputs
483 * interpreted as an integer, performs the two's complement negation.
484 */
485 bool negate;
486
487 /**
488 * Clears the sign bit for floating point values, and computes the integer
489 * absolute value for integers. Note that the negate modifier acts after
490 * the absolute value modifier, therefore if both are set then all inputs
491 * will become negative.
492 */
493 bool abs;
494 /*@}*/
495
496 /**
497 * For each input component, says which component of the register it is
498 * chosen from. Note that which elements of the swizzle are used and which
499 * are ignored are based on the write mask for most opcodes - for example,
500 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
501 * a swizzle of {2, x, 1, 0} where x means "don't care."
502 */
503 uint8_t swizzle[4];
504 } nir_alu_src;
505
506 typedef struct {
507 nir_dest dest;
508
509 /**
510 * \name saturate output modifier
511 *
512 * Only valid for opcodes that output floating-point numbers. Clamps the
513 * output to between 0.0 and 1.0 inclusive.
514 */
515
516 bool saturate;
517
518 unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
519 } nir_alu_dest;
520
521 #define OPCODE(name, num_inputs, per_component, output_size, output_type, \
522 input_sizes, input_types) \
523 nir_op_##name,
524
525 #define LAST_OPCODE(name) nir_last_opcode = nir_op_##name,
526
527 typedef enum {
528 #include "nir_opcodes.h"
529 nir_num_opcodes = nir_last_opcode + 1
530 } nir_op;
531
532 #undef OPCODE
533 #undef LAST_OPCODE
534
535 typedef enum {
536 nir_type_float,
537 nir_type_int,
538 nir_type_unsigned,
539 nir_type_bool
540 } nir_alu_type;
541
542 typedef struct {
543 const char *name;
544
545 unsigned num_inputs;
546
547 /**
548 * If true, the opcode acts in the standard, per-component manner; the
549 * operation is performed on each component (except the ones that are masked
550 * out) with the input being taken from the input swizzle for that component.
551 *
552 * If false, the size of the output and inputs are explicitly given; swizzle
553 * and writemask are still in effect, but if the output component is masked
554 * out, then the input component may still be in use.
555 *
556 * The size of some of the inputs may be given (i.e. non-zero) even though
557 * per_component is false; in that case, each component of the input acts
558 * per-component, while the rest of the inputs and the output are normal.
559 * For example, for conditional select the condition is per-component but
560 * everything else is normal.
561 */
562 bool per_component;
563
564 /**
565 * If per_component is false, the number of components in the output.
566 */
567 unsigned output_size;
568
569 /**
570 * The type of vector that the instruction outputs. Note that this
571 * determines whether the saturate modifier is allowed.
572 */
573
574 nir_alu_type output_type;
575
576 /**
577 * If per_component is false, the number of components in each input.
578 */
579 unsigned input_sizes[4];
580
581 /**
582 * The type of vector that each input takes. Note that negate is only
583 * allowed on inputs with int or float type, and behaves differently on the
584 * two, and absolute value is only allowed on float type inputs.
585 */
586 nir_alu_type input_types[4];
587 } nir_op_info;
588
589 extern const nir_op_info nir_op_infos[nir_num_opcodes];
590
591 typedef struct nir_alu_instr {
592 nir_instr instr;
593 nir_op op;
594 bool has_predicate;
595 nir_src predicate;
596 nir_alu_dest dest;
597 nir_alu_src src[];
598 } nir_alu_instr;
599
600 /* is this source channel used? */
601 static inline bool
602 nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)
603 {
604 if (nir_op_infos[instr->op].input_sizes[src] > 0)
605 return channel < nir_op_infos[instr->op].input_sizes[src];
606
607 return (instr->dest.write_mask >> channel) & 1;
608 }
609
610 typedef enum {
611 nir_deref_type_var,
612 nir_deref_type_array,
613 nir_deref_type_struct
614 } nir_deref_type;
615
616 typedef struct nir_deref {
617 nir_deref_type deref_type;
618 struct nir_deref *child;
619 const struct glsl_type *type;
620 } nir_deref;
621
622 typedef struct {
623 nir_deref deref;
624
625 nir_variable *var;
626 } nir_deref_var;
627
628 /* This enum describes how the array is referenced. If the deref is
629 * direct then the base_offset is used. If the deref is indirect then then
630 * offset is given by base_offset + indirect. If the deref is a wildcard
631 * then the deref refers to all of the elements of the array at the same
632 * time. Wildcard dereferences are only ever allowed in copy_var
633 * intrinsics and the source and destination derefs must have matching
634 * wildcards.
635 */
636 typedef enum {
637 nir_deref_array_type_direct,
638 nir_deref_array_type_indirect,
639 nir_deref_array_type_wildcard,
640 } nir_deref_array_type;
641
642 typedef struct {
643 nir_deref deref;
644
645 nir_deref_array_type deref_array_type;
646 unsigned base_offset;
647 nir_src indirect;
648 } nir_deref_array;
649
650 typedef struct {
651 nir_deref deref;
652
653 unsigned index;
654 } nir_deref_struct;
655
656 #define nir_deref_as_var(_deref) exec_node_data(nir_deref_var, _deref, deref)
657 #define nir_deref_as_array(_deref) \
658 exec_node_data(nir_deref_array, _deref, deref)
659 #define nir_deref_as_struct(_deref) \
660 exec_node_data(nir_deref_struct, _deref, deref)
661
662 typedef struct {
663 nir_instr instr;
664
665 unsigned num_params;
666 nir_deref_var **params;
667 nir_deref_var *return_deref;
668
669 bool has_predicate;
670 nir_src predicate;
671
672 struct nir_function_overload *callee;
673 } nir_call_instr;
674
675 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
676 num_variables, num_indices, flags) \
677 nir_intrinsic_##name,
678
679 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
680
681 typedef enum {
682 #include "nir_intrinsics.h"
683 nir_num_intrinsics = nir_last_intrinsic + 1
684 } nir_intrinsic_op;
685
686 #undef INTRINSIC
687 #undef LAST_INTRINSIC
688
689 typedef struct {
690 nir_instr instr;
691
692 nir_intrinsic_op intrinsic;
693
694 nir_dest dest;
695
696 int const_index[3];
697
698 nir_deref_var *variables[2];
699
700 bool has_predicate;
701 nir_src predicate;
702
703 nir_src src[];
704 } nir_intrinsic_instr;
705
706 /**
707 * \name NIR intrinsics semantic flags
708 *
709 * information about what the compiler can do with the intrinsics.
710 *
711 * \sa nir_intrinsic_info::flags
712 */
713 /*@{*/
714 /**
715 * whether the intrinsic can be safely eliminated if none of its register
716 * outputs are being used.
717 */
718 #define NIR_INTRINSIC_CAN_ELIMINATE (1 << 0)
719
720 /**
721 * Whether the intrinsic can be reordered with respect to any other intrinsic,
722 * i.e. whether the only reodering dependencies of the intrinsic are due to the
723 * register reads/writes.
724 */
725 #define NIR_INTRINSIC_CAN_REORDER (1 << 1)
726 /*@}*/
727
728 #define NIR_INTRINSIC_MAX_INPUTS 4
729
730 typedef struct {
731 const char *name;
732
733 unsigned num_srcs; /** < number of register/SSA inputs */
734
735 /** number of components of each input register */
736 unsigned src_components[NIR_INTRINSIC_MAX_INPUTS];
737
738 bool has_dest;
739
740 /** number of components of each output register */
741 unsigned dest_components;
742
743 /** the number of inputs/outputs that are variables */
744 unsigned num_variables;
745
746 /** the number of constant indices used by the intrinsic */
747 unsigned num_indices;
748
749 /** semantic flags for calls to this intrinsic */
750 unsigned flags;
751 } nir_intrinsic_info;
752
753 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
754
755 /**
756 * \group texture information
757 *
758 * This gives semantic information about textures which is useful to the
759 * frontend, the backend, and lowering passes, but not the optimizer.
760 */
761
762 typedef enum {
763 nir_tex_src_coord,
764 nir_tex_src_projector,
765 nir_tex_src_comparitor, /* shadow comparitor */
766 nir_tex_src_offset,
767 nir_tex_src_bias,
768 nir_tex_src_lod,
769 nir_tex_src_ms_index, /* MSAA sample index */
770 nir_tex_src_ddx,
771 nir_tex_src_ddy,
772 nir_tex_src_sampler_index, /* < dynamically uniform indirect index */
773 nir_num_texinput_types
774 } nir_texinput_type;
775
776 typedef enum {
777 nir_texop_tex, /**< Regular texture look-up */
778 nir_texop_txb, /**< Texture look-up with LOD bias */
779 nir_texop_txl, /**< Texture look-up with explicit LOD */
780 nir_texop_txd, /**< Texture look-up with partial derivatvies */
781 nir_texop_txf, /**< Texel fetch with explicit LOD */
782 nir_texop_txf_ms, /**< Multisample texture fetch */
783 nir_texop_txs, /**< Texture size */
784 nir_texop_lod, /**< Texture lod query */
785 nir_texop_tg4, /**< Texture gather */
786 nir_texop_query_levels /**< Texture levels query */
787 } nir_texop;
788
789 typedef struct {
790 nir_instr instr;
791
792 bool has_predicate;
793 nir_src predicate;
794
795 enum glsl_sampler_dim sampler_dim;
796 nir_alu_type dest_type;
797
798 nir_texop op;
799 nir_dest dest;
800 nir_src src[4];
801 nir_texinput_type src_type[4];
802 unsigned num_srcs, coord_components;
803 bool is_array, is_shadow;
804
805 /**
806 * If is_shadow is true, whether this is the old-style shadow that outputs 4
807 * components or the new-style shadow that outputs 1 component.
808 */
809 bool is_new_style_shadow;
810
811 /* constant offset - must be 0 if the offset source is used */
812 int const_offset[4];
813
814 /* gather component selector */
815 unsigned component : 2;
816
817 unsigned sampler_index;
818 nir_deref_var *sampler; /* if this is NULL, use sampler_index instead */
819 } nir_tex_instr;
820
821 static inline unsigned
822 nir_tex_instr_dest_size(nir_tex_instr *instr)
823 {
824 if (instr->op == nir_texop_txs) {
825 unsigned ret;
826 switch (instr->sampler_dim) {
827 case GLSL_SAMPLER_DIM_1D:
828 case GLSL_SAMPLER_DIM_BUF:
829 ret = 1;
830 break;
831 case GLSL_SAMPLER_DIM_2D:
832 case GLSL_SAMPLER_DIM_CUBE:
833 case GLSL_SAMPLER_DIM_MS:
834 case GLSL_SAMPLER_DIM_RECT:
835 case GLSL_SAMPLER_DIM_EXTERNAL:
836 ret = 2;
837 break;
838 case GLSL_SAMPLER_DIM_3D:
839 ret = 3;
840 break;
841 default:
842 assert(0);
843 break;
844 }
845 if (instr->is_array)
846 ret++;
847 return ret;
848 }
849
850 if (instr->op == nir_texop_query_levels)
851 return 2;
852
853 if (instr->is_shadow && instr->is_new_style_shadow)
854 return 1;
855
856 return 4;
857 }
858
859 static inline unsigned
860 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
861 {
862 if (instr->src_type[src] == nir_tex_src_coord)
863 return instr->coord_components;
864
865
866 if (instr->src_type[src] == nir_tex_src_offset ||
867 instr->src_type[src] == nir_tex_src_ddx ||
868 instr->src_type[src] == nir_tex_src_ddy) {
869 if (instr->is_array)
870 return instr->coord_components - 1;
871 else
872 return instr->coord_components;
873 }
874
875 return 1;
876 }
877
878 static inline int
879 nir_tex_instr_src_index(nir_tex_instr *instr, nir_texinput_type type)
880 {
881 for (unsigned i = 0; i < instr->num_srcs; i++)
882 if (instr->src_type[i] == type)
883 return (int) i;
884
885 return -1;
886 }
887
888 typedef struct {
889 union {
890 float f[4];
891 int32_t i[4];
892 uint32_t u[4];
893 };
894 } nir_const_value;
895
896 typedef struct {
897 nir_instr instr;
898
899 union {
900 nir_const_value value;
901 nir_const_value *array;
902 };
903
904 unsigned num_components;
905
906 /**
907 * The number of constant array elements to be copied into the variable. If
908 * this != 0, then value.array holds the array of size array_elems;
909 * otherwise, value.value holds the single vector constant (the more common
910 * case, and the only case for SSA destinations).
911 */
912 unsigned array_elems;
913
914 bool has_predicate;
915 nir_src predicate;
916
917 nir_dest dest;
918 } nir_load_const_instr;
919
920 typedef enum {
921 nir_jump_return,
922 nir_jump_break,
923 nir_jump_continue,
924 } nir_jump_type;
925
926 typedef struct {
927 nir_instr instr;
928 nir_jump_type type;
929 } nir_jump_instr;
930
931 /* creates a new SSA variable in an undefined state */
932
933 typedef struct {
934 nir_instr instr;
935 nir_ssa_def def;
936 } nir_ssa_undef_instr;
937
938 typedef struct {
939 struct exec_node node;
940 struct nir_block *pred;
941 nir_src src;
942 } nir_phi_src;
943
944 typedef struct {
945 nir_instr instr;
946
947 struct exec_list srcs;
948 nir_dest dest;
949 } nir_phi_instr;
950
951 typedef struct {
952 struct exec_node node;
953 nir_src src;
954 nir_dest dest;
955 } nir_parallel_copy_copy;
956
957 typedef struct {
958 nir_instr instr;
959
960 /* Indicates that this is the parallel copy at the end of the block.
961 * When isolating phi nodes, we create 2 parallel copies in most blocks;
962 * this flag helps tell them apart.
963 */
964 bool at_end;
965
966 struct exec_list copies;
967 } nir_parallel_copy_instr;
968
969 #define nir_instr_as_alu(_instr) exec_node_data(nir_alu_instr, _instr, instr)
970 #define nir_instr_as_call(_instr) exec_node_data(nir_call_instr, _instr, instr)
971 #define nir_instr_as_jump(_instr) exec_node_data(nir_jump_instr, _instr, instr)
972 #define nir_instr_as_texture(_instr) \
973 exec_node_data(nir_tex_instr, _instr, instr)
974 #define nir_instr_as_intrinsic(_instr) \
975 exec_node_data(nir_intrinsic_instr, _instr, instr)
976 #define nir_instr_as_load_const(_instr) \
977 exec_node_data(nir_load_const_instr, _instr, instr)
978 #define nir_instr_as_ssa_undef(_instr) \
979 exec_node_data(nir_ssa_undef_instr, _instr, instr)
980 #define nir_instr_as_phi(_instr) \
981 exec_node_data(nir_phi_instr, _instr, instr)
982 #define nir_instr_as_parallel_copy(_instr) \
983 exec_node_data(nir_parallel_copy_instr, _instr, instr)
984
985
986 /*
987 * Control flow
988 *
989 * Control flow consists of a tree of control flow nodes, which include
990 * if-statements and loops. The leaves of the tree are basic blocks, lists of
991 * instructions that always run start-to-finish. Each basic block also keeps
992 * track of its successors (blocks which may run immediately after the current
993 * block) and predecessors (blocks which could have run immediately before the
994 * current block). Each function also has a start block and an end block which
995 * all return statements point to (which is always empty). Together, all the
996 * blocks with their predecessors and successors make up the control flow
997 * graph (CFG) of the function. There are helpers that modify the tree of
998 * control flow nodes while modifying the CFG appropriately; these should be
999 * used instead of modifying the tree directly.
1000 */
1001
1002 typedef enum {
1003 nir_cf_node_block,
1004 nir_cf_node_if,
1005 nir_cf_node_loop,
1006 nir_cf_node_function
1007 } nir_cf_node_type;
1008
1009 typedef struct nir_cf_node {
1010 struct exec_node node;
1011 nir_cf_node_type type;
1012 struct nir_cf_node *parent;
1013 } nir_cf_node;
1014
1015 typedef struct nir_block {
1016 nir_cf_node cf_node;
1017 struct exec_list instr_list;
1018
1019 unsigned index;
1020
1021 /*
1022 * Each block can only have up to 2 successors, so we put them in a simple
1023 * array - no need for anything more complicated.
1024 */
1025 struct nir_block *successors[2];
1026
1027 struct set *predecessors;
1028
1029 /*
1030 * this node's immediate dominator in the dominance tree - set to NULL for
1031 * the start block.
1032 */
1033 struct nir_block *imm_dom;
1034
1035 /* This node's children in the dominance tree */
1036 unsigned num_dom_children;
1037 struct nir_block **dom_children;
1038
1039 struct set *dom_frontier;
1040
1041 /* live in and out for this block; used for liveness analysis */
1042 BITSET_WORD *live_in;
1043 BITSET_WORD *live_out;
1044 } nir_block;
1045
1046 #define nir_block_first_instr(block) \
1047 exec_node_data(nir_instr, exec_list_get_head(&(block)->instr_list), node)
1048 #define nir_block_last_instr(block) \
1049 exec_node_data(nir_instr, exec_list_get_tail(&(block)->instr_list), node)
1050
1051 #define nir_foreach_instr(block, instr) \
1052 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1053 #define nir_foreach_instr_reverse(block, instr) \
1054 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1055 #define nir_foreach_instr_safe(block, instr) \
1056 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1057
1058 typedef struct {
1059 nir_cf_node cf_node;
1060 nir_src condition;
1061 struct exec_list then_list;
1062 struct exec_list else_list;
1063 } nir_if;
1064
1065 #define nir_if_first_then_node(if) \
1066 exec_node_data(nir_cf_node, exec_list_get_head(&(if)->then_list), node)
1067 #define nir_if_last_then_node(if) \
1068 exec_node_data(nir_cf_node, exec_list_get_tail(&(if)->then_list), node)
1069 #define nir_if_first_else_node(if) \
1070 exec_node_data(nir_cf_node, exec_list_get_head(&(if)->else_list), node)
1071 #define nir_if_last_else_node(if) \
1072 exec_node_data(nir_cf_node, exec_list_get_tail(&(if)->else_list), node)
1073
1074 typedef struct {
1075 nir_cf_node cf_node;
1076 struct exec_list body;
1077 } nir_loop;
1078
1079 #define nir_loop_first_cf_node(loop) \
1080 exec_node_data(nir_cf_node, exec_list_get_head(&(loop)->body), node)
1081 #define nir_loop_last_cf_node(loop) \
1082 exec_node_data(nir_cf_node, exec_list_get_tail(&(loop)->body), node)
1083
1084 /**
1085 * Various bits of metadata that can may be created or required by
1086 * optimization and analysis passes
1087 */
1088 typedef enum {
1089 nir_metadata_none = 0x0,
1090 nir_metadata_block_index = 0x1,
1091 nir_metadata_dominance = 0x2,
1092 nir_metadata_live_variables = 0x4,
1093 } nir_metadata;
1094
1095 typedef struct {
1096 nir_cf_node cf_node;
1097
1098 /** pointer to the overload of which this is an implementation */
1099 struct nir_function_overload *overload;
1100
1101 struct exec_list body; /** < list of nir_cf_node */
1102
1103 nir_block *start_block, *end_block;
1104
1105 /** list for all local variables in the function */
1106 struct exec_list locals;
1107
1108 /** array of variables used as parameters */
1109 unsigned num_params;
1110 nir_variable **params;
1111
1112 /** variable used to hold the result of the function */
1113 nir_variable *return_var;
1114
1115 /** list of local registers in the function */
1116 struct exec_list registers;
1117
1118 /** next available local register index */
1119 unsigned reg_alloc;
1120
1121 /** next available SSA value index */
1122 unsigned ssa_alloc;
1123
1124 /* total number of basic blocks, only valid when block_index_dirty = false */
1125 unsigned num_blocks;
1126
1127 nir_metadata valid_metadata;
1128 } nir_function_impl;
1129
1130 #define nir_cf_node_next(_node) \
1131 exec_node_data(nir_cf_node, exec_node_get_next(&(_node)->node), node)
1132
1133 #define nir_cf_node_prev(_node) \
1134 exec_node_data(nir_cf_node, exec_node_get_prev(&(_node)->node), node)
1135
1136 #define nir_cf_node_is_first(_node) \
1137 exec_node_is_head_sentinel((_node)->node.prev)
1138
1139 #define nir_cf_node_is_last(_node) \
1140 exec_node_is_tail_sentinel((_node)->node.next)
1141
1142 #define nir_cf_node_as_block(node) \
1143 exec_node_data(nir_block, node, cf_node)
1144
1145 #define nir_cf_node_as_if(node) \
1146 exec_node_data(nir_if, node, cf_node)
1147
1148 #define nir_cf_node_as_loop(node) \
1149 exec_node_data(nir_loop, node, cf_node)
1150
1151 #define nir_cf_node_as_function(node) \
1152 exec_node_data(nir_function_impl, node, cf_node)
1153
1154 typedef enum {
1155 nir_parameter_in,
1156 nir_parameter_out,
1157 nir_parameter_inout,
1158 } nir_parameter_type;
1159
1160 typedef struct {
1161 nir_parameter_type param_type;
1162 const struct glsl_type *type;
1163 } nir_parameter;
1164
1165 typedef struct nir_function_overload {
1166 struct exec_node node;
1167
1168 unsigned num_params;
1169 nir_parameter *params;
1170 const struct glsl_type *return_type;
1171
1172 nir_function_impl *impl; /** < NULL if the overload is only declared yet */
1173
1174 /** pointer to the function of which this is an overload */
1175 struct nir_function *function;
1176 } nir_function_overload;
1177
1178 typedef struct nir_function {
1179 struct exec_node node;
1180
1181 struct exec_list overload_list;
1182 const char *name;
1183 struct nir_shader *shader;
1184 } nir_function;
1185
1186 #define nir_function_first_overload(func) \
1187 exec_node_data(nir_function_overload, \
1188 exec_list_get_head(&(func)->overload_list), node)
1189
1190 typedef struct nir_shader {
1191 /** hash table of name -> uniform */
1192 struct hash_table *uniforms;
1193
1194 /** hash table of name -> input */
1195 struct hash_table *inputs;
1196
1197 /** hash table of name -> output */
1198 struct hash_table *outputs;
1199
1200 /** list of global variables in the shader */
1201 struct exec_list globals;
1202
1203 struct exec_list system_values;
1204
1205 struct exec_list functions;
1206
1207 /** list of global registers in the shader */
1208 struct exec_list registers;
1209
1210 /** structures used in this shader */
1211 unsigned num_user_structures;
1212 struct glsl_type **user_structures;
1213
1214 /** next available global register index */
1215 unsigned reg_alloc;
1216
1217 /**
1218 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1219 * access plus one
1220 */
1221 unsigned num_inputs, num_uniforms, num_outputs;
1222 } nir_shader;
1223
1224 #define nir_foreach_overload(shader, overload) \
1225 foreach_list_typed(nir_function, func, node, &(shader)->functions) \
1226 foreach_list_typed(nir_function_overload, overload, node, \
1227 &(func)->overload_list)
1228
1229 nir_shader *nir_shader_create(void *mem_ctx);
1230
1231 /** creates a register, including assigning it an index and adding it to the list */
1232 nir_register *nir_global_reg_create(nir_shader *shader);
1233
1234 nir_register *nir_local_reg_create(nir_function_impl *impl);
1235
1236 void nir_reg_remove(nir_register *reg);
1237
1238 /** creates a function and adds it to the shader's list of functions */
1239 nir_function *nir_function_create(nir_shader *shader, const char *name);
1240
1241 /** creates a null function returning null */
1242 nir_function_overload *nir_function_overload_create(nir_function *func);
1243
1244 nir_function_impl *nir_function_impl_create(nir_function_overload *func);
1245
1246 nir_block *nir_block_create(void *mem_ctx);
1247 nir_if *nir_if_create(void *mem_ctx);
1248 nir_loop *nir_loop_create(void *mem_ctx);
1249
1250 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
1251
1252 /** puts a control flow node immediately after another control flow node */
1253 void nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after);
1254
1255 /** puts a control flow node immediately before another control flow node */
1256 void nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before);
1257
1258 /** puts a control flow node at the beginning of a list from an if, loop, or function */
1259 void nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node);
1260
1261 /** puts a control flow node at the end of a list from an if, loop, or function */
1262 void nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node);
1263
1264 /** removes a control flow node, doing any cleanup necessary */
1265 void nir_cf_node_remove(nir_cf_node *node);
1266
1267 /** requests that the given pieces of metadata be generated */
1268 void nir_metadata_require(nir_function_impl *impl, nir_metadata required);
1269 /** dirties all but the preserved metadata */
1270 void nir_metadata_dirty(nir_function_impl *impl, nir_metadata preserved);
1271
1272 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1273 nir_alu_instr *nir_alu_instr_create(void *mem_ctx, nir_op op);
1274
1275 nir_jump_instr *nir_jump_instr_create(void *mem_ctx, nir_jump_type type);
1276
1277 nir_load_const_instr *nir_load_const_instr_create(void *mem_ctx);
1278
1279 nir_intrinsic_instr *nir_intrinsic_instr_create(void *mem_ctx,
1280 nir_intrinsic_op op);
1281
1282 nir_call_instr *nir_call_instr_create(void *mem_ctx,
1283 nir_function_overload *callee);
1284
1285 nir_tex_instr *nir_tex_instr_create(void *mem_ctx, unsigned num_srcs);
1286
1287 nir_phi_instr *nir_phi_instr_create(void *mem_ctx);
1288
1289 nir_parallel_copy_instr *nir_parallel_copy_instr_create(void *mem_ctx);
1290
1291 nir_ssa_undef_instr *nir_ssa_undef_instr_create(void *mem_ctx);
1292
1293 nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
1294 nir_deref_array *nir_deref_array_create(void *mem_ctx);
1295 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
1296
1297 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
1298
1299 void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
1300 void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
1301
1302 void nir_instr_insert_before_block(nir_block *block, nir_instr *before);
1303 void nir_instr_insert_after_block(nir_block *block, nir_instr *after);
1304
1305 void nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before);
1306 void nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after);
1307
1308 void nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before);
1309 void nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after);
1310
1311 void nir_instr_remove(nir_instr *instr);
1312
1313 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
1314 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
1315 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
1316 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
1317
1318 bool nir_srcs_equal(nir_src src1, nir_src src2);
1319 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
1320
1321 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1322 unsigned num_components, const char *name);
1323 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx);
1324
1325 /* visits basic blocks in source-code order */
1326 typedef bool (*nir_foreach_block_cb)(nir_block *block, void *state);
1327 bool nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb,
1328 void *state);
1329 bool nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
1330 void *state);
1331
1332 /* If the following CF node is an if, this function returns that if.
1333 * Otherwise, it returns NULL.
1334 */
1335 nir_if *nir_block_following_if(nir_block *block);
1336
1337 void nir_index_local_regs(nir_function_impl *impl);
1338 void nir_index_global_regs(nir_shader *shader);
1339 void nir_index_ssa_defs(nir_function_impl *impl);
1340
1341 void nir_index_blocks(nir_function_impl *impl);
1342
1343 void nir_print_shader(nir_shader *shader, FILE *fp);
1344
1345 void nir_validate_shader(nir_shader *shader);
1346
1347 void nir_calc_dominance_impl(nir_function_impl *impl);
1348 void nir_calc_dominance(nir_shader *shader);
1349
1350 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
1351 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
1352
1353 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
1354 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
1355
1356 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
1357 void nir_dump_cfg(nir_shader *shader, FILE *fp);
1358
1359 void nir_split_var_copies(nir_shader *shader);
1360
1361 void nir_lower_variables(nir_shader *shader);
1362
1363 void nir_lower_variables_scalar(nir_shader *shader, bool lower_globals,
1364 bool lower_io, bool add_names,
1365 bool native_integers);
1366
1367 void nir_remove_dead_variables(nir_shader *shader);
1368
1369 void nir_lower_vec_to_movs(nir_shader *shader);
1370
1371 void nir_lower_samplers(nir_shader *shader,
1372 struct gl_shader_program *shader_program,
1373 struct gl_program *prog);
1374
1375 void nir_lower_system_values(nir_shader *shader);
1376
1377 void nir_lower_atomics(nir_shader *shader);
1378
1379 void nir_live_variables_impl(nir_function_impl *impl);
1380 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
1381
1382 void nir_convert_to_ssa_impl(nir_function_impl *impl);
1383 void nir_convert_to_ssa(nir_shader *shader);
1384 void nir_convert_from_ssa(nir_shader *shader);
1385
1386 bool nir_opt_global_to_local(nir_shader *shader);
1387
1388 bool nir_copy_prop_impl(nir_function_impl *impl);
1389 bool nir_copy_prop(nir_shader *shader);
1390
1391 bool nir_opt_cse(nir_shader *shader);
1392
1393 bool nir_opt_dce_impl(nir_function_impl *impl);
1394 bool nir_opt_dce(nir_shader *shader);
1395
1396 bool nir_opt_peephole_select(nir_shader *shader);
1397 bool nir_opt_peephole_ffma(nir_shader *shader);
1398
1399 #ifdef __cplusplus
1400 } /* extern "C" */
1401 #endif