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_intrinsic(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1767 switch (instr
->intrinsic
) {
1768 case nir_intrinsic_load_uniform
:
1769 if (nir_src_is_const(instr
->src
[0])) {
1770 int offset
= (nir_intrinsic_base(instr
) +
1771 nir_src_as_uint(instr
->src
[0]));
1772 assert(offset
% 4 == 0);
1773 /* We need dwords */
1774 offset
= offset
/ 4;
1775 for (int i
= 0; i
< instr
->num_components
; i
++) {
1776 ntq_store_dest(c
, &instr
->dest
, i
,
1777 vir_uniform(c
, QUNIFORM_UNIFORM
,
1781 ntq_emit_tmu_general(c
, instr
, false);
1785 case nir_intrinsic_load_ubo
:
1786 ntq_emit_tmu_general(c
, instr
, false);
1789 case nir_intrinsic_ssbo_atomic_add
:
1790 case nir_intrinsic_ssbo_atomic_imin
:
1791 case nir_intrinsic_ssbo_atomic_umin
:
1792 case nir_intrinsic_ssbo_atomic_imax
:
1793 case nir_intrinsic_ssbo_atomic_umax
:
1794 case nir_intrinsic_ssbo_atomic_and
:
1795 case nir_intrinsic_ssbo_atomic_or
:
1796 case nir_intrinsic_ssbo_atomic_xor
:
1797 case nir_intrinsic_ssbo_atomic_exchange
:
1798 case nir_intrinsic_ssbo_atomic_comp_swap
:
1799 case nir_intrinsic_load_ssbo
:
1800 case nir_intrinsic_store_ssbo
:
1801 ntq_emit_tmu_general(c
, instr
, false);
1804 case nir_intrinsic_shared_atomic_add
:
1805 case nir_intrinsic_shared_atomic_imin
:
1806 case nir_intrinsic_shared_atomic_umin
:
1807 case nir_intrinsic_shared_atomic_imax
:
1808 case nir_intrinsic_shared_atomic_umax
:
1809 case nir_intrinsic_shared_atomic_and
:
1810 case nir_intrinsic_shared_atomic_or
:
1811 case nir_intrinsic_shared_atomic_xor
:
1812 case nir_intrinsic_shared_atomic_exchange
:
1813 case nir_intrinsic_shared_atomic_comp_swap
:
1814 case nir_intrinsic_load_shared
:
1815 case nir_intrinsic_store_shared
:
1816 case nir_intrinsic_load_scratch
:
1817 case nir_intrinsic_store_scratch
:
1818 ntq_emit_tmu_general(c
, instr
, true);
1821 case nir_intrinsic_image_deref_load
:
1822 case nir_intrinsic_image_deref_store
:
1823 case nir_intrinsic_image_deref_atomic_add
:
1824 case nir_intrinsic_image_deref_atomic_min
:
1825 case nir_intrinsic_image_deref_atomic_max
:
1826 case nir_intrinsic_image_deref_atomic_and
:
1827 case nir_intrinsic_image_deref_atomic_or
:
1828 case nir_intrinsic_image_deref_atomic_xor
:
1829 case nir_intrinsic_image_deref_atomic_exchange
:
1830 case nir_intrinsic_image_deref_atomic_comp_swap
:
1831 v3d40_vir_emit_image_load_store(c
, instr
);
1834 case nir_intrinsic_get_buffer_size
:
1835 ntq_store_dest(c
, &instr
->dest
, 0,
1836 vir_uniform(c
, QUNIFORM_GET_BUFFER_SIZE
,
1837 nir_src_as_uint(instr
->src
[0])));
1840 case nir_intrinsic_load_user_clip_plane
:
1841 for (int i
= 0; i
< instr
->num_components
; i
++) {
1842 ntq_store_dest(c
, &instr
->dest
, i
,
1843 vir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1844 nir_intrinsic_ucp_id(instr
) *
1849 case nir_intrinsic_load_viewport_x_scale
:
1850 ntq_store_dest(c
, &instr
->dest
, 0,
1851 vir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
, 0));
1854 case nir_intrinsic_load_viewport_y_scale
:
1855 ntq_store_dest(c
, &instr
->dest
, 0,
1856 vir_uniform(c
, QUNIFORM_VIEWPORT_Y_SCALE
, 0));
1859 case nir_intrinsic_load_viewport_z_scale
:
1860 ntq_store_dest(c
, &instr
->dest
, 0,
1861 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0));
1864 case nir_intrinsic_load_viewport_z_offset
:
1865 ntq_store_dest(c
, &instr
->dest
, 0,
1866 vir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0));
1869 case nir_intrinsic_load_alpha_ref_float
:
1870 ntq_store_dest(c
, &instr
->dest
, 0,
1871 vir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1874 case nir_intrinsic_load_sample_mask_in
:
1875 ntq_store_dest(c
, &instr
->dest
, 0, vir_MSF(c
));
1878 case nir_intrinsic_load_helper_invocation
:
1879 vir_set_pf(vir_MSF_dest(c
, vir_nop_reg()), V3D_QPU_PF_PUSHZ
);
1880 ntq_store_dest(c
, &instr
->dest
, 0,
1881 vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1882 vir_uniform_ui(c
, ~0),
1883 vir_uniform_ui(c
, 0))));
1886 case nir_intrinsic_load_front_face
:
1887 /* The register contains 0 (front) or 1 (back), and we need to
1888 * turn it into a NIR bool where true means front.
1890 ntq_store_dest(c
, &instr
->dest
, 0,
1892 vir_uniform_ui(c
, -1),
1896 case nir_intrinsic_load_instance_id
:
1897 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->iid
));
1900 case nir_intrinsic_load_vertex_id
:
1901 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->vid
));
1904 case nir_intrinsic_load_tlb_color_v3d
:
1905 vir_emit_tlb_color_read(c
, instr
);
1908 case nir_intrinsic_load_input
:
1909 /* Use ldvpmv (uniform offset) or ldvpmd (non-uniform offset)
1910 * and enable PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR.
1912 offset
= (nir_intrinsic_base(instr
) +
1913 nir_src_as_uint(instr
->src
[0]));
1914 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
&&
1915 c
->devinfo
->ver
>= 40) {
1916 /* Emit the LDVPM directly now, rather than at the top
1917 * of the shader like we did for V3D 3.x (which needs
1918 * vpmsetup when not just taking the next offset).
1920 * Note that delaying like this may introduce stalls,
1921 * as LDVPMV takes a minimum of 1 instruction but may
1922 * be slower if the VPM unit is busy with another QPU.
1925 if (c
->s
->info
.system_values_read
&
1926 (1ull << SYSTEM_VALUE_INSTANCE_ID
)) {
1929 if (c
->s
->info
.system_values_read
&
1930 (1ull << SYSTEM_VALUE_VERTEX_ID
)) {
1933 for (int i
= 0; i
< offset
; i
++)
1934 index
+= c
->vattr_sizes
[i
];
1935 index
+= nir_intrinsic_component(instr
);
1936 for (int i
= 0; i
< instr
->num_components
; i
++) {
1937 struct qreg vpm_offset
=
1938 vir_uniform_ui(c
, index
++);
1939 ntq_store_dest(c
, &instr
->dest
, i
,
1940 vir_LDVPMV_IN(c
, vpm_offset
));
1943 for (int i
= 0; i
< instr
->num_components
; i
++) {
1944 int comp
= nir_intrinsic_component(instr
) + i
;
1945 ntq_store_dest(c
, &instr
->dest
, i
,
1946 vir_MOV(c
, c
->inputs
[offset
* 4 +
1952 case nir_intrinsic_store_output
:
1953 /* XXX perf: Use stvpmv with uniform non-constant offsets and
1954 * stvpmd with non-uniform offsets and enable
1955 * PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR.
1957 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1958 offset
= ((nir_intrinsic_base(instr
) +
1959 nir_src_as_uint(instr
->src
[1])) * 4 +
1960 nir_intrinsic_component(instr
));
1961 for (int i
= 0; i
< instr
->num_components
; i
++) {
1962 c
->outputs
[offset
+ i
] =
1968 assert(instr
->num_components
== 1);
1971 ntq_get_src(c
, instr
->src
[0], 0),
1972 nir_intrinsic_base(instr
));
1976 case nir_intrinsic_image_deref_size
:
1977 ntq_emit_image_size(c
, instr
);
1980 case nir_intrinsic_discard
:
1981 if (vir_in_nonuniform_control_flow(c
)) {
1982 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
1984 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
1985 vir_uniform_ui(c
, 0)),
1988 vir_SETMSF_dest(c
, vir_nop_reg(),
1989 vir_uniform_ui(c
, 0));
1993 case nir_intrinsic_discard_if
: {
1994 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, instr
->src
[0]);
1996 if (vir_in_nonuniform_control_flow(c
)) {
1997 struct qinst
*exec_flag
= vir_MOV_dest(c
, vir_nop_reg(),
1999 if (cond
== V3D_QPU_COND_IFA
) {
2000 vir_set_uf(exec_flag
, V3D_QPU_UF_ANDZ
);
2002 vir_set_uf(exec_flag
, V3D_QPU_UF_NORNZ
);
2003 cond
= V3D_QPU_COND_IFA
;
2007 vir_set_cond(vir_SETMSF_dest(c
, vir_nop_reg(),
2008 vir_uniform_ui(c
, 0)), cond
);
2013 case nir_intrinsic_memory_barrier
:
2014 case nir_intrinsic_memory_barrier_atomic_counter
:
2015 case nir_intrinsic_memory_barrier_buffer
:
2016 case nir_intrinsic_memory_barrier_image
:
2017 case nir_intrinsic_memory_barrier_shared
:
2018 case nir_intrinsic_group_memory_barrier
:
2019 /* We don't do any instruction scheduling of these NIR
2020 * instructions between each other, so we just need to make
2021 * sure that the TMU operations before the barrier are flushed
2022 * before the ones after the barrier. That is currently
2023 * handled by having a THRSW in each of them and a LDTMU
2024 * series or a TMUWT after.
2028 case nir_intrinsic_barrier
:
2029 /* Emit a TSY op to get all invocations in the workgroup
2030 * (actually supergroup) to block until the last invocation
2031 * reaches the TSY op.
2033 if (c
->devinfo
->ver
>= 42) {
2034 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
2035 V3D_QPU_WADDR_SYNCB
));
2037 struct qinst
*sync
=
2038 vir_BARRIERID_dest(c
,
2039 vir_reg(QFILE_MAGIC
,
2040 V3D_QPU_WADDR_SYNCU
));
2042 vir_get_uniform_index(c
, QUNIFORM_CONSTANT
,
2044 V3D_TSY_WAIT_INC_CHECK
);
2048 /* The blocking of a TSY op only happens at the next thread
2049 * switch. No texturing may be outstanding at the time of a
2050 * TSY blocking operation.
2055 case nir_intrinsic_load_num_work_groups
:
2056 for (int i
= 0; i
< 3; i
++) {
2057 ntq_store_dest(c
, &instr
->dest
, i
,
2058 vir_uniform(c
, QUNIFORM_NUM_WORK_GROUPS
,
2063 case nir_intrinsic_load_local_invocation_index
:
2064 ntq_store_dest(c
, &instr
->dest
, 0,
2065 vir_SHR(c
, c
->cs_payload
[1],
2066 vir_uniform_ui(c
, 32 - c
->local_invocation_index_bits
)));
2069 case nir_intrinsic_load_work_group_id
:
2070 ntq_store_dest(c
, &instr
->dest
, 0,
2071 vir_AND(c
, c
->cs_payload
[0],
2072 vir_uniform_ui(c
, 0xffff)));
2073 ntq_store_dest(c
, &instr
->dest
, 1,
2074 vir_SHR(c
, c
->cs_payload
[0],
2075 vir_uniform_ui(c
, 16)));
2076 ntq_store_dest(c
, &instr
->dest
, 2,
2077 vir_AND(c
, c
->cs_payload
[1],
2078 vir_uniform_ui(c
, 0xffff)));
2081 case nir_intrinsic_load_subgroup_id
:
2082 ntq_store_dest(c
, &instr
->dest
, 0, vir_EIDX(c
));
2086 fprintf(stderr
, "Unknown intrinsic: ");
2087 nir_print_instr(&instr
->instr
, stderr
);
2088 fprintf(stderr
, "\n");
2093 /* Clears (activates) the execute flags for any channels whose jump target
2094 * matches this block.
2096 * XXX perf: Could we be using flpush/flpop somehow for our execution channel
2099 * XXX perf: For uniform control flow, we should be able to skip c->execute
2100 * handling entirely.
2103 ntq_activate_execute_for_block(struct v3d_compile
*c
)
2105 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
2106 c
->execute
, vir_uniform_ui(c
, c
->cur_block
->index
)),
2109 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
2113 ntq_emit_uniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
2115 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
2116 bool empty_else_block
=
2117 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
2118 exec_list_is_empty(&nir_else_block
->instr_list
));
2120 struct qblock
*then_block
= vir_new_block(c
);
2121 struct qblock
*after_block
= vir_new_block(c
);
2122 struct qblock
*else_block
;
2123 if (empty_else_block
)
2124 else_block
= after_block
;
2126 else_block
= vir_new_block(c
);
2128 /* Set up the flags for the IF condition (taking the THEN branch). */
2129 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
2132 vir_BRANCH(c
, cond
== V3D_QPU_COND_IFA
?
2133 V3D_QPU_BRANCH_COND_ALLNA
:
2134 V3D_QPU_BRANCH_COND_ALLA
);
2135 vir_link_blocks(c
->cur_block
, else_block
);
2136 vir_link_blocks(c
->cur_block
, then_block
);
2138 /* Process the THEN block. */
2139 vir_set_emit_block(c
, then_block
);
2140 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
2142 if (!empty_else_block
) {
2143 /* At the end of the THEN block, jump to ENDIF */
2144 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALWAYS
);
2145 vir_link_blocks(c
->cur_block
, after_block
);
2147 /* Emit the else block. */
2148 vir_set_emit_block(c
, else_block
);
2149 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
2152 vir_link_blocks(c
->cur_block
, after_block
);
2154 vir_set_emit_block(c
, after_block
);
2158 ntq_emit_nonuniform_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
2160 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
2161 bool empty_else_block
=
2162 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
2163 exec_list_is_empty(&nir_else_block
->instr_list
));
2165 struct qblock
*then_block
= vir_new_block(c
);
2166 struct qblock
*after_block
= vir_new_block(c
);
2167 struct qblock
*else_block
;
2168 if (empty_else_block
)
2169 else_block
= after_block
;
2171 else_block
= vir_new_block(c
);
2173 bool was_uniform_control_flow
= false;
2174 if (!vir_in_nonuniform_control_flow(c
)) {
2175 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
2176 was_uniform_control_flow
= true;
2179 /* Set up the flags for the IF condition (taking the THEN branch). */
2180 enum v3d_qpu_cond cond
= ntq_emit_bool_to_cond(c
, if_stmt
->condition
);
2182 /* Update the flags+cond to mean "Taking the ELSE branch (!cond) and
2183 * was previously active (execute Z) for updating the exec flags.
2185 if (was_uniform_control_flow
) {
2186 cond
= v3d_qpu_cond_invert(cond
);
2188 struct qinst
*inst
= vir_MOV_dest(c
, vir_nop_reg(), c
->execute
);
2189 if (cond
== V3D_QPU_COND_IFA
) {
2190 vir_set_uf(inst
, V3D_QPU_UF_NORNZ
);
2192 vir_set_uf(inst
, V3D_QPU_UF_ANDZ
);
2193 cond
= V3D_QPU_COND_IFA
;
2197 vir_MOV_cond(c
, cond
,
2199 vir_uniform_ui(c
, else_block
->index
));
2201 /* Jump to ELSE if nothing is active for THEN, otherwise fall
2204 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
2205 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLNA
);
2206 vir_link_blocks(c
->cur_block
, else_block
);
2207 vir_link_blocks(c
->cur_block
, then_block
);
2209 /* Process the THEN block. */
2210 vir_set_emit_block(c
, then_block
);
2211 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
2213 if (!empty_else_block
) {
2214 /* Handle the end of the THEN block. First, all currently
2215 * active channels update their execute flags to point to
2218 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2220 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2221 vir_uniform_ui(c
, after_block
->index
));
2223 /* If everything points at ENDIF, then jump there immediately. */
2224 vir_set_pf(vir_XOR_dest(c
, vir_nop_reg(),
2226 vir_uniform_ui(c
, after_block
->index
)),
2228 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLA
);
2229 vir_link_blocks(c
->cur_block
, after_block
);
2230 vir_link_blocks(c
->cur_block
, else_block
);
2232 vir_set_emit_block(c
, else_block
);
2233 ntq_activate_execute_for_block(c
);
2234 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
2237 vir_link_blocks(c
->cur_block
, after_block
);
2239 vir_set_emit_block(c
, after_block
);
2240 if (was_uniform_control_flow
)
2241 c
->execute
= c
->undef
;
2243 ntq_activate_execute_for_block(c
);
2247 ntq_emit_if(struct v3d_compile
*c
, nir_if
*nif
)
2249 bool was_in_control_flow
= c
->in_control_flow
;
2250 c
->in_control_flow
= true;
2251 if (!vir_in_nonuniform_control_flow(c
) &&
2252 nir_src_is_dynamically_uniform(nif
->condition
)) {
2253 ntq_emit_uniform_if(c
, nif
);
2255 ntq_emit_nonuniform_if(c
, nif
);
2257 c
->in_control_flow
= was_in_control_flow
;
2261 ntq_emit_jump(struct v3d_compile
*c
, nir_jump_instr
*jump
)
2263 switch (jump
->type
) {
2264 case nir_jump_break
:
2265 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2267 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2268 vir_uniform_ui(c
, c
->loop_break_block
->index
));
2271 case nir_jump_continue
:
2272 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
),
2274 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
2275 vir_uniform_ui(c
, c
->loop_cont_block
->index
));
2278 case nir_jump_return
:
2279 unreachable("All returns shouold be lowered\n");
2284 ntq_emit_instr(struct v3d_compile
*c
, nir_instr
*instr
)
2286 switch (instr
->type
) {
2287 case nir_instr_type_deref
:
2288 /* ignored, will be walked by the intrinsic using it. */
2291 case nir_instr_type_alu
:
2292 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
2295 case nir_instr_type_intrinsic
:
2296 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
2299 case nir_instr_type_load_const
:
2300 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
2303 case nir_instr_type_ssa_undef
:
2304 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
2307 case nir_instr_type_tex
:
2308 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
2311 case nir_instr_type_jump
:
2312 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
2316 fprintf(stderr
, "Unknown NIR instr type: ");
2317 nir_print_instr(instr
, stderr
);
2318 fprintf(stderr
, "\n");
2324 ntq_emit_block(struct v3d_compile
*c
, nir_block
*block
)
2326 nir_foreach_instr(instr
, block
) {
2327 ntq_emit_instr(c
, instr
);
2331 static void ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
2334 ntq_emit_loop(struct v3d_compile
*c
, nir_loop
*loop
)
2336 bool was_in_control_flow
= c
->in_control_flow
;
2337 c
->in_control_flow
= true;
2339 bool was_uniform_control_flow
= false;
2340 if (!vir_in_nonuniform_control_flow(c
)) {
2341 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
2342 was_uniform_control_flow
= true;
2345 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
2346 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
2348 c
->loop_cont_block
= vir_new_block(c
);
2349 c
->loop_break_block
= vir_new_block(c
);
2351 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2352 vir_set_emit_block(c
, c
->loop_cont_block
);
2353 ntq_activate_execute_for_block(c
);
2355 ntq_emit_cf_list(c
, &loop
->body
);
2357 /* Re-enable any previous continues now, so our ANYA check below
2360 * XXX: Use the .ORZ flags update, instead.
2362 vir_set_pf(vir_XOR_dest(c
,
2365 vir_uniform_ui(c
, c
->loop_cont_block
->index
)),
2367 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
2369 vir_set_pf(vir_MOV_dest(c
, vir_nop_reg(), c
->execute
), V3D_QPU_PF_PUSHZ
);
2371 struct qinst
*branch
= vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ANYA
);
2372 /* Pixels that were not dispatched or have been discarded should not
2373 * contribute to looping again.
2375 branch
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_P
;
2376 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2377 vir_link_blocks(c
->cur_block
, c
->loop_break_block
);
2379 vir_set_emit_block(c
, c
->loop_break_block
);
2380 if (was_uniform_control_flow
)
2381 c
->execute
= c
->undef
;
2383 ntq_activate_execute_for_block(c
);
2385 c
->loop_break_block
= save_loop_break_block
;
2386 c
->loop_cont_block
= save_loop_cont_block
;
2390 c
->in_control_flow
= was_in_control_flow
;
2394 ntq_emit_function(struct v3d_compile
*c
, nir_function_impl
*func
)
2396 fprintf(stderr
, "FUNCTIONS not handled.\n");
2401 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
)
2403 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2404 switch (node
->type
) {
2405 case nir_cf_node_block
:
2406 ntq_emit_block(c
, nir_cf_node_as_block(node
));
2409 case nir_cf_node_if
:
2410 ntq_emit_if(c
, nir_cf_node_as_if(node
));
2413 case nir_cf_node_loop
:
2414 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
2417 case nir_cf_node_function
:
2418 ntq_emit_function(c
, nir_cf_node_as_function(node
));
2422 fprintf(stderr
, "Unknown NIR node type\n");
2429 ntq_emit_impl(struct v3d_compile
*c
, nir_function_impl
*impl
)
2431 ntq_setup_registers(c
, &impl
->registers
);
2432 ntq_emit_cf_list(c
, &impl
->body
);
2436 nir_to_vir(struct v3d_compile
*c
)
2438 switch (c
->s
->info
.stage
) {
2439 case MESA_SHADER_FRAGMENT
:
2440 c
->payload_w
= vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2441 c
->payload_w_centroid
= vir_MOV(c
, vir_reg(QFILE_REG
, 1));
2442 c
->payload_z
= vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2444 /* V3D 4.x can disable implicit point coordinate varyings if
2445 * they are not used.
2447 if (c
->fs_key
->is_points
&&
2448 (c
->devinfo
->ver
< 40 || program_reads_point_coord(c
))) {
2449 c
->point_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2450 c
->point_y
= emit_fragment_varying(c
, NULL
, 0, 0);
2451 c
->uses_implicit_point_line_varyings
= true;
2452 } else if (c
->fs_key
->is_lines
&& c
->devinfo
->ver
< 40) {
2453 c
->line_x
= emit_fragment_varying(c
, NULL
, 0, 0);
2454 c
->uses_implicit_point_line_varyings
= true;
2457 case MESA_SHADER_COMPUTE
:
2458 /* Set up the TSO for barriers, assuming we do some. */
2459 if (c
->devinfo
->ver
< 42) {
2460 vir_BARRIERID_dest(c
, vir_reg(QFILE_MAGIC
,
2461 V3D_QPU_WADDR_SYNC
));
2464 c
->cs_payload
[0] = vir_MOV(c
, vir_reg(QFILE_REG
, 0));
2465 c
->cs_payload
[1] = vir_MOV(c
, vir_reg(QFILE_REG
, 2));
2467 /* Set up the division between gl_LocalInvocationIndex and
2468 * wg_in_mem in the payload reg.
2470 int wg_size
= (c
->s
->info
.cs
.local_size
[0] *
2471 c
->s
->info
.cs
.local_size
[1] *
2472 c
->s
->info
.cs
.local_size
[2]);
2473 c
->local_invocation_index_bits
=
2474 ffs(util_next_power_of_two(MAX2(wg_size
, 64))) - 1;
2475 assert(c
->local_invocation_index_bits
<= 8);
2477 if (c
->s
->info
.cs
.shared_size
) {
2478 struct qreg wg_in_mem
= vir_SHR(c
, c
->cs_payload
[1],
2479 vir_uniform_ui(c
, 16));
2480 if (c
->s
->info
.cs
.local_size
[0] != 1 ||
2481 c
->s
->info
.cs
.local_size
[1] != 1 ||
2482 c
->s
->info
.cs
.local_size
[2] != 1) {
2484 c
->local_invocation_index_bits
);
2485 int wg_mask
= (1 << wg_bits
) - 1;
2486 wg_in_mem
= vir_AND(c
, wg_in_mem
,
2487 vir_uniform_ui(c
, wg_mask
));
2489 struct qreg shared_per_wg
=
2490 vir_uniform_ui(c
, c
->s
->info
.cs
.shared_size
);
2492 c
->cs_shared_offset
=
2494 vir_uniform(c
, QUNIFORM_SHARED_OFFSET
,0),
2495 vir_UMUL(c
, wg_in_mem
, shared_per_wg
));
2502 if (c
->s
->scratch_size
) {
2503 v3d_setup_spill_base(c
);
2504 c
->spill_size
+= V3D_CHANNELS
* c
->s
->scratch_size
;
2507 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
)
2508 ntq_setup_fs_inputs(c
);
2510 ntq_setup_vpm_inputs(c
);
2512 ntq_setup_outputs(c
);
2514 /* Find the main function and emit the body. */
2515 nir_foreach_function(function
, c
->s
) {
2516 assert(strcmp(function
->name
, "main") == 0);
2517 assert(function
->impl
);
2518 ntq_emit_impl(c
, function
->impl
);
2522 const nir_shader_compiler_options v3d_nir_options
= {
2523 .lower_all_io_to_temps
= true,
2524 .lower_extract_byte
= true,
2525 .lower_extract_word
= true,
2526 .lower_bitfield_insert_to_shifts
= true,
2527 .lower_bitfield_extract_to_shifts
= true,
2528 .lower_bitfield_reverse
= true,
2529 .lower_bit_count
= true,
2530 .lower_cs_local_id_from_index
= true,
2531 .lower_ffract
= true,
2533 .lower_pack_unorm_2x16
= true,
2534 .lower_pack_snorm_2x16
= true,
2535 .lower_pack_unorm_4x8
= true,
2536 .lower_pack_snorm_4x8
= true,
2537 .lower_unpack_unorm_4x8
= true,
2538 .lower_unpack_snorm_4x8
= true,
2539 .lower_pack_half_2x16
= true,
2540 .lower_unpack_half_2x16
= true,
2542 .lower_find_lsb
= true,
2544 .lower_flrp32
= true,
2547 .lower_fsqrt
= true,
2548 .lower_ifind_msb
= true,
2549 .lower_isign
= true,
2550 .lower_ldexp
= true,
2551 .lower_mul_high
= true,
2552 .lower_wpos_pntc
= true,
2553 .lower_rotate
= true,
2557 * When demoting a shader down to single-threaded, removes the THRSW
2558 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2562 vir_remove_thrsw(struct v3d_compile
*c
)
2564 vir_for_each_block(block
, c
) {
2565 vir_for_each_inst_safe(inst
, block
) {
2566 if (inst
->qpu
.sig
.thrsw
)
2567 vir_remove_instruction(c
, inst
);
2571 c
->last_thrsw
= NULL
;
2575 vir_emit_last_thrsw(struct v3d_compile
*c
)
2577 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2578 * switching, so disable threads if we didn't do any TMU ops (each of
2579 * which would have emitted a THRSW).
2581 if (!c
->last_thrsw_at_top_level
&& c
->devinfo
->ver
< 41) {
2584 vir_remove_thrsw(c
);
2588 /* If we're threaded and the last THRSW was in conditional code, then
2589 * we need to emit another one so that we can flag it as the last
2592 if (c
->last_thrsw
&& !c
->last_thrsw_at_top_level
) {
2593 assert(c
->devinfo
->ver
>= 41);
2597 /* If we're threaded, then we need to mark the last THRSW instruction
2598 * so we can emit a pair of them at QPU emit time.
2600 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2601 * post-last-THRSW state, so we can skip this.
2603 if (!c
->last_thrsw
&& c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2604 assert(c
->devinfo
->ver
>= 41);
2609 c
->last_thrsw
->is_last_thrsw
= true;
2612 /* There's a flag in the shader for "center W is needed for reasons other than
2613 * non-centroid varyings", so we just walk the program after VIR optimization
2614 * to see if it's used. It should be harmless to set even if we only use
2615 * center W for varyings.
2618 vir_check_payload_w(struct v3d_compile
*c
)
2620 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
2623 vir_for_each_inst_inorder(inst
, c
) {
2624 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
2625 if (inst
->src
[i
].file
== QFILE_REG
&&
2626 inst
->src
[i
].index
== 0) {
2627 c
->uses_center_w
= true;
2636 v3d_nir_to_vir(struct v3d_compile
*c
)
2638 if (V3D_DEBUG
& (V3D_DEBUG_NIR
|
2639 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2640 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2641 vir_get_stage_name(c
),
2642 c
->program_id
, c
->variant_id
);
2643 nir_print_shader(c
->s
, stderr
);
2648 /* Emit the last THRSW before STVPM and TLB writes. */
2649 vir_emit_last_thrsw(c
);
2651 switch (c
->s
->info
.stage
) {
2652 case MESA_SHADER_FRAGMENT
:
2655 case MESA_SHADER_VERTEX
:
2658 case MESA_SHADER_COMPUTE
:
2661 unreachable("bad stage");
2664 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2665 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2666 fprintf(stderr
, "%s prog %d/%d pre-opt VIR:\n",
2667 vir_get_stage_name(c
),
2668 c
->program_id
, c
->variant_id
);
2670 fprintf(stderr
, "\n");
2675 vir_check_payload_w(c
);
2677 /* XXX perf: On VC4, we do a VIR-level instruction scheduling here.
2678 * We used that on that platform to pipeline TMU writes and reduce the
2679 * number of thread switches, as well as try (mostly successfully) to
2680 * reduce maximum register pressure to allow more threads. We should
2681 * do something of that sort for V3D -- either instruction scheduling
2682 * here, or delay the the THRSW and LDTMUs from our texture
2683 * instructions until the results are needed.
2686 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2687 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2688 fprintf(stderr
, "%s prog %d/%d VIR:\n",
2689 vir_get_stage_name(c
),
2690 c
->program_id
, c
->variant_id
);
2692 fprintf(stderr
, "\n");
2695 /* Attempt to allocate registers for the temporaries. If we fail,
2696 * reduce thread count and try again.
2698 int min_threads
= (c
->devinfo
->ver
>= 41) ? 2 : 1;
2699 struct qpu_reg
*temp_registers
;
2702 temp_registers
= v3d_register_allocate(c
, &spilled
);
2709 if (c
->threads
== min_threads
) {
2710 fprintf(stderr
, "Failed to register allocate at %d threads:\n",
2719 if (c
->threads
== 1)
2720 vir_remove_thrsw(c
);
2724 (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2725 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
)))) {
2726 fprintf(stderr
, "%s prog %d/%d spilled VIR:\n",
2727 vir_get_stage_name(c
),
2728 c
->program_id
, c
->variant_id
);
2730 fprintf(stderr
, "\n");
2733 v3d_vir_to_qpu(c
, temp_registers
);