2 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_math.h"
33 #include "ir3_compiler.h"
34 #include "ir3_image.h"
35 #include "ir3_shader.h"
38 #include "instr-a3xx.h"
40 #include "ir3_context.h"
43 static struct ir3_instruction
*
44 create_indirect_load(struct ir3_context
*ctx
, unsigned arrsz
, int n
,
45 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
47 struct ir3_block
*block
= ctx
->block
;
48 struct ir3_instruction
*mov
;
49 struct ir3_register
*src
;
51 mov
= ir3_instr_create(block
, OPC_MOV
);
52 mov
->cat1
.src_type
= TYPE_U32
;
53 mov
->cat1
.dst_type
= TYPE_U32
;
55 src
= __ssa_src(mov
, collect
, IR3_REG_RELATIV
);
57 src
->array
.offset
= n
;
59 ir3_instr_set_address(mov
, address
);
64 static struct ir3_instruction
*
65 create_input(struct ir3_context
*ctx
, unsigned compmask
)
67 struct ir3_instruction
*in
;
69 in
= ir3_instr_create(ctx
->in_block
, OPC_META_INPUT
);
70 in
->input
.sysval
= ~0;
71 __ssa_dst(in
)->wrmask
= compmask
;
73 array_insert(ctx
->ir
, ctx
->ir
->inputs
, in
);
78 static struct ir3_instruction
*
79 create_frag_input(struct ir3_context
*ctx
, bool use_ldlv
, unsigned n
)
81 struct ir3_block
*block
= ctx
->block
;
82 struct ir3_instruction
*instr
;
83 /* packed inloc is fixed up later: */
84 struct ir3_instruction
*inloc
= create_immed(block
, n
);
87 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
88 instr
->cat6
.type
= TYPE_U32
;
89 instr
->cat6
.iim_val
= 1;
91 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->ij_pixel
, 0);
92 instr
->regs
[2]->wrmask
= 0x3;
98 static struct ir3_instruction
*
99 create_driver_param(struct ir3_context
*ctx
, enum ir3_driver_param dp
)
101 /* first four vec4 sysval's reserved for UBOs: */
102 /* NOTE: dp is in scalar, but there can be >4 dp components: */
103 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
104 unsigned n
= const_state
->offsets
.driver_param
;
105 unsigned r
= regid(n
+ dp
/ 4, dp
% 4);
106 return create_uniform(ctx
->block
, r
);
110 * Adreno uses uint rather than having dedicated bool type,
111 * which (potentially) requires some conversion, in particular
112 * when using output of an bool instr to int input, or visa
116 * -------+---------+-------+-
120 * To convert from an adreno bool (uint) to nir, use:
122 * absneg.s dst, (neg)src
124 * To convert back in the other direction:
126 * absneg.s dst, (abs)arc
128 * The CP step can clean up the absneg.s that cancel each other
129 * out, and with a slight bit of extra cleverness (to recognize
130 * the instructions which produce either a 0 or 1) can eliminate
131 * the absneg.s's completely when an instruction that wants
132 * 0/1 consumes the result. For example, when a nir 'bcsel'
133 * consumes the result of 'feq'. So we should be able to get by
134 * without a boolean resolve step, and without incuring any
135 * extra penalty in instruction count.
138 /* NIR bool -> native (adreno): */
139 static struct ir3_instruction
*
140 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
142 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
145 /* native (adreno) -> NIR bool: */
146 static struct ir3_instruction
*
147 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
149 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
153 * alu/sfu instructions:
156 static struct ir3_instruction
*
157 create_cov(struct ir3_context
*ctx
, struct ir3_instruction
*src
,
158 unsigned src_bitsize
, nir_op op
)
160 type_t src_type
, dst_type
;
164 case nir_op_f2f16_rtne
:
165 case nir_op_f2f16_rtz
:
173 switch (src_bitsize
) {
181 ir3_context_error(ctx
, "invalid src bit size: %u", src_bitsize
);
190 switch (src_bitsize
) {
201 ir3_context_error(ctx
, "invalid src bit size: %u", src_bitsize
);
210 switch (src_bitsize
) {
221 ir3_context_error(ctx
, "invalid src bit size: %u", src_bitsize
);
226 ir3_context_error(ctx
, "invalid conversion op: %u", op
);
236 case nir_op_f2f16_rtne
:
237 case nir_op_f2f16_rtz
:
239 /* TODO how to handle rounding mode? */
276 ir3_context_error(ctx
, "invalid conversion op: %u", op
);
279 return ir3_COV(ctx
->block
, src
, src_type
, dst_type
);
283 emit_alu(struct ir3_context
*ctx
, nir_alu_instr
*alu
)
285 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
286 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
287 unsigned bs
[info
->num_inputs
]; /* bit size */
288 struct ir3_block
*b
= ctx
->block
;
289 unsigned dst_sz
, wrmask
;
290 type_t dst_type
= nir_dest_bit_size(alu
->dest
.dest
) < 32 ?
293 if (alu
->dest
.dest
.is_ssa
) {
294 dst_sz
= alu
->dest
.dest
.ssa
.num_components
;
295 wrmask
= (1 << dst_sz
) - 1;
297 dst_sz
= alu
->dest
.dest
.reg
.reg
->num_components
;
298 wrmask
= alu
->dest
.write_mask
;
301 dst
= ir3_get_dst(ctx
, &alu
->dest
.dest
, dst_sz
);
303 /* Vectors are special in that they have non-scalarized writemasks,
304 * and just take the first swizzle channel for each argument in
305 * order into each writemask channel.
307 if ((alu
->op
== nir_op_vec2
) ||
308 (alu
->op
== nir_op_vec3
) ||
309 (alu
->op
== nir_op_vec4
)) {
311 for (int i
= 0; i
< info
->num_inputs
; i
++) {
312 nir_alu_src
*asrc
= &alu
->src
[i
];
314 compile_assert(ctx
, !asrc
->abs
);
315 compile_assert(ctx
, !asrc
->negate
);
317 src
[i
] = ir3_get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
319 src
[i
] = create_immed_typed(ctx
->block
, 0, dst_type
);
320 dst
[i
] = ir3_MOV(b
, src
[i
], dst_type
);
323 ir3_put_dst(ctx
, &alu
->dest
.dest
);
327 /* We also get mov's with more than one component for mov's so
328 * handle those specially:
330 if (alu
->op
== nir_op_mov
) {
331 nir_alu_src
*asrc
= &alu
->src
[0];
332 struct ir3_instruction
*const *src0
= ir3_get_src(ctx
, &asrc
->src
);
334 for (unsigned i
= 0; i
< dst_sz
; i
++) {
335 if (wrmask
& (1 << i
)) {
336 dst
[i
] = ir3_MOV(b
, src0
[asrc
->swizzle
[i
]], dst_type
);
342 ir3_put_dst(ctx
, &alu
->dest
.dest
);
346 /* General case: We can just grab the one used channel per src. */
347 for (int i
= 0; i
< info
->num_inputs
; i
++) {
348 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
349 nir_alu_src
*asrc
= &alu
->src
[i
];
351 compile_assert(ctx
, !asrc
->abs
);
352 compile_assert(ctx
, !asrc
->negate
);
354 src
[i
] = ir3_get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
355 bs
[i
] = nir_src_bit_size(asrc
->src
);
357 compile_assert(ctx
, src
[i
]);
362 case nir_op_f2f16_rtne
:
363 case nir_op_f2f16_rtz
:
381 dst
[0] = create_cov(ctx
, src
[0], bs
[0], alu
->op
);
383 case nir_op_fquantize2f16
:
384 dst
[0] = create_cov(ctx
,
385 create_cov(ctx
, src
[0], 32, nir_op_f2f16
),
389 struct ir3_instruction
*zero
= create_immed_typed(b
, 0, TYPE_F16
);
390 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, zero
, 0);
391 dst
[0]->cat2
.condition
= IR3_COND_NE
;
395 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
396 dst
[0]->cat2
.condition
= IR3_COND_NE
;
399 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F16
);
402 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
407 dst
[0] = ir3_b2n(b
, src
[0]);
410 struct ir3_instruction
*zero
= create_immed_typed(b
, 0, TYPE_S16
);
411 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, zero
, 0);
412 dst
[0]->cat2
.condition
= IR3_COND_NE
;
416 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
417 dst
[0]->cat2
.condition
= IR3_COND_NE
;
421 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
424 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
427 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
430 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
433 /* if there is just a single use of the src, and it supports
434 * (sat) bit, we can just fold the (sat) flag back to the
435 * src instruction and create a mov. This is easier for cp
438 * TODO probably opc_cat==4 is ok too
440 if (alu
->src
[0].src
.is_ssa
&&
441 (list_length(&alu
->src
[0].src
.ssa
->uses
) == 1) &&
442 ((opc_cat(src
[0]->opc
) == 2) || (opc_cat(src
[0]->opc
) == 3))) {
443 src
[0]->flags
|= IR3_INSTR_SAT
;
444 dst
[0] = ir3_MOV(b
, src
[0], dst_type
);
446 /* otherwise generate a max.f that saturates.. blob does
447 * similar (generating a cat2 mov using max.f)
449 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[0], 0);
450 dst
[0]->flags
|= IR3_INSTR_SAT
;
454 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
457 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
460 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
463 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
466 case nir_op_fddx_coarse
:
467 dst
[0] = ir3_DSX(b
, src
[0], 0);
468 dst
[0]->cat5
.type
= TYPE_F32
;
470 case nir_op_fddx_fine
:
471 dst
[0] = ir3_DSXPP_1(b
, src
[0], 0);
472 dst
[0]->cat5
.type
= TYPE_F32
;
475 case nir_op_fddy_coarse
:
476 dst
[0] = ir3_DSY(b
, src
[0], 0);
477 dst
[0]->cat5
.type
= TYPE_F32
;
480 case nir_op_fddy_fine
:
481 dst
[0] = ir3_DSYPP_1(b
, src
[0], 0);
482 dst
[0]->cat5
.type
= TYPE_F32
;
486 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
487 dst
[0]->cat2
.condition
= IR3_COND_LT
;
491 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
492 dst
[0]->cat2
.condition
= IR3_COND_GE
;
496 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
497 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
501 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
502 dst
[0]->cat2
.condition
= IR3_COND_NE
;
505 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
508 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
511 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
513 case nir_op_fround_even
:
514 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
517 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
521 dst
[0] = ir3_SIN(b
, src
[0], 0);
524 dst
[0] = ir3_COS(b
, src
[0], 0);
527 dst
[0] = ir3_RSQ(b
, src
[0], 0);
530 dst
[0] = ir3_RCP(b
, src
[0], 0);
533 dst
[0] = ir3_LOG2(b
, src
[0], 0);
536 dst
[0] = ir3_EXP2(b
, src
[0], 0);
539 dst
[0] = ir3_SQRT(b
, src
[0], 0);
543 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
546 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
549 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
552 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
555 dst
[0] = ir3_MAX_U(b
, src
[0], 0, src
[1], 0);
558 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
561 dst
[0] = ir3_MIN_U(b
, src
[0], 0, src
[1], 0);
563 case nir_op_umul_low
:
564 dst
[0] = ir3_MULL_U(b
, src
[0], 0, src
[1], 0);
566 case nir_op_imadsh_mix16
:
567 dst
[0] = ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0, src
[2], 0);
569 case nir_op_imad24_ir3
:
570 dst
[0] = ir3_MAD_S24(b
, src
[0], 0, src
[1], 0, src
[2], 0);
573 dst
[0] = ir3_MUL_S24(b
, src
[0], 0, src
[1], 0);
576 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
579 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
582 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
585 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
588 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
591 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
594 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
597 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
601 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
602 dst
[0]->cat2
.condition
= IR3_COND_LT
;
606 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
607 dst
[0]->cat2
.condition
= IR3_COND_GE
;
611 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
612 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
616 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
617 dst
[0]->cat2
.condition
= IR3_COND_NE
;
621 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
622 dst
[0]->cat2
.condition
= IR3_COND_LT
;
626 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
627 dst
[0]->cat2
.condition
= IR3_COND_GE
;
631 case nir_op_b32csel
: {
632 struct ir3_instruction
*cond
= ir3_b2n(b
, src
[0]);
634 if ((src
[0]->regs
[0]->flags
& IR3_REG_HALF
))
635 cond
->regs
[0]->flags
|= IR3_REG_HALF
;
637 compile_assert(ctx
, bs
[1] == bs
[2]);
638 /* Make sure the boolean condition has the same bit size as the other
639 * two arguments, adding a conversion if necessary.
642 cond
= ir3_COV(b
, cond
, TYPE_U32
, TYPE_U16
);
643 else if (bs
[1] > bs
[0])
644 cond
= ir3_COV(b
, cond
, TYPE_U16
, TYPE_U32
);
647 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, cond
, 0, src
[2], 0);
649 dst
[0] = ir3_SEL_B16(b
, src
[1], 0, cond
, 0, src
[2], 0);
652 case nir_op_bit_count
: {
653 // TODO, we need to do this 16b at a time on a5xx+a6xx.. need to
654 // double check on earlier gen's. Once half-precision support is
655 // in place, this should probably move to a NIR lowering pass:
656 struct ir3_instruction
*hi
, *lo
;
658 hi
= ir3_COV(b
, ir3_SHR_B(b
, src
[0], 0, create_immed(b
, 16), 0),
660 lo
= ir3_COV(b
, src
[0], TYPE_U32
, TYPE_U16
);
662 hi
= ir3_CBITS_B(b
, hi
, 0);
663 lo
= ir3_CBITS_B(b
, lo
, 0);
665 // TODO maybe the builders should default to making dst half-precision
666 // if the src's were half precision, to make this less awkward.. otoh
667 // we should probably just do this lowering in NIR.
668 hi
->regs
[0]->flags
|= IR3_REG_HALF
;
669 lo
->regs
[0]->flags
|= IR3_REG_HALF
;
671 dst
[0] = ir3_ADD_S(b
, hi
, 0, lo
, 0);
672 dst
[0]->regs
[0]->flags
|= IR3_REG_HALF
;
673 dst
[0] = ir3_COV(b
, dst
[0], TYPE_U16
, TYPE_U32
);
676 case nir_op_ifind_msb
: {
677 struct ir3_instruction
*cmp
;
678 dst
[0] = ir3_CLZ_S(b
, src
[0], 0);
679 cmp
= ir3_CMPS_S(b
, dst
[0], 0, create_immed(b
, 0), 0);
680 cmp
->cat2
.condition
= IR3_COND_GE
;
681 dst
[0] = ir3_SEL_B32(b
,
682 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
686 case nir_op_ufind_msb
:
687 dst
[0] = ir3_CLZ_B(b
, src
[0], 0);
688 dst
[0] = ir3_SEL_B32(b
,
689 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
690 src
[0], 0, dst
[0], 0);
692 case nir_op_find_lsb
:
693 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
694 dst
[0] = ir3_CLZ_B(b
, dst
[0], 0);
696 case nir_op_bitfield_reverse
:
697 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
701 ir3_context_error(ctx
, "Unhandled ALU op: %s\n",
702 nir_op_infos
[alu
->op
].name
);
706 if (nir_alu_type_get_base_type(info
->output_type
) == nir_type_bool
) {
709 if (nir_dest_bit_size(alu
->dest
.dest
) < 32)
710 dst
[0]->regs
[0]->flags
|= IR3_REG_HALF
;
712 dst
[0] = ir3_n2b(b
, dst
[0]);
715 if (nir_dest_bit_size(alu
->dest
.dest
) < 32) {
716 for (unsigned i
= 0; i
< dst_sz
; i
++) {
717 dst
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
721 ir3_put_dst(ctx
, &alu
->dest
.dest
);
724 /* handles direct/indirect UBO reads: */
726 emit_intrinsic_load_ubo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
727 struct ir3_instruction
**dst
)
729 struct ir3_block
*b
= ctx
->block
;
730 struct ir3_instruction
*base_lo
, *base_hi
, *addr
, *src0
, *src1
;
731 /* UBO addresses are the first driver params, but subtract 2 here to
732 * account for nir_lower_uniforms_to_ubo rebasing the UBOs such that UBO 0
733 * is the uniforms: */
734 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
735 unsigned ubo
= regid(const_state
->offsets
.ubo
, 0) - 2;
736 const unsigned ptrsz
= ir3_pointer_size(ctx
->compiler
);
740 /* First src is ubo index, which could either be an immed or not: */
741 src0
= ir3_get_src(ctx
, &intr
->src
[0])[0];
742 if (is_same_type_mov(src0
) &&
743 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
744 base_lo
= create_uniform(b
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
));
745 base_hi
= create_uniform(b
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
) + 1);
747 base_lo
= create_uniform_indirect(b
, ubo
, ir3_get_addr(ctx
, src0
, ptrsz
));
748 base_hi
= create_uniform_indirect(b
, ubo
+ 1, ir3_get_addr(ctx
, src0
, ptrsz
));
750 /* NOTE: since relative addressing is used, make sure constlen is
751 * at least big enough to cover all the UBO addresses, since the
752 * assembler won't know what the max address reg is.
754 ctx
->so
->constlen
= MAX2(ctx
->so
->constlen
,
755 const_state
->offsets
.ubo
+ (ctx
->s
->info
.num_ubos
* ptrsz
));
758 /* note: on 32bit gpu's base_hi is ignored and DCE'd */
761 if (nir_src_is_const(intr
->src
[1])) {
762 off
+= nir_src_as_uint(intr
->src
[1]);
764 /* For load_ubo_indirect, second src is indirect offset: */
765 src1
= ir3_get_src(ctx
, &intr
->src
[1])[0];
767 /* and add offset to addr: */
768 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
771 /* if offset is to large to encode in the ldg, split it out: */
772 if ((off
+ (intr
->num_components
* 4)) > 1024) {
773 /* split out the minimal amount to improve the odds that
774 * cp can fit the immediate in the add.s instruction:
776 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
777 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
782 struct ir3_instruction
*carry
;
784 /* handle 32b rollover, ie:
785 * if (addr < base_lo)
788 carry
= ir3_CMPS_U(b
, addr
, 0, base_lo
, 0);
789 carry
->cat2
.condition
= IR3_COND_LT
;
790 base_hi
= ir3_ADD_S(b
, base_hi
, 0, carry
, 0);
792 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){ addr
, base_hi
}, 2);
795 for (int i
= 0; i
< intr
->num_components
; i
++) {
796 struct ir3_instruction
*load
=
797 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0, /* num components */
798 create_immed(b
, off
+ i
* 4), 0);
799 load
->cat6
.type
= TYPE_U32
;
804 /* src[] = { block_index } */
806 emit_intrinsic_ssbo_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
807 struct ir3_instruction
**dst
)
809 /* SSBO size stored as a const starting at ssbo_sizes: */
810 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
811 unsigned blk_idx
= nir_src_as_uint(intr
->src
[0]);
812 unsigned idx
= regid(const_state
->offsets
.ssbo_sizes
, 0) +
813 const_state
->ssbo_size
.off
[blk_idx
];
815 debug_assert(const_state
->ssbo_size
.mask
& (1 << blk_idx
));
817 dst
[0] = create_uniform(ctx
->block
, idx
);
820 /* src[] = { offset }. const_index[] = { base } */
822 emit_intrinsic_load_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
823 struct ir3_instruction
**dst
)
825 struct ir3_block
*b
= ctx
->block
;
826 struct ir3_instruction
*ldl
, *offset
;
829 offset
= ir3_get_src(ctx
, &intr
->src
[0])[0];
830 base
= nir_intrinsic_base(intr
);
832 ldl
= ir3_LDL(b
, offset
, 0,
833 create_immed(b
, intr
->num_components
), 0,
834 create_immed(b
, base
), 0);
836 ldl
->cat6
.type
= utype_dst(intr
->dest
);
837 ldl
->regs
[0]->wrmask
= MASK(intr
->num_components
);
839 ldl
->barrier_class
= IR3_BARRIER_SHARED_R
;
840 ldl
->barrier_conflict
= IR3_BARRIER_SHARED_W
;
842 ir3_split_dest(b
, dst
, ldl
, 0, intr
->num_components
);
845 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
847 emit_intrinsic_store_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
849 struct ir3_block
*b
= ctx
->block
;
850 struct ir3_instruction
*stl
, *offset
;
851 struct ir3_instruction
* const *value
;
852 unsigned base
, wrmask
;
854 value
= ir3_get_src(ctx
, &intr
->src
[0]);
855 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
857 base
= nir_intrinsic_base(intr
);
858 wrmask
= nir_intrinsic_write_mask(intr
);
860 /* Combine groups of consecutive enabled channels in one write
861 * message. We use ffs to find the first enabled channel and then ffs on
862 * the bit-inverse, down-shifted writemask to determine the length of
863 * the block of enabled bits.
865 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
868 unsigned first_component
= ffs(wrmask
) - 1;
869 unsigned length
= ffs(~(wrmask
>> first_component
)) - 1;
871 stl
= ir3_STL(b
, offset
, 0,
872 ir3_create_collect(ctx
, &value
[first_component
], length
), 0,
873 create_immed(b
, length
), 0);
874 stl
->cat6
.dst_offset
= first_component
+ base
;
875 stl
->cat6
.type
= utype_src(intr
->src
[0]);
876 stl
->barrier_class
= IR3_BARRIER_SHARED_W
;
877 stl
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
879 array_insert(b
, b
->keeps
, stl
);
881 /* Clear the bits in the writemask that we just wrote, then try
882 * again to see if more channels are left.
884 wrmask
&= (15 << (first_component
+ length
));
888 /* src[] = { offset }. const_index[] = { base } */
890 emit_intrinsic_load_shared_ir3(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
891 struct ir3_instruction
**dst
)
893 struct ir3_block
*b
= ctx
->block
;
894 struct ir3_instruction
*load
, *offset
;
897 offset
= ir3_get_src(ctx
, &intr
->src
[0])[0];
898 base
= nir_intrinsic_base(intr
);
900 load
= ir3_LDLW(b
, offset
, 0,
901 create_immed(b
, intr
->num_components
), 0,
902 create_immed(b
, base
), 0);
904 load
->cat6
.type
= utype_dst(intr
->dest
);
905 load
->regs
[0]->wrmask
= MASK(intr
->num_components
);
907 load
->barrier_class
= IR3_BARRIER_SHARED_R
;
908 load
->barrier_conflict
= IR3_BARRIER_SHARED_W
;
910 ir3_split_dest(b
, dst
, load
, 0, intr
->num_components
);
913 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
915 emit_intrinsic_store_shared_ir3(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
917 struct ir3_block
*b
= ctx
->block
;
918 struct ir3_instruction
*store
, *offset
;
919 struct ir3_instruction
* const *value
;
920 unsigned base
, wrmask
;
922 value
= ir3_get_src(ctx
, &intr
->src
[0]);
923 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
925 base
= nir_intrinsic_base(intr
);
926 wrmask
= nir_intrinsic_write_mask(intr
);
928 /* Combine groups of consecutive enabled channels in one write
929 * message. We use ffs to find the first enabled channel and then ffs on
930 * the bit-inverse, down-shifted writemask to determine the length of
931 * the block of enabled bits.
933 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
936 unsigned first_component
= ffs(wrmask
) - 1;
937 unsigned length
= ffs(~(wrmask
>> first_component
)) - 1;
939 store
= ir3_STLW(b
, offset
, 0,
940 ir3_create_collect(ctx
, &value
[first_component
], length
), 0,
941 create_immed(b
, length
), 0);
943 store
->cat6
.dst_offset
= first_component
+ base
;
944 store
->cat6
.type
= utype_src(intr
->src
[0]);
945 store
->barrier_class
= IR3_BARRIER_SHARED_W
;
946 store
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
948 array_insert(b
, b
->keeps
, store
);
950 /* Clear the bits in the writemask that we just wrote, then try
951 * again to see if more channels are left.
953 wrmask
&= (15 << (first_component
+ length
));
958 * CS shared variable atomic intrinsics
960 * All of the shared variable atomic memory operations read a value from
961 * memory, compute a new value using one of the operations below, write the
962 * new value to memory, and return the original value read.
964 * All operations take 2 sources except CompSwap that takes 3. These
967 * 0: The offset into the shared variable storage region that the atomic
968 * operation will operate on.
969 * 1: The data parameter to the atomic function (i.e. the value to add
970 * in shared_atomic_add, etc).
971 * 2: For CompSwap only: the second data parameter.
973 static struct ir3_instruction
*
974 emit_intrinsic_atomic_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
976 struct ir3_block
*b
= ctx
->block
;
977 struct ir3_instruction
*atomic
, *src0
, *src1
;
978 type_t type
= TYPE_U32
;
980 src0
= ir3_get_src(ctx
, &intr
->src
[0])[0]; /* offset */
981 src1
= ir3_get_src(ctx
, &intr
->src
[1])[0]; /* value */
983 switch (intr
->intrinsic
) {
984 case nir_intrinsic_shared_atomic_add
:
985 atomic
= ir3_ATOMIC_ADD(b
, src0
, 0, src1
, 0);
987 case nir_intrinsic_shared_atomic_imin
:
988 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
991 case nir_intrinsic_shared_atomic_umin
:
992 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
994 case nir_intrinsic_shared_atomic_imax
:
995 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
998 case nir_intrinsic_shared_atomic_umax
:
999 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
1001 case nir_intrinsic_shared_atomic_and
:
1002 atomic
= ir3_ATOMIC_AND(b
, src0
, 0, src1
, 0);
1004 case nir_intrinsic_shared_atomic_or
:
1005 atomic
= ir3_ATOMIC_OR(b
, src0
, 0, src1
, 0);
1007 case nir_intrinsic_shared_atomic_xor
:
1008 atomic
= ir3_ATOMIC_XOR(b
, src0
, 0, src1
, 0);
1010 case nir_intrinsic_shared_atomic_exchange
:
1011 atomic
= ir3_ATOMIC_XCHG(b
, src0
, 0, src1
, 0);
1013 case nir_intrinsic_shared_atomic_comp_swap
:
1014 /* for cmpxchg, src1 is [ui]vec2(data, compare): */
1015 src1
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1016 ir3_get_src(ctx
, &intr
->src
[2])[0],
1019 atomic
= ir3_ATOMIC_CMPXCHG(b
, src0
, 0, src1
, 0);
1025 atomic
->cat6
.iim_val
= 1;
1027 atomic
->cat6
.type
= type
;
1028 atomic
->barrier_class
= IR3_BARRIER_SHARED_W
;
1029 atomic
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
1031 /* even if nothing consume the result, we can't DCE the instruction: */
1032 array_insert(b
, b
->keeps
, atomic
);
1037 /* TODO handle actual indirect/dynamic case.. which is going to be weird
1038 * to handle with the image_mapping table..
1040 static struct ir3_instruction
*
1041 get_image_samp_tex_src(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1043 unsigned slot
= nir_src_as_uint(intr
->src
[0]);
1044 unsigned tex_idx
= ir3_image_to_tex(&ctx
->so
->image_mapping
, slot
);
1045 struct ir3_instruction
*texture
, *sampler
;
1047 texture
= create_immed_typed(ctx
->block
, tex_idx
, TYPE_U16
);
1048 sampler
= create_immed_typed(ctx
->block
, tex_idx
, TYPE_U16
);
1050 return ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1056 /* src[] = { deref, coord, sample_index }. const_index[] = {} */
1058 emit_intrinsic_load_image(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1059 struct ir3_instruction
**dst
)
1061 struct ir3_block
*b
= ctx
->block
;
1062 struct ir3_instruction
*samp_tex
= get_image_samp_tex_src(ctx
, intr
);
1063 struct ir3_instruction
*sam
;
1064 struct ir3_instruction
* const *src0
= ir3_get_src(ctx
, &intr
->src
[1]);
1065 struct ir3_instruction
*coords
[4];
1066 unsigned flags
, ncoords
= ir3_get_image_coords(intr
, &flags
);
1067 type_t type
= ir3_get_type_for_image_intrinsic(intr
);
1069 /* hmm, this seems a bit odd, but it is what blob does and (at least
1070 * a5xx) just faults on bogus addresses otherwise:
1072 if (flags
& IR3_INSTR_3D
) {
1073 flags
&= ~IR3_INSTR_3D
;
1074 flags
|= IR3_INSTR_A
;
1077 for (unsigned i
= 0; i
< ncoords
; i
++)
1078 coords
[i
] = src0
[i
];
1081 coords
[ncoords
++] = create_immed(b
, 0);
1083 sam
= ir3_SAM(b
, OPC_ISAM
, type
, 0b1111, flags
,
1084 samp_tex
, ir3_create_collect(ctx
, coords
, ncoords
), NULL
);
1086 sam
->barrier_class
= IR3_BARRIER_IMAGE_R
;
1087 sam
->barrier_conflict
= IR3_BARRIER_IMAGE_W
;
1089 ir3_split_dest(b
, dst
, sam
, 0, 4);
1093 emit_intrinsic_image_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1094 struct ir3_instruction
**dst
)
1096 struct ir3_block
*b
= ctx
->block
;
1097 struct ir3_instruction
*samp_tex
= get_image_samp_tex_src(ctx
, intr
);
1098 struct ir3_instruction
*sam
, *lod
;
1099 unsigned flags
, ncoords
= ir3_get_image_coords(intr
, &flags
);
1100 type_t dst_type
= nir_dest_bit_size(intr
->dest
) < 32 ?
1101 TYPE_U16
: TYPE_U32
;
1103 lod
= create_immed(b
, 0);
1104 sam
= ir3_SAM(b
, OPC_GETSIZE
, dst_type
, 0b1111, flags
,
1105 samp_tex
, lod
, NULL
);
1107 /* Array size actually ends up in .w rather than .z. This doesn't
1108 * matter for miplevel 0, but for higher mips the value in z is
1109 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1110 * returned, which means that we have to add 1 to it for arrays for
1113 * Note use a temporary dst and then copy, since the size of the dst
1114 * array that is passed in is based on nir's understanding of the
1115 * result size, not the hardware's
1117 struct ir3_instruction
*tmp
[4];
1119 ir3_split_dest(b
, tmp
, sam
, 0, 4);
1121 /* get_size instruction returns size in bytes instead of texels
1122 * for imageBuffer, so we need to divide it by the pixel size
1123 * of the image format.
1125 * TODO: This is at least true on a5xx. Check other gens.
1127 if (nir_intrinsic_image_dim(intr
) == GLSL_SAMPLER_DIM_BUF
) {
1128 /* Since all the possible values the divisor can take are
1129 * power-of-two (4, 8, or 16), the division is implemented
1131 * During shader setup, the log2 of the image format's
1132 * bytes-per-pixel should have been emitted in 2nd slot of
1133 * image_dims. See ir3_shader::emit_image_dims().
1135 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
1136 unsigned cb
= regid(const_state
->offsets
.image_dims
, 0) +
1137 const_state
->image_dims
.off
[nir_src_as_uint(intr
->src
[0])];
1138 struct ir3_instruction
*aux
= create_uniform(b
, cb
+ 1);
1140 tmp
[0] = ir3_SHR_B(b
, tmp
[0], 0, aux
, 0);
1143 for (unsigned i
= 0; i
< ncoords
; i
++)
1146 if (flags
& IR3_INSTR_A
) {
1147 if (ctx
->compiler
->levels_add_one
) {
1148 dst
[ncoords
-1] = ir3_ADD_U(b
, tmp
[3], 0, create_immed(b
, 1), 0);
1150 dst
[ncoords
-1] = ir3_MOV(b
, tmp
[3], TYPE_U32
);
1156 emit_intrinsic_barrier(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1158 struct ir3_block
*b
= ctx
->block
;
1159 struct ir3_instruction
*barrier
;
1161 switch (intr
->intrinsic
) {
1162 case nir_intrinsic_control_barrier
:
1163 barrier
= ir3_BAR(b
);
1164 barrier
->cat7
.g
= true;
1165 barrier
->cat7
.l
= true;
1166 barrier
->flags
= IR3_INSTR_SS
| IR3_INSTR_SY
;
1167 barrier
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1169 case nir_intrinsic_memory_barrier
:
1170 barrier
= ir3_FENCE(b
);
1171 barrier
->cat7
.g
= true;
1172 barrier
->cat7
.r
= true;
1173 barrier
->cat7
.w
= true;
1174 barrier
->cat7
.l
= true;
1175 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
|
1176 IR3_BARRIER_BUFFER_W
;
1177 barrier
->barrier_conflict
=
1178 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
1179 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1181 case nir_intrinsic_memory_barrier_buffer
:
1182 barrier
= ir3_FENCE(b
);
1183 barrier
->cat7
.g
= true;
1184 barrier
->cat7
.r
= true;
1185 barrier
->cat7
.w
= true;
1186 barrier
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1187 barrier
->barrier_conflict
= IR3_BARRIER_BUFFER_R
|
1188 IR3_BARRIER_BUFFER_W
;
1190 case nir_intrinsic_memory_barrier_image
:
1191 // TODO double check if this should have .g set
1192 barrier
= ir3_FENCE(b
);
1193 barrier
->cat7
.g
= true;
1194 barrier
->cat7
.r
= true;
1195 barrier
->cat7
.w
= true;
1196 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
;
1197 barrier
->barrier_conflict
= IR3_BARRIER_IMAGE_R
|
1198 IR3_BARRIER_IMAGE_W
;
1200 case nir_intrinsic_memory_barrier_shared
:
1201 barrier
= ir3_FENCE(b
);
1202 barrier
->cat7
.g
= true;
1203 barrier
->cat7
.l
= true;
1204 barrier
->cat7
.r
= true;
1205 barrier
->cat7
.w
= true;
1206 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
;
1207 barrier
->barrier_conflict
= IR3_BARRIER_SHARED_R
|
1208 IR3_BARRIER_SHARED_W
;
1210 case nir_intrinsic_group_memory_barrier
:
1211 barrier
= ir3_FENCE(b
);
1212 barrier
->cat7
.g
= true;
1213 barrier
->cat7
.l
= true;
1214 barrier
->cat7
.r
= true;
1215 barrier
->cat7
.w
= true;
1216 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
|
1217 IR3_BARRIER_IMAGE_W
|
1218 IR3_BARRIER_BUFFER_W
;
1219 barrier
->barrier_conflict
=
1220 IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
|
1221 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
1222 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1228 /* make sure barrier doesn't get DCE'd */
1229 array_insert(b
, b
->keeps
, barrier
);
1232 static void add_sysval_input_compmask(struct ir3_context
*ctx
,
1233 gl_system_value slot
, unsigned compmask
,
1234 struct ir3_instruction
*instr
)
1236 struct ir3_shader_variant
*so
= ctx
->so
;
1237 unsigned n
= so
->inputs_count
++;
1239 assert(instr
->opc
== OPC_META_INPUT
);
1240 instr
->input
.inidx
= n
;
1241 instr
->input
.sysval
= slot
;
1243 so
->inputs
[n
].sysval
= true;
1244 so
->inputs
[n
].slot
= slot
;
1245 so
->inputs
[n
].compmask
= compmask
;
1246 so
->inputs
[n
].interpolate
= INTERP_MODE_FLAT
;
1250 static struct ir3_instruction
*
1251 create_sysval_input(struct ir3_context
*ctx
, gl_system_value slot
,
1255 struct ir3_instruction
*sysval
= create_input(ctx
, compmask
);
1256 add_sysval_input_compmask(ctx
, slot
, compmask
, sysval
);
1260 static struct ir3_instruction
*
1261 get_barycentric_centroid(struct ir3_context
*ctx
)
1263 if (!ctx
->ij_centroid
) {
1264 struct ir3_instruction
*xy
[2];
1265 struct ir3_instruction
*ij
;
1267 ij
= create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
, 0x3);
1268 ir3_split_dest(ctx
->block
, xy
, ij
, 0, 2);
1270 ctx
->ij_centroid
= ir3_create_collect(ctx
, xy
, 2);
1273 return ctx
->ij_centroid
;
1276 static struct ir3_instruction
*
1277 get_barycentric_sample(struct ir3_context
*ctx
)
1279 if (!ctx
->ij_sample
) {
1280 struct ir3_instruction
*xy
[2];
1281 struct ir3_instruction
*ij
;
1283 ij
= create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE
, 0x3);
1284 ir3_split_dest(ctx
->block
, xy
, ij
, 0, 2);
1286 ctx
->ij_sample
= ir3_create_collect(ctx
, xy
, 2);
1289 return ctx
->ij_sample
;
1292 static struct ir3_instruction
*
1293 get_barycentric_pixel(struct ir3_context
*ctx
)
1295 /* TODO when tgsi_to_nir supports "new-style" FS inputs switch
1296 * this to create ij_pixel only on demand:
1298 return ctx
->ij_pixel
;
1301 static struct ir3_instruction
*
1302 get_frag_coord(struct ir3_context
*ctx
)
1304 if (!ctx
->frag_coord
) {
1305 struct ir3_block
*b
= ctx
->in_block
;
1306 struct ir3_instruction
*xyzw
[4];
1307 struct ir3_instruction
*hw_frag_coord
;
1309 hw_frag_coord
= create_sysval_input(ctx
, SYSTEM_VALUE_FRAG_COORD
, 0xf);
1310 ir3_split_dest(b
, xyzw
, hw_frag_coord
, 0, 4);
1312 /* for frag_coord.xy, we get unsigned values.. we need
1313 * to subtract (integer) 8 and divide by 16 (right-
1314 * shift by 4) then convert to float:
1318 * mov.u32f32 dst, tmp
1321 for (int i
= 0; i
< 2; i
++) {
1322 xyzw
[i
] = ir3_SUB_S(b
, xyzw
[i
], 0,
1323 create_immed(b
, 8), 0);
1324 xyzw
[i
] = ir3_SHR_B(b
, xyzw
[i
], 0,
1325 create_immed(b
, 4), 0);
1326 xyzw
[i
] = ir3_COV(b
, xyzw
[i
], TYPE_U32
, TYPE_F32
);
1329 ctx
->frag_coord
= ir3_create_collect(ctx
, xyzw
, 4);
1330 ctx
->so
->frag_coord
= true;
1333 return ctx
->frag_coord
;
1337 emit_intrinsic(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1339 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1340 struct ir3_instruction
**dst
;
1341 struct ir3_instruction
* const *src
;
1342 struct ir3_block
*b
= ctx
->block
;
1345 if (info
->has_dest
) {
1346 unsigned n
= nir_intrinsic_dest_components(intr
);
1347 dst
= ir3_get_dst(ctx
, &intr
->dest
, n
);
1352 const unsigned primitive_param
= ctx
->so
->shader
->const_state
.offsets
.primitive_param
* 4;
1353 const unsigned primitive_map
= ctx
->so
->shader
->const_state
.offsets
.primitive_map
* 4;
1355 switch (intr
->intrinsic
) {
1356 case nir_intrinsic_load_uniform
:
1357 idx
= nir_intrinsic_base(intr
);
1358 if (nir_src_is_const(intr
->src
[0])) {
1359 idx
+= nir_src_as_uint(intr
->src
[0]);
1360 for (int i
= 0; i
< intr
->num_components
; i
++) {
1361 dst
[i
] = create_uniform_typed(b
, idx
+ i
,
1362 nir_dest_bit_size(intr
->dest
) < 32 ? TYPE_F16
: TYPE_F32
);
1365 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1366 for (int i
= 0; i
< intr
->num_components
; i
++) {
1367 dst
[i
] = create_uniform_indirect(b
, idx
+ i
,
1368 ir3_get_addr(ctx
, src
[0], 1));
1370 /* NOTE: if relative addressing is used, we set
1371 * constlen in the compiler (to worst-case value)
1372 * since we don't know in the assembler what the max
1373 * addr reg value can be:
1375 ctx
->so
->constlen
= MAX2(ctx
->so
->constlen
,
1376 ctx
->so
->shader
->ubo_state
.size
/ 16);
1380 case nir_intrinsic_load_vs_primitive_stride_ir3
:
1381 dst
[0] = create_uniform(b
, primitive_param
+ 0);
1383 case nir_intrinsic_load_vs_vertex_stride_ir3
:
1384 dst
[0] = create_uniform(b
, primitive_param
+ 1);
1386 case nir_intrinsic_load_hs_patch_stride_ir3
:
1387 dst
[0] = create_uniform(b
, primitive_param
+ 2);
1389 case nir_intrinsic_load_patch_vertices_in
:
1390 dst
[0] = create_uniform(b
, primitive_param
+ 3);
1392 case nir_intrinsic_load_tess_param_base_ir3
:
1393 dst
[0] = create_uniform(b
, primitive_param
+ 4);
1394 dst
[1] = create_uniform(b
, primitive_param
+ 5);
1396 case nir_intrinsic_load_tess_factor_base_ir3
:
1397 dst
[0] = create_uniform(b
, primitive_param
+ 6);
1398 dst
[1] = create_uniform(b
, primitive_param
+ 7);
1401 case nir_intrinsic_load_primitive_location_ir3
:
1402 idx
= nir_intrinsic_driver_location(intr
);
1403 dst
[0] = create_uniform(b
, primitive_map
+ idx
);
1406 case nir_intrinsic_load_gs_header_ir3
:
1407 dst
[0] = ctx
->gs_header
;
1409 case nir_intrinsic_load_tcs_header_ir3
:
1410 dst
[0] = ctx
->tcs_header
;
1413 case nir_intrinsic_load_primitive_id
:
1414 dst
[0] = ctx
->primitive_id
;
1417 case nir_intrinsic_load_tess_coord
:
1418 if (!ctx
->tess_coord
) {
1420 create_sysval_input(ctx
, SYSTEM_VALUE_TESS_COORD
, 0x3);
1422 ir3_split_dest(b
, dst
, ctx
->tess_coord
, 0, 2);
1424 /* Unused, but ir3_put_dst() below wants to free something */
1425 dst
[2] = create_immed(b
, 0);
1428 case nir_intrinsic_end_patch_ir3
:
1429 assert(ctx
->so
->type
== MESA_SHADER_TESS_CTRL
);
1430 struct ir3_instruction
*end
= ir3_ENDIF(b
);
1431 array_insert(b
, b
->keeps
, end
);
1433 end
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1434 end
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
1437 case nir_intrinsic_store_global_ir3
: {
1438 struct ir3_instruction
*value
, *addr
, *offset
;
1440 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1441 ir3_get_src(ctx
, &intr
->src
[1])[0],
1442 ir3_get_src(ctx
, &intr
->src
[1])[1]
1445 offset
= ir3_get_src(ctx
, &intr
->src
[2])[0];
1447 value
= ir3_create_collect(ctx
, ir3_get_src(ctx
, &intr
->src
[0]),
1448 intr
->num_components
);
1450 struct ir3_instruction
*stg
=
1451 ir3_STG_G(ctx
->block
, addr
, 0, value
, 0,
1452 create_immed(ctx
->block
, intr
->num_components
), 0, offset
, 0);
1453 stg
->cat6
.type
= TYPE_U32
;
1454 stg
->cat6
.iim_val
= 1;
1456 array_insert(b
, b
->keeps
, stg
);
1458 stg
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1459 stg
->barrier_conflict
= IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1463 case nir_intrinsic_load_global_ir3
: {
1464 struct ir3_instruction
*addr
, *offset
;
1466 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1467 ir3_get_src(ctx
, &intr
->src
[0])[0],
1468 ir3_get_src(ctx
, &intr
->src
[0])[1]
1471 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
1473 struct ir3_instruction
*load
=
1474 ir3_LDG(b
, addr
, 0, create_immed(ctx
->block
, intr
->num_components
),
1476 load
->cat6
.type
= TYPE_U32
;
1477 load
->regs
[0]->wrmask
= MASK(intr
->num_components
);
1479 load
->barrier_class
= IR3_BARRIER_BUFFER_R
;
1480 load
->barrier_conflict
= IR3_BARRIER_BUFFER_W
;
1482 ir3_split_dest(b
, dst
, load
, 0, intr
->num_components
);
1486 case nir_intrinsic_load_ubo
:
1487 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
1489 case nir_intrinsic_load_frag_coord
:
1490 ir3_split_dest(b
, dst
, get_frag_coord(ctx
), 0, 4);
1492 case nir_intrinsic_load_sample_pos_from_id
: {
1493 /* NOTE: blob seems to always use TYPE_F16 and then cov.f16f32,
1494 * but that doesn't seem necessary.
1496 struct ir3_instruction
*offset
=
1497 ir3_RGETPOS(b
, ir3_get_src(ctx
, &intr
->src
[0])[0], 0);
1498 offset
->regs
[0]->wrmask
= 0x3;
1499 offset
->cat5
.type
= TYPE_F32
;
1501 ir3_split_dest(b
, dst
, offset
, 0, 2);
1505 case nir_intrinsic_load_size_ir3
:
1506 if (!ctx
->ij_size
) {
1508 create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_PERSP_SIZE
, 0x1);
1510 dst
[0] = ctx
->ij_size
;
1512 case nir_intrinsic_load_barycentric_centroid
:
1513 ir3_split_dest(b
, dst
, get_barycentric_centroid(ctx
), 0, 2);
1515 case nir_intrinsic_load_barycentric_sample
:
1516 if (ctx
->so
->key
.msaa
) {
1517 ir3_split_dest(b
, dst
, get_barycentric_sample(ctx
), 0, 2);
1519 ir3_split_dest(b
, dst
, get_barycentric_pixel(ctx
), 0, 2);
1522 case nir_intrinsic_load_barycentric_pixel
:
1523 ir3_split_dest(b
, dst
, get_barycentric_pixel(ctx
), 0, 2);
1525 case nir_intrinsic_load_interpolated_input
:
1526 idx
= nir_intrinsic_base(intr
);
1527 comp
= nir_intrinsic_component(intr
);
1528 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1529 if (nir_src_is_const(intr
->src
[1])) {
1530 struct ir3_instruction
*coord
= ir3_create_collect(ctx
, src
, 2);
1531 idx
+= nir_src_as_uint(intr
->src
[1]);
1532 for (int i
= 0; i
< intr
->num_components
; i
++) {
1533 unsigned inloc
= idx
* 4 + i
+ comp
;
1534 if (ctx
->so
->inputs
[idx
].bary
&&
1535 !ctx
->so
->inputs
[idx
].use_ldlv
) {
1536 dst
[i
] = ir3_BARY_F(b
, create_immed(b
, inloc
), 0, coord
, 0);
1538 /* for non-varyings use the pre-setup input, since
1539 * that is easier than mapping things back to a
1540 * nir_variable to figure out what it is.
1542 dst
[i
] = ctx
->inputs
[inloc
];
1546 ir3_context_error(ctx
, "unhandled");
1549 case nir_intrinsic_load_input
:
1550 idx
= nir_intrinsic_base(intr
);
1551 comp
= nir_intrinsic_component(intr
);
1552 if (nir_src_is_const(intr
->src
[0])) {
1553 idx
+= nir_src_as_uint(intr
->src
[0]);
1554 for (int i
= 0; i
< intr
->num_components
; i
++) {
1555 unsigned n
= idx
* 4 + i
+ comp
;
1556 dst
[i
] = ctx
->inputs
[n
];
1557 compile_assert(ctx
, ctx
->inputs
[n
]);
1560 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1561 struct ir3_instruction
*collect
=
1562 ir3_create_collect(ctx
, ctx
->ir
->inputs
, ctx
->ninputs
);
1563 struct ir3_instruction
*addr
= ir3_get_addr(ctx
, src
[0], 4);
1564 for (int i
= 0; i
< intr
->num_components
; i
++) {
1565 unsigned n
= idx
* 4 + i
+ comp
;
1566 dst
[i
] = create_indirect_load(ctx
, ctx
->ninputs
,
1571 /* All SSBO intrinsics should have been lowered by 'lower_io_offsets'
1572 * pass and replaced by an ir3-specifc version that adds the
1573 * dword-offset in the last source.
1575 case nir_intrinsic_load_ssbo_ir3
:
1576 ctx
->funcs
->emit_intrinsic_load_ssbo(ctx
, intr
, dst
);
1578 case nir_intrinsic_store_ssbo_ir3
:
1579 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1580 !ctx
->s
->info
.fs
.early_fragment_tests
)
1581 ctx
->so
->no_earlyz
= true;
1582 ctx
->funcs
->emit_intrinsic_store_ssbo(ctx
, intr
);
1584 case nir_intrinsic_get_buffer_size
:
1585 emit_intrinsic_ssbo_size(ctx
, intr
, dst
);
1587 case nir_intrinsic_ssbo_atomic_add_ir3
:
1588 case nir_intrinsic_ssbo_atomic_imin_ir3
:
1589 case nir_intrinsic_ssbo_atomic_umin_ir3
:
1590 case nir_intrinsic_ssbo_atomic_imax_ir3
:
1591 case nir_intrinsic_ssbo_atomic_umax_ir3
:
1592 case nir_intrinsic_ssbo_atomic_and_ir3
:
1593 case nir_intrinsic_ssbo_atomic_or_ir3
:
1594 case nir_intrinsic_ssbo_atomic_xor_ir3
:
1595 case nir_intrinsic_ssbo_atomic_exchange_ir3
:
1596 case nir_intrinsic_ssbo_atomic_comp_swap_ir3
:
1597 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1598 !ctx
->s
->info
.fs
.early_fragment_tests
)
1599 ctx
->so
->no_earlyz
= true;
1600 dst
[0] = ctx
->funcs
->emit_intrinsic_atomic_ssbo(ctx
, intr
);
1602 case nir_intrinsic_load_shared
:
1603 emit_intrinsic_load_shared(ctx
, intr
, dst
);
1605 case nir_intrinsic_store_shared
:
1606 emit_intrinsic_store_shared(ctx
, intr
);
1608 case nir_intrinsic_shared_atomic_add
:
1609 case nir_intrinsic_shared_atomic_imin
:
1610 case nir_intrinsic_shared_atomic_umin
:
1611 case nir_intrinsic_shared_atomic_imax
:
1612 case nir_intrinsic_shared_atomic_umax
:
1613 case nir_intrinsic_shared_atomic_and
:
1614 case nir_intrinsic_shared_atomic_or
:
1615 case nir_intrinsic_shared_atomic_xor
:
1616 case nir_intrinsic_shared_atomic_exchange
:
1617 case nir_intrinsic_shared_atomic_comp_swap
:
1618 dst
[0] = emit_intrinsic_atomic_shared(ctx
, intr
);
1620 case nir_intrinsic_image_load
:
1621 emit_intrinsic_load_image(ctx
, intr
, dst
);
1623 case nir_intrinsic_image_store
:
1624 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1625 !ctx
->s
->info
.fs
.early_fragment_tests
)
1626 ctx
->so
->no_earlyz
= true;
1627 ctx
->funcs
->emit_intrinsic_store_image(ctx
, intr
);
1629 case nir_intrinsic_image_size
:
1630 emit_intrinsic_image_size(ctx
, intr
, dst
);
1632 case nir_intrinsic_image_atomic_add
:
1633 case nir_intrinsic_image_atomic_imin
:
1634 case nir_intrinsic_image_atomic_umin
:
1635 case nir_intrinsic_image_atomic_imax
:
1636 case nir_intrinsic_image_atomic_umax
:
1637 case nir_intrinsic_image_atomic_and
:
1638 case nir_intrinsic_image_atomic_or
:
1639 case nir_intrinsic_image_atomic_xor
:
1640 case nir_intrinsic_image_atomic_exchange
:
1641 case nir_intrinsic_image_atomic_comp_swap
:
1642 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1643 !ctx
->s
->info
.fs
.early_fragment_tests
)
1644 ctx
->so
->no_earlyz
= true;
1645 dst
[0] = ctx
->funcs
->emit_intrinsic_atomic_image(ctx
, intr
);
1647 case nir_intrinsic_control_barrier
:
1648 case nir_intrinsic_memory_barrier
:
1649 case nir_intrinsic_group_memory_barrier
:
1650 case nir_intrinsic_memory_barrier_buffer
:
1651 case nir_intrinsic_memory_barrier_image
:
1652 case nir_intrinsic_memory_barrier_shared
:
1653 emit_intrinsic_barrier(ctx
, intr
);
1654 /* note that blk ptr no longer valid, make that obvious: */
1657 case nir_intrinsic_store_output
:
1658 idx
= nir_intrinsic_base(intr
);
1659 comp
= nir_intrinsic_component(intr
);
1660 compile_assert(ctx
, nir_src_is_const(intr
->src
[1]));
1661 idx
+= nir_src_as_uint(intr
->src
[1]);
1663 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1664 for (int i
= 0; i
< intr
->num_components
; i
++) {
1665 unsigned n
= idx
* 4 + i
+ comp
;
1666 ctx
->outputs
[n
] = src
[i
];
1669 case nir_intrinsic_load_base_vertex
:
1670 case nir_intrinsic_load_first_vertex
:
1671 if (!ctx
->basevertex
) {
1672 ctx
->basevertex
= create_driver_param(ctx
, IR3_DP_VTXID_BASE
);
1674 dst
[0] = ctx
->basevertex
;
1676 case nir_intrinsic_load_base_instance
:
1677 if (!ctx
->base_instance
) {
1678 ctx
->base_instance
= create_driver_param(ctx
, IR3_DP_INSTID_BASE
);
1680 dst
[0] = ctx
->base_instance
;
1682 case nir_intrinsic_load_vertex_id_zero_base
:
1683 case nir_intrinsic_load_vertex_id
:
1684 if (!ctx
->vertex_id
) {
1685 gl_system_value sv
= (intr
->intrinsic
== nir_intrinsic_load_vertex_id
) ?
1686 SYSTEM_VALUE_VERTEX_ID
: SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1687 ctx
->vertex_id
= create_sysval_input(ctx
, sv
, 0x1);
1689 dst
[0] = ctx
->vertex_id
;
1691 case nir_intrinsic_load_instance_id
:
1692 if (!ctx
->instance_id
) {
1693 ctx
->instance_id
= create_sysval_input(ctx
, SYSTEM_VALUE_INSTANCE_ID
, 0x1);
1695 dst
[0] = ctx
->instance_id
;
1697 case nir_intrinsic_load_sample_id
:
1698 ctx
->so
->per_samp
= true;
1700 case nir_intrinsic_load_sample_id_no_per_sample
:
1701 if (!ctx
->samp_id
) {
1702 ctx
->samp_id
= create_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_ID
, 0x1);
1703 ctx
->samp_id
->regs
[0]->flags
|= IR3_REG_HALF
;
1705 dst
[0] = ir3_COV(b
, ctx
->samp_id
, TYPE_U16
, TYPE_U32
);
1707 case nir_intrinsic_load_sample_mask_in
:
1708 if (!ctx
->samp_mask_in
) {
1709 ctx
->samp_mask_in
= create_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_MASK_IN
, 0x1);
1711 dst
[0] = ctx
->samp_mask_in
;
1713 case nir_intrinsic_load_user_clip_plane
:
1714 idx
= nir_intrinsic_ucp_id(intr
);
1715 for (int i
= 0; i
< intr
->num_components
; i
++) {
1716 unsigned n
= idx
* 4 + i
;
1717 dst
[i
] = create_driver_param(ctx
, IR3_DP_UCP0_X
+ n
);
1720 case nir_intrinsic_load_front_face
:
1721 if (!ctx
->frag_face
) {
1722 ctx
->so
->frag_face
= true;
1723 ctx
->frag_face
= create_sysval_input(ctx
, SYSTEM_VALUE_FRONT_FACE
, 0x1);
1724 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
1726 /* for fragface, we get -1 for back and 0 for front. However this is
1727 * the inverse of what nir expects (where ~0 is true).
1729 dst
[0] = ir3_COV(b
, ctx
->frag_face
, TYPE_S16
, TYPE_S32
);
1730 dst
[0] = ir3_NOT_B(b
, dst
[0], 0);
1732 case nir_intrinsic_load_local_invocation_id
:
1733 if (!ctx
->local_invocation_id
) {
1734 ctx
->local_invocation_id
=
1735 create_sysval_input(ctx
, SYSTEM_VALUE_LOCAL_INVOCATION_ID
, 0x7);
1737 ir3_split_dest(b
, dst
, ctx
->local_invocation_id
, 0, 3);
1739 case nir_intrinsic_load_work_group_id
:
1740 if (!ctx
->work_group_id
) {
1741 ctx
->work_group_id
=
1742 create_sysval_input(ctx
, SYSTEM_VALUE_WORK_GROUP_ID
, 0x7);
1743 ctx
->work_group_id
->regs
[0]->flags
|= IR3_REG_HIGH
;
1745 ir3_split_dest(b
, dst
, ctx
->work_group_id
, 0, 3);
1747 case nir_intrinsic_load_num_work_groups
:
1748 for (int i
= 0; i
< intr
->num_components
; i
++) {
1749 dst
[i
] = create_driver_param(ctx
, IR3_DP_NUM_WORK_GROUPS_X
+ i
);
1752 case nir_intrinsic_load_local_group_size
:
1753 for (int i
= 0; i
< intr
->num_components
; i
++) {
1754 dst
[i
] = create_driver_param(ctx
, IR3_DP_LOCAL_GROUP_SIZE_X
+ i
);
1757 case nir_intrinsic_discard_if
:
1758 case nir_intrinsic_discard
: {
1759 struct ir3_instruction
*cond
, *kill
;
1761 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1762 /* conditional discard: */
1763 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1764 cond
= ir3_b2n(b
, src
[0]);
1766 /* unconditional discard: */
1767 cond
= create_immed(b
, 1);
1770 /* NOTE: only cmps.*.* can write p0.x: */
1771 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1772 cond
->cat2
.condition
= IR3_COND_NE
;
1774 /* condition always goes in predicate register: */
1775 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1776 cond
->regs
[0]->flags
&= ~IR3_REG_SSA
;
1778 kill
= ir3_KILL(b
, cond
, 0);
1779 kill
->regs
[1]->num
= regid(REG_P0
, 0);
1780 array_insert(ctx
->ir
, ctx
->ir
->predicates
, kill
);
1782 array_insert(b
, b
->keeps
, kill
);
1783 ctx
->so
->no_earlyz
= true;
1788 case nir_intrinsic_cond_end_ir3
: {
1789 struct ir3_instruction
*cond
, *kill
;
1791 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1792 cond
= ir3_b2n(b
, src
[0]);
1794 /* NOTE: only cmps.*.* can write p0.x: */
1795 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1796 cond
->cat2
.condition
= IR3_COND_NE
;
1798 /* condition always goes in predicate register: */
1799 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1801 kill
= ir3_IF(b
, cond
, 0);
1803 kill
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1804 kill
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
1806 array_insert(ctx
->ir
, ctx
->ir
->predicates
, kill
);
1807 array_insert(b
, b
->keeps
, kill
);
1811 case nir_intrinsic_load_shared_ir3
:
1812 emit_intrinsic_load_shared_ir3(ctx
, intr
, dst
);
1814 case nir_intrinsic_store_shared_ir3
:
1815 emit_intrinsic_store_shared_ir3(ctx
, intr
);
1818 ir3_context_error(ctx
, "Unhandled intrinsic type: %s\n",
1819 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1824 ir3_put_dst(ctx
, &intr
->dest
);
1828 emit_load_const(struct ir3_context
*ctx
, nir_load_const_instr
*instr
)
1830 struct ir3_instruction
**dst
= ir3_get_dst_ssa(ctx
, &instr
->def
,
1831 instr
->def
.num_components
);
1833 if (instr
->def
.bit_size
< 32) {
1834 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1835 dst
[i
] = create_immed_typed(ctx
->block
,
1836 instr
->value
[i
].u16
,
1839 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1840 dst
[i
] = create_immed_typed(ctx
->block
,
1841 instr
->value
[i
].u32
,
1848 emit_undef(struct ir3_context
*ctx
, nir_ssa_undef_instr
*undef
)
1850 struct ir3_instruction
**dst
= ir3_get_dst_ssa(ctx
, &undef
->def
,
1851 undef
->def
.num_components
);
1852 type_t type
= (undef
->def
.bit_size
< 32) ? TYPE_U16
: TYPE_U32
;
1854 /* backend doesn't want undefined instructions, so just plug
1857 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1858 dst
[i
] = create_immed_typed(ctx
->block
, fui(0.0), type
);
1862 * texture fetch/sample instructions:
1866 get_tex_dest_type(nir_tex_instr
*tex
)
1870 switch (nir_alu_type_get_base_type(tex
->dest_type
)) {
1871 case nir_type_invalid
:
1872 case nir_type_float
:
1873 type
= nir_dest_bit_size(tex
->dest
) < 32 ? TYPE_F16
: TYPE_F32
;
1876 type
= nir_dest_bit_size(tex
->dest
) < 32 ? TYPE_S16
: TYPE_S32
;
1880 type
= nir_dest_bit_size(tex
->dest
) < 32 ? TYPE_U16
: TYPE_U32
;
1883 unreachable("bad dest_type");
1890 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1892 unsigned coords
= glsl_get_sampler_dim_coordinate_components(tex
->sampler_dim
);
1895 /* note: would use tex->coord_components.. except txs.. also,
1896 * since array index goes after shadow ref, we don't want to
1900 flags
|= IR3_INSTR_3D
;
1902 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
1903 flags
|= IR3_INSTR_S
;
1905 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
)
1906 flags
|= IR3_INSTR_A
;
1912 /* Gets the sampler/texture idx as a hvec2. Which could either be dynamic
1913 * or immediate (in which case it will get lowered later to a non .s2en
1914 * version of the tex instruction which encode tex/samp as immediates:
1916 static struct ir3_instruction
*
1917 get_tex_samp_tex_src(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
1919 int texture_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_texture_offset
);
1920 int sampler_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_sampler_offset
);
1921 struct ir3_instruction
*texture
, *sampler
;
1923 if (texture_idx
>= 0) {
1924 texture
= ir3_get_src(ctx
, &tex
->src
[texture_idx
].src
)[0];
1925 texture
= ir3_COV(ctx
->block
, texture
, TYPE_U32
, TYPE_U16
);
1927 /* TODO what to do for dynamic case? I guess we only need the
1928 * max index for astc srgb workaround so maybe not a problem
1929 * to worry about if we don't enable indirect samplers for
1932 ctx
->max_texture_index
= MAX2(ctx
->max_texture_index
, tex
->texture_index
);
1933 texture
= create_immed_typed(ctx
->block
, tex
->texture_index
, TYPE_U16
);
1936 if (sampler_idx
>= 0) {
1937 sampler
= ir3_get_src(ctx
, &tex
->src
[sampler_idx
].src
)[0];
1938 sampler
= ir3_COV(ctx
->block
, sampler
, TYPE_U32
, TYPE_U16
);
1940 sampler
= create_immed_typed(ctx
->block
, tex
->sampler_index
, TYPE_U16
);
1943 return ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1950 emit_tex(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
1952 struct ir3_block
*b
= ctx
->block
;
1953 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1954 struct ir3_instruction
* const *coord
, * const *off
, * const *ddx
, * const *ddy
;
1955 struct ir3_instruction
*lod
, *compare
, *proj
, *sample_index
;
1956 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1957 unsigned i
, coords
, flags
, ncomp
;
1958 unsigned nsrc0
= 0, nsrc1
= 0;
1962 ncomp
= nir_dest_num_components(tex
->dest
);
1964 coord
= off
= ddx
= ddy
= NULL
;
1965 lod
= proj
= compare
= sample_index
= NULL
;
1967 dst
= ir3_get_dst(ctx
, &tex
->dest
, ncomp
);
1969 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1970 switch (tex
->src
[i
].src_type
) {
1971 case nir_tex_src_coord
:
1972 coord
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1974 case nir_tex_src_bias
:
1975 lod
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1978 case nir_tex_src_lod
:
1979 lod
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1982 case nir_tex_src_comparator
: /* shadow comparator */
1983 compare
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1985 case nir_tex_src_projector
:
1986 proj
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1989 case nir_tex_src_offset
:
1990 off
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1993 case nir_tex_src_ddx
:
1994 ddx
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1996 case nir_tex_src_ddy
:
1997 ddy
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1999 case nir_tex_src_ms_index
:
2000 sample_index
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
2002 case nir_tex_src_texture_offset
:
2003 case nir_tex_src_sampler_offset
:
2004 /* handled in get_tex_samp_src() */
2007 ir3_context_error(ctx
, "Unhandled NIR tex src type: %d\n",
2008 tex
->src
[i
].src_type
);
2014 case nir_texop_tex_prefetch
:
2015 compile_assert(ctx
, !has_bias
);
2016 compile_assert(ctx
, !has_lod
);
2017 compile_assert(ctx
, !compare
);
2018 compile_assert(ctx
, !has_proj
);
2019 compile_assert(ctx
, !has_off
);
2020 compile_assert(ctx
, !ddx
);
2021 compile_assert(ctx
, !ddy
);
2022 compile_assert(ctx
, !sample_index
);
2023 compile_assert(ctx
, nir_tex_instr_src_index(tex
, nir_tex_src_texture_offset
) < 0);
2024 compile_assert(ctx
, nir_tex_instr_src_index(tex
, nir_tex_src_sampler_offset
) < 0);
2026 if (ctx
->so
->num_sampler_prefetch
< IR3_MAX_SAMPLER_PREFETCH
) {
2027 opc
= OPC_META_TEX_PREFETCH
;
2028 ctx
->so
->num_sampler_prefetch
++;
2032 case nir_texop_tex
: opc
= has_lod
? OPC_SAML
: OPC_SAM
; break;
2033 case nir_texop_txb
: opc
= OPC_SAMB
; break;
2034 case nir_texop_txl
: opc
= OPC_SAML
; break;
2035 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
2036 case nir_texop_txf
: opc
= OPC_ISAML
; break;
2037 case nir_texop_lod
: opc
= OPC_GETLOD
; break;
2039 /* NOTE: a4xx might need to emulate gather w/ txf (this is
2040 * what blob does, seems gather is broken?), and a3xx did
2041 * not support it (but probably could also emulate).
2043 switch (tex
->component
) {
2044 case 0: opc
= OPC_GATHER4R
; break;
2045 case 1: opc
= OPC_GATHER4G
; break;
2046 case 2: opc
= OPC_GATHER4B
; break;
2047 case 3: opc
= OPC_GATHER4A
; break;
2050 case nir_texop_txf_ms_fb
:
2051 case nir_texop_txf_ms
: opc
= OPC_ISAMM
; break;
2053 ir3_context_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
2057 tex_info(tex
, &flags
, &coords
);
2060 * lay out the first argument in the proper order:
2061 * - actual coordinates first
2062 * - shadow reference
2065 * - starting at offset 4, dpdx.xy, dpdy.xy
2067 * bias/lod go into the second arg
2070 /* insert tex coords: */
2071 for (i
= 0; i
< coords
; i
++)
2076 /* scale up integer coords for TXF based on the LOD */
2077 if (ctx
->compiler
->unminify_coords
&& (opc
== OPC_ISAML
)) {
2079 for (i
= 0; i
< coords
; i
++)
2080 src0
[i
] = ir3_SHL_B(b
, src0
[i
], 0, lod
, 0);
2084 /* hw doesn't do 1d, so we treat it as 2d with
2085 * height of 1, and patch up the y coord.
2088 src0
[nsrc0
++] = create_immed(b
, 0);
2090 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
2094 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
2095 src0
[nsrc0
++] = compare
;
2097 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
) {
2098 struct ir3_instruction
*idx
= coord
[coords
];
2100 /* the array coord for cube arrays needs 0.5 added to it */
2101 if (ctx
->compiler
->array_index_add_half
&& !is_isam(opc
))
2102 idx
= ir3_ADD_F(b
, idx
, 0, create_immed(b
, fui(0.5)), 0);
2104 src0
[nsrc0
++] = idx
;
2108 src0
[nsrc0
++] = proj
;
2109 flags
|= IR3_INSTR_P
;
2112 /* pad to 4, then ddx/ddy: */
2113 if (tex
->op
== nir_texop_txd
) {
2115 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2116 for (i
= 0; i
< coords
; i
++)
2117 src0
[nsrc0
++] = ddx
[i
];
2119 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2120 for (i
= 0; i
< coords
; i
++)
2121 src0
[nsrc0
++] = ddy
[i
];
2123 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2126 /* NOTE a3xx (and possibly a4xx?) might be different, using isaml
2127 * with scaled x coord according to requested sample:
2129 if (opc
== OPC_ISAMM
) {
2130 if (ctx
->compiler
->txf_ms_with_isaml
) {
2131 /* the samples are laid out in x dimension as
2133 * x_ms = (x << ms) + sample_index;
2135 struct ir3_instruction
*ms
;
2136 ms
= create_immed(b
, (ctx
->samples
>> (2 * tex
->texture_index
)) & 3);
2138 src0
[0] = ir3_SHL_B(b
, src0
[0], 0, ms
, 0);
2139 src0
[0] = ir3_ADD_U(b
, src0
[0], 0, sample_index
, 0);
2143 src0
[nsrc0
++] = sample_index
;
2148 * second argument (if applicable):
2153 if (has_off
| has_lod
| has_bias
) {
2155 unsigned off_coords
= coords
;
2156 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2158 for (i
= 0; i
< off_coords
; i
++)
2159 src1
[nsrc1
++] = off
[i
];
2161 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
2162 flags
|= IR3_INSTR_O
;
2165 if (has_lod
| has_bias
)
2166 src1
[nsrc1
++] = lod
;
2169 type
= get_tex_dest_type(tex
);
2171 if (opc
== OPC_GETLOD
)
2174 struct ir3_instruction
*samp_tex
;
2176 if (tex
->op
== nir_texop_txf_ms_fb
) {
2177 /* only expect a single txf_ms_fb per shader: */
2178 compile_assert(ctx
, !ctx
->so
->fb_read
);
2179 compile_assert(ctx
, ctx
->so
->type
== MESA_SHADER_FRAGMENT
);
2181 ctx
->so
->fb_read
= true;
2182 samp_tex
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
2183 create_immed_typed(ctx
->block
, ctx
->so
->num_samp
, TYPE_U16
),
2184 create_immed_typed(ctx
->block
, ctx
->so
->num_samp
, TYPE_U16
),
2187 ctx
->so
->num_samp
++;
2189 samp_tex
= get_tex_samp_tex_src(ctx
, tex
);
2192 struct ir3_instruction
*col0
= ir3_create_collect(ctx
, src0
, nsrc0
);
2193 struct ir3_instruction
*col1
= ir3_create_collect(ctx
, src1
, nsrc1
);
2195 if (opc
== OPC_META_TEX_PREFETCH
) {
2196 int idx
= nir_tex_instr_src_index(tex
, nir_tex_src_coord
);
2198 compile_assert(ctx
, tex
->src
[idx
].src
.is_ssa
);
2200 sam
= ir3_META_TEX_PREFETCH(b
);
2201 __ssa_dst(sam
)->wrmask
= MASK(ncomp
); /* dst */
2202 sam
->prefetch
.input_offset
=
2203 ir3_nir_coord_offset(tex
->src
[idx
].src
.ssa
);
2204 sam
->prefetch
.tex
= tex
->texture_index
;
2205 sam
->prefetch
.samp
= tex
->sampler_index
;
2207 sam
= ir3_SAM(b
, opc
, type
, MASK(ncomp
), flags
,
2208 samp_tex
, col0
, col1
);
2211 if ((ctx
->astc_srgb
& (1 << tex
->texture_index
)) && !nir_tex_instr_is_query(tex
)) {
2212 assert(opc
!= OPC_META_TEX_PREFETCH
);
2214 /* only need first 3 components: */
2215 sam
->regs
[0]->wrmask
= 0x7;
2216 ir3_split_dest(b
, dst
, sam
, 0, 3);
2218 /* we need to sample the alpha separately with a non-ASTC
2221 sam
= ir3_SAM(b
, opc
, type
, 0b1000, flags
,
2222 samp_tex
, col0
, col1
);
2224 array_insert(ctx
->ir
, ctx
->ir
->astc_srgb
, sam
);
2226 /* fixup .w component: */
2227 ir3_split_dest(b
, &dst
[3], sam
, 3, 1);
2229 /* normal (non-workaround) case: */
2230 ir3_split_dest(b
, dst
, sam
, 0, ncomp
);
2233 /* GETLOD returns results in 4.8 fixed point */
2234 if (opc
== OPC_GETLOD
) {
2235 struct ir3_instruction
*factor
= create_immed(b
, fui(1.0 / 256));
2237 compile_assert(ctx
, tex
->dest_type
== nir_type_float
);
2238 for (i
= 0; i
< 2; i
++) {
2239 dst
[i
] = ir3_MUL_F(b
, ir3_COV(b
, dst
[i
], TYPE_S32
, TYPE_F32
), 0,
2244 ir3_put_dst(ctx
, &tex
->dest
);
2248 emit_tex_info(struct ir3_context
*ctx
, nir_tex_instr
*tex
, unsigned idx
)
2250 struct ir3_block
*b
= ctx
->block
;
2251 struct ir3_instruction
**dst
, *sam
;
2252 type_t dst_type
= get_tex_dest_type(tex
);
2254 dst
= ir3_get_dst(ctx
, &tex
->dest
, 1);
2256 sam
= ir3_SAM(b
, OPC_GETINFO
, dst_type
, 1 << idx
, 0,
2257 get_tex_samp_tex_src(ctx
, tex
), NULL
, NULL
);
2259 /* even though there is only one component, since it ends
2260 * up in .y/.z/.w rather than .x, we need a split_dest()
2263 ir3_split_dest(b
, dst
, sam
, 0, idx
+ 1);
2265 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
2266 * the value in TEX_CONST_0 is zero-based.
2268 if (ctx
->compiler
->levels_add_one
)
2269 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
2271 ir3_put_dst(ctx
, &tex
->dest
);
2275 emit_tex_txs(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
2277 struct ir3_block
*b
= ctx
->block
;
2278 struct ir3_instruction
**dst
, *sam
;
2279 struct ir3_instruction
*lod
;
2280 unsigned flags
, coords
;
2281 type_t dst_type
= get_tex_dest_type(tex
);
2283 tex_info(tex
, &flags
, &coords
);
2285 /* Actually we want the number of dimensions, not coordinates. This
2286 * distinction only matters for cubes.
2288 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2291 dst
= ir3_get_dst(ctx
, &tex
->dest
, 4);
2293 compile_assert(ctx
, tex
->num_srcs
== 1);
2294 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
2296 lod
= ir3_get_src(ctx
, &tex
->src
[0].src
)[0];
2298 sam
= ir3_SAM(b
, OPC_GETSIZE
, dst_type
, 0b1111, flags
,
2299 get_tex_samp_tex_src(ctx
, tex
), lod
, NULL
);
2301 ir3_split_dest(b
, dst
, sam
, 0, 4);
2303 /* Array size actually ends up in .w rather than .z. This doesn't
2304 * matter for miplevel 0, but for higher mips the value in z is
2305 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
2306 * returned, which means that we have to add 1 to it for arrays.
2308 if (tex
->is_array
) {
2309 if (ctx
->compiler
->levels_add_one
) {
2310 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
2312 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
2316 ir3_put_dst(ctx
, &tex
->dest
);
2320 emit_jump(struct ir3_context
*ctx
, nir_jump_instr
*jump
)
2322 switch (jump
->type
) {
2323 case nir_jump_break
:
2324 case nir_jump_continue
:
2325 case nir_jump_return
:
2326 /* I *think* we can simply just ignore this, and use the
2327 * successor block link to figure out where we need to
2328 * jump to for break/continue
2332 ir3_context_error(ctx
, "Unhandled NIR jump type: %d\n", jump
->type
);
2338 emit_instr(struct ir3_context
*ctx
, nir_instr
*instr
)
2340 switch (instr
->type
) {
2341 case nir_instr_type_alu
:
2342 emit_alu(ctx
, nir_instr_as_alu(instr
));
2344 case nir_instr_type_deref
:
2345 /* ignored, handled as part of the intrinsic they are src to */
2347 case nir_instr_type_intrinsic
:
2348 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2350 case nir_instr_type_load_const
:
2351 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2353 case nir_instr_type_ssa_undef
:
2354 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
2356 case nir_instr_type_tex
: {
2357 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
2358 /* couple tex instructions get special-cased:
2362 emit_tex_txs(ctx
, tex
);
2364 case nir_texop_query_levels
:
2365 emit_tex_info(ctx
, tex
, 2);
2367 case nir_texop_texture_samples
:
2368 emit_tex_info(ctx
, tex
, 3);
2376 case nir_instr_type_jump
:
2377 emit_jump(ctx
, nir_instr_as_jump(instr
));
2379 case nir_instr_type_phi
:
2380 /* we have converted phi webs to regs in NIR by now */
2381 ir3_context_error(ctx
, "Unexpected NIR instruction type: %d\n", instr
->type
);
2383 case nir_instr_type_call
:
2384 case nir_instr_type_parallel_copy
:
2385 ir3_context_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
2390 static struct ir3_block
*
2391 get_block(struct ir3_context
*ctx
, const nir_block
*nblock
)
2393 struct ir3_block
*block
;
2394 struct hash_entry
*hentry
;
2396 hentry
= _mesa_hash_table_search(ctx
->block_ht
, nblock
);
2398 return hentry
->data
;
2400 block
= ir3_block_create(ctx
->ir
);
2401 block
->nblock
= nblock
;
2402 _mesa_hash_table_insert(ctx
->block_ht
, nblock
, block
);
2404 block
->predecessors
= _mesa_pointer_set_create(block
);
2405 set_foreach(nblock
->predecessors
, sentry
) {
2406 _mesa_set_add(block
->predecessors
, get_block(ctx
, sentry
->key
));
2413 emit_block(struct ir3_context
*ctx
, nir_block
*nblock
)
2415 struct ir3_block
*block
= get_block(ctx
, nblock
);
2417 for (int i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
2418 if (nblock
->successors
[i
]) {
2419 block
->successors
[i
] =
2420 get_block(ctx
, nblock
->successors
[i
]);
2425 list_addtail(&block
->node
, &ctx
->ir
->block_list
);
2427 /* re-emit addr register in each block if needed: */
2428 for (int i
= 0; i
< ARRAY_SIZE(ctx
->addr_ht
); i
++) {
2429 _mesa_hash_table_destroy(ctx
->addr_ht
[i
], NULL
);
2430 ctx
->addr_ht
[i
] = NULL
;
2433 nir_foreach_instr(instr
, nblock
) {
2434 ctx
->cur_instr
= instr
;
2435 emit_instr(ctx
, instr
);
2436 ctx
->cur_instr
= NULL
;
2442 static void emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
);
2445 emit_if(struct ir3_context
*ctx
, nir_if
*nif
)
2447 struct ir3_instruction
*condition
= ir3_get_src(ctx
, &nif
->condition
)[0];
2449 ctx
->block
->condition
=
2450 ir3_get_predicate(ctx
, ir3_b2n(condition
->block
, condition
));
2452 emit_cf_list(ctx
, &nif
->then_list
);
2453 emit_cf_list(ctx
, &nif
->else_list
);
2457 emit_loop(struct ir3_context
*ctx
, nir_loop
*nloop
)
2459 emit_cf_list(ctx
, &nloop
->body
);
2464 stack_push(struct ir3_context
*ctx
)
2467 ctx
->max_stack
= MAX2(ctx
->max_stack
, ctx
->stack
);
2471 stack_pop(struct ir3_context
*ctx
)
2473 compile_assert(ctx
, ctx
->stack
> 0);
2478 emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
)
2480 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2481 switch (node
->type
) {
2482 case nir_cf_node_block
:
2483 emit_block(ctx
, nir_cf_node_as_block(node
));
2485 case nir_cf_node_if
:
2487 emit_if(ctx
, nir_cf_node_as_if(node
));
2490 case nir_cf_node_loop
:
2492 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2495 case nir_cf_node_function
:
2496 ir3_context_error(ctx
, "TODO\n");
2502 /* emit stream-out code. At this point, the current block is the original
2503 * (nir) end block, and nir ensures that all flow control paths terminate
2504 * into the end block. We re-purpose the original end block to generate
2505 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
2506 * block holding stream-out write instructions, followed by the new end
2510 * p0.x = (vtxcnt < maxvtxcnt)
2511 * // succs: blockStreamOut, blockNewEnd
2514 * ... stream-out instructions ...
2515 * // succs: blockNewEnd
2521 emit_stream_out(struct ir3_context
*ctx
)
2523 struct ir3
*ir
= ctx
->ir
;
2524 struct ir3_stream_output_info
*strmout
=
2525 &ctx
->so
->shader
->stream_output
;
2526 struct ir3_block
*orig_end_block
, *stream_out_block
, *new_end_block
;
2527 struct ir3_instruction
*vtxcnt
, *maxvtxcnt
, *cond
;
2528 struct ir3_instruction
*bases
[IR3_MAX_SO_BUFFERS
];
2530 /* create vtxcnt input in input block at top of shader,
2531 * so that it is seen as live over the entire duration
2534 vtxcnt
= create_sysval_input(ctx
, SYSTEM_VALUE_VERTEX_CNT
, 0x1);
2535 maxvtxcnt
= create_driver_param(ctx
, IR3_DP_VTXCNT_MAX
);
2537 /* at this point, we are at the original 'end' block,
2538 * re-purpose this block to stream-out condition, then
2539 * append stream-out block and new-end block
2541 orig_end_block
= ctx
->block
;
2543 // TODO these blocks need to update predecessors..
2544 // maybe w/ store_global intrinsic, we could do this
2545 // stuff in nir->nir pass
2547 stream_out_block
= ir3_block_create(ir
);
2548 list_addtail(&stream_out_block
->node
, &ir
->block_list
);
2550 new_end_block
= ir3_block_create(ir
);
2551 list_addtail(&new_end_block
->node
, &ir
->block_list
);
2553 orig_end_block
->successors
[0] = stream_out_block
;
2554 orig_end_block
->successors
[1] = new_end_block
;
2555 stream_out_block
->successors
[0] = new_end_block
;
2557 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
2558 cond
= ir3_CMPS_S(ctx
->block
, vtxcnt
, 0, maxvtxcnt
, 0);
2559 cond
->regs
[0]->num
= regid(REG_P0
, 0);
2560 cond
->regs
[0]->flags
&= ~IR3_REG_SSA
;
2561 cond
->cat2
.condition
= IR3_COND_LT
;
2563 /* condition goes on previous block to the conditional,
2564 * since it is used to pick which of the two successor
2567 orig_end_block
->condition
= cond
;
2569 /* switch to stream_out_block to generate the stream-out
2572 ctx
->block
= stream_out_block
;
2574 /* Calculate base addresses based on vtxcnt. Instructions
2575 * generated for bases not used in following loop will be
2576 * stripped out in the backend.
2578 for (unsigned i
= 0; i
< IR3_MAX_SO_BUFFERS
; i
++) {
2579 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
2580 unsigned stride
= strmout
->stride
[i
];
2581 struct ir3_instruction
*base
, *off
;
2583 base
= create_uniform(ctx
->block
, regid(const_state
->offsets
.tfbo
, i
));
2585 /* 24-bit should be enough: */
2586 off
= ir3_MUL_U24(ctx
->block
, vtxcnt
, 0,
2587 create_immed(ctx
->block
, stride
* 4), 0);
2589 bases
[i
] = ir3_ADD_S(ctx
->block
, off
, 0, base
, 0);
2592 /* Generate the per-output store instructions: */
2593 for (unsigned i
= 0; i
< strmout
->num_outputs
; i
++) {
2594 for (unsigned j
= 0; j
< strmout
->output
[i
].num_components
; j
++) {
2595 unsigned c
= j
+ strmout
->output
[i
].start_component
;
2596 struct ir3_instruction
*base
, *out
, *stg
;
2598 base
= bases
[strmout
->output
[i
].output_buffer
];
2599 out
= ctx
->ir
->outputs
[regid(strmout
->output
[i
].register_index
, c
)];
2601 stg
= ir3_STG(ctx
->block
, base
, 0, out
, 0,
2602 create_immed(ctx
->block
, 1), 0);
2603 stg
->cat6
.type
= TYPE_U32
;
2604 stg
->cat6
.dst_offset
= (strmout
->output
[i
].dst_offset
+ j
) * 4;
2606 array_insert(ctx
->block
, ctx
->block
->keeps
, stg
);
2610 /* and finally switch to the new_end_block: */
2611 ctx
->block
= new_end_block
;
2615 emit_function(struct ir3_context
*ctx
, nir_function_impl
*impl
)
2617 nir_metadata_require(impl
, nir_metadata_block_index
);
2619 compile_assert(ctx
, ctx
->stack
== 0);
2621 emit_cf_list(ctx
, &impl
->body
);
2622 emit_block(ctx
, impl
->end_block
);
2624 compile_assert(ctx
, ctx
->stack
== 0);
2626 /* at this point, we should have a single empty block,
2627 * into which we emit the 'end' instruction.
2629 compile_assert(ctx
, list_is_empty(&ctx
->block
->instr_list
));
2631 /* If stream-out (aka transform-feedback) enabled, emit the
2632 * stream-out instructions, followed by a new empty block (into
2633 * which the 'end' instruction lands).
2635 * NOTE: it is done in this order, rather than inserting before
2636 * we emit end_block, because NIR guarantees that all blocks
2637 * flow into end_block, and that end_block has no successors.
2638 * So by re-purposing end_block as the first block of stream-
2639 * out, we guarantee that all exit paths flow into the stream-
2642 if ((ctx
->compiler
->gpu_id
< 500) &&
2643 (ctx
->so
->shader
->stream_output
.num_outputs
> 0) &&
2644 !ctx
->so
->binning_pass
) {
2645 debug_assert(ctx
->so
->type
== MESA_SHADER_VERTEX
);
2646 emit_stream_out(ctx
);
2649 /* Vertex shaders in a tessellation or geometry pipeline treat END as a
2650 * NOP and has an epilogue that writes the VS outputs to local storage, to
2651 * be read by the HS. Then it resets execution mask (chmask) and chains
2652 * to the next shader (chsh).
2654 if ((ctx
->so
->type
== MESA_SHADER_VERTEX
&&
2655 (ctx
->so
->key
.has_gs
|| ctx
->so
->key
.tessellation
)) ||
2656 (ctx
->so
->type
== MESA_SHADER_TESS_EVAL
&& ctx
->so
->key
.has_gs
)) {
2657 struct ir3_instruction
*chmask
=
2658 ir3_CHMASK(ctx
->block
);
2659 chmask
->barrier_class
= IR3_BARRIER_EVERYTHING
;
2660 chmask
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
2662 struct ir3_instruction
*chsh
=
2663 ir3_CHSH(ctx
->block
);
2664 chsh
->barrier_class
= IR3_BARRIER_EVERYTHING
;
2665 chsh
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
2667 ir3_END(ctx
->block
);
2672 setup_input(struct ir3_context
*ctx
, nir_variable
*in
)
2674 struct ir3_shader_variant
*so
= ctx
->so
;
2675 unsigned ncomp
= glsl_get_components(in
->type
);
2676 unsigned n
= in
->data
.driver_location
;
2677 unsigned frac
= in
->data
.location_frac
;
2678 unsigned slot
= in
->data
.location
;
2680 /* Inputs are loaded using ldlw or ldg for these stages. */
2681 if (ctx
->so
->type
== MESA_SHADER_TESS_CTRL
||
2682 ctx
->so
->type
== MESA_SHADER_TESS_EVAL
||
2683 ctx
->so
->type
== MESA_SHADER_GEOMETRY
)
2686 /* skip unread inputs, we could end up with (for example), unsplit
2687 * matrix/etc inputs in the case they are not read, so just silently
2693 so
->inputs
[n
].slot
= slot
;
2694 so
->inputs
[n
].compmask
|= (1 << (ncomp
+ frac
)) - 1;
2695 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
2696 so
->inputs
[n
].interpolate
= in
->data
.interpolation
;
2698 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
2700 /* if any varyings have 'sample' qualifer, that triggers us
2701 * to run in per-sample mode:
2703 so
->per_samp
|= in
->data
.sample
;
2705 for (int i
= 0; i
< ncomp
; i
++) {
2706 struct ir3_instruction
*instr
= NULL
;
2707 unsigned idx
= (n
* 4) + i
+ frac
;
2709 if (slot
== VARYING_SLOT_POS
) {
2710 ir3_context_error(ctx
, "fragcoord should be a sysval!\n");
2711 } else if (slot
== VARYING_SLOT_PNTC
) {
2712 /* see for example st_nir_fixup_varying_slots().. this is
2713 * maybe a bit mesa/st specific. But we need things to line
2714 * up for this in fdN_program:
2715 * unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
2716 * if (emit->sprite_coord_enable & texmask) {
2720 so
->inputs
[n
].slot
= VARYING_SLOT_VAR8
;
2721 so
->inputs
[n
].bary
= true;
2722 instr
= create_frag_input(ctx
, false, idx
);
2724 /* detect the special case for front/back colors where
2725 * we need to do flat vs smooth shading depending on
2728 if (in
->data
.interpolation
== INTERP_MODE_NONE
) {
2730 case VARYING_SLOT_COL0
:
2731 case VARYING_SLOT_COL1
:
2732 case VARYING_SLOT_BFC0
:
2733 case VARYING_SLOT_BFC1
:
2734 so
->inputs
[n
].rasterflat
= true;
2741 if (ctx
->compiler
->flat_bypass
) {
2742 if ((so
->inputs
[n
].interpolate
== INTERP_MODE_FLAT
) ||
2743 (so
->inputs
[n
].rasterflat
&& ctx
->so
->key
.rasterflat
))
2744 so
->inputs
[n
].use_ldlv
= true;
2747 so
->inputs
[n
].bary
= true;
2749 instr
= create_frag_input(ctx
, so
->inputs
[n
].use_ldlv
, idx
);
2752 compile_assert(ctx
, idx
< ctx
->ninputs
);
2754 ctx
->inputs
[idx
] = instr
;
2756 } else if (ctx
->so
->type
== MESA_SHADER_VERTEX
) {
2757 struct ir3_instruction
*input
= NULL
, *in
;
2758 struct ir3_instruction
*components
[4];
2759 unsigned mask
= (1 << (ncomp
+ frac
)) - 1;
2761 foreach_input(in
, ctx
->ir
) {
2762 if (in
->input
.inidx
== n
) {
2769 input
= create_input(ctx
, mask
);
2770 input
->input
.inidx
= n
;
2772 input
->regs
[0]->wrmask
|= mask
;
2775 ir3_split_dest(ctx
->block
, components
, input
, frac
, ncomp
);
2777 for (int i
= 0; i
< ncomp
; i
++) {
2778 unsigned idx
= (n
* 4) + i
+ frac
;
2779 compile_assert(ctx
, idx
< ctx
->ninputs
);
2780 ctx
->inputs
[idx
] = components
[i
];
2783 ir3_context_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2786 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== MESA_SHADER_VERTEX
)) {
2787 so
->total_in
+= ncomp
;
2791 /* Initially we assign non-packed inloc's for varyings, as we don't really
2792 * know up-front which components will be unused. After all the compilation
2793 * stages we scan the shader to see which components are actually used, and
2794 * re-pack the inlocs to eliminate unneeded varyings.
2797 pack_inlocs(struct ir3_context
*ctx
)
2799 struct ir3_shader_variant
*so
= ctx
->so
;
2800 uint8_t used_components
[so
->inputs_count
];
2802 memset(used_components
, 0, sizeof(used_components
));
2805 * First Step: scan shader to find which bary.f/ldlv remain:
2808 foreach_block (block
, &ctx
->ir
->block_list
) {
2809 foreach_instr (instr
, &block
->instr_list
) {
2810 if (is_input(instr
)) {
2811 unsigned inloc
= instr
->regs
[1]->iim_val
;
2812 unsigned i
= inloc
/ 4;
2813 unsigned j
= inloc
% 4;
2815 compile_assert(ctx
, instr
->regs
[1]->flags
& IR3_REG_IMMED
);
2816 compile_assert(ctx
, i
< so
->inputs_count
);
2818 used_components
[i
] |= 1 << j
;
2819 } else if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
2820 for (int n
= 0; n
< 2; n
++) {
2821 unsigned inloc
= instr
->prefetch
.input_offset
+ n
;
2822 unsigned i
= inloc
/ 4;
2823 unsigned j
= inloc
% 4;
2825 compile_assert(ctx
, i
< so
->inputs_count
);
2827 used_components
[i
] |= 1 << j
;
2834 * Second Step: reassign varying inloc/slots:
2837 unsigned actual_in
= 0;
2840 for (unsigned i
= 0; i
< so
->inputs_count
; i
++) {
2841 unsigned compmask
= 0, maxcomp
= 0;
2843 so
->inputs
[i
].inloc
= inloc
;
2844 so
->inputs
[i
].bary
= false;
2846 for (unsigned j
= 0; j
< 4; j
++) {
2847 if (!(used_components
[i
] & (1 << j
)))
2850 compmask
|= (1 << j
);
2854 /* at this point, since used_components[i] mask is only
2855 * considering varyings (ie. not sysvals) we know this
2858 so
->inputs
[i
].bary
= true;
2861 if (so
->inputs
[i
].bary
) {
2863 so
->inputs
[i
].compmask
= (1 << maxcomp
) - 1;
2869 * Third Step: reassign packed inloc's:
2872 foreach_block (block
, &ctx
->ir
->block_list
) {
2873 foreach_instr (instr
, &block
->instr_list
) {
2874 if (is_input(instr
)) {
2875 unsigned inloc
= instr
->regs
[1]->iim_val
;
2876 unsigned i
= inloc
/ 4;
2877 unsigned j
= inloc
% 4;
2879 instr
->regs
[1]->iim_val
= so
->inputs
[i
].inloc
+ j
;
2880 } else if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
2881 unsigned i
= instr
->prefetch
.input_offset
/ 4;
2882 unsigned j
= instr
->prefetch
.input_offset
% 4;
2883 instr
->prefetch
.input_offset
= so
->inputs
[i
].inloc
+ j
;
2890 setup_output(struct ir3_context
*ctx
, nir_variable
*out
)
2892 struct ir3_shader_variant
*so
= ctx
->so
;
2893 unsigned ncomp
= glsl_get_components(out
->type
);
2894 unsigned n
= out
->data
.driver_location
;
2895 unsigned frac
= out
->data
.location_frac
;
2896 unsigned slot
= out
->data
.location
;
2899 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
2901 case FRAG_RESULT_DEPTH
:
2902 comp
= 2; /* tgsi will write to .z component */
2903 so
->writes_pos
= true;
2905 case FRAG_RESULT_COLOR
:
2908 case FRAG_RESULT_SAMPLE_MASK
:
2909 so
->writes_smask
= true;
2912 if (slot
>= FRAG_RESULT_DATA0
)
2914 ir3_context_error(ctx
, "unknown FS output name: %s\n",
2915 gl_frag_result_name(slot
));
2917 } else if (ctx
->so
->type
== MESA_SHADER_VERTEX
||
2918 ctx
->so
->type
== MESA_SHADER_TESS_EVAL
||
2919 ctx
->so
->type
== MESA_SHADER_GEOMETRY
) {
2921 case VARYING_SLOT_POS
:
2922 so
->writes_pos
= true;
2924 case VARYING_SLOT_PSIZ
:
2925 so
->writes_psize
= true;
2927 case VARYING_SLOT_PRIMITIVE_ID
:
2928 case VARYING_SLOT_LAYER
:
2929 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3
:
2930 debug_assert(ctx
->so
->type
== MESA_SHADER_GEOMETRY
);
2932 case VARYING_SLOT_COL0
:
2933 case VARYING_SLOT_COL1
:
2934 case VARYING_SLOT_BFC0
:
2935 case VARYING_SLOT_BFC1
:
2936 case VARYING_SLOT_FOGC
:
2937 case VARYING_SLOT_CLIP_DIST0
:
2938 case VARYING_SLOT_CLIP_DIST1
:
2939 case VARYING_SLOT_CLIP_VERTEX
:
2942 if (slot
>= VARYING_SLOT_VAR0
)
2944 if ((VARYING_SLOT_TEX0
<= slot
) && (slot
<= VARYING_SLOT_TEX7
))
2946 ir3_context_error(ctx
, "unknown %s shader output name: %s\n",
2947 _mesa_shader_stage_to_string(ctx
->so
->type
),
2948 gl_varying_slot_name(slot
));
2950 } else if (ctx
->so
->type
== MESA_SHADER_TESS_CTRL
) {
2951 /* output lowered to buffer writes. */
2954 ir3_context_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2957 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
2959 so
->outputs
[n
].slot
= slot
;
2960 so
->outputs
[n
].regid
= regid(n
, comp
);
2961 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
2963 for (int i
= 0; i
< ncomp
; i
++) {
2964 unsigned idx
= (n
* 4) + i
+ frac
;
2965 compile_assert(ctx
, idx
< ctx
->noutputs
);
2966 ctx
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2969 /* if varying packing doesn't happen, we could end up in a situation
2970 * with "holes" in the output, and since the per-generation code that
2971 * sets up varying linkage registers doesn't expect to have more than
2972 * one varying per vec4 slot, pad the holes.
2974 * Note that this should probably generate a performance warning of
2977 for (int i
= 0; i
< frac
; i
++) {
2978 unsigned idx
= (n
* 4) + i
;
2979 if (!ctx
->outputs
[idx
]) {
2980 ctx
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2986 max_drvloc(struct exec_list
*vars
)
2989 nir_foreach_variable(var
, vars
) {
2990 drvloc
= MAX2(drvloc
, (int)var
->data
.driver_location
);
2996 emit_instructions(struct ir3_context
*ctx
)
2998 nir_function_impl
*fxn
= nir_shader_get_entrypoint(ctx
->s
);
3000 ctx
->ninputs
= (max_drvloc(&ctx
->s
->inputs
) + 1) * 4;
3001 ctx
->noutputs
= (max_drvloc(&ctx
->s
->outputs
) + 1) * 4;
3003 ctx
->inputs
= rzalloc_array(ctx
, struct ir3_instruction
*, ctx
->ninputs
);
3004 ctx
->outputs
= rzalloc_array(ctx
, struct ir3_instruction
*, ctx
->noutputs
);
3006 ctx
->ir
= ir3_create(ctx
->compiler
, ctx
->so
->type
);
3008 /* Create inputs in first block: */
3009 ctx
->block
= get_block(ctx
, nir_start_block(fxn
));
3010 ctx
->in_block
= ctx
->block
;
3011 list_addtail(&ctx
->block
->node
, &ctx
->ir
->block_list
);
3013 /* for fragment shader, the vcoord input register is used as the
3014 * base for bary.f varying fetch instrs:
3016 * TODO defer creating ctx->ij_pixel and corresponding sysvals
3017 * until emit_intrinsic when we know they are actually needed.
3018 * For now, we defer creating ctx->ij_centroid, etc, since we
3019 * only need ij_pixel for "old style" varying inputs (ie.
3022 struct ir3_instruction
*vcoord
= NULL
;
3023 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
3024 struct ir3_instruction
*xy
[2];
3026 vcoord
= create_input(ctx
, 0x3);
3027 ir3_split_dest(ctx
->block
, xy
, vcoord
, 0, 2);
3029 ctx
->ij_pixel
= ir3_create_collect(ctx
, xy
, 2);
3033 nir_foreach_variable(var
, &ctx
->s
->inputs
) {
3034 setup_input(ctx
, var
);
3037 /* Defer add_sysval_input() stuff until after setup_inputs(),
3038 * because sysvals need to be appended after varyings:
3041 add_sysval_input_compmask(ctx
, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
,
3046 /* Tesselation shaders always need primitive ID for indexing the
3047 * BO. Geometry shaders don't always need it but when they do it has be
3048 * delivered and unclobbered in the VS. To make things easy, we always
3049 * make room for it in VS/DS.
3051 bool has_tess
= ctx
->so
->key
.tessellation
!= IR3_TESS_NONE
;
3052 bool has_gs
= ctx
->so
->key
.has_gs
;
3053 switch (ctx
->so
->type
) {
3054 case MESA_SHADER_VERTEX
:
3056 ctx
->tcs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_TCS_HEADER_IR3
, 0x1);
3057 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3058 } else if (has_gs
) {
3059 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3060 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3063 case MESA_SHADER_TESS_CTRL
:
3064 ctx
->tcs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_TCS_HEADER_IR3
, 0x1);
3065 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3067 case MESA_SHADER_TESS_EVAL
:
3069 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3070 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3072 case MESA_SHADER_GEOMETRY
:
3073 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3074 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3080 /* Setup outputs: */
3081 nir_foreach_variable(var
, &ctx
->s
->outputs
) {
3082 setup_output(ctx
, var
);
3085 /* Find # of samplers: */
3086 nir_foreach_variable(var
, &ctx
->s
->uniforms
) {
3087 ctx
->so
->num_samp
+= glsl_type_get_sampler_count(var
->type
);
3088 /* just assume that we'll be reading from images.. if it
3089 * is write-only we don't have to count it, but not sure
3090 * if there is a good way to know?
3092 ctx
->so
->num_samp
+= glsl_type_get_image_count(var
->type
);
3095 /* NOTE: need to do something more clever when we support >1 fxn */
3096 nir_foreach_register(reg
, &fxn
->registers
) {
3097 ir3_declare_array(ctx
, reg
);
3099 /* And emit the body: */
3101 emit_function(ctx
, fxn
);
3104 /* Fixup tex sampler state for astc/srgb workaround instructions. We
3105 * need to assign the tex state indexes for these after we know the
3109 fixup_astc_srgb(struct ir3_context
*ctx
)
3111 struct ir3_shader_variant
*so
= ctx
->so
;
3112 /* indexed by original tex idx, value is newly assigned alpha sampler
3113 * state tex idx. Zero is invalid since there is at least one sampler
3116 unsigned alt_tex_state
[16] = {0};
3117 unsigned tex_idx
= ctx
->max_texture_index
+ 1;
3120 so
->astc_srgb
.base
= tex_idx
;
3122 for (unsigned i
= 0; i
< ctx
->ir
->astc_srgb_count
; i
++) {
3123 struct ir3_instruction
*sam
= ctx
->ir
->astc_srgb
[i
];
3125 compile_assert(ctx
, sam
->cat5
.tex
< ARRAY_SIZE(alt_tex_state
));
3127 if (alt_tex_state
[sam
->cat5
.tex
] == 0) {
3128 /* assign new alternate/alpha tex state slot: */
3129 alt_tex_state
[sam
->cat5
.tex
] = tex_idx
++;
3130 so
->astc_srgb
.orig_idx
[idx
++] = sam
->cat5
.tex
;
3131 so
->astc_srgb
.count
++;
3134 sam
->cat5
.tex
= alt_tex_state
[sam
->cat5
.tex
];
3139 fixup_binning_pass(struct ir3_context
*ctx
)
3141 struct ir3_shader_variant
*so
= ctx
->so
;
3142 struct ir3
*ir
= ctx
->ir
;
3145 /* first pass, remove unused outputs from the IR level outputs: */
3146 for (i
= 0, j
= 0; i
< ir
->outputs_count
; i
++) {
3147 struct ir3_instruction
*out
= ir
->outputs
[i
];
3148 assert(out
->opc
== OPC_META_COLLECT
);
3149 unsigned outidx
= out
->collect
.outidx
;
3150 unsigned slot
= so
->outputs
[outidx
].slot
;
3152 /* throw away everything but first position/psize */
3153 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
3154 ir
->outputs
[j
] = ir
->outputs
[i
];
3158 ir
->outputs_count
= j
;
3160 /* second pass, cleanup the unused slots in ir3_shader_variant::outputs
3163 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
3164 unsigned slot
= so
->outputs
[i
].slot
;
3166 /* throw away everything but first position/psize */
3167 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
3168 so
->outputs
[j
] = so
->outputs
[i
];
3170 /* fixup outidx to point to new output table entry: */
3171 struct ir3_instruction
*out
;
3172 foreach_output(out
, ir
) {
3173 if (out
->collect
.outidx
== i
) {
3174 out
->collect
.outidx
= j
;
3182 so
->outputs_count
= j
;
3186 collect_tex_prefetches(struct ir3_context
*ctx
, struct ir3
*ir
)
3190 /* Collect sampling instructions eligible for pre-dispatch. */
3191 foreach_block (block
, &ir
->block_list
) {
3192 foreach_instr_safe (instr
, &block
->instr_list
) {
3193 if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
3194 assert(idx
< ARRAY_SIZE(ctx
->so
->sampler_prefetch
));
3195 struct ir3_sampler_prefetch
*fetch
=
3196 &ctx
->so
->sampler_prefetch
[idx
];
3199 fetch
->cmd
= IR3_SAMPLER_PREFETCH_CMD
;
3200 fetch
->wrmask
= instr
->regs
[0]->wrmask
;
3201 fetch
->tex_id
= instr
->prefetch
.tex
;
3202 fetch
->samp_id
= instr
->prefetch
.samp
;
3203 fetch
->dst
= instr
->regs
[0]->num
;
3204 fetch
->src
= instr
->prefetch
.input_offset
;
3207 MAX2(ctx
->so
->total_in
, instr
->prefetch
.input_offset
+ 2);
3209 /* Disable half precision until supported. */
3210 fetch
->half_precision
= !!(instr
->regs
[0]->flags
& IR3_REG_HALF
);
3212 /* Remove the prefetch placeholder instruction: */
3213 list_delinit(&instr
->node
);
3220 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
3221 struct ir3_shader_variant
*so
)
3223 struct ir3_context
*ctx
;
3225 int ret
= 0, max_bary
;
3229 ctx
= ir3_context_init(compiler
, so
);
3231 DBG("INIT failed!");
3236 emit_instructions(ctx
);
3239 DBG("EMIT failed!");
3244 ir
= so
->ir
= ctx
->ir
;
3246 assert((ctx
->noutputs
% 4) == 0);
3248 /* Setup IR level outputs, which are "collects" that gather
3249 * the scalar components of outputs.
3251 for (unsigned i
= 0; i
< ctx
->noutputs
; i
+= 4) {
3253 /* figure out the # of components written:
3255 * TODO do we need to handle holes, ie. if .x and .z
3256 * components written, but .y component not written?
3258 for (unsigned j
= 0; j
< 4; j
++) {
3259 if (!ctx
->outputs
[i
+ j
])
3264 /* Note that in some stages, like TCS, store_output is
3265 * lowered to memory writes, so no components of the
3266 * are "written" from the PoV of traditional store-
3267 * output instructions:
3272 struct ir3_instruction
*out
=
3273 ir3_create_collect(ctx
, &ctx
->outputs
[i
], ncomp
);
3276 assert(outidx
< so
->outputs_count
);
3278 /* stash index into so->outputs[] so we can map the
3279 * output back to slot/etc later:
3281 out
->collect
.outidx
= outidx
;
3283 array_insert(ir
, ir
->outputs
, out
);
3286 /* Set up the gs header as an output for the vertex shader so it won't
3287 * clobber it for the tess ctrl shader.
3289 * TODO this could probably be done more cleanly in a nir pass.
3291 if (ctx
->so
->type
== MESA_SHADER_VERTEX
||
3292 (ctx
->so
->key
.has_gs
&& ctx
->so
->type
== MESA_SHADER_TESS_EVAL
)) {
3293 if (ctx
->primitive_id
) {
3294 unsigned n
= so
->outputs_count
++;
3295 so
->outputs
[n
].slot
= VARYING_SLOT_PRIMITIVE_ID
;
3297 struct ir3_instruction
*out
=
3298 ir3_create_collect(ctx
, &ctx
->primitive_id
, 1);
3299 out
->collect
.outidx
= n
;
3300 array_insert(ir
, ir
->outputs
, out
);
3303 if (ctx
->gs_header
) {
3304 unsigned n
= so
->outputs_count
++;
3305 so
->outputs
[n
].slot
= VARYING_SLOT_GS_HEADER_IR3
;
3306 struct ir3_instruction
*out
=
3307 ir3_create_collect(ctx
, &ctx
->gs_header
, 1);
3308 out
->collect
.outidx
= n
;
3309 array_insert(ir
, ir
->outputs
, out
);
3312 if (ctx
->tcs_header
) {
3313 unsigned n
= so
->outputs_count
++;
3314 so
->outputs
[n
].slot
= VARYING_SLOT_TCS_HEADER_IR3
;
3315 struct ir3_instruction
*out
=
3316 ir3_create_collect(ctx
, &ctx
->tcs_header
, 1);
3317 out
->collect
.outidx
= n
;
3318 array_insert(ir
, ir
->outputs
, out
);
3322 /* at this point, for binning pass, throw away unneeded outputs: */
3323 if (so
->binning_pass
&& (ctx
->compiler
->gpu_id
< 600))
3324 fixup_binning_pass(ctx
);
3326 ir3_debug_print(ir
, "BEFORE CF");
3330 ir3_debug_print(ir
, "BEFORE CP");
3334 /* at this point, for binning pass, throw away unneeded outputs:
3335 * Note that for a6xx and later, we do this after ir3_cp to ensure
3336 * that the uniform/constant layout for BS and VS matches, so that
3337 * we can re-use same VS_CONST state group.
3339 if (so
->binning_pass
&& (ctx
->compiler
->gpu_id
>= 600))
3340 fixup_binning_pass(ctx
);
3342 /* for a6xx+, binning and draw pass VS use same VBO state, so we
3343 * need to make sure not to remove any inputs that are used by
3344 * the nonbinning VS.
3346 if (ctx
->compiler
->gpu_id
>= 600 && so
->binning_pass
&&
3347 so
->type
== MESA_SHADER_VERTEX
) {
3348 for (int i
= 0; i
< ctx
->ninputs
; i
++) {
3349 struct ir3_instruction
*in
= ctx
->inputs
[i
];
3357 debug_assert(n
< so
->nonbinning
->inputs_count
);
3359 if (so
->nonbinning
->inputs
[n
].sysval
)
3362 /* be sure to keep inputs, even if only used in VS */
3363 if (so
->nonbinning
->inputs
[n
].compmask
& (1 << c
))
3364 array_insert(in
->block
, in
->block
->keeps
, in
);
3368 ir3_debug_print(ir
, "BEFORE GROUPING");
3370 ir3_sched_add_deps(ir
);
3372 /* Group left/right neighbors, inserting mov's where needed to
3377 ir3_debug_print(ir
, "AFTER GROUPING");
3381 ir3_debug_print(ir
, "AFTER DEPTH");
3383 /* do Sethi–Ullman numbering before scheduling: */
3386 ret
= ir3_sched(ir
);
3388 DBG("SCHED failed!");
3392 ir3_debug_print(ir
, "AFTER SCHED");
3394 /* Pre-assign VS inputs on a6xx+ binning pass shader, to align
3395 * with draw pass VS, so binning and draw pass can both use the
3398 * Note that VS inputs are expected to be full precision.
3400 bool pre_assign_inputs
= (ir
->compiler
->gpu_id
>= 600) &&
3401 (ir
->type
== MESA_SHADER_VERTEX
) &&
3404 if (pre_assign_inputs
) {
3405 for (unsigned i
= 0; i
< ctx
->ninputs
; i
++) {
3406 struct ir3_instruction
*instr
= ctx
->inputs
[i
];
3413 unsigned regid
= so
->nonbinning
->inputs
[n
].regid
+ c
;
3415 instr
->regs
[0]->num
= regid
;
3418 ret
= ir3_ra(so
, ctx
->inputs
, ctx
->ninputs
);
3419 } else if (ctx
->tcs_header
) {
3420 /* We need to have these values in the same registers between VS and TCS
3421 * since the VS chains to TCS and doesn't get the sysvals redelivered.
3424 ctx
->tcs_header
->regs
[0]->num
= regid(0, 0);
3425 ctx
->primitive_id
->regs
[0]->num
= regid(0, 1);
3426 struct ir3_instruction
*precolor
[] = { ctx
->tcs_header
, ctx
->primitive_id
};
3427 ret
= ir3_ra(so
, precolor
, ARRAY_SIZE(precolor
));
3428 } else if (ctx
->gs_header
) {
3429 /* We need to have these values in the same registers between producer
3430 * (VS or DS) and GS since the producer chains to GS and doesn't get
3431 * the sysvals redelivered.
3434 ctx
->gs_header
->regs
[0]->num
= regid(0, 0);
3435 ctx
->primitive_id
->regs
[0]->num
= regid(0, 1);
3436 struct ir3_instruction
*precolor
[] = { ctx
->gs_header
, ctx
->primitive_id
};
3437 ret
= ir3_ra(so
, precolor
, ARRAY_SIZE(precolor
));
3438 } else if (so
->num_sampler_prefetch
) {
3439 assert(so
->type
== MESA_SHADER_FRAGMENT
);
3440 struct ir3_instruction
*instr
, *precolor
[2];
3443 foreach_input(instr
, ir
) {
3444 if (instr
->input
.sysval
!= SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
)
3447 assert(idx
< ARRAY_SIZE(precolor
));
3449 precolor
[idx
] = instr
;
3450 instr
->regs
[0]->num
= idx
;
3454 ret
= ir3_ra(so
, precolor
, idx
);
3456 ret
= ir3_ra(so
, NULL
, 0);
3465 ir3_debug_print(ir
, "AFTER POSTSCHED");
3467 if (compiler
->gpu_id
>= 600) {
3468 if (ir3_a6xx_fixup_atomic_dests(ir
, so
)) {
3469 ir3_debug_print(ir
, "AFTER ATOMIC FIXUP");
3473 if (so
->type
== MESA_SHADER_FRAGMENT
)
3477 * Fixup inputs/outputs to point to the actual registers assigned:
3479 * 1) initialize to r63.x (invalid/unused)
3480 * 2) iterate IR level inputs/outputs and update the variants
3481 * inputs/outputs table based on the assigned registers for
3482 * the remaining inputs/outputs.
3485 for (unsigned i
= 0; i
< so
->inputs_count
; i
++)
3486 so
->inputs
[i
].regid
= INVALID_REG
;
3487 for (unsigned i
= 0; i
< so
->outputs_count
; i
++)
3488 so
->outputs
[i
].regid
= INVALID_REG
;
3490 struct ir3_instruction
*out
;
3491 foreach_output(out
, ir
) {
3492 assert(out
->opc
== OPC_META_COLLECT
);
3493 unsigned outidx
= out
->collect
.outidx
;
3495 so
->outputs
[outidx
].regid
= out
->regs
[0]->num
;
3496 so
->outputs
[outidx
].half
= !!(out
->regs
[0]->flags
& IR3_REG_HALF
);
3499 struct ir3_instruction
*in
;
3500 foreach_input(in
, ir
) {
3501 assert(in
->opc
== OPC_META_INPUT
);
3502 unsigned inidx
= in
->input
.inidx
;
3504 if (pre_assign_inputs
&& !so
->inputs
[inidx
].sysval
) {
3505 if (VALIDREG(so
->nonbinning
->inputs
[inidx
].regid
)) {
3506 compile_assert(ctx
, in
->regs
[0]->num
==
3507 so
->nonbinning
->inputs
[inidx
].regid
);
3508 compile_assert(ctx
, !!(in
->regs
[0]->flags
& IR3_REG_HALF
) ==
3509 so
->nonbinning
->inputs
[inidx
].half
);
3511 so
->inputs
[inidx
].regid
= so
->nonbinning
->inputs
[inidx
].regid
;
3512 so
->inputs
[inidx
].half
= so
->nonbinning
->inputs
[inidx
].half
;
3514 so
->inputs
[inidx
].regid
= in
->regs
[0]->num
;
3515 so
->inputs
[inidx
].half
= !!(in
->regs
[0]->flags
& IR3_REG_HALF
);
3520 fixup_astc_srgb(ctx
);
3522 /* We need to do legalize after (for frag shader's) the "bary.f"
3523 * offsets (inloc) have been assigned.
3525 ir3_legalize(ir
, so
, &max_bary
);
3527 ir3_debug_print(ir
, "AFTER LEGALIZE");
3529 /* Set (ss)(sy) on first TCS and GEOMETRY instructions, since we don't
3530 * know what we might have to wait on when coming in from VS chsh.
3532 if (so
->type
== MESA_SHADER_TESS_CTRL
||
3533 so
->type
== MESA_SHADER_GEOMETRY
) {
3534 foreach_block (block
, &ir
->block_list
) {
3535 foreach_instr (instr
, &block
->instr_list
) {
3536 instr
->flags
|= IR3_INSTR_SS
| IR3_INSTR_SY
;
3542 so
->branchstack
= ctx
->max_stack
;
3544 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
3545 if (so
->type
== MESA_SHADER_FRAGMENT
)
3546 so
->total_in
= max_bary
+ 1;
3548 so
->max_sun
= ir
->max_sun
;
3550 /* Collect sampling instructions eligible for pre-dispatch. */
3551 collect_tex_prefetches(ctx
, ir
);
3556 ir3_destroy(so
->ir
);
3559 ir3_context_free(ctx
);