2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "util/u_format.h"
27 #include "util/crc32.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/ralloc.h"
31 #include "util/hash_table.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "compiler/nir/nir.h"
35 #include "compiler/nir/nir_builder.h"
36 #include "nir/tgsi_to_nir.h"
37 #include "vc4_context.h"
40 #include "mesa/state_tracker/st_glsl_types.h"
43 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
45 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
48 resize_qreg_array(struct vc4_compile
*c
,
53 if (*size
>= decl_size
)
56 uint32_t old_size
= *size
;
57 *size
= MAX2(*size
* 2, decl_size
);
58 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
60 fprintf(stderr
, "Malloc failure\n");
64 for (uint32_t i
= old_size
; i
< *size
; i
++)
65 (*regs
)[i
] = c
->undef
;
69 ntq_emit_thrsw(struct vc4_compile
*c
)
74 /* Always thread switch after each texture operation for now.
76 * We could do better by batching a bunch of texture fetches up and
77 * then doing one thread switch and collecting all their results
80 qir_emit_nondef(c
, qir_inst(QOP_THRSW
, c
->undef
,
82 c
->last_thrsw_at_top_level
= (c
->execute
.file
== QFILE_NULL
);
86 indirect_uniform_load(struct vc4_compile
*c
, nir_intrinsic_instr
*intr
)
88 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
89 uint32_t offset
= nir_intrinsic_base(intr
);
90 struct vc4_compiler_ubo_range
*range
= NULL
;
92 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
93 range
= &c
->ubo_ranges
[i
];
94 if (offset
>= range
->src_offset
&&
95 offset
< range
->src_offset
+ range
->size
) {
99 /* The driver-location-based offset always has to be within a declared
105 range
->dst_offset
= c
->next_ubo_dst_offset
;
106 c
->next_ubo_dst_offset
+= range
->size
;
110 offset
-= range
->src_offset
;
112 /* Adjust for where we stored the TGSI register base. */
113 indirect_offset
= qir_ADD(c
, indirect_offset
,
114 qir_uniform_ui(c
, (range
->dst_offset
+
117 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
118 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
119 indirect_offset
= qir_MIN_NOIMM(c
, indirect_offset
,
120 qir_uniform_ui(c
, (range
->dst_offset
+
123 qir_ADD_dest(c
, qir_reg(QFILE_TEX_S_DIRECT
, 0),
125 qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
127 c
->num_texture_samples
++;
131 return qir_TEX_RESULT(c
);
135 vc4_nir_get_swizzled_channel(nir_builder
*b
, nir_ssa_def
**srcs
, int swiz
)
139 case PIPE_SWIZZLE_NONE
:
140 fprintf(stderr
, "warning: unknown swizzle\n");
143 return nir_imm_float(b
, 0.0);
145 return nir_imm_float(b
, 1.0);
155 ntq_init_ssa_def(struct vc4_compile
*c
, nir_ssa_def
*def
)
157 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
158 def
->num_components
);
159 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
164 * This function is responsible for getting QIR results into the associated
165 * storage for a NIR instruction.
167 * If it's a NIR SSA def, then we just set the associated hash table entry to
170 * If it's a NIR reg, then we need to update the existing qreg assigned to the
171 * NIR destination with the incoming value. To do that without introducing
172 * new MOVs, we require that the incoming qreg either be a uniform, or be
173 * SSA-defined by the previous QIR instruction in the block and rewritable by
174 * this function. That lets us sneak ahead and insert the SF flag beforehand
175 * (knowing that the previous instruction doesn't depend on flags) and rewrite
176 * its destination to be the NIR reg's destination
179 ntq_store_dest(struct vc4_compile
*c
, nir_dest
*dest
, int chan
,
182 struct qinst
*last_inst
= NULL
;
183 if (!list_empty(&c
->cur_block
->instructions
))
184 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
186 assert(result
.file
== QFILE_UNIF
||
187 (result
.file
== QFILE_TEMP
&&
188 last_inst
&& last_inst
== c
->defs
[result
.index
]));
191 assert(chan
< dest
->ssa
.num_components
);
194 struct hash_entry
*entry
=
195 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
200 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
202 qregs
[chan
] = result
;
204 nir_register
*reg
= dest
->reg
.reg
;
205 assert(dest
->reg
.base_offset
== 0);
206 assert(reg
->num_array_elems
== 0);
207 struct hash_entry
*entry
=
208 _mesa_hash_table_search(c
->def_ht
, reg
);
209 struct qreg
*qregs
= entry
->data
;
211 /* Insert a MOV if the source wasn't an SSA def in the
212 * previous instruction.
214 if (result
.file
== QFILE_UNIF
) {
215 result
= qir_MOV(c
, result
);
216 last_inst
= c
->defs
[result
.index
];
219 /* We know they're both temps, so just rewrite index. */
220 c
->defs
[last_inst
->dst
.index
] = NULL
;
221 last_inst
->dst
.index
= qregs
[chan
].index
;
223 /* If we're in control flow, then make this update of the reg
224 * conditional on the execution mask.
226 if (c
->execute
.file
!= QFILE_NULL
) {
227 last_inst
->dst
.index
= qregs
[chan
].index
;
229 /* Set the flags to the current exec mask. To insert
230 * the SF, we temporarily remove our SSA instruction.
232 list_del(&last_inst
->link
);
233 qir_SF(c
, c
->execute
);
234 list_addtail(&last_inst
->link
,
235 &c
->cur_block
->instructions
);
237 last_inst
->cond
= QPU_COND_ZS
;
238 last_inst
->cond_is_exec_mask
= true;
244 ntq_get_dest(struct vc4_compile
*c
, nir_dest
*dest
)
247 struct qreg
*qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
248 for (int i
= 0; i
< dest
->ssa
.num_components
; i
++)
252 nir_register
*reg
= dest
->reg
.reg
;
253 assert(dest
->reg
.base_offset
== 0);
254 assert(reg
->num_array_elems
== 0);
255 struct hash_entry
*entry
=
256 _mesa_hash_table_search(c
->def_ht
, reg
);
262 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
264 struct hash_entry
*entry
;
266 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
267 assert(i
< src
.ssa
->num_components
);
269 nir_register
*reg
= src
.reg
.reg
;
270 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
271 assert(reg
->num_array_elems
== 0);
272 assert(src
.reg
.base_offset
== 0);
273 assert(i
< reg
->num_components
);
276 struct qreg
*qregs
= entry
->data
;
281 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
284 assert(util_is_power_of_two(instr
->dest
.write_mask
));
285 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
286 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
287 instr
->src
[src
].swizzle
[chan
]);
289 assert(!instr
->src
[src
].abs
);
290 assert(!instr
->src
[src
].negate
);
295 static inline struct qreg
296 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
299 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
300 qir_uniform_f(c
, 0.0));
304 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
306 struct qreg r
= qir_RCP(c
, x
);
308 /* Apply a Newton-Raphson step to improve the accuracy. */
309 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
310 qir_uniform_f(c
, 2.0),
317 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
319 struct qreg r
= qir_RSQ(c
, x
);
321 /* Apply a Newton-Raphson step to improve the accuracy. */
322 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
323 qir_uniform_f(c
, 1.5),
325 qir_uniform_f(c
, 0.5),
327 qir_FMUL(c
, r
, r
)))));
333 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
335 struct qreg src0_hi
= qir_SHR(c
, src0
,
336 qir_uniform_ui(c
, 24));
337 struct qreg src1_hi
= qir_SHR(c
, src1
,
338 qir_uniform_ui(c
, 24));
340 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
341 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
342 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
344 return qir_ADD(c
, lolo
, qir_SHL(c
,
345 qir_ADD(c
, hilo
, lohi
),
346 qir_uniform_ui(c
, 24)));
350 ntq_scale_depth_texture(struct vc4_compile
*c
, struct qreg src
)
352 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, src
,
353 qir_uniform_ui(c
, 8)));
354 return qir_FMUL(c
, depthf
, qir_uniform_f(c
, 1.0f
/0xffffff));
358 * Emits a lowered TXF_MS from an MSAA texture.
360 * The addressing math has been lowered in NIR, and now we just need to read
364 ntq_emit_txf(struct vc4_compile
*c
, nir_tex_instr
*instr
)
366 uint32_t tile_width
= 32;
367 uint32_t tile_height
= 32;
368 uint32_t tile_size
= (tile_height
* tile_width
*
369 VC4_MAX_SAMPLES
* sizeof(uint32_t));
371 unsigned unit
= instr
->texture_index
;
372 uint32_t w
= align(c
->key
->tex
[unit
].msaa_width
, tile_width
);
373 uint32_t w_tiles
= w
/ tile_width
;
374 uint32_t h
= align(c
->key
->tex
[unit
].msaa_height
, tile_height
);
375 uint32_t h_tiles
= h
/ tile_height
;
376 uint32_t size
= w_tiles
* h_tiles
* tile_size
;
379 assert(instr
->num_srcs
== 1);
380 assert(instr
->src
[0].src_type
== nir_tex_src_coord
);
381 addr
= ntq_get_src(c
, instr
->src
[0].src
, 0);
383 /* Perform the clamping required by kernel validation. */
384 addr
= qir_MAX(c
, addr
, qir_uniform_ui(c
, 0));
385 addr
= qir_MIN_NOIMM(c
, addr
, qir_uniform_ui(c
, size
- 4));
387 qir_ADD_dest(c
, qir_reg(QFILE_TEX_S_DIRECT
, 0),
388 addr
, qir_uniform(c
, QUNIFORM_TEXTURE_MSAA_ADDR
, unit
));
392 struct qreg tex
= qir_TEX_RESULT(c
);
393 c
->num_texture_samples
++;
395 enum pipe_format format
= c
->key
->tex
[unit
].format
;
396 if (util_format_is_depth_or_stencil(format
)) {
397 struct qreg scaled
= ntq_scale_depth_texture(c
, tex
);
398 for (int i
= 0; i
< 4; i
++)
399 ntq_store_dest(c
, &instr
->dest
, i
, qir_MOV(c
, scaled
));
401 for (int i
= 0; i
< 4; i
++)
402 ntq_store_dest(c
, &instr
->dest
, i
,
403 qir_UNPACK_8_F(c
, tex
, i
));
408 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
410 struct qreg s
, t
, r
, lod
, compare
;
411 bool is_txb
= false, is_txl
= false;
412 unsigned unit
= instr
->texture_index
;
414 if (instr
->op
== nir_texop_txf
) {
415 ntq_emit_txf(c
, instr
);
419 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
420 switch (instr
->src
[i
].src_type
) {
421 case nir_tex_src_coord
:
422 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
423 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
424 t
= qir_uniform_f(c
, 0.5);
426 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
427 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
428 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
430 case nir_tex_src_bias
:
431 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
434 case nir_tex_src_lod
:
435 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
438 case nir_tex_src_comparator
:
439 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
442 unreachable("unknown texture source");
446 if (c
->stage
!= QSTAGE_FRAG
&& !is_txl
) {
447 /* From the GLSL 1.20 spec:
449 * "If it is mip-mapped and running on the vertex shader,
450 * then the base texture is used."
453 lod
= qir_uniform_ui(c
, 0);
456 if (c
->key
->tex
[unit
].force_first_level
) {
457 lod
= qir_uniform(c
, QUNIFORM_TEXTURE_FIRST_LEVEL
, unit
);
462 struct qreg texture_u
[] = {
463 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
464 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
465 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
466 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
468 uint32_t next_texture_u
= 0;
470 /* There is no native support for GL texture rectangle coordinates, so
471 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
474 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
476 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
478 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
481 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
482 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
483 unit
| (is_txl
<< 16));
487 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
488 tmu
= qir_MOV_dest(c
, qir_reg(QFILE_TEX_R
, 0), r
);
489 tmu
->src
[qir_get_tex_uniform_src(tmu
)] =
490 texture_u
[next_texture_u
++];
491 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
492 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
493 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
494 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
495 tmu
= qir_MOV_dest(c
, qir_reg(QFILE_TEX_R
, 0),
496 qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
,
498 tmu
->src
[qir_get_tex_uniform_src(tmu
)] =
499 texture_u
[next_texture_u
++];
502 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
506 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
510 tmu
= qir_MOV_dest(c
, qir_reg(QFILE_TEX_T
, 0), t
);
511 tmu
->src
[qir_get_tex_uniform_src(tmu
)] =
512 texture_u
[next_texture_u
++];
514 if (is_txl
|| is_txb
) {
515 tmu
= qir_MOV_dest(c
, qir_reg(QFILE_TEX_B
, 0), lod
);
516 tmu
->src
[qir_get_tex_uniform_src(tmu
)] =
517 texture_u
[next_texture_u
++];
520 tmu
= qir_MOV_dest(c
, qir_reg(QFILE_TEX_S
, 0), s
);
521 tmu
->src
[qir_get_tex_uniform_src(tmu
)] = texture_u
[next_texture_u
++];
523 c
->num_texture_samples
++;
527 struct qreg tex
= qir_TEX_RESULT(c
);
529 enum pipe_format format
= c
->key
->tex
[unit
].format
;
531 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
);
532 if (util_format_is_depth_or_stencil(format
)) {
533 struct qreg normalized
= ntq_scale_depth_texture(c
, tex
);
534 struct qreg depth_output
;
536 struct qreg u0
= qir_uniform_f(c
, 0.0f
);
537 struct qreg u1
= qir_uniform_f(c
, 1.0f
);
538 if (c
->key
->tex
[unit
].compare_mode
) {
539 /* From the GL_ARB_shadow spec:
541 * "Let Dt (D subscript t) be the depth texture
542 * value, in the range [0, 1]. Let R be the
543 * interpolated texture coordinate clamped to the
546 compare
= qir_SAT(c
, compare
);
548 switch (c
->key
->tex
[unit
].compare_func
) {
549 case PIPE_FUNC_NEVER
:
550 depth_output
= qir_uniform_f(c
, 0.0f
);
552 case PIPE_FUNC_ALWAYS
:
555 case PIPE_FUNC_EQUAL
:
556 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
557 depth_output
= qir_SEL(c
, QPU_COND_ZS
, u1
, u0
);
559 case PIPE_FUNC_NOTEQUAL
:
560 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
561 depth_output
= qir_SEL(c
, QPU_COND_ZC
, u1
, u0
);
563 case PIPE_FUNC_GREATER
:
564 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
565 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
567 case PIPE_FUNC_GEQUAL
:
568 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
569 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
572 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
573 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
575 case PIPE_FUNC_LEQUAL
:
576 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
577 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
581 depth_output
= normalized
;
584 for (int i
= 0; i
< 4; i
++)
585 dest
[i
] = depth_output
;
587 for (int i
= 0; i
< 4; i
++)
588 dest
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
593 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
597 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
599 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
600 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
603 qir_FADD_dest(c
, diff
,
604 diff
, qir_uniform_f(c
, 1.0))->cond
= QPU_COND_NS
;
606 return qir_MOV(c
, diff
);
610 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
614 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
616 struct qreg result
= qir_ITOF(c
, qir_FTOI(c
, src
));
618 /* This will be < 0 if we truncated and the truncation was of a value
619 * that was < 0 in the first place.
621 qir_SF(c
, qir_FSUB(c
, src
, result
));
623 struct qinst
*sub
= qir_FSUB_dest(c
, result
,
624 result
, qir_uniform_f(c
, 1.0));
625 sub
->cond
= QPU_COND_NS
;
627 return qir_MOV(c
, result
);
631 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
635 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
637 struct qreg result
= qir_ITOF(c
, qir_FTOI(c
, src
));
639 /* This will be < 0 if we truncated and the truncation was of a value
640 * that was > 0 in the first place.
642 qir_SF(c
, qir_FSUB(c
, result
, src
));
644 qir_FADD_dest(c
, result
,
645 result
, qir_uniform_f(c
, 1.0))->cond
= QPU_COND_NS
;
647 return qir_MOV(c
, result
);
651 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
655 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
656 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
657 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
658 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
661 struct qreg scaled_x
=
664 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
666 struct qreg x
= qir_FADD(c
,
667 ntq_ffract(c
, scaled_x
),
668 qir_uniform_f(c
, -0.5));
669 struct qreg x2
= qir_FMUL(c
, x
, x
);
670 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
671 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
672 x
= qir_FMUL(c
, x
, x2
);
677 qir_uniform_f(c
, coeff
[i
])));
683 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
687 pow(2.0 * M_PI
, 2) / (2 * 1),
688 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
689 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
690 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
691 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
694 struct qreg scaled_x
=
696 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
697 struct qreg x_frac
= qir_FADD(c
,
698 ntq_ffract(c
, scaled_x
),
699 qir_uniform_f(c
, -0.5));
701 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
702 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
703 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
704 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
706 x
= qir_FMUL(c
, x
, x2
);
708 struct qreg mul
= qir_FMUL(c
,
710 qir_uniform_f(c
, coeff
[i
]));
714 sum
= qir_FADD(c
, sum
, mul
);
720 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
722 struct qreg t
= qir_get_temp(c
);
725 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 0.0));
726 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 1.0))->cond
= QPU_COND_ZC
;
727 qir_MOV_dest(c
, t
, qir_uniform_f(c
, -1.0))->cond
= QPU_COND_NS
;
728 return qir_MOV(c
, t
);
732 emit_vertex_input(struct vc4_compile
*c
, int attr
)
734 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
735 uint32_t attr_size
= util_format_get_blocksize(format
);
736 uint32_t vpm_attr
= c
->next_vpm_input
++;
738 c
->vpm_input_order
[vpm_attr
] = attr
;
740 c
->vattr_sizes
[vpm_attr
] = align(attr_size
, 4);
741 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
742 c
->inputs
[attr
* 4 + i
] =
743 qir_MOV(c
, qir_reg(QFILE_VPM
, vpm_attr
* 4 + i
));
749 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
751 c
->inputs
[attr
* 4 + 0] = qir_ITOF(c
, qir_reg(QFILE_FRAG_X
, 0));
752 c
->inputs
[attr
* 4 + 1] = qir_ITOF(c
, qir_reg(QFILE_FRAG_Y
, 0));
753 c
->inputs
[attr
* 4 + 2] =
755 qir_ITOF(c
, qir_FRAG_Z(c
)),
756 qir_uniform_f(c
, 1.0 / 0xffffff));
757 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
761 emit_fragment_varying(struct vc4_compile
*c
, gl_varying_slot slot
,
764 uint32_t i
= c
->num_input_slots
++;
770 if (c
->num_input_slots
>= c
->input_slots_array_size
) {
771 c
->input_slots_array_size
=
772 MAX2(4, c
->input_slots_array_size
* 2);
774 c
->input_slots
= reralloc(c
, c
->input_slots
,
775 struct vc4_varying_slot
,
776 c
->input_slots_array_size
);
779 c
->input_slots
[i
].slot
= slot
;
780 c
->input_slots
[i
].swizzle
= swizzle
;
782 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
786 emit_fragment_input(struct vc4_compile
*c
, int attr
, gl_varying_slot slot
)
788 for (int i
= 0; i
< 4; i
++) {
789 c
->inputs
[attr
* 4 + i
] =
790 emit_fragment_varying(c
, slot
, i
);
796 add_output(struct vc4_compile
*c
,
797 uint32_t decl_offset
,
801 uint32_t old_array_size
= c
->outputs_array_size
;
802 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
805 if (old_array_size
!= c
->outputs_array_size
) {
806 c
->output_slots
= reralloc(c
,
808 struct vc4_varying_slot
,
809 c
->outputs_array_size
);
812 c
->output_slots
[decl_offset
].slot
= slot
;
813 c
->output_slots
[decl_offset
].swizzle
= swizzle
;
817 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
819 unsigned array_id
= c
->num_uniform_ranges
++;
820 if (array_id
>= c
->ubo_ranges_array_size
) {
821 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
823 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
824 struct vc4_compiler_ubo_range
,
825 c
->ubo_ranges_array_size
);
828 c
->ubo_ranges
[array_id
].dst_offset
= 0;
829 c
->ubo_ranges
[array_id
].src_offset
= start
;
830 c
->ubo_ranges
[array_id
].size
= size
;
831 c
->ubo_ranges
[array_id
].used
= false;
835 ntq_src_is_only_ssa_def_user(nir_src
*src
)
840 if (!list_empty(&src
->ssa
->if_uses
))
843 return (src
->ssa
->uses
.next
== &src
->use_link
&&
844 src
->ssa
->uses
.next
->next
== &src
->ssa
->uses
);
848 * In general, emits a nir_pack_unorm_4x8 as a series of MOVs with the pack
851 * However, as an optimization, it tries to find the instructions generating
852 * the sources to be packed and just emit the pack flag there, if possible.
855 ntq_emit_pack_unorm_4x8(struct vc4_compile
*c
, nir_alu_instr
*instr
)
857 struct qreg result
= qir_get_temp(c
);
858 struct nir_alu_instr
*vec4
= NULL
;
860 /* If packing from a vec4 op (as expected), identify it so that we can
861 * peek back at what generated its sources.
863 if (instr
->src
[0].src
.is_ssa
&&
864 instr
->src
[0].src
.ssa
->parent_instr
->type
== nir_instr_type_alu
&&
865 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
)->op
==
867 vec4
= nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
870 /* If the pack is replicating the same channel 4 times, use the 8888
871 * pack flag. This is common for blending using the alpha
874 if (instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[1] &&
875 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[2] &&
876 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[3]) {
877 struct qreg rep
= ntq_get_src(c
,
879 instr
->src
[0].swizzle
[0]);
880 ntq_store_dest(c
, &instr
->dest
.dest
, 0, qir_PACK_8888_F(c
, rep
));
884 for (int i
= 0; i
< 4; i
++) {
885 int swiz
= instr
->src
[0].swizzle
[i
];
888 src
= ntq_get_src(c
, vec4
->src
[swiz
].src
,
889 vec4
->src
[swiz
].swizzle
[0]);
891 src
= ntq_get_src(c
, instr
->src
[0].src
, swiz
);
895 ntq_src_is_only_ssa_def_user(&vec4
->src
[swiz
].src
) &&
896 src
.file
== QFILE_TEMP
&&
897 c
->defs
[src
.index
] &&
898 qir_is_mul(c
->defs
[src
.index
]) &&
899 !c
->defs
[src
.index
]->dst
.pack
) {
900 struct qinst
*rewrite
= c
->defs
[src
.index
];
901 c
->defs
[src
.index
] = NULL
;
902 rewrite
->dst
= result
;
903 rewrite
->dst
.pack
= QPU_PACK_MUL_8A
+ i
;
907 qir_PACK_8_F(c
, result
, src
, i
);
910 ntq_store_dest(c
, &instr
->dest
.dest
, 0, qir_MOV(c
, result
));
913 /** Handles sign-extended bitfield extracts for 16 bits. */
915 ntq_emit_ibfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
918 assert(bits
.file
== QFILE_UNIF
&&
919 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
920 c
->uniform_data
[bits
.index
] == 16);
922 assert(offset
.file
== QFILE_UNIF
&&
923 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
924 int offset_bit
= c
->uniform_data
[offset
.index
];
925 assert(offset_bit
% 16 == 0);
927 return qir_UNPACK_16_I(c
, base
, offset_bit
/ 16);
930 /** Handles unsigned bitfield extracts for 8 bits. */
932 ntq_emit_ubfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
935 assert(bits
.file
== QFILE_UNIF
&&
936 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
937 c
->uniform_data
[bits
.index
] == 8);
939 assert(offset
.file
== QFILE_UNIF
&&
940 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
941 int offset_bit
= c
->uniform_data
[offset
.index
];
942 assert(offset_bit
% 8 == 0);
944 return qir_UNPACK_8_I(c
, base
, offset_bit
/ 8);
948 * If compare_instr is a valid comparison instruction, emits the
949 * compare_instr's comparison and returns the sel_instr's return value based
950 * on the compare_instr's result.
953 ntq_emit_comparison(struct vc4_compile
*c
, struct qreg
*dest
,
954 nir_alu_instr
*compare_instr
,
955 nir_alu_instr
*sel_instr
)
959 switch (compare_instr
->op
) {
985 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
986 struct qreg src1
= ntq_get_alu_src(c
, compare_instr
, 1);
988 unsigned unsized_type
=
989 nir_alu_type_get_base_type(nir_op_infos
[compare_instr
->op
].input_types
[0]);
990 if (unsized_type
== nir_type_float
)
991 qir_SF(c
, qir_FSUB(c
, src0
, src1
));
993 qir_SF(c
, qir_SUB(c
, src0
, src1
));
995 switch (sel_instr
->op
) {
1000 *dest
= qir_SEL(c
, cond
,
1001 qir_uniform_f(c
, 1.0), qir_uniform_f(c
, 0.0));
1005 *dest
= qir_SEL(c
, cond
,
1006 ntq_get_alu_src(c
, sel_instr
, 1),
1007 ntq_get_alu_src(c
, sel_instr
, 2));
1011 *dest
= qir_SEL(c
, cond
,
1012 qir_uniform_ui(c
, ~0), qir_uniform_ui(c
, 0));
1016 /* Make the temporary for nir_store_dest(). */
1017 *dest
= qir_MOV(c
, *dest
);
1023 * Attempts to fold a comparison generating a boolean result into the
1024 * condition code for selecting between two values, instead of comparing the
1025 * boolean result against 0 to generate the condition code.
1027 static struct qreg
ntq_emit_bcsel(struct vc4_compile
*c
, nir_alu_instr
*instr
,
1030 if (!instr
->src
[0].src
.is_ssa
)
1032 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
1034 nir_alu_instr
*compare
=
1035 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
1040 if (ntq_emit_comparison(c
, &dest
, compare
, instr
))
1045 return qir_MOV(c
, qir_SEL(c
, QPU_COND_NS
, src
[1], src
[2]));
1049 ntq_fddx(struct vc4_compile
*c
, struct qreg src
)
1051 /* Make sure that we have a bare temp to use for MUL rotation, so it
1052 * can be allocated to an accumulator.
1054 if (src
.pack
|| src
.file
!= QFILE_TEMP
)
1055 src
= qir_MOV(c
, src
);
1057 struct qreg from_left
= qir_ROT_MUL(c
, src
, 1);
1058 struct qreg from_right
= qir_ROT_MUL(c
, src
, 15);
1060 /* Distinguish left/right pixels of the quad. */
1061 qir_SF(c
, qir_AND(c
, qir_reg(QFILE_QPU_ELEMENT
, 0),
1062 qir_uniform_ui(c
, 1)));
1064 return qir_MOV(c
, qir_SEL(c
, QPU_COND_ZS
,
1065 qir_FSUB(c
, from_right
, src
),
1066 qir_FSUB(c
, src
, from_left
)));
1070 ntq_fddy(struct vc4_compile
*c
, struct qreg src
)
1072 if (src
.pack
|| src
.file
!= QFILE_TEMP
)
1073 src
= qir_MOV(c
, src
);
1075 struct qreg from_bottom
= qir_ROT_MUL(c
, src
, 2);
1076 struct qreg from_top
= qir_ROT_MUL(c
, src
, 14);
1078 /* Distinguish top/bottom pixels of the quad. */
1079 qir_SF(c
, qir_AND(c
,
1080 qir_reg(QFILE_QPU_ELEMENT
, 0),
1081 qir_uniform_ui(c
, 2)));
1083 return qir_MOV(c
, qir_SEL(c
, QPU_COND_ZS
,
1084 qir_FSUB(c
, from_top
, src
),
1085 qir_FSUB(c
, src
, from_bottom
)));
1089 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
1091 /* This should always be lowered to ALU operations for VC4. */
1092 assert(!instr
->dest
.saturate
);
1094 /* Vectors are special in that they have non-scalarized writemasks,
1095 * and just take the first swizzle channel for each argument in order
1096 * into each writemask channel.
1098 if (instr
->op
== nir_op_vec2
||
1099 instr
->op
== nir_op_vec3
||
1100 instr
->op
== nir_op_vec4
) {
1101 struct qreg srcs
[4];
1102 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1103 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
1104 instr
->src
[i
].swizzle
[0]);
1105 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1106 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
1107 qir_MOV(c
, srcs
[i
]));
1111 if (instr
->op
== nir_op_pack_unorm_4x8
) {
1112 ntq_emit_pack_unorm_4x8(c
, instr
);
1116 if (instr
->op
== nir_op_unpack_unorm_4x8
) {
1117 struct qreg src
= ntq_get_src(c
, instr
->src
[0].src
,
1118 instr
->src
[0].swizzle
[0]);
1119 for (int i
= 0; i
< 4; i
++) {
1120 if (instr
->dest
.write_mask
& (1 << i
))
1121 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
1122 qir_UNPACK_8_F(c
, src
, i
));
1127 /* General case: We can just grab the one used channel per src. */
1128 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
1129 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1130 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
1135 switch (instr
->op
) {
1138 result
= qir_MOV(c
, src
[0]);
1141 result
= qir_FMUL(c
, src
[0], src
[1]);
1144 result
= qir_FADD(c
, src
[0], src
[1]);
1147 result
= qir_FSUB(c
, src
[0], src
[1]);
1150 result
= qir_FMIN(c
, src
[0], src
[1]);
1153 result
= qir_FMAX(c
, src
[0], src
[1]);
1158 result
= qir_FTOI(c
, src
[0]);
1162 result
= qir_ITOF(c
, src
[0]);
1165 result
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
1168 result
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
1173 result
= qir_MOV(c
, qir_SEL(c
, QPU_COND_ZC
,
1174 qir_uniform_ui(c
, ~0),
1175 qir_uniform_ui(c
, 0)));
1179 result
= qir_ADD(c
, src
[0], src
[1]);
1182 result
= qir_SHR(c
, src
[0], src
[1]);
1185 result
= qir_SUB(c
, src
[0], src
[1]);
1188 result
= qir_ASR(c
, src
[0], src
[1]);
1191 result
= qir_SHL(c
, src
[0], src
[1]);
1194 result
= qir_MIN(c
, src
[0], src
[1]);
1197 result
= qir_MAX(c
, src
[0], src
[1]);
1200 result
= qir_AND(c
, src
[0], src
[1]);
1203 result
= qir_OR(c
, src
[0], src
[1]);
1206 result
= qir_XOR(c
, src
[0], src
[1]);
1209 result
= qir_NOT(c
, src
[0]);
1213 result
= ntq_umul(c
, src
[0], src
[1]);
1229 if (!ntq_emit_comparison(c
, &result
, instr
, instr
)) {
1230 fprintf(stderr
, "Bad comparison instruction\n");
1235 result
= ntq_emit_bcsel(c
, instr
, src
);
1239 result
= qir_MOV(c
, qir_SEL(c
, QPU_COND_ZC
, src
[1], src
[2]));
1243 result
= ntq_rcp(c
, src
[0]);
1246 result
= ntq_rsq(c
, src
[0]);
1249 result
= qir_EXP2(c
, src
[0]);
1252 result
= qir_LOG2(c
, src
[0]);
1256 result
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1259 result
= ntq_fceil(c
, src
[0]);
1262 result
= ntq_ffract(c
, src
[0]);
1265 result
= ntq_ffloor(c
, src
[0]);
1269 result
= ntq_fsin(c
, src
[0]);
1272 result
= ntq_fcos(c
, src
[0]);
1276 result
= ntq_fsign(c
, src
[0]);
1280 result
= qir_FMAXABS(c
, src
[0], src
[0]);
1283 result
= qir_MAX(c
, src
[0],
1284 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1287 case nir_op_ibitfield_extract
:
1288 result
= ntq_emit_ibfe(c
, src
[0], src
[1], src
[2]);
1291 case nir_op_ubitfield_extract
:
1292 result
= ntq_emit_ubfe(c
, src
[0], src
[1], src
[2]);
1295 case nir_op_usadd_4x8
:
1296 result
= qir_V8ADDS(c
, src
[0], src
[1]);
1299 case nir_op_ussub_4x8
:
1300 result
= qir_V8SUBS(c
, src
[0], src
[1]);
1303 case nir_op_umin_4x8
:
1304 result
= qir_V8MIN(c
, src
[0], src
[1]);
1307 case nir_op_umax_4x8
:
1308 result
= qir_V8MAX(c
, src
[0], src
[1]);
1311 case nir_op_umul_unorm_4x8
:
1312 result
= qir_V8MULD(c
, src
[0], src
[1]);
1316 case nir_op_fddx_coarse
:
1317 case nir_op_fddx_fine
:
1318 result
= ntq_fddx(c
, src
[0]);
1322 case nir_op_fddy_coarse
:
1323 case nir_op_fddy_fine
:
1324 result
= ntq_fddy(c
, src
[0]);
1328 fprintf(stderr
, "unknown NIR ALU inst: ");
1329 nir_print_instr(&instr
->instr
, stderr
);
1330 fprintf(stderr
, "\n");
1334 /* We have a scalar result, so the instruction should only have a
1335 * single channel written to.
1337 assert(util_is_power_of_two(instr
->dest
.write_mask
));
1338 ntq_store_dest(c
, &instr
->dest
.dest
,
1339 ffs(instr
->dest
.write_mask
) - 1, result
);
1343 emit_frag_end(struct vc4_compile
*c
)
1346 if (c
->output_color_index
!= -1) {
1347 color
= c
->outputs
[c
->output_color_index
];
1349 color
= qir_uniform_ui(c
, 0);
1352 uint32_t discard_cond
= QPU_COND_ALWAYS
;
1353 if (c
->s
->info
->fs
.uses_discard
) {
1354 qir_SF(c
, c
->discard
);
1355 discard_cond
= QPU_COND_ZS
;
1358 if (c
->fs_key
->stencil_enabled
) {
1359 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1360 qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1361 if (c
->fs_key
->stencil_twoside
) {
1362 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1363 qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1365 if (c
->fs_key
->stencil_full_writemasks
) {
1366 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1367 qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1371 if (c
->output_sample_mask_index
!= -1) {
1372 qir_MS_MASK(c
, c
->outputs
[c
->output_sample_mask_index
]);
1375 if (c
->fs_key
->depth_enabled
) {
1376 if (c
->output_position_index
!= -1) {
1377 qir_FTOI_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1379 c
->outputs
[c
->output_position_index
],
1380 qir_uniform_f(c
, 0xffffff)))->cond
= discard_cond
;
1382 qir_MOV_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1383 qir_FRAG_Z(c
))->cond
= discard_cond
;
1387 if (!c
->msaa_per_sample_output
) {
1388 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE
, 0),
1389 color
)->cond
= discard_cond
;
1391 for (int i
= 0; i
< VC4_MAX_SAMPLES
; i
++) {
1392 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE_MS
, 0),
1393 c
->sample_colors
[i
])->cond
= discard_cond
;
1399 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1401 struct qreg packed
= qir_get_temp(c
);
1403 for (int i
= 0; i
< 2; i
++) {
1405 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1407 struct qreg packed_chan
= packed
;
1408 packed_chan
.pack
= QPU_PACK_A_16A
+ i
;
1410 qir_FTOI_dest(c
, packed_chan
,
1413 c
->outputs
[c
->output_position_index
+ i
],
1418 qir_VPM_WRITE(c
, packed
);
1422 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1424 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1425 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1427 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1428 c
->outputs
[c
->output_position_index
+ 2],
1435 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1437 qir_VPM_WRITE(c
, rcp_w
);
1441 emit_point_size_write(struct vc4_compile
*c
)
1443 struct qreg point_size
;
1445 if (c
->output_point_size_index
!= -1)
1446 point_size
= c
->outputs
[c
->output_point_size_index
];
1448 point_size
= qir_uniform_f(c
, 1.0);
1450 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1453 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1455 qir_VPM_WRITE(c
, point_size
);
1459 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1461 * The simulator insists that there be at least one vertex attribute, so
1462 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1463 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1464 * to consume it here.
1467 emit_stub_vpm_read(struct vc4_compile
*c
)
1472 c
->next_vpm_input
++;
1473 c
->vattr_sizes
[0] = 4;
1474 (void)qir_MOV(c
, qir_reg(QFILE_VPM
, 0));
1479 emit_vert_end(struct vc4_compile
*c
,
1480 struct vc4_varying_slot
*fs_inputs
,
1481 uint32_t num_fs_inputs
)
1483 struct qreg rcp_w
= ntq_rcp(c
, c
->outputs
[c
->output_position_index
+ 3]);
1485 emit_stub_vpm_read(c
);
1487 emit_scaled_viewport_write(c
, rcp_w
);
1488 emit_zs_write(c
, rcp_w
);
1489 emit_rcp_wc_write(c
, rcp_w
);
1490 if (c
->vs_key
->per_vertex_point_size
)
1491 emit_point_size_write(c
);
1493 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1494 struct vc4_varying_slot
*input
= &fs_inputs
[i
];
1497 for (j
= 0; j
< c
->num_outputs
; j
++) {
1498 struct vc4_varying_slot
*output
=
1499 &c
->output_slots
[j
];
1501 if (input
->slot
== output
->slot
&&
1502 input
->swizzle
== output
->swizzle
) {
1503 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1507 /* Emit padding if we didn't find a declared VS output for
1510 if (j
== c
->num_outputs
)
1511 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1516 emit_coord_end(struct vc4_compile
*c
)
1518 struct qreg rcp_w
= ntq_rcp(c
, c
->outputs
[c
->output_position_index
+ 3]);
1520 emit_stub_vpm_read(c
);
1522 for (int i
= 0; i
< 4; i
++)
1523 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1525 emit_scaled_viewport_write(c
, rcp_w
);
1526 emit_zs_write(c
, rcp_w
);
1527 emit_rcp_wc_write(c
, rcp_w
);
1528 if (c
->vs_key
->per_vertex_point_size
)
1529 emit_point_size_write(c
);
1533 vc4_optimize_nir(struct nir_shader
*s
)
1540 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1541 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
);
1542 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
1543 NIR_PASS(progress
, s
, nir_copy_prop
);
1544 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1545 NIR_PASS(progress
, s
, nir_opt_dce
);
1546 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1547 NIR_PASS(progress
, s
, nir_opt_cse
);
1548 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8);
1549 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1550 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1551 NIR_PASS(progress
, s
, nir_opt_undef
);
1552 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
1554 nir_var_shader_out
|
1560 ntq_setup_outputs(struct vc4_compile
*c
)
1562 nir_foreach_variable(var
, &c
->s
->outputs
) {
1563 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1564 unsigned loc
= var
->data
.driver_location
* 4;
1566 assert(array_len
== 1);
1569 for (int i
= 0; i
< 4; i
++)
1570 add_output(c
, loc
+ i
, var
->data
.location
, i
);
1572 if (c
->stage
== QSTAGE_FRAG
) {
1573 switch (var
->data
.location
) {
1574 case FRAG_RESULT_COLOR
:
1575 case FRAG_RESULT_DATA0
:
1576 c
->output_color_index
= loc
;
1578 case FRAG_RESULT_DEPTH
:
1579 c
->output_position_index
= loc
;
1581 case FRAG_RESULT_SAMPLE_MASK
:
1582 c
->output_sample_mask_index
= loc
;
1586 switch (var
->data
.location
) {
1587 case VARYING_SLOT_POS
:
1588 c
->output_position_index
= loc
;
1590 case VARYING_SLOT_PSIZ
:
1591 c
->output_point_size_index
= loc
;
1599 ntq_setup_uniforms(struct vc4_compile
*c
)
1601 nir_foreach_variable(var
, &c
->s
->uniforms
) {
1602 uint32_t vec4_count
= st_glsl_type_size(var
->type
);
1603 unsigned vec4_size
= 4 * sizeof(float);
1605 declare_uniform_range(c
, var
->data
.driver_location
* vec4_size
,
1606 vec4_count
* vec4_size
);
1612 * Sets up the mapping from nir_register to struct qreg *.
1614 * Each nir_register gets a struct qreg per 32-bit component being stored.
1617 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1619 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1620 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1621 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1623 nir_reg
->num_components
);
1625 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1627 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1628 qregs
[i
] = qir_get_temp(c
);
1633 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1635 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1636 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1637 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u32
[i
]);
1639 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1643 ntq_emit_ssa_undef(struct vc4_compile
*c
, nir_ssa_undef_instr
*instr
)
1645 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1647 /* QIR needs there to be *some* value, so pick 0 (same as for
1648 * ntq_setup_registers().
1650 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1651 qregs
[i
] = qir_uniform_ui(c
, 0);
1655 ntq_emit_color_read(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1657 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
1658 assert(const_offset
->u32
[0] == 0);
1660 /* Reads of the per-sample color need to be done in
1663 int sample_index
= (nir_intrinsic_base(instr
) -
1664 VC4_NIR_TLB_COLOR_READ_INPUT
);
1665 for (int i
= 0; i
<= sample_index
; i
++) {
1666 if (c
->color_reads
[i
].file
== QFILE_NULL
) {
1668 qir_TLB_COLOR_READ(c
);
1671 ntq_store_dest(c
, &instr
->dest
, 0,
1672 qir_MOV(c
, c
->color_reads
[sample_index
]));
1676 ntq_emit_load_input(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1678 assert(instr
->num_components
== 1);
1680 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
1681 assert(const_offset
&& "vc4 doesn't support indirect inputs");
1683 if (c
->stage
== QSTAGE_FRAG
&&
1684 nir_intrinsic_base(instr
) >= VC4_NIR_TLB_COLOR_READ_INPUT
) {
1685 ntq_emit_color_read(c
, instr
);
1689 /* Size our inputs array as far as this input. Input arrays are
1690 * small, and we don't have a shader_info field that tells us up front
1691 * what the maximum driver_location is.
1693 uint32_t loc
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1694 if ((loc
+ 1) * 4 > c
->inputs_array_size
) {
1695 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1699 /* If we've already loaded this input, just return it. This would
1700 * happen for VPM loads, where we load an entire vertex attribute at
1701 * once, or possibly also in the FS if we haven't CSEed away repeated
1704 int comp
= nir_intrinsic_component(instr
);
1705 if (c
->inputs
[loc
* 4 + comp
].file
!= QFILE_NULL
) {
1706 ntq_store_dest(c
, &instr
->dest
, 0,
1707 qir_MOV(c
, c
->inputs
[loc
* 4 + comp
]));
1711 /* In the FS, we always have to fully drain our FS FIFO before
1712 * terminating the shader. For the VS we only have to drain whatever
1713 * VPM setup we configure, but vc4_qpu_emit.c configures it for the
1714 * entire vertex attribute space. Because of this, we emit our lazy
1715 * varying/VPM loads at the last top level basic block.
1717 struct qblock
*saved_cur_block
= c
->cur_block
;
1718 c
->cur_block
= c
->last_top_block
;
1720 /* Look up the NIR variable for this input, so we can see how big the
1721 * input is, or what sort of interpolation is necessary.
1723 nir_variable
*var
= NULL
;
1724 nir_foreach_variable(search_var
, &c
->s
->inputs
) {
1725 unsigned search_len
= MAX2(glsl_get_length(search_var
->type
), 1);
1726 unsigned search_loc
= search_var
->data
.driver_location
;
1728 if (loc
>= search_loc
&& loc
< search_loc
+ search_len
) {
1735 if (c
->stage
== QSTAGE_FRAG
) {
1736 if (var
->data
.location
== VARYING_SLOT_POS
) {
1737 emit_fragcoord_input(c
, loc
);
1738 } else if (var
->data
.location
== VARYING_SLOT_PNTC
||
1739 (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1740 (c
->fs_key
->point_sprite_mask
&
1741 (1 << (var
->data
.location
-
1742 VARYING_SLOT_VAR0
))))) {
1743 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1744 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1746 emit_fragment_input(c
, loc
, var
->data
.location
);
1749 emit_vertex_input(c
, loc
);
1752 c
->cur_block
= saved_cur_block
;
1754 ntq_store_dest(c
, &instr
->dest
, 0,
1755 qir_MOV(c
, c
->inputs
[loc
* 4 + comp
]));
1759 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1761 nir_const_value
*const_offset
;
1764 switch (instr
->intrinsic
) {
1765 case nir_intrinsic_load_uniform
:
1766 assert(instr
->num_components
== 1);
1767 const_offset
= nir_src_as_const_value(instr
->src
[0]);
1769 offset
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1770 assert(offset
% 4 == 0);
1771 /* We need dwords */
1772 offset
= offset
/ 4;
1773 ntq_store_dest(c
, &instr
->dest
, 0,
1774 qir_uniform(c
, QUNIFORM_UNIFORM
,
1777 ntq_store_dest(c
, &instr
->dest
, 0,
1778 indirect_uniform_load(c
, instr
));
1782 case nir_intrinsic_load_user_clip_plane
:
1783 for (int i
= 0; i
< instr
->num_components
; i
++) {
1784 ntq_store_dest(c
, &instr
->dest
, i
,
1785 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1786 nir_intrinsic_ucp_id(instr
) *
1791 case nir_intrinsic_load_blend_const_color_r_float
:
1792 case nir_intrinsic_load_blend_const_color_g_float
:
1793 case nir_intrinsic_load_blend_const_color_b_float
:
1794 case nir_intrinsic_load_blend_const_color_a_float
:
1795 ntq_store_dest(c
, &instr
->dest
, 0,
1796 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_X
+
1798 nir_intrinsic_load_blend_const_color_r_float
),
1802 case nir_intrinsic_load_blend_const_color_rgba8888_unorm
:
1803 ntq_store_dest(c
, &instr
->dest
, 0,
1804 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_RGBA
,
1808 case nir_intrinsic_load_blend_const_color_aaaa8888_unorm
:
1809 ntq_store_dest(c
, &instr
->dest
, 0,
1810 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_AAAA
,
1814 case nir_intrinsic_load_alpha_ref_float
:
1815 ntq_store_dest(c
, &instr
->dest
, 0,
1816 qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1819 case nir_intrinsic_load_sample_mask_in
:
1820 ntq_store_dest(c
, &instr
->dest
, 0,
1821 qir_uniform(c
, QUNIFORM_SAMPLE_MASK
, 0));
1824 case nir_intrinsic_load_front_face
:
1825 /* The register contains 0 (front) or 1 (back), and we need to
1826 * turn it into a NIR bool where true means front.
1828 ntq_store_dest(c
, &instr
->dest
, 0,
1830 qir_uniform_ui(c
, -1),
1831 qir_reg(QFILE_FRAG_REV_FLAG
, 0)));
1834 case nir_intrinsic_load_input
:
1835 ntq_emit_load_input(c
, instr
);
1838 case nir_intrinsic_store_output
:
1839 const_offset
= nir_src_as_const_value(instr
->src
[1]);
1840 assert(const_offset
&& "vc4 doesn't support indirect outputs");
1841 offset
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1843 /* MSAA color outputs are the only case where we have an
1844 * output that's not lowered to being a store of a single 32
1847 if (c
->stage
== QSTAGE_FRAG
&& instr
->num_components
== 4) {
1848 assert(offset
== c
->output_color_index
);
1849 for (int i
= 0; i
< 4; i
++) {
1850 c
->sample_colors
[i
] =
1851 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0],
1855 offset
= offset
* 4 + nir_intrinsic_component(instr
);
1856 assert(instr
->num_components
== 1);
1857 c
->outputs
[offset
] =
1858 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1859 c
->num_outputs
= MAX2(c
->num_outputs
, offset
+ 1);
1863 case nir_intrinsic_discard
:
1864 if (c
->execute
.file
!= QFILE_NULL
) {
1865 qir_SF(c
, c
->execute
);
1866 qir_MOV_cond(c
, QPU_COND_ZS
, c
->discard
,
1867 qir_uniform_ui(c
, ~0));
1869 qir_MOV_dest(c
, c
->discard
, qir_uniform_ui(c
, ~0));
1873 case nir_intrinsic_discard_if
: {
1874 /* true (~0) if we're discarding */
1875 struct qreg cond
= ntq_get_src(c
, instr
->src
[0], 0);
1877 if (c
->execute
.file
!= QFILE_NULL
) {
1878 /* execute == 0 means the channel is active. Invert
1879 * the condition so that we can use zero as "executing
1882 qir_SF(c
, qir_AND(c
, c
->execute
, qir_NOT(c
, cond
)));
1883 qir_MOV_cond(c
, QPU_COND_ZS
, c
->discard
, cond
);
1885 qir_OR_dest(c
, c
->discard
, c
->discard
,
1886 ntq_get_src(c
, instr
->src
[0], 0));
1893 fprintf(stderr
, "Unknown intrinsic: ");
1894 nir_print_instr(&instr
->instr
, stderr
);
1895 fprintf(stderr
, "\n");
1900 /* Clears (activates) the execute flags for any channels whose jump target
1901 * matches this block.
1904 ntq_activate_execute_for_block(struct vc4_compile
*c
)
1906 qir_SF(c
, qir_SUB(c
,
1908 qir_uniform_ui(c
, c
->cur_block
->index
)));
1909 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
, qir_uniform_ui(c
, 0));
1913 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1915 if (!c
->vc4
->screen
->has_control_flow
) {
1917 "IF statement support requires updated kernel.\n");
1921 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
1922 bool empty_else_block
=
1923 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
1924 exec_list_is_empty(&nir_else_block
->instr_list
));
1926 struct qblock
*then_block
= qir_new_block(c
);
1927 struct qblock
*after_block
= qir_new_block(c
);
1928 struct qblock
*else_block
;
1929 if (empty_else_block
)
1930 else_block
= after_block
;
1932 else_block
= qir_new_block(c
);
1934 bool was_top_level
= false;
1935 if (c
->execute
.file
== QFILE_NULL
) {
1936 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
1937 was_top_level
= true;
1940 /* Set ZS for executing (execute == 0) and jumping (if->condition ==
1941 * 0) channels, and then update execute flags for those to point to
1946 ntq_get_src(c
, if_stmt
->condition
, 0)));
1947 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1948 qir_uniform_ui(c
, else_block
->index
));
1950 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1953 qir_SF(c
, c
->execute
);
1954 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZC
);
1955 qir_link_blocks(c
->cur_block
, else_block
);
1956 qir_link_blocks(c
->cur_block
, then_block
);
1958 /* Process the THEN block. */
1959 qir_set_emit_block(c
, then_block
);
1960 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
1962 if (!empty_else_block
) {
1963 /* Handle the end of the THEN block. First, all currently
1964 * active channels update their execute flags to point to
1967 qir_SF(c
, c
->execute
);
1968 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1969 qir_uniform_ui(c
, after_block
->index
));
1971 /* If everything points at ENDIF, then jump there immediately. */
1972 qir_SF(c
, qir_SUB(c
, c
->execute
, qir_uniform_ui(c
, after_block
->index
)));
1973 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZS
);
1974 qir_link_blocks(c
->cur_block
, after_block
);
1975 qir_link_blocks(c
->cur_block
, else_block
);
1977 qir_set_emit_block(c
, else_block
);
1978 ntq_activate_execute_for_block(c
);
1979 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
1982 qir_link_blocks(c
->cur_block
, after_block
);
1984 qir_set_emit_block(c
, after_block
);
1985 if (was_top_level
) {
1986 c
->execute
= c
->undef
;
1987 c
->last_top_block
= c
->cur_block
;
1989 ntq_activate_execute_for_block(c
);
1994 ntq_emit_jump(struct vc4_compile
*c
, nir_jump_instr
*jump
)
1996 struct qblock
*jump_block
;
1997 switch (jump
->type
) {
1998 case nir_jump_break
:
1999 jump_block
= c
->loop_break_block
;
2001 case nir_jump_continue
:
2002 jump_block
= c
->loop_cont_block
;
2005 unreachable("Unsupported jump type\n");
2008 qir_SF(c
, c
->execute
);
2009 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
2010 qir_uniform_ui(c
, jump_block
->index
));
2012 /* Jump to the destination block if everyone has taken the jump. */
2013 qir_SF(c
, qir_SUB(c
, c
->execute
, qir_uniform_ui(c
, jump_block
->index
)));
2014 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZS
);
2015 struct qblock
*new_block
= qir_new_block(c
);
2016 qir_link_blocks(c
->cur_block
, jump_block
);
2017 qir_link_blocks(c
->cur_block
, new_block
);
2018 qir_set_emit_block(c
, new_block
);
2022 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
2024 switch (instr
->type
) {
2025 case nir_instr_type_alu
:
2026 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
2029 case nir_instr_type_intrinsic
:
2030 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
2033 case nir_instr_type_load_const
:
2034 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
2037 case nir_instr_type_ssa_undef
:
2038 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
2041 case nir_instr_type_tex
:
2042 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
2045 case nir_instr_type_jump
:
2046 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
2050 fprintf(stderr
, "Unknown NIR instr type: ");
2051 nir_print_instr(instr
, stderr
);
2052 fprintf(stderr
, "\n");
2058 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
2060 nir_foreach_instr(instr
, block
) {
2061 ntq_emit_instr(c
, instr
);
2065 static void ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
2068 ntq_emit_loop(struct vc4_compile
*c
, nir_loop
*loop
)
2070 if (!c
->vc4
->screen
->has_control_flow
) {
2072 "loop support requires updated kernel.\n");
2073 ntq_emit_cf_list(c
, &loop
->body
);
2077 bool was_top_level
= false;
2078 if (c
->execute
.file
== QFILE_NULL
) {
2079 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
2080 was_top_level
= true;
2083 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
2084 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
2086 c
->loop_cont_block
= qir_new_block(c
);
2087 c
->loop_break_block
= qir_new_block(c
);
2089 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2090 qir_set_emit_block(c
, c
->loop_cont_block
);
2091 ntq_activate_execute_for_block(c
);
2093 ntq_emit_cf_list(c
, &loop
->body
);
2095 /* If anything had explicitly continued, or is here at the end of the
2096 * loop, then we need to loop again. SF updates are masked by the
2097 * instruction's condition, so we can do the OR of the two conditions
2100 qir_SF(c
, c
->execute
);
2101 struct qinst
*cont_check
=
2105 qir_uniform_ui(c
, c
->loop_cont_block
->index
));
2106 cont_check
->cond
= QPU_COND_ZC
;
2107 cont_check
->sf
= true;
2109 qir_BRANCH(c
, QPU_COND_BRANCH_ANY_ZS
);
2110 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
2111 qir_link_blocks(c
->cur_block
, c
->loop_break_block
);
2113 qir_set_emit_block(c
, c
->loop_break_block
);
2114 if (was_top_level
) {
2115 c
->execute
= c
->undef
;
2116 c
->last_top_block
= c
->cur_block
;
2118 ntq_activate_execute_for_block(c
);
2121 c
->loop_break_block
= save_loop_break_block
;
2122 c
->loop_cont_block
= save_loop_cont_block
;
2126 ntq_emit_function(struct vc4_compile
*c
, nir_function_impl
*func
)
2128 fprintf(stderr
, "FUNCTIONS not handled.\n");
2133 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
2135 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2136 switch (node
->type
) {
2137 case nir_cf_node_block
:
2138 ntq_emit_block(c
, nir_cf_node_as_block(node
));
2141 case nir_cf_node_if
:
2142 ntq_emit_if(c
, nir_cf_node_as_if(node
));
2145 case nir_cf_node_loop
:
2146 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
2149 case nir_cf_node_function
:
2150 ntq_emit_function(c
, nir_cf_node_as_function(node
));
2154 fprintf(stderr
, "Unknown NIR node type\n");
2161 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
2163 ntq_setup_registers(c
, &impl
->registers
);
2164 ntq_emit_cf_list(c
, &impl
->body
);
2168 nir_to_qir(struct vc4_compile
*c
)
2170 if (c
->stage
== QSTAGE_FRAG
&& c
->s
->info
->fs
.uses_discard
)
2171 c
->discard
= qir_MOV(c
, qir_uniform_ui(c
, 0));
2173 ntq_setup_outputs(c
);
2174 ntq_setup_uniforms(c
);
2175 ntq_setup_registers(c
, &c
->s
->registers
);
2177 /* Find the main function and emit the body. */
2178 nir_foreach_function(function
, c
->s
) {
2179 assert(strcmp(function
->name
, "main") == 0);
2180 assert(function
->impl
);
2181 ntq_emit_impl(c
, function
->impl
);
2185 static const nir_shader_compiler_options nir_options
= {
2186 .lower_extract_byte
= true,
2187 .lower_extract_word
= true,
2189 .lower_flrp32
= true,
2192 .lower_fsqrt
= true,
2193 .lower_negate
= true,
2194 .native_integers
= true,
2195 .max_unroll_iterations
= 32,
2199 vc4_screen_get_compiler_options(struct pipe_screen
*pscreen
,
2200 enum pipe_shader_ir ir
,
2201 enum pipe_shader_type shader
)
2203 return &nir_options
;
2207 count_nir_instrs(nir_shader
*nir
)
2210 nir_foreach_function(function
, nir
) {
2211 if (!function
->impl
)
2213 nir_foreach_block(block
, function
->impl
) {
2214 nir_foreach_instr(instr
, block
)
2221 static struct vc4_compile
*
2222 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
2223 struct vc4_key
*key
, bool fs_threaded
)
2225 struct vc4_compile
*c
= qir_compile_init();
2229 c
->shader_state
= &key
->shader_state
->base
;
2230 c
->program_id
= key
->shader_state
->program_id
;
2232 p_atomic_inc_return(&key
->shader_state
->compiled_variant_count
);
2233 c
->fs_threaded
= fs_threaded
;
2238 c
->fs_key
= (struct vc4_fs_key
*)key
;
2239 if (c
->fs_key
->is_points
) {
2240 c
->point_x
= emit_fragment_varying(c
, ~0, 0);
2241 c
->point_y
= emit_fragment_varying(c
, ~0, 0);
2242 } else if (c
->fs_key
->is_lines
) {
2243 c
->line_x
= emit_fragment_varying(c
, ~0, 0);
2247 c
->vs_key
= (struct vc4_vs_key
*)key
;
2250 c
->vs_key
= (struct vc4_vs_key
*)key
;
2254 c
->s
= nir_shader_clone(c
, key
->shader_state
->base
.ir
.nir
);
2256 if (stage
== QSTAGE_FRAG
)
2257 NIR_PASS_V(c
->s
, vc4_nir_lower_blend
, c
);
2259 struct nir_lower_tex_options tex_options
= {
2260 /* We would need to implement txs, but we don't want the
2261 * int/float conversions
2263 .lower_rect
= false,
2267 /* Apply swizzles to all samplers. */
2268 .swizzle_result
= ~0,
2271 /* Lower the format swizzle and ARB_texture_swizzle-style swizzle.
2272 * The format swizzling applies before sRGB decode, and
2273 * ARB_texture_swizzle is the last thing before returning the sample.
2275 for (int i
= 0; i
< ARRAY_SIZE(key
->tex
); i
++) {
2276 enum pipe_format format
= c
->key
->tex
[i
].format
;
2281 const uint8_t *format_swizzle
= vc4_get_format_swizzle(format
);
2283 for (int j
= 0; j
< 4; j
++) {
2284 uint8_t arb_swiz
= c
->key
->tex
[i
].swizzle
[j
];
2286 if (arb_swiz
<= 3) {
2287 tex_options
.swizzles
[i
][j
] =
2288 format_swizzle
[arb_swiz
];
2290 tex_options
.swizzles
[i
][j
] = arb_swiz
;
2294 if (util_format_is_srgb(format
))
2295 tex_options
.lower_srgb
|= (1 << i
);
2298 NIR_PASS_V(c
->s
, nir_lower_tex
, &tex_options
);
2300 if (c
->fs_key
&& c
->fs_key
->light_twoside
)
2301 NIR_PASS_V(c
->s
, nir_lower_two_sided_color
);
2303 if (c
->vs_key
&& c
->vs_key
->clamp_color
)
2304 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
2306 if (c
->key
->ucp_enables
) {
2307 if (stage
== QSTAGE_FRAG
) {
2308 NIR_PASS_V(c
->s
, nir_lower_clip_fs
, c
->key
->ucp_enables
);
2310 NIR_PASS_V(c
->s
, nir_lower_clip_vs
, c
->key
->ucp_enables
);
2311 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
,
2312 nir_var_shader_out
);
2316 /* FS input scalarizing must happen after nir_lower_two_sided_color,
2317 * which only handles a vec4 at a time. Similarly, VS output
2318 * scalarizing must happen after nir_lower_clip_vs.
2320 if (c
->stage
== QSTAGE_FRAG
)
2321 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_in
);
2323 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_out
);
2325 NIR_PASS_V(c
->s
, vc4_nir_lower_io
, c
);
2326 NIR_PASS_V(c
->s
, vc4_nir_lower_txf_ms
, c
);
2327 NIR_PASS_V(c
->s
, nir_lower_idiv
);
2329 vc4_optimize_nir(c
->s
);
2331 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
2333 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2334 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2335 qir_get_stage_name(c
->stage
),
2336 c
->program_id
, c
->variant_id
,
2337 count_nir_instrs(c
->s
));
2340 if (vc4_debug
& VC4_DEBUG_NIR
) {
2341 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2342 qir_get_stage_name(c
->stage
),
2343 c
->program_id
, c
->variant_id
);
2344 nir_print_shader(c
->s
, stderr
);
2351 /* FS threading requires that the thread execute
2352 * QPU_SIG_LAST_THREAD_SWITCH exactly once before terminating
2353 * (with no other THRSW afterwards, obviously). If we didn't
2354 * fetch a texture at a top level block, this wouldn't be
2357 if (c
->fs_threaded
&& !c
->last_thrsw_at_top_level
) {
2366 c
->vs_key
->fs_inputs
->input_slots
,
2367 c
->vs_key
->fs_inputs
->num_inputs
);
2374 if (vc4_debug
& VC4_DEBUG_QIR
) {
2375 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2376 qir_get_stage_name(c
->stage
),
2377 c
->program_id
, c
->variant_id
);
2379 fprintf(stderr
, "\n");
2383 qir_lower_uniforms(c
);
2385 qir_schedule_instructions(c
);
2386 qir_emit_uniform_stream_resets(c
);
2388 if (vc4_debug
& VC4_DEBUG_QIR
) {
2389 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2390 qir_get_stage_name(c
->stage
),
2391 c
->program_id
, c
->variant_id
);
2393 fprintf(stderr
, "\n");
2396 qir_reorder_uniforms(c
);
2397 vc4_generate_code(vc4
, c
);
2399 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2400 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2401 qir_get_stage_name(c
->stage
),
2402 c
->program_id
, c
->variant_id
,
2404 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2405 qir_get_stage_name(c
->stage
),
2406 c
->program_id
, c
->variant_id
,
2416 vc4_shader_state_create(struct pipe_context
*pctx
,
2417 const struct pipe_shader_state
*cso
)
2419 struct vc4_context
*vc4
= vc4_context(pctx
);
2420 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2424 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2428 if (cso
->type
== PIPE_SHADER_IR_NIR
) {
2429 /* The backend takes ownership of the NIR shader on state
2434 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
2436 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2437 fprintf(stderr
, "prog %d TGSI:\n",
2439 tgsi_dump(cso
->tokens
, 0);
2440 fprintf(stderr
, "\n");
2442 s
= tgsi_to_nir(cso
->tokens
, &nir_options
);
2445 NIR_PASS_V(s
, nir_opt_global_to_local
);
2446 NIR_PASS_V(s
, nir_lower_regs_to_ssa
);
2447 NIR_PASS_V(s
, nir_normalize_cubemap_coords
);
2449 NIR_PASS_V(s
, nir_lower_load_const_to_scalar
);
2451 vc4_optimize_nir(s
);
2453 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_local
);
2455 /* Garbage collect dead instructions */
2458 so
->base
.type
= PIPE_SHADER_IR_NIR
;
2459 so
->base
.ir
.nir
= s
;
2461 if (vc4_debug
& VC4_DEBUG_NIR
) {
2462 fprintf(stderr
, "%s prog %d NIR:\n",
2463 gl_shader_stage_name(s
->stage
),
2465 nir_print_shader(s
, stderr
);
2466 fprintf(stderr
, "\n");
2473 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2474 struct vc4_compile
*c
)
2476 int count
= c
->num_uniforms
;
2477 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2479 uinfo
->count
= count
;
2480 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2481 memcpy(uinfo
->data
, c
->uniform_data
,
2482 count
* sizeof(*uinfo
->data
));
2483 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2484 memcpy(uinfo
->contents
, c
->uniform_contents
,
2485 count
* sizeof(*uinfo
->contents
));
2486 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2488 vc4_set_shader_uniform_dirty_flags(shader
);
2492 vc4_setup_compiled_fs_inputs(struct vc4_context
*vc4
, struct vc4_compile
*c
,
2493 struct vc4_compiled_shader
*shader
)
2495 struct vc4_fs_inputs inputs
;
2497 memset(&inputs
, 0, sizeof(inputs
));
2498 inputs
.input_slots
= ralloc_array(shader
,
2499 struct vc4_varying_slot
,
2500 c
->num_input_slots
);
2502 bool input_live
[c
->num_input_slots
];
2504 memset(input_live
, 0, sizeof(input_live
));
2505 qir_for_each_inst_inorder(inst
, c
) {
2506 for (int i
= 0; i
< qir_get_nsrc(inst
); i
++) {
2507 if (inst
->src
[i
].file
== QFILE_VARY
)
2508 input_live
[inst
->src
[i
].index
] = true;
2512 for (int i
= 0; i
< c
->num_input_slots
; i
++) {
2513 struct vc4_varying_slot
*slot
= &c
->input_slots
[i
];
2518 /* Skip non-VS-output inputs. */
2519 if (slot
->slot
== (uint8_t)~0)
2522 if (slot
->slot
== VARYING_SLOT_COL0
||
2523 slot
->slot
== VARYING_SLOT_COL1
||
2524 slot
->slot
== VARYING_SLOT_BFC0
||
2525 slot
->slot
== VARYING_SLOT_BFC1
) {
2526 shader
->color_inputs
|= (1 << inputs
.num_inputs
);
2529 inputs
.input_slots
[inputs
.num_inputs
] = *slot
;
2530 inputs
.num_inputs
++;
2532 shader
->num_inputs
= inputs
.num_inputs
;
2534 /* Add our set of inputs to the set of all inputs seen. This way, we
2535 * can have a single pointer that identifies an FS inputs set,
2536 * allowing VS to avoid recompiling when the FS is recompiled (or a
2537 * new one is bound using separate shader objects) but the inputs
2540 struct set_entry
*entry
= _mesa_set_search(vc4
->fs_inputs_set
, &inputs
);
2542 shader
->fs_inputs
= entry
->key
;
2543 ralloc_free(inputs
.input_slots
);
2545 struct vc4_fs_inputs
*alloc_inputs
;
2547 alloc_inputs
= rzalloc(vc4
->fs_inputs_set
, struct vc4_fs_inputs
);
2548 memcpy(alloc_inputs
, &inputs
, sizeof(inputs
));
2549 ralloc_steal(alloc_inputs
, inputs
.input_slots
);
2550 _mesa_set_add(vc4
->fs_inputs_set
, alloc_inputs
);
2552 shader
->fs_inputs
= alloc_inputs
;
2556 static struct vc4_compiled_shader
*
2557 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2558 struct vc4_key
*key
)
2560 struct hash_table
*ht
;
2564 if (stage
== QSTAGE_FRAG
) {
2566 key_size
= sizeof(struct vc4_fs_key
);
2567 try_threading
= vc4
->screen
->has_threaded_fs
;
2570 key_size
= sizeof(struct vc4_vs_key
);
2571 try_threading
= false;
2574 struct vc4_compiled_shader
*shader
;
2575 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2579 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
, try_threading
);
2580 /* If the FS failed to compile threaded, fall back to single threaded. */
2581 if (try_threading
&& c
->failed
) {
2582 qir_compile_destroy(c
);
2583 c
= vc4_shader_ntq(vc4
, stage
, key
, false);
2586 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2588 shader
->program_id
= vc4
->next_compiled_program_id
++;
2589 if (stage
== QSTAGE_FRAG
) {
2590 vc4_setup_compiled_fs_inputs(vc4
, c
, shader
);
2592 /* Note: the temporary clone in c->s has been freed. */
2593 nir_shader
*orig_shader
= key
->shader_state
->base
.ir
.nir
;
2594 if (orig_shader
->info
->outputs_written
& (1 << FRAG_RESULT_DEPTH
))
2595 shader
->disable_early_z
= true;
2597 shader
->num_inputs
= c
->num_inputs
;
2599 uint8_t next_vattr_offset
= 0;
2600 for (int i
= 0; i
< c
->next_vpm_input
; i
++) {
2601 if (!c
->vattr_sizes
[i
])
2604 uint32_t nir_attr
= c
->vpm_input_order
[i
];
2605 shader
->vattr_offsets
[nir_attr
] = next_vattr_offset
;
2606 next_vattr_offset
+= c
->vattr_sizes
[i
];
2607 shader
->vattrs_live
|= (1 << nir_attr
);
2609 shader
->vattr_total_size
= next_vattr_offset
;
2612 shader
->failed
= c
->failed
;
2614 shader
->failed
= true;
2616 copy_uniform_state_to_shader(shader
, c
);
2617 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2622 shader
->fs_threaded
= c
->fs_threaded
;
2624 /* Copy the compiler UBO range state to the compiled shader, dropping
2625 * out arrays that were never referenced by an indirect load.
2627 * (Note that QIR dead code elimination of an array access still
2628 * leaves that array alive, though)
2630 if (c
->num_ubo_ranges
) {
2631 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2632 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2635 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2636 struct vc4_compiler_ubo_range
*range
=
2641 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2642 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2643 shader
->ubo_ranges
[j
].size
= range
->size
;
2644 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2648 if (shader
->ubo_size
) {
2649 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2650 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2651 qir_get_stage_name(c
->stage
),
2652 c
->program_id
, c
->variant_id
,
2653 shader
->ubo_size
/ 4);
2657 qir_compile_destroy(c
);
2659 struct vc4_key
*dup_key
;
2660 dup_key
= rzalloc_size(shader
, key_size
); /* TODO: don't use rzalloc */
2661 memcpy(dup_key
, key
, key_size
);
2662 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2668 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2669 struct vc4_texture_stateobj
*texstate
)
2671 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2672 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2673 struct vc4_sampler_view
*vc4_sampler
= vc4_sampler_view(sampler
);
2674 struct pipe_sampler_state
*sampler_state
=
2675 texstate
->samplers
[i
];
2680 key
->tex
[i
].format
= sampler
->format
;
2681 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2682 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2683 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2684 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2686 if (sampler
->texture
->nr_samples
> 1) {
2687 key
->tex
[i
].msaa_width
= sampler
->texture
->width0
;
2688 key
->tex
[i
].msaa_height
= sampler
->texture
->height0
;
2689 } else if (sampler
){
2690 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2691 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2692 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2693 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2694 key
->tex
[i
].force_first_level
=
2695 vc4_sampler
->force_first_level
;
2699 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2703 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2705 struct vc4_job
*job
= vc4
->job
;
2706 struct vc4_fs_key local_key
;
2707 struct vc4_fs_key
*key
= &local_key
;
2709 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2711 VC4_DIRTY_FRAMEBUFFER
|
2713 VC4_DIRTY_RASTERIZER
|
2714 VC4_DIRTY_SAMPLE_MASK
|
2716 VC4_DIRTY_UNCOMPILED_FS
))) {
2720 memset(key
, 0, sizeof(*key
));
2721 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2722 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2723 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2724 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2725 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2726 key
->blend
= vc4
->blend
->rt
[0];
2727 if (vc4
->blend
->logicop_enable
) {
2728 key
->logicop_func
= vc4
->blend
->logicop_func
;
2730 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2733 key
->msaa
= vc4
->rasterizer
->base
.multisample
;
2734 key
->sample_coverage
= (vc4
->sample_mask
!= (1 << VC4_MAX_SAMPLES
) - 1);
2735 key
->sample_alpha_to_coverage
= vc4
->blend
->alpha_to_coverage
;
2736 key
->sample_alpha_to_one
= vc4
->blend
->alpha_to_one
;
2739 if (vc4
->framebuffer
.cbufs
[0])
2740 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2742 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2743 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2744 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2745 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2746 key
->stencil_enabled
);
2747 if (vc4
->zsa
->base
.alpha
.enabled
) {
2748 key
->alpha_test
= true;
2749 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2752 if (key
->is_points
) {
2753 key
->point_sprite_mask
=
2754 vc4
->rasterizer
->base
.sprite_coord_enable
;
2755 key
->point_coord_upper_left
=
2756 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2757 PIPE_SPRITE_COORD_UPPER_LEFT
);
2760 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2762 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2763 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2764 if (vc4
->prog
.fs
== old_fs
)
2767 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2769 if (vc4
->rasterizer
->base
.flatshade
&&
2770 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2771 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2774 if (old_fs
&& vc4
->prog
.fs
->fs_inputs
!= old_fs
->fs_inputs
)
2775 vc4
->dirty
|= VC4_DIRTY_FS_INPUTS
;
2779 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2781 struct vc4_vs_key local_key
;
2782 struct vc4_vs_key
*key
= &local_key
;
2784 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2785 VC4_DIRTY_RASTERIZER
|
2787 VC4_DIRTY_VTXSTATE
|
2788 VC4_DIRTY_UNCOMPILED_VS
|
2789 VC4_DIRTY_FS_INPUTS
))) {
2793 memset(key
, 0, sizeof(*key
));
2794 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2795 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2796 key
->fs_inputs
= vc4
->prog
.fs
->fs_inputs
;
2797 key
->clamp_color
= vc4
->rasterizer
->base
.clamp_vertex_color
;
2799 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2800 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2802 key
->per_vertex_point_size
=
2803 (prim_mode
== PIPE_PRIM_POINTS
&&
2804 vc4
->rasterizer
->base
.point_size_per_vertex
);
2806 struct vc4_compiled_shader
*vs
=
2807 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2808 if (vs
!= vc4
->prog
.vs
) {
2810 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2813 key
->is_coord
= true;
2814 /* Coord shaders don't care what the FS inputs are. */
2815 key
->fs_inputs
= NULL
;
2816 struct vc4_compiled_shader
*cs
=
2817 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2818 if (cs
!= vc4
->prog
.cs
) {
2820 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2825 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2827 vc4_update_compiled_fs(vc4
, prim_mode
);
2828 vc4_update_compiled_vs(vc4
, prim_mode
);
2830 return !(vc4
->prog
.cs
->failed
||
2831 vc4
->prog
.vs
->failed
||
2832 vc4
->prog
.fs
->failed
);
2836 fs_cache_hash(const void *key
)
2838 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2842 vs_cache_hash(const void *key
)
2844 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2848 fs_cache_compare(const void *key1
, const void *key2
)
2850 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2854 vs_cache_compare(const void *key1
, const void *key2
)
2856 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2860 fs_inputs_hash(const void *key
)
2862 const struct vc4_fs_inputs
*inputs
= key
;
2864 return _mesa_hash_data(inputs
->input_slots
,
2865 sizeof(*inputs
->input_slots
) *
2866 inputs
->num_inputs
);
2870 fs_inputs_compare(const void *key1
, const void *key2
)
2872 const struct vc4_fs_inputs
*inputs1
= key1
;
2873 const struct vc4_fs_inputs
*inputs2
= key2
;
2875 return (inputs1
->num_inputs
== inputs2
->num_inputs
&&
2876 memcmp(inputs1
->input_slots
,
2877 inputs2
->input_slots
,
2878 sizeof(*inputs1
->input_slots
) *
2879 inputs1
->num_inputs
) == 0);
2883 delete_from_cache_if_matches(struct hash_table
*ht
,
2884 struct hash_entry
*entry
,
2885 struct vc4_uncompiled_shader
*so
)
2887 const struct vc4_key
*key
= entry
->key
;
2889 if (key
->shader_state
== so
) {
2890 struct vc4_compiled_shader
*shader
= entry
->data
;
2891 _mesa_hash_table_remove(ht
, entry
);
2892 vc4_bo_unreference(&shader
->bo
);
2893 ralloc_free(shader
);
2898 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2900 struct vc4_context
*vc4
= vc4_context(pctx
);
2901 struct vc4_uncompiled_shader
*so
= hwcso
;
2903 struct hash_entry
*entry
;
2904 hash_table_foreach(vc4
->fs_cache
, entry
)
2905 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2906 hash_table_foreach(vc4
->vs_cache
, entry
)
2907 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2909 ralloc_free(so
->base
.ir
.nir
);
2914 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2916 struct vc4_context
*vc4
= vc4_context(pctx
);
2917 vc4
->prog
.bind_fs
= hwcso
;
2918 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2922 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2924 struct vc4_context
*vc4
= vc4_context(pctx
);
2925 vc4
->prog
.bind_vs
= hwcso
;
2926 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2930 vc4_program_init(struct pipe_context
*pctx
)
2932 struct vc4_context
*vc4
= vc4_context(pctx
);
2934 pctx
->create_vs_state
= vc4_shader_state_create
;
2935 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2937 pctx
->create_fs_state
= vc4_shader_state_create
;
2938 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2940 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2941 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2943 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2945 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2947 vc4
->fs_inputs_set
= _mesa_set_create(pctx
, fs_inputs_hash
,
2952 vc4_program_fini(struct pipe_context
*pctx
)
2954 struct vc4_context
*vc4
= vc4_context(pctx
);
2956 struct hash_entry
*entry
;
2957 hash_table_foreach(vc4
->fs_cache
, entry
) {
2958 struct vc4_compiled_shader
*shader
= entry
->data
;
2959 vc4_bo_unreference(&shader
->bo
);
2960 ralloc_free(shader
);
2961 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2964 hash_table_foreach(vc4
->vs_cache
, entry
) {
2965 struct vc4_compiled_shader
*shader
= entry
->data
;
2966 vc4_bo_unreference(&shader
->bo
);
2967 ralloc_free(shader
);
2968 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);