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
;
471 case nir_op_fddy_coarse
:
472 dst
[0] = ir3_DSY(b
, src
[0], 0);
473 dst
[0]->cat5
.type
= TYPE_F32
;
478 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
479 dst
[0]->cat2
.condition
= IR3_COND_LT
;
483 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
484 dst
[0]->cat2
.condition
= IR3_COND_GE
;
488 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
489 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
493 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
494 dst
[0]->cat2
.condition
= IR3_COND_NE
;
497 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
500 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
503 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
505 case nir_op_fround_even
:
506 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
509 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
513 dst
[0] = ir3_SIN(b
, src
[0], 0);
516 dst
[0] = ir3_COS(b
, src
[0], 0);
519 dst
[0] = ir3_RSQ(b
, src
[0], 0);
522 dst
[0] = ir3_RCP(b
, src
[0], 0);
525 dst
[0] = ir3_LOG2(b
, src
[0], 0);
528 dst
[0] = ir3_EXP2(b
, src
[0], 0);
531 dst
[0] = ir3_SQRT(b
, src
[0], 0);
535 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
538 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
541 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
544 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
547 dst
[0] = ir3_MAX_U(b
, src
[0], 0, src
[1], 0);
550 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
553 dst
[0] = ir3_MIN_U(b
, src
[0], 0, src
[1], 0);
555 case nir_op_umul_low
:
556 dst
[0] = ir3_MULL_U(b
, src
[0], 0, src
[1], 0);
558 case nir_op_imadsh_mix16
:
559 dst
[0] = ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0, src
[2], 0);
561 case nir_op_imad24_ir3
:
562 dst
[0] = ir3_MAD_S24(b
, src
[0], 0, src
[1], 0, src
[2], 0);
565 dst
[0] = ir3_MUL_S24(b
, src
[0], 0, src
[1], 0);
568 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
571 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
574 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
577 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
580 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
583 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
586 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
589 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
593 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
594 dst
[0]->cat2
.condition
= IR3_COND_LT
;
598 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
599 dst
[0]->cat2
.condition
= IR3_COND_GE
;
603 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
604 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
608 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
609 dst
[0]->cat2
.condition
= IR3_COND_NE
;
613 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
614 dst
[0]->cat2
.condition
= IR3_COND_LT
;
618 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
619 dst
[0]->cat2
.condition
= IR3_COND_GE
;
623 case nir_op_b32csel
: {
624 struct ir3_instruction
*cond
= ir3_b2n(b
, src
[0]);
626 if ((src
[0]->regs
[0]->flags
& IR3_REG_HALF
))
627 cond
->regs
[0]->flags
|= IR3_REG_HALF
;
629 compile_assert(ctx
, bs
[1] == bs
[2]);
630 /* Make sure the boolean condition has the same bit size as the other
631 * two arguments, adding a conversion if necessary.
634 cond
= ir3_COV(b
, cond
, TYPE_U32
, TYPE_U16
);
635 else if (bs
[1] > bs
[0])
636 cond
= ir3_COV(b
, cond
, TYPE_U16
, TYPE_U32
);
639 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, cond
, 0, src
[2], 0);
641 dst
[0] = ir3_SEL_B16(b
, src
[1], 0, cond
, 0, src
[2], 0);
644 case nir_op_bit_count
: {
645 // TODO, we need to do this 16b at a time on a5xx+a6xx.. need to
646 // double check on earlier gen's. Once half-precision support is
647 // in place, this should probably move to a NIR lowering pass:
648 struct ir3_instruction
*hi
, *lo
;
650 hi
= ir3_COV(b
, ir3_SHR_B(b
, src
[0], 0, create_immed(b
, 16), 0),
652 lo
= ir3_COV(b
, src
[0], TYPE_U32
, TYPE_U16
);
654 hi
= ir3_CBITS_B(b
, hi
, 0);
655 lo
= ir3_CBITS_B(b
, lo
, 0);
657 // TODO maybe the builders should default to making dst half-precision
658 // if the src's were half precision, to make this less awkward.. otoh
659 // we should probably just do this lowering in NIR.
660 hi
->regs
[0]->flags
|= IR3_REG_HALF
;
661 lo
->regs
[0]->flags
|= IR3_REG_HALF
;
663 dst
[0] = ir3_ADD_S(b
, hi
, 0, lo
, 0);
664 dst
[0]->regs
[0]->flags
|= IR3_REG_HALF
;
665 dst
[0] = ir3_COV(b
, dst
[0], TYPE_U16
, TYPE_U32
);
668 case nir_op_ifind_msb
: {
669 struct ir3_instruction
*cmp
;
670 dst
[0] = ir3_CLZ_S(b
, src
[0], 0);
671 cmp
= ir3_CMPS_S(b
, dst
[0], 0, create_immed(b
, 0), 0);
672 cmp
->cat2
.condition
= IR3_COND_GE
;
673 dst
[0] = ir3_SEL_B32(b
,
674 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
678 case nir_op_ufind_msb
:
679 dst
[0] = ir3_CLZ_B(b
, src
[0], 0);
680 dst
[0] = ir3_SEL_B32(b
,
681 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
682 src
[0], 0, dst
[0], 0);
684 case nir_op_find_lsb
:
685 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
686 dst
[0] = ir3_CLZ_B(b
, dst
[0], 0);
688 case nir_op_bitfield_reverse
:
689 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
693 ir3_context_error(ctx
, "Unhandled ALU op: %s\n",
694 nir_op_infos
[alu
->op
].name
);
698 if (nir_alu_type_get_base_type(info
->output_type
) == nir_type_bool
) {
701 if (nir_dest_bit_size(alu
->dest
.dest
) < 32)
702 dst
[0]->regs
[0]->flags
|= IR3_REG_HALF
;
704 dst
[0] = ir3_n2b(b
, dst
[0]);
707 if (nir_dest_bit_size(alu
->dest
.dest
) < 32) {
708 for (unsigned i
= 0; i
< dst_sz
; i
++) {
709 dst
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
713 ir3_put_dst(ctx
, &alu
->dest
.dest
);
716 /* handles direct/indirect UBO reads: */
718 emit_intrinsic_load_ubo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
719 struct ir3_instruction
**dst
)
721 struct ir3_block
*b
= ctx
->block
;
722 struct ir3_instruction
*base_lo
, *base_hi
, *addr
, *src0
, *src1
;
723 /* UBO addresses are the first driver params, but subtract 2 here to
724 * account for nir_lower_uniforms_to_ubo rebasing the UBOs such that UBO 0
725 * is the uniforms: */
726 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
727 unsigned ubo
= regid(const_state
->offsets
.ubo
, 0) - 2;
728 const unsigned ptrsz
= ir3_pointer_size(ctx
->compiler
);
732 /* First src is ubo index, which could either be an immed or not: */
733 src0
= ir3_get_src(ctx
, &intr
->src
[0])[0];
734 if (is_same_type_mov(src0
) &&
735 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
736 base_lo
= create_uniform(b
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
));
737 base_hi
= create_uniform(b
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
) + 1);
739 base_lo
= create_uniform_indirect(b
, ubo
, ir3_get_addr(ctx
, src0
, ptrsz
));
740 base_hi
= create_uniform_indirect(b
, ubo
+ 1, ir3_get_addr(ctx
, src0
, ptrsz
));
742 /* NOTE: since relative addressing is used, make sure constlen is
743 * at least big enough to cover all the UBO addresses, since the
744 * assembler won't know what the max address reg is.
746 ctx
->so
->constlen
= MAX2(ctx
->so
->constlen
,
747 const_state
->offsets
.ubo
+ (ctx
->s
->info
.num_ubos
* ptrsz
));
750 /* note: on 32bit gpu's base_hi is ignored and DCE'd */
753 if (nir_src_is_const(intr
->src
[1])) {
754 off
+= nir_src_as_uint(intr
->src
[1]);
756 /* For load_ubo_indirect, second src is indirect offset: */
757 src1
= ir3_get_src(ctx
, &intr
->src
[1])[0];
759 /* and add offset to addr: */
760 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
763 /* if offset is to large to encode in the ldg, split it out: */
764 if ((off
+ (intr
->num_components
* 4)) > 1024) {
765 /* split out the minimal amount to improve the odds that
766 * cp can fit the immediate in the add.s instruction:
768 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
769 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
774 struct ir3_instruction
*carry
;
776 /* handle 32b rollover, ie:
777 * if (addr < base_lo)
780 carry
= ir3_CMPS_U(b
, addr
, 0, base_lo
, 0);
781 carry
->cat2
.condition
= IR3_COND_LT
;
782 base_hi
= ir3_ADD_S(b
, base_hi
, 0, carry
, 0);
784 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){ addr
, base_hi
}, 2);
787 for (int i
= 0; i
< intr
->num_components
; i
++) {
788 struct ir3_instruction
*load
=
789 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0, /* num components */
790 create_immed(b
, off
+ i
* 4), 0);
791 load
->cat6
.type
= TYPE_U32
;
796 /* src[] = { block_index } */
798 emit_intrinsic_ssbo_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
799 struct ir3_instruction
**dst
)
801 /* SSBO size stored as a const starting at ssbo_sizes: */
802 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
803 unsigned blk_idx
= nir_src_as_uint(intr
->src
[0]);
804 unsigned idx
= regid(const_state
->offsets
.ssbo_sizes
, 0) +
805 const_state
->ssbo_size
.off
[blk_idx
];
807 debug_assert(const_state
->ssbo_size
.mask
& (1 << blk_idx
));
809 dst
[0] = create_uniform(ctx
->block
, idx
);
812 /* src[] = { offset }. const_index[] = { base } */
814 emit_intrinsic_load_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
815 struct ir3_instruction
**dst
)
817 struct ir3_block
*b
= ctx
->block
;
818 struct ir3_instruction
*ldl
, *offset
;
821 offset
= ir3_get_src(ctx
, &intr
->src
[0])[0];
822 base
= nir_intrinsic_base(intr
);
824 ldl
= ir3_LDL(b
, offset
, 0,
825 create_immed(b
, intr
->num_components
), 0,
826 create_immed(b
, base
), 0);
828 ldl
->cat6
.type
= utype_dst(intr
->dest
);
829 ldl
->regs
[0]->wrmask
= MASK(intr
->num_components
);
831 ldl
->barrier_class
= IR3_BARRIER_SHARED_R
;
832 ldl
->barrier_conflict
= IR3_BARRIER_SHARED_W
;
834 ir3_split_dest(b
, dst
, ldl
, 0, intr
->num_components
);
837 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
839 emit_intrinsic_store_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
841 struct ir3_block
*b
= ctx
->block
;
842 struct ir3_instruction
*stl
, *offset
;
843 struct ir3_instruction
* const *value
;
844 unsigned base
, wrmask
;
846 value
= ir3_get_src(ctx
, &intr
->src
[0]);
847 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
849 base
= nir_intrinsic_base(intr
);
850 wrmask
= nir_intrinsic_write_mask(intr
);
852 /* Combine groups of consecutive enabled channels in one write
853 * message. We use ffs to find the first enabled channel and then ffs on
854 * the bit-inverse, down-shifted writemask to determine the length of
855 * the block of enabled bits.
857 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
860 unsigned first_component
= ffs(wrmask
) - 1;
861 unsigned length
= ffs(~(wrmask
>> first_component
)) - 1;
863 stl
= ir3_STL(b
, offset
, 0,
864 ir3_create_collect(ctx
, &value
[first_component
], length
), 0,
865 create_immed(b
, length
), 0);
866 stl
->cat6
.dst_offset
= first_component
+ base
;
867 stl
->cat6
.type
= utype_src(intr
->src
[0]);
868 stl
->barrier_class
= IR3_BARRIER_SHARED_W
;
869 stl
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
871 array_insert(b
, b
->keeps
, stl
);
873 /* Clear the bits in the writemask that we just wrote, then try
874 * again to see if more channels are left.
876 wrmask
&= (15 << (first_component
+ length
));
880 /* src[] = { offset }. const_index[] = { base } */
882 emit_intrinsic_load_shared_ir3(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
883 struct ir3_instruction
**dst
)
885 struct ir3_block
*b
= ctx
->block
;
886 struct ir3_instruction
*load
, *offset
;
889 offset
= ir3_get_src(ctx
, &intr
->src
[0])[0];
890 base
= nir_intrinsic_base(intr
);
892 load
= ir3_LDLW(b
, offset
, 0,
893 create_immed(b
, intr
->num_components
), 0,
894 create_immed(b
, base
), 0);
896 load
->cat6
.type
= utype_dst(intr
->dest
);
897 load
->regs
[0]->wrmask
= MASK(intr
->num_components
);
899 load
->barrier_class
= IR3_BARRIER_SHARED_R
;
900 load
->barrier_conflict
= IR3_BARRIER_SHARED_W
;
902 ir3_split_dest(b
, dst
, load
, 0, intr
->num_components
);
905 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
907 emit_intrinsic_store_shared_ir3(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
909 struct ir3_block
*b
= ctx
->block
;
910 struct ir3_instruction
*store
, *offset
;
911 struct ir3_instruction
* const *value
;
912 unsigned base
, wrmask
;
914 value
= ir3_get_src(ctx
, &intr
->src
[0]);
915 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
917 base
= nir_intrinsic_base(intr
);
918 wrmask
= nir_intrinsic_write_mask(intr
);
920 /* Combine groups of consecutive enabled channels in one write
921 * message. We use ffs to find the first enabled channel and then ffs on
922 * the bit-inverse, down-shifted writemask to determine the length of
923 * the block of enabled bits.
925 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
928 unsigned first_component
= ffs(wrmask
) - 1;
929 unsigned length
= ffs(~(wrmask
>> first_component
)) - 1;
931 store
= ir3_STLW(b
, offset
, 0,
932 ir3_create_collect(ctx
, &value
[first_component
], length
), 0,
933 create_immed(b
, length
), 0);
935 store
->cat6
.dst_offset
= first_component
+ base
;
936 store
->cat6
.type
= utype_src(intr
->src
[0]);
937 store
->barrier_class
= IR3_BARRIER_SHARED_W
;
938 store
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
940 array_insert(b
, b
->keeps
, store
);
942 /* Clear the bits in the writemask that we just wrote, then try
943 * again to see if more channels are left.
945 wrmask
&= (15 << (first_component
+ length
));
950 * CS shared variable atomic intrinsics
952 * All of the shared variable atomic memory operations read a value from
953 * memory, compute a new value using one of the operations below, write the
954 * new value to memory, and return the original value read.
956 * All operations take 2 sources except CompSwap that takes 3. These
959 * 0: The offset into the shared variable storage region that the atomic
960 * operation will operate on.
961 * 1: The data parameter to the atomic function (i.e. the value to add
962 * in shared_atomic_add, etc).
963 * 2: For CompSwap only: the second data parameter.
965 static struct ir3_instruction
*
966 emit_intrinsic_atomic_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
968 struct ir3_block
*b
= ctx
->block
;
969 struct ir3_instruction
*atomic
, *src0
, *src1
;
970 type_t type
= TYPE_U32
;
972 src0
= ir3_get_src(ctx
, &intr
->src
[0])[0]; /* offset */
973 src1
= ir3_get_src(ctx
, &intr
->src
[1])[0]; /* value */
975 switch (intr
->intrinsic
) {
976 case nir_intrinsic_shared_atomic_add
:
977 atomic
= ir3_ATOMIC_ADD(b
, src0
, 0, src1
, 0);
979 case nir_intrinsic_shared_atomic_imin
:
980 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
983 case nir_intrinsic_shared_atomic_umin
:
984 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
986 case nir_intrinsic_shared_atomic_imax
:
987 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
990 case nir_intrinsic_shared_atomic_umax
:
991 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
993 case nir_intrinsic_shared_atomic_and
:
994 atomic
= ir3_ATOMIC_AND(b
, src0
, 0, src1
, 0);
996 case nir_intrinsic_shared_atomic_or
:
997 atomic
= ir3_ATOMIC_OR(b
, src0
, 0, src1
, 0);
999 case nir_intrinsic_shared_atomic_xor
:
1000 atomic
= ir3_ATOMIC_XOR(b
, src0
, 0, src1
, 0);
1002 case nir_intrinsic_shared_atomic_exchange
:
1003 atomic
= ir3_ATOMIC_XCHG(b
, src0
, 0, src1
, 0);
1005 case nir_intrinsic_shared_atomic_comp_swap
:
1006 /* for cmpxchg, src1 is [ui]vec2(data, compare): */
1007 src1
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1008 ir3_get_src(ctx
, &intr
->src
[2])[0],
1011 atomic
= ir3_ATOMIC_CMPXCHG(b
, src0
, 0, src1
, 0);
1017 atomic
->cat6
.iim_val
= 1;
1019 atomic
->cat6
.type
= type
;
1020 atomic
->barrier_class
= IR3_BARRIER_SHARED_W
;
1021 atomic
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
1023 /* even if nothing consume the result, we can't DCE the instruction: */
1024 array_insert(b
, b
->keeps
, atomic
);
1029 /* TODO handle actual indirect/dynamic case.. which is going to be weird
1030 * to handle with the image_mapping table..
1032 static struct ir3_instruction
*
1033 get_image_samp_tex_src(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1035 unsigned slot
= ir3_get_image_slot(nir_src_as_deref(intr
->src
[0]));
1036 unsigned tex_idx
= ir3_image_to_tex(&ctx
->so
->image_mapping
, slot
);
1037 struct ir3_instruction
*texture
, *sampler
;
1039 texture
= create_immed_typed(ctx
->block
, tex_idx
, TYPE_U16
);
1040 sampler
= create_immed_typed(ctx
->block
, tex_idx
, TYPE_U16
);
1042 return ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1048 /* src[] = { deref, coord, sample_index }. const_index[] = {} */
1050 emit_intrinsic_load_image(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1051 struct ir3_instruction
**dst
)
1053 struct ir3_block
*b
= ctx
->block
;
1054 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1055 struct ir3_instruction
*samp_tex
= get_image_samp_tex_src(ctx
, intr
);
1056 struct ir3_instruction
*sam
;
1057 struct ir3_instruction
* const *src0
= ir3_get_src(ctx
, &intr
->src
[1]);
1058 struct ir3_instruction
*coords
[4];
1059 unsigned flags
, ncoords
= ir3_get_image_coords(var
, &flags
);
1060 type_t type
= ir3_get_image_type(var
);
1062 /* hmm, this seems a bit odd, but it is what blob does and (at least
1063 * a5xx) just faults on bogus addresses otherwise:
1065 if (flags
& IR3_INSTR_3D
) {
1066 flags
&= ~IR3_INSTR_3D
;
1067 flags
|= IR3_INSTR_A
;
1070 for (unsigned i
= 0; i
< ncoords
; i
++)
1071 coords
[i
] = src0
[i
];
1074 coords
[ncoords
++] = create_immed(b
, 0);
1076 sam
= ir3_SAM(b
, OPC_ISAM
, type
, 0b1111, flags
,
1077 samp_tex
, ir3_create_collect(ctx
, coords
, ncoords
), NULL
);
1079 sam
->barrier_class
= IR3_BARRIER_IMAGE_R
;
1080 sam
->barrier_conflict
= IR3_BARRIER_IMAGE_W
;
1082 ir3_split_dest(b
, dst
, sam
, 0, 4);
1086 emit_intrinsic_image_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1087 struct ir3_instruction
**dst
)
1089 struct ir3_block
*b
= ctx
->block
;
1090 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1091 struct ir3_instruction
*samp_tex
= get_image_samp_tex_src(ctx
, intr
);
1092 struct ir3_instruction
*sam
, *lod
;
1093 unsigned flags
, ncoords
= ir3_get_image_coords(var
, &flags
);
1095 lod
= create_immed(b
, 0);
1096 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, 0b1111, flags
,
1097 samp_tex
, lod
, NULL
);
1099 /* Array size actually ends up in .w rather than .z. This doesn't
1100 * matter for miplevel 0, but for higher mips the value in z is
1101 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1102 * returned, which means that we have to add 1 to it for arrays for
1105 * Note use a temporary dst and then copy, since the size of the dst
1106 * array that is passed in is based on nir's understanding of the
1107 * result size, not the hardware's
1109 struct ir3_instruction
*tmp
[4];
1111 ir3_split_dest(b
, tmp
, sam
, 0, 4);
1113 /* get_size instruction returns size in bytes instead of texels
1114 * for imageBuffer, so we need to divide it by the pixel size
1115 * of the image format.
1117 * TODO: This is at least true on a5xx. Check other gens.
1119 enum glsl_sampler_dim dim
=
1120 glsl_get_sampler_dim(glsl_without_array(var
->type
));
1121 if (dim
== GLSL_SAMPLER_DIM_BUF
) {
1122 /* Since all the possible values the divisor can take are
1123 * power-of-two (4, 8, or 16), the division is implemented
1125 * During shader setup, the log2 of the image format's
1126 * bytes-per-pixel should have been emitted in 2nd slot of
1127 * image_dims. See ir3_shader::emit_image_dims().
1129 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
1130 unsigned cb
= regid(const_state
->offsets
.image_dims
, 0) +
1131 const_state
->image_dims
.off
[var
->data
.driver_location
];
1132 struct ir3_instruction
*aux
= create_uniform(b
, cb
+ 1);
1134 tmp
[0] = ir3_SHR_B(b
, tmp
[0], 0, aux
, 0);
1137 for (unsigned i
= 0; i
< ncoords
; i
++)
1140 if (flags
& IR3_INSTR_A
) {
1141 if (ctx
->compiler
->levels_add_one
) {
1142 dst
[ncoords
-1] = ir3_ADD_U(b
, tmp
[3], 0, create_immed(b
, 1), 0);
1144 dst
[ncoords
-1] = ir3_MOV(b
, tmp
[3], TYPE_U32
);
1150 emit_intrinsic_barrier(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1152 struct ir3_block
*b
= ctx
->block
;
1153 struct ir3_instruction
*barrier
;
1155 switch (intr
->intrinsic
) {
1156 case nir_intrinsic_barrier
:
1157 barrier
= ir3_BAR(b
);
1158 barrier
->cat7
.g
= true;
1159 barrier
->cat7
.l
= true;
1160 barrier
->flags
= IR3_INSTR_SS
| IR3_INSTR_SY
;
1161 barrier
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1163 case nir_intrinsic_memory_barrier
:
1164 barrier
= ir3_FENCE(b
);
1165 barrier
->cat7
.g
= true;
1166 barrier
->cat7
.r
= true;
1167 barrier
->cat7
.w
= true;
1168 barrier
->cat7
.l
= true;
1169 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
|
1170 IR3_BARRIER_BUFFER_W
;
1171 barrier
->barrier_conflict
=
1172 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
1173 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1175 case nir_intrinsic_memory_barrier_atomic_counter
:
1176 case nir_intrinsic_memory_barrier_buffer
:
1177 barrier
= ir3_FENCE(b
);
1178 barrier
->cat7
.g
= true;
1179 barrier
->cat7
.r
= true;
1180 barrier
->cat7
.w
= true;
1181 barrier
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1182 barrier
->barrier_conflict
= IR3_BARRIER_BUFFER_R
|
1183 IR3_BARRIER_BUFFER_W
;
1185 case nir_intrinsic_memory_barrier_image
:
1186 // TODO double check if this should have .g set
1187 barrier
= ir3_FENCE(b
);
1188 barrier
->cat7
.g
= true;
1189 barrier
->cat7
.r
= true;
1190 barrier
->cat7
.w
= true;
1191 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
;
1192 barrier
->barrier_conflict
= IR3_BARRIER_IMAGE_R
|
1193 IR3_BARRIER_IMAGE_W
;
1195 case nir_intrinsic_memory_barrier_shared
:
1196 barrier
= ir3_FENCE(b
);
1197 barrier
->cat7
.g
= true;
1198 barrier
->cat7
.l
= true;
1199 barrier
->cat7
.r
= true;
1200 barrier
->cat7
.w
= true;
1201 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
;
1202 barrier
->barrier_conflict
= IR3_BARRIER_SHARED_R
|
1203 IR3_BARRIER_SHARED_W
;
1205 case nir_intrinsic_group_memory_barrier
:
1206 barrier
= ir3_FENCE(b
);
1207 barrier
->cat7
.g
= true;
1208 barrier
->cat7
.l
= true;
1209 barrier
->cat7
.r
= true;
1210 barrier
->cat7
.w
= true;
1211 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
|
1212 IR3_BARRIER_IMAGE_W
|
1213 IR3_BARRIER_BUFFER_W
;
1214 barrier
->barrier_conflict
=
1215 IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
|
1216 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
1217 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1223 /* make sure barrier doesn't get DCE'd */
1224 array_insert(b
, b
->keeps
, barrier
);
1227 static void add_sysval_input_compmask(struct ir3_context
*ctx
,
1228 gl_system_value slot
, unsigned compmask
,
1229 struct ir3_instruction
*instr
)
1231 struct ir3_shader_variant
*so
= ctx
->so
;
1232 unsigned n
= so
->inputs_count
++;
1234 assert(instr
->opc
== OPC_META_INPUT
);
1235 instr
->input
.inidx
= n
;
1236 instr
->input
.sysval
= slot
;
1238 so
->inputs
[n
].sysval
= true;
1239 so
->inputs
[n
].slot
= slot
;
1240 so
->inputs
[n
].compmask
= compmask
;
1241 so
->inputs
[n
].interpolate
= INTERP_MODE_FLAT
;
1245 static struct ir3_instruction
*
1246 create_sysval_input(struct ir3_context
*ctx
, gl_system_value slot
,
1250 struct ir3_instruction
*sysval
= create_input(ctx
, compmask
);
1251 add_sysval_input_compmask(ctx
, slot
, compmask
, sysval
);
1255 static struct ir3_instruction
*
1256 get_barycentric_centroid(struct ir3_context
*ctx
)
1258 if (!ctx
->ij_centroid
) {
1259 struct ir3_instruction
*xy
[2];
1260 struct ir3_instruction
*ij
;
1262 ij
= create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_CENTROID
, 0x3);
1263 ir3_split_dest(ctx
->block
, xy
, ij
, 0, 2);
1265 ctx
->ij_centroid
= ir3_create_collect(ctx
, xy
, 2);
1268 return ctx
->ij_centroid
;
1271 static struct ir3_instruction
*
1272 get_barycentric_sample(struct ir3_context
*ctx
)
1274 if (!ctx
->ij_sample
) {
1275 struct ir3_instruction
*xy
[2];
1276 struct ir3_instruction
*ij
;
1278 ij
= create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_SAMPLE
, 0x3);
1279 ir3_split_dest(ctx
->block
, xy
, ij
, 0, 2);
1281 ctx
->ij_sample
= ir3_create_collect(ctx
, xy
, 2);
1284 return ctx
->ij_sample
;
1287 static struct ir3_instruction
*
1288 get_barycentric_pixel(struct ir3_context
*ctx
)
1290 /* TODO when tgsi_to_nir supports "new-style" FS inputs switch
1291 * this to create ij_pixel only on demand:
1293 return ctx
->ij_pixel
;
1296 static struct ir3_instruction
*
1297 get_frag_coord(struct ir3_context
*ctx
)
1299 if (!ctx
->frag_coord
) {
1300 struct ir3_block
*b
= ctx
->block
;
1301 struct ir3_instruction
*xyzw
[4];
1302 struct ir3_instruction
*hw_frag_coord
;
1304 hw_frag_coord
= create_sysval_input(ctx
, SYSTEM_VALUE_FRAG_COORD
, 0xf);
1305 ir3_split_dest(ctx
->block
, xyzw
, hw_frag_coord
, 0, 4);
1307 /* for frag_coord.xy, we get unsigned values.. we need
1308 * to subtract (integer) 8 and divide by 16 (right-
1309 * shift by 4) then convert to float:
1313 * mov.u32f32 dst, tmp
1316 for (int i
= 0; i
< 2; i
++) {
1317 xyzw
[i
] = ir3_SUB_S(b
, xyzw
[i
], 0,
1318 create_immed(b
, 8), 0);
1319 xyzw
[i
] = ir3_SHR_B(b
, xyzw
[i
], 0,
1320 create_immed(b
, 4), 0);
1321 xyzw
[i
] = ir3_COV(b
, xyzw
[i
], TYPE_U32
, TYPE_F32
);
1324 ctx
->frag_coord
= ir3_create_collect(ctx
, xyzw
, 4);
1325 ctx
->so
->frag_coord
= true;
1328 return ctx
->frag_coord
;
1332 emit_intrinsic(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1334 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1335 struct ir3_instruction
**dst
;
1336 struct ir3_instruction
* const *src
;
1337 struct ir3_block
*b
= ctx
->block
;
1340 if (info
->has_dest
) {
1341 unsigned n
= nir_intrinsic_dest_components(intr
);
1342 dst
= ir3_get_dst(ctx
, &intr
->dest
, n
);
1347 const unsigned primitive_param
= ctx
->so
->shader
->const_state
.offsets
.primitive_param
* 4;
1348 const unsigned primitive_map
= ctx
->so
->shader
->const_state
.offsets
.primitive_map
* 4;
1350 switch (intr
->intrinsic
) {
1351 case nir_intrinsic_load_uniform
:
1352 idx
= nir_intrinsic_base(intr
);
1353 if (nir_src_is_const(intr
->src
[0])) {
1354 idx
+= nir_src_as_uint(intr
->src
[0]);
1355 for (int i
= 0; i
< intr
->num_components
; i
++) {
1356 dst
[i
] = create_uniform_typed(b
, idx
+ i
,
1357 nir_dest_bit_size(intr
->dest
) < 32 ? TYPE_F16
: TYPE_F32
);
1360 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1361 for (int i
= 0; i
< intr
->num_components
; i
++) {
1362 dst
[i
] = create_uniform_indirect(b
, idx
+ i
,
1363 ir3_get_addr(ctx
, src
[0], 1));
1365 /* NOTE: if relative addressing is used, we set
1366 * constlen in the compiler (to worst-case value)
1367 * since we don't know in the assembler what the max
1368 * addr reg value can be:
1370 ctx
->so
->constlen
= MAX2(ctx
->so
->constlen
,
1371 ctx
->so
->shader
->ubo_state
.size
/ 16);
1375 case nir_intrinsic_load_vs_primitive_stride_ir3
:
1376 dst
[0] = create_uniform(b
, primitive_param
+ 0);
1378 case nir_intrinsic_load_vs_vertex_stride_ir3
:
1379 dst
[0] = create_uniform(b
, primitive_param
+ 1);
1381 case nir_intrinsic_load_hs_patch_stride_ir3
:
1382 dst
[0] = create_uniform(b
, primitive_param
+ 2);
1384 case nir_intrinsic_load_patch_vertices_in
:
1385 dst
[0] = create_uniform(b
, primitive_param
+ 3);
1387 case nir_intrinsic_load_tess_param_base_ir3
:
1388 dst
[0] = create_uniform(b
, primitive_param
+ 4);
1389 dst
[1] = create_uniform(b
, primitive_param
+ 5);
1391 case nir_intrinsic_load_tess_factor_base_ir3
:
1392 dst
[0] = create_uniform(b
, primitive_param
+ 6);
1393 dst
[1] = create_uniform(b
, primitive_param
+ 7);
1396 case nir_intrinsic_load_primitive_location_ir3
:
1397 idx
= nir_intrinsic_driver_location(intr
);
1398 dst
[0] = create_uniform(b
, primitive_map
+ idx
);
1401 case nir_intrinsic_load_gs_header_ir3
:
1402 dst
[0] = ctx
->gs_header
;
1404 case nir_intrinsic_load_tcs_header_ir3
:
1405 dst
[0] = ctx
->tcs_header
;
1408 case nir_intrinsic_load_primitive_id
:
1409 dst
[0] = ctx
->primitive_id
;
1412 case nir_intrinsic_load_tess_coord
:
1413 if (!ctx
->tess_coord
) {
1415 create_sysval_input(ctx
, SYSTEM_VALUE_TESS_COORD
, 0x3);
1417 ir3_split_dest(b
, dst
, ctx
->tess_coord
, 0, 2);
1419 /* Unused, but ir3_put_dst() below wants to free something */
1420 dst
[2] = create_immed(b
, 0);
1423 case nir_intrinsic_end_patch_ir3
:
1424 assert(ctx
->so
->type
== MESA_SHADER_TESS_CTRL
);
1425 struct ir3_instruction
*end
= ir3_ENDPATCH(b
);
1426 array_insert(b
, b
->keeps
, end
);
1428 end
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1429 end
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
1432 case nir_intrinsic_store_global_ir3
: {
1433 struct ir3_instruction
*value
, *addr
, *offset
;
1435 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1436 ir3_get_src(ctx
, &intr
->src
[1])[0],
1437 ir3_get_src(ctx
, &intr
->src
[1])[1]
1440 offset
= ir3_get_src(ctx
, &intr
->src
[2])[0];
1442 value
= ir3_create_collect(ctx
, ir3_get_src(ctx
, &intr
->src
[0]),
1443 intr
->num_components
);
1445 struct ir3_instruction
*stg
=
1446 ir3_STG_G(ctx
->block
, addr
, 0, value
, 0,
1447 create_immed(ctx
->block
, intr
->num_components
), 0, offset
, 0);
1448 stg
->cat6
.type
= TYPE_U32
;
1449 stg
->cat6
.iim_val
= 1;
1451 array_insert(b
, b
->keeps
, stg
);
1453 stg
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1454 stg
->barrier_conflict
= IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1458 case nir_intrinsic_load_global_ir3
: {
1459 struct ir3_instruction
*addr
, *offset
;
1461 addr
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1462 ir3_get_src(ctx
, &intr
->src
[0])[0],
1463 ir3_get_src(ctx
, &intr
->src
[0])[1]
1466 offset
= ir3_get_src(ctx
, &intr
->src
[1])[0];
1468 struct ir3_instruction
*load
=
1469 ir3_LDG(b
, addr
, 0, create_immed(ctx
->block
, intr
->num_components
),
1471 load
->cat6
.type
= TYPE_U32
;
1472 load
->regs
[0]->wrmask
= MASK(intr
->num_components
);
1474 load
->barrier_class
= IR3_BARRIER_BUFFER_R
;
1475 load
->barrier_conflict
= IR3_BARRIER_BUFFER_W
;
1477 ir3_split_dest(b
, dst
, load
, 0, intr
->num_components
);
1481 case nir_intrinsic_load_ubo
:
1482 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
1484 case nir_intrinsic_load_frag_coord
:
1485 ir3_split_dest(b
, dst
, get_frag_coord(ctx
), 0, 4);
1487 case nir_intrinsic_load_sample_pos_from_id
: {
1488 /* NOTE: blob seems to always use TYPE_F16 and then cov.f16f32,
1489 * but that doesn't seem necessary.
1491 struct ir3_instruction
*offset
=
1492 ir3_RGETPOS(b
, ir3_get_src(ctx
, &intr
->src
[0])[0], 0);
1493 offset
->regs
[0]->wrmask
= 0x3;
1494 offset
->cat5
.type
= TYPE_F32
;
1496 ir3_split_dest(b
, dst
, offset
, 0, 2);
1500 case nir_intrinsic_load_size_ir3
:
1501 if (!ctx
->ij_size
) {
1503 create_sysval_input(ctx
, SYSTEM_VALUE_BARYCENTRIC_SIZE
, 0x1);
1505 dst
[0] = ctx
->ij_size
;
1507 case nir_intrinsic_load_barycentric_centroid
:
1508 ir3_split_dest(b
, dst
, get_barycentric_centroid(ctx
), 0, 2);
1510 case nir_intrinsic_load_barycentric_sample
:
1511 if (ctx
->so
->key
.msaa
) {
1512 ir3_split_dest(b
, dst
, get_barycentric_sample(ctx
), 0, 2);
1514 ir3_split_dest(b
, dst
, get_barycentric_pixel(ctx
), 0, 2);
1517 case nir_intrinsic_load_barycentric_pixel
:
1518 ir3_split_dest(b
, dst
, get_barycentric_pixel(ctx
), 0, 2);
1520 case nir_intrinsic_load_interpolated_input
:
1521 idx
= nir_intrinsic_base(intr
);
1522 comp
= nir_intrinsic_component(intr
);
1523 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1524 if (nir_src_is_const(intr
->src
[1])) {
1525 struct ir3_instruction
*coord
= ir3_create_collect(ctx
, src
, 2);
1526 idx
+= nir_src_as_uint(intr
->src
[1]);
1527 for (int i
= 0; i
< intr
->num_components
; i
++) {
1528 unsigned inloc
= idx
* 4 + i
+ comp
;
1529 if (ctx
->so
->inputs
[idx
].bary
&&
1530 !ctx
->so
->inputs
[idx
].use_ldlv
) {
1531 dst
[i
] = ir3_BARY_F(b
, create_immed(b
, inloc
), 0, coord
, 0);
1533 /* for non-varyings use the pre-setup input, since
1534 * that is easier than mapping things back to a
1535 * nir_variable to figure out what it is.
1537 dst
[i
] = ctx
->ir
->inputs
[inloc
];
1541 ir3_context_error(ctx
, "unhandled");
1544 case nir_intrinsic_load_input
:
1545 idx
= nir_intrinsic_base(intr
);
1546 comp
= nir_intrinsic_component(intr
);
1547 if (nir_src_is_const(intr
->src
[0])) {
1548 idx
+= nir_src_as_uint(intr
->src
[0]);
1549 for (int i
= 0; i
< intr
->num_components
; i
++) {
1550 unsigned n
= idx
* 4 + i
+ comp
;
1551 dst
[i
] = ctx
->inputs
[n
];
1552 compile_assert(ctx
, ctx
->inputs
[n
]);
1555 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1556 struct ir3_instruction
*collect
=
1557 ir3_create_collect(ctx
, ctx
->ir
->inputs
, ctx
->ninputs
);
1558 struct ir3_instruction
*addr
= ir3_get_addr(ctx
, src
[0], 4);
1559 for (int i
= 0; i
< intr
->num_components
; i
++) {
1560 unsigned n
= idx
* 4 + i
+ comp
;
1561 dst
[i
] = create_indirect_load(ctx
, ctx
->ninputs
,
1566 /* All SSBO intrinsics should have been lowered by 'lower_io_offsets'
1567 * pass and replaced by an ir3-specifc version that adds the
1568 * dword-offset in the last source.
1570 case nir_intrinsic_load_ssbo_ir3
:
1571 ctx
->funcs
->emit_intrinsic_load_ssbo(ctx
, intr
, dst
);
1573 case nir_intrinsic_store_ssbo_ir3
:
1574 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1575 !ctx
->s
->info
.fs
.early_fragment_tests
)
1576 ctx
->so
->no_earlyz
= true;
1577 ctx
->funcs
->emit_intrinsic_store_ssbo(ctx
, intr
);
1579 case nir_intrinsic_get_buffer_size
:
1580 emit_intrinsic_ssbo_size(ctx
, intr
, dst
);
1582 case nir_intrinsic_ssbo_atomic_add_ir3
:
1583 case nir_intrinsic_ssbo_atomic_imin_ir3
:
1584 case nir_intrinsic_ssbo_atomic_umin_ir3
:
1585 case nir_intrinsic_ssbo_atomic_imax_ir3
:
1586 case nir_intrinsic_ssbo_atomic_umax_ir3
:
1587 case nir_intrinsic_ssbo_atomic_and_ir3
:
1588 case nir_intrinsic_ssbo_atomic_or_ir3
:
1589 case nir_intrinsic_ssbo_atomic_xor_ir3
:
1590 case nir_intrinsic_ssbo_atomic_exchange_ir3
:
1591 case nir_intrinsic_ssbo_atomic_comp_swap_ir3
:
1592 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1593 !ctx
->s
->info
.fs
.early_fragment_tests
)
1594 ctx
->so
->no_earlyz
= true;
1595 dst
[0] = ctx
->funcs
->emit_intrinsic_atomic_ssbo(ctx
, intr
);
1597 case nir_intrinsic_load_shared
:
1598 emit_intrinsic_load_shared(ctx
, intr
, dst
);
1600 case nir_intrinsic_store_shared
:
1601 emit_intrinsic_store_shared(ctx
, intr
);
1603 case nir_intrinsic_shared_atomic_add
:
1604 case nir_intrinsic_shared_atomic_imin
:
1605 case nir_intrinsic_shared_atomic_umin
:
1606 case nir_intrinsic_shared_atomic_imax
:
1607 case nir_intrinsic_shared_atomic_umax
:
1608 case nir_intrinsic_shared_atomic_and
:
1609 case nir_intrinsic_shared_atomic_or
:
1610 case nir_intrinsic_shared_atomic_xor
:
1611 case nir_intrinsic_shared_atomic_exchange
:
1612 case nir_intrinsic_shared_atomic_comp_swap
:
1613 dst
[0] = emit_intrinsic_atomic_shared(ctx
, intr
);
1615 case nir_intrinsic_image_deref_load
:
1616 emit_intrinsic_load_image(ctx
, intr
, dst
);
1618 case nir_intrinsic_image_deref_store
:
1619 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1620 !ctx
->s
->info
.fs
.early_fragment_tests
)
1621 ctx
->so
->no_earlyz
= true;
1622 ctx
->funcs
->emit_intrinsic_store_image(ctx
, intr
);
1624 case nir_intrinsic_image_deref_size
:
1625 emit_intrinsic_image_size(ctx
, intr
, dst
);
1627 case nir_intrinsic_image_deref_atomic_add
:
1628 case nir_intrinsic_image_deref_atomic_imin
:
1629 case nir_intrinsic_image_deref_atomic_umin
:
1630 case nir_intrinsic_image_deref_atomic_imax
:
1631 case nir_intrinsic_image_deref_atomic_umax
:
1632 case nir_intrinsic_image_deref_atomic_and
:
1633 case nir_intrinsic_image_deref_atomic_or
:
1634 case nir_intrinsic_image_deref_atomic_xor
:
1635 case nir_intrinsic_image_deref_atomic_exchange
:
1636 case nir_intrinsic_image_deref_atomic_comp_swap
:
1637 if ((ctx
->so
->type
== MESA_SHADER_FRAGMENT
) &&
1638 !ctx
->s
->info
.fs
.early_fragment_tests
)
1639 ctx
->so
->no_earlyz
= true;
1640 dst
[0] = ctx
->funcs
->emit_intrinsic_atomic_image(ctx
, intr
);
1642 case nir_intrinsic_barrier
:
1643 case nir_intrinsic_memory_barrier
:
1644 case nir_intrinsic_group_memory_barrier
:
1645 case nir_intrinsic_memory_barrier_atomic_counter
:
1646 case nir_intrinsic_memory_barrier_buffer
:
1647 case nir_intrinsic_memory_barrier_image
:
1648 case nir_intrinsic_memory_barrier_shared
:
1649 emit_intrinsic_barrier(ctx
, intr
);
1650 /* note that blk ptr no longer valid, make that obvious: */
1653 case nir_intrinsic_store_output
:
1654 idx
= nir_intrinsic_base(intr
);
1655 comp
= nir_intrinsic_component(intr
);
1656 compile_assert(ctx
, nir_src_is_const(intr
->src
[1]));
1657 idx
+= nir_src_as_uint(intr
->src
[1]);
1659 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1660 for (int i
= 0; i
< intr
->num_components
; i
++) {
1661 unsigned n
= idx
* 4 + i
+ comp
;
1662 ctx
->outputs
[n
] = src
[i
];
1665 case nir_intrinsic_load_base_vertex
:
1666 case nir_intrinsic_load_first_vertex
:
1667 if (!ctx
->basevertex
) {
1668 ctx
->basevertex
= create_driver_param(ctx
, IR3_DP_VTXID_BASE
);
1670 dst
[0] = ctx
->basevertex
;
1672 case nir_intrinsic_load_vertex_id_zero_base
:
1673 case nir_intrinsic_load_vertex_id
:
1674 if (!ctx
->vertex_id
) {
1675 gl_system_value sv
= (intr
->intrinsic
== nir_intrinsic_load_vertex_id
) ?
1676 SYSTEM_VALUE_VERTEX_ID
: SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1677 ctx
->vertex_id
= create_sysval_input(ctx
, sv
, 0x1);
1679 dst
[0] = ctx
->vertex_id
;
1681 case nir_intrinsic_load_instance_id
:
1682 if (!ctx
->instance_id
) {
1683 ctx
->instance_id
= create_sysval_input(ctx
, SYSTEM_VALUE_INSTANCE_ID
, 0x1);
1685 dst
[0] = ctx
->instance_id
;
1687 case nir_intrinsic_load_sample_id
:
1688 ctx
->so
->per_samp
= true;
1690 case nir_intrinsic_load_sample_id_no_per_sample
:
1691 if (!ctx
->samp_id
) {
1692 ctx
->samp_id
= create_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_ID
, 0x1);
1693 ctx
->samp_id
->regs
[0]->flags
|= IR3_REG_HALF
;
1695 dst
[0] = ir3_COV(b
, ctx
->samp_id
, TYPE_U16
, TYPE_U32
);
1697 case nir_intrinsic_load_sample_mask_in
:
1698 if (!ctx
->samp_mask_in
) {
1699 ctx
->samp_mask_in
= create_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_MASK_IN
, 0x1);
1701 dst
[0] = ctx
->samp_mask_in
;
1703 case nir_intrinsic_load_user_clip_plane
:
1704 idx
= nir_intrinsic_ucp_id(intr
);
1705 for (int i
= 0; i
< intr
->num_components
; i
++) {
1706 unsigned n
= idx
* 4 + i
;
1707 dst
[i
] = create_driver_param(ctx
, IR3_DP_UCP0_X
+ n
);
1710 case nir_intrinsic_load_front_face
:
1711 if (!ctx
->frag_face
) {
1712 ctx
->so
->frag_face
= true;
1713 ctx
->frag_face
= create_sysval_input(ctx
, SYSTEM_VALUE_FRONT_FACE
, 0x1);
1714 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
1716 /* for fragface, we get -1 for back and 0 for front. However this is
1717 * the inverse of what nir expects (where ~0 is true).
1719 dst
[0] = ir3_COV(b
, ctx
->frag_face
, TYPE_S16
, TYPE_S32
);
1720 dst
[0] = ir3_NOT_B(b
, dst
[0], 0);
1722 case nir_intrinsic_load_local_invocation_id
:
1723 if (!ctx
->local_invocation_id
) {
1724 ctx
->local_invocation_id
=
1725 create_sysval_input(ctx
, SYSTEM_VALUE_LOCAL_INVOCATION_ID
, 0x7);
1727 ir3_split_dest(b
, dst
, ctx
->local_invocation_id
, 0, 3);
1729 case nir_intrinsic_load_work_group_id
:
1730 if (!ctx
->work_group_id
) {
1731 ctx
->work_group_id
=
1732 create_sysval_input(ctx
, SYSTEM_VALUE_WORK_GROUP_ID
, 0x7);
1733 ctx
->work_group_id
->regs
[0]->flags
|= IR3_REG_HIGH
;
1735 ir3_split_dest(b
, dst
, ctx
->work_group_id
, 0, 3);
1737 case nir_intrinsic_load_num_work_groups
:
1738 for (int i
= 0; i
< intr
->num_components
; i
++) {
1739 dst
[i
] = create_driver_param(ctx
, IR3_DP_NUM_WORK_GROUPS_X
+ i
);
1742 case nir_intrinsic_load_local_group_size
:
1743 for (int i
= 0; i
< intr
->num_components
; i
++) {
1744 dst
[i
] = create_driver_param(ctx
, IR3_DP_LOCAL_GROUP_SIZE_X
+ i
);
1747 case nir_intrinsic_discard_if
:
1748 case nir_intrinsic_discard
: {
1749 struct ir3_instruction
*cond
, *kill
;
1751 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1752 /* conditional discard: */
1753 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1754 cond
= ir3_b2n(b
, src
[0]);
1756 /* unconditional discard: */
1757 cond
= create_immed(b
, 1);
1760 /* NOTE: only cmps.*.* can write p0.x: */
1761 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1762 cond
->cat2
.condition
= IR3_COND_NE
;
1764 /* condition always goes in predicate register: */
1765 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1766 cond
->regs
[0]->flags
&= ~IR3_REG_SSA
;
1768 kill
= ir3_KILL(b
, cond
, 0);
1769 array_insert(ctx
->ir
, ctx
->ir
->predicates
, kill
);
1771 array_insert(b
, b
->keeps
, kill
);
1772 ctx
->so
->no_earlyz
= true;
1777 case nir_intrinsic_cond_end_ir3
: {
1778 struct ir3_instruction
*cond
, *kill
;
1780 src
= ir3_get_src(ctx
, &intr
->src
[0]);
1781 cond
= ir3_b2n(b
, src
[0]);
1783 /* NOTE: only cmps.*.* can write p0.x: */
1784 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1785 cond
->cat2
.condition
= IR3_COND_NE
;
1787 /* condition always goes in predicate register: */
1788 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1790 kill
= ir3_CONDEND(b
, cond
, 0);
1792 kill
->barrier_class
= IR3_BARRIER_EVERYTHING
;
1793 kill
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
1795 array_insert(ctx
->ir
, ctx
->ir
->predicates
, kill
);
1796 array_insert(b
, b
->keeps
, kill
);
1800 case nir_intrinsic_load_shared_ir3
:
1801 emit_intrinsic_load_shared_ir3(ctx
, intr
, dst
);
1803 case nir_intrinsic_store_shared_ir3
:
1804 emit_intrinsic_store_shared_ir3(ctx
, intr
);
1807 ir3_context_error(ctx
, "Unhandled intrinsic type: %s\n",
1808 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1813 ir3_put_dst(ctx
, &intr
->dest
);
1817 emit_load_const(struct ir3_context
*ctx
, nir_load_const_instr
*instr
)
1819 struct ir3_instruction
**dst
= ir3_get_dst_ssa(ctx
, &instr
->def
,
1820 instr
->def
.num_components
);
1822 if (instr
->def
.bit_size
< 32) {
1823 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1824 dst
[i
] = create_immed_typed(ctx
->block
,
1825 instr
->value
[i
].u16
,
1828 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1829 dst
[i
] = create_immed_typed(ctx
->block
,
1830 instr
->value
[i
].u32
,
1837 emit_undef(struct ir3_context
*ctx
, nir_ssa_undef_instr
*undef
)
1839 struct ir3_instruction
**dst
= ir3_get_dst_ssa(ctx
, &undef
->def
,
1840 undef
->def
.num_components
);
1841 type_t type
= (undef
->def
.bit_size
< 32) ? TYPE_U16
: TYPE_U32
;
1843 /* backend doesn't want undefined instructions, so just plug
1846 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1847 dst
[i
] = create_immed_typed(ctx
->block
, fui(0.0), type
);
1851 * texture fetch/sample instructions:
1855 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1857 unsigned coords
, flags
= 0;
1859 /* note: would use tex->coord_components.. except txs.. also,
1860 * since array index goes after shadow ref, we don't want to
1863 switch (tex
->sampler_dim
) {
1864 case GLSL_SAMPLER_DIM_1D
:
1865 case GLSL_SAMPLER_DIM_BUF
:
1868 case GLSL_SAMPLER_DIM_2D
:
1869 case GLSL_SAMPLER_DIM_RECT
:
1870 case GLSL_SAMPLER_DIM_EXTERNAL
:
1871 case GLSL_SAMPLER_DIM_MS
:
1874 case GLSL_SAMPLER_DIM_3D
:
1875 case GLSL_SAMPLER_DIM_CUBE
:
1877 flags
|= IR3_INSTR_3D
;
1880 unreachable("bad sampler_dim");
1883 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
1884 flags
|= IR3_INSTR_S
;
1886 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
)
1887 flags
|= IR3_INSTR_A
;
1893 /* Gets the sampler/texture idx as a hvec2. Which could either be dynamic
1894 * or immediate (in which case it will get lowered later to a non .s2en
1895 * version of the tex instruction which encode tex/samp as immediates:
1897 static struct ir3_instruction
*
1898 get_tex_samp_tex_src(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
1900 int texture_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_texture_offset
);
1901 int sampler_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_sampler_offset
);
1902 struct ir3_instruction
*texture
, *sampler
;
1904 if (texture_idx
>= 0) {
1905 texture
= ir3_get_src(ctx
, &tex
->src
[texture_idx
].src
)[0];
1906 texture
= ir3_COV(ctx
->block
, texture
, TYPE_U32
, TYPE_U16
);
1908 /* TODO what to do for dynamic case? I guess we only need the
1909 * max index for astc srgb workaround so maybe not a problem
1910 * to worry about if we don't enable indirect samplers for
1913 ctx
->max_texture_index
= MAX2(ctx
->max_texture_index
, tex
->texture_index
);
1914 texture
= create_immed_typed(ctx
->block
, tex
->texture_index
, TYPE_U16
);
1917 if (sampler_idx
>= 0) {
1918 sampler
= ir3_get_src(ctx
, &tex
->src
[sampler_idx
].src
)[0];
1919 sampler
= ir3_COV(ctx
->block
, sampler
, TYPE_U32
, TYPE_U16
);
1921 sampler
= create_immed_typed(ctx
->block
, tex
->sampler_index
, TYPE_U16
);
1924 return ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
1931 emit_tex(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
1933 struct ir3_block
*b
= ctx
->block
;
1934 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1935 struct ir3_instruction
* const *coord
, * const *off
, * const *ddx
, * const *ddy
;
1936 struct ir3_instruction
*lod
, *compare
, *proj
, *sample_index
;
1937 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1938 unsigned i
, coords
, flags
, ncomp
;
1939 unsigned nsrc0
= 0, nsrc1
= 0;
1943 ncomp
= nir_dest_num_components(tex
->dest
);
1945 coord
= off
= ddx
= ddy
= NULL
;
1946 lod
= proj
= compare
= sample_index
= NULL
;
1948 dst
= ir3_get_dst(ctx
, &tex
->dest
, ncomp
);
1950 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1951 switch (tex
->src
[i
].src_type
) {
1952 case nir_tex_src_coord
:
1953 coord
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1955 case nir_tex_src_bias
:
1956 lod
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1959 case nir_tex_src_lod
:
1960 lod
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1963 case nir_tex_src_comparator
: /* shadow comparator */
1964 compare
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1966 case nir_tex_src_projector
:
1967 proj
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1970 case nir_tex_src_offset
:
1971 off
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1974 case nir_tex_src_ddx
:
1975 ddx
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1977 case nir_tex_src_ddy
:
1978 ddy
= ir3_get_src(ctx
, &tex
->src
[i
].src
);
1980 case nir_tex_src_ms_index
:
1981 sample_index
= ir3_get_src(ctx
, &tex
->src
[i
].src
)[0];
1983 case nir_tex_src_texture_offset
:
1984 case nir_tex_src_sampler_offset
:
1985 /* handled in get_tex_samp_src() */
1988 ir3_context_error(ctx
, "Unhandled NIR tex src type: %d\n",
1989 tex
->src
[i
].src_type
);
1995 case nir_texop_tex_prefetch
:
1996 compile_assert(ctx
, !has_bias
);
1997 compile_assert(ctx
, !has_lod
);
1998 compile_assert(ctx
, !compare
);
1999 compile_assert(ctx
, !has_proj
);
2000 compile_assert(ctx
, !has_off
);
2001 compile_assert(ctx
, !ddx
);
2002 compile_assert(ctx
, !ddy
);
2003 compile_assert(ctx
, !sample_index
);
2004 compile_assert(ctx
, nir_tex_instr_src_index(tex
, nir_tex_src_texture_offset
) < 0);
2005 compile_assert(ctx
, nir_tex_instr_src_index(tex
, nir_tex_src_sampler_offset
) < 0);
2007 if (ctx
->so
->num_sampler_prefetch
< IR3_MAX_SAMPLER_PREFETCH
) {
2008 opc
= OPC_META_TEX_PREFETCH
;
2009 ctx
->so
->num_sampler_prefetch
++;
2013 case nir_texop_tex
: opc
= has_lod
? OPC_SAML
: OPC_SAM
; break;
2014 case nir_texop_txb
: opc
= OPC_SAMB
; break;
2015 case nir_texop_txl
: opc
= OPC_SAML
; break;
2016 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
2017 case nir_texop_txf
: opc
= OPC_ISAML
; break;
2018 case nir_texop_lod
: opc
= OPC_GETLOD
; break;
2020 /* NOTE: a4xx might need to emulate gather w/ txf (this is
2021 * what blob does, seems gather is broken?), and a3xx did
2022 * not support it (but probably could also emulate).
2024 switch (tex
->component
) {
2025 case 0: opc
= OPC_GATHER4R
; break;
2026 case 1: opc
= OPC_GATHER4G
; break;
2027 case 2: opc
= OPC_GATHER4B
; break;
2028 case 3: opc
= OPC_GATHER4A
; break;
2031 case nir_texop_txf_ms_fb
:
2032 case nir_texop_txf_ms
: opc
= OPC_ISAMM
; break;
2034 ir3_context_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
2038 tex_info(tex
, &flags
, &coords
);
2041 * lay out the first argument in the proper order:
2042 * - actual coordinates first
2043 * - shadow reference
2046 * - starting at offset 4, dpdx.xy, dpdy.xy
2048 * bias/lod go into the second arg
2051 /* insert tex coords: */
2052 for (i
= 0; i
< coords
; i
++)
2057 /* scale up integer coords for TXF based on the LOD */
2058 if (ctx
->compiler
->unminify_coords
&& (opc
== OPC_ISAML
)) {
2060 for (i
= 0; i
< coords
; i
++)
2061 src0
[i
] = ir3_SHL_B(b
, src0
[i
], 0, lod
, 0);
2065 /* hw doesn't do 1d, so we treat it as 2d with
2066 * height of 1, and patch up the y coord.
2069 src0
[nsrc0
++] = create_immed(b
, 0);
2071 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
2075 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
2076 src0
[nsrc0
++] = compare
;
2078 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
) {
2079 struct ir3_instruction
*idx
= coord
[coords
];
2081 /* the array coord for cube arrays needs 0.5 added to it */
2082 if (ctx
->compiler
->array_index_add_half
&& !is_isam(opc
))
2083 idx
= ir3_ADD_F(b
, idx
, 0, create_immed(b
, fui(0.5)), 0);
2085 src0
[nsrc0
++] = idx
;
2089 src0
[nsrc0
++] = proj
;
2090 flags
|= IR3_INSTR_P
;
2093 /* pad to 4, then ddx/ddy: */
2094 if (tex
->op
== nir_texop_txd
) {
2096 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2097 for (i
= 0; i
< coords
; i
++)
2098 src0
[nsrc0
++] = ddx
[i
];
2100 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2101 for (i
= 0; i
< coords
; i
++)
2102 src0
[nsrc0
++] = ddy
[i
];
2104 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2107 /* NOTE a3xx (and possibly a4xx?) might be different, using isaml
2108 * with scaled x coord according to requested sample:
2110 if (opc
== OPC_ISAMM
) {
2111 if (ctx
->compiler
->txf_ms_with_isaml
) {
2112 /* the samples are laid out in x dimension as
2114 * x_ms = (x << ms) + sample_index;
2116 struct ir3_instruction
*ms
;
2117 ms
= create_immed(b
, (ctx
->samples
>> (2 * tex
->texture_index
)) & 3);
2119 src0
[0] = ir3_SHL_B(b
, src0
[0], 0, ms
, 0);
2120 src0
[0] = ir3_ADD_U(b
, src0
[0], 0, sample_index
, 0);
2124 src0
[nsrc0
++] = sample_index
;
2129 * second argument (if applicable):
2134 if (has_off
| has_lod
| has_bias
) {
2136 unsigned off_coords
= coords
;
2137 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2139 for (i
= 0; i
< off_coords
; i
++)
2140 src1
[nsrc1
++] = off
[i
];
2142 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
2143 flags
|= IR3_INSTR_O
;
2146 if (has_lod
| has_bias
)
2147 src1
[nsrc1
++] = lod
;
2150 switch (tex
->dest_type
) {
2151 case nir_type_invalid
:
2152 case nir_type_float
:
2163 unreachable("bad dest_type");
2166 if (opc
== OPC_GETLOD
)
2169 struct ir3_instruction
*samp_tex
;
2171 if (tex
->op
== nir_texop_txf_ms_fb
) {
2172 /* only expect a single txf_ms_fb per shader: */
2173 compile_assert(ctx
, !ctx
->so
->fb_read
);
2174 compile_assert(ctx
, ctx
->so
->type
== MESA_SHADER_FRAGMENT
);
2176 ctx
->so
->fb_read
= true;
2177 samp_tex
= ir3_create_collect(ctx
, (struct ir3_instruction
*[]){
2178 create_immed_typed(ctx
->block
, ctx
->so
->num_samp
, TYPE_U16
),
2179 create_immed_typed(ctx
->block
, ctx
->so
->num_samp
, TYPE_U16
),
2182 ctx
->so
->num_samp
++;
2184 samp_tex
= get_tex_samp_tex_src(ctx
, tex
);
2187 struct ir3_instruction
*col0
= ir3_create_collect(ctx
, src0
, nsrc0
);
2188 struct ir3_instruction
*col1
= ir3_create_collect(ctx
, src1
, nsrc1
);
2190 if (opc
== OPC_META_TEX_PREFETCH
) {
2191 int idx
= nir_tex_instr_src_index(tex
, nir_tex_src_coord
);
2193 compile_assert(ctx
, tex
->src
[idx
].src
.is_ssa
);
2195 sam
= ir3_META_TEX_PREFETCH(b
);
2196 __ssa_dst(sam
)->wrmask
= MASK(ncomp
); /* dst */
2197 sam
->prefetch
.input_offset
=
2198 ir3_nir_coord_offset(tex
->src
[idx
].src
.ssa
);
2199 sam
->prefetch
.tex
= tex
->texture_index
;
2200 sam
->prefetch
.samp
= tex
->sampler_index
;
2202 sam
= ir3_SAM(b
, opc
, type
, MASK(ncomp
), flags
,
2203 samp_tex
, col0
, col1
);
2206 if ((ctx
->astc_srgb
& (1 << tex
->texture_index
)) && !nir_tex_instr_is_query(tex
)) {
2207 assert(opc
!= OPC_META_TEX_PREFETCH
);
2209 /* only need first 3 components: */
2210 sam
->regs
[0]->wrmask
= 0x7;
2211 ir3_split_dest(b
, dst
, sam
, 0, 3);
2213 /* we need to sample the alpha separately with a non-ASTC
2216 sam
= ir3_SAM(b
, opc
, type
, 0b1000, flags
,
2217 samp_tex
, col0
, col1
);
2219 array_insert(ctx
->ir
, ctx
->ir
->astc_srgb
, sam
);
2221 /* fixup .w component: */
2222 ir3_split_dest(b
, &dst
[3], sam
, 3, 1);
2224 /* normal (non-workaround) case: */
2225 ir3_split_dest(b
, dst
, sam
, 0, ncomp
);
2228 /* GETLOD returns results in 4.8 fixed point */
2229 if (opc
== OPC_GETLOD
) {
2230 struct ir3_instruction
*factor
= create_immed(b
, fui(1.0 / 256));
2232 compile_assert(ctx
, tex
->dest_type
== nir_type_float
);
2233 for (i
= 0; i
< 2; i
++) {
2234 dst
[i
] = ir3_MUL_F(b
, ir3_COV(b
, dst
[i
], TYPE_S32
, TYPE_F32
), 0,
2239 ir3_put_dst(ctx
, &tex
->dest
);
2243 emit_tex_info(struct ir3_context
*ctx
, nir_tex_instr
*tex
, unsigned idx
)
2245 struct ir3_block
*b
= ctx
->block
;
2246 struct ir3_instruction
**dst
, *sam
;
2248 dst
= ir3_get_dst(ctx
, &tex
->dest
, 1);
2250 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, 1 << idx
, 0,
2251 get_tex_samp_tex_src(ctx
, tex
), NULL
, NULL
);
2253 /* even though there is only one component, since it ends
2254 * up in .y/.z/.w rather than .x, we need a split_dest()
2257 ir3_split_dest(b
, dst
, sam
, 0, idx
+ 1);
2259 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
2260 * the value in TEX_CONST_0 is zero-based.
2262 if (ctx
->compiler
->levels_add_one
)
2263 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
2265 ir3_put_dst(ctx
, &tex
->dest
);
2269 emit_tex_txs(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
2271 struct ir3_block
*b
= ctx
->block
;
2272 struct ir3_instruction
**dst
, *sam
;
2273 struct ir3_instruction
*lod
;
2274 unsigned flags
, coords
;
2276 tex_info(tex
, &flags
, &coords
);
2278 /* Actually we want the number of dimensions, not coordinates. This
2279 * distinction only matters for cubes.
2281 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2284 dst
= ir3_get_dst(ctx
, &tex
->dest
, 4);
2286 compile_assert(ctx
, tex
->num_srcs
== 1);
2287 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
2289 lod
= ir3_get_src(ctx
, &tex
->src
[0].src
)[0];
2291 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, 0b1111, flags
,
2292 get_tex_samp_tex_src(ctx
, tex
), lod
, NULL
);
2294 ir3_split_dest(b
, dst
, sam
, 0, 4);
2296 /* Array size actually ends up in .w rather than .z. This doesn't
2297 * matter for miplevel 0, but for higher mips the value in z is
2298 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
2299 * returned, which means that we have to add 1 to it for arrays.
2301 if (tex
->is_array
) {
2302 if (ctx
->compiler
->levels_add_one
) {
2303 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
2305 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
2309 ir3_put_dst(ctx
, &tex
->dest
);
2313 emit_jump(struct ir3_context
*ctx
, nir_jump_instr
*jump
)
2315 switch (jump
->type
) {
2316 case nir_jump_break
:
2317 case nir_jump_continue
:
2318 case nir_jump_return
:
2319 /* I *think* we can simply just ignore this, and use the
2320 * successor block link to figure out where we need to
2321 * jump to for break/continue
2325 ir3_context_error(ctx
, "Unhandled NIR jump type: %d\n", jump
->type
);
2331 emit_instr(struct ir3_context
*ctx
, nir_instr
*instr
)
2333 switch (instr
->type
) {
2334 case nir_instr_type_alu
:
2335 emit_alu(ctx
, nir_instr_as_alu(instr
));
2337 case nir_instr_type_deref
:
2338 /* ignored, handled as part of the intrinsic they are src to */
2340 case nir_instr_type_intrinsic
:
2341 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2343 case nir_instr_type_load_const
:
2344 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2346 case nir_instr_type_ssa_undef
:
2347 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
2349 case nir_instr_type_tex
: {
2350 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
2351 /* couple tex instructions get special-cased:
2355 emit_tex_txs(ctx
, tex
);
2357 case nir_texop_query_levels
:
2358 emit_tex_info(ctx
, tex
, 2);
2360 case nir_texop_texture_samples
:
2361 emit_tex_info(ctx
, tex
, 3);
2369 case nir_instr_type_jump
:
2370 emit_jump(ctx
, nir_instr_as_jump(instr
));
2372 case nir_instr_type_phi
:
2373 /* we have converted phi webs to regs in NIR by now */
2374 ir3_context_error(ctx
, "Unexpected NIR instruction type: %d\n", instr
->type
);
2376 case nir_instr_type_call
:
2377 case nir_instr_type_parallel_copy
:
2378 ir3_context_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
2383 static struct ir3_block
*
2384 get_block(struct ir3_context
*ctx
, const nir_block
*nblock
)
2386 struct ir3_block
*block
;
2387 struct hash_entry
*hentry
;
2389 hentry
= _mesa_hash_table_search(ctx
->block_ht
, nblock
);
2391 return hentry
->data
;
2393 block
= ir3_block_create(ctx
->ir
);
2394 block
->nblock
= nblock
;
2395 _mesa_hash_table_insert(ctx
->block_ht
, nblock
, block
);
2397 block
->predecessors
= _mesa_pointer_set_create(block
);
2398 set_foreach(nblock
->predecessors
, sentry
) {
2399 _mesa_set_add(block
->predecessors
, get_block(ctx
, sentry
->key
));
2406 emit_block(struct ir3_context
*ctx
, nir_block
*nblock
)
2408 struct ir3_block
*block
= get_block(ctx
, nblock
);
2410 for (int i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
2411 if (nblock
->successors
[i
]) {
2412 block
->successors
[i
] =
2413 get_block(ctx
, nblock
->successors
[i
]);
2418 list_addtail(&block
->node
, &ctx
->ir
->block_list
);
2420 /* re-emit addr register in each block if needed: */
2421 for (int i
= 0; i
< ARRAY_SIZE(ctx
->addr_ht
); i
++) {
2422 _mesa_hash_table_destroy(ctx
->addr_ht
[i
], NULL
);
2423 ctx
->addr_ht
[i
] = NULL
;
2426 nir_foreach_instr(instr
, nblock
) {
2427 ctx
->cur_instr
= instr
;
2428 emit_instr(ctx
, instr
);
2429 ctx
->cur_instr
= NULL
;
2435 static void emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
);
2438 emit_if(struct ir3_context
*ctx
, nir_if
*nif
)
2440 struct ir3_instruction
*condition
= ir3_get_src(ctx
, &nif
->condition
)[0];
2442 ctx
->block
->condition
=
2443 ir3_get_predicate(ctx
, ir3_b2n(condition
->block
, condition
));
2445 emit_cf_list(ctx
, &nif
->then_list
);
2446 emit_cf_list(ctx
, &nif
->else_list
);
2450 emit_loop(struct ir3_context
*ctx
, nir_loop
*nloop
)
2452 emit_cf_list(ctx
, &nloop
->body
);
2457 stack_push(struct ir3_context
*ctx
)
2460 ctx
->max_stack
= MAX2(ctx
->max_stack
, ctx
->stack
);
2464 stack_pop(struct ir3_context
*ctx
)
2466 compile_assert(ctx
, ctx
->stack
> 0);
2471 emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
)
2473 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2474 switch (node
->type
) {
2475 case nir_cf_node_block
:
2476 emit_block(ctx
, nir_cf_node_as_block(node
));
2478 case nir_cf_node_if
:
2480 emit_if(ctx
, nir_cf_node_as_if(node
));
2483 case nir_cf_node_loop
:
2485 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2488 case nir_cf_node_function
:
2489 ir3_context_error(ctx
, "TODO\n");
2495 /* emit stream-out code. At this point, the current block is the original
2496 * (nir) end block, and nir ensures that all flow control paths terminate
2497 * into the end block. We re-purpose the original end block to generate
2498 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
2499 * block holding stream-out write instructions, followed by the new end
2503 * p0.x = (vtxcnt < maxvtxcnt)
2504 * // succs: blockStreamOut, blockNewEnd
2507 * ... stream-out instructions ...
2508 * // succs: blockNewEnd
2514 emit_stream_out(struct ir3_context
*ctx
)
2516 struct ir3
*ir
= ctx
->ir
;
2517 struct ir3_stream_output_info
*strmout
=
2518 &ctx
->so
->shader
->stream_output
;
2519 struct ir3_block
*orig_end_block
, *stream_out_block
, *new_end_block
;
2520 struct ir3_instruction
*vtxcnt
, *maxvtxcnt
, *cond
;
2521 struct ir3_instruction
*bases
[IR3_MAX_SO_BUFFERS
];
2523 /* create vtxcnt input in input block at top of shader,
2524 * so that it is seen as live over the entire duration
2527 vtxcnt
= create_sysval_input(ctx
, SYSTEM_VALUE_VERTEX_CNT
, 0x1);
2528 maxvtxcnt
= create_driver_param(ctx
, IR3_DP_VTXCNT_MAX
);
2530 /* at this point, we are at the original 'end' block,
2531 * re-purpose this block to stream-out condition, then
2532 * append stream-out block and new-end block
2534 orig_end_block
= ctx
->block
;
2536 // TODO these blocks need to update predecessors..
2537 // maybe w/ store_global intrinsic, we could do this
2538 // stuff in nir->nir pass
2540 stream_out_block
= ir3_block_create(ir
);
2541 list_addtail(&stream_out_block
->node
, &ir
->block_list
);
2543 new_end_block
= ir3_block_create(ir
);
2544 list_addtail(&new_end_block
->node
, &ir
->block_list
);
2546 orig_end_block
->successors
[0] = stream_out_block
;
2547 orig_end_block
->successors
[1] = new_end_block
;
2548 stream_out_block
->successors
[0] = new_end_block
;
2550 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
2551 cond
= ir3_CMPS_S(ctx
->block
, vtxcnt
, 0, maxvtxcnt
, 0);
2552 cond
->regs
[0]->num
= regid(REG_P0
, 0);
2553 cond
->regs
[0]->flags
&= ~IR3_REG_SSA
;
2554 cond
->cat2
.condition
= IR3_COND_LT
;
2556 /* condition goes on previous block to the conditional,
2557 * since it is used to pick which of the two successor
2560 orig_end_block
->condition
= cond
;
2562 /* switch to stream_out_block to generate the stream-out
2565 ctx
->block
= stream_out_block
;
2567 /* Calculate base addresses based on vtxcnt. Instructions
2568 * generated for bases not used in following loop will be
2569 * stripped out in the backend.
2571 for (unsigned i
= 0; i
< IR3_MAX_SO_BUFFERS
; i
++) {
2572 struct ir3_const_state
*const_state
= &ctx
->so
->shader
->const_state
;
2573 unsigned stride
= strmout
->stride
[i
];
2574 struct ir3_instruction
*base
, *off
;
2576 base
= create_uniform(ctx
->block
, regid(const_state
->offsets
.tfbo
, i
));
2578 /* 24-bit should be enough: */
2579 off
= ir3_MUL_U24(ctx
->block
, vtxcnt
, 0,
2580 create_immed(ctx
->block
, stride
* 4), 0);
2582 bases
[i
] = ir3_ADD_S(ctx
->block
, off
, 0, base
, 0);
2585 /* Generate the per-output store instructions: */
2586 for (unsigned i
= 0; i
< strmout
->num_outputs
; i
++) {
2587 for (unsigned j
= 0; j
< strmout
->output
[i
].num_components
; j
++) {
2588 unsigned c
= j
+ strmout
->output
[i
].start_component
;
2589 struct ir3_instruction
*base
, *out
, *stg
;
2591 base
= bases
[strmout
->output
[i
].output_buffer
];
2592 out
= ctx
->ir
->outputs
[regid(strmout
->output
[i
].register_index
, c
)];
2594 stg
= ir3_STG(ctx
->block
, base
, 0, out
, 0,
2595 create_immed(ctx
->block
, 1), 0);
2596 stg
->cat6
.type
= TYPE_U32
;
2597 stg
->cat6
.dst_offset
= (strmout
->output
[i
].dst_offset
+ j
) * 4;
2599 array_insert(ctx
->block
, ctx
->block
->keeps
, stg
);
2603 /* and finally switch to the new_end_block: */
2604 ctx
->block
= new_end_block
;
2608 emit_function(struct ir3_context
*ctx
, nir_function_impl
*impl
)
2610 nir_metadata_require(impl
, nir_metadata_block_index
);
2612 compile_assert(ctx
, ctx
->stack
== 0);
2614 emit_cf_list(ctx
, &impl
->body
);
2615 emit_block(ctx
, impl
->end_block
);
2617 compile_assert(ctx
, ctx
->stack
== 0);
2619 /* at this point, we should have a single empty block,
2620 * into which we emit the 'end' instruction.
2622 compile_assert(ctx
, list_is_empty(&ctx
->block
->instr_list
));
2624 /* If stream-out (aka transform-feedback) enabled, emit the
2625 * stream-out instructions, followed by a new empty block (into
2626 * which the 'end' instruction lands).
2628 * NOTE: it is done in this order, rather than inserting before
2629 * we emit end_block, because NIR guarantees that all blocks
2630 * flow into end_block, and that end_block has no successors.
2631 * So by re-purposing end_block as the first block of stream-
2632 * out, we guarantee that all exit paths flow into the stream-
2635 if ((ctx
->compiler
->gpu_id
< 500) &&
2636 (ctx
->so
->shader
->stream_output
.num_outputs
> 0) &&
2637 !ctx
->so
->binning_pass
) {
2638 debug_assert(ctx
->so
->type
== MESA_SHADER_VERTEX
);
2639 emit_stream_out(ctx
);
2642 /* Vertex shaders in a tessellation or geometry pipeline treat END as a
2643 * NOP and has an epilogue that writes the VS outputs to local storage, to
2644 * be read by the HS. Then it resets execution mask (chmask) and chains
2645 * to the next shader (chsh).
2647 if ((ctx
->so
->type
== MESA_SHADER_VERTEX
&&
2648 (ctx
->so
->key
.has_gs
|| ctx
->so
->key
.tessellation
)) ||
2649 (ctx
->so
->type
== MESA_SHADER_TESS_EVAL
&& ctx
->so
->key
.has_gs
)) {
2650 struct ir3_instruction
*chmask
=
2651 ir3_CHMASK(ctx
->block
);
2652 chmask
->barrier_class
= IR3_BARRIER_EVERYTHING
;
2653 chmask
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
2655 struct ir3_instruction
*chsh
=
2656 ir3_CHSH(ctx
->block
);
2657 chsh
->barrier_class
= IR3_BARRIER_EVERYTHING
;
2658 chsh
->barrier_conflict
= IR3_BARRIER_EVERYTHING
;
2660 ir3_END(ctx
->block
);
2665 setup_input(struct ir3_context
*ctx
, nir_variable
*in
)
2667 struct ir3_shader_variant
*so
= ctx
->so
;
2668 unsigned ncomp
= glsl_get_components(in
->type
);
2669 unsigned n
= in
->data
.driver_location
;
2670 unsigned frac
= in
->data
.location_frac
;
2671 unsigned slot
= in
->data
.location
;
2673 /* Inputs are loaded using ldlw or ldg for these stages. */
2674 if (ctx
->so
->type
== MESA_SHADER_TESS_CTRL
||
2675 ctx
->so
->type
== MESA_SHADER_TESS_EVAL
||
2676 ctx
->so
->type
== MESA_SHADER_GEOMETRY
)
2679 /* skip unread inputs, we could end up with (for example), unsplit
2680 * matrix/etc inputs in the case they are not read, so just silently
2686 so
->inputs
[n
].slot
= slot
;
2687 so
->inputs
[n
].compmask
= (1 << (ncomp
+ frac
)) - 1;
2688 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
2689 so
->inputs
[n
].interpolate
= in
->data
.interpolation
;
2691 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
2693 /* if any varyings have 'sample' qualifer, that triggers us
2694 * to run in per-sample mode:
2696 so
->per_samp
|= in
->data
.sample
;
2698 for (int i
= 0; i
< ncomp
; i
++) {
2699 struct ir3_instruction
*instr
= NULL
;
2700 unsigned idx
= (n
* 4) + i
+ frac
;
2702 if (slot
== VARYING_SLOT_POS
) {
2703 ir3_context_error(ctx
, "fragcoord should be a sysval!\n");
2704 } else if (slot
== VARYING_SLOT_PNTC
) {
2705 /* see for example st_nir_fixup_varying_slots().. this is
2706 * maybe a bit mesa/st specific. But we need things to line
2707 * up for this in fdN_program:
2708 * unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
2709 * if (emit->sprite_coord_enable & texmask) {
2713 so
->inputs
[n
].slot
= VARYING_SLOT_VAR8
;
2714 so
->inputs
[n
].bary
= true;
2715 instr
= create_frag_input(ctx
, false, idx
);
2717 /* detect the special case for front/back colors where
2718 * we need to do flat vs smooth shading depending on
2721 if (in
->data
.interpolation
== INTERP_MODE_NONE
) {
2723 case VARYING_SLOT_COL0
:
2724 case VARYING_SLOT_COL1
:
2725 case VARYING_SLOT_BFC0
:
2726 case VARYING_SLOT_BFC1
:
2727 so
->inputs
[n
].rasterflat
= true;
2734 if (ctx
->compiler
->flat_bypass
) {
2735 if ((so
->inputs
[n
].interpolate
== INTERP_MODE_FLAT
) ||
2736 (so
->inputs
[n
].rasterflat
&& ctx
->so
->key
.rasterflat
))
2737 so
->inputs
[n
].use_ldlv
= true;
2740 so
->inputs
[n
].bary
= true;
2742 instr
= create_frag_input(ctx
, so
->inputs
[n
].use_ldlv
, idx
);
2745 compile_assert(ctx
, idx
< ctx
->ninputs
);
2747 ctx
->inputs
[idx
] = instr
;
2749 } else if (ctx
->so
->type
== MESA_SHADER_VERTEX
) {
2750 /* We shouldn't have fractional input for VS input.. that only shows
2751 * up with varying packing
2755 struct ir3_instruction
*input
= create_input(ctx
, (1 << ncomp
) - 1);
2756 struct ir3_instruction
*components
[ncomp
];
2758 input
->input
.inidx
= n
;
2760 ir3_split_dest(ctx
->block
, components
, input
, 0, ncomp
);
2762 for (int i
= 0; i
< ncomp
; i
++) {
2763 unsigned idx
= (n
* 4) + i
+ frac
;
2764 compile_assert(ctx
, idx
< ctx
->ninputs
);
2765 ctx
->inputs
[idx
] = components
[i
];
2768 ir3_context_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2771 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== MESA_SHADER_VERTEX
)) {
2772 so
->total_in
+= ncomp
;
2776 /* Initially we assign non-packed inloc's for varyings, as we don't really
2777 * know up-front which components will be unused. After all the compilation
2778 * stages we scan the shader to see which components are actually used, and
2779 * re-pack the inlocs to eliminate unneeded varyings.
2782 pack_inlocs(struct ir3_context
*ctx
)
2784 struct ir3_shader_variant
*so
= ctx
->so
;
2785 uint8_t used_components
[so
->inputs_count
];
2787 memset(used_components
, 0, sizeof(used_components
));
2790 * First Step: scan shader to find which bary.f/ldlv remain:
2793 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
2794 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
2795 if (is_input(instr
)) {
2796 unsigned inloc
= instr
->regs
[1]->iim_val
;
2797 unsigned i
= inloc
/ 4;
2798 unsigned j
= inloc
% 4;
2800 compile_assert(ctx
, instr
->regs
[1]->flags
& IR3_REG_IMMED
);
2801 compile_assert(ctx
, i
< so
->inputs_count
);
2803 used_components
[i
] |= 1 << j
;
2804 } else if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
2805 for (int n
= 0; n
< 2; n
++) {
2806 unsigned inloc
= instr
->prefetch
.input_offset
+ n
;
2807 unsigned i
= inloc
/ 4;
2808 unsigned j
= inloc
% 4;
2810 compile_assert(ctx
, i
< so
->inputs_count
);
2812 used_components
[i
] |= 1 << j
;
2819 * Second Step: reassign varying inloc/slots:
2822 unsigned actual_in
= 0;
2825 for (unsigned i
= 0; i
< so
->inputs_count
; i
++) {
2826 unsigned compmask
= 0, maxcomp
= 0;
2828 so
->inputs
[i
].inloc
= inloc
;
2829 so
->inputs
[i
].bary
= false;
2831 for (unsigned j
= 0; j
< 4; j
++) {
2832 if (!(used_components
[i
] & (1 << j
)))
2835 compmask
|= (1 << j
);
2839 /* at this point, since used_components[i] mask is only
2840 * considering varyings (ie. not sysvals) we know this
2843 so
->inputs
[i
].bary
= true;
2846 if (so
->inputs
[i
].bary
) {
2848 so
->inputs
[i
].compmask
= (1 << maxcomp
) - 1;
2854 * Third Step: reassign packed inloc's:
2857 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
2858 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
2859 if (is_input(instr
)) {
2860 unsigned inloc
= instr
->regs
[1]->iim_val
;
2861 unsigned i
= inloc
/ 4;
2862 unsigned j
= inloc
% 4;
2864 instr
->regs
[1]->iim_val
= so
->inputs
[i
].inloc
+ j
;
2871 setup_output(struct ir3_context
*ctx
, nir_variable
*out
)
2873 struct ir3_shader_variant
*so
= ctx
->so
;
2874 unsigned ncomp
= glsl_get_components(out
->type
);
2875 unsigned n
= out
->data
.driver_location
;
2876 unsigned frac
= out
->data
.location_frac
;
2877 unsigned slot
= out
->data
.location
;
2880 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
2882 case FRAG_RESULT_DEPTH
:
2883 comp
= 2; /* tgsi will write to .z component */
2884 so
->writes_pos
= true;
2886 case FRAG_RESULT_COLOR
:
2889 case FRAG_RESULT_SAMPLE_MASK
:
2890 so
->writes_smask
= true;
2893 if (slot
>= FRAG_RESULT_DATA0
)
2895 ir3_context_error(ctx
, "unknown FS output name: %s\n",
2896 gl_frag_result_name(slot
));
2898 } else if (ctx
->so
->type
== MESA_SHADER_VERTEX
||
2899 ctx
->so
->type
== MESA_SHADER_TESS_EVAL
||
2900 ctx
->so
->type
== MESA_SHADER_GEOMETRY
) {
2902 case VARYING_SLOT_POS
:
2903 so
->writes_pos
= true;
2905 case VARYING_SLOT_PSIZ
:
2906 so
->writes_psize
= true;
2908 case VARYING_SLOT_PRIMITIVE_ID
:
2909 case VARYING_SLOT_LAYER
:
2910 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3
:
2911 debug_assert(ctx
->so
->type
== MESA_SHADER_GEOMETRY
);
2913 case VARYING_SLOT_COL0
:
2914 case VARYING_SLOT_COL1
:
2915 case VARYING_SLOT_BFC0
:
2916 case VARYING_SLOT_BFC1
:
2917 case VARYING_SLOT_FOGC
:
2918 case VARYING_SLOT_CLIP_DIST0
:
2919 case VARYING_SLOT_CLIP_DIST1
:
2920 case VARYING_SLOT_CLIP_VERTEX
:
2923 if (slot
>= VARYING_SLOT_VAR0
)
2925 if ((VARYING_SLOT_TEX0
<= slot
) && (slot
<= VARYING_SLOT_TEX7
))
2927 ir3_context_error(ctx
, "unknown %s shader output name: %s\n",
2928 _mesa_shader_stage_to_string(ctx
->so
->type
),
2929 gl_varying_slot_name(slot
));
2931 } else if (ctx
->so
->type
== MESA_SHADER_TESS_CTRL
) {
2932 /* output lowered to buffer writes. */
2935 ir3_context_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2938 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
2940 so
->outputs
[n
].slot
= slot
;
2941 so
->outputs
[n
].regid
= regid(n
, comp
);
2942 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
2944 for (int i
= 0; i
< ncomp
; i
++) {
2945 unsigned idx
= (n
* 4) + i
+ frac
;
2946 compile_assert(ctx
, idx
< ctx
->noutputs
);
2947 ctx
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2950 /* if varying packing doesn't happen, we could end up in a situation
2951 * with "holes" in the output, and since the per-generation code that
2952 * sets up varying linkage registers doesn't expect to have more than
2953 * one varying per vec4 slot, pad the holes.
2955 * Note that this should probably generate a performance warning of
2958 for (int i
= 0; i
< frac
; i
++) {
2959 unsigned idx
= (n
* 4) + i
;
2960 if (!ctx
->outputs
[idx
]) {
2961 ctx
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2967 max_drvloc(struct exec_list
*vars
)
2970 nir_foreach_variable(var
, vars
) {
2971 drvloc
= MAX2(drvloc
, (int)var
->data
.driver_location
);
2977 emit_instructions(struct ir3_context
*ctx
)
2979 nir_function_impl
*fxn
= nir_shader_get_entrypoint(ctx
->s
);
2981 ctx
->ninputs
= (max_drvloc(&ctx
->s
->inputs
) + 1) * 4;
2982 ctx
->noutputs
= (max_drvloc(&ctx
->s
->outputs
) + 1) * 4;
2984 ctx
->inputs
= rzalloc_array(ctx
, struct ir3_instruction
*, ctx
->ninputs
);
2985 ctx
->outputs
= rzalloc_array(ctx
, struct ir3_instruction
*, ctx
->noutputs
);
2987 ctx
->ir
= ir3_create(ctx
->compiler
, ctx
->so
->type
);
2989 /* Create inputs in first block: */
2990 ctx
->block
= get_block(ctx
, nir_start_block(fxn
));
2991 ctx
->in_block
= ctx
->block
;
2992 list_addtail(&ctx
->block
->node
, &ctx
->ir
->block_list
);
2994 /* for fragment shader, the vcoord input register is used as the
2995 * base for bary.f varying fetch instrs:
2997 * TODO defer creating ctx->ij_pixel and corresponding sysvals
2998 * until emit_intrinsic when we know they are actually needed.
2999 * For now, we defer creating ctx->ij_centroid, etc, since we
3000 * only need ij_pixel for "old style" varying inputs (ie.
3003 struct ir3_instruction
*vcoord
= NULL
;
3004 if (ctx
->so
->type
== MESA_SHADER_FRAGMENT
) {
3005 struct ir3_instruction
*xy
[2];
3007 vcoord
= create_input(ctx
, 0x3);
3008 ir3_split_dest(ctx
->block
, xy
, vcoord
, 0, 2);
3010 ctx
->ij_pixel
= ir3_create_collect(ctx
, xy
, 2);
3014 nir_foreach_variable(var
, &ctx
->s
->inputs
) {
3015 setup_input(ctx
, var
);
3018 /* Defer add_sysval_input() stuff until after setup_inputs(),
3019 * because sysvals need to be appended after varyings:
3022 add_sysval_input_compmask(ctx
, SYSTEM_VALUE_BARYCENTRIC_PIXEL
,
3027 /* Tesselation shaders always need primitive ID for indexing the
3028 * BO. Geometry shaders don't always need it but when they do it has be
3029 * delivered and unclobbered in the VS. To make things easy, we always
3030 * make room for it in VS/DS.
3032 bool has_tess
= ctx
->so
->key
.tessellation
!= IR3_TESS_NONE
;
3033 bool has_gs
= ctx
->so
->key
.has_gs
;
3034 switch (ctx
->so
->type
) {
3035 case MESA_SHADER_VERTEX
:
3037 ctx
->tcs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_TCS_HEADER_IR3
, 0x1);
3038 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3039 } else if (has_gs
) {
3040 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3041 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3044 case MESA_SHADER_TESS_CTRL
:
3045 ctx
->tcs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_TCS_HEADER_IR3
, 0x1);
3046 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3048 case MESA_SHADER_TESS_EVAL
:
3050 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3051 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3053 case MESA_SHADER_GEOMETRY
:
3054 ctx
->gs_header
= create_sysval_input(ctx
, SYSTEM_VALUE_GS_HEADER_IR3
, 0x1);
3055 ctx
->primitive_id
= create_sysval_input(ctx
, SYSTEM_VALUE_PRIMITIVE_ID
, 0x1);
3061 /* Setup outputs: */
3062 nir_foreach_variable(var
, &ctx
->s
->outputs
) {
3063 setup_output(ctx
, var
);
3066 /* Find # of samplers: */
3067 nir_foreach_variable(var
, &ctx
->s
->uniforms
) {
3068 ctx
->so
->num_samp
+= glsl_type_get_sampler_count(var
->type
);
3069 /* just assume that we'll be reading from images.. if it
3070 * is write-only we don't have to count it, but not sure
3071 * if there is a good way to know?
3073 ctx
->so
->num_samp
+= glsl_type_get_image_count(var
->type
);
3076 /* NOTE: need to do something more clever when we support >1 fxn */
3077 nir_foreach_register(reg
, &fxn
->registers
) {
3078 ir3_declare_array(ctx
, reg
);
3080 /* And emit the body: */
3082 emit_function(ctx
, fxn
);
3085 /* Fixup tex sampler state for astc/srgb workaround instructions. We
3086 * need to assign the tex state indexes for these after we know the
3090 fixup_astc_srgb(struct ir3_context
*ctx
)
3092 struct ir3_shader_variant
*so
= ctx
->so
;
3093 /* indexed by original tex idx, value is newly assigned alpha sampler
3094 * state tex idx. Zero is invalid since there is at least one sampler
3097 unsigned alt_tex_state
[16] = {0};
3098 unsigned tex_idx
= ctx
->max_texture_index
+ 1;
3101 so
->astc_srgb
.base
= tex_idx
;
3103 for (unsigned i
= 0; i
< ctx
->ir
->astc_srgb_count
; i
++) {
3104 struct ir3_instruction
*sam
= ctx
->ir
->astc_srgb
[i
];
3106 compile_assert(ctx
, sam
->cat5
.tex
< ARRAY_SIZE(alt_tex_state
));
3108 if (alt_tex_state
[sam
->cat5
.tex
] == 0) {
3109 /* assign new alternate/alpha tex state slot: */
3110 alt_tex_state
[sam
->cat5
.tex
] = tex_idx
++;
3111 so
->astc_srgb
.orig_idx
[idx
++] = sam
->cat5
.tex
;
3112 so
->astc_srgb
.count
++;
3115 sam
->cat5
.tex
= alt_tex_state
[sam
->cat5
.tex
];
3120 fixup_binning_pass(struct ir3_context
*ctx
)
3122 struct ir3_shader_variant
*so
= ctx
->so
;
3123 struct ir3
*ir
= ctx
->ir
;
3126 /* first pass, remove unused outputs from the IR level outputs: */
3127 for (i
= 0, j
= 0; i
< ir
->outputs_count
; i
++) {
3128 struct ir3_instruction
*out
= ir
->outputs
[i
];
3129 assert(out
->opc
== OPC_META_COLLECT
);
3130 unsigned outidx
= out
->collect
.outidx
;
3131 unsigned slot
= so
->outputs
[outidx
].slot
;
3133 /* throw away everything but first position/psize */
3134 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
3135 ir
->outputs
[j
] = ir
->outputs
[i
];
3139 ir
->outputs_count
= j
;
3141 /* second pass, cleanup the unused slots in ir3_shader_variant::outputs
3144 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
3145 unsigned slot
= so
->outputs
[i
].slot
;
3147 /* throw away everything but first position/psize */
3148 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
3149 so
->outputs
[j
] = so
->outputs
[i
];
3151 /* fixup outidx to point to new output table entry: */
3152 struct ir3_instruction
*out
;
3153 foreach_output(out
, ir
) {
3154 if (out
->collect
.outidx
== i
) {
3155 out
->collect
.outidx
= j
;
3163 so
->outputs_count
= j
;
3167 collect_tex_prefetches(struct ir3_context
*ctx
, struct ir3
*ir
)
3171 /* Collect sampling instructions eligible for pre-dispatch. */
3172 list_for_each_entry(struct ir3_block
, block
, &ir
->block_list
, node
) {
3173 list_for_each_entry_safe(struct ir3_instruction
, instr
,
3174 &block
->instr_list
, node
) {
3175 if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
3176 assert(idx
< ARRAY_SIZE(ctx
->so
->sampler_prefetch
));
3177 struct ir3_sampler_prefetch
*fetch
=
3178 &ctx
->so
->sampler_prefetch
[idx
];
3181 fetch
->cmd
= IR3_SAMPLER_PREFETCH_CMD
;
3182 fetch
->wrmask
= instr
->regs
[0]->wrmask
;
3183 fetch
->tex_id
= instr
->prefetch
.tex
;
3184 fetch
->samp_id
= instr
->prefetch
.samp
;
3185 fetch
->dst
= instr
->regs
[0]->num
;
3186 fetch
->src
= instr
->prefetch
.input_offset
;
3189 MAX2(ctx
->so
->total_in
, instr
->prefetch
.input_offset
+ 2);
3191 /* Disable half precision until supported. */
3192 fetch
->half_precision
= 0x0;
3194 /* Remove the prefetch placeholder instruction: */
3195 list_delinit(&instr
->node
);
3202 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
3203 struct ir3_shader_variant
*so
)
3205 struct ir3_context
*ctx
;
3207 int ret
= 0, max_bary
;
3211 ctx
= ir3_context_init(compiler
, so
);
3213 DBG("INIT failed!");
3218 emit_instructions(ctx
);
3221 DBG("EMIT failed!");
3226 ir
= so
->ir
= ctx
->ir
;
3228 assert((ctx
->noutputs
% 4) == 0);
3230 /* Setup IR level outputs, which are "collects" that gather
3231 * the scalar components of outputs.
3233 for (unsigned i
= 0; i
< ctx
->noutputs
; i
+= 4) {
3235 /* figure out the # of components written:
3237 * TODO do we need to handle holes, ie. if .x and .z
3238 * components written, but .y component not written?
3240 for (unsigned j
= 0; j
< 4; j
++) {
3241 if (!ctx
->outputs
[i
+ j
])
3246 /* Note that in some stages, like TCS, store_output is
3247 * lowered to memory writes, so no components of the
3248 * are "written" from the PoV of traditional store-
3249 * output instructions:
3254 struct ir3_instruction
*out
=
3255 ir3_create_collect(ctx
, &ctx
->outputs
[i
], ncomp
);
3258 assert(outidx
< so
->outputs_count
);
3260 /* stash index into so->outputs[] so we can map the
3261 * output back to slot/etc later:
3263 out
->collect
.outidx
= outidx
;
3265 array_insert(ir
, ir
->outputs
, out
);
3268 /* Set up the gs header as an output for the vertex shader so it won't
3269 * clobber it for the tess ctrl shader.
3271 * TODO this could probably be done more cleanly in a nir pass.
3273 if (ctx
->so
->type
== MESA_SHADER_VERTEX
||
3274 (ctx
->so
->key
.has_gs
&& ctx
->so
->type
== MESA_SHADER_TESS_EVAL
)) {
3275 if (ctx
->primitive_id
) {
3276 unsigned n
= so
->outputs_count
++;
3277 so
->outputs
[n
].slot
= VARYING_SLOT_PRIMITIVE_ID
;
3279 struct ir3_instruction
*out
=
3280 ir3_create_collect(ctx
, &ctx
->primitive_id
, 1);
3281 out
->collect
.outidx
= n
;
3282 array_insert(ir
, ir
->outputs
, out
);
3285 if (ctx
->gs_header
) {
3286 unsigned n
= so
->outputs_count
++;
3287 so
->outputs
[n
].slot
= VARYING_SLOT_GS_HEADER_IR3
;
3288 struct ir3_instruction
*out
=
3289 ir3_create_collect(ctx
, &ctx
->gs_header
, 1);
3290 out
->collect
.outidx
= n
;
3291 array_insert(ir
, ir
->outputs
, out
);
3294 if (ctx
->tcs_header
) {
3295 unsigned n
= so
->outputs_count
++;
3296 so
->outputs
[n
].slot
= VARYING_SLOT_TCS_HEADER_IR3
;
3297 struct ir3_instruction
*out
=
3298 ir3_create_collect(ctx
, &ctx
->tcs_header
, 1);
3299 out
->collect
.outidx
= n
;
3300 array_insert(ir
, ir
->outputs
, out
);
3304 /* at this point, for binning pass, throw away unneeded outputs: */
3305 if (so
->binning_pass
&& (ctx
->compiler
->gpu_id
< 600))
3306 fixup_binning_pass(ctx
);
3308 ir3_debug_print(ir
, "BEFORE CP");
3312 /* at this point, for binning pass, throw away unneeded outputs:
3313 * Note that for a6xx and later, we do this after ir3_cp to ensure
3314 * that the uniform/constant layout for BS and VS matches, so that
3315 * we can re-use same VS_CONST state group.
3317 if (so
->binning_pass
&& (ctx
->compiler
->gpu_id
>= 600))
3318 fixup_binning_pass(ctx
);
3320 /* for a6xx+, binning and draw pass VS use same VBO state, so we
3321 * need to make sure not to remove any inputs that are used by
3322 * the nonbinning VS.
3324 if (ctx
->compiler
->gpu_id
>= 600 && so
->binning_pass
&&
3325 so
->type
== MESA_SHADER_VERTEX
) {
3326 for (int i
= 0; i
< ctx
->ninputs
; i
++) {
3327 struct ir3_instruction
*in
= ctx
->inputs
[i
];
3335 debug_assert(n
< so
->nonbinning
->inputs_count
);
3337 if (so
->nonbinning
->inputs
[n
].sysval
)
3340 /* be sure to keep inputs, even if only used in VS */
3341 if (so
->nonbinning
->inputs
[n
].compmask
& (1 << c
))
3342 array_insert(in
->block
, in
->block
->keeps
, in
);
3346 ir3_debug_print(ir
, "BEFORE GROUPING");
3348 ir3_sched_add_deps(ir
);
3350 /* Group left/right neighbors, inserting mov's where needed to
3355 ir3_debug_print(ir
, "AFTER GROUPING");
3359 ir3_debug_print(ir
, "AFTER DEPTH");
3361 /* do Sethi–Ullman numbering before scheduling: */
3364 ret
= ir3_sched(ir
);
3366 DBG("SCHED failed!");
3370 if (compiler
->gpu_id
>= 600) {
3371 ir3_a6xx_fixup_atomic_dests(ir
, so
);
3374 ir3_debug_print(ir
, "AFTER SCHED");
3376 /* Pre-assign VS inputs on a6xx+ binning pass shader, to align
3377 * with draw pass VS, so binning and draw pass can both use the
3380 * Note that VS inputs are expected to be full precision.
3382 bool pre_assign_inputs
= (ir
->compiler
->gpu_id
>= 600) &&
3383 (ir
->type
== MESA_SHADER_VERTEX
) &&
3386 if (pre_assign_inputs
) {
3387 for (unsigned i
= 0; i
< ctx
->ninputs
; i
++) {
3388 struct ir3_instruction
*instr
= ctx
->inputs
[i
];
3395 unsigned regid
= so
->nonbinning
->inputs
[n
].regid
+ c
;
3397 instr
->regs
[0]->num
= regid
;
3400 ret
= ir3_ra(so
, ctx
->inputs
, ctx
->ninputs
);
3401 } else if (ctx
->tcs_header
) {
3402 /* We need to have these values in the same registers between VS and TCS
3403 * since the VS chains to TCS and doesn't get the sysvals redelivered.
3406 ctx
->tcs_header
->regs
[0]->num
= regid(0, 0);
3407 ctx
->primitive_id
->regs
[0]->num
= regid(0, 1);
3408 struct ir3_instruction
*precolor
[] = { ctx
->tcs_header
, ctx
->primitive_id
};
3409 ret
= ir3_ra(so
, precolor
, ARRAY_SIZE(precolor
));
3410 } else if (ctx
->gs_header
) {
3411 /* We need to have these values in the same registers between producer
3412 * (VS or DS) and GS since the producer chains to GS and doesn't get
3413 * the sysvals redelivered.
3416 ctx
->gs_header
->regs
[0]->num
= regid(0, 0);
3417 ctx
->primitive_id
->regs
[0]->num
= regid(0, 1);
3418 struct ir3_instruction
*precolor
[] = { ctx
->gs_header
, ctx
->primitive_id
};
3419 ret
= ir3_ra(so
, precolor
, ARRAY_SIZE(precolor
));
3420 } else if (so
->num_sampler_prefetch
) {
3421 assert(so
->type
== MESA_SHADER_FRAGMENT
);
3422 struct ir3_instruction
*instr
, *precolor
[2];
3425 foreach_input(instr
, ir
) {
3426 if (instr
->input
.sysval
!= SYSTEM_VALUE_BARYCENTRIC_PIXEL
)
3429 assert(idx
< ARRAY_SIZE(precolor
));
3431 precolor
[idx
] = instr
;
3432 instr
->regs
[0]->num
= idx
;
3436 ret
= ir3_ra(so
, precolor
, idx
);
3438 ret
= ir3_ra(so
, NULL
, 0);
3446 ir3_debug_print(ir
, "AFTER RA");
3448 if (so
->type
== MESA_SHADER_FRAGMENT
)
3452 * Fixup inputs/outputs to point to the actual registers assigned:
3454 * 1) initialize to r63.x (invalid/unused)
3455 * 2) iterate IR level inputs/outputs and update the variants
3456 * inputs/outputs table based on the assigned registers for
3457 * the remaining inputs/outputs.
3460 for (unsigned i
= 0; i
< so
->inputs_count
; i
++)
3461 so
->inputs
[i
].regid
= regid(63, 0);
3462 for (unsigned i
= 0; i
< so
->outputs_count
; i
++)
3463 so
->outputs
[i
].regid
= regid(63, 0);
3465 struct ir3_instruction
*out
;
3466 foreach_output(out
, ir
) {
3467 assert(out
->opc
== OPC_META_COLLECT
);
3468 unsigned outidx
= out
->collect
.outidx
;
3470 so
->outputs
[outidx
].regid
= out
->regs
[0]->num
;
3471 so
->outputs
[outidx
].half
= !!(out
->regs
[0]->flags
& IR3_REG_HALF
);
3474 struct ir3_instruction
*in
;
3475 foreach_input(in
, ir
) {
3476 assert(in
->opc
== OPC_META_INPUT
);
3477 unsigned inidx
= in
->input
.inidx
;
3479 so
->inputs
[inidx
].regid
= in
->regs
[0]->num
;
3480 so
->inputs
[inidx
].half
= !!(in
->regs
[0]->flags
& IR3_REG_HALF
);
3484 fixup_astc_srgb(ctx
);
3486 /* We need to do legalize after (for frag shader's) the "bary.f"
3487 * offsets (inloc) have been assigned.
3489 ir3_legalize(ir
, &so
->has_ssbo
, &so
->need_pixlod
, &max_bary
);
3491 ir3_debug_print(ir
, "AFTER LEGALIZE");
3493 /* Set (ss)(sy) on first TCS and GEOMETRY instructions, since we don't
3494 * know what we might have to wait on when coming in from VS chsh.
3496 if (so
->type
== MESA_SHADER_TESS_CTRL
||
3497 so
->type
== MESA_SHADER_GEOMETRY
) {
3498 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
3499 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
3500 instr
->flags
|= IR3_INSTR_SS
| IR3_INSTR_SY
;
3506 so
->branchstack
= ctx
->max_stack
;
3508 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
3509 if (so
->type
== MESA_SHADER_FRAGMENT
)
3510 so
->total_in
= max_bary
+ 1;
3512 so
->max_sun
= ir
->max_sun
;
3514 /* Collect sampling instructions eligible for pre-dispatch. */
3515 collect_tex_prefetches(ctx
, ir
);
3520 ir3_destroy(so
->ir
);
3523 ir3_context_free(ctx
);