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 "pipe/p_state.h"
27 #include "util/u_format.h"
28 #include "util/u_hash.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/ralloc.h"
32 #include "util/hash_table.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_info.h"
35 #include "tgsi/tgsi_lowering.h"
36 #include "tgsi/tgsi_parse.h"
37 #include "nir/tgsi_to_nir.h"
39 #include "vc4_context.h"
42 #ifdef USE_VC4_SIMULATOR
43 #include "simpenrose/simpenrose.h"
47 struct vc4_uncompiled_shader
*shader_state
;
49 enum pipe_format format
;
50 unsigned compare_mode
:1;
51 unsigned compare_func
:3;
55 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
61 enum pipe_format color_format
;
65 bool stencil_full_writemasks
;
69 bool point_coord_upper_left
;
71 uint8_t alpha_test_func
;
73 uint32_t point_sprite_mask
;
75 struct pipe_rt_blend_state blend
;
82 * This is a proxy for the array of FS input semantics, which is
83 * larger than we would want to put in the key.
85 uint64_t compiled_fs_id
;
87 enum pipe_format attr_formats
[8];
89 bool per_vertex_point_size
;
93 resize_qreg_array(struct vc4_compile
*c
,
98 if (*size
>= decl_size
)
101 uint32_t old_size
= *size
;
102 *size
= MAX2(*size
* 2, decl_size
);
103 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
105 fprintf(stderr
, "Malloc failure\n");
109 for (uint32_t i
= old_size
; i
< *size
; i
++)
110 (*regs
)[i
] = c
->undef
;
114 indirect_uniform_load(struct vc4_compile
*c
,
115 struct qreg indirect_offset
,
118 struct vc4_compiler_ubo_range
*range
= NULL
;
120 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
121 range
= &c
->ubo_ranges
[i
];
122 if (offset
>= range
->src_offset
&&
123 offset
< range
->src_offset
+ range
->size
) {
127 /* The driver-location-based offset always has to be within a declared
133 range
->dst_offset
= c
->next_ubo_dst_offset
;
134 c
->next_ubo_dst_offset
+= range
->size
;
138 offset
-= range
->src_offset
;
139 /* Translate the user's TGSI register index from the TGSI register
140 * base to a byte offset.
142 indirect_offset
= qir_SHL(c
, indirect_offset
, qir_uniform_ui(c
, 4));
144 /* Adjust for where we stored the TGSI register base. */
145 indirect_offset
= qir_ADD(c
, indirect_offset
,
146 qir_uniform_ui(c
, (range
->dst_offset
+
149 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
150 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
151 indirect_offset
= qir_MIN(c
, indirect_offset
,
152 qir_uniform_ui(c
, (range
->dst_offset
+
155 qir_TEX_DIRECT(c
, indirect_offset
, qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
156 struct qreg r4
= qir_TEX_RESULT(c
);
157 c
->num_texture_samples
++;
158 return qir_MOV(c
, r4
);
162 ntq_get_dest(struct vc4_compile
*c
, nir_dest dest
)
164 assert(!dest
.is_ssa
);
165 nir_register
*reg
= dest
.reg
.reg
;
166 struct hash_entry
*entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
167 assert(reg
->num_array_elems
== 0);
168 assert(dest
.reg
.base_offset
== 0);
170 struct qreg
*qregs
= entry
->data
;
175 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
177 struct hash_entry
*entry
;
179 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
180 assert(i
< src
.ssa
->num_components
);
182 nir_register
*reg
= src
.reg
.reg
;
183 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
184 assert(reg
->num_array_elems
== 0);
185 assert(src
.reg
.base_offset
== 0);
186 assert(i
< reg
->num_components
);
189 struct qreg
*qregs
= entry
->data
;
194 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
197 assert(util_is_power_of_two(instr
->dest
.write_mask
));
198 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
199 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
200 instr
->src
[src
].swizzle
[chan
]);
202 assert(!instr
->src
[src
].abs
);
203 assert(!instr
->src
[src
].negate
);
209 get_swizzled_channel(struct vc4_compile
*c
,
210 struct qreg
*srcs
, int swiz
)
214 case UTIL_FORMAT_SWIZZLE_NONE
:
215 fprintf(stderr
, "warning: unknown swizzle\n");
217 case UTIL_FORMAT_SWIZZLE_0
:
218 return qir_uniform_f(c
, 0.0);
219 case UTIL_FORMAT_SWIZZLE_1
:
220 return qir_uniform_f(c
, 1.0);
221 case UTIL_FORMAT_SWIZZLE_X
:
222 case UTIL_FORMAT_SWIZZLE_Y
:
223 case UTIL_FORMAT_SWIZZLE_Z
:
224 case UTIL_FORMAT_SWIZZLE_W
:
229 static inline struct qreg
230 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
233 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
234 qir_uniform_f(c
, 0.0));
238 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
240 struct qreg r
= qir_RCP(c
, x
);
242 /* Apply a Newton-Raphson step to improve the accuracy. */
243 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
244 qir_uniform_f(c
, 2.0),
251 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
253 struct qreg r
= qir_RSQ(c
, x
);
255 /* Apply a Newton-Raphson step to improve the accuracy. */
256 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
257 qir_uniform_f(c
, 1.5),
259 qir_uniform_f(c
, 0.5),
261 qir_FMUL(c
, r
, r
)))));
267 qir_srgb_decode(struct vc4_compile
*c
, struct qreg srgb
)
269 struct qreg low
= qir_FMUL(c
, srgb
, qir_uniform_f(c
, 1.0 / 12.92));
270 struct qreg high
= qir_POW(c
,
274 qir_uniform_f(c
, 0.055)),
275 qir_uniform_f(c
, 1.0 / 1.055)),
276 qir_uniform_f(c
, 2.4));
278 qir_SF(c
, qir_FSUB(c
, srgb
, qir_uniform_f(c
, 0.04045)));
279 return qir_SEL_X_Y_NS(c
, low
, high
);
283 qir_srgb_encode(struct vc4_compile
*c
, struct qreg linear
)
285 struct qreg low
= qir_FMUL(c
, linear
, qir_uniform_f(c
, 12.92));
286 struct qreg high
= qir_FSUB(c
,
288 qir_uniform_f(c
, 1.055),
291 qir_uniform_f(c
, 0.41666))),
292 qir_uniform_f(c
, 0.055));
294 qir_SF(c
, qir_FSUB(c
, linear
, qir_uniform_f(c
, 0.0031308)));
295 return qir_SEL_X_Y_NS(c
, low
, high
);
299 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
301 struct qreg src0_hi
= qir_SHR(c
, src0
,
302 qir_uniform_ui(c
, 24));
303 struct qreg src1_hi
= qir_SHR(c
, src1
,
304 qir_uniform_ui(c
, 24));
306 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
307 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
308 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
310 return qir_ADD(c
, lolo
, qir_SHL(c
,
311 qir_ADD(c
, hilo
, lohi
),
312 qir_uniform_ui(c
, 24)));
316 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
318 struct qreg s
, t
, r
, lod
, proj
, compare
;
319 bool is_txb
= false, is_txl
= false, has_proj
= false;
320 unsigned unit
= instr
->sampler_index
;
322 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
323 switch (instr
->src
[i
].src_type
) {
324 case nir_tex_src_coord
:
325 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
326 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
327 t
= qir_uniform_f(c
, 0.5);
329 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
330 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
331 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
333 case nir_tex_src_bias
:
334 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
337 case nir_tex_src_lod
:
338 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
341 case nir_tex_src_comparitor
:
342 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
344 case nir_tex_src_projector
:
345 proj
= qir_RCP(c
, ntq_get_src(c
, instr
->src
[i
].src
, 0));
346 s
= qir_FMUL(c
, s
, proj
);
347 t
= qir_FMUL(c
, t
, proj
);
351 unreachable("unknown texture source");
355 struct qreg texture_u
[] = {
356 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
357 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
358 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
359 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
361 uint32_t next_texture_u
= 0;
363 /* There is no native support for GL texture rectangle coordinates, so
364 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
367 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
369 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
371 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
374 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
375 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
376 unit
| (is_txl
<< 16));
379 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
380 struct qreg ma
= qir_FMAXABS(c
, qir_FMAXABS(c
, s
, t
), r
);
381 struct qreg rcp_ma
= qir_RCP(c
, ma
);
382 s
= qir_FMUL(c
, s
, rcp_ma
);
383 t
= qir_FMUL(c
, t
, rcp_ma
);
384 r
= qir_FMUL(c
, r
, rcp_ma
);
386 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
387 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
388 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
389 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
390 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
391 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
392 texture_u
[next_texture_u
++]);
395 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
399 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
403 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
405 if (is_txl
|| is_txb
)
406 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
408 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
410 c
->num_texture_samples
++;
411 struct qreg r4
= qir_TEX_RESULT(c
);
413 enum pipe_format format
= c
->key
->tex
[unit
].format
;
415 struct qreg unpacked
[4];
416 if (util_format_is_depth_or_stencil(format
)) {
417 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, r4
,
418 qir_uniform_ui(c
, 8)));
419 struct qreg normalized
= qir_FMUL(c
, depthf
,
420 qir_uniform_f(c
, 1.0f
/0xffffff));
422 struct qreg depth_output
;
424 struct qreg one
= qir_uniform_f(c
, 1.0f
);
425 if (c
->key
->tex
[unit
].compare_mode
) {
427 compare
= qir_FMUL(c
, compare
, proj
);
429 switch (c
->key
->tex
[unit
].compare_func
) {
430 case PIPE_FUNC_NEVER
:
431 depth_output
= qir_uniform_f(c
, 0.0f
);
433 case PIPE_FUNC_ALWAYS
:
436 case PIPE_FUNC_EQUAL
:
437 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
438 depth_output
= qir_SEL_X_0_ZS(c
, one
);
440 case PIPE_FUNC_NOTEQUAL
:
441 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
442 depth_output
= qir_SEL_X_0_ZC(c
, one
);
444 case PIPE_FUNC_GREATER
:
445 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
446 depth_output
= qir_SEL_X_0_NC(c
, one
);
448 case PIPE_FUNC_GEQUAL
:
449 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
450 depth_output
= qir_SEL_X_0_NS(c
, one
);
453 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
454 depth_output
= qir_SEL_X_0_NS(c
, one
);
456 case PIPE_FUNC_LEQUAL
:
457 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
458 depth_output
= qir_SEL_X_0_NC(c
, one
);
462 depth_output
= normalized
;
465 for (int i
= 0; i
< 4; i
++)
466 unpacked
[i
] = depth_output
;
468 for (int i
= 0; i
< 4; i
++)
469 unpacked
[i
] = qir_R4_UNPACK(c
, r4
, i
);
472 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
473 struct qreg texture_output
[4];
474 for (int i
= 0; i
< 4; i
++) {
475 texture_output
[i
] = get_swizzled_channel(c
, unpacked
,
479 if (util_format_is_srgb(format
)) {
480 for (int i
= 0; i
< 3; i
++)
481 texture_output
[i
] = qir_srgb_decode(c
,
485 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
);
486 for (int i
= 0; i
< 4; i
++) {
487 dest
[i
] = get_swizzled_channel(c
, texture_output
,
488 c
->key
->tex
[unit
].swizzle
[i
]);
493 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
497 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
499 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
500 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
502 return qir_SEL_X_Y_NS(c
,
503 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
508 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
512 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
514 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
516 /* This will be < 0 if we truncated and the truncation was of a value
517 * that was < 0 in the first place.
519 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
521 return qir_SEL_X_Y_NS(c
,
522 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
527 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
531 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
533 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
535 /* This will be < 0 if we truncated and the truncation was of a value
536 * that was > 0 in the first place.
538 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
540 return qir_SEL_X_Y_NS(c
,
541 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)),
546 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
550 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
551 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
552 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
553 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
556 struct qreg scaled_x
=
559 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
561 struct qreg x
= qir_FADD(c
,
562 ntq_ffract(c
, scaled_x
),
563 qir_uniform_f(c
, -0.5));
564 struct qreg x2
= qir_FMUL(c
, x
, x
);
565 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
566 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
567 x
= qir_FMUL(c
, x
, x2
);
572 qir_uniform_f(c
, coeff
[i
])));
578 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
582 pow(2.0 * M_PI
, 2) / (2 * 1),
583 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
584 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
585 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
586 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
589 struct qreg scaled_x
=
591 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
592 struct qreg x_frac
= qir_FADD(c
,
593 ntq_ffract(c
, scaled_x
),
594 qir_uniform_f(c
, -0.5));
596 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
597 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
598 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
599 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
601 x
= qir_FMUL(c
, x
, x2
);
603 struct qreg mul
= qir_FMUL(c
,
605 qir_uniform_f(c
, coeff
[i
]));
609 sum
= qir_FADD(c
, sum
, mul
);
615 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
618 return qir_SEL_X_Y_NC(c
,
619 qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0)),
620 qir_uniform_f(c
, -1.0));
624 get_channel_from_vpm(struct vc4_compile
*c
,
625 struct qreg
*vpm_reads
,
627 const struct util_format_description
*desc
)
629 const struct util_format_channel_description
*chan
=
630 &desc
->channel
[swiz
];
633 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
634 return get_swizzled_channel(c
, vpm_reads
, swiz
);
635 else if (chan
->size
== 32 &&
636 chan
->type
== UTIL_FORMAT_TYPE_FLOAT
) {
637 return get_swizzled_channel(c
, vpm_reads
, swiz
);
638 } else if (chan
->size
== 32 &&
639 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
640 if (chan
->normalized
) {
642 qir_ITOF(c
, vpm_reads
[swiz
]),
646 return qir_ITOF(c
, vpm_reads
[swiz
]);
648 } else if (chan
->size
== 8 &&
649 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
650 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
651 struct qreg vpm
= vpm_reads
[0];
652 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
653 temp
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
654 if (chan
->normalized
) {
655 return qir_FSUB(c
, qir_FMUL(c
,
656 qir_UNPACK_8_F(c
, temp
, swiz
),
657 qir_uniform_f(c
, 2.0)),
658 qir_uniform_f(c
, 1.0));
662 qir_UNPACK_8_I(c
, temp
,
664 qir_uniform_f(c
, -128.0));
667 if (chan
->normalized
) {
668 return qir_UNPACK_8_F(c
, vpm
, swiz
);
670 return qir_ITOF(c
, qir_UNPACK_8_I(c
, vpm
, swiz
));
673 } else if (chan
->size
== 16 &&
674 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
675 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
676 struct qreg vpm
= vpm_reads
[swiz
/ 2];
678 /* Note that UNPACK_16F eats a half float, not ints, so we use
679 * UNPACK_16_I for all of these.
681 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
682 temp
= qir_ITOF(c
, qir_UNPACK_16_I(c
, vpm
, swiz
% 2));
683 if (chan
->normalized
) {
684 return qir_FMUL(c
, temp
,
685 qir_uniform_f(c
, 1/32768.0f
));
690 /* UNPACK_16I sign-extends, so we have to emit ANDs. */
692 if (swiz
== 1 || swiz
== 3)
693 temp
= qir_UNPACK_16_I(c
, temp
, 1);
694 temp
= qir_AND(c
, temp
, qir_uniform_ui(c
, 0xffff));
695 temp
= qir_ITOF(c
, temp
);
697 if (chan
->normalized
) {
698 return qir_FMUL(c
, temp
,
699 qir_uniform_f(c
, 1 / 65535.0));
710 emit_vertex_input(struct vc4_compile
*c
, int attr
)
712 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
713 uint32_t attr_size
= util_format_get_blocksize(format
);
714 struct qreg vpm_reads
[4];
716 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
717 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
718 struct qreg vpm
= { QFILE_VPM
, attr
* 4 + i
};
719 vpm_reads
[i
] = qir_MOV(c
, vpm
);
723 bool format_warned
= false;
724 const struct util_format_description
*desc
=
725 util_format_description(format
);
727 for (int i
= 0; i
< 4; i
++) {
728 uint8_t swiz
= desc
->swizzle
[i
];
729 struct qreg result
= get_channel_from_vpm(c
, vpm_reads
,
732 if (result
.file
== QFILE_NULL
) {
733 if (!format_warned
) {
735 "vtx element %d unsupported type: %s\n",
736 attr
, util_format_name(format
));
737 format_warned
= true;
739 result
= qir_uniform_f(c
, 0.0);
741 c
->inputs
[attr
* 4 + i
] = result
;
746 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
748 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
749 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
750 c
->inputs
[attr
* 4 + 2] =
752 qir_ITOF(c
, qir_FRAG_Z(c
)),
753 qir_uniform_f(c
, 1.0 / 0xffffff));
754 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
758 emit_point_coord_input(struct vc4_compile
*c
, int attr
)
760 if (c
->point_x
.file
== QFILE_NULL
) {
761 c
->point_x
= qir_uniform_f(c
, 0.0);
762 c
->point_y
= qir_uniform_f(c
, 0.0);
765 c
->inputs
[attr
* 4 + 0] = c
->point_x
;
766 if (c
->fs_key
->point_coord_upper_left
) {
767 c
->inputs
[attr
* 4 + 1] = qir_FSUB(c
,
768 qir_uniform_f(c
, 1.0),
771 c
->inputs
[attr
* 4 + 1] = c
->point_y
;
773 c
->inputs
[attr
* 4 + 2] = qir_uniform_f(c
, 0.0);
774 c
->inputs
[attr
* 4 + 3] = qir_uniform_f(c
, 1.0);
778 emit_fragment_varying(struct vc4_compile
*c
, uint8_t semantic
,
779 uint8_t index
, uint8_t swizzle
)
781 uint32_t i
= c
->num_input_semantics
++;
787 if (c
->num_input_semantics
>= c
->input_semantics_array_size
) {
788 c
->input_semantics_array_size
=
789 MAX2(4, c
->input_semantics_array_size
* 2);
791 c
->input_semantics
= reralloc(c
, c
->input_semantics
,
792 struct vc4_varying_semantic
,
793 c
->input_semantics_array_size
);
796 c
->input_semantics
[i
].semantic
= semantic
;
797 c
->input_semantics
[i
].index
= index
;
798 c
->input_semantics
[i
].swizzle
= swizzle
;
800 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
804 emit_fragment_input(struct vc4_compile
*c
, int attr
,
805 unsigned semantic_name
, unsigned semantic_index
)
807 for (int i
= 0; i
< 4; i
++) {
808 c
->inputs
[attr
* 4 + i
] =
809 emit_fragment_varying(c
,
818 emit_face_input(struct vc4_compile
*c
, int attr
)
820 c
->inputs
[attr
* 4 + 0] = qir_FSUB(c
,
821 qir_uniform_f(c
, 1.0),
823 qir_ITOF(c
, qir_FRAG_REV_FLAG(c
)),
824 qir_uniform_f(c
, 2.0)));
825 c
->inputs
[attr
* 4 + 1] = qir_uniform_f(c
, 0.0);
826 c
->inputs
[attr
* 4 + 2] = qir_uniform_f(c
, 0.0);
827 c
->inputs
[attr
* 4 + 3] = qir_uniform_f(c
, 1.0);
831 add_output(struct vc4_compile
*c
,
832 uint32_t decl_offset
,
833 uint8_t semantic_name
,
834 uint8_t semantic_index
,
835 uint8_t semantic_swizzle
)
837 uint32_t old_array_size
= c
->outputs_array_size
;
838 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
841 if (old_array_size
!= c
->outputs_array_size
) {
842 c
->output_semantics
= reralloc(c
,
844 struct vc4_varying_semantic
,
845 c
->outputs_array_size
);
848 c
->output_semantics
[decl_offset
].semantic
= semantic_name
;
849 c
->output_semantics
[decl_offset
].index
= semantic_index
;
850 c
->output_semantics
[decl_offset
].swizzle
= semantic_swizzle
;
854 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
856 unsigned array_id
= c
->num_uniform_ranges
++;
857 if (array_id
>= c
->ubo_ranges_array_size
) {
858 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
860 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
861 struct vc4_compiler_ubo_range
,
862 c
->ubo_ranges_array_size
);
865 c
->ubo_ranges
[array_id
].dst_offset
= 0;
866 c
->ubo_ranges
[array_id
].src_offset
= start
;
867 c
->ubo_ranges
[array_id
].size
= size
;
868 c
->ubo_ranges
[array_id
].used
= false;
872 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
874 /* Vectors are special in that they have non-scalarized writemasks,
875 * and just take the first swizzle channel for each argument in order
876 * into each writemask channel.
878 if (instr
->op
== nir_op_vec2
||
879 instr
->op
== nir_op_vec3
||
880 instr
->op
== nir_op_vec4
) {
882 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
883 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
884 instr
->src
[i
].swizzle
[0]);
885 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
886 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
891 /* General case: We can just grab the one used channel per src. */
892 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
893 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
894 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
897 /* Pick the channel to store the output in. */
898 assert(!instr
->dest
.saturate
);
899 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
900 assert(util_is_power_of_two(instr
->dest
.write_mask
));
901 dest
+= ffs(instr
->dest
.write_mask
) - 1;
906 *dest
= qir_MOV(c
, src
[0]);
909 *dest
= qir_FMUL(c
, src
[0], src
[1]);
912 *dest
= qir_FADD(c
, src
[0], src
[1]);
915 *dest
= qir_FSUB(c
, src
[0], src
[1]);
918 *dest
= qir_FMIN(c
, src
[0], src
[1]);
921 *dest
= qir_FMAX(c
, src
[0], src
[1]);
926 *dest
= qir_FTOI(c
, src
[0]);
930 *dest
= qir_ITOF(c
, src
[0]);
933 *dest
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
936 *dest
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
941 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
945 *dest
= qir_ADD(c
, src
[0], src
[1]);
948 *dest
= qir_SHR(c
, src
[0], src
[1]);
951 *dest
= qir_SUB(c
, src
[0], src
[1]);
954 *dest
= qir_ASR(c
, src
[0], src
[1]);
957 *dest
= qir_SHL(c
, src
[0], src
[1]);
960 *dest
= qir_MIN(c
, src
[0], src
[1]);
963 *dest
= qir_MAX(c
, src
[0], src
[1]);
966 *dest
= qir_AND(c
, src
[0], src
[1]);
969 *dest
= qir_OR(c
, src
[0], src
[1]);
972 *dest
= qir_XOR(c
, src
[0], src
[1]);
975 *dest
= qir_NOT(c
, src
[0]);
979 *dest
= ntq_umul(c
, src
[0], src
[1]);
983 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
984 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
987 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
988 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
991 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
992 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
995 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
996 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
999 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
1000 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
1003 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
1004 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
1007 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
1008 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
1011 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
1012 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
1015 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1016 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
1019 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1020 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
1023 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1024 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
1027 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1028 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
1033 *dest
= qir_SEL_X_Y_NS(c
, src
[1], src
[2]);
1037 *dest
= qir_SEL_X_Y_ZC(c
, src
[1], src
[2]);
1041 *dest
= ntq_rcp(c
, src
[0]);
1044 *dest
= ntq_rsq(c
, src
[0]);
1047 *dest
= qir_EXP2(c
, src
[0]);
1050 *dest
= qir_LOG2(c
, src
[0]);
1054 *dest
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1057 *dest
= ntq_fceil(c
, src
[0]);
1060 *dest
= ntq_ffract(c
, src
[0]);
1063 *dest
= ntq_ffloor(c
, src
[0]);
1067 *dest
= ntq_fsin(c
, src
[0]);
1070 *dest
= ntq_fcos(c
, src
[0]);
1074 *dest
= ntq_fsign(c
, src
[0]);
1078 *dest
= qir_FMAXABS(c
, src
[0], src
[0]);
1081 *dest
= qir_MAX(c
, src
[0],
1082 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1086 fprintf(stderr
, "unknown NIR ALU inst: ");
1087 nir_print_instr(&instr
->instr
, stderr
);
1088 fprintf(stderr
, "\n");
1094 vc4_blend_channel(struct vc4_compile
*c
,
1102 case PIPE_BLENDFACTOR_ONE
:
1104 case PIPE_BLENDFACTOR_SRC_COLOR
:
1105 return qir_FMUL(c
, val
, src
[channel
]);
1106 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1107 return qir_FMUL(c
, val
, src
[3]);
1108 case PIPE_BLENDFACTOR_DST_ALPHA
:
1109 return qir_FMUL(c
, val
, dst
[3]);
1110 case PIPE_BLENDFACTOR_DST_COLOR
:
1111 return qir_FMUL(c
, val
, dst
[channel
]);
1112 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1119 qir_uniform_f(c
, 1.0),
1124 case PIPE_BLENDFACTOR_CONST_COLOR
:
1125 return qir_FMUL(c
, val
,
1126 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
,
1128 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1129 return qir_FMUL(c
, val
,
1130 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
, 3));
1131 case PIPE_BLENDFACTOR_ZERO
:
1132 return qir_uniform_f(c
, 0.0);
1133 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1134 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1136 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1137 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1139 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1140 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1142 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1143 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1145 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1146 return qir_FMUL(c
, val
,
1147 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1149 QUNIFORM_BLEND_CONST_COLOR
,
1151 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1152 return qir_FMUL(c
, val
,
1153 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1155 QUNIFORM_BLEND_CONST_COLOR
,
1159 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1160 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1161 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1162 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1164 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1170 vc4_blend_func(struct vc4_compile
*c
,
1171 struct qreg src
, struct qreg dst
,
1175 case PIPE_BLEND_ADD
:
1176 return qir_FADD(c
, src
, dst
);
1177 case PIPE_BLEND_SUBTRACT
:
1178 return qir_FSUB(c
, src
, dst
);
1179 case PIPE_BLEND_REVERSE_SUBTRACT
:
1180 return qir_FSUB(c
, dst
, src
);
1181 case PIPE_BLEND_MIN
:
1182 return qir_FMIN(c
, src
, dst
);
1183 case PIPE_BLEND_MAX
:
1184 return qir_FMAX(c
, src
, dst
);
1188 fprintf(stderr
, "Unknown blend func %d\n", func
);
1195 * Implements fixed function blending in shader code.
1197 * VC4 doesn't have any hardware support for blending. Instead, you read the
1198 * current contents of the destination from the tile buffer after having
1199 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1200 * math using your output color and that destination value, and update the
1201 * output color appropriately.
1204 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1205 struct qreg
*dst_color
, struct qreg
*src_color
)
1207 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1209 if (!blend
->blend_enable
) {
1210 for (int i
= 0; i
< 4; i
++)
1211 result
[i
] = src_color
[i
];
1215 struct qreg clamped_src
[4];
1216 struct qreg clamped_dst
[4];
1217 for (int i
= 0; i
< 4; i
++) {
1218 clamped_src
[i
] = qir_SAT(c
, src_color
[i
]);
1219 clamped_dst
[i
] = qir_SAT(c
, dst_color
[i
]);
1221 src_color
= clamped_src
;
1222 dst_color
= clamped_dst
;
1224 struct qreg src_blend
[4], dst_blend
[4];
1225 for (int i
= 0; i
< 3; i
++) {
1226 src_blend
[i
] = vc4_blend_channel(c
,
1227 dst_color
, src_color
,
1229 blend
->rgb_src_factor
, i
);
1230 dst_blend
[i
] = vc4_blend_channel(c
,
1231 dst_color
, src_color
,
1233 blend
->rgb_dst_factor
, i
);
1235 src_blend
[3] = vc4_blend_channel(c
,
1236 dst_color
, src_color
,
1238 blend
->alpha_src_factor
, 3);
1239 dst_blend
[3] = vc4_blend_channel(c
,
1240 dst_color
, src_color
,
1242 blend
->alpha_dst_factor
, 3);
1244 for (int i
= 0; i
< 3; i
++) {
1245 result
[i
] = vc4_blend_func(c
,
1246 src_blend
[i
], dst_blend
[i
],
1249 result
[3] = vc4_blend_func(c
,
1250 src_blend
[3], dst_blend
[3],
1255 clip_distance_discard(struct vc4_compile
*c
)
1257 for (int i
= 0; i
< PIPE_MAX_CLIP_PLANES
; i
++) {
1258 if (!(c
->key
->ucp_enables
& (1 << i
)))
1261 struct qreg dist
= emit_fragment_varying(c
,
1262 TGSI_SEMANTIC_CLIPDIST
,
1268 if (c
->discard
.file
== QFILE_NULL
)
1269 c
->discard
= qir_uniform_ui(c
, 0);
1271 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_ui(c
, ~0),
1277 alpha_test_discard(struct vc4_compile
*c
)
1279 struct qreg src_alpha
;
1280 struct qreg alpha_ref
= qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0);
1282 if (!c
->fs_key
->alpha_test
)
1285 if (c
->output_color_index
!= -1)
1286 src_alpha
= c
->outputs
[c
->output_color_index
+ 3];
1288 src_alpha
= qir_uniform_f(c
, 1.0);
1290 if (c
->discard
.file
== QFILE_NULL
)
1291 c
->discard
= qir_uniform_ui(c
, 0);
1293 switch (c
->fs_key
->alpha_test_func
) {
1294 case PIPE_FUNC_NEVER
:
1295 c
->discard
= qir_uniform_ui(c
, ~0);
1297 case PIPE_FUNC_ALWAYS
:
1299 case PIPE_FUNC_EQUAL
:
1300 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1301 c
->discard
= qir_SEL_X_Y_ZS(c
, c
->discard
,
1302 qir_uniform_ui(c
, ~0));
1304 case PIPE_FUNC_NOTEQUAL
:
1305 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1306 c
->discard
= qir_SEL_X_Y_ZC(c
, c
->discard
,
1307 qir_uniform_ui(c
, ~0));
1309 case PIPE_FUNC_GREATER
:
1310 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1311 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1312 qir_uniform_ui(c
, ~0));
1314 case PIPE_FUNC_GEQUAL
:
1315 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1316 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1317 qir_uniform_ui(c
, ~0));
1319 case PIPE_FUNC_LESS
:
1320 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1321 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1322 qir_uniform_ui(c
, ~0));
1324 case PIPE_FUNC_LEQUAL
:
1325 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1326 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1327 qir_uniform_ui(c
, ~0));
1333 vc4_logicop(struct vc4_compile
*c
, struct qreg src
, struct qreg dst
)
1335 switch (c
->fs_key
->logicop_func
) {
1336 case PIPE_LOGICOP_CLEAR
:
1337 return qir_uniform_f(c
, 0.0);
1338 case PIPE_LOGICOP_NOR
:
1339 return qir_NOT(c
, qir_OR(c
, src
, dst
));
1340 case PIPE_LOGICOP_AND_INVERTED
:
1341 return qir_AND(c
, qir_NOT(c
, src
), dst
);
1342 case PIPE_LOGICOP_COPY_INVERTED
:
1343 return qir_NOT(c
, src
);
1344 case PIPE_LOGICOP_AND_REVERSE
:
1345 return qir_AND(c
, src
, qir_NOT(c
, dst
));
1346 case PIPE_LOGICOP_INVERT
:
1347 return qir_NOT(c
, dst
);
1348 case PIPE_LOGICOP_XOR
:
1349 return qir_XOR(c
, src
, dst
);
1350 case PIPE_LOGICOP_NAND
:
1351 return qir_NOT(c
, qir_AND(c
, src
, dst
));
1352 case PIPE_LOGICOP_AND
:
1353 return qir_AND(c
, src
, dst
);
1354 case PIPE_LOGICOP_EQUIV
:
1355 return qir_NOT(c
, qir_XOR(c
, src
, dst
));
1356 case PIPE_LOGICOP_NOOP
:
1358 case PIPE_LOGICOP_OR_INVERTED
:
1359 return qir_OR(c
, qir_NOT(c
, src
), dst
);
1360 case PIPE_LOGICOP_OR_REVERSE
:
1361 return qir_OR(c
, src
, qir_NOT(c
, dst
));
1362 case PIPE_LOGICOP_OR
:
1363 return qir_OR(c
, src
, dst
);
1364 case PIPE_LOGICOP_SET
:
1365 return qir_uniform_ui(c
, ~0);
1366 case PIPE_LOGICOP_COPY
:
1373 * Applies the GL blending pipeline and returns the packed (8888) output
1377 blend_pipeline(struct vc4_compile
*c
)
1379 enum pipe_format color_format
= c
->fs_key
->color_format
;
1380 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1381 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1382 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1383 struct qreg linear_dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1384 struct qreg packed_dst_color
= c
->undef
;
1386 if (c
->fs_key
->blend
.blend_enable
||
1387 c
->fs_key
->blend
.colormask
!= 0xf ||
1388 c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1389 struct qreg r4
= qir_TLB_COLOR_READ(c
);
1390 for (int i
= 0; i
< 4; i
++)
1391 tlb_read_color
[i
] = qir_R4_UNPACK(c
, r4
, i
);
1392 for (int i
= 0; i
< 4; i
++) {
1393 dst_color
[i
] = get_swizzled_channel(c
,
1396 if (util_format_is_srgb(color_format
) && i
!= 3) {
1397 linear_dst_color
[i
] =
1398 qir_srgb_decode(c
, dst_color
[i
]);
1400 linear_dst_color
[i
] = dst_color
[i
];
1404 /* Save the packed value for logic ops. Can't reuse r4
1405 * because other things might smash it (like sRGB)
1407 packed_dst_color
= qir_MOV(c
, r4
);
1410 struct qreg undef_array
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1411 const struct qreg
*output_colors
= (c
->output_color_index
!= -1 ?
1412 c
->outputs
+ c
->output_color_index
:
1414 struct qreg blend_src_color
[4];
1415 for (int i
= 0; i
< 4; i
++)
1416 blend_src_color
[i
] = output_colors
[i
];
1418 struct qreg blend_color
[4];
1419 vc4_blend(c
, blend_color
, linear_dst_color
, blend_src_color
);
1421 if (util_format_is_srgb(color_format
)) {
1422 for (int i
= 0; i
< 3; i
++)
1423 blend_color
[i
] = qir_srgb_encode(c
, blend_color
[i
]);
1426 /* Debug: Sometimes you're getting a black output and just want to see
1427 * if the FS is getting executed at all. Spam magenta into the color
1431 blend_color
[0] = qir_uniform_f(c
, 1.0);
1432 blend_color
[1] = qir_uniform_f(c
, 0.0);
1433 blend_color
[2] = qir_uniform_f(c
, 1.0);
1434 blend_color
[3] = qir_uniform_f(c
, 0.5);
1437 struct qreg swizzled_outputs
[4];
1438 for (int i
= 0; i
< 4; i
++) {
1439 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1443 struct qreg packed_color
= c
->undef
;
1444 for (int i
= 0; i
< 4; i
++) {
1445 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1447 if (packed_color
.file
== QFILE_NULL
) {
1448 packed_color
= qir_PACK_8888_F(c
, swizzled_outputs
[i
]);
1450 packed_color
= qir_PACK_8_F(c
,
1452 swizzled_outputs
[i
],
1457 if (packed_color
.file
== QFILE_NULL
)
1458 packed_color
= qir_uniform_ui(c
, 0);
1460 if (c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1461 packed_color
= vc4_logicop(c
, packed_color
, packed_dst_color
);
1464 /* If the bit isn't set in the color mask, then just return the
1465 * original dst color, instead.
1467 uint32_t colormask
= 0xffffffff;
1468 for (int i
= 0; i
< 4; i
++) {
1469 if (format_swiz
[i
] < 4 &&
1470 !(c
->fs_key
->blend
.colormask
& (1 << format_swiz
[i
]))) {
1471 colormask
&= ~(0xff << (i
* 8));
1474 if (colormask
!= 0xffffffff) {
1475 packed_color
= qir_OR(c
,
1476 qir_AND(c
, packed_color
,
1477 qir_uniform_ui(c
, colormask
)),
1478 qir_AND(c
, packed_dst_color
,
1479 qir_uniform_ui(c
, ~colormask
)));
1482 return packed_color
;
1486 emit_frag_end(struct vc4_compile
*c
)
1488 clip_distance_discard(c
);
1489 alpha_test_discard(c
);
1490 struct qreg color
= blend_pipeline(c
);
1492 if (c
->discard
.file
!= QFILE_NULL
)
1493 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1495 if (c
->fs_key
->stencil_enabled
) {
1496 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1497 if (c
->fs_key
->stencil_twoside
) {
1498 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1500 if (c
->fs_key
->stencil_full_writemasks
) {
1501 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1505 if (c
->fs_key
->depth_enabled
) {
1507 if (c
->output_position_index
!= -1) {
1508 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1509 qir_uniform_f(c
, 0xffffff)));
1513 qir_TLB_Z_WRITE(c
, z
);
1516 qir_TLB_COLOR_WRITE(c
, color
);
1520 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1524 for (int i
= 0; i
< 2; i
++) {
1526 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1528 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1530 c
->outputs
[c
->output_position_index
+ i
],
1535 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1539 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1541 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1542 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1544 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1545 c
->outputs
[c
->output_position_index
+ 2],
1552 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1554 qir_VPM_WRITE(c
, rcp_w
);
1558 emit_point_size_write(struct vc4_compile
*c
)
1560 struct qreg point_size
;
1562 if (c
->output_point_size_index
!= -1)
1563 point_size
= c
->outputs
[c
->output_point_size_index
+ 3];
1565 point_size
= qir_uniform_f(c
, 1.0);
1567 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1570 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1572 qir_VPM_WRITE(c
, point_size
);
1576 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1578 * The simulator insists that there be at least one vertex attribute, so
1579 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1580 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1581 * to consume it here.
1584 emit_stub_vpm_read(struct vc4_compile
*c
)
1589 c
->vattr_sizes
[0] = 4;
1590 struct qreg vpm
= { QFILE_VPM
, 0 };
1591 (void)qir_MOV(c
, vpm
);
1596 emit_ucp_clipdistance(struct vc4_compile
*c
)
1599 if (c
->output_clipvertex_index
!= -1)
1600 cv
= c
->output_clipvertex_index
;
1601 else if (c
->output_position_index
!= -1)
1602 cv
= c
->output_position_index
;
1606 for (int plane
= 0; plane
< PIPE_MAX_CLIP_PLANES
; plane
++) {
1607 if (!(c
->key
->ucp_enables
& (1 << plane
)))
1610 /* Pick the next outputs[] that hasn't been written to, since
1611 * there are no other program writes left to be processed at
1612 * this point. If something had been declared but not written
1613 * (like a w component), we'll just smash over the top of it.
1615 uint32_t output_index
= c
->num_outputs
++;
1616 add_output(c
, output_index
,
1617 TGSI_SEMANTIC_CLIPDIST
,
1622 struct qreg dist
= qir_uniform_f(c
, 0.0);
1623 for (int i
= 0; i
< 4; i
++) {
1624 struct qreg pos_chan
= c
->outputs
[cv
+ i
];
1626 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1628 dist
= qir_FADD(c
, dist
, qir_FMUL(c
, pos_chan
, ucp
));
1631 c
->outputs
[output_index
] = dist
;
1636 emit_vert_end(struct vc4_compile
*c
,
1637 struct vc4_varying_semantic
*fs_inputs
,
1638 uint32_t num_fs_inputs
)
1640 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1642 emit_stub_vpm_read(c
);
1643 emit_ucp_clipdistance(c
);
1645 emit_scaled_viewport_write(c
, rcp_w
);
1646 emit_zs_write(c
, rcp_w
);
1647 emit_rcp_wc_write(c
, rcp_w
);
1648 if (c
->vs_key
->per_vertex_point_size
)
1649 emit_point_size_write(c
);
1651 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1652 struct vc4_varying_semantic
*input
= &fs_inputs
[i
];
1655 for (j
= 0; j
< c
->num_outputs
; j
++) {
1656 struct vc4_varying_semantic
*output
=
1657 &c
->output_semantics
[j
];
1659 if (input
->semantic
== output
->semantic
&&
1660 input
->index
== output
->index
&&
1661 input
->swizzle
== output
->swizzle
) {
1662 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1666 /* Emit padding if we didn't find a declared VS output for
1669 if (j
== c
->num_outputs
)
1670 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1675 emit_coord_end(struct vc4_compile
*c
)
1677 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1679 emit_stub_vpm_read(c
);
1681 for (int i
= 0; i
< 4; i
++)
1682 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1684 emit_scaled_viewport_write(c
, rcp_w
);
1685 emit_zs_write(c
, rcp_w
);
1686 emit_rcp_wc_write(c
, rcp_w
);
1687 if (c
->vs_key
->per_vertex_point_size
)
1688 emit_point_size_write(c
);
1692 vc4_optimize_nir(struct nir_shader
*s
)
1699 nir_lower_vars_to_ssa(s
);
1700 nir_lower_alu_to_scalar(s
);
1702 progress
= nir_copy_prop(s
) || progress
;
1703 progress
= nir_opt_dce(s
) || progress
;
1704 progress
= nir_opt_cse(s
) || progress
;
1705 progress
= nir_opt_peephole_select(s
) || progress
;
1706 progress
= nir_opt_algebraic(s
) || progress
;
1707 progress
= nir_opt_constant_folding(s
) || progress
;
1712 driver_location_compare(const void *in_a
, const void *in_b
)
1714 const nir_variable
*const *a
= in_a
;
1715 const nir_variable
*const *b
= in_b
;
1717 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1721 ntq_setup_inputs(struct vc4_compile
*c
)
1723 unsigned num_entries
= 0;
1724 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1727 nir_variable
*vars
[num_entries
];
1730 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1733 /* Sort the variables so that we emit the input setup in
1734 * driver_location order. This is required for VPM reads, whose data
1735 * is fetched into the VPM in driver_location (TGSI register index)
1738 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1740 for (unsigned i
= 0; i
< num_entries
; i
++) {
1741 nir_variable
*var
= vars
[i
];
1742 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1743 /* XXX: map loc slots to semantics */
1744 unsigned semantic_name
= var
->data
.location
;
1745 unsigned semantic_index
= var
->data
.index
;
1746 unsigned loc
= var
->data
.driver_location
;
1748 assert(array_len
== 1);
1750 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1753 if (c
->stage
== QSTAGE_FRAG
) {
1754 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1755 emit_fragcoord_input(c
, loc
);
1756 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1757 emit_face_input(c
, loc
);
1758 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1759 (c
->fs_key
->point_sprite_mask
&
1760 (1 << semantic_index
))) {
1761 emit_point_coord_input(c
, loc
);
1763 emit_fragment_input(c
, loc
,
1768 emit_vertex_input(c
, loc
);
1774 ntq_setup_outputs(struct vc4_compile
*c
)
1776 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->outputs
) {
1777 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1778 /* XXX: map loc slots to semantics */
1779 unsigned semantic_name
= var
->data
.location
;
1780 unsigned semantic_index
= var
->data
.index
;
1781 unsigned loc
= var
->data
.driver_location
* 4;
1783 assert(array_len
== 1);
1786 /* NIR hack to pass through
1787 * TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS */
1788 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1789 semantic_index
== -1)
1792 for (int i
= 0; i
< 4; i
++) {
1800 switch (semantic_name
) {
1801 case TGSI_SEMANTIC_POSITION
:
1802 c
->output_position_index
= loc
;
1804 case TGSI_SEMANTIC_CLIPVERTEX
:
1805 c
->output_clipvertex_index
= loc
;
1807 case TGSI_SEMANTIC_COLOR
:
1808 c
->output_color_index
= loc
;
1810 case TGSI_SEMANTIC_PSIZE
:
1811 c
->output_point_size_index
= loc
;
1819 ntq_setup_uniforms(struct vc4_compile
*c
)
1821 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->uniforms
) {
1822 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1823 unsigned array_elem_size
= 4 * sizeof(float);
1825 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1826 array_len
* array_elem_size
);
1832 * Sets up the mapping from nir_register to struct qreg *.
1834 * Each nir_register gets a struct qreg per 32-bit component being stored.
1837 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1839 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1840 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1841 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1843 nir_reg
->num_components
);
1845 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1847 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1848 qregs
[i
] = qir_uniform_ui(c
, 0);
1853 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1855 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1856 instr
->def
.num_components
);
1857 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1858 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u
[i
]);
1860 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1864 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1866 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1867 struct qreg
*dest
= NULL
;
1869 if (info
->has_dest
) {
1870 dest
= ntq_get_dest(c
, instr
->dest
);
1873 switch (instr
->intrinsic
) {
1874 case nir_intrinsic_load_uniform
:
1875 for (int i
= 0; i
< instr
->num_components
; i
++) {
1876 dest
[i
] = qir_uniform(c
, QUNIFORM_UNIFORM
,
1877 instr
->const_index
[0] * 4 + i
);
1881 case nir_intrinsic_load_uniform_indirect
:
1882 for (int i
= 0; i
< instr
->num_components
; i
++) {
1883 dest
[i
] = indirect_uniform_load(c
,
1884 ntq_get_src(c
, instr
->src
[0], 0),
1885 (instr
->const_index
[0] *
1886 4 + i
) * sizeof(float));
1891 case nir_intrinsic_load_input
:
1892 for (int i
= 0; i
< instr
->num_components
; i
++)
1893 dest
[i
] = c
->inputs
[instr
->const_index
[0] * 4 + i
];
1897 case nir_intrinsic_store_output
:
1898 for (int i
= 0; i
< instr
->num_components
; i
++) {
1899 c
->outputs
[instr
->const_index
[0] * 4 + i
] =
1900 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], i
));
1902 c
->num_outputs
= MAX2(c
->num_outputs
,
1903 instr
->const_index
[0] * 4 +
1904 instr
->num_components
+ 1);
1907 case nir_intrinsic_discard
:
1908 c
->discard
= qir_uniform_ui(c
, ~0);
1911 case nir_intrinsic_discard_if
:
1912 if (c
->discard
.file
== QFILE_NULL
)
1913 c
->discard
= qir_uniform_ui(c
, 0);
1914 c
->discard
= qir_OR(c
, c
->discard
,
1915 ntq_get_src(c
, instr
->src
[0], 0));
1919 fprintf(stderr
, "Unknown intrinsic: ");
1920 nir_print_instr(&instr
->instr
, stderr
);
1921 fprintf(stderr
, "\n");
1927 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1929 fprintf(stderr
, "general IF statements not handled.\n");
1933 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1935 switch (instr
->type
) {
1936 case nir_instr_type_alu
:
1937 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1940 case nir_instr_type_intrinsic
:
1941 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1944 case nir_instr_type_load_const
:
1945 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1948 case nir_instr_type_tex
:
1949 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1953 fprintf(stderr
, "Unknown NIR instr type: ");
1954 nir_print_instr(instr
, stderr
);
1955 fprintf(stderr
, "\n");
1961 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1963 nir_foreach_instr(block
, instr
) {
1964 ntq_emit_instr(c
, instr
);
1969 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1971 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1972 switch (node
->type
) {
1973 /* case nir_cf_node_loop: */
1974 case nir_cf_node_block
:
1975 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1978 case nir_cf_node_if
:
1979 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1989 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1991 ntq_setup_registers(c
, &impl
->registers
);
1992 ntq_emit_cf_list(c
, &impl
->body
);
1996 nir_to_qir(struct vc4_compile
*c
)
1998 ntq_setup_inputs(c
);
1999 ntq_setup_outputs(c
);
2000 ntq_setup_uniforms(c
);
2001 ntq_setup_registers(c
, &c
->s
->registers
);
2003 /* Find the main function and emit the body. */
2004 nir_foreach_overload(c
->s
, overload
) {
2005 assert(strcmp(overload
->function
->name
, "main") == 0);
2006 assert(overload
->impl
);
2007 ntq_emit_impl(c
, overload
->impl
);
2011 static const nir_shader_compiler_options nir_options
= {
2016 .lower_fsqrt
= true,
2017 .lower_negate
= true,
2021 count_nir_instrs_in_block(nir_block
*block
, void *state
)
2023 int *count
= (int *) state
;
2024 nir_foreach_instr(block
, instr
) {
2025 *count
= *count
+ 1;
2031 count_nir_instrs(nir_shader
*nir
)
2034 nir_foreach_overload(nir
, overload
) {
2035 if (!overload
->impl
)
2037 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
2042 static struct vc4_compile
*
2043 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
2044 struct vc4_key
*key
)
2046 struct vc4_compile
*c
= qir_compile_init();
2049 c
->shader_state
= &key
->shader_state
->base
;
2050 c
->program_id
= key
->shader_state
->program_id
;
2051 c
->variant_id
= key
->shader_state
->compiled_variant_count
++;
2056 c
->fs_key
= (struct vc4_fs_key
*)key
;
2057 if (c
->fs_key
->is_points
) {
2058 c
->point_x
= emit_fragment_varying(c
, ~0, ~0, 0);
2059 c
->point_y
= emit_fragment_varying(c
, ~0, ~0, 0);
2060 } else if (c
->fs_key
->is_lines
) {
2061 c
->line_x
= emit_fragment_varying(c
, ~0, ~0, 0);
2065 c
->vs_key
= (struct vc4_vs_key
*)key
;
2068 c
->vs_key
= (struct vc4_vs_key
*)key
;
2072 const struct tgsi_token
*tokens
= key
->shader_state
->base
.tokens
;
2073 if (c
->fs_key
&& c
->fs_key
->light_twoside
) {
2074 if (!key
->shader_state
->twoside_tokens
) {
2075 const struct tgsi_lowering_config lowering_config
= {
2076 .color_two_side
= true,
2078 struct tgsi_shader_info info
;
2079 key
->shader_state
->twoside_tokens
=
2080 tgsi_transform_lowering(&lowering_config
,
2081 key
->shader_state
->base
.tokens
,
2084 /* If no transformation occurred, then NULL is
2085 * returned and we just use our original tokens.
2087 if (!key
->shader_state
->twoside_tokens
) {
2088 key
->shader_state
->twoside_tokens
=
2089 key
->shader_state
->base
.tokens
;
2092 tokens
= key
->shader_state
->twoside_tokens
;
2095 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2096 fprintf(stderr
, "%s prog %d/%d TGSI:\n",
2097 qir_get_stage_name(c
->stage
),
2098 c
->program_id
, c
->variant_id
);
2099 tgsi_dump(tokens
, 0);
2102 c
->s
= tgsi_to_nir(tokens
, &nir_options
);
2103 nir_opt_global_to_local(c
->s
);
2104 nir_convert_to_ssa(c
->s
);
2105 nir_lower_idiv(c
->s
);
2107 vc4_optimize_nir(c
->s
);
2109 nir_remove_dead_variables(c
->s
);
2111 nir_convert_from_ssa(c
->s
, false);
2113 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2114 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2115 qir_get_stage_name(c
->stage
),
2116 c
->program_id
, c
->variant_id
,
2117 count_nir_instrs(c
->s
));
2120 if (vc4_debug
& VC4_DEBUG_NIR
) {
2121 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2122 qir_get_stage_name(c
->stage
),
2123 c
->program_id
, c
->variant_id
);
2124 nir_print_shader(c
->s
, stderr
);
2135 vc4
->prog
.fs
->input_semantics
,
2136 vc4
->prog
.fs
->num_inputs
);
2143 if (vc4_debug
& VC4_DEBUG_QIR
) {
2144 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2145 qir_get_stage_name(c
->stage
),
2146 c
->program_id
, c
->variant_id
);
2151 qir_lower_uniforms(c
);
2153 if (vc4_debug
& VC4_DEBUG_QIR
) {
2154 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2155 qir_get_stage_name(c
->stage
),
2156 c
->program_id
, c
->variant_id
);
2159 qir_reorder_uniforms(c
);
2160 vc4_generate_code(vc4
, c
);
2162 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2163 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2164 qir_get_stage_name(c
->stage
),
2165 c
->program_id
, c
->variant_id
,
2167 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2168 qir_get_stage_name(c
->stage
),
2169 c
->program_id
, c
->variant_id
,
2179 vc4_shader_state_create(struct pipe_context
*pctx
,
2180 const struct pipe_shader_state
*cso
)
2182 struct vc4_context
*vc4
= vc4_context(pctx
);
2183 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2187 so
->base
.tokens
= tgsi_dup_tokens(cso
->tokens
);
2188 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2194 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2195 struct vc4_compile
*c
)
2197 int count
= c
->num_uniforms
;
2198 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2200 uinfo
->count
= count
;
2201 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2202 memcpy(uinfo
->data
, c
->uniform_data
,
2203 count
* sizeof(*uinfo
->data
));
2204 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2205 memcpy(uinfo
->contents
, c
->uniform_contents
,
2206 count
* sizeof(*uinfo
->contents
));
2207 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2210 static struct vc4_compiled_shader
*
2211 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2212 struct vc4_key
*key
)
2214 struct hash_table
*ht
;
2216 if (stage
== QSTAGE_FRAG
) {
2218 key_size
= sizeof(struct vc4_fs_key
);
2221 key_size
= sizeof(struct vc4_vs_key
);
2224 struct vc4_compiled_shader
*shader
;
2225 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2229 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2230 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2232 shader
->program_id
= vc4
->next_compiled_program_id
++;
2233 if (stage
== QSTAGE_FRAG
) {
2234 bool input_live
[c
->num_input_semantics
];
2236 memset(input_live
, 0, sizeof(input_live
));
2237 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
2238 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2239 if (inst
->src
[i
].file
== QFILE_VARY
)
2240 input_live
[inst
->src
[i
].index
] = true;
2244 shader
->input_semantics
= ralloc_array(shader
,
2245 struct vc4_varying_semantic
,
2246 c
->num_input_semantics
);
2248 for (int i
= 0; i
< c
->num_input_semantics
; i
++) {
2249 struct vc4_varying_semantic
*sem
= &c
->input_semantics
[i
];
2254 /* Skip non-VS-output inputs. */
2255 if (sem
->semantic
== (uint8_t)~0)
2258 if (sem
->semantic
== TGSI_SEMANTIC_COLOR
||
2259 sem
->semantic
== TGSI_SEMANTIC_BCOLOR
) {
2260 shader
->color_inputs
|= (1 << shader
->num_inputs
);
2263 shader
->input_semantics
[shader
->num_inputs
] = *sem
;
2264 shader
->num_inputs
++;
2267 shader
->num_inputs
= c
->num_inputs
;
2269 shader
->vattr_offsets
[0] = 0;
2270 for (int i
= 0; i
< 8; i
++) {
2271 shader
->vattr_offsets
[i
+ 1] =
2272 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2274 if (c
->vattr_sizes
[i
])
2275 shader
->vattrs_live
|= (1 << i
);
2279 copy_uniform_state_to_shader(shader
, c
);
2280 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, c
->qpu_insts
,
2281 c
->qpu_inst_count
* sizeof(uint64_t),
2284 /* Copy the compiler UBO range state to the compiled shader, dropping
2285 * out arrays that were never referenced by an indirect load.
2287 * (Note that QIR dead code elimination of an array access still
2288 * leaves that array alive, though)
2290 if (c
->num_ubo_ranges
) {
2291 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2292 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2295 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2296 struct vc4_compiler_ubo_range
*range
=
2301 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2302 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2303 shader
->ubo_ranges
[j
].size
= range
->size
;
2304 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2308 if (shader
->ubo_size
) {
2309 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2310 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2311 qir_get_stage_name(c
->stage
),
2312 c
->program_id
, c
->variant_id
,
2313 shader
->ubo_size
/ 4);
2317 qir_compile_destroy(c
);
2319 struct vc4_key
*dup_key
;
2320 dup_key
= ralloc_size(shader
, key_size
);
2321 memcpy(dup_key
, key
, key_size
);
2322 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2328 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2329 struct vc4_texture_stateobj
*texstate
)
2331 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2332 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2333 struct pipe_sampler_state
*sampler_state
=
2334 texstate
->samplers
[i
];
2337 key
->tex
[i
].format
= sampler
->format
;
2338 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2339 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2340 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2341 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2342 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2343 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2344 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2345 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2349 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2353 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2355 struct vc4_fs_key local_key
;
2356 struct vc4_fs_key
*key
= &local_key
;
2358 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2360 VC4_DIRTY_FRAMEBUFFER
|
2362 VC4_DIRTY_RASTERIZER
|
2364 VC4_DIRTY_TEXSTATE
|
2365 VC4_DIRTY_UNCOMPILED_FS
))) {
2369 memset(key
, 0, sizeof(*key
));
2370 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2371 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2372 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2373 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2374 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2375 key
->blend
= vc4
->blend
->rt
[0];
2376 if (vc4
->blend
->logicop_enable
) {
2377 key
->logicop_func
= vc4
->blend
->logicop_func
;
2379 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2381 if (vc4
->framebuffer
.cbufs
[0])
2382 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2384 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2385 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2386 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2387 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2388 key
->stencil_enabled
);
2389 if (vc4
->zsa
->base
.alpha
.enabled
) {
2390 key
->alpha_test
= true;
2391 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2394 if (key
->is_points
) {
2395 key
->point_sprite_mask
=
2396 vc4
->rasterizer
->base
.sprite_coord_enable
;
2397 key
->point_coord_upper_left
=
2398 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2399 PIPE_SPRITE_COORD_UPPER_LEFT
);
2402 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2404 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2405 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2406 if (vc4
->prog
.fs
== old_fs
)
2409 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2410 if (vc4
->rasterizer
->base
.flatshade
&&
2411 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2412 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2417 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2419 struct vc4_vs_key local_key
;
2420 struct vc4_vs_key
*key
= &local_key
;
2422 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2423 VC4_DIRTY_RASTERIZER
|
2425 VC4_DIRTY_TEXSTATE
|
2426 VC4_DIRTY_VTXSTATE
|
2427 VC4_DIRTY_UNCOMPILED_VS
|
2428 VC4_DIRTY_COMPILED_FS
))) {
2432 memset(key
, 0, sizeof(*key
));
2433 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2434 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2435 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2437 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2438 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2440 key
->per_vertex_point_size
=
2441 (prim_mode
== PIPE_PRIM_POINTS
&&
2442 vc4
->rasterizer
->base
.point_size_per_vertex
);
2444 vc4
->prog
.vs
= vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2445 key
->is_coord
= true;
2446 vc4
->prog
.cs
= vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2450 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2452 vc4_update_compiled_fs(vc4
, prim_mode
);
2453 vc4_update_compiled_vs(vc4
, prim_mode
);
2457 fs_cache_hash(const void *key
)
2459 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2463 vs_cache_hash(const void *key
)
2465 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2469 fs_cache_compare(const void *key1
, const void *key2
)
2471 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2475 vs_cache_compare(const void *key1
, const void *key2
)
2477 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2481 delete_from_cache_if_matches(struct hash_table
*ht
,
2482 struct hash_entry
*entry
,
2483 struct vc4_uncompiled_shader
*so
)
2485 const struct vc4_key
*key
= entry
->key
;
2487 if (key
->shader_state
== so
) {
2488 struct vc4_compiled_shader
*shader
= entry
->data
;
2489 _mesa_hash_table_remove(ht
, entry
);
2490 vc4_bo_unreference(&shader
->bo
);
2491 ralloc_free(shader
);
2496 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2498 struct vc4_context
*vc4
= vc4_context(pctx
);
2499 struct vc4_uncompiled_shader
*so
= hwcso
;
2501 struct hash_entry
*entry
;
2502 hash_table_foreach(vc4
->fs_cache
, entry
)
2503 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2504 hash_table_foreach(vc4
->vs_cache
, entry
)
2505 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2507 if (so
->twoside_tokens
!= so
->base
.tokens
)
2508 free((void *)so
->twoside_tokens
);
2509 free((void *)so
->base
.tokens
);
2514 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2516 struct vc4_context
*vc4
= vc4_context(pctx
);
2517 vc4
->prog
.bind_fs
= hwcso
;
2518 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2522 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2524 struct vc4_context
*vc4
= vc4_context(pctx
);
2525 vc4
->prog
.bind_vs
= hwcso
;
2526 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2530 vc4_program_init(struct pipe_context
*pctx
)
2532 struct vc4_context
*vc4
= vc4_context(pctx
);
2534 pctx
->create_vs_state
= vc4_shader_state_create
;
2535 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2537 pctx
->create_fs_state
= vc4_shader_state_create
;
2538 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2540 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2541 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2543 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2545 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2550 vc4_program_fini(struct pipe_context
*pctx
)
2552 struct vc4_context
*vc4
= vc4_context(pctx
);
2554 struct hash_entry
*entry
;
2555 hash_table_foreach(vc4
->fs_cache
, entry
) {
2556 struct vc4_compiled_shader
*shader
= entry
->data
;
2557 vc4_bo_unreference(&shader
->bo
);
2558 ralloc_free(shader
);
2559 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2562 hash_table_foreach(vc4
->vs_cache
, entry
) {
2563 struct vc4_compiled_shader
*shader
= entry
->data
;
2564 vc4_bo_unreference(&shader
->bo
);
2565 ralloc_free(shader
);
2566 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);