2 * Copyright © 2016 Broadcom
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "util/u_format.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/ralloc.h"
29 #include "util/hash_table.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_builder.h"
32 #include "common/v3d_device_info.h"
33 #include "v3d_compiler.h"
35 #define GENERAL_TMU_LOOKUP_PER_QUAD (0 << 7)
36 #define GENERAL_TMU_LOOKUP_PER_PIXEL (1 << 7)
37 #define GENERAL_TMU_READ_OP_PREFETCH (0 << 3)
38 #define GENERAL_TMU_READ_OP_CACHE_CLEAR (1 << 3)
39 #define GENERAL_TMU_READ_OP_CACHE_FLUSH (3 << 3)
40 #define GENERAL_TMU_READ_OP_CACHE_CLEAN (3 << 3)
41 #define GENERAL_TMU_READ_OP_CACHE_L1T_CLEAR (4 << 3)
42 #define GENERAL_TMU_READ_OP_CACHE_L1T_FLUSH_AGGREGATION (5 << 3)
43 #define GENERAL_TMU_READ_OP_ATOMIC_INC (8 << 3)
44 #define GENERAL_TMU_READ_OP_ATOMIC_DEC (9 << 3)
45 #define GENERAL_TMU_READ_OP_ATOMIC_NOT (10 << 3)
46 #define GENERAL_TMU_READ_OP_READ (15 << 3)
47 #define GENERAL_TMU_LOOKUP_TYPE_8BIT_I (0 << 0)
48 #define GENERAL_TMU_LOOKUP_TYPE_16BIT_I (1 << 0)
49 #define GENERAL_TMU_LOOKUP_TYPE_VEC2 (2 << 0)
50 #define GENERAL_TMU_LOOKUP_TYPE_VEC3 (3 << 0)
51 #define GENERAL_TMU_LOOKUP_TYPE_VEC4 (4 << 0)
52 #define GENERAL_TMU_LOOKUP_TYPE_8BIT_UI (5 << 0)
53 #define GENERAL_TMU_LOOKUP_TYPE_16BIT_UI (6 << 0)
54 #define GENERAL_TMU_LOOKUP_TYPE_32BIT_UI (7 << 0)
56 #define GENERAL_TMU_WRITE_OP_ATOMIC_ADD_WRAP (0 << 3)
57 #define GENERAL_TMU_WRITE_OP_ATOMIC_SUB_WRAP (1 << 3)
58 #define GENERAL_TMU_WRITE_OP_ATOMIC_XCHG (2 << 3)
59 #define GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG (3 << 3)
60 #define GENERAL_TMU_WRITE_OP_ATOMIC_UMIN (4 << 3)
61 #define GENERAL_TMU_WRITE_OP_ATOMIC_UMAX (5 << 3)
62 #define GENERAL_TMU_WRITE_OP_ATOMIC_SMIN (6 << 3)
63 #define GENERAL_TMU_WRITE_OP_ATOMIC_SMAX (7 << 3)
64 #define GENERAL_TMU_WRITE_OP_ATOMIC_AND (8 << 3)
65 #define GENERAL_TMU_WRITE_OP_ATOMIC_OR (9 << 3)
66 #define GENERAL_TMU_WRITE_OP_ATOMIC_XOR (10 << 3)
67 #define GENERAL_TMU_WRITE_OP_WRITE (15 << 3)
69 #define V3D_TSY_SET_QUORUM 0
70 #define V3D_TSY_INC_WAITERS 1
71 #define V3D_TSY_DEC_WAITERS 2
72 #define V3D_TSY_INC_QUORUM 3
73 #define V3D_TSY_DEC_QUORUM 4
74 #define V3D_TSY_FREE_ALL 5
75 #define V3D_TSY_RELEASE 6
76 #define V3D_TSY_ACQUIRE 7
77 #define V3D_TSY_WAIT 8
78 #define V3D_TSY_WAIT_INC 9
79 #define V3D_TSY_WAIT_CHECK 10
80 #define V3D_TSY_WAIT_INC_CHECK 11
81 #define V3D_TSY_WAIT_CV 12
82 #define V3D_TSY_INC_SEMAPHORE 13
83 #define V3D_TSY_DEC_SEMAPHORE 14
84 #define V3D_TSY_SET_QUORUM_FREE_ALL 15
87 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
90 resize_qreg_array(struct v3d_compile
*c
,
95 if (*size
>= decl_size
)
98 uint32_t old_size
= *size
;
99 *size
= MAX2(*size
* 2, decl_size
);
100 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
102 fprintf(stderr
, "Malloc failure\n");
106 for (uint32_t i
= old_size
; i
< *size
; i
++)
107 (*regs
)[i
] = c
->undef
;
111 vir_emit_thrsw(struct v3d_compile
*c
)
116 /* Always thread switch after each texture operation for now.
118 * We could do better by batching a bunch of texture fetches up and
119 * then doing one thread switch and collecting all their results
122 c
->last_thrsw
= vir_NOP(c
);
123 c
->last_thrsw
->qpu
.sig
.thrsw
= true;
124 c
->last_thrsw_at_top_level
= !c
->in_control_flow
;
128 v3d_general_tmu_op(nir_intrinsic_instr
*instr
)
130 switch (instr
->intrinsic
) {
131 case nir_intrinsic_load_ssbo
:
132 case nir_intrinsic_load_ubo
:
133 case nir_intrinsic_load_uniform
:
134 case nir_intrinsic_load_shared
:
135 return GENERAL_TMU_READ_OP_READ
;
136 case nir_intrinsic_store_ssbo
:
137 case nir_intrinsic_store_shared
:
138 return GENERAL_TMU_WRITE_OP_WRITE
;
139 case nir_intrinsic_ssbo_atomic_add
:
140 case nir_intrinsic_shared_atomic_add
:
141 return GENERAL_TMU_WRITE_OP_ATOMIC_ADD_WRAP
;
142 case nir_intrinsic_ssbo_atomic_imin
:
143 case nir_intrinsic_shared_atomic_imin
:
144 return GENERAL_TMU_WRITE_OP_ATOMIC_SMIN
;
145 case nir_intrinsic_ssbo_atomic_umin
:
146 case nir_intrinsic_shared_atomic_umin
:
147 return GENERAL_TMU_WRITE_OP_ATOMIC_UMIN
;
148 case nir_intrinsic_ssbo_atomic_imax
:
149 case nir_intrinsic_shared_atomic_imax
:
150 return GENERAL_TMU_WRITE_OP_ATOMIC_SMAX
;
151 case nir_intrinsic_ssbo_atomic_umax
:
152 case nir_intrinsic_shared_atomic_umax
:
153 return GENERAL_TMU_WRITE_OP_ATOMIC_UMAX
;
154 case nir_intrinsic_ssbo_atomic_and
:
155 case nir_intrinsic_shared_atomic_and
:
156 return GENERAL_TMU_WRITE_OP_ATOMIC_AND
;
157 case nir_intrinsic_ssbo_atomic_or
:
158 case nir_intrinsic_shared_atomic_or
:
159 return GENERAL_TMU_WRITE_OP_ATOMIC_OR
;
160 case nir_intrinsic_ssbo_atomic_xor
:
161 case nir_intrinsic_shared_atomic_xor
:
162 return GENERAL_TMU_WRITE_OP_ATOMIC_XOR
;
163 case nir_intrinsic_ssbo_atomic_exchange
:
164 case nir_intrinsic_shared_atomic_exchange
:
165 return GENERAL_TMU_WRITE_OP_ATOMIC_XCHG
;
166 case nir_intrinsic_ssbo_atomic_comp_swap
:
167 case nir_intrinsic_shared_atomic_comp_swap
:
168 return GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG
;
170 unreachable("unknown intrinsic op");
175 * Implements indirect uniform loads and SSBO accesses through the TMU general
176 * memory access interface.
179 ntq_emit_tmu_general(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
,
182 /* XXX perf: We should turn add/sub of 1 to inc/dec. Perhaps NIR
183 * wants to have support for inc/dec?
186 uint32_t tmu_op
= v3d_general_tmu_op(instr
);
187 bool is_store
= (instr
->intrinsic
== nir_intrinsic_store_ssbo
||
188 instr
->intrinsic
== nir_intrinsic_store_shared
);
189 bool has_index
= !is_shared
;
192 int tmu_writes
= 1; /* address */
193 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
195 } else if (instr
->intrinsic
== nir_intrinsic_load_ssbo
||
196 instr
->intrinsic
== nir_intrinsic_load_ubo
||
197 instr
->intrinsic
== nir_intrinsic_load_shared
) {
198 offset_src
= 0 + has_index
;
199 } else if (is_store
) {
200 offset_src
= 1 + has_index
;
201 for (int i
= 0; i
< instr
->num_components
; i
++) {
203 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
204 ntq_get_src(c
, instr
->src
[0], i
));
208 offset_src
= 0 + has_index
;
210 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
211 ntq_get_src(c
, instr
->src
[1 + has_index
], 0));
213 if (tmu_op
== GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG
) {
215 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
216 ntq_get_src(c
, instr
->src
[2 + has_index
],
222 uint32_t const_offset
= 0;
223 if (nir_src_is_const(instr
->src
[offset_src
]))
224 const_offset
= nir_src_as_uint(instr
->src
[offset_src
]);
226 /* Make sure we won't exceed the 16-entry TMU fifo if each thread is
227 * storing at the same time.
229 while (tmu_writes
> 16 / c
->threads
)
233 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
234 const_offset
+= nir_intrinsic_base(instr
);
235 offset
= vir_uniform(c
, QUNIFORM_UBO_ADDR
,
236 v3d_unit_data_create(0, const_offset
));
238 } else if (instr
->intrinsic
== nir_intrinsic_load_ubo
) {
239 uint32_t index
= nir_src_as_uint(instr
->src
[0]) + 1;
240 /* Note that QUNIFORM_UBO_ADDR takes a UBO index shifted up by
241 * 1 (0 is gallium's constant buffer 0).
243 offset
= vir_uniform(c
, QUNIFORM_UBO_ADDR
,
244 v3d_unit_data_create(index
, const_offset
));
246 } else if (is_shared
) {
247 /* Shared variables have no buffer index, and all start from a
248 * common base that we set up at the start of dispatch
250 offset
= c
->cs_shared_offset
;
252 offset
= vir_uniform(c
, QUNIFORM_SSBO_OFFSET
,
253 nir_src_as_uint(instr
->src
[is_store
?
257 uint32_t config
= (0xffffff00 |
259 GENERAL_TMU_LOOKUP_PER_PIXEL
);
260 if (instr
->num_components
== 1) {
261 config
|= GENERAL_TMU_LOOKUP_TYPE_32BIT_UI
;
263 config
|= (GENERAL_TMU_LOOKUP_TYPE_VEC2
+
264 instr
->num_components
- 2);
267 if (vir_in_nonuniform_control_flow(c
)) {
268 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
274 dest
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUA
);
276 dest
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUAU
);
279 if (nir_src_is_const(instr
->src
[offset_src
]) && const_offset
== 0) {
280 tmu
= vir_MOV_dest(c
, dest
, offset
);
282 tmu
= vir_ADD_dest(c
, dest
,
284 ntq_get_src(c
, instr
->src
[offset_src
], 0));
288 tmu
->uniform
= vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
292 if (vir_in_nonuniform_control_flow(c
))
293 vir_set_cond(tmu
, V3D_QPU_COND_IFA
);
297 /* Read the result, or wait for the TMU op to complete. */
298 for (int i
= 0; i
< nir_intrinsic_dest_components(instr
); i
++)
299 ntq_store_dest(c
, &instr
->dest
, i
, vir_MOV(c
, vir_LDTMU(c
)));
301 if (nir_intrinsic_dest_components(instr
) == 0)
306 ntq_init_ssa_def(struct v3d_compile
*c
, nir_ssa_def
*def
)
308 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
309 def
->num_components
);
310 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
315 * This function is responsible for getting VIR results into the associated
316 * storage for a NIR instruction.
318 * If it's a NIR SSA def, then we just set the associated hash table entry to
321 * If it's a NIR reg, then we need to update the existing qreg assigned to the
322 * NIR destination with the incoming value. To do that without introducing
323 * new MOVs, we require that the incoming qreg either be a uniform, or be
324 * SSA-defined by the previous VIR instruction in the block and rewritable by
325 * this function. That lets us sneak ahead and insert the SF flag beforehand
326 * (knowing that the previous instruction doesn't depend on flags) and rewrite
327 * its destination to be the NIR reg's destination
330 ntq_store_dest(struct v3d_compile
*c
, nir_dest
*dest
, int chan
,
333 struct qinst
*last_inst
= NULL
;
334 if (!list_empty(&c
->cur_block
->instructions
))
335 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
337 assert((result
.file
== QFILE_TEMP
&&
338 last_inst
&& last_inst
== c
->defs
[result
.index
]));
341 assert(chan
< dest
->ssa
.num_components
);
344 struct hash_entry
*entry
=
345 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
350 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
352 qregs
[chan
] = result
;
354 nir_register
*reg
= dest
->reg
.reg
;
355 assert(dest
->reg
.base_offset
== 0);
356 assert(reg
->num_array_elems
== 0);
357 struct hash_entry
*entry
=
358 _mesa_hash_table_search(c
->def_ht
, reg
);
359 struct qreg
*qregs
= entry
->data
;
361 /* Insert a MOV if the source wasn't an SSA def in the
362 * previous instruction.
364 if ((vir_in_nonuniform_control_flow(c
) &&
365 c
->defs
[last_inst
->dst
.index
]->qpu
.sig
.ldunif
)) {
366 result
= vir_MOV(c
, result
);
367 last_inst
= c
->defs
[result
.index
];
370 /* We know they're both temps, so just rewrite index. */
371 c
->defs
[last_inst
->dst
.index
] = NULL
;
372 last_inst
->dst
.index
= qregs
[chan
].index
;
374 /* If we're in control flow, then make this update of the reg
375 * conditional on the execution mask.
377 if (vir_in_nonuniform_control_flow(c
)) {
378 last_inst
->dst
.index
= qregs
[chan
].index
;
380 /* Set the flags to the current exec mask.
382 c
->cursor
= vir_before_inst(last_inst
);
383 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
385 c
->cursor
= vir_after_inst(last_inst
);
387 vir_set_cond(last_inst
, V3D_QPU_COND_IFA
);
393 ntq_get_src(struct v3d_compile
*c
, nir_src src
, int i
)
395 struct hash_entry
*entry
;
397 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
398 assert(i
< src
.ssa
->num_components
);
400 nir_register
*reg
= src
.reg
.reg
;
401 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
402 assert(reg
->num_array_elems
== 0);
403 assert(src
.reg
.base_offset
== 0);
404 assert(i
< reg
->num_components
);
407 struct qreg
*qregs
= entry
->data
;
412 ntq_get_alu_src(struct v3d_compile
*c
, nir_alu_instr
*instr
,
415 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
416 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
417 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
418 instr
->src
[src
].swizzle
[chan
]);
420 assert(!instr
->src
[src
].abs
);
421 assert(!instr
->src
[src
].negate
);
427 ntq_minify(struct v3d_compile
*c
, struct qreg size
, struct qreg level
)
429 return vir_MAX(c
, vir_SHR(c
, size
, level
), vir_uniform_ui(c
, 1));
433 ntq_emit_txs(struct v3d_compile
*c
, nir_tex_instr
*instr
)
435 unsigned unit
= instr
->texture_index
;
436 int lod_index
= nir_tex_instr_src_index(instr
, nir_tex_src_lod
);
437 int dest_size
= nir_tex_instr_dest_size(instr
);
439 struct qreg lod
= c
->undef
;
441 lod
= ntq_get_src(c
, instr
->src
[lod_index
].src
, 0);
443 for (int i
= 0; i
< dest_size
; i
++) {
445 enum quniform_contents contents
;
447 if (instr
->is_array
&& i
== dest_size
- 1)
448 contents
= QUNIFORM_TEXTURE_ARRAY_SIZE
;
450 contents
= QUNIFORM_TEXTURE_WIDTH
+ i
;
452 struct qreg size
= vir_uniform(c
, contents
, unit
);
454 switch (instr
->sampler_dim
) {
455 case GLSL_SAMPLER_DIM_1D
:
456 case GLSL_SAMPLER_DIM_2D
:
457 case GLSL_SAMPLER_DIM_MS
:
458 case GLSL_SAMPLER_DIM_3D
:
459 case GLSL_SAMPLER_DIM_CUBE
:
460 /* Don't minify the array size. */
461 if (!(instr
->is_array
&& i
== dest_size
- 1)) {
462 size
= ntq_minify(c
, size
, lod
);
466 case GLSL_SAMPLER_DIM_RECT
:
467 /* There's no LOD field for rects */
471 unreachable("Bad sampler type");
474 ntq_store_dest(c
, &instr
->dest
, i
, size
);
479 ntq_emit_tex(struct v3d_compile
*c
, nir_tex_instr
*instr
)
481 unsigned unit
= instr
->texture_index
;
483 /* Since each texture sampling op requires uploading uniforms to
484 * reference the texture, there's no HW support for texture size and
485 * you just upload uniforms containing the size.
488 case nir_texop_query_levels
:
489 ntq_store_dest(c
, &instr
->dest
, 0,
490 vir_uniform(c
, QUNIFORM_TEXTURE_LEVELS
, unit
));
493 ntq_emit_txs(c
, instr
);
499 if (c
->devinfo
->ver
>= 40)
500 v3d40_vir_emit_tex(c
, instr
);
502 v3d33_vir_emit_tex(c
, instr
);
506 ntq_fsincos(struct v3d_compile
*c
, struct qreg src
, bool is_cos
)
508 struct qreg input
= vir_FMUL(c
, src
, vir_uniform_f(c
, 1.0f
/ M_PI
));
510 input
= vir_FADD(c
, input
, vir_uniform_f(c
, 0.5));
512 struct qreg periods
= vir_FROUND(c
, input
);
513 struct qreg sin_output
= vir_SIN(c
, vir_FSUB(c
, input
, periods
));
514 return vir_XOR(c
, sin_output
, vir_SHL(c
,
515 vir_FTOIN(c
, periods
),
516 vir_uniform_ui(c
, -1)));
520 ntq_fsign(struct v3d_compile
*c
, struct qreg src
)
522 struct qreg t
= vir_get_temp(c
);
524 vir_MOV_dest(c
, t
, vir_uniform_f(c
, 0.0));
525 vir_set_pf(vir_FMOV_dest(c
, vir_nop_reg(), src
), V3D_QPU_PF_PUSHZ
);
526 vir_MOV_cond(c
, V3D_QPU_COND_IFNA
, t
, vir_uniform_f(c
, 1.0));
527 vir_set_pf(vir_FMOV_dest(c
, vir_nop_reg(), src
), V3D_QPU_PF_PUSHN
);
528 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, t
, vir_uniform_f(c
, -1.0));
529 return vir_MOV(c
, t
);
533 emit_fragcoord_input(struct v3d_compile
*c
, int attr
)
535 c
->inputs
[attr
* 4 + 0] = vir_FXCD(c
);
536 c
->inputs
[attr
* 4 + 1] = vir_FYCD(c
);
537 c
->inputs
[attr
* 4 + 2] = c
->payload_z
;
538 c
->inputs
[attr
* 4 + 3] = vir_RECIP(c
, c
->payload_w
);
542 emit_fragment_varying(struct v3d_compile
*c
, nir_variable
*var
,
543 uint8_t swizzle
, int array_index
)
545 struct qreg r3
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R3
);
546 struct qreg r5
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R5
);
549 if (c
->devinfo
->ver
>= 41) {
550 struct qinst
*ldvary
= vir_add_inst(V3D_QPU_A_NOP
, c
->undef
,
552 ldvary
->qpu
.sig
.ldvary
= true;
553 vary
= vir_emit_def(c
, ldvary
);
555 vir_NOP(c
)->qpu
.sig
.ldvary
= true;
559 /* For gl_PointCoord input or distance along a line, we'll be called
560 * with no nir_variable, and we don't count toward VPM size so we
561 * don't track an input slot.
564 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
567 int i
= c
->num_inputs
++;
569 v3d_slot_from_slot_and_component(var
->data
.location
+
570 array_index
, swizzle
);
572 switch (var
->data
.interpolation
) {
573 case INTERP_MODE_NONE
:
574 /* If a gl_FrontColor or gl_BackColor input has no interp
575 * qualifier, then if we're using glShadeModel(GL_FLAT) it
576 * needs to be flat shaded.
578 switch (var
->data
.location
+ array_index
) {
579 case VARYING_SLOT_COL0
:
580 case VARYING_SLOT_COL1
:
581 case VARYING_SLOT_BFC0
:
582 case VARYING_SLOT_BFC1
:
583 if (c
->fs_key
->shade_model_flat
) {
584 BITSET_SET(c
->flat_shade_flags
, i
);
585 vir_MOV_dest(c
, c
->undef
, vary
);
586 return vir_MOV(c
, r5
);
588 return vir_FADD(c
, vir_FMUL(c
, vary
,
595 case INTERP_MODE_SMOOTH
:
596 if (var
->data
.centroid
) {
597 BITSET_SET(c
->centroid_flags
, i
);
598 return vir_FADD(c
, vir_FMUL(c
, vary
,
599 c
->payload_w_centroid
), r5
);
601 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
603 case INTERP_MODE_NOPERSPECTIVE
:
604 BITSET_SET(c
->noperspective_flags
, i
);
605 return vir_FADD(c
, vir_MOV(c
, vary
), r5
);
606 case INTERP_MODE_FLAT
:
607 BITSET_SET(c
->flat_shade_flags
, i
);
608 vir_MOV_dest(c
, c
->undef
, vary
);
609 return vir_MOV(c
, r5
);
611 unreachable("Bad interp mode");
616 emit_fragment_input(struct v3d_compile
*c
, int attr
, nir_variable
*var
,
619 for (int i
= 0; i
< glsl_get_vector_elements(var
->type
); i
++) {
620 int chan
= var
->data
.location_frac
+ i
;
621 c
->inputs
[attr
* 4 + chan
] =
622 emit_fragment_varying(c
, var
, chan
, array_index
);
627 add_output(struct v3d_compile
*c
,
628 uint32_t decl_offset
,
632 uint32_t old_array_size
= c
->outputs_array_size
;
633 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
636 if (old_array_size
!= c
->outputs_array_size
) {
637 c
->output_slots
= reralloc(c
,
639 struct v3d_varying_slot
,
640 c
->outputs_array_size
);
643 c
->output_slots
[decl_offset
] =
644 v3d_slot_from_slot_and_component(slot
, swizzle
);
648 * If compare_instr is a valid comparison instruction, emits the
649 * compare_instr's comparison and returns the sel_instr's return value based
650 * on the compare_instr's result.
653 ntq_emit_comparison(struct v3d_compile
*c
,
654 nir_alu_instr
*compare_instr
,
655 enum v3d_qpu_cond
*out_cond
)
657 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
659 if (nir_op_infos
[compare_instr
->op
].num_inputs
> 1)
660 src1
= ntq_get_alu_src(c
, compare_instr
, 1);
661 bool cond_invert
= false;
662 struct qreg nop
= vir_nop_reg();
664 switch (compare_instr
->op
) {
667 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
670 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
675 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
679 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
685 vir_set_pf(vir_FCMP_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
688 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
692 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
698 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHN
);
701 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
704 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
708 vir_set_pf(vir_MOV_dest(c
, nop
, src0
), V3D_QPU_PF_PUSHZ
);
713 vir_set_pf(vir_FMOV_dest(c
, nop
, src0
), V3D_QPU_PF_PUSHZ
);
721 *out_cond
= cond_invert
? V3D_QPU_COND_IFNA
: V3D_QPU_COND_IFA
;
726 /* Finds an ALU instruction that generates our src value that could
727 * (potentially) be greedily emitted in the consuming instruction.
729 static struct nir_alu_instr
*
730 ntq_get_alu_parent(nir_src src
)
732 if (!src
.is_ssa
|| src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
734 nir_alu_instr
*instr
= nir_instr_as_alu(src
.ssa
->parent_instr
);
738 /* If the ALU instr's srcs are non-SSA, then we would have to avoid
739 * moving emission of the ALU instr down past another write of the
742 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
743 if (!instr
->src
[i
].src
.is_ssa
)
750 /* Turns a NIR bool into a condition code to predicate on. */
751 static enum v3d_qpu_cond
752 ntq_emit_bool_to_cond(struct v3d_compile
*c
, nir_src src
)
754 nir_alu_instr
*compare
= ntq_get_alu_parent(src
);
758 enum v3d_qpu_cond cond
;
759 if (ntq_emit_comparison(c
, compare
, &cond
))
763 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), ntq_get_src(c
, src
, 0)),
765 return V3D_QPU_COND_IFNA
;
769 ntq_emit_alu(struct v3d_compile
*c
, nir_alu_instr
*instr
)
771 /* This should always be lowered to ALU operations for V3D. */
772 assert(!instr
->dest
.saturate
);
774 /* Vectors are special in that they have non-scalarized writemasks,
775 * and just take the first swizzle channel for each argument in order
776 * into each writemask channel.
778 if (instr
->op
== nir_op_vec2
||
779 instr
->op
== nir_op_vec3
||
780 instr
->op
== nir_op_vec4
) {
782 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
783 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
784 instr
->src
[i
].swizzle
[0]);
785 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
786 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
787 vir_MOV(c
, srcs
[i
]));
791 /* General case: We can just grab the one used channel per src. */
792 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
793 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
794 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
802 result
= vir_MOV(c
, src
[0]);
806 result
= vir_XOR(c
, src
[0], vir_uniform_ui(c
, 1 << 31));
809 result
= vir_NEG(c
, src
[0]);
813 result
= vir_FMUL(c
, src
[0], src
[1]);
816 result
= vir_FADD(c
, src
[0], src
[1]);
819 result
= vir_FSUB(c
, src
[0], src
[1]);
822 result
= vir_FMIN(c
, src
[0], src
[1]);
825 result
= vir_FMAX(c
, src
[0], src
[1]);
829 nir_alu_instr
*src0_alu
= ntq_get_alu_parent(instr
->src
[0].src
);
830 if (src0_alu
&& src0_alu
->op
== nir_op_fround_even
) {
831 result
= vir_FTOIN(c
, ntq_get_alu_src(c
, src0_alu
, 0));
833 result
= vir_FTOIZ(c
, src
[0]);
839 result
= vir_FTOUZ(c
, src
[0]);
842 result
= vir_ITOF(c
, src
[0]);
845 result
= vir_UTOF(c
, src
[0]);
848 result
= vir_AND(c
, src
[0], vir_uniform_f(c
, 1.0));
851 result
= vir_AND(c
, src
[0], vir_uniform_ui(c
, 1));
855 result
= vir_ADD(c
, src
[0], src
[1]);
858 result
= vir_SHR(c
, src
[0], src
[1]);
861 result
= vir_SUB(c
, src
[0], src
[1]);
864 result
= vir_ASR(c
, src
[0], src
[1]);
867 result
= vir_SHL(c
, src
[0], src
[1]);
870 result
= vir_MIN(c
, src
[0], src
[1]);
873 result
= vir_UMIN(c
, src
[0], src
[1]);
876 result
= vir_MAX(c
, src
[0], src
[1]);
879 result
= vir_UMAX(c
, src
[0], src
[1]);
882 result
= vir_AND(c
, src
[0], src
[1]);
885 result
= vir_OR(c
, src
[0], src
[1]);
888 result
= vir_XOR(c
, src
[0], src
[1]);
891 result
= vir_NOT(c
, src
[0]);
894 case nir_op_ufind_msb
:
895 result
= vir_SUB(c
, vir_uniform_ui(c
, 31), vir_CLZ(c
, src
[0]));
899 result
= vir_UMUL(c
, src
[0], src
[1]);
906 enum v3d_qpu_cond cond
;
907 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
909 result
= vir_MOV(c
, vir_SEL(c
, cond
,
910 vir_uniform_f(c
, 1.0),
911 vir_uniform_f(c
, 0.0)));
927 enum v3d_qpu_cond cond
;
928 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
930 result
= vir_MOV(c
, vir_SEL(c
, cond
,
931 vir_uniform_ui(c
, ~0),
932 vir_uniform_ui(c
, 0)));
939 ntq_emit_bool_to_cond(c
, instr
->src
[0].src
),
944 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), src
[0]),
946 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFNA
,
951 result
= vir_RECIP(c
, src
[0]);
954 result
= vir_RSQRT(c
, src
[0]);
957 result
= vir_EXP(c
, src
[0]);
960 result
= vir_LOG(c
, src
[0]);
964 result
= vir_FCEIL(c
, src
[0]);
967 result
= vir_FFLOOR(c
, src
[0]);
969 case nir_op_fround_even
:
970 result
= vir_FROUND(c
, src
[0]);
973 result
= vir_FTRUNC(c
, src
[0]);
977 result
= ntq_fsincos(c
, src
[0], false);
980 result
= ntq_fsincos(c
, src
[0], true);
984 result
= ntq_fsign(c
, src
[0]);
988 result
= vir_FMOV(c
, src
[0]);
989 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_ABS
);
994 result
= vir_MAX(c
, src
[0], vir_NEG(c
, src
[0]));
998 case nir_op_fddx_coarse
:
999 case nir_op_fddx_fine
:
1000 result
= vir_FDX(c
, src
[0]);
1004 case nir_op_fddy_coarse
:
1005 case nir_op_fddy_fine
:
1006 result
= vir_FDY(c
, src
[0]);
1009 case nir_op_uadd_carry
:
1010 vir_set_pf(vir_ADD_dest(c
, vir_nop_reg(), src
[0], src
[1]),
1012 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1013 vir_uniform_ui(c
, ~0),
1014 vir_uniform_ui(c
, 0)));
1017 case nir_op_pack_half_2x16_split
:
1018 result
= vir_VFPACK(c
, src
[0], src
[1]);
1021 case nir_op_unpack_half_2x16_split_x
:
1022 result
= vir_FMOV(c
, src
[0]);
1023 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_L
);
1026 case nir_op_unpack_half_2x16_split_y
:
1027 result
= vir_FMOV(c
, src
[0]);
1028 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_H
);
1032 fprintf(stderr
, "unknown NIR ALU inst: ");
1033 nir_print_instr(&instr
->instr
, stderr
);
1034 fprintf(stderr
, "\n");
1038 /* We have a scalar result, so the instruction should only have a
1039 * single channel written to.
1041 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
1042 ntq_store_dest(c
, &instr
->dest
.dest
,
1043 ffs(instr
->dest
.write_mask
) - 1, result
);
1046 /* Each TLB read/write setup (a render target or depth buffer) takes an 8-bit
1047 * specifier. They come from a register that's preloaded with 0xffffffff
1048 * (0xff gets you normal vec4 f16 RT0 writes), and when one is neaded the low
1049 * 8 bits are shifted off the bottom and 0xff shifted in from the top.
1051 #define TLB_TYPE_F16_COLOR (3 << 6)
1052 #define TLB_TYPE_I32_COLOR (1 << 6)
1053 #define TLB_TYPE_F32_COLOR (0 << 6)
1054 #define TLB_RENDER_TARGET_SHIFT 3 /* Reversed! 7 = RT 0, 0 = RT 7. */
1055 #define TLB_SAMPLE_MODE_PER_SAMPLE (0 << 2)
1056 #define TLB_SAMPLE_MODE_PER_PIXEL (1 << 2)
1057 #define TLB_F16_SWAP_HI_LO (1 << 1)
1058 #define TLB_VEC_SIZE_4_F16 (1 << 0)
1059 #define TLB_VEC_SIZE_2_F16 (0 << 0)
1060 #define TLB_VEC_SIZE_MINUS_1_SHIFT 0
1062 /* Triggers Z/Stencil testing, used when the shader state's "FS modifies Z"
1065 #define TLB_TYPE_DEPTH ((2 << 6) | (0 << 4))
1066 #define TLB_DEPTH_TYPE_INVARIANT (0 << 2) /* Unmodified sideband input used */
1067 #define TLB_DEPTH_TYPE_PER_PIXEL (1 << 2) /* QPU result used */
1068 #define TLB_V42_DEPTH_TYPE_INVARIANT (0 << 3) /* Unmodified sideband input used */
1069 #define TLB_V42_DEPTH_TYPE_PER_PIXEL (1 << 3) /* QPU result used */
1071 /* Stencil is a single 32-bit write. */
1072 #define TLB_TYPE_STENCIL_ALPHA ((2 << 6) | (1 << 4))
1075 emit_frag_end(struct v3d_compile
*c
)
1078 if (c->output_sample_mask_index != -1) {
1079 vir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
1083 bool has_any_tlb_color_write
= false;
1084 for (int rt
= 0; rt
< V3D_MAX_DRAW_BUFFERS
; rt
++) {
1085 if (c
->fs_key
->cbufs
& (1 << rt
) && c
->output_color_var
[rt
])
1086 has_any_tlb_color_write
= true;
1089 if (c
->fs_key
->sample_alpha_to_coverage
&& c
->output_color_var
[0]) {
1090 struct nir_variable
*var
= c
->output_color_var
[0];
1091 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
1093 vir_SETMSF_dest(c
, vir_nop_reg(),
1096 vir_FTOC(c
, color
[3])));
1099 struct qreg tlb_reg
= vir_magic_reg(V3D_QPU_WADDR_TLB
);
1100 struct qreg tlbu_reg
= vir_magic_reg(V3D_QPU_WADDR_TLBU
);
1101 if (c
->output_position_index
!= -1) {
1102 struct qinst
*inst
= vir_MOV_dest(c
, tlbu_reg
,
1103 c
->outputs
[c
->output_position_index
]);
1104 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
1106 if (c
->devinfo
->ver
>= 42) {
1107 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_PER_PIXEL
|
1108 TLB_SAMPLE_MODE_PER_PIXEL
);
1110 tlb_specifier
|= TLB_DEPTH_TYPE_PER_PIXEL
;
1112 inst
->uniform
= vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
1116 } else if (c
->s
->info
.fs
.uses_discard
||
1117 !c
->s
->info
.fs
.early_fragment_tests
||
1118 c
->fs_key
->sample_alpha_to_coverage
||
1119 !has_any_tlb_color_write
) {
1120 /* Emit passthrough Z if it needed to be delayed until shader
1121 * end due to potential discards.
1123 * Since (single-threaded) fragment shaders always need a TLB
1124 * write, emit passthrouh Z if we didn't have any color
1125 * buffers and flag us as potentially discarding, so that we
1126 * can use Z as the TLB write.
1128 c
->s
->info
.fs
.uses_discard
= true;
1130 struct qinst
*inst
= vir_MOV_dest(c
, tlbu_reg
,
1132 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
1134 if (c
->devinfo
->ver
>= 42) {
1135 /* The spec says the PER_PIXEL flag is ignored for
1136 * invariant writes, but the simulator demands it.
1138 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_INVARIANT
|
1139 TLB_SAMPLE_MODE_PER_PIXEL
);
1141 tlb_specifier
|= TLB_DEPTH_TYPE_INVARIANT
;
1144 inst
->uniform
= vir_get_uniform_index(c
,
1151 /* XXX: Performance improvement: Merge Z write and color writes TLB
1155 for (int rt
= 0; rt
< V3D_MAX_DRAW_BUFFERS
; rt
++) {
1156 if (!(c
->fs_key
->cbufs
& (1 << rt
)) || !c
->output_color_var
[rt
])
1159 nir_variable
*var
= c
->output_color_var
[rt
];
1160 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
1161 int num_components
= glsl_get_vector_elements(var
->type
);
1162 uint32_t conf
= 0xffffff00;
1165 conf
|= TLB_SAMPLE_MODE_PER_PIXEL
;
1166 conf
|= (7 - rt
) << TLB_RENDER_TARGET_SHIFT
;
1168 if (c
->fs_key
->swap_color_rb
& (1 << rt
))
1169 num_components
= MAX2(num_components
, 3);
1171 assert(num_components
!= 0);
1172 switch (glsl_get_base_type(var
->type
)) {
1173 case GLSL_TYPE_UINT
:
1175 /* The F32 vs I32 distinction was dropped in 4.2. */
1176 if (c
->devinfo
->ver
< 42)
1177 conf
|= TLB_TYPE_I32_COLOR
;
1179 conf
|= TLB_TYPE_F32_COLOR
;
1180 conf
|= ((num_components
- 1) <<
1181 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1183 inst
= vir_MOV_dest(c
, tlbu_reg
, color
[0]);
1184 inst
->uniform
= vir_get_uniform_index(c
,
1188 for (int i
= 1; i
< num_components
; i
++) {
1189 inst
= vir_MOV_dest(c
, tlb_reg
, color
[i
]);
1194 struct qreg r
= color
[0];
1195 struct qreg g
= color
[1];
1196 struct qreg b
= color
[2];
1197 struct qreg a
= color
[3];
1199 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1200 conf
|= TLB_TYPE_F32_COLOR
;
1201 conf
|= ((num_components
- 1) <<
1202 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1204 conf
|= TLB_TYPE_F16_COLOR
;
1205 conf
|= TLB_F16_SWAP_HI_LO
;
1206 if (num_components
>= 3)
1207 conf
|= TLB_VEC_SIZE_4_F16
;
1209 conf
|= TLB_VEC_SIZE_2_F16
;
1212 if (c
->fs_key
->swap_color_rb
& (1 << rt
)) {
1217 if (c
->fs_key
->sample_alpha_to_one
)
1218 a
= vir_uniform_f(c
, 1.0);
1220 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1221 inst
= vir_MOV_dest(c
, tlbu_reg
, r
);
1222 inst
->uniform
= vir_get_uniform_index(c
,
1226 if (num_components
>= 2)
1227 vir_MOV_dest(c
, tlb_reg
, g
);
1228 if (num_components
>= 3)
1229 vir_MOV_dest(c
, tlb_reg
, b
);
1230 if (num_components
>= 4)
1231 vir_MOV_dest(c
, tlb_reg
, a
);
1233 inst
= vir_VFPACK_dest(c
, tlb_reg
, r
, g
);
1235 inst
->dst
= tlbu_reg
;
1236 inst
->uniform
= vir_get_uniform_index(c
,
1241 if (num_components
>= 3)
1242 inst
= vir_VFPACK_dest(c
, tlb_reg
, b
, a
);
1251 vir_VPM_WRITE(struct v3d_compile
*c
, struct qreg val
, uint32_t vpm_index
)
1253 if (c
->devinfo
->ver
>= 40) {
1254 vir_STVPMV(c
, vir_uniform_ui(c
, vpm_index
), val
);
1256 /* XXX: v3d33_vir_vpm_write_setup(c); */
1257 vir_MOV_dest(c
, vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_VPM
), val
);
1262 emit_vert_end(struct v3d_compile
*c
)
1264 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1266 if (c
->devinfo
->ver
>= 40 && c
->devinfo
->ver
<= 42)
1271 v3d_optimize_nir(struct nir_shader
*s
)
1278 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1279 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
);
1280 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
1281 NIR_PASS(progress
, s
, nir_copy_prop
);
1282 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1283 NIR_PASS(progress
, s
, nir_opt_dce
);
1284 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1285 NIR_PASS(progress
, s
, nir_opt_cse
);
1286 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
1287 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1288 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1289 NIR_PASS(progress
, s
, nir_opt_undef
);
1292 NIR_PASS(progress
, s
, nir_opt_move_load_ubo
);
1296 driver_location_compare(const void *in_a
, const void *in_b
)
1298 const nir_variable
*const *a
= in_a
;
1299 const nir_variable
*const *b
= in_b
;
1301 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1305 ntq_emit_vpm_read(struct v3d_compile
*c
,
1306 uint32_t *num_components_queued
,
1307 uint32_t *remaining
,
1310 struct qreg vpm
= vir_reg(QFILE_VPM
, vpm_index
);
1312 if (c
->devinfo
->ver
>= 40 ) {
1313 return vir_LDVPMV_IN(c
,
1315 (*num_components_queued
)++));
1318 if (*num_components_queued
!= 0) {
1319 (*num_components_queued
)--;
1320 return vir_MOV(c
, vpm
);
1323 uint32_t num_components
= MIN2(*remaining
, 32);
1325 v3d33_vir_vpm_read_setup(c
, num_components
);
1327 *num_components_queued
= num_components
- 1;
1328 *remaining
-= num_components
;
1330 return vir_MOV(c
, vpm
);
1334 ntq_setup_vpm_inputs(struct v3d_compile
*c
)
1336 /* Figure out how many components of each vertex attribute the shader
1337 * uses. Each variable should have been split to individual
1338 * components and unused ones DCEed. The vertex fetcher will load
1339 * from the start of the attribute to the number of components we
1340 * declare we need in c->vattr_sizes[].
1342 nir_foreach_variable(var
, &c
->s
->inputs
) {
1343 /* No VS attribute array support. */
1344 assert(MAX2(glsl_get_length(var
->type
), 1) == 1);
1346 unsigned loc
= var
->data
.driver_location
;
1347 int start_component
= var
->data
.location_frac
;
1348 int num_components
= glsl_get_components(var
->type
);
1350 c
->vattr_sizes
[loc
] = MAX2(c
->vattr_sizes
[loc
],
1351 start_component
+ num_components
);
1354 unsigned num_components
= 0;
1355 uint32_t vpm_components_queued
= 0;
1356 bool uses_iid
= c
->s
->info
.system_values_read
&
1357 (1ull << SYSTEM_VALUE_INSTANCE_ID
);
1358 bool uses_vid
= c
->s
->info
.system_values_read
&
1359 (1ull << SYSTEM_VALUE_VERTEX_ID
);
1360 num_components
+= uses_iid
;
1361 num_components
+= uses_vid
;
1363 for (int i
= 0; i
< ARRAY_SIZE(c
->vattr_sizes
); i
++)
1364 num_components
+= c
->vattr_sizes
[i
];
1367 c
->iid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1368 &num_components
, ~0);
1372 c
->vid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1373 &num_components
, ~0);
1376 /* The actual loads will happen directly in nir_intrinsic_load_input
1377 * on newer versions.
1379 if (c
->devinfo
->ver
>= 40)
1382 for (int loc
= 0; loc
< ARRAY_SIZE(c
->vattr_sizes
); loc
++) {
1383 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1386 for (int i
= 0; i
< c
->vattr_sizes
[loc
]; i
++) {
1387 c
->inputs
[loc
* 4 + i
] =
1388 ntq_emit_vpm_read(c
,
1389 &vpm_components_queued
,
1396 if (c
->devinfo
->ver
>= 40) {
1397 assert(vpm_components_queued
== num_components
);
1399 assert(vpm_components_queued
== 0);
1400 assert(num_components
== 0);
1405 ntq_setup_fs_inputs(struct v3d_compile
*c
)
1407 unsigned num_entries
= 0;
1408 unsigned num_components
= 0;
1409 nir_foreach_variable(var
, &c
->s
->inputs
) {
1411 num_components
+= glsl_get_components(var
->type
);
1414 nir_variable
*vars
[num_entries
];
1417 nir_foreach_variable(var
, &c
->s
->inputs
)
1420 /* Sort the variables so that we emit the input setup in
1421 * driver_location order. This is required for VPM reads, whose data
1422 * is fetched into the VPM in driver_location (TGSI register index)
1425 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1427 for (unsigned i
= 0; i
< num_entries
; i
++) {
1428 nir_variable
*var
= vars
[i
];
1429 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1430 unsigned loc
= var
->data
.driver_location
;
1432 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1433 (loc
+ array_len
) * 4);
1435 if (var
->data
.location
== VARYING_SLOT_POS
) {
1436 emit_fragcoord_input(c
, loc
);
1437 } else if (var
->data
.location
== VARYING_SLOT_PNTC
||
1438 (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1439 (c
->fs_key
->point_sprite_mask
&
1440 (1 << (var
->data
.location
-
1441 VARYING_SLOT_VAR0
))))) {
1442 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1443 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1445 for (int j
= 0; j
< array_len
; j
++)
1446 emit_fragment_input(c
, loc
+ j
, var
, j
);
1452 ntq_setup_outputs(struct v3d_compile
*c
)
1454 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
1457 nir_foreach_variable(var
, &c
->s
->outputs
) {
1458 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1459 unsigned loc
= var
->data
.driver_location
* 4;
1461 assert(array_len
== 1);
1464 for (int i
= 0; i
< 4 - var
->data
.location_frac
; i
++) {
1465 add_output(c
, loc
+ var
->data
.location_frac
+ i
,
1467 var
->data
.location_frac
+ i
);
1470 switch (var
->data
.location
) {
1471 case FRAG_RESULT_COLOR
:
1472 c
->output_color_var
[0] = var
;
1473 c
->output_color_var
[1] = var
;
1474 c
->output_color_var
[2] = var
;
1475 c
->output_color_var
[3] = var
;
1477 case FRAG_RESULT_DATA0
:
1478 case FRAG_RESULT_DATA1
:
1479 case FRAG_RESULT_DATA2
:
1480 case FRAG_RESULT_DATA3
:
1481 c
->output_color_var
[var
->data
.location
-
1482 FRAG_RESULT_DATA0
] = var
;
1484 case FRAG_RESULT_DEPTH
:
1485 c
->output_position_index
= loc
;
1487 case FRAG_RESULT_SAMPLE_MASK
:
1488 c
->output_sample_mask_index
= loc
;
1495 * Sets up the mapping from nir_register to struct qreg *.
1497 * Each nir_register gets a struct qreg per 32-bit component being stored.
1500 ntq_setup_registers(struct v3d_compile
*c
, struct exec_list
*list
)
1502 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1503 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1504 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1506 nir_reg
->num_components
);
1508 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1510 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1511 qregs
[i
] = vir_get_temp(c
);
1516 ntq_emit_load_const(struct v3d_compile
*c
, nir_load_const_instr
*instr
)
1518 /* XXX perf: Experiment with using immediate loads to avoid having
1519 * these end up in the uniform stream. Watch out for breaking the
1520 * small immediates optimization in the process!
1522 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1523 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1524 qregs
[i
] = vir_uniform_ui(c
, instr
->value
.u32
[i
]);
1526 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1530 ntq_emit_ssa_undef(struct v3d_compile
*c
, nir_ssa_undef_instr
*instr
)
1532 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1534 /* VIR needs there to be *some* value, so pick 0 (same as for
1535 * ntq_setup_registers().
1537 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1538 qregs
[i
] = vir_uniform_ui(c
, 0);
1542 ntq_emit_image_size(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1544 assert(instr
->intrinsic
== nir_intrinsic_image_deref_size
);
1545 nir_variable
*var
= nir_intrinsic_get_var(instr
, 0);
1546 unsigned image_index
= var
->data
.driver_location
;
1547 const struct glsl_type
*sampler_type
= glsl_without_array(var
->type
);
1548 bool is_array
= glsl_sampler_type_is_array(sampler_type
);
1550 ntq_store_dest(c
, &instr
->dest
, 0,
1551 vir_uniform(c
, QUNIFORM_IMAGE_WIDTH
, image_index
));
1552 if (instr
->num_components
> 1) {
1553 ntq_store_dest(c
, &instr
->dest
, 1,
1554 vir_uniform(c
, QUNIFORM_IMAGE_HEIGHT
,
1557 if (instr
->num_components
> 2) {
1558 ntq_store_dest(c
, &instr
->dest
, 2,
1561 QUNIFORM_IMAGE_ARRAY_SIZE
:
1562 QUNIFORM_IMAGE_DEPTH
,
1568 ntq_emit_intrinsic(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1572 switch (instr
->intrinsic
) {
1573 case nir_intrinsic_load_uniform
:
1574 if (nir_src_is_const(instr
->src
[0])) {
1575 int offset
= (nir_intrinsic_base(instr
) +
1576 nir_src_as_uint(instr
->src
[0]));
1577 assert(offset
% 4 == 0);
1578 /* We need dwords */
1579 offset
= offset
/ 4;
1580 for (int i
= 0; i
< instr
->num_components
; i
++) {
1581 ntq_store_dest(c
, &instr
->dest
, i
,
1582 vir_uniform(c
, QUNIFORM_UNIFORM
,
1586 ntq_emit_tmu_general(c
, instr
, false);
1590 case nir_intrinsic_load_ubo
:
1591 ntq_emit_tmu_general(c
, instr
, false);
1594 case nir_intrinsic_ssbo_atomic_add
:
1595 case nir_intrinsic_ssbo_atomic_imin
:
1596 case nir_intrinsic_ssbo_atomic_umin
:
1597 case nir_intrinsic_ssbo_atomic_imax
:
1598 case nir_intrinsic_ssbo_atomic_umax
:
1599 case nir_intrinsic_ssbo_atomic_and
:
1600 case nir_intrinsic_ssbo_atomic_or
:
1601 case nir_intrinsic_ssbo_atomic_xor
:
1602 case nir_intrinsic_ssbo_atomic_exchange
:
1603 case nir_intrinsic_ssbo_atomic_comp_swap
:
1604 case nir_intrinsic_load_ssbo
:
1605 case nir_intrinsic_store_ssbo
:
1606 ntq_emit_tmu_general(c
, instr
, false);
1609 case nir_intrinsic_shared_atomic_add
:
1610 case nir_intrinsic_shared_atomic_imin
:
1611 case nir_intrinsic_shared_atomic_umin
:
1612 case nir_intrinsic_shared_atomic_imax
:
1613 case nir_intrinsic_shared_atomic_umax
:
1614 case nir_intrinsic_shared_atomic_and
:
1615 case nir_intrinsic_shared_atomic_or
:
1616 case nir_intrinsic_shared_atomic_xor
:
1617 case nir_intrinsic_shared_atomic_exchange
:
1618 case nir_intrinsic_shared_atomic_comp_swap
:
1619 case nir_intrinsic_load_shared
:
1620 case nir_intrinsic_store_shared
:
1621 ntq_emit_tmu_general(c
, instr
, true);
1624 case nir_intrinsic_image_deref_load
:
1625 case nir_intrinsic_image_deref_store
:
1626 case nir_intrinsic_image_deref_atomic_add
:
1627 case nir_intrinsic_image_deref_atomic_min
:
1628 case nir_intrinsic_image_deref_atomic_max
:
1629 case nir_intrinsic_image_deref_atomic_and
:
1630 case nir_intrinsic_image_deref_atomic_or
:
1631 case nir_intrinsic_image_deref_atomic_xor
:
1632 case nir_intrinsic_image_deref_atomic_exchange
:
1633 case nir_intrinsic_image_deref_atomic_comp_swap
:
1634 v3d40_vir_emit_image_load_store(c
, instr
);
1637 case nir_intrinsic_get_buffer_size
:
1638 ntq_store_dest(c
, &instr
->dest
, 0,
1639 vir_uniform(c
, QUNIFORM_GET_BUFFER_SIZE
,
1640 nir_src_as_uint(instr
->src
[0])));
1643 case nir_intrinsic_load_user_clip_plane
:
1644 for (int i
= 0; i
< instr
->num_components
; i
++) {
1645 ntq_store_dest(c
, &instr
->dest
, i
,
1646 vir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1647 nir_intrinsic_ucp_id(instr
) *
1652 case nir_intrinsic_load_viewport_x_scale
:
1653 ntq_store_dest(c
, &instr
->dest
, 0,
1654 vir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
, 0));
1657 case nir_intrinsic_load_viewport_y_scale
:
1658 ntq_store_dest(c
, &instr
->dest
, 0,
1659 vir_uniform(c
, QUNIFORM_VIEWPORT_Y_SCALE
, 0));
1662 case nir_intrinsic_load_viewport_z_scale
:
1663 ntq_store_dest(c
, &instr
->dest
, 0,
1664 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0));
1667 case nir_intrinsic_load_viewport_z_offset
:
1668 ntq_store_dest(c
, &instr
->dest
, 0,
1669 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0));
1672 case nir_intrinsic_load_alpha_ref_float
:
1673 ntq_store_dest(c
, &instr
->dest
, 0,
1674 vir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1677 case nir_intrinsic_load_sample_mask_in
:
1678 ntq_store_dest(c
, &instr
->dest
, 0, vir_MSF(c
));
1681 case nir_intrinsic_load_helper_invocation
:
1682 vir_set_pf(vir_MSF_dest(c
, vir_nop_reg()), V3D_QPU_PF_PUSHZ
);
1683 ntq_store_dest(c
, &instr
->dest
, 0,
1684 vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1685 vir_uniform_ui(c
, ~0),
1686 vir_uniform_ui(c
, 0))));
1689 case nir_intrinsic_load_front_face
:
1690 /* The register contains 0 (front) or 1 (back), and we need to
1691 * turn it into a NIR bool where true means front.
1693 ntq_store_dest(c
, &instr
->dest
, 0,
1695 vir_uniform_ui(c
, -1),
1699 case nir_intrinsic_load_instance_id
:
1700 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->iid
));
1703 case nir_intrinsic_load_vertex_id
:
1704 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->vid
));
1707 case nir_intrinsic_load_input
:
1708 offset
= (nir_intrinsic_base(instr
) +
1709 nir_src_as_uint(instr
->src
[0]));
1710 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
&&
1711 c
->devinfo
->ver
>= 40) {
1712 /* Emit the LDVPM directly now, rather than at the top
1713 * of the shader like we did for V3D 3.x (which needs
1714 * vpmsetup when not just taking the next offset).
1716 * Note that delaying like this may introduce stalls,
1717 * as LDVPMV takes a minimum of 1 instruction but may
1718 * be slower if the VPM unit is busy with another QPU.
1721 if (c
->s
->info
.system_values_read
&
1722 (1ull << SYSTEM_VALUE_INSTANCE_ID
)) {
1725 if (c
->s
->info
.system_values_read
&
1726 (1ull << SYSTEM_VALUE_VERTEX_ID
)) {
1729 for (int i
= 0; i
< offset
; i
++)
1730 index
+= c
->vattr_sizes
[i
];
1731 index
+= nir_intrinsic_component(instr
);
1732 for (int i
= 0; i
< instr
->num_components
; i
++) {
1733 struct qreg vpm_offset
=
1734 vir_uniform_ui(c
, index
++);
1735 ntq_store_dest(c
, &instr
->dest
, i
,
1736 vir_LDVPMV_IN(c
, vpm_offset
));
1739 for (int i
= 0; i
< instr
->num_components
; i
++) {
1740 int comp
= nir_intrinsic_component(instr
) + i
;
1741 ntq_store_dest(c
, &instr
->dest
, i
,
1742 vir_MOV(c
, c
->inputs
[offset
* 4 +
1748 case nir_intrinsic_store_output
:
1749 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1750 offset
= ((nir_intrinsic_base(instr
) +
1751 nir_src_as_uint(instr
->src
[1])) * 4 +
1752 nir_intrinsic_component(instr
));
1753 for (int i
= 0; i
< instr
->num_components
; i
++) {
1754 c
->outputs
[offset
+ i
] =
1760 assert(instr
->num_components
== 1);
1763 ntq_get_src(c
, instr
->src
[0], 0),
1764 nir_intrinsic_base(instr
));
1768 case nir_intrinsic_image_deref_size
:
1769 ntq_emit_image_size(c
, instr
);
1772 case nir_intrinsic_discard
:
1773 if (vir_in_nonuniform_control_flow(c
)) {
1774 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
1776 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
1777 vir_uniform_ui(c
, 0)),
1780 vir_SETMSF_dest(c
, vir_nop_reg(),
1781 vir_uniform_ui(c
, 0));
1785 case nir_intrinsic_discard_if
: {
1786 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, instr
->src
[0]);
1788 if (vir_in_nonuniform_control_flow(c
)) {
1789 struct qinst
*exec_flag
= vir_MOV_dest(c
, vir_nop_reg(),
1791 if (cond
== V3D_QPU_COND_IFA
) {
1792 vir_set_uf(exec_flag
, V3D_QPU_UF_ANDZ
);
1794 vir_set_uf(exec_flag
, V3D_QPU_UF_NORNZ
);
1795 cond
= V3D_QPU_COND_IFA
;
1799 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
1800 vir_uniform_ui(c
, 0)), cond
);
1805 case nir_intrinsic_memory_barrier
:
1806 case nir_intrinsic_memory_barrier_atomic_counter
:
1807 case nir_intrinsic_memory_barrier_buffer
:
1808 case nir_intrinsic_memory_barrier_image
:
1809 case nir_intrinsic_memory_barrier_shared
:
1810 /* We don't do any instruction scheduling of these NIR
1811 * instructions between each other, so we just need to make
1812 * sure that the TMU operations before the barrier are flushed
1813 * before the ones after the barrier. That is currently
1814 * handled by having a THRSW in each of them and a LDTMU
1815 * series or a TMUWT after.
1819 case nir_intrinsic_barrier
:
1820 /* Emit a TSY op to get all invocations in the workgroup
1821 * (actually supergroup) to block until the last invocation
1822 * reaches the TSY op.
1824 if (c
->devinfo
->ver
>= 42) {
1825 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
1826 V3D_QPU_WADDR_SYNCB
));
1828 struct qinst
*sync
=
1829 vir_BARRIERID_dest(c
,
1830 vir_reg(QFILE_MAGIC
,
1831 V3D_QPU_WADDR_SYNCU
));
1833 vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
1835 V3D_TSY_WAIT_INC_CHECK
);
1839 /* The blocking of a TSY op only happens at the next thread
1840 * switch. No texturing may be outstanding at the time of a
1841 * TSY blocking operation.
1846 case nir_intrinsic_load_num_work_groups
:
1847 for (int i
= 0; i
< 3; i
++) {
1848 ntq_store_dest(c
, &instr
->dest
, i
,
1849 vir_uniform(c
, QUNIFORM_NUM_WORK_GROUPS
,
1854 case nir_intrinsic_load_local_invocation_index
:
1855 ntq_store_dest(c
, &instr
->dest
, 0,
1856 vir_SHR(c
, c
->cs_payload
[1],
1857 vir_uniform_ui(c
, 32 - c
->local_invocation_index_bits
)));
1860 case nir_intrinsic_load_work_group_id
:
1861 ntq_store_dest(c
, &instr
->dest
, 0,
1862 vir_AND(c
, c
->cs_payload
[0],
1863 vir_uniform_ui(c
, 0xffff)));
1864 ntq_store_dest(c
, &instr
->dest
, 1,
1865 vir_SHR(c
, c
->cs_payload
[0],
1866 vir_uniform_ui(c
, 16)));
1867 ntq_store_dest(c
, &instr
->dest
, 2,
1868 vir_AND(c
, c
->cs_payload
[1],
1869 vir_uniform_ui(c
, 0xffff)));
1873 fprintf(stderr
, "Unknown intrinsic: ");
1874 nir_print_instr(&instr
->instr
, stderr
);
1875 fprintf(stderr
, "\n");
1880 /* Clears (activates) the execute flags for any channels whose jump target
1881 * matches this block.
1883 * XXX perf: Could we be using flpush/flpop somehow for our execution channel
1886 * XXX perf: For uniform control flow, we should be able to skip c->execute
1887 * handling entirely.
1890 ntq_activate_execute_for_block(struct v3d_compile
*c
)
1892 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
1893 c
->execute
, vir_uniform_ui(c
, c
->cur_block
->index
)),
1896 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
1900 ntq_emit_uniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
1902 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
1903 bool empty_else_block
=
1904 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
1905 exec_list_is_empty(&nir_else_block
->instr_list
));
1907 struct qblock
*then_block
= vir_new_block(c
);
1908 struct qblock
*after_block
= vir_new_block(c
);
1909 struct qblock
*else_block
;
1910 if (empty_else_block
)
1911 else_block
= after_block
;
1913 else_block
= vir_new_block(c
);
1915 /* Set up the flags for the IF condition (taking the THEN branch). */
1916 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
1919 vir_BRANCH(c
, cond
== V3D_QPU_COND_IFA
?
1920 V3D_QPU_BRANCH_COND_ALLNA
:
1921 V3D_QPU_BRANCH_COND_ALLA
);
1922 vir_link_blocks(c
->cur_block
, else_block
);
1923 vir_link_blocks(c
->cur_block
, then_block
);
1925 /* Process the THEN block. */
1926 vir_set_emit_block(c
, then_block
);
1927 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
1929 if (!empty_else_block
) {
1930 /* At the end of the THEN block, jump to ENDIF */
1931 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALWAYS
);
1932 vir_link_blocks(c
->cur_block
, after_block
);
1934 /* Emit the else block. */
1935 vir_set_emit_block(c
, else_block
);
1936 ntq_activate_execute_for_block(c
);
1937 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
1940 vir_link_blocks(c
->cur_block
, after_block
);
1942 vir_set_emit_block(c
, after_block
);
1946 ntq_emit_nonuniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
1948 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
1949 bool empty_else_block
=
1950 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
1951 exec_list_is_empty(&nir_else_block
->instr_list
));
1953 struct qblock
*then_block
= vir_new_block(c
);
1954 struct qblock
*after_block
= vir_new_block(c
);
1955 struct qblock
*else_block
;
1956 if (empty_else_block
)
1957 else_block
= after_block
;
1959 else_block
= vir_new_block(c
);
1961 bool was_uniform_control_flow
= false;
1962 if (!vir_in_nonuniform_control_flow(c
)) {
1963 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
1964 was_uniform_control_flow
= true;
1967 /* Set up the flags for the IF condition (taking the THEN branch). */
1968 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
1970 /* Update the flags+cond to mean "Taking the ELSE branch (!cond) and
1971 * was previously active (execute Z) for updating the exec flags.
1973 if (was_uniform_control_flow
) {
1974 cond
= v3d_qpu_cond_invert(cond
);
1976 struct qinst
*inst
= vir_MOV_dest(c
, vir_nop_reg(), c
->execute
);
1977 if (cond
== V3D_QPU_COND_IFA
) {
1978 vir_set_uf(inst
, V3D_QPU_UF_NORNZ
);
1980 vir_set_uf(inst
, V3D_QPU_UF_ANDZ
);
1981 cond
= V3D_QPU_COND_IFA
;
1985 vir_MOV_cond(c
, cond
,
1987 vir_uniform_ui(c
, else_block
->index
));
1989 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1992 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
1993 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLNA
);
1994 vir_link_blocks(c
->cur_block
, else_block
);
1995 vir_link_blocks(c
->cur_block
, then_block
);
1997 /* Process the THEN block. */
1998 vir_set_emit_block(c
, then_block
);
1999 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
2001 if (!empty_else_block
) {
2002 /* Handle the end of the THEN block. First, all currently
2003 * active channels update their execute flags to point to
2006 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2008 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2009 vir_uniform_ui(c
, after_block
->index
));
2011 /* If everything points at ENDIF, then jump there immediately. */
2012 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
2014 vir_uniform_ui(c
, after_block
->index
)),
2016 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLA
);
2017 vir_link_blocks(c
->cur_block
, after_block
);
2018 vir_link_blocks(c
->cur_block
, else_block
);
2020 vir_set_emit_block(c
, else_block
);
2021 ntq_activate_execute_for_block(c
);
2022 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
2025 vir_link_blocks(c
->cur_block
, after_block
);
2027 vir_set_emit_block(c
, after_block
);
2028 if (was_uniform_control_flow
)
2029 c
->execute
= c
->undef
;
2031 ntq_activate_execute_for_block(c
);
2035 ntq_emit_if(struct v3d_compile
*c
, nir_if
*nif
)
2037 bool was_in_control_flow
= c
->in_control_flow
;
2038 c
->in_control_flow
= true;
2039 if (!vir_in_nonuniform_control_flow(c
) &&
2040 nir_src_is_dynamically_uniform(nif
->condition
)) {
2041 ntq_emit_uniform_if(c
, nif
);
2043 ntq_emit_nonuniform_if(c
, nif
);
2045 c
->in_control_flow
= was_in_control_flow
;
2049 ntq_emit_jump(struct v3d_compile
*c
, nir_jump_instr
*jump
)
2051 switch (jump
->type
) {
2052 case nir_jump_break
:
2053 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2055 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2056 vir_uniform_ui(c
, c
->loop_break_block
->index
));
2059 case nir_jump_continue
:
2060 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2062 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2063 vir_uniform_ui(c
, c
->loop_cont_block
->index
));
2066 case nir_jump_return
:
2067 unreachable("All returns shouold be lowered\n");
2072 ntq_emit_instr(struct v3d_compile
*c
, nir_instr
*instr
)
2074 switch (instr
->type
) {
2075 case nir_instr_type_deref
:
2076 /* ignored, will be walked by the intrinsic using it. */
2079 case nir_instr_type_alu
:
2080 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
2083 case nir_instr_type_intrinsic
:
2084 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
2087 case nir_instr_type_load_const
:
2088 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
2091 case nir_instr_type_ssa_undef
:
2092 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
2095 case nir_instr_type_tex
:
2096 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
2099 case nir_instr_type_jump
:
2100 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
2104 fprintf(stderr
, "Unknown NIR instr type: ");
2105 nir_print_instr(instr
, stderr
);
2106 fprintf(stderr
, "\n");
2112 ntq_emit_block(struct v3d_compile
*c
, nir_block
*block
)
2114 nir_foreach_instr(instr
, block
) {
2115 ntq_emit_instr(c
, instr
);
2119 static void ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
2122 ntq_emit_loop(struct v3d_compile
*c
, nir_loop
*loop
)
2124 bool was_in_control_flow
= c
->in_control_flow
;
2125 c
->in_control_flow
= true;
2127 bool was_uniform_control_flow
= false;
2128 if (!vir_in_nonuniform_control_flow(c
)) {
2129 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
2130 was_uniform_control_flow
= true;
2133 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
2134 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
2136 c
->loop_cont_block
= vir_new_block(c
);
2137 c
->loop_break_block
= vir_new_block(c
);
2139 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2140 vir_set_emit_block(c
, c
->loop_cont_block
);
2141 ntq_activate_execute_for_block(c
);
2143 ntq_emit_cf_list(c
, &loop
->body
);
2145 /* Re-enable any previous continues now, so our ANYA check below
2148 * XXX: Use the .ORZ flags update, instead.
2150 vir_set_pf(vir_XOR_dest(c
,
2153 vir_uniform_ui(c
, c
->loop_cont_block
->index
)),
2155 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
2157 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
2159 struct qinst
*branch
= vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ANYA
);
2160 /* Pixels that were not dispatched or have been discarded should not
2161 * contribute to looping again.
2163 branch
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_P
;
2164 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2165 vir_link_blocks(c
->cur_block
, c
->loop_break_block
);
2167 vir_set_emit_block(c
, c
->loop_break_block
);
2168 if (was_uniform_control_flow
)
2169 c
->execute
= c
->undef
;
2171 ntq_activate_execute_for_block(c
);
2173 c
->loop_break_block
= save_loop_break_block
;
2174 c
->loop_cont_block
= save_loop_cont_block
;
2178 c
->in_control_flow
= was_in_control_flow
;
2182 ntq_emit_function(struct v3d_compile
*c
, nir_function_impl
*func
)
2184 fprintf(stderr
, "FUNCTIONS not handled.\n");
2189 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
)
2191 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2192 switch (node
->type
) {
2193 case nir_cf_node_block
:
2194 ntq_emit_block(c
, nir_cf_node_as_block(node
));
2197 case nir_cf_node_if
:
2198 ntq_emit_if(c
, nir_cf_node_as_if(node
));
2201 case nir_cf_node_loop
:
2202 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
2205 case nir_cf_node_function
:
2206 ntq_emit_function(c
, nir_cf_node_as_function(node
));
2210 fprintf(stderr
, "Unknown NIR node type\n");
2217 ntq_emit_impl(struct v3d_compile
*c
, nir_function_impl
*impl
)
2219 ntq_setup_registers(c
, &impl
->registers
);
2220 ntq_emit_cf_list(c
, &impl
->body
);
2224 nir_to_vir(struct v3d_compile
*c
)
2226 switch (c
->s
->info
.stage
) {
2227 case MESA_SHADER_FRAGMENT
:
2228 c
->payload_w
= vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2229 c
->payload_w_centroid
= vir_MOV(c
, vir_reg(QFILE_REG
, 1));
2230 c
->payload_z
= vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2232 /* XXX perf: We could set the "disable implicit point/line
2233 * varyings" field in the shader record and not emit these, if
2234 * they're not going to be used.
2236 if (c
->fs_key
->is_points
) {
2237 c
->point_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2238 c
->point_y
= emit_fragment_varying(c
, NULL
, 0, 0);
2239 } else if (c
->fs_key
->is_lines
) {
2240 c
->line_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2243 case MESA_SHADER_COMPUTE
:
2244 /* Set up the TSO for barriers, assuming we do some. */
2245 if (c
->devinfo
->ver
< 42) {
2246 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
2247 V3D_QPU_WADDR_SYNC
));
2250 if (c
->s
->info
.system_values_read
&
2251 ((1ull << SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
) |
2252 (1ull << SYSTEM_VALUE_WORK_GROUP_ID
))) {
2253 c
->cs_payload
[0] = vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2255 if ((c
->s
->info
.system_values_read
&
2256 ((1ull << SYSTEM_VALUE_WORK_GROUP_ID
))) ||
2257 c
->s
->info
.cs
.shared_size
) {
2258 c
->cs_payload
[1] = vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2261 /* Set up the division between gl_LocalInvocationIndex and
2262 * wg_in_mem in the payload reg.
2264 int wg_size
= (c
->s
->info
.cs
.local_size
[0] *
2265 c
->s
->info
.cs
.local_size
[1] *
2266 c
->s
->info
.cs
.local_size
[2]);
2267 c
->local_invocation_index_bits
=
2268 ffs(util_next_power_of_two(MAX2(wg_size
, 64))) - 1;
2269 assert(c
->local_invocation_index_bits
<= 8);
2271 if (c
->s
->info
.cs
.shared_size
) {
2272 struct qreg wg_in_mem
= vir_SHR(c
, c
->cs_payload
[1],
2273 vir_uniform_ui(c
, 16));
2274 if (c
->s
->info
.cs
.local_size
[0] != 1 ||
2275 c
->s
->info
.cs
.local_size
[1] != 1 ||
2276 c
->s
->info
.cs
.local_size
[2] != 1) {
2278 c
->local_invocation_index_bits
);
2279 int wg_mask
= (1 << wg_bits
) - 1;
2280 wg_in_mem
= vir_AND(c
, wg_in_mem
,
2281 vir_uniform_ui(c
, wg_mask
));
2283 struct qreg shared_per_wg
=
2284 vir_uniform_ui(c
, c
->s
->info
.cs
.shared_size
);
2286 c
->cs_shared_offset
=
2288 vir_uniform(c
, QUNIFORM_SHARED_OFFSET
,0),
2289 vir_UMUL(c
, wg_in_mem
, shared_per_wg
));
2296 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
)
2297 ntq_setup_fs_inputs(c
);
2299 ntq_setup_vpm_inputs(c
);
2301 ntq_setup_outputs(c
);
2303 /* Find the main function and emit the body. */
2304 nir_foreach_function(function
, c
->s
) {
2305 assert(strcmp(function
->name
, "main") == 0);
2306 assert(function
->impl
);
2307 ntq_emit_impl(c
, function
->impl
);
2311 const nir_shader_compiler_options v3d_nir_options
= {
2312 .lower_all_io_to_temps
= true,
2313 .lower_extract_byte
= true,
2314 .lower_extract_word
= true,
2316 .lower_bitfield_insert_to_shifts
= true,
2317 .lower_bitfield_extract_to_shifts
= true,
2318 .lower_bitfield_reverse
= true,
2319 .lower_bit_count
= true,
2320 .lower_cs_local_id_from_index
= true,
2321 .lower_ffract
= true,
2322 .lower_pack_unorm_2x16
= true,
2323 .lower_pack_snorm_2x16
= true,
2324 .lower_pack_unorm_4x8
= true,
2325 .lower_pack_snorm_4x8
= true,
2326 .lower_unpack_unorm_4x8
= true,
2327 .lower_unpack_snorm_4x8
= true,
2328 .lower_pack_half_2x16
= true,
2329 .lower_unpack_half_2x16
= true,
2331 .lower_find_lsb
= true,
2333 .lower_flrp32
= true,
2336 .lower_fsqrt
= true,
2337 .lower_ifind_msb
= true,
2338 .lower_isign
= true,
2339 .lower_ldexp
= true,
2340 .lower_mul_high
= true,
2341 .lower_wpos_pntc
= true,
2342 .native_integers
= true,
2346 * When demoting a shader down to single-threaded, removes the THRSW
2347 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2351 vir_remove_thrsw(struct v3d_compile
*c
)
2353 vir_for_each_block(block
, c
) {
2354 vir_for_each_inst_safe(inst
, block
) {
2355 if (inst
->qpu
.sig
.thrsw
)
2356 vir_remove_instruction(c
, inst
);
2360 c
->last_thrsw
= NULL
;
2364 vir_emit_last_thrsw(struct v3d_compile
*c
)
2366 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2367 * switching, so disable threads if we didn't do any TMU ops (each of
2368 * which would have emitted a THRSW).
2370 if (!c
->last_thrsw_at_top_level
&& c
->devinfo
->ver
< 41) {
2373 vir_remove_thrsw(c
);
2377 /* If we're threaded and the last THRSW was in conditional code, then
2378 * we need to emit another one so that we can flag it as the last
2381 if (c
->last_thrsw
&& !c
->last_thrsw_at_top_level
) {
2382 assert(c
->devinfo
->ver
>= 41);
2386 /* If we're threaded, then we need to mark the last THRSW instruction
2387 * so we can emit a pair of them at QPU emit time.
2389 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2390 * post-last-THRSW state, so we can skip this.
2392 if (!c
->last_thrsw
&& c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2393 assert(c
->devinfo
->ver
>= 41);
2398 c
->last_thrsw
->is_last_thrsw
= true;
2401 /* There's a flag in the shader for "center W is needed for reasons other than
2402 * non-centroid varyings", so we just walk the program after VIR optimization
2403 * to see if it's used. It should be harmless to set even if we only use
2404 * center W for varyings.
2407 vir_check_payload_w(struct v3d_compile
*c
)
2409 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
2412 vir_for_each_inst_inorder(inst
, c
) {
2413 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
2414 if (inst
->src
[i
].file
== QFILE_REG
&&
2415 inst
->src
[i
].index
== 0) {
2416 c
->uses_center_w
= true;
2425 v3d_nir_to_vir(struct v3d_compile
*c
)
2427 if (V3D_DEBUG
& (V3D_DEBUG_NIR
|
2428 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2429 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2430 vir_get_stage_name(c
),
2431 c
->program_id
, c
->variant_id
);
2432 nir_print_shader(c
->s
, stderr
);
2437 /* Emit the last THRSW before STVPM and TLB writes. */
2438 vir_emit_last_thrsw(c
);
2440 switch (c
->s
->info
.stage
) {
2441 case MESA_SHADER_FRAGMENT
:
2444 case MESA_SHADER_VERTEX
:
2448 unreachable("bad stage");
2451 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2452 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2453 fprintf(stderr
, "%s prog %d/%d pre-opt VIR:\n",
2454 vir_get_stage_name(c
),
2455 c
->program_id
, c
->variant_id
);
2457 fprintf(stderr
, "\n");
2462 vir_check_payload_w(c
);
2464 /* XXX perf: On VC4, we do a VIR-level instruction scheduling here.
2465 * We used that on that platform to pipeline TMU writes and reduce the
2466 * number of thread switches, as well as try (mostly successfully) to
2467 * reduce maximum register pressure to allow more threads. We should
2468 * do something of that sort for V3D -- either instruction scheduling
2469 * here, or delay the the THRSW and LDTMUs from our texture
2470 * instructions until the results are needed.
2473 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2474 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2475 fprintf(stderr
, "%s prog %d/%d VIR:\n",
2476 vir_get_stage_name(c
),
2477 c
->program_id
, c
->variant_id
);
2479 fprintf(stderr
, "\n");
2482 /* Attempt to allocate registers for the temporaries. If we fail,
2483 * reduce thread count and try again.
2485 int min_threads
= (c
->devinfo
->ver
>= 41) ? 2 : 1;
2486 struct qpu_reg
*temp_registers
;
2489 temp_registers
= v3d_register_allocate(c
, &spilled
);
2496 if (c
->threads
== min_threads
) {
2497 fprintf(stderr
, "Failed to register allocate at %d threads:\n",
2506 if (c
->threads
== 1)
2507 vir_remove_thrsw(c
);
2510 if (c
->spill_size
&&
2511 (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2512 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
)))) {
2513 fprintf(stderr
, "%s prog %d/%d spilled VIR:\n",
2514 vir_get_stage_name(c
),
2515 c
->program_id
, c
->variant_id
);
2517 fprintf(stderr
, "\n");
2520 v3d_vir_to_qpu(c
, temp_registers
);