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"
36 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
39 resize_qreg_array(struct v3d_compile
*c
,
44 if (*size
>= decl_size
)
47 uint32_t old_size
= *size
;
48 *size
= MAX2(*size
* 2, decl_size
);
49 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
51 fprintf(stderr
, "Malloc failure\n");
55 for (uint32_t i
= old_size
; i
< *size
; i
++)
56 (*regs
)[i
] = c
->undef
;
60 vir_emit_thrsw(struct v3d_compile
*c
)
65 /* Always thread switch after each texture operation for now.
67 * We could do better by batching a bunch of texture fetches up and
68 * then doing one thread switch and collecting all their results
71 c
->last_thrsw
= vir_NOP(c
);
72 c
->last_thrsw
->qpu
.sig
.thrsw
= true;
73 c
->last_thrsw_at_top_level
= (c
->execute
.file
== QFILE_NULL
);
77 indirect_uniform_load(struct v3d_compile
*c
, nir_intrinsic_instr
*intr
)
79 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
80 uint32_t offset
= nir_intrinsic_base(intr
);
81 struct v3d_ubo_range
*range
= NULL
;
84 for (i
= 0; i
< c
->num_ubo_ranges
; i
++) {
85 range
= &c
->ubo_ranges
[i
];
86 if (offset
>= range
->src_offset
&&
87 offset
< range
->src_offset
+ range
->size
) {
91 /* The driver-location-based offset always has to be within a declared
94 assert(i
!= c
->num_ubo_ranges
);
95 if (!c
->ubo_range_used
[i
]) {
96 c
->ubo_range_used
[i
] = true;
97 range
->dst_offset
= c
->next_ubo_dst_offset
;
98 c
->next_ubo_dst_offset
+= range
->size
;
101 offset
-= range
->src_offset
;
103 if (range
->dst_offset
+ offset
!= 0) {
104 indirect_offset
= vir_ADD(c
, indirect_offset
,
105 vir_uniform_ui(c
, range
->dst_offset
+
109 /* Adjust for where we stored the TGSI register base. */
111 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUA
),
112 vir_uniform(c
, QUNIFORM_UBO_ADDR
, 0),
120 ntq_init_ssa_def(struct v3d_compile
*c
, nir_ssa_def
*def
)
122 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
123 def
->num_components
);
124 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
129 * This function is responsible for getting VIR results into the associated
130 * storage for a NIR instruction.
132 * If it's a NIR SSA def, then we just set the associated hash table entry to
135 * If it's a NIR reg, then we need to update the existing qreg assigned to the
136 * NIR destination with the incoming value. To do that without introducing
137 * new MOVs, we require that the incoming qreg either be a uniform, or be
138 * SSA-defined by the previous VIR instruction in the block and rewritable by
139 * this function. That lets us sneak ahead and insert the SF flag beforehand
140 * (knowing that the previous instruction doesn't depend on flags) and rewrite
141 * its destination to be the NIR reg's destination
144 ntq_store_dest(struct v3d_compile
*c
, nir_dest
*dest
, int chan
,
147 struct qinst
*last_inst
= NULL
;
148 if (!list_empty(&c
->cur_block
->instructions
))
149 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
151 assert(result
.file
== QFILE_UNIF
||
152 (result
.file
== QFILE_TEMP
&&
153 last_inst
&& last_inst
== c
->defs
[result
.index
]));
156 assert(chan
< dest
->ssa
.num_components
);
159 struct hash_entry
*entry
=
160 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
165 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
167 qregs
[chan
] = result
;
169 nir_register
*reg
= dest
->reg
.reg
;
170 assert(dest
->reg
.base_offset
== 0);
171 assert(reg
->num_array_elems
== 0);
172 struct hash_entry
*entry
=
173 _mesa_hash_table_search(c
->def_ht
, reg
);
174 struct qreg
*qregs
= entry
->data
;
176 /* Insert a MOV if the source wasn't an SSA def in the
177 * previous instruction.
179 if (result
.file
== QFILE_UNIF
) {
180 result
= vir_MOV(c
, result
);
181 last_inst
= c
->defs
[result
.index
];
184 /* We know they're both temps, so just rewrite index. */
185 c
->defs
[last_inst
->dst
.index
] = NULL
;
186 last_inst
->dst
.index
= qregs
[chan
].index
;
188 /* If we're in control flow, then make this update of the reg
189 * conditional on the execution mask.
191 if (c
->execute
.file
!= QFILE_NULL
) {
192 last_inst
->dst
.index
= qregs
[chan
].index
;
194 /* Set the flags to the current exec mask.
196 c
->cursor
= vir_before_inst(last_inst
);
197 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
198 c
->cursor
= vir_after_inst(last_inst
);
200 vir_set_cond(last_inst
, V3D_QPU_COND_IFA
);
201 last_inst
->cond_is_exec_mask
= true;
207 ntq_get_src(struct v3d_compile
*c
, nir_src src
, int i
)
209 struct hash_entry
*entry
;
211 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
212 assert(i
< src
.ssa
->num_components
);
214 nir_register
*reg
= src
.reg
.reg
;
215 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
216 assert(reg
->num_array_elems
== 0);
217 assert(src
.reg
.base_offset
== 0);
218 assert(i
< reg
->num_components
);
221 struct qreg
*qregs
= entry
->data
;
226 ntq_get_alu_src(struct v3d_compile
*c
, nir_alu_instr
*instr
,
229 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
230 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
231 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
232 instr
->src
[src
].swizzle
[chan
]);
234 assert(!instr
->src
[src
].abs
);
235 assert(!instr
->src
[src
].negate
);
241 ntq_minify(struct v3d_compile
*c
, struct qreg size
, struct qreg level
)
243 return vir_MAX(c
, vir_SHR(c
, size
, level
), vir_uniform_ui(c
, 1));
247 ntq_emit_txs(struct v3d_compile
*c
, nir_tex_instr
*instr
)
249 unsigned unit
= instr
->texture_index
;
250 int lod_index
= nir_tex_instr_src_index(instr
, nir_tex_src_lod
);
251 int dest_size
= nir_tex_instr_dest_size(instr
);
253 struct qreg lod
= c
->undef
;
255 lod
= ntq_get_src(c
, instr
->src
[lod_index
].src
, 0);
257 for (int i
= 0; i
< dest_size
; i
++) {
259 enum quniform_contents contents
;
261 if (instr
->is_array
&& i
== dest_size
- 1)
262 contents
= QUNIFORM_TEXTURE_ARRAY_SIZE
;
264 contents
= QUNIFORM_TEXTURE_WIDTH
+ i
;
266 struct qreg size
= vir_uniform(c
, contents
, unit
);
268 switch (instr
->sampler_dim
) {
269 case GLSL_SAMPLER_DIM_1D
:
270 case GLSL_SAMPLER_DIM_2D
:
271 case GLSL_SAMPLER_DIM_MS
:
272 case GLSL_SAMPLER_DIM_3D
:
273 case GLSL_SAMPLER_DIM_CUBE
:
274 /* Don't minify the array size. */
275 if (!(instr
->is_array
&& i
== dest_size
- 1)) {
276 size
= ntq_minify(c
, size
, lod
);
280 case GLSL_SAMPLER_DIM_RECT
:
281 /* There's no LOD field for rects */
285 unreachable("Bad sampler type");
288 ntq_store_dest(c
, &instr
->dest
, i
, size
);
293 ntq_emit_tex(struct v3d_compile
*c
, nir_tex_instr
*instr
)
295 unsigned unit
= instr
->texture_index
;
297 /* Since each texture sampling op requires uploading uniforms to
298 * reference the texture, there's no HW support for texture size and
299 * you just upload uniforms containing the size.
302 case nir_texop_query_levels
:
303 ntq_store_dest(c
, &instr
->dest
, 0,
304 vir_uniform(c
, QUNIFORM_TEXTURE_LEVELS
, unit
));
307 ntq_emit_txs(c
, instr
);
313 if (c
->devinfo
->ver
>= 40)
314 v3d40_vir_emit_tex(c
, instr
);
316 v3d33_vir_emit_tex(c
, instr
);
320 ntq_fsincos(struct v3d_compile
*c
, struct qreg src
, bool is_cos
)
322 struct qreg input
= vir_FMUL(c
, src
, vir_uniform_f(c
, 1.0f
/ M_PI
));
324 input
= vir_FADD(c
, input
, vir_uniform_f(c
, 0.5));
326 struct qreg periods
= vir_FROUND(c
, input
);
327 struct qreg sin_output
= vir_SIN(c
, vir_FSUB(c
, input
, periods
));
328 return vir_XOR(c
, sin_output
, vir_SHL(c
,
329 vir_FTOIN(c
, periods
),
330 vir_uniform_ui(c
, -1)));
334 ntq_fsign(struct v3d_compile
*c
, struct qreg src
)
336 struct qreg t
= vir_get_temp(c
);
338 vir_MOV_dest(c
, t
, vir_uniform_f(c
, 0.0));
339 vir_PF(c
, vir_FMOV(c
, src
), V3D_QPU_PF_PUSHZ
);
340 vir_MOV_cond(c
, V3D_QPU_COND_IFNA
, t
, vir_uniform_f(c
, 1.0));
341 vir_PF(c
, vir_FMOV(c
, src
), V3D_QPU_PF_PUSHN
);
342 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, t
, vir_uniform_f(c
, -1.0));
343 return vir_MOV(c
, t
);
347 ntq_isign(struct v3d_compile
*c
, struct qreg src
)
349 struct qreg t
= vir_get_temp(c
);
351 vir_MOV_dest(c
, t
, vir_uniform_ui(c
, 0));
352 vir_PF(c
, vir_MOV(c
, src
), V3D_QPU_PF_PUSHZ
);
353 vir_MOV_cond(c
, V3D_QPU_COND_IFNA
, t
, vir_uniform_ui(c
, 1));
354 vir_PF(c
, vir_MOV(c
, src
), V3D_QPU_PF_PUSHN
);
355 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, t
, vir_uniform_ui(c
, -1));
356 return vir_MOV(c
, t
);
360 emit_fragcoord_input(struct v3d_compile
*c
, int attr
)
362 c
->inputs
[attr
* 4 + 0] = vir_FXCD(c
);
363 c
->inputs
[attr
* 4 + 1] = vir_FYCD(c
);
364 c
->inputs
[attr
* 4 + 2] = c
->payload_z
;
365 c
->inputs
[attr
* 4 + 3] = vir_RECIP(c
, c
->payload_w
);
369 emit_fragment_varying(struct v3d_compile
*c
, nir_variable
*var
,
372 struct qreg r3
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R3
);
373 struct qreg r5
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_R5
);
376 if (c
->devinfo
->ver
>= 41) {
377 struct qinst
*ldvary
= vir_add_inst(V3D_QPU_A_NOP
, c
->undef
,
379 ldvary
->qpu
.sig
.ldvary
= true;
380 vary
= vir_emit_def(c
, ldvary
);
382 vir_NOP(c
)->qpu
.sig
.ldvary
= true;
386 /* For gl_PointCoord input or distance along a line, we'll be called
387 * with no nir_variable, and we don't count toward VPM size so we
388 * don't track an input slot.
391 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
394 int i
= c
->num_inputs
++;
395 c
->input_slots
[i
] = v3d_slot_from_slot_and_component(var
->data
.location
,
398 switch (var
->data
.interpolation
) {
399 case INTERP_MODE_NONE
:
400 /* If a gl_FrontColor or gl_BackColor input has no interp
401 * qualifier, then if we're using glShadeModel(GL_FLAT) it
402 * needs to be flat shaded.
404 switch (var
->data
.location
) {
405 case VARYING_SLOT_COL0
:
406 case VARYING_SLOT_COL1
:
407 case VARYING_SLOT_BFC0
:
408 case VARYING_SLOT_BFC1
:
409 if (c
->fs_key
->shade_model_flat
) {
410 BITSET_SET(c
->flat_shade_flags
, i
);
411 vir_MOV_dest(c
, c
->undef
, vary
);
412 return vir_MOV(c
, r5
);
414 return vir_FADD(c
, vir_FMUL(c
, vary
,
421 case INTERP_MODE_SMOOTH
:
422 if (var
->data
.centroid
) {
423 BITSET_SET(c
->centroid_flags
, i
);
424 return vir_FADD(c
, vir_FMUL(c
, vary
,
425 c
->payload_w_centroid
), r5
);
427 return vir_FADD(c
, vir_FMUL(c
, vary
, c
->payload_w
), r5
);
429 case INTERP_MODE_NOPERSPECTIVE
:
430 BITSET_SET(c
->noperspective_flags
, i
);
431 return vir_FADD(c
, vir_MOV(c
, vary
), r5
);
432 case INTERP_MODE_FLAT
:
433 BITSET_SET(c
->flat_shade_flags
, i
);
434 vir_MOV_dest(c
, c
->undef
, vary
);
435 return vir_MOV(c
, r5
);
437 unreachable("Bad interp mode");
442 emit_fragment_input(struct v3d_compile
*c
, int attr
, nir_variable
*var
)
444 for (int i
= 0; i
< glsl_get_vector_elements(var
->type
); i
++) {
445 int chan
= var
->data
.location_frac
+ i
;
446 c
->inputs
[attr
* 4 + chan
] =
447 emit_fragment_varying(c
, var
, chan
);
452 add_output(struct v3d_compile
*c
,
453 uint32_t decl_offset
,
457 uint32_t old_array_size
= c
->outputs_array_size
;
458 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
461 if (old_array_size
!= c
->outputs_array_size
) {
462 c
->output_slots
= reralloc(c
,
464 struct v3d_varying_slot
,
465 c
->outputs_array_size
);
468 c
->output_slots
[decl_offset
] =
469 v3d_slot_from_slot_and_component(slot
, swizzle
);
473 declare_uniform_range(struct v3d_compile
*c
, uint32_t start
, uint32_t size
)
475 unsigned array_id
= c
->num_ubo_ranges
++;
476 if (array_id
>= c
->ubo_ranges_array_size
) {
477 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
479 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
480 struct v3d_ubo_range
,
481 c
->ubo_ranges_array_size
);
482 c
->ubo_range_used
= reralloc(c
, c
->ubo_range_used
,
484 c
->ubo_ranges_array_size
);
487 c
->ubo_ranges
[array_id
].dst_offset
= 0;
488 c
->ubo_ranges
[array_id
].src_offset
= start
;
489 c
->ubo_ranges
[array_id
].size
= size
;
490 c
->ubo_range_used
[array_id
] = false;
494 * If compare_instr is a valid comparison instruction, emits the
495 * compare_instr's comparison and returns the sel_instr's return value based
496 * on the compare_instr's result.
499 ntq_emit_comparison(struct v3d_compile
*c
,
500 nir_alu_instr
*compare_instr
,
501 enum v3d_qpu_cond
*out_cond
)
503 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
505 if (nir_op_infos
[compare_instr
->op
].num_inputs
> 1)
506 src1
= ntq_get_alu_src(c
, compare_instr
, 1);
507 bool cond_invert
= false;
508 struct qreg nop
= vir_reg(QFILE_NULL
, 0);
510 switch (compare_instr
->op
) {
513 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
516 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
521 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
525 vir_set_pf(vir_XOR_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHZ
);
531 vir_set_pf(vir_FCMP_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
534 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
538 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
544 vir_set_pf(vir_FCMP_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHN
);
547 vir_set_pf(vir_MIN_dest(c
, nop
, src1
, src0
), V3D_QPU_PF_PUSHC
);
550 vir_set_pf(vir_SUB_dest(c
, nop
, src0
, src1
), V3D_QPU_PF_PUSHC
);
557 *out_cond
= cond_invert
? V3D_QPU_COND_IFNA
: V3D_QPU_COND_IFA
;
562 static struct nir_alu_instr
*
563 ntq_get_alu_parent(nir_src src
)
565 if (!src
.is_ssa
|| src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
567 return nir_instr_as_alu(src
.ssa
->parent_instr
);
571 * Attempts to fold a comparison generating a boolean result into the
572 * condition code for selecting between two values, instead of comparing the
573 * boolean result against 0 to generate the condition code.
575 static struct qreg
ntq_emit_bcsel(struct v3d_compile
*c
, nir_alu_instr
*instr
,
578 nir_alu_instr
*compare
= ntq_get_alu_parent(instr
->src
[0].src
);
582 enum v3d_qpu_cond cond
;
583 if (ntq_emit_comparison(c
, compare
, &cond
))
584 return vir_MOV(c
, vir_SEL(c
, cond
, src
[1], src
[2]));
587 vir_PF(c
, src
[0], V3D_QPU_PF_PUSHZ
);
588 return vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFNA
, src
[1], src
[2]));
593 ntq_emit_alu(struct v3d_compile
*c
, nir_alu_instr
*instr
)
595 /* This should always be lowered to ALU operations for V3D. */
596 assert(!instr
->dest
.saturate
);
598 /* Vectors are special in that they have non-scalarized writemasks,
599 * and just take the first swizzle channel for each argument in order
600 * into each writemask channel.
602 if (instr
->op
== nir_op_vec2
||
603 instr
->op
== nir_op_vec3
||
604 instr
->op
== nir_op_vec4
) {
606 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
607 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
608 instr
->src
[i
].swizzle
[0]);
609 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
610 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
611 vir_MOV(c
, srcs
[i
]));
615 /* General case: We can just grab the one used channel per src. */
616 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
617 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
618 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
626 result
= vir_MOV(c
, src
[0]);
630 result
= vir_XOR(c
, src
[0], vir_uniform_ui(c
, 1 << 31));
633 result
= vir_NEG(c
, src
[0]);
637 result
= vir_FMUL(c
, src
[0], src
[1]);
640 result
= vir_FADD(c
, src
[0], src
[1]);
643 result
= vir_FSUB(c
, src
[0], src
[1]);
646 result
= vir_FMIN(c
, src
[0], src
[1]);
649 result
= vir_FMAX(c
, src
[0], src
[1]);
653 result
= vir_FTOIZ(c
, src
[0]);
656 result
= vir_FTOUZ(c
, src
[0]);
659 result
= vir_ITOF(c
, src
[0]);
662 result
= vir_UTOF(c
, src
[0]);
665 result
= vir_AND(c
, src
[0], vir_uniform_f(c
, 1.0));
668 result
= vir_AND(c
, src
[0], vir_uniform_ui(c
, 1));
672 vir_PF(c
, src
[0], V3D_QPU_PF_PUSHZ
);
673 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFNA
,
674 vir_uniform_ui(c
, ~0),
675 vir_uniform_ui(c
, 0)));
679 result
= vir_ADD(c
, src
[0], src
[1]);
682 result
= vir_SHR(c
, src
[0], src
[1]);
685 result
= vir_SUB(c
, src
[0], src
[1]);
688 result
= vir_ASR(c
, src
[0], src
[1]);
691 result
= vir_SHL(c
, src
[0], src
[1]);
694 result
= vir_MIN(c
, src
[0], src
[1]);
697 result
= vir_UMIN(c
, src
[0], src
[1]);
700 result
= vir_MAX(c
, src
[0], src
[1]);
703 result
= vir_UMAX(c
, src
[0], src
[1]);
706 result
= vir_AND(c
, src
[0], src
[1]);
709 result
= vir_OR(c
, src
[0], src
[1]);
712 result
= vir_XOR(c
, src
[0], src
[1]);
715 result
= vir_NOT(c
, src
[0]);
718 case nir_op_ufind_msb
:
719 result
= vir_SUB(c
, vir_uniform_ui(c
, 31), vir_CLZ(c
, src
[0]));
723 result
= vir_UMUL(c
, src
[0], src
[1]);
730 enum v3d_qpu_cond cond
;
731 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
733 result
= vir_MOV(c
, vir_SEL(c
, cond
,
734 vir_uniform_f(c
, 1.0),
735 vir_uniform_f(c
, 0.0)));
749 enum v3d_qpu_cond cond
;
750 MAYBE_UNUSED
bool ok
= ntq_emit_comparison(c
, instr
, &cond
);
752 result
= vir_MOV(c
, vir_SEL(c
, cond
,
753 vir_uniform_ui(c
, ~0),
754 vir_uniform_ui(c
, 0)));
759 result
= ntq_emit_bcsel(c
, instr
, src
);
762 vir_PF(c
, src
[0], V3D_QPU_PF_PUSHZ
);
763 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFNA
,
768 result
= vir_RECIP(c
, src
[0]);
771 result
= vir_RSQRT(c
, src
[0]);
774 result
= vir_EXP(c
, src
[0]);
777 result
= vir_LOG(c
, src
[0]);
781 result
= vir_FCEIL(c
, src
[0]);
784 result
= vir_FFLOOR(c
, src
[0]);
786 case nir_op_fround_even
:
787 result
= vir_FROUND(c
, src
[0]);
790 result
= vir_FTRUNC(c
, src
[0]);
793 result
= vir_FSUB(c
, src
[0], vir_FFLOOR(c
, src
[0]));
797 result
= ntq_fsincos(c
, src
[0], false);
800 result
= ntq_fsincos(c
, src
[0], true);
804 result
= ntq_fsign(c
, src
[0]);
807 result
= ntq_isign(c
, src
[0]);
811 result
= vir_FMOV(c
, src
[0]);
812 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_ABS
);
817 result
= vir_MAX(c
, src
[0],
818 vir_SUB(c
, vir_uniform_ui(c
, 0), src
[0]));
822 case nir_op_fddx_coarse
:
823 case nir_op_fddx_fine
:
824 result
= vir_FDX(c
, src
[0]);
828 case nir_op_fddy_coarse
:
829 case nir_op_fddy_fine
:
830 result
= vir_FDY(c
, src
[0]);
833 case nir_op_uadd_carry
:
834 vir_PF(c
, vir_ADD(c
, src
[0], src
[1]), V3D_QPU_PF_PUSHC
);
835 result
= vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
836 vir_uniform_ui(c
, ~0),
837 vir_uniform_ui(c
, 0)));
840 case nir_op_pack_half_2x16_split
:
841 result
= vir_VFPACK(c
, src
[0], src
[1]);
844 case nir_op_unpack_half_2x16_split_x
:
845 /* XXX perf: It would be good to be able to merge this unpack
846 * with whatever uses our result.
848 result
= vir_FMOV(c
, src
[0]);
849 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_L
);
852 case nir_op_unpack_half_2x16_split_y
:
853 result
= vir_FMOV(c
, src
[0]);
854 vir_set_unpack(c
->defs
[result
.index
], 0, V3D_QPU_UNPACK_H
);
858 fprintf(stderr
, "unknown NIR ALU inst: ");
859 nir_print_instr(&instr
->instr
, stderr
);
860 fprintf(stderr
, "\n");
864 /* We have a scalar result, so the instruction should only have a
865 * single channel written to.
867 assert(util_is_power_of_two_or_zero(instr
->dest
.write_mask
));
868 ntq_store_dest(c
, &instr
->dest
.dest
,
869 ffs(instr
->dest
.write_mask
) - 1, result
);
872 /* Each TLB read/write setup (a render target or depth buffer) takes an 8-bit
873 * specifier. They come from a register that's preloaded with 0xffffffff
874 * (0xff gets you normal vec4 f16 RT0 writes), and when one is neaded the low
875 * 8 bits are shifted off the bottom and 0xff shifted in from the top.
877 #define TLB_TYPE_F16_COLOR (3 << 6)
878 #define TLB_TYPE_I32_COLOR (1 << 6)
879 #define TLB_TYPE_F32_COLOR (0 << 6)
880 #define TLB_RENDER_TARGET_SHIFT 3 /* Reversed! 7 = RT 0, 0 = RT 7. */
881 #define TLB_SAMPLE_MODE_PER_SAMPLE (0 << 2)
882 #define TLB_SAMPLE_MODE_PER_PIXEL (1 << 2)
883 #define TLB_F16_SWAP_HI_LO (1 << 1)
884 #define TLB_VEC_SIZE_4_F16 (1 << 0)
885 #define TLB_VEC_SIZE_2_F16 (0 << 0)
886 #define TLB_VEC_SIZE_MINUS_1_SHIFT 0
888 /* Triggers Z/Stencil testing, used when the shader state's "FS modifies Z"
891 #define TLB_TYPE_DEPTH ((2 << 6) | (0 << 4))
892 #define TLB_DEPTH_TYPE_INVARIANT (0 << 2) /* Unmodified sideband input used */
893 #define TLB_DEPTH_TYPE_PER_PIXEL (1 << 2) /* QPU result used */
894 #define TLB_V42_DEPTH_TYPE_INVARIANT (0 << 3) /* Unmodified sideband input used */
895 #define TLB_V42_DEPTH_TYPE_PER_PIXEL (1 << 3) /* QPU result used */
897 /* Stencil is a single 32-bit write. */
898 #define TLB_TYPE_STENCIL_ALPHA ((2 << 6) | (1 << 4))
901 emit_frag_end(struct v3d_compile
*c
)
904 if (c->output_sample_mask_index != -1) {
905 vir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
909 bool has_any_tlb_color_write
= false;
910 for (int rt
= 0; rt
< c
->fs_key
->nr_cbufs
; rt
++) {
911 if (c
->output_color_var
[rt
])
912 has_any_tlb_color_write
= true;
915 if (c
->fs_key
->sample_alpha_to_coverage
&& c
->output_color_var
[0]) {
916 struct nir_variable
*var
= c
->output_color_var
[0];
917 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
919 vir_SETMSF_dest(c
, vir_reg(QFILE_NULL
, 0),
922 vir_FTOC(c
, color
[3])));
925 if (c
->output_position_index
!= -1) {
926 struct qinst
*inst
= vir_MOV_dest(c
,
927 vir_reg(QFILE_TLBU
, 0),
928 c
->outputs
[c
->output_position_index
]);
929 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
931 if (c
->devinfo
->ver
>= 42) {
932 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_PER_PIXEL
|
933 TLB_SAMPLE_MODE_PER_PIXEL
);
935 tlb_specifier
|= TLB_DEPTH_TYPE_PER_PIXEL
;
937 inst
->src
[vir_get_implicit_uniform_src(inst
)] =
938 vir_uniform_ui(c
, tlb_specifier
| 0xffffff00);
939 } else if (c
->s
->info
.fs
.uses_discard
||
940 c
->fs_key
->sample_alpha_to_coverage
||
941 !has_any_tlb_color_write
) {
942 /* Emit passthrough Z if it needed to be delayed until shader
943 * end due to potential discards.
945 * Since (single-threaded) fragment shaders always need a TLB
946 * write, emit passthrouh Z if we didn't have any color
947 * buffers and flag us as potentially discarding, so that we
948 * can use Z as the TLB write.
950 c
->s
->info
.fs
.uses_discard
= true;
952 struct qinst
*inst
= vir_MOV_dest(c
,
953 vir_reg(QFILE_TLBU
, 0),
954 vir_reg(QFILE_NULL
, 0));
955 uint8_t tlb_specifier
= TLB_TYPE_DEPTH
;
957 if (c
->devinfo
->ver
>= 42) {
958 /* The spec says the PER_PIXEL flag is ignored for
959 * invariant writes, but the simulator demands it.
961 tlb_specifier
|= (TLB_V42_DEPTH_TYPE_INVARIANT
|
962 TLB_SAMPLE_MODE_PER_PIXEL
);
964 tlb_specifier
|= TLB_DEPTH_TYPE_INVARIANT
;
967 inst
->src
[vir_get_implicit_uniform_src(inst
)] =
968 vir_uniform_ui(c
, tlb_specifier
| 0xffffff00);
971 /* XXX: Performance improvement: Merge Z write and color writes TLB
975 for (int rt
= 0; rt
< c
->fs_key
->nr_cbufs
; rt
++) {
976 if (!c
->output_color_var
[rt
])
979 nir_variable
*var
= c
->output_color_var
[rt
];
980 struct qreg
*color
= &c
->outputs
[var
->data
.driver_location
* 4];
981 int num_components
= glsl_get_vector_elements(var
->type
);
982 uint32_t conf
= 0xffffff00;
985 conf
|= TLB_SAMPLE_MODE_PER_PIXEL
;
986 conf
|= (7 - rt
) << TLB_RENDER_TARGET_SHIFT
;
988 if (c
->fs_key
->swap_color_rb
& (1 << rt
))
989 num_components
= MAX2(num_components
, 3);
991 assert(num_components
!= 0);
992 switch (glsl_get_base_type(var
->type
)) {
995 /* The F32 vs I32 distinction was dropped in 4.2. */
996 if (c
->devinfo
->ver
< 42)
997 conf
|= TLB_TYPE_I32_COLOR
;
999 conf
|= TLB_TYPE_F32_COLOR
;
1000 conf
|= ((num_components
- 1) <<
1001 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1003 inst
= vir_MOV_dest(c
, vir_reg(QFILE_TLBU
, 0), color
[0]);
1004 inst
->src
[vir_get_implicit_uniform_src(inst
)] =
1005 vir_uniform_ui(c
, conf
);
1007 for (int i
= 1; i
< num_components
; i
++) {
1008 inst
= vir_MOV_dest(c
, vir_reg(QFILE_TLB
, 0),
1014 struct qreg r
= color
[0];
1015 struct qreg g
= color
[1];
1016 struct qreg b
= color
[2];
1017 struct qreg a
= color
[3];
1019 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1020 conf
|= TLB_TYPE_F32_COLOR
;
1021 conf
|= ((num_components
- 1) <<
1022 TLB_VEC_SIZE_MINUS_1_SHIFT
);
1024 conf
|= TLB_TYPE_F16_COLOR
;
1025 conf
|= TLB_F16_SWAP_HI_LO
;
1026 if (num_components
>= 3)
1027 conf
|= TLB_VEC_SIZE_4_F16
;
1029 conf
|= TLB_VEC_SIZE_2_F16
;
1032 if (c
->fs_key
->swap_color_rb
& (1 << rt
)) {
1037 if (c
->fs_key
->sample_alpha_to_one
)
1038 a
= vir_uniform_f(c
, 1.0);
1040 if (c
->fs_key
->f32_color_rb
& (1 << rt
)) {
1041 inst
= vir_MOV_dest(c
, vir_reg(QFILE_TLBU
, 0), r
);
1042 inst
->src
[vir_get_implicit_uniform_src(inst
)] =
1043 vir_uniform_ui(c
, conf
);
1045 if (num_components
>= 2)
1046 vir_MOV_dest(c
, vir_reg(QFILE_TLB
, 0), g
);
1047 if (num_components
>= 3)
1048 vir_MOV_dest(c
, vir_reg(QFILE_TLB
, 0), b
);
1049 if (num_components
>= 4)
1050 vir_MOV_dest(c
, vir_reg(QFILE_TLB
, 0), a
);
1052 inst
= vir_VFPACK_dest(c
, vir_reg(QFILE_TLB
, 0), r
, g
);
1054 inst
->dst
.file
= QFILE_TLBU
;
1055 inst
->src
[vir_get_implicit_uniform_src(inst
)] =
1056 vir_uniform_ui(c
, conf
);
1059 if (num_components
>= 3)
1060 inst
= vir_VFPACK_dest(c
, vir_reg(QFILE_TLB
, 0), b
, a
);
1069 vir_VPM_WRITE(struct v3d_compile
*c
, struct qreg val
, uint32_t *vpm_index
)
1071 if (c
->devinfo
->ver
>= 40) {
1072 vir_STVPMV(c
, vir_uniform_ui(c
, *vpm_index
), val
);
1073 *vpm_index
= *vpm_index
+ 1;
1075 vir_MOV_dest(c
, vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_VPM
), val
);
1078 c
->num_vpm_writes
++;
1082 emit_scaled_viewport_write(struct v3d_compile
*c
, struct qreg rcp_w
,
1083 uint32_t *vpm_index
)
1085 for (int i
= 0; i
< 2; i
++) {
1086 struct qreg coord
= c
->outputs
[c
->output_position_index
+ i
];
1087 coord
= vir_FMUL(c
, coord
,
1088 vir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
,
1090 coord
= vir_FMUL(c
, coord
, rcp_w
);
1091 vir_VPM_WRITE(c
, vir_FTOIN(c
, coord
), vpm_index
);
1097 emit_zs_write(struct v3d_compile
*c
, struct qreg rcp_w
, uint32_t *vpm_index
)
1099 struct qreg zscale
= vir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1100 struct qreg zoffset
= vir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1102 struct qreg z
= c
->outputs
[c
->output_position_index
+ 2];
1103 z
= vir_FMUL(c
, z
, zscale
);
1104 z
= vir_FMUL(c
, z
, rcp_w
);
1105 z
= vir_FADD(c
, z
, zoffset
);
1106 vir_VPM_WRITE(c
, z
, vpm_index
);
1110 emit_rcp_wc_write(struct v3d_compile
*c
, struct qreg rcp_w
, uint32_t *vpm_index
)
1112 vir_VPM_WRITE(c
, rcp_w
, vpm_index
);
1116 emit_point_size_write(struct v3d_compile
*c
, uint32_t *vpm_index
)
1118 struct qreg point_size
;
1120 if (c
->output_point_size_index
!= -1)
1121 point_size
= c
->outputs
[c
->output_point_size_index
];
1123 point_size
= vir_uniform_f(c
, 1.0);
1125 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1128 point_size
= vir_FMAX(c
, point_size
, vir_uniform_f(c
, .125));
1130 vir_VPM_WRITE(c
, point_size
, vpm_index
);
1134 emit_vpm_write_setup(struct v3d_compile
*c
)
1136 if (c
->devinfo
->ver
>= 40)
1139 v3d33_vir_vpm_write_setup(c
);
1143 * Sets up c->outputs[c->output_position_index] for the vertex shader
1144 * epilogue, if an output vertex position wasn't specified in the user's
1145 * shader. This may be the case for transform feedback with rasterizer
1149 setup_default_position(struct v3d_compile
*c
)
1151 if (c
->output_position_index
!= -1)
1154 c
->output_position_index
= c
->outputs_array_size
;
1155 for (int i
= 0; i
< 4; i
++) {
1157 c
->output_position_index
+ i
,
1158 VARYING_SLOT_POS
, i
);
1163 emit_vert_end(struct v3d_compile
*c
)
1165 setup_default_position(c
);
1167 uint32_t vpm_index
= 0;
1168 struct qreg rcp_w
= vir_RECIP(c
,
1169 c
->outputs
[c
->output_position_index
+ 3]);
1171 emit_vpm_write_setup(c
);
1173 if (c
->vs_key
->is_coord
) {
1174 for (int i
= 0; i
< 4; i
++)
1175 vir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
],
1177 emit_scaled_viewport_write(c
, rcp_w
, &vpm_index
);
1178 if (c
->vs_key
->per_vertex_point_size
) {
1179 emit_point_size_write(c
, &vpm_index
);
1180 /* emit_rcp_wc_write(c, rcp_w); */
1182 /* XXX: Z-only rendering */
1184 emit_zs_write(c
, rcp_w
, &vpm_index
);
1186 emit_scaled_viewport_write(c
, rcp_w
, &vpm_index
);
1187 emit_zs_write(c
, rcp_w
, &vpm_index
);
1188 emit_rcp_wc_write(c
, rcp_w
, &vpm_index
);
1189 if (c
->vs_key
->per_vertex_point_size
)
1190 emit_point_size_write(c
, &vpm_index
);
1193 for (int i
= 0; i
< c
->vs_key
->num_fs_inputs
; i
++) {
1194 struct v3d_varying_slot input
= c
->vs_key
->fs_inputs
[i
];
1197 for (j
= 0; j
< c
->num_outputs
; j
++) {
1198 struct v3d_varying_slot output
= c
->output_slots
[j
];
1200 if (!memcmp(&input
, &output
, sizeof(input
))) {
1201 vir_VPM_WRITE(c
, c
->outputs
[j
],
1206 /* Emit padding if we didn't find a declared VS output for
1209 if (j
== c
->num_outputs
)
1210 vir_VPM_WRITE(c
, vir_uniform_f(c
, 0.0),
1214 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1216 if (c
->devinfo
->ver
>= 40 && c
->devinfo
->ver
<= 42)
1221 v3d_optimize_nir(struct nir_shader
*s
)
1228 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1229 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
);
1230 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
1231 NIR_PASS(progress
, s
, nir_copy_prop
);
1232 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1233 NIR_PASS(progress
, s
, nir_opt_dce
);
1234 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1235 NIR_PASS(progress
, s
, nir_opt_cse
);
1236 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
1237 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1238 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1239 NIR_PASS(progress
, s
, nir_opt_undef
);
1242 NIR_PASS(progress
, s
, nir_opt_move_load_ubo
);
1246 driver_location_compare(const void *in_a
, const void *in_b
)
1248 const nir_variable
*const *a
= in_a
;
1249 const nir_variable
*const *b
= in_b
;
1251 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1255 ntq_emit_vpm_read(struct v3d_compile
*c
,
1256 uint32_t *num_components_queued
,
1257 uint32_t *remaining
,
1260 struct qreg vpm
= vir_reg(QFILE_VPM
, vpm_index
);
1262 if (c
->devinfo
->ver
>= 40 ) {
1263 return vir_LDVPMV_IN(c
,
1265 (*num_components_queued
)++));
1268 if (*num_components_queued
!= 0) {
1269 (*num_components_queued
)--;
1271 return vir_MOV(c
, vpm
);
1274 uint32_t num_components
= MIN2(*remaining
, 32);
1276 v3d33_vir_vpm_read_setup(c
, num_components
);
1278 *num_components_queued
= num_components
- 1;
1279 *remaining
-= num_components
;
1282 return vir_MOV(c
, vpm
);
1286 ntq_setup_vpm_inputs(struct v3d_compile
*c
)
1288 /* Figure out how many components of each vertex attribute the shader
1289 * uses. Each variable should have been split to individual
1290 * components and unused ones DCEed. The vertex fetcher will load
1291 * from the start of the attribute to the number of components we
1292 * declare we need in c->vattr_sizes[].
1294 nir_foreach_variable(var
, &c
->s
->inputs
) {
1295 /* No VS attribute array support. */
1296 assert(MAX2(glsl_get_length(var
->type
), 1) == 1);
1298 unsigned loc
= var
->data
.driver_location
;
1299 int start_component
= var
->data
.location_frac
;
1300 int num_components
= glsl_get_components(var
->type
);
1302 c
->vattr_sizes
[loc
] = MAX2(c
->vattr_sizes
[loc
],
1303 start_component
+ num_components
);
1306 unsigned num_components
= 0;
1307 uint32_t vpm_components_queued
= 0;
1308 bool uses_iid
= c
->s
->info
.system_values_read
&
1309 (1ull << SYSTEM_VALUE_INSTANCE_ID
);
1310 bool uses_vid
= c
->s
->info
.system_values_read
&
1311 (1ull << SYSTEM_VALUE_VERTEX_ID
);
1312 num_components
+= uses_iid
;
1313 num_components
+= uses_vid
;
1315 for (int i
= 0; i
< ARRAY_SIZE(c
->vattr_sizes
); i
++)
1316 num_components
+= c
->vattr_sizes
[i
];
1319 c
->iid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1320 &num_components
, ~0);
1324 c
->vid
= ntq_emit_vpm_read(c
, &vpm_components_queued
,
1325 &num_components
, ~0);
1328 for (int loc
= 0; loc
< ARRAY_SIZE(c
->vattr_sizes
); loc
++) {
1329 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1332 for (int i
= 0; i
< c
->vattr_sizes
[loc
]; i
++) {
1333 c
->inputs
[loc
* 4 + i
] =
1334 ntq_emit_vpm_read(c
,
1335 &vpm_components_queued
,
1342 if (c
->devinfo
->ver
>= 40) {
1343 assert(vpm_components_queued
== num_components
);
1345 assert(vpm_components_queued
== 0);
1346 assert(num_components
== 0);
1351 ntq_setup_fs_inputs(struct v3d_compile
*c
)
1353 unsigned num_entries
= 0;
1354 unsigned num_components
= 0;
1355 nir_foreach_variable(var
, &c
->s
->inputs
) {
1357 num_components
+= glsl_get_components(var
->type
);
1360 nir_variable
*vars
[num_entries
];
1363 nir_foreach_variable(var
, &c
->s
->inputs
)
1366 /* Sort the variables so that we emit the input setup in
1367 * driver_location order. This is required for VPM reads, whose data
1368 * is fetched into the VPM in driver_location (TGSI register index)
1371 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1373 for (unsigned i
= 0; i
< num_entries
; i
++) {
1374 nir_variable
*var
= vars
[i
];
1375 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1376 unsigned loc
= var
->data
.driver_location
;
1378 assert(array_len
== 1);
1380 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1383 if (var
->data
.location
== VARYING_SLOT_POS
) {
1384 emit_fragcoord_input(c
, loc
);
1385 } else if (var
->data
.location
== VARYING_SLOT_PNTC
||
1386 (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1387 (c
->fs_key
->point_sprite_mask
&
1388 (1 << (var
->data
.location
-
1389 VARYING_SLOT_VAR0
))))) {
1390 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1391 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1393 emit_fragment_input(c
, loc
, var
);
1399 ntq_setup_outputs(struct v3d_compile
*c
)
1401 nir_foreach_variable(var
, &c
->s
->outputs
) {
1402 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1403 unsigned loc
= var
->data
.driver_location
* 4;
1405 assert(array_len
== 1);
1408 for (int i
= 0; i
< 4 - var
->data
.location_frac
; i
++) {
1409 add_output(c
, loc
+ var
->data
.location_frac
+ i
,
1411 var
->data
.location_frac
+ i
);
1414 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1415 switch (var
->data
.location
) {
1416 case FRAG_RESULT_COLOR
:
1417 c
->output_color_var
[0] = var
;
1418 c
->output_color_var
[1] = var
;
1419 c
->output_color_var
[2] = var
;
1420 c
->output_color_var
[3] = var
;
1422 case FRAG_RESULT_DATA0
:
1423 case FRAG_RESULT_DATA1
:
1424 case FRAG_RESULT_DATA2
:
1425 case FRAG_RESULT_DATA3
:
1426 c
->output_color_var
[var
->data
.location
-
1427 FRAG_RESULT_DATA0
] = var
;
1429 case FRAG_RESULT_DEPTH
:
1430 c
->output_position_index
= loc
;
1432 case FRAG_RESULT_SAMPLE_MASK
:
1433 c
->output_sample_mask_index
= loc
;
1437 switch (var
->data
.location
) {
1438 case VARYING_SLOT_POS
:
1439 c
->output_position_index
= loc
;
1441 case VARYING_SLOT_PSIZ
:
1442 c
->output_point_size_index
= loc
;
1450 ntq_setup_uniforms(struct v3d_compile
*c
)
1452 nir_foreach_variable(var
, &c
->s
->uniforms
) {
1453 uint32_t vec4_count
= glsl_count_attribute_slots(var
->type
,
1455 unsigned vec4_size
= 4 * sizeof(float);
1457 declare_uniform_range(c
, var
->data
.driver_location
* vec4_size
,
1458 vec4_count
* vec4_size
);
1464 * Sets up the mapping from nir_register to struct qreg *.
1466 * Each nir_register gets a struct qreg per 32-bit component being stored.
1469 ntq_setup_registers(struct v3d_compile
*c
, struct exec_list
*list
)
1471 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1472 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1473 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1475 nir_reg
->num_components
);
1477 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1479 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1480 qregs
[i
] = vir_get_temp(c
);
1485 ntq_emit_load_const(struct v3d_compile
*c
, nir_load_const_instr
*instr
)
1487 /* XXX perf: Experiment with using immediate loads to avoid having
1488 * these end up in the uniform stream. Watch out for breaking the
1489 * small immediates optimization in the process!
1491 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1492 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1493 qregs
[i
] = vir_uniform_ui(c
, instr
->value
.u32
[i
]);
1495 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1499 ntq_emit_ssa_undef(struct v3d_compile
*c
, nir_ssa_undef_instr
*instr
)
1501 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1503 /* VIR needs there to be *some* value, so pick 0 (same as for
1504 * ntq_setup_registers().
1506 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1507 qregs
[i
] = vir_uniform_ui(c
, 0);
1511 ntq_emit_intrinsic(struct v3d_compile
*c
, nir_intrinsic_instr
*instr
)
1515 switch (instr
->intrinsic
) {
1516 case nir_intrinsic_load_uniform
:
1517 assert(instr
->num_components
== 1);
1518 if (nir_src_is_const(instr
->src
[0])) {
1519 offset
= (nir_intrinsic_base(instr
) +
1520 nir_src_as_uint(instr
->src
[0]));
1521 assert(offset
% 4 == 0);
1522 /* We need dwords */
1523 offset
= offset
/ 4;
1524 ntq_store_dest(c
, &instr
->dest
, 0,
1525 vir_uniform(c
, QUNIFORM_UNIFORM
,
1528 ntq_store_dest(c
, &instr
->dest
, 0,
1529 indirect_uniform_load(c
, instr
));
1533 case nir_intrinsic_load_ubo
:
1534 for (int i
= 0; i
< instr
->num_components
; i
++) {
1535 int ubo
= nir_src_as_uint(instr
->src
[0]);
1537 /* XXX perf: On V3D 4.x with uniform offsets, we
1538 * should probably try setting UBOs up in the A
1539 * register file and doing a sequence of loads that
1542 /* Adjust for where we stored the TGSI register base. */
1544 vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUA
),
1545 vir_uniform(c
, QUNIFORM_UBO_ADDR
, 1 + ubo
),
1547 ntq_get_src(c
, instr
->src
[1], 0),
1548 vir_uniform_ui(c
, i
* 4)));
1552 ntq_store_dest(c
, &instr
->dest
, i
, vir_LDTMU(c
));
1556 if (nir_src_is_const(instr
->src
[0])) {
1557 offset
= (nir_intrinsic_base(instr
) +
1558 nir_src_as_uint(instr
->src
[0]));
1559 assert(offset
% 4 == 0);
1560 /* We need dwords */
1561 offset
= offset
/ 4;
1562 ntq_store_dest(c
, &instr
->dest
, 0,
1563 vir_uniform(c
, QUNIFORM_UNIFORM
,
1566 ntq_store_dest(c
, &instr
->dest
, 0,
1567 indirect_uniform_load(c
, instr
));
1571 case nir_intrinsic_load_user_clip_plane
:
1572 for (int i
= 0; i
< instr
->num_components
; i
++) {
1573 ntq_store_dest(c
, &instr
->dest
, i
,
1574 vir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1575 nir_intrinsic_ucp_id(instr
) *
1580 case nir_intrinsic_load_alpha_ref_float
:
1581 ntq_store_dest(c
, &instr
->dest
, 0,
1582 vir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1585 case nir_intrinsic_load_sample_mask_in
:
1586 ntq_store_dest(c
, &instr
->dest
, 0, vir_MSF(c
));
1589 case nir_intrinsic_load_helper_invocation
:
1590 vir_PF(c
, vir_MSF(c
), V3D_QPU_PF_PUSHZ
);
1591 ntq_store_dest(c
, &instr
->dest
, 0,
1592 vir_MOV(c
, vir_SEL(c
, V3D_QPU_COND_IFA
,
1593 vir_uniform_ui(c
, ~0),
1594 vir_uniform_ui(c
, 0))));
1597 case nir_intrinsic_load_front_face
:
1598 /* The register contains 0 (front) or 1 (back), and we need to
1599 * turn it into a NIR bool where true means front.
1601 ntq_store_dest(c
, &instr
->dest
, 0,
1603 vir_uniform_ui(c
, -1),
1607 case nir_intrinsic_load_instance_id
:
1608 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->iid
));
1611 case nir_intrinsic_load_vertex_id
:
1612 ntq_store_dest(c
, &instr
->dest
, 0, vir_MOV(c
, c
->vid
));
1615 case nir_intrinsic_load_input
:
1616 for (int i
= 0; i
< instr
->num_components
; i
++) {
1617 offset
= (nir_intrinsic_base(instr
) +
1618 nir_src_as_uint(instr
->src
[0]));
1619 int comp
= nir_intrinsic_component(instr
) + i
;
1620 ntq_store_dest(c
, &instr
->dest
, i
,
1621 vir_MOV(c
, c
->inputs
[offset
* 4 + comp
]));
1625 case nir_intrinsic_store_output
:
1626 offset
= ((nir_intrinsic_base(instr
) +
1627 nir_src_as_uint(instr
->src
[1])) * 4 +
1628 nir_intrinsic_component(instr
));
1630 for (int i
= 0; i
< instr
->num_components
; i
++) {
1631 c
->outputs
[offset
+ i
] =
1632 vir_MOV(c
, ntq_get_src(c
, instr
->src
[0], i
));
1634 c
->num_outputs
= MAX2(c
->num_outputs
,
1635 offset
+ instr
->num_components
);
1638 case nir_intrinsic_discard
:
1639 if (c
->execute
.file
!= QFILE_NULL
) {
1640 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1641 vir_set_cond(vir_SETMSF_dest(c
, vir_reg(QFILE_NULL
, 0),
1642 vir_uniform_ui(c
, 0)),
1645 vir_SETMSF_dest(c
, vir_reg(QFILE_NULL
, 0),
1646 vir_uniform_ui(c
, 0));
1650 case nir_intrinsic_discard_if
: {
1651 /* true (~0) if we're discarding */
1652 struct qreg cond
= ntq_get_src(c
, instr
->src
[0], 0);
1654 if (c
->execute
.file
!= QFILE_NULL
) {
1655 /* execute == 0 means the channel is active. Invert
1656 * the condition so that we can use zero as "executing
1659 vir_PF(c
, vir_OR(c
, c
->execute
, vir_NOT(c
, cond
)),
1661 vir_set_cond(vir_SETMSF_dest(c
, vir_reg(QFILE_NULL
, 0),
1662 vir_uniform_ui(c
, 0)),
1665 vir_PF(c
, cond
, V3D_QPU_PF_PUSHZ
);
1666 vir_set_cond(vir_SETMSF_dest(c
, vir_reg(QFILE_NULL
, 0),
1667 vir_uniform_ui(c
, 0)),
1675 fprintf(stderr
, "Unknown intrinsic: ");
1676 nir_print_instr(&instr
->instr
, stderr
);
1677 fprintf(stderr
, "\n");
1682 /* Clears (activates) the execute flags for any channels whose jump target
1683 * matches this block.
1685 * XXX perf: Could we be using flpush/flpop somehow for our execution channel
1688 * XXX perf: For uniform control flow, we should be able to skip c->execute
1689 * handling entirely.
1692 ntq_activate_execute_for_block(struct v3d_compile
*c
)
1694 vir_set_pf(vir_XOR_dest(c
, vir_reg(QFILE_NULL
, 0),
1695 c
->execute
, vir_uniform_ui(c
, c
->cur_block
->index
)),
1698 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
1702 ntq_emit_if(struct v3d_compile
*c
, nir_if
*if_stmt
)
1704 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
1705 bool empty_else_block
=
1706 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
1707 exec_list_is_empty(&nir_else_block
->instr_list
));
1709 struct qblock
*then_block
= vir_new_block(c
);
1710 struct qblock
*after_block
= vir_new_block(c
);
1711 struct qblock
*else_block
;
1712 if (empty_else_block
)
1713 else_block
= after_block
;
1715 else_block
= vir_new_block(c
);
1717 bool was_top_level
= false;
1718 if (c
->execute
.file
== QFILE_NULL
) {
1719 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
1720 was_top_level
= true;
1723 /* Set A for executing (execute == 0) and jumping (if->condition ==
1724 * 0) channels, and then update execute flags for those to point to
1727 * XXX perf: we could reuse ntq_emit_comparison() to generate our if
1728 * condition, and the .uf field to ignore non-executing channels, to
1729 * reduce the overhead of if statements.
1733 ntq_get_src(c
, if_stmt
->condition
, 0)),
1735 vir_MOV_cond(c
, V3D_QPU_COND_IFA
,
1737 vir_uniform_ui(c
, else_block
->index
));
1739 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1742 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1743 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLNA
);
1744 vir_link_blocks(c
->cur_block
, else_block
);
1745 vir_link_blocks(c
->cur_block
, then_block
);
1747 /* Process the THEN block. */
1748 vir_set_emit_block(c
, then_block
);
1749 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
1751 if (!empty_else_block
) {
1752 /* Handle the end of the THEN block. First, all currently
1753 * active channels update their execute flags to point to
1756 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1757 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
1758 vir_uniform_ui(c
, after_block
->index
));
1760 /* If everything points at ENDIF, then jump there immediately. */
1761 vir_PF(c
, vir_XOR(c
, c
->execute
,
1762 vir_uniform_ui(c
, after_block
->index
)),
1764 vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ALLA
);
1765 vir_link_blocks(c
->cur_block
, after_block
);
1766 vir_link_blocks(c
->cur_block
, else_block
);
1768 vir_set_emit_block(c
, else_block
);
1769 ntq_activate_execute_for_block(c
);
1770 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
1773 vir_link_blocks(c
->cur_block
, after_block
);
1775 vir_set_emit_block(c
, after_block
);
1777 c
->execute
= c
->undef
;
1779 ntq_activate_execute_for_block(c
);
1783 ntq_emit_jump(struct v3d_compile
*c
, nir_jump_instr
*jump
)
1785 switch (jump
->type
) {
1786 case nir_jump_break
:
1787 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1788 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
1789 vir_uniform_ui(c
, c
->loop_break_block
->index
));
1792 case nir_jump_continue
:
1793 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1794 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
,
1795 vir_uniform_ui(c
, c
->loop_cont_block
->index
));
1798 case nir_jump_return
:
1799 unreachable("All returns shouold be lowered\n");
1804 ntq_emit_instr(struct v3d_compile
*c
, nir_instr
*instr
)
1806 switch (instr
->type
) {
1807 case nir_instr_type_alu
:
1808 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1811 case nir_instr_type_intrinsic
:
1812 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1815 case nir_instr_type_load_const
:
1816 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1819 case nir_instr_type_ssa_undef
:
1820 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
1823 case nir_instr_type_tex
:
1824 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1827 case nir_instr_type_jump
:
1828 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
1832 fprintf(stderr
, "Unknown NIR instr type: ");
1833 nir_print_instr(instr
, stderr
);
1834 fprintf(stderr
, "\n");
1840 ntq_emit_block(struct v3d_compile
*c
, nir_block
*block
)
1842 nir_foreach_instr(instr
, block
) {
1843 ntq_emit_instr(c
, instr
);
1847 static void ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
);
1850 ntq_emit_loop(struct v3d_compile
*c
, nir_loop
*loop
)
1852 bool was_top_level
= false;
1853 if (c
->execute
.file
== QFILE_NULL
) {
1854 c
->execute
= vir_MOV(c
, vir_uniform_ui(c
, 0));
1855 was_top_level
= true;
1858 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
1859 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
1861 c
->loop_cont_block
= vir_new_block(c
);
1862 c
->loop_break_block
= vir_new_block(c
);
1864 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1865 vir_set_emit_block(c
, c
->loop_cont_block
);
1866 ntq_activate_execute_for_block(c
);
1868 ntq_emit_cf_list(c
, &loop
->body
);
1870 /* Re-enable any previous continues now, so our ANYA check below
1873 * XXX: Use the .ORZ flags update, instead.
1875 vir_PF(c
, vir_XOR(c
,
1877 vir_uniform_ui(c
, c
->loop_cont_block
->index
)),
1879 vir_MOV_cond(c
, V3D_QPU_COND_IFA
, c
->execute
, vir_uniform_ui(c
, 0));
1881 vir_PF(c
, c
->execute
, V3D_QPU_PF_PUSHZ
);
1883 struct qinst
*branch
= vir_BRANCH(c
, V3D_QPU_BRANCH_COND_ANYA
);
1884 /* Pixels that were not dispatched or have been discarded should not
1885 * contribute to looping again.
1887 branch
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_P
;
1888 vir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1889 vir_link_blocks(c
->cur_block
, c
->loop_break_block
);
1891 vir_set_emit_block(c
, c
->loop_break_block
);
1893 c
->execute
= c
->undef
;
1895 ntq_activate_execute_for_block(c
);
1897 c
->loop_break_block
= save_loop_break_block
;
1898 c
->loop_cont_block
= save_loop_cont_block
;
1904 ntq_emit_function(struct v3d_compile
*c
, nir_function_impl
*func
)
1906 fprintf(stderr
, "FUNCTIONS not handled.\n");
1911 ntq_emit_cf_list(struct v3d_compile
*c
, struct exec_list
*list
)
1913 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1914 switch (node
->type
) {
1915 case nir_cf_node_block
:
1916 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1919 case nir_cf_node_if
:
1920 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1923 case nir_cf_node_loop
:
1924 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
1927 case nir_cf_node_function
:
1928 ntq_emit_function(c
, nir_cf_node_as_function(node
));
1932 fprintf(stderr
, "Unknown NIR node type\n");
1939 ntq_emit_impl(struct v3d_compile
*c
, nir_function_impl
*impl
)
1941 ntq_setup_registers(c
, &impl
->registers
);
1942 ntq_emit_cf_list(c
, &impl
->body
);
1946 nir_to_vir(struct v3d_compile
*c
)
1948 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1949 c
->payload_w
= vir_MOV(c
, vir_reg(QFILE_REG
, 0));
1950 c
->payload_w_centroid
= vir_MOV(c
, vir_reg(QFILE_REG
, 1));
1951 c
->payload_z
= vir_MOV(c
, vir_reg(QFILE_REG
, 2));
1953 /* XXX perf: We could set the "disable implicit point/line
1954 * varyings" field in the shader record and not emit these, if
1955 * they're not going to be used.
1957 if (c
->fs_key
->is_points
) {
1958 c
->point_x
= emit_fragment_varying(c
, NULL
, 0);
1959 c
->point_y
= emit_fragment_varying(c
, NULL
, 0);
1960 } else if (c
->fs_key
->is_lines
) {
1961 c
->line_x
= emit_fragment_varying(c
, NULL
, 0);
1965 if (c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1966 ntq_setup_fs_inputs(c
);
1968 ntq_setup_vpm_inputs(c
);
1970 ntq_setup_outputs(c
);
1971 ntq_setup_uniforms(c
);
1972 ntq_setup_registers(c
, &c
->s
->registers
);
1974 /* Find the main function and emit the body. */
1975 nir_foreach_function(function
, c
->s
) {
1976 assert(strcmp(function
->name
, "main") == 0);
1977 assert(function
->impl
);
1978 ntq_emit_impl(c
, function
->impl
);
1982 const nir_shader_compiler_options v3d_nir_options
= {
1983 .lower_all_io_to_temps
= true,
1984 .lower_extract_byte
= true,
1985 .lower_extract_word
= true,
1987 .lower_bitfield_insert_to_shifts
= true,
1988 .lower_bitfield_extract_to_shifts
= true,
1989 .lower_bitfield_reverse
= true,
1990 .lower_bit_count
= true,
1991 .lower_pack_unorm_2x16
= true,
1992 .lower_pack_snorm_2x16
= true,
1993 .lower_pack_unorm_4x8
= true,
1994 .lower_pack_snorm_4x8
= true,
1995 .lower_unpack_unorm_4x8
= true,
1996 .lower_unpack_snorm_4x8
= true,
1997 .lower_pack_half_2x16
= true,
1998 .lower_unpack_half_2x16
= true,
2000 .lower_find_lsb
= true,
2002 .lower_flrp32
= true,
2005 .lower_fsqrt
= true,
2006 .lower_ifind_msb
= true,
2007 .lower_ldexp
= true,
2008 .lower_mul_high
= true,
2009 .lower_wpos_pntc
= true,
2010 .native_integers
= true,
2014 * When demoting a shader down to single-threaded, removes the THRSW
2015 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2019 vir_remove_thrsw(struct v3d_compile
*c
)
2021 vir_for_each_block(block
, c
) {
2022 vir_for_each_inst_safe(inst
, block
) {
2023 if (inst
->qpu
.sig
.thrsw
)
2024 vir_remove_instruction(c
, inst
);
2028 c
->last_thrsw
= NULL
;
2032 vir_emit_last_thrsw(struct v3d_compile
*c
)
2034 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2035 * switching, so disable threads if we didn't do any TMU ops (each of
2036 * which would have emitted a THRSW).
2038 if (!c
->last_thrsw_at_top_level
&& c
->devinfo
->ver
< 41) {
2041 vir_remove_thrsw(c
);
2045 /* If we're threaded and the last THRSW was in conditional code, then
2046 * we need to emit another one so that we can flag it as the last
2049 if (c
->last_thrsw
&& !c
->last_thrsw_at_top_level
) {
2050 assert(c
->devinfo
->ver
>= 41);
2054 /* If we're threaded, then we need to mark the last THRSW instruction
2055 * so we can emit a pair of them at QPU emit time.
2057 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2058 * post-last-THRSW state, so we can skip this.
2060 if (!c
->last_thrsw
&& c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2061 assert(c
->devinfo
->ver
>= 41);
2066 c
->last_thrsw
->is_last_thrsw
= true;
2069 /* There's a flag in the shader for "center W is needed for reasons other than
2070 * non-centroid varyings", so we just walk the program after VIR optimization
2071 * to see if it's used. It should be harmless to set even if we only use
2072 * center W for varyings.
2075 vir_check_payload_w(struct v3d_compile
*c
)
2077 if (c
->s
->info
.stage
!= MESA_SHADER_FRAGMENT
)
2080 vir_for_each_inst_inorder(inst
, c
) {
2081 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
2082 if (inst
->src
[i
].file
== QFILE_REG
&&
2083 inst
->src
[i
].index
== 0) {
2084 c
->uses_center_w
= true;
2093 v3d_nir_to_vir(struct v3d_compile
*c
)
2095 if (V3D_DEBUG
& (V3D_DEBUG_NIR
|
2096 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2097 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2098 vir_get_stage_name(c
),
2099 c
->program_id
, c
->variant_id
);
2100 nir_print_shader(c
->s
, stderr
);
2105 /* Emit the last THRSW before STVPM and TLB writes. */
2106 vir_emit_last_thrsw(c
);
2108 switch (c
->s
->info
.stage
) {
2109 case MESA_SHADER_FRAGMENT
:
2112 case MESA_SHADER_VERTEX
:
2116 unreachable("bad stage");
2119 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2120 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2121 fprintf(stderr
, "%s prog %d/%d pre-opt VIR:\n",
2122 vir_get_stage_name(c
),
2123 c
->program_id
, c
->variant_id
);
2125 fprintf(stderr
, "\n");
2129 vir_lower_uniforms(c
);
2131 vir_check_payload_w(c
);
2133 /* XXX perf: On VC4, we do a VIR-level instruction scheduling here.
2134 * We used that on that platform to pipeline TMU writes and reduce the
2135 * number of thread switches, as well as try (mostly successfully) to
2136 * reduce maximum register pressure to allow more threads. We should
2137 * do something of that sort for V3D -- either instruction scheduling
2138 * here, or delay the the THRSW and LDTMUs from our texture
2139 * instructions until the results are needed.
2142 if (V3D_DEBUG
& (V3D_DEBUG_VIR
|
2143 v3d_debug_flag_for_shader_stage(c
->s
->info
.stage
))) {
2144 fprintf(stderr
, "%s prog %d/%d VIR:\n",
2145 vir_get_stage_name(c
),
2146 c
->program_id
, c
->variant_id
);
2148 fprintf(stderr
, "\n");
2151 /* Attempt to allocate registers for the temporaries. If we fail,
2152 * reduce thread count and try again.
2154 int min_threads
= (c
->devinfo
->ver
>= 41) ? 2 : 1;
2155 struct qpu_reg
*temp_registers
;
2158 temp_registers
= v3d_register_allocate(c
, &spilled
);
2165 if (c
->threads
== min_threads
) {
2166 fprintf(stderr
, "Failed to register allocate at %d threads:\n",
2175 if (c
->threads
== 1)
2176 vir_remove_thrsw(c
);
2179 v3d_vir_to_qpu(c
, temp_registers
);