1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
31 #include "pipe/p_state.h"
32 #include "util/u_string.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "tgsi/tgsi_lowering.h"
36 #include "tgsi/tgsi_strings.h"
38 #include "nir/tgsi_to_nir.h"
39 #include "glsl/shader_enums.h"
41 #include "freedreno_util.h"
43 #include "ir3_compiler.h"
44 #include "ir3_shader.h"
47 #include "instr-a3xx.h"
52 struct ir3_compiler
*compiler
;
54 const struct tgsi_token
*tokens
;
58 struct ir3_shader_variant
*so
;
60 struct ir3_block
*block
; /* the current block */
61 struct ir3_block
*in_block
; /* block created for shader inputs */
63 nir_function_impl
*impl
;
65 /* For fragment shaders, from the hw perspective the only
66 * actual input is r0.xy position register passed to bary.f.
67 * But TGSI doesn't know that, it still declares things as
68 * IN[] registers. So we do all the input tracking normally
69 * and fix things up after compile_instructions()
71 * NOTE that frag_pos is the hardware position (possibly it
72 * is actually an index or tag or some such.. it is *not*
73 * values that can be directly used for gl_FragCoord..)
75 struct ir3_instruction
*frag_pos
, *frag_face
, *frag_coord
[4];
77 /* For vertex shaders, keep track of the system values sources */
78 struct ir3_instruction
*vertex_id
, *basevertex
, *instance_id
;
80 /* mapping from nir_register to defining instruction: */
81 struct hash_table
*def_ht
;
83 /* mapping from nir_variable to ir3_array: */
84 struct hash_table
*var_ht
;
87 /* a common pattern for indirect addressing is to request the
88 * same address register multiple times. To avoid generating
89 * duplicate instruction sequences (which our backend does not
90 * try to clean up, since that should be done as the NIR stage)
91 * we cache the address value generated for a given src value:
93 struct hash_table
*addr_ht
;
95 /* maps nir_block to ir3_block, mostly for the purposes of
96 * figuring out the blocks successors
98 struct hash_table
*block_ht
;
100 /* for calculating input/output positions/linkages: */
103 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
104 * so we need to use ldlv.u32 to load the varying directly:
108 /* on a3xx, we need to add one to # of array levels:
112 /* on a3xx, we need to scale up integer coords for isaml based
115 bool unminify_coords
;
117 /* for looking up which system value is which */
118 unsigned sysval_semantics
[8];
120 /* list of kill instructions: */
121 struct ir3_instruction
*kill
[16];
122 unsigned int kill_count
;
124 /* set if we encounter something we can't handle yet, so we
125 * can bail cleanly and fallback to TGSI compiler f/e
131 static struct ir3_instruction
* create_immed(struct ir3_block
*block
, uint32_t val
);
132 static struct ir3_block
* get_block(struct ir3_compile
*ctx
, nir_block
*nblock
);
134 static struct nir_shader
*to_nir(const struct tgsi_token
*tokens
)
136 struct nir_shader_compiler_options options
= {
141 .native_integers
= true,
145 struct nir_shader
*s
= tgsi_to_nir(tokens
, &options
);
147 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
148 debug_printf("----------------------\n");
149 nir_print_shader(s
, stdout
);
150 debug_printf("----------------------\n");
153 nir_opt_global_to_local(s
);
154 nir_convert_to_ssa(s
);
160 nir_lower_vars_to_ssa(s
);
161 nir_lower_alu_to_scalar(s
);
162 nir_lower_phis_to_scalar(s
);
164 progress
|= nir_copy_prop(s
);
165 progress
|= nir_opt_dce(s
);
166 progress
|= nir_opt_cse(s
);
167 progress
|= ir3_nir_lower_if_else(s
);
168 progress
|= nir_opt_algebraic(s
);
169 progress
|= nir_opt_constant_folding(s
);
173 nir_remove_dead_variables(s
);
174 nir_validate_shader(s
);
176 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
177 debug_printf("----------------------\n");
178 nir_print_shader(s
, stdout
);
179 debug_printf("----------------------\n");
185 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
186 static const struct tgsi_token
*
187 lower_tgsi(struct ir3_compile
*ctx
, const struct tgsi_token
*tokens
,
188 struct ir3_shader_variant
*so
)
190 struct tgsi_shader_info info
;
191 struct tgsi_lowering_config lconfig
= {
192 .color_two_side
= so
->key
.color_two_side
,
197 case SHADER_FRAGMENT
:
199 lconfig
.saturate_s
= so
->key
.fsaturate_s
;
200 lconfig
.saturate_t
= so
->key
.fsaturate_t
;
201 lconfig
.saturate_r
= so
->key
.fsaturate_r
;
204 lconfig
.saturate_s
= so
->key
.vsaturate_s
;
205 lconfig
.saturate_t
= so
->key
.vsaturate_t
;
206 lconfig
.saturate_r
= so
->key
.vsaturate_r
;
210 if (ctx
->compiler
->gpu_id
>= 400) {
211 /* a4xx seems to have *no* sam.p */
212 lconfig
.lower_TXP
= ~0; /* lower all txp */
214 /* a3xx just needs to avoid sam.p for 3d tex */
215 lconfig
.lower_TXP
= (1 << TGSI_TEXTURE_3D
);
218 return tgsi_transform_lowering(&lconfig
, tokens
, &info
);
221 static struct ir3_compile
*
222 compile_init(struct ir3_compiler
*compiler
,
223 struct ir3_shader_variant
*so
,
224 const struct tgsi_token
*tokens
)
226 struct ir3_compile
*ctx
= rzalloc(NULL
, struct ir3_compile
);
227 const struct tgsi_token
*lowered_tokens
;
229 if (compiler
->gpu_id
>= 400) {
230 /* need special handling for "flat" */
231 ctx
->flat_bypass
= true;
232 ctx
->levels_add_one
= false;
233 ctx
->unminify_coords
= false;
235 /* no special handling for "flat" */
236 ctx
->flat_bypass
= false;
237 ctx
->levels_add_one
= true;
238 ctx
->unminify_coords
= true;
241 ctx
->compiler
= compiler
;
245 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
246 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
247 ctx
->var_ht
= _mesa_hash_table_create(ctx
,
248 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
249 ctx
->addr_ht
= _mesa_hash_table_create(ctx
,
250 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
251 ctx
->block_ht
= _mesa_hash_table_create(ctx
,
252 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
254 lowered_tokens
= lower_tgsi(ctx
, tokens
, so
);
256 lowered_tokens
= tokens
;
257 ctx
->s
= to_nir(lowered_tokens
);
259 if (lowered_tokens
!= tokens
)
260 free((void *)lowered_tokens
);
262 so
->first_driver_param
= so
->first_immediate
= ctx
->s
->num_uniforms
;
264 /* one (vec4) slot for vertex id base: */
265 if (so
->type
== SHADER_VERTEX
)
266 so
->first_immediate
++;
268 /* reserve 4 (vec4) slots for ubo base addresses: */
269 so
->first_immediate
+= 4;
275 compile_error(struct ir3_compile
*ctx
, const char *format
, ...)
278 va_start(ap
, format
);
279 _debug_vprintf(format
, ap
);
281 nir_print_shader(ctx
->s
, stdout
);
286 #define compile_assert(ctx, cond) do { \
287 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
291 compile_free(struct ir3_compile
*ctx
)
296 /* global per-array information: */
298 unsigned length
, aid
;
301 /* per-block array state: */
302 struct ir3_array_value
{
303 /* TODO drop length/aid, and just have ptr back to ir3_array */
304 unsigned length
, aid
;
305 /* initial array element values are phi's, other than for the
306 * entry block. The phi src's get added later in a resolve step
307 * after we have visited all the blocks, to account for back
310 struct ir3_instruction
**phis
;
311 /* current array element values (as block is processed). When
312 * the array phi's are resolved, it will contain the array state
313 * at exit of block, so successor blocks can use it to add their
316 struct ir3_instruction
*arr
[];
319 /* track array assignments per basic block. When an array is read
320 * outside of the same basic block, we can use NIR's dominance-frontier
321 * information to figure out where phi nodes are needed.
323 struct ir3_nir_block_data
{
325 /* indexed by array-id (aid): */
326 struct ir3_array_value
*arrs
[];
329 static struct ir3_nir_block_data
*
330 get_block_data(struct ir3_compile
*ctx
, struct ir3_block
*block
)
333 struct ir3_nir_block_data
*bd
= ralloc_size(ctx
, sizeof(*bd
) +
334 ((ctx
->num_arrays
+ 1) * sizeof(bd
->arrs
[0])));
341 declare_var(struct ir3_compile
*ctx
, nir_variable
*var
)
343 unsigned length
= glsl_get_length(var
->type
) * 4; /* always vec4, at least with ttn */
344 struct ir3_array
*arr
= ralloc(ctx
, struct ir3_array
);
345 arr
->length
= length
;
346 arr
->aid
= ++ctx
->num_arrays
;
347 _mesa_hash_table_insert(ctx
->var_ht
, var
, arr
);
351 nir_block_pred(nir_block
*block
)
353 assert(block
->predecessors
->entries
< 2);
354 if (block
->predecessors
->entries
== 0)
356 return (nir_block
*)_mesa_set_next_entry(block
->predecessors
, NULL
)->key
;
359 static struct ir3_array_value
*
360 get_var(struct ir3_compile
*ctx
, nir_variable
*var
)
362 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->var_ht
, var
);
363 struct ir3_block
*block
= ctx
->block
;
364 struct ir3_nir_block_data
*bd
= get_block_data(ctx
, block
);
365 struct ir3_array
*arr
= entry
->data
;
367 if (!bd
->arrs
[arr
->aid
]) {
368 struct ir3_array_value
*av
= ralloc_size(bd
, sizeof(*av
) +
369 (arr
->length
* sizeof(av
->arr
[0])));
370 struct ir3_array_value
*defn
= NULL
;
371 nir_block
*pred_block
;
373 av
->length
= arr
->length
;
376 /* For loops, we have to consider that we have not visited some
377 * of the blocks who should feed into the phi (ie. back-edges in
378 * the cfg).. for example:
381 * block { load_var; ... }
382 * if then block {} else block {}
383 * block { store_var; ... }
384 * if then block {} else block {}
388 * We can skip the phi if we can chase the block predecessors
389 * until finding the block previously defining the array without
390 * crossing a block that has more than one predecessor.
392 * Otherwise create phi's and resolve them as a post-pass after
393 * all the blocks have been visited (to handle back-edges).
396 for (pred_block
= block
->nblock
;
397 pred_block
&& (pred_block
->predecessors
->entries
< 2) && !defn
;
398 pred_block
= nir_block_pred(pred_block
)) {
399 struct ir3_block
*pblock
= get_block(ctx
, pred_block
);
400 struct ir3_nir_block_data
*pbd
= pblock
->bd
;
403 defn
= pbd
->arrs
[arr
->aid
];
407 /* only one possible definer: */
408 for (unsigned i
= 0; i
< arr
->length
; i
++)
409 av
->arr
[i
] = defn
->arr
[i
];
410 } else if (pred_block
) {
411 /* not the first block, and multiple potential definers: */
412 av
->phis
= ralloc_size(av
, arr
->length
* sizeof(av
->phis
[0]));
414 for (unsigned i
= 0; i
< arr
->length
; i
++) {
415 struct ir3_instruction
*phi
;
417 phi
= ir3_instr_create2(block
, -1, OPC_META_PHI
,
418 1 + ctx
->impl
->num_blocks
);
419 ir3_reg_create(phi
, 0, 0); /* dst */
421 /* phi's should go at head of block: */
422 list_delinit(&phi
->node
);
423 list_add(&phi
->node
, &block
->instr_list
);
425 av
->phis
[i
] = av
->arr
[i
] = phi
;
428 /* Some shaders end up reading array elements without
429 * first writing.. so initialize things to prevent null
432 for (unsigned i
= 0; i
< arr
->length
; i
++)
433 av
->arr
[i
] = create_immed(block
, 0);
436 bd
->arrs
[arr
->aid
] = av
;
439 return bd
->arrs
[arr
->aid
];
443 add_array_phi_srcs(struct ir3_compile
*ctx
, nir_block
*nblock
,
444 struct ir3_array_value
*av
, BITSET_WORD
*visited
)
446 struct ir3_block
*block
;
447 struct ir3_nir_block_data
*bd
;
449 if (BITSET_TEST(visited
, nblock
->index
))
452 BITSET_SET(visited
, nblock
->index
);
454 block
= get_block(ctx
, nblock
);
457 if (bd
&& bd
->arrs
[av
->aid
]) {
458 struct ir3_array_value
*dav
= bd
->arrs
[av
->aid
];
459 for (unsigned i
= 0; i
< av
->length
; i
++) {
460 ir3_reg_create(av
->phis
[i
], 0, IR3_REG_SSA
)->instr
=
464 /* didn't find defn, recurse predecessors: */
465 struct set_entry
*entry
;
466 set_foreach(nblock
->predecessors
, entry
) {
467 add_array_phi_srcs(ctx
, (nir_block
*)entry
->key
, av
, visited
);
473 resolve_array_phis(struct ir3_compile
*ctx
, struct ir3_block
*block
)
475 struct ir3_nir_block_data
*bd
= block
->bd
;
476 unsigned bitset_words
= BITSET_WORDS(ctx
->impl
->num_blocks
);
481 /* TODO use nir dom_frontier to help us with this? */
483 for (unsigned i
= 1; i
<= ctx
->num_arrays
; i
++) {
484 struct ir3_array_value
*av
= bd
->arrs
[i
];
485 BITSET_WORD visited
[bitset_words
];
486 struct set_entry
*entry
;
488 if (!(av
&& av
->phis
))
491 memset(visited
, 0, sizeof(visited
));
492 set_foreach(block
->nblock
->predecessors
, entry
) {
493 add_array_phi_srcs(ctx
, (nir_block
*)entry
->key
, av
, visited
);
498 /* allocate a n element value array (to be populated by caller) and
501 static struct ir3_instruction
**
502 __get_dst(struct ir3_compile
*ctx
, void *key
, unsigned n
)
504 struct ir3_instruction
**value
=
505 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
506 _mesa_hash_table_insert(ctx
->def_ht
, key
, value
);
510 static struct ir3_instruction
**
511 get_dst(struct ir3_compile
*ctx
, nir_dest
*dst
, unsigned n
)
514 return __get_dst(ctx
, &dst
->ssa
, n
);
516 return __get_dst(ctx
, dst
->reg
.reg
, n
);
520 static struct ir3_instruction
**
521 get_dst_ssa(struct ir3_compile
*ctx
, nir_ssa_def
*dst
, unsigned n
)
523 return __get_dst(ctx
, dst
, n
);
526 static struct ir3_instruction
**
527 get_src(struct ir3_compile
*ctx
, nir_src
*src
)
529 struct hash_entry
*entry
;
531 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
533 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->reg
.reg
);
535 compile_assert(ctx
, entry
);
539 static struct ir3_instruction
*
540 create_immed(struct ir3_block
*block
, uint32_t val
)
542 struct ir3_instruction
*mov
;
544 mov
= ir3_instr_create(block
, 1, 0);
545 mov
->cat1
.src_type
= TYPE_U32
;
546 mov
->cat1
.dst_type
= TYPE_U32
;
547 ir3_reg_create(mov
, 0, 0);
548 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
553 static struct ir3_instruction
*
554 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
)
556 struct ir3_instruction
*instr
, *immed
;
558 /* TODO in at least some cases, the backend could probably be
559 * made clever enough to propagate IR3_REG_HALF..
561 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
562 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
564 immed
= create_immed(block
, 2);
565 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
567 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
568 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
569 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
571 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
572 instr
->regs
[0]->num
= regid(REG_A0
, 0);
573 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
574 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
579 /* caches addr values to avoid generating multiple cov/shl/mova
580 * sequences for each use of a given NIR level src as address
582 static struct ir3_instruction
*
583 get_addr(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
585 struct ir3_instruction
*addr
;
586 struct hash_entry
*entry
;
587 entry
= _mesa_hash_table_search(ctx
->addr_ht
, src
);
591 /* TODO do we need to cache per block? */
592 addr
= create_addr(ctx
->block
, src
);
593 _mesa_hash_table_insert(ctx
->addr_ht
, src
, addr
);
598 static struct ir3_instruction
*
599 get_predicate(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
601 struct ir3_block
*b
= ctx
->block
;
602 struct ir3_instruction
*cond
;
604 /* NOTE: only cmps.*.* can write p0.x: */
605 cond
= ir3_CMPS_S(b
, src
, 0, create_immed(b
, 0), 0);
606 cond
->cat2
.condition
= IR3_COND_NE
;
608 /* condition always goes in predicate register: */
609 cond
->regs
[0]->num
= regid(REG_P0
, 0);
614 static struct ir3_instruction
*
615 create_uniform(struct ir3_compile
*ctx
, unsigned n
)
617 struct ir3_instruction
*mov
;
619 mov
= ir3_instr_create(ctx
->block
, 1, 0);
620 /* TODO get types right? */
621 mov
->cat1
.src_type
= TYPE_F32
;
622 mov
->cat1
.dst_type
= TYPE_F32
;
623 ir3_reg_create(mov
, 0, 0);
624 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
629 static struct ir3_instruction
*
630 create_uniform_indirect(struct ir3_compile
*ctx
, unsigned n
,
631 struct ir3_instruction
*address
)
633 struct ir3_instruction
*mov
;
635 mov
= ir3_instr_create(ctx
->block
, 1, 0);
636 mov
->cat1
.src_type
= TYPE_U32
;
637 mov
->cat1
.dst_type
= TYPE_U32
;
638 ir3_reg_create(mov
, 0, 0);
639 ir3_reg_create(mov
, n
, IR3_REG_CONST
| IR3_REG_RELATIV
);
641 ir3_instr_set_address(mov
, address
);
646 static struct ir3_instruction
*
647 create_collect(struct ir3_block
*block
, struct ir3_instruction
**arr
,
650 struct ir3_instruction
*collect
;
655 collect
= ir3_instr_create2(block
, -1, OPC_META_FI
, 1 + arrsz
);
656 ir3_reg_create(collect
, 0, 0); /* dst */
657 for (unsigned i
= 0; i
< arrsz
; i
++)
658 ir3_reg_create(collect
, 0, IR3_REG_SSA
)->instr
= arr
[i
];
663 static struct ir3_instruction
*
664 create_indirect_load(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
665 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
667 struct ir3_block
*block
= ctx
->block
;
668 struct ir3_instruction
*mov
;
669 struct ir3_register
*src
;
671 mov
= ir3_instr_create(block
, 1, 0);
672 mov
->cat1
.src_type
= TYPE_U32
;
673 mov
->cat1
.dst_type
= TYPE_U32
;
674 ir3_reg_create(mov
, 0, 0);
675 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
676 src
->instr
= collect
;
680 ir3_instr_set_address(mov
, address
);
685 static struct ir3_instruction
*
686 create_indirect_store(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
687 struct ir3_instruction
*src
, struct ir3_instruction
*address
,
688 struct ir3_instruction
*collect
)
690 struct ir3_block
*block
= ctx
->block
;
691 struct ir3_instruction
*mov
;
692 struct ir3_register
*dst
;
694 mov
= ir3_instr_create(block
, 1, 0);
695 mov
->cat1
.src_type
= TYPE_U32
;
696 mov
->cat1
.dst_type
= TYPE_U32
;
697 dst
= ir3_reg_create(mov
, 0, IR3_REG_RELATIV
);
700 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
701 mov
->fanin
= collect
;
703 ir3_instr_set_address(mov
, address
);
708 static struct ir3_instruction
*
709 create_input(struct ir3_block
*block
, struct ir3_instruction
*instr
,
712 struct ir3_instruction
*in
;
714 in
= ir3_instr_create(block
, -1, OPC_META_INPUT
);
715 in
->inout
.block
= block
;
716 ir3_reg_create(in
, n
, 0);
718 ir3_reg_create(in
, 0, IR3_REG_SSA
)->instr
= instr
;
723 static struct ir3_instruction
*
724 create_frag_input(struct ir3_compile
*ctx
, unsigned n
, bool use_ldlv
)
726 struct ir3_block
*block
= ctx
->block
;
727 struct ir3_instruction
*instr
;
728 struct ir3_instruction
*inloc
= create_immed(block
, n
);
731 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
732 instr
->cat6
.type
= TYPE_U32
;
733 instr
->cat6
.iim_val
= 1;
735 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_pos
, 0);
736 instr
->regs
[2]->wrmask
= 0x3;
742 static struct ir3_instruction
*
743 create_frag_coord(struct ir3_compile
*ctx
, unsigned comp
)
745 struct ir3_block
*block
= ctx
->block
;
746 struct ir3_instruction
*instr
;
748 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
750 ctx
->frag_coord
[comp
] = create_input(ctx
->block
, NULL
, 0);
755 /* for frag_coord, we get unsigned values.. we need
756 * to subtract (integer) 8 and divide by 16 (right-
757 * shift by 4) then convert to float:
761 * mov.u32f32 dst, tmp
764 instr
= ir3_SUB_S(block
, ctx
->frag_coord
[comp
], 0,
765 create_immed(block
, 8), 0);
766 instr
= ir3_SHR_B(block
, instr
, 0,
767 create_immed(block
, 4), 0);
768 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
774 /* seems that we can use these as-is: */
775 return ctx
->frag_coord
[comp
];
779 static struct ir3_instruction
*
780 create_frag_face(struct ir3_compile
*ctx
, unsigned comp
)
782 struct ir3_block
*block
= ctx
->block
;
783 struct ir3_instruction
*instr
;
787 compile_assert(ctx
, !ctx
->frag_face
);
789 ctx
->frag_face
= create_input(block
, NULL
, 0);
790 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
792 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
793 * positive vs negative float.. and piglit further seems to
794 * expect -1.0 or 1.0:
796 * mul.s tmp, hr0.x, 2
798 * mov.s32f32, dst, tmp
801 instr
= ir3_MUL_S(block
, ctx
->frag_face
, 0,
802 create_immed(block
, 2), 0);
803 instr
= ir3_ADD_S(block
, instr
, 0,
804 create_immed(block
, 1), 0);
805 instr
= ir3_COV(block
, instr
, TYPE_S32
, TYPE_F32
);
810 return create_immed(block
, fui(0.0));
813 return create_immed(block
, fui(1.0));
817 /* helper for instructions that produce multiple consecutive scalar
818 * outputs which need to have a split/fanout meta instruction inserted
821 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
822 struct ir3_instruction
*src
, unsigned n
)
824 struct ir3_instruction
*prev
= NULL
;
825 for (int i
= 0, j
= 0; i
< n
; i
++) {
826 struct ir3_instruction
*split
=
827 ir3_instr_create(block
, -1, OPC_META_FO
);
828 ir3_reg_create(split
, 0, IR3_REG_SSA
);
829 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
833 split
->cp
.left
= prev
;
834 split
->cp
.left_cnt
++;
835 prev
->cp
.right
= split
;
836 prev
->cp
.right_cnt
++;
840 if (src
->regs
[0]->wrmask
& (1 << i
))
846 * Adreno uses uint rather than having dedicated bool type,
847 * which (potentially) requires some conversion, in particular
848 * when using output of an bool instr to int input, or visa
852 * -------+---------+-------+-
856 * To convert from an adreno bool (uint) to nir, use:
858 * absneg.s dst, (neg)src
860 * To convert back in the other direction:
862 * absneg.s dst, (abs)arc
864 * The CP step can clean up the absneg.s that cancel each other
865 * out, and with a slight bit of extra cleverness (to recognize
866 * the instructions which produce either a 0 or 1) can eliminate
867 * the absneg.s's completely when an instruction that wants
868 * 0/1 consumes the result. For example, when a nir 'bcsel'
869 * consumes the result of 'feq'. So we should be able to get by
870 * without a boolean resolve step, and without incuring any
871 * extra penalty in instruction count.
874 /* NIR bool -> native (adreno): */
875 static struct ir3_instruction
*
876 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
878 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
881 /* native (adreno) -> NIR bool: */
882 static struct ir3_instruction
*
883 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
885 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
889 * alu/sfu instructions:
893 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
895 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
896 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
897 struct ir3_block
*b
= ctx
->block
;
899 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
901 /* Vectors are special in that they have non-scalarized writemasks,
902 * and just take the first swizzle channel for each argument in
903 * order into each writemask channel.
905 if ((alu
->op
== nir_op_vec2
) ||
906 (alu
->op
== nir_op_vec3
) ||
907 (alu
->op
== nir_op_vec4
)) {
909 for (int i
= 0; i
< info
->num_inputs
; i
++) {
910 nir_alu_src
*asrc
= &alu
->src
[i
];
912 compile_assert(ctx
, !asrc
->abs
);
913 compile_assert(ctx
, !asrc
->negate
);
915 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
917 src
[i
] = create_immed(ctx
->block
, 0);
918 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
924 /* General case: We can just grab the one used channel per src. */
925 for (int i
= 0; i
< info
->num_inputs
; i
++) {
926 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
927 nir_alu_src
*asrc
= &alu
->src
[i
];
929 compile_assert(ctx
, !asrc
->abs
);
930 compile_assert(ctx
, !asrc
->negate
);
932 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
934 compile_assert(ctx
, src
[i
]);
939 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
942 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
945 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
948 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
951 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
954 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
957 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
958 dst
[0]->cat2
.condition
= IR3_COND_NE
;
959 dst
[0] = ir3_n2b(b
, dst
[0]);
962 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
965 dst
[0] = ir3_b2n(b
, src
[0]);
968 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
969 dst
[0]->cat2
.condition
= IR3_COND_NE
;
970 dst
[0] = ir3_n2b(b
, dst
[0]);
974 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
977 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
980 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
983 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
986 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
989 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
992 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
995 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
998 dst
[0] = ir3_DSX(b
, src
[0], 0);
999 dst
[0]->cat5
.type
= TYPE_F32
;
1002 dst
[0] = ir3_DSY(b
, src
[0], 0);
1003 dst
[0]->cat5
.type
= TYPE_F32
;
1007 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1008 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1009 dst
[0] = ir3_n2b(b
, dst
[0]);
1012 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1013 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1014 dst
[0] = ir3_n2b(b
, dst
[0]);
1017 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1018 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
1019 dst
[0] = ir3_n2b(b
, dst
[0]);
1022 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1023 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1024 dst
[0] = ir3_n2b(b
, dst
[0]);
1027 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
1030 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
1033 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
1035 case nir_op_fround_even
:
1036 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
1039 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
1043 dst
[0] = ir3_SIN(b
, src
[0], 0);
1046 dst
[0] = ir3_COS(b
, src
[0], 0);
1049 dst
[0] = ir3_RSQ(b
, src
[0], 0);
1052 dst
[0] = ir3_RCP(b
, src
[0], 0);
1055 dst
[0] = ir3_LOG2(b
, src
[0], 0);
1058 dst
[0] = ir3_EXP2(b
, src
[0], 0);
1061 dst
[0] = ir3_SQRT(b
, src
[0], 0);
1065 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
1068 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
1071 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
1074 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
1077 dst
[0] = ir3_MAX_U(b
, src
[0], 0, src
[1], 0);
1080 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
1083 dst
[0] = ir3_MIN_U(b
, src
[0], 0, src
[1], 0);
1087 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
1088 * mull.u tmp0, a, b ; mul low, i.e. al * bl
1089 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
1090 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
1092 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
1093 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
1094 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
1097 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
1100 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
1103 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
1106 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
1109 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
1111 case nir_op_isign
: {
1112 /* maybe this would be sane to lower in nir.. */
1113 struct ir3_instruction
*neg
, *pos
;
1115 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
1116 neg
->cat2
.condition
= IR3_COND_LT
;
1118 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
1119 pos
->cat2
.condition
= IR3_COND_GT
;
1121 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
1126 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
1129 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
1132 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
1135 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1136 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1137 dst
[0] = ir3_n2b(b
, dst
[0]);
1140 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1141 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1142 dst
[0] = ir3_n2b(b
, dst
[0]);
1145 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1146 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
1147 dst
[0] = ir3_n2b(b
, dst
[0]);
1150 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1151 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1152 dst
[0] = ir3_n2b(b
, dst
[0]);
1155 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
1156 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1157 dst
[0] = ir3_n2b(b
, dst
[0]);
1160 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
1161 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1162 dst
[0] = ir3_n2b(b
, dst
[0]);
1166 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
1170 compile_error(ctx
, "Unhandled ALU op: %s\n",
1171 nir_op_infos
[alu
->op
].name
);
1176 /* handles direct/indirect UBO reads: */
1178 emit_intrinsic_load_ubo(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
1179 struct ir3_instruction
**dst
)
1181 struct ir3_block
*b
= ctx
->block
;
1182 struct ir3_instruction
*addr
, *src0
, *src1
;
1183 /* UBO addresses are the first driver params: */
1184 unsigned ubo
= regid(ctx
->so
->first_driver_param
, 0);
1185 unsigned off
= intr
->const_index
[0];
1187 /* First src is ubo index, which could either be an immed or not: */
1188 src0
= get_src(ctx
, &intr
->src
[0])[0];
1189 if (is_same_type_mov(src0
) &&
1190 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
1191 addr
= create_uniform(ctx
, ubo
+ src0
->regs
[1]->iim_val
);
1193 addr
= create_uniform_indirect(ctx
, ubo
, get_addr(ctx
, src0
));
1196 if (intr
->intrinsic
== nir_intrinsic_load_ubo_indirect
) {
1197 /* For load_ubo_indirect, second src is indirect offset: */
1198 src1
= get_src(ctx
, &intr
->src
[1])[0];
1200 /* and add offset to addr: */
1201 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
1204 /* if offset is to large to encode in the ldg, split it out: */
1205 if ((off
+ (intr
->num_components
* 4)) > 1024) {
1206 /* split out the minimal amount to improve the odds that
1207 * cp can fit the immediate in the add.s instruction:
1209 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
1210 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
1214 for (int i
= 0; i
< intr
->num_components
; i
++) {
1215 struct ir3_instruction
*load
=
1216 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0);
1217 load
->cat6
.type
= TYPE_U32
;
1218 load
->cat6
.offset
= off
+ i
* 4; /* byte offset */
1223 /* handles array reads: */
1225 emit_intrinisic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
1226 struct ir3_instruction
**dst
)
1228 nir_deref_var
*dvar
= intr
->variables
[0];
1229 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1230 struct ir3_array_value
*arr
= get_var(ctx
, dvar
->var
);
1232 compile_assert(ctx
, dvar
->deref
.child
&&
1233 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1235 switch (darr
->deref_array_type
) {
1236 case nir_deref_array_type_direct
:
1237 /* direct access does not require anything special: */
1238 for (int i
= 0; i
< intr
->num_components
; i
++) {
1239 unsigned n
= darr
->base_offset
* 4 + i
;
1240 compile_assert(ctx
, n
< arr
->length
);
1241 dst
[i
] = arr
->arr
[n
];
1244 case nir_deref_array_type_indirect
: {
1245 /* for indirect, we need to collect all the array elements: */
1246 struct ir3_instruction
*collect
=
1247 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1248 struct ir3_instruction
*addr
=
1249 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1250 for (int i
= 0; i
< intr
->num_components
; i
++) {
1251 unsigned n
= darr
->base_offset
* 4 + i
;
1252 compile_assert(ctx
, n
< arr
->length
);
1253 dst
[i
] = create_indirect_load(ctx
, arr
->length
, n
, addr
, collect
);
1258 compile_error(ctx
, "Unhandled load deref type: %u\n",
1259 darr
->deref_array_type
);
1264 /* handles array writes: */
1266 emit_intrinisic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1268 nir_deref_var
*dvar
= intr
->variables
[0];
1269 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1270 struct ir3_array_value
*arr
= get_var(ctx
, dvar
->var
);
1271 struct ir3_instruction
**src
;
1273 compile_assert(ctx
, dvar
->deref
.child
&&
1274 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1276 src
= get_src(ctx
, &intr
->src
[0]);
1278 switch (darr
->deref_array_type
) {
1279 case nir_deref_array_type_direct
:
1280 /* direct access does not require anything special: */
1281 for (int i
= 0; i
< intr
->num_components
; i
++) {
1282 unsigned n
= darr
->base_offset
* 4 + i
;
1283 compile_assert(ctx
, n
< arr
->length
);
1284 arr
->arr
[n
] = src
[i
];
1287 case nir_deref_array_type_indirect
: {
1288 /* for indirect, create indirect-store and fan that out: */
1289 struct ir3_instruction
*collect
=
1290 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1291 struct ir3_instruction
*addr
=
1292 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1293 for (int i
= 0; i
< intr
->num_components
; i
++) {
1294 struct ir3_instruction
*store
;
1295 unsigned n
= darr
->base_offset
* 4 + i
;
1296 compile_assert(ctx
, n
< arr
->length
);
1298 store
= create_indirect_store(ctx
, arr
->length
,
1299 n
, src
[i
], addr
, collect
);
1301 store
->fanin
->fi
.aid
= arr
->aid
;
1303 /* TODO: probably split this out to be used for
1304 * store_output_indirect? or move this into
1305 * create_indirect_store()?
1307 for (int j
= i
; j
< arr
->length
; j
+= intr
->num_components
) {
1308 struct ir3_instruction
*split
;
1310 split
= ir3_instr_create(ctx
->block
, -1, OPC_META_FO
);
1312 ir3_reg_create(split
, 0, 0);
1313 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= store
;
1315 arr
->arr
[j
] = split
;
1318 /* fixup fanout/split neighbors: */
1319 for (int i
= 0; i
< arr
->length
; i
++) {
1320 arr
->arr
[i
]->cp
.right
= (i
< (arr
->length
- 1)) ?
1321 arr
->arr
[i
+1] : NULL
;
1322 arr
->arr
[i
]->cp
.left
= (i
> 0) ?
1323 arr
->arr
[i
-1] : NULL
;
1328 compile_error(ctx
, "Unhandled store deref type: %u\n",
1329 darr
->deref_array_type
);
1334 static void add_sysval_input(struct ir3_compile
*ctx
, unsigned name
,
1335 struct ir3_instruction
*instr
)
1337 struct ir3_shader_variant
*so
= ctx
->so
;
1338 unsigned r
= regid(so
->inputs_count
, 0);
1339 unsigned n
= so
->inputs_count
++;
1341 so
->inputs
[n
].semantic
= ir3_semantic_name(name
, 0);
1342 so
->inputs
[n
].compmask
= 1;
1343 so
->inputs
[n
].regid
= r
;
1344 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1347 ctx
->ir
->ninputs
= MAX2(ctx
->ir
->ninputs
, r
+ 1);
1348 ctx
->ir
->inputs
[r
] = instr
;
1352 emit_intrinisic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1354 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1355 struct ir3_instruction
**dst
, **src
;
1356 struct ir3_block
*b
= ctx
->block
;
1357 unsigned idx
= intr
->const_index
[0];
1359 if (info
->has_dest
) {
1360 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1365 switch (intr
->intrinsic
) {
1366 case nir_intrinsic_load_uniform
:
1367 for (int i
= 0; i
< intr
->num_components
; i
++) {
1368 unsigned n
= idx
* 4 + i
;
1369 dst
[i
] = create_uniform(ctx
, n
);
1372 case nir_intrinsic_load_uniform_indirect
:
1373 src
= get_src(ctx
, &intr
->src
[0]);
1374 for (int i
= 0; i
< intr
->num_components
; i
++) {
1375 unsigned n
= idx
* 4 + i
;
1376 dst
[i
] = create_uniform_indirect(ctx
, n
,
1377 get_addr(ctx
, src
[0]));
1379 /* NOTE: if relative addressing is used, we set constlen in
1380 * the compiler (to worst-case value) since we don't know in
1381 * the assembler what the max addr reg value can be:
1383 ctx
->so
->constlen
= ctx
->s
->num_uniforms
;
1385 case nir_intrinsic_load_ubo
:
1386 case nir_intrinsic_load_ubo_indirect
:
1387 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
1389 case nir_intrinsic_load_input
:
1390 for (int i
= 0; i
< intr
->num_components
; i
++) {
1391 unsigned n
= idx
* 4 + i
;
1392 dst
[i
] = ctx
->ir
->inputs
[n
];
1395 case nir_intrinsic_load_input_indirect
:
1396 src
= get_src(ctx
, &intr
->src
[0]);
1397 struct ir3_instruction
*collect
=
1398 create_collect(b
, ctx
->ir
->inputs
, ctx
->ir
->ninputs
);
1399 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1400 for (int i
= 0; i
< intr
->num_components
; i
++) {
1401 unsigned n
= idx
* 4 + i
;
1402 dst
[i
] = create_indirect_load(ctx
, ctx
->ir
->ninputs
,
1406 case nir_intrinsic_load_var
:
1407 emit_intrinisic_load_var(ctx
, intr
, dst
);
1409 case nir_intrinsic_store_var
:
1410 emit_intrinisic_store_var(ctx
, intr
);
1412 case nir_intrinsic_store_output
:
1413 src
= get_src(ctx
, &intr
->src
[0]);
1414 for (int i
= 0; i
< intr
->num_components
; i
++) {
1415 unsigned n
= idx
* 4 + i
;
1416 ctx
->ir
->outputs
[n
] = src
[i
];
1419 case nir_intrinsic_load_base_vertex
:
1420 if (!ctx
->basevertex
) {
1421 /* first four vec4 sysval's reserved for UBOs: */
1422 unsigned r
= regid(ctx
->so
->first_driver_param
+ 4, 0);
1423 ctx
->basevertex
= create_uniform(ctx
, r
);
1424 add_sysval_input(ctx
, TGSI_SEMANTIC_BASEVERTEX
,
1427 dst
[0] = ctx
->basevertex
;
1429 case nir_intrinsic_load_vertex_id_zero_base
:
1430 if (!ctx
->vertex_id
) {
1431 ctx
->vertex_id
= create_input(ctx
->block
, NULL
, 0);
1432 add_sysval_input(ctx
, TGSI_SEMANTIC_VERTEXID_NOBASE
,
1435 dst
[0] = ctx
->vertex_id
;
1437 case nir_intrinsic_load_instance_id
:
1438 if (!ctx
->instance_id
) {
1439 ctx
->instance_id
= create_input(ctx
->block
, NULL
, 0);
1440 add_sysval_input(ctx
, TGSI_SEMANTIC_INSTANCEID
,
1443 dst
[0] = ctx
->instance_id
;
1445 case nir_intrinsic_discard_if
:
1446 case nir_intrinsic_discard
: {
1447 struct ir3_instruction
*cond
, *kill
;
1449 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1450 /* conditional discard: */
1451 src
= get_src(ctx
, &intr
->src
[0]);
1452 cond
= ir3_b2n(b
, src
[0]);
1454 /* unconditional discard: */
1455 cond
= create_immed(b
, 1);
1458 /* NOTE: only cmps.*.* can write p0.x: */
1459 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1460 cond
->cat2
.condition
= IR3_COND_NE
;
1462 /* condition always goes in predicate register: */
1463 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1465 kill
= ir3_KILL(b
, cond
, 0);
1466 array_insert(ctx
->ir
->predicates
, kill
);
1468 ctx
->kill
[ctx
->kill_count
++] = kill
;
1469 ctx
->so
->has_kill
= true;
1474 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1475 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1481 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1483 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1484 instr
->def
.num_components
);
1485 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1486 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u
[i
]);
1490 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1492 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1493 undef
->def
.num_components
);
1494 /* backend doesn't want undefined instructions, so just plug
1497 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1498 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1502 * texture fetch/sample instructions:
1506 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1508 unsigned coords
, flags
= 0;
1510 /* note: would use tex->coord_components.. except txs.. also,
1511 * since array index goes after shadow ref, we don't want to
1514 switch (tex
->sampler_dim
) {
1515 case GLSL_SAMPLER_DIM_1D
:
1516 case GLSL_SAMPLER_DIM_BUF
:
1519 case GLSL_SAMPLER_DIM_2D
:
1520 case GLSL_SAMPLER_DIM_RECT
:
1521 case GLSL_SAMPLER_DIM_EXTERNAL
:
1522 case GLSL_SAMPLER_DIM_MS
:
1525 case GLSL_SAMPLER_DIM_3D
:
1526 case GLSL_SAMPLER_DIM_CUBE
:
1528 flags
|= IR3_INSTR_3D
;
1531 unreachable("bad sampler_dim");
1535 flags
|= IR3_INSTR_S
;
1538 flags
|= IR3_INSTR_A
;
1545 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1547 struct ir3_block
*b
= ctx
->block
;
1548 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1549 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1550 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1551 unsigned i
, coords
, flags
;
1552 unsigned nsrc0
= 0, nsrc1
= 0;
1556 coord
= off
= ddx
= ddy
= NULL
;
1557 lod
= proj
= compare
= NULL
;
1559 /* TODO: might just be one component for gathers? */
1560 dst
= get_dst(ctx
, &tex
->dest
, 4);
1562 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1563 switch (tex
->src
[i
].src_type
) {
1564 case nir_tex_src_coord
:
1565 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1567 case nir_tex_src_bias
:
1568 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1571 case nir_tex_src_lod
:
1572 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1575 case nir_tex_src_comparitor
: /* shadow comparator */
1576 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1578 case nir_tex_src_projector
:
1579 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1582 case nir_tex_src_offset
:
1583 off
= get_src(ctx
, &tex
->src
[i
].src
);
1586 case nir_tex_src_ddx
:
1587 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1589 case nir_tex_src_ddy
:
1590 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1593 compile_error(ctx
, "Unhandled NIR tex serc type: %d\n",
1594 tex
->src
[i
].src_type
);
1600 case nir_texop_tex
: opc
= OPC_SAM
; break;
1601 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1602 case nir_texop_txl
: opc
= OPC_SAML
; break;
1603 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1604 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1605 case nir_texop_txf_ms
:
1609 case nir_texop_query_levels
:
1610 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1614 tex_info(tex
, &flags
, &coords
);
1616 /* scale up integer coords for TXF based on the LOD */
1617 if (ctx
->unminify_coords
&& (opc
== OPC_ISAML
)) {
1619 for (i
= 0; i
< coords
; i
++)
1620 coord
[i
] = ir3_SHL_B(b
, coord
[i
], 0, lod
, 0);
1624 * lay out the first argument in the proper order:
1625 * - actual coordinates first
1626 * - shadow reference
1629 * - starting at offset 4, dpdx.xy, dpdy.xy
1631 * bias/lod go into the second arg
1634 /* insert tex coords: */
1635 for (i
= 0; i
< coords
; i
++)
1636 src0
[nsrc0
++] = coord
[i
];
1639 /* hw doesn't do 1d, so we treat it as 2d with
1640 * height of 1, and patch up the y coord.
1641 * TODO: y coord should be (int)0 in some cases..
1643 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1647 src0
[nsrc0
++] = compare
;
1650 src0
[nsrc0
++] = coord
[coords
];
1653 src0
[nsrc0
++] = proj
;
1654 flags
|= IR3_INSTR_P
;
1657 /* pad to 4, then ddx/ddy: */
1658 if (tex
->op
== nir_texop_txd
) {
1660 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1661 for (i
= 0; i
< coords
; i
++)
1662 src0
[nsrc0
++] = ddx
[i
];
1664 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1665 for (i
= 0; i
< coords
; i
++)
1666 src0
[nsrc0
++] = ddy
[i
];
1668 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1672 * second argument (if applicable):
1677 if (has_off
| has_lod
| has_bias
) {
1679 for (i
= 0; i
< coords
; i
++)
1680 src1
[nsrc1
++] = off
[i
];
1682 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1683 flags
|= IR3_INSTR_O
;
1686 if (has_lod
| has_bias
)
1687 src1
[nsrc1
++] = lod
;
1690 switch (tex
->dest_type
) {
1691 case nir_type_invalid
:
1692 case nir_type_float
:
1698 case nir_type_unsigned
:
1703 unreachable("bad dest_type");
1706 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
,
1707 flags
, tex
->sampler_index
, tex
->sampler_index
,
1708 create_collect(b
, src0
, nsrc0
),
1709 create_collect(b
, src1
, nsrc1
));
1711 split_dest(b
, dst
, sam
, 4);
1715 emit_tex_query_levels(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1717 struct ir3_block
*b
= ctx
->block
;
1718 struct ir3_instruction
**dst
, *sam
;
1720 dst
= get_dst(ctx
, &tex
->dest
, 1);
1722 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, TGSI_WRITEMASK_Z
, 0,
1723 tex
->sampler_index
, tex
->sampler_index
, NULL
, NULL
);
1725 /* even though there is only one component, since it ends
1726 * up in .z rather than .x, we need a split_dest()
1728 split_dest(b
, dst
, sam
, 3);
1730 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1731 * the value in TEX_CONST_0 is zero-based.
1733 if (ctx
->levels_add_one
)
1734 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
1738 emit_tex_txs(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1740 struct ir3_block
*b
= ctx
->block
;
1741 struct ir3_instruction
**dst
, *sam
, *lod
;
1742 unsigned flags
, coords
;
1744 tex_info(tex
, &flags
, &coords
);
1746 dst
= get_dst(ctx
, &tex
->dest
, 4);
1748 compile_assert(ctx
, tex
->num_srcs
== 1);
1749 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
1751 lod
= get_src(ctx
, &tex
->src
[0].src
)[0];
1753 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
1754 tex
->sampler_index
, tex
->sampler_index
, lod
, NULL
);
1756 split_dest(b
, dst
, sam
, 4);
1758 /* Array size actually ends up in .w rather than .z. This doesn't
1759 * matter for miplevel 0, but for higher mips the value in z is
1760 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1761 * returned, which means that we have to add 1 to it for arrays.
1763 if (tex
->is_array
) {
1764 if (ctx
->levels_add_one
) {
1765 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
1767 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
1773 emit_phi(struct ir3_compile
*ctx
, nir_phi_instr
*nphi
)
1775 struct ir3_instruction
*phi
, **dst
;
1777 /* NOTE: phi's should be lowered to scalar at this point */
1778 compile_assert(ctx
, nphi
->dest
.ssa
.num_components
== 1);
1780 dst
= get_dst(ctx
, &nphi
->dest
, 1);
1782 phi
= ir3_instr_create2(ctx
->block
, -1, OPC_META_PHI
,
1783 1 + exec_list_length(&nphi
->srcs
));
1784 ir3_reg_create(phi
, 0, 0); /* dst */
1785 phi
->phi
.nphi
= nphi
;
1790 /* phi instructions are left partially constructed. We don't resolve
1791 * their srcs until the end of the block, since (eg. loops) one of
1792 * the phi's srcs might be defined after the phi due to back edges in
1796 resolve_phis(struct ir3_compile
*ctx
, struct ir3_block
*block
)
1798 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
1799 nir_phi_instr
*nphi
;
1801 /* phi's only come at start of block: */
1802 if (!(is_meta(instr
) && (instr
->opc
== OPC_META_PHI
)))
1805 if (!instr
->phi
.nphi
)
1808 nphi
= instr
->phi
.nphi
;
1809 instr
->phi
.nphi
= NULL
;
1811 foreach_list_typed(nir_phi_src
, nsrc
, node
, &nphi
->srcs
) {
1812 struct ir3_instruction
*src
= get_src(ctx
, &nsrc
->src
)[0];
1813 ir3_reg_create(instr
, 0, IR3_REG_SSA
)->instr
= src
;
1817 resolve_array_phis(ctx
, block
);
1821 emit_jump(struct ir3_compile
*ctx
, nir_jump_instr
*jump
)
1823 switch (jump
->type
) {
1824 case nir_jump_break
:
1825 case nir_jump_continue
:
1826 /* I *think* we can simply just ignore this, and use the
1827 * successor block link to figure out where we need to
1828 * jump to for break/continue
1832 compile_error(ctx
, "Unhandled NIR jump type: %d\n", jump
->type
);
1838 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1840 switch (instr
->type
) {
1841 case nir_instr_type_alu
:
1842 emit_alu(ctx
, nir_instr_as_alu(instr
));
1844 case nir_instr_type_intrinsic
:
1845 emit_intrinisic(ctx
, nir_instr_as_intrinsic(instr
));
1847 case nir_instr_type_load_const
:
1848 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1850 case nir_instr_type_ssa_undef
:
1851 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1853 case nir_instr_type_tex
: {
1854 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
1855 /* couple tex instructions get special-cased:
1859 emit_tex_txs(ctx
, tex
);
1861 case nir_texop_query_levels
:
1862 emit_tex_query_levels(ctx
, tex
);
1870 case nir_instr_type_phi
:
1871 emit_phi(ctx
, nir_instr_as_phi(instr
));
1873 case nir_instr_type_jump
:
1874 emit_jump(ctx
, nir_instr_as_jump(instr
));
1876 case nir_instr_type_call
:
1877 case nir_instr_type_parallel_copy
:
1878 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1883 static struct ir3_block
*
1884 get_block(struct ir3_compile
*ctx
, nir_block
*nblock
)
1886 struct ir3_block
*block
;
1887 struct hash_entry
*entry
;
1888 entry
= _mesa_hash_table_search(ctx
->block_ht
, nblock
);
1892 block
= ir3_block_create(ctx
->ir
);
1893 block
->nblock
= nblock
;
1894 _mesa_hash_table_insert(ctx
->block_ht
, nblock
, block
);
1900 emit_block(struct ir3_compile
*ctx
, nir_block
*nblock
)
1902 struct ir3_block
*block
= get_block(ctx
, nblock
);
1904 for (int i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
1905 if (nblock
->successors
[i
]) {
1906 block
->successors
[i
] =
1907 get_block(ctx
, nblock
->successors
[i
]);
1912 list_addtail(&block
->node
, &ctx
->ir
->block_list
);
1914 nir_foreach_instr(nblock
, instr
) {
1915 emit_instr(ctx
, instr
);
1921 static void emit_cf_list(struct ir3_compile
*ctx
, struct exec_list
*list
);
1924 emit_if(struct ir3_compile
*ctx
, nir_if
*nif
)
1926 struct ir3_instruction
*condition
= get_src(ctx
, &nif
->condition
)[0];
1928 ctx
->block
->condition
=
1929 get_predicate(ctx
, ir3_b2n(condition
->block
, condition
));
1931 emit_cf_list(ctx
, &nif
->then_list
);
1932 emit_cf_list(ctx
, &nif
->else_list
);
1936 emit_loop(struct ir3_compile
*ctx
, nir_loop
*nloop
)
1938 emit_cf_list(ctx
, &nloop
->body
);
1942 emit_cf_list(struct ir3_compile
*ctx
, struct exec_list
*list
)
1944 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1945 switch (node
->type
) {
1946 case nir_cf_node_block
:
1947 emit_block(ctx
, nir_cf_node_as_block(node
));
1949 case nir_cf_node_if
:
1950 emit_if(ctx
, nir_cf_node_as_if(node
));
1952 case nir_cf_node_loop
:
1953 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1955 case nir_cf_node_function
:
1956 compile_error(ctx
, "TODO\n");
1963 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1965 emit_cf_list(ctx
, &impl
->body
);
1966 emit_block(ctx
, impl
->end_block
);
1968 /* at this point, we should have a single empty block,
1969 * into which we emit the 'end' instruction.
1971 compile_assert(ctx
, list_empty(&ctx
->block
->instr_list
));
1972 ir3_END(ctx
->block
);
1976 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1978 struct ir3_shader_variant
*so
= ctx
->so
;
1979 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1980 unsigned ncomp
= glsl_get_components(in
->type
);
1981 /* XXX: map loc slots to semantics */
1982 unsigned semantic_name
= in
->data
.location
;
1983 unsigned semantic_index
= in
->data
.index
;
1984 unsigned n
= in
->data
.driver_location
;
1986 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1987 semantic_name
, semantic_index
, array_len
,
1990 so
->inputs
[n
].semantic
=
1991 ir3_semantic_name(semantic_name
, semantic_index
);
1992 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1993 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1994 so
->inputs
[n
].interpolate
= 0;
1995 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1997 /* the fdN_program_emit() code expects tgsi consts here, so map
1998 * things back to tgsi for now:
2000 switch (in
->data
.interpolation
) {
2001 case INTERP_QUALIFIER_FLAT
:
2002 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
2004 case INTERP_QUALIFIER_NOPERSPECTIVE
:
2005 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_LINEAR
;
2007 case INTERP_QUALIFIER_SMOOTH
:
2008 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
2012 for (int i
= 0; i
< ncomp
; i
++) {
2013 struct ir3_instruction
*instr
= NULL
;
2014 unsigned idx
= (n
* 4) + i
;
2016 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2017 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2018 so
->inputs
[n
].bary
= false;
2019 so
->frag_coord
= true;
2020 instr
= create_frag_coord(ctx
, i
);
2021 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
2022 so
->inputs
[n
].bary
= false;
2023 so
->frag_face
= true;
2024 instr
= create_frag_face(ctx
, i
);
2026 bool use_ldlv
= false;
2028 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2029 * from the semantic name:
2031 if ((in
->data
.interpolation
== INTERP_QUALIFIER_NONE
) &&
2032 ((semantic_name
== TGSI_SEMANTIC_COLOR
) ||
2033 (semantic_name
== TGSI_SEMANTIC_BCOLOR
)))
2034 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_COLOR
;
2036 if (ctx
->flat_bypass
) {
2037 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2038 * from the semantic name:
2040 switch (so
->inputs
[n
].interpolate
) {
2041 case TGSI_INTERPOLATE_COLOR
:
2042 if (!ctx
->so
->key
.rasterflat
)
2045 case TGSI_INTERPOLATE_CONSTANT
:
2051 so
->inputs
[n
].bary
= true;
2053 instr
= create_frag_input(ctx
,
2054 so
->inputs
[n
].inloc
+ i
- 8, use_ldlv
);
2057 instr
= create_input(ctx
->block
, NULL
, idx
);
2060 ctx
->ir
->inputs
[idx
] = instr
;
2063 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
2064 ctx
->next_inloc
+= ncomp
;
2065 so
->total_in
+= ncomp
;
2070 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
2072 struct ir3_shader_variant
*so
= ctx
->so
;
2073 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
2074 unsigned ncomp
= glsl_get_components(out
->type
);
2075 /* XXX: map loc slots to semantics */
2076 unsigned semantic_name
= out
->data
.location
;
2077 unsigned semantic_index
= out
->data
.index
;
2078 unsigned n
= out
->data
.driver_location
;
2081 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
2082 semantic_name
, semantic_index
, array_len
,
2085 if (ctx
->so
->type
== SHADER_VERTEX
) {
2086 switch (semantic_name
) {
2087 case TGSI_SEMANTIC_POSITION
:
2088 so
->writes_pos
= true;
2090 case TGSI_SEMANTIC_PSIZE
:
2091 so
->writes_psize
= true;
2093 case TGSI_SEMANTIC_COLOR
:
2094 case TGSI_SEMANTIC_BCOLOR
:
2095 case TGSI_SEMANTIC_GENERIC
:
2096 case TGSI_SEMANTIC_FOG
:
2097 case TGSI_SEMANTIC_TEXCOORD
:
2100 compile_error(ctx
, "unknown VS semantic name: %s\n",
2101 tgsi_semantic_names
[semantic_name
]);
2104 switch (semantic_name
) {
2105 case TGSI_SEMANTIC_POSITION
:
2106 comp
= 2; /* tgsi will write to .z component */
2107 so
->writes_pos
= true;
2109 case TGSI_SEMANTIC_COLOR
:
2110 if (semantic_index
== -1) {
2116 compile_error(ctx
, "unknown FS semantic name: %s\n",
2117 tgsi_semantic_names
[semantic_name
]);
2121 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
2123 so
->outputs
[n
].semantic
=
2124 ir3_semantic_name(semantic_name
, semantic_index
);
2125 so
->outputs
[n
].regid
= regid(n
, comp
);
2126 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
2128 for (int i
= 0; i
< ncomp
; i
++) {
2129 unsigned idx
= (n
* 4) + i
;
2131 ctx
->ir
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2136 emit_instructions(struct ir3_compile
*ctx
)
2138 unsigned ninputs
, noutputs
;
2139 nir_function_impl
*fxn
= NULL
;
2141 /* Find the main function: */
2142 nir_foreach_overload(ctx
->s
, overload
) {
2143 compile_assert(ctx
, strcmp(overload
->function
->name
, "main") == 0);
2144 compile_assert(ctx
, overload
->impl
);
2145 fxn
= overload
->impl
;
2149 ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
2150 noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
2152 /* we need to allocate big enough outputs array so that
2153 * we can stuff the kill's at the end. Likewise for vtx
2154 * shaders, we need to leave room for sysvals:
2156 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2157 noutputs
+= ARRAY_SIZE(ctx
->kill
);
2158 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
2162 ctx
->ir
= ir3_create(ctx
->compiler
, ninputs
, noutputs
);
2164 /* Create inputs in first block: */
2165 ctx
->block
= get_block(ctx
, fxn
->start_block
);
2166 ctx
->in_block
= ctx
->block
;
2167 list_addtail(&ctx
->block
->node
, &ctx
->ir
->block_list
);
2169 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2170 ctx
->ir
->noutputs
-= ARRAY_SIZE(ctx
->kill
);
2171 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
2172 ctx
->ir
->ninputs
-= 8;
2175 /* for fragment shader, we have a single input register (usually
2176 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2178 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2179 // TODO maybe a helper for fi since we need it a few places..
2180 struct ir3_instruction
*instr
;
2181 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
2182 ir3_reg_create(instr
, 0, 0);
2183 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
2184 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
2185 ctx
->frag_pos
= instr
;
2189 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->inputs
) {
2190 setup_input(ctx
, var
);
2193 /* Setup outputs: */
2194 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->outputs
) {
2195 setup_output(ctx
, var
);
2198 /* Setup variables (which should only be arrays): */
2199 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->globals
) {
2200 declare_var(ctx
, var
);
2203 /* And emit the body: */
2205 emit_function(ctx
, fxn
);
2207 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
2208 resolve_phis(ctx
, block
);
2212 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2213 * for a fragment shader are just bary.f instructions. The *actual* inputs
2214 * from the hw perspective are the frag_pos and optionally frag_coord and
2218 fixup_frag_inputs(struct ir3_compile
*ctx
)
2220 struct ir3_shader_variant
*so
= ctx
->so
;
2221 struct ir3
*ir
= ctx
->ir
;
2222 struct ir3_instruction
**inputs
;
2223 struct ir3_instruction
*instr
;
2228 n
= 4; /* always have frag_pos */
2229 n
+= COND(so
->frag_face
, 4);
2230 n
+= COND(so
->frag_coord
, 4);
2232 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
2234 if (so
->frag_face
) {
2235 /* this ultimately gets assigned to hr0.x so doesn't conflict
2236 * with frag_coord/frag_pos..
2238 inputs
[ir
->ninputs
++] = ctx
->frag_face
;
2239 ctx
->frag_face
->regs
[0]->num
= 0;
2241 /* remaining channels not used, but let's avoid confusing
2242 * other parts that expect inputs to come in groups of vec4
2244 inputs
[ir
->ninputs
++] = NULL
;
2245 inputs
[ir
->ninputs
++] = NULL
;
2246 inputs
[ir
->ninputs
++] = NULL
;
2249 /* since we don't know where to set the regid for frag_coord,
2250 * we have to use r0.x for it. But we don't want to *always*
2251 * use r1.x for frag_pos as that could increase the register
2252 * footprint on simple shaders:
2254 if (so
->frag_coord
) {
2255 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
2256 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
2257 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
2258 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
2260 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[0];
2261 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[1];
2262 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[2];
2263 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[3];
2266 /* we always have frag_pos: */
2267 so
->pos_regid
= regid
;
2270 instr
= create_input(ctx
->in_block
, NULL
, ir
->ninputs
);
2271 instr
->regs
[0]->num
= regid
++;
2272 inputs
[ir
->ninputs
++] = instr
;
2273 ctx
->frag_pos
->regs
[1]->instr
= instr
;
2276 instr
= create_input(ctx
->in_block
, NULL
, ir
->ninputs
);
2277 instr
->regs
[0]->num
= regid
++;
2278 inputs
[ir
->ninputs
++] = instr
;
2279 ctx
->frag_pos
->regs
[2]->instr
= instr
;
2281 ir
->inputs
= inputs
;
2285 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
2286 struct ir3_shader_variant
*so
,
2287 const struct tgsi_token
*tokens
,
2288 struct ir3_shader_key key
)
2290 struct ir3_compile
*ctx
;
2292 struct ir3_instruction
**inputs
;
2293 unsigned i
, j
, actual_in
;
2294 int ret
= 0, max_bary
;
2298 ctx
= compile_init(compiler
, so
, tokens
);
2300 DBG("INIT failed!");
2305 emit_instructions(ctx
);
2308 DBG("EMIT failed!");
2313 ir
= so
->ir
= ctx
->ir
;
2315 /* keep track of the inputs from TGSI perspective.. */
2316 inputs
= ir
->inputs
;
2318 /* but fixup actual inputs for frag shader: */
2319 if (so
->type
== SHADER_FRAGMENT
)
2320 fixup_frag_inputs(ctx
);
2322 /* at this point, for binning pass, throw away unneeded outputs: */
2323 if (key
.binning_pass
) {
2324 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
2325 unsigned name
= sem2name(so
->outputs
[i
].semantic
);
2326 unsigned idx
= sem2idx(so
->outputs
[i
].semantic
);
2328 /* throw away everything but first position/psize */
2329 if ((idx
== 0) && ((name
== TGSI_SEMANTIC_POSITION
) ||
2330 (name
== TGSI_SEMANTIC_PSIZE
))) {
2332 so
->outputs
[j
] = so
->outputs
[i
];
2333 ir
->outputs
[(j
*4)+0] = ir
->outputs
[(i
*4)+0];
2334 ir
->outputs
[(j
*4)+1] = ir
->outputs
[(i
*4)+1];
2335 ir
->outputs
[(j
*4)+2] = ir
->outputs
[(i
*4)+2];
2336 ir
->outputs
[(j
*4)+3] = ir
->outputs
[(i
*4)+3];
2341 so
->outputs_count
= j
;
2342 ir
->noutputs
= j
* 4;
2345 /* if we want half-precision outputs, mark the output registers
2348 if (key
.half_precision
) {
2349 for (i
= 0; i
< ir
->noutputs
; i
++) {
2350 struct ir3_instruction
*out
= ir
->outputs
[i
];
2353 out
->regs
[0]->flags
|= IR3_REG_HALF
;
2354 /* output could be a fanout (ie. texture fetch output)
2355 * in which case we need to propagate the half-reg flag
2356 * up to the definer so that RA sees it:
2358 if (is_meta(out
) && (out
->opc
== OPC_META_FO
)) {
2359 out
= out
->regs
[1]->instr
;
2360 out
->regs
[0]->flags
|= IR3_REG_HALF
;
2363 if (out
->category
== 1) {
2364 out
->cat1
.dst_type
= half_type(out
->cat1
.dst_type
);
2369 /* at this point, we want the kill's in the outputs array too,
2370 * so that they get scheduled (since they have no dst).. we've
2371 * already ensured that the array is big enough in push_block():
2373 if (so
->type
== SHADER_FRAGMENT
) {
2374 for (i
= 0; i
< ctx
->kill_count
; i
++)
2375 ir
->outputs
[ir
->noutputs
++] = ctx
->kill
[i
];
2378 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2379 printf("BEFORE CP:\n");
2385 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2386 printf("BEFORE GROUPING:\n");
2390 /* Group left/right neighbors, inserting mov's where needed to
2397 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2398 printf("AFTER DEPTH:\n");
2402 ret
= ir3_sched(ir
);
2404 DBG("SCHED failed!");
2408 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2409 printf("AFTER SCHED:\n");
2413 ret
= ir3_ra(ir
, so
->type
, so
->frag_coord
, so
->frag_face
);
2419 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2420 printf("AFTER RA:\n");
2424 ir3_legalize(ir
, &so
->has_samp
, &max_bary
);
2426 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2427 printf("AFTER LEGALIZE:\n");
2431 /* fixup input/outputs: */
2432 for (i
= 0; i
< so
->outputs_count
; i
++) {
2433 so
->outputs
[i
].regid
= ir
->outputs
[i
*4]->regs
[0]->num
;
2434 /* preserve hack for depth output.. tgsi writes depth to .z,
2435 * but what we give the hw is the scalar register:
2437 if ((so
->type
== SHADER_FRAGMENT
) &&
2438 (sem2name(so
->outputs
[i
].semantic
) == TGSI_SEMANTIC_POSITION
))
2439 so
->outputs
[i
].regid
+= 2;
2442 /* Note that some or all channels of an input may be unused: */
2444 for (i
= 0; i
< so
->inputs_count
; i
++) {
2445 unsigned j
, regid
= ~0, compmask
= 0;
2446 so
->inputs
[i
].ncomp
= 0;
2447 for (j
= 0; j
< 4; j
++) {
2448 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
2450 compmask
|= (1 << j
);
2451 regid
= in
->regs
[0]->num
- j
;
2453 so
->inputs
[i
].ncomp
++;
2456 so
->inputs
[i
].regid
= regid
;
2457 so
->inputs
[i
].compmask
= compmask
;
2460 /* fragment shader always gets full vec4's even if it doesn't
2461 * fetch all components, but vertex shader we need to update
2462 * with the actual number of components fetch, otherwise thing
2463 * will hang due to mismaptch between VFD_DECODE's and
2466 if (so
->type
== SHADER_VERTEX
)
2467 so
->total_in
= actual_in
;
2469 so
->total_in
= align(max_bary
+ 1, 4);
2474 ir3_destroy(so
->ir
);