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/u_hash.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"
41 #ifdef USE_VC4_SIMULATOR
42 #include "simpenrose/simpenrose.h"
46 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
48 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
51 resize_qreg_array(struct vc4_compile
*c
,
56 if (*size
>= decl_size
)
59 uint32_t old_size
= *size
;
60 *size
= MAX2(*size
* 2, decl_size
);
61 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
63 fprintf(stderr
, "Malloc failure\n");
67 for (uint32_t i
= old_size
; i
< *size
; i
++)
68 (*regs
)[i
] = c
->undef
;
72 indirect_uniform_load(struct vc4_compile
*c
, nir_intrinsic_instr
*intr
)
74 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
75 uint32_t offset
= nir_intrinsic_base(intr
);
76 struct vc4_compiler_ubo_range
*range
= NULL
;
78 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
79 range
= &c
->ubo_ranges
[i
];
80 if (offset
>= range
->src_offset
&&
81 offset
< range
->src_offset
+ range
->size
) {
85 /* The driver-location-based offset always has to be within a declared
91 range
->dst_offset
= c
->next_ubo_dst_offset
;
92 c
->next_ubo_dst_offset
+= range
->size
;
96 offset
-= range
->src_offset
;
98 /* Adjust for where we stored the TGSI register base. */
99 indirect_offset
= qir_ADD(c
, indirect_offset
,
100 qir_uniform_ui(c
, (range
->dst_offset
+
103 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
104 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
105 indirect_offset
= qir_MIN(c
, indirect_offset
,
106 qir_uniform_ui(c
, (range
->dst_offset
+
109 qir_TEX_DIRECT(c
, indirect_offset
, qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
110 c
->num_texture_samples
++;
111 return qir_TEX_RESULT(c
);
115 vc4_nir_get_swizzled_channel(nir_builder
*b
, nir_ssa_def
**srcs
, int swiz
)
119 case PIPE_SWIZZLE_NONE
:
120 fprintf(stderr
, "warning: unknown swizzle\n");
123 return nir_imm_float(b
, 0.0);
125 return nir_imm_float(b
, 1.0);
135 ntq_init_ssa_def(struct vc4_compile
*c
, nir_ssa_def
*def
)
137 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
138 def
->num_components
);
139 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
144 ntq_store_dest(struct vc4_compile
*c
, nir_dest
*dest
, int chan
,
148 assert(chan
< dest
->ssa
.num_components
);
151 struct hash_entry
*entry
=
152 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
157 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
159 qregs
[chan
] = result
;
161 nir_register
*reg
= dest
->reg
.reg
;
162 assert(dest
->reg
.base_offset
== 0);
163 assert(reg
->num_array_elems
== 0);
164 struct hash_entry
*entry
=
165 _mesa_hash_table_search(c
->def_ht
, reg
);
166 struct qreg
*qregs
= entry
->data
;
168 /* Conditionally move the result to the destination if the
171 if (c
->execute
.file
!= QFILE_NULL
) {
174 qir_SF(c
, c
->execute
);
175 mov
= qir_MOV_cond(c
, QPU_COND_ZS
, qregs
[chan
], result
);
176 mov
->cond_is_exec_mask
= true;
178 qir_MOV_dest(c
, qregs
[chan
], result
);
184 ntq_get_dest(struct vc4_compile
*c
, nir_dest
*dest
)
187 struct qreg
*qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
188 for (int i
= 0; i
< dest
->ssa
.num_components
; i
++)
192 nir_register
*reg
= dest
->reg
.reg
;
193 assert(dest
->reg
.base_offset
== 0);
194 assert(reg
->num_array_elems
== 0);
195 struct hash_entry
*entry
=
196 _mesa_hash_table_search(c
->def_ht
, reg
);
202 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
204 struct hash_entry
*entry
;
206 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
207 assert(i
< src
.ssa
->num_components
);
209 nir_register
*reg
= src
.reg
.reg
;
210 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
211 assert(reg
->num_array_elems
== 0);
212 assert(src
.reg
.base_offset
== 0);
213 assert(i
< reg
->num_components
);
216 struct qreg
*qregs
= entry
->data
;
221 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
224 assert(util_is_power_of_two(instr
->dest
.write_mask
));
225 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
226 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
227 instr
->src
[src
].swizzle
[chan
]);
229 assert(!instr
->src
[src
].abs
);
230 assert(!instr
->src
[src
].negate
);
235 static inline struct qreg
236 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
239 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
240 qir_uniform_f(c
, 0.0));
244 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
246 struct qreg r
= qir_RCP(c
, x
);
248 /* Apply a Newton-Raphson step to improve the accuracy. */
249 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
250 qir_uniform_f(c
, 2.0),
257 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
259 struct qreg r
= qir_RSQ(c
, x
);
261 /* Apply a Newton-Raphson step to improve the accuracy. */
262 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
263 qir_uniform_f(c
, 1.5),
265 qir_uniform_f(c
, 0.5),
267 qir_FMUL(c
, r
, r
)))));
273 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
275 struct qreg src0_hi
= qir_SHR(c
, src0
,
276 qir_uniform_ui(c
, 24));
277 struct qreg src1_hi
= qir_SHR(c
, src1
,
278 qir_uniform_ui(c
, 24));
280 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
281 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
282 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
284 return qir_ADD(c
, lolo
, qir_SHL(c
,
285 qir_ADD(c
, hilo
, lohi
),
286 qir_uniform_ui(c
, 24)));
290 ntq_scale_depth_texture(struct vc4_compile
*c
, struct qreg src
)
292 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, src
,
293 qir_uniform_ui(c
, 8)));
294 return qir_FMUL(c
, depthf
, qir_uniform_f(c
, 1.0f
/0xffffff));
298 * Emits a lowered TXF_MS from an MSAA texture.
300 * The addressing math has been lowered in NIR, and now we just need to read
304 ntq_emit_txf(struct vc4_compile
*c
, nir_tex_instr
*instr
)
306 uint32_t tile_width
= 32;
307 uint32_t tile_height
= 32;
308 uint32_t tile_size
= (tile_height
* tile_width
*
309 VC4_MAX_SAMPLES
* sizeof(uint32_t));
311 unsigned unit
= instr
->texture_index
;
312 uint32_t w
= align(c
->key
->tex
[unit
].msaa_width
, tile_width
);
313 uint32_t w_tiles
= w
/ tile_width
;
314 uint32_t h
= align(c
->key
->tex
[unit
].msaa_height
, tile_height
);
315 uint32_t h_tiles
= h
/ tile_height
;
316 uint32_t size
= w_tiles
* h_tiles
* tile_size
;
319 assert(instr
->num_srcs
== 1);
320 assert(instr
->src
[0].src_type
== nir_tex_src_coord
);
321 addr
= ntq_get_src(c
, instr
->src
[0].src
, 0);
323 /* Perform the clamping required by kernel validation. */
324 addr
= qir_MAX(c
, addr
, qir_uniform_ui(c
, 0));
325 addr
= qir_MIN(c
, addr
, qir_uniform_ui(c
, size
- 4));
327 qir_TEX_DIRECT(c
, addr
, qir_uniform(c
, QUNIFORM_TEXTURE_MSAA_ADDR
, unit
));
329 struct qreg tex
= qir_TEX_RESULT(c
);
330 c
->num_texture_samples
++;
333 enum pipe_format format
= c
->key
->tex
[unit
].format
;
334 if (util_format_is_depth_or_stencil(format
)) {
335 struct qreg scaled
= ntq_scale_depth_texture(c
, tex
);
336 for (int i
= 0; i
< 4; i
++)
339 for (int i
= 0; i
< 4; i
++)
340 dest
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
343 for (int i
= 0; i
< 4; i
++)
344 ntq_store_dest(c
, &instr
->dest
, i
, dest
[i
]);
348 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
350 struct qreg s
, t
, r
, lod
, compare
;
351 bool is_txb
= false, is_txl
= false;
352 unsigned unit
= instr
->texture_index
;
354 if (instr
->op
== nir_texop_txf
) {
355 ntq_emit_txf(c
, instr
);
359 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
360 switch (instr
->src
[i
].src_type
) {
361 case nir_tex_src_coord
:
362 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
363 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
364 t
= qir_uniform_f(c
, 0.5);
366 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
367 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
368 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
370 case nir_tex_src_bias
:
371 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
374 case nir_tex_src_lod
:
375 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
378 case nir_tex_src_comparitor
:
379 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
382 unreachable("unknown texture source");
386 if (c
->key
->tex
[unit
].force_first_level
) {
387 lod
= qir_uniform(c
, QUNIFORM_TEXTURE_FIRST_LEVEL
, unit
);
392 struct qreg texture_u
[] = {
393 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
394 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
395 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
396 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
398 uint32_t next_texture_u
= 0;
400 /* There is no native support for GL texture rectangle coordinates, so
401 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
404 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
406 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
408 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
411 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
412 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
413 unit
| (is_txl
<< 16));
416 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
417 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
418 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
419 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
420 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
421 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
422 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
423 texture_u
[next_texture_u
++]);
426 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
430 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
434 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
436 if (is_txl
|| is_txb
)
437 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
439 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
441 c
->num_texture_samples
++;
442 struct qreg tex
= qir_TEX_RESULT(c
);
444 enum pipe_format format
= c
->key
->tex
[unit
].format
;
446 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
);
447 if (util_format_is_depth_or_stencil(format
)) {
448 struct qreg normalized
= ntq_scale_depth_texture(c
, tex
);
449 struct qreg depth_output
;
451 struct qreg u0
= qir_uniform_f(c
, 0.0f
);
452 struct qreg u1
= qir_uniform_f(c
, 1.0f
);
453 if (c
->key
->tex
[unit
].compare_mode
) {
454 switch (c
->key
->tex
[unit
].compare_func
) {
455 case PIPE_FUNC_NEVER
:
456 depth_output
= qir_uniform_f(c
, 0.0f
);
458 case PIPE_FUNC_ALWAYS
:
461 case PIPE_FUNC_EQUAL
:
462 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
463 depth_output
= qir_SEL(c
, QPU_COND_ZS
, u1
, u0
);
465 case PIPE_FUNC_NOTEQUAL
:
466 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
467 depth_output
= qir_SEL(c
, QPU_COND_ZC
, u1
, u0
);
469 case PIPE_FUNC_GREATER
:
470 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
471 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
473 case PIPE_FUNC_GEQUAL
:
474 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
475 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
478 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
479 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
481 case PIPE_FUNC_LEQUAL
:
482 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
483 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
487 depth_output
= normalized
;
490 for (int i
= 0; i
< 4; i
++)
491 dest
[i
] = depth_output
;
493 for (int i
= 0; i
< 4; i
++)
494 dest
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
499 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
503 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
505 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
506 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
508 return qir_SEL(c
, QPU_COND_NS
,
509 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)), diff
);
513 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
517 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
519 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
521 /* This will be < 0 if we truncated and the truncation was of a value
522 * that was < 0 in the first place.
524 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
526 return qir_SEL(c
, QPU_COND_NS
,
527 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)), trunc
);
531 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
535 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
537 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
539 /* This will be < 0 if we truncated and the truncation was of a value
540 * that was > 0 in the first place.
542 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
544 return qir_SEL(c
, QPU_COND_NS
,
545 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)), trunc
);
549 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
553 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
554 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
555 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
556 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
559 struct qreg scaled_x
=
562 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
564 struct qreg x
= qir_FADD(c
,
565 ntq_ffract(c
, scaled_x
),
566 qir_uniform_f(c
, -0.5));
567 struct qreg x2
= qir_FMUL(c
, x
, x
);
568 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
569 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
570 x
= qir_FMUL(c
, x
, x2
);
575 qir_uniform_f(c
, coeff
[i
])));
581 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
585 pow(2.0 * M_PI
, 2) / (2 * 1),
586 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
587 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
588 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
589 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
592 struct qreg scaled_x
=
594 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
595 struct qreg x_frac
= qir_FADD(c
,
596 ntq_ffract(c
, scaled_x
),
597 qir_uniform_f(c
, -0.5));
599 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
600 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
601 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
602 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
604 x
= qir_FMUL(c
, x
, x2
);
606 struct qreg mul
= qir_FMUL(c
,
608 qir_uniform_f(c
, coeff
[i
]));
612 sum
= qir_FADD(c
, sum
, mul
);
618 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
620 struct qreg t
= qir_get_temp(c
);
623 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 0.0));
624 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 1.0))->cond
= QPU_COND_ZC
;
625 qir_MOV_dest(c
, t
, qir_uniform_f(c
, -1.0))->cond
= QPU_COND_NS
;
630 emit_vertex_input(struct vc4_compile
*c
, int attr
)
632 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
633 uint32_t attr_size
= util_format_get_blocksize(format
);
635 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
636 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
637 c
->inputs
[attr
* 4 + i
] =
638 qir_MOV(c
, qir_reg(QFILE_VPM
, attr
* 4 + i
));
644 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
646 c
->inputs
[attr
* 4 + 0] = qir_ITOF(c
, qir_reg(QFILE_FRAG_X
, 0));
647 c
->inputs
[attr
* 4 + 1] = qir_ITOF(c
, qir_reg(QFILE_FRAG_Y
, 0));
648 c
->inputs
[attr
* 4 + 2] =
650 qir_ITOF(c
, qir_FRAG_Z(c
)),
651 qir_uniform_f(c
, 1.0 / 0xffffff));
652 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
656 emit_fragment_varying(struct vc4_compile
*c
, gl_varying_slot slot
,
659 uint32_t i
= c
->num_input_slots
++;
665 if (c
->num_input_slots
>= c
->input_slots_array_size
) {
666 c
->input_slots_array_size
=
667 MAX2(4, c
->input_slots_array_size
* 2);
669 c
->input_slots
= reralloc(c
, c
->input_slots
,
670 struct vc4_varying_slot
,
671 c
->input_slots_array_size
);
674 c
->input_slots
[i
].slot
= slot
;
675 c
->input_slots
[i
].swizzle
= swizzle
;
677 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
681 emit_fragment_input(struct vc4_compile
*c
, int attr
, gl_varying_slot slot
)
683 for (int i
= 0; i
< 4; i
++) {
684 c
->inputs
[attr
* 4 + i
] =
685 emit_fragment_varying(c
, slot
, i
);
691 add_output(struct vc4_compile
*c
,
692 uint32_t decl_offset
,
696 uint32_t old_array_size
= c
->outputs_array_size
;
697 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
700 if (old_array_size
!= c
->outputs_array_size
) {
701 c
->output_slots
= reralloc(c
,
703 struct vc4_varying_slot
,
704 c
->outputs_array_size
);
707 c
->output_slots
[decl_offset
].slot
= slot
;
708 c
->output_slots
[decl_offset
].swizzle
= swizzle
;
712 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
714 unsigned array_id
= c
->num_uniform_ranges
++;
715 if (array_id
>= c
->ubo_ranges_array_size
) {
716 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
718 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
719 struct vc4_compiler_ubo_range
,
720 c
->ubo_ranges_array_size
);
723 c
->ubo_ranges
[array_id
].dst_offset
= 0;
724 c
->ubo_ranges
[array_id
].src_offset
= start
;
725 c
->ubo_ranges
[array_id
].size
= size
;
726 c
->ubo_ranges
[array_id
].used
= false;
730 ntq_src_is_only_ssa_def_user(nir_src
*src
)
735 if (!list_empty(&src
->ssa
->if_uses
))
738 return (src
->ssa
->uses
.next
== &src
->use_link
&&
739 src
->ssa
->uses
.next
->next
== &src
->ssa
->uses
);
743 * In general, emits a nir_pack_unorm_4x8 as a series of MOVs with the pack
746 * However, as an optimization, it tries to find the instructions generating
747 * the sources to be packed and just emit the pack flag there, if possible.
750 ntq_emit_pack_unorm_4x8(struct vc4_compile
*c
, nir_alu_instr
*instr
)
752 struct qreg result
= qir_get_temp(c
);
753 struct nir_alu_instr
*vec4
= NULL
;
755 /* If packing from a vec4 op (as expected), identify it so that we can
756 * peek back at what generated its sources.
758 if (instr
->src
[0].src
.is_ssa
&&
759 instr
->src
[0].src
.ssa
->parent_instr
->type
== nir_instr_type_alu
&&
760 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
)->op
==
762 vec4
= nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
765 /* If the pack is replicating the same channel 4 times, use the 8888
766 * pack flag. This is common for blending using the alpha
769 if (instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[1] &&
770 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[2] &&
771 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[3]) {
772 struct qreg rep
= ntq_get_src(c
,
774 instr
->src
[0].swizzle
[0]);
775 ntq_store_dest(c
, &instr
->dest
.dest
, 0, qir_PACK_8888_F(c
, rep
));
779 for (int i
= 0; i
< 4; i
++) {
780 int swiz
= instr
->src
[0].swizzle
[i
];
783 src
= ntq_get_src(c
, vec4
->src
[swiz
].src
,
784 vec4
->src
[swiz
].swizzle
[0]);
786 src
= ntq_get_src(c
, instr
->src
[0].src
, swiz
);
790 ntq_src_is_only_ssa_def_user(&vec4
->src
[swiz
].src
) &&
791 src
.file
== QFILE_TEMP
&&
792 c
->defs
[src
.index
] &&
793 qir_is_mul(c
->defs
[src
.index
]) &&
794 !c
->defs
[src
.index
]->dst
.pack
) {
795 struct qinst
*rewrite
= c
->defs
[src
.index
];
796 c
->defs
[src
.index
] = NULL
;
797 rewrite
->dst
= result
;
798 rewrite
->dst
.pack
= QPU_PACK_MUL_8A
+ i
;
802 qir_PACK_8_F(c
, result
, src
, i
);
805 ntq_store_dest(c
, &instr
->dest
.dest
, 0, result
);
808 /** Handles sign-extended bitfield extracts for 16 bits. */
810 ntq_emit_ibfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
813 assert(bits
.file
== QFILE_UNIF
&&
814 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
815 c
->uniform_data
[bits
.index
] == 16);
817 assert(offset
.file
== QFILE_UNIF
&&
818 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
819 int offset_bit
= c
->uniform_data
[offset
.index
];
820 assert(offset_bit
% 16 == 0);
822 return qir_UNPACK_16_I(c
, base
, offset_bit
/ 16);
825 /** Handles unsigned bitfield extracts for 8 bits. */
827 ntq_emit_ubfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
830 assert(bits
.file
== QFILE_UNIF
&&
831 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
832 c
->uniform_data
[bits
.index
] == 8);
834 assert(offset
.file
== QFILE_UNIF
&&
835 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
836 int offset_bit
= c
->uniform_data
[offset
.index
];
837 assert(offset_bit
% 8 == 0);
839 return qir_UNPACK_8_I(c
, base
, offset_bit
/ 8);
843 * If compare_instr is a valid comparison instruction, emits the
844 * compare_instr's comparison and returns the sel_instr's return value based
845 * on the compare_instr's result.
848 ntq_emit_comparison(struct vc4_compile
*c
, struct qreg
*dest
,
849 nir_alu_instr
*compare_instr
,
850 nir_alu_instr
*sel_instr
)
854 switch (compare_instr
->op
) {
880 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
881 struct qreg src1
= ntq_get_alu_src(c
, compare_instr
, 1);
883 unsigned unsized_type
=
884 nir_alu_type_get_base_type(nir_op_infos
[compare_instr
->op
].input_types
[0]);
885 if (unsized_type
== nir_type_float
)
886 qir_SF(c
, qir_FSUB(c
, src0
, src1
));
888 qir_SF(c
, qir_SUB(c
, src0
, src1
));
890 switch (sel_instr
->op
) {
895 *dest
= qir_SEL(c
, cond
,
896 qir_uniform_f(c
, 1.0), qir_uniform_f(c
, 0.0));
900 *dest
= qir_SEL(c
, cond
,
901 ntq_get_alu_src(c
, sel_instr
, 1),
902 ntq_get_alu_src(c
, sel_instr
, 2));
906 *dest
= qir_SEL(c
, cond
,
907 qir_uniform_ui(c
, ~0), qir_uniform_ui(c
, 0));
915 * Attempts to fold a comparison generating a boolean result into the
916 * condition code for selecting between two values, instead of comparing the
917 * boolean result against 0 to generate the condition code.
919 static struct qreg
ntq_emit_bcsel(struct vc4_compile
*c
, nir_alu_instr
*instr
,
922 if (!instr
->src
[0].src
.is_ssa
)
924 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
926 nir_alu_instr
*compare
=
927 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
932 if (ntq_emit_comparison(c
, &dest
, compare
, instr
))
937 return qir_SEL(c
, QPU_COND_NS
, src
[1], src
[2]);
941 ntq_fddx(struct vc4_compile
*c
, struct qreg src
)
943 /* Make sure that we have a bare temp to use for MUL rotation, so it
944 * can be allocated to an accumulator.
946 if (src
.pack
|| src
.file
!= QFILE_TEMP
)
947 src
= qir_MOV(c
, src
);
949 struct qreg from_left
= qir_ROT_MUL(c
, src
, 1);
950 struct qreg from_right
= qir_ROT_MUL(c
, src
, 15);
952 /* Distinguish left/right pixels of the quad. */
953 qir_SF(c
, qir_AND(c
, qir_reg(QFILE_QPU_ELEMENT
, 0),
954 qir_uniform_ui(c
, 1)));
956 return qir_SEL(c
, QPU_COND_ZS
,
957 qir_FSUB(c
, from_right
, src
),
958 qir_FSUB(c
, src
, from_left
));
962 ntq_fddy(struct vc4_compile
*c
, struct qreg src
)
964 if (src
.pack
|| src
.file
!= QFILE_TEMP
)
965 src
= qir_MOV(c
, src
);
967 struct qreg from_bottom
= qir_ROT_MUL(c
, src
, 2);
968 struct qreg from_top
= qir_ROT_MUL(c
, src
, 14);
970 /* Distinguish top/bottom pixels of the quad. */
972 qir_reg(QFILE_QPU_ELEMENT
, 0),
973 qir_uniform_ui(c
, 2)));
975 return qir_SEL(c
, QPU_COND_ZS
,
976 qir_FSUB(c
, from_top
, src
),
977 qir_FSUB(c
, src
, from_bottom
));
981 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
983 /* This should always be lowered to ALU operations for VC4. */
984 assert(!instr
->dest
.saturate
);
986 /* Vectors are special in that they have non-scalarized writemasks,
987 * and just take the first swizzle channel for each argument in order
988 * into each writemask channel.
990 if (instr
->op
== nir_op_vec2
||
991 instr
->op
== nir_op_vec3
||
992 instr
->op
== nir_op_vec4
) {
994 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
995 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
996 instr
->src
[i
].swizzle
[0]);
997 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
998 ntq_store_dest(c
, &instr
->dest
.dest
, i
, srcs
[i
]);
1002 if (instr
->op
== nir_op_pack_unorm_4x8
) {
1003 ntq_emit_pack_unorm_4x8(c
, instr
);
1007 if (instr
->op
== nir_op_unpack_unorm_4x8
) {
1008 struct qreg src
= ntq_get_src(c
, instr
->src
[0].src
,
1009 instr
->src
[0].swizzle
[0]);
1010 for (int i
= 0; i
< 4; i
++) {
1011 if (instr
->dest
.write_mask
& (1 << i
))
1012 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
1013 qir_UNPACK_8_F(c
, src
, i
));
1018 /* General case: We can just grab the one used channel per src. */
1019 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
1020 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1021 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
1026 switch (instr
->op
) {
1029 result
= qir_MOV(c
, src
[0]);
1032 result
= qir_FMUL(c
, src
[0], src
[1]);
1035 result
= qir_FADD(c
, src
[0], src
[1]);
1038 result
= qir_FSUB(c
, src
[0], src
[1]);
1041 result
= qir_FMIN(c
, src
[0], src
[1]);
1044 result
= qir_FMAX(c
, src
[0], src
[1]);
1049 result
= qir_FTOI(c
, src
[0]);
1053 result
= qir_ITOF(c
, src
[0]);
1056 result
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
1059 result
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
1064 result
= qir_SEL(c
, QPU_COND_ZC
,
1065 qir_uniform_ui(c
, ~0),
1066 qir_uniform_ui(c
, 0));
1070 result
= qir_ADD(c
, src
[0], src
[1]);
1073 result
= qir_SHR(c
, src
[0], src
[1]);
1076 result
= qir_SUB(c
, src
[0], src
[1]);
1079 result
= qir_ASR(c
, src
[0], src
[1]);
1082 result
= qir_SHL(c
, src
[0], src
[1]);
1085 result
= qir_MIN(c
, src
[0], src
[1]);
1088 result
= qir_MAX(c
, src
[0], src
[1]);
1091 result
= qir_AND(c
, src
[0], src
[1]);
1094 result
= qir_OR(c
, src
[0], src
[1]);
1097 result
= qir_XOR(c
, src
[0], src
[1]);
1100 result
= qir_NOT(c
, src
[0]);
1104 result
= ntq_umul(c
, src
[0], src
[1]);
1120 if (!ntq_emit_comparison(c
, &result
, instr
, instr
)) {
1121 fprintf(stderr
, "Bad comparison instruction\n");
1126 result
= ntq_emit_bcsel(c
, instr
, src
);
1130 result
= qir_SEL(c
, QPU_COND_ZC
, src
[1], src
[2]);
1134 result
= ntq_rcp(c
, src
[0]);
1137 result
= ntq_rsq(c
, src
[0]);
1140 result
= qir_EXP2(c
, src
[0]);
1143 result
= qir_LOG2(c
, src
[0]);
1147 result
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1150 result
= ntq_fceil(c
, src
[0]);
1153 result
= ntq_ffract(c
, src
[0]);
1156 result
= ntq_ffloor(c
, src
[0]);
1160 result
= ntq_fsin(c
, src
[0]);
1163 result
= ntq_fcos(c
, src
[0]);
1167 result
= ntq_fsign(c
, src
[0]);
1171 result
= qir_FMAXABS(c
, src
[0], src
[0]);
1174 result
= qir_MAX(c
, src
[0],
1175 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1178 case nir_op_ibitfield_extract
:
1179 result
= ntq_emit_ibfe(c
, src
[0], src
[1], src
[2]);
1182 case nir_op_ubitfield_extract
:
1183 result
= ntq_emit_ubfe(c
, src
[0], src
[1], src
[2]);
1186 case nir_op_usadd_4x8
:
1187 result
= qir_V8ADDS(c
, src
[0], src
[1]);
1190 case nir_op_ussub_4x8
:
1191 result
= qir_V8SUBS(c
, src
[0], src
[1]);
1194 case nir_op_umin_4x8
:
1195 result
= qir_V8MIN(c
, src
[0], src
[1]);
1198 case nir_op_umax_4x8
:
1199 result
= qir_V8MAX(c
, src
[0], src
[1]);
1202 case nir_op_umul_unorm_4x8
:
1203 result
= qir_V8MULD(c
, src
[0], src
[1]);
1207 case nir_op_fddx_coarse
:
1208 case nir_op_fddx_fine
:
1209 result
= ntq_fddx(c
, src
[0]);
1213 case nir_op_fddy_coarse
:
1214 case nir_op_fddy_fine
:
1215 result
= ntq_fddy(c
, src
[0]);
1219 fprintf(stderr
, "unknown NIR ALU inst: ");
1220 nir_print_instr(&instr
->instr
, stderr
);
1221 fprintf(stderr
, "\n");
1225 /* We have a scalar result, so the instruction should only have a
1226 * single channel written to.
1228 assert(util_is_power_of_two(instr
->dest
.write_mask
));
1229 ntq_store_dest(c
, &instr
->dest
.dest
,
1230 ffs(instr
->dest
.write_mask
) - 1, result
);
1234 emit_frag_end(struct vc4_compile
*c
)
1237 if (c
->output_color_index
!= -1) {
1238 color
= c
->outputs
[c
->output_color_index
];
1240 color
= qir_uniform_ui(c
, 0);
1243 uint32_t discard_cond
= QPU_COND_ALWAYS
;
1244 if (c
->s
->info
.fs
.uses_discard
) {
1245 qir_SF(c
, c
->discard
);
1246 discard_cond
= QPU_COND_ZS
;
1249 if (c
->fs_key
->stencil_enabled
) {
1250 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1251 qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1252 if (c
->fs_key
->stencil_twoside
) {
1253 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1254 qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1256 if (c
->fs_key
->stencil_full_writemasks
) {
1257 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1258 qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1262 if (c
->output_sample_mask_index
!= -1) {
1263 qir_MS_MASK(c
, c
->outputs
[c
->output_sample_mask_index
]);
1266 if (c
->fs_key
->depth_enabled
) {
1267 if (c
->output_position_index
!= -1) {
1268 qir_FTOI_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1270 c
->outputs
[c
->output_position_index
],
1271 qir_uniform_f(c
, 0xffffff)))->cond
= discard_cond
;
1273 qir_MOV_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1274 qir_FRAG_Z(c
))->cond
= discard_cond
;
1278 if (!c
->msaa_per_sample_output
) {
1279 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE
, 0),
1280 color
)->cond
= discard_cond
;
1282 for (int i
= 0; i
< VC4_MAX_SAMPLES
; i
++) {
1283 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE_MS
, 0),
1284 c
->sample_colors
[i
])->cond
= discard_cond
;
1290 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1292 struct qreg packed
= qir_get_temp(c
);
1294 for (int i
= 0; i
< 2; i
++) {
1296 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1298 struct qreg packed_chan
= packed
;
1299 packed_chan
.pack
= QPU_PACK_A_16A
+ i
;
1301 qir_FTOI_dest(c
, packed_chan
,
1304 c
->outputs
[c
->output_position_index
+ i
],
1309 qir_VPM_WRITE(c
, packed
);
1313 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1315 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1316 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1318 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1319 c
->outputs
[c
->output_position_index
+ 2],
1326 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1328 qir_VPM_WRITE(c
, rcp_w
);
1332 emit_point_size_write(struct vc4_compile
*c
)
1334 struct qreg point_size
;
1336 if (c
->output_point_size_index
!= -1)
1337 point_size
= c
->outputs
[c
->output_point_size_index
];
1339 point_size
= qir_uniform_f(c
, 1.0);
1341 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1344 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1346 qir_VPM_WRITE(c
, point_size
);
1350 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1352 * The simulator insists that there be at least one vertex attribute, so
1353 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1354 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1355 * to consume it here.
1358 emit_stub_vpm_read(struct vc4_compile
*c
)
1363 c
->vattr_sizes
[0] = 4;
1364 (void)qir_MOV(c
, qir_reg(QFILE_VPM
, 0));
1369 emit_vert_end(struct vc4_compile
*c
,
1370 struct vc4_varying_slot
*fs_inputs
,
1371 uint32_t num_fs_inputs
)
1373 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1375 emit_stub_vpm_read(c
);
1377 emit_scaled_viewport_write(c
, rcp_w
);
1378 emit_zs_write(c
, rcp_w
);
1379 emit_rcp_wc_write(c
, rcp_w
);
1380 if (c
->vs_key
->per_vertex_point_size
)
1381 emit_point_size_write(c
);
1383 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1384 struct vc4_varying_slot
*input
= &fs_inputs
[i
];
1387 for (j
= 0; j
< c
->num_outputs
; j
++) {
1388 struct vc4_varying_slot
*output
=
1389 &c
->output_slots
[j
];
1391 if (input
->slot
== output
->slot
&&
1392 input
->swizzle
== output
->swizzle
) {
1393 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1397 /* Emit padding if we didn't find a declared VS output for
1400 if (j
== c
->num_outputs
)
1401 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1406 emit_coord_end(struct vc4_compile
*c
)
1408 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1410 emit_stub_vpm_read(c
);
1412 for (int i
= 0; i
< 4; i
++)
1413 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1415 emit_scaled_viewport_write(c
, rcp_w
);
1416 emit_zs_write(c
, rcp_w
);
1417 emit_rcp_wc_write(c
, rcp_w
);
1418 if (c
->vs_key
->per_vertex_point_size
)
1419 emit_point_size_write(c
);
1423 vc4_optimize_nir(struct nir_shader
*s
)
1430 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1431 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
);
1432 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
1433 NIR_PASS(progress
, s
, nir_copy_prop
);
1434 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1435 NIR_PASS(progress
, s
, nir_opt_dce
);
1436 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1437 NIR_PASS(progress
, s
, nir_opt_cse
);
1438 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8);
1439 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1440 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1441 NIR_PASS(progress
, s
, nir_opt_undef
);
1446 driver_location_compare(const void *in_a
, const void *in_b
)
1448 const nir_variable
*const *a
= in_a
;
1449 const nir_variable
*const *b
= in_b
;
1451 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1455 ntq_setup_inputs(struct vc4_compile
*c
)
1457 unsigned num_entries
= 0;
1458 nir_foreach_variable(var
, &c
->s
->inputs
)
1461 nir_variable
*vars
[num_entries
];
1464 nir_foreach_variable(var
, &c
->s
->inputs
)
1467 /* Sort the variables so that we emit the input setup in
1468 * driver_location order. This is required for VPM reads, whose data
1469 * is fetched into the VPM in driver_location (TGSI register index)
1472 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1474 for (unsigned i
= 0; i
< num_entries
; i
++) {
1475 nir_variable
*var
= vars
[i
];
1476 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1477 unsigned loc
= var
->data
.driver_location
;
1479 assert(array_len
== 1);
1481 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1484 if (c
->stage
== QSTAGE_FRAG
) {
1485 if (var
->data
.location
== VARYING_SLOT_POS
) {
1486 emit_fragcoord_input(c
, loc
);
1487 } else if (var
->data
.location
== VARYING_SLOT_PNTC
||
1488 (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1489 (c
->fs_key
->point_sprite_mask
&
1490 (1 << (var
->data
.location
-
1491 VARYING_SLOT_VAR0
))))) {
1492 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1493 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1495 emit_fragment_input(c
, loc
, var
->data
.location
);
1498 emit_vertex_input(c
, loc
);
1504 ntq_setup_outputs(struct vc4_compile
*c
)
1506 nir_foreach_variable(var
, &c
->s
->outputs
) {
1507 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1508 unsigned loc
= var
->data
.driver_location
* 4;
1510 assert(array_len
== 1);
1513 for (int i
= 0; i
< 4; i
++)
1514 add_output(c
, loc
+ i
, var
->data
.location
, i
);
1516 if (c
->stage
== QSTAGE_FRAG
) {
1517 switch (var
->data
.location
) {
1518 case FRAG_RESULT_COLOR
:
1519 case FRAG_RESULT_DATA0
:
1520 c
->output_color_index
= loc
;
1522 case FRAG_RESULT_DEPTH
:
1523 c
->output_position_index
= loc
;
1525 case FRAG_RESULT_SAMPLE_MASK
:
1526 c
->output_sample_mask_index
= loc
;
1530 switch (var
->data
.location
) {
1531 case VARYING_SLOT_POS
:
1532 c
->output_position_index
= loc
;
1534 case VARYING_SLOT_PSIZ
:
1535 c
->output_point_size_index
= loc
;
1543 ntq_setup_uniforms(struct vc4_compile
*c
)
1545 nir_foreach_variable(var
, &c
->s
->uniforms
) {
1546 uint32_t vec4_count
= st_glsl_type_size(var
->type
);
1547 unsigned vec4_size
= 4 * sizeof(float);
1549 declare_uniform_range(c
, var
->data
.driver_location
* vec4_size
,
1550 vec4_count
* vec4_size
);
1556 * Sets up the mapping from nir_register to struct qreg *.
1558 * Each nir_register gets a struct qreg per 32-bit component being stored.
1561 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1563 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1564 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1565 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1567 nir_reg
->num_components
);
1569 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1571 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1572 qregs
[i
] = qir_get_temp(c
);
1577 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1579 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1580 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1581 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u32
[i
]);
1583 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1587 ntq_emit_ssa_undef(struct vc4_compile
*c
, nir_ssa_undef_instr
*instr
)
1589 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1591 /* QIR needs there to be *some* value, so pick 0 (same as for
1592 * ntq_setup_registers().
1594 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1595 qregs
[i
] = qir_uniform_ui(c
, 0);
1599 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1601 nir_const_value
*const_offset
;
1604 switch (instr
->intrinsic
) {
1605 case nir_intrinsic_load_uniform
:
1606 assert(instr
->num_components
== 1);
1607 const_offset
= nir_src_as_const_value(instr
->src
[0]);
1609 offset
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1610 assert(offset
% 4 == 0);
1611 /* We need dwords */
1612 offset
= offset
/ 4;
1613 ntq_store_dest(c
, &instr
->dest
, 0,
1614 qir_uniform(c
, QUNIFORM_UNIFORM
,
1617 ntq_store_dest(c
, &instr
->dest
, 0,
1618 indirect_uniform_load(c
, instr
));
1622 case nir_intrinsic_load_user_clip_plane
:
1623 for (int i
= 0; i
< instr
->num_components
; i
++) {
1624 ntq_store_dest(c
, &instr
->dest
, i
,
1625 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1626 nir_intrinsic_ucp_id(instr
) *
1631 case nir_intrinsic_load_blend_const_color_r_float
:
1632 case nir_intrinsic_load_blend_const_color_g_float
:
1633 case nir_intrinsic_load_blend_const_color_b_float
:
1634 case nir_intrinsic_load_blend_const_color_a_float
:
1635 ntq_store_dest(c
, &instr
->dest
, 0,
1636 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_X
+
1638 nir_intrinsic_load_blend_const_color_r_float
),
1642 case nir_intrinsic_load_blend_const_color_rgba8888_unorm
:
1643 ntq_store_dest(c
, &instr
->dest
, 0,
1644 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_RGBA
,
1648 case nir_intrinsic_load_blend_const_color_aaaa8888_unorm
:
1649 ntq_store_dest(c
, &instr
->dest
, 0,
1650 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR_AAAA
,
1654 case nir_intrinsic_load_alpha_ref_float
:
1655 ntq_store_dest(c
, &instr
->dest
, 0,
1656 qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0));
1659 case nir_intrinsic_load_sample_mask_in
:
1660 ntq_store_dest(c
, &instr
->dest
, 0,
1661 qir_uniform(c
, QUNIFORM_SAMPLE_MASK
, 0));
1664 case nir_intrinsic_load_front_face
:
1665 /* The register contains 0 (front) or 1 (back), and we need to
1666 * turn it into a NIR bool where true means front.
1668 ntq_store_dest(c
, &instr
->dest
, 0,
1670 qir_uniform_ui(c
, -1),
1671 qir_reg(QFILE_FRAG_REV_FLAG
, 0)));
1674 case nir_intrinsic_load_input
:
1675 assert(instr
->num_components
== 1);
1676 const_offset
= nir_src_as_const_value(instr
->src
[0]);
1677 assert(const_offset
&& "vc4 doesn't support indirect inputs");
1678 if (c
->stage
== QSTAGE_FRAG
&&
1679 nir_intrinsic_base(instr
) >= VC4_NIR_TLB_COLOR_READ_INPUT
) {
1680 assert(const_offset
->u32
[0] == 0);
1681 /* Reads of the per-sample color need to be done in
1684 int sample_index
= (nir_intrinsic_base(instr
) -
1685 VC4_NIR_TLB_COLOR_READ_INPUT
);
1686 for (int i
= 0; i
<= sample_index
; i
++) {
1687 if (c
->color_reads
[i
].file
== QFILE_NULL
) {
1689 qir_TLB_COLOR_READ(c
);
1692 ntq_store_dest(c
, &instr
->dest
, 0,
1693 c
->color_reads
[sample_index
]);
1695 offset
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1696 int comp
= nir_intrinsic_component(instr
);
1697 ntq_store_dest(c
, &instr
->dest
, 0,
1698 c
->inputs
[offset
* 4 + comp
]);
1702 case nir_intrinsic_store_output
:
1703 const_offset
= nir_src_as_const_value(instr
->src
[1]);
1704 assert(const_offset
&& "vc4 doesn't support indirect outputs");
1705 offset
= nir_intrinsic_base(instr
) + const_offset
->u32
[0];
1707 /* MSAA color outputs are the only case where we have an
1708 * output that's not lowered to being a store of a single 32
1711 if (c
->stage
== QSTAGE_FRAG
&& instr
->num_components
== 4) {
1712 assert(offset
== c
->output_color_index
);
1713 for (int i
= 0; i
< 4; i
++) {
1714 c
->sample_colors
[i
] =
1715 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0],
1719 offset
= offset
* 4 + nir_intrinsic_component(instr
);
1720 assert(instr
->num_components
== 1);
1721 c
->outputs
[offset
] =
1722 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1723 c
->num_outputs
= MAX2(c
->num_outputs
, offset
+ 1);
1727 case nir_intrinsic_discard
:
1728 if (c
->execute
.file
!= QFILE_NULL
) {
1729 qir_SF(c
, c
->execute
);
1730 qir_MOV_cond(c
, QPU_COND_ZS
, c
->discard
,
1731 qir_uniform_ui(c
, ~0));
1733 qir_MOV_dest(c
, c
->discard
, qir_uniform_ui(c
, ~0));
1737 case nir_intrinsic_discard_if
: {
1738 /* true (~0) if we're discarding */
1739 struct qreg cond
= ntq_get_src(c
, instr
->src
[0], 0);
1741 if (c
->execute
.file
!= QFILE_NULL
) {
1742 /* execute == 0 means the channel is active. Invert
1743 * the condition so that we can use zero as "executing
1746 qir_SF(c
, qir_AND(c
, c
->execute
, qir_NOT(c
, cond
)));
1747 qir_MOV_cond(c
, QPU_COND_ZS
, c
->discard
, cond
);
1749 qir_OR_dest(c
, c
->discard
, c
->discard
,
1750 ntq_get_src(c
, instr
->src
[0], 0));
1757 fprintf(stderr
, "Unknown intrinsic: ");
1758 nir_print_instr(&instr
->instr
, stderr
);
1759 fprintf(stderr
, "\n");
1764 /* Clears (activates) the execute flags for any channels whose jump target
1765 * matches this block.
1768 ntq_activate_execute_for_block(struct vc4_compile
*c
)
1770 qir_SF(c
, qir_SUB(c
,
1772 qir_uniform_ui(c
, c
->cur_block
->index
)));
1773 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
, qir_uniform_ui(c
, 0));
1777 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1779 if (!c
->vc4
->screen
->has_control_flow
) {
1781 "IF statement support requires updated kernel.\n");
1785 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
1786 bool empty_else_block
=
1787 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
1788 exec_list_is_empty(&nir_else_block
->instr_list
));
1790 struct qblock
*then_block
= qir_new_block(c
);
1791 struct qblock
*after_block
= qir_new_block(c
);
1792 struct qblock
*else_block
;
1793 if (empty_else_block
)
1794 else_block
= after_block
;
1796 else_block
= qir_new_block(c
);
1798 bool was_top_level
= false;
1799 if (c
->execute
.file
== QFILE_NULL
) {
1800 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
1801 was_top_level
= true;
1804 /* Set ZS for executing (execute == 0) and jumping (if->condition ==
1805 * 0) channels, and then update execute flags for those to point to
1810 ntq_get_src(c
, if_stmt
->condition
, 0)));
1811 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1812 qir_uniform_ui(c
, else_block
->index
));
1814 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1817 qir_SF(c
, c
->execute
);
1818 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZC
);
1819 qir_link_blocks(c
->cur_block
, else_block
);
1820 qir_link_blocks(c
->cur_block
, then_block
);
1822 /* Process the THEN block. */
1823 qir_set_emit_block(c
, then_block
);
1824 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
1826 if (!empty_else_block
) {
1827 /* Handle the end of the THEN block. First, all currently
1828 * active channels update their execute flags to point to
1831 qir_SF(c
, c
->execute
);
1832 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1833 qir_uniform_ui(c
, after_block
->index
));
1835 /* If everything points at ENDIF, then jump there immediately. */
1836 qir_SF(c
, qir_SUB(c
, c
->execute
, qir_uniform_ui(c
, after_block
->index
)));
1837 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZS
);
1838 qir_link_blocks(c
->cur_block
, after_block
);
1839 qir_link_blocks(c
->cur_block
, else_block
);
1841 qir_set_emit_block(c
, else_block
);
1842 ntq_activate_execute_for_block(c
);
1843 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
1846 qir_link_blocks(c
->cur_block
, after_block
);
1848 qir_set_emit_block(c
, after_block
);
1850 c
->execute
= c
->undef
;
1852 ntq_activate_execute_for_block(c
);
1857 ntq_emit_jump(struct vc4_compile
*c
, nir_jump_instr
*jump
)
1859 switch (jump
->type
) {
1860 case nir_jump_break
:
1861 qir_SF(c
, c
->execute
);
1862 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1863 qir_uniform_ui(c
, c
->loop_break_block
->index
));
1866 case nir_jump_continue
:
1867 qir_SF(c
, c
->execute
);
1868 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1869 qir_uniform_ui(c
, c
->loop_cont_block
->index
));
1872 case nir_jump_return
:
1873 unreachable("All returns shouold be lowered\n");
1878 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1880 switch (instr
->type
) {
1881 case nir_instr_type_alu
:
1882 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1885 case nir_instr_type_intrinsic
:
1886 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1889 case nir_instr_type_load_const
:
1890 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1893 case nir_instr_type_ssa_undef
:
1894 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
1897 case nir_instr_type_tex
:
1898 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1901 case nir_instr_type_jump
:
1902 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
1906 fprintf(stderr
, "Unknown NIR instr type: ");
1907 nir_print_instr(instr
, stderr
);
1908 fprintf(stderr
, "\n");
1914 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1916 nir_foreach_instr(instr
, block
) {
1917 ntq_emit_instr(c
, instr
);
1921 static void ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
1924 ntq_emit_loop(struct vc4_compile
*c
, nir_loop
*loop
)
1926 if (!c
->vc4
->screen
->has_control_flow
) {
1928 "loop support requires updated kernel.\n");
1929 ntq_emit_cf_list(c
, &loop
->body
);
1933 bool was_top_level
= false;
1934 if (c
->execute
.file
== QFILE_NULL
) {
1935 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
1936 was_top_level
= true;
1939 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
1940 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
1942 c
->loop_cont_block
= qir_new_block(c
);
1943 c
->loop_break_block
= qir_new_block(c
);
1945 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1946 qir_set_emit_block(c
, c
->loop_cont_block
);
1947 ntq_activate_execute_for_block(c
);
1949 ntq_emit_cf_list(c
, &loop
->body
);
1951 /* If anything had explicitly continued, or is here at the end of the
1952 * loop, then we need to loop again. SF updates are masked by the
1953 * instruction's condition, so we can do the OR of the two conditions
1956 qir_SF(c
, c
->execute
);
1957 struct qinst
*cont_check
=
1961 qir_uniform_ui(c
, c
->loop_cont_block
->index
));
1962 cont_check
->cond
= QPU_COND_ZC
;
1963 cont_check
->sf
= true;
1965 qir_BRANCH(c
, QPU_COND_BRANCH_ANY_ZS
);
1966 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1967 qir_link_blocks(c
->cur_block
, c
->loop_break_block
);
1969 qir_set_emit_block(c
, c
->loop_break_block
);
1971 c
->execute
= c
->undef
;
1973 ntq_activate_execute_for_block(c
);
1975 c
->loop_break_block
= save_loop_break_block
;
1976 c
->loop_cont_block
= save_loop_cont_block
;
1980 ntq_emit_function(struct vc4_compile
*c
, nir_function_impl
*func
)
1982 fprintf(stderr
, "FUNCTIONS not handled.\n");
1987 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1989 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1990 switch (node
->type
) {
1991 case nir_cf_node_block
:
1992 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1995 case nir_cf_node_if
:
1996 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1999 case nir_cf_node_loop
:
2000 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
2003 case nir_cf_node_function
:
2004 ntq_emit_function(c
, nir_cf_node_as_function(node
));
2008 fprintf(stderr
, "Unknown NIR node type\n");
2015 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
2017 ntq_setup_registers(c
, &impl
->registers
);
2018 ntq_emit_cf_list(c
, &impl
->body
);
2022 nir_to_qir(struct vc4_compile
*c
)
2024 if (c
->stage
== QSTAGE_FRAG
&& c
->s
->info
.fs
.uses_discard
)
2025 c
->discard
= qir_MOV(c
, qir_uniform_ui(c
, 0));
2027 ntq_setup_inputs(c
);
2028 ntq_setup_outputs(c
);
2029 ntq_setup_uniforms(c
);
2030 ntq_setup_registers(c
, &c
->s
->registers
);
2032 /* Find the main function and emit the body. */
2033 nir_foreach_function(function
, c
->s
) {
2034 assert(strcmp(function
->name
, "main") == 0);
2035 assert(function
->impl
);
2036 ntq_emit_impl(c
, function
->impl
);
2040 static const nir_shader_compiler_options nir_options
= {
2041 .lower_extract_byte
= true,
2042 .lower_extract_word
= true,
2044 .lower_flrp32
= true,
2047 .lower_fsqrt
= true,
2048 .lower_negate
= true,
2049 .native_integers
= true,
2053 vc4_screen_get_compiler_options(struct pipe_screen
*pscreen
,
2054 enum pipe_shader_ir ir
, unsigned shader
)
2056 return &nir_options
;
2060 count_nir_instrs(nir_shader
*nir
)
2063 nir_foreach_function(function
, nir
) {
2064 if (!function
->impl
)
2066 nir_foreach_block(block
, function
->impl
) {
2067 nir_foreach_instr(instr
, block
)
2074 static struct vc4_compile
*
2075 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
2076 struct vc4_key
*key
)
2078 struct vc4_compile
*c
= qir_compile_init();
2082 c
->shader_state
= &key
->shader_state
->base
;
2083 c
->program_id
= key
->shader_state
->program_id
;
2085 p_atomic_inc_return(&key
->shader_state
->compiled_variant_count
);
2090 c
->fs_key
= (struct vc4_fs_key
*)key
;
2091 if (c
->fs_key
->is_points
) {
2092 c
->point_x
= emit_fragment_varying(c
, ~0, 0);
2093 c
->point_y
= emit_fragment_varying(c
, ~0, 0);
2094 } else if (c
->fs_key
->is_lines
) {
2095 c
->line_x
= emit_fragment_varying(c
, ~0, 0);
2099 c
->vs_key
= (struct vc4_vs_key
*)key
;
2102 c
->vs_key
= (struct vc4_vs_key
*)key
;
2106 c
->s
= nir_shader_clone(c
, key
->shader_state
->base
.ir
.nir
);
2108 if (stage
== QSTAGE_FRAG
)
2109 NIR_PASS_V(c
->s
, vc4_nir_lower_blend
, c
);
2111 struct nir_lower_tex_options tex_options
= {
2112 /* We would need to implement txs, but we don't want the
2113 * int/float conversions
2115 .lower_rect
= false,
2119 /* Apply swizzles to all samplers. */
2120 .swizzle_result
= ~0,
2123 /* Lower the format swizzle and ARB_texture_swizzle-style swizzle.
2124 * The format swizzling applies before sRGB decode, and
2125 * ARB_texture_swizzle is the last thing before returning the sample.
2127 for (int i
= 0; i
< ARRAY_SIZE(key
->tex
); i
++) {
2128 enum pipe_format format
= c
->key
->tex
[i
].format
;
2133 const uint8_t *format_swizzle
= vc4_get_format_swizzle(format
);
2135 for (int j
= 0; j
< 4; j
++) {
2136 uint8_t arb_swiz
= c
->key
->tex
[i
].swizzle
[j
];
2138 if (arb_swiz
<= 3) {
2139 tex_options
.swizzles
[i
][j
] =
2140 format_swizzle
[arb_swiz
];
2142 tex_options
.swizzles
[i
][j
] = arb_swiz
;
2146 if (util_format_is_srgb(format
))
2147 tex_options
.lower_srgb
|= (1 << i
);
2150 NIR_PASS_V(c
->s
, nir_lower_tex
, &tex_options
);
2152 if (c
->fs_key
&& c
->fs_key
->light_twoside
)
2153 NIR_PASS_V(c
->s
, nir_lower_two_sided_color
);
2155 if (c
->vs_key
&& c
->vs_key
->clamp_color
)
2156 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
2158 if (c
->key
->ucp_enables
) {
2159 if (stage
== QSTAGE_FRAG
) {
2160 NIR_PASS_V(c
->s
, nir_lower_clip_fs
, c
->key
->ucp_enables
);
2162 NIR_PASS_V(c
->s
, nir_lower_clip_vs
, c
->key
->ucp_enables
);
2163 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
,
2164 nir_var_shader_out
);
2168 /* FS input scalarizing must happen after nir_lower_two_sided_color,
2169 * which only handles a vec4 at a time. Similarly, VS output
2170 * scalarizing must happen after nir_lower_clip_vs.
2172 if (c
->stage
== QSTAGE_FRAG
)
2173 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_in
);
2175 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_out
);
2177 NIR_PASS_V(c
->s
, vc4_nir_lower_io
, c
);
2178 NIR_PASS_V(c
->s
, vc4_nir_lower_txf_ms
, c
);
2179 NIR_PASS_V(c
->s
, nir_lower_idiv
);
2181 vc4_optimize_nir(c
->s
);
2183 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
2185 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2186 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2187 qir_get_stage_name(c
->stage
),
2188 c
->program_id
, c
->variant_id
,
2189 count_nir_instrs(c
->s
));
2192 if (vc4_debug
& VC4_DEBUG_NIR
) {
2193 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2194 qir_get_stage_name(c
->stage
),
2195 c
->program_id
, c
->variant_id
);
2196 nir_print_shader(c
->s
, stderr
);
2207 c
->vs_key
->fs_inputs
->input_slots
,
2208 c
->vs_key
->fs_inputs
->num_inputs
);
2215 if (vc4_debug
& VC4_DEBUG_QIR
) {
2216 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2217 qir_get_stage_name(c
->stage
),
2218 c
->program_id
, c
->variant_id
);
2220 fprintf(stderr
, "\n");
2224 qir_lower_uniforms(c
);
2226 qir_schedule_instructions(c
);
2227 qir_emit_uniform_stream_resets(c
);
2229 if (vc4_debug
& VC4_DEBUG_QIR
) {
2230 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2231 qir_get_stage_name(c
->stage
),
2232 c
->program_id
, c
->variant_id
);
2234 fprintf(stderr
, "\n");
2237 qir_reorder_uniforms(c
);
2238 vc4_generate_code(vc4
, c
);
2240 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2241 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2242 qir_get_stage_name(c
->stage
),
2243 c
->program_id
, c
->variant_id
,
2245 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2246 qir_get_stage_name(c
->stage
),
2247 c
->program_id
, c
->variant_id
,
2257 vc4_shader_state_create(struct pipe_context
*pctx
,
2258 const struct pipe_shader_state
*cso
)
2260 struct vc4_context
*vc4
= vc4_context(pctx
);
2261 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2265 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2269 if (cso
->type
== PIPE_SHADER_IR_NIR
) {
2270 /* The backend takes ownership of the NIR shader on state
2275 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
2277 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2278 fprintf(stderr
, "prog %d TGSI:\n",
2280 tgsi_dump(cso
->tokens
, 0);
2281 fprintf(stderr
, "\n");
2283 s
= tgsi_to_nir(cso
->tokens
, &nir_options
);
2286 NIR_PASS_V(s
, nir_opt_global_to_local
);
2287 NIR_PASS_V(s
, nir_convert_to_ssa
);
2288 NIR_PASS_V(s
, nir_normalize_cubemap_coords
);
2290 NIR_PASS_V(s
, nir_lower_load_const_to_scalar
);
2292 vc4_optimize_nir(s
);
2294 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_local
);
2296 /* Garbage collect dead instructions */
2299 so
->base
.type
= PIPE_SHADER_IR_NIR
;
2300 so
->base
.ir
.nir
= s
;
2302 if (vc4_debug
& VC4_DEBUG_NIR
) {
2303 fprintf(stderr
, "%s prog %d NIR:\n",
2304 gl_shader_stage_name(s
->stage
),
2306 nir_print_shader(s
, stderr
);
2307 fprintf(stderr
, "\n");
2314 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2315 struct vc4_compile
*c
)
2317 int count
= c
->num_uniforms
;
2318 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2320 uinfo
->count
= count
;
2321 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2322 memcpy(uinfo
->data
, c
->uniform_data
,
2323 count
* sizeof(*uinfo
->data
));
2324 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2325 memcpy(uinfo
->contents
, c
->uniform_contents
,
2326 count
* sizeof(*uinfo
->contents
));
2327 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2329 vc4_set_shader_uniform_dirty_flags(shader
);
2333 vc4_setup_compiled_fs_inputs(struct vc4_context
*vc4
, struct vc4_compile
*c
,
2334 struct vc4_compiled_shader
*shader
)
2336 struct vc4_fs_inputs inputs
;
2338 memset(&inputs
, 0, sizeof(inputs
));
2339 inputs
.input_slots
= ralloc_array(shader
,
2340 struct vc4_varying_slot
,
2341 c
->num_input_slots
);
2343 bool input_live
[c
->num_input_slots
];
2345 memset(input_live
, 0, sizeof(input_live
));
2346 qir_for_each_inst_inorder(inst
, c
) {
2347 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2348 if (inst
->src
[i
].file
== QFILE_VARY
)
2349 input_live
[inst
->src
[i
].index
] = true;
2353 for (int i
= 0; i
< c
->num_input_slots
; i
++) {
2354 struct vc4_varying_slot
*slot
= &c
->input_slots
[i
];
2359 /* Skip non-VS-output inputs. */
2360 if (slot
->slot
== (uint8_t)~0)
2363 if (slot
->slot
== VARYING_SLOT_COL0
||
2364 slot
->slot
== VARYING_SLOT_COL1
||
2365 slot
->slot
== VARYING_SLOT_BFC0
||
2366 slot
->slot
== VARYING_SLOT_BFC1
) {
2367 shader
->color_inputs
|= (1 << inputs
.num_inputs
);
2370 inputs
.input_slots
[inputs
.num_inputs
] = *slot
;
2371 inputs
.num_inputs
++;
2373 shader
->num_inputs
= inputs
.num_inputs
;
2375 /* Add our set of inputs to the set of all inputs seen. This way, we
2376 * can have a single pointer that identifies an FS inputs set,
2377 * allowing VS to avoid recompiling when the FS is recompiled (or a
2378 * new one is bound using separate shader objects) but the inputs
2381 struct set_entry
*entry
= _mesa_set_search(vc4
->fs_inputs_set
, &inputs
);
2383 shader
->fs_inputs
= entry
->key
;
2384 ralloc_free(inputs
.input_slots
);
2386 struct vc4_fs_inputs
*alloc_inputs
;
2388 alloc_inputs
= rzalloc(vc4
->fs_inputs_set
, struct vc4_fs_inputs
);
2389 memcpy(alloc_inputs
, &inputs
, sizeof(inputs
));
2390 ralloc_steal(alloc_inputs
, inputs
.input_slots
);
2391 _mesa_set_add(vc4
->fs_inputs_set
, alloc_inputs
);
2393 shader
->fs_inputs
= alloc_inputs
;
2397 static struct vc4_compiled_shader
*
2398 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2399 struct vc4_key
*key
)
2401 struct hash_table
*ht
;
2403 if (stage
== QSTAGE_FRAG
) {
2405 key_size
= sizeof(struct vc4_fs_key
);
2408 key_size
= sizeof(struct vc4_vs_key
);
2411 struct vc4_compiled_shader
*shader
;
2412 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2416 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2417 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2419 shader
->program_id
= vc4
->next_compiled_program_id
++;
2420 if (stage
== QSTAGE_FRAG
) {
2421 vc4_setup_compiled_fs_inputs(vc4
, c
, shader
);
2423 /* Note: the temporary clone in c->s has been freed. */
2424 nir_shader
*orig_shader
= key
->shader_state
->base
.ir
.nir
;
2425 if (orig_shader
->info
.outputs_written
& (1 << FRAG_RESULT_DEPTH
))
2426 shader
->disable_early_z
= true;
2428 shader
->num_inputs
= c
->num_inputs
;
2430 shader
->vattr_offsets
[0] = 0;
2431 for (int i
= 0; i
< 8; i
++) {
2432 shader
->vattr_offsets
[i
+ 1] =
2433 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2435 if (c
->vattr_sizes
[i
])
2436 shader
->vattrs_live
|= (1 << i
);
2440 copy_uniform_state_to_shader(shader
, c
);
2441 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2442 c
->qpu_inst_count
* sizeof(uint64_t));
2444 /* Copy the compiler UBO range state to the compiled shader, dropping
2445 * out arrays that were never referenced by an indirect load.
2447 * (Note that QIR dead code elimination of an array access still
2448 * leaves that array alive, though)
2450 if (c
->num_ubo_ranges
) {
2451 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2452 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2455 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2456 struct vc4_compiler_ubo_range
*range
=
2461 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2462 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2463 shader
->ubo_ranges
[j
].size
= range
->size
;
2464 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2468 if (shader
->ubo_size
) {
2469 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2470 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2471 qir_get_stage_name(c
->stage
),
2472 c
->program_id
, c
->variant_id
,
2473 shader
->ubo_size
/ 4);
2477 qir_compile_destroy(c
);
2479 struct vc4_key
*dup_key
;
2480 dup_key
= ralloc_size(shader
, key_size
);
2481 memcpy(dup_key
, key
, key_size
);
2482 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2488 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2489 struct vc4_texture_stateobj
*texstate
)
2491 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2492 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2493 struct vc4_sampler_view
*vc4_sampler
= vc4_sampler_view(sampler
);
2494 struct pipe_sampler_state
*sampler_state
=
2495 texstate
->samplers
[i
];
2500 key
->tex
[i
].format
= sampler
->format
;
2501 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2502 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2503 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2504 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2506 if (sampler
->texture
->nr_samples
> 1) {
2507 key
->tex
[i
].msaa_width
= sampler
->texture
->width0
;
2508 key
->tex
[i
].msaa_height
= sampler
->texture
->height0
;
2509 } else if (sampler
){
2510 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2511 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2512 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2513 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2514 key
->tex
[i
].force_first_level
=
2515 vc4_sampler
->force_first_level
;
2519 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2523 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2525 struct vc4_job
*job
= vc4
->job
;
2526 struct vc4_fs_key local_key
;
2527 struct vc4_fs_key
*key
= &local_key
;
2529 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2531 VC4_DIRTY_FRAMEBUFFER
|
2533 VC4_DIRTY_RASTERIZER
|
2534 VC4_DIRTY_SAMPLE_MASK
|
2536 VC4_DIRTY_UNCOMPILED_FS
))) {
2540 memset(key
, 0, sizeof(*key
));
2541 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2542 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2543 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2544 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2545 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2546 key
->blend
= vc4
->blend
->rt
[0];
2547 if (vc4
->blend
->logicop_enable
) {
2548 key
->logicop_func
= vc4
->blend
->logicop_func
;
2550 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2553 key
->msaa
= vc4
->rasterizer
->base
.multisample
;
2554 key
->sample_coverage
= (vc4
->rasterizer
->base
.multisample
&&
2555 vc4
->sample_mask
!= (1 << VC4_MAX_SAMPLES
) - 1);
2556 key
->sample_alpha_to_coverage
= vc4
->blend
->alpha_to_coverage
;
2557 key
->sample_alpha_to_one
= vc4
->blend
->alpha_to_one
;
2560 if (vc4
->framebuffer
.cbufs
[0])
2561 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2563 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2564 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2565 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2566 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2567 key
->stencil_enabled
);
2568 if (vc4
->zsa
->base
.alpha
.enabled
) {
2569 key
->alpha_test
= true;
2570 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2573 if (key
->is_points
) {
2574 key
->point_sprite_mask
=
2575 vc4
->rasterizer
->base
.sprite_coord_enable
;
2576 key
->point_coord_upper_left
=
2577 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2578 PIPE_SPRITE_COORD_UPPER_LEFT
);
2581 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2583 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2584 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2585 if (vc4
->prog
.fs
== old_fs
)
2588 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2590 if (vc4
->rasterizer
->base
.flatshade
&&
2591 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2592 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2595 if (old_fs
&& vc4
->prog
.fs
->fs_inputs
!= old_fs
->fs_inputs
)
2596 vc4
->dirty
|= VC4_DIRTY_FS_INPUTS
;
2600 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2602 struct vc4_vs_key local_key
;
2603 struct vc4_vs_key
*key
= &local_key
;
2605 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2606 VC4_DIRTY_RASTERIZER
|
2608 VC4_DIRTY_VTXSTATE
|
2609 VC4_DIRTY_UNCOMPILED_VS
|
2610 VC4_DIRTY_FS_INPUTS
))) {
2614 memset(key
, 0, sizeof(*key
));
2615 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2616 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2617 key
->fs_inputs
= vc4
->prog
.fs
->fs_inputs
;
2618 key
->clamp_color
= vc4
->rasterizer
->base
.clamp_vertex_color
;
2620 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2621 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2623 key
->per_vertex_point_size
=
2624 (prim_mode
== PIPE_PRIM_POINTS
&&
2625 vc4
->rasterizer
->base
.point_size_per_vertex
);
2627 struct vc4_compiled_shader
*vs
=
2628 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2629 if (vs
!= vc4
->prog
.vs
) {
2631 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2634 key
->is_coord
= true;
2635 /* Coord shaders don't care what the FS inputs are. */
2636 key
->fs_inputs
= NULL
;
2637 struct vc4_compiled_shader
*cs
=
2638 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2639 if (cs
!= vc4
->prog
.cs
) {
2641 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2646 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2648 vc4_update_compiled_fs(vc4
, prim_mode
);
2649 vc4_update_compiled_vs(vc4
, prim_mode
);
2653 fs_cache_hash(const void *key
)
2655 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2659 vs_cache_hash(const void *key
)
2661 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2665 fs_cache_compare(const void *key1
, const void *key2
)
2667 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2671 vs_cache_compare(const void *key1
, const void *key2
)
2673 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2677 fs_inputs_hash(const void *key
)
2679 const struct vc4_fs_inputs
*inputs
= key
;
2681 return _mesa_hash_data(inputs
->input_slots
,
2682 sizeof(*inputs
->input_slots
) *
2683 inputs
->num_inputs
);
2687 fs_inputs_compare(const void *key1
, const void *key2
)
2689 const struct vc4_fs_inputs
*inputs1
= key1
;
2690 const struct vc4_fs_inputs
*inputs2
= key2
;
2692 return (inputs1
->num_inputs
== inputs2
->num_inputs
&&
2693 memcmp(inputs1
->input_slots
,
2694 inputs2
->input_slots
,
2695 sizeof(*inputs1
->input_slots
) *
2696 inputs1
->num_inputs
) == 0);
2700 delete_from_cache_if_matches(struct hash_table
*ht
,
2701 struct hash_entry
*entry
,
2702 struct vc4_uncompiled_shader
*so
)
2704 const struct vc4_key
*key
= entry
->key
;
2706 if (key
->shader_state
== so
) {
2707 struct vc4_compiled_shader
*shader
= entry
->data
;
2708 _mesa_hash_table_remove(ht
, entry
);
2709 vc4_bo_unreference(&shader
->bo
);
2710 ralloc_free(shader
);
2715 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2717 struct vc4_context
*vc4
= vc4_context(pctx
);
2718 struct vc4_uncompiled_shader
*so
= hwcso
;
2720 struct hash_entry
*entry
;
2721 hash_table_foreach(vc4
->fs_cache
, entry
)
2722 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2723 hash_table_foreach(vc4
->vs_cache
, entry
)
2724 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2726 ralloc_free(so
->base
.ir
.nir
);
2731 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2733 struct vc4_context
*vc4
= vc4_context(pctx
);
2734 vc4
->prog
.bind_fs
= hwcso
;
2735 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2739 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2741 struct vc4_context
*vc4
= vc4_context(pctx
);
2742 vc4
->prog
.bind_vs
= hwcso
;
2743 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2747 vc4_program_init(struct pipe_context
*pctx
)
2749 struct vc4_context
*vc4
= vc4_context(pctx
);
2751 pctx
->create_vs_state
= vc4_shader_state_create
;
2752 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2754 pctx
->create_fs_state
= vc4_shader_state_create
;
2755 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2757 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2758 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2760 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2762 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2764 vc4
->fs_inputs_set
= _mesa_set_create(pctx
, fs_inputs_hash
,
2769 vc4_program_fini(struct pipe_context
*pctx
)
2771 struct vc4_context
*vc4
= vc4_context(pctx
);
2773 struct hash_entry
*entry
;
2774 hash_table_foreach(vc4
->fs_cache
, entry
) {
2775 struct vc4_compiled_shader
*shader
= entry
->data
;
2776 vc4_bo_unreference(&shader
->bo
);
2777 ralloc_free(shader
);
2778 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2781 hash_table_foreach(vc4
->vs_cache
, entry
) {
2782 struct vc4_compiled_shader
*shader
= entry
->data
;
2783 vc4_bo_unreference(&shader
->bo
);
2784 ralloc_free(shader
);
2785 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);