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));
610 * Adreno uses uint rather than having dedicated bool type,
611 * which (potentially) requires some conversion, in particular
612 * when using output of an bool instr to int input, or visa
616 * -------+---------+-------+-
620 * To convert from an adreno bool (uint) to nir, use:
622 * absneg.s dst, (neg)src
624 * To convert back in the other direction:
626 * absneg.s dst, (abs)arc
628 * The CP step can clean up the absneg.s that cancel each other
629 * out, and with a slight bit of extra cleverness (to recognize
630 * the instructions which produce either a 0 or 1) can eliminate
631 * the absneg.s's completely when an instruction that wants
632 * 0/1 consumes the result. For example, when a nir 'bcsel'
633 * consumes the result of 'feq'. So we should be able to get by
634 * without a boolean resolve step, and without incuring any
635 * extra penalty in instruction count.
638 /* NIR bool -> native (adreno): */
639 static struct ir3_instruction
*
640 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
642 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
645 /* native (adreno) -> NIR bool: */
646 static struct ir3_instruction
*
647 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
649 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
653 * alu/sfu instructions:
657 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
659 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
660 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
661 struct ir3_block
*b
= ctx
->block
;
663 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
665 /* Vectors are special in that they have non-scalarized writemasks,
666 * and just take the first swizzle channel for each argument in
667 * order into each writemask channel.
669 if ((alu
->op
== nir_op_vec2
) ||
670 (alu
->op
== nir_op_vec3
) ||
671 (alu
->op
== nir_op_vec4
)) {
673 for (int i
= 0; i
< info
->num_inputs
; i
++) {
674 nir_alu_src
*asrc
= &alu
->src
[i
];
676 compile_assert(ctx
, !asrc
->abs
);
677 compile_assert(ctx
, !asrc
->negate
);
679 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
681 src
[i
] = create_immed(ctx
->block
, 0);
682 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
688 /* General case: We can just grab the one used channel per src. */
689 for (int i
= 0; i
< info
->num_inputs
; i
++) {
690 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
691 nir_alu_src
*asrc
= &alu
->src
[i
];
693 compile_assert(ctx
, !asrc
->abs
);
694 compile_assert(ctx
, !asrc
->negate
);
696 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
701 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
704 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
707 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
710 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
713 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
716 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
719 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
720 dst
[0]->cat2
.condition
= IR3_COND_NE
;
721 dst
[0] = ir3_n2b(b
, dst
[0]);
724 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
727 dst
[0] = ir3_b2n(b
, src
[0]);
730 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
731 dst
[0]->cat2
.condition
= IR3_COND_NE
;
732 dst
[0] = ir3_n2b(b
, dst
[0]);
736 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
739 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
742 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
745 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
748 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
751 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
754 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
757 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
760 dst
[0] = ir3_DSX(b
, src
[0], 0);
761 dst
[0]->cat5
.type
= TYPE_F32
;
764 dst
[0] = ir3_DSY(b
, src
[0], 0);
765 dst
[0]->cat5
.type
= TYPE_F32
;
769 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
770 dst
[0]->cat2
.condition
= IR3_COND_LT
;
771 dst
[0] = ir3_n2b(b
, dst
[0]);
774 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
775 dst
[0]->cat2
.condition
= IR3_COND_GE
;
776 dst
[0] = ir3_n2b(b
, dst
[0]);
779 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
780 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
781 dst
[0] = ir3_n2b(b
, dst
[0]);
784 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
785 dst
[0]->cat2
.condition
= IR3_COND_NE
;
786 dst
[0] = ir3_n2b(b
, dst
[0]);
789 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
792 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
795 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
797 case nir_op_fround_even
:
798 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
801 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
805 dst
[0] = ir3_SIN(b
, src
[0], 0);
808 dst
[0] = ir3_COS(b
, src
[0], 0);
811 dst
[0] = ir3_RSQ(b
, src
[0], 0);
814 dst
[0] = ir3_RCP(b
, src
[0], 0);
817 dst
[0] = ir3_LOG2(b
, src
[0], 0);
820 dst
[0] = ir3_EXP2(b
, src
[0], 0);
823 dst
[0] = ir3_SQRT(b
, src
[0], 0);
827 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
830 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
833 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
836 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
839 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
843 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
844 * mull.u tmp0, a, b ; mul low, i.e. al * bl
845 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
846 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
848 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
849 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
850 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
853 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
856 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
859 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
862 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
865 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
868 /* maybe this would be sane to lower in nir.. */
869 struct ir3_instruction
*neg
, *pos
;
871 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
872 neg
->cat2
.condition
= IR3_COND_LT
;
874 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
875 pos
->cat2
.condition
= IR3_COND_GT
;
877 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
882 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
885 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
888 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
891 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
892 dst
[0]->cat2
.condition
= IR3_COND_LT
;
893 dst
[0] = ir3_n2b(b
, dst
[0]);
896 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
897 dst
[0]->cat2
.condition
= IR3_COND_GE
;
898 dst
[0] = ir3_n2b(b
, dst
[0]);
901 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
902 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
903 dst
[0] = ir3_n2b(b
, dst
[0]);
906 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
907 dst
[0]->cat2
.condition
= IR3_COND_NE
;
908 dst
[0] = ir3_n2b(b
, dst
[0]);
911 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
912 dst
[0]->cat2
.condition
= IR3_COND_LT
;
913 dst
[0] = ir3_n2b(b
, dst
[0]);
916 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
917 dst
[0]->cat2
.condition
= IR3_COND_GE
;
918 dst
[0] = ir3_n2b(b
, dst
[0]);
922 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
926 compile_error(ctx
, "Unhandled ALU op: %s\n",
927 nir_op_infos
[alu
->op
].name
);
932 /* handles array reads: */
934 emit_intrinisic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
935 struct ir3_instruction
**dst
)
937 nir_deref_var
*dvar
= intr
->variables
[0];
938 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
939 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
941 compile_assert(ctx
, dvar
->deref
.child
&&
942 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
944 switch (darr
->deref_array_type
) {
945 case nir_deref_array_type_direct
:
946 /* direct access does not require anything special: */
947 for (int i
= 0; i
< intr
->num_components
; i
++) {
948 unsigned n
= darr
->base_offset
* 4 + i
;
949 compile_assert(ctx
, n
< arr
->length
);
950 dst
[i
] = arr
->arr
[n
];
953 case nir_deref_array_type_indirect
: {
954 /* for indirect, we need to collect all the array elements: */
955 struct ir3_instruction
*collect
=
956 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
957 struct ir3_instruction
*addr
=
958 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
959 for (int i
= 0; i
< intr
->num_components
; i
++) {
960 unsigned n
= darr
->base_offset
* 4 + i
;
961 compile_assert(ctx
, n
< arr
->length
);
962 dst
[i
] = create_indirect_load(ctx
, arr
->length
, n
, addr
, collect
);
967 compile_error(ctx
, "Unhandled load deref type: %u\n",
968 darr
->deref_array_type
);
973 /* handles array writes: */
975 emit_intrinisic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
977 nir_deref_var
*dvar
= intr
->variables
[0];
978 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
979 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
980 struct ir3_instruction
**src
;
982 compile_assert(ctx
, dvar
->deref
.child
&&
983 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
985 src
= get_src(ctx
, &intr
->src
[0]);
987 switch (darr
->deref_array_type
) {
988 case nir_deref_array_type_direct
:
989 /* direct access does not require anything special: */
990 for (int i
= 0; i
< intr
->num_components
; i
++) {
991 unsigned n
= darr
->base_offset
* 4 + i
;
992 compile_assert(ctx
, n
< arr
->length
);
993 arr
->arr
[n
] = src
[i
];
996 case nir_deref_array_type_indirect
: {
997 /* for indirect, create indirect-store and fan that out: */
998 struct ir3_instruction
*collect
=
999 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1000 struct ir3_instruction
*addr
=
1001 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1002 for (int i
= 0; i
< intr
->num_components
; i
++) {
1003 struct ir3_instruction
*store
;
1004 unsigned n
= darr
->base_offset
* 4 + i
;
1005 compile_assert(ctx
, n
< arr
->length
);
1007 store
= create_indirect_store(ctx
, arr
->length
,
1008 n
, src
[i
], addr
, collect
);
1010 store
->fanin
->fi
.aid
= arr
->aid
;
1012 /* TODO: probably split this out to be used for
1013 * store_output_indirect? or move this into
1014 * create_indirect_store()?
1016 for (int j
= i
; j
< arr
->length
; j
+= 4) {
1017 struct ir3_instruction
*split
;
1019 split
= ir3_instr_create(ctx
->block
, -1, OPC_META_FO
);
1021 ir3_reg_create(split
, 0, 0);
1022 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= store
;
1024 arr
->arr
[j
] = split
;
1030 compile_error(ctx
, "Unhandled store deref type: %u\n",
1031 darr
->deref_array_type
);
1037 emit_intrinisic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1039 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1040 struct ir3_instruction
**dst
, **src
;
1041 struct ir3_block
*b
= ctx
->block
;
1042 unsigned idx
= intr
->const_index
[0];
1044 if (info
->has_dest
) {
1045 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1048 switch (intr
->intrinsic
) {
1049 case nir_intrinsic_load_uniform
:
1050 compile_assert(ctx
, intr
->const_index
[1] == 1);
1051 for (int i
= 0; i
< intr
->num_components
; i
++) {
1052 unsigned n
= idx
* 4 + i
;
1053 dst
[i
] = create_uniform(ctx
, n
);
1056 case nir_intrinsic_load_uniform_indirect
:
1057 compile_assert(ctx
, intr
->const_index
[1] == 1);
1058 src
= get_src(ctx
, &intr
->src
[0]);
1059 for (int i
= 0; i
< intr
->num_components
; i
++) {
1060 unsigned n
= idx
* 4 + i
;
1061 dst
[i
] = create_uniform_indirect(ctx
, n
,
1062 get_addr(ctx
, src
[0]));
1065 case nir_intrinsic_load_input
:
1066 compile_assert(ctx
, intr
->const_index
[1] == 1);
1067 for (int i
= 0; i
< intr
->num_components
; i
++) {
1068 unsigned n
= idx
* 4 + i
;
1069 dst
[i
] = b
->inputs
[n
];
1072 case nir_intrinsic_load_input_indirect
:
1073 compile_assert(ctx
, intr
->const_index
[1] == 1);
1074 src
= get_src(ctx
, &intr
->src
[0]);
1075 struct ir3_instruction
*collect
=
1076 create_collect(b
, b
->inputs
, b
->ninputs
);
1077 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1078 for (int i
= 0; i
< intr
->num_components
; i
++) {
1079 unsigned n
= idx
* 4 + i
;
1080 dst
[i
] = create_indirect_load(ctx
, b
->ninputs
, n
, addr
, collect
);
1083 case nir_intrinsic_load_var
:
1084 emit_intrinisic_load_var(ctx
, intr
, dst
);
1086 case nir_intrinsic_store_var
:
1087 emit_intrinisic_store_var(ctx
, intr
);
1089 case nir_intrinsic_store_output
:
1090 compile_assert(ctx
, intr
->const_index
[1] == 1);
1091 src
= get_src(ctx
, &intr
->src
[0]);
1092 for (int i
= 0; i
< intr
->num_components
; i
++) {
1093 unsigned n
= idx
* 4 + i
;
1094 b
->outputs
[n
] = src
[i
];
1097 case nir_intrinsic_discard_if
:
1098 case nir_intrinsic_discard
: {
1099 struct ir3_instruction
*cond
, *kill
;
1101 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1102 /* conditional discard: */
1103 src
= get_src(ctx
, &intr
->src
[0]);
1104 cond
= ir3_b2n(b
, src
[0]);
1106 /* unconditional discard: */
1107 cond
= create_immed(b
, 1);
1110 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1111 cond
->cat2
.condition
= IR3_COND_NE
;
1113 /* condition always goes in predicate register: */
1114 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1116 kill
= ir3_KILL(b
, cond
, 0);
1118 ctx
->kill
[ctx
->kill_count
++] = kill
;
1119 ctx
->so
->has_kill
= true;
1124 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1125 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1131 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1133 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1134 instr
->def
.num_components
);
1135 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1136 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u
[i
]);
1140 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1142 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1143 undef
->def
.num_components
);
1144 /* backend doesn't want undefined instructions, so just plug
1147 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1148 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1152 * texture fetch/sample instructions:
1156 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1158 struct ir3_block
*b
= ctx
->block
;
1159 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1160 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1161 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1162 unsigned i
, coords
, flags
= 0;
1163 unsigned nsrc0
= 0, nsrc1
= 0;
1167 /* TODO: might just be one component for gathers? */
1168 dst
= get_dst(ctx
, &tex
->dest
, 4);
1170 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1171 switch (tex
->src
[i
].src_type
) {
1172 case nir_tex_src_coord
:
1173 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1175 case nir_tex_src_bias
:
1176 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1179 case nir_tex_src_lod
:
1180 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1183 case nir_tex_src_comparitor
: /* shadow comparator */
1184 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1186 case nir_tex_src_projector
:
1187 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1190 case nir_tex_src_offset
:
1191 off
= get_src(ctx
, &tex
->src
[i
].src
);
1194 case nir_tex_src_ddx
:
1195 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1197 case nir_tex_src_ddy
:
1198 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1201 compile_error(ctx
, "Unhandled NIR tex serc type: %d\n",
1202 tex
->src
[i
].src_type
);
1208 * lay out the first argument in the proper order:
1209 * - actual coordinates first
1210 * - shadow reference
1213 * - starting at offset 4, dpdx.xy, dpdy.xy
1215 * bias/lod go into the second arg
1218 coords
= tex
->coord_components
;
1219 if (tex
->is_array
) /* array idx goes after shadow ref */
1222 /* insert tex coords: */
1223 for (i
= 0; i
< coords
; i
++)
1224 src0
[nsrc0
++] = coord
[i
];
1227 /* hw doesn't do 1d, so we treat it as 2d with
1228 * height of 1, and patch up the y coord.
1229 * TODO: y coord should be (int)0 in some cases..
1231 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1232 } else if (coords
== 3) {
1233 flags
|= IR3_INSTR_3D
;
1236 if (tex
->is_shadow
) {
1237 src0
[nsrc0
++] = compare
;
1238 flags
|= IR3_INSTR_S
;
1241 if (tex
->is_array
) {
1242 src0
[nsrc0
++] = coord
[coords
];
1243 flags
|= IR3_INSTR_A
;
1247 src0
[nsrc0
++] = proj
;
1248 flags
|= IR3_INSTR_P
;
1251 /* pad to 4, then ddx/ddy: */
1252 if (tex
->op
== nir_texop_txd
) {
1254 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1255 for (i
= 0; i
< coords
; i
++)
1256 src0
[nsrc0
++] = ddx
[i
];
1258 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1259 for (i
= 0; i
< coords
; i
++)
1260 src0
[nsrc0
++] = ddy
[i
];
1262 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1266 * second argument (if applicable):
1271 if (has_off
| has_lod
| has_bias
) {
1273 for (i
= 0; i
< coords
; i
++)
1274 src1
[nsrc1
++] = off
[i
];
1276 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1277 flags
|= IR3_INSTR_O
;
1280 if (has_lod
| has_bias
)
1281 src1
[nsrc1
++] = lod
;
1285 case nir_texop_tex
: opc
= OPC_SAM
; break;
1286 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1287 case nir_texop_txl
: opc
= OPC_SAML
; break;
1288 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1289 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1290 case nir_texop_txf_ms
:
1294 case nir_texop_query_levels
:
1295 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1299 switch (tex
->dest_type
) {
1300 case nir_type_invalid
:
1301 case nir_type_float
:
1307 case nir_type_unsigned
:
1313 sam
= ir3_SAM(b
, opc
, type
, 0xf, flags
,
1314 tex
->sampler_index
, tex
->sampler_index
,
1315 create_collect(b
, src0
, nsrc0
),
1316 create_collect(b
, src1
, nsrc1
));
1318 // TODO maybe split this out into a helper, for other cases that
1320 struct ir3_instruction
*prev
= NULL
;
1321 for (int i
= 0; i
< 4; i
++) {
1322 struct ir3_instruction
*split
=
1323 ir3_instr_create(b
, -1, OPC_META_FO
);
1324 ir3_reg_create(split
, 0, IR3_REG_SSA
);
1325 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= sam
;
1329 split
->cp
.left
= prev
;
1330 split
->cp
.left_cnt
++;
1331 prev
->cp
.right
= split
;
1332 prev
->cp
.right_cnt
++;
1342 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1344 switch (instr
->type
) {
1345 case nir_instr_type_alu
:
1346 emit_alu(ctx
, nir_instr_as_alu(instr
));
1348 case nir_instr_type_intrinsic
:
1349 emit_intrinisic(ctx
, nir_instr_as_intrinsic(instr
));
1351 case nir_instr_type_load_const
:
1352 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1354 case nir_instr_type_ssa_undef
:
1355 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1357 case nir_instr_type_tex
:
1358 emit_tex(ctx
, nir_instr_as_tex(instr
));
1361 case nir_instr_type_call
:
1362 case nir_instr_type_jump
:
1363 case nir_instr_type_phi
:
1364 case nir_instr_type_parallel_copy
:
1365 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1371 emit_block(struct ir3_compile
*ctx
, nir_block
*block
)
1373 nir_foreach_instr(block
, instr
) {
1374 emit_instr(ctx
, instr
);
1381 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1383 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1384 switch (node
->type
) {
1385 case nir_cf_node_block
:
1386 emit_block(ctx
, nir_cf_node_as_block(node
));
1388 case nir_cf_node_if
:
1389 case nir_cf_node_loop
:
1390 case nir_cf_node_function
:
1391 compile_error(ctx
, "TODO\n");
1400 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1402 struct ir3_shader_variant
*so
= ctx
->so
;
1403 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1404 unsigned ncomp
= glsl_get_components(in
->type
);
1405 /* XXX: map loc slots to semantics */
1406 unsigned semantic_name
= in
->data
.location
;
1407 unsigned semantic_index
= in
->data
.index
;
1408 unsigned n
= in
->data
.driver_location
;
1410 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1411 semantic_name
, semantic_index
, array_len
,
1414 so
->inputs
[n
].semantic
=
1415 ir3_semantic_name(semantic_name
, semantic_index
);
1416 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1417 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1418 so
->inputs
[n
].interpolate
= 0;
1419 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1421 /* the fdN_program_emit() code expects tgsi consts here, so map
1422 * things back to tgsi for now:
1424 switch (in
->data
.interpolation
) {
1425 case INTERP_QUALIFIER_FLAT
:
1426 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1428 case INTERP_QUALIFIER_NOPERSPECTIVE
:
1429 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_LINEAR
;
1431 case INTERP_QUALIFIER_SMOOTH
:
1432 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
1436 for (int i
= 0; i
< ncomp
; i
++) {
1437 struct ir3_instruction
*instr
= NULL
;
1438 unsigned idx
= (n
* 4) + i
;
1440 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1441 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1442 so
->inputs
[n
].bary
= false;
1443 so
->frag_coord
= true;
1444 instr
= create_frag_coord(ctx
, i
);
1445 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1446 so
->inputs
[n
].bary
= false;
1447 so
->frag_face
= true;
1448 instr
= create_frag_face(ctx
, i
);
1450 bool use_ldlv
= false;
1452 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1453 * from the semantic name:
1455 if (semantic_name
== TGSI_SEMANTIC_COLOR
)
1456 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_COLOR
;
1458 if (ctx
->flat_bypass
) {
1459 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1460 * from the semantic name:
1462 switch (so
->inputs
[n
].interpolate
) {
1463 case TGSI_INTERPOLATE_COLOR
:
1464 if (!ctx
->so
->key
.rasterflat
)
1467 case TGSI_INTERPOLATE_CONSTANT
:
1473 so
->inputs
[n
].bary
= true;
1475 instr
= create_frag_input(ctx
, idx
, use_ldlv
);
1478 instr
= create_input(ctx
->block
, NULL
, idx
);
1481 ctx
->block
->inputs
[idx
] = instr
;
1484 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
1485 ctx
->next_inloc
+= ncomp
;
1486 so
->total_in
+= ncomp
;
1491 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
1493 struct ir3_shader_variant
*so
= ctx
->so
;
1494 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
1495 unsigned ncomp
= glsl_get_components(out
->type
);
1496 /* XXX: map loc slots to semantics */
1497 unsigned semantic_name
= out
->data
.location
;
1498 unsigned semantic_index
= out
->data
.index
;
1499 unsigned n
= out
->data
.driver_location
;
1502 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
1503 semantic_name
, semantic_index
, array_len
,
1506 if (ctx
->so
->type
== SHADER_VERTEX
) {
1507 switch (semantic_name
) {
1508 case TGSI_SEMANTIC_POSITION
:
1509 so
->writes_pos
= true;
1511 case TGSI_SEMANTIC_PSIZE
:
1512 so
->writes_psize
= true;
1514 case TGSI_SEMANTIC_COLOR
:
1515 case TGSI_SEMANTIC_BCOLOR
:
1516 case TGSI_SEMANTIC_GENERIC
:
1517 case TGSI_SEMANTIC_FOG
:
1518 case TGSI_SEMANTIC_TEXCOORD
:
1521 compile_error(ctx
, "unknown VS semantic name: %s\n",
1522 tgsi_semantic_names
[semantic_name
]);
1525 switch (semantic_name
) {
1526 case TGSI_SEMANTIC_POSITION
:
1527 comp
= 2; /* tgsi will write to .z component */
1528 so
->writes_pos
= true;
1530 case TGSI_SEMANTIC_COLOR
:
1533 compile_error(ctx
, "unknown FS semantic name: %s\n",
1534 tgsi_semantic_names
[semantic_name
]);
1538 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
1540 so
->outputs
[n
].semantic
=
1541 ir3_semantic_name(semantic_name
, semantic_index
);
1542 so
->outputs
[n
].regid
= regid(n
, comp
);
1543 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
1545 for (int i
= 0; i
< ncomp
; i
++) {
1546 unsigned idx
= (n
* 4) + i
;
1548 ctx
->block
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
1553 emit_instructions(struct ir3_compile
*ctx
)
1555 unsigned ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
1556 unsigned noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
1558 /* we need to allocate big enough outputs array so that
1559 * we can stuff the kill's at the end:
1561 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1562 noutputs
+= ARRAY_SIZE(ctx
->kill
);
1564 ctx
->block
= ir3_block_create(ctx
->ir
, 0, ninputs
, noutputs
);
1566 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1567 ctx
->block
->noutputs
-= ARRAY_SIZE(ctx
->kill
);
1570 /* for fragment shader, we have a single input register (usually
1571 * r0.xy) which is used as the base for bary.f varying fetch instrs:
1573 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1574 // TODO maybe a helper for fi since we need it a few places..
1575 struct ir3_instruction
*instr
;
1576 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
1577 ir3_reg_create(instr
, 0, 0);
1578 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
1579 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
1580 ctx
->frag_pos
= instr
;
1584 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->inputs
) {
1585 setup_input(ctx
, var
);
1588 /* Setup outputs: */
1589 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->outputs
) {
1590 setup_output(ctx
, var
);
1593 /* Setup variables (which should only be arrays): */
1594 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->globals
) {
1595 declare_var(ctx
, var
);
1598 /* Find the main function and emit the body: */
1599 nir_foreach_overload(ctx
->s
, overload
) {
1600 compile_assert(ctx
, strcmp(overload
->function
->name
, "main") == 0);
1601 compile_assert(ctx
, overload
->impl
);
1602 emit_function(ctx
, overload
->impl
);
1608 /* from NIR perspective, we actually have inputs. But most of the "inputs"
1609 * for a fragment shader are just bary.f instructions. The *actual* inputs
1610 * from the hw perspective are the frag_pos and optionally frag_coord and
1614 fixup_frag_inputs(struct ir3_compile
*ctx
)
1616 struct ir3_shader_variant
*so
= ctx
->so
;
1617 struct ir3_block
*block
= ctx
->block
;
1618 struct ir3_instruction
**inputs
;
1619 struct ir3_instruction
*instr
;
1624 n
= 4; /* always have frag_pos */
1625 n
+= COND(so
->frag_face
, 4);
1626 n
+= COND(so
->frag_coord
, 4);
1628 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
1630 if (so
->frag_face
) {
1631 /* this ultimately gets assigned to hr0.x so doesn't conflict
1632 * with frag_coord/frag_pos..
1634 inputs
[block
->ninputs
++] = ctx
->frag_face
;
1635 ctx
->frag_face
->regs
[0]->num
= 0;
1637 /* remaining channels not used, but let's avoid confusing
1638 * other parts that expect inputs to come in groups of vec4
1640 inputs
[block
->ninputs
++] = NULL
;
1641 inputs
[block
->ninputs
++] = NULL
;
1642 inputs
[block
->ninputs
++] = NULL
;
1645 /* since we don't know where to set the regid for frag_coord,
1646 * we have to use r0.x for it. But we don't want to *always*
1647 * use r1.x for frag_pos as that could increase the register
1648 * footprint on simple shaders:
1650 if (so
->frag_coord
) {
1651 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
1652 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
1653 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
1654 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
1656 inputs
[block
->ninputs
++] = ctx
->frag_coord
[0];
1657 inputs
[block
->ninputs
++] = ctx
->frag_coord
[1];
1658 inputs
[block
->ninputs
++] = ctx
->frag_coord
[2];
1659 inputs
[block
->ninputs
++] = ctx
->frag_coord
[3];
1662 /* we always have frag_pos: */
1663 so
->pos_regid
= regid
;
1666 instr
= create_input(block
, NULL
, block
->ninputs
);
1667 instr
->regs
[0]->num
= regid
++;
1668 inputs
[block
->ninputs
++] = instr
;
1669 ctx
->frag_pos
->regs
[1]->instr
= instr
;
1672 instr
= create_input(block
, NULL
, block
->ninputs
);
1673 instr
->regs
[0]->num
= regid
++;
1674 inputs
[block
->ninputs
++] = instr
;
1675 ctx
->frag_pos
->regs
[2]->instr
= instr
;
1677 block
->inputs
= inputs
;
1681 compile_dump(struct ir3_compile
*ctx
)
1683 const char *name
= (ctx
->so
->type
== SHADER_VERTEX
) ? "vert" : "frag";
1684 static unsigned n
= 0;
1687 snprintf(fname
, sizeof(fname
), "%s-%04u.dot", name
, n
++);
1688 f
= fopen(fname
, "w");
1691 ir3_block_depth(ctx
->block
);
1692 ir3_dump(ctx
->ir
, name
, ctx
->block
, f
);
1697 ir3_compile_shader_nir(struct ir3_shader_variant
*so
,
1698 const struct tgsi_token
*tokens
, struct ir3_shader_key key
)
1700 struct ir3_compile
*ctx
;
1701 struct ir3_block
*block
;
1702 struct ir3_instruction
**inputs
;
1703 unsigned i
, j
, actual_in
;
1704 int ret
= 0, max_bary
;
1708 so
->ir
= ir3_create();
1712 ctx
= compile_init(so
, tokens
);
1714 DBG("INIT failed!");
1719 emit_instructions(ctx
);
1722 DBG("EMIT failed!");
1728 so
->ir
->block
= block
;
1730 /* keep track of the inputs from TGSI perspective.. */
1731 inputs
= block
->inputs
;
1733 /* but fixup actual inputs for frag shader: */
1734 if (so
->type
== SHADER_FRAGMENT
)
1735 fixup_frag_inputs(ctx
);
1737 /* at this point, for binning pass, throw away unneeded outputs: */
1738 if (key
.binning_pass
) {
1739 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
1740 unsigned name
= sem2name(so
->outputs
[i
].semantic
);
1741 unsigned idx
= sem2idx(so
->outputs
[i
].semantic
);
1743 /* throw away everything but first position/psize */
1744 if ((idx
== 0) && ((name
== TGSI_SEMANTIC_POSITION
) ||
1745 (name
== TGSI_SEMANTIC_PSIZE
))) {
1747 so
->outputs
[j
] = so
->outputs
[i
];
1748 block
->outputs
[(j
*4)+0] = block
->outputs
[(i
*4)+0];
1749 block
->outputs
[(j
*4)+1] = block
->outputs
[(i
*4)+1];
1750 block
->outputs
[(j
*4)+2] = block
->outputs
[(i
*4)+2];
1751 block
->outputs
[(j
*4)+3] = block
->outputs
[(i
*4)+3];
1756 so
->outputs_count
= j
;
1757 block
->noutputs
= j
* 4;
1760 /* if we want half-precision outputs, mark the output registers
1763 if (key
.half_precision
) {
1764 for (i
= 0; i
< block
->noutputs
; i
++) {
1765 if (!block
->outputs
[i
])
1767 block
->outputs
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
1771 /* at this point, we want the kill's in the outputs array too,
1772 * so that they get scheduled (since they have no dst).. we've
1773 * already ensured that the array is big enough in push_block():
1775 if (so
->type
== SHADER_FRAGMENT
) {
1776 for (i
= 0; i
< ctx
->kill_count
; i
++)
1777 block
->outputs
[block
->noutputs
++] = ctx
->kill
[i
];
1780 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1783 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1784 printf("BEFORE CP:\n");
1785 ir3_dump_instr_list(block
->head
);
1788 ir3_block_depth(block
);
1790 ir3_block_cp(block
);
1792 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1793 printf("BEFORE GROUPING:\n");
1794 ir3_dump_instr_list(block
->head
);
1797 /* Group left/right neighbors, inserting mov's where needed to
1800 ir3_block_group(block
);
1802 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1805 ir3_block_depth(block
);
1807 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1808 printf("AFTER DEPTH:\n");
1809 ir3_dump_instr_list(block
->head
);
1812 ret
= ir3_block_sched(block
);
1814 DBG("SCHED failed!");
1818 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1819 printf("AFTER SCHED:\n");
1820 ir3_dump_instr_list(block
->head
);
1823 ret
= ir3_block_ra(block
, so
->type
, so
->frag_coord
, so
->frag_face
);
1829 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1830 printf("AFTER RA:\n");
1831 ir3_dump_instr_list(block
->head
);
1834 ir3_block_legalize(block
, &so
->has_samp
, &max_bary
);
1836 /* fixup input/outputs: */
1837 for (i
= 0; i
< so
->outputs_count
; i
++) {
1838 so
->outputs
[i
].regid
= block
->outputs
[i
*4]->regs
[0]->num
;
1839 /* preserve hack for depth output.. tgsi writes depth to .z,
1840 * but what we give the hw is the scalar register:
1842 if ((so
->type
== SHADER_FRAGMENT
) &&
1843 (sem2name(so
->outputs
[i
].semantic
) == TGSI_SEMANTIC_POSITION
))
1844 so
->outputs
[i
].regid
+= 2;
1847 /* Note that some or all channels of an input may be unused: */
1849 for (i
= 0; i
< so
->inputs_count
; i
++) {
1850 unsigned j
, regid
= ~0, compmask
= 0;
1851 so
->inputs
[i
].ncomp
= 0;
1852 for (j
= 0; j
< 4; j
++) {
1853 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
1855 compmask
|= (1 << j
);
1856 regid
= in
->regs
[0]->num
- j
;
1858 so
->inputs
[i
].ncomp
++;
1861 so
->inputs
[i
].regid
= regid
;
1862 so
->inputs
[i
].compmask
= compmask
;
1865 /* fragment shader always gets full vec4's even if it doesn't
1866 * fetch all components, but vertex shader we need to update
1867 * with the actual number of components fetch, otherwise thing
1868 * will hang due to mismaptch between VFD_DECODE's and
1871 if (so
->type
== SHADER_VERTEX
)
1872 so
->total_in
= actual_in
;
1874 so
->total_in
= align(max_bary
+ 1, 4);
1878 ir3_destroy(so
->ir
);