1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
31 #include "pipe/p_state.h"
32 #include "util/u_string.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "tgsi/tgsi_lowering.h"
36 #include "tgsi/tgsi_strings.h"
38 #include "nir/tgsi_to_nir.h"
39 #include "glsl/shader_enums.h"
41 #include "freedreno_util.h"
43 #include "ir3_compiler.h"
44 #include "ir3_shader.h"
47 #include "instr-a3xx.h"
51 static struct ir3_instruction
* create_immed(struct ir3_block
*block
, uint32_t val
);
54 const struct tgsi_token
*tokens
;
58 struct ir3_shader_variant
*so
;
60 /* bitmask of which samplers are integer: */
63 struct ir3_block
*block
;
65 /* For fragment shaders, from the hw perspective the only
66 * actual input is r0.xy position register passed to bary.f.
67 * But TGSI doesn't know that, it still declares things as
68 * IN[] registers. So we do all the input tracking normally
69 * and fix things up after compile_instructions()
71 * NOTE that frag_pos is the hardware position (possibly it
72 * is actually an index or tag or some such.. it is *not*
73 * values that can be directly used for gl_FragCoord..)
75 struct ir3_instruction
*frag_pos
, *frag_face
, *frag_coord
[4];
77 /* For vertex shaders, keep track of the system values sources */
78 struct ir3_instruction
*vertex_id
, *basevertex
, *instance_id
;
80 /* mapping from nir_register to defining instruction: */
81 struct hash_table
*def_ht
;
83 /* mapping from nir_variable to ir3_array: */
84 struct hash_table
*var_ht
;
87 /* a common pattern for indirect addressing is to request the
88 * same address register multiple times. To avoid generating
89 * duplicate instruction sequences (which our backend does not
90 * try to clean up, since that should be done as the NIR stage)
91 * we cache the address value generated for a given src value:
93 struct hash_table
*addr_ht
;
95 /* for calculating input/output positions/linkages: */
98 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
99 * so we need to use ldlv.u32 to load the varying directly:
103 /* on a3xx, we need to add one to # of array levels:
107 /* for looking up which system value is which */
108 unsigned sysval_semantics
[8];
110 /* list of kill instructions: */
111 struct ir3_instruction
*kill
[16];
112 unsigned int kill_count
;
114 /* set if we encounter something we can't handle yet, so we
115 * can bail cleanly and fallback to TGSI compiler f/e
121 static struct nir_shader
*to_nir(const struct tgsi_token
*tokens
)
123 struct nir_shader_compiler_options options
= {
128 .native_integers
= true,
132 struct nir_shader
*s
= tgsi_to_nir(tokens
, &options
);
134 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
135 debug_printf("----------------------\n");
136 nir_print_shader(s
, stdout
);
137 debug_printf("----------------------\n");
140 nir_opt_global_to_local(s
);
141 nir_convert_to_ssa(s
);
147 nir_lower_vars_to_ssa(s
);
148 nir_lower_alu_to_scalar(s
);
150 progress
|= nir_copy_prop(s
);
151 progress
|= nir_opt_dce(s
);
152 progress
|= nir_opt_cse(s
);
153 progress
|= ir3_nir_lower_if_else(s
);
154 progress
|= nir_opt_algebraic(s
);
155 progress
|= nir_opt_constant_folding(s
);
159 nir_remove_dead_variables(s
);
160 nir_validate_shader(s
);
162 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
163 debug_printf("----------------------\n");
164 nir_print_shader(s
, stdout
);
165 debug_printf("----------------------\n");
171 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
172 static const struct tgsi_token
*
173 lower_tgsi(const struct tgsi_token
*tokens
, struct ir3_shader_variant
*so
)
175 struct tgsi_shader_info info
;
176 struct tgsi_lowering_config lconfig
= {
177 .color_two_side
= so
->key
.color_two_side
,
182 case SHADER_FRAGMENT
:
184 lconfig
.saturate_s
= so
->key
.fsaturate_s
;
185 lconfig
.saturate_t
= so
->key
.fsaturate_t
;
186 lconfig
.saturate_r
= so
->key
.fsaturate_r
;
189 lconfig
.saturate_s
= so
->key
.vsaturate_s
;
190 lconfig
.saturate_t
= so
->key
.vsaturate_t
;
191 lconfig
.saturate_r
= so
->key
.vsaturate_r
;
195 if (so
->ir
->compiler
->gpu_id
>= 400) {
196 /* a4xx seems to have *no* sam.p */
197 lconfig
.lower_TXP
= ~0; /* lower all txp */
199 /* a3xx just needs to avoid sam.p for 3d tex */
200 lconfig
.lower_TXP
= (1 << TGSI_TEXTURE_3D
);
203 return tgsi_transform_lowering(&lconfig
, tokens
, &info
);
206 static struct ir3_compile
*
207 compile_init(struct ir3_shader_variant
*so
,
208 const struct tgsi_token
*tokens
)
210 struct ir3_compile
*ctx
= rzalloc(NULL
, struct ir3_compile
);
211 const struct tgsi_token
*lowered_tokens
;
213 if (so
->ir
->compiler
->gpu_id
>= 400) {
214 /* need special handling for "flat" */
215 ctx
->flat_bypass
= true;
216 ctx
->levels_add_one
= false;
218 /* no special handling for "flat" */
219 ctx
->flat_bypass
= false;
220 ctx
->levels_add_one
= true;
224 case SHADER_FRAGMENT
:
226 ctx
->integer_s
= so
->key
.finteger_s
;
229 ctx
->integer_s
= so
->key
.vinteger_s
;
236 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
237 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
238 ctx
->var_ht
= _mesa_hash_table_create(ctx
,
239 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
240 ctx
->addr_ht
= _mesa_hash_table_create(ctx
,
241 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
243 lowered_tokens
= lower_tgsi(tokens
, so
);
245 lowered_tokens
= tokens
;
246 ctx
->s
= to_nir(lowered_tokens
);
248 if (lowered_tokens
!= tokens
)
249 free((void *)lowered_tokens
);
251 so
->first_driver_param
= so
->first_immediate
= ctx
->s
->num_uniforms
;
253 /* one (vec4) slot for vertex id base: */
254 if (so
->type
== SHADER_VERTEX
)
255 so
->first_immediate
++;
257 /* reserve 4 (vec4) slots for ubo base addresses: */
258 so
->first_immediate
+= 4;
264 compile_error(struct ir3_compile
*ctx
, const char *format
, ...)
267 va_start(ap
, format
);
268 _debug_vprintf(format
, ap
);
270 nir_print_shader(ctx
->s
, stdout
);
275 #define compile_assert(ctx, cond) do { \
276 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
280 compile_free(struct ir3_compile
*ctx
)
287 unsigned length
, aid
;
288 struct ir3_instruction
*arr
[];
292 declare_var(struct ir3_compile
*ctx
, nir_variable
*var
)
294 unsigned length
= glsl_get_length(var
->type
) * 4; /* always vec4, at least with ttn */
295 struct ir3_array
*arr
= ralloc_size(ctx
, sizeof(*arr
) +
296 (length
* sizeof(arr
->arr
[0])));
297 arr
->length
= length
;
298 arr
->aid
= ++ctx
->num_arrays
;
299 /* Some shaders end up reading array elements without first writing..
300 * so initialize things to prevent null instr ptrs later:
302 for (unsigned i
= 0; i
< length
; i
++)
303 arr
->arr
[i
] = create_immed(ctx
->block
, 0);
304 _mesa_hash_table_insert(ctx
->var_ht
, var
, arr
);
307 static struct ir3_array
*
308 get_var(struct ir3_compile
*ctx
, nir_variable
*var
)
310 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->var_ht
, var
);
314 /* allocate a n element value array (to be populated by caller) and
317 static struct ir3_instruction
**
318 __get_dst(struct ir3_compile
*ctx
, void *key
, unsigned n
)
320 struct ir3_instruction
**value
=
321 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
322 _mesa_hash_table_insert(ctx
->def_ht
, key
, value
);
326 static struct ir3_instruction
**
327 get_dst(struct ir3_compile
*ctx
, nir_dest
*dst
, unsigned n
)
330 return __get_dst(ctx
, &dst
->ssa
, n
);
332 return __get_dst(ctx
, dst
->reg
.reg
, n
);
336 static struct ir3_instruction
**
337 get_dst_ssa(struct ir3_compile
*ctx
, nir_ssa_def
*dst
, unsigned n
)
339 return __get_dst(ctx
, dst
, n
);
342 static struct ir3_instruction
**
343 get_src(struct ir3_compile
*ctx
, nir_src
*src
)
345 struct hash_entry
*entry
;
347 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
349 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->reg
.reg
);
351 compile_assert(ctx
, entry
);
355 static struct ir3_instruction
*
356 create_immed(struct ir3_block
*block
, uint32_t val
)
358 struct ir3_instruction
*mov
;
360 mov
= ir3_instr_create(block
, 1, 0);
361 mov
->cat1
.src_type
= TYPE_U32
;
362 mov
->cat1
.dst_type
= TYPE_U32
;
363 ir3_reg_create(mov
, 0, 0);
364 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
369 static struct ir3_instruction
*
370 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
)
372 struct ir3_instruction
*instr
, *immed
;
374 /* TODO in at least some cases, the backend could probably be
375 * made clever enough to propagate IR3_REG_HALF..
377 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
378 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
380 immed
= create_immed(block
, 2);
381 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
383 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
384 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
385 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
387 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
388 instr
->regs
[0]->flags
|= IR3_REG_ADDR
| IR3_REG_HALF
;
389 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
394 /* caches addr values to avoid generating multiple cov/shl/mova
395 * sequences for each use of a given NIR level src as address
397 static struct ir3_instruction
*
398 get_addr(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
400 struct ir3_instruction
*addr
;
401 struct hash_entry
*entry
;
402 entry
= _mesa_hash_table_search(ctx
->addr_ht
, src
);
406 /* TODO do we need to cache per block? */
407 addr
= create_addr(ctx
->block
, src
);
408 _mesa_hash_table_insert(ctx
->addr_ht
, src
, addr
);
413 static struct ir3_instruction
*
414 create_uniform(struct ir3_compile
*ctx
, unsigned n
)
416 struct ir3_instruction
*mov
;
418 mov
= ir3_instr_create(ctx
->block
, 1, 0);
419 /* TODO get types right? */
420 mov
->cat1
.src_type
= TYPE_F32
;
421 mov
->cat1
.dst_type
= TYPE_F32
;
422 ir3_reg_create(mov
, 0, 0);
423 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
428 static struct ir3_instruction
*
429 create_uniform_indirect(struct ir3_compile
*ctx
, unsigned n
,
430 struct ir3_instruction
*address
)
432 struct ir3_instruction
*mov
;
434 mov
= ir3_instr_create(ctx
->block
, 1, 0);
435 mov
->cat1
.src_type
= TYPE_U32
;
436 mov
->cat1
.dst_type
= TYPE_U32
;
437 ir3_reg_create(mov
, 0, 0);
438 ir3_reg_create(mov
, n
, IR3_REG_CONST
| IR3_REG_RELATIV
);
439 mov
->address
= address
;
441 array_insert(ctx
->ir
->indirects
, mov
);
446 static struct ir3_instruction
*
447 create_collect(struct ir3_block
*block
, struct ir3_instruction
**arr
,
450 struct ir3_instruction
*collect
;
455 collect
= ir3_instr_create2(block
, -1, OPC_META_FI
, 1 + arrsz
);
456 ir3_reg_create(collect
, 0, 0);
457 for (unsigned i
= 0; i
< arrsz
; i
++)
458 ir3_reg_create(collect
, 0, IR3_REG_SSA
)->instr
= arr
[i
];
463 static struct ir3_instruction
*
464 create_indirect_load(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
465 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
467 struct ir3_block
*block
= ctx
->block
;
468 struct ir3_instruction
*mov
;
469 struct ir3_register
*src
;
471 mov
= ir3_instr_create(block
, 1, 0);
472 mov
->cat1
.src_type
= TYPE_U32
;
473 mov
->cat1
.dst_type
= TYPE_U32
;
474 ir3_reg_create(mov
, 0, 0);
475 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
476 src
->instr
= collect
;
479 mov
->address
= address
;
481 array_insert(ctx
->ir
->indirects
, mov
);
486 static struct ir3_instruction
*
487 create_indirect_store(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
488 struct ir3_instruction
*src
, struct ir3_instruction
*address
,
489 struct ir3_instruction
*collect
)
491 struct ir3_block
*block
= ctx
->block
;
492 struct ir3_instruction
*mov
;
493 struct ir3_register
*dst
;
495 mov
= ir3_instr_create(block
, 1, 0);
496 mov
->cat1
.src_type
= TYPE_U32
;
497 mov
->cat1
.dst_type
= TYPE_U32
;
498 dst
= ir3_reg_create(mov
, 0, IR3_REG_RELATIV
);
501 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
502 mov
->address
= address
;
503 mov
->fanin
= collect
;
505 array_insert(ctx
->ir
->indirects
, mov
);
510 static struct ir3_instruction
*
511 create_input(struct ir3_block
*block
, struct ir3_instruction
*instr
,
514 struct ir3_instruction
*in
;
516 in
= ir3_instr_create(block
, -1, OPC_META_INPUT
);
517 in
->inout
.block
= block
;
518 ir3_reg_create(in
, n
, 0);
520 ir3_reg_create(in
, 0, IR3_REG_SSA
)->instr
= instr
;
525 static struct ir3_instruction
*
526 create_frag_input(struct ir3_compile
*ctx
, unsigned n
, bool use_ldlv
)
528 struct ir3_block
*block
= ctx
->block
;
529 struct ir3_instruction
*instr
;
530 struct ir3_instruction
*inloc
= create_immed(block
, n
);
533 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
534 instr
->cat6
.type
= TYPE_U32
;
535 instr
->cat6
.iim_val
= 1;
537 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_pos
, 0);
538 instr
->regs
[2]->wrmask
= 0x3;
544 static struct ir3_instruction
*
545 create_frag_coord(struct ir3_compile
*ctx
, unsigned comp
)
547 struct ir3_block
*block
= ctx
->block
;
548 struct ir3_instruction
*instr
;
550 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
552 ctx
->frag_coord
[comp
] = create_input(ctx
->block
, NULL
, 0);
557 /* for frag_coord, we get unsigned values.. we need
558 * to subtract (integer) 8 and divide by 16 (right-
559 * shift by 4) then convert to float:
563 * mov.u32f32 dst, tmp
566 instr
= ir3_SUB_S(block
, ctx
->frag_coord
[comp
], 0,
567 create_immed(block
, 8), 0);
568 instr
= ir3_SHR_B(block
, instr
, 0,
569 create_immed(block
, 4), 0);
570 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
576 /* seems that we can use these as-is: */
577 return ctx
->frag_coord
[comp
];
581 static struct ir3_instruction
*
582 create_frag_face(struct ir3_compile
*ctx
, unsigned comp
)
584 struct ir3_block
*block
= ctx
->block
;
585 struct ir3_instruction
*instr
;
589 compile_assert(ctx
, !ctx
->frag_face
);
591 ctx
->frag_face
= create_input(block
, NULL
, 0);
593 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
594 * positive vs negative float.. and piglit further seems to
595 * expect -1.0 or 1.0:
597 * mul.s tmp, hr0.x, 2
599 * mov.s32f32, dst, tmp
602 instr
= ir3_MUL_S(block
, ctx
->frag_face
, 0,
603 create_immed(block
, 2), 0);
604 instr
= ir3_ADD_S(block
, instr
, 0,
605 create_immed(block
, 1), 0);
606 instr
= ir3_COV(block
, instr
, TYPE_S32
, TYPE_F32
);
611 return create_immed(block
, fui(0.0));
614 return create_immed(block
, fui(1.0));
618 /* helper for instructions that produce multiple consecutive scalar
619 * outputs which need to have a split/fanout meta instruction inserted
622 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
623 struct ir3_instruction
*src
)
625 struct ir3_instruction
*prev
= NULL
;
626 for (int i
= 0, j
= 0; i
< 4; i
++) {
627 struct ir3_instruction
*split
=
628 ir3_instr_create(block
, -1, OPC_META_FO
);
629 ir3_reg_create(split
, 0, IR3_REG_SSA
);
630 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
634 split
->cp
.left
= prev
;
635 split
->cp
.left_cnt
++;
636 prev
->cp
.right
= split
;
637 prev
->cp
.right_cnt
++;
641 if (src
->regs
[0]->wrmask
& (1 << i
))
647 * Adreno uses uint rather than having dedicated bool type,
648 * which (potentially) requires some conversion, in particular
649 * when using output of an bool instr to int input, or visa
653 * -------+---------+-------+-
657 * To convert from an adreno bool (uint) to nir, use:
659 * absneg.s dst, (neg)src
661 * To convert back in the other direction:
663 * absneg.s dst, (abs)arc
665 * The CP step can clean up the absneg.s that cancel each other
666 * out, and with a slight bit of extra cleverness (to recognize
667 * the instructions which produce either a 0 or 1) can eliminate
668 * the absneg.s's completely when an instruction that wants
669 * 0/1 consumes the result. For example, when a nir 'bcsel'
670 * consumes the result of 'feq'. So we should be able to get by
671 * without a boolean resolve step, and without incuring any
672 * extra penalty in instruction count.
675 /* NIR bool -> native (adreno): */
676 static struct ir3_instruction
*
677 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
679 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
682 /* native (adreno) -> NIR bool: */
683 static struct ir3_instruction
*
684 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
686 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
690 * alu/sfu instructions:
694 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
696 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
697 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
698 struct ir3_block
*b
= ctx
->block
;
700 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
702 /* Vectors are special in that they have non-scalarized writemasks,
703 * and just take the first swizzle channel for each argument in
704 * order into each writemask channel.
706 if ((alu
->op
== nir_op_vec2
) ||
707 (alu
->op
== nir_op_vec3
) ||
708 (alu
->op
== nir_op_vec4
)) {
710 for (int i
= 0; i
< info
->num_inputs
; i
++) {
711 nir_alu_src
*asrc
= &alu
->src
[i
];
713 compile_assert(ctx
, !asrc
->abs
);
714 compile_assert(ctx
, !asrc
->negate
);
716 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
718 src
[i
] = create_immed(ctx
->block
, 0);
719 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
725 /* General case: We can just grab the one used channel per src. */
726 for (int i
= 0; i
< info
->num_inputs
; i
++) {
727 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
728 nir_alu_src
*asrc
= &alu
->src
[i
];
730 compile_assert(ctx
, !asrc
->abs
);
731 compile_assert(ctx
, !asrc
->negate
);
733 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
735 compile_assert(ctx
, src
[i
]);
740 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
743 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
746 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
749 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
752 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
755 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
758 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
759 dst
[0]->cat2
.condition
= IR3_COND_NE
;
760 dst
[0] = ir3_n2b(b
, dst
[0]);
763 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
766 dst
[0] = ir3_b2n(b
, src
[0]);
769 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
770 dst
[0]->cat2
.condition
= IR3_COND_NE
;
771 dst
[0] = ir3_n2b(b
, dst
[0]);
775 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
778 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
781 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
784 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
787 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
790 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
793 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
796 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
799 dst
[0] = ir3_DSX(b
, src
[0], 0);
800 dst
[0]->cat5
.type
= TYPE_F32
;
803 dst
[0] = ir3_DSY(b
, src
[0], 0);
804 dst
[0]->cat5
.type
= TYPE_F32
;
808 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
809 dst
[0]->cat2
.condition
= IR3_COND_LT
;
810 dst
[0] = ir3_n2b(b
, dst
[0]);
813 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
814 dst
[0]->cat2
.condition
= IR3_COND_GE
;
815 dst
[0] = ir3_n2b(b
, dst
[0]);
818 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
819 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
820 dst
[0] = ir3_n2b(b
, dst
[0]);
823 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
824 dst
[0]->cat2
.condition
= IR3_COND_NE
;
825 dst
[0] = ir3_n2b(b
, dst
[0]);
828 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
831 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
834 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
836 case nir_op_fround_even
:
837 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
840 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
844 dst
[0] = ir3_SIN(b
, src
[0], 0);
847 dst
[0] = ir3_COS(b
, src
[0], 0);
850 dst
[0] = ir3_RSQ(b
, src
[0], 0);
853 dst
[0] = ir3_RCP(b
, src
[0], 0);
856 dst
[0] = ir3_LOG2(b
, src
[0], 0);
859 dst
[0] = ir3_EXP2(b
, src
[0], 0);
862 dst
[0] = ir3_SQRT(b
, src
[0], 0);
866 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
869 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
872 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
875 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
878 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
882 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
883 * mull.u tmp0, a, b ; mul low, i.e. al * bl
884 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
885 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
887 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
888 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
889 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
892 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
895 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
898 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
901 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
904 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
907 /* maybe this would be sane to lower in nir.. */
908 struct ir3_instruction
*neg
, *pos
;
910 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
911 neg
->cat2
.condition
= IR3_COND_LT
;
913 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
914 pos
->cat2
.condition
= IR3_COND_GT
;
916 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
921 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
924 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
927 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
930 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
931 dst
[0]->cat2
.condition
= IR3_COND_LT
;
932 dst
[0] = ir3_n2b(b
, dst
[0]);
935 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
936 dst
[0]->cat2
.condition
= IR3_COND_GE
;
937 dst
[0] = ir3_n2b(b
, dst
[0]);
940 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
941 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
942 dst
[0] = ir3_n2b(b
, dst
[0]);
945 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
946 dst
[0]->cat2
.condition
= IR3_COND_NE
;
947 dst
[0] = ir3_n2b(b
, dst
[0]);
950 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
951 dst
[0]->cat2
.condition
= IR3_COND_LT
;
952 dst
[0] = ir3_n2b(b
, dst
[0]);
955 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
956 dst
[0]->cat2
.condition
= IR3_COND_GE
;
957 dst
[0] = ir3_n2b(b
, dst
[0]);
961 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
965 compile_error(ctx
, "Unhandled ALU op: %s\n",
966 nir_op_infos
[alu
->op
].name
);
971 /* handles direct/indirect UBO reads: */
973 emit_intrinsic_load_ubo(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
974 struct ir3_instruction
**dst
)
976 struct ir3_block
*b
= ctx
->block
;
977 struct ir3_instruction
*addr
, *src0
, *src1
;
978 /* UBO addresses are the first driver params: */
979 unsigned ubo
= regid(ctx
->so
->first_driver_param
, 0);
980 unsigned off
= intr
->const_index
[0];
982 /* First src is ubo index, which could either be an immed or not: */
983 src0
= get_src(ctx
, &intr
->src
[0])[0];
984 if (is_same_type_mov(src0
) &&
985 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
986 addr
= create_uniform(ctx
, ubo
+ src0
->regs
[1]->iim_val
);
988 addr
= create_uniform_indirect(ctx
, ubo
, get_addr(ctx
, src0
));
991 if (intr
->intrinsic
== nir_intrinsic_load_ubo_indirect
) {
992 /* For load_ubo_indirect, second src is indirect offset: */
993 src1
= get_src(ctx
, &intr
->src
[1])[0];
995 /* and add offset to addr: */
996 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
999 /* if offset is to large to encode in the ldg, split it out: */
1000 if ((off
+ (intr
->num_components
* 4)) > 1024) {
1001 /* split out the minimal amount to improve the odds that
1002 * cp can fit the immediate in the add.s instruction:
1004 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
1005 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
1009 for (int i
= 0; i
< intr
->num_components
; i
++) {
1010 struct ir3_instruction
*load
=
1011 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0);
1012 load
->cat6
.type
= TYPE_U32
;
1013 load
->cat6
.offset
= off
+ i
* 4; /* byte offset */
1018 /* handles array reads: */
1020 emit_intrinisic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
1021 struct ir3_instruction
**dst
)
1023 nir_deref_var
*dvar
= intr
->variables
[0];
1024 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1025 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1027 compile_assert(ctx
, dvar
->deref
.child
&&
1028 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1030 switch (darr
->deref_array_type
) {
1031 case nir_deref_array_type_direct
:
1032 /* direct access does not require anything special: */
1033 for (int i
= 0; i
< intr
->num_components
; i
++) {
1034 unsigned n
= darr
->base_offset
* 4 + i
;
1035 compile_assert(ctx
, n
< arr
->length
);
1036 dst
[i
] = arr
->arr
[n
];
1039 case nir_deref_array_type_indirect
: {
1040 /* for indirect, we need to collect all the array elements: */
1041 struct ir3_instruction
*collect
=
1042 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1043 struct ir3_instruction
*addr
=
1044 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1045 for (int i
= 0; i
< intr
->num_components
; i
++) {
1046 unsigned n
= darr
->base_offset
* 4 + i
;
1047 compile_assert(ctx
, n
< arr
->length
);
1048 dst
[i
] = create_indirect_load(ctx
, arr
->length
, n
, addr
, collect
);
1053 compile_error(ctx
, "Unhandled load deref type: %u\n",
1054 darr
->deref_array_type
);
1059 /* handles array writes: */
1061 emit_intrinisic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1063 nir_deref_var
*dvar
= intr
->variables
[0];
1064 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1065 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1066 struct ir3_instruction
**src
;
1068 compile_assert(ctx
, dvar
->deref
.child
&&
1069 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1071 src
= get_src(ctx
, &intr
->src
[0]);
1073 switch (darr
->deref_array_type
) {
1074 case nir_deref_array_type_direct
:
1075 /* direct access does not require anything special: */
1076 for (int i
= 0; i
< intr
->num_components
; i
++) {
1077 unsigned n
= darr
->base_offset
* 4 + i
;
1078 compile_assert(ctx
, n
< arr
->length
);
1079 arr
->arr
[n
] = src
[i
];
1082 case nir_deref_array_type_indirect
: {
1083 /* for indirect, create indirect-store and fan that out: */
1084 struct ir3_instruction
*collect
=
1085 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1086 struct ir3_instruction
*addr
=
1087 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1088 for (int i
= 0; i
< intr
->num_components
; i
++) {
1089 struct ir3_instruction
*store
;
1090 unsigned n
= darr
->base_offset
* 4 + i
;
1091 compile_assert(ctx
, n
< arr
->length
);
1093 store
= create_indirect_store(ctx
, arr
->length
,
1094 n
, src
[i
], addr
, collect
);
1096 store
->fanin
->fi
.aid
= arr
->aid
;
1098 /* TODO: probably split this out to be used for
1099 * store_output_indirect? or move this into
1100 * create_indirect_store()?
1102 for (int j
= i
; j
< arr
->length
; j
+= 4) {
1103 struct ir3_instruction
*split
;
1105 split
= ir3_instr_create(ctx
->block
, -1, OPC_META_FO
);
1107 ir3_reg_create(split
, 0, 0);
1108 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= store
;
1110 arr
->arr
[j
] = split
;
1116 compile_error(ctx
, "Unhandled store deref type: %u\n",
1117 darr
->deref_array_type
);
1122 static void add_sysval_input(struct ir3_compile
*ctx
, unsigned name
,
1123 struct ir3_instruction
*instr
)
1125 struct ir3_shader_variant
*so
= ctx
->so
;
1126 unsigned r
= regid(so
->inputs_count
, 0);
1127 unsigned n
= so
->inputs_count
++;
1129 so
->inputs
[n
].semantic
= ir3_semantic_name(name
, 0);
1130 so
->inputs
[n
].compmask
= 1;
1131 so
->inputs
[n
].regid
= r
;
1132 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1135 ctx
->block
->ninputs
= MAX2(ctx
->block
->ninputs
, r
+ 1);
1136 ctx
->block
->inputs
[r
] = instr
;
1140 emit_intrinisic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1142 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1143 struct ir3_instruction
**dst
, **src
;
1144 struct ir3_block
*b
= ctx
->block
;
1145 unsigned idx
= intr
->const_index
[0];
1147 if (info
->has_dest
) {
1148 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1153 switch (intr
->intrinsic
) {
1154 case nir_intrinsic_load_uniform
:
1155 for (int i
= 0; i
< intr
->num_components
; i
++) {
1156 unsigned n
= idx
* 4 + i
;
1157 dst
[i
] = create_uniform(ctx
, n
);
1160 case nir_intrinsic_load_uniform_indirect
:
1161 src
= get_src(ctx
, &intr
->src
[0]);
1162 for (int i
= 0; i
< intr
->num_components
; i
++) {
1163 unsigned n
= idx
* 4 + i
;
1164 dst
[i
] = create_uniform_indirect(ctx
, n
,
1165 get_addr(ctx
, src
[0]));
1168 case nir_intrinsic_load_ubo
:
1169 case nir_intrinsic_load_ubo_indirect
:
1170 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
1172 case nir_intrinsic_load_input
:
1173 for (int i
= 0; i
< intr
->num_components
; i
++) {
1174 unsigned n
= idx
* 4 + i
;
1175 dst
[i
] = b
->inputs
[n
];
1178 case nir_intrinsic_load_input_indirect
:
1179 src
= get_src(ctx
, &intr
->src
[0]);
1180 struct ir3_instruction
*collect
=
1181 create_collect(b
, b
->inputs
, b
->ninputs
);
1182 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1183 for (int i
= 0; i
< intr
->num_components
; i
++) {
1184 unsigned n
= idx
* 4 + i
;
1185 dst
[i
] = create_indirect_load(ctx
, b
->ninputs
, n
, addr
, collect
);
1188 case nir_intrinsic_load_var
:
1189 emit_intrinisic_load_var(ctx
, intr
, dst
);
1191 case nir_intrinsic_store_var
:
1192 emit_intrinisic_store_var(ctx
, intr
);
1194 case nir_intrinsic_store_output
:
1195 src
= get_src(ctx
, &intr
->src
[0]);
1196 for (int i
= 0; i
< intr
->num_components
; i
++) {
1197 unsigned n
= idx
* 4 + i
;
1198 b
->outputs
[n
] = src
[i
];
1201 case nir_intrinsic_load_base_vertex
:
1202 if (!ctx
->basevertex
) {
1203 /* first four vec4 sysval's reserved for UBOs: */
1204 unsigned r
= regid(ctx
->so
->first_driver_param
+ 4, 0);
1205 ctx
->basevertex
= create_uniform(ctx
, r
);
1206 add_sysval_input(ctx
, TGSI_SEMANTIC_BASEVERTEX
,
1209 dst
[0] = ctx
->basevertex
;
1211 case nir_intrinsic_load_vertex_id_zero_base
:
1212 if (!ctx
->vertex_id
) {
1213 ctx
->vertex_id
= create_input(ctx
->block
, NULL
, 0);
1214 add_sysval_input(ctx
, TGSI_SEMANTIC_VERTEXID_NOBASE
,
1217 dst
[0] = ctx
->vertex_id
;
1219 case nir_intrinsic_load_instance_id
:
1220 if (!ctx
->instance_id
) {
1221 ctx
->instance_id
= create_input(ctx
->block
, NULL
, 0);
1222 add_sysval_input(ctx
, TGSI_SEMANTIC_INSTANCEID
,
1225 dst
[0] = ctx
->instance_id
;
1227 case nir_intrinsic_discard_if
:
1228 case nir_intrinsic_discard
: {
1229 struct ir3_instruction
*cond
, *kill
;
1231 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1232 /* conditional discard: */
1233 src
= get_src(ctx
, &intr
->src
[0]);
1234 cond
= ir3_b2n(b
, src
[0]);
1236 /* unconditional discard: */
1237 cond
= create_immed(b
, 1);
1240 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1241 cond
->cat2
.condition
= IR3_COND_NE
;
1243 /* condition always goes in predicate register: */
1244 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1246 kill
= ir3_KILL(b
, cond
, 0);
1247 array_insert(ctx
->ir
->predicates
, kill
);
1249 ctx
->kill
[ctx
->kill_count
++] = kill
;
1250 ctx
->so
->has_kill
= true;
1255 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1256 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1262 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1264 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1265 instr
->def
.num_components
);
1266 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1267 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u
[i
]);
1271 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1273 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1274 undef
->def
.num_components
);
1275 /* backend doesn't want undefined instructions, so just plug
1278 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1279 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1283 * texture fetch/sample instructions:
1287 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1289 unsigned coords
, flags
= 0;
1291 /* note: would use tex->coord_components.. except txs.. also,
1292 * since array index goes after shadow ref, we don't want to
1295 switch (tex
->sampler_dim
) {
1296 case GLSL_SAMPLER_DIM_1D
:
1297 case GLSL_SAMPLER_DIM_BUF
:
1300 case GLSL_SAMPLER_DIM_2D
:
1301 case GLSL_SAMPLER_DIM_RECT
:
1302 case GLSL_SAMPLER_DIM_EXTERNAL
:
1303 case GLSL_SAMPLER_DIM_MS
:
1306 case GLSL_SAMPLER_DIM_3D
:
1307 case GLSL_SAMPLER_DIM_CUBE
:
1309 flags
|= IR3_INSTR_3D
;
1312 unreachable("bad sampler_dim");
1316 flags
|= IR3_INSTR_S
;
1319 flags
|= IR3_INSTR_A
;
1326 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1328 struct ir3_block
*b
= ctx
->block
;
1329 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1330 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1331 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1332 unsigned i
, coords
, flags
;
1333 unsigned nsrc0
= 0, nsrc1
= 0;
1337 coord
= off
= ddx
= ddy
= NULL
;
1338 lod
= proj
= compare
= NULL
;
1340 /* TODO: might just be one component for gathers? */
1341 dst
= get_dst(ctx
, &tex
->dest
, 4);
1343 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1344 switch (tex
->src
[i
].src_type
) {
1345 case nir_tex_src_coord
:
1346 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1348 case nir_tex_src_bias
:
1349 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1352 case nir_tex_src_lod
:
1353 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1356 case nir_tex_src_comparitor
: /* shadow comparator */
1357 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1359 case nir_tex_src_projector
:
1360 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1363 case nir_tex_src_offset
:
1364 off
= get_src(ctx
, &tex
->src
[i
].src
);
1367 case nir_tex_src_ddx
:
1368 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1370 case nir_tex_src_ddy
:
1371 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1374 compile_error(ctx
, "Unhandled NIR tex serc type: %d\n",
1375 tex
->src
[i
].src_type
);
1381 case nir_texop_tex
: opc
= OPC_SAM
; break;
1382 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1383 case nir_texop_txl
: opc
= OPC_SAML
; break;
1384 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1385 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1386 case nir_texop_txf_ms
:
1390 case nir_texop_query_levels
:
1391 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1395 tex_info(tex
, &flags
, &coords
);
1397 /* scale up integer coords for TXF based on the LOD */
1398 if (opc
== OPC_ISAML
) {
1400 for (i
= 0; i
< coords
; i
++)
1401 coord
[i
] = ir3_SHL_B(b
, coord
[i
], 0, lod
, 0);
1404 * lay out the first argument in the proper order:
1405 * - actual coordinates first
1406 * - shadow reference
1409 * - starting at offset 4, dpdx.xy, dpdy.xy
1411 * bias/lod go into the second arg
1414 /* insert tex coords: */
1415 for (i
= 0; i
< coords
; i
++)
1416 src0
[nsrc0
++] = coord
[i
];
1419 /* hw doesn't do 1d, so we treat it as 2d with
1420 * height of 1, and patch up the y coord.
1421 * TODO: y coord should be (int)0 in some cases..
1423 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1427 src0
[nsrc0
++] = compare
;
1430 src0
[nsrc0
++] = coord
[coords
];
1433 src0
[nsrc0
++] = proj
;
1434 flags
|= IR3_INSTR_P
;
1437 /* pad to 4, then ddx/ddy: */
1438 if (tex
->op
== nir_texop_txd
) {
1440 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1441 for (i
= 0; i
< coords
; i
++)
1442 src0
[nsrc0
++] = ddx
[i
];
1444 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1445 for (i
= 0; i
< coords
; i
++)
1446 src0
[nsrc0
++] = ddy
[i
];
1448 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1452 * second argument (if applicable):
1457 if (has_off
| has_lod
| has_bias
) {
1459 for (i
= 0; i
< coords
; i
++)
1460 src1
[nsrc1
++] = off
[i
];
1462 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1463 flags
|= IR3_INSTR_O
;
1466 if (has_lod
| has_bias
)
1467 src1
[nsrc1
++] = lod
;
1470 switch (tex
->dest_type
) {
1471 case nir_type_invalid
:
1472 case nir_type_float
:
1478 case nir_type_unsigned
:
1483 unreachable("bad dest_type");
1486 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
,
1487 flags
, tex
->sampler_index
, tex
->sampler_index
,
1488 create_collect(b
, src0
, nsrc0
),
1489 create_collect(b
, src1
, nsrc1
));
1491 split_dest(b
, dst
, sam
);
1495 emit_tex_query_levels(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1497 struct ir3_block
*b
= ctx
->block
;
1498 struct ir3_instruction
**dst
, *sam
;
1500 dst
= get_dst(ctx
, &tex
->dest
, 1);
1502 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, TGSI_WRITEMASK_Z
, 0,
1503 tex
->sampler_index
, tex
->sampler_index
, NULL
, NULL
);
1505 /* even though there is only one component, since it ends
1506 * up in .z rather than .x, we need a split_dest()
1508 split_dest(b
, dst
, sam
);
1510 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1511 * the value in TEX_CONST_0 is zero-based.
1513 if (ctx
->levels_add_one
)
1514 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
1518 emit_tex_txs(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1520 struct ir3_block
*b
= ctx
->block
;
1521 struct ir3_instruction
**dst
, *sam
, *lod
;
1522 unsigned flags
, coords
;
1524 tex_info(tex
, &flags
, &coords
);
1526 dst
= get_dst(ctx
, &tex
->dest
, 4);
1528 compile_assert(ctx
, tex
->num_srcs
== 1);
1529 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
1531 lod
= get_src(ctx
, &tex
->src
[0].src
)[0];
1533 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
1534 tex
->sampler_index
, tex
->sampler_index
, lod
, NULL
);
1536 split_dest(b
, dst
, sam
);
1538 /* Array size actually ends up in .w rather than .z. This doesn't
1539 * matter for miplevel 0, but for higher mips the value in z is
1540 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1541 * returned, which means that we have to add 1 to it for arrays.
1543 if (tex
->is_array
) {
1544 if (ctx
->levels_add_one
) {
1545 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
1547 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
1553 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1555 switch (instr
->type
) {
1556 case nir_instr_type_alu
:
1557 emit_alu(ctx
, nir_instr_as_alu(instr
));
1559 case nir_instr_type_intrinsic
:
1560 emit_intrinisic(ctx
, nir_instr_as_intrinsic(instr
));
1562 case nir_instr_type_load_const
:
1563 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1565 case nir_instr_type_ssa_undef
:
1566 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1568 case nir_instr_type_tex
: {
1569 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
1570 /* couple tex instructions get special-cased:
1574 emit_tex_txs(ctx
, tex
);
1576 case nir_texop_query_levels
:
1577 emit_tex_query_levels(ctx
, tex
);
1585 case nir_instr_type_call
:
1586 case nir_instr_type_jump
:
1587 case nir_instr_type_phi
:
1588 case nir_instr_type_parallel_copy
:
1589 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1595 emit_block(struct ir3_compile
*ctx
, nir_block
*block
)
1597 nir_foreach_instr(block
, instr
) {
1598 emit_instr(ctx
, instr
);
1605 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1607 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1608 switch (node
->type
) {
1609 case nir_cf_node_block
:
1610 emit_block(ctx
, nir_cf_node_as_block(node
));
1612 case nir_cf_node_if
:
1613 case nir_cf_node_loop
:
1614 case nir_cf_node_function
:
1615 compile_error(ctx
, "TODO\n");
1624 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1626 struct ir3_shader_variant
*so
= ctx
->so
;
1627 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1628 unsigned ncomp
= glsl_get_components(in
->type
);
1629 /* XXX: map loc slots to semantics */
1630 unsigned semantic_name
= in
->data
.location
;
1631 unsigned semantic_index
= in
->data
.index
;
1632 unsigned n
= in
->data
.driver_location
;
1634 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1635 semantic_name
, semantic_index
, array_len
,
1638 so
->inputs
[n
].semantic
=
1639 ir3_semantic_name(semantic_name
, semantic_index
);
1640 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1641 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1642 so
->inputs
[n
].interpolate
= 0;
1643 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1645 /* the fdN_program_emit() code expects tgsi consts here, so map
1646 * things back to tgsi for now:
1648 switch (in
->data
.interpolation
) {
1649 case INTERP_QUALIFIER_FLAT
:
1650 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1652 case INTERP_QUALIFIER_NOPERSPECTIVE
:
1653 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_LINEAR
;
1655 case INTERP_QUALIFIER_SMOOTH
:
1656 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
1660 for (int i
= 0; i
< ncomp
; i
++) {
1661 struct ir3_instruction
*instr
= NULL
;
1662 unsigned idx
= (n
* 4) + i
;
1664 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1665 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1666 so
->inputs
[n
].bary
= false;
1667 so
->frag_coord
= true;
1668 instr
= create_frag_coord(ctx
, i
);
1669 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1670 so
->inputs
[n
].bary
= false;
1671 so
->frag_face
= true;
1672 instr
= create_frag_face(ctx
, i
);
1674 bool use_ldlv
= false;
1676 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1677 * from the semantic name:
1679 if ((in
->data
.interpolation
== INTERP_QUALIFIER_NONE
) &&
1680 ((semantic_name
== TGSI_SEMANTIC_COLOR
) ||
1681 (semantic_name
== TGSI_SEMANTIC_BCOLOR
)))
1682 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_COLOR
;
1684 if (ctx
->flat_bypass
) {
1685 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1686 * from the semantic name:
1688 switch (so
->inputs
[n
].interpolate
) {
1689 case TGSI_INTERPOLATE_COLOR
:
1690 if (!ctx
->so
->key
.rasterflat
)
1693 case TGSI_INTERPOLATE_CONSTANT
:
1699 so
->inputs
[n
].bary
= true;
1701 instr
= create_frag_input(ctx
,
1702 so
->inputs
[n
].inloc
+ i
- 8, use_ldlv
);
1705 instr
= create_input(ctx
->block
, NULL
, idx
);
1708 ctx
->block
->inputs
[idx
] = instr
;
1711 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
1712 ctx
->next_inloc
+= ncomp
;
1713 so
->total_in
+= ncomp
;
1718 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
1720 struct ir3_shader_variant
*so
= ctx
->so
;
1721 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
1722 unsigned ncomp
= glsl_get_components(out
->type
);
1723 /* XXX: map loc slots to semantics */
1724 unsigned semantic_name
= out
->data
.location
;
1725 unsigned semantic_index
= out
->data
.index
;
1726 unsigned n
= out
->data
.driver_location
;
1729 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
1730 semantic_name
, semantic_index
, array_len
,
1733 if (ctx
->so
->type
== SHADER_VERTEX
) {
1734 switch (semantic_name
) {
1735 case TGSI_SEMANTIC_POSITION
:
1736 so
->writes_pos
= true;
1738 case TGSI_SEMANTIC_PSIZE
:
1739 so
->writes_psize
= true;
1741 case TGSI_SEMANTIC_COLOR
:
1742 case TGSI_SEMANTIC_BCOLOR
:
1743 case TGSI_SEMANTIC_GENERIC
:
1744 case TGSI_SEMANTIC_FOG
:
1745 case TGSI_SEMANTIC_TEXCOORD
:
1748 compile_error(ctx
, "unknown VS semantic name: %s\n",
1749 tgsi_semantic_names
[semantic_name
]);
1752 switch (semantic_name
) {
1753 case TGSI_SEMANTIC_POSITION
:
1754 comp
= 2; /* tgsi will write to .z component */
1755 so
->writes_pos
= true;
1757 case TGSI_SEMANTIC_COLOR
:
1760 compile_error(ctx
, "unknown FS semantic name: %s\n",
1761 tgsi_semantic_names
[semantic_name
]);
1765 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
1767 so
->outputs
[n
].semantic
=
1768 ir3_semantic_name(semantic_name
, semantic_index
);
1769 so
->outputs
[n
].regid
= regid(n
, comp
);
1770 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
1772 for (int i
= 0; i
< ncomp
; i
++) {
1773 unsigned idx
= (n
* 4) + i
;
1775 ctx
->block
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
1780 emit_instructions(struct ir3_compile
*ctx
)
1782 unsigned ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
1783 unsigned noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
1785 /* we need to allocate big enough outputs array so that
1786 * we can stuff the kill's at the end. Likewise for vtx
1787 * shaders, we need to leave room for sysvals:
1789 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1790 noutputs
+= ARRAY_SIZE(ctx
->kill
);
1791 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
1795 ctx
->block
= ir3_block_create(ctx
->ir
, 0, ninputs
, noutputs
);
1797 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1798 ctx
->block
->noutputs
-= ARRAY_SIZE(ctx
->kill
);
1799 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
1800 ctx
->block
->ninputs
-= 8;
1803 /* for fragment shader, we have a single input register (usually
1804 * r0.xy) which is used as the base for bary.f varying fetch instrs:
1806 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1807 // TODO maybe a helper for fi since we need it a few places..
1808 struct ir3_instruction
*instr
;
1809 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
1810 ir3_reg_create(instr
, 0, 0);
1811 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
1812 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
1813 ctx
->frag_pos
= instr
;
1817 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->inputs
) {
1818 setup_input(ctx
, var
);
1821 /* Setup outputs: */
1822 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->outputs
) {
1823 setup_output(ctx
, var
);
1826 /* Setup variables (which should only be arrays): */
1827 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->globals
) {
1828 declare_var(ctx
, var
);
1831 /* Find the main function and emit the body: */
1832 nir_foreach_overload(ctx
->s
, overload
) {
1833 compile_assert(ctx
, strcmp(overload
->function
->name
, "main") == 0);
1834 compile_assert(ctx
, overload
->impl
);
1835 emit_function(ctx
, overload
->impl
);
1841 /* from NIR perspective, we actually have inputs. But most of the "inputs"
1842 * for a fragment shader are just bary.f instructions. The *actual* inputs
1843 * from the hw perspective are the frag_pos and optionally frag_coord and
1847 fixup_frag_inputs(struct ir3_compile
*ctx
)
1849 struct ir3_shader_variant
*so
= ctx
->so
;
1850 struct ir3_block
*block
= ctx
->block
;
1851 struct ir3_instruction
**inputs
;
1852 struct ir3_instruction
*instr
;
1857 n
= 4; /* always have frag_pos */
1858 n
+= COND(so
->frag_face
, 4);
1859 n
+= COND(so
->frag_coord
, 4);
1861 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
1863 if (so
->frag_face
) {
1864 /* this ultimately gets assigned to hr0.x so doesn't conflict
1865 * with frag_coord/frag_pos..
1867 inputs
[block
->ninputs
++] = ctx
->frag_face
;
1868 ctx
->frag_face
->regs
[0]->num
= 0;
1870 /* remaining channels not used, but let's avoid confusing
1871 * other parts that expect inputs to come in groups of vec4
1873 inputs
[block
->ninputs
++] = NULL
;
1874 inputs
[block
->ninputs
++] = NULL
;
1875 inputs
[block
->ninputs
++] = NULL
;
1878 /* since we don't know where to set the regid for frag_coord,
1879 * we have to use r0.x for it. But we don't want to *always*
1880 * use r1.x for frag_pos as that could increase the register
1881 * footprint on simple shaders:
1883 if (so
->frag_coord
) {
1884 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
1885 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
1886 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
1887 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
1889 inputs
[block
->ninputs
++] = ctx
->frag_coord
[0];
1890 inputs
[block
->ninputs
++] = ctx
->frag_coord
[1];
1891 inputs
[block
->ninputs
++] = ctx
->frag_coord
[2];
1892 inputs
[block
->ninputs
++] = ctx
->frag_coord
[3];
1895 /* we always have frag_pos: */
1896 so
->pos_regid
= regid
;
1899 instr
= create_input(block
, NULL
, block
->ninputs
);
1900 instr
->regs
[0]->num
= regid
++;
1901 inputs
[block
->ninputs
++] = instr
;
1902 ctx
->frag_pos
->regs
[1]->instr
= instr
;
1905 instr
= create_input(block
, NULL
, block
->ninputs
);
1906 instr
->regs
[0]->num
= regid
++;
1907 inputs
[block
->ninputs
++] = instr
;
1908 ctx
->frag_pos
->regs
[2]->instr
= instr
;
1910 block
->inputs
= inputs
;
1914 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
1915 struct ir3_shader_variant
*so
,
1916 const struct tgsi_token
*tokens
,
1917 struct ir3_shader_key key
)
1919 struct ir3_compile
*ctx
;
1920 struct ir3_block
*block
;
1921 struct ir3_instruction
**inputs
;
1922 unsigned i
, j
, actual_in
;
1923 int ret
= 0, max_bary
;
1927 so
->ir
= ir3_create(compiler
);
1931 ctx
= compile_init(so
, tokens
);
1933 DBG("INIT failed!");
1938 emit_instructions(ctx
);
1941 DBG("EMIT failed!");
1947 so
->ir
->block
= block
;
1949 /* keep track of the inputs from TGSI perspective.. */
1950 inputs
= block
->inputs
;
1952 /* but fixup actual inputs for frag shader: */
1953 if (so
->type
== SHADER_FRAGMENT
)
1954 fixup_frag_inputs(ctx
);
1956 /* at this point, for binning pass, throw away unneeded outputs: */
1957 if (key
.binning_pass
) {
1958 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
1959 unsigned name
= sem2name(so
->outputs
[i
].semantic
);
1960 unsigned idx
= sem2idx(so
->outputs
[i
].semantic
);
1962 /* throw away everything but first position/psize */
1963 if ((idx
== 0) && ((name
== TGSI_SEMANTIC_POSITION
) ||
1964 (name
== TGSI_SEMANTIC_PSIZE
))) {
1966 so
->outputs
[j
] = so
->outputs
[i
];
1967 block
->outputs
[(j
*4)+0] = block
->outputs
[(i
*4)+0];
1968 block
->outputs
[(j
*4)+1] = block
->outputs
[(i
*4)+1];
1969 block
->outputs
[(j
*4)+2] = block
->outputs
[(i
*4)+2];
1970 block
->outputs
[(j
*4)+3] = block
->outputs
[(i
*4)+3];
1975 so
->outputs_count
= j
;
1976 block
->noutputs
= j
* 4;
1979 /* if we want half-precision outputs, mark the output registers
1982 if (key
.half_precision
) {
1983 for (i
= 0; i
< block
->noutputs
; i
++) {
1984 if (!block
->outputs
[i
])
1986 block
->outputs
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
1990 /* at this point, we want the kill's in the outputs array too,
1991 * so that they get scheduled (since they have no dst).. we've
1992 * already ensured that the array is big enough in push_block():
1994 if (so
->type
== SHADER_FRAGMENT
) {
1995 for (i
= 0; i
< ctx
->kill_count
; i
++)
1996 block
->outputs
[block
->noutputs
++] = ctx
->kill
[i
];
1999 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2000 printf("BEFORE CP:\n");
2004 ir3_block_depth(block
);
2006 ir3_block_cp(block
);
2008 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2009 printf("BEFORE GROUPING:\n");
2013 /* Group left/right neighbors, inserting mov's where needed to
2016 ir3_block_group(block
);
2018 ir3_block_depth(block
);
2020 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2021 printf("AFTER DEPTH:\n");
2025 ret
= ir3_block_sched(block
);
2027 DBG("SCHED failed!");
2031 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2032 printf("AFTER SCHED:\n");
2036 ret
= ir3_block_ra(block
, so
->type
, so
->frag_coord
, so
->frag_face
);
2042 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2043 printf("AFTER RA:\n");
2047 ir3_block_legalize(block
, &so
->has_samp
, &max_bary
);
2049 /* fixup input/outputs: */
2050 for (i
= 0; i
< so
->outputs_count
; i
++) {
2051 so
->outputs
[i
].regid
= block
->outputs
[i
*4]->regs
[0]->num
;
2052 /* preserve hack for depth output.. tgsi writes depth to .z,
2053 * but what we give the hw is the scalar register:
2055 if ((so
->type
== SHADER_FRAGMENT
) &&
2056 (sem2name(so
->outputs
[i
].semantic
) == TGSI_SEMANTIC_POSITION
))
2057 so
->outputs
[i
].regid
+= 2;
2060 /* Note that some or all channels of an input may be unused: */
2062 for (i
= 0; i
< so
->inputs_count
; i
++) {
2063 unsigned j
, regid
= ~0, compmask
= 0;
2064 so
->inputs
[i
].ncomp
= 0;
2065 for (j
= 0; j
< 4; j
++) {
2066 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
2068 compmask
|= (1 << j
);
2069 regid
= in
->regs
[0]->num
- j
;
2071 so
->inputs
[i
].ncomp
++;
2074 so
->inputs
[i
].regid
= regid
;
2075 so
->inputs
[i
].compmask
= compmask
;
2078 /* fragment shader always gets full vec4's even if it doesn't
2079 * fetch all components, but vertex shader we need to update
2080 * with the actual number of components fetch, otherwise thing
2081 * will hang due to mismaptch between VFD_DECODE's and
2084 if (so
->type
== SHADER_VERTEX
)
2085 so
->total_in
= actual_in
;
2087 so
->total_in
= align(max_bary
+ 1, 4);
2091 ir3_destroy(so
->ir
);