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
;
126 /* We need to lock the scoreboard before any tlb acess happens. If this
127 * thread switch comes after we have emitted a tlb load, then it means
128 * that we can't lock on the last thread switch any more.
130 if (c
->emitted_tlb_load
)
131 c
->lock_scoreboard_on_first_thrsw
= true;
135 v3d_general_tmu_op(nir_intrinsic_instr
*instr
)
137 switch (instr
->intrinsic
) {
138 case nir_intrinsic_load_ssbo
:
139 case nir_intrinsic_load_ubo
:
140 case nir_intrinsic_load_uniform
:
141 case nir_intrinsic_load_shared
:
142 case nir_intrinsic_load_scratch
:
143 return GENERAL_TMU_READ_OP_READ
;
144 case nir_intrinsic_store_ssbo
:
145 case nir_intrinsic_store_shared
:
146 case nir_intrinsic_store_scratch
:
147 return GENERAL_TMU_WRITE_OP_WRITE
;
148 case nir_intrinsic_ssbo_atomic_add
:
149 case nir_intrinsic_shared_atomic_add
:
150 return GENERAL_TMU_WRITE_OP_ATOMIC_ADD_WRAP
;
151 case nir_intrinsic_ssbo_atomic_imin
:
152 case nir_intrinsic_shared_atomic_imin
:
153 return GENERAL_TMU_WRITE_OP_ATOMIC_SMIN
;
154 case nir_intrinsic_ssbo_atomic_umin
:
155 case nir_intrinsic_shared_atomic_umin
:
156 return GENERAL_TMU_WRITE_OP_ATOMIC_UMIN
;
157 case nir_intrinsic_ssbo_atomic_imax
:
158 case nir_intrinsic_shared_atomic_imax
:
159 return GENERAL_TMU_WRITE_OP_ATOMIC_SMAX
;
160 case nir_intrinsic_ssbo_atomic_umax
:
161 case nir_intrinsic_shared_atomic_umax
:
162 return GENERAL_TMU_WRITE_OP_ATOMIC_UMAX
;
163 case nir_intrinsic_ssbo_atomic_and
:
164 case nir_intrinsic_shared_atomic_and
:
165 return GENERAL_TMU_WRITE_OP_ATOMIC_AND
;
166 case nir_intrinsic_ssbo_atomic_or
:
167 case nir_intrinsic_shared_atomic_or
:
168 return GENERAL_TMU_WRITE_OP_ATOMIC_OR
;
169 case nir_intrinsic_ssbo_atomic_xor
:
170 case nir_intrinsic_shared_atomic_xor
:
171 return GENERAL_TMU_WRITE_OP_ATOMIC_XOR
;
172 case nir_intrinsic_ssbo_atomic_exchange
:
173 case nir_intrinsic_shared_atomic_exchange
:
174 return GENERAL_TMU_WRITE_OP_ATOMIC_XCHG
;
175 case nir_intrinsic_ssbo_atomic_comp_swap
:
176 case nir_intrinsic_shared_atomic_comp_swap
:
177 return GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG
;
179 unreachable("unknown intrinsic op");
184 * Implements indirect uniform loads and SSBO accesses through the TMU general
185 * memory access interface.
188 ntq_emit_tmu_general(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
,
189 bool is_shared_or_scratch
)
191 /* XXX perf: We should turn add/sub of 1 to inc/dec. Perhaps NIR
192 * wants to have support for inc/dec?
195 uint32_t tmu_op
= v3d_general_tmu_op(instr
);
196 bool is_store
= (instr
->intrinsic
== nir_intrinsic_store_ssbo
||
197 instr
->intrinsic
== nir_intrinsic_store_scratch
||
198 instr
->intrinsic
== nir_intrinsic_store_shared
);
199 bool has_index
= !is_shared_or_scratch
;
202 int tmu_writes
= 1; /* address */
203 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
205 } else if (instr
->intrinsic
== nir_intrinsic_load_ssbo
||
206 instr
->intrinsic
== nir_intrinsic_load_ubo
||
207 instr
->intrinsic
== nir_intrinsic_load_scratch
||
208 instr
->intrinsic
== nir_intrinsic_load_shared
) {
209 offset_src
= 0 + has_index
;
210 } else if (is_store
) {
211 offset_src
= 1 + has_index
;
212 for (int i
= 0; i
< instr
->num_components
; i
++) {
214 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
215 ntq_get_src(c
, instr
->src
[0], i
));
219 offset_src
= 0 + has_index
;
221 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
222 ntq_get_src(c
, instr
->src
[1 + has_index
], 0));
224 if (tmu_op
== GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG
) {
226 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUD
),
227 ntq_get_src(c
, instr
->src
[2 + has_index
],
233 bool dynamic_src
= !nir_src_is_const(instr
->src
[offset_src
]);
234 uint32_t const_offset
= 0;
236 const_offset
= nir_src_as_uint(instr
->src
[offset_src
]);
238 /* Make sure we won't exceed the 16-entry TMU fifo if each thread is
239 * storing at the same time.
241 while (tmu_writes
> 16 / c
->threads
)
245 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
246 const_offset
+= nir_intrinsic_base(instr
);
247 offset
= vir_uniform(c
, QUNIFORM_UBO_ADDR
,
248 v3d_unit_data_create(0, const_offset
));
250 } else if (instr
->intrinsic
== nir_intrinsic_load_ubo
) {
251 uint32_t index
= nir_src_as_uint(instr
->src
[0]) + 1;
252 /* Note that QUNIFORM_UBO_ADDR takes a UBO index shifted up by
253 * 1 (0 is gallium's constant buffer 0).
255 offset
= vir_uniform(c
, QUNIFORM_UBO_ADDR
,
256 v3d_unit_data_create(index
, const_offset
));
258 } else if (is_shared_or_scratch
) {
259 /* Shared and scratch variables have no buffer index, and all
260 * start from a common base that we set up at the start of
263 if (instr
->intrinsic
== nir_intrinsic_load_scratch
||
264 instr
->intrinsic
== nir_intrinsic_store_scratch
) {
265 offset
= c
->spill_base
;
267 offset
= c
->cs_shared_offset
;
268 const_offset
+= nir_intrinsic_base(instr
);
271 offset
= vir_uniform(c
, QUNIFORM_SSBO_OFFSET
,
272 nir_src_as_uint(instr
->src
[is_store
?
276 /* The spec says that for atomics, the TYPE field is ignored, but that
277 * doesn't seem to be the case for CMPXCHG. Just use the number of
278 * tmud writes we did to decide the type (or choose "32bit" for atomic
279 * reads, which has been fine).
282 if (tmu_op
== GENERAL_TMU_WRITE_OP_ATOMIC_CMPXCHG
)
285 num_components
= instr
->num_components
;
287 uint32_t config
= (0xffffff00 |
289 GENERAL_TMU_LOOKUP_PER_PIXEL
);
290 if (num_components
== 1) {
291 config
|= GENERAL_TMU_LOOKUP_TYPE_32BIT_UI
;
293 config
|= GENERAL_TMU_LOOKUP_TYPE_VEC2
+ num_components
- 2;
296 if (vir_in_nonuniform_control_flow(c
)) {
297 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
303 tmua
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUA
);
305 tmua
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUAU
);
309 if (const_offset
!= 0) {
310 offset
= vir_ADD(c
, offset
,
311 vir_uniform_ui(c
, const_offset
));
313 tmu
= vir_ADD_dest(c
, tmua
, offset
,
314 ntq_get_src(c
, instr
->src
[offset_src
], 0));
316 if (const_offset
!= 0) {
317 tmu
= vir_ADD_dest(c
, tmua
, offset
,
318 vir_uniform_ui(c
, const_offset
));
320 tmu
= vir_MOV_dest(c
, tmua
, offset
);
325 tmu
->uniform
= vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
329 if (vir_in_nonuniform_control_flow(c
))
330 vir_set_cond(tmu
, V3D_QPU_COND_IFA
);
334 /* Read the result, or wait for the TMU op to complete. */
335 for (int i
= 0; i
< nir_intrinsic_dest_components(instr
); i
++)
336 ntq_store_dest(c
, &instr
->dest
, i
, vir_MOV(c
, vir_LDTMU(c
)));
338 if (nir_intrinsic_dest_components(instr
) == 0)
343 ntq_init_ssa_def(struct v3d_compile
*c
, nir_ssa_def
*def
)
345 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
346 def
->num_components
);
347 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
352 * This function is responsible for getting VIR results into the associated
353 * storage for a NIR instruction.
355 * If it's a NIR SSA def, then we just set the associated hash table entry to
358 * If it's a NIR reg, then we need to update the existing qreg assigned to the
359 * NIR destination with the incoming value. To do that without introducing
360 * new MOVs, we require that the incoming qreg either be a uniform, or be
361 * SSA-defined by the previous VIR instruction in the block and rewritable by
362 * this function. That lets us sneak ahead and insert the SF flag beforehand
363 * (knowing that the previous instruction doesn't depend on flags) and rewrite
364 * its destination to be the NIR reg's destination
367 ntq_store_dest(struct v3d_compile
*c
, nir_dest
*dest
, int chan
,
370 struct qinst
*last_inst
= NULL
;
371 if (!list_empty(&c
->cur_block
->instructions
))
372 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
374 assert((result
.file
== QFILE_TEMP
&&
375 last_inst
&& last_inst
== c
->defs
[result
.index
]));
378 assert(chan
< dest
->ssa
.num_components
);
381 struct hash_entry
*entry
=
382 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
387 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
389 qregs
[chan
] = result
;
391 nir_register
*reg
= dest
->reg
.reg
;
392 assert(dest
->reg
.base_offset
== 0);
393 assert(reg
->num_array_elems
== 0);
394 struct hash_entry
*entry
=
395 _mesa_hash_table_search(c
->def_ht
, reg
);
396 struct qreg
*qregs
= entry
->data
;
398 /* Insert a MOV if the source wasn't an SSA def in the
399 * previous instruction.
401 if ((vir_in_nonuniform_control_flow(c
) &&
402 c
->defs
[last_inst
->dst
.index
]->qpu
.sig
.ldunif
)) {
403 result
= vir_MOV(c
, result
);
404 last_inst
= c
->defs
[result
.index
];
407 /* We know they're both temps, so just rewrite index. */
408 c
->defs
[last_inst
->dst
.index
] = NULL
;
409 last_inst
->dst
.index
= qregs
[chan
].index
;
411 /* If we're in control flow, then make this update of the reg
412 * conditional on the execution mask.
414 if (vir_in_nonuniform_control_flow(c
)) {
415 last_inst
->dst
.index
= qregs
[chan
].index
;
417 /* Set the flags to the current exec mask.
419 c
->cursor
= vir_before_inst(last_inst
);
420 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
422 c
->cursor
= vir_after_inst(last_inst
);
424 vir_set_cond(last_inst
, V3D_QPU_COND_IFA
);
430 ntq_get_src(struct v3d_compile
*c
, nir_src src
, int i
)
432 struct hash_entry
*entry
;
434 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
435 assert(i
< src
.ssa
->num_components
);
437 nir_register
*reg
= src
.reg
.reg
;
438 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
439 assert(reg
->num_array_elems
== 0);
440 assert(src
.reg
.base_offset
== 0);
441 assert(i
< reg
->num_components
);
444 struct qreg
*qregs
= entry
->data
;
449 ntq_get_alu_src(struct v3d_compile
*c
, nir_alu_instr
*instr
,
452 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
453 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
454 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
455 instr
->src
[src
].swizzle
[chan
]);
457 assert(!instr
->src
[src
].abs
);
458 assert(!instr
->src
[src
].negate
);
464 ntq_minify(struct v3d_compile
*c
, struct qreg size
, struct qreg level
)
466 return vir_MAX(c
, vir_SHR(c
, size
, level
), vir_uniform_ui(c
, 1));
470 ntq_emit_txs(struct v3d_compile
*c
, nir_tex_instr
*instr
)
472 unsigned unit
= instr
->texture_index
;
473 int lod_index
= nir_tex_instr_src_index(instr
, nir_tex_src_lod
);
474 int dest_size
= nir_tex_instr_dest_size(instr
);
476 struct qreg lod
= c
->undef
;
478 lod
= ntq_get_src(c
, instr
->src
[lod_index
].src
, 0);
480 for (int i
= 0; i
< dest_size
; i
++) {
482 enum quniform_contents contents
;
484 if (instr
->is_array
&& i
== dest_size
- 1)
485 contents
= QUNIFORM_TEXTURE_ARRAY_SIZE
;
487 contents
= QUNIFORM_TEXTURE_WIDTH
+ i
;
489 struct qreg size
= vir_uniform(c
, contents
, unit
);
491 switch (instr
->sampler_dim
) {
492 case GLSL_SAMPLER_DIM_1D
:
493 case GLSL_SAMPLER_DIM_2D
:
494 case GLSL_SAMPLER_DIM_MS
:
495 case GLSL_SAMPLER_DIM_3D
:
496 case GLSL_SAMPLER_DIM_CUBE
:
497 /* Don't minify the array size. */
498 if (!(instr
->is_array
&& i
== dest_size
- 1)) {
499 size
= ntq_minify(c
, size
, lod
);
503 case GLSL_SAMPLER_DIM_RECT
:
504 /* There's no LOD field for rects */
508 unreachable("Bad sampler type");
511 ntq_store_dest(c
, &instr
->dest
, i
, size
);
516 ntq_emit_tex(struct v3d_compile
*c
, nir_tex_instr
*instr
)
518 unsigned unit
= instr
->texture_index
;
520 /* Since each texture sampling op requires uploading uniforms to
521 * reference the texture, there's no HW support for texture size and
522 * you just upload uniforms containing the size.
525 case nir_texop_query_levels
:
526 ntq_store_dest(c
, &instr
->dest
, 0,
527 vir_uniform(c
, QUNIFORM_TEXTURE_LEVELS
, unit
));
530 ntq_emit_txs(c
, instr
);
536 if (c
->devinfo
->ver
>= 40)
537 v3d40_vir_emit_tex(c
, instr
);
539 v3d33_vir_emit_tex(c
, instr
);
543 ntq_fsincos(struct v3d_compile
*c
, struct qreg src
, bool is_cos
)
545 struct qreg input
= vir_FMUL(c
, src
, vir_uniform_f(c
, 1.0f
/ M_PI
));
547 input
= vir_FADD(c
, input
, vir_uniform_f(c
, 0.5));
549 struct qreg periods
= vir_FROUND(c
, input
);
550 struct qreg sin_output
= vir_SIN(c
, vir_FSUB(c
, input
, periods
));
551 return vir_XOR(c
, sin_output
, vir_SHL(c
,
552 vir_FTOIN(c
, periods
),
553 vir_uniform_ui(c
, -1)));
557 ntq_fsign(struct v3d_compile
*c
, struct qreg src
)
559 struct qreg t
= vir_get_temp(c
);
561 vir_MOV_dest(c
, t
, vir_uniform_f(c
, 0.0));
562 vir_set_pf(vir_FMOV_dest(c
, vir_nop_reg(), src
), V3D_QPU_PF_PUSHZ
);
563 vir_MOV_cond(c
, V3D_QPU_COND_IFNA
, t
, vir_uniform_f(c
, 1.0));
564 vir_set_pf(vir_FMOV_dest(c
, vir_nop_reg(), src
), V3D_QPU_PF_PUSHN
);
565 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, t
, vir_uniform_f(c
, -1.0));
566 return vir_MOV(c
, t
);
570 emit_fragcoord_input(struct v3d_compile
*c
, int attr
)
572 c
->inputs
[attr
* 4 + 0] = vir_FXCD(c
);
573 c
->inputs
[attr
* 4 + 1] = vir_FYCD(c
);
574 c
->inputs
[attr
* 4 + 2] = c
->payload_z
;
575 c
->inputs
[attr
* 4 + 3] = vir_RECIP(c
, c
->payload_w
);
579 emit_fragment_varying(struct v3d_compile
*c
, nir_variable
*var
,
580 uint8_t swizzle
, int array_index
)
582 struct qreg r3
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R3
);
583 struct qreg r5
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R5
);
586 if (c
->devinfo
->ver
>= 41) {
587 struct qinst
*ldvary
= vir_add_inst(V3D_QPU_A_NOP
, c
->undef
,
589 ldvary
->qpu
.sig
.ldvary
= true;
590 vary
= vir_emit_def(c
, ldvary
);
592 vir_NOP(c
)->qpu
.sig
.ldvary
= true;
596 /* For gl_PointCoord input or distance along a line, we'll be called
597 * with no nir_variable, and we don't count toward VPM size so we
598 * don't track an input slot.
601 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
604 int i
= c
->num_inputs
++;
606 v3d_slot_from_slot_and_component(var
->data
.location
+
607 array_index
, swizzle
);
609 switch (var
->data
.interpolation
) {
610 case INTERP_MODE_NONE
:
611 /* If a gl_FrontColor or gl_BackColor input has no interp
612 * qualifier, then if we're using glShadeModel(GL_FLAT) it
613 * needs to be flat shaded.
615 switch (var
->data
.location
+ array_index
) {
616 case VARYING_SLOT_COL0
:
617 case VARYING_SLOT_COL1
:
618 case VARYING_SLOT_BFC0
:
619 case VARYING_SLOT_BFC1
:
620 if (c
->fs_key
->shade_model_flat
) {
621 BITSET_SET(c
->flat_shade_flags
, i
);
622 vir_MOV_dest(c
, c
->undef
, vary
);
623 return vir_MOV(c
, r5
);
625 return vir_FADD(c
, vir_FMUL(c
, vary
,
632 case INTERP_MODE_SMOOTH
:
633 if (var
->data
.centroid
) {
634 BITSET_SET(c
->centroid_flags
, i
);
635 return vir_FADD(c
, vir_FMUL(c
, vary
,
636 c
->payload_w_centroid
), r5
);
638 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
640 case INTERP_MODE_NOPERSPECTIVE
:
641 BITSET_SET(c
->noperspective_flags
, i
);
642 return vir_FADD(c
, vir_MOV(c
, vary
), r5
);
643 case INTERP_MODE_FLAT
:
644 BITSET_SET(c
->flat_shade_flags
, i
);
645 vir_MOV_dest(c
, c
->undef
, vary
);
646 return vir_MOV(c
, r5
);
648 unreachable("Bad interp mode");
653 emit_fragment_input(struct v3d_compile
*c
, int attr
, nir_variable
*var
,
656 for (int i
= 0; i
< glsl_get_vector_elements(var
->type
); i
++) {
657 int chan
= var
->data
.location_frac
+ i
;
658 c
->inputs
[attr
* 4 + chan
] =
659 emit_fragment_varying(c
, var
, chan
, array_index
);
664 add_output(struct v3d_compile
*c
,
665 uint32_t decl_offset
,
669 uint32_t old_array_size
= c
->outputs_array_size
;
670 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
673 if (old_array_size
!= c
->outputs_array_size
) {
674 c
->output_slots
= reralloc(c
,
676 struct v3d_varying_slot
,
677 c
->outputs_array_size
);
680 c
->output_slots
[decl_offset
] =
681 v3d_slot_from_slot_and_component(slot
, swizzle
);
685 * If compare_instr is a valid comparison instruction, emits the
686 * compare_instr's comparison and returns the sel_instr's return value based
687 * on the compare_instr's result.
690 ntq_emit_comparison(struct v3d_compile
*c
,
691 nir_alu_instr
*compare_instr
,
692 enum v3d_qpu_cond
*out_cond
)
694 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
696 if (nir_op_infos
[compare_instr
->op
].num_inputs
> 1)
697 src1
= ntq_get_alu_src(c
, compare_instr
, 1);
698 bool cond_invert
= false;
699 struct qreg nop
= vir_nop_reg();
701 switch (compare_instr
->op
) {
704 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
707 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
712 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
716 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
722 vir_set_pf(vir_FCMP_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
725 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
729 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
735 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHN
);
738 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
741 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
745 vir_set_pf(vir_MOV_dest(c
, nop
, src0
), V3D_QPU_PF_PUSHZ
);
750 vir_set_pf(vir_FMOV_dest(c
, nop
, src0
), V3D_QPU_PF_PUSHZ
);
758 *out_cond
= cond_invert
? V3D_QPU_COND_IFNA
: V3D_QPU_COND_IFA
;
763 /* Finds an ALU instruction that generates our src value that could
764 * (potentially) be greedily emitted in the consuming instruction.
766 static struct nir_alu_instr
*
767 ntq_get_alu_parent(nir_src src
)
769 if (!src
.is_ssa
|| src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
771 nir_alu_instr
*instr
= nir_instr_as_alu(src
.ssa
->parent_instr
);
775 /* If the ALU instr's srcs are non-SSA, then we would have to avoid
776 * moving emission of the ALU instr down past another write of the
779 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
780 if (!instr
->src
[i
].src
.is_ssa
)
787 /* Turns a NIR bool into a condition code to predicate on. */
788 static enum v3d_qpu_cond
789 ntq_emit_bool_to_cond(struct v3d_compile
*c
, nir_src src
)
791 nir_alu_instr
*compare
= ntq_get_alu_parent(src
);
795 enum v3d_qpu_cond cond
;
796 if (ntq_emit_comparison(c
, compare
, &cond
))
800 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), ntq_get_src(c
, src
, 0)),
802 return V3D_QPU_COND_IFNA
;
806 ntq_emit_alu(struct v3d_compile
*c
, nir_alu_instr
*instr
)
808 /* This should always be lowered to ALU operations for V3D. */
809 assert(!instr
->dest
.saturate
);
811 /* Vectors are special in that they have non-scalarized writemasks,
812 * and just take the first swizzle channel for each argument in order
813 * into each writemask channel.
815 if (instr
->op
== nir_op_vec2
||
816 instr
->op
== nir_op_vec3
||
817 instr
->op
== nir_op_vec4
) {
819 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
820 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
821 instr
->src
[i
].swizzle
[0]);
822 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
823 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
824 vir_MOV(c
, srcs
[i
]));
828 /* General case: We can just grab the one used channel per src. */
829 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
830 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
831 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
838 result
= vir_MOV(c
, src
[0]);
842 result
= vir_XOR(c
, src
[0], vir_uniform_ui(c
, 1 << 31));
845 result
= vir_NEG(c
, src
[0]);
849 result
= vir_FMUL(c
, src
[0], src
[1]);
852 result
= vir_FADD(c
, src
[0], src
[1]);
855 result
= vir_FSUB(c
, src
[0], src
[1]);
858 result
= vir_FMIN(c
, src
[0], src
[1]);
861 result
= vir_FMAX(c
, src
[0], src
[1]);
865 nir_alu_instr
*src0_alu
= ntq_get_alu_parent(instr
->src
[0].src
);
866 if (src0_alu
&& src0_alu
->op
== nir_op_fround_even
) {
867 result
= vir_FTOIN(c
, ntq_get_alu_src(c
, src0_alu
, 0));
869 result
= vir_FTOIZ(c
, src
[0]);
875 result
= vir_FTOUZ(c
, src
[0]);
878 result
= vir_ITOF(c
, src
[0]);
881 result
= vir_UTOF(c
, src
[0]);
884 result
= vir_AND(c
, src
[0], vir_uniform_f(c
, 1.0));
887 result
= vir_AND(c
, src
[0], vir_uniform_ui(c
, 1));
891 result
= vir_ADD(c
, src
[0], src
[1]);
894 result
= vir_SHR(c
, src
[0], src
[1]);
897 result
= vir_SUB(c
, src
[0], src
[1]);
900 result
= vir_ASR(c
, src
[0], src
[1]);
903 result
= vir_SHL(c
, src
[0], src
[1]);
906 result
= vir_MIN(c
, src
[0], src
[1]);
909 result
= vir_UMIN(c
, src
[0], src
[1]);
912 result
= vir_MAX(c
, src
[0], src
[1]);
915 result
= vir_UMAX(c
, src
[0], src
[1]);
918 result
= vir_AND(c
, src
[0], src
[1]);
921 result
= vir_OR(c
, src
[0], src
[1]);
924 result
= vir_XOR(c
, src
[0], src
[1]);
927 result
= vir_NOT(c
, src
[0]);
930 case nir_op_ufind_msb
:
931 result
= vir_SUB(c
, vir_uniform_ui(c
, 31), vir_CLZ(c
, src
[0]));
935 result
= vir_UMUL(c
, src
[0], src
[1]);
942 enum v3d_qpu_cond cond
;
943 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
945 result
= vir_MOV(c
, vir_SEL(c
, cond
,
946 vir_uniform_f(c
, 1.0),
947 vir_uniform_f(c
, 0.0)));
963 enum v3d_qpu_cond cond
;
964 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
966 result
= vir_MOV(c
, vir_SEL(c
, cond
,
967 vir_uniform_ui(c
, ~0),
968 vir_uniform_ui(c
, 0)));
975 ntq_emit_bool_to_cond(c
, instr
->src
[0].src
),
980 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), src
[0]),
982 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFNA
,
987 result
= vir_RECIP(c
, src
[0]);
990 result
= vir_RSQRT(c
, src
[0]);
993 result
= vir_EXP(c
, src
[0]);
996 result
= vir_LOG(c
, src
[0]);
1000 result
= vir_FCEIL(c
, src
[0]);
1003 result
= vir_FFLOOR(c
, src
[0]);
1005 case nir_op_fround_even
:
1006 result
= vir_FROUND(c
, src
[0]);
1009 result
= vir_FTRUNC(c
, src
[0]);
1013 result
= ntq_fsincos(c
, src
[0], false);
1016 result
= ntq_fsincos(c
, src
[0], true);
1020 result
= ntq_fsign(c
, src
[0]);
1024 result
= vir_FMOV(c
, src
[0]);
1025 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_ABS
);
1030 result
= vir_MAX(c
, src
[0], vir_NEG(c
, src
[0]));
1034 case nir_op_fddx_coarse
:
1035 case nir_op_fddx_fine
:
1036 result
= vir_FDX(c
, src
[0]);
1040 case nir_op_fddy_coarse
:
1041 case nir_op_fddy_fine
:
1042 result
= vir_FDY(c
, src
[0]);
1045 case nir_op_uadd_carry
:
1046 vir_set_pf(vir_ADD_dest(c
, vir_nop_reg(), src
[0], src
[1]),
1048 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1049 vir_uniform_ui(c
, ~0),
1050 vir_uniform_ui(c
, 0)));
1053 case nir_op_pack_half_2x16_split
:
1054 result
= vir_VFPACK(c
, src
[0], src
[1]);
1057 case nir_op_unpack_half_2x16_split_x
:
1058 result
= vir_FMOV(c
, src
[0]);
1059 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_L
);
1062 case nir_op_unpack_half_2x16_split_y
:
1063 result
= vir_FMOV(c
, src
[0]);
1064 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_H
);
1068 fprintf(stderr
, "unknown NIR ALU inst: ");
1069 nir_print_instr(&instr
->instr
, stderr
);
1070 fprintf(stderr
, "\n");
1074 /* We have a scalar result, so the instruction should only have a
1075 * single channel written to.
1077 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
1078 ntq_store_dest(c
, &instr
->dest
.dest
,
1079 ffs(instr
->dest
.write_mask
) - 1, result
);
1082 /* Each TLB read/write setup (a render target or depth buffer) takes an 8-bit
1083 * specifier. They come from a register that's preloaded with 0xffffffff
1084 * (0xff gets you normal vec4 f16 RT0 writes), and when one is neaded the low
1085 * 8 bits are shifted off the bottom and 0xff shifted in from the top.
1087 #define TLB_TYPE_F16_COLOR (3 << 6)
1088 #define TLB_TYPE_I32_COLOR (1 << 6)
1089 #define TLB_TYPE_F32_COLOR (0 << 6)
1090 #define TLB_RENDER_TARGET_SHIFT 3 /* Reversed! 7 = RT 0, 0 = RT 7. */
1091 #define TLB_SAMPLE_MODE_PER_SAMPLE (0 << 2)
1092 #define TLB_SAMPLE_MODE_PER_PIXEL (1 << 2)
1093 #define TLB_F16_SWAP_HI_LO (1 << 1)
1094 #define TLB_VEC_SIZE_4_F16 (1 << 0)
1095 #define TLB_VEC_SIZE_2_F16 (0 << 0)
1096 #define TLB_VEC_SIZE_MINUS_1_SHIFT 0
1098 /* Triggers Z/Stencil testing, used when the shader state's "FS modifies Z"
1101 #define TLB_TYPE_DEPTH ((2 << 6) | (0 << 4))
1102 #define TLB_DEPTH_TYPE_INVARIANT (0 << 2) /* Unmodified sideband input used */
1103 #define TLB_DEPTH_TYPE_PER_PIXEL (1 << 2) /* QPU result used */
1104 #define TLB_V42_DEPTH_TYPE_INVARIANT (0 << 3) /* Unmodified sideband input used */
1105 #define TLB_V42_DEPTH_TYPE_PER_PIXEL (1 << 3) /* QPU result used */
1107 /* Stencil is a single 32-bit write. */
1108 #define TLB_TYPE_STENCIL_ALPHA ((2 << 6) | (1 << 4))
1111 emit_frag_end(struct v3d_compile
*c
)
1114 if (c->output_sample_mask_index != -1) {
1115 vir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
1119 bool has_any_tlb_color_write
= false;
1120 for (int rt
= 0; rt
< V3D_MAX_DRAW_BUFFERS
; rt
++) {
1121 if (c
->fs_key
->cbufs
& (1 << rt
) && c
->output_color_var
[rt
])
1122 has_any_tlb_color_write
= true;
1125 if (c
->fs_key
->sample_alpha_to_coverage
&& c
->output_color_var
[0]) {
1126 struct nir_variable
*var
= c
->output_color_var
[0];
1127 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
1129 vir_SETMSF_dest(c
, vir_nop_reg(),
1132 vir_FTOC(c
, color
[3])));
1135 struct qreg tlb_reg
= vir_magic_reg(V3D_QPU_WADDR_TLB
);
1136 struct qreg tlbu_reg
= vir_magic_reg(V3D_QPU_WADDR_TLBU
);
1137 if (c
->output_position_index
!= -1) {
1138 struct qinst
*inst
= vir_MOV_dest(c
, tlbu_reg
,
1139 c
->outputs
[c
->output_position_index
]);
1140 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
1142 if (c
->devinfo
->ver
>= 42) {
1143 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_PER_PIXEL
|
1144 TLB_SAMPLE_MODE_PER_PIXEL
);
1146 tlb_specifier
|= TLB_DEPTH_TYPE_PER_PIXEL
;
1148 inst
->uniform
= vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
1152 } else if (c
->s
->info
.fs
.uses_discard
||
1153 !c
->s
->info
.fs
.early_fragment_tests
||
1154 c
->fs_key
->sample_alpha_to_coverage
||
1155 !has_any_tlb_color_write
) {
1156 /* Emit passthrough Z if it needed to be delayed until shader
1157 * end due to potential discards.
1159 * Since (single-threaded) fragment shaders always need a TLB
1160 * write, emit passthrouh Z if we didn't have any color
1161 * buffers and flag us as potentially discarding, so that we
1162 * can use Z as the TLB write.
1164 c
->s
->info
.fs
.uses_discard
= true;
1166 struct qinst
*inst
= vir_MOV_dest(c
, tlbu_reg
,
1168 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
1170 if (c
->devinfo
->ver
>= 42) {
1171 /* The spec says the PER_PIXEL flag is ignored for
1172 * invariant writes, but the simulator demands it.
1174 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_INVARIANT
|
1175 TLB_SAMPLE_MODE_PER_PIXEL
);
1177 tlb_specifier
|= TLB_DEPTH_TYPE_INVARIANT
;
1180 inst
->uniform
= vir_get_uniform_index(c
,
1187 /* XXX: Performance improvement: Merge Z write and color writes TLB
1191 for (int rt
= 0; rt
< V3D_MAX_DRAW_BUFFERS
; rt
++) {
1192 if (!(c
->fs_key
->cbufs
& (1 << rt
)) || !c
->output_color_var
[rt
])
1195 nir_variable
*var
= c
->output_color_var
[rt
];
1196 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
1197 int num_components
= glsl_get_vector_elements(var
->type
);
1198 uint32_t conf
= 0xffffff00;
1201 conf
|= TLB_SAMPLE_MODE_PER_PIXEL
;
1202 conf
|= (7 - rt
) << TLB_RENDER_TARGET_SHIFT
;
1204 if (c
->fs_key
->swap_color_rb
& (1 << rt
))
1205 num_components
= MAX2(num_components
, 3);
1207 assert(num_components
!= 0);
1208 switch (glsl_get_base_type(var
->type
)) {
1209 case GLSL_TYPE_UINT
:
1211 /* The F32 vs I32 distinction was dropped in 4.2. */
1212 if (c
->devinfo
->ver
< 42)
1213 conf
|= TLB_TYPE_I32_COLOR
;
1215 conf
|= TLB_TYPE_F32_COLOR
;
1216 conf
|= ((num_components
- 1) <<
1217 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1219 inst
= vir_MOV_dest(c
, tlbu_reg
, color
[0]);
1220 inst
->uniform
= vir_get_uniform_index(c
,
1224 for (int i
= 1; i
< num_components
; i
++) {
1225 inst
= vir_MOV_dest(c
, tlb_reg
, color
[i
]);
1230 struct qreg r
= color
[0];
1231 struct qreg g
= color
[1];
1232 struct qreg b
= color
[2];
1233 struct qreg a
= color
[3];
1235 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1236 conf
|= TLB_TYPE_F32_COLOR
;
1237 conf
|= ((num_components
- 1) <<
1238 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1240 conf
|= TLB_TYPE_F16_COLOR
;
1241 conf
|= TLB_F16_SWAP_HI_LO
;
1242 if (num_components
>= 3)
1243 conf
|= TLB_VEC_SIZE_4_F16
;
1245 conf
|= TLB_VEC_SIZE_2_F16
;
1248 if (c
->fs_key
->swap_color_rb
& (1 << rt
)) {
1253 if (c
->fs_key
->sample_alpha_to_one
)
1254 a
= vir_uniform_f(c
, 1.0);
1256 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1257 inst
= vir_MOV_dest(c
, tlbu_reg
, r
);
1258 inst
->uniform
= vir_get_uniform_index(c
,
1262 if (num_components
>= 2)
1263 vir_MOV_dest(c
, tlb_reg
, g
);
1264 if (num_components
>= 3)
1265 vir_MOV_dest(c
, tlb_reg
, b
);
1266 if (num_components
>= 4)
1267 vir_MOV_dest(c
, tlb_reg
, a
);
1269 inst
= vir_VFPACK_dest(c
, tlb_reg
, r
, g
);
1271 inst
->dst
= tlbu_reg
;
1272 inst
->uniform
= vir_get_uniform_index(c
,
1277 if (num_components
>= 3)
1278 inst
= vir_VFPACK_dest(c
, tlb_reg
, b
, a
);
1287 vir_VPM_WRITE(struct v3d_compile
*c
, struct qreg val
, uint32_t vpm_index
)
1289 if (c
->devinfo
->ver
>= 40) {
1290 vir_STVPMV(c
, vir_uniform_ui(c
, vpm_index
), val
);
1292 /* XXX: v3d33_vir_vpm_write_setup(c); */
1293 vir_MOV_dest(c
, vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_VPM
), val
);
1298 emit_vert_end(struct v3d_compile
*c
)
1300 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1302 if (c
->devinfo
->ver
>= 40 && c
->devinfo
->ver
<= 42)
1307 v3d_optimize_nir(struct nir_shader
*s
)
1310 unsigned lower_flrp
=
1311 (s
->options
->lower_flrp16
? 16 : 0) |
1312 (s
->options
->lower_flrp32
? 32 : 0) |
1313 (s
->options
->lower_flrp64
? 64 : 0);
1318 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1319 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, NULL
);
1320 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
1321 NIR_PASS(progress
, s
, nir_copy_prop
);
1322 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1323 NIR_PASS(progress
, s
, nir_opt_dce
);
1324 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1325 NIR_PASS(progress
, s
, nir_opt_cse
);
1326 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
1327 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1328 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1330 if (lower_flrp
!= 0) {
1331 bool lower_flrp_progress
= false;
1333 NIR_PASS(lower_flrp_progress
, s
, nir_lower_flrp
,
1335 false /* always_precise */,
1336 s
->options
->lower_ffma
);
1337 if (lower_flrp_progress
) {
1338 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1342 /* Nothing should rematerialize any flrps, so we only
1343 * need to do this lowering once.
1348 NIR_PASS(progress
, s
, nir_opt_undef
);
1351 NIR_PASS(progress
, s
, nir_opt_move_load_ubo
);
1355 driver_location_compare(const void *in_a
, const void *in_b
)
1357 const nir_variable
*const *a
= in_a
;
1358 const nir_variable
*const *b
= in_b
;
1360 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1364 ntq_emit_vpm_read(struct v3d_compile
*c
,
1365 uint32_t *num_components_queued
,
1366 uint32_t *remaining
,
1369 struct qreg vpm
= vir_reg(QFILE_VPM
, vpm_index
);
1371 if (c
->devinfo
->ver
>= 40 ) {
1372 return vir_LDVPMV_IN(c
,
1374 (*num_components_queued
)++));
1377 if (*num_components_queued
!= 0) {
1378 (*num_components_queued
)--;
1379 return vir_MOV(c
, vpm
);
1382 uint32_t num_components
= MIN2(*remaining
, 32);
1384 v3d33_vir_vpm_read_setup(c
, num_components
);
1386 *num_components_queued
= num_components
- 1;
1387 *remaining
-= num_components
;
1389 return vir_MOV(c
, vpm
);
1393 ntq_setup_vpm_inputs(struct v3d_compile
*c
)
1395 /* Figure out how many components of each vertex attribute the shader
1396 * uses. Each variable should have been split to individual
1397 * components and unused ones DCEed. The vertex fetcher will load
1398 * from the start of the attribute to the number of components we
1399 * declare we need in c->vattr_sizes[].
1401 nir_foreach_variable(var
, &c
->s
->inputs
) {
1402 /* No VS attribute array support. */
1403 assert(MAX2(glsl_get_length(var
->type
), 1) == 1);
1405 unsigned loc
= var
->data
.driver_location
;
1406 int start_component
= var
->data
.location_frac
;
1407 int num_components
= glsl_get_components(var
->type
);
1409 c
->vattr_sizes
[loc
] = MAX2(c
->vattr_sizes
[loc
],
1410 start_component
+ num_components
);
1413 unsigned num_components
= 0;
1414 uint32_t vpm_components_queued
= 0;
1415 bool uses_iid
= c
->s
->info
.system_values_read
&
1416 (1ull << SYSTEM_VALUE_INSTANCE_ID
);
1417 bool uses_vid
= c
->s
->info
.system_values_read
&
1418 (1ull << SYSTEM_VALUE_VERTEX_ID
);
1419 num_components
+= uses_iid
;
1420 num_components
+= uses_vid
;
1422 for (int i
= 0; i
< ARRAY_SIZE(c
->vattr_sizes
); i
++)
1423 num_components
+= c
->vattr_sizes
[i
];
1426 c
->iid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1427 &num_components
, ~0);
1431 c
->vid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1432 &num_components
, ~0);
1435 /* The actual loads will happen directly in nir_intrinsic_load_input
1436 * on newer versions.
1438 if (c
->devinfo
->ver
>= 40)
1441 for (int loc
= 0; loc
< ARRAY_SIZE(c
->vattr_sizes
); loc
++) {
1442 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1445 for (int i
= 0; i
< c
->vattr_sizes
[loc
]; i
++) {
1446 c
->inputs
[loc
* 4 + i
] =
1447 ntq_emit_vpm_read(c
,
1448 &vpm_components_queued
,
1455 if (c
->devinfo
->ver
>= 40) {
1456 assert(vpm_components_queued
== num_components
);
1458 assert(vpm_components_queued
== 0);
1459 assert(num_components
== 0);
1464 var_needs_point_coord(struct v3d_compile
*c
, nir_variable
*var
)
1466 return (var
->data
.location
== VARYING_SLOT_PNTC
||
1467 (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1468 (c
->fs_key
->point_sprite_mask
&
1469 (1 << (var
->data
.location
- VARYING_SLOT_VAR0
)))));
1473 program_reads_point_coord(struct v3d_compile
*c
)
1475 nir_foreach_variable(var
, &c
->s
->inputs
) {
1476 if (var_needs_point_coord(c
, var
))
1484 ntq_setup_fs_inputs(struct v3d_compile
*c
)
1486 unsigned num_entries
= 0;
1487 unsigned num_components
= 0;
1488 nir_foreach_variable(var
, &c
->s
->inputs
) {
1490 num_components
+= glsl_get_components(var
->type
);
1493 nir_variable
*vars
[num_entries
];
1496 nir_foreach_variable(var
, &c
->s
->inputs
)
1499 /* Sort the variables so that we emit the input setup in
1500 * driver_location order. This is required for VPM reads, whose data
1501 * is fetched into the VPM in driver_location (TGSI register index)
1504 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1506 for (unsigned i
= 0; i
< num_entries
; i
++) {
1507 nir_variable
*var
= vars
[i
];
1508 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1509 unsigned loc
= var
->data
.driver_location
;
1511 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1512 (loc
+ array_len
) * 4);
1514 if (var
->data
.location
== VARYING_SLOT_POS
) {
1515 emit_fragcoord_input(c
, loc
);
1516 } else if (var_needs_point_coord(c
, var
)) {
1517 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1518 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1520 for (int j
= 0; j
< array_len
; j
++)
1521 emit_fragment_input(c
, loc
+ j
, var
, j
);
1527 ntq_setup_outputs(struct v3d_compile
*c
)
1529 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
1532 nir_foreach_variable(var
, &c
->s
->outputs
) {
1533 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1534 unsigned loc
= var
->data
.driver_location
* 4;
1536 assert(array_len
== 1);
1539 for (int i
= 0; i
< 4 - var
->data
.location_frac
; i
++) {
1540 add_output(c
, loc
+ var
->data
.location_frac
+ i
,
1542 var
->data
.location_frac
+ i
);
1545 switch (var
->data
.location
) {
1546 case FRAG_RESULT_COLOR
:
1547 c
->output_color_var
[0] = var
;
1548 c
->output_color_var
[1] = var
;
1549 c
->output_color_var
[2] = var
;
1550 c
->output_color_var
[3] = var
;
1552 case FRAG_RESULT_DATA0
:
1553 case FRAG_RESULT_DATA1
:
1554 case FRAG_RESULT_DATA2
:
1555 case FRAG_RESULT_DATA3
:
1556 c
->output_color_var
[var
->data
.location
-
1557 FRAG_RESULT_DATA0
] = var
;
1559 case FRAG_RESULT_DEPTH
:
1560 c
->output_position_index
= loc
;
1562 case FRAG_RESULT_SAMPLE_MASK
:
1563 c
->output_sample_mask_index
= loc
;
1570 * Sets up the mapping from nir_register to struct qreg *.
1572 * Each nir_register gets a struct qreg per 32-bit component being stored.
1575 ntq_setup_registers(struct v3d_compile
*c
, struct exec_list
*list
)
1577 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1578 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1579 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1581 nir_reg
->num_components
);
1583 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1585 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1586 qregs
[i
] = vir_get_temp(c
);
1591 ntq_emit_load_const(struct v3d_compile
*c
, nir_load_const_instr
*instr
)
1593 /* XXX perf: Experiment with using immediate loads to avoid having
1594 * these end up in the uniform stream. Watch out for breaking the
1595 * small immediates optimization in the process!
1597 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1598 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1599 qregs
[i
] = vir_uniform_ui(c
, instr
->value
[i
].u32
);
1601 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1605 ntq_emit_ssa_undef(struct v3d_compile
*c
, nir_ssa_undef_instr
*instr
)
1607 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1609 /* VIR needs there to be *some* value, so pick 0 (same as for
1610 * ntq_setup_registers().
1612 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1613 qregs
[i
] = vir_uniform_ui(c
, 0);
1617 ntq_emit_image_size(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1619 assert(instr
->intrinsic
== nir_intrinsic_image_deref_size
);
1620 nir_variable
*var
= nir_intrinsic_get_var(instr
, 0);
1621 unsigned image_index
= var
->data
.driver_location
;
1622 const struct glsl_type
*sampler_type
= glsl_without_array(var
->type
);
1623 bool is_array
= glsl_sampler_type_is_array(sampler_type
);
1625 ntq_store_dest(c
, &instr
->dest
, 0,
1626 vir_uniform(c
, QUNIFORM_IMAGE_WIDTH
, image_index
));
1627 if (instr
->num_components
> 1) {
1628 ntq_store_dest(c
, &instr
->dest
, 1,
1629 vir_uniform(c
, QUNIFORM_IMAGE_HEIGHT
,
1632 if (instr
->num_components
> 2) {
1633 ntq_store_dest(c
, &instr
->dest
, 2,
1636 QUNIFORM_IMAGE_ARRAY_SIZE
:
1637 QUNIFORM_IMAGE_DEPTH
,
1643 vir_emit_tlb_color_read(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1645 assert(c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
);
1647 int rt
= nir_src_as_uint(instr
->src
[0]);
1648 assert(rt
< V3D_MAX_DRAW_BUFFERS
);
1650 int sample_index
= nir_intrinsic_base(instr
) ;
1651 assert(sample_index
== 0); /* XXX: multisample */
1653 int component
= nir_intrinsic_component(instr
);
1654 assert(component
< 4);
1656 /* We need to emit our TLB reads after we have acquired the scoreboard
1657 * lock, or the GPU will hang. Usually, we do our scoreboard locking on
1658 * the last thread switch to improve parallelism, however, that is only
1659 * guaranteed to happen before the tlb color writes.
1661 * To fix that, we make sure we always emit a thread switch before the
1662 * first tlb color read. If that happens to be the last thread switch
1663 * we emit, then everything is fine, but otherwsie, if any code after
1664 * this point needs to emit additional thread switches, then we will
1665 * switch the strategy to locking the scoreboard on the first thread
1666 * switch instead -- see vir_emit_thrsw().
1668 if (!c
->emitted_tlb_load
) {
1669 if (!c
->last_thrsw_at_top_level
) {
1670 assert(c
->devinfo
->ver
>= 41);
1674 c
->emitted_tlb_load
= true;
1677 struct qreg
*color_reads
=
1678 &c
->color_reads
[(rt
* V3D_MAX_SAMPLES
+ sample_index
) * 4];
1680 if (color_reads
[component
].file
== QFILE_NULL
) {
1681 enum pipe_format rt_format
= c
->fs_key
->color_fmt
[rt
].format
;
1682 int num_components
=
1683 util_format_get_nr_components(rt_format
);
1685 const bool swap_rb
= c
->fs_key
->swap_color_rb
& (1 << rt
);
1687 num_components
= MAX2(num_components
, 3);
1689 nir_variable
*var
= c
->output_color_var
[rt
];
1690 enum glsl_base_type type
= glsl_get_base_type(var
->type
);
1692 bool is_int_format
= type
== GLSL_TYPE_INT
||
1693 type
== GLSL_TYPE_UINT
;
1695 bool is_32b_tlb_format
= is_int_format
||
1696 (c
->fs_key
->f32_color_rb
& (1 << rt
));
1698 uint32_t conf
= 0xffffff00;
1699 conf
|= TLB_SAMPLE_MODE_PER_PIXEL
; /* XXX: multisample */
1700 conf
|= (7 - rt
) << TLB_RENDER_TARGET_SHIFT
;
1702 if (is_32b_tlb_format
) {
1703 /* The F32 vs I32 distinction was dropped in 4.2. */
1704 conf
|= (c
->devinfo
->ver
< 42 && is_int_format
) ?
1705 TLB_TYPE_I32_COLOR
: TLB_TYPE_F32_COLOR
;
1707 conf
|= ((num_components
- 1) <<
1708 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1710 conf
|= TLB_TYPE_F16_COLOR
;
1711 conf
|= TLB_F16_SWAP_HI_LO
;
1713 if (num_components
>= 3)
1714 conf
|= TLB_VEC_SIZE_4_F16
;
1716 conf
|= TLB_VEC_SIZE_2_F16
;
1719 struct qreg r
, g
, b
, a
;
1720 if (is_32b_tlb_format
) {
1721 r
= conf
!= 0xffffffff ? vir_TLBU_COLOR_READ(c
, conf
) :
1722 vir_TLB_COLOR_READ(c
);
1723 if (num_components
>= 2)
1724 g
= vir_TLB_COLOR_READ(c
);
1725 if (num_components
>= 3)
1726 b
= vir_TLB_COLOR_READ(c
);
1727 if (num_components
>= 4)
1728 a
= vir_TLB_COLOR_READ(c
);
1730 struct qreg rg
= conf
!= 0xffffffff ?
1731 vir_TLBU_COLOR_READ(c
, conf
) :
1732 vir_TLB_COLOR_READ(c
);
1733 r
= vir_FMOV(c
, rg
);
1734 vir_set_unpack(c
->defs
[r
.index
], 0, V3D_QPU_UNPACK_L
);
1735 g
= vir_FMOV(c
, rg
);
1736 vir_set_unpack(c
->defs
[g
.index
], 0, V3D_QPU_UNPACK_H
);
1738 if (num_components
> 2) {
1739 struct qreg ba
= vir_TLB_COLOR_READ(c
);
1740 b
= vir_FMOV(c
, ba
);
1741 vir_set_unpack(c
->defs
[b
.index
], 0,
1743 a
= vir_FMOV(c
, ba
);
1744 vir_set_unpack(c
->defs
[a
.index
], 0,
1749 color_reads
[0] = swap_rb
? b
: r
;
1750 if (num_components
>= 2)
1752 if (num_components
>= 3)
1753 color_reads
[2] = swap_rb
? r
: b
;
1754 if (num_components
>= 4)
1758 assert(color_reads
[component
].file
!= QFILE_NULL
);
1759 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, color_reads
[component
]));
1763 ntq_emit_load_uniform(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1765 if (nir_src_is_const(instr
->src
[0])) {
1766 int offset
= (nir_intrinsic_base(instr
) +
1767 nir_src_as_uint(instr
->src
[0]));
1768 assert(offset
% 4 == 0);
1769 /* We need dwords */
1770 offset
= offset
/ 4;
1771 for (int i
= 0; i
< instr
->num_components
; i
++) {
1772 ntq_store_dest(c
, &instr
->dest
, i
,
1773 vir_uniform(c
, QUNIFORM_UNIFORM
,
1777 ntq_emit_tmu_general(c
, instr
, false);
1782 ntq_emit_load_input(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1784 /* XXX: Use ldvpmv (uniform offset) or ldvpmd (non-uniform offset)
1785 * and enable PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR.
1788 nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[0]);
1790 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
&& c
->devinfo
->ver
>= 40) {
1791 /* Emit the LDVPM directly now, rather than at the top
1792 * of the shader like we did for V3D 3.x (which needs
1793 * vpmsetup when not just taking the next offset).
1795 * Note that delaying like this may introduce stalls,
1796 * as LDVPMV takes a minimum of 1 instruction but may
1797 * be slower if the VPM unit is busy with another QPU.
1800 if (c
->s
->info
.system_values_read
&
1801 (1ull << SYSTEM_VALUE_INSTANCE_ID
)) {
1804 if (c
->s
->info
.system_values_read
&
1805 (1ull << SYSTEM_VALUE_VERTEX_ID
)) {
1808 for (int i
= 0; i
< offset
; i
++)
1809 index
+= c
->vattr_sizes
[i
];
1810 index
+= nir_intrinsic_component(instr
);
1811 for (int i
= 0; i
< instr
->num_components
; i
++) {
1812 struct qreg vpm_offset
= vir_uniform_ui(c
, index
++);
1813 ntq_store_dest(c
, &instr
->dest
, i
,
1814 vir_LDVPMV_IN(c
, vpm_offset
));
1817 for (int i
= 0; i
< instr
->num_components
; i
++) {
1818 int comp
= nir_intrinsic_component(instr
) + i
;
1819 ntq_store_dest(c
, &instr
->dest
, i
,
1820 vir_MOV(c
, c
->inputs
[offset
* 4 + comp
]));
1826 ntq_emit_store_output(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1828 /* XXX perf: Use stvpmv with uniform non-constant offsets and
1829 * stvpmd with non-uniform offsets and enable
1830 * PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR.
1832 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1833 unsigned offset
= ((nir_intrinsic_base(instr
) +
1834 nir_src_as_uint(instr
->src
[1])) * 4 +
1835 nir_intrinsic_component(instr
));
1836 for (int i
= 0; i
< instr
->num_components
; i
++) {
1837 c
->outputs
[offset
+ i
] =
1838 vir_MOV(c
, ntq_get_src(c
, instr
->src
[0], i
));
1841 assert(instr
->num_components
== 1);
1844 ntq_get_src(c
, instr
->src
[0], 0),
1845 nir_intrinsic_base(instr
));
1850 ntq_emit_intrinsic(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1852 switch (instr
->intrinsic
) {
1853 case nir_intrinsic_load_uniform
:
1854 ntq_emit_load_uniform(c
, instr
);
1857 case nir_intrinsic_load_ubo
:
1858 ntq_emit_tmu_general(c
, instr
, false);
1861 case nir_intrinsic_ssbo_atomic_add
:
1862 case nir_intrinsic_ssbo_atomic_imin
:
1863 case nir_intrinsic_ssbo_atomic_umin
:
1864 case nir_intrinsic_ssbo_atomic_imax
:
1865 case nir_intrinsic_ssbo_atomic_umax
:
1866 case nir_intrinsic_ssbo_atomic_and
:
1867 case nir_intrinsic_ssbo_atomic_or
:
1868 case nir_intrinsic_ssbo_atomic_xor
:
1869 case nir_intrinsic_ssbo_atomic_exchange
:
1870 case nir_intrinsic_ssbo_atomic_comp_swap
:
1871 case nir_intrinsic_load_ssbo
:
1872 case nir_intrinsic_store_ssbo
:
1873 ntq_emit_tmu_general(c
, instr
, false);
1876 case nir_intrinsic_shared_atomic_add
:
1877 case nir_intrinsic_shared_atomic_imin
:
1878 case nir_intrinsic_shared_atomic_umin
:
1879 case nir_intrinsic_shared_atomic_imax
:
1880 case nir_intrinsic_shared_atomic_umax
:
1881 case nir_intrinsic_shared_atomic_and
:
1882 case nir_intrinsic_shared_atomic_or
:
1883 case nir_intrinsic_shared_atomic_xor
:
1884 case nir_intrinsic_shared_atomic_exchange
:
1885 case nir_intrinsic_shared_atomic_comp_swap
:
1886 case nir_intrinsic_load_shared
:
1887 case nir_intrinsic_store_shared
:
1888 case nir_intrinsic_load_scratch
:
1889 case nir_intrinsic_store_scratch
:
1890 ntq_emit_tmu_general(c
, instr
, true);
1893 case nir_intrinsic_image_deref_load
:
1894 case nir_intrinsic_image_deref_store
:
1895 case nir_intrinsic_image_deref_atomic_add
:
1896 case nir_intrinsic_image_deref_atomic_min
:
1897 case nir_intrinsic_image_deref_atomic_max
:
1898 case nir_intrinsic_image_deref_atomic_and
:
1899 case nir_intrinsic_image_deref_atomic_or
:
1900 case nir_intrinsic_image_deref_atomic_xor
:
1901 case nir_intrinsic_image_deref_atomic_exchange
:
1902 case nir_intrinsic_image_deref_atomic_comp_swap
:
1903 v3d40_vir_emit_image_load_store(c
, instr
);
1906 case nir_intrinsic_get_buffer_size
:
1907 ntq_store_dest(c
, &instr
->dest
, 0,
1908 vir_uniform(c
, QUNIFORM_GET_BUFFER_SIZE
,
1909 nir_src_as_uint(instr
->src
[0])));
1912 case nir_intrinsic_load_user_clip_plane
:
1913 for (int i
= 0; i
< instr
->num_components
; i
++) {
1914 ntq_store_dest(c
, &instr
->dest
, i
,
1915 vir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1916 nir_intrinsic_ucp_id(instr
) *
1921 case nir_intrinsic_load_viewport_x_scale
:
1922 ntq_store_dest(c
, &instr
->dest
, 0,
1923 vir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
, 0));
1926 case nir_intrinsic_load_viewport_y_scale
:
1927 ntq_store_dest(c
, &instr
->dest
, 0,
1928 vir_uniform(c
, QUNIFORM_VIEWPORT_Y_SCALE
, 0));
1931 case nir_intrinsic_load_viewport_z_scale
:
1932 ntq_store_dest(c
, &instr
->dest
, 0,
1933 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0));
1936 case nir_intrinsic_load_viewport_z_offset
:
1937 ntq_store_dest(c
, &instr
->dest
, 0,
1938 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0));
1941 case nir_intrinsic_load_alpha_ref_float
:
1942 ntq_store_dest(c
, &instr
->dest
, 0,
1943 vir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1946 case nir_intrinsic_load_sample_mask_in
:
1947 ntq_store_dest(c
, &instr
->dest
, 0, vir_MSF(c
));
1950 case nir_intrinsic_load_helper_invocation
:
1951 vir_set_pf(vir_MSF_dest(c
, vir_nop_reg()), V3D_QPU_PF_PUSHZ
);
1952 ntq_store_dest(c
, &instr
->dest
, 0,
1953 vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1954 vir_uniform_ui(c
, ~0),
1955 vir_uniform_ui(c
, 0))));
1958 case nir_intrinsic_load_front_face
:
1959 /* The register contains 0 (front) or 1 (back), and we need to
1960 * turn it into a NIR bool where true means front.
1962 ntq_store_dest(c
, &instr
->dest
, 0,
1964 vir_uniform_ui(c
, -1),
1968 case nir_intrinsic_load_instance_id
:
1969 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->iid
));
1972 case nir_intrinsic_load_vertex_id
:
1973 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->vid
));
1976 case nir_intrinsic_load_tlb_color_v3d
:
1977 vir_emit_tlb_color_read(c
, instr
);
1980 case nir_intrinsic_load_input
:
1981 ntq_emit_load_input(c
, instr
);
1984 case nir_intrinsic_store_output
:
1985 ntq_emit_store_output(c
, instr
);
1988 case nir_intrinsic_image_deref_size
:
1989 ntq_emit_image_size(c
, instr
);
1992 case nir_intrinsic_discard
:
1993 if (vir_in_nonuniform_control_flow(c
)) {
1994 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
1996 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
1997 vir_uniform_ui(c
, 0)),
2000 vir_SETMSF_dest(c
, vir_nop_reg(),
2001 vir_uniform_ui(c
, 0));
2005 case nir_intrinsic_discard_if
: {
2006 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, instr
->src
[0]);
2008 if (vir_in_nonuniform_control_flow(c
)) {
2009 struct qinst
*exec_flag
= vir_MOV_dest(c
, vir_nop_reg(),
2011 if (cond
== V3D_QPU_COND_IFA
) {
2012 vir_set_uf(exec_flag
, V3D_QPU_UF_ANDZ
);
2014 vir_set_uf(exec_flag
, V3D_QPU_UF_NORNZ
);
2015 cond
= V3D_QPU_COND_IFA
;
2019 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
2020 vir_uniform_ui(c
, 0)), cond
);
2025 case nir_intrinsic_memory_barrier
:
2026 case nir_intrinsic_memory_barrier_atomic_counter
:
2027 case nir_intrinsic_memory_barrier_buffer
:
2028 case nir_intrinsic_memory_barrier_image
:
2029 case nir_intrinsic_memory_barrier_shared
:
2030 case nir_intrinsic_group_memory_barrier
:
2031 /* We don't do any instruction scheduling of these NIR
2032 * instructions between each other, so we just need to make
2033 * sure that the TMU operations before the barrier are flushed
2034 * before the ones after the barrier. That is currently
2035 * handled by having a THRSW in each of them and a LDTMU
2036 * series or a TMUWT after.
2040 case nir_intrinsic_barrier
:
2041 /* Emit a TSY op to get all invocations in the workgroup
2042 * (actually supergroup) to block until the last invocation
2043 * reaches the TSY op.
2045 if (c
->devinfo
->ver
>= 42) {
2046 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
2047 V3D_QPU_WADDR_SYNCB
));
2049 struct qinst
*sync
=
2050 vir_BARRIERID_dest(c
,
2051 vir_reg(QFILE_MAGIC
,
2052 V3D_QPU_WADDR_SYNCU
));
2054 vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
2056 V3D_TSY_WAIT_INC_CHECK
);
2060 /* The blocking of a TSY op only happens at the next thread
2061 * switch. No texturing may be outstanding at the time of a
2062 * TSY blocking operation.
2067 case nir_intrinsic_load_num_work_groups
:
2068 for (int i
= 0; i
< 3; i
++) {
2069 ntq_store_dest(c
, &instr
->dest
, i
,
2070 vir_uniform(c
, QUNIFORM_NUM_WORK_GROUPS
,
2075 case nir_intrinsic_load_local_invocation_index
:
2076 ntq_store_dest(c
, &instr
->dest
, 0,
2077 vir_SHR(c
, c
->cs_payload
[1],
2078 vir_uniform_ui(c
, 32 - c
->local_invocation_index_bits
)));
2081 case nir_intrinsic_load_work_group_id
:
2082 ntq_store_dest(c
, &instr
->dest
, 0,
2083 vir_AND(c
, c
->cs_payload
[0],
2084 vir_uniform_ui(c
, 0xffff)));
2085 ntq_store_dest(c
, &instr
->dest
, 1,
2086 vir_SHR(c
, c
->cs_payload
[0],
2087 vir_uniform_ui(c
, 16)));
2088 ntq_store_dest(c
, &instr
->dest
, 2,
2089 vir_AND(c
, c
->cs_payload
[1],
2090 vir_uniform_ui(c
, 0xffff)));
2093 case nir_intrinsic_load_subgroup_id
:
2094 ntq_store_dest(c
, &instr
->dest
, 0, vir_EIDX(c
));
2098 fprintf(stderr
, "Unknown intrinsic: ");
2099 nir_print_instr(&instr
->instr
, stderr
);
2100 fprintf(stderr
, "\n");
2105 /* Clears (activates) the execute flags for any channels whose jump target
2106 * matches this block.
2108 * XXX perf: Could we be using flpush/flpop somehow for our execution channel
2111 * XXX perf: For uniform control flow, we should be able to skip c->execute
2112 * handling entirely.
2115 ntq_activate_execute_for_block(struct v3d_compile
*c
)
2117 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
2118 c
->execute
, vir_uniform_ui(c
, c
->cur_block
->index
)),
2121 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
2125 ntq_emit_uniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
2127 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
2128 bool empty_else_block
=
2129 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
2130 exec_list_is_empty(&nir_else_block
->instr_list
));
2132 struct qblock
*then_block
= vir_new_block(c
);
2133 struct qblock
*after_block
= vir_new_block(c
);
2134 struct qblock
*else_block
;
2135 if (empty_else_block
)
2136 else_block
= after_block
;
2138 else_block
= vir_new_block(c
);
2140 /* Set up the flags for the IF condition (taking the THEN branch). */
2141 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
2144 vir_BRANCH(c
, cond
== V3D_QPU_COND_IFA
?
2145 V3D_QPU_BRANCH_COND_ALLNA
:
2146 V3D_QPU_BRANCH_COND_ALLA
);
2147 vir_link_blocks(c
->cur_block
, else_block
);
2148 vir_link_blocks(c
->cur_block
, then_block
);
2150 /* Process the THEN block. */
2151 vir_set_emit_block(c
, then_block
);
2152 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
2154 if (!empty_else_block
) {
2155 /* At the end of the THEN block, jump to ENDIF */
2156 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALWAYS
);
2157 vir_link_blocks(c
->cur_block
, after_block
);
2159 /* Emit the else block. */
2160 vir_set_emit_block(c
, else_block
);
2161 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
2164 vir_link_blocks(c
->cur_block
, after_block
);
2166 vir_set_emit_block(c
, after_block
);
2170 ntq_emit_nonuniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
2172 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
2173 bool empty_else_block
=
2174 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
2175 exec_list_is_empty(&nir_else_block
->instr_list
));
2177 struct qblock
*then_block
= vir_new_block(c
);
2178 struct qblock
*after_block
= vir_new_block(c
);
2179 struct qblock
*else_block
;
2180 if (empty_else_block
)
2181 else_block
= after_block
;
2183 else_block
= vir_new_block(c
);
2185 bool was_uniform_control_flow
= false;
2186 if (!vir_in_nonuniform_control_flow(c
)) {
2187 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
2188 was_uniform_control_flow
= true;
2191 /* Set up the flags for the IF condition (taking the THEN branch). */
2192 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
2194 /* Update the flags+cond to mean "Taking the ELSE branch (!cond) and
2195 * was previously active (execute Z) for updating the exec flags.
2197 if (was_uniform_control_flow
) {
2198 cond
= v3d_qpu_cond_invert(cond
);
2200 struct qinst
*inst
= vir_MOV_dest(c
, vir_nop_reg(), c
->execute
);
2201 if (cond
== V3D_QPU_COND_IFA
) {
2202 vir_set_uf(inst
, V3D_QPU_UF_NORNZ
);
2204 vir_set_uf(inst
, V3D_QPU_UF_ANDZ
);
2205 cond
= V3D_QPU_COND_IFA
;
2209 vir_MOV_cond(c
, cond
,
2211 vir_uniform_ui(c
, else_block
->index
));
2213 /* Jump to ELSE if nothing is active for THEN, otherwise fall
2216 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
2217 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLNA
);
2218 vir_link_blocks(c
->cur_block
, else_block
);
2219 vir_link_blocks(c
->cur_block
, then_block
);
2221 /* Process the THEN block. */
2222 vir_set_emit_block(c
, then_block
);
2223 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
2225 if (!empty_else_block
) {
2226 /* Handle the end of the THEN block. First, all currently
2227 * active channels update their execute flags to point to
2230 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2232 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2233 vir_uniform_ui(c
, after_block
->index
));
2235 /* If everything points at ENDIF, then jump there immediately. */
2236 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
2238 vir_uniform_ui(c
, after_block
->index
)),
2240 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLA
);
2241 vir_link_blocks(c
->cur_block
, after_block
);
2242 vir_link_blocks(c
->cur_block
, else_block
);
2244 vir_set_emit_block(c
, else_block
);
2245 ntq_activate_execute_for_block(c
);
2246 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
2249 vir_link_blocks(c
->cur_block
, after_block
);
2251 vir_set_emit_block(c
, after_block
);
2252 if (was_uniform_control_flow
)
2253 c
->execute
= c
->undef
;
2255 ntq_activate_execute_for_block(c
);
2259 ntq_emit_if(struct v3d_compile
*c
, nir_if
*nif
)
2261 bool was_in_control_flow
= c
->in_control_flow
;
2262 c
->in_control_flow
= true;
2263 if (!vir_in_nonuniform_control_flow(c
) &&
2264 nir_src_is_dynamically_uniform(nif
->condition
)) {
2265 ntq_emit_uniform_if(c
, nif
);
2267 ntq_emit_nonuniform_if(c
, nif
);
2269 c
->in_control_flow
= was_in_control_flow
;
2273 ntq_emit_jump(struct v3d_compile
*c
, nir_jump_instr
*jump
)
2275 switch (jump
->type
) {
2276 case nir_jump_break
:
2277 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2279 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2280 vir_uniform_ui(c
, c
->loop_break_block
->index
));
2283 case nir_jump_continue
:
2284 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2286 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2287 vir_uniform_ui(c
, c
->loop_cont_block
->index
));
2290 case nir_jump_return
:
2291 unreachable("All returns shouold be lowered\n");
2296 ntq_emit_instr(struct v3d_compile
*c
, nir_instr
*instr
)
2298 switch (instr
->type
) {
2299 case nir_instr_type_deref
:
2300 /* ignored, will be walked by the intrinsic using it. */
2303 case nir_instr_type_alu
:
2304 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
2307 case nir_instr_type_intrinsic
:
2308 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
2311 case nir_instr_type_load_const
:
2312 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
2315 case nir_instr_type_ssa_undef
:
2316 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
2319 case nir_instr_type_tex
:
2320 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
2323 case nir_instr_type_jump
:
2324 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
2328 fprintf(stderr
, "Unknown NIR instr type: ");
2329 nir_print_instr(instr
, stderr
);
2330 fprintf(stderr
, "\n");
2336 ntq_emit_block(struct v3d_compile
*c
, nir_block
*block
)
2338 nir_foreach_instr(instr
, block
) {
2339 ntq_emit_instr(c
, instr
);
2343 static void ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
2346 ntq_emit_loop(struct v3d_compile
*c
, nir_loop
*loop
)
2348 bool was_in_control_flow
= c
->in_control_flow
;
2349 c
->in_control_flow
= true;
2351 bool was_uniform_control_flow
= false;
2352 if (!vir_in_nonuniform_control_flow(c
)) {
2353 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
2354 was_uniform_control_flow
= true;
2357 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
2358 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
2360 c
->loop_cont_block
= vir_new_block(c
);
2361 c
->loop_break_block
= vir_new_block(c
);
2363 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2364 vir_set_emit_block(c
, c
->loop_cont_block
);
2365 ntq_activate_execute_for_block(c
);
2367 ntq_emit_cf_list(c
, &loop
->body
);
2369 /* Re-enable any previous continues now, so our ANYA check below
2372 * XXX: Use the .ORZ flags update, instead.
2374 vir_set_pf(vir_XOR_dest(c
,
2377 vir_uniform_ui(c
, c
->loop_cont_block
->index
)),
2379 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
2381 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
2383 struct qinst
*branch
= vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ANYA
);
2384 /* Pixels that were not dispatched or have been discarded should not
2385 * contribute to looping again.
2387 branch
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_P
;
2388 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2389 vir_link_blocks(c
->cur_block
, c
->loop_break_block
);
2391 vir_set_emit_block(c
, c
->loop_break_block
);
2392 if (was_uniform_control_flow
)
2393 c
->execute
= c
->undef
;
2395 ntq_activate_execute_for_block(c
);
2397 c
->loop_break_block
= save_loop_break_block
;
2398 c
->loop_cont_block
= save_loop_cont_block
;
2402 c
->in_control_flow
= was_in_control_flow
;
2406 ntq_emit_function(struct v3d_compile
*c
, nir_function_impl
*func
)
2408 fprintf(stderr
, "FUNCTIONS not handled.\n");
2413 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
)
2415 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2416 switch (node
->type
) {
2417 case nir_cf_node_block
:
2418 ntq_emit_block(c
, nir_cf_node_as_block(node
));
2421 case nir_cf_node_if
:
2422 ntq_emit_if(c
, nir_cf_node_as_if(node
));
2425 case nir_cf_node_loop
:
2426 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
2429 case nir_cf_node_function
:
2430 ntq_emit_function(c
, nir_cf_node_as_function(node
));
2434 fprintf(stderr
, "Unknown NIR node type\n");
2441 ntq_emit_impl(struct v3d_compile
*c
, nir_function_impl
*impl
)
2443 ntq_setup_registers(c
, &impl
->registers
);
2444 ntq_emit_cf_list(c
, &impl
->body
);
2448 nir_to_vir(struct v3d_compile
*c
)
2450 switch (c
->s
->info
.stage
) {
2451 case MESA_SHADER_FRAGMENT
:
2452 c
->payload_w
= vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2453 c
->payload_w_centroid
= vir_MOV(c
, vir_reg(QFILE_REG
, 1));
2454 c
->payload_z
= vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2456 /* V3D 4.x can disable implicit point coordinate varyings if
2457 * they are not used.
2459 if (c
->fs_key
->is_points
&&
2460 (c
->devinfo
->ver
< 40 || program_reads_point_coord(c
))) {
2461 c
->point_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2462 c
->point_y
= emit_fragment_varying(c
, NULL
, 0, 0);
2463 c
->uses_implicit_point_line_varyings
= true;
2464 } else if (c
->fs_key
->is_lines
&& c
->devinfo
->ver
< 40) {
2465 c
->line_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2466 c
->uses_implicit_point_line_varyings
= true;
2469 case MESA_SHADER_COMPUTE
:
2470 /* Set up the TSO for barriers, assuming we do some. */
2471 if (c
->devinfo
->ver
< 42) {
2472 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
2473 V3D_QPU_WADDR_SYNC
));
2476 c
->cs_payload
[0] = vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2477 c
->cs_payload
[1] = vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2479 /* Set up the division between gl_LocalInvocationIndex and
2480 * wg_in_mem in the payload reg.
2482 int wg_size
= (c
->s
->info
.cs
.local_size
[0] *
2483 c
->s
->info
.cs
.local_size
[1] *
2484 c
->s
->info
.cs
.local_size
[2]);
2485 c
->local_invocation_index_bits
=
2486 ffs(util_next_power_of_two(MAX2(wg_size
, 64))) - 1;
2487 assert(c
->local_invocation_index_bits
<= 8);
2489 if (c
->s
->info
.cs
.shared_size
) {
2490 struct qreg wg_in_mem
= vir_SHR(c
, c
->cs_payload
[1],
2491 vir_uniform_ui(c
, 16));
2492 if (c
->s
->info
.cs
.local_size
[0] != 1 ||
2493 c
->s
->info
.cs
.local_size
[1] != 1 ||
2494 c
->s
->info
.cs
.local_size
[2] != 1) {
2496 c
->local_invocation_index_bits
);
2497 int wg_mask
= (1 << wg_bits
) - 1;
2498 wg_in_mem
= vir_AND(c
, wg_in_mem
,
2499 vir_uniform_ui(c
, wg_mask
));
2501 struct qreg shared_per_wg
=
2502 vir_uniform_ui(c
, c
->s
->info
.cs
.shared_size
);
2504 c
->cs_shared_offset
=
2506 vir_uniform(c
, QUNIFORM_SHARED_OFFSET
,0),
2507 vir_UMUL(c
, wg_in_mem
, shared_per_wg
));
2514 if (c
->s
->scratch_size
) {
2515 v3d_setup_spill_base(c
);
2516 c
->spill_size
+= V3D_CHANNELS
* c
->s
->scratch_size
;
2519 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
)
2520 ntq_setup_fs_inputs(c
);
2522 ntq_setup_vpm_inputs(c
);
2524 ntq_setup_outputs(c
);
2526 /* Find the main function and emit the body. */
2527 nir_foreach_function(function
, c
->s
) {
2528 assert(strcmp(function
->name
, "main") == 0);
2529 assert(function
->impl
);
2530 ntq_emit_impl(c
, function
->impl
);
2534 const nir_shader_compiler_options v3d_nir_options
= {
2535 .lower_all_io_to_temps
= true,
2536 .lower_extract_byte
= true,
2537 .lower_extract_word
= true,
2538 .lower_bitfield_insert_to_shifts
= true,
2539 .lower_bitfield_extract_to_shifts
= true,
2540 .lower_bitfield_reverse
= true,
2541 .lower_bit_count
= true,
2542 .lower_cs_local_id_from_index
= true,
2543 .lower_ffract
= true,
2545 .lower_pack_unorm_2x16
= true,
2546 .lower_pack_snorm_2x16
= true,
2547 .lower_pack_unorm_4x8
= true,
2548 .lower_pack_snorm_4x8
= true,
2549 .lower_unpack_unorm_4x8
= true,
2550 .lower_unpack_snorm_4x8
= true,
2551 .lower_pack_half_2x16
= true,
2552 .lower_unpack_half_2x16
= true,
2554 .lower_find_lsb
= true,
2556 .lower_flrp32
= true,
2559 .lower_fsqrt
= true,
2560 .lower_ifind_msb
= true,
2561 .lower_isign
= true,
2562 .lower_ldexp
= true,
2563 .lower_mul_high
= true,
2564 .lower_wpos_pntc
= true,
2565 .lower_rotate
= true,
2569 * When demoting a shader down to single-threaded, removes the THRSW
2570 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2574 vir_remove_thrsw(struct v3d_compile
*c
)
2576 vir_for_each_block(block
, c
) {
2577 vir_for_each_inst_safe(inst
, block
) {
2578 if (inst
->qpu
.sig
.thrsw
)
2579 vir_remove_instruction(c
, inst
);
2583 c
->last_thrsw
= NULL
;
2587 vir_emit_last_thrsw(struct v3d_compile
*c
)
2589 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2590 * switching, so disable threads if we didn't do any TMU ops (each of
2591 * which would have emitted a THRSW).
2593 if (!c
->last_thrsw_at_top_level
&& c
->devinfo
->ver
< 41) {
2596 vir_remove_thrsw(c
);
2600 /* If we're threaded and the last THRSW was in conditional code, then
2601 * we need to emit another one so that we can flag it as the last
2604 if (c
->last_thrsw
&& !c
->last_thrsw_at_top_level
) {
2605 assert(c
->devinfo
->ver
>= 41);
2609 /* If we're threaded, then we need to mark the last THRSW instruction
2610 * so we can emit a pair of them at QPU emit time.
2612 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2613 * post-last-THRSW state, so we can skip this.
2615 if (!c
->last_thrsw
&& c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2616 assert(c
->devinfo
->ver
>= 41);
2621 c
->last_thrsw
->is_last_thrsw
= true;
2624 /* There's a flag in the shader for "center W is needed for reasons other than
2625 * non-centroid varyings", so we just walk the program after VIR optimization
2626 * to see if it's used. It should be harmless to set even if we only use
2627 * center W for varyings.
2630 vir_check_payload_w(struct v3d_compile
*c
)
2632 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
2635 vir_for_each_inst_inorder(inst
, c
) {
2636 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
2637 if (inst
->src
[i
].file
== QFILE_REG
&&
2638 inst
->src
[i
].index
== 0) {
2639 c
->uses_center_w
= true;
2648 v3d_nir_to_vir(struct v3d_compile
*c
)
2650 if (V3D_DEBUG
& (V3D_DEBUG_NIR
|
2651 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2652 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2653 vir_get_stage_name(c
),
2654 c
->program_id
, c
->variant_id
);
2655 nir_print_shader(c
->s
, stderr
);
2660 /* Emit the last THRSW before STVPM and TLB writes. */
2661 vir_emit_last_thrsw(c
);
2663 switch (c
->s
->info
.stage
) {
2664 case MESA_SHADER_FRAGMENT
:
2667 case MESA_SHADER_VERTEX
:
2670 case MESA_SHADER_COMPUTE
:
2673 unreachable("bad stage");
2676 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2677 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2678 fprintf(stderr
, "%s prog %d/%d pre-opt VIR:\n",
2679 vir_get_stage_name(c
),
2680 c
->program_id
, c
->variant_id
);
2682 fprintf(stderr
, "\n");
2687 vir_check_payload_w(c
);
2689 /* XXX perf: On VC4, we do a VIR-level instruction scheduling here.
2690 * We used that on that platform to pipeline TMU writes and reduce the
2691 * number of thread switches, as well as try (mostly successfully) to
2692 * reduce maximum register pressure to allow more threads. We should
2693 * do something of that sort for V3D -- either instruction scheduling
2694 * here, or delay the the THRSW and LDTMUs from our texture
2695 * instructions until the results are needed.
2698 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2699 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2700 fprintf(stderr
, "%s prog %d/%d VIR:\n",
2701 vir_get_stage_name(c
),
2702 c
->program_id
, c
->variant_id
);
2704 fprintf(stderr
, "\n");
2707 /* Attempt to allocate registers for the temporaries. If we fail,
2708 * reduce thread count and try again.
2710 int min_threads
= (c
->devinfo
->ver
>= 41) ? 2 : 1;
2711 struct qpu_reg
*temp_registers
;
2714 temp_registers
= v3d_register_allocate(c
, &spilled
);
2721 if (c
->threads
== min_threads
) {
2722 fprintf(stderr
, "Failed to register allocate at %d threads:\n",
2731 if (c
->threads
== 1)
2732 vir_remove_thrsw(c
);
2736 (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2737 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
)))) {
2738 fprintf(stderr
, "%s prog %d/%d spilled VIR:\n",
2739 vir_get_stage_name(c
),
2740 c
->program_id
, c
->variant_id
);
2742 fprintf(stderr
, "\n");
2745 v3d_vir_to_qpu(c
, temp_registers
);