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, varyings are not actual shader inputs,
60 * instead the hw passes a varying-coord which is used with
63 * But NIR doesn't know that, it still declares varyings as
64 * inputs. So we do all the input tracking normally and fix
65 * things up after compile_instructions()
67 * NOTE that frag_vcoord is the hardware position (possibly it
68 * is actually an index or tag or some such.. it is *not*
69 * values that can be directly used for gl_FragCoord..)
71 struct ir3_instruction
*frag_vcoord
;
73 /* for fragment shaders, for gl_FrontFacing and gl_FragCoord: */
74 struct ir3_instruction
*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 /* For fragment shaders: */
80 struct ir3_instruction
*samp_id
, *samp_mask_in
;
82 /* Compute shader inputs: */
83 struct ir3_instruction
*local_invocation_id
, *work_group_id
;
85 /* mapping from nir_register to defining instruction: */
86 struct hash_table
*def_ht
;
90 /* a common pattern for indirect addressing is to request the
91 * same address register multiple times. To avoid generating
92 * duplicate instruction sequences (which our backend does not
93 * try to clean up, since that should be done as the NIR stage)
94 * we cache the address value generated for a given src value:
96 * Note that we have to cache these per alignment, since same
97 * src used for an array of vec1 cannot be also used for an
100 struct hash_table
*addr_ht
[4];
102 /* last dst array, for indirect we need to insert a var-store.
104 struct ir3_instruction
**last_dst
;
107 /* maps nir_block to ir3_block, mostly for the purposes of
108 * figuring out the blocks successors
110 struct hash_table
*block_ht
;
112 /* on a4xx, bitmask of samplers which need astc+srgb workaround: */
115 unsigned samples
; /* bitmask of x,y sample shifts */
117 unsigned max_texture_index
;
119 /* set if we encounter something we can't handle yet, so we
120 * can bail cleanly and fallback to TGSI compiler f/e
125 /* gpu pointer size in units of 32bit registers/slots */
126 static unsigned pointer_size(struct ir3_context
*ctx
)
128 return (ctx
->compiler
->gpu_id
>= 500) ? 2 : 1;
131 static struct ir3_instruction
* create_immed(struct ir3_block
*block
, uint32_t val
);
132 static struct ir3_block
* get_block(struct ir3_context
*ctx
, const nir_block
*nblock
);
135 static struct ir3_context
*
136 compile_init(struct ir3_compiler
*compiler
,
137 struct ir3_shader_variant
*so
)
139 struct ir3_context
*ctx
= rzalloc(NULL
, struct ir3_context
);
141 if (compiler
->gpu_id
>= 400) {
142 if (so
->type
== SHADER_VERTEX
) {
143 ctx
->astc_srgb
= so
->key
.vastc_srgb
;
144 } else if (so
->type
== SHADER_FRAGMENT
) {
145 ctx
->astc_srgb
= so
->key
.fastc_srgb
;
149 if (so
->type
== SHADER_VERTEX
) {
150 ctx
->samples
= so
->key
.vsamples
;
151 } else if (so
->type
== SHADER_FRAGMENT
) {
152 ctx
->samples
= so
->key
.fsamples
;
156 ctx
->compiler
= compiler
;
158 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
159 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
160 ctx
->block_ht
= _mesa_hash_table_create(ctx
,
161 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
163 /* TODO: maybe generate some sort of bitmask of what key
164 * lowers vs what shader has (ie. no need to lower
165 * texture clamp lowering if no texture sample instrs)..
166 * although should be done further up the stack to avoid
167 * creating duplicate variants..
170 if (ir3_key_lowers_nir(&so
->key
)) {
171 nir_shader
*s
= nir_shader_clone(ctx
, so
->shader
->nir
);
172 ctx
->s
= ir3_optimize_nir(so
->shader
, s
, &so
->key
);
174 /* fast-path for shader key that lowers nothing in NIR: */
175 ctx
->s
= so
->shader
->nir
;
178 /* this needs to be the last pass run, so do this here instead of
179 * in ir3_optimize_nir():
181 NIR_PASS_V(ctx
->s
, nir_lower_locals_to_regs
);
182 NIR_PASS_V(ctx
->s
, nir_convert_from_ssa
, true);
184 if (fd_mesa_debug
& FD_DBG_DISASM
) {
185 DBG("dump nir%dv%d: type=%d, k={bp=%u,cts=%u,hp=%u}",
186 so
->shader
->id
, so
->id
, so
->type
,
187 so
->key
.binning_pass
, so
->key
.color_two_side
,
188 so
->key
.half_precision
);
189 nir_print_shader(ctx
->s
, stdout
);
192 if (shader_debug_enabled(so
->type
)) {
193 fprintf(stderr
, "NIR (final form) for %s shader:\n",
194 shader_stage_name(so
->type
));
195 nir_print_shader(ctx
->s
, stderr
);
198 ir3_nir_scan_driver_consts(ctx
->s
, &so
->const_layout
);
200 so
->num_uniforms
= ctx
->s
->num_uniforms
;
201 so
->num_ubos
= ctx
->s
->info
.num_ubos
;
203 /* Layout of constant registers, each section aligned to vec4. Note
204 * that pointer size (ubo, etc) changes depending on generation.
209 * if (vertex shader) {
210 * driver params (IR3_DP_*)
211 * if (stream_output.num_outputs > 0)
212 * stream-out addresses
216 * Immediates go last mostly because they are inserted in the CP pass
217 * after the nir -> ir3 frontend.
219 unsigned constoff
= align(ctx
->s
->num_uniforms
, 4);
220 unsigned ptrsz
= pointer_size(ctx
);
222 memset(&so
->constbase
, ~0, sizeof(so
->constbase
));
224 if (so
->num_ubos
> 0) {
225 so
->constbase
.ubo
= constoff
;
226 constoff
+= align(ctx
->s
->info
.num_ubos
* ptrsz
, 4) / 4;
229 if (so
->const_layout
.ssbo_size
.count
> 0) {
230 unsigned cnt
= so
->const_layout
.ssbo_size
.count
;
231 so
->constbase
.ssbo_sizes
= constoff
;
232 constoff
+= align(cnt
, 4) / 4;
235 if (so
->const_layout
.image_dims
.count
> 0) {
236 unsigned cnt
= so
->const_layout
.image_dims
.count
;
237 so
->constbase
.image_dims
= constoff
;
238 constoff
+= align(cnt
, 4) / 4;
241 unsigned num_driver_params
= 0;
242 if (so
->type
== SHADER_VERTEX
) {
243 num_driver_params
= IR3_DP_VS_COUNT
;
244 } else if (so
->type
== SHADER_COMPUTE
) {
245 num_driver_params
= IR3_DP_CS_COUNT
;
248 so
->constbase
.driver_param
= constoff
;
249 constoff
+= align(num_driver_params
, 4) / 4;
251 if ((so
->type
== SHADER_VERTEX
) &&
252 (compiler
->gpu_id
< 500) &&
253 so
->shader
->stream_output
.num_outputs
> 0) {
254 so
->constbase
.tfbo
= constoff
;
255 constoff
+= align(PIPE_MAX_SO_BUFFERS
* ptrsz
, 4) / 4;
258 so
->constbase
.immediate
= constoff
;
264 compile_error(struct ir3_context
*ctx
, const char *format
, ...)
267 va_start(ap
, format
);
268 _debug_vprintf(format
, ap
);
270 nir_print_shader(ctx
->s
, stdout
);
275 #define compile_assert(ctx, cond) do { \
276 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
280 compile_free(struct ir3_context
*ctx
)
286 declare_array(struct ir3_context
*ctx
, nir_register
*reg
)
288 struct ir3_array
*arr
= rzalloc(ctx
, struct ir3_array
);
289 arr
->id
= ++ctx
->num_arrays
;
290 /* NOTE: sometimes we get non array regs, for example for arrays of
291 * length 1. See fs-const-array-of-struct-of-array.shader_test. So
292 * treat a non-array as if it was an array of length 1.
294 * It would be nice if there was a nir pass to convert arrays of
297 arr
->length
= reg
->num_components
* MAX2(1, reg
->num_array_elems
);
298 compile_assert(ctx
, arr
->length
> 0);
300 list_addtail(&arr
->node
, &ctx
->ir
->array_list
);
303 static struct ir3_array
*
304 get_array(struct ir3_context
*ctx
, nir_register
*reg
)
306 list_for_each_entry (struct ir3_array
, arr
, &ctx
->ir
->array_list
, node
) {
310 compile_error(ctx
, "bogus reg: %s\n", reg
->name
);
314 /* relative (indirect) if address!=NULL */
315 static struct ir3_instruction
*
316 create_array_load(struct ir3_context
*ctx
, struct ir3_array
*arr
, int n
,
317 struct ir3_instruction
*address
)
319 struct ir3_block
*block
= ctx
->block
;
320 struct ir3_instruction
*mov
;
321 struct ir3_register
*src
;
323 mov
= ir3_instr_create(block
, OPC_MOV
);
324 mov
->cat1
.src_type
= TYPE_U32
;
325 mov
->cat1
.dst_type
= TYPE_U32
;
326 mov
->barrier_class
= IR3_BARRIER_ARRAY_R
;
327 mov
->barrier_conflict
= IR3_BARRIER_ARRAY_W
;
328 ir3_reg_create(mov
, 0, 0);
329 src
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
330 COND(address
, IR3_REG_RELATIV
));
331 src
->instr
= arr
->last_write
;
332 src
->size
= arr
->length
;
333 src
->array
.id
= arr
->id
;
334 src
->array
.offset
= n
;
337 ir3_instr_set_address(mov
, address
);
342 /* relative (indirect) if address!=NULL */
344 create_array_store(struct ir3_context
*ctx
, struct ir3_array
*arr
, int n
,
345 struct ir3_instruction
*src
, struct ir3_instruction
*address
)
347 struct ir3_block
*block
= ctx
->block
;
348 struct ir3_instruction
*mov
;
349 struct ir3_register
*dst
;
351 /* if not relative store, don't create an extra mov, since that
352 * ends up being difficult for cp to remove.
357 src
->barrier_class
|= IR3_BARRIER_ARRAY_W
;
358 src
->barrier_conflict
|= IR3_BARRIER_ARRAY_R
| IR3_BARRIER_ARRAY_W
;
360 dst
->flags
|= IR3_REG_ARRAY
;
361 dst
->instr
= arr
->last_write
;
362 dst
->size
= arr
->length
;
363 dst
->array
.id
= arr
->id
;
364 dst
->array
.offset
= n
;
366 arr
->last_write
= src
;
368 array_insert(block
, block
->keeps
, src
);
373 mov
= ir3_instr_create(block
, OPC_MOV
);
374 mov
->cat1
.src_type
= TYPE_U32
;
375 mov
->cat1
.dst_type
= TYPE_U32
;
376 mov
->barrier_class
= IR3_BARRIER_ARRAY_W
;
377 mov
->barrier_conflict
= IR3_BARRIER_ARRAY_R
| IR3_BARRIER_ARRAY_W
;
378 dst
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
379 COND(address
, IR3_REG_RELATIV
));
380 dst
->instr
= arr
->last_write
;
381 dst
->size
= arr
->length
;
382 dst
->array
.id
= arr
->id
;
383 dst
->array
.offset
= n
;
384 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
387 ir3_instr_set_address(mov
, address
);
389 arr
->last_write
= mov
;
391 /* the array store may only matter to something in an earlier
392 * block (ie. loops), but since arrays are not in SSA, depth
393 * pass won't know this.. so keep all array stores:
395 array_insert(block
, block
->keeps
, mov
);
398 static inline type_t
utype_for_size(unsigned bit_size
)
401 case 32: return TYPE_U32
;
402 case 16: return TYPE_U16
;
403 case 8: return TYPE_U8
;
404 default: unreachable("bad bitsize"); return ~0;
408 static inline type_t
utype_src(nir_src src
)
409 { return utype_for_size(nir_src_bit_size(src
)); }
411 static inline type_t
utype_dst(nir_dest dst
)
412 { return utype_for_size(nir_dest_bit_size(dst
)); }
414 /* allocate a n element value array (to be populated by caller) and
417 static struct ir3_instruction
**
418 get_dst_ssa(struct ir3_context
*ctx
, nir_ssa_def
*dst
, unsigned n
)
420 struct ir3_instruction
**value
=
421 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
422 _mesa_hash_table_insert(ctx
->def_ht
, dst
, value
);
426 static struct ir3_instruction
**
427 get_dst(struct ir3_context
*ctx
, nir_dest
*dst
, unsigned n
)
429 struct ir3_instruction
**value
;
432 value
= get_dst_ssa(ctx
, &dst
->ssa
, n
);
434 value
= ralloc_array(ctx
, struct ir3_instruction
*, n
);
437 /* NOTE: in non-ssa case, we don't really need to store last_dst
438 * but this helps us catch cases where put_dst() call is forgotten
440 compile_assert(ctx
, !ctx
->last_dst
);
441 ctx
->last_dst
= value
;
447 static struct ir3_instruction
* get_addr(struct ir3_context
*ctx
, struct ir3_instruction
*src
, int align
);
449 static struct ir3_instruction
* const *
450 get_src(struct ir3_context
*ctx
, nir_src
*src
)
453 struct hash_entry
*entry
;
454 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
455 compile_assert(ctx
, entry
);
458 nir_register
*reg
= src
->reg
.reg
;
459 struct ir3_array
*arr
= get_array(ctx
, reg
);
460 unsigned num_components
= arr
->r
->num_components
;
461 struct ir3_instruction
*addr
= NULL
;
462 struct ir3_instruction
**value
=
463 ralloc_array(ctx
, struct ir3_instruction
*, num_components
);
465 if (src
->reg
.indirect
)
466 addr
= get_addr(ctx
, get_src(ctx
, src
->reg
.indirect
)[0],
467 reg
->num_components
);
469 for (unsigned i
= 0; i
< num_components
; i
++) {
470 unsigned n
= src
->reg
.base_offset
* reg
->num_components
+ i
;
471 compile_assert(ctx
, n
< arr
->length
);
472 value
[i
] = create_array_load(ctx
, arr
, n
, addr
);
480 put_dst(struct ir3_context
*ctx
, nir_dest
*dst
)
482 unsigned bit_size
= nir_dest_bit_size(*dst
);
485 for (unsigned i
= 0; i
< ctx
->last_dst_n
; i
++) {
486 struct ir3_instruction
*dst
= ctx
->last_dst
[i
];
487 dst
->regs
[0]->flags
|= IR3_REG_HALF
;
488 if (ctx
->last_dst
[i
]->opc
== OPC_META_FO
)
489 dst
->regs
[1]->instr
->regs
[0]->flags
|= IR3_REG_HALF
;
494 nir_register
*reg
= dst
->reg
.reg
;
495 struct ir3_array
*arr
= get_array(ctx
, reg
);
496 unsigned num_components
= ctx
->last_dst_n
;
497 struct ir3_instruction
*addr
= NULL
;
499 if (dst
->reg
.indirect
)
500 addr
= get_addr(ctx
, get_src(ctx
, dst
->reg
.indirect
)[0],
501 reg
->num_components
);
503 for (unsigned i
= 0; i
< num_components
; i
++) {
504 unsigned n
= dst
->reg
.base_offset
* reg
->num_components
+ i
;
505 compile_assert(ctx
, n
< arr
->length
);
506 if (!ctx
->last_dst
[i
])
508 create_array_store(ctx
, arr
, n
, ctx
->last_dst
[i
], addr
);
511 ralloc_free(ctx
->last_dst
);
513 ctx
->last_dst
= NULL
;
517 static struct ir3_instruction
*
518 create_immed_typed(struct ir3_block
*block
, uint32_t val
, type_t type
)
520 struct ir3_instruction
*mov
;
521 unsigned flags
= (type_size(type
) < 32) ? IR3_REG_HALF
: 0;
523 mov
= ir3_instr_create(block
, OPC_MOV
);
524 mov
->cat1
.src_type
= type
;
525 mov
->cat1
.dst_type
= type
;
526 ir3_reg_create(mov
, 0, flags
);
527 ir3_reg_create(mov
, 0, IR3_REG_IMMED
)->uim_val
= val
;
532 static struct ir3_instruction
*
533 create_immed(struct ir3_block
*block
, uint32_t val
)
535 return create_immed_typed(block
, val
, TYPE_U32
);
538 static struct ir3_instruction
*
539 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
, int align
)
541 struct ir3_instruction
*instr
, *immed
;
543 /* TODO in at least some cases, the backend could probably be
544 * made clever enough to propagate IR3_REG_HALF..
546 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
547 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
554 /* src *= 2 => src <<= 1: */
555 immed
= create_immed(block
, 1);
556 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
558 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
559 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
560 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
564 immed
= create_immed(block
, 3);
565 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
567 instr
= ir3_MULL_U(block
, instr
, 0, immed
, 0);
568 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
569 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
572 /* src *= 4 => src <<= 2: */
573 immed
= create_immed(block
, 2);
574 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
576 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
577 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
578 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
581 unreachable("bad align");
585 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
586 instr
->regs
[0]->num
= regid(REG_A0
, 0);
587 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
588 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
593 /* caches addr values to avoid generating multiple cov/shl/mova
594 * sequences for each use of a given NIR level src as address
596 static struct ir3_instruction
*
597 get_addr(struct ir3_context
*ctx
, struct ir3_instruction
*src
, int align
)
599 struct ir3_instruction
*addr
;
600 unsigned idx
= align
- 1;
602 compile_assert(ctx
, idx
< ARRAY_SIZE(ctx
->addr_ht
));
604 if (!ctx
->addr_ht
[idx
]) {
605 ctx
->addr_ht
[idx
] = _mesa_hash_table_create(ctx
,
606 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
608 struct hash_entry
*entry
;
609 entry
= _mesa_hash_table_search(ctx
->addr_ht
[idx
], src
);
614 addr
= create_addr(ctx
->block
, src
, align
);
615 _mesa_hash_table_insert(ctx
->addr_ht
[idx
], src
, addr
);
620 static struct ir3_instruction
*
621 get_predicate(struct ir3_context
*ctx
, struct ir3_instruction
*src
)
623 struct ir3_block
*b
= ctx
->block
;
624 struct ir3_instruction
*cond
;
626 /* NOTE: only cmps.*.* can write p0.x: */
627 cond
= ir3_CMPS_S(b
, src
, 0, create_immed(b
, 0), 0);
628 cond
->cat2
.condition
= IR3_COND_NE
;
630 /* condition always goes in predicate register: */
631 cond
->regs
[0]->num
= regid(REG_P0
, 0);
636 static struct ir3_instruction
*
637 create_uniform(struct ir3_context
*ctx
, unsigned n
)
639 struct ir3_instruction
*mov
;
641 mov
= ir3_instr_create(ctx
->block
, OPC_MOV
);
642 /* TODO get types right? */
643 mov
->cat1
.src_type
= TYPE_F32
;
644 mov
->cat1
.dst_type
= TYPE_F32
;
645 ir3_reg_create(mov
, 0, 0);
646 ir3_reg_create(mov
, n
, IR3_REG_CONST
);
651 static struct ir3_instruction
*
652 create_uniform_indirect(struct ir3_context
*ctx
, int n
,
653 struct ir3_instruction
*address
)
655 struct ir3_instruction
*mov
;
657 mov
= ir3_instr_create(ctx
->block
, OPC_MOV
);
658 mov
->cat1
.src_type
= TYPE_U32
;
659 mov
->cat1
.dst_type
= TYPE_U32
;
660 ir3_reg_create(mov
, 0, 0);
661 ir3_reg_create(mov
, 0, IR3_REG_CONST
| IR3_REG_RELATIV
)->array
.offset
= n
;
663 ir3_instr_set_address(mov
, address
);
668 static struct ir3_instruction
*
669 create_collect(struct ir3_context
*ctx
, struct ir3_instruction
*const *arr
,
672 struct ir3_block
*block
= ctx
->block
;
673 struct ir3_instruction
*collect
;
678 unsigned flags
= arr
[0]->regs
[0]->flags
& IR3_REG_HALF
;
680 collect
= ir3_instr_create2(block
, OPC_META_FI
, 1 + arrsz
);
681 ir3_reg_create(collect
, 0, flags
); /* dst */
682 for (unsigned i
= 0; i
< arrsz
; i
++) {
683 struct ir3_instruction
*elem
= arr
[i
];
685 /* Since arrays are pre-colored in RA, we can't assume that
686 * things will end up in the right place. (Ie. if a collect
687 * joins elements from two different arrays.) So insert an
690 * We could possibly skip this if all the collected elements
691 * are contiguous elements in a single array.. not sure how
692 * likely that is to happen.
694 * Fixes a problem with glamor shaders, that in effect do
701 * color = texture2D(tex, texcoord);
703 * In this case, texcoord will end up as nir registers (which
704 * translate to ir3 array's of length 1. And we can't assume
705 * the two (or more) arrays will get allocated in consecutive
709 if (elem
->regs
[0]->flags
& IR3_REG_ARRAY
) {
710 type_t type
= (flags
& IR3_REG_HALF
) ? TYPE_U16
: TYPE_U32
;
711 elem
= ir3_MOV(block
, elem
, type
);
714 compile_assert(ctx
, (elem
->regs
[0]->flags
& IR3_REG_HALF
) == flags
);
715 ir3_reg_create(collect
, 0, IR3_REG_SSA
| flags
)->instr
= elem
;
721 static struct ir3_instruction
*
722 create_indirect_load(struct ir3_context
*ctx
, unsigned arrsz
, int n
,
723 struct ir3_instruction
*address
, struct ir3_instruction
*collect
)
725 struct ir3_block
*block
= ctx
->block
;
726 struct ir3_instruction
*mov
;
727 struct ir3_register
*src
;
729 mov
= ir3_instr_create(block
, OPC_MOV
);
730 mov
->cat1
.src_type
= TYPE_U32
;
731 mov
->cat1
.dst_type
= TYPE_U32
;
732 ir3_reg_create(mov
, 0, 0);
733 src
= ir3_reg_create(mov
, 0, IR3_REG_SSA
| IR3_REG_RELATIV
);
734 src
->instr
= collect
;
736 src
->array
.offset
= n
;
738 ir3_instr_set_address(mov
, address
);
743 static struct ir3_instruction
*
744 create_input_compmask(struct ir3_context
*ctx
, unsigned n
, unsigned compmask
)
746 struct ir3_instruction
*in
;
748 in
= ir3_instr_create(ctx
->in_block
, OPC_META_INPUT
);
749 in
->inout
.block
= ctx
->in_block
;
750 ir3_reg_create(in
, n
, 0);
752 in
->regs
[0]->wrmask
= compmask
;
757 static struct ir3_instruction
*
758 create_input(struct ir3_context
*ctx
, unsigned n
)
760 return create_input_compmask(ctx
, n
, 0x1);
763 static struct ir3_instruction
*
764 create_frag_input(struct ir3_context
*ctx
, bool use_ldlv
)
766 struct ir3_block
*block
= ctx
->block
;
767 struct ir3_instruction
*instr
;
768 /* actual inloc is assigned and fixed up later: */
769 struct ir3_instruction
*inloc
= create_immed(block
, 0);
772 instr
= ir3_LDLV(block
, inloc
, 0, create_immed(block
, 1), 0);
773 instr
->cat6
.type
= TYPE_U32
;
774 instr
->cat6
.iim_val
= 1;
776 instr
= ir3_BARY_F(block
, inloc
, 0, ctx
->frag_vcoord
, 0);
777 instr
->regs
[2]->wrmask
= 0x3;
783 static struct ir3_instruction
*
784 create_frag_coord(struct ir3_context
*ctx
, unsigned comp
)
786 struct ir3_block
*block
= ctx
->block
;
787 struct ir3_instruction
*instr
;
789 compile_assert(ctx
, !ctx
->frag_coord
[comp
]);
791 ctx
->frag_coord
[comp
] = create_input(ctx
, 0);
796 /* for frag_coord, we get unsigned values.. we need
797 * to subtract (integer) 8 and divide by 16 (right-
798 * shift by 4) then convert to float:
802 * mov.u32f32 dst, tmp
805 instr
= ir3_SUB_S(block
, ctx
->frag_coord
[comp
], 0,
806 create_immed(block
, 8), 0);
807 instr
= ir3_SHR_B(block
, instr
, 0,
808 create_immed(block
, 4), 0);
809 instr
= ir3_COV(block
, instr
, TYPE_U32
, TYPE_F32
);
815 /* seems that we can use these as-is: */
816 return ctx
->frag_coord
[comp
];
820 static struct ir3_instruction
*
821 create_driver_param(struct ir3_context
*ctx
, enum ir3_driver_param dp
)
823 /* first four vec4 sysval's reserved for UBOs: */
824 /* NOTE: dp is in scalar, but there can be >4 dp components: */
825 unsigned n
= ctx
->so
->constbase
.driver_param
;
826 unsigned r
= regid(n
+ dp
/ 4, dp
% 4);
827 return create_uniform(ctx
, r
);
830 /* helper for instructions that produce multiple consecutive scalar
831 * outputs which need to have a split/fanout meta instruction inserted
834 split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
835 struct ir3_instruction
*src
, unsigned base
, unsigned n
)
837 struct ir3_instruction
*prev
= NULL
;
839 if ((n
== 1) && (src
->regs
[0]->wrmask
== 0x1)) {
844 for (int i
= 0, j
= 0; i
< n
; i
++) {
845 struct ir3_instruction
*split
= ir3_instr_create(block
, OPC_META_FO
);
846 ir3_reg_create(split
, 0, IR3_REG_SSA
);
847 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
848 split
->fo
.off
= i
+ base
;
851 split
->cp
.left
= prev
;
852 split
->cp
.left_cnt
++;
853 prev
->cp
.right
= split
;
854 prev
->cp
.right_cnt
++;
858 if (src
->regs
[0]->wrmask
& (1 << (i
+ base
)))
864 * Adreno uses uint rather than having dedicated bool type,
865 * which (potentially) requires some conversion, in particular
866 * when using output of an bool instr to int input, or visa
870 * -------+---------+-------+-
874 * To convert from an adreno bool (uint) to nir, use:
876 * absneg.s dst, (neg)src
878 * To convert back in the other direction:
880 * absneg.s dst, (abs)arc
882 * The CP step can clean up the absneg.s that cancel each other
883 * out, and with a slight bit of extra cleverness (to recognize
884 * the instructions which produce either a 0 or 1) can eliminate
885 * the absneg.s's completely when an instruction that wants
886 * 0/1 consumes the result. For example, when a nir 'bcsel'
887 * consumes the result of 'feq'. So we should be able to get by
888 * without a boolean resolve step, and without incuring any
889 * extra penalty in instruction count.
892 /* NIR bool -> native (adreno): */
893 static struct ir3_instruction
*
894 ir3_b2n(struct ir3_block
*block
, struct ir3_instruction
*instr
)
896 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SABS
);
899 /* native (adreno) -> NIR bool: */
900 static struct ir3_instruction
*
901 ir3_n2b(struct ir3_block
*block
, struct ir3_instruction
*instr
)
903 return ir3_ABSNEG_S(block
, instr
, IR3_REG_SNEG
);
907 * alu/sfu instructions:
910 static struct ir3_instruction
*
911 create_cov(struct ir3_context
*ctx
, struct ir3_instruction
*src
,
912 unsigned src_bitsize
, nir_op op
)
914 type_t src_type
, dst_type
;
918 case nir_op_f2f16_rtne
:
919 case nir_op_f2f16_rtz
:
927 switch (src_bitsize
) {
935 compile_error(ctx
, "invalid src bit size: %u", src_bitsize
);
944 switch (src_bitsize
) {
955 compile_error(ctx
, "invalid src bit size: %u", src_bitsize
);
964 switch (src_bitsize
) {
975 compile_error(ctx
, "invalid src bit size: %u", src_bitsize
);
980 compile_error(ctx
, "invalid conversion op: %u", op
);
990 case nir_op_f2f16_rtne
:
991 case nir_op_f2f16_rtz
:
993 /* TODO how to handle rounding mode? */
1001 dst_type
= TYPE_S32
;
1006 dst_type
= TYPE_S16
;
1016 dst_type
= TYPE_U32
;
1021 dst_type
= TYPE_U16
;
1030 compile_error(ctx
, "invalid conversion op: %u", op
);
1033 return ir3_COV(ctx
->block
, src
, src_type
, dst_type
);
1037 emit_alu(struct ir3_context
*ctx
, nir_alu_instr
*alu
)
1039 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
1040 struct ir3_instruction
**dst
, *src
[info
->num_inputs
];
1041 unsigned bs
[info
->num_inputs
]; /* bit size */
1042 struct ir3_block
*b
= ctx
->block
;
1043 unsigned dst_sz
, wrmask
;
1045 if (alu
->dest
.dest
.is_ssa
) {
1046 dst_sz
= alu
->dest
.dest
.ssa
.num_components
;
1047 wrmask
= (1 << dst_sz
) - 1;
1049 dst_sz
= alu
->dest
.dest
.reg
.reg
->num_components
;
1050 wrmask
= alu
->dest
.write_mask
;
1053 dst
= get_dst(ctx
, &alu
->dest
.dest
, dst_sz
);
1055 /* Vectors are special in that they have non-scalarized writemasks,
1056 * and just take the first swizzle channel for each argument in
1057 * order into each writemask channel.
1059 if ((alu
->op
== nir_op_vec2
) ||
1060 (alu
->op
== nir_op_vec3
) ||
1061 (alu
->op
== nir_op_vec4
)) {
1063 for (int i
= 0; i
< info
->num_inputs
; i
++) {
1064 nir_alu_src
*asrc
= &alu
->src
[i
];
1066 compile_assert(ctx
, !asrc
->abs
);
1067 compile_assert(ctx
, !asrc
->negate
);
1069 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[0]];
1071 src
[i
] = create_immed(ctx
->block
, 0);
1072 dst
[i
] = ir3_MOV(b
, src
[i
], TYPE_U32
);
1075 put_dst(ctx
, &alu
->dest
.dest
);
1079 /* We also get mov's with more than one component for mov's so
1080 * handle those specially:
1082 if ((alu
->op
== nir_op_imov
) || (alu
->op
== nir_op_fmov
)) {
1083 type_t type
= (alu
->op
== nir_op_imov
) ? TYPE_U32
: TYPE_F32
;
1084 nir_alu_src
*asrc
= &alu
->src
[0];
1085 struct ir3_instruction
*const *src0
= get_src(ctx
, &asrc
->src
);
1087 for (unsigned i
= 0; i
< dst_sz
; i
++) {
1088 if (wrmask
& (1 << i
)) {
1089 dst
[i
] = ir3_MOV(b
, src0
[asrc
->swizzle
[i
]], type
);
1095 put_dst(ctx
, &alu
->dest
.dest
);
1099 /* General case: We can just grab the one used channel per src. */
1100 for (int i
= 0; i
< info
->num_inputs
; i
++) {
1101 unsigned chan
= ffs(alu
->dest
.write_mask
) - 1;
1102 nir_alu_src
*asrc
= &alu
->src
[i
];
1104 compile_assert(ctx
, !asrc
->abs
);
1105 compile_assert(ctx
, !asrc
->negate
);
1107 src
[i
] = get_src(ctx
, &asrc
->src
)[asrc
->swizzle
[chan
]];
1108 bs
[i
] = nir_src_bit_size(asrc
->src
);
1110 compile_assert(ctx
, src
[i
]);
1115 case nir_op_f2f16_rtne
:
1116 case nir_op_f2f16_rtz
:
1134 dst
[0] = create_cov(ctx
, src
[0], bs
[0], alu
->op
);
1137 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, create_immed(b
, fui(0.0)), 0);
1138 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1139 dst
[0] = ir3_n2b(b
, dst
[0]);
1142 dst
[0] = ir3_COV(b
, ir3_b2n(b
, src
[0]), TYPE_U32
, TYPE_F32
);
1145 dst
[0] = ir3_b2n(b
, src
[0]);
1148 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
1149 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1150 dst
[0] = ir3_n2b(b
, dst
[0]);
1154 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FNEG
);
1157 dst
[0] = ir3_ABSNEG_F(b
, src
[0], IR3_REG_FABS
);
1160 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[1], 0);
1163 dst
[0] = ir3_MIN_F(b
, src
[0], 0, src
[1], 0);
1166 /* if there is just a single use of the src, and it supports
1167 * (sat) bit, we can just fold the (sat) flag back to the
1168 * src instruction and create a mov. This is easier for cp
1171 * TODO probably opc_cat==4 is ok too
1173 if (alu
->src
[0].src
.is_ssa
&&
1174 (list_length(&alu
->src
[0].src
.ssa
->uses
) == 1) &&
1175 ((opc_cat(src
[0]->opc
) == 2) || (opc_cat(src
[0]->opc
) == 3))) {
1176 src
[0]->flags
|= IR3_INSTR_SAT
;
1177 dst
[0] = ir3_MOV(b
, src
[0], TYPE_U32
);
1179 /* otherwise generate a max.f that saturates.. blob does
1180 * similar (generating a cat2 mov using max.f)
1182 dst
[0] = ir3_MAX_F(b
, src
[0], 0, src
[0], 0);
1183 dst
[0]->flags
|= IR3_INSTR_SAT
;
1187 dst
[0] = ir3_MUL_F(b
, src
[0], 0, src
[1], 0);
1190 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], 0);
1193 dst
[0] = ir3_ADD_F(b
, src
[0], 0, src
[1], IR3_REG_FNEG
);
1196 dst
[0] = ir3_MAD_F32(b
, src
[0], 0, src
[1], 0, src
[2], 0);
1199 dst
[0] = ir3_DSX(b
, src
[0], 0);
1200 dst
[0]->cat5
.type
= TYPE_F32
;
1203 dst
[0] = ir3_DSY(b
, src
[0], 0);
1204 dst
[0]->cat5
.type
= TYPE_F32
;
1208 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1209 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1210 dst
[0] = ir3_n2b(b
, dst
[0]);
1213 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1214 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1215 dst
[0] = ir3_n2b(b
, dst
[0]);
1218 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1219 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
1220 dst
[0] = ir3_n2b(b
, dst
[0]);
1223 dst
[0] = ir3_CMPS_F(b
, src
[0], 0, src
[1], 0);
1224 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1225 dst
[0] = ir3_n2b(b
, dst
[0]);
1228 dst
[0] = ir3_CEIL_F(b
, src
[0], 0);
1231 dst
[0] = ir3_FLOOR_F(b
, src
[0], 0);
1234 dst
[0] = ir3_TRUNC_F(b
, src
[0], 0);
1236 case nir_op_fround_even
:
1237 dst
[0] = ir3_RNDNE_F(b
, src
[0], 0);
1240 dst
[0] = ir3_SIGN_F(b
, src
[0], 0);
1244 dst
[0] = ir3_SIN(b
, src
[0], 0);
1247 dst
[0] = ir3_COS(b
, src
[0], 0);
1250 dst
[0] = ir3_RSQ(b
, src
[0], 0);
1253 dst
[0] = ir3_RCP(b
, src
[0], 0);
1256 dst
[0] = ir3_LOG2(b
, src
[0], 0);
1259 dst
[0] = ir3_EXP2(b
, src
[0], 0);
1262 dst
[0] = ir3_SQRT(b
, src
[0], 0);
1266 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SABS
);
1269 dst
[0] = ir3_ADD_U(b
, src
[0], 0, src
[1], 0);
1272 dst
[0] = ir3_AND_B(b
, src
[0], 0, src
[1], 0);
1275 dst
[0] = ir3_MAX_S(b
, src
[0], 0, src
[1], 0);
1278 dst
[0] = ir3_MAX_U(b
, src
[0], 0, src
[1], 0);
1281 dst
[0] = ir3_MIN_S(b
, src
[0], 0, src
[1], 0);
1284 dst
[0] = ir3_MIN_U(b
, src
[0], 0, src
[1], 0);
1288 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
1289 * mull.u tmp0, a, b ; mul low, i.e. al * bl
1290 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
1291 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
1293 dst
[0] = ir3_MADSH_M16(b
, src
[1], 0, src
[0], 0,
1294 ir3_MADSH_M16(b
, src
[0], 0, src
[1], 0,
1295 ir3_MULL_U(b
, src
[0], 0, src
[1], 0), 0), 0);
1298 dst
[0] = ir3_ABSNEG_S(b
, src
[0], IR3_REG_SNEG
);
1301 dst
[0] = ir3_NOT_B(b
, src
[0], 0);
1304 dst
[0] = ir3_OR_B(b
, src
[0], 0, src
[1], 0);
1307 dst
[0] = ir3_SHL_B(b
, src
[0], 0, src
[1], 0);
1310 dst
[0] = ir3_ASHR_B(b
, src
[0], 0, src
[1], 0);
1312 case nir_op_isign
: {
1313 /* maybe this would be sane to lower in nir.. */
1314 struct ir3_instruction
*neg
, *pos
;
1316 neg
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
1317 neg
->cat2
.condition
= IR3_COND_LT
;
1319 pos
= ir3_CMPS_S(b
, src
[0], 0, create_immed(b
, 0), 0);
1320 pos
->cat2
.condition
= IR3_COND_GT
;
1322 dst
[0] = ir3_SUB_U(b
, pos
, 0, neg
, 0);
1327 dst
[0] = ir3_SUB_U(b
, src
[0], 0, src
[1], 0);
1330 dst
[0] = ir3_XOR_B(b
, src
[0], 0, src
[1], 0);
1333 dst
[0] = ir3_SHR_B(b
, src
[0], 0, src
[1], 0);
1336 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1337 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1338 dst
[0] = ir3_n2b(b
, dst
[0]);
1341 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1342 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1343 dst
[0] = ir3_n2b(b
, dst
[0]);
1346 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1347 dst
[0]->cat2
.condition
= IR3_COND_EQ
;
1348 dst
[0] = ir3_n2b(b
, dst
[0]);
1351 dst
[0] = ir3_CMPS_S(b
, src
[0], 0, src
[1], 0);
1352 dst
[0]->cat2
.condition
= IR3_COND_NE
;
1353 dst
[0] = ir3_n2b(b
, dst
[0]);
1356 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
1357 dst
[0]->cat2
.condition
= IR3_COND_LT
;
1358 dst
[0] = ir3_n2b(b
, dst
[0]);
1361 dst
[0] = ir3_CMPS_U(b
, src
[0], 0, src
[1], 0);
1362 dst
[0]->cat2
.condition
= IR3_COND_GE
;
1363 dst
[0] = ir3_n2b(b
, dst
[0]);
1366 case nir_op_bcsel
: {
1367 struct ir3_instruction
*cond
= ir3_b2n(b
, src
[0]);
1368 compile_assert(ctx
, bs
[1] == bs
[2]);
1369 /* the boolean condition is 32b even if src[1] and src[2] are
1370 * half-precision, but sel.b16 wants all three src's to be the
1374 cond
= ir3_COV(b
, cond
, TYPE_U32
, TYPE_U16
);
1375 dst
[0] = ir3_SEL_B32(b
, src
[1], 0, cond
, 0, src
[2], 0);
1378 case nir_op_bit_count
:
1379 dst
[0] = ir3_CBITS_B(b
, src
[0], 0);
1381 case nir_op_ifind_msb
: {
1382 struct ir3_instruction
*cmp
;
1383 dst
[0] = ir3_CLZ_S(b
, src
[0], 0);
1384 cmp
= ir3_CMPS_S(b
, dst
[0], 0, create_immed(b
, 0), 0);
1385 cmp
->cat2
.condition
= IR3_COND_GE
;
1386 dst
[0] = ir3_SEL_B32(b
,
1387 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
1391 case nir_op_ufind_msb
:
1392 dst
[0] = ir3_CLZ_B(b
, src
[0], 0);
1393 dst
[0] = ir3_SEL_B32(b
,
1394 ir3_SUB_U(b
, create_immed(b
, 31), 0, dst
[0], 0), 0,
1395 src
[0], 0, dst
[0], 0);
1397 case nir_op_find_lsb
:
1398 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
1399 dst
[0] = ir3_CLZ_B(b
, dst
[0], 0);
1401 case nir_op_bitfield_reverse
:
1402 dst
[0] = ir3_BFREV_B(b
, src
[0], 0);
1406 compile_error(ctx
, "Unhandled ALU op: %s\n",
1407 nir_op_infos
[alu
->op
].name
);
1411 put_dst(ctx
, &alu
->dest
.dest
);
1414 /* handles direct/indirect UBO reads: */
1416 emit_intrinsic_load_ubo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1417 struct ir3_instruction
**dst
)
1419 struct ir3_block
*b
= ctx
->block
;
1420 struct ir3_instruction
*base_lo
, *base_hi
, *addr
, *src0
, *src1
;
1421 nir_const_value
*const_offset
;
1422 /* UBO addresses are the first driver params: */
1423 unsigned ubo
= regid(ctx
->so
->constbase
.ubo
, 0);
1424 const unsigned ptrsz
= pointer_size(ctx
);
1428 /* First src is ubo index, which could either be an immed or not: */
1429 src0
= get_src(ctx
, &intr
->src
[0])[0];
1430 if (is_same_type_mov(src0
) &&
1431 (src0
->regs
[1]->flags
& IR3_REG_IMMED
)) {
1432 base_lo
= create_uniform(ctx
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
));
1433 base_hi
= create_uniform(ctx
, ubo
+ (src0
->regs
[1]->iim_val
* ptrsz
) + 1);
1435 base_lo
= create_uniform_indirect(ctx
, ubo
, get_addr(ctx
, src0
, 4));
1436 base_hi
= create_uniform_indirect(ctx
, ubo
+ 1, get_addr(ctx
, src0
, 4));
1439 /* note: on 32bit gpu's base_hi is ignored and DCE'd */
1442 const_offset
= nir_src_as_const_value(intr
->src
[1]);
1444 off
+= const_offset
->u32
[0];
1446 /* For load_ubo_indirect, second src is indirect offset: */
1447 src1
= get_src(ctx
, &intr
->src
[1])[0];
1449 /* and add offset to addr: */
1450 addr
= ir3_ADD_S(b
, addr
, 0, src1
, 0);
1453 /* if offset is to large to encode in the ldg, split it out: */
1454 if ((off
+ (intr
->num_components
* 4)) > 1024) {
1455 /* split out the minimal amount to improve the odds that
1456 * cp can fit the immediate in the add.s instruction:
1458 unsigned off2
= off
+ (intr
->num_components
* 4) - 1024;
1459 addr
= ir3_ADD_S(b
, addr
, 0, create_immed(b
, off2
), 0);
1464 struct ir3_instruction
*carry
;
1466 /* handle 32b rollover, ie:
1467 * if (addr < base_lo)
1470 carry
= ir3_CMPS_U(b
, addr
, 0, base_lo
, 0);
1471 carry
->cat2
.condition
= IR3_COND_LT
;
1472 base_hi
= ir3_ADD_S(b
, base_hi
, 0, carry
, 0);
1474 addr
= create_collect(ctx
, (struct ir3_instruction
*[]){ addr
, base_hi
}, 2);
1477 for (int i
= 0; i
< intr
->num_components
; i
++) {
1478 struct ir3_instruction
*load
=
1479 ir3_LDG(b
, addr
, 0, create_immed(b
, 1), 0);
1480 load
->cat6
.type
= TYPE_U32
;
1481 load
->cat6
.src_offset
= off
+ i
* 4; /* byte offset */
1486 /* src[] = { buffer_index, offset }. No const_index */
1488 emit_intrinsic_load_ssbo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1489 struct ir3_instruction
**dst
)
1491 struct ir3_block
*b
= ctx
->block
;
1492 struct ir3_instruction
*ldgb
, *src0
, *src1
, *offset
;
1493 nir_const_value
*const_offset
;
1495 /* can this be non-const buffer_index? how do we handle that? */
1496 const_offset
= nir_src_as_const_value(intr
->src
[0]);
1497 compile_assert(ctx
, const_offset
);
1499 offset
= get_src(ctx
, &intr
->src
[1])[0];
1501 /* src0 is uvec2(offset*4, 0), src1 is offset.. nir already *= 4: */
1502 src0
= create_collect(ctx
, (struct ir3_instruction
*[]){
1506 src1
= ir3_SHR_B(b
, offset
, 0, create_immed(b
, 2), 0);
1508 ldgb
= ir3_LDGB(b
, create_immed(b
, const_offset
->u32
[0]), 0,
1510 ldgb
->regs
[0]->wrmask
= MASK(intr
->num_components
);
1511 ldgb
->cat6
.iim_val
= intr
->num_components
;
1513 ldgb
->cat6
.type
= TYPE_U32
;
1514 ldgb
->barrier_class
= IR3_BARRIER_BUFFER_R
;
1515 ldgb
->barrier_conflict
= IR3_BARRIER_BUFFER_W
;
1517 split_dest(b
, dst
, ldgb
, 0, intr
->num_components
);
1520 /* src[] = { value, block_index, offset }. const_index[] = { write_mask } */
1522 emit_intrinsic_store_ssbo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1524 struct ir3_block
*b
= ctx
->block
;
1525 struct ir3_instruction
*stgb
, *src0
, *src1
, *src2
, *offset
;
1526 nir_const_value
*const_offset
;
1527 /* TODO handle wrmask properly, see _store_shared().. but I think
1528 * it is more a PITA than that, since blob ends up loading the
1529 * masked components and writing them back out.
1531 unsigned wrmask
= intr
->const_index
[0];
1532 unsigned ncomp
= ffs(~wrmask
) - 1;
1534 /* can this be non-const buffer_index? how do we handle that? */
1535 const_offset
= nir_src_as_const_value(intr
->src
[1]);
1536 compile_assert(ctx
, const_offset
);
1538 offset
= get_src(ctx
, &intr
->src
[2])[0];
1540 /* src0 is value, src1 is offset, src2 is uvec2(offset*4, 0)..
1543 src0
= create_collect(ctx
, get_src(ctx
, &intr
->src
[0]), ncomp
);
1544 src1
= ir3_SHR_B(b
, offset
, 0, create_immed(b
, 2), 0);
1545 src2
= create_collect(ctx
, (struct ir3_instruction
*[]){
1550 stgb
= ir3_STGB(b
, create_immed(b
, const_offset
->u32
[0]), 0,
1551 src0
, 0, src1
, 0, src2
, 0);
1552 stgb
->cat6
.iim_val
= ncomp
;
1554 stgb
->cat6
.type
= TYPE_U32
;
1555 stgb
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1556 stgb
->barrier_conflict
= IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1558 array_insert(b
, b
->keeps
, stgb
);
1561 /* src[] = { block_index } */
1563 emit_intrinsic_ssbo_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1564 struct ir3_instruction
**dst
)
1566 /* SSBO size stored as a const starting at ssbo_sizes: */
1567 unsigned blk_idx
= nir_src_as_const_value(intr
->src
[0])->u32
[0];
1568 unsigned idx
= regid(ctx
->so
->constbase
.ssbo_sizes
, 0) +
1569 ctx
->so
->const_layout
.ssbo_size
.off
[blk_idx
];
1571 debug_assert(ctx
->so
->const_layout
.ssbo_size
.mask
& (1 << blk_idx
));
1573 dst
[0] = create_uniform(ctx
, idx
);
1577 * SSBO atomic intrinsics
1579 * All of the SSBO atomic memory operations read a value from memory,
1580 * compute a new value using one of the operations below, write the new
1581 * value to memory, and return the original value read.
1583 * All operations take 3 sources except CompSwap that takes 4. These
1584 * sources represent:
1586 * 0: The SSBO buffer index.
1587 * 1: The offset into the SSBO buffer of the variable that the atomic
1588 * operation will operate on.
1589 * 2: The data parameter to the atomic function (i.e. the value to add
1590 * in ssbo_atomic_add, etc).
1591 * 3: For CompSwap only: the second data parameter.
1593 static struct ir3_instruction
*
1594 emit_intrinsic_atomic_ssbo(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1596 struct ir3_block
*b
= ctx
->block
;
1597 struct ir3_instruction
*atomic
, *ssbo
, *src0
, *src1
, *src2
, *offset
;
1598 nir_const_value
*const_offset
;
1599 type_t type
= TYPE_U32
;
1601 /* can this be non-const buffer_index? how do we handle that? */
1602 const_offset
= nir_src_as_const_value(intr
->src
[0]);
1603 compile_assert(ctx
, const_offset
);
1604 ssbo
= create_immed(b
, const_offset
->u32
[0]);
1606 offset
= get_src(ctx
, &intr
->src
[1])[0];
1608 /* src0 is data (or uvec2(data, compare))
1610 * src2 is uvec2(offset*4, 0) (appears to be 64b byte offset)
1612 * Note that nir already multiplies the offset by four
1614 src0
= get_src(ctx
, &intr
->src
[2])[0];
1615 src1
= ir3_SHR_B(b
, offset
, 0, create_immed(b
, 2), 0);
1616 src2
= create_collect(ctx
, (struct ir3_instruction
*[]){
1621 switch (intr
->intrinsic
) {
1622 case nir_intrinsic_ssbo_atomic_add
:
1623 atomic
= ir3_ATOMIC_ADD_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1625 case nir_intrinsic_ssbo_atomic_imin
:
1626 atomic
= ir3_ATOMIC_MIN_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1629 case nir_intrinsic_ssbo_atomic_umin
:
1630 atomic
= ir3_ATOMIC_MIN_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1632 case nir_intrinsic_ssbo_atomic_imax
:
1633 atomic
= ir3_ATOMIC_MAX_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1636 case nir_intrinsic_ssbo_atomic_umax
:
1637 atomic
= ir3_ATOMIC_MAX_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1639 case nir_intrinsic_ssbo_atomic_and
:
1640 atomic
= ir3_ATOMIC_AND_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1642 case nir_intrinsic_ssbo_atomic_or
:
1643 atomic
= ir3_ATOMIC_OR_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1645 case nir_intrinsic_ssbo_atomic_xor
:
1646 atomic
= ir3_ATOMIC_XOR_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1648 case nir_intrinsic_ssbo_atomic_exchange
:
1649 atomic
= ir3_ATOMIC_XCHG_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1651 case nir_intrinsic_ssbo_atomic_comp_swap
:
1652 /* for cmpxchg, src0 is [ui]vec2(data, compare): */
1653 src0
= create_collect(ctx
, (struct ir3_instruction
*[]){
1655 get_src(ctx
, &intr
->src
[3])[0],
1657 atomic
= ir3_ATOMIC_CMPXCHG_G(b
, ssbo
, 0, src0
, 0, src1
, 0, src2
, 0);
1663 atomic
->cat6
.iim_val
= 1;
1665 atomic
->cat6
.type
= type
;
1666 atomic
->barrier_class
= IR3_BARRIER_BUFFER_W
;
1667 atomic
->barrier_conflict
= IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
1669 /* even if nothing consume the result, we can't DCE the instruction: */
1670 array_insert(b
, b
->keeps
, atomic
);
1675 /* src[] = { offset }. const_index[] = { base } */
1677 emit_intrinsic_load_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1678 struct ir3_instruction
**dst
)
1680 struct ir3_block
*b
= ctx
->block
;
1681 struct ir3_instruction
*ldl
, *offset
;
1684 offset
= get_src(ctx
, &intr
->src
[0])[0];
1685 base
= nir_intrinsic_base(intr
);
1687 ldl
= ir3_LDL(b
, offset
, 0, create_immed(b
, intr
->num_components
), 0);
1688 ldl
->cat6
.src_offset
= base
;
1689 ldl
->cat6
.type
= utype_dst(intr
->dest
);
1690 ldl
->regs
[0]->wrmask
= MASK(intr
->num_components
);
1692 ldl
->barrier_class
= IR3_BARRIER_SHARED_R
;
1693 ldl
->barrier_conflict
= IR3_BARRIER_SHARED_W
;
1695 split_dest(b
, dst
, ldl
, 0, intr
->num_components
);
1698 /* src[] = { value, offset }. const_index[] = { base, write_mask } */
1700 emit_intrinsic_store_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1702 struct ir3_block
*b
= ctx
->block
;
1703 struct ir3_instruction
*stl
, *offset
;
1704 struct ir3_instruction
* const *value
;
1705 unsigned base
, wrmask
;
1707 value
= get_src(ctx
, &intr
->src
[0]);
1708 offset
= get_src(ctx
, &intr
->src
[1])[0];
1710 base
= nir_intrinsic_base(intr
);
1711 wrmask
= nir_intrinsic_write_mask(intr
);
1713 /* Combine groups of consecutive enabled channels in one write
1714 * message. We use ffs to find the first enabled channel and then ffs on
1715 * the bit-inverse, down-shifted writemask to determine the length of
1716 * the block of enabled bits.
1718 * (trick stolen from i965's fs_visitor::nir_emit_cs_intrinsic())
1721 unsigned first_component
= ffs(wrmask
) - 1;
1722 unsigned length
= ffs(~(wrmask
>> first_component
)) - 1;
1724 stl
= ir3_STL(b
, offset
, 0,
1725 create_collect(ctx
, &value
[first_component
], length
), 0,
1726 create_immed(b
, length
), 0);
1727 stl
->cat6
.dst_offset
= first_component
+ base
;
1728 stl
->cat6
.type
= utype_src(intr
->src
[0]);
1729 stl
->barrier_class
= IR3_BARRIER_SHARED_W
;
1730 stl
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
1732 array_insert(b
, b
->keeps
, stl
);
1734 /* Clear the bits in the writemask that we just wrote, then try
1735 * again to see if more channels are left.
1737 wrmask
&= (15 << (first_component
+ length
));
1742 * CS shared variable atomic intrinsics
1744 * All of the shared variable atomic memory operations read a value from
1745 * memory, compute a new value using one of the operations below, write the
1746 * new value to memory, and return the original value read.
1748 * All operations take 2 sources except CompSwap that takes 3. These
1749 * sources represent:
1751 * 0: The offset into the shared variable storage region that the atomic
1752 * operation will operate on.
1753 * 1: The data parameter to the atomic function (i.e. the value to add
1754 * in shared_atomic_add, etc).
1755 * 2: For CompSwap only: the second data parameter.
1757 static struct ir3_instruction
*
1758 emit_intrinsic_atomic_shared(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
1760 struct ir3_block
*b
= ctx
->block
;
1761 struct ir3_instruction
*atomic
, *src0
, *src1
;
1762 type_t type
= TYPE_U32
;
1764 src0
= get_src(ctx
, &intr
->src
[0])[0]; /* offset */
1765 src1
= get_src(ctx
, &intr
->src
[1])[0]; /* value */
1767 switch (intr
->intrinsic
) {
1768 case nir_intrinsic_shared_atomic_add
:
1769 atomic
= ir3_ATOMIC_ADD(b
, src0
, 0, src1
, 0);
1771 case nir_intrinsic_shared_atomic_imin
:
1772 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
1775 case nir_intrinsic_shared_atomic_umin
:
1776 atomic
= ir3_ATOMIC_MIN(b
, src0
, 0, src1
, 0);
1778 case nir_intrinsic_shared_atomic_imax
:
1779 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
1782 case nir_intrinsic_shared_atomic_umax
:
1783 atomic
= ir3_ATOMIC_MAX(b
, src0
, 0, src1
, 0);
1785 case nir_intrinsic_shared_atomic_and
:
1786 atomic
= ir3_ATOMIC_AND(b
, src0
, 0, src1
, 0);
1788 case nir_intrinsic_shared_atomic_or
:
1789 atomic
= ir3_ATOMIC_OR(b
, src0
, 0, src1
, 0);
1791 case nir_intrinsic_shared_atomic_xor
:
1792 atomic
= ir3_ATOMIC_XOR(b
, src0
, 0, src1
, 0);
1794 case nir_intrinsic_shared_atomic_exchange
:
1795 atomic
= ir3_ATOMIC_XCHG(b
, src0
, 0, src1
, 0);
1797 case nir_intrinsic_shared_atomic_comp_swap
:
1798 /* for cmpxchg, src1 is [ui]vec2(data, compare): */
1799 src1
= create_collect(ctx
, (struct ir3_instruction
*[]){
1800 get_src(ctx
, &intr
->src
[2])[0],
1803 atomic
= ir3_ATOMIC_CMPXCHG(b
, src0
, 0, src1
, 0);
1809 atomic
->cat6
.iim_val
= 1;
1811 atomic
->cat6
.type
= type
;
1812 atomic
->barrier_class
= IR3_BARRIER_SHARED_W
;
1813 atomic
->barrier_conflict
= IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
;
1815 /* even if nothing consume the result, we can't DCE the instruction: */
1816 array_insert(b
, b
->keeps
, atomic
);
1821 /* Images get mapped into SSBO/image state (for store/atomic) and texture
1822 * state block (for load). To simplify things, invert the image id and
1823 * map it from end of state block, ie. image 0 becomes num-1, image 1
1824 * becomes num-2, etc. This potentially avoids needing to re-emit texture
1825 * state when switching shaders.
1827 * TODO is max # of samplers and SSBOs the same. This shouldn't be hard-
1828 * coded. Also, since all the gl shader stages (ie. everything but CS)
1829 * share the same SSBO/image state block, this might require some more
1830 * logic if we supported images in anything other than FS..
1833 get_image_slot(struct ir3_context
*ctx
, nir_deref_instr
*deref
)
1835 unsigned int loc
= 0;
1836 unsigned inner_size
= 1;
1838 while (deref
->deref_type
!= nir_deref_type_var
) {
1839 assert(deref
->deref_type
== nir_deref_type_array
);
1840 nir_const_value
*const_index
= nir_src_as_const_value(deref
->arr
.index
);
1841 assert(const_index
);
1843 /* Go to the next instruction */
1844 deref
= nir_deref_instr_parent(deref
);
1846 assert(glsl_type_is_array(deref
->type
));
1847 const unsigned array_len
= glsl_get_length(deref
->type
);
1848 loc
+= MIN2(const_index
->u32
[0], array_len
- 1) * inner_size
;
1850 /* Update the inner size */
1851 inner_size
*= array_len
;
1854 loc
+= deref
->var
->data
.driver_location
;
1856 /* TODO figure out real limit per generation, and don't hardcode: */
1857 const unsigned max_samplers
= 16;
1858 return max_samplers
- loc
- 1;
1861 /* see tex_info() for equiv logic for texture instructions.. it would be
1862 * nice if this could be better unified..
1865 get_image_coords(const nir_variable
*var
, unsigned *flagsp
)
1867 const struct glsl_type
*type
= glsl_without_array(var
->type
);
1868 unsigned coords
, flags
= 0;
1870 switch (glsl_get_sampler_dim(type
)) {
1871 case GLSL_SAMPLER_DIM_1D
:
1872 case GLSL_SAMPLER_DIM_BUF
:
1875 case GLSL_SAMPLER_DIM_2D
:
1876 case GLSL_SAMPLER_DIM_RECT
:
1877 case GLSL_SAMPLER_DIM_EXTERNAL
:
1878 case GLSL_SAMPLER_DIM_MS
:
1881 case GLSL_SAMPLER_DIM_3D
:
1882 case GLSL_SAMPLER_DIM_CUBE
:
1883 flags
|= IR3_INSTR_3D
;
1887 unreachable("bad sampler dim");
1891 if (glsl_sampler_type_is_array(type
)) {
1892 /* note: unlike tex_info(), adjust # of coords to include array idx: */
1894 flags
|= IR3_INSTR_A
;
1904 get_image_type(const nir_variable
*var
)
1906 switch (glsl_get_sampler_result_type(glsl_without_array(var
->type
))) {
1907 case GLSL_TYPE_UINT
:
1911 case GLSL_TYPE_FLOAT
:
1914 unreachable("bad sampler type.");
1919 static struct ir3_instruction
*
1920 get_image_offset(struct ir3_context
*ctx
, const nir_variable
*var
,
1921 struct ir3_instruction
* const *coords
, bool byteoff
)
1923 struct ir3_block
*b
= ctx
->block
;
1924 struct ir3_instruction
*offset
;
1925 unsigned ncoords
= get_image_coords(var
, NULL
);
1927 /* to calculate the byte offset (yes, uggg) we need (up to) three
1928 * const values to know the bytes per pixel, and y and z stride:
1930 unsigned cb
= regid(ctx
->so
->constbase
.image_dims
, 0) +
1931 ctx
->so
->const_layout
.image_dims
.off
[var
->data
.driver_location
];
1933 debug_assert(ctx
->so
->const_layout
.image_dims
.mask
&
1934 (1 << var
->data
.driver_location
));
1936 /* offset = coords.x * bytes_per_pixel: */
1937 offset
= ir3_MUL_S(b
, coords
[0], 0, create_uniform(ctx
, cb
+ 0), 0);
1939 /* offset += coords.y * y_pitch: */
1940 offset
= ir3_MAD_S24(b
, create_uniform(ctx
, cb
+ 1), 0,
1941 coords
[1], 0, offset
, 0);
1944 /* offset += coords.z * z_pitch: */
1945 offset
= ir3_MAD_S24(b
, create_uniform(ctx
, cb
+ 2), 0,
1946 coords
[2], 0, offset
, 0);
1950 /* Some cases, like atomics, seem to use dword offset instead
1951 * of byte offsets.. blob just puts an extra shr.b in there
1954 offset
= ir3_SHR_B(b
, offset
, 0, create_immed(b
, 2), 0);
1957 return create_collect(ctx
, (struct ir3_instruction
*[]){
1963 /* src[] = { deref, coord, sample_index }. const_index[] = {} */
1965 emit_intrinsic_load_image(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
1966 struct ir3_instruction
**dst
)
1968 struct ir3_block
*b
= ctx
->block
;
1969 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1970 struct ir3_instruction
*sam
;
1971 struct ir3_instruction
* const *src0
= get_src(ctx
, &intr
->src
[1]);
1972 struct ir3_instruction
*coords
[4];
1973 unsigned flags
, ncoords
= get_image_coords(var
, &flags
);
1974 unsigned tex_idx
= get_image_slot(ctx
, nir_src_as_deref(intr
->src
[0]));
1975 type_t type
= get_image_type(var
);
1977 /* hmm, this seems a bit odd, but it is what blob does and (at least
1978 * a5xx) just faults on bogus addresses otherwise:
1980 if (flags
& IR3_INSTR_3D
) {
1981 flags
&= ~IR3_INSTR_3D
;
1982 flags
|= IR3_INSTR_A
;
1985 for (unsigned i
= 0; i
< ncoords
; i
++)
1986 coords
[i
] = src0
[i
];
1989 coords
[ncoords
++] = create_immed(b
, 0);
1991 sam
= ir3_SAM(b
, OPC_ISAM
, type
, TGSI_WRITEMASK_XYZW
, flags
,
1992 tex_idx
, tex_idx
, create_collect(ctx
, coords
, ncoords
), NULL
);
1994 sam
->barrier_class
= IR3_BARRIER_IMAGE_R
;
1995 sam
->barrier_conflict
= IR3_BARRIER_IMAGE_W
;
1997 split_dest(b
, dst
, sam
, 0, 4);
2000 /* src[] = { deref, coord, sample_index, value }. const_index[] = {} */
2002 emit_intrinsic_store_image(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
2004 struct ir3_block
*b
= ctx
->block
;
2005 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
2006 struct ir3_instruction
*stib
, *offset
;
2007 struct ir3_instruction
* const *value
= get_src(ctx
, &intr
->src
[3]);
2008 struct ir3_instruction
* const *coords
= get_src(ctx
, &intr
->src
[1]);
2009 unsigned ncoords
= get_image_coords(var
, NULL
);
2010 unsigned tex_idx
= get_image_slot(ctx
, nir_src_as_deref(intr
->src
[0]));
2014 * src2 is 64b byte offset
2017 offset
= get_image_offset(ctx
, var
, coords
, true);
2019 /* NOTE: stib seems to take byte offset, but stgb.typed can be used
2020 * too and takes a dword offset.. not quite sure yet why blob uses
2021 * one over the other in various cases.
2024 stib
= ir3_STIB(b
, create_immed(b
, tex_idx
), 0,
2025 create_collect(ctx
, value
, 4), 0,
2026 create_collect(ctx
, coords
, ncoords
), 0,
2028 stib
->cat6
.iim_val
= 4;
2029 stib
->cat6
.d
= ncoords
;
2030 stib
->cat6
.type
= get_image_type(var
);
2031 stib
->cat6
.typed
= true;
2032 stib
->barrier_class
= IR3_BARRIER_IMAGE_W
;
2033 stib
->barrier_conflict
= IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
;
2035 array_insert(b
, b
->keeps
, stib
);
2039 emit_intrinsic_image_size(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
,
2040 struct ir3_instruction
**dst
)
2042 struct ir3_block
*b
= ctx
->block
;
2043 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
2044 unsigned tex_idx
= get_image_slot(ctx
, nir_src_as_deref(intr
->src
[0]));
2045 struct ir3_instruction
*sam
, *lod
;
2046 unsigned flags
, ncoords
= get_image_coords(var
, &flags
);
2048 lod
= create_immed(b
, 0);
2049 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
2050 tex_idx
, tex_idx
, lod
, NULL
);
2052 /* Array size actually ends up in .w rather than .z. This doesn't
2053 * matter for miplevel 0, but for higher mips the value in z is
2054 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
2055 * returned, which means that we have to add 1 to it for arrays for
2058 * Note use a temporary dst and then copy, since the size of the dst
2059 * array that is passed in is based on nir's understanding of the
2060 * result size, not the hardware's
2062 struct ir3_instruction
*tmp
[4];
2064 split_dest(b
, tmp
, sam
, 0, 4);
2066 for (unsigned i
= 0; i
< ncoords
; i
++)
2069 if (flags
& IR3_INSTR_A
) {
2070 if (ctx
->compiler
->levels_add_one
) {
2071 dst
[ncoords
-1] = ir3_ADD_U(b
, tmp
[3], 0, create_immed(b
, 1), 0);
2073 dst
[ncoords
-1] = ir3_MOV(b
, tmp
[3], TYPE_U32
);
2078 /* src[] = { deref, coord, sample_index, value, compare }. const_index[] = {} */
2079 static struct ir3_instruction
*
2080 emit_intrinsic_atomic_image(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
2082 struct ir3_block
*b
= ctx
->block
;
2083 const nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
2084 struct ir3_instruction
*atomic
, *image
, *src0
, *src1
, *src2
;
2085 struct ir3_instruction
* const *coords
= get_src(ctx
, &intr
->src
[1]);
2086 unsigned ncoords
= get_image_coords(var
, NULL
);
2088 image
= create_immed(b
, get_image_slot(ctx
, nir_src_as_deref(intr
->src
[0])));
2090 /* src0 is value (or uvec2(value, compare))
2092 * src2 is 64b byte offset
2094 src0
= get_src(ctx
, &intr
->src
[3])[0];
2095 src1
= create_collect(ctx
, coords
, ncoords
);
2096 src2
= get_image_offset(ctx
, var
, coords
, false);
2098 switch (intr
->intrinsic
) {
2099 case nir_intrinsic_image_deref_atomic_add
:
2100 atomic
= ir3_ATOMIC_ADD_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2102 case nir_intrinsic_image_deref_atomic_min
:
2103 atomic
= ir3_ATOMIC_MIN_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2105 case nir_intrinsic_image_deref_atomic_max
:
2106 atomic
= ir3_ATOMIC_MAX_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2108 case nir_intrinsic_image_deref_atomic_and
:
2109 atomic
= ir3_ATOMIC_AND_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2111 case nir_intrinsic_image_deref_atomic_or
:
2112 atomic
= ir3_ATOMIC_OR_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2114 case nir_intrinsic_image_deref_atomic_xor
:
2115 atomic
= ir3_ATOMIC_XOR_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2117 case nir_intrinsic_image_deref_atomic_exchange
:
2118 atomic
= ir3_ATOMIC_XCHG_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2120 case nir_intrinsic_image_deref_atomic_comp_swap
:
2121 /* for cmpxchg, src0 is [ui]vec2(data, compare): */
2122 src0
= create_collect(ctx
, (struct ir3_instruction
*[]){
2124 get_src(ctx
, &intr
->src
[4])[0],
2126 atomic
= ir3_ATOMIC_CMPXCHG_G(b
, image
, 0, src0
, 0, src1
, 0, src2
, 0);
2132 atomic
->cat6
.iim_val
= 1;
2133 atomic
->cat6
.d
= ncoords
;
2134 atomic
->cat6
.type
= get_image_type(var
);
2135 atomic
->cat6
.typed
= true;
2136 atomic
->barrier_class
= IR3_BARRIER_IMAGE_W
;
2137 atomic
->barrier_conflict
= IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
;
2139 /* even if nothing consume the result, we can't DCE the instruction: */
2140 array_insert(b
, b
->keeps
, atomic
);
2146 emit_intrinsic_barrier(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
2148 struct ir3_block
*b
= ctx
->block
;
2149 struct ir3_instruction
*barrier
;
2151 switch (intr
->intrinsic
) {
2152 case nir_intrinsic_barrier
:
2153 barrier
= ir3_BAR(b
);
2154 barrier
->cat7
.g
= true;
2155 barrier
->cat7
.l
= true;
2156 barrier
->flags
= IR3_INSTR_SS
| IR3_INSTR_SY
;
2157 barrier
->barrier_class
= IR3_BARRIER_EVERYTHING
;
2159 case nir_intrinsic_memory_barrier
:
2160 barrier
= ir3_FENCE(b
);
2161 barrier
->cat7
.g
= true;
2162 barrier
->cat7
.r
= true;
2163 barrier
->cat7
.w
= true;
2164 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
|
2165 IR3_BARRIER_BUFFER_W
;
2166 barrier
->barrier_conflict
=
2167 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
2168 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
2170 case nir_intrinsic_memory_barrier_atomic_counter
:
2171 case nir_intrinsic_memory_barrier_buffer
:
2172 barrier
= ir3_FENCE(b
);
2173 barrier
->cat7
.g
= true;
2174 barrier
->cat7
.r
= true;
2175 barrier
->cat7
.w
= true;
2176 barrier
->barrier_class
= IR3_BARRIER_BUFFER_W
;
2177 barrier
->barrier_conflict
= IR3_BARRIER_BUFFER_R
|
2178 IR3_BARRIER_BUFFER_W
;
2180 case nir_intrinsic_memory_barrier_image
:
2181 // TODO double check if this should have .g set
2182 barrier
= ir3_FENCE(b
);
2183 barrier
->cat7
.g
= true;
2184 barrier
->cat7
.r
= true;
2185 barrier
->cat7
.w
= true;
2186 barrier
->barrier_class
= IR3_BARRIER_IMAGE_W
;
2187 barrier
->barrier_conflict
= IR3_BARRIER_IMAGE_R
|
2188 IR3_BARRIER_IMAGE_W
;
2190 case nir_intrinsic_memory_barrier_shared
:
2191 barrier
= ir3_FENCE(b
);
2192 barrier
->cat7
.g
= true;
2193 barrier
->cat7
.l
= true;
2194 barrier
->cat7
.r
= true;
2195 barrier
->cat7
.w
= true;
2196 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
;
2197 barrier
->barrier_conflict
= IR3_BARRIER_SHARED_R
|
2198 IR3_BARRIER_SHARED_W
;
2200 case nir_intrinsic_group_memory_barrier
:
2201 barrier
= ir3_FENCE(b
);
2202 barrier
->cat7
.g
= true;
2203 barrier
->cat7
.l
= true;
2204 barrier
->cat7
.r
= true;
2205 barrier
->cat7
.w
= true;
2206 barrier
->barrier_class
= IR3_BARRIER_SHARED_W
|
2207 IR3_BARRIER_IMAGE_W
|
2208 IR3_BARRIER_BUFFER_W
;
2209 barrier
->barrier_conflict
=
2210 IR3_BARRIER_SHARED_R
| IR3_BARRIER_SHARED_W
|
2211 IR3_BARRIER_IMAGE_R
| IR3_BARRIER_IMAGE_W
|
2212 IR3_BARRIER_BUFFER_R
| IR3_BARRIER_BUFFER_W
;
2218 /* make sure barrier doesn't get DCE'd */
2219 array_insert(b
, b
->keeps
, barrier
);
2222 static void add_sysval_input_compmask(struct ir3_context
*ctx
,
2223 gl_system_value slot
, unsigned compmask
,
2224 struct ir3_instruction
*instr
)
2226 struct ir3_shader_variant
*so
= ctx
->so
;
2227 unsigned r
= regid(so
->inputs_count
, 0);
2228 unsigned n
= so
->inputs_count
++;
2230 so
->inputs
[n
].sysval
= true;
2231 so
->inputs
[n
].slot
= slot
;
2232 so
->inputs
[n
].compmask
= compmask
;
2233 so
->inputs
[n
].regid
= r
;
2234 so
->inputs
[n
].interpolate
= INTERP_MODE_FLAT
;
2237 ctx
->ir
->ninputs
= MAX2(ctx
->ir
->ninputs
, r
+ 1);
2238 ctx
->ir
->inputs
[r
] = instr
;
2241 static void add_sysval_input(struct ir3_context
*ctx
, gl_system_value slot
,
2242 struct ir3_instruction
*instr
)
2244 add_sysval_input_compmask(ctx
, slot
, 0x1, instr
);
2248 emit_intrinsic(struct ir3_context
*ctx
, nir_intrinsic_instr
*intr
)
2250 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[intr
->intrinsic
];
2251 struct ir3_instruction
**dst
;
2252 struct ir3_instruction
* const *src
;
2253 struct ir3_block
*b
= ctx
->block
;
2254 nir_const_value
*const_offset
;
2257 if (info
->has_dest
) {
2258 unsigned n
= nir_intrinsic_dest_components(intr
);
2259 dst
= get_dst(ctx
, &intr
->dest
, n
);
2264 switch (intr
->intrinsic
) {
2265 case nir_intrinsic_load_uniform
:
2266 idx
= nir_intrinsic_base(intr
);
2267 const_offset
= nir_src_as_const_value(intr
->src
[0]);
2269 idx
+= const_offset
->u32
[0];
2270 for (int i
= 0; i
< intr
->num_components
; i
++) {
2271 unsigned n
= idx
* 4 + i
;
2272 dst
[i
] = create_uniform(ctx
, n
);
2275 src
= get_src(ctx
, &intr
->src
[0]);
2276 for (int i
= 0; i
< intr
->num_components
; i
++) {
2277 int n
= idx
* 4 + i
;
2278 dst
[i
] = create_uniform_indirect(ctx
, n
,
2279 get_addr(ctx
, src
[0], 4));
2281 /* NOTE: if relative addressing is used, we set
2282 * constlen in the compiler (to worst-case value)
2283 * since we don't know in the assembler what the max
2284 * addr reg value can be:
2286 ctx
->so
->constlen
= ctx
->s
->num_uniforms
;
2289 case nir_intrinsic_load_ubo
:
2290 emit_intrinsic_load_ubo(ctx
, intr
, dst
);
2292 case nir_intrinsic_load_input
:
2293 idx
= nir_intrinsic_base(intr
);
2294 comp
= nir_intrinsic_component(intr
);
2295 const_offset
= nir_src_as_const_value(intr
->src
[0]);
2297 idx
+= const_offset
->u32
[0];
2298 for (int i
= 0; i
< intr
->num_components
; i
++) {
2299 unsigned n
= idx
* 4 + i
+ comp
;
2300 dst
[i
] = ctx
->ir
->inputs
[n
];
2303 src
= get_src(ctx
, &intr
->src
[0]);
2304 struct ir3_instruction
*collect
=
2305 create_collect(ctx
, ctx
->ir
->inputs
, ctx
->ir
->ninputs
);
2306 struct ir3_instruction
*addr
= get_addr(ctx
, src
[0], 4);
2307 for (int i
= 0; i
< intr
->num_components
; i
++) {
2308 unsigned n
= idx
* 4 + i
+ comp
;
2309 dst
[i
] = create_indirect_load(ctx
, ctx
->ir
->ninputs
,
2314 case nir_intrinsic_load_ssbo
:
2315 emit_intrinsic_load_ssbo(ctx
, intr
, dst
);
2317 case nir_intrinsic_store_ssbo
:
2318 emit_intrinsic_store_ssbo(ctx
, intr
);
2320 case nir_intrinsic_get_buffer_size
:
2321 emit_intrinsic_ssbo_size(ctx
, intr
, dst
);
2323 case nir_intrinsic_ssbo_atomic_add
:
2324 case nir_intrinsic_ssbo_atomic_imin
:
2325 case nir_intrinsic_ssbo_atomic_umin
:
2326 case nir_intrinsic_ssbo_atomic_imax
:
2327 case nir_intrinsic_ssbo_atomic_umax
:
2328 case nir_intrinsic_ssbo_atomic_and
:
2329 case nir_intrinsic_ssbo_atomic_or
:
2330 case nir_intrinsic_ssbo_atomic_xor
:
2331 case nir_intrinsic_ssbo_atomic_exchange
:
2332 case nir_intrinsic_ssbo_atomic_comp_swap
:
2333 dst
[0] = emit_intrinsic_atomic_ssbo(ctx
, intr
);
2335 case nir_intrinsic_load_shared
:
2336 emit_intrinsic_load_shared(ctx
, intr
, dst
);
2338 case nir_intrinsic_store_shared
:
2339 emit_intrinsic_store_shared(ctx
, intr
);
2341 case nir_intrinsic_shared_atomic_add
:
2342 case nir_intrinsic_shared_atomic_imin
:
2343 case nir_intrinsic_shared_atomic_umin
:
2344 case nir_intrinsic_shared_atomic_imax
:
2345 case nir_intrinsic_shared_atomic_umax
:
2346 case nir_intrinsic_shared_atomic_and
:
2347 case nir_intrinsic_shared_atomic_or
:
2348 case nir_intrinsic_shared_atomic_xor
:
2349 case nir_intrinsic_shared_atomic_exchange
:
2350 case nir_intrinsic_shared_atomic_comp_swap
:
2351 dst
[0] = emit_intrinsic_atomic_shared(ctx
, intr
);
2353 case nir_intrinsic_image_deref_load
:
2354 emit_intrinsic_load_image(ctx
, intr
, dst
);
2356 case nir_intrinsic_image_deref_store
:
2357 emit_intrinsic_store_image(ctx
, intr
);
2359 case nir_intrinsic_image_deref_size
:
2360 emit_intrinsic_image_size(ctx
, intr
, dst
);
2362 case nir_intrinsic_image_deref_atomic_add
:
2363 case nir_intrinsic_image_deref_atomic_min
:
2364 case nir_intrinsic_image_deref_atomic_max
:
2365 case nir_intrinsic_image_deref_atomic_and
:
2366 case nir_intrinsic_image_deref_atomic_or
:
2367 case nir_intrinsic_image_deref_atomic_xor
:
2368 case nir_intrinsic_image_deref_atomic_exchange
:
2369 case nir_intrinsic_image_deref_atomic_comp_swap
:
2370 dst
[0] = emit_intrinsic_atomic_image(ctx
, intr
);
2372 case nir_intrinsic_barrier
:
2373 case nir_intrinsic_memory_barrier
:
2374 case nir_intrinsic_group_memory_barrier
:
2375 case nir_intrinsic_memory_barrier_atomic_counter
:
2376 case nir_intrinsic_memory_barrier_buffer
:
2377 case nir_intrinsic_memory_barrier_image
:
2378 case nir_intrinsic_memory_barrier_shared
:
2379 emit_intrinsic_barrier(ctx
, intr
);
2380 /* note that blk ptr no longer valid, make that obvious: */
2383 case nir_intrinsic_store_output
:
2384 idx
= nir_intrinsic_base(intr
);
2385 comp
= nir_intrinsic_component(intr
);
2386 const_offset
= nir_src_as_const_value(intr
->src
[1]);
2387 compile_assert(ctx
, const_offset
!= NULL
);
2388 idx
+= const_offset
->u32
[0];
2390 src
= get_src(ctx
, &intr
->src
[0]);
2391 for (int i
= 0; i
< intr
->num_components
; i
++) {
2392 unsigned n
= idx
* 4 + i
+ comp
;
2393 ctx
->ir
->outputs
[n
] = src
[i
];
2396 case nir_intrinsic_load_base_vertex
:
2397 case nir_intrinsic_load_first_vertex
:
2398 if (!ctx
->basevertex
) {
2399 ctx
->basevertex
= create_driver_param(ctx
, IR3_DP_VTXID_BASE
);
2400 add_sysval_input(ctx
, SYSTEM_VALUE_FIRST_VERTEX
, ctx
->basevertex
);
2402 dst
[0] = ctx
->basevertex
;
2404 case nir_intrinsic_load_vertex_id_zero_base
:
2405 case nir_intrinsic_load_vertex_id
:
2406 if (!ctx
->vertex_id
) {
2407 gl_system_value sv
= (intr
->intrinsic
== nir_intrinsic_load_vertex_id
) ?
2408 SYSTEM_VALUE_VERTEX_ID
: SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
2409 ctx
->vertex_id
= create_input(ctx
, 0);
2410 add_sysval_input(ctx
, sv
, ctx
->vertex_id
);
2412 dst
[0] = ctx
->vertex_id
;
2414 case nir_intrinsic_load_instance_id
:
2415 if (!ctx
->instance_id
) {
2416 ctx
->instance_id
= create_input(ctx
, 0);
2417 add_sysval_input(ctx
, SYSTEM_VALUE_INSTANCE_ID
,
2420 dst
[0] = ctx
->instance_id
;
2422 case nir_intrinsic_load_sample_id
:
2423 case nir_intrinsic_load_sample_id_no_per_sample
:
2424 if (!ctx
->samp_id
) {
2425 ctx
->samp_id
= create_input(ctx
, 0);
2426 ctx
->samp_id
->regs
[0]->flags
|= IR3_REG_HALF
;
2427 add_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_ID
,
2430 dst
[0] = ir3_COV(b
, ctx
->samp_id
, TYPE_U16
, TYPE_U32
);
2432 case nir_intrinsic_load_sample_mask_in
:
2433 if (!ctx
->samp_mask_in
) {
2434 ctx
->samp_mask_in
= create_input(ctx
, 0);
2435 add_sysval_input(ctx
, SYSTEM_VALUE_SAMPLE_MASK_IN
,
2438 dst
[0] = ctx
->samp_mask_in
;
2440 case nir_intrinsic_load_user_clip_plane
:
2441 idx
= nir_intrinsic_ucp_id(intr
);
2442 for (int i
= 0; i
< intr
->num_components
; i
++) {
2443 unsigned n
= idx
* 4 + i
;
2444 dst
[i
] = create_driver_param(ctx
, IR3_DP_UCP0_X
+ n
);
2447 case nir_intrinsic_load_front_face
:
2448 if (!ctx
->frag_face
) {
2449 ctx
->so
->frag_face
= true;
2450 ctx
->frag_face
= create_input(ctx
, 0);
2451 ctx
->frag_face
->regs
[0]->flags
|= IR3_REG_HALF
;
2453 /* for fragface, we get -1 for back and 0 for front. However this is
2454 * the inverse of what nir expects (where ~0 is true).
2456 dst
[0] = ir3_COV(b
, ctx
->frag_face
, TYPE_S16
, TYPE_S32
);
2457 dst
[0] = ir3_NOT_B(b
, dst
[0], 0);
2459 case nir_intrinsic_load_local_invocation_id
:
2460 if (!ctx
->local_invocation_id
) {
2461 ctx
->local_invocation_id
= create_input_compmask(ctx
, 0, 0x7);
2462 add_sysval_input_compmask(ctx
, SYSTEM_VALUE_LOCAL_INVOCATION_ID
,
2463 0x7, ctx
->local_invocation_id
);
2465 split_dest(b
, dst
, ctx
->local_invocation_id
, 0, 3);
2467 case nir_intrinsic_load_work_group_id
:
2468 if (!ctx
->work_group_id
) {
2469 ctx
->work_group_id
= create_input_compmask(ctx
, 0, 0x7);
2470 add_sysval_input_compmask(ctx
, SYSTEM_VALUE_WORK_GROUP_ID
,
2471 0x7, ctx
->work_group_id
);
2472 ctx
->work_group_id
->regs
[0]->flags
|= IR3_REG_HIGH
;
2474 split_dest(b
, dst
, ctx
->work_group_id
, 0, 3);
2476 case nir_intrinsic_load_num_work_groups
:
2477 for (int i
= 0; i
< intr
->num_components
; i
++) {
2478 dst
[i
] = create_driver_param(ctx
, IR3_DP_NUM_WORK_GROUPS_X
+ i
);
2481 case nir_intrinsic_load_local_group_size
:
2482 for (int i
= 0; i
< intr
->num_components
; i
++) {
2483 dst
[i
] = create_driver_param(ctx
, IR3_DP_LOCAL_GROUP_SIZE_X
+ i
);
2486 case nir_intrinsic_discard_if
:
2487 case nir_intrinsic_discard
: {
2488 struct ir3_instruction
*cond
, *kill
;
2490 if (intr
->intrinsic
== nir_intrinsic_discard_if
) {
2491 /* conditional discard: */
2492 src
= get_src(ctx
, &intr
->src
[0]);
2493 cond
= ir3_b2n(b
, src
[0]);
2495 /* unconditional discard: */
2496 cond
= create_immed(b
, 1);
2499 /* NOTE: only cmps.*.* can write p0.x: */
2500 cond
= ir3_CMPS_S(b
, cond
, 0, create_immed(b
, 0), 0);
2501 cond
->cat2
.condition
= IR3_COND_NE
;
2503 /* condition always goes in predicate register: */
2504 cond
->regs
[0]->num
= regid(REG_P0
, 0);
2506 kill
= ir3_KILL(b
, cond
, 0);
2507 array_insert(ctx
->ir
, ctx
->ir
->predicates
, kill
);
2509 array_insert(b
, b
->keeps
, kill
);
2510 ctx
->so
->has_kill
= true;
2515 compile_error(ctx
, "Unhandled intrinsic type: %s\n",
2516 nir_intrinsic_infos
[intr
->intrinsic
].name
);
2521 put_dst(ctx
, &intr
->dest
);
2525 emit_load_const(struct ir3_context
*ctx
, nir_load_const_instr
*instr
)
2527 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &instr
->def
,
2528 instr
->def
.num_components
);
2529 type_t type
= (instr
->def
.bit_size
< 32) ? TYPE_U16
: TYPE_U32
;
2531 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
2532 dst
[i
] = create_immed_typed(ctx
->block
, instr
->value
.u32
[i
], type
);
2536 emit_undef(struct ir3_context
*ctx
, nir_ssa_undef_instr
*undef
)
2538 struct ir3_instruction
**dst
= get_dst_ssa(ctx
, &undef
->def
,
2539 undef
->def
.num_components
);
2540 type_t type
= (undef
->def
.bit_size
< 32) ? TYPE_U16
: TYPE_U32
;
2542 /* backend doesn't want undefined instructions, so just plug
2545 for (int i
= 0; i
< undef
->def
.num_components
; i
++)
2546 dst
[i
] = create_immed_typed(ctx
->block
, fui(0.0), type
);
2550 * texture fetch/sample instructions:
2554 tex_info(nir_tex_instr
*tex
, unsigned *flagsp
, unsigned *coordsp
)
2556 unsigned coords
, flags
= 0;
2558 /* note: would use tex->coord_components.. except txs.. also,
2559 * since array index goes after shadow ref, we don't want to
2562 switch (tex
->sampler_dim
) {
2563 case GLSL_SAMPLER_DIM_1D
:
2564 case GLSL_SAMPLER_DIM_BUF
:
2567 case GLSL_SAMPLER_DIM_2D
:
2568 case GLSL_SAMPLER_DIM_RECT
:
2569 case GLSL_SAMPLER_DIM_EXTERNAL
:
2570 case GLSL_SAMPLER_DIM_MS
:
2573 case GLSL_SAMPLER_DIM_3D
:
2574 case GLSL_SAMPLER_DIM_CUBE
:
2576 flags
|= IR3_INSTR_3D
;
2579 unreachable("bad sampler_dim");
2582 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
2583 flags
|= IR3_INSTR_S
;
2585 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
)
2586 flags
|= IR3_INSTR_A
;
2593 emit_tex(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
2595 struct ir3_block
*b
= ctx
->block
;
2596 struct ir3_instruction
**dst
, *sam
, *src0
[12], *src1
[4];
2597 struct ir3_instruction
* const *coord
, * const *off
, * const *ddx
, * const *ddy
;
2598 struct ir3_instruction
*lod
, *compare
, *proj
, *sample_index
;
2599 bool has_bias
= false, has_lod
= false, has_proj
= false, has_off
= false;
2600 unsigned i
, coords
, flags
;
2601 unsigned nsrc0
= 0, nsrc1
= 0;
2605 coord
= off
= ddx
= ddy
= NULL
;
2606 lod
= proj
= compare
= sample_index
= NULL
;
2608 /* TODO: might just be one component for gathers? */
2609 dst
= get_dst(ctx
, &tex
->dest
, 4);
2611 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
2612 switch (tex
->src
[i
].src_type
) {
2613 case nir_tex_src_coord
:
2614 coord
= get_src(ctx
, &tex
->src
[i
].src
);
2616 case nir_tex_src_bias
:
2617 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
2620 case nir_tex_src_lod
:
2621 lod
= get_src(ctx
, &tex
->src
[i
].src
)[0];
2624 case nir_tex_src_comparator
: /* shadow comparator */
2625 compare
= get_src(ctx
, &tex
->src
[i
].src
)[0];
2627 case nir_tex_src_projector
:
2628 proj
= get_src(ctx
, &tex
->src
[i
].src
)[0];
2631 case nir_tex_src_offset
:
2632 off
= get_src(ctx
, &tex
->src
[i
].src
);
2635 case nir_tex_src_ddx
:
2636 ddx
= get_src(ctx
, &tex
->src
[i
].src
);
2638 case nir_tex_src_ddy
:
2639 ddy
= get_src(ctx
, &tex
->src
[i
].src
);
2641 case nir_tex_src_ms_index
:
2642 sample_index
= get_src(ctx
, &tex
->src
[i
].src
)[0];
2645 compile_error(ctx
, "Unhandled NIR tex src type: %d\n",
2646 tex
->src
[i
].src_type
);
2652 case nir_texop_tex
: opc
= has_lod
? OPC_SAML
: OPC_SAM
; break;
2653 case nir_texop_txb
: opc
= OPC_SAMB
; break;
2654 case nir_texop_txl
: opc
= OPC_SAML
; break;
2655 case nir_texop_txd
: opc
= OPC_SAMGQ
; break;
2656 case nir_texop_txf
: opc
= OPC_ISAML
; break;
2657 case nir_texop_lod
: opc
= OPC_GETLOD
; break;
2659 /* NOTE: a4xx might need to emulate gather w/ txf (this is
2660 * what blob does, seems gather is broken?), and a3xx did
2661 * not support it (but probably could also emulate).
2663 switch (tex
->component
) {
2664 case 0: opc
= OPC_GATHER4R
; break;
2665 case 1: opc
= OPC_GATHER4G
; break;
2666 case 2: opc
= OPC_GATHER4B
; break;
2667 case 3: opc
= OPC_GATHER4A
; break;
2670 case nir_texop_txf_ms
: opc
= OPC_ISAMM
; break;
2672 case nir_texop_query_levels
:
2673 case nir_texop_texture_samples
:
2674 case nir_texop_samples_identical
:
2675 case nir_texop_txf_ms_mcs
:
2676 compile_error(ctx
, "Unhandled NIR tex type: %d\n", tex
->op
);
2680 tex_info(tex
, &flags
, &coords
);
2683 * lay out the first argument in the proper order:
2684 * - actual coordinates first
2685 * - shadow reference
2688 * - starting at offset 4, dpdx.xy, dpdy.xy
2690 * bias/lod go into the second arg
2693 /* insert tex coords: */
2694 for (i
= 0; i
< coords
; i
++)
2699 /* NOTE a3xx (and possibly a4xx?) might be different, using isaml
2700 * with scaled x coord according to requested sample:
2702 if (tex
->op
== nir_texop_txf_ms
) {
2703 if (ctx
->compiler
->txf_ms_with_isaml
) {
2704 /* the samples are laid out in x dimension as
2706 * x_ms = (x << ms) + sample_index;
2708 struct ir3_instruction
*ms
;
2709 ms
= create_immed(b
, (ctx
->samples
>> (2 * tex
->texture_index
)) & 3);
2711 src0
[0] = ir3_SHL_B(b
, src0
[0], 0, ms
, 0);
2712 src0
[0] = ir3_ADD_U(b
, src0
[0], 0, sample_index
, 0);
2716 src0
[nsrc0
++] = sample_index
;
2720 /* scale up integer coords for TXF based on the LOD */
2721 if (ctx
->compiler
->unminify_coords
&& (opc
== OPC_ISAML
)) {
2723 for (i
= 0; i
< coords
; i
++)
2724 src0
[i
] = ir3_SHL_B(b
, src0
[i
], 0, lod
, 0);
2728 /* hw doesn't do 1d, so we treat it as 2d with
2729 * height of 1, and patch up the y coord.
2730 * TODO: y coord should be (int)0 in some cases..
2732 src0
[nsrc0
++] = create_immed(b
, fui(0.5));
2735 if (tex
->is_shadow
&& tex
->op
!= nir_texop_lod
)
2736 src0
[nsrc0
++] = compare
;
2738 if (tex
->is_array
&& tex
->op
!= nir_texop_lod
) {
2739 struct ir3_instruction
*idx
= coord
[coords
];
2741 /* the array coord for cube arrays needs 0.5 added to it */
2742 if (ctx
->compiler
->array_index_add_half
&& (opc
!= OPC_ISAML
))
2743 idx
= ir3_ADD_F(b
, idx
, 0, create_immed(b
, fui(0.5)), 0);
2745 src0
[nsrc0
++] = idx
;
2749 src0
[nsrc0
++] = proj
;
2750 flags
|= IR3_INSTR_P
;
2753 /* pad to 4, then ddx/ddy: */
2754 if (tex
->op
== nir_texop_txd
) {
2756 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2757 for (i
= 0; i
< coords
; i
++)
2758 src0
[nsrc0
++] = ddx
[i
];
2760 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2761 for (i
= 0; i
< coords
; i
++)
2762 src0
[nsrc0
++] = ddy
[i
];
2764 src0
[nsrc0
++] = create_immed(b
, fui(0.0));
2768 * second argument (if applicable):
2773 if (has_off
| has_lod
| has_bias
) {
2775 unsigned off_coords
= coords
;
2776 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2778 for (i
= 0; i
< off_coords
; i
++)
2779 src1
[nsrc1
++] = off
[i
];
2781 src1
[nsrc1
++] = create_immed(b
, fui(0.0));
2782 flags
|= IR3_INSTR_O
;
2785 if (has_lod
| has_bias
)
2786 src1
[nsrc1
++] = lod
;
2789 switch (tex
->dest_type
) {
2790 case nir_type_invalid
:
2791 case nir_type_float
:
2802 unreachable("bad dest_type");
2805 if (opc
== OPC_GETLOD
)
2808 unsigned tex_idx
= tex
->texture_index
;
2810 ctx
->max_texture_index
= MAX2(ctx
->max_texture_index
, tex_idx
);
2812 struct ir3_instruction
*col0
= create_collect(ctx
, src0
, nsrc0
);
2813 struct ir3_instruction
*col1
= create_collect(ctx
, src1
, nsrc1
);
2815 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_XYZW
, flags
,
2816 tex_idx
, tex_idx
, col0
, col1
);
2818 if ((ctx
->astc_srgb
& (1 << tex_idx
)) && !nir_tex_instr_is_query(tex
)) {
2819 /* only need first 3 components: */
2820 sam
->regs
[0]->wrmask
= 0x7;
2821 split_dest(b
, dst
, sam
, 0, 3);
2823 /* we need to sample the alpha separately with a non-ASTC
2826 sam
= ir3_SAM(b
, opc
, type
, TGSI_WRITEMASK_W
, flags
,
2827 tex_idx
, tex_idx
, col0
, col1
);
2829 array_insert(ctx
->ir
, ctx
->ir
->astc_srgb
, sam
);
2831 /* fixup .w component: */
2832 split_dest(b
, &dst
[3], sam
, 3, 1);
2834 /* normal (non-workaround) case: */
2835 split_dest(b
, dst
, sam
, 0, 4);
2838 /* GETLOD returns results in 4.8 fixed point */
2839 if (opc
== OPC_GETLOD
) {
2840 struct ir3_instruction
*factor
= create_immed(b
, fui(1.0 / 256));
2842 compile_assert(ctx
, tex
->dest_type
== nir_type_float
);
2843 for (i
= 0; i
< 2; i
++) {
2844 dst
[i
] = ir3_MUL_F(b
, ir3_COV(b
, dst
[i
], TYPE_U32
, TYPE_F32
), 0,
2849 put_dst(ctx
, &tex
->dest
);
2853 emit_tex_query_levels(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
2855 struct ir3_block
*b
= ctx
->block
;
2856 struct ir3_instruction
**dst
, *sam
;
2858 dst
= get_dst(ctx
, &tex
->dest
, 1);
2860 sam
= ir3_SAM(b
, OPC_GETINFO
, TYPE_U32
, TGSI_WRITEMASK_Z
, 0,
2861 tex
->texture_index
, tex
->texture_index
, NULL
, NULL
);
2863 /* even though there is only one component, since it ends
2864 * up in .z rather than .x, we need a split_dest()
2866 split_dest(b
, dst
, sam
, 0, 3);
2868 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
2869 * the value in TEX_CONST_0 is zero-based.
2871 if (ctx
->compiler
->levels_add_one
)
2872 dst
[0] = ir3_ADD_U(b
, dst
[0], 0, create_immed(b
, 1), 0);
2874 put_dst(ctx
, &tex
->dest
);
2878 emit_tex_txs(struct ir3_context
*ctx
, nir_tex_instr
*tex
)
2880 struct ir3_block
*b
= ctx
->block
;
2881 struct ir3_instruction
**dst
, *sam
;
2882 struct ir3_instruction
*lod
;
2883 unsigned flags
, coords
;
2885 tex_info(tex
, &flags
, &coords
);
2887 /* Actually we want the number of dimensions, not coordinates. This
2888 * distinction only matters for cubes.
2890 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
2893 dst
= get_dst(ctx
, &tex
->dest
, 4);
2895 compile_assert(ctx
, tex
->num_srcs
== 1);
2896 compile_assert(ctx
, tex
->src
[0].src_type
== nir_tex_src_lod
);
2898 lod
= get_src(ctx
, &tex
->src
[0].src
)[0];
2900 sam
= ir3_SAM(b
, OPC_GETSIZE
, TYPE_U32
, TGSI_WRITEMASK_XYZW
, flags
,
2901 tex
->texture_index
, tex
->texture_index
, lod
, NULL
);
2903 split_dest(b
, dst
, sam
, 0, 4);
2905 /* Array size actually ends up in .w rather than .z. This doesn't
2906 * matter for miplevel 0, but for higher mips the value in z is
2907 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
2908 * returned, which means that we have to add 1 to it for arrays.
2910 if (tex
->is_array
) {
2911 if (ctx
->compiler
->levels_add_one
) {
2912 dst
[coords
] = ir3_ADD_U(b
, dst
[3], 0, create_immed(b
, 1), 0);
2914 dst
[coords
] = ir3_MOV(b
, dst
[3], TYPE_U32
);
2918 put_dst(ctx
, &tex
->dest
);
2922 emit_jump(struct ir3_context
*ctx
, nir_jump_instr
*jump
)
2924 switch (jump
->type
) {
2925 case nir_jump_break
:
2926 case nir_jump_continue
:
2927 case nir_jump_return
:
2928 /* I *think* we can simply just ignore this, and use the
2929 * successor block link to figure out where we need to
2930 * jump to for break/continue
2934 compile_error(ctx
, "Unhandled NIR jump type: %d\n", jump
->type
);
2940 emit_instr(struct ir3_context
*ctx
, nir_instr
*instr
)
2942 switch (instr
->type
) {
2943 case nir_instr_type_alu
:
2944 emit_alu(ctx
, nir_instr_as_alu(instr
));
2946 case nir_instr_type_deref
:
2947 /* ignored, handled as part of the intrinsic they are src to */
2949 case nir_instr_type_intrinsic
:
2950 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2952 case nir_instr_type_load_const
:
2953 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2955 case nir_instr_type_ssa_undef
:
2956 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
2958 case nir_instr_type_tex
: {
2959 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
2960 /* couple tex instructions get special-cased:
2964 emit_tex_txs(ctx
, tex
);
2966 case nir_texop_query_levels
:
2967 emit_tex_query_levels(ctx
, tex
);
2975 case nir_instr_type_jump
:
2976 emit_jump(ctx
, nir_instr_as_jump(instr
));
2978 case nir_instr_type_phi
:
2979 /* we have converted phi webs to regs in NIR by now */
2980 compile_error(ctx
, "Unexpected NIR instruction type: %d\n", instr
->type
);
2982 case nir_instr_type_call
:
2983 case nir_instr_type_parallel_copy
:
2984 compile_error(ctx
, "Unhandled NIR instruction type: %d\n", instr
->type
);
2989 static struct ir3_block
*
2990 get_block(struct ir3_context
*ctx
, const nir_block
*nblock
)
2992 struct ir3_block
*block
;
2993 struct hash_entry
*hentry
;
2994 struct set_entry
*sentry
;
2997 hentry
= _mesa_hash_table_search(ctx
->block_ht
, nblock
);
2999 return hentry
->data
;
3001 block
= ir3_block_create(ctx
->ir
);
3002 block
->nblock
= nblock
;
3003 _mesa_hash_table_insert(ctx
->block_ht
, nblock
, block
);
3005 block
->predecessors_count
= nblock
->predecessors
->entries
;
3006 block
->predecessors
= ralloc_array_size(block
,
3007 sizeof(block
->predecessors
[0]), block
->predecessors_count
);
3009 set_foreach(nblock
->predecessors
, sentry
) {
3010 block
->predecessors
[i
++] = get_block(ctx
, sentry
->key
);
3017 emit_block(struct ir3_context
*ctx
, nir_block
*nblock
)
3019 struct ir3_block
*block
= get_block(ctx
, nblock
);
3021 for (int i
= 0; i
< ARRAY_SIZE(block
->successors
); i
++) {
3022 if (nblock
->successors
[i
]) {
3023 block
->successors
[i
] =
3024 get_block(ctx
, nblock
->successors
[i
]);
3029 list_addtail(&block
->node
, &ctx
->ir
->block_list
);
3031 /* re-emit addr register in each block if needed: */
3032 for (int i
= 0; i
< ARRAY_SIZE(ctx
->addr_ht
); i
++) {
3033 _mesa_hash_table_destroy(ctx
->addr_ht
[i
], NULL
);
3034 ctx
->addr_ht
[i
] = NULL
;
3037 nir_foreach_instr(instr
, nblock
) {
3038 emit_instr(ctx
, instr
);
3044 static void emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
);
3047 emit_if(struct ir3_context
*ctx
, nir_if
*nif
)
3049 struct ir3_instruction
*condition
= get_src(ctx
, &nif
->condition
)[0];
3051 ctx
->block
->condition
=
3052 get_predicate(ctx
, ir3_b2n(condition
->block
, condition
));
3054 emit_cf_list(ctx
, &nif
->then_list
);
3055 emit_cf_list(ctx
, &nif
->else_list
);
3059 emit_loop(struct ir3_context
*ctx
, nir_loop
*nloop
)
3061 emit_cf_list(ctx
, &nloop
->body
);
3065 emit_cf_list(struct ir3_context
*ctx
, struct exec_list
*list
)
3067 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
3068 switch (node
->type
) {
3069 case nir_cf_node_block
:
3070 emit_block(ctx
, nir_cf_node_as_block(node
));
3072 case nir_cf_node_if
:
3073 emit_if(ctx
, nir_cf_node_as_if(node
));
3075 case nir_cf_node_loop
:
3076 emit_loop(ctx
, nir_cf_node_as_loop(node
));
3078 case nir_cf_node_function
:
3079 compile_error(ctx
, "TODO\n");
3085 /* emit stream-out code. At this point, the current block is the original
3086 * (nir) end block, and nir ensures that all flow control paths terminate
3087 * into the end block. We re-purpose the original end block to generate
3088 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
3089 * block holding stream-out write instructions, followed by the new end
3093 * p0.x = (vtxcnt < maxvtxcnt)
3094 * // succs: blockStreamOut, blockNewEnd
3097 * ... stream-out instructions ...
3098 * // succs: blockNewEnd
3104 emit_stream_out(struct ir3_context
*ctx
)
3106 struct ir3_shader_variant
*v
= ctx
->so
;
3107 struct ir3
*ir
= ctx
->ir
;
3108 struct pipe_stream_output_info
*strmout
=
3109 &ctx
->so
->shader
->stream_output
;
3110 struct ir3_block
*orig_end_block
, *stream_out_block
, *new_end_block
;
3111 struct ir3_instruction
*vtxcnt
, *maxvtxcnt
, *cond
;
3112 struct ir3_instruction
*bases
[PIPE_MAX_SO_BUFFERS
];
3114 /* create vtxcnt input in input block at top of shader,
3115 * so that it is seen as live over the entire duration
3118 vtxcnt
= create_input(ctx
, 0);
3119 add_sysval_input(ctx
, SYSTEM_VALUE_VERTEX_CNT
, vtxcnt
);
3121 maxvtxcnt
= create_driver_param(ctx
, IR3_DP_VTXCNT_MAX
);
3123 /* at this point, we are at the original 'end' block,
3124 * re-purpose this block to stream-out condition, then
3125 * append stream-out block and new-end block
3127 orig_end_block
= ctx
->block
;
3129 // TODO these blocks need to update predecessors..
3130 // maybe w/ store_global intrinsic, we could do this
3131 // stuff in nir->nir pass
3133 stream_out_block
= ir3_block_create(ir
);
3134 list_addtail(&stream_out_block
->node
, &ir
->block_list
);
3136 new_end_block
= ir3_block_create(ir
);
3137 list_addtail(&new_end_block
->node
, &ir
->block_list
);
3139 orig_end_block
->successors
[0] = stream_out_block
;
3140 orig_end_block
->successors
[1] = new_end_block
;
3141 stream_out_block
->successors
[0] = new_end_block
;
3143 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
3144 cond
= ir3_CMPS_S(ctx
->block
, vtxcnt
, 0, maxvtxcnt
, 0);
3145 cond
->regs
[0]->num
= regid(REG_P0
, 0);
3146 cond
->cat2
.condition
= IR3_COND_LT
;
3148 /* condition goes on previous block to the conditional,
3149 * since it is used to pick which of the two successor
3152 orig_end_block
->condition
= cond
;
3154 /* switch to stream_out_block to generate the stream-out
3157 ctx
->block
= stream_out_block
;
3159 /* Calculate base addresses based on vtxcnt. Instructions
3160 * generated for bases not used in following loop will be
3161 * stripped out in the backend.
3163 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
3164 unsigned stride
= strmout
->stride
[i
];
3165 struct ir3_instruction
*base
, *off
;
3167 base
= create_uniform(ctx
, regid(v
->constbase
.tfbo
, i
));
3169 /* 24-bit should be enough: */
3170 off
= ir3_MUL_U(ctx
->block
, vtxcnt
, 0,
3171 create_immed(ctx
->block
, stride
* 4), 0);
3173 bases
[i
] = ir3_ADD_S(ctx
->block
, off
, 0, base
, 0);
3176 /* Generate the per-output store instructions: */
3177 for (unsigned i
= 0; i
< strmout
->num_outputs
; i
++) {
3178 for (unsigned j
= 0; j
< strmout
->output
[i
].num_components
; j
++) {
3179 unsigned c
= j
+ strmout
->output
[i
].start_component
;
3180 struct ir3_instruction
*base
, *out
, *stg
;
3182 base
= bases
[strmout
->output
[i
].output_buffer
];
3183 out
= ctx
->ir
->outputs
[regid(strmout
->output
[i
].register_index
, c
)];
3185 stg
= ir3_STG(ctx
->block
, base
, 0, out
, 0,
3186 create_immed(ctx
->block
, 1), 0);
3187 stg
->cat6
.type
= TYPE_U32
;
3188 stg
->cat6
.dst_offset
= (strmout
->output
[i
].dst_offset
+ j
) * 4;
3190 array_insert(ctx
->block
, ctx
->block
->keeps
, stg
);
3194 /* and finally switch to the new_end_block: */
3195 ctx
->block
= new_end_block
;
3199 emit_function(struct ir3_context
*ctx
, nir_function_impl
*impl
)
3201 nir_metadata_require(impl
, nir_metadata_block_index
);
3203 emit_cf_list(ctx
, &impl
->body
);
3204 emit_block(ctx
, impl
->end_block
);
3206 /* at this point, we should have a single empty block,
3207 * into which we emit the 'end' instruction.
3209 compile_assert(ctx
, list_empty(&ctx
->block
->instr_list
));
3211 /* If stream-out (aka transform-feedback) enabled, emit the
3212 * stream-out instructions, followed by a new empty block (into
3213 * which the 'end' instruction lands).
3215 * NOTE: it is done in this order, rather than inserting before
3216 * we emit end_block, because NIR guarantees that all blocks
3217 * flow into end_block, and that end_block has no successors.
3218 * So by re-purposing end_block as the first block of stream-
3219 * out, we guarantee that all exit paths flow into the stream-
3222 if ((ctx
->compiler
->gpu_id
< 500) &&
3223 (ctx
->so
->shader
->stream_output
.num_outputs
> 0) &&
3224 !ctx
->so
->key
.binning_pass
) {
3225 debug_assert(ctx
->so
->type
== SHADER_VERTEX
);
3226 emit_stream_out(ctx
);
3229 ir3_END(ctx
->block
);
3233 setup_input(struct ir3_context
*ctx
, nir_variable
*in
)
3235 struct ir3_shader_variant
*so
= ctx
->so
;
3236 unsigned array_len
= MAX2(glsl_get_length(in
->type
), 1);
3237 unsigned ncomp
= glsl_get_components(in
->type
);
3238 unsigned n
= in
->data
.driver_location
;
3239 unsigned slot
= in
->data
.location
;
3241 DBG("; in: slot=%u, len=%ux%u, drvloc=%u",
3242 slot
, array_len
, ncomp
, n
);
3244 /* let's pretend things other than vec4 don't exist: */
3245 ncomp
= MAX2(ncomp
, 4);
3246 compile_assert(ctx
, ncomp
== 4);
3248 so
->inputs
[n
].slot
= slot
;
3249 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
3250 so
->inputs_count
= MAX2(so
->inputs_count
, n
+ 1);
3251 so
->inputs
[n
].interpolate
= in
->data
.interpolation
;
3253 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
3254 for (int i
= 0; i
< ncomp
; i
++) {
3255 struct ir3_instruction
*instr
= NULL
;
3256 unsigned idx
= (n
* 4) + i
;
3258 if (slot
== VARYING_SLOT_POS
) {
3259 so
->inputs
[n
].bary
= false;
3260 so
->frag_coord
= true;
3261 instr
= create_frag_coord(ctx
, i
);
3262 } else if (slot
== VARYING_SLOT_PNTC
) {
3263 /* see for example st_get_generic_varying_index().. this is
3264 * maybe a bit mesa/st specific. But we need things to line
3265 * up for this in fdN_program:
3266 * unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
3267 * if (emit->sprite_coord_enable & texmask) {
3271 so
->inputs
[n
].slot
= VARYING_SLOT_VAR8
;
3272 so
->inputs
[n
].bary
= true;
3273 instr
= create_frag_input(ctx
, false);
3275 bool use_ldlv
= false;
3277 /* detect the special case for front/back colors where
3278 * we need to do flat vs smooth shading depending on
3281 if (in
->data
.interpolation
== INTERP_MODE_NONE
) {
3283 case VARYING_SLOT_COL0
:
3284 case VARYING_SLOT_COL1
:
3285 case VARYING_SLOT_BFC0
:
3286 case VARYING_SLOT_BFC1
:
3287 so
->inputs
[n
].rasterflat
= true;
3294 if (ctx
->compiler
->flat_bypass
) {
3295 if ((so
->inputs
[n
].interpolate
== INTERP_MODE_FLAT
) ||
3296 (so
->inputs
[n
].rasterflat
&& ctx
->so
->key
.rasterflat
))
3300 so
->inputs
[n
].bary
= true;
3302 instr
= create_frag_input(ctx
, use_ldlv
);
3305 compile_assert(ctx
, idx
< ctx
->ir
->ninputs
);
3307 ctx
->ir
->inputs
[idx
] = instr
;
3309 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
3310 for (int i
= 0; i
< ncomp
; i
++) {
3311 unsigned idx
= (n
* 4) + i
;
3312 compile_assert(ctx
, idx
< ctx
->ir
->ninputs
);
3313 ctx
->ir
->inputs
[idx
] = create_input(ctx
, idx
);
3316 compile_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
3319 if (so
->inputs
[n
].bary
|| (ctx
->so
->type
== SHADER_VERTEX
)) {
3320 so
->total_in
+= ncomp
;
3325 setup_output(struct ir3_context
*ctx
, nir_variable
*out
)
3327 struct ir3_shader_variant
*so
= ctx
->so
;
3328 unsigned array_len
= MAX2(glsl_get_length(out
->type
), 1);
3329 unsigned ncomp
= glsl_get_components(out
->type
);
3330 unsigned n
= out
->data
.driver_location
;
3331 unsigned slot
= out
->data
.location
;
3334 DBG("; out: slot=%u, len=%ux%u, drvloc=%u",
3335 slot
, array_len
, ncomp
, n
);
3337 /* let's pretend things other than vec4 don't exist: */
3338 ncomp
= MAX2(ncomp
, 4);
3339 compile_assert(ctx
, ncomp
== 4);
3341 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
3343 case FRAG_RESULT_DEPTH
:
3344 comp
= 2; /* tgsi will write to .z component */
3345 so
->writes_pos
= true;
3347 case FRAG_RESULT_COLOR
:
3351 if (slot
>= FRAG_RESULT_DATA0
)
3353 compile_error(ctx
, "unknown FS output name: %s\n",
3354 gl_frag_result_name(slot
));
3356 } else if (ctx
->so
->type
== SHADER_VERTEX
) {
3358 case VARYING_SLOT_POS
:
3359 so
->writes_pos
= true;
3361 case VARYING_SLOT_PSIZ
:
3362 so
->writes_psize
= true;
3364 case VARYING_SLOT_COL0
:
3365 case VARYING_SLOT_COL1
:
3366 case VARYING_SLOT_BFC0
:
3367 case VARYING_SLOT_BFC1
:
3368 case VARYING_SLOT_FOGC
:
3369 case VARYING_SLOT_CLIP_DIST0
:
3370 case VARYING_SLOT_CLIP_DIST1
:
3371 case VARYING_SLOT_CLIP_VERTEX
:
3374 if (slot
>= VARYING_SLOT_VAR0
)
3376 if ((VARYING_SLOT_TEX0
<= slot
) && (slot
<= VARYING_SLOT_TEX7
))
3378 compile_error(ctx
, "unknown VS output name: %s\n",
3379 gl_varying_slot_name(slot
));
3382 compile_error(ctx
, "unknown shader type: %d\n", ctx
->so
->type
);
3385 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
3387 so
->outputs
[n
].slot
= slot
;
3388 so
->outputs
[n
].regid
= regid(n
, comp
);
3389 so
->outputs_count
= MAX2(so
->outputs_count
, n
+ 1);
3391 for (int i
= 0; i
< ncomp
; i
++) {
3392 unsigned idx
= (n
* 4) + i
;
3393 compile_assert(ctx
, idx
< ctx
->ir
->noutputs
);
3394 ctx
->ir
->outputs
[idx
] = create_immed(ctx
->block
, fui(0.0));
3399 max_drvloc(struct exec_list
*vars
)
3402 nir_foreach_variable(var
, vars
) {
3403 drvloc
= MAX2(drvloc
, (int)var
->data
.driver_location
);
3408 static const unsigned max_sysvals
[SHADER_MAX
] = {
3409 [SHADER_FRAGMENT
] = 8,
3410 [SHADER_VERTEX
] = 16,
3411 [SHADER_COMPUTE
] = 16, // TODO how many do we actually need?
3415 emit_instructions(struct ir3_context
*ctx
)
3417 unsigned ninputs
, noutputs
;
3418 nir_function_impl
*fxn
= nir_shader_get_entrypoint(ctx
->s
);
3420 ninputs
= (max_drvloc(&ctx
->s
->inputs
) + 1) * 4;
3421 noutputs
= (max_drvloc(&ctx
->s
->outputs
) + 1) * 4;
3423 /* we need to leave room for sysvals:
3425 ninputs
+= max_sysvals
[ctx
->so
->type
];
3427 ctx
->ir
= ir3_create(ctx
->compiler
, ninputs
, noutputs
);
3429 /* Create inputs in first block: */
3430 ctx
->block
= get_block(ctx
, nir_start_block(fxn
));
3431 ctx
->in_block
= ctx
->block
;
3432 list_addtail(&ctx
->block
->node
, &ctx
->ir
->block_list
);
3434 ninputs
-= max_sysvals
[ctx
->so
->type
];
3436 /* for fragment shader, we have a single input register (usually
3437 * r0.xy) which is used as the base for bary.f varying fetch instrs:
3439 if (ctx
->so
->type
== SHADER_FRAGMENT
) {
3440 // TODO maybe a helper for fi since we need it a few places..
3441 struct ir3_instruction
*instr
;
3442 instr
= ir3_instr_create(ctx
->block
, OPC_META_FI
);
3443 ir3_reg_create(instr
, 0, 0);
3444 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.x */
3445 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* r0.y */
3446 ctx
->frag_vcoord
= instr
;
3450 nir_foreach_variable(var
, &ctx
->s
->inputs
) {
3451 setup_input(ctx
, var
);
3454 /* Setup outputs: */
3455 nir_foreach_variable(var
, &ctx
->s
->outputs
) {
3456 setup_output(ctx
, var
);
3459 /* Setup registers (which should only be arrays): */
3460 nir_foreach_register(reg
, &ctx
->s
->registers
) {
3461 declare_array(ctx
, reg
);
3464 /* NOTE: need to do something more clever when we support >1 fxn */
3465 nir_foreach_register(reg
, &fxn
->registers
) {
3466 declare_array(ctx
, reg
);
3468 /* And emit the body: */
3470 emit_function(ctx
, fxn
);
3473 /* from NIR perspective, we actually have inputs. But most of the "inputs"
3474 * for a fragment shader are just bary.f instructions. The *actual* inputs
3475 * from the hw perspective are the frag_vcoord and optionally frag_coord and
3479 fixup_frag_inputs(struct ir3_context
*ctx
)
3481 struct ir3_shader_variant
*so
= ctx
->so
;
3482 struct ir3
*ir
= ctx
->ir
;
3483 struct ir3_instruction
**inputs
;
3484 struct ir3_instruction
*instr
;
3489 n
= 4; /* always have frag_vcoord */
3490 n
+= COND(so
->frag_face
, 4);
3491 n
+= COND(so
->frag_coord
, 4);
3493 inputs
= ir3_alloc(ctx
->ir
, n
* (sizeof(struct ir3_instruction
*)));
3495 if (so
->frag_face
) {
3496 /* this ultimately gets assigned to hr0.x so doesn't conflict
3497 * with frag_coord/frag_vcoord..
3499 inputs
[ir
->ninputs
++] = ctx
->frag_face
;
3500 ctx
->frag_face
->regs
[0]->num
= 0;
3502 /* remaining channels not used, but let's avoid confusing
3503 * other parts that expect inputs to come in groups of vec4
3505 inputs
[ir
->ninputs
++] = NULL
;
3506 inputs
[ir
->ninputs
++] = NULL
;
3507 inputs
[ir
->ninputs
++] = NULL
;
3510 /* since we don't know where to set the regid for frag_coord,
3511 * we have to use r0.x for it. But we don't want to *always*
3512 * use r1.x for frag_vcoord as that could increase the register
3513 * footprint on simple shaders:
3515 if (so
->frag_coord
) {
3516 ctx
->frag_coord
[0]->regs
[0]->num
= regid
++;
3517 ctx
->frag_coord
[1]->regs
[0]->num
= regid
++;
3518 ctx
->frag_coord
[2]->regs
[0]->num
= regid
++;
3519 ctx
->frag_coord
[3]->regs
[0]->num
= regid
++;
3521 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[0];
3522 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[1];
3523 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[2];
3524 inputs
[ir
->ninputs
++] = ctx
->frag_coord
[3];
3527 /* we always have frag_vcoord: */
3528 so
->pos_regid
= regid
;
3531 instr
= create_input(ctx
, ir
->ninputs
);
3532 instr
->regs
[0]->num
= regid
++;
3533 inputs
[ir
->ninputs
++] = instr
;
3534 ctx
->frag_vcoord
->regs
[1]->instr
= instr
;
3537 instr
= create_input(ctx
, ir
->ninputs
);
3538 instr
->regs
[0]->num
= regid
++;
3539 inputs
[ir
->ninputs
++] = instr
;
3540 ctx
->frag_vcoord
->regs
[2]->instr
= instr
;
3542 ir
->inputs
= inputs
;
3545 /* Fixup tex sampler state for astc/srgb workaround instructions. We
3546 * need to assign the tex state indexes for these after we know the
3550 fixup_astc_srgb(struct ir3_context
*ctx
)
3552 struct ir3_shader_variant
*so
= ctx
->so
;
3553 /* indexed by original tex idx, value is newly assigned alpha sampler
3554 * state tex idx. Zero is invalid since there is at least one sampler
3557 unsigned alt_tex_state
[16] = {0};
3558 unsigned tex_idx
= ctx
->max_texture_index
+ 1;
3561 so
->astc_srgb
.base
= tex_idx
;
3563 for (unsigned i
= 0; i
< ctx
->ir
->astc_srgb_count
; i
++) {
3564 struct ir3_instruction
*sam
= ctx
->ir
->astc_srgb
[i
];
3566 compile_assert(ctx
, sam
->cat5
.tex
< ARRAY_SIZE(alt_tex_state
));
3568 if (alt_tex_state
[sam
->cat5
.tex
] == 0) {
3569 /* assign new alternate/alpha tex state slot: */
3570 alt_tex_state
[sam
->cat5
.tex
] = tex_idx
++;
3571 so
->astc_srgb
.orig_idx
[idx
++] = sam
->cat5
.tex
;
3572 so
->astc_srgb
.count
++;
3575 sam
->cat5
.tex
= alt_tex_state
[sam
->cat5
.tex
];
3580 ir3_compile_shader_nir(struct ir3_compiler
*compiler
,
3581 struct ir3_shader_variant
*so
)
3583 struct ir3_context
*ctx
;
3585 struct ir3_instruction
**inputs
;
3586 unsigned i
, j
, actual_in
, inloc
;
3587 int ret
= 0, max_bary
;
3591 ctx
= compile_init(compiler
, so
);
3593 DBG("INIT failed!");
3598 emit_instructions(ctx
);
3601 DBG("EMIT failed!");
3606 ir
= so
->ir
= ctx
->ir
;
3608 /* keep track of the inputs from TGSI perspective.. */
3609 inputs
= ir
->inputs
;
3611 /* but fixup actual inputs for frag shader: */
3612 if (so
->type
== SHADER_FRAGMENT
)
3613 fixup_frag_inputs(ctx
);
3615 /* at this point, for binning pass, throw away unneeded outputs: */
3616 if (so
->key
.binning_pass
) {
3617 for (i
= 0, j
= 0; i
< so
->outputs_count
; i
++) {
3618 unsigned slot
= so
->outputs
[i
].slot
;
3620 /* throw away everything but first position/psize */
3621 if ((slot
== VARYING_SLOT_POS
) || (slot
== VARYING_SLOT_PSIZ
)) {
3623 so
->outputs
[j
] = so
->outputs
[i
];
3624 ir
->outputs
[(j
*4)+0] = ir
->outputs
[(i
*4)+0];
3625 ir
->outputs
[(j
*4)+1] = ir
->outputs
[(i
*4)+1];
3626 ir
->outputs
[(j
*4)+2] = ir
->outputs
[(i
*4)+2];
3627 ir
->outputs
[(j
*4)+3] = ir
->outputs
[(i
*4)+3];
3632 so
->outputs_count
= j
;
3633 ir
->noutputs
= j
* 4;
3636 /* if we want half-precision outputs, mark the output registers
3639 if (so
->key
.half_precision
) {
3640 for (i
= 0; i
< ir
->noutputs
; i
++) {
3641 struct ir3_instruction
*out
= ir
->outputs
[i
];
3646 /* if frag shader writes z, that needs to be full precision: */
3647 if (so
->outputs
[i
/4].slot
== FRAG_RESULT_DEPTH
)
3650 out
->regs
[0]->flags
|= IR3_REG_HALF
;
3651 /* output could be a fanout (ie. texture fetch output)
3652 * in which case we need to propagate the half-reg flag
3653 * up to the definer so that RA sees it:
3655 if (out
->opc
== OPC_META_FO
) {
3656 out
= out
->regs
[1]->instr
;
3657 out
->regs
[0]->flags
|= IR3_REG_HALF
;
3660 if (out
->opc
== OPC_MOV
) {
3661 out
->cat1
.dst_type
= half_type(out
->cat1
.dst_type
);
3666 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3667 printf("BEFORE CP:\n");
3673 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3674 printf("BEFORE GROUPING:\n");
3678 ir3_sched_add_deps(ir
);
3680 /* Group left/right neighbors, inserting mov's where needed to
3685 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3686 printf("AFTER GROUPING:\n");
3692 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3693 printf("AFTER DEPTH:\n");
3697 ret
= ir3_sched(ir
);
3699 DBG("SCHED failed!");
3703 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3704 printf("AFTER SCHED:\n");
3708 ret
= ir3_ra(ir
, so
->type
, so
->frag_coord
, so
->frag_face
);
3714 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3715 printf("AFTER RA:\n");
3719 /* fixup input/outputs: */
3720 for (i
= 0; i
< so
->outputs_count
; i
++) {
3721 so
->outputs
[i
].regid
= ir
->outputs
[i
*4]->regs
[0]->num
;
3724 /* Note that some or all channels of an input may be unused: */
3727 for (i
= 0; i
< so
->inputs_count
; i
++) {
3728 unsigned j
, reg
= regid(63,0), compmask
= 0, maxcomp
= 0;
3729 so
->inputs
[i
].ncomp
= 0;
3730 so
->inputs
[i
].inloc
= inloc
;
3731 for (j
= 0; j
< 4; j
++) {
3732 struct ir3_instruction
*in
= inputs
[(i
*4) + j
];
3733 if (in
&& !(in
->flags
& IR3_INSTR_UNUSED
)) {
3734 compmask
|= (1 << j
);
3735 reg
= in
->regs
[0]->num
- j
;
3737 so
->inputs
[i
].ncomp
++;
3738 if ((so
->type
== SHADER_FRAGMENT
) && so
->inputs
[i
].bary
) {
3740 assert(in
->regs
[1]->flags
& IR3_REG_IMMED
);
3741 in
->regs
[1]->iim_val
= inloc
+ j
;
3746 if ((so
->type
== SHADER_FRAGMENT
) && compmask
&& so
->inputs
[i
].bary
) {
3748 so
->inputs
[i
].compmask
= (1 << maxcomp
) - 1;
3750 } else if (!so
->inputs
[i
].sysval
) {
3751 so
->inputs
[i
].compmask
= compmask
;
3753 so
->inputs
[i
].regid
= reg
;
3757 fixup_astc_srgb(ctx
);
3759 /* We need to do legalize after (for frag shader's) the "bary.f"
3760 * offsets (inloc) have been assigned.
3762 ir3_legalize(ir
, &so
->has_samp
, &so
->has_ssbo
, &max_bary
);
3764 if (fd_mesa_debug
& FD_DBG_OPTMSGS
) {
3765 printf("AFTER LEGALIZE:\n");
3769 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
3770 if (so
->type
== SHADER_VERTEX
)
3771 so
->total_in
= actual_in
;
3773 so
->total_in
= max_bary
+ 1;
3778 ir3_destroy(so
->ir
);