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 /* on a3xx, we need to add one to # of array levels:
106 /* for looking up which system value is which */
107 unsigned sysval_semantics
[8];
109 /* list of kill instructions: */
110 struct ir3_instruction
*kill
[16];
111 unsigned int kill_count
;
113 /* set if we encounter something we can't handle yet, so we
114 * can bail cleanly and fallback to TGSI compiler f/e
120 static struct nir_shader
*to_nir(const struct tgsi_token
*tokens
)
122 struct nir_shader_compiler_options options
= {
127 .native_integers
= true,
131 struct nir_shader
*s
= tgsi_to_nir(tokens
, &options
);
133 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
134 debug_printf("----------------------\n");
135 nir_print_shader(s
, stdout
);
136 debug_printf("----------------------\n");
139 nir_opt_global_to_local(s
);
140 nir_convert_to_ssa(s
);
146 nir_lower_vars_to_ssa(s
);
147 nir_lower_alu_to_scalar(s
);
149 progress
|= nir_copy_prop(s
);
150 progress
|= nir_opt_dce(s
);
151 progress
|= nir_opt_cse(s
);
152 progress
|= nir_opt_peephole_select(s
);
153 progress
|= nir_opt_algebraic(s
);
154 progress
|= nir_opt_constant_folding(s
);
158 nir_remove_dead_variables(s
);
159 nir_validate_shader(s
);
161 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
162 debug_printf("----------------------\n");
163 nir_print_shader(s
, stdout
);
164 debug_printf("----------------------\n");
170 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
171 static const struct tgsi_token
*
172 lower_tgsi(const struct tgsi_token
*tokens
, struct ir3_shader_variant
*so
)
174 struct tgsi_shader_info info
;
175 struct tgsi_lowering_config lconfig
= {
176 .color_two_side
= so
->key
.color_two_side
,
181 case SHADER_FRAGMENT
:
183 lconfig
.saturate_s
= so
->key
.fsaturate_s
;
184 lconfig
.saturate_t
= so
->key
.fsaturate_t
;
185 lconfig
.saturate_r
= so
->key
.fsaturate_r
;
188 lconfig
.saturate_s
= so
->key
.vsaturate_s
;
189 lconfig
.saturate_t
= so
->key
.vsaturate_t
;
190 lconfig
.saturate_r
= so
->key
.vsaturate_r
;
195 /* hack for standalone compiler which does not have
198 } else if (ir3_shader_gpuid(so
->shader
) >= 400) {
199 /* a4xx seems to have *no* sam.p */
200 lconfig
.lower_TXP
= ~0; /* lower all txp */
202 /* a3xx just needs to avoid sam.p for 3d tex */
203 lconfig
.lower_TXP
= (1 << TGSI_TEXTURE_3D
);
206 return tgsi_transform_lowering(&lconfig
, tokens
, &info
);
209 static struct ir3_compile
*
210 compile_init(struct ir3_shader_variant
*so
,
211 const struct tgsi_token
*tokens
)
213 struct ir3_compile
*ctx
= rzalloc(NULL
, struct ir3_compile
);
214 const struct tgsi_token
*lowered_tokens
;
217 /* hack for standalone compiler which does not have
220 } else if (ir3_shader_gpuid(so
->shader
) >= 400) {
221 /* need special handling for "flat" */
222 ctx
->flat_bypass
= true;
223 ctx
->levels_add_one
= false;
225 /* no special handling for "flat" */
226 ctx
->flat_bypass
= false;
227 ctx
->levels_add_one
= true;
231 case SHADER_FRAGMENT
:
233 ctx
->integer_s
= so
->key
.finteger_s
;
236 ctx
->integer_s
= so
->key
.vinteger_s
;
243 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
244 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
245 ctx
->var_ht
= _mesa_hash_table_create(ctx
,
246 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
247 ctx
->addr_ht
= _mesa_hash_table_create(ctx
,
248 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
250 lowered_tokens
= lower_tgsi(tokens
, so
);
252 lowered_tokens
= tokens
;
253 ctx
->s
= to_nir(lowered_tokens
);
255 if (lowered_tokens
!= tokens
)
256 free((void *)lowered_tokens
);
258 so
->first_driver_param
= so
->first_immediate
= ctx
->s
->num_uniforms
;
260 /* one (vec4) slot for vertex id base: */
261 if (so
->type
== SHADER_VERTEX
)
262 so
->first_immediate
++;
264 /* reserve 4 (vec4) slots for ubo base addresses: */
265 so
->first_immediate
+= 4;
271 compile_error(struct ir3_compile
*ctx
, const char *format
, ...)
274 va_start(ap
, format
);
275 _debug_vprintf(format
, ap
);
277 nir_print_shader(ctx
->s
, stdout
);
282 #define compile_assert(ctx, cond) do { \
283 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
287 compile_free(struct ir3_compile
*ctx
)
294 unsigned length
, aid
;
295 struct ir3_instruction
*arr
[];
299 declare_var(struct ir3_compile
*ctx
, nir_variable
*var
)
301 unsigned length
= glsl_get_length(var
->type
) * 4; /* always vec4, at least with ttn */
302 struct ir3_array
*arr
= ralloc_size(ctx
, sizeof(*arr
) +
303 (length
* sizeof(arr
->arr
[0])));
304 arr
->length
= length
;
305 arr
->aid
= ++ctx
->num_arrays
;
306 /* Some shaders end up reading array elements without first writing..
307 * so initialize things to prevent null instr ptrs later:
309 for (unsigned i
= 0; i
< length
; i
++)
310 arr
->arr
[i
] = create_immed(ctx
->block
, 0);
311 _mesa_hash_table_insert(ctx
->var_ht
, var
, arr
);
314 static struct ir3_array
*
315 get_var(struct ir3_compile
*ctx
, nir_variable
*var
)
317 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->var_ht
, var
);
321 /* allocate a n element value array (to be populated by caller) and
324 static struct ir3_instruction
**
325 __get_dst(struct ir3_compile
*ctx
, void *key
, unsigned n
)
327 struct ir3_instruction
**value
=
328 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
329 _mesa_hash_table_insert(ctx
->def_ht
, key
, value
);
333 static struct ir3_instruction
**
334 get_dst(struct ir3_compile
*ctx
, nir_dest
*dst
, unsigned n
)
337 return __get_dst(ctx
, &dst
->ssa
, n
);
339 return __get_dst(ctx
, dst
->reg
.reg
, n
);
343 static struct ir3_instruction
**
344 get_dst_ssa(struct ir3_compile
*ctx
, nir_ssa_def
*dst
, unsigned n
)
346 return __get_dst(ctx
, dst
, n
);
349 static struct ir3_instruction
**
350 get_src(struct ir3_compile
*ctx
, nir_src
*src
)
352 struct hash_entry
*entry
;
354 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
356 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->reg
.reg
);
358 compile_assert(ctx
, entry
);
362 static struct ir3_instruction
*
363 create_immed(struct ir3_block
*block
, uint32_t val
)
365 struct ir3_instruction
*mov
;
367 mov
= ir3_instr_create(block
, 1, 0);
368 mov
->cat1
.src_type
= TYPE_U32
;
369 mov
->cat1
.dst_type
= TYPE_U32
;
370 ir3_reg_create(mov
, 0, 0);
371 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
376 static struct ir3_instruction
*
377 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
)
379 struct ir3_instruction
*instr
, *immed
;
381 /* TODO in at least some cases, the backend could probably be
382 * made clever enough to propagate IR3_REG_HALF..
384 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
385 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
387 immed
= create_immed(block
, 2);
388 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
390 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
391 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
392 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
394 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
395 instr
->regs
[0]->flags
|= IR3_REG_ADDR
| IR3_REG_HALF
;
396 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
401 /* caches addr values to avoid generating multiple cov/shl/mova
402 * sequences for each use of a given NIR level src as address
404 static struct ir3_instruction
*
405 get_addr(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
407 struct ir3_instruction
*addr
;
408 struct hash_entry
*entry
;
409 entry
= _mesa_hash_table_search(ctx
->addr_ht
, src
);
413 /* TODO do we need to cache per block? */
414 addr
= create_addr(ctx
->block
, src
);
415 _mesa_hash_table_insert(ctx
->addr_ht
, src
, addr
);
420 static struct ir3_instruction
*
421 create_uniform(struct ir3_compile
*ctx
, unsigned n
)
423 struct ir3_instruction
*mov
;
425 mov
= ir3_instr_create(ctx
->block
, 1, 0);
426 /* TODO get types right? */
427 mov
->cat1
.src_type
= TYPE_F32
;
428 mov
->cat1
.dst_type
= TYPE_F32
;
429 ir3_reg_create(mov
, 0, 0);
430 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
435 static struct ir3_instruction
*
436 create_uniform_indirect(struct ir3_compile
*ctx
, unsigned n
,
437 struct ir3_instruction
*address
)
439 struct ir3_instruction
*mov
;
441 mov
= ir3_instr_create(ctx
->block
, 1, 0);
442 mov
->cat1
.src_type
= TYPE_U32
;
443 mov
->cat1
.dst_type
= TYPE_U32
;
444 ir3_reg_create(mov
, 0, 0);
445 ir3_reg_create(mov
, n
, IR3_REG_CONST
| IR3_REG_RELATIV
);
446 mov
->address
= address
;
448 array_insert(ctx
->ir
->indirects
, mov
);
453 static struct ir3_instruction
*
454 create_collect(struct ir3_block
*block
, struct ir3_instruction
**arr
,
457 struct ir3_instruction
*collect
;
462 collect
= ir3_instr_create2(block
, -1, OPC_META_FI
, 1 + arrsz
);
463 ir3_reg_create(collect
, 0, 0);
464 for (unsigned i
= 0; i
< arrsz
; i
++)
465 ir3_reg_create(collect
, 0, IR3_REG_SSA
)->instr
= arr
[i
];
470 static struct ir3_instruction
*
471 create_indirect_load(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
472 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
474 struct ir3_block
*block
= ctx
->block
;
475 struct ir3_instruction
*mov
;
476 struct ir3_register
*src
;
478 mov
= ir3_instr_create(block
, 1, 0);
479 mov
->cat1
.src_type
= TYPE_U32
;
480 mov
->cat1
.dst_type
= TYPE_U32
;
481 ir3_reg_create(mov
, 0, 0);
482 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
483 src
->instr
= collect
;
486 mov
->address
= address
;
488 array_insert(ctx
->ir
->indirects
, mov
);
493 static struct ir3_instruction
*
494 create_indirect_store(struct ir3_compile
*ctx
, unsigned arrsz
, unsigned n
,
495 struct ir3_instruction
*src
, struct ir3_instruction
*address
,
496 struct ir3_instruction
*collect
)
498 struct ir3_block
*block
= ctx
->block
;
499 struct ir3_instruction
*mov
;
500 struct ir3_register
*dst
;
502 mov
= ir3_instr_create(block
, 1, 0);
503 mov
->cat1
.src_type
= TYPE_U32
;
504 mov
->cat1
.dst_type
= TYPE_U32
;
505 dst
= ir3_reg_create(mov
, 0, IR3_REG_RELATIV
);
508 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
509 mov
->address
= address
;
510 mov
->fanin
= collect
;
512 array_insert(ctx
->ir
->indirects
, mov
);
517 static struct ir3_instruction
*
518 create_input(struct ir3_block
*block
, struct ir3_instruction
*instr
,
521 struct ir3_instruction
*in
;
523 in
= ir3_instr_create(block
, -1, OPC_META_INPUT
);
524 in
->inout
.block
= block
;
525 ir3_reg_create(in
, n
, 0);
527 ir3_reg_create(in
, 0, IR3_REG_SSA
)->instr
= instr
;
532 static struct ir3_instruction
*
533 create_frag_input(struct ir3_compile
*ctx
, unsigned n
, bool use_ldlv
)
535 struct ir3_block
*block
= ctx
->block
;
536 struct ir3_instruction
*instr
;
537 struct ir3_instruction
*inloc
= create_immed(block
, n
);
540 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
541 instr
->cat6
.type
= TYPE_U32
;
542 instr
->cat6
.iim_val
= 1;
544 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_pos
, 0);
545 instr
->regs
[2]->wrmask
= 0x3;
551 static struct ir3_instruction
*
552 create_frag_coord(struct ir3_compile
*ctx
, unsigned comp
)
554 struct ir3_block
*block
= ctx
->block
;
555 struct ir3_instruction
*instr
;
557 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
559 ctx
->frag_coord
[comp
] = create_input(ctx
->block
, NULL
, 0);
564 /* for frag_coord, we get unsigned values.. we need
565 * to subtract (integer) 8 and divide by 16 (right-
566 * shift by 4) then convert to float:
570 * mov.u32f32 dst, tmp
573 instr
= ir3_ADD_S(block
, ctx
->frag_coord
[comp
], 0,
574 create_immed(block
, -8), 0);
575 instr
= ir3_SHR_B(block
, instr
, 0,
576 create_immed(block
, 4), 0);
577 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
583 /* seems that we can use these as-is: */
584 return ctx
->frag_coord
[comp
];
588 static struct ir3_instruction
*
589 create_frag_face(struct ir3_compile
*ctx
, unsigned comp
)
591 struct ir3_block
*block
= ctx
->block
;
592 struct ir3_instruction
*instr
;
596 compile_assert(ctx
, !ctx
->frag_face
);
598 ctx
->frag_face
= create_input(block
, NULL
, 0);
600 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
601 * positive vs negative float.. and piglit further seems to
602 * expect -1.0 or 1.0:
604 * mul.s tmp, hr0.x, 2
606 * mov.s32f32, dst, tmp
609 instr
= ir3_MUL_S(block
, ctx
->frag_face
, 0,
610 create_immed(block
, 2), 0);
611 instr
= ir3_ADD_S(block
, instr
, 0,
612 create_immed(block
, 1), 0);
613 instr
= ir3_COV(block
, instr
, TYPE_S32
, TYPE_F32
);
618 return create_immed(block
, fui(0.0));
621 return create_immed(block
, fui(1.0));
625 /* helper for instructions that produce multiple consecutive scalar
626 * outputs which need to have a split/fanout meta instruction inserted
629 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
630 struct ir3_instruction
*src
)
632 struct ir3_instruction
*prev
= NULL
;
633 for (int i
= 0, j
= 0; i
< 4; i
++) {
634 struct ir3_instruction
*split
=
635 ir3_instr_create(block
, -1, OPC_META_FO
);
636 ir3_reg_create(split
, 0, IR3_REG_SSA
);
637 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
641 split
->cp
.left
= prev
;
642 split
->cp
.left_cnt
++;
643 prev
->cp
.right
= split
;
644 prev
->cp
.right_cnt
++;
648 if (src
->regs
[0]->wrmask
& (1 << i
))
654 * Adreno uses uint rather than having dedicated bool type,
655 * which (potentially) requires some conversion, in particular
656 * when using output of an bool instr to int input, or visa
660 * -------+---------+-------+-
664 * To convert from an adreno bool (uint) to nir, use:
666 * absneg.s dst, (neg)src
668 * To convert back in the other direction:
670 * absneg.s dst, (abs)arc
672 * The CP step can clean up the absneg.s that cancel each other
673 * out, and with a slight bit of extra cleverness (to recognize
674 * the instructions which produce either a 0 or 1) can eliminate
675 * the absneg.s's completely when an instruction that wants
676 * 0/1 consumes the result. For example, when a nir 'bcsel'
677 * consumes the result of 'feq'. So we should be able to get by
678 * without a boolean resolve step, and without incuring any
679 * extra penalty in instruction count.
682 /* NIR bool -> native (adreno): */
683 static struct ir3_instruction
*
684 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
686 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
689 /* native (adreno) -> NIR bool: */
690 static struct ir3_instruction
*
691 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
693 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
697 * alu/sfu instructions:
701 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
703 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
704 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
705 struct ir3_block
*b
= ctx
->block
;
707 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
709 /* Vectors are special in that they have non-scalarized writemasks,
710 * and just take the first swizzle channel for each argument in
711 * order into each writemask channel.
713 if ((alu
->op
== nir_op_vec2
) ||
714 (alu
->op
== nir_op_vec3
) ||
715 (alu
->op
== nir_op_vec4
)) {
717 for (int i
= 0; i
< info
->num_inputs
; i
++) {
718 nir_alu_src
*asrc
= &alu
->src
[i
];
720 compile_assert(ctx
, !asrc
->abs
);
721 compile_assert(ctx
, !asrc
->negate
);
723 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
725 src
[i
] = create_immed(ctx
->block
, 0);
726 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
732 /* General case: We can just grab the one used channel per src. */
733 for (int i
= 0; i
< info
->num_inputs
; i
++) {
734 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
735 nir_alu_src
*asrc
= &alu
->src
[i
];
737 compile_assert(ctx
, !asrc
->abs
);
738 compile_assert(ctx
, !asrc
->negate
);
740 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
742 compile_assert(ctx
, src
[i
]);
747 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
750 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
753 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
756 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
759 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
762 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
765 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
766 dst
[0]->cat2
.condition
= IR3_COND_NE
;
767 dst
[0] = ir3_n2b(b
, dst
[0]);
770 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
773 dst
[0] = ir3_b2n(b
, src
[0]);
776 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
777 dst
[0]->cat2
.condition
= IR3_COND_NE
;
778 dst
[0] = ir3_n2b(b
, dst
[0]);
782 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
785 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
788 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
791 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
794 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
797 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
800 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
803 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
806 dst
[0] = ir3_DSX(b
, src
[0], 0);
807 dst
[0]->cat5
.type
= TYPE_F32
;
810 dst
[0] = ir3_DSY(b
, src
[0], 0);
811 dst
[0]->cat5
.type
= TYPE_F32
;
815 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
816 dst
[0]->cat2
.condition
= IR3_COND_LT
;
817 dst
[0] = ir3_n2b(b
, dst
[0]);
820 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
821 dst
[0]->cat2
.condition
= IR3_COND_GE
;
822 dst
[0] = ir3_n2b(b
, dst
[0]);
825 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
826 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
827 dst
[0] = ir3_n2b(b
, dst
[0]);
830 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
831 dst
[0]->cat2
.condition
= IR3_COND_NE
;
832 dst
[0] = ir3_n2b(b
, dst
[0]);
835 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
838 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
841 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
843 case nir_op_fround_even
:
844 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
847 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
851 dst
[0] = ir3_SIN(b
, src
[0], 0);
854 dst
[0] = ir3_COS(b
, src
[0], 0);
857 dst
[0] = ir3_RSQ(b
, src
[0], 0);
860 dst
[0] = ir3_RCP(b
, src
[0], 0);
863 dst
[0] = ir3_LOG2(b
, src
[0], 0);
866 dst
[0] = ir3_EXP2(b
, src
[0], 0);
869 dst
[0] = ir3_SQRT(b
, src
[0], 0);
873 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
876 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
879 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
882 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
885 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
889 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
890 * mull.u tmp0, a, b ; mul low, i.e. al * bl
891 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
892 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
894 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
895 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
896 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
899 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
902 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
905 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
908 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
911 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
914 /* maybe this would be sane to lower in nir.. */
915 struct ir3_instruction
*neg
, *pos
;
917 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
918 neg
->cat2
.condition
= IR3_COND_LT
;
920 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
921 pos
->cat2
.condition
= IR3_COND_GT
;
923 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
928 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
931 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
934 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
937 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
938 dst
[0]->cat2
.condition
= IR3_COND_LT
;
939 dst
[0] = ir3_n2b(b
, dst
[0]);
942 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
943 dst
[0]->cat2
.condition
= IR3_COND_GE
;
944 dst
[0] = ir3_n2b(b
, dst
[0]);
947 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
948 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
949 dst
[0] = ir3_n2b(b
, dst
[0]);
952 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
953 dst
[0]->cat2
.condition
= IR3_COND_NE
;
954 dst
[0] = ir3_n2b(b
, dst
[0]);
957 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
958 dst
[0]->cat2
.condition
= IR3_COND_LT
;
959 dst
[0] = ir3_n2b(b
, dst
[0]);
962 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
963 dst
[0]->cat2
.condition
= IR3_COND_GE
;
964 dst
[0] = ir3_n2b(b
, dst
[0]);
968 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
972 compile_error(ctx
, "Unhandled ALU op: %s\n",
973 nir_op_infos
[alu
->op
].name
);
978 /* handles array reads: */
980 emit_intrinisic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
981 struct ir3_instruction
**dst
)
983 nir_deref_var
*dvar
= intr
->variables
[0];
984 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
985 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
987 compile_assert(ctx
, dvar
->deref
.child
&&
988 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
990 switch (darr
->deref_array_type
) {
991 case nir_deref_array_type_direct
:
992 /* direct access does not require anything special: */
993 for (int i
= 0; i
< intr
->num_components
; i
++) {
994 unsigned n
= darr
->base_offset
* 4 + i
;
995 compile_assert(ctx
, n
< arr
->length
);
996 dst
[i
] = arr
->arr
[n
];
999 case nir_deref_array_type_indirect
: {
1000 /* for indirect, we need to collect all the array elements: */
1001 struct ir3_instruction
*collect
=
1002 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1003 struct ir3_instruction
*addr
=
1004 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1005 for (int i
= 0; i
< intr
->num_components
; i
++) {
1006 unsigned n
= darr
->base_offset
* 4 + i
;
1007 compile_assert(ctx
, n
< arr
->length
);
1008 dst
[i
] = create_indirect_load(ctx
, arr
->length
, n
, addr
, collect
);
1013 compile_error(ctx
, "Unhandled load deref type: %u\n",
1014 darr
->deref_array_type
);
1019 /* handles array writes: */
1021 emit_intrinisic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1023 nir_deref_var
*dvar
= intr
->variables
[0];
1024 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1025 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1026 struct ir3_instruction
**src
;
1028 compile_assert(ctx
, dvar
->deref
.child
&&
1029 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1031 src
= get_src(ctx
, &intr
->src
[0]);
1033 switch (darr
->deref_array_type
) {
1034 case nir_deref_array_type_direct
:
1035 /* direct access does not require anything special: */
1036 for (int i
= 0; i
< intr
->num_components
; i
++) {
1037 unsigned n
= darr
->base_offset
* 4 + i
;
1038 compile_assert(ctx
, n
< arr
->length
);
1039 arr
->arr
[n
] = src
[i
];
1042 case nir_deref_array_type_indirect
: {
1043 /* for indirect, create indirect-store and fan that out: */
1044 struct ir3_instruction
*collect
=
1045 create_collect(ctx
->block
, arr
->arr
, arr
->length
);
1046 struct ir3_instruction
*addr
=
1047 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1048 for (int i
= 0; i
< intr
->num_components
; i
++) {
1049 struct ir3_instruction
*store
;
1050 unsigned n
= darr
->base_offset
* 4 + i
;
1051 compile_assert(ctx
, n
< arr
->length
);
1053 store
= create_indirect_store(ctx
, arr
->length
,
1054 n
, src
[i
], addr
, collect
);
1056 store
->fanin
->fi
.aid
= arr
->aid
;
1058 /* TODO: probably split this out to be used for
1059 * store_output_indirect? or move this into
1060 * create_indirect_store()?
1062 for (int j
= i
; j
< arr
->length
; j
+= 4) {
1063 struct ir3_instruction
*split
;
1065 split
= ir3_instr_create(ctx
->block
, -1, OPC_META_FO
);
1067 ir3_reg_create(split
, 0, 0);
1068 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= store
;
1070 arr
->arr
[j
] = split
;
1076 compile_error(ctx
, "Unhandled store deref type: %u\n",
1077 darr
->deref_array_type
);
1082 static void add_sysval_input(struct ir3_compile
*ctx
, unsigned name
,
1083 struct ir3_instruction
*instr
)
1085 struct ir3_shader_variant
*so
= ctx
->so
;
1086 unsigned r
= regid(so
->inputs_count
, 0);
1087 unsigned n
= so
->inputs_count
++;
1089 so
->inputs
[n
].semantic
= ir3_semantic_name(name
, 0);
1090 so
->inputs
[n
].compmask
= 1;
1091 so
->inputs
[n
].regid
= r
;
1092 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1095 ctx
->block
->inputs
[r
] = instr
;
1099 emit_intrinisic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1101 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1102 struct ir3_instruction
**dst
, **src
;
1103 struct ir3_block
*b
= ctx
->block
;
1104 unsigned idx
= intr
->const_index
[0];
1106 if (info
->has_dest
) {
1107 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1110 switch (intr
->intrinsic
) {
1111 case nir_intrinsic_load_uniform
:
1112 compile_assert(ctx
, intr
->const_index
[1] == 1);
1113 for (int i
= 0; i
< intr
->num_components
; i
++) {
1114 unsigned n
= idx
* 4 + i
;
1115 dst
[i
] = create_uniform(ctx
, n
);
1118 case nir_intrinsic_load_uniform_indirect
:
1119 compile_assert(ctx
, intr
->const_index
[1] == 1);
1120 src
= get_src(ctx
, &intr
->src
[0]);
1121 for (int i
= 0; i
< intr
->num_components
; i
++) {
1122 unsigned n
= idx
* 4 + i
;
1123 dst
[i
] = create_uniform_indirect(ctx
, n
,
1124 get_addr(ctx
, src
[0]));
1127 case nir_intrinsic_load_input
:
1128 compile_assert(ctx
, intr
->const_index
[1] == 1);
1129 for (int i
= 0; i
< intr
->num_components
; i
++) {
1130 unsigned n
= idx
* 4 + i
;
1131 dst
[i
] = b
->inputs
[n
];
1134 case nir_intrinsic_load_input_indirect
:
1135 compile_assert(ctx
, intr
->const_index
[1] == 1);
1136 src
= get_src(ctx
, &intr
->src
[0]);
1137 struct ir3_instruction
*collect
=
1138 create_collect(b
, b
->inputs
, b
->ninputs
);
1139 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1140 for (int i
= 0; i
< intr
->num_components
; i
++) {
1141 unsigned n
= idx
* 4 + i
;
1142 dst
[i
] = create_indirect_load(ctx
, b
->ninputs
, n
, addr
, collect
);
1145 case nir_intrinsic_load_var
:
1146 emit_intrinisic_load_var(ctx
, intr
, dst
);
1148 case nir_intrinsic_store_var
:
1149 emit_intrinisic_store_var(ctx
, intr
);
1151 case nir_intrinsic_store_output
:
1152 compile_assert(ctx
, intr
->const_index
[1] == 1);
1153 src
= get_src(ctx
, &intr
->src
[0]);
1154 for (int i
= 0; i
< intr
->num_components
; i
++) {
1155 unsigned n
= idx
* 4 + i
;
1156 b
->outputs
[n
] = src
[i
];
1159 case nir_intrinsic_load_base_vertex
:
1160 if (!ctx
->basevertex
) {
1161 /* first four vec4 sysval's reserved for UBOs: */
1162 unsigned r
= regid(ctx
->so
->first_driver_param
+ 4, 0);
1163 ctx
->basevertex
= create_uniform(ctx
, r
);
1164 add_sysval_input(ctx
, TGSI_SEMANTIC_BASEVERTEX
,
1167 dst
[0] = ctx
->basevertex
;
1169 case nir_intrinsic_load_vertex_id_zero_base
:
1170 if (!ctx
->vertex_id
) {
1171 ctx
->vertex_id
= create_input(ctx
->block
, NULL
, 0);
1172 add_sysval_input(ctx
, TGSI_SEMANTIC_VERTEXID_NOBASE
,
1175 dst
[0] = ctx
->vertex_id
;
1177 case nir_intrinsic_load_instance_id
:
1178 if (!ctx
->instance_id
) {
1179 ctx
->instance_id
= create_input(ctx
->block
, NULL
, 0);
1180 add_sysval_input(ctx
, TGSI_SEMANTIC_INSTANCEID
,
1183 dst
[0] = ctx
->instance_id
;
1185 case nir_intrinsic_discard_if
:
1186 case nir_intrinsic_discard
: {
1187 struct ir3_instruction
*cond
, *kill
;
1189 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1190 /* conditional discard: */
1191 src
= get_src(ctx
, &intr
->src
[0]);
1192 cond
= ir3_b2n(b
, src
[0]);
1194 /* unconditional discard: */
1195 cond
= create_immed(b
, 1);
1198 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1199 cond
->cat2
.condition
= IR3_COND_NE
;
1201 /* condition always goes in predicate register: */
1202 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1204 kill
= ir3_KILL(b
, cond
, 0);
1206 ctx
->kill
[ctx
->kill_count
++] = kill
;
1207 ctx
->so
->has_kill
= true;
1212 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1213 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1219 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1221 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1222 instr
->def
.num_components
);
1223 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1224 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u
[i
]);
1228 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1230 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1231 undef
->def
.num_components
);
1232 /* backend doesn't want undefined instructions, so just plug
1235 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1236 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1240 * texture fetch/sample instructions:
1244 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1246 unsigned coords
, flags
= 0;
1248 /* note: would use tex->coord_components.. except txs.. also,
1249 * since array index goes after shadow ref, we don't want to
1252 switch (tex
->sampler_dim
) {
1253 case GLSL_SAMPLER_DIM_1D
:
1254 case GLSL_SAMPLER_DIM_BUF
:
1257 case GLSL_SAMPLER_DIM_2D
:
1258 case GLSL_SAMPLER_DIM_RECT
:
1259 case GLSL_SAMPLER_DIM_EXTERNAL
:
1260 case GLSL_SAMPLER_DIM_MS
:
1263 case GLSL_SAMPLER_DIM_3D
:
1264 case GLSL_SAMPLER_DIM_CUBE
:
1266 flags
|= IR3_INSTR_3D
;
1271 flags
|= IR3_INSTR_S
;
1274 flags
|= IR3_INSTR_A
;
1281 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1283 struct ir3_block
*b
= ctx
->block
;
1284 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1285 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1286 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1287 unsigned i
, coords
, flags
;
1288 unsigned nsrc0
= 0, nsrc1
= 0;
1292 /* TODO: might just be one component for gathers? */
1293 dst
= get_dst(ctx
, &tex
->dest
, 4);
1295 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1296 switch (tex
->src
[i
].src_type
) {
1297 case nir_tex_src_coord
:
1298 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1300 case nir_tex_src_bias
:
1301 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1304 case nir_tex_src_lod
:
1305 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1308 case nir_tex_src_comparitor
: /* shadow comparator */
1309 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1311 case nir_tex_src_projector
:
1312 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1315 case nir_tex_src_offset
:
1316 off
= get_src(ctx
, &tex
->src
[i
].src
);
1319 case nir_tex_src_ddx
:
1320 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1322 case nir_tex_src_ddy
:
1323 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1326 compile_error(ctx
, "Unhandled NIR tex serc type: %d\n",
1327 tex
->src
[i
].src_type
);
1333 * lay out the first argument in the proper order:
1334 * - actual coordinates first
1335 * - shadow reference
1338 * - starting at offset 4, dpdx.xy, dpdy.xy
1340 * bias/lod go into the second arg
1343 tex_info(tex
, &flags
, &coords
);
1345 /* insert tex coords: */
1346 for (i
= 0; i
< coords
; i
++)
1347 src0
[nsrc0
++] = coord
[i
];
1350 /* hw doesn't do 1d, so we treat it as 2d with
1351 * height of 1, and patch up the y coord.
1352 * TODO: y coord should be (int)0 in some cases..
1354 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1358 src0
[nsrc0
++] = compare
;
1361 src0
[nsrc0
++] = coord
[coords
];
1364 src0
[nsrc0
++] = proj
;
1365 flags
|= IR3_INSTR_P
;
1368 /* pad to 4, then ddx/ddy: */
1369 if (tex
->op
== nir_texop_txd
) {
1371 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1372 for (i
= 0; i
< coords
; i
++)
1373 src0
[nsrc0
++] = ddx
[i
];
1375 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1376 for (i
= 0; i
< coords
; i
++)
1377 src0
[nsrc0
++] = ddy
[i
];
1379 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1383 * second argument (if applicable):
1388 if (has_off
| has_lod
| has_bias
) {
1390 for (i
= 0; i
< coords
; i
++)
1391 src1
[nsrc1
++] = off
[i
];
1393 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1394 flags
|= IR3_INSTR_O
;
1397 if (has_lod
| has_bias
)
1398 src1
[nsrc1
++] = lod
;
1402 case nir_texop_tex
: opc
= OPC_SAM
; break;
1403 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1404 case nir_texop_txl
: opc
= OPC_SAML
; break;
1405 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1406 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1407 case nir_texop_txf_ms
:
1411 case nir_texop_query_levels
:
1412 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1416 switch (tex
->dest_type
) {
1417 case nir_type_invalid
:
1418 case nir_type_float
:
1424 case nir_type_unsigned
:
1430 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
,
1431 flags
, tex
->sampler_index
, tex
->sampler_index
,
1432 create_collect(b
, src0
, nsrc0
),
1433 create_collect(b
, src1
, nsrc1
));
1435 split_dest(b
, dst
, sam
);
1439 emit_tex_query_levels(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1441 struct ir3_block
*b
= ctx
->block
;
1442 struct ir3_instruction
**dst
, *sam
;
1444 dst
= get_dst(ctx
, &tex
->dest
, 1);
1446 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, TGSI_WRITEMASK_Z
, 0,
1447 tex
->sampler_index
, tex
->sampler_index
, NULL
, NULL
);
1449 /* even though there is only one component, since it ends
1450 * up in .z rather than .x, we need a split_dest()
1452 split_dest(b
, dst
, sam
);
1454 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1455 * the value in TEX_CONST_0 is zero-based.
1457 if (ctx
->levels_add_one
)
1458 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
1462 emit_tex_txs(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1464 struct ir3_block
*b
= ctx
->block
;
1465 struct ir3_instruction
**dst
, *sam
, *lod
;
1466 unsigned flags
, coords
;
1468 tex_info(tex
, &flags
, &coords
);
1470 dst
= get_dst(ctx
, &tex
->dest
, 4);
1472 compile_assert(ctx
, tex
->num_srcs
== 1);
1473 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
1475 lod
= get_src(ctx
, &tex
->src
[0].src
)[0];
1477 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
1478 tex
->sampler_index
, tex
->sampler_index
, lod
, NULL
);
1480 split_dest(b
, dst
, sam
);
1482 /* Array size actually ends up in .w rather than .z. This doesn't
1483 * matter for miplevel 0, but for higher mips the value in z is
1484 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1485 * returned, which means that we have to add 1 to it for arrays.
1487 if (tex
->is_array
) {
1488 if (ctx
->levels_add_one
) {
1489 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
1491 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
1497 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1499 switch (instr
->type
) {
1500 case nir_instr_type_alu
:
1501 emit_alu(ctx
, nir_instr_as_alu(instr
));
1503 case nir_instr_type_intrinsic
:
1504 emit_intrinisic(ctx
, nir_instr_as_intrinsic(instr
));
1506 case nir_instr_type_load_const
:
1507 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1509 case nir_instr_type_ssa_undef
:
1510 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1512 case nir_instr_type_tex
: {
1513 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
1514 /* couple tex instructions get special-cased:
1518 emit_tex_txs(ctx
, tex
);
1520 case nir_texop_query_levels
:
1521 emit_tex_query_levels(ctx
, tex
);
1529 case nir_instr_type_call
:
1530 case nir_instr_type_jump
:
1531 case nir_instr_type_phi
:
1532 case nir_instr_type_parallel_copy
:
1533 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1539 emit_block(struct ir3_compile
*ctx
, nir_block
*block
)
1541 nir_foreach_instr(block
, instr
) {
1542 emit_instr(ctx
, instr
);
1549 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1551 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1552 switch (node
->type
) {
1553 case nir_cf_node_block
:
1554 emit_block(ctx
, nir_cf_node_as_block(node
));
1556 case nir_cf_node_if
:
1557 case nir_cf_node_loop
:
1558 case nir_cf_node_function
:
1559 compile_error(ctx
, "TODO\n");
1568 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1570 struct ir3_shader_variant
*so
= ctx
->so
;
1571 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1572 unsigned ncomp
= glsl_get_components(in
->type
);
1573 /* XXX: map loc slots to semantics */
1574 unsigned semantic_name
= in
->data
.location
;
1575 unsigned semantic_index
= in
->data
.index
;
1576 unsigned n
= in
->data
.driver_location
;
1578 DBG("; in: %u:%u, len=%ux%u, loc=%u\n",
1579 semantic_name
, semantic_index
, array_len
,
1582 so
->inputs
[n
].semantic
=
1583 ir3_semantic_name(semantic_name
, semantic_index
);
1584 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1585 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1586 so
->inputs
[n
].interpolate
= 0;
1587 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1589 /* the fdN_program_emit() code expects tgsi consts here, so map
1590 * things back to tgsi for now:
1592 switch (in
->data
.interpolation
) {
1593 case INTERP_QUALIFIER_FLAT
:
1594 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_CONSTANT
;
1596 case INTERP_QUALIFIER_NOPERSPECTIVE
:
1597 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_LINEAR
;
1599 case INTERP_QUALIFIER_SMOOTH
:
1600 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
1604 for (int i
= 0; i
< ncomp
; i
++) {
1605 struct ir3_instruction
*instr
= NULL
;
1606 unsigned idx
= (n
* 4) + i
;
1608 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1609 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1610 so
->inputs
[n
].bary
= false;
1611 so
->frag_coord
= true;
1612 instr
= create_frag_coord(ctx
, i
);
1613 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1614 so
->inputs
[n
].bary
= false;
1615 so
->frag_face
= true;
1616 instr
= create_frag_face(ctx
, i
);
1618 bool use_ldlv
= false;
1620 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1621 * from the semantic name:
1623 if ((in
->data
.interpolation
== INTERP_QUALIFIER_NONE
) &&
1624 ((semantic_name
== TGSI_SEMANTIC_COLOR
) ||
1625 (semantic_name
== TGSI_SEMANTIC_BCOLOR
)))
1626 so
->inputs
[n
].interpolate
= TGSI_INTERPOLATE_COLOR
;
1628 if (ctx
->flat_bypass
) {
1629 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
1630 * from the semantic name:
1632 switch (so
->inputs
[n
].interpolate
) {
1633 case TGSI_INTERPOLATE_COLOR
:
1634 if (!ctx
->so
->key
.rasterflat
)
1637 case TGSI_INTERPOLATE_CONSTANT
:
1643 so
->inputs
[n
].bary
= true;
1645 instr
= create_frag_input(ctx
, idx
, use_ldlv
);
1648 instr
= create_input(ctx
->block
, NULL
, idx
);
1651 ctx
->block
->inputs
[idx
] = instr
;
1654 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
1655 ctx
->next_inloc
+= ncomp
;
1656 so
->total_in
+= ncomp
;
1661 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
1663 struct ir3_shader_variant
*so
= ctx
->so
;
1664 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
1665 unsigned ncomp
= glsl_get_components(out
->type
);
1666 /* XXX: map loc slots to semantics */
1667 unsigned semantic_name
= out
->data
.location
;
1668 unsigned semantic_index
= out
->data
.index
;
1669 unsigned n
= out
->data
.driver_location
;
1672 DBG("; out: %u:%u, len=%ux%u, loc=%u\n",
1673 semantic_name
, semantic_index
, array_len
,
1676 if (ctx
->so
->type
== SHADER_VERTEX
) {
1677 switch (semantic_name
) {
1678 case TGSI_SEMANTIC_POSITION
:
1679 so
->writes_pos
= true;
1681 case TGSI_SEMANTIC_PSIZE
:
1682 so
->writes_psize
= true;
1684 case TGSI_SEMANTIC_COLOR
:
1685 case TGSI_SEMANTIC_BCOLOR
:
1686 case TGSI_SEMANTIC_GENERIC
:
1687 case TGSI_SEMANTIC_FOG
:
1688 case TGSI_SEMANTIC_TEXCOORD
:
1691 compile_error(ctx
, "unknown VS semantic name: %s\n",
1692 tgsi_semantic_names
[semantic_name
]);
1695 switch (semantic_name
) {
1696 case TGSI_SEMANTIC_POSITION
:
1697 comp
= 2; /* tgsi will write to .z component */
1698 so
->writes_pos
= true;
1700 case TGSI_SEMANTIC_COLOR
:
1703 compile_error(ctx
, "unknown FS semantic name: %s\n",
1704 tgsi_semantic_names
[semantic_name
]);
1708 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
1710 so
->outputs
[n
].semantic
=
1711 ir3_semantic_name(semantic_name
, semantic_index
);
1712 so
->outputs
[n
].regid
= regid(n
, comp
);
1713 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
1715 for (int i
= 0; i
< ncomp
; i
++) {
1716 unsigned idx
= (n
* 4) + i
;
1718 ctx
->block
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
1723 emit_instructions(struct ir3_compile
*ctx
)
1725 unsigned ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
1726 unsigned noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
1728 /* we need to allocate big enough outputs array so that
1729 * we can stuff the kill's at the end:
1731 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1732 noutputs
+= ARRAY_SIZE(ctx
->kill
);
1734 ctx
->block
= ir3_block_create(ctx
->ir
, 0, ninputs
, noutputs
);
1736 if (ctx
->so
->type
== SHADER_FRAGMENT
)
1737 ctx
->block
->noutputs
-= ARRAY_SIZE(ctx
->kill
);
1740 /* for fragment shader, we have a single input register (usually
1741 * r0.xy) which is used as the base for bary.f varying fetch instrs:
1743 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1744 // TODO maybe a helper for fi since we need it a few places..
1745 struct ir3_instruction
*instr
;
1746 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
1747 ir3_reg_create(instr
, 0, 0);
1748 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
1749 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
1750 ctx
->frag_pos
= instr
;
1754 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->inputs
) {
1755 setup_input(ctx
, var
);
1758 /* Setup outputs: */
1759 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->outputs
) {
1760 setup_output(ctx
, var
);
1763 /* Setup variables (which should only be arrays): */
1764 foreach_list_typed(nir_variable
, var
, node
, &ctx
->s
->globals
) {
1765 declare_var(ctx
, var
);
1768 /* Find the main function and emit the body: */
1769 nir_foreach_overload(ctx
->s
, overload
) {
1770 compile_assert(ctx
, strcmp(overload
->function
->name
, "main") == 0);
1771 compile_assert(ctx
, overload
->impl
);
1772 emit_function(ctx
, overload
->impl
);
1778 /* from NIR perspective, we actually have inputs. But most of the "inputs"
1779 * for a fragment shader are just bary.f instructions. The *actual* inputs
1780 * from the hw perspective are the frag_pos and optionally frag_coord and
1784 fixup_frag_inputs(struct ir3_compile
*ctx
)
1786 struct ir3_shader_variant
*so
= ctx
->so
;
1787 struct ir3_block
*block
= ctx
->block
;
1788 struct ir3_instruction
**inputs
;
1789 struct ir3_instruction
*instr
;
1794 n
= 4; /* always have frag_pos */
1795 n
+= COND(so
->frag_face
, 4);
1796 n
+= COND(so
->frag_coord
, 4);
1798 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
1800 if (so
->frag_face
) {
1801 /* this ultimately gets assigned to hr0.x so doesn't conflict
1802 * with frag_coord/frag_pos..
1804 inputs
[block
->ninputs
++] = ctx
->frag_face
;
1805 ctx
->frag_face
->regs
[0]->num
= 0;
1807 /* remaining channels not used, but let's avoid confusing
1808 * other parts that expect inputs to come in groups of vec4
1810 inputs
[block
->ninputs
++] = NULL
;
1811 inputs
[block
->ninputs
++] = NULL
;
1812 inputs
[block
->ninputs
++] = NULL
;
1815 /* since we don't know where to set the regid for frag_coord,
1816 * we have to use r0.x for it. But we don't want to *always*
1817 * use r1.x for frag_pos as that could increase the register
1818 * footprint on simple shaders:
1820 if (so
->frag_coord
) {
1821 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
1822 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
1823 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
1824 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
1826 inputs
[block
->ninputs
++] = ctx
->frag_coord
[0];
1827 inputs
[block
->ninputs
++] = ctx
->frag_coord
[1];
1828 inputs
[block
->ninputs
++] = ctx
->frag_coord
[2];
1829 inputs
[block
->ninputs
++] = ctx
->frag_coord
[3];
1832 /* we always have frag_pos: */
1833 so
->pos_regid
= regid
;
1836 instr
= create_input(block
, NULL
, block
->ninputs
);
1837 instr
->regs
[0]->num
= regid
++;
1838 inputs
[block
->ninputs
++] = instr
;
1839 ctx
->frag_pos
->regs
[1]->instr
= instr
;
1842 instr
= create_input(block
, NULL
, block
->ninputs
);
1843 instr
->regs
[0]->num
= regid
++;
1844 inputs
[block
->ninputs
++] = instr
;
1845 ctx
->frag_pos
->regs
[2]->instr
= instr
;
1847 block
->inputs
= inputs
;
1851 compile_dump(struct ir3_compile
*ctx
)
1853 const char *name
= (ctx
->so
->type
== SHADER_VERTEX
) ? "vert" : "frag";
1854 static unsigned n
= 0;
1857 snprintf(fname
, sizeof(fname
), "%s-%04u.dot", name
, n
++);
1858 f
= fopen(fname
, "w");
1861 ir3_block_depth(ctx
->block
);
1862 ir3_dump(ctx
->ir
, name
, ctx
->block
, f
);
1867 ir3_compile_shader_nir(struct ir3_shader_variant
*so
,
1868 const struct tgsi_token
*tokens
, struct ir3_shader_key key
)
1870 struct ir3_compile
*ctx
;
1871 struct ir3_block
*block
;
1872 struct ir3_instruction
**inputs
;
1873 unsigned i
, j
, actual_in
;
1874 int ret
= 0, max_bary
;
1878 so
->ir
= ir3_create();
1882 ctx
= compile_init(so
, tokens
);
1884 DBG("INIT failed!");
1889 emit_instructions(ctx
);
1892 DBG("EMIT failed!");
1898 so
->ir
->block
= block
;
1900 /* keep track of the inputs from TGSI perspective.. */
1901 inputs
= block
->inputs
;
1903 /* but fixup actual inputs for frag shader: */
1904 if (so
->type
== SHADER_FRAGMENT
)
1905 fixup_frag_inputs(ctx
);
1907 /* at this point, for binning pass, throw away unneeded outputs: */
1908 if (key
.binning_pass
) {
1909 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
1910 unsigned name
= sem2name(so
->outputs
[i
].semantic
);
1911 unsigned idx
= sem2idx(so
->outputs
[i
].semantic
);
1913 /* throw away everything but first position/psize */
1914 if ((idx
== 0) && ((name
== TGSI_SEMANTIC_POSITION
) ||
1915 (name
== TGSI_SEMANTIC_PSIZE
))) {
1917 so
->outputs
[j
] = so
->outputs
[i
];
1918 block
->outputs
[(j
*4)+0] = block
->outputs
[(i
*4)+0];
1919 block
->outputs
[(j
*4)+1] = block
->outputs
[(i
*4)+1];
1920 block
->outputs
[(j
*4)+2] = block
->outputs
[(i
*4)+2];
1921 block
->outputs
[(j
*4)+3] = block
->outputs
[(i
*4)+3];
1926 so
->outputs_count
= j
;
1927 block
->noutputs
= j
* 4;
1930 /* if we want half-precision outputs, mark the output registers
1933 if (key
.half_precision
) {
1934 for (i
= 0; i
< block
->noutputs
; i
++) {
1935 if (!block
->outputs
[i
])
1937 block
->outputs
[i
]->regs
[0]->flags
|= IR3_REG_HALF
;
1941 /* at this point, we want the kill's in the outputs array too,
1942 * so that they get scheduled (since they have no dst).. we've
1943 * already ensured that the array is big enough in push_block():
1945 if (so
->type
== SHADER_FRAGMENT
) {
1946 for (i
= 0; i
< ctx
->kill_count
; i
++)
1947 block
->outputs
[block
->noutputs
++] = ctx
->kill
[i
];
1950 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1953 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1954 printf("BEFORE CP:\n");
1955 ir3_dump_instr_list(block
->head
);
1958 ir3_block_depth(block
);
1960 ir3_block_cp(block
);
1962 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1963 printf("BEFORE GROUPING:\n");
1964 ir3_dump_instr_list(block
->head
);
1967 /* Group left/right neighbors, inserting mov's where needed to
1970 ir3_block_group(block
);
1972 if (fd_mesa_debug
& FD_DBG_OPTDUMP
)
1975 ir3_block_depth(block
);
1977 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1978 printf("AFTER DEPTH:\n");
1979 ir3_dump_instr_list(block
->head
);
1982 ret
= ir3_block_sched(block
);
1984 DBG("SCHED failed!");
1988 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
1989 printf("AFTER SCHED:\n");
1990 ir3_dump_instr_list(block
->head
);
1993 ret
= ir3_block_ra(block
, so
->type
, so
->frag_coord
, so
->frag_face
);
1999 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2000 printf("AFTER RA:\n");
2001 ir3_dump_instr_list(block
->head
);
2004 ir3_block_legalize(block
, &so
->has_samp
, &max_bary
);
2006 /* fixup input/outputs: */
2007 for (i
= 0; i
< so
->outputs_count
; i
++) {
2008 so
->outputs
[i
].regid
= block
->outputs
[i
*4]->regs
[0]->num
;
2009 /* preserve hack for depth output.. tgsi writes depth to .z,
2010 * but what we give the hw is the scalar register:
2012 if ((so
->type
== SHADER_FRAGMENT
) &&
2013 (sem2name(so
->outputs
[i
].semantic
) == TGSI_SEMANTIC_POSITION
))
2014 so
->outputs
[i
].regid
+= 2;
2017 /* Note that some or all channels of an input may be unused: */
2019 for (i
= 0; i
< so
->inputs_count
; i
++) {
2020 unsigned j
, regid
= ~0, compmask
= 0;
2021 so
->inputs
[i
].ncomp
= 0;
2022 for (j
= 0; j
< 4; j
++) {
2023 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
2025 compmask
|= (1 << j
);
2026 regid
= in
->regs
[0]->num
- j
;
2028 so
->inputs
[i
].ncomp
++;
2031 so
->inputs
[i
].regid
= regid
;
2032 so
->inputs
[i
].compmask
= compmask
;
2035 /* fragment shader always gets full vec4's even if it doesn't
2036 * fetch all components, but vertex shader we need to update
2037 * with the actual number of components fetch, otherwise thing
2038 * will hang due to mismaptch between VFD_DECODE's and
2041 if (so
->type
== SHADER_VERTEX
)
2042 so
->total_in
= actual_in
;
2044 so
->total_in
= align(max_bary
+ 1, 4);
2048 ir3_destroy(so
->ir
);