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"
46 #include "instr-a3xx.h"
50 static struct ir3_instruction
* create_immed(struct ir3_block
*block
, uint32_t val
);
53 const struct tgsi_token
*tokens
;
57 struct ir3_shader_variant
*so
;
59 /* bitmask of which samplers are integer: */
62 struct ir3_block
*block
;
64 /* For fragment shaders, from the hw perspective the only
65 * actual input is r0.xy position register passed to bary.f.
66 * But TGSI doesn't know that, it still declares things as
67 * IN[] registers. So we do all the input tracking normally
68 * and fix things up after compile_instructions()
70 * NOTE that frag_pos is the hardware position (possibly it
71 * is actually an index or tag or some such.. it is *not*
72 * values that can be directly used for gl_FragCoord..)
74 struct ir3_instruction
*frag_pos
, *frag_face
, *frag_coord
[4];
76 /* For vertex shaders, keep track of the system values sources */
77 struct ir3_instruction
*vertex_id
, *basevertex
, *instance_id
;
79 /* mapping from nir_register to defining instruction: */
80 struct hash_table
*def_ht
;
82 /* mapping from nir_variable to ir3_array: */
83 struct hash_table
*var_ht
;
86 /* a common pattern for indirect addressing is to request the
87 * same address register multiple times. To avoid generating
88 * duplicate instruction sequences (which our backend does not
89 * try to clean up, since that should be done as the NIR stage)
90 * we cache the address value generated for a given src value:
92 struct hash_table
*addr_ht
;
94 /* for calculating input/output positions/linkages: */
97 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
98 * so we need to use ldlv.u32 to load the varying directly:
102 /* for looking up which system value is which */
103 unsigned sysval_semantics
[8];
105 /* list of kill instructions: */
106 struct ir3_instruction
*kill
[16];
107 unsigned int kill_count
;
109 /* set if we encounter something we can't handle yet, so we
110 * can bail cleanly and fallback to TGSI compiler f/e
116 static struct nir_shader
*to_nir(const struct tgsi_token
*tokens
)
118 struct nir_shader_compiler_options options
= {
123 .native_integers
= true,
127 struct nir_shader
*s
= tgsi_to_nir(tokens
, &options
);
129 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
130 debug_printf("----------------------\n");
131 nir_print_shader(s
, stdout
);
132 debug_printf("----------------------\n");
135 nir_opt_global_to_local(s
);
136 nir_convert_to_ssa(s
);
142 nir_lower_vars_to_ssa(s
);
143 nir_lower_alu_to_scalar(s
);
145 progress
|= nir_copy_prop(s
);
146 progress
|= nir_opt_dce(s
);
147 progress
|= nir_opt_cse(s
);
148 progress
|= nir_opt_peephole_select(s
);
149 progress
|= nir_opt_algebraic(s
);
150 progress
|= nir_opt_constant_folding(s
);
154 nir_remove_dead_variables(s
);
155 nir_validate_shader(s
);
157 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
158 debug_printf("----------------------\n");
159 nir_print_shader(s
, stdout
);
160 debug_printf("----------------------\n");
166 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
167 static const struct tgsi_token
*
168 lower_tgsi(const struct tgsi_token
*tokens
, struct ir3_shader_variant
*so
)
170 struct tgsi_shader_info info
;
171 struct tgsi_lowering_config lconfig
= {
172 .color_two_side
= so
->key
.color_two_side
,
177 case SHADER_FRAGMENT
:
179 lconfig
.saturate_s
= so
->key
.fsaturate_s
;
180 lconfig
.saturate_t
= so
->key
.fsaturate_t
;
181 lconfig
.saturate_r
= so
->key
.fsaturate_r
;
184 lconfig
.saturate_s
= so
->key
.vsaturate_s
;
185 lconfig
.saturate_t
= so
->key
.vsaturate_t
;
186 lconfig
.saturate_r
= so
->key
.vsaturate_r
;
191 /* hack for standalone compiler which does not have
194 } else if (ir3_shader_gpuid(so
->shader
) >= 400) {
195 /* a4xx seems to have *no* sam.p */
196 lconfig
.lower_TXP
= ~0; /* lower all txp */
198 /* a3xx just needs to avoid sam.p for 3d tex */
199 lconfig
.lower_TXP
= (1 << TGSI_TEXTURE_3D
);
202 return tgsi_transform_lowering(&lconfig
, tokens
, &info
);
205 static struct ir3_compile
*
206 compile_init(struct ir3_shader_variant
*so
,
207 const struct tgsi_token
*tokens
)
209 struct ir3_compile
*ctx
= rzalloc(NULL
, struct ir3_compile
);
210 const struct tgsi_token
*lowered_tokens
;
213 /* hack for standalone compiler which does not have
216 } else if (ir3_shader_gpuid(so
->shader
) >= 400) {
217 /* need special handling for "flat" */
218 ctx
->flat_bypass
= true;
220 /* no special handling for "flat" */
221 ctx
->flat_bypass
= false;
225 case SHADER_FRAGMENT
:
227 ctx
->integer_s
= so
->key
.finteger_s
;
230 ctx
->integer_s
= so
->key
.vinteger_s
;
237 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
238 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
239 ctx
->var_ht
= _mesa_hash_table_create(ctx
,
240 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
241 ctx
->addr_ht
= _mesa_hash_table_create(ctx
,
242 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
244 lowered_tokens
= lower_tgsi(tokens
, so
);
246 lowered_tokens
= tokens
;
247 ctx
->s
= to_nir(lowered_tokens
);
249 if (lowered_tokens
!= tokens
)
250 free((void *)lowered_tokens
);
252 so
->first_immediate
= ctx
->s
->num_uniforms
;
253 /* for now, now driver params: */
254 so
->first_driver_param
= so
->first_immediate
;
260 compile_error(struct ir3_compile
*ctx
, const char *format
, ...)
263 va_start(ap
, format
);
264 _debug_vprintf(format
, ap
);
266 nir_print_shader(ctx
->s
, stdout
);
271 #define compile_assert(ctx, cond) do { \
272 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
276 compile_free(struct ir3_compile
*ctx
)
283 unsigned length
, aid
;
284 struct ir3_instruction
*arr
[];
288 declare_var(struct ir3_compile
*ctx
, nir_variable
*var
)
290 unsigned length
= glsl_get_length(var
->type
) * 4; /* always vec4, at least with ttn */
291 struct ir3_array
*arr
= ralloc_size(ctx
, sizeof(*arr
) +
292 (length
* sizeof(arr
->arr
[0])));
293 arr
->length
= length
;
294 arr
->aid
= ++ctx
->num_arrays
;
295 _mesa_hash_table_insert(ctx
->var_ht
, var
, arr
);
298 static struct ir3_array
*
299 get_var(struct ir3_compile
*ctx
, nir_variable
*var
)
301 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->var_ht
, var
);
305 /* allocate a n element value array (to be populated by caller) and
308 static struct ir3_instruction
**
309 __get_dst(struct ir3_compile
*ctx
, void *key
, unsigned n
)
311 struct ir3_instruction
**value
=
312 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
313 _mesa_hash_table_insert(ctx
->def_ht
, key
, value
);
317 static struct ir3_instruction
**
318 get_dst(struct ir3_compile
*ctx
, nir_dest
*dst
, unsigned n
)
321 return __get_dst(ctx
, &dst
->ssa
, n
);
323 return __get_dst(ctx
, dst
->reg
.reg
, n
);
327 static struct ir3_instruction
**
328 get_dst_ssa(struct ir3_compile
*ctx
, nir_ssa_def
*dst
, unsigned n
)
330 return __get_dst(ctx
, dst
, n
);
333 static struct ir3_instruction
**
334 get_src(struct ir3_compile
*ctx
, nir_src
*src
)
336 struct hash_entry
*entry
;
338 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
340 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->reg
.reg
);
342 compile_assert(ctx
, entry
);
346 static struct ir3_instruction
*
347 create_immed(struct ir3_block
*block
, uint32_t val
)
349 struct ir3_instruction
*mov
;
351 mov
= ir3_instr_create(block
, 1, 0);
352 mov
->cat1
.src_type
= TYPE_U32
;
353 mov
->cat1
.dst_type
= TYPE_U32
;
354 ir3_reg_create(mov
, 0, 0);
355 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
360 static struct ir3_instruction
*
361 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
)
363 struct ir3_instruction
*instr
, *immed
;
365 /* TODO in at least some cases, the backend could probably be
366 * made clever enough to propagate IR3_REG_HALF..
368 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
369 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
371 immed
= create_immed(block
, 2);
372 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
374 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
375 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
376 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
378 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
379 instr
->regs
[0]->flags
|= IR3_REG_ADDR
| IR3_REG_HALF
;
380 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
385 /* caches addr values to avoid generating multiple cov/shl/mova
386 * sequences for each use of a given NIR level src as address
388 static struct ir3_instruction
*
389 get_addr(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
391 struct ir3_instruction
*addr
;
392 struct hash_entry
*entry
;
393 entry
= _mesa_hash_table_search(ctx
->addr_ht
, src
);
397 /* TODO do we need to cache per block? */
398 addr
= create_addr(ctx
->block
, src
);
399 _mesa_hash_table_insert(ctx
->addr_ht
, src
, addr
);
404 static struct ir3_instruction
*
405 create_uniform(struct ir3_compile
*ctx
, unsigned n
)
407 struct ir3_instruction
*mov
;
409 mov
= ir3_instr_create(ctx
->block
, 1, 0);
410 /* TODO get types right? */
411 mov
->cat1
.src_type
= TYPE_F32
;
412 mov
->cat1
.dst_type
= TYPE_F32
;
413 ir3_reg_create(mov
, 0, 0);
414 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
419 static struct ir3_instruction
*
420 create_uniform_indirect(struct ir3_compile
*ctx
, unsigned n
,
421 struct ir3_instruction
*address
)
423 struct ir3_instruction
*mov
;
425 mov
= ir3_instr_create(ctx
->block
, 1, 0);
426 mov
->cat1
.src_type
= TYPE_U32
;
427 mov
->cat1
.dst_type
= TYPE_U32
;
428 ir3_reg_create(mov
, 0, 0);
429 ir3_reg_create(mov
, n
, IR3_REG_CONST
| IR3_REG_RELATIV
);
430 mov
->address
= address
;
432 array_insert(ctx
->ir
->indirects
, mov
);
437 static struct ir3_instruction
*
438 create_collect(struct ir3_block
*block
, struct ir3_instruction
**arr
,
441 struct ir3_instruction
*collect
;
446 collect
= ir3_instr_create2(block
, -1, OPC_META_FI
, 1 + arrsz
);
447 ir3_reg_create(collect
, 0, 0);
448 for (unsigned i
= 0; i
< arrsz
; i
++)
449 ir3_reg_create(collect
, 0, IR3_REG_SSA
)->instr
= arr
[i
];
454 static struct ir3_instruction
*
455 create_indirect_load(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
456 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
458 struct ir3_block
*block
= ctx
->block
;
459 struct ir3_instruction
*mov
;
460 struct ir3_register
*src
;
462 mov
= ir3_instr_create(block
, 1, 0);
463 mov
->cat1
.src_type
= TYPE_U32
;
464 mov
->cat1
.dst_type
= TYPE_U32
;
465 ir3_reg_create(mov
, 0, 0);
466 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
467 src
->instr
= collect
;
470 mov
->address
= address
;
472 array_insert(ctx
->ir
->indirects
, mov
);
477 static struct ir3_instruction
*
478 create_indirect_store(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
479 struct ir3_instruction
*src
, struct ir3_instruction
*address
,
480 struct ir3_instruction
*collect
)
482 struct ir3_block
*block
= ctx
->block
;
483 struct ir3_instruction
*mov
;
484 struct ir3_register
*dst
;
486 mov
= ir3_instr_create(block
, 1, 0);
487 mov
->cat1
.src_type
= TYPE_U32
;
488 mov
->cat1
.dst_type
= TYPE_U32
;
489 dst
= ir3_reg_create(mov
, 0, IR3_REG_RELATIV
);
492 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
493 mov
->address
= address
;
494 mov
->fanin
= collect
;
496 array_insert(ctx
->ir
->indirects
, mov
);
501 static struct ir3_instruction
*
502 create_input(struct ir3_block
*block
, struct ir3_instruction
*instr
,
505 struct ir3_instruction
*in
;
507 in
= ir3_instr_create(block
, -1, OPC_META_INPUT
);
508 in
->inout
.block
= block
;
509 ir3_reg_create(in
, n
, 0);
511 ir3_reg_create(in
, 0, IR3_REG_SSA
)->instr
= instr
;
516 static struct ir3_instruction
*
517 create_frag_input(struct ir3_compile
*ctx
, unsigned n
, bool use_ldlv
)
519 struct ir3_block
*block
= ctx
->block
;
520 struct ir3_instruction
*instr
;
521 struct ir3_instruction
*inloc
= create_immed(block
, n
);
524 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
525 instr
->cat6
.type
= TYPE_U32
;
526 instr
->cat6
.iim_val
= 1;
528 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_pos
, 0);
529 instr
->regs
[2]->wrmask
= 0x3;
535 static struct ir3_instruction
*
536 create_frag_coord(struct ir3_compile
*ctx
, unsigned comp
)
538 struct ir3_block
*block
= ctx
->block
;
539 struct ir3_instruction
*instr
;
541 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
543 ctx
->frag_coord
[comp
] = create_input(ctx
->block
, NULL
, 0);
548 /* for frag_coord, we get unsigned values.. we need
549 * to subtract (integer) 8 and divide by 16 (right-
550 * shift by 4) then convert to float:
554 * mov.u32f32 dst, tmp
557 instr
= ir3_ADD_S(block
, ctx
->frag_coord
[comp
], 0,
558 create_immed(block
, -8), 0);
559 instr
= ir3_SHR_B(block
, instr
, 0,
560 create_immed(block
, 4), 0);
561 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
567 /* seems that we can use these as-is: */
568 return ctx
->frag_coord
[comp
];
572 static struct ir3_instruction
*
573 create_frag_face(struct ir3_compile
*ctx
, unsigned comp
)
575 struct ir3_block
*block
= ctx
->block
;
576 struct ir3_instruction
*instr
;
580 compile_assert(ctx
, !ctx
->frag_face
);
582 ctx
->frag_face
= create_input(block
, NULL
, 0);
584 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
585 * positive vs negative float.. and piglit further seems to
586 * expect -1.0 or 1.0:
588 * mul.s tmp, hr0.x, 2
590 * mov.s32f32, dst, tmp
593 instr
= ir3_MUL_S(block
, ctx
->frag_face
, 0,
594 create_immed(block
, 2), 0);
595 instr
= ir3_ADD_S(block
, instr
, 0,
596 create_immed(block
, 1), 0);
597 instr
= ir3_COV(block
, instr
, TYPE_S32
, TYPE_F32
);
602 return create_immed(block
, fui(0.0));
605 return create_immed(block
, fui(1.0));
609 /* helper for instructions that produce multiple consecutive scalar
610 * outputs which need to have a split/fanout meta instruction inserted
613 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
614 struct ir3_instruction
*src
)
616 struct ir3_instruction
*prev
= NULL
;
617 for (int i
= 0, j
= 0; i
< 4; i
++) {
618 struct ir3_instruction
*split
=
619 ir3_instr_create(block
, -1, OPC_META_FO
);
620 ir3_reg_create(split
, 0, IR3_REG_SSA
);
621 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
625 split
->cp
.left
= prev
;
626 split
->cp
.left_cnt
++;
627 prev
->cp
.right
= split
;
628 prev
->cp
.right_cnt
++;
632 if (src
->regs
[0]->wrmask
& (1 << i
))
638 * Adreno uses uint rather than having dedicated bool type,
639 * which (potentially) requires some conversion, in particular
640 * when using output of an bool instr to int input, or visa
644 * -------+---------+-------+-
648 * To convert from an adreno bool (uint) to nir, use:
650 * absneg.s dst, (neg)src
652 * To convert back in the other direction:
654 * absneg.s dst, (abs)arc
656 * The CP step can clean up the absneg.s that cancel each other
657 * out, and with a slight bit of extra cleverness (to recognize
658 * the instructions which produce either a 0 or 1) can eliminate
659 * the absneg.s's completely when an instruction that wants
660 * 0/1 consumes the result. For example, when a nir 'bcsel'
661 * consumes the result of 'feq'. So we should be able to get by
662 * without a boolean resolve step, and without incuring any
663 * extra penalty in instruction count.
666 /* NIR bool -> native (adreno): */
667 static struct ir3_instruction
*
668 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
670 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
673 /* native (adreno) -> NIR bool: */
674 static struct ir3_instruction
*
675 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
677 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
681 * alu/sfu instructions:
685 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
687 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
688 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
689 struct ir3_block
*b
= ctx
->block
;
691 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
693 /* Vectors are special in that they have non-scalarized writemasks,
694 * and just take the first swizzle channel for each argument in
695 * order into each writemask channel.
697 if ((alu
->op
== nir_op_vec2
) ||
698 (alu
->op
== nir_op_vec3
) ||
699 (alu
->op
== nir_op_vec4
)) {
701 for (int i
= 0; i
< info
->num_inputs
; i
++) {
702 nir_alu_src
*asrc
= &alu
->src
[i
];
704 compile_assert(ctx
, !asrc
->abs
);
705 compile_assert(ctx
, !asrc
->negate
);
707 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
709 src
[i
] = create_immed(ctx
->block
, 0);
710 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
716 /* General case: We can just grab the one used channel per src. */
717 for (int i
= 0; i
< info
->num_inputs
; i
++) {
718 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
719 nir_alu_src
*asrc
= &alu
->src
[i
];
721 compile_assert(ctx
, !asrc
->abs
);
722 compile_assert(ctx
, !asrc
->negate
);
724 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
729 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
732 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
735 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
738 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
741 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
744 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
747 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
748 dst
[0]->cat2
.condition
= IR3_COND_NE
;
749 dst
[0] = ir3_n2b(b
, dst
[0]);
752 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
755 dst
[0] = ir3_b2n(b
, src
[0]);
758 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
759 dst
[0]->cat2
.condition
= IR3_COND_NE
;
760 dst
[0] = ir3_n2b(b
, dst
[0]);
764 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
767 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
770 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
773 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
776 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
779 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
782 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
785 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
788 dst
[0] = ir3_DSX(b
, src
[0], 0);
789 dst
[0]->cat5
.type
= TYPE_F32
;
792 dst
[0] = ir3_DSY(b
, src
[0], 0);
793 dst
[0]->cat5
.type
= TYPE_F32
;
797 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
798 dst
[0]->cat2
.condition
= IR3_COND_LT
;
799 dst
[0] = ir3_n2b(b
, dst
[0]);
802 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
803 dst
[0]->cat2
.condition
= IR3_COND_GE
;
804 dst
[0] = ir3_n2b(b
, dst
[0]);
807 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
808 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
809 dst
[0] = ir3_n2b(b
, dst
[0]);
812 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
813 dst
[0]->cat2
.condition
= IR3_COND_NE
;
814 dst
[0] = ir3_n2b(b
, dst
[0]);
817 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
820 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
823 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
825 case nir_op_fround_even
:
826 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
829 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
833 dst
[0] = ir3_SIN(b
, src
[0], 0);
836 dst
[0] = ir3_COS(b
, src
[0], 0);
839 dst
[0] = ir3_RSQ(b
, src
[0], 0);
842 dst
[0] = ir3_RCP(b
, src
[0], 0);
845 dst
[0] = ir3_LOG2(b
, src
[0], 0);
848 dst
[0] = ir3_EXP2(b
, src
[0], 0);
851 dst
[0] = ir3_SQRT(b
, src
[0], 0);
855 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
858 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
861 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
864 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
867 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
871 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
872 * mull.u tmp0, a, b ; mul low, i.e. al * bl
873 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
874 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
876 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
877 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
878 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
881 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
884 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
887 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
890 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
893 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
896 /* maybe this would be sane to lower in nir.. */
897 struct ir3_instruction
*neg
, *pos
;
899 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
900 neg
->cat2
.condition
= IR3_COND_LT
;
902 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
903 pos
->cat2
.condition
= IR3_COND_GT
;
905 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
910 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
913 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
916 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
919 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
920 dst
[0]->cat2
.condition
= IR3_COND_LT
;
921 dst
[0] = ir3_n2b(b
, dst
[0]);
924 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
925 dst
[0]->cat2
.condition
= IR3_COND_GE
;
926 dst
[0] = ir3_n2b(b
, dst
[0]);
929 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
930 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
931 dst
[0] = ir3_n2b(b
, dst
[0]);
934 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
935 dst
[0]->cat2
.condition
= IR3_COND_NE
;
936 dst
[0] = ir3_n2b(b
, dst
[0]);
939 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
940 dst
[0]->cat2
.condition
= IR3_COND_LT
;
941 dst
[0] = ir3_n2b(b
, dst
[0]);
944 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
945 dst
[0]->cat2
.condition
= IR3_COND_GE
;
946 dst
[0] = ir3_n2b(b
, dst
[0]);
950 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
954 compile_error(ctx
, "Unhandled ALU op: %s\n",
955 nir_op_infos
[alu
->op
].name
);
960 /* handles array reads: */
962 emit_intrinisic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
963 struct ir3_instruction
**dst
)
965 nir_deref_var
*dvar
= intr
->variables
[0];
966 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
967 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
969 compile_assert(ctx
, dvar
->deref
.child
&&
970 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
972 switch (darr
->deref_array_type
) {
973 case nir_deref_array_type_direct
:
974 /* direct access does not require anything special: */
975 for (int i
= 0; i
< intr
->num_components
; i
++) {
976 unsigned n
= darr
->base_offset
* 4 + i
;
977 compile_assert(ctx
, n
< arr
->length
);
978 dst
[i
] = arr
->arr
[n
];
981 case nir_deref_array_type_indirect
: {
982 /* for indirect, we need to collect all the array elements: */
983 struct ir3_instruction
*collect
=
984 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
985 struct ir3_instruction
*addr
=
986 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
987 for (int i
= 0; i
< intr
->num_components
; i
++) {
988 unsigned n
= darr
->base_offset
* 4 + i
;
989 compile_assert(ctx
, n
< arr
->length
);
990 dst
[i
] = create_indirect_load(ctx
, arr
->length
, n
, addr
, collect
);
995 compile_error(ctx
, "Unhandled load deref type: %u\n",
996 darr
->deref_array_type
);
1001 /* handles array writes: */
1003 emit_intrinisic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1005 nir_deref_var
*dvar
= intr
->variables
[0];
1006 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1007 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1008 struct ir3_instruction
**src
;
1010 compile_assert(ctx
, dvar
->deref
.child
&&
1011 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1013 src
= get_src(ctx
, &intr
->src
[0]);
1015 switch (darr
->deref_array_type
) {
1016 case nir_deref_array_type_direct
:
1017 /* direct access does not require anything special: */
1018 for (int i
= 0; i
< intr
->num_components
; i
++) {
1019 unsigned n
= darr
->base_offset
* 4 + i
;
1020 compile_assert(ctx
, n
< arr
->length
);
1021 arr
->arr
[n
] = src
[i
];
1024 case nir_deref_array_type_indirect
: {
1025 /* for indirect, create indirect-store and fan that out: */
1026 struct ir3_instruction
*collect
=
1027 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1028 struct ir3_instruction
*addr
=
1029 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1030 for (int i
= 0; i
< intr
->num_components
; i
++) {
1031 struct ir3_instruction
*store
;
1032 unsigned n
= darr
->base_offset
* 4 + i
;
1033 compile_assert(ctx
, n
< arr
->length
);
1035 store
= create_indirect_store(ctx
, arr
->length
,
1036 n
, src
[i
], addr
, collect
);
1038 store
->fanin
->fi
.aid
= arr
->aid
;
1040 /* TODO: probably split this out to be used for
1041 * store_output_indirect? or move this into
1042 * create_indirect_store()?
1044 for (int j
= i
; j
< arr
->length
; j
+= 4) {
1045 struct ir3_instruction
*split
;
1047 split
= ir3_instr_create(ctx
->block
, -1, OPC_META_FO
);
1049 ir3_reg_create(split
, 0, 0);
1050 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= store
;
1052 arr
->arr
[j
] = split
;
1058 compile_error(ctx
, "Unhandled store deref type: %u\n",
1059 darr
->deref_array_type
);
1065 emit_intrinisic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1067 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1068 struct ir3_instruction
**dst
, **src
;
1069 struct ir3_block
*b
= ctx
->block
;
1070 unsigned idx
= intr
->const_index
[0];
1072 if (info
->has_dest
) {
1073 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1076 switch (intr
->intrinsic
) {
1077 case nir_intrinsic_load_uniform
:
1078 compile_assert(ctx
, intr
->const_index
[1] == 1);
1079 for (int i
= 0; i
< intr
->num_components
; i
++) {
1080 unsigned n
= idx
* 4 + i
;
1081 dst
[i
] = create_uniform(ctx
, n
);
1084 case nir_intrinsic_load_uniform_indirect
:
1085 compile_assert(ctx
, intr
->const_index
[1] == 1);
1086 src
= get_src(ctx
, &intr
->src
[0]);
1087 for (int i
= 0; i
< intr
->num_components
; i
++) {
1088 unsigned n
= idx
* 4 + i
;
1089 dst
[i
] = create_uniform_indirect(ctx
, n
,
1090 get_addr(ctx
, src
[0]));
1093 case nir_intrinsic_load_input
:
1094 compile_assert(ctx
, intr
->const_index
[1] == 1);
1095 for (int i
= 0; i
< intr
->num_components
; i
++) {
1096 unsigned n
= idx
* 4 + i
;
1097 dst
[i
] = b
->inputs
[n
];
1100 case nir_intrinsic_load_input_indirect
:
1101 compile_assert(ctx
, intr
->const_index
[1] == 1);
1102 src
= get_src(ctx
, &intr
->src
[0]);
1103 struct ir3_instruction
*collect
=
1104 create_collect(b
, b
->inputs
, b
->ninputs
);
1105 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1106 for (int i
= 0; i
< intr
->num_components
; i
++) {
1107 unsigned n
= idx
* 4 + i
;
1108 dst
[i
] = create_indirect_load(ctx
, b
->ninputs
, n
, addr
, collect
);
1111 case nir_intrinsic_load_var
:
1112 emit_intrinisic_load_var(ctx
, intr
, dst
);
1114 case nir_intrinsic_store_var
:
1115 emit_intrinisic_store_var(ctx
, intr
);
1117 case nir_intrinsic_store_output
:
1118 compile_assert(ctx
, intr
->const_index
[1] == 1);
1119 src
= get_src(ctx
, &intr
->src
[0]);
1120 for (int i
= 0; i
< intr
->num_components
; i
++) {
1121 unsigned n
= idx
* 4 + i
;
1122 b
->outputs
[n
] = src
[i
];
1125 case nir_intrinsic_discard_if
:
1126 case nir_intrinsic_discard
: {
1127 struct ir3_instruction
*cond
, *kill
;
1129 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1130 /* conditional discard: */
1131 src
= get_src(ctx
, &intr
->src
[0]);
1132 cond
= ir3_b2n(b
, src
[0]);
1134 /* unconditional discard: */
1135 cond
= create_immed(b
, 1);
1138 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1139 cond
->cat2
.condition
= IR3_COND_NE
;
1141 /* condition always goes in predicate register: */
1142 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1144 kill
= ir3_KILL(b
, cond
, 0);
1146 ctx
->kill
[ctx
->kill_count
++] = kill
;
1147 ctx
->so
->has_kill
= true;
1152 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1153 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1159 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1161 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1162 instr
->def
.num_components
);
1163 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1164 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u
[i
]);
1168 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1170 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1171 undef
->def
.num_components
);
1172 /* backend doesn't want undefined instructions, so just plug
1175 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1176 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1180 * texture fetch/sample instructions:
1184 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1186 unsigned coords
, flags
= 0;
1188 /* note: would use tex->coord_components.. except txs.. also,
1189 * since array index goes after shadow ref, we don't want to
1192 switch (tex
->sampler_dim
) {
1193 case GLSL_SAMPLER_DIM_1D
:
1194 case GLSL_SAMPLER_DIM_BUF
:
1197 case GLSL_SAMPLER_DIM_2D
:
1198 case GLSL_SAMPLER_DIM_RECT
:
1199 case GLSL_SAMPLER_DIM_EXTERNAL
:
1200 case GLSL_SAMPLER_DIM_MS
:
1203 case GLSL_SAMPLER_DIM_3D
:
1204 case GLSL_SAMPLER_DIM_CUBE
:
1206 flags
|= IR3_INSTR_3D
;
1211 flags
|= IR3_INSTR_S
;
1214 flags
|= IR3_INSTR_A
;
1221 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1223 struct ir3_block
*b
= ctx
->block
;
1224 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1225 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1226 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1227 unsigned i
, coords
, flags
;
1228 unsigned nsrc0
= 0, nsrc1
= 0;
1232 /* TODO: might just be one component for gathers? */
1233 dst
= get_dst(ctx
, &tex
->dest
, 4);
1235 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1236 switch (tex
->src
[i
].src_type
) {
1237 case nir_tex_src_coord
:
1238 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1240 case nir_tex_src_bias
:
1241 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1244 case nir_tex_src_lod
:
1245 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1248 case nir_tex_src_comparitor
: /* shadow comparator */
1249 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1251 case nir_tex_src_projector
:
1252 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1255 case nir_tex_src_offset
:
1256 off
= get_src(ctx
, &tex
->src
[i
].src
);
1259 case nir_tex_src_ddx
:
1260 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1262 case nir_tex_src_ddy
:
1263 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1266 compile_error(ctx
, "Unhandled NIR tex serc type: %d\n",
1267 tex
->src
[i
].src_type
);
1273 * lay out the first argument in the proper order:
1274 * - actual coordinates first
1275 * - shadow reference
1278 * - starting at offset 4, dpdx.xy, dpdy.xy
1280 * bias/lod go into the second arg
1283 tex_info(tex
, &flags
, &coords
);
1285 /* insert tex coords: */
1286 for (i
= 0; i
< coords
; i
++)
1287 src0
[nsrc0
++] = coord
[i
];
1290 /* hw doesn't do 1d, so we treat it as 2d with
1291 * height of 1, and patch up the y coord.
1292 * TODO: y coord should be (int)0 in some cases..
1294 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1298 src0
[nsrc0
++] = compare
;
1301 src0
[nsrc0
++] = coord
[coords
];
1304 src0
[nsrc0
++] = proj
;
1305 flags
|= IR3_INSTR_P
;
1308 /* pad to 4, then ddx/ddy: */
1309 if (tex
->op
== nir_texop_txd
) {
1311 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1312 for (i
= 0; i
< coords
; i
++)
1313 src0
[nsrc0
++] = ddx
[i
];
1315 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1316 for (i
= 0; i
< coords
; i
++)
1317 src0
[nsrc0
++] = ddy
[i
];
1319 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1323 * second argument (if applicable):
1328 if (has_off
| has_lod
| has_bias
) {
1330 for (i
= 0; i
< coords
; i
++)
1331 src1
[nsrc1
++] = off
[i
];
1333 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1334 flags
|= IR3_INSTR_O
;
1337 if (has_lod
| has_bias
)
1338 src1
[nsrc1
++] = lod
;
1342 case nir_texop_tex
: opc
= OPC_SAM
; break;
1343 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1344 case nir_texop_txl
: opc
= OPC_SAML
; break;
1345 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1346 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1347 case nir_texop_txf_ms
:
1351 case nir_texop_query_levels
:
1352 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1356 switch (tex
->dest_type
) {
1357 case nir_type_invalid
:
1358 case nir_type_float
:
1364 case nir_type_unsigned
:
1370 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
,
1371 flags
, tex
->sampler_index
, tex
->sampler_index
,
1372 create_collect(b
, src0
, nsrc0
),
1373 create_collect(b
, src1
, nsrc1
));
1375 split_dest(b
, dst
, sam
);
1380 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1382 switch (instr
->type
) {
1383 case nir_instr_type_alu
:
1384 emit_alu(ctx
, nir_instr_as_alu(instr
));
1386 case nir_instr_type_intrinsic
:
1387 emit_intrinisic(ctx
, nir_instr_as_intrinsic(instr
));
1389 case nir_instr_type_load_const
:
1390 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1392 case nir_instr_type_ssa_undef
:
1393 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1395 case nir_instr_type_tex
:
1396 emit_tex(ctx
, nir_instr_as_tex(instr
));
1399 case nir_instr_type_call
:
1400 case nir_instr_type_jump
:
1401 case nir_instr_type_phi
:
1402 case nir_instr_type_parallel_copy
:
1403 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1409 emit_block(struct ir3_compile
*ctx
, nir_block
*block
)
1411 nir_foreach_instr(block
, instr
) {
1412 emit_instr(ctx
, instr
);
1419 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1421 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1422 switch (node
->type
) {
1423 case nir_cf_node_block
:
1424 emit_block(ctx
, nir_cf_node_as_block(node
));
1426 case nir_cf_node_if
:
1427 case nir_cf_node_loop
:
1428 case nir_cf_node_function
:
1429 compile_error(ctx
, "TODO\n");
1438 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1440 struct ir3_shader_variant
*so
= ctx
->so
;
1441 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1442 unsigned ncomp
= glsl_get_components(in
->type
);
1443 /* XXX: map loc slots to semantics */
1444 unsigned semantic_name
= in
->data
.location
;
1445 unsigned semantic_index
= in
->data
.index
;
1446 unsigned n
= in
->data
.driver_location
;
1448 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1449 semantic_name
, semantic_index
, array_len
,
1452 so
->inputs
[n
].semantic
=
1453 ir3_semantic_name(semantic_name
, semantic_index
);
1454 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1455 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1456 so
->inputs
[n
].interpolate
= 0;
1457 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1459 /* the fdN_program_emit() code expects tgsi consts here, so map
1460 * things back to tgsi for now:
1462 switch (in
->data
.interpolation
) {
1463 case INTERP_QUALIFIER_FLAT
:
1464 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1466 case INTERP_QUALIFIER_NOPERSPECTIVE
:
1467 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_LINEAR
;
1469 case INTERP_QUALIFIER_SMOOTH
:
1470 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
1474 for (int i
= 0; i
< ncomp
; i
++) {
1475 struct ir3_instruction
*instr
= NULL
;
1476 unsigned idx
= (n
* 4) + i
;
1478 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1479 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1480 so
->inputs
[n
].bary
= false;
1481 so
->frag_coord
= true;
1482 instr
= create_frag_coord(ctx
, i
);
1483 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1484 so
->inputs
[n
].bary
= false;
1485 so
->frag_face
= true;
1486 instr
= create_frag_face(ctx
, i
);
1488 bool use_ldlv
= false;
1490 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1491 * from the semantic name:
1493 if (semantic_name
== TGSI_SEMANTIC_COLOR
)
1494 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_COLOR
;
1496 if (ctx
->flat_bypass
) {
1497 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1498 * from the semantic name:
1500 switch (so
->inputs
[n
].interpolate
) {
1501 case TGSI_INTERPOLATE_COLOR
:
1502 if (!ctx
->so
->key
.rasterflat
)
1505 case TGSI_INTERPOLATE_CONSTANT
:
1511 so
->inputs
[n
].bary
= true;
1513 instr
= create_frag_input(ctx
, idx
, use_ldlv
);
1516 instr
= create_input(ctx
->block
, NULL
, idx
);
1519 ctx
->block
->inputs
[idx
] = instr
;
1522 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
1523 ctx
->next_inloc
+= ncomp
;
1524 so
->total_in
+= ncomp
;
1529 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
1531 struct ir3_shader_variant
*so
= ctx
->so
;
1532 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
1533 unsigned ncomp
= glsl_get_components(out
->type
);
1534 /* XXX: map loc slots to semantics */
1535 unsigned semantic_name
= out
->data
.location
;
1536 unsigned semantic_index
= out
->data
.index
;
1537 unsigned n
= out
->data
.driver_location
;
1540 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
1541 semantic_name
, semantic_index
, array_len
,
1544 if (ctx
->so
->type
== SHADER_VERTEX
) {
1545 switch (semantic_name
) {
1546 case TGSI_SEMANTIC_POSITION
:
1547 so
->writes_pos
= true;
1549 case TGSI_SEMANTIC_PSIZE
:
1550 so
->writes_psize
= true;
1552 case TGSI_SEMANTIC_COLOR
:
1553 case TGSI_SEMANTIC_BCOLOR
:
1554 case TGSI_SEMANTIC_GENERIC
:
1555 case TGSI_SEMANTIC_FOG
:
1556 case TGSI_SEMANTIC_TEXCOORD
:
1559 compile_error(ctx
, "unknown VS semantic name: %s\n",
1560 tgsi_semantic_names
[semantic_name
]);
1563 switch (semantic_name
) {
1564 case TGSI_SEMANTIC_POSITION
:
1565 comp
= 2; /* tgsi will write to .z component */
1566 so
->writes_pos
= true;
1568 case TGSI_SEMANTIC_COLOR
:
1571 compile_error(ctx
, "unknown FS semantic name: %s\n",
1572 tgsi_semantic_names
[semantic_name
]);
1576 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
1578 so
->outputs
[n
].semantic
=
1579 ir3_semantic_name(semantic_name
, semantic_index
);
1580 so
->outputs
[n
].regid
= regid(n
, comp
);
1581 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
1583 for (int i
= 0; i
< ncomp
; i
++) {
1584 unsigned idx
= (n
* 4) + i
;
1586 ctx
->block
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
1591 emit_instructions(struct ir3_compile
*ctx
)
1593 unsigned ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
1594 unsigned noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
1596 /* we need to allocate big enough outputs array so that
1597 * we can stuff the kill's at the end:
1599 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1600 noutputs
+= ARRAY_SIZE(ctx
->kill
);
1602 ctx
->block
= ir3_block_create(ctx
->ir
, 0, ninputs
, noutputs
);
1604 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1605 ctx
->block
->noutputs
-= ARRAY_SIZE(ctx
->kill
);
1608 /* for fragment shader, we have a single input register (usually
1609 * r0.xy) which is used as the base for bary.f varying fetch instrs:
1611 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1612 // TODO maybe a helper for fi since we need it a few places..
1613 struct ir3_instruction
*instr
;
1614 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
1615 ir3_reg_create(instr
, 0, 0);
1616 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
1617 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
1618 ctx
->frag_pos
= instr
;
1622 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->inputs
) {
1623 setup_input(ctx
, var
);
1626 /* Setup outputs: */
1627 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->outputs
) {
1628 setup_output(ctx
, var
);
1631 /* Setup variables (which should only be arrays): */
1632 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->globals
) {
1633 declare_var(ctx
, var
);
1636 /* Find the main function and emit the body: */
1637 nir_foreach_overload(ctx
->s
, overload
) {
1638 compile_assert(ctx
, strcmp(overload
->function
->name
, "main") == 0);
1639 compile_assert(ctx
, overload
->impl
);
1640 emit_function(ctx
, overload
->impl
);
1646 /* from NIR perspective, we actually have inputs. But most of the "inputs"
1647 * for a fragment shader are just bary.f instructions. The *actual* inputs
1648 * from the hw perspective are the frag_pos and optionally frag_coord and
1652 fixup_frag_inputs(struct ir3_compile
*ctx
)
1654 struct ir3_shader_variant
*so
= ctx
->so
;
1655 struct ir3_block
*block
= ctx
->block
;
1656 struct ir3_instruction
**inputs
;
1657 struct ir3_instruction
*instr
;
1662 n
= 4; /* always have frag_pos */
1663 n
+= COND(so
->frag_face
, 4);
1664 n
+= COND(so
->frag_coord
, 4);
1666 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
1668 if (so
->frag_face
) {
1669 /* this ultimately gets assigned to hr0.x so doesn't conflict
1670 * with frag_coord/frag_pos..
1672 inputs
[block
->ninputs
++] = ctx
->frag_face
;
1673 ctx
->frag_face
->regs
[0]->num
= 0;
1675 /* remaining channels not used, but let's avoid confusing
1676 * other parts that expect inputs to come in groups of vec4
1678 inputs
[block
->ninputs
++] = NULL
;
1679 inputs
[block
->ninputs
++] = NULL
;
1680 inputs
[block
->ninputs
++] = NULL
;
1683 /* since we don't know where to set the regid for frag_coord,
1684 * we have to use r0.x for it. But we don't want to *always*
1685 * use r1.x for frag_pos as that could increase the register
1686 * footprint on simple shaders:
1688 if (so
->frag_coord
) {
1689 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
1690 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
1691 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
1692 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
1694 inputs
[block
->ninputs
++] = ctx
->frag_coord
[0];
1695 inputs
[block
->ninputs
++] = ctx
->frag_coord
[1];
1696 inputs
[block
->ninputs
++] = ctx
->frag_coord
[2];
1697 inputs
[block
->ninputs
++] = ctx
->frag_coord
[3];
1700 /* we always have frag_pos: */
1701 so
->pos_regid
= regid
;
1704 instr
= create_input(block
, NULL
, block
->ninputs
);
1705 instr
->regs
[0]->num
= regid
++;
1706 inputs
[block
->ninputs
++] = instr
;
1707 ctx
->frag_pos
->regs
[1]->instr
= instr
;
1710 instr
= create_input(block
, NULL
, block
->ninputs
);
1711 instr
->regs
[0]->num
= regid
++;
1712 inputs
[block
->ninputs
++] = instr
;
1713 ctx
->frag_pos
->regs
[2]->instr
= instr
;
1715 block
->inputs
= inputs
;
1719 compile_dump(struct ir3_compile
*ctx
)
1721 const char *name
= (ctx
->so
->type
== SHADER_VERTEX
) ? "vert" : "frag";
1722 static unsigned n
= 0;
1725 snprintf(fname
, sizeof(fname
), "%s-%04u.dot", name
, n
++);
1726 f
= fopen(fname
, "w");
1729 ir3_block_depth(ctx
->block
);
1730 ir3_dump(ctx
->ir
, name
, ctx
->block
, f
);
1735 ir3_compile_shader_nir(struct ir3_shader_variant
*so
,
1736 const struct tgsi_token
*tokens
, struct ir3_shader_key key
)
1738 struct ir3_compile
*ctx
;
1739 struct ir3_block
*block
;
1740 struct ir3_instruction
**inputs
;
1741 unsigned i
, j
, actual_in
;
1742 int ret
= 0, max_bary
;
1746 so
->ir
= ir3_create();
1750 ctx
= compile_init(so
, tokens
);
1752 DBG("INIT failed!");
1757 emit_instructions(ctx
);
1760 DBG("EMIT failed!");
1766 so
->ir
->block
= block
;
1768 /* keep track of the inputs from TGSI perspective.. */
1769 inputs
= block
->inputs
;
1771 /* but fixup actual inputs for frag shader: */
1772 if (so
->type
== SHADER_FRAGMENT
)
1773 fixup_frag_inputs(ctx
);
1775 /* at this point, for binning pass, throw away unneeded outputs: */
1776 if (key
.binning_pass
) {
1777 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
1778 unsigned name
= sem2name(so
->outputs
[i
].semantic
);
1779 unsigned idx
= sem2idx(so
->outputs
[i
].semantic
);
1781 /* throw away everything but first position/psize */
1782 if ((idx
== 0) && ((name
== TGSI_SEMANTIC_POSITION
) ||
1783 (name
== TGSI_SEMANTIC_PSIZE
))) {
1785 so
->outputs
[j
] = so
->outputs
[i
];
1786 block
->outputs
[(j
*4)+0] = block
->outputs
[(i
*4)+0];
1787 block
->outputs
[(j
*4)+1] = block
->outputs
[(i
*4)+1];
1788 block
->outputs
[(j
*4)+2] = block
->outputs
[(i
*4)+2];
1789 block
->outputs
[(j
*4)+3] = block
->outputs
[(i
*4)+3];
1794 so
->outputs_count
= j
;
1795 block
->noutputs
= j
* 4;
1798 /* if we want half-precision outputs, mark the output registers
1801 if (key
.half_precision
) {
1802 for (i
= 0; i
< block
->noutputs
; i
++) {
1803 if (!block
->outputs
[i
])
1805 block
->outputs
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
1809 /* at this point, we want the kill's in the outputs array too,
1810 * so that they get scheduled (since they have no dst).. we've
1811 * already ensured that the array is big enough in push_block():
1813 if (so
->type
== SHADER_FRAGMENT
) {
1814 for (i
= 0; i
< ctx
->kill_count
; i
++)
1815 block
->outputs
[block
->noutputs
++] = ctx
->kill
[i
];
1818 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1821 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1822 printf("BEFORE CP:\n");
1823 ir3_dump_instr_list(block
->head
);
1826 ir3_block_depth(block
);
1828 ir3_block_cp(block
);
1830 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1831 printf("BEFORE GROUPING:\n");
1832 ir3_dump_instr_list(block
->head
);
1835 /* Group left/right neighbors, inserting mov's where needed to
1838 ir3_block_group(block
);
1840 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1843 ir3_block_depth(block
);
1845 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1846 printf("AFTER DEPTH:\n");
1847 ir3_dump_instr_list(block
->head
);
1850 ret
= ir3_block_sched(block
);
1852 DBG("SCHED failed!");
1856 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1857 printf("AFTER SCHED:\n");
1858 ir3_dump_instr_list(block
->head
);
1861 ret
= ir3_block_ra(block
, so
->type
, so
->frag_coord
, so
->frag_face
);
1867 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1868 printf("AFTER RA:\n");
1869 ir3_dump_instr_list(block
->head
);
1872 ir3_block_legalize(block
, &so
->has_samp
, &max_bary
);
1874 /* fixup input/outputs: */
1875 for (i
= 0; i
< so
->outputs_count
; i
++) {
1876 so
->outputs
[i
].regid
= block
->outputs
[i
*4]->regs
[0]->num
;
1877 /* preserve hack for depth output.. tgsi writes depth to .z,
1878 * but what we give the hw is the scalar register:
1880 if ((so
->type
== SHADER_FRAGMENT
) &&
1881 (sem2name(so
->outputs
[i
].semantic
) == TGSI_SEMANTIC_POSITION
))
1882 so
->outputs
[i
].regid
+= 2;
1885 /* Note that some or all channels of an input may be unused: */
1887 for (i
= 0; i
< so
->inputs_count
; i
++) {
1888 unsigned j
, regid
= ~0, compmask
= 0;
1889 so
->inputs
[i
].ncomp
= 0;
1890 for (j
= 0; j
< 4; j
++) {
1891 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
1893 compmask
|= (1 << j
);
1894 regid
= in
->regs
[0]->num
- j
;
1896 so
->inputs
[i
].ncomp
++;
1899 so
->inputs
[i
].regid
= regid
;
1900 so
->inputs
[i
].compmask
= compmask
;
1903 /* fragment shader always gets full vec4's even if it doesn't
1904 * fetch all components, but vertex shader we need to update
1905 * with the actual number of components fetch, otherwise thing
1906 * will hang due to mismaptch between VFD_DECODE's and
1909 if (so
->type
== SHADER_VERTEX
)
1910 so
->total_in
= actual_in
;
1912 so
->total_in
= align(max_bary
+ 1, 4);
1916 ir3_destroy(so
->ir
);