2 * Copyright © 2014-2015 Broadcom
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:
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
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
27 #include "nir_control_flow.h"
31 typedef struct nir_builder
{
34 /* Whether new ALU instructions will be marked "exact" */
38 nir_function_impl
*impl
;
42 nir_builder_init(nir_builder
*build
, nir_function_impl
*impl
)
44 memset(build
, 0, sizeof(*build
));
47 build
->shader
= impl
->function
->shader
;
51 nir_builder_init_simple_shader(nir_builder
*build
, void *mem_ctx
,
52 gl_shader_stage stage
,
53 const nir_shader_compiler_options
*options
)
55 build
->shader
= nir_shader_create(mem_ctx
, stage
, options
, NULL
);
56 nir_function
*func
= nir_function_create(build
->shader
, "main");
58 build
->impl
= nir_function_impl_create(func
);
59 build
->cursor
= nir_after_cf_list(&build
->impl
->body
);
63 nir_builder_instr_insert(nir_builder
*build
, nir_instr
*instr
)
65 nir_instr_insert(build
->cursor
, instr
);
67 /* Move the cursor forward. */
68 build
->cursor
= nir_after_instr(instr
);
71 static inline nir_instr
*
72 nir_builder_last_instr(nir_builder
*build
)
74 assert(build
->cursor
.option
== nir_cursor_after_instr
);
75 return build
->cursor
.instr
;
79 nir_builder_cf_insert(nir_builder
*build
, nir_cf_node
*cf
)
81 nir_cf_node_insert(build
->cursor
, cf
);
85 nir_builder_is_inside_cf(nir_builder
*build
, nir_cf_node
*cf_node
)
87 nir_block
*block
= nir_cursor_current_block(build
->cursor
);
88 for (nir_cf_node
*n
= &block
->cf_node
; n
; n
= n
->parent
) {
95 static inline nir_if
*
96 nir_push_if(nir_builder
*build
, nir_ssa_def
*condition
)
98 nir_if
*nif
= nir_if_create(build
->shader
);
99 nif
->condition
= nir_src_for_ssa(condition
);
100 nir_builder_cf_insert(build
, &nif
->cf_node
);
101 build
->cursor
= nir_before_cf_list(&nif
->then_list
);
105 static inline nir_if
*
106 nir_push_else(nir_builder
*build
, nir_if
*nif
)
109 assert(nir_builder_is_inside_cf(build
, &nif
->cf_node
));
111 nir_block
*block
= nir_cursor_current_block(build
->cursor
);
112 nif
= nir_cf_node_as_if(block
->cf_node
.parent
);
114 build
->cursor
= nir_before_cf_list(&nif
->else_list
);
119 nir_pop_if(nir_builder
*build
, nir_if
*nif
)
122 assert(nir_builder_is_inside_cf(build
, &nif
->cf_node
));
124 nir_block
*block
= nir_cursor_current_block(build
->cursor
);
125 nif
= nir_cf_node_as_if(block
->cf_node
.parent
);
127 build
->cursor
= nir_after_cf_node(&nif
->cf_node
);
130 static inline nir_ssa_def
*
131 nir_if_phi(nir_builder
*build
, nir_ssa_def
*then_def
, nir_ssa_def
*else_def
)
133 nir_block
*block
= nir_cursor_current_block(build
->cursor
);
134 nir_if
*nif
= nir_cf_node_as_if(nir_cf_node_prev(&block
->cf_node
));
136 nir_phi_instr
*phi
= nir_phi_instr_create(build
->shader
);
138 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
139 src
->pred
= nir_if_last_then_block(nif
);
140 src
->src
= nir_src_for_ssa(then_def
);
141 exec_list_push_tail(&phi
->srcs
, &src
->node
);
143 src
= ralloc(phi
, nir_phi_src
);
144 src
->pred
= nir_if_last_else_block(nif
);
145 src
->src
= nir_src_for_ssa(else_def
);
146 exec_list_push_tail(&phi
->srcs
, &src
->node
);
148 assert(then_def
->num_components
== else_def
->num_components
);
149 assert(then_def
->bit_size
== else_def
->bit_size
);
150 nir_ssa_dest_init(&phi
->instr
, &phi
->dest
,
151 then_def
->num_components
, then_def
->bit_size
, NULL
);
153 nir_builder_instr_insert(build
, &phi
->instr
);
155 return &phi
->dest
.ssa
;
158 static inline nir_loop
*
159 nir_push_loop(nir_builder
*build
)
161 nir_loop
*loop
= nir_loop_create(build
->shader
);
162 nir_builder_cf_insert(build
, &loop
->cf_node
);
163 build
->cursor
= nir_before_cf_list(&loop
->body
);
168 nir_pop_loop(nir_builder
*build
, nir_loop
*loop
)
171 assert(nir_builder_is_inside_cf(build
, &loop
->cf_node
));
173 nir_block
*block
= nir_cursor_current_block(build
->cursor
);
174 loop
= nir_cf_node_as_loop(block
->cf_node
.parent
);
176 build
->cursor
= nir_after_cf_node(&loop
->cf_node
);
179 static inline nir_ssa_def
*
180 nir_ssa_undef(nir_builder
*build
, unsigned num_components
, unsigned bit_size
)
182 nir_ssa_undef_instr
*undef
=
183 nir_ssa_undef_instr_create(build
->shader
, num_components
, bit_size
);
187 nir_instr_insert(nir_before_cf_list(&build
->impl
->body
), &undef
->instr
);
192 static inline nir_ssa_def
*
193 nir_build_imm(nir_builder
*build
, unsigned num_components
,
194 unsigned bit_size
, nir_const_value value
)
196 nir_load_const_instr
*load_const
=
197 nir_load_const_instr_create(build
->shader
, num_components
, bit_size
);
201 load_const
->value
= value
;
203 nir_builder_instr_insert(build
, &load_const
->instr
);
205 return &load_const
->def
;
208 static inline nir_ssa_def
*
209 nir_imm_float(nir_builder
*build
, float x
)
213 memset(&v
, 0, sizeof(v
));
216 return nir_build_imm(build
, 1, 32, v
);
219 static inline nir_ssa_def
*
220 nir_imm_double(nir_builder
*build
, double x
)
224 memset(&v
, 0, sizeof(v
));
227 return nir_build_imm(build
, 1, 64, v
);
230 static inline nir_ssa_def
*
231 nir_imm_floatN_t(nir_builder
*build
, double x
, unsigned bit_size
)
235 return nir_imm_float(build
, x
);
237 return nir_imm_double(build
, x
);
240 unreachable("unknown float immediate bit size");
243 static inline nir_ssa_def
*
244 nir_imm_vec4(nir_builder
*build
, float x
, float y
, float z
, float w
)
248 memset(&v
, 0, sizeof(v
));
254 return nir_build_imm(build
, 4, 32, v
);
257 static inline nir_ssa_def
*
258 nir_imm_int(nir_builder
*build
, int x
)
262 memset(&v
, 0, sizeof(v
));
265 return nir_build_imm(build
, 1, 32, v
);
268 static inline nir_ssa_def
*
269 nir_imm_int64(nir_builder
*build
, int64_t x
)
273 memset(&v
, 0, sizeof(v
));
276 return nir_build_imm(build
, 1, 64, v
);
279 static inline nir_ssa_def
*
280 nir_imm_intN_t(nir_builder
*build
, uint64_t x
, unsigned bit_size
)
284 memset(&v
, 0, sizeof(v
));
285 assert(bit_size
<= 64);
286 v
.i64
[0] = x
& (~0ull >> (64 - bit_size
));
288 return nir_build_imm(build
, 1, bit_size
, v
);
291 static inline nir_ssa_def
*
292 nir_imm_ivec4(nir_builder
*build
, int x
, int y
, int z
, int w
)
296 memset(&v
, 0, sizeof(v
));
302 return nir_build_imm(build
, 4, 32, v
);
305 static inline nir_ssa_def
*
306 nir_build_alu(nir_builder
*build
, nir_op op
, nir_ssa_def
*src0
,
307 nir_ssa_def
*src1
, nir_ssa_def
*src2
, nir_ssa_def
*src3
)
309 const nir_op_info
*op_info
= &nir_op_infos
[op
];
310 nir_alu_instr
*instr
= nir_alu_instr_create(build
->shader
, op
);
314 instr
->exact
= build
->exact
;
316 instr
->src
[0].src
= nir_src_for_ssa(src0
);
318 instr
->src
[1].src
= nir_src_for_ssa(src1
);
320 instr
->src
[2].src
= nir_src_for_ssa(src2
);
322 instr
->src
[3].src
= nir_src_for_ssa(src3
);
324 /* Guess the number of components the destination temporary should have
325 * based on our input sizes, if it's not fixed for the op.
327 unsigned num_components
= op_info
->output_size
;
328 if (num_components
== 0) {
329 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
330 if (op_info
->input_sizes
[i
] == 0)
331 num_components
= MAX2(num_components
,
332 instr
->src
[i
].src
.ssa
->num_components
);
335 assert(num_components
!= 0);
337 /* Figure out the bitwidth based on the source bitwidth if the instruction
340 unsigned bit_size
= nir_alu_type_get_type_size(op_info
->output_type
);
342 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
343 unsigned src_bit_size
= instr
->src
[i
].src
.ssa
->bit_size
;
344 if (nir_alu_type_get_type_size(op_info
->input_types
[i
]) == 0) {
346 assert(src_bit_size
== bit_size
);
348 bit_size
= src_bit_size
;
350 assert(src_bit_size
==
351 nir_alu_type_get_type_size(op_info
->input_types
[i
]));
356 /* When in doubt, assume 32. */
360 /* Make sure we don't swizzle from outside of our source vector (like if a
361 * scalar value was passed into a multiply with a vector).
363 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
364 for (unsigned j
= instr
->src
[i
].src
.ssa
->num_components
;
365 j
< NIR_MAX_VEC_COMPONENTS
; j
++) {
366 instr
->src
[i
].swizzle
[j
] = instr
->src
[i
].src
.ssa
->num_components
- 1;
370 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
,
372 instr
->dest
.write_mask
= (1 << num_components
) - 1;
374 nir_builder_instr_insert(build
, &instr
->instr
);
376 return &instr
->dest
.dest
.ssa
;
379 #include "nir_builder_opcodes.h"
381 static inline nir_ssa_def
*
382 nir_vec(nir_builder
*build
, nir_ssa_def
**comp
, unsigned num_components
)
384 switch (num_components
) {
386 return nir_vec4(build
, comp
[0], comp
[1], comp
[2], comp
[3]);
388 return nir_vec3(build
, comp
[0], comp
[1], comp
[2]);
390 return nir_vec2(build
, comp
[0], comp
[1]);
394 unreachable("bad component count");
400 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
402 static inline nir_ssa_def
*
403 nir_fmov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
405 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_fmov
);
406 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
,
407 nir_src_bit_size(src
.src
), NULL
);
408 mov
->exact
= build
->exact
;
409 mov
->dest
.write_mask
= (1 << num_components
) - 1;
411 nir_builder_instr_insert(build
, &mov
->instr
);
413 return &mov
->dest
.dest
.ssa
;
416 static inline nir_ssa_def
*
417 nir_imov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
419 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_imov
);
420 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
,
421 nir_src_bit_size(src
.src
), NULL
);
422 mov
->exact
= build
->exact
;
423 mov
->dest
.write_mask
= (1 << num_components
) - 1;
425 nir_builder_instr_insert(build
, &mov
->instr
);
427 return &mov
->dest
.dest
.ssa
;
431 * Construct an fmov or imov that reswizzles the source's components.
433 static inline nir_ssa_def
*
434 nir_swizzle(nir_builder
*build
, nir_ssa_def
*src
, const unsigned *swiz
,
435 unsigned num_components
, bool use_fmov
)
437 assert(num_components
<= NIR_MAX_VEC_COMPONENTS
);
438 nir_alu_src alu_src
= { NIR_SRC_INIT
};
439 alu_src
.src
= nir_src_for_ssa(src
);
440 for (unsigned i
= 0; i
< num_components
&& i
< NIR_MAX_VEC_COMPONENTS
; i
++)
441 alu_src
.swizzle
[i
] = swiz
[i
];
443 return use_fmov
? nir_fmov_alu(build
, alu_src
, num_components
) :
444 nir_imov_alu(build
, alu_src
, num_components
);
447 /* Selects the right fdot given the number of components in each source. */
448 static inline nir_ssa_def
*
449 nir_fdot(nir_builder
*build
, nir_ssa_def
*src0
, nir_ssa_def
*src1
)
451 assert(src0
->num_components
== src1
->num_components
);
452 switch (src0
->num_components
) {
453 case 1: return nir_fmul(build
, src0
, src1
);
454 case 2: return nir_fdot2(build
, src0
, src1
);
455 case 3: return nir_fdot3(build
, src0
, src1
);
456 case 4: return nir_fdot4(build
, src0
, src1
);
458 unreachable("bad component size");
464 static inline nir_ssa_def
*
465 nir_bany_inequal(nir_builder
*b
, nir_ssa_def
*src0
, nir_ssa_def
*src1
)
467 switch (src0
->num_components
) {
468 case 1: return nir_ine(b
, src0
, src1
);
469 case 2: return nir_bany_inequal2(b
, src0
, src1
);
470 case 3: return nir_bany_inequal3(b
, src0
, src1
);
471 case 4: return nir_bany_inequal4(b
, src0
, src1
);
473 unreachable("bad component size");
477 static inline nir_ssa_def
*
478 nir_bany(nir_builder
*b
, nir_ssa_def
*src
)
480 return nir_bany_inequal(b
, src
, nir_imm_int(b
, 0));
483 static inline nir_ssa_def
*
484 nir_channel(nir_builder
*b
, nir_ssa_def
*def
, unsigned c
)
486 return nir_swizzle(b
, def
, &c
, 1, false);
489 static inline nir_ssa_def
*
490 nir_channels(nir_builder
*b
, nir_ssa_def
*def
, nir_component_mask_t mask
)
492 unsigned num_channels
= 0, swizzle
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
494 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
495 if ((mask
& (1 << i
)) == 0)
497 swizzle
[num_channels
++] = i
;
500 return nir_swizzle(b
, def
, swizzle
, num_channels
, false);
504 * Turns a nir_src into a nir_ssa_def * so it can be passed to
505 * nir_build_alu()-based builder calls.
507 * See nir_ssa_for_alu_src() for alu instructions.
509 static inline nir_ssa_def
*
510 nir_ssa_for_src(nir_builder
*build
, nir_src src
, int num_components
)
512 if (src
.is_ssa
&& src
.ssa
->num_components
== num_components
)
515 nir_alu_src alu
= { NIR_SRC_INIT
};
517 for (int j
= 0; j
< 4; j
++)
520 return nir_imov_alu(build
, alu
, num_components
);
524 * Similar to nir_ssa_for_src(), but for alu srcs, respecting the
525 * nir_alu_src's swizzle.
527 static inline nir_ssa_def
*
528 nir_ssa_for_alu_src(nir_builder
*build
, nir_alu_instr
*instr
, unsigned srcn
)
530 static uint8_t trivial_swizzle
[NIR_MAX_VEC_COMPONENTS
];
531 for (int i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
)
532 trivial_swizzle
[i
] = i
;
533 nir_alu_src
*src
= &instr
->src
[srcn
];
534 unsigned num_components
= nir_ssa_alu_instr_src_components(instr
, srcn
);
536 if (src
->src
.is_ssa
&& (src
->src
.ssa
->num_components
== num_components
) &&
537 !src
->abs
&& !src
->negate
&&
538 (memcmp(src
->swizzle
, trivial_swizzle
, num_components
) == 0))
541 return nir_imov_alu(build
, *src
, num_components
);
544 static inline nir_deref_instr
*
545 nir_build_deref_var(nir_builder
*build
, nir_variable
*var
)
547 nir_deref_instr
*deref
=
548 nir_deref_instr_create(build
->shader
, nir_deref_type_var
);
550 deref
->mode
= var
->data
.mode
;
551 deref
->type
= var
->type
;
554 nir_ssa_dest_init(&deref
->instr
, &deref
->dest
, 1, 32, NULL
);
556 nir_builder_instr_insert(build
, &deref
->instr
);
561 static inline nir_deref_instr
*
562 nir_build_deref_array(nir_builder
*build
, nir_deref_instr
*parent
,
565 assert(glsl_type_is_array(parent
->type
) ||
566 glsl_type_is_matrix(parent
->type
) ||
567 glsl_type_is_vector(parent
->type
));
569 nir_deref_instr
*deref
=
570 nir_deref_instr_create(build
->shader
, nir_deref_type_array
);
572 deref
->mode
= parent
->mode
;
573 deref
->type
= glsl_get_array_element(parent
->type
);
574 deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
575 deref
->arr
.index
= nir_src_for_ssa(index
);
577 nir_ssa_dest_init(&deref
->instr
, &deref
->dest
,
578 parent
->dest
.ssa
.num_components
,
579 parent
->dest
.ssa
.bit_size
, NULL
);
581 nir_builder_instr_insert(build
, &deref
->instr
);
586 static inline nir_deref_instr
*
587 nir_build_deref_array_wildcard(nir_builder
*build
, nir_deref_instr
*parent
)
589 assert(glsl_type_is_array(parent
->type
) ||
590 glsl_type_is_matrix(parent
->type
));
592 nir_deref_instr
*deref
=
593 nir_deref_instr_create(build
->shader
, nir_deref_type_array_wildcard
);
595 deref
->mode
= parent
->mode
;
596 deref
->type
= glsl_get_array_element(parent
->type
);
597 deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
599 nir_ssa_dest_init(&deref
->instr
, &deref
->dest
,
600 parent
->dest
.ssa
.num_components
,
601 parent
->dest
.ssa
.bit_size
, NULL
);
603 nir_builder_instr_insert(build
, &deref
->instr
);
608 static inline nir_deref_instr
*
609 nir_build_deref_struct(nir_builder
*build
, nir_deref_instr
*parent
,
612 assert(glsl_type_is_struct(parent
->type
));
614 nir_deref_instr
*deref
=
615 nir_deref_instr_create(build
->shader
, nir_deref_type_struct
);
617 deref
->mode
= parent
->mode
;
618 deref
->type
= glsl_get_struct_field(parent
->type
, index
);
619 deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
620 deref
->strct
.index
= index
;
622 nir_ssa_dest_init(&deref
->instr
, &deref
->dest
,
623 parent
->dest
.ssa
.num_components
,
624 parent
->dest
.ssa
.bit_size
, NULL
);
626 nir_builder_instr_insert(build
, &deref
->instr
);
631 static inline nir_deref_instr
*
632 nir_build_deref_cast(nir_builder
*build
, nir_ssa_def
*parent
,
633 nir_variable_mode mode
, const struct glsl_type
*type
)
635 nir_deref_instr
*deref
=
636 nir_deref_instr_create(build
->shader
, nir_deref_type_cast
);
640 deref
->parent
= nir_src_for_ssa(parent
);
642 nir_ssa_dest_init(&deref
->instr
, &deref
->dest
,
643 parent
->num_components
, parent
->bit_size
, NULL
);
645 nir_builder_instr_insert(build
, &deref
->instr
);
650 /** Returns a deref that follows another but starting from the given parent
652 * The new deref will be the same type and take the same array or struct index
653 * as the leader deref but it may have a different parent. This is very
654 * useful for walking deref paths.
656 static inline nir_deref_instr
*
657 nir_build_deref_follower(nir_builder
*b
, nir_deref_instr
*parent
,
658 nir_deref_instr
*leader
)
660 /* If the derefs would have the same parent, don't make a new one */
661 assert(leader
->parent
.is_ssa
);
662 if (leader
->parent
.ssa
== &parent
->dest
.ssa
)
665 UNUSED nir_deref_instr
*leader_parent
= nir_src_as_deref(leader
->parent
);
667 switch (leader
->deref_type
) {
668 case nir_deref_type_var
:
669 unreachable("A var dereference cannot have a parent");
672 case nir_deref_type_array
:
673 case nir_deref_type_array_wildcard
:
674 assert(glsl_type_is_matrix(parent
->type
) ||
675 glsl_type_is_array(parent
->type
));
676 assert(glsl_get_length(parent
->type
) ==
677 glsl_get_length(leader_parent
->type
));
679 if (leader
->deref_type
== nir_deref_type_array
) {
680 assert(leader
->arr
.index
.is_ssa
);
681 return nir_build_deref_array(b
, parent
, leader
->arr
.index
.ssa
);
683 return nir_build_deref_array_wildcard(b
, parent
);
686 case nir_deref_type_struct
:
687 assert(glsl_type_is_struct(parent
->type
));
688 assert(glsl_get_length(parent
->type
) ==
689 glsl_get_length(leader_parent
->type
));
691 return nir_build_deref_struct(b
, parent
, leader
->strct
.index
);
694 unreachable("Invalid deref instruction type");
698 static inline nir_ssa_def
*
699 nir_load_reg(nir_builder
*build
, nir_register
*reg
)
701 return nir_ssa_for_src(build
, nir_src_for_reg(reg
), reg
->num_components
);
704 static inline nir_ssa_def
*
705 nir_load_deref(nir_builder
*build
, nir_deref_instr
*deref
)
707 nir_intrinsic_instr
*load
=
708 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_load_deref
);
709 load
->num_components
= glsl_get_vector_elements(deref
->type
);
710 load
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
711 nir_ssa_dest_init(&load
->instr
, &load
->dest
, load
->num_components
,
712 glsl_get_bit_size(deref
->type
), NULL
);
713 nir_builder_instr_insert(build
, &load
->instr
);
714 return &load
->dest
.ssa
;
718 nir_store_deref(nir_builder
*build
, nir_deref_instr
*deref
,
719 nir_ssa_def
*value
, unsigned writemask
)
721 nir_intrinsic_instr
*store
=
722 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_store_deref
);
723 store
->num_components
= glsl_get_vector_elements(deref
->type
);
724 store
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
725 store
->src
[1] = nir_src_for_ssa(value
);
726 nir_intrinsic_set_write_mask(store
,
727 writemask
& ((1 << store
->num_components
) - 1));
728 nir_builder_instr_insert(build
, &store
->instr
);
732 nir_copy_deref(nir_builder
*build
, nir_deref_instr
*dest
, nir_deref_instr
*src
)
734 nir_intrinsic_instr
*copy
=
735 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_copy_deref
);
736 copy
->src
[0] = nir_src_for_ssa(&dest
->dest
.ssa
);
737 copy
->src
[1] = nir_src_for_ssa(&src
->dest
.ssa
);
738 nir_builder_instr_insert(build
, ©
->instr
);
741 static inline nir_ssa_def
*
742 nir_load_var(nir_builder
*build
, nir_variable
*var
)
744 return nir_load_deref(build
, nir_build_deref_var(build
, var
));
748 nir_store_var(nir_builder
*build
, nir_variable
*var
, nir_ssa_def
*value
,
751 nir_store_deref(build
, nir_build_deref_var(build
, var
), value
, writemask
);
755 nir_copy_var(nir_builder
*build
, nir_variable
*dest
, nir_variable
*src
)
757 nir_copy_deref(build
, nir_build_deref_var(build
, dest
),
758 nir_build_deref_var(build
, src
));
761 static inline nir_ssa_def
*
762 nir_load_param(nir_builder
*build
, uint32_t param_idx
)
764 assert(param_idx
< build
->impl
->function
->num_params
);
765 nir_parameter
*param
= &build
->impl
->function
->params
[param_idx
];
767 nir_intrinsic_instr
*load
=
768 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_load_param
);
769 nir_intrinsic_set_param_idx(load
, param_idx
);
770 load
->num_components
= param
->num_components
;
771 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
772 param
->num_components
, param
->bit_size
, NULL
);
773 nir_builder_instr_insert(build
, &load
->instr
);
774 return &load
->dest
.ssa
;
777 #include "nir_builder_opcodes.h"
779 static inline nir_ssa_def
*
780 nir_load_barycentric(nir_builder
*build
, nir_intrinsic_op op
,
781 unsigned interp_mode
)
783 nir_intrinsic_instr
*bary
= nir_intrinsic_instr_create(build
->shader
, op
);
784 nir_ssa_dest_init(&bary
->instr
, &bary
->dest
, 2, 32, NULL
);
785 nir_intrinsic_set_interp_mode(bary
, interp_mode
);
786 nir_builder_instr_insert(build
, &bary
->instr
);
787 return &bary
->dest
.ssa
;
791 nir_jump(nir_builder
*build
, nir_jump_type jump_type
)
793 nir_jump_instr
*jump
= nir_jump_instr_create(build
->shader
, jump_type
);
794 nir_builder_instr_insert(build
, &jump
->instr
);
797 static inline nir_ssa_def
*
798 nir_compare_func(nir_builder
*b
, enum compare_func func
,
799 nir_ssa_def
*src0
, nir_ssa_def
*src1
)
802 case COMPARE_FUNC_NEVER
:
803 return nir_imm_int(b
, 0);
804 case COMPARE_FUNC_ALWAYS
:
805 return nir_imm_int(b
, ~0);
806 case COMPARE_FUNC_EQUAL
:
807 return nir_feq(b
, src0
, src1
);
808 case COMPARE_FUNC_NOTEQUAL
:
809 return nir_fne(b
, src0
, src1
);
810 case COMPARE_FUNC_GREATER
:
811 return nir_flt(b
, src1
, src0
);
812 case COMPARE_FUNC_GEQUAL
:
813 return nir_fge(b
, src0
, src1
);
814 case COMPARE_FUNC_LESS
:
815 return nir_flt(b
, src0
, src1
);
816 case COMPARE_FUNC_LEQUAL
:
817 return nir_fge(b
, src1
, src0
);
819 unreachable("bad compare func");
822 #endif /* NIR_BUILDER_H */