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_info.h"
34 #include "tgsi/tgsi_lowering.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "nir/tgsi_to_nir.h"
38 #include "vc4_context.h"
41 #ifdef USE_VC4_SIMULATOR
42 #include "simpenrose/simpenrose.h"
46 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
49 resize_qreg_array(struct vc4_compile
*c
,
54 if (*size
>= decl_size
)
57 uint32_t old_size
= *size
;
58 *size
= MAX2(*size
* 2, decl_size
);
59 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
61 fprintf(stderr
, "Malloc failure\n");
65 for (uint32_t i
= old_size
; i
< *size
; i
++)
66 (*regs
)[i
] = c
->undef
;
70 indirect_uniform_load(struct vc4_compile
*c
, nir_intrinsic_instr
*intr
)
72 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
73 uint32_t offset
= intr
->const_index
[0];
74 struct vc4_compiler_ubo_range
*range
= NULL
;
76 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
77 range
= &c
->ubo_ranges
[i
];
78 if (offset
>= range
->src_offset
&&
79 offset
< range
->src_offset
+ range
->size
) {
83 /* The driver-location-based offset always has to be within a declared
89 range
->dst_offset
= c
->next_ubo_dst_offset
;
90 c
->next_ubo_dst_offset
+= range
->size
;
94 offset
-= range
->src_offset
;
96 /* Adjust for where we stored the TGSI register base. */
97 indirect_offset
= qir_ADD(c
, indirect_offset
,
98 qir_uniform_ui(c
, (range
->dst_offset
+
101 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
102 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
103 indirect_offset
= qir_MIN(c
, indirect_offset
,
104 qir_uniform_ui(c
, (range
->dst_offset
+
107 qir_TEX_DIRECT(c
, indirect_offset
, qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
108 c
->num_texture_samples
++;
109 return qir_TEX_RESULT(c
);
113 ntq_get_dest(struct vc4_compile
*c
, nir_dest dest
)
115 assert(!dest
.is_ssa
);
116 nir_register
*reg
= dest
.reg
.reg
;
117 struct hash_entry
*entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
118 assert(reg
->num_array_elems
== 0);
119 assert(dest
.reg
.base_offset
== 0);
121 struct qreg
*qregs
= entry
->data
;
126 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
128 struct hash_entry
*entry
;
130 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
131 assert(i
< src
.ssa
->num_components
);
133 nir_register
*reg
= src
.reg
.reg
;
134 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
135 assert(reg
->num_array_elems
== 0);
136 assert(src
.reg
.base_offset
== 0);
137 assert(i
< reg
->num_components
);
140 struct qreg
*qregs
= entry
->data
;
145 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
148 assert(util_is_power_of_two(instr
->dest
.write_mask
));
149 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
150 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
151 instr
->src
[src
].swizzle
[chan
]);
153 assert(!instr
->src
[src
].abs
);
154 assert(!instr
->src
[src
].negate
);
160 get_swizzled_channel(struct vc4_compile
*c
,
161 struct qreg
*srcs
, int swiz
)
165 case UTIL_FORMAT_SWIZZLE_NONE
:
166 fprintf(stderr
, "warning: unknown swizzle\n");
168 case UTIL_FORMAT_SWIZZLE_0
:
169 return qir_uniform_f(c
, 0.0);
170 case UTIL_FORMAT_SWIZZLE_1
:
171 return qir_uniform_f(c
, 1.0);
172 case UTIL_FORMAT_SWIZZLE_X
:
173 case UTIL_FORMAT_SWIZZLE_Y
:
174 case UTIL_FORMAT_SWIZZLE_Z
:
175 case UTIL_FORMAT_SWIZZLE_W
:
180 static inline struct qreg
181 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
184 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
185 qir_uniform_f(c
, 0.0));
189 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
191 struct qreg r
= qir_RCP(c
, x
);
193 /* Apply a Newton-Raphson step to improve the accuracy. */
194 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
195 qir_uniform_f(c
, 2.0),
202 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
204 struct qreg r
= qir_RSQ(c
, x
);
206 /* Apply a Newton-Raphson step to improve the accuracy. */
207 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
208 qir_uniform_f(c
, 1.5),
210 qir_uniform_f(c
, 0.5),
212 qir_FMUL(c
, r
, r
)))));
218 qir_srgb_decode(struct vc4_compile
*c
, struct qreg srgb
)
220 struct qreg low
= qir_FMUL(c
, srgb
, qir_uniform_f(c
, 1.0 / 12.92));
221 struct qreg high
= qir_POW(c
,
225 qir_uniform_f(c
, 0.055)),
226 qir_uniform_f(c
, 1.0 / 1.055)),
227 qir_uniform_f(c
, 2.4));
229 qir_SF(c
, qir_FSUB(c
, srgb
, qir_uniform_f(c
, 0.04045)));
230 return qir_SEL_X_Y_NS(c
, low
, high
);
234 qir_srgb_encode(struct vc4_compile
*c
, struct qreg linear
)
236 struct qreg low
= qir_FMUL(c
, linear
, qir_uniform_f(c
, 12.92));
237 struct qreg high
= qir_FSUB(c
,
239 qir_uniform_f(c
, 1.055),
242 qir_uniform_f(c
, 0.41666))),
243 qir_uniform_f(c
, 0.055));
245 qir_SF(c
, qir_FSUB(c
, linear
, qir_uniform_f(c
, 0.0031308)));
246 return qir_SEL_X_Y_NS(c
, low
, high
);
250 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
252 struct qreg src0_hi
= qir_SHR(c
, src0
,
253 qir_uniform_ui(c
, 24));
254 struct qreg src1_hi
= qir_SHR(c
, src1
,
255 qir_uniform_ui(c
, 24));
257 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
258 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
259 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
261 return qir_ADD(c
, lolo
, qir_SHL(c
,
262 qir_ADD(c
, hilo
, lohi
),
263 qir_uniform_ui(c
, 24)));
267 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
269 struct qreg s
, t
, r
, lod
, proj
, compare
;
270 bool is_txb
= false, is_txl
= false, has_proj
= false;
271 unsigned unit
= instr
->sampler_index
;
273 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
274 switch (instr
->src
[i
].src_type
) {
275 case nir_tex_src_coord
:
276 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
277 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
278 t
= qir_uniform_f(c
, 0.5);
280 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
281 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
282 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
284 case nir_tex_src_bias
:
285 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
288 case nir_tex_src_lod
:
289 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
292 case nir_tex_src_comparitor
:
293 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
295 case nir_tex_src_projector
:
296 proj
= qir_RCP(c
, ntq_get_src(c
, instr
->src
[i
].src
, 0));
297 s
= qir_FMUL(c
, s
, proj
);
298 t
= qir_FMUL(c
, t
, proj
);
302 unreachable("unknown texture source");
306 struct qreg texture_u
[] = {
307 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
308 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
309 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
310 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
312 uint32_t next_texture_u
= 0;
314 /* There is no native support for GL texture rectangle coordinates, so
315 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
318 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
320 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
322 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
325 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
326 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
327 unit
| (is_txl
<< 16));
330 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
331 struct qreg ma
= qir_FMAXABS(c
, qir_FMAXABS(c
, s
, t
), r
);
332 struct qreg rcp_ma
= qir_RCP(c
, ma
);
333 s
= qir_FMUL(c
, s
, rcp_ma
);
334 t
= qir_FMUL(c
, t
, rcp_ma
);
335 r
= qir_FMUL(c
, r
, rcp_ma
);
337 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
338 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
339 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
340 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
341 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
342 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
343 texture_u
[next_texture_u
++]);
346 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
350 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
354 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
356 if (is_txl
|| is_txb
)
357 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
359 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
361 c
->num_texture_samples
++;
362 struct qreg tex
= qir_TEX_RESULT(c
);
364 enum pipe_format format
= c
->key
->tex
[unit
].format
;
366 struct qreg unpacked
[4];
367 if (util_format_is_depth_or_stencil(format
)) {
368 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, tex
,
369 qir_uniform_ui(c
, 8)));
370 struct qreg normalized
= qir_FMUL(c
, depthf
,
371 qir_uniform_f(c
, 1.0f
/0xffffff));
373 struct qreg depth_output
;
375 struct qreg one
= qir_uniform_f(c
, 1.0f
);
376 if (c
->key
->tex
[unit
].compare_mode
) {
378 compare
= qir_FMUL(c
, compare
, proj
);
380 switch (c
->key
->tex
[unit
].compare_func
) {
381 case PIPE_FUNC_NEVER
:
382 depth_output
= qir_uniform_f(c
, 0.0f
);
384 case PIPE_FUNC_ALWAYS
:
387 case PIPE_FUNC_EQUAL
:
388 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
389 depth_output
= qir_SEL_X_0_ZS(c
, one
);
391 case PIPE_FUNC_NOTEQUAL
:
392 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
393 depth_output
= qir_SEL_X_0_ZC(c
, one
);
395 case PIPE_FUNC_GREATER
:
396 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
397 depth_output
= qir_SEL_X_0_NC(c
, one
);
399 case PIPE_FUNC_GEQUAL
:
400 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
401 depth_output
= qir_SEL_X_0_NS(c
, one
);
404 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
405 depth_output
= qir_SEL_X_0_NS(c
, one
);
407 case PIPE_FUNC_LEQUAL
:
408 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
409 depth_output
= qir_SEL_X_0_NC(c
, one
);
413 depth_output
= normalized
;
416 for (int i
= 0; i
< 4; i
++)
417 unpacked
[i
] = depth_output
;
419 for (int i
= 0; i
< 4; i
++)
420 unpacked
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
423 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
424 struct qreg texture_output
[4];
425 for (int i
= 0; i
< 4; i
++) {
426 texture_output
[i
] = get_swizzled_channel(c
, unpacked
,
430 if (util_format_is_srgb(format
)) {
431 for (int i
= 0; i
< 3; i
++)
432 texture_output
[i
] = qir_srgb_decode(c
,
436 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
);
437 for (int i
= 0; i
< 4; i
++) {
438 dest
[i
] = get_swizzled_channel(c
, texture_output
,
439 c
->key
->tex
[unit
].swizzle
[i
]);
444 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
448 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
450 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
451 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
453 return qir_SEL_X_Y_NS(c
,
454 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
459 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
463 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
465 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
467 /* This will be < 0 if we truncated and the truncation was of a value
468 * that was < 0 in the first place.
470 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
472 return qir_SEL_X_Y_NS(c
,
473 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
478 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
482 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
484 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
486 /* This will be < 0 if we truncated and the truncation was of a value
487 * that was > 0 in the first place.
489 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
491 return qir_SEL_X_Y_NS(c
,
492 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)),
497 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
501 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
502 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
503 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
504 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
507 struct qreg scaled_x
=
510 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
512 struct qreg x
= qir_FADD(c
,
513 ntq_ffract(c
, scaled_x
),
514 qir_uniform_f(c
, -0.5));
515 struct qreg x2
= qir_FMUL(c
, x
, x
);
516 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
517 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
518 x
= qir_FMUL(c
, x
, x2
);
523 qir_uniform_f(c
, coeff
[i
])));
529 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
533 pow(2.0 * M_PI
, 2) / (2 * 1),
534 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
535 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
536 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
537 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
540 struct qreg scaled_x
=
542 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
543 struct qreg x_frac
= qir_FADD(c
,
544 ntq_ffract(c
, scaled_x
),
545 qir_uniform_f(c
, -0.5));
547 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
548 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
549 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
550 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
552 x
= qir_FMUL(c
, x
, x2
);
554 struct qreg mul
= qir_FMUL(c
,
556 qir_uniform_f(c
, coeff
[i
]));
560 sum
= qir_FADD(c
, sum
, mul
);
566 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
569 return qir_SEL_X_Y_NC(c
,
570 qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0)),
571 qir_uniform_f(c
, -1.0));
575 get_channel_from_vpm(struct vc4_compile
*c
,
576 struct qreg
*vpm_reads
,
578 const struct util_format_description
*desc
)
580 const struct util_format_channel_description
*chan
=
581 &desc
->channel
[swiz
];
584 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
585 return get_swizzled_channel(c
, vpm_reads
, swiz
);
586 else if (chan
->size
== 32 &&
587 chan
->type
== UTIL_FORMAT_TYPE_FLOAT
) {
588 return get_swizzled_channel(c
, vpm_reads
, swiz
);
589 } else if (chan
->size
== 32 &&
590 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
591 if (chan
->normalized
) {
593 qir_ITOF(c
, vpm_reads
[swiz
]),
597 return qir_ITOF(c
, vpm_reads
[swiz
]);
599 } else if (chan
->size
== 8 &&
600 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
601 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
602 struct qreg vpm
= vpm_reads
[0];
603 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
604 temp
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
605 if (chan
->normalized
) {
606 return qir_FSUB(c
, qir_FMUL(c
,
607 qir_UNPACK_8_F(c
, temp
, swiz
),
608 qir_uniform_f(c
, 2.0)),
609 qir_uniform_f(c
, 1.0));
613 qir_UNPACK_8_I(c
, temp
,
615 qir_uniform_f(c
, -128.0));
618 if (chan
->normalized
) {
619 return qir_UNPACK_8_F(c
, vpm
, swiz
);
621 return qir_ITOF(c
, qir_UNPACK_8_I(c
, vpm
, swiz
));
624 } else if (chan
->size
== 16 &&
625 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
626 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
627 struct qreg vpm
= vpm_reads
[swiz
/ 2];
629 /* Note that UNPACK_16F eats a half float, not ints, so we use
630 * UNPACK_16_I for all of these.
632 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
633 temp
= qir_ITOF(c
, qir_UNPACK_16_I(c
, vpm
, swiz
% 2));
634 if (chan
->normalized
) {
635 return qir_FMUL(c
, temp
,
636 qir_uniform_f(c
, 1/32768.0f
));
641 /* UNPACK_16I sign-extends, so we have to emit ANDs. */
643 if (swiz
== 1 || swiz
== 3)
644 temp
= qir_UNPACK_16_I(c
, temp
, 1);
645 temp
= qir_AND(c
, temp
, qir_uniform_ui(c
, 0xffff));
646 temp
= qir_ITOF(c
, temp
);
648 if (chan
->normalized
) {
649 return qir_FMUL(c
, temp
,
650 qir_uniform_f(c
, 1 / 65535.0));
661 emit_vertex_input(struct vc4_compile
*c
, int attr
)
663 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
664 uint32_t attr_size
= util_format_get_blocksize(format
);
665 struct qreg vpm_reads
[4];
667 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
668 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
669 struct qreg vpm
= { QFILE_VPM
, attr
* 4 + i
};
670 vpm_reads
[i
] = qir_MOV(c
, vpm
);
674 bool format_warned
= false;
675 const struct util_format_description
*desc
=
676 util_format_description(format
);
678 for (int i
= 0; i
< 4; i
++) {
679 uint8_t swiz
= desc
->swizzle
[i
];
680 struct qreg result
= get_channel_from_vpm(c
, vpm_reads
,
683 if (result
.file
== QFILE_NULL
) {
684 if (!format_warned
) {
686 "vtx element %d unsupported type: %s\n",
687 attr
, util_format_name(format
));
688 format_warned
= true;
690 result
= qir_uniform_f(c
, 0.0);
692 c
->inputs
[attr
* 4 + i
] = result
;
697 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
699 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
700 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
701 c
->inputs
[attr
* 4 + 2] =
703 qir_ITOF(c
, qir_FRAG_Z(c
)),
704 qir_uniform_f(c
, 1.0 / 0xffffff));
705 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
709 emit_fragment_varying(struct vc4_compile
*c
, uint8_t semantic
,
710 uint8_t index
, uint8_t swizzle
)
712 uint32_t i
= c
->num_input_semantics
++;
718 if (c
->num_input_semantics
>= c
->input_semantics_array_size
) {
719 c
->input_semantics_array_size
=
720 MAX2(4, c
->input_semantics_array_size
* 2);
722 c
->input_semantics
= reralloc(c
, c
->input_semantics
,
723 struct vc4_varying_semantic
,
724 c
->input_semantics_array_size
);
727 c
->input_semantics
[i
].semantic
= semantic
;
728 c
->input_semantics
[i
].index
= index
;
729 c
->input_semantics
[i
].swizzle
= swizzle
;
731 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
735 emit_fragment_input(struct vc4_compile
*c
, int attr
,
736 unsigned semantic_name
, unsigned semantic_index
)
738 for (int i
= 0; i
< 4; i
++) {
739 c
->inputs
[attr
* 4 + i
] =
740 emit_fragment_varying(c
,
749 add_output(struct vc4_compile
*c
,
750 uint32_t decl_offset
,
751 uint8_t semantic_name
,
752 uint8_t semantic_index
,
753 uint8_t semantic_swizzle
)
755 uint32_t old_array_size
= c
->outputs_array_size
;
756 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
759 if (old_array_size
!= c
->outputs_array_size
) {
760 c
->output_semantics
= reralloc(c
,
762 struct vc4_varying_semantic
,
763 c
->outputs_array_size
);
766 c
->output_semantics
[decl_offset
].semantic
= semantic_name
;
767 c
->output_semantics
[decl_offset
].index
= semantic_index
;
768 c
->output_semantics
[decl_offset
].swizzle
= semantic_swizzle
;
772 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
774 unsigned array_id
= c
->num_uniform_ranges
++;
775 if (array_id
>= c
->ubo_ranges_array_size
) {
776 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
778 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
779 struct vc4_compiler_ubo_range
,
780 c
->ubo_ranges_array_size
);
783 c
->ubo_ranges
[array_id
].dst_offset
= 0;
784 c
->ubo_ranges
[array_id
].src_offset
= start
;
785 c
->ubo_ranges
[array_id
].size
= size
;
786 c
->ubo_ranges
[array_id
].used
= false;
790 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
792 /* Vectors are special in that they have non-scalarized writemasks,
793 * and just take the first swizzle channel for each argument in order
794 * into each writemask channel.
796 if (instr
->op
== nir_op_vec2
||
797 instr
->op
== nir_op_vec3
||
798 instr
->op
== nir_op_vec4
) {
800 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
801 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
802 instr
->src
[i
].swizzle
[0]);
803 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
804 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
809 /* General case: We can just grab the one used channel per src. */
810 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
811 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
812 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
815 /* Pick the channel to store the output in. */
816 assert(!instr
->dest
.saturate
);
817 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
818 assert(util_is_power_of_two(instr
->dest
.write_mask
));
819 dest
+= ffs(instr
->dest
.write_mask
) - 1;
824 *dest
= qir_MOV(c
, src
[0]);
827 *dest
= qir_FMUL(c
, src
[0], src
[1]);
830 *dest
= qir_FADD(c
, src
[0], src
[1]);
833 *dest
= qir_FSUB(c
, src
[0], src
[1]);
836 *dest
= qir_FMIN(c
, src
[0], src
[1]);
839 *dest
= qir_FMAX(c
, src
[0], src
[1]);
844 *dest
= qir_FTOI(c
, src
[0]);
848 *dest
= qir_ITOF(c
, src
[0]);
851 *dest
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
854 *dest
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
859 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
863 *dest
= qir_ADD(c
, src
[0], src
[1]);
866 *dest
= qir_SHR(c
, src
[0], src
[1]);
869 *dest
= qir_SUB(c
, src
[0], src
[1]);
872 *dest
= qir_ASR(c
, src
[0], src
[1]);
875 *dest
= qir_SHL(c
, src
[0], src
[1]);
878 *dest
= qir_MIN(c
, src
[0], src
[1]);
881 *dest
= qir_MAX(c
, src
[0], src
[1]);
884 *dest
= qir_AND(c
, src
[0], src
[1]);
887 *dest
= qir_OR(c
, src
[0], src
[1]);
890 *dest
= qir_XOR(c
, src
[0], src
[1]);
893 *dest
= qir_NOT(c
, src
[0]);
897 *dest
= ntq_umul(c
, src
[0], src
[1]);
901 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
902 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
905 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
906 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
909 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
910 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
913 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
914 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
917 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
918 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
921 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
922 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
925 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
926 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
929 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
930 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
933 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
934 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
937 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
938 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
941 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
942 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
945 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
946 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
951 *dest
= qir_SEL_X_Y_NS(c
, src
[1], src
[2]);
955 *dest
= qir_SEL_X_Y_ZC(c
, src
[1], src
[2]);
959 *dest
= ntq_rcp(c
, src
[0]);
962 *dest
= ntq_rsq(c
, src
[0]);
965 *dest
= qir_EXP2(c
, src
[0]);
968 *dest
= qir_LOG2(c
, src
[0]);
972 *dest
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
975 *dest
= ntq_fceil(c
, src
[0]);
978 *dest
= ntq_ffract(c
, src
[0]);
981 *dest
= ntq_ffloor(c
, src
[0]);
985 *dest
= ntq_fsin(c
, src
[0]);
988 *dest
= ntq_fcos(c
, src
[0]);
992 *dest
= ntq_fsign(c
, src
[0]);
996 *dest
= qir_FMAXABS(c
, src
[0], src
[0]);
999 *dest
= qir_MAX(c
, src
[0],
1000 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1004 fprintf(stderr
, "unknown NIR ALU inst: ");
1005 nir_print_instr(&instr
->instr
, stderr
);
1006 fprintf(stderr
, "\n");
1012 vc4_blend_channel(struct vc4_compile
*c
,
1020 case PIPE_BLENDFACTOR_ONE
:
1022 case PIPE_BLENDFACTOR_SRC_COLOR
:
1023 return qir_FMUL(c
, val
, src
[channel
]);
1024 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1025 return qir_FMUL(c
, val
, src
[3]);
1026 case PIPE_BLENDFACTOR_DST_ALPHA
:
1027 return qir_FMUL(c
, val
, dst
[3]);
1028 case PIPE_BLENDFACTOR_DST_COLOR
:
1029 return qir_FMUL(c
, val
, dst
[channel
]);
1030 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1037 qir_uniform_f(c
, 1.0),
1042 case PIPE_BLENDFACTOR_CONST_COLOR
:
1043 return qir_FMUL(c
, val
,
1044 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
,
1046 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1047 return qir_FMUL(c
, val
,
1048 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
, 3));
1049 case PIPE_BLENDFACTOR_ZERO
:
1050 return qir_uniform_f(c
, 0.0);
1051 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1052 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1054 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1055 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1057 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1058 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1060 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1061 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1063 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1064 return qir_FMUL(c
, val
,
1065 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1067 QUNIFORM_BLEND_CONST_COLOR
,
1069 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1070 return qir_FMUL(c
, val
,
1071 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1073 QUNIFORM_BLEND_CONST_COLOR
,
1077 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1078 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1079 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1080 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1082 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1088 vc4_blend_func(struct vc4_compile
*c
,
1089 struct qreg src
, struct qreg dst
,
1093 case PIPE_BLEND_ADD
:
1094 return qir_FADD(c
, src
, dst
);
1095 case PIPE_BLEND_SUBTRACT
:
1096 return qir_FSUB(c
, src
, dst
);
1097 case PIPE_BLEND_REVERSE_SUBTRACT
:
1098 return qir_FSUB(c
, dst
, src
);
1099 case PIPE_BLEND_MIN
:
1100 return qir_FMIN(c
, src
, dst
);
1101 case PIPE_BLEND_MAX
:
1102 return qir_FMAX(c
, src
, dst
);
1106 fprintf(stderr
, "Unknown blend func %d\n", func
);
1113 * Implements fixed function blending in shader code.
1115 * VC4 doesn't have any hardware support for blending. Instead, you read the
1116 * current contents of the destination from the tile buffer after having
1117 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1118 * math using your output color and that destination value, and update the
1119 * output color appropriately.
1122 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1123 struct qreg
*dst_color
, struct qreg
*src_color
)
1125 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1127 if (!blend
->blend_enable
) {
1128 for (int i
= 0; i
< 4; i
++)
1129 result
[i
] = src_color
[i
];
1133 for (int i
= 0; i
< 4; i
++)
1134 src_color
[i
] = qir_SAT(c
, src_color
[i
]);
1136 struct qreg src_blend
[4], dst_blend
[4];
1137 for (int i
= 0; i
< 3; i
++) {
1138 src_blend
[i
] = vc4_blend_channel(c
,
1139 dst_color
, src_color
,
1141 blend
->rgb_src_factor
, i
);
1142 dst_blend
[i
] = vc4_blend_channel(c
,
1143 dst_color
, src_color
,
1145 blend
->rgb_dst_factor
, i
);
1147 src_blend
[3] = vc4_blend_channel(c
,
1148 dst_color
, src_color
,
1150 blend
->alpha_src_factor
, 3);
1151 dst_blend
[3] = vc4_blend_channel(c
,
1152 dst_color
, src_color
,
1154 blend
->alpha_dst_factor
, 3);
1156 for (int i
= 0; i
< 3; i
++) {
1157 result
[i
] = vc4_blend_func(c
,
1158 src_blend
[i
], dst_blend
[i
],
1161 result
[3] = vc4_blend_func(c
,
1162 src_blend
[3], dst_blend
[3],
1167 clip_distance_discard(struct vc4_compile
*c
)
1169 for (int i
= 0; i
< PIPE_MAX_CLIP_PLANES
; i
++) {
1170 if (!(c
->key
->ucp_enables
& (1 << i
)))
1173 struct qreg dist
= emit_fragment_varying(c
,
1174 TGSI_SEMANTIC_CLIPDIST
,
1180 if (c
->discard
.file
== QFILE_NULL
)
1181 c
->discard
= qir_uniform_ui(c
, 0);
1183 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_ui(c
, ~0),
1189 alpha_test_discard(struct vc4_compile
*c
)
1191 struct qreg src_alpha
;
1192 struct qreg alpha_ref
= qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0);
1194 if (!c
->fs_key
->alpha_test
)
1197 if (c
->output_color_index
!= -1)
1198 src_alpha
= c
->outputs
[c
->output_color_index
+ 3];
1200 src_alpha
= qir_uniform_f(c
, 1.0);
1202 if (c
->discard
.file
== QFILE_NULL
)
1203 c
->discard
= qir_uniform_ui(c
, 0);
1205 switch (c
->fs_key
->alpha_test_func
) {
1206 case PIPE_FUNC_NEVER
:
1207 c
->discard
= qir_uniform_ui(c
, ~0);
1209 case PIPE_FUNC_ALWAYS
:
1211 case PIPE_FUNC_EQUAL
:
1212 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1213 c
->discard
= qir_SEL_X_Y_ZS(c
, c
->discard
,
1214 qir_uniform_ui(c
, ~0));
1216 case PIPE_FUNC_NOTEQUAL
:
1217 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1218 c
->discard
= qir_SEL_X_Y_ZC(c
, c
->discard
,
1219 qir_uniform_ui(c
, ~0));
1221 case PIPE_FUNC_GREATER
:
1222 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1223 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1224 qir_uniform_ui(c
, ~0));
1226 case PIPE_FUNC_GEQUAL
:
1227 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1228 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1229 qir_uniform_ui(c
, ~0));
1231 case PIPE_FUNC_LESS
:
1232 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1233 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1234 qir_uniform_ui(c
, ~0));
1236 case PIPE_FUNC_LEQUAL
:
1237 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1238 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1239 qir_uniform_ui(c
, ~0));
1245 vc4_logicop(struct vc4_compile
*c
, struct qreg src
, struct qreg dst
)
1247 switch (c
->fs_key
->logicop_func
) {
1248 case PIPE_LOGICOP_CLEAR
:
1249 return qir_uniform_f(c
, 0.0);
1250 case PIPE_LOGICOP_NOR
:
1251 return qir_NOT(c
, qir_OR(c
, src
, dst
));
1252 case PIPE_LOGICOP_AND_INVERTED
:
1253 return qir_AND(c
, qir_NOT(c
, src
), dst
);
1254 case PIPE_LOGICOP_COPY_INVERTED
:
1255 return qir_NOT(c
, src
);
1256 case PIPE_LOGICOP_AND_REVERSE
:
1257 return qir_AND(c
, src
, qir_NOT(c
, dst
));
1258 case PIPE_LOGICOP_INVERT
:
1259 return qir_NOT(c
, dst
);
1260 case PIPE_LOGICOP_XOR
:
1261 return qir_XOR(c
, src
, dst
);
1262 case PIPE_LOGICOP_NAND
:
1263 return qir_NOT(c
, qir_AND(c
, src
, dst
));
1264 case PIPE_LOGICOP_AND
:
1265 return qir_AND(c
, src
, dst
);
1266 case PIPE_LOGICOP_EQUIV
:
1267 return qir_NOT(c
, qir_XOR(c
, src
, dst
));
1268 case PIPE_LOGICOP_NOOP
:
1270 case PIPE_LOGICOP_OR_INVERTED
:
1271 return qir_OR(c
, qir_NOT(c
, src
), dst
);
1272 case PIPE_LOGICOP_OR_REVERSE
:
1273 return qir_OR(c
, src
, qir_NOT(c
, dst
));
1274 case PIPE_LOGICOP_OR
:
1275 return qir_OR(c
, src
, dst
);
1276 case PIPE_LOGICOP_SET
:
1277 return qir_uniform_ui(c
, ~0);
1278 case PIPE_LOGICOP_COPY
:
1285 * Applies the GL blending pipeline and returns the packed (8888) output
1289 blend_pipeline(struct vc4_compile
*c
)
1291 enum pipe_format color_format
= c
->fs_key
->color_format
;
1292 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1293 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1294 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1295 struct qreg linear_dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1296 struct qreg packed_dst_color
= c
->undef
;
1298 if (c
->fs_key
->blend
.blend_enable
||
1299 c
->fs_key
->blend
.colormask
!= 0xf ||
1300 c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1301 packed_dst_color
= qir_TLB_COLOR_READ(c
);
1302 for (int i
= 0; i
< 4; i
++)
1303 tlb_read_color
[i
] = qir_UNPACK_8_F(c
,
1304 packed_dst_color
, i
);
1305 for (int i
= 0; i
< 4; i
++) {
1306 dst_color
[i
] = get_swizzled_channel(c
,
1309 if (util_format_is_srgb(color_format
) && i
!= 3) {
1310 linear_dst_color
[i
] =
1311 qir_srgb_decode(c
, dst_color
[i
]);
1313 linear_dst_color
[i
] = dst_color
[i
];
1318 struct qreg undef_array
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1319 const struct qreg
*output_colors
= (c
->output_color_index
!= -1 ?
1320 c
->outputs
+ c
->output_color_index
:
1322 struct qreg blend_src_color
[4];
1323 for (int i
= 0; i
< 4; i
++)
1324 blend_src_color
[i
] = output_colors
[i
];
1326 struct qreg blend_color
[4];
1327 vc4_blend(c
, blend_color
, linear_dst_color
, blend_src_color
);
1329 if (util_format_is_srgb(color_format
)) {
1330 for (int i
= 0; i
< 3; i
++)
1331 blend_color
[i
] = qir_srgb_encode(c
, blend_color
[i
]);
1334 /* Debug: Sometimes you're getting a black output and just want to see
1335 * if the FS is getting executed at all. Spam magenta into the color
1339 blend_color
[0] = qir_uniform_f(c
, 1.0);
1340 blend_color
[1] = qir_uniform_f(c
, 0.0);
1341 blend_color
[2] = qir_uniform_f(c
, 1.0);
1342 blend_color
[3] = qir_uniform_f(c
, 0.5);
1345 struct qreg swizzled_outputs
[4];
1346 for (int i
= 0; i
< 4; i
++) {
1347 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1351 struct qreg packed_color
= c
->undef
;
1352 for (int i
= 0; i
< 4; i
++) {
1353 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1355 if (packed_color
.file
== QFILE_NULL
) {
1356 packed_color
= qir_PACK_8888_F(c
, swizzled_outputs
[i
]);
1358 packed_color
= qir_PACK_8_F(c
,
1360 swizzled_outputs
[i
],
1365 if (packed_color
.file
== QFILE_NULL
)
1366 packed_color
= qir_uniform_ui(c
, 0);
1368 if (c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1369 packed_color
= vc4_logicop(c
, packed_color
, packed_dst_color
);
1372 /* If the bit isn't set in the color mask, then just return the
1373 * original dst color, instead.
1375 uint32_t colormask
= 0xffffffff;
1376 for (int i
= 0; i
< 4; i
++) {
1377 if (format_swiz
[i
] < 4 &&
1378 !(c
->fs_key
->blend
.colormask
& (1 << format_swiz
[i
]))) {
1379 colormask
&= ~(0xff << (i
* 8));
1382 if (colormask
!= 0xffffffff) {
1383 packed_color
= qir_OR(c
,
1384 qir_AND(c
, packed_color
,
1385 qir_uniform_ui(c
, colormask
)),
1386 qir_AND(c
, packed_dst_color
,
1387 qir_uniform_ui(c
, ~colormask
)));
1390 return packed_color
;
1394 emit_frag_end(struct vc4_compile
*c
)
1396 clip_distance_discard(c
);
1397 alpha_test_discard(c
);
1398 struct qreg color
= blend_pipeline(c
);
1400 if (c
->discard
.file
!= QFILE_NULL
)
1401 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1403 if (c
->fs_key
->stencil_enabled
) {
1404 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1405 if (c
->fs_key
->stencil_twoside
) {
1406 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1408 if (c
->fs_key
->stencil_full_writemasks
) {
1409 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1413 if (c
->fs_key
->depth_enabled
) {
1415 if (c
->output_position_index
!= -1) {
1416 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1417 qir_uniform_f(c
, 0xffffff)));
1421 qir_TLB_Z_WRITE(c
, z
);
1424 qir_TLB_COLOR_WRITE(c
, color
);
1428 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1432 for (int i
= 0; i
< 2; i
++) {
1434 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1436 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1438 c
->outputs
[c
->output_position_index
+ i
],
1443 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1447 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1449 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1450 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1452 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1453 c
->outputs
[c
->output_position_index
+ 2],
1460 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1462 qir_VPM_WRITE(c
, rcp_w
);
1466 emit_point_size_write(struct vc4_compile
*c
)
1468 struct qreg point_size
;
1470 if (c
->output_point_size_index
!= -1)
1471 point_size
= c
->outputs
[c
->output_point_size_index
+ 3];
1473 point_size
= qir_uniform_f(c
, 1.0);
1475 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1478 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1480 qir_VPM_WRITE(c
, point_size
);
1484 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1486 * The simulator insists that there be at least one vertex attribute, so
1487 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1488 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1489 * to consume it here.
1492 emit_stub_vpm_read(struct vc4_compile
*c
)
1497 c
->vattr_sizes
[0] = 4;
1498 struct qreg vpm
= { QFILE_VPM
, 0 };
1499 (void)qir_MOV(c
, vpm
);
1504 emit_ucp_clipdistance(struct vc4_compile
*c
)
1507 if (c
->output_clipvertex_index
!= -1)
1508 cv
= c
->output_clipvertex_index
;
1509 else if (c
->output_position_index
!= -1)
1510 cv
= c
->output_position_index
;
1514 for (int plane
= 0; plane
< PIPE_MAX_CLIP_PLANES
; plane
++) {
1515 if (!(c
->key
->ucp_enables
& (1 << plane
)))
1518 /* Pick the next outputs[] that hasn't been written to, since
1519 * there are no other program writes left to be processed at
1520 * this point. If something had been declared but not written
1521 * (like a w component), we'll just smash over the top of it.
1523 uint32_t output_index
= c
->num_outputs
++;
1524 add_output(c
, output_index
,
1525 TGSI_SEMANTIC_CLIPDIST
,
1530 struct qreg dist
= qir_uniform_f(c
, 0.0);
1531 for (int i
= 0; i
< 4; i
++) {
1532 struct qreg pos_chan
= c
->outputs
[cv
+ i
];
1534 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1536 dist
= qir_FADD(c
, dist
, qir_FMUL(c
, pos_chan
, ucp
));
1539 c
->outputs
[output_index
] = dist
;
1544 emit_vert_end(struct vc4_compile
*c
,
1545 struct vc4_varying_semantic
*fs_inputs
,
1546 uint32_t num_fs_inputs
)
1548 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1550 emit_stub_vpm_read(c
);
1551 emit_ucp_clipdistance(c
);
1553 emit_scaled_viewport_write(c
, rcp_w
);
1554 emit_zs_write(c
, rcp_w
);
1555 emit_rcp_wc_write(c
, rcp_w
);
1556 if (c
->vs_key
->per_vertex_point_size
)
1557 emit_point_size_write(c
);
1559 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1560 struct vc4_varying_semantic
*input
= &fs_inputs
[i
];
1563 for (j
= 0; j
< c
->num_outputs
; j
++) {
1564 struct vc4_varying_semantic
*output
=
1565 &c
->output_semantics
[j
];
1567 if (input
->semantic
== output
->semantic
&&
1568 input
->index
== output
->index
&&
1569 input
->swizzle
== output
->swizzle
) {
1570 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1574 /* Emit padding if we didn't find a declared VS output for
1577 if (j
== c
->num_outputs
)
1578 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1583 emit_coord_end(struct vc4_compile
*c
)
1585 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1587 emit_stub_vpm_read(c
);
1589 for (int i
= 0; i
< 4; i
++)
1590 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1592 emit_scaled_viewport_write(c
, rcp_w
);
1593 emit_zs_write(c
, rcp_w
);
1594 emit_rcp_wc_write(c
, rcp_w
);
1595 if (c
->vs_key
->per_vertex_point_size
)
1596 emit_point_size_write(c
);
1600 vc4_optimize_nir(struct nir_shader
*s
)
1607 nir_lower_vars_to_ssa(s
);
1608 nir_lower_alu_to_scalar(s
);
1610 progress
= nir_copy_prop(s
) || progress
;
1611 progress
= nir_opt_dce(s
) || progress
;
1612 progress
= nir_opt_cse(s
) || progress
;
1613 progress
= nir_opt_peephole_select(s
) || progress
;
1614 progress
= nir_opt_algebraic(s
) || progress
;
1615 progress
= nir_opt_constant_folding(s
) || progress
;
1620 driver_location_compare(const void *in_a
, const void *in_b
)
1622 const nir_variable
*const *a
= in_a
;
1623 const nir_variable
*const *b
= in_b
;
1625 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1629 ntq_setup_inputs(struct vc4_compile
*c
)
1631 unsigned num_entries
= 0;
1632 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1635 nir_variable
*vars
[num_entries
];
1638 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1641 /* Sort the variables so that we emit the input setup in
1642 * driver_location order. This is required for VPM reads, whose data
1643 * is fetched into the VPM in driver_location (TGSI register index)
1646 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1648 for (unsigned i
= 0; i
< num_entries
; i
++) {
1649 nir_variable
*var
= vars
[i
];
1650 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1651 /* XXX: map loc slots to semantics */
1652 unsigned semantic_name
= var
->data
.location
;
1653 unsigned semantic_index
= var
->data
.index
;
1654 unsigned loc
= var
->data
.driver_location
;
1656 assert(array_len
== 1);
1658 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1661 if (c
->stage
== QSTAGE_FRAG
) {
1662 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1663 emit_fragcoord_input(c
, loc
);
1664 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1665 c
->inputs
[loc
* 4 + 0] = qir_FRAG_REV_FLAG(c
);
1666 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1667 (c
->fs_key
->point_sprite_mask
&
1668 (1 << semantic_index
))) {
1669 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1670 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1672 emit_fragment_input(c
, loc
,
1677 emit_vertex_input(c
, loc
);
1683 ntq_setup_outputs(struct vc4_compile
*c
)
1685 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->outputs
) {
1686 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1687 /* XXX: map loc slots to semantics */
1688 unsigned semantic_name
= var
->data
.location
;
1689 unsigned semantic_index
= var
->data
.index
;
1690 unsigned loc
= var
->data
.driver_location
* 4;
1692 assert(array_len
== 1);
1695 /* NIR hack to pass through
1696 * TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS */
1697 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1698 semantic_index
== -1)
1701 for (int i
= 0; i
< 4; i
++) {
1709 switch (semantic_name
) {
1710 case TGSI_SEMANTIC_POSITION
:
1711 c
->output_position_index
= loc
;
1713 case TGSI_SEMANTIC_CLIPVERTEX
:
1714 c
->output_clipvertex_index
= loc
;
1716 case TGSI_SEMANTIC_COLOR
:
1717 c
->output_color_index
= loc
;
1719 case TGSI_SEMANTIC_PSIZE
:
1720 c
->output_point_size_index
= loc
;
1728 ntq_setup_uniforms(struct vc4_compile
*c
)
1730 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->uniforms
) {
1731 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1732 unsigned array_elem_size
= 4 * sizeof(float);
1734 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1735 array_len
* array_elem_size
);
1741 * Sets up the mapping from nir_register to struct qreg *.
1743 * Each nir_register gets a struct qreg per 32-bit component being stored.
1746 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1748 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1749 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1750 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1752 nir_reg
->num_components
);
1754 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1756 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1757 qregs
[i
] = qir_uniform_ui(c
, 0);
1762 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1764 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1765 instr
->def
.num_components
);
1766 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1767 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u
[i
]);
1769 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1773 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1775 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1776 struct qreg
*dest
= NULL
;
1778 if (info
->has_dest
) {
1779 dest
= ntq_get_dest(c
, instr
->dest
);
1782 switch (instr
->intrinsic
) {
1783 case nir_intrinsic_load_uniform
:
1784 assert(instr
->num_components
== 1);
1785 *dest
= qir_uniform(c
, QUNIFORM_UNIFORM
, instr
->const_index
[0]);
1788 case nir_intrinsic_load_uniform_indirect
:
1789 *dest
= indirect_uniform_load(c
, instr
);
1793 case nir_intrinsic_load_input
:
1794 assert(instr
->num_components
== 1);
1795 *dest
= c
->inputs
[instr
->const_index
[0]];
1799 case nir_intrinsic_store_output
:
1800 assert(instr
->num_components
== 1);
1801 c
->outputs
[instr
->const_index
[0]] =
1802 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1803 c
->num_outputs
= MAX2(c
->num_outputs
, instr
->const_index
[0] + 1);
1806 case nir_intrinsic_discard
:
1807 c
->discard
= qir_uniform_ui(c
, ~0);
1810 case nir_intrinsic_discard_if
:
1811 if (c
->discard
.file
== QFILE_NULL
)
1812 c
->discard
= qir_uniform_ui(c
, 0);
1813 c
->discard
= qir_OR(c
, c
->discard
,
1814 ntq_get_src(c
, instr
->src
[0], 0));
1818 fprintf(stderr
, "Unknown intrinsic: ");
1819 nir_print_instr(&instr
->instr
, stderr
);
1820 fprintf(stderr
, "\n");
1826 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1828 fprintf(stderr
, "general IF statements not handled.\n");
1832 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1834 switch (instr
->type
) {
1835 case nir_instr_type_alu
:
1836 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1839 case nir_instr_type_intrinsic
:
1840 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1843 case nir_instr_type_load_const
:
1844 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1847 case nir_instr_type_tex
:
1848 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1852 fprintf(stderr
, "Unknown NIR instr type: ");
1853 nir_print_instr(instr
, stderr
);
1854 fprintf(stderr
, "\n");
1860 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1862 nir_foreach_instr(block
, instr
) {
1863 ntq_emit_instr(c
, instr
);
1868 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1870 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1871 switch (node
->type
) {
1872 /* case nir_cf_node_loop: */
1873 case nir_cf_node_block
:
1874 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1877 case nir_cf_node_if
:
1878 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1888 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1890 ntq_setup_registers(c
, &impl
->registers
);
1891 ntq_emit_cf_list(c
, &impl
->body
);
1895 nir_to_qir(struct vc4_compile
*c
)
1897 ntq_setup_inputs(c
);
1898 ntq_setup_outputs(c
);
1899 ntq_setup_uniforms(c
);
1900 ntq_setup_registers(c
, &c
->s
->registers
);
1902 /* Find the main function and emit the body. */
1903 nir_foreach_overload(c
->s
, overload
) {
1904 assert(strcmp(overload
->function
->name
, "main") == 0);
1905 assert(overload
->impl
);
1906 ntq_emit_impl(c
, overload
->impl
);
1910 static const nir_shader_compiler_options nir_options
= {
1915 .lower_fsqrt
= true,
1916 .lower_negate
= true,
1920 count_nir_instrs_in_block(nir_block
*block
, void *state
)
1922 int *count
= (int *) state
;
1923 nir_foreach_instr(block
, instr
) {
1924 *count
= *count
+ 1;
1930 count_nir_instrs(nir_shader
*nir
)
1933 nir_foreach_overload(nir
, overload
) {
1934 if (!overload
->impl
)
1936 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
1941 static struct vc4_compile
*
1942 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
1943 struct vc4_key
*key
)
1945 struct vc4_compile
*c
= qir_compile_init();
1948 c
->shader_state
= &key
->shader_state
->base
;
1949 c
->program_id
= key
->shader_state
->program_id
;
1950 c
->variant_id
= key
->shader_state
->compiled_variant_count
++;
1955 c
->fs_key
= (struct vc4_fs_key
*)key
;
1956 if (c
->fs_key
->is_points
) {
1957 c
->point_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1958 c
->point_y
= emit_fragment_varying(c
, ~0, ~0, 0);
1959 } else if (c
->fs_key
->is_lines
) {
1960 c
->line_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1964 c
->vs_key
= (struct vc4_vs_key
*)key
;
1967 c
->vs_key
= (struct vc4_vs_key
*)key
;
1971 const struct tgsi_token
*tokens
= key
->shader_state
->base
.tokens
;
1972 if (c
->fs_key
&& c
->fs_key
->light_twoside
) {
1973 if (!key
->shader_state
->twoside_tokens
) {
1974 const struct tgsi_lowering_config lowering_config
= {
1975 .color_two_side
= true,
1977 struct tgsi_shader_info info
;
1978 key
->shader_state
->twoside_tokens
=
1979 tgsi_transform_lowering(&lowering_config
,
1980 key
->shader_state
->base
.tokens
,
1983 /* If no transformation occurred, then NULL is
1984 * returned and we just use our original tokens.
1986 if (!key
->shader_state
->twoside_tokens
) {
1987 key
->shader_state
->twoside_tokens
=
1988 key
->shader_state
->base
.tokens
;
1991 tokens
= key
->shader_state
->twoside_tokens
;
1994 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1995 fprintf(stderr
, "%s prog %d/%d TGSI:\n",
1996 qir_get_stage_name(c
->stage
),
1997 c
->program_id
, c
->variant_id
);
1998 tgsi_dump(tokens
, 0);
2001 c
->s
= tgsi_to_nir(tokens
, &nir_options
);
2002 nir_opt_global_to_local(c
->s
);
2003 nir_convert_to_ssa(c
->s
);
2004 vc4_nir_lower_io(c
);
2005 nir_lower_idiv(c
->s
);
2007 vc4_optimize_nir(c
->s
);
2009 nir_remove_dead_variables(c
->s
);
2011 nir_convert_from_ssa(c
->s
, false);
2013 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2014 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2015 qir_get_stage_name(c
->stage
),
2016 c
->program_id
, c
->variant_id
,
2017 count_nir_instrs(c
->s
));
2020 if (vc4_debug
& VC4_DEBUG_NIR
) {
2021 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2022 qir_get_stage_name(c
->stage
),
2023 c
->program_id
, c
->variant_id
);
2024 nir_print_shader(c
->s
, stderr
);
2035 vc4
->prog
.fs
->input_semantics
,
2036 vc4
->prog
.fs
->num_inputs
);
2043 if (vc4_debug
& VC4_DEBUG_QIR
) {
2044 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2045 qir_get_stage_name(c
->stage
),
2046 c
->program_id
, c
->variant_id
);
2051 qir_lower_uniforms(c
);
2053 if (vc4_debug
& VC4_DEBUG_QIR
) {
2054 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2055 qir_get_stage_name(c
->stage
),
2056 c
->program_id
, c
->variant_id
);
2059 qir_reorder_uniforms(c
);
2060 vc4_generate_code(vc4
, c
);
2062 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2063 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2064 qir_get_stage_name(c
->stage
),
2065 c
->program_id
, c
->variant_id
,
2067 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2068 qir_get_stage_name(c
->stage
),
2069 c
->program_id
, c
->variant_id
,
2079 vc4_shader_state_create(struct pipe_context
*pctx
,
2080 const struct pipe_shader_state
*cso
)
2082 struct vc4_context
*vc4
= vc4_context(pctx
);
2083 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2087 so
->base
.tokens
= tgsi_dup_tokens(cso
->tokens
);
2088 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2094 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2095 struct vc4_compile
*c
)
2097 int count
= c
->num_uniforms
;
2098 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2100 uinfo
->count
= count
;
2101 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2102 memcpy(uinfo
->data
, c
->uniform_data
,
2103 count
* sizeof(*uinfo
->data
));
2104 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2105 memcpy(uinfo
->contents
, c
->uniform_contents
,
2106 count
* sizeof(*uinfo
->contents
));
2107 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2109 vc4_set_shader_uniform_dirty_flags(shader
);
2112 static struct vc4_compiled_shader
*
2113 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2114 struct vc4_key
*key
)
2116 struct hash_table
*ht
;
2118 if (stage
== QSTAGE_FRAG
) {
2120 key_size
= sizeof(struct vc4_fs_key
);
2123 key_size
= sizeof(struct vc4_vs_key
);
2126 struct vc4_compiled_shader
*shader
;
2127 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2131 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2132 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2134 shader
->program_id
= vc4
->next_compiled_program_id
++;
2135 if (stage
== QSTAGE_FRAG
) {
2136 bool input_live
[c
->num_input_semantics
];
2138 memset(input_live
, 0, sizeof(input_live
));
2139 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
2140 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2141 if (inst
->src
[i
].file
== QFILE_VARY
)
2142 input_live
[inst
->src
[i
].index
] = true;
2146 shader
->input_semantics
= ralloc_array(shader
,
2147 struct vc4_varying_semantic
,
2148 c
->num_input_semantics
);
2150 for (int i
= 0; i
< c
->num_input_semantics
; i
++) {
2151 struct vc4_varying_semantic
*sem
= &c
->input_semantics
[i
];
2156 /* Skip non-VS-output inputs. */
2157 if (sem
->semantic
== (uint8_t)~0)
2160 if (sem
->semantic
== TGSI_SEMANTIC_COLOR
||
2161 sem
->semantic
== TGSI_SEMANTIC_BCOLOR
) {
2162 shader
->color_inputs
|= (1 << shader
->num_inputs
);
2165 shader
->input_semantics
[shader
->num_inputs
] = *sem
;
2166 shader
->num_inputs
++;
2169 shader
->num_inputs
= c
->num_inputs
;
2171 shader
->vattr_offsets
[0] = 0;
2172 for (int i
= 0; i
< 8; i
++) {
2173 shader
->vattr_offsets
[i
+ 1] =
2174 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2176 if (c
->vattr_sizes
[i
])
2177 shader
->vattrs_live
|= (1 << i
);
2181 copy_uniform_state_to_shader(shader
, c
);
2182 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2183 c
->qpu_inst_count
* sizeof(uint64_t));
2185 /* Copy the compiler UBO range state to the compiled shader, dropping
2186 * out arrays that were never referenced by an indirect load.
2188 * (Note that QIR dead code elimination of an array access still
2189 * leaves that array alive, though)
2191 if (c
->num_ubo_ranges
) {
2192 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2193 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2196 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2197 struct vc4_compiler_ubo_range
*range
=
2202 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2203 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2204 shader
->ubo_ranges
[j
].size
= range
->size
;
2205 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2209 if (shader
->ubo_size
) {
2210 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2211 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2212 qir_get_stage_name(c
->stage
),
2213 c
->program_id
, c
->variant_id
,
2214 shader
->ubo_size
/ 4);
2218 qir_compile_destroy(c
);
2220 struct vc4_key
*dup_key
;
2221 dup_key
= ralloc_size(shader
, key_size
);
2222 memcpy(dup_key
, key
, key_size
);
2223 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2229 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2230 struct vc4_texture_stateobj
*texstate
)
2232 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2233 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2234 struct pipe_sampler_state
*sampler_state
=
2235 texstate
->samplers
[i
];
2238 key
->tex
[i
].format
= sampler
->format
;
2239 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2240 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2241 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2242 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2243 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2244 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2245 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2246 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2250 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2254 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2256 struct vc4_fs_key local_key
;
2257 struct vc4_fs_key
*key
= &local_key
;
2259 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2261 VC4_DIRTY_FRAMEBUFFER
|
2263 VC4_DIRTY_RASTERIZER
|
2265 VC4_DIRTY_TEXSTATE
|
2266 VC4_DIRTY_UNCOMPILED_FS
))) {
2270 memset(key
, 0, sizeof(*key
));
2271 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2272 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2273 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2274 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2275 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2276 key
->blend
= vc4
->blend
->rt
[0];
2277 if (vc4
->blend
->logicop_enable
) {
2278 key
->logicop_func
= vc4
->blend
->logicop_func
;
2280 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2282 if (vc4
->framebuffer
.cbufs
[0])
2283 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2285 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2286 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2287 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2288 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2289 key
->stencil_enabled
);
2290 if (vc4
->zsa
->base
.alpha
.enabled
) {
2291 key
->alpha_test
= true;
2292 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2295 if (key
->is_points
) {
2296 key
->point_sprite_mask
=
2297 vc4
->rasterizer
->base
.sprite_coord_enable
;
2298 key
->point_coord_upper_left
=
2299 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2300 PIPE_SPRITE_COORD_UPPER_LEFT
);
2303 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2305 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2306 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2307 if (vc4
->prog
.fs
== old_fs
)
2310 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2311 if (vc4
->rasterizer
->base
.flatshade
&&
2312 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2313 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2318 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2320 struct vc4_vs_key local_key
;
2321 struct vc4_vs_key
*key
= &local_key
;
2323 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2324 VC4_DIRTY_RASTERIZER
|
2326 VC4_DIRTY_TEXSTATE
|
2327 VC4_DIRTY_VTXSTATE
|
2328 VC4_DIRTY_UNCOMPILED_VS
|
2329 VC4_DIRTY_COMPILED_FS
))) {
2333 memset(key
, 0, sizeof(*key
));
2334 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2335 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2336 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2338 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2339 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2341 key
->per_vertex_point_size
=
2342 (prim_mode
== PIPE_PRIM_POINTS
&&
2343 vc4
->rasterizer
->base
.point_size_per_vertex
);
2345 struct vc4_compiled_shader
*vs
=
2346 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2347 if (vs
!= vc4
->prog
.vs
) {
2349 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2352 key
->is_coord
= true;
2353 struct vc4_compiled_shader
*cs
=
2354 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2355 if (cs
!= vc4
->prog
.cs
) {
2357 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2362 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2364 vc4_update_compiled_fs(vc4
, prim_mode
);
2365 vc4_update_compiled_vs(vc4
, prim_mode
);
2369 fs_cache_hash(const void *key
)
2371 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2375 vs_cache_hash(const void *key
)
2377 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2381 fs_cache_compare(const void *key1
, const void *key2
)
2383 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2387 vs_cache_compare(const void *key1
, const void *key2
)
2389 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2393 delete_from_cache_if_matches(struct hash_table
*ht
,
2394 struct hash_entry
*entry
,
2395 struct vc4_uncompiled_shader
*so
)
2397 const struct vc4_key
*key
= entry
->key
;
2399 if (key
->shader_state
== so
) {
2400 struct vc4_compiled_shader
*shader
= entry
->data
;
2401 _mesa_hash_table_remove(ht
, entry
);
2402 vc4_bo_unreference(&shader
->bo
);
2403 ralloc_free(shader
);
2408 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2410 struct vc4_context
*vc4
= vc4_context(pctx
);
2411 struct vc4_uncompiled_shader
*so
= hwcso
;
2413 struct hash_entry
*entry
;
2414 hash_table_foreach(vc4
->fs_cache
, entry
)
2415 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2416 hash_table_foreach(vc4
->vs_cache
, entry
)
2417 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2419 if (so
->twoside_tokens
!= so
->base
.tokens
)
2420 free((void *)so
->twoside_tokens
);
2421 free((void *)so
->base
.tokens
);
2426 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2428 struct vc4_context
*vc4
= vc4_context(pctx
);
2429 vc4
->prog
.bind_fs
= hwcso
;
2430 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2434 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2436 struct vc4_context
*vc4
= vc4_context(pctx
);
2437 vc4
->prog
.bind_vs
= hwcso
;
2438 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2442 vc4_program_init(struct pipe_context
*pctx
)
2444 struct vc4_context
*vc4
= vc4_context(pctx
);
2446 pctx
->create_vs_state
= vc4_shader_state_create
;
2447 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2449 pctx
->create_fs_state
= vc4_shader_state_create
;
2450 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2452 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2453 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2455 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2457 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2462 vc4_program_fini(struct pipe_context
*pctx
)
2464 struct vc4_context
*vc4
= vc4_context(pctx
);
2466 struct hash_entry
*entry
;
2467 hash_table_foreach(vc4
->fs_cache
, entry
) {
2468 struct vc4_compiled_shader
*shader
= entry
->data
;
2469 vc4_bo_unreference(&shader
->bo
);
2470 ralloc_free(shader
);
2471 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2474 hash_table_foreach(vc4
->vs_cache
, entry
) {
2475 struct vc4_compiled_shader
*shader
= entry
->data
;
2476 vc4_bo_unreference(&shader
->bo
);
2477 ralloc_free(shader
);
2478 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);