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"
36 #include "freedreno_util.h"
38 #include "ir3_compiler.h"
39 #include "ir3_shader.h"
42 #include "instr-a3xx.h"
47 struct ir3_compiler
*compiler
;
52 struct ir3_shader_variant
*so
;
54 struct ir3_block
*block
; /* the current block */
55 struct ir3_block
*in_block
; /* block created for shader inputs */
57 nir_function_impl
*impl
;
59 /* For fragment shaders, from the hw perspective the only
60 * actual input is r0.xy position register passed to bary.f.
61 * But TGSI doesn't know that, it still declares things as
62 * IN[] registers. So we do all the input tracking normally
63 * and fix things up after compile_instructions()
65 * NOTE that frag_pos is the hardware position (possibly it
66 * is actually an index or tag or some such.. it is *not*
67 * values that can be directly used for gl_FragCoord..)
69 struct ir3_instruction
*frag_pos
, *frag_face
, *frag_coord
[4];
71 /* For vertex shaders, keep track of the system values sources */
72 struct ir3_instruction
*vertex_id
, *basevertex
, *instance_id
;
74 /* mapping from nir_register to defining instruction: */
75 struct hash_table
*def_ht
;
79 /* a common pattern for indirect addressing is to request the
80 * same address register multiple times. To avoid generating
81 * duplicate instruction sequences (which our backend does not
82 * try to clean up, since that should be done as the NIR stage)
83 * we cache the address value generated for a given src value:
85 struct hash_table
*addr_ht
;
87 /* maps nir_block to ir3_block, mostly for the purposes of
88 * figuring out the blocks successors
90 struct hash_table
*block_ht
;
92 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
93 * so we need to use ldlv.u32 to load the varying directly:
97 /* on a3xx, we need to add one to # of array levels:
101 /* on a3xx, we need to scale up integer coords for isaml based
104 bool unminify_coords
;
106 /* for looking up which system value is which */
107 unsigned sysval_semantics
[8];
109 /* set if we encounter something we can't handle yet, so we
110 * can bail cleanly and fallback to TGSI compiler f/e
116 static struct ir3_instruction
* create_immed(struct ir3_block
*block
, uint32_t val
);
117 static struct ir3_block
* get_block(struct ir3_compile
*ctx
, nir_block
*nblock
);
120 static struct ir3_compile
*
121 compile_init(struct ir3_compiler
*compiler
,
122 struct ir3_shader_variant
*so
)
124 struct ir3_compile
*ctx
= rzalloc(NULL
, struct ir3_compile
);
126 if (compiler
->gpu_id
>= 400) {
127 /* need special handling for "flat" */
128 ctx
->flat_bypass
= true;
129 ctx
->levels_add_one
= false;
130 ctx
->unminify_coords
= false;
132 /* no special handling for "flat" */
133 ctx
->flat_bypass
= false;
134 ctx
->levels_add_one
= true;
135 ctx
->unminify_coords
= true;
138 ctx
->compiler
= compiler
;
141 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
142 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
143 ctx
->block_ht
= _mesa_hash_table_create(ctx
,
144 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
146 /* TODO: maybe generate some sort of bitmask of what key
147 * lowers vs what shader has (ie. no need to lower
148 * texture clamp lowering if no texture sample instrs)..
149 * although should be done further up the stack to avoid
150 * creating duplicate variants..
153 if (ir3_key_lowers_nir(&so
->key
)) {
154 nir_shader
*s
= nir_shader_clone(ctx
, so
->shader
->nir
);
155 ctx
->s
= ir3_optimize_nir(so
->shader
, s
, &so
->key
);
157 /* fast-path for shader key that lowers nothing in NIR: */
158 ctx
->s
= so
->shader
->nir
;
161 if (fd_mesa_debug
& FD_DBG_DISASM
) {
162 DBG("dump nir%dv%d: type=%d, k={bp=%u,cts=%u,hp=%u}",
163 so
->shader
->id
, so
->id
, so
->type
,
164 so
->key
.binning_pass
, so
->key
.color_two_side
,
165 so
->key
.half_precision
);
166 nir_print_shader(ctx
->s
, stdout
);
169 so
->first_driver_param
= so
->first_immediate
= ctx
->s
->num_uniforms
;
171 /* Layout of constant registers:
173 * num_uniform * vec4 - user consts
174 * 4 * vec4 - UBO addresses
175 * if (vertex shader) {
176 * N * vec4 - driver params (IR3_DP_*)
177 * 1 * vec4 - stream-out addresses
180 * TODO this could be made more dynamic, to at least skip sections
181 * that we don't need..
184 /* reserve 4 (vec4) slots for ubo base addresses: */
185 so
->first_immediate
+= 4;
187 if (so
->type
== SHADER_VERTEX
) {
188 /* driver params (see ir3_driver_param): */
189 so
->first_immediate
+= IR3_DP_COUNT
/4; /* convert to vec4 */
190 /* one (vec4) slot for stream-output base addresses: */
191 so
->first_immediate
++;
198 compile_error(struct ir3_compile
*ctx
, const char *format
, ...)
201 va_start(ap
, format
);
202 _debug_vprintf(format
, ap
);
204 nir_print_shader(ctx
->s
, stdout
);
209 #define compile_assert(ctx, cond) do { \
210 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
214 compile_free(struct ir3_compile
*ctx
)
220 declare_var(struct ir3_compile
*ctx
, nir_variable
*var
)
222 unsigned length
= glsl_get_length(var
->type
) * 4; /* always vec4, at least with ttn */
223 struct ir3_array
*arr
= ralloc(ctx
, struct ir3_array
);
224 arr
->id
= ++ctx
->num_arrays
;
225 arr
->length
= length
;
227 list_addtail(&arr
->node
, &ctx
->ir
->array_list
);
230 static struct ir3_array
*
231 get_var(struct ir3_compile
*ctx
, nir_variable
*var
)
233 list_for_each_entry (struct ir3_array
, arr
, &ctx
->ir
->array_list
, node
) {
237 compile_error(ctx
, "bogus var: %s\n", var
->name
);
241 /* allocate a n element value array (to be populated by caller) and
244 static struct ir3_instruction
**
245 __get_dst(struct ir3_compile
*ctx
, void *key
, unsigned n
)
247 struct ir3_instruction
**value
=
248 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
249 _mesa_hash_table_insert(ctx
->def_ht
, key
, value
);
253 static struct ir3_instruction
**
254 get_dst(struct ir3_compile
*ctx
, nir_dest
*dst
, unsigned n
)
256 compile_assert(ctx
, dst
->is_ssa
);
258 return __get_dst(ctx
, &dst
->ssa
, n
);
260 return __get_dst(ctx
, dst
->reg
.reg
, n
);
264 static struct ir3_instruction
**
265 get_dst_ssa(struct ir3_compile
*ctx
, nir_ssa_def
*dst
, unsigned n
)
267 return __get_dst(ctx
, dst
, n
);
270 static struct ir3_instruction
**
271 get_src(struct ir3_compile
*ctx
, nir_src
*src
)
273 struct hash_entry
*entry
;
274 compile_assert(ctx
, src
->is_ssa
);
276 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
278 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->reg
.reg
);
280 compile_assert(ctx
, entry
);
284 static struct ir3_instruction
*
285 create_immed(struct ir3_block
*block
, uint32_t val
)
287 struct ir3_instruction
*mov
;
289 mov
= ir3_instr_create(block
, 1, 0);
290 mov
->cat1
.src_type
= TYPE_U32
;
291 mov
->cat1
.dst_type
= TYPE_U32
;
292 ir3_reg_create(mov
, 0, 0);
293 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
298 static struct ir3_instruction
*
299 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
)
301 struct ir3_instruction
*instr
, *immed
;
303 /* TODO in at least some cases, the backend could probably be
304 * made clever enough to propagate IR3_REG_HALF..
306 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
307 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
309 immed
= create_immed(block
, 2);
310 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
312 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
313 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
314 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
316 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
317 instr
->regs
[0]->num
= regid(REG_A0
, 0);
318 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
319 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
324 /* caches addr values to avoid generating multiple cov/shl/mova
325 * sequences for each use of a given NIR level src as address
327 static struct ir3_instruction
*
328 get_addr(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
330 struct ir3_instruction
*addr
;
333 ctx
->addr_ht
= _mesa_hash_table_create(ctx
,
334 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
336 struct hash_entry
*entry
;
337 entry
= _mesa_hash_table_search(ctx
->addr_ht
, src
);
342 addr
= create_addr(ctx
->block
, src
);
343 _mesa_hash_table_insert(ctx
->addr_ht
, src
, addr
);
348 static struct ir3_instruction
*
349 get_predicate(struct ir3_compile
*ctx
, struct ir3_instruction
*src
)
351 struct ir3_block
*b
= ctx
->block
;
352 struct ir3_instruction
*cond
;
354 /* NOTE: only cmps.*.* can write p0.x: */
355 cond
= ir3_CMPS_S(b
, src
, 0, create_immed(b
, 0), 0);
356 cond
->cat2
.condition
= IR3_COND_NE
;
358 /* condition always goes in predicate register: */
359 cond
->regs
[0]->num
= regid(REG_P0
, 0);
364 static struct ir3_instruction
*
365 create_uniform(struct ir3_compile
*ctx
, unsigned n
)
367 struct ir3_instruction
*mov
;
369 mov
= ir3_instr_create(ctx
->block
, 1, 0);
370 /* TODO get types right? */
371 mov
->cat1
.src_type
= TYPE_F32
;
372 mov
->cat1
.dst_type
= TYPE_F32
;
373 ir3_reg_create(mov
, 0, 0);
374 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
379 static struct ir3_instruction
*
380 create_uniform_indirect(struct ir3_compile
*ctx
, int n
,
381 struct ir3_instruction
*address
)
383 struct ir3_instruction
*mov
;
385 mov
= ir3_instr_create(ctx
->block
, 1, 0);
386 mov
->cat1
.src_type
= TYPE_U32
;
387 mov
->cat1
.dst_type
= TYPE_U32
;
388 ir3_reg_create(mov
, 0, 0);
389 ir3_reg_create(mov
, 0, IR3_REG_CONST
| IR3_REG_RELATIV
)->array
.offset
= n
;
391 ir3_instr_set_address(mov
, address
);
396 static struct ir3_instruction
*
397 create_collect(struct ir3_block
*block
, struct ir3_instruction
**arr
,
400 struct ir3_instruction
*collect
;
405 collect
= ir3_instr_create2(block
, -1, OPC_META_FI
, 1 + arrsz
);
406 ir3_reg_create(collect
, 0, 0); /* dst */
407 for (unsigned i
= 0; i
< arrsz
; i
++)
408 ir3_reg_create(collect
, 0, IR3_REG_SSA
)->instr
= arr
[i
];
413 static struct ir3_instruction
*
414 create_indirect_load(struct ir3_compile
*ctx
, unsigned arrsz
, int n
,
415 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
417 struct ir3_block
*block
= ctx
->block
;
418 struct ir3_instruction
*mov
;
419 struct ir3_register
*src
;
421 mov
= ir3_instr_create(block
, 1, 0);
422 mov
->cat1
.src_type
= TYPE_U32
;
423 mov
->cat1
.dst_type
= TYPE_U32
;
424 ir3_reg_create(mov
, 0, 0);
425 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
426 src
->instr
= collect
;
428 src
->array
.offset
= n
;
430 ir3_instr_set_address(mov
, address
);
435 /* relative (indirect) if address!=NULL */
436 static struct ir3_instruction
*
437 create_var_load(struct ir3_compile
*ctx
, struct ir3_array
*arr
, int n
,
438 struct ir3_instruction
*address
)
440 struct ir3_block
*block
= ctx
->block
;
441 struct ir3_instruction
*mov
;
442 struct ir3_register
*src
;
444 mov
= ir3_instr_create(block
, 1, 0);
445 mov
->cat1
.src_type
= TYPE_U32
;
446 mov
->cat1
.dst_type
= TYPE_U32
;
447 ir3_reg_create(mov
, 0, 0);
448 src
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
449 COND(address
, IR3_REG_RELATIV
));
450 src
->instr
= arr
->last_write
;
451 src
->size
= arr
->length
;
452 src
->array
.id
= arr
->id
;
453 src
->array
.offset
= n
;
456 ir3_instr_set_address(mov
, address
);
458 arr
->last_access
= mov
;
463 /* relative (indirect) if address!=NULL */
464 static struct ir3_instruction
*
465 create_var_store(struct ir3_compile
*ctx
, struct ir3_array
*arr
, int n
,
466 struct ir3_instruction
*src
, struct ir3_instruction
*address
)
468 struct ir3_block
*block
= ctx
->block
;
469 struct ir3_instruction
*mov
;
470 struct ir3_register
*dst
;
472 mov
= ir3_instr_create(block
, 1, 0);
473 mov
->cat1
.src_type
= TYPE_U32
;
474 mov
->cat1
.dst_type
= TYPE_U32
;
475 dst
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
476 COND(address
, IR3_REG_RELATIV
));
477 dst
->instr
= arr
->last_access
;
478 dst
->size
= arr
->length
;
479 dst
->array
.id
= arr
->id
;
480 dst
->array
.offset
= n
;
481 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
483 ir3_instr_set_address(mov
, address
);
485 arr
->last_write
= arr
->last_access
= mov
;
490 static struct ir3_instruction
*
491 create_input(struct ir3_block
*block
, unsigned n
)
493 struct ir3_instruction
*in
;
495 in
= ir3_instr_create(block
, -1, OPC_META_INPUT
);
496 in
->inout
.block
= block
;
497 ir3_reg_create(in
, n
, 0);
502 static struct ir3_instruction
*
503 create_frag_input(struct ir3_compile
*ctx
, bool use_ldlv
)
505 struct ir3_block
*block
= ctx
->block
;
506 struct ir3_instruction
*instr
;
507 /* actual inloc is assigned and fixed up later: */
508 struct ir3_instruction
*inloc
= create_immed(block
, 0);
511 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
512 instr
->cat6
.type
= TYPE_U32
;
513 instr
->cat6
.iim_val
= 1;
515 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_pos
, 0);
516 instr
->regs
[2]->wrmask
= 0x3;
522 static struct ir3_instruction
*
523 create_frag_coord(struct ir3_compile
*ctx
, unsigned comp
)
525 struct ir3_block
*block
= ctx
->block
;
526 struct ir3_instruction
*instr
;
528 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
530 ctx
->frag_coord
[comp
] = create_input(ctx
->block
, 0);
535 /* for frag_coord, we get unsigned values.. we need
536 * to subtract (integer) 8 and divide by 16 (right-
537 * shift by 4) then convert to float:
541 * mov.u32f32 dst, tmp
544 instr
= ir3_SUB_S(block
, ctx
->frag_coord
[comp
], 0,
545 create_immed(block
, 8), 0);
546 instr
= ir3_SHR_B(block
, instr
, 0,
547 create_immed(block
, 4), 0);
548 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
554 /* seems that we can use these as-is: */
555 return ctx
->frag_coord
[comp
];
559 /* NOTE: this creates the "TGSI" style fragface (ie. input slot
560 * VARYING_SLOT_FACE). For NIR style nir_intrinsic_load_front_face
561 * we can just use the value from hw directly (since it is boolean)
563 static struct ir3_instruction
*
564 create_frag_face(struct ir3_compile
*ctx
, unsigned comp
)
566 struct ir3_block
*block
= ctx
->block
;
567 struct ir3_instruction
*instr
;
571 compile_assert(ctx
, !ctx
->frag_face
);
573 ctx
->frag_face
= create_input(block
, 0);
574 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
576 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
577 * positive vs negative float.. and piglit further seems to
578 * expect -1.0 or 1.0:
580 * mul.s tmp, hr0.x, 2
582 * mov.s32f32, dst, tmp
585 instr
= ir3_MUL_S(block
, ctx
->frag_face
, 0,
586 create_immed(block
, 2), 0);
587 instr
= ir3_ADD_S(block
, instr
, 0,
588 create_immed(block
, 1), 0);
589 instr
= ir3_COV(block
, instr
, TYPE_S32
, TYPE_F32
);
594 return create_immed(block
, fui(0.0));
597 return create_immed(block
, fui(1.0));
601 static struct ir3_instruction
*
602 create_driver_param(struct ir3_compile
*ctx
, enum ir3_driver_param dp
)
604 /* first four vec4 sysval's reserved for UBOs: */
605 /* NOTE: dp is in scalar, but there can be >4 dp components: */
606 unsigned n
= ctx
->so
->first_driver_param
+ IR3_DRIVER_PARAM_OFF
;
607 unsigned r
= regid(n
+ dp
/ 4, dp
% 4);
608 return create_uniform(ctx
, r
);
611 /* helper for instructions that produce multiple consecutive scalar
612 * outputs which need to have a split/fanout meta instruction inserted
615 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
616 struct ir3_instruction
*src
, unsigned n
)
618 struct ir3_instruction
*prev
= NULL
;
619 for (int i
= 0, j
= 0; i
< n
; i
++) {
620 struct ir3_instruction
*split
=
621 ir3_instr_create(block
, -1, OPC_META_FO
);
622 ir3_reg_create(split
, 0, IR3_REG_SSA
);
623 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
627 split
->cp
.left
= prev
;
628 split
->cp
.left_cnt
++;
629 prev
->cp
.right
= split
;
630 prev
->cp
.right_cnt
++;
634 if (src
->regs
[0]->wrmask
& (1 << i
))
640 * Adreno uses uint rather than having dedicated bool type,
641 * which (potentially) requires some conversion, in particular
642 * when using output of an bool instr to int input, or visa
646 * -------+---------+-------+-
650 * To convert from an adreno bool (uint) to nir, use:
652 * absneg.s dst, (neg)src
654 * To convert back in the other direction:
656 * absneg.s dst, (abs)arc
658 * The CP step can clean up the absneg.s that cancel each other
659 * out, and with a slight bit of extra cleverness (to recognize
660 * the instructions which produce either a 0 or 1) can eliminate
661 * the absneg.s's completely when an instruction that wants
662 * 0/1 consumes the result. For example, when a nir 'bcsel'
663 * consumes the result of 'feq'. So we should be able to get by
664 * without a boolean resolve step, and without incuring any
665 * extra penalty in instruction count.
668 /* NIR bool -> native (adreno): */
669 static struct ir3_instruction
*
670 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
672 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
675 /* native (adreno) -> NIR bool: */
676 static struct ir3_instruction
*
677 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
679 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
683 * alu/sfu instructions:
687 emit_alu(struct ir3_compile
*ctx
, nir_alu_instr
*alu
)
689 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
690 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
691 struct ir3_block
*b
= ctx
->block
;
693 dst
= get_dst(ctx
, &alu
->dest
.dest
, MAX2(info
->output_size
, 1));
695 /* Vectors are special in that they have non-scalarized writemasks,
696 * and just take the first swizzle channel for each argument in
697 * order into each writemask channel.
699 if ((alu
->op
== nir_op_vec2
) ||
700 (alu
->op
== nir_op_vec3
) ||
701 (alu
->op
== nir_op_vec4
)) {
703 for (int i
= 0; i
< info
->num_inputs
; i
++) {
704 nir_alu_src
*asrc
= &alu
->src
[i
];
706 compile_assert(ctx
, !asrc
->abs
);
707 compile_assert(ctx
, !asrc
->negate
);
709 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
711 src
[i
] = create_immed(ctx
->block
, 0);
712 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
718 /* General case: We can just grab the one used channel per src. */
719 for (int i
= 0; i
< info
->num_inputs
; i
++) {
720 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
721 nir_alu_src
*asrc
= &alu
->src
[i
];
723 compile_assert(ctx
, !asrc
->abs
);
724 compile_assert(ctx
, !asrc
->negate
);
726 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
728 compile_assert(ctx
, src
[i
]);
733 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_S32
);
736 dst
[0] = ir3_COV(b
, src
[0], TYPE_F32
, TYPE_U32
);
739 dst
[0] = ir3_COV(b
, src
[0], TYPE_S32
, TYPE_F32
);
742 dst
[0] = ir3_COV(b
, src
[0], TYPE_U32
, TYPE_F32
);
745 dst
[0] = ir3_MOV(b
, src
[0], TYPE_S32
);
748 dst
[0] = ir3_MOV(b
, src
[0], TYPE_F32
);
751 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
752 dst
[0]->cat2
.condition
= IR3_COND_NE
;
753 dst
[0] = ir3_n2b(b
, dst
[0]);
756 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
759 dst
[0] = ir3_b2n(b
, src
[0]);
762 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
763 dst
[0]->cat2
.condition
= IR3_COND_NE
;
764 dst
[0] = ir3_n2b(b
, dst
[0]);
768 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
771 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
774 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
777 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
780 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
783 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
786 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
789 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
792 dst
[0] = ir3_DSX(b
, src
[0], 0);
793 dst
[0]->cat5
.type
= TYPE_F32
;
796 dst
[0] = ir3_DSY(b
, src
[0], 0);
797 dst
[0]->cat5
.type
= TYPE_F32
;
801 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
802 dst
[0]->cat2
.condition
= IR3_COND_LT
;
803 dst
[0] = ir3_n2b(b
, dst
[0]);
806 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
807 dst
[0]->cat2
.condition
= IR3_COND_GE
;
808 dst
[0] = ir3_n2b(b
, dst
[0]);
811 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
812 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
813 dst
[0] = ir3_n2b(b
, dst
[0]);
816 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
817 dst
[0]->cat2
.condition
= IR3_COND_NE
;
818 dst
[0] = ir3_n2b(b
, dst
[0]);
821 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
824 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
827 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
829 case nir_op_fround_even
:
830 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
833 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
837 dst
[0] = ir3_SIN(b
, src
[0], 0);
840 dst
[0] = ir3_COS(b
, src
[0], 0);
843 dst
[0] = ir3_RSQ(b
, src
[0], 0);
846 dst
[0] = ir3_RCP(b
, src
[0], 0);
849 dst
[0] = ir3_LOG2(b
, src
[0], 0);
852 dst
[0] = ir3_EXP2(b
, src
[0], 0);
855 dst
[0] = ir3_SQRT(b
, src
[0], 0);
859 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
862 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
865 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
868 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
871 dst
[0] = ir3_MAX_U(b
, src
[0], 0, src
[1], 0);
874 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
877 dst
[0] = ir3_MIN_U(b
, src
[0], 0, src
[1], 0);
881 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
882 * mull.u tmp0, a, b ; mul low, i.e. al * bl
883 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
884 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
886 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
887 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
888 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
891 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
894 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
897 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
900 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
903 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
906 /* maybe this would be sane to lower in nir.. */
907 struct ir3_instruction
*neg
, *pos
;
909 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
910 neg
->cat2
.condition
= IR3_COND_LT
;
912 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
913 pos
->cat2
.condition
= IR3_COND_GT
;
915 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
920 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
923 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
926 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
929 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
930 dst
[0]->cat2
.condition
= IR3_COND_LT
;
931 dst
[0] = ir3_n2b(b
, dst
[0]);
934 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
935 dst
[0]->cat2
.condition
= IR3_COND_GE
;
936 dst
[0] = ir3_n2b(b
, dst
[0]);
939 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
940 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
941 dst
[0] = ir3_n2b(b
, dst
[0]);
944 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
945 dst
[0]->cat2
.condition
= IR3_COND_NE
;
946 dst
[0] = ir3_n2b(b
, dst
[0]);
949 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
950 dst
[0]->cat2
.condition
= IR3_COND_LT
;
951 dst
[0] = ir3_n2b(b
, dst
[0]);
954 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
955 dst
[0]->cat2
.condition
= IR3_COND_GE
;
956 dst
[0] = ir3_n2b(b
, dst
[0]);
960 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, ir3_b2n(b
, src
[0]), 0, src
[2], 0);
963 case nir_op_bit_count
:
964 dst
[0] = ir3_CBITS_B(b
, src
[0], 0);
966 case nir_op_ifind_msb
: {
967 struct ir3_instruction
*cmp
;
968 dst
[0] = ir3_CLZ_S(b
, src
[0], 0);
969 cmp
= ir3_CMPS_S(b
, dst
[0], 0, create_immed(b
, 0), 0);
970 cmp
->cat2
.condition
= IR3_COND_GE
;
971 dst
[0] = ir3_SEL_B32(b
,
972 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
976 case nir_op_ufind_msb
:
977 dst
[0] = ir3_CLZ_B(b
, src
[0], 0);
978 dst
[0] = ir3_SEL_B32(b
,
979 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
980 src
[0], 0, dst
[0], 0);
982 case nir_op_find_lsb
:
983 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
984 dst
[0] = ir3_CLZ_B(b
, dst
[0], 0);
986 case nir_op_bitfield_reverse
:
987 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
991 compile_error(ctx
, "Unhandled ALU op: %s\n",
992 nir_op_infos
[alu
->op
].name
);
997 /* handles direct/indirect UBO reads: */
999 emit_intrinsic_load_ubo(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
1000 struct ir3_instruction
**dst
)
1002 struct ir3_block
*b
= ctx
->block
;
1003 struct ir3_instruction
*addr
, *src0
, *src1
;
1004 nir_const_value
*const_offset
;
1005 /* UBO addresses are the first driver params: */
1006 unsigned ubo
= regid(ctx
->so
->first_driver_param
+ IR3_UBOS_OFF
, 0);
1009 /* First src is ubo index, which could either be an immed or not: */
1010 src0
= get_src(ctx
, &intr
->src
[0])[0];
1011 if (is_same_type_mov(src0
) &&
1012 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
1013 addr
= create_uniform(ctx
, ubo
+ src0
->regs
[1]->iim_val
);
1015 addr
= create_uniform_indirect(ctx
, ubo
, get_addr(ctx
, src0
));
1018 const_offset
= nir_src_as_const_value(intr
->src
[1]);
1020 off
+= const_offset
->u32
[0];
1022 /* For load_ubo_indirect, second src is indirect offset: */
1023 src1
= get_src(ctx
, &intr
->src
[1])[0];
1025 /* and add offset to addr: */
1026 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
1029 /* if offset is to large to encode in the ldg, split it out: */
1030 if ((off
+ (intr
->num_components
* 4)) > 1024) {
1031 /* split out the minimal amount to improve the odds that
1032 * cp can fit the immediate in the add.s instruction:
1034 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
1035 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
1039 for (int i
= 0; i
< intr
->num_components
; i
++) {
1040 struct ir3_instruction
*load
=
1041 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0);
1042 load
->cat6
.type
= TYPE_U32
;
1043 load
->cat6
.src_offset
= off
+ i
* 4; /* byte offset */
1048 /* handles array reads: */
1050 emit_intrinsic_load_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
,
1051 struct ir3_instruction
**dst
)
1053 nir_deref_var
*dvar
= intr
->variables
[0];
1054 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1055 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1057 compile_assert(ctx
, dvar
->deref
.child
&&
1058 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1060 switch (darr
->deref_array_type
) {
1061 case nir_deref_array_type_direct
:
1062 /* direct access does not require anything special: */
1063 for (int i
= 0; i
< intr
->num_components
; i
++) {
1064 unsigned n
= darr
->base_offset
* 4 + i
;
1065 compile_assert(ctx
, n
< arr
->length
);
1066 dst
[i
] = create_var_load(ctx
, arr
, n
, NULL
);
1069 case nir_deref_array_type_indirect
: {
1070 /* for indirect, we need to collect all the array elements: */
1071 struct ir3_instruction
*addr
=
1072 get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1073 for (int i
= 0; i
< intr
->num_components
; i
++) {
1074 unsigned n
= darr
->base_offset
* 4 + i
;
1075 compile_assert(ctx
, n
< arr
->length
);
1076 dst
[i
] = create_var_load(ctx
, arr
, n
, addr
);
1081 compile_error(ctx
, "Unhandled load deref type: %u\n",
1082 darr
->deref_array_type
);
1087 /* handles array writes: */
1089 emit_intrinsic_store_var(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1091 nir_deref_var
*dvar
= intr
->variables
[0];
1092 nir_deref_array
*darr
= nir_deref_as_array(dvar
->deref
.child
);
1093 struct ir3_array
*arr
= get_var(ctx
, dvar
->var
);
1094 struct ir3_instruction
*addr
, **src
;
1095 unsigned wrmask
= nir_intrinsic_write_mask(intr
);
1097 compile_assert(ctx
, dvar
->deref
.child
&&
1098 (dvar
->deref
.child
->deref_type
== nir_deref_type_array
));
1100 src
= get_src(ctx
, &intr
->src
[0]);
1102 switch (darr
->deref_array_type
) {
1103 case nir_deref_array_type_direct
:
1106 case nir_deref_array_type_indirect
:
1107 addr
= get_addr(ctx
, get_src(ctx
, &darr
->indirect
)[0]);
1110 compile_error(ctx
, "Unhandled store deref type: %u\n",
1111 darr
->deref_array_type
);
1115 for (int i
= 0; i
< intr
->num_components
; i
++) {
1116 if (!(wrmask
& (1 << i
)))
1118 unsigned n
= darr
->base_offset
* 4 + i
;
1119 compile_assert(ctx
, n
< arr
->length
);
1120 create_var_store(ctx
, arr
, n
, src
[i
], addr
);
1124 static void add_sysval_input(struct ir3_compile
*ctx
, gl_system_value slot
,
1125 struct ir3_instruction
*instr
)
1127 struct ir3_shader_variant
*so
= ctx
->so
;
1128 unsigned r
= regid(so
->inputs_count
, 0);
1129 unsigned n
= so
->inputs_count
++;
1131 so
->inputs
[n
].sysval
= true;
1132 so
->inputs
[n
].slot
= slot
;
1133 so
->inputs
[n
].compmask
= 1;
1134 so
->inputs
[n
].regid
= r
;
1135 so
->inputs
[n
].interpolate
= INTERP_QUALIFIER_FLAT
;
1138 ctx
->ir
->ninputs
= MAX2(ctx
->ir
->ninputs
, r
+ 1);
1139 ctx
->ir
->inputs
[r
] = instr
;
1143 emit_intrinsic(struct ir3_compile
*ctx
, nir_intrinsic_instr
*intr
)
1145 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
1146 struct ir3_instruction
**dst
, **src
;
1147 struct ir3_block
*b
= ctx
->block
;
1148 nir_const_value
*const_offset
;
1151 if (info
->has_dest
) {
1152 dst
= get_dst(ctx
, &intr
->dest
, intr
->num_components
);
1157 switch (intr
->intrinsic
) {
1158 case nir_intrinsic_load_uniform
:
1159 idx
= nir_intrinsic_base(intr
);
1160 const_offset
= nir_src_as_const_value(intr
->src
[0]);
1162 idx
+= const_offset
->u32
[0];
1163 for (int i
= 0; i
< intr
->num_components
; i
++) {
1164 unsigned n
= idx
* 4 + i
;
1165 dst
[i
] = create_uniform(ctx
, n
);
1168 src
= get_src(ctx
, &intr
->src
[0]);
1169 for (int i
= 0; i
< intr
->num_components
; i
++) {
1170 int n
= idx
* 4 + i
;
1171 dst
[i
] = create_uniform_indirect(ctx
, n
,
1172 get_addr(ctx
, src
[0]));
1174 /* NOTE: if relative addressing is used, we set
1175 * constlen in the compiler (to worst-case value)
1176 * since we don't know in the assembler what the max
1177 * addr reg value can be:
1179 ctx
->so
->constlen
= ctx
->s
->num_uniforms
;
1182 case nir_intrinsic_load_ubo
:
1183 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
1185 case nir_intrinsic_load_input
:
1186 idx
= nir_intrinsic_base(intr
);
1187 const_offset
= nir_src_as_const_value(intr
->src
[0]);
1189 idx
+= const_offset
->u32
[0];
1190 for (int i
= 0; i
< intr
->num_components
; i
++) {
1191 unsigned n
= idx
* 4 + i
;
1192 dst
[i
] = ctx
->ir
->inputs
[n
];
1195 src
= get_src(ctx
, &intr
->src
[0]);
1196 struct ir3_instruction
*collect
=
1197 create_collect(b
, ctx
->ir
->inputs
, ctx
->ir
->ninputs
);
1198 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0]);
1199 for (int i
= 0; i
< intr
->num_components
; i
++) {
1200 unsigned n
= idx
* 4 + i
;
1201 dst
[i
] = create_indirect_load(ctx
, ctx
->ir
->ninputs
,
1206 case nir_intrinsic_load_var
:
1207 emit_intrinsic_load_var(ctx
, intr
, dst
);
1209 case nir_intrinsic_store_var
:
1210 emit_intrinsic_store_var(ctx
, intr
);
1212 case nir_intrinsic_store_output
:
1213 idx
= nir_intrinsic_base(intr
);
1214 const_offset
= nir_src_as_const_value(intr
->src
[1]);
1215 compile_assert(ctx
, const_offset
!= NULL
);
1216 idx
+= const_offset
->u32
[0];
1218 src
= get_src(ctx
, &intr
->src
[0]);
1219 for (int i
= 0; i
< intr
->num_components
; i
++) {
1220 unsigned n
= idx
* 4 + i
;
1221 ctx
->ir
->outputs
[n
] = src
[i
];
1224 case nir_intrinsic_load_base_vertex
:
1225 if (!ctx
->basevertex
) {
1226 ctx
->basevertex
= create_driver_param(ctx
, IR3_DP_VTXID_BASE
);
1227 add_sysval_input(ctx
, SYSTEM_VALUE_BASE_VERTEX
,
1230 dst
[0] = ctx
->basevertex
;
1232 case nir_intrinsic_load_vertex_id_zero_base
:
1233 if (!ctx
->vertex_id
) {
1234 ctx
->vertex_id
= create_input(b
, 0);
1235 add_sysval_input(ctx
, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
,
1238 dst
[0] = ctx
->vertex_id
;
1240 case nir_intrinsic_load_instance_id
:
1241 if (!ctx
->instance_id
) {
1242 ctx
->instance_id
= create_input(b
, 0);
1243 add_sysval_input(ctx
, SYSTEM_VALUE_INSTANCE_ID
,
1246 dst
[0] = ctx
->instance_id
;
1248 case nir_intrinsic_load_user_clip_plane
:
1249 idx
= nir_intrinsic_ucp_id(intr
);
1250 for (int i
= 0; i
< intr
->num_components
; i
++) {
1251 unsigned n
= idx
* 4 + i
;
1252 dst
[i
] = create_driver_param(ctx
, IR3_DP_UCP0_X
+ n
);
1255 case nir_intrinsic_load_front_face
:
1256 if (!ctx
->frag_face
) {
1257 ctx
->so
->frag_face
= true;
1258 ctx
->frag_face
= create_input(b
, 0);
1259 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
1261 dst
[0] = ir3_ADD_S(b
, ctx
->frag_face
, 0, create_immed(b
, 1), 0);
1263 case nir_intrinsic_discard_if
:
1264 case nir_intrinsic_discard
: {
1265 struct ir3_instruction
*cond
, *kill
;
1267 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
1268 /* conditional discard: */
1269 src
= get_src(ctx
, &intr
->src
[0]);
1270 cond
= ir3_b2n(b
, src
[0]);
1272 /* unconditional discard: */
1273 cond
= create_immed(b
, 1);
1276 /* NOTE: only cmps.*.* can write p0.x: */
1277 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
1278 cond
->cat2
.condition
= IR3_COND_NE
;
1280 /* condition always goes in predicate register: */
1281 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1283 kill
= ir3_KILL(b
, cond
, 0);
1284 array_insert(ctx
->ir
->predicates
, kill
);
1286 array_insert(ctx
->ir
->keeps
, kill
);
1287 ctx
->so
->has_kill
= true;
1292 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
1293 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1299 emit_load_const(struct ir3_compile
*ctx
, nir_load_const_instr
*instr
)
1301 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
1302 instr
->def
.num_components
);
1303 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1304 dst
[i
] = create_immed(ctx
->block
, instr
->value
.u32
[i
]);
1308 emit_undef(struct ir3_compile
*ctx
, nir_ssa_undef_instr
*undef
)
1310 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
1311 undef
->def
.num_components
);
1312 /* backend doesn't want undefined instructions, so just plug
1315 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
1316 dst
[i
] = create_immed(ctx
->block
, fui(0.0));
1320 * texture fetch/sample instructions:
1324 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
1326 unsigned coords
, flags
= 0;
1328 /* note: would use tex->coord_components.. except txs.. also,
1329 * since array index goes after shadow ref, we don't want to
1332 switch (tex
->sampler_dim
) {
1333 case GLSL_SAMPLER_DIM_1D
:
1334 case GLSL_SAMPLER_DIM_BUF
:
1337 case GLSL_SAMPLER_DIM_2D
:
1338 case GLSL_SAMPLER_DIM_RECT
:
1339 case GLSL_SAMPLER_DIM_EXTERNAL
:
1340 case GLSL_SAMPLER_DIM_MS
:
1343 case GLSL_SAMPLER_DIM_3D
:
1344 case GLSL_SAMPLER_DIM_CUBE
:
1346 flags
|= IR3_INSTR_3D
;
1349 unreachable("bad sampler_dim");
1352 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
1353 flags
|= IR3_INSTR_S
;
1355 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
)
1356 flags
|= IR3_INSTR_A
;
1363 emit_tex(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1365 struct ir3_block
*b
= ctx
->block
;
1366 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
1367 struct ir3_instruction
**coord
, *lod
, *compare
, *proj
, **off
, **ddx
, **ddy
;
1368 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
1369 unsigned i
, coords
, flags
;
1370 unsigned nsrc0
= 0, nsrc1
= 0;
1374 coord
= off
= ddx
= ddy
= NULL
;
1375 lod
= proj
= compare
= NULL
;
1377 /* TODO: might just be one component for gathers? */
1378 dst
= get_dst(ctx
, &tex
->dest
, 4);
1380 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1381 switch (tex
->src
[i
].src_type
) {
1382 case nir_tex_src_coord
:
1383 coord
= get_src(ctx
, &tex
->src
[i
].src
);
1385 case nir_tex_src_bias
:
1386 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1389 case nir_tex_src_lod
:
1390 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1393 case nir_tex_src_comparitor
: /* shadow comparator */
1394 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1396 case nir_tex_src_projector
:
1397 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
1400 case nir_tex_src_offset
:
1401 off
= get_src(ctx
, &tex
->src
[i
].src
);
1404 case nir_tex_src_ddx
:
1405 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
1407 case nir_tex_src_ddy
:
1408 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
1411 compile_error(ctx
, "Unhandled NIR tex src type: %d\n",
1412 tex
->src
[i
].src_type
);
1418 case nir_texop_tex
: opc
= OPC_SAM
; break;
1419 case nir_texop_txb
: opc
= OPC_SAMB
; break;
1420 case nir_texop_txl
: opc
= OPC_SAML
; break;
1421 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
1422 case nir_texop_txf
: opc
= OPC_ISAML
; break;
1423 case nir_texop_lod
: opc
= OPC_GETLOD
; break;
1424 case nir_texop_txf_ms
:
1427 case nir_texop_query_levels
:
1428 case nir_texop_texture_samples
:
1429 case nir_texop_samples_identical
:
1430 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
1434 tex_info(tex
, &flags
, &coords
);
1436 /* scale up integer coords for TXF based on the LOD */
1437 if (ctx
->unminify_coords
&& (opc
== OPC_ISAML
)) {
1439 for (i
= 0; i
< coords
; i
++)
1440 coord
[i
] = ir3_SHL_B(b
, coord
[i
], 0, lod
, 0);
1443 /* the array coord for cube arrays needs 0.5 added to it */
1444 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&& tex
->is_array
&&
1446 coord
[3] = ir3_ADD_F(b
, coord
[3], 0, create_immed(b
, fui(0.5)), 0);
1449 * lay out the first argument in the proper order:
1450 * - actual coordinates first
1451 * - shadow reference
1454 * - starting at offset 4, dpdx.xy, dpdy.xy
1456 * bias/lod go into the second arg
1459 /* insert tex coords: */
1460 for (i
= 0; i
< coords
; i
++)
1461 src0
[nsrc0
++] = coord
[i
];
1464 /* hw doesn't do 1d, so we treat it as 2d with
1465 * height of 1, and patch up the y coord.
1466 * TODO: y coord should be (int)0 in some cases..
1468 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
1471 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
1472 src0
[nsrc0
++] = compare
;
1474 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
)
1475 src0
[nsrc0
++] = coord
[coords
];
1478 src0
[nsrc0
++] = proj
;
1479 flags
|= IR3_INSTR_P
;
1482 /* pad to 4, then ddx/ddy: */
1483 if (tex
->op
== nir_texop_txd
) {
1485 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1486 for (i
= 0; i
< coords
; i
++)
1487 src0
[nsrc0
++] = ddx
[i
];
1489 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1490 for (i
= 0; i
< coords
; i
++)
1491 src0
[nsrc0
++] = ddy
[i
];
1493 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
1497 * second argument (if applicable):
1502 if (has_off
| has_lod
| has_bias
) {
1504 for (i
= 0; i
< coords
; i
++)
1505 src1
[nsrc1
++] = off
[i
];
1507 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
1508 flags
|= IR3_INSTR_O
;
1511 if (has_lod
| has_bias
)
1512 src1
[nsrc1
++] = lod
;
1515 switch (tex
->dest_type
) {
1516 case nir_type_invalid
:
1517 case nir_type_float
:
1528 unreachable("bad dest_type");
1531 if (opc
== OPC_GETLOD
)
1534 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
,
1535 flags
, tex
->texture_index
, tex
->texture_index
,
1536 create_collect(b
, src0
, nsrc0
),
1537 create_collect(b
, src1
, nsrc1
));
1539 split_dest(b
, dst
, sam
, 4);
1541 /* GETLOD returns results in 4.8 fixed point */
1542 if (opc
== OPC_GETLOD
) {
1543 struct ir3_instruction
*factor
= create_immed(b
, fui(1.0 / 256));
1545 compile_assert(ctx
, tex
->dest_type
== nir_type_float
);
1546 for (i
= 0; i
< 2; i
++) {
1547 dst
[i
] = ir3_MUL_F(b
, ir3_COV(b
, dst
[i
], TYPE_U32
, TYPE_F32
), 0,
1554 emit_tex_query_levels(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1556 struct ir3_block
*b
= ctx
->block
;
1557 struct ir3_instruction
**dst
, *sam
;
1559 dst
= get_dst(ctx
, &tex
->dest
, 1);
1561 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, TGSI_WRITEMASK_Z
, 0,
1562 tex
->texture_index
, tex
->texture_index
, NULL
, NULL
);
1564 /* even though there is only one component, since it ends
1565 * up in .z rather than .x, we need a split_dest()
1567 split_dest(b
, dst
, sam
, 3);
1569 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1570 * the value in TEX_CONST_0 is zero-based.
1572 if (ctx
->levels_add_one
)
1573 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
1577 emit_tex_txs(struct ir3_compile
*ctx
, nir_tex_instr
*tex
)
1579 struct ir3_block
*b
= ctx
->block
;
1580 struct ir3_instruction
**dst
, *sam
, *lod
;
1581 unsigned flags
, coords
;
1583 tex_info(tex
, &flags
, &coords
);
1585 /* Actually we want the number of dimensions, not coordinates. This
1586 * distinction only matters for cubes.
1588 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
1591 dst
= get_dst(ctx
, &tex
->dest
, 4);
1593 compile_assert(ctx
, tex
->num_srcs
== 1);
1594 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
1596 lod
= get_src(ctx
, &tex
->src
[0].src
)[0];
1598 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
1599 tex
->texture_index
, tex
->texture_index
, lod
, NULL
);
1601 split_dest(b
, dst
, sam
, 4);
1603 /* Array size actually ends up in .w rather than .z. This doesn't
1604 * matter for miplevel 0, but for higher mips the value in z is
1605 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1606 * returned, which means that we have to add 1 to it for arrays.
1608 if (tex
->is_array
) {
1609 if (ctx
->levels_add_one
) {
1610 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
1612 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
1618 emit_phi(struct ir3_compile
*ctx
, nir_phi_instr
*nphi
)
1620 struct ir3_instruction
*phi
, **dst
;
1622 /* NOTE: phi's should be lowered to scalar at this point */
1623 compile_assert(ctx
, nphi
->dest
.ssa
.num_components
== 1);
1625 dst
= get_dst(ctx
, &nphi
->dest
, 1);
1627 phi
= ir3_instr_create2(ctx
->block
, -1, OPC_META_PHI
,
1628 1 + exec_list_length(&nphi
->srcs
));
1629 ir3_reg_create(phi
, 0, 0); /* dst */
1630 phi
->phi
.nphi
= nphi
;
1635 /* phi instructions are left partially constructed. We don't resolve
1636 * their srcs until the end of the block, since (eg. loops) one of
1637 * the phi's srcs might be defined after the phi due to back edges in
1641 resolve_phis(struct ir3_compile
*ctx
, struct ir3_block
*block
)
1643 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
1644 nir_phi_instr
*nphi
;
1646 /* phi's only come at start of block: */
1647 if (!(is_meta(instr
) && (instr
->opc
== OPC_META_PHI
)))
1650 if (!instr
->phi
.nphi
)
1653 nphi
= instr
->phi
.nphi
;
1654 instr
->phi
.nphi
= NULL
;
1656 foreach_list_typed(nir_phi_src
, nsrc
, node
, &nphi
->srcs
) {
1657 struct ir3_instruction
*src
= get_src(ctx
, &nsrc
->src
)[0];
1658 ir3_reg_create(instr
, 0, IR3_REG_SSA
)->instr
= src
;
1664 emit_jump(struct ir3_compile
*ctx
, nir_jump_instr
*jump
)
1666 switch (jump
->type
) {
1667 case nir_jump_break
:
1668 case nir_jump_continue
:
1669 /* I *think* we can simply just ignore this, and use the
1670 * successor block link to figure out where we need to
1671 * jump to for break/continue
1675 compile_error(ctx
, "Unhandled NIR jump type: %d\n", jump
->type
);
1681 emit_instr(struct ir3_compile
*ctx
, nir_instr
*instr
)
1683 switch (instr
->type
) {
1684 case nir_instr_type_alu
:
1685 emit_alu(ctx
, nir_instr_as_alu(instr
));
1687 case nir_instr_type_intrinsic
:
1688 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1690 case nir_instr_type_load_const
:
1691 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1693 case nir_instr_type_ssa_undef
:
1694 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1696 case nir_instr_type_tex
: {
1697 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
1698 /* couple tex instructions get special-cased:
1702 emit_tex_txs(ctx
, tex
);
1704 case nir_texop_query_levels
:
1705 emit_tex_query_levels(ctx
, tex
);
1713 case nir_instr_type_phi
:
1714 emit_phi(ctx
, nir_instr_as_phi(instr
));
1716 case nir_instr_type_jump
:
1717 emit_jump(ctx
, nir_instr_as_jump(instr
));
1719 case nir_instr_type_call
:
1720 case nir_instr_type_parallel_copy
:
1721 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
1726 static struct ir3_block
*
1727 get_block(struct ir3_compile
*ctx
, nir_block
*nblock
)
1729 struct ir3_block
*block
;
1730 struct hash_entry
*entry
;
1731 entry
= _mesa_hash_table_search(ctx
->block_ht
, nblock
);
1735 block
= ir3_block_create(ctx
->ir
);
1736 block
->nblock
= nblock
;
1737 _mesa_hash_table_insert(ctx
->block_ht
, nblock
, block
);
1743 emit_block(struct ir3_compile
*ctx
, nir_block
*nblock
)
1745 struct ir3_block
*block
= get_block(ctx
, nblock
);
1747 for (int i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
1748 if (nblock
->successors
[i
]) {
1749 block
->successors
[i
] =
1750 get_block(ctx
, nblock
->successors
[i
]);
1755 list_addtail(&block
->node
, &ctx
->ir
->block_list
);
1757 /* re-emit addr register in each block if needed: */
1758 _mesa_hash_table_destroy(ctx
->addr_ht
, NULL
);
1759 ctx
->addr_ht
= NULL
;
1761 nir_foreach_instr(nblock
, instr
) {
1762 emit_instr(ctx
, instr
);
1768 static void emit_cf_list(struct ir3_compile
*ctx
, struct exec_list
*list
);
1771 emit_if(struct ir3_compile
*ctx
, nir_if
*nif
)
1773 struct ir3_instruction
*condition
= get_src(ctx
, &nif
->condition
)[0];
1775 ctx
->block
->condition
=
1776 get_predicate(ctx
, ir3_b2n(condition
->block
, condition
));
1778 emit_cf_list(ctx
, &nif
->then_list
);
1779 emit_cf_list(ctx
, &nif
->else_list
);
1783 emit_loop(struct ir3_compile
*ctx
, nir_loop
*nloop
)
1785 emit_cf_list(ctx
, &nloop
->body
);
1789 emit_cf_list(struct ir3_compile
*ctx
, struct exec_list
*list
)
1791 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1792 switch (node
->type
) {
1793 case nir_cf_node_block
:
1794 emit_block(ctx
, nir_cf_node_as_block(node
));
1796 case nir_cf_node_if
:
1797 emit_if(ctx
, nir_cf_node_as_if(node
));
1799 case nir_cf_node_loop
:
1800 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1802 case nir_cf_node_function
:
1803 compile_error(ctx
, "TODO\n");
1809 /* emit stream-out code. At this point, the current block is the original
1810 * (nir) end block, and nir ensures that all flow control paths terminate
1811 * into the end block. We re-purpose the original end block to generate
1812 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
1813 * block holding stream-out write instructions, followed by the new end
1817 * p0.x = (vtxcnt < maxvtxcnt)
1818 * // succs: blockStreamOut, blockNewEnd
1821 * ... stream-out instructions ...
1822 * // succs: blockNewEnd
1828 emit_stream_out(struct ir3_compile
*ctx
)
1830 struct ir3_shader_variant
*v
= ctx
->so
;
1831 struct ir3
*ir
= ctx
->ir
;
1832 struct pipe_stream_output_info
*strmout
=
1833 &ctx
->so
->shader
->stream_output
;
1834 struct ir3_block
*orig_end_block
, *stream_out_block
, *new_end_block
;
1835 struct ir3_instruction
*vtxcnt
, *maxvtxcnt
, *cond
;
1836 struct ir3_instruction
*bases
[PIPE_MAX_SO_BUFFERS
];
1838 /* create vtxcnt input in input block at top of shader,
1839 * so that it is seen as live over the entire duration
1842 vtxcnt
= create_input(ctx
->in_block
, 0);
1843 add_sysval_input(ctx
, SYSTEM_VALUE_VERTEX_CNT
, vtxcnt
);
1845 maxvtxcnt
= create_driver_param(ctx
, IR3_DP_VTXCNT_MAX
);
1847 /* at this point, we are at the original 'end' block,
1848 * re-purpose this block to stream-out condition, then
1849 * append stream-out block and new-end block
1851 orig_end_block
= ctx
->block
;
1853 stream_out_block
= ir3_block_create(ir
);
1854 list_addtail(&stream_out_block
->node
, &ir
->block_list
);
1856 new_end_block
= ir3_block_create(ir
);
1857 list_addtail(&new_end_block
->node
, &ir
->block_list
);
1859 orig_end_block
->successors
[0] = stream_out_block
;
1860 orig_end_block
->successors
[1] = new_end_block
;
1861 stream_out_block
->successors
[0] = new_end_block
;
1863 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
1864 cond
= ir3_CMPS_S(ctx
->block
, vtxcnt
, 0, maxvtxcnt
, 0);
1865 cond
->regs
[0]->num
= regid(REG_P0
, 0);
1866 cond
->cat2
.condition
= IR3_COND_LT
;
1868 /* condition goes on previous block to the conditional,
1869 * since it is used to pick which of the two successor
1872 orig_end_block
->condition
= cond
;
1874 /* switch to stream_out_block to generate the stream-out
1877 ctx
->block
= stream_out_block
;
1879 /* Calculate base addresses based on vtxcnt. Instructions
1880 * generated for bases not used in following loop will be
1881 * stripped out in the backend.
1883 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
1884 unsigned stride
= strmout
->stride
[i
];
1885 struct ir3_instruction
*base
, *off
;
1887 base
= create_uniform(ctx
, regid(v
->first_driver_param
+ IR3_TFBOS_OFF
, i
));
1889 /* 24-bit should be enough: */
1890 off
= ir3_MUL_U(ctx
->block
, vtxcnt
, 0,
1891 create_immed(ctx
->block
, stride
* 4), 0);
1893 bases
[i
] = ir3_ADD_S(ctx
->block
, off
, 0, base
, 0);
1896 /* Generate the per-output store instructions: */
1897 for (unsigned i
= 0; i
< strmout
->num_outputs
; i
++) {
1898 for (unsigned j
= 0; j
< strmout
->output
[i
].num_components
; j
++) {
1899 unsigned c
= j
+ strmout
->output
[i
].start_component
;
1900 struct ir3_instruction
*base
, *out
, *stg
;
1902 base
= bases
[strmout
->output
[i
].output_buffer
];
1903 out
= ctx
->ir
->outputs
[regid(strmout
->output
[i
].register_index
, c
)];
1905 stg
= ir3_STG(ctx
->block
, base
, 0, out
, 0,
1906 create_immed(ctx
->block
, 1), 0);
1907 stg
->cat6
.type
= TYPE_U32
;
1908 stg
->cat6
.dst_offset
= (strmout
->output
[i
].dst_offset
+ j
) * 4;
1910 array_insert(ctx
->ir
->keeps
, stg
);
1914 /* and finally switch to the new_end_block: */
1915 ctx
->block
= new_end_block
;
1919 emit_function(struct ir3_compile
*ctx
, nir_function_impl
*impl
)
1921 nir_metadata_require(impl
, nir_metadata_block_index
);
1923 emit_cf_list(ctx
, &impl
->body
);
1924 emit_block(ctx
, impl
->end_block
);
1926 /* at this point, we should have a single empty block,
1927 * into which we emit the 'end' instruction.
1929 compile_assert(ctx
, list_empty(&ctx
->block
->instr_list
));
1931 /* If stream-out (aka transform-feedback) enabled, emit the
1932 * stream-out instructions, followed by a new empty block (into
1933 * which the 'end' instruction lands).
1935 * NOTE: it is done in this order, rather than inserting before
1936 * we emit end_block, because NIR guarantees that all blocks
1937 * flow into end_block, and that end_block has no successors.
1938 * So by re-purposing end_block as the first block of stream-
1939 * out, we guarantee that all exit paths flow into the stream-
1942 if ((ctx
->so
->shader
->stream_output
.num_outputs
> 0) &&
1943 !ctx
->so
->key
.binning_pass
) {
1944 debug_assert(ctx
->so
->type
== SHADER_VERTEX
);
1945 emit_stream_out(ctx
);
1948 ir3_END(ctx
->block
);
1952 setup_input(struct ir3_compile
*ctx
, nir_variable
*in
)
1954 struct ir3_shader_variant
*so
= ctx
->so
;
1955 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
1956 unsigned ncomp
= glsl_get_components(in
->type
);
1957 unsigned n
= in
->data
.driver_location
;
1958 unsigned slot
= in
->data
.location
;
1960 DBG("; in: slot=%u, len=%ux%u, drvloc=%u",
1961 slot
, array_len
, ncomp
, n
);
1963 so
->inputs
[n
].slot
= slot
;
1964 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1965 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
1966 so
->inputs
[n
].interpolate
= in
->data
.interpolation
;
1968 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
1969 for (int i
= 0; i
< ncomp
; i
++) {
1970 struct ir3_instruction
*instr
= NULL
;
1971 unsigned idx
= (n
* 4) + i
;
1973 if (slot
== VARYING_SLOT_POS
) {
1974 so
->inputs
[n
].bary
= false;
1975 so
->frag_coord
= true;
1976 instr
= create_frag_coord(ctx
, i
);
1977 } else if (slot
== VARYING_SLOT_FACE
) {
1978 so
->inputs
[n
].bary
= false;
1979 so
->frag_face
= true;
1980 instr
= create_frag_face(ctx
, i
);
1982 bool use_ldlv
= false;
1984 /* detect the special case for front/back colors where
1985 * we need to do flat vs smooth shading depending on
1988 if (in
->data
.interpolation
== INTERP_QUALIFIER_NONE
) {
1990 case VARYING_SLOT_COL0
:
1991 case VARYING_SLOT_COL1
:
1992 case VARYING_SLOT_BFC0
:
1993 case VARYING_SLOT_BFC1
:
1994 so
->inputs
[n
].rasterflat
= true;
2001 if (ctx
->flat_bypass
) {
2002 if ((so
->inputs
[n
].interpolate
== INTERP_QUALIFIER_FLAT
) ||
2003 (so
->inputs
[n
].rasterflat
&& ctx
->so
->key
.rasterflat
))
2007 so
->inputs
[n
].bary
= true;
2009 instr
= create_frag_input(ctx
, use_ldlv
);
2012 ctx
->ir
->inputs
[idx
] = instr
;
2014 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
2015 for (int i
= 0; i
< ncomp
; i
++) {
2016 unsigned idx
= (n
* 4) + i
;
2017 ctx
->ir
->inputs
[idx
] = create_input(ctx
->block
, idx
);
2020 compile_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2023 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
2024 so
->total_in
+= ncomp
;
2029 setup_output(struct ir3_compile
*ctx
, nir_variable
*out
)
2031 struct ir3_shader_variant
*so
= ctx
->so
;
2032 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
2033 unsigned ncomp
= glsl_get_components(out
->type
);
2034 unsigned n
= out
->data
.driver_location
;
2035 unsigned slot
= out
->data
.location
;
2038 DBG("; out: slot=%u, len=%ux%u, drvloc=%u",
2039 slot
, array_len
, ncomp
, n
);
2041 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2043 case FRAG_RESULT_DEPTH
:
2044 comp
= 2; /* tgsi will write to .z component */
2045 so
->writes_pos
= true;
2047 case FRAG_RESULT_COLOR
:
2051 if (slot
>= FRAG_RESULT_DATA0
)
2053 compile_error(ctx
, "unknown FS output name: %s\n",
2054 gl_frag_result_name(slot
));
2056 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
2058 case VARYING_SLOT_POS
:
2059 so
->writes_pos
= true;
2061 case VARYING_SLOT_PSIZ
:
2062 so
->writes_psize
= true;
2064 case VARYING_SLOT_COL0
:
2065 case VARYING_SLOT_COL1
:
2066 case VARYING_SLOT_BFC0
:
2067 case VARYING_SLOT_BFC1
:
2068 case VARYING_SLOT_FOGC
:
2069 case VARYING_SLOT_CLIP_DIST0
:
2070 case VARYING_SLOT_CLIP_DIST1
:
2072 case VARYING_SLOT_CLIP_VERTEX
:
2073 /* handled entirely in nir_lower_clip: */
2076 if (slot
>= VARYING_SLOT_VAR0
)
2078 if ((VARYING_SLOT_TEX0
<= slot
) && (slot
<= VARYING_SLOT_TEX7
))
2080 compile_error(ctx
, "unknown VS output name: %s\n",
2081 gl_varying_slot_name(slot
));
2084 compile_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
2087 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
2089 so
->outputs
[n
].slot
= slot
;
2090 so
->outputs
[n
].regid
= regid(n
, comp
);
2091 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
2093 for (int i
= 0; i
< ncomp
; i
++) {
2094 unsigned idx
= (n
* 4) + i
;
2096 ctx
->ir
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
2101 emit_instructions(struct ir3_compile
*ctx
)
2103 unsigned ninputs
, noutputs
;
2104 nir_function_impl
*fxn
= NULL
;
2106 /* Find the main function: */
2107 nir_foreach_function(ctx
->s
, function
) {
2108 compile_assert(ctx
, strcmp(function
->name
, "main") == 0);
2109 compile_assert(ctx
, function
->impl
);
2110 fxn
= function
->impl
;
2114 ninputs
= exec_list_length(&ctx
->s
->inputs
) * 4;
2115 noutputs
= exec_list_length(&ctx
->s
->outputs
) * 4;
2117 /* or vtx shaders, we need to leave room for sysvals:
2119 if (ctx
->so
->type
== SHADER_VERTEX
) {
2123 ctx
->ir
= ir3_create(ctx
->compiler
, ninputs
, noutputs
);
2125 /* Create inputs in first block: */
2126 ctx
->block
= get_block(ctx
, nir_start_block(fxn
));
2127 ctx
->in_block
= ctx
->block
;
2128 list_addtail(&ctx
->block
->node
, &ctx
->ir
->block_list
);
2130 if (ctx
->so
->type
== SHADER_VERTEX
) {
2131 ctx
->ir
->ninputs
-= 8;
2134 /* for fragment shader, we have a single input register (usually
2135 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2137 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
2138 // TODO maybe a helper for fi since we need it a few places..
2139 struct ir3_instruction
*instr
;
2140 instr
= ir3_instr_create(ctx
->block
, -1, OPC_META_FI
);
2141 ir3_reg_create(instr
, 0, 0);
2142 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
2143 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
2144 ctx
->frag_pos
= instr
;
2148 nir_foreach_variable(var
, &ctx
->s
->inputs
) {
2149 setup_input(ctx
, var
);
2152 /* Setup outputs: */
2153 nir_foreach_variable(var
, &ctx
->s
->outputs
) {
2154 setup_output(ctx
, var
);
2157 /* Setup global variables (which should only be arrays): */
2158 nir_foreach_variable(var
, &ctx
->s
->globals
) {
2159 declare_var(ctx
, var
);
2162 /* Setup local variables (which should only be arrays): */
2163 /* NOTE: need to do something more clever when we support >1 fxn */
2164 nir_foreach_variable(var
, &fxn
->locals
) {
2165 declare_var(ctx
, var
);
2168 /* And emit the body: */
2170 emit_function(ctx
, fxn
);
2172 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
2173 resolve_phis(ctx
, block
);
2177 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2178 * for a fragment shader are just bary.f instructions. The *actual* inputs
2179 * from the hw perspective are the frag_pos and optionally frag_coord and
2183 fixup_frag_inputs(struct ir3_compile
*ctx
)
2185 struct ir3_shader_variant
*so
= ctx
->so
;
2186 struct ir3
*ir
= ctx
->ir
;
2187 struct ir3_instruction
**inputs
;
2188 struct ir3_instruction
*instr
;
2193 n
= 4; /* always have frag_pos */
2194 n
+= COND(so
->frag_face
, 4);
2195 n
+= COND(so
->frag_coord
, 4);
2197 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
2199 if (so
->frag_face
) {
2200 /* this ultimately gets assigned to hr0.x so doesn't conflict
2201 * with frag_coord/frag_pos..
2203 inputs
[ir
->ninputs
++] = ctx
->frag_face
;
2204 ctx
->frag_face
->regs
[0]->num
= 0;
2206 /* remaining channels not used, but let's avoid confusing
2207 * other parts that expect inputs to come in groups of vec4
2209 inputs
[ir
->ninputs
++] = NULL
;
2210 inputs
[ir
->ninputs
++] = NULL
;
2211 inputs
[ir
->ninputs
++] = NULL
;
2214 /* since we don't know where to set the regid for frag_coord,
2215 * we have to use r0.x for it. But we don't want to *always*
2216 * use r1.x for frag_pos as that could increase the register
2217 * footprint on simple shaders:
2219 if (so
->frag_coord
) {
2220 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
2221 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
2222 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
2223 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
2225 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[0];
2226 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[1];
2227 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[2];
2228 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[3];
2231 /* we always have frag_pos: */
2232 so
->pos_regid
= regid
;
2235 instr
= create_input(ctx
->in_block
, ir
->ninputs
);
2236 instr
->regs
[0]->num
= regid
++;
2237 inputs
[ir
->ninputs
++] = instr
;
2238 ctx
->frag_pos
->regs
[1]->instr
= instr
;
2241 instr
= create_input(ctx
->in_block
, ir
->ninputs
);
2242 instr
->regs
[0]->num
= regid
++;
2243 inputs
[ir
->ninputs
++] = instr
;
2244 ctx
->frag_pos
->regs
[2]->instr
= instr
;
2246 ir
->inputs
= inputs
;
2250 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
2251 struct ir3_shader_variant
*so
)
2253 struct ir3_compile
*ctx
;
2255 struct ir3_instruction
**inputs
;
2256 unsigned i
, j
, actual_in
, inloc
;
2257 int ret
= 0, max_bary
;
2261 ctx
= compile_init(compiler
, so
);
2263 DBG("INIT failed!");
2268 emit_instructions(ctx
);
2271 DBG("EMIT failed!");
2276 ir
= so
->ir
= ctx
->ir
;
2278 /* keep track of the inputs from TGSI perspective.. */
2279 inputs
= ir
->inputs
;
2281 /* but fixup actual inputs for frag shader: */
2282 if (so
->type
== SHADER_FRAGMENT
)
2283 fixup_frag_inputs(ctx
);
2285 /* at this point, for binning pass, throw away unneeded outputs: */
2286 if (so
->key
.binning_pass
) {
2287 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
2288 unsigned slot
= so
->outputs
[i
].slot
;
2290 /* throw away everything but first position/psize */
2291 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
2293 so
->outputs
[j
] = so
->outputs
[i
];
2294 ir
->outputs
[(j
*4)+0] = ir
->outputs
[(i
*4)+0];
2295 ir
->outputs
[(j
*4)+1] = ir
->outputs
[(i
*4)+1];
2296 ir
->outputs
[(j
*4)+2] = ir
->outputs
[(i
*4)+2];
2297 ir
->outputs
[(j
*4)+3] = ir
->outputs
[(i
*4)+3];
2302 so
->outputs_count
= j
;
2303 ir
->noutputs
= j
* 4;
2306 /* if we want half-precision outputs, mark the output registers
2309 if (so
->key
.half_precision
) {
2310 for (i
= 0; i
< ir
->noutputs
; i
++) {
2311 struct ir3_instruction
*out
= ir
->outputs
[i
];
2314 out
->regs
[0]->flags
|= IR3_REG_HALF
;
2315 /* output could be a fanout (ie. texture fetch output)
2316 * in which case we need to propagate the half-reg flag
2317 * up to the definer so that RA sees it:
2319 if (is_meta(out
) && (out
->opc
== OPC_META_FO
)) {
2320 out
= out
->regs
[1]->instr
;
2321 out
->regs
[0]->flags
|= IR3_REG_HALF
;
2324 if (out
->category
== 1) {
2325 out
->cat1
.dst_type
= half_type(out
->cat1
.dst_type
);
2330 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2331 printf("BEFORE CP:\n");
2337 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2338 printf("BEFORE GROUPING:\n");
2342 /* Group left/right neighbors, inserting mov's where needed to
2349 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2350 printf("AFTER DEPTH:\n");
2354 ret
= ir3_sched(ir
);
2356 DBG("SCHED failed!");
2360 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2361 printf("AFTER SCHED:\n");
2365 ret
= ir3_ra(ir
, so
->type
, so
->frag_coord
, so
->frag_face
);
2371 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2372 printf("AFTER RA:\n");
2376 /* fixup input/outputs: */
2377 for (i
= 0; i
< so
->outputs_count
; i
++) {
2378 so
->outputs
[i
].regid
= ir
->outputs
[i
*4]->regs
[0]->num
;
2379 /* preserve hack for depth output.. tgsi writes depth to .z,
2380 * but what we give the hw is the scalar register:
2382 if ((so
->type
== SHADER_FRAGMENT
) &&
2383 (so
->outputs
[i
].slot
== FRAG_RESULT_DEPTH
))
2384 so
->outputs
[i
].regid
+= 2;
2387 /* Note that some or all channels of an input may be unused: */
2390 for (i
= 0; i
< so
->inputs_count
; i
++) {
2391 unsigned j
, regid
= ~0, compmask
= 0;
2392 so
->inputs
[i
].ncomp
= 0;
2393 so
->inputs
[i
].inloc
= inloc
+ 8;
2394 for (j
= 0; j
< 4; j
++) {
2395 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
2396 if (in
&& !(in
->flags
& IR3_INSTR_UNUSED
)) {
2397 compmask
|= (1 << j
);
2398 regid
= in
->regs
[0]->num
- j
;
2400 so
->inputs
[i
].ncomp
++;
2401 if ((so
->type
== SHADER_FRAGMENT
) && so
->inputs
[i
].bary
) {
2403 assert(in
->regs
[1]->flags
& IR3_REG_IMMED
);
2404 in
->regs
[1]->iim_val
= inloc
++;
2408 if ((so
->type
== SHADER_FRAGMENT
) && compmask
&& so
->inputs
[i
].bary
)
2410 so
->inputs
[i
].regid
= regid
;
2411 so
->inputs
[i
].compmask
= compmask
;
2414 /* We need to do legalize after (for frag shader's) the "bary.f"
2415 * offsets (inloc) have been assigned.
2417 ir3_legalize(ir
, &so
->has_samp
, &max_bary
);
2419 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
2420 printf("AFTER LEGALIZE:\n");
2424 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
2425 if (so
->type
== SHADER_VERTEX
)
2426 so
->total_in
= actual_in
;
2428 so
->total_in
= max_bary
+ 1;
2433 ir3_destroy(so
->ir
);