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 resize_qreg_array(struct vc4_compile
*c
,
51 if (*size
>= decl_size
)
54 uint32_t old_size
= *size
;
55 *size
= MAX2(*size
* 2, decl_size
);
56 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
58 fprintf(stderr
, "Malloc failure\n");
62 for (uint32_t i
= old_size
; i
< *size
; i
++)
63 (*regs
)[i
] = c
->undef
;
67 indirect_uniform_load(struct vc4_compile
*c
,
68 struct qreg indirect_offset
,
71 struct vc4_compiler_ubo_range
*range
= NULL
;
73 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
74 range
= &c
->ubo_ranges
[i
];
75 if (offset
>= range
->src_offset
&&
76 offset
< range
->src_offset
+ range
->size
) {
80 /* The driver-location-based offset always has to be within a declared
86 range
->dst_offset
= c
->next_ubo_dst_offset
;
87 c
->next_ubo_dst_offset
+= range
->size
;
91 offset
-= range
->src_offset
;
92 /* Translate the user's TGSI register index from the TGSI register
93 * base to a byte offset.
95 indirect_offset
= qir_SHL(c
, indirect_offset
, qir_uniform_ui(c
, 4));
97 /* Adjust for where we stored the TGSI register base. */
98 indirect_offset
= qir_ADD(c
, indirect_offset
,
99 qir_uniform_ui(c
, (range
->dst_offset
+
102 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
103 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
104 indirect_offset
= qir_MIN(c
, indirect_offset
,
105 qir_uniform_ui(c
, (range
->dst_offset
+
108 qir_TEX_DIRECT(c
, indirect_offset
, qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
109 struct qreg r4
= qir_TEX_RESULT(c
);
110 c
->num_texture_samples
++;
111 return qir_MOV(c
, r4
);
115 ntq_get_dest(struct vc4_compile
*c
, nir_dest dest
)
117 assert(!dest
.is_ssa
);
118 nir_register
*reg
= dest
.reg
.reg
;
119 struct hash_entry
*entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
120 assert(reg
->num_array_elems
== 0);
121 assert(dest
.reg
.base_offset
== 0);
123 struct qreg
*qregs
= entry
->data
;
128 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
130 struct hash_entry
*entry
;
132 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
133 assert(i
< src
.ssa
->num_components
);
135 nir_register
*reg
= src
.reg
.reg
;
136 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
137 assert(reg
->num_array_elems
== 0);
138 assert(src
.reg
.base_offset
== 0);
139 assert(i
< reg
->num_components
);
142 struct qreg
*qregs
= entry
->data
;
147 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
150 assert(util_is_power_of_two(instr
->dest
.write_mask
));
151 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
152 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
153 instr
->src
[src
].swizzle
[chan
]);
155 assert(!instr
->src
[src
].abs
);
156 assert(!instr
->src
[src
].negate
);
162 get_swizzled_channel(struct vc4_compile
*c
,
163 struct qreg
*srcs
, int swiz
)
167 case UTIL_FORMAT_SWIZZLE_NONE
:
168 fprintf(stderr
, "warning: unknown swizzle\n");
170 case UTIL_FORMAT_SWIZZLE_0
:
171 return qir_uniform_f(c
, 0.0);
172 case UTIL_FORMAT_SWIZZLE_1
:
173 return qir_uniform_f(c
, 1.0);
174 case UTIL_FORMAT_SWIZZLE_X
:
175 case UTIL_FORMAT_SWIZZLE_Y
:
176 case UTIL_FORMAT_SWIZZLE_Z
:
177 case UTIL_FORMAT_SWIZZLE_W
:
182 static inline struct qreg
183 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
186 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
187 qir_uniform_f(c
, 0.0));
191 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
193 struct qreg r
= qir_RCP(c
, x
);
195 /* Apply a Newton-Raphson step to improve the accuracy. */
196 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
197 qir_uniform_f(c
, 2.0),
204 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
206 struct qreg r
= qir_RSQ(c
, x
);
208 /* Apply a Newton-Raphson step to improve the accuracy. */
209 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
210 qir_uniform_f(c
, 1.5),
212 qir_uniform_f(c
, 0.5),
214 qir_FMUL(c
, r
, r
)))));
220 qir_srgb_decode(struct vc4_compile
*c
, struct qreg srgb
)
222 struct qreg low
= qir_FMUL(c
, srgb
, qir_uniform_f(c
, 1.0 / 12.92));
223 struct qreg high
= qir_POW(c
,
227 qir_uniform_f(c
, 0.055)),
228 qir_uniform_f(c
, 1.0 / 1.055)),
229 qir_uniform_f(c
, 2.4));
231 qir_SF(c
, qir_FSUB(c
, srgb
, qir_uniform_f(c
, 0.04045)));
232 return qir_SEL_X_Y_NS(c
, low
, high
);
236 qir_srgb_encode(struct vc4_compile
*c
, struct qreg linear
)
238 struct qreg low
= qir_FMUL(c
, linear
, qir_uniform_f(c
, 12.92));
239 struct qreg high
= qir_FSUB(c
,
241 qir_uniform_f(c
, 1.055),
244 qir_uniform_f(c
, 0.41666))),
245 qir_uniform_f(c
, 0.055));
247 qir_SF(c
, qir_FSUB(c
, linear
, qir_uniform_f(c
, 0.0031308)));
248 return qir_SEL_X_Y_NS(c
, low
, high
);
252 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
254 struct qreg src0_hi
= qir_SHR(c
, src0
,
255 qir_uniform_ui(c
, 24));
256 struct qreg src1_hi
= qir_SHR(c
, src1
,
257 qir_uniform_ui(c
, 24));
259 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
260 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
261 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
263 return qir_ADD(c
, lolo
, qir_SHL(c
,
264 qir_ADD(c
, hilo
, lohi
),
265 qir_uniform_ui(c
, 24)));
269 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
271 struct qreg s
, t
, r
, lod
, proj
, compare
;
272 bool is_txb
= false, is_txl
= false, has_proj
= false;
273 unsigned unit
= instr
->sampler_index
;
275 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
276 switch (instr
->src
[i
].src_type
) {
277 case nir_tex_src_coord
:
278 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
279 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
280 t
= qir_uniform_f(c
, 0.5);
282 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
283 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
284 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
286 case nir_tex_src_bias
:
287 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
290 case nir_tex_src_lod
:
291 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
294 case nir_tex_src_comparitor
:
295 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
297 case nir_tex_src_projector
:
298 proj
= qir_RCP(c
, ntq_get_src(c
, instr
->src
[i
].src
, 0));
299 s
= qir_FMUL(c
, s
, proj
);
300 t
= qir_FMUL(c
, t
, proj
);
304 unreachable("unknown texture source");
308 struct qreg texture_u
[] = {
309 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
310 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
311 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
312 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
314 uint32_t next_texture_u
= 0;
316 /* There is no native support for GL texture rectangle coordinates, so
317 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
320 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
322 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
324 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
327 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
328 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
329 unit
| (is_txl
<< 16));
332 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
333 struct qreg ma
= qir_FMAXABS(c
, qir_FMAXABS(c
, s
, t
), r
);
334 struct qreg rcp_ma
= qir_RCP(c
, ma
);
335 s
= qir_FMUL(c
, s
, rcp_ma
);
336 t
= qir_FMUL(c
, t
, rcp_ma
);
337 r
= qir_FMUL(c
, r
, rcp_ma
);
339 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
340 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
341 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
342 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
343 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
344 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
345 texture_u
[next_texture_u
++]);
348 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
352 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
356 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
358 if (is_txl
|| is_txb
)
359 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
361 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
363 c
->num_texture_samples
++;
364 struct qreg r4
= qir_TEX_RESULT(c
);
366 enum pipe_format format
= c
->key
->tex
[unit
].format
;
368 struct qreg unpacked
[4];
369 if (util_format_is_depth_or_stencil(format
)) {
370 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, r4
,
371 qir_uniform_ui(c
, 8)));
372 struct qreg normalized
= qir_FMUL(c
, depthf
,
373 qir_uniform_f(c
, 1.0f
/0xffffff));
375 struct qreg depth_output
;
377 struct qreg one
= qir_uniform_f(c
, 1.0f
);
378 if (c
->key
->tex
[unit
].compare_mode
) {
380 compare
= qir_FMUL(c
, compare
, proj
);
382 switch (c
->key
->tex
[unit
].compare_func
) {
383 case PIPE_FUNC_NEVER
:
384 depth_output
= qir_uniform_f(c
, 0.0f
);
386 case PIPE_FUNC_ALWAYS
:
389 case PIPE_FUNC_EQUAL
:
390 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
391 depth_output
= qir_SEL_X_0_ZS(c
, one
);
393 case PIPE_FUNC_NOTEQUAL
:
394 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
395 depth_output
= qir_SEL_X_0_ZC(c
, one
);
397 case PIPE_FUNC_GREATER
:
398 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
399 depth_output
= qir_SEL_X_0_NC(c
, one
);
401 case PIPE_FUNC_GEQUAL
:
402 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
403 depth_output
= qir_SEL_X_0_NS(c
, one
);
406 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
407 depth_output
= qir_SEL_X_0_NS(c
, one
);
409 case PIPE_FUNC_LEQUAL
:
410 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
411 depth_output
= qir_SEL_X_0_NC(c
, one
);
415 depth_output
= normalized
;
418 for (int i
= 0; i
< 4; i
++)
419 unpacked
[i
] = depth_output
;
421 for (int i
= 0; i
< 4; i
++)
422 unpacked
[i
] = qir_R4_UNPACK(c
, r4
, i
);
425 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
426 struct qreg texture_output
[4];
427 for (int i
= 0; i
< 4; i
++) {
428 texture_output
[i
] = get_swizzled_channel(c
, unpacked
,
432 if (util_format_is_srgb(format
)) {
433 for (int i
= 0; i
< 3; i
++)
434 texture_output
[i
] = qir_srgb_decode(c
,
438 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
);
439 for (int i
= 0; i
< 4; i
++) {
440 dest
[i
] = get_swizzled_channel(c
, texture_output
,
441 c
->key
->tex
[unit
].swizzle
[i
]);
446 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
450 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
452 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
453 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
455 return qir_SEL_X_Y_NS(c
,
456 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
461 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
465 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
467 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
469 /* This will be < 0 if we truncated and the truncation was of a value
470 * that was < 0 in the first place.
472 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
474 return qir_SEL_X_Y_NS(c
,
475 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
480 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
484 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
486 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
488 /* This will be < 0 if we truncated and the truncation was of a value
489 * that was > 0 in the first place.
491 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
493 return qir_SEL_X_Y_NS(c
,
494 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)),
499 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
503 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
504 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
505 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
506 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
509 struct qreg scaled_x
=
512 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
514 struct qreg x
= qir_FADD(c
,
515 ntq_ffract(c
, scaled_x
),
516 qir_uniform_f(c
, -0.5));
517 struct qreg x2
= qir_FMUL(c
, x
, x
);
518 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
519 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
520 x
= qir_FMUL(c
, x
, x2
);
525 qir_uniform_f(c
, coeff
[i
])));
531 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
535 pow(2.0 * M_PI
, 2) / (2 * 1),
536 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
537 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
538 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
539 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
542 struct qreg scaled_x
=
544 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
545 struct qreg x_frac
= qir_FADD(c
,
546 ntq_ffract(c
, scaled_x
),
547 qir_uniform_f(c
, -0.5));
549 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
550 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
551 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
552 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
554 x
= qir_FMUL(c
, x
, x2
);
556 struct qreg mul
= qir_FMUL(c
,
558 qir_uniform_f(c
, coeff
[i
]));
562 sum
= qir_FADD(c
, sum
, mul
);
568 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
571 return qir_SEL_X_Y_NC(c
,
572 qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0)),
573 qir_uniform_f(c
, -1.0));
577 get_channel_from_vpm(struct vc4_compile
*c
,
578 struct qreg
*vpm_reads
,
580 const struct util_format_description
*desc
)
582 const struct util_format_channel_description
*chan
=
583 &desc
->channel
[swiz
];
586 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
587 return get_swizzled_channel(c
, vpm_reads
, swiz
);
588 else if (chan
->size
== 32 &&
589 chan
->type
== UTIL_FORMAT_TYPE_FLOAT
) {
590 return get_swizzled_channel(c
, vpm_reads
, swiz
);
591 } else if (chan
->size
== 32 &&
592 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
593 if (chan
->normalized
) {
595 qir_ITOF(c
, vpm_reads
[swiz
]),
599 return qir_ITOF(c
, vpm_reads
[swiz
]);
601 } else if (chan
->size
== 8 &&
602 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
603 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
604 struct qreg vpm
= vpm_reads
[0];
605 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
606 temp
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
607 if (chan
->normalized
) {
608 return qir_FSUB(c
, qir_FMUL(c
,
609 qir_UNPACK_8_F(c
, temp
, swiz
),
610 qir_uniform_f(c
, 2.0)),
611 qir_uniform_f(c
, 1.0));
615 qir_UNPACK_8_I(c
, temp
,
617 qir_uniform_f(c
, -128.0));
620 if (chan
->normalized
) {
621 return qir_UNPACK_8_F(c
, vpm
, swiz
);
623 return qir_ITOF(c
, qir_UNPACK_8_I(c
, vpm
, swiz
));
626 } else if (chan
->size
== 16 &&
627 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
628 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
629 struct qreg vpm
= vpm_reads
[swiz
/ 2];
631 /* Note that UNPACK_16F eats a half float, not ints, so we use
632 * UNPACK_16_I for all of these.
634 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
635 temp
= qir_ITOF(c
, qir_UNPACK_16_I(c
, vpm
, swiz
% 2));
636 if (chan
->normalized
) {
637 return qir_FMUL(c
, temp
,
638 qir_uniform_f(c
, 1/32768.0f
));
643 /* UNPACK_16I sign-extends, so we have to emit ANDs. */
645 if (swiz
== 1 || swiz
== 3)
646 temp
= qir_UNPACK_16_I(c
, temp
, 1);
647 temp
= qir_AND(c
, temp
, qir_uniform_ui(c
, 0xffff));
648 temp
= qir_ITOF(c
, temp
);
650 if (chan
->normalized
) {
651 return qir_FMUL(c
, temp
,
652 qir_uniform_f(c
, 1 / 65535.0));
663 emit_vertex_input(struct vc4_compile
*c
, int attr
)
665 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
666 uint32_t attr_size
= util_format_get_blocksize(format
);
667 struct qreg vpm_reads
[4];
669 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
670 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
671 struct qreg vpm
= { QFILE_VPM
, attr
* 4 + i
};
672 vpm_reads
[i
] = qir_MOV(c
, vpm
);
676 bool format_warned
= false;
677 const struct util_format_description
*desc
=
678 util_format_description(format
);
680 for (int i
= 0; i
< 4; i
++) {
681 uint8_t swiz
= desc
->swizzle
[i
];
682 struct qreg result
= get_channel_from_vpm(c
, vpm_reads
,
685 if (result
.file
== QFILE_NULL
) {
686 if (!format_warned
) {
688 "vtx element %d unsupported type: %s\n",
689 attr
, util_format_name(format
));
690 format_warned
= true;
692 result
= qir_uniform_f(c
, 0.0);
694 c
->inputs
[attr
* 4 + i
] = result
;
699 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
701 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
702 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
703 c
->inputs
[attr
* 4 + 2] =
705 qir_ITOF(c
, qir_FRAG_Z(c
)),
706 qir_uniform_f(c
, 1.0 / 0xffffff));
707 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
711 emit_fragment_varying(struct vc4_compile
*c
, uint8_t semantic
,
712 uint8_t index
, uint8_t swizzle
)
714 uint32_t i
= c
->num_input_semantics
++;
720 if (c
->num_input_semantics
>= c
->input_semantics_array_size
) {
721 c
->input_semantics_array_size
=
722 MAX2(4, c
->input_semantics_array_size
* 2);
724 c
->input_semantics
= reralloc(c
, c
->input_semantics
,
725 struct vc4_varying_semantic
,
726 c
->input_semantics_array_size
);
729 c
->input_semantics
[i
].semantic
= semantic
;
730 c
->input_semantics
[i
].index
= index
;
731 c
->input_semantics
[i
].swizzle
= swizzle
;
733 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
737 emit_fragment_input(struct vc4_compile
*c
, int attr
,
738 unsigned semantic_name
, unsigned semantic_index
)
740 for (int i
= 0; i
< 4; i
++) {
741 c
->inputs
[attr
* 4 + i
] =
742 emit_fragment_varying(c
,
751 add_output(struct vc4_compile
*c
,
752 uint32_t decl_offset
,
753 uint8_t semantic_name
,
754 uint8_t semantic_index
,
755 uint8_t semantic_swizzle
)
757 uint32_t old_array_size
= c
->outputs_array_size
;
758 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
761 if (old_array_size
!= c
->outputs_array_size
) {
762 c
->output_semantics
= reralloc(c
,
764 struct vc4_varying_semantic
,
765 c
->outputs_array_size
);
768 c
->output_semantics
[decl_offset
].semantic
= semantic_name
;
769 c
->output_semantics
[decl_offset
].index
= semantic_index
;
770 c
->output_semantics
[decl_offset
].swizzle
= semantic_swizzle
;
774 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
776 unsigned array_id
= c
->num_uniform_ranges
++;
777 if (array_id
>= c
->ubo_ranges_array_size
) {
778 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
780 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
781 struct vc4_compiler_ubo_range
,
782 c
->ubo_ranges_array_size
);
785 c
->ubo_ranges
[array_id
].dst_offset
= 0;
786 c
->ubo_ranges
[array_id
].src_offset
= start
;
787 c
->ubo_ranges
[array_id
].size
= size
;
788 c
->ubo_ranges
[array_id
].used
= false;
792 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
794 /* Vectors are special in that they have non-scalarized writemasks,
795 * and just take the first swizzle channel for each argument in order
796 * into each writemask channel.
798 if (instr
->op
== nir_op_vec2
||
799 instr
->op
== nir_op_vec3
||
800 instr
->op
== nir_op_vec4
) {
802 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
803 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
804 instr
->src
[i
].swizzle
[0]);
805 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
806 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
811 /* General case: We can just grab the one used channel per src. */
812 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
813 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
814 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
817 /* Pick the channel to store the output in. */
818 assert(!instr
->dest
.saturate
);
819 struct qreg
*dest
= ntq_get_dest(c
, instr
->dest
.dest
);
820 assert(util_is_power_of_two(instr
->dest
.write_mask
));
821 dest
+= ffs(instr
->dest
.write_mask
) - 1;
826 *dest
= qir_MOV(c
, src
[0]);
829 *dest
= qir_FMUL(c
, src
[0], src
[1]);
832 *dest
= qir_FADD(c
, src
[0], src
[1]);
835 *dest
= qir_FSUB(c
, src
[0], src
[1]);
838 *dest
= qir_FMIN(c
, src
[0], src
[1]);
841 *dest
= qir_FMAX(c
, src
[0], src
[1]);
846 *dest
= qir_FTOI(c
, src
[0]);
850 *dest
= qir_ITOF(c
, src
[0]);
853 *dest
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
856 *dest
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
861 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
865 *dest
= qir_ADD(c
, src
[0], src
[1]);
868 *dest
= qir_SHR(c
, src
[0], src
[1]);
871 *dest
= qir_SUB(c
, src
[0], src
[1]);
874 *dest
= qir_ASR(c
, src
[0], src
[1]);
877 *dest
= qir_SHL(c
, src
[0], src
[1]);
880 *dest
= qir_MIN(c
, src
[0], src
[1]);
883 *dest
= qir_MAX(c
, src
[0], src
[1]);
886 *dest
= qir_AND(c
, src
[0], src
[1]);
889 *dest
= qir_OR(c
, src
[0], src
[1]);
892 *dest
= qir_XOR(c
, src
[0], src
[1]);
895 *dest
= qir_NOT(c
, src
[0]);
899 *dest
= ntq_umul(c
, src
[0], src
[1]);
903 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
904 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
907 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
908 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
911 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
912 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
915 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
916 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
919 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
920 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
923 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
924 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
927 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
928 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
931 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
932 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
935 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
936 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
939 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
940 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
943 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
944 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
947 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
948 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
953 *dest
= qir_SEL_X_Y_NS(c
, src
[1], src
[2]);
957 *dest
= qir_SEL_X_Y_ZC(c
, src
[1], src
[2]);
961 *dest
= ntq_rcp(c
, src
[0]);
964 *dest
= ntq_rsq(c
, src
[0]);
967 *dest
= qir_EXP2(c
, src
[0]);
970 *dest
= qir_LOG2(c
, src
[0]);
974 *dest
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
977 *dest
= ntq_fceil(c
, src
[0]);
980 *dest
= ntq_ffract(c
, src
[0]);
983 *dest
= ntq_ffloor(c
, src
[0]);
987 *dest
= ntq_fsin(c
, src
[0]);
990 *dest
= ntq_fcos(c
, src
[0]);
994 *dest
= ntq_fsign(c
, src
[0]);
998 *dest
= qir_FMAXABS(c
, src
[0], src
[0]);
1001 *dest
= qir_MAX(c
, src
[0],
1002 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1006 fprintf(stderr
, "unknown NIR ALU inst: ");
1007 nir_print_instr(&instr
->instr
, stderr
);
1008 fprintf(stderr
, "\n");
1014 vc4_blend_channel(struct vc4_compile
*c
,
1022 case PIPE_BLENDFACTOR_ONE
:
1024 case PIPE_BLENDFACTOR_SRC_COLOR
:
1025 return qir_FMUL(c
, val
, src
[channel
]);
1026 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1027 return qir_FMUL(c
, val
, src
[3]);
1028 case PIPE_BLENDFACTOR_DST_ALPHA
:
1029 return qir_FMUL(c
, val
, dst
[3]);
1030 case PIPE_BLENDFACTOR_DST_COLOR
:
1031 return qir_FMUL(c
, val
, dst
[channel
]);
1032 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1039 qir_uniform_f(c
, 1.0),
1044 case PIPE_BLENDFACTOR_CONST_COLOR
:
1045 return qir_FMUL(c
, val
,
1046 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
,
1048 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1049 return qir_FMUL(c
, val
,
1050 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
, 3));
1051 case PIPE_BLENDFACTOR_ZERO
:
1052 return qir_uniform_f(c
, 0.0);
1053 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1054 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1056 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1057 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1059 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1060 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1062 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1063 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1065 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1066 return qir_FMUL(c
, val
,
1067 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1069 QUNIFORM_BLEND_CONST_COLOR
,
1071 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1072 return qir_FMUL(c
, val
,
1073 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1075 QUNIFORM_BLEND_CONST_COLOR
,
1079 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1080 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1081 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1082 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1084 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1090 vc4_blend_func(struct vc4_compile
*c
,
1091 struct qreg src
, struct qreg dst
,
1095 case PIPE_BLEND_ADD
:
1096 return qir_FADD(c
, src
, dst
);
1097 case PIPE_BLEND_SUBTRACT
:
1098 return qir_FSUB(c
, src
, dst
);
1099 case PIPE_BLEND_REVERSE_SUBTRACT
:
1100 return qir_FSUB(c
, dst
, src
);
1101 case PIPE_BLEND_MIN
:
1102 return qir_FMIN(c
, src
, dst
);
1103 case PIPE_BLEND_MAX
:
1104 return qir_FMAX(c
, src
, dst
);
1108 fprintf(stderr
, "Unknown blend func %d\n", func
);
1115 * Implements fixed function blending in shader code.
1117 * VC4 doesn't have any hardware support for blending. Instead, you read the
1118 * current contents of the destination from the tile buffer after having
1119 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1120 * math using your output color and that destination value, and update the
1121 * output color appropriately.
1124 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1125 struct qreg
*dst_color
, struct qreg
*src_color
)
1127 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1129 if (!blend
->blend_enable
) {
1130 for (int i
= 0; i
< 4; i
++)
1131 result
[i
] = src_color
[i
];
1135 struct qreg clamped_src
[4];
1136 struct qreg clamped_dst
[4];
1137 for (int i
= 0; i
< 4; i
++) {
1138 clamped_src
[i
] = qir_SAT(c
, src_color
[i
]);
1139 clamped_dst
[i
] = qir_SAT(c
, dst_color
[i
]);
1141 src_color
= clamped_src
;
1142 dst_color
= clamped_dst
;
1144 struct qreg src_blend
[4], dst_blend
[4];
1145 for (int i
= 0; i
< 3; i
++) {
1146 src_blend
[i
] = vc4_blend_channel(c
,
1147 dst_color
, src_color
,
1149 blend
->rgb_src_factor
, i
);
1150 dst_blend
[i
] = vc4_blend_channel(c
,
1151 dst_color
, src_color
,
1153 blend
->rgb_dst_factor
, i
);
1155 src_blend
[3] = vc4_blend_channel(c
,
1156 dst_color
, src_color
,
1158 blend
->alpha_src_factor
, 3);
1159 dst_blend
[3] = vc4_blend_channel(c
,
1160 dst_color
, src_color
,
1162 blend
->alpha_dst_factor
, 3);
1164 for (int i
= 0; i
< 3; i
++) {
1165 result
[i
] = vc4_blend_func(c
,
1166 src_blend
[i
], dst_blend
[i
],
1169 result
[3] = vc4_blend_func(c
,
1170 src_blend
[3], dst_blend
[3],
1175 clip_distance_discard(struct vc4_compile
*c
)
1177 for (int i
= 0; i
< PIPE_MAX_CLIP_PLANES
; i
++) {
1178 if (!(c
->key
->ucp_enables
& (1 << i
)))
1181 struct qreg dist
= emit_fragment_varying(c
,
1182 TGSI_SEMANTIC_CLIPDIST
,
1188 if (c
->discard
.file
== QFILE_NULL
)
1189 c
->discard
= qir_uniform_ui(c
, 0);
1191 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_ui(c
, ~0),
1197 alpha_test_discard(struct vc4_compile
*c
)
1199 struct qreg src_alpha
;
1200 struct qreg alpha_ref
= qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0);
1202 if (!c
->fs_key
->alpha_test
)
1205 if (c
->output_color_index
!= -1)
1206 src_alpha
= c
->outputs
[c
->output_color_index
+ 3];
1208 src_alpha
= qir_uniform_f(c
, 1.0);
1210 if (c
->discard
.file
== QFILE_NULL
)
1211 c
->discard
= qir_uniform_ui(c
, 0);
1213 switch (c
->fs_key
->alpha_test_func
) {
1214 case PIPE_FUNC_NEVER
:
1215 c
->discard
= qir_uniform_ui(c
, ~0);
1217 case PIPE_FUNC_ALWAYS
:
1219 case PIPE_FUNC_EQUAL
:
1220 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1221 c
->discard
= qir_SEL_X_Y_ZS(c
, c
->discard
,
1222 qir_uniform_ui(c
, ~0));
1224 case PIPE_FUNC_NOTEQUAL
:
1225 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1226 c
->discard
= qir_SEL_X_Y_ZC(c
, c
->discard
,
1227 qir_uniform_ui(c
, ~0));
1229 case PIPE_FUNC_GREATER
:
1230 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1231 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1232 qir_uniform_ui(c
, ~0));
1234 case PIPE_FUNC_GEQUAL
:
1235 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1236 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1237 qir_uniform_ui(c
, ~0));
1239 case PIPE_FUNC_LESS
:
1240 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1241 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1242 qir_uniform_ui(c
, ~0));
1244 case PIPE_FUNC_LEQUAL
:
1245 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1246 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1247 qir_uniform_ui(c
, ~0));
1253 vc4_logicop(struct vc4_compile
*c
, struct qreg src
, struct qreg dst
)
1255 switch (c
->fs_key
->logicop_func
) {
1256 case PIPE_LOGICOP_CLEAR
:
1257 return qir_uniform_f(c
, 0.0);
1258 case PIPE_LOGICOP_NOR
:
1259 return qir_NOT(c
, qir_OR(c
, src
, dst
));
1260 case PIPE_LOGICOP_AND_INVERTED
:
1261 return qir_AND(c
, qir_NOT(c
, src
), dst
);
1262 case PIPE_LOGICOP_COPY_INVERTED
:
1263 return qir_NOT(c
, src
);
1264 case PIPE_LOGICOP_AND_REVERSE
:
1265 return qir_AND(c
, src
, qir_NOT(c
, dst
));
1266 case PIPE_LOGICOP_INVERT
:
1267 return qir_NOT(c
, dst
);
1268 case PIPE_LOGICOP_XOR
:
1269 return qir_XOR(c
, src
, dst
);
1270 case PIPE_LOGICOP_NAND
:
1271 return qir_NOT(c
, qir_AND(c
, src
, dst
));
1272 case PIPE_LOGICOP_AND
:
1273 return qir_AND(c
, src
, dst
);
1274 case PIPE_LOGICOP_EQUIV
:
1275 return qir_NOT(c
, qir_XOR(c
, src
, dst
));
1276 case PIPE_LOGICOP_NOOP
:
1278 case PIPE_LOGICOP_OR_INVERTED
:
1279 return qir_OR(c
, qir_NOT(c
, src
), dst
);
1280 case PIPE_LOGICOP_OR_REVERSE
:
1281 return qir_OR(c
, src
, qir_NOT(c
, dst
));
1282 case PIPE_LOGICOP_OR
:
1283 return qir_OR(c
, src
, dst
);
1284 case PIPE_LOGICOP_SET
:
1285 return qir_uniform_ui(c
, ~0);
1286 case PIPE_LOGICOP_COPY
:
1293 * Applies the GL blending pipeline and returns the packed (8888) output
1297 blend_pipeline(struct vc4_compile
*c
)
1299 enum pipe_format color_format
= c
->fs_key
->color_format
;
1300 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1301 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1302 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1303 struct qreg linear_dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1304 struct qreg packed_dst_color
= c
->undef
;
1306 if (c
->fs_key
->blend
.blend_enable
||
1307 c
->fs_key
->blend
.colormask
!= 0xf ||
1308 c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1309 struct qreg r4
= qir_TLB_COLOR_READ(c
);
1310 for (int i
= 0; i
< 4; i
++)
1311 tlb_read_color
[i
] = qir_R4_UNPACK(c
, r4
, i
);
1312 for (int i
= 0; i
< 4; i
++) {
1313 dst_color
[i
] = get_swizzled_channel(c
,
1316 if (util_format_is_srgb(color_format
) && i
!= 3) {
1317 linear_dst_color
[i
] =
1318 qir_srgb_decode(c
, dst_color
[i
]);
1320 linear_dst_color
[i
] = dst_color
[i
];
1324 /* Save the packed value for logic ops. Can't reuse r4
1325 * because other things might smash it (like sRGB)
1327 packed_dst_color
= qir_MOV(c
, r4
);
1330 struct qreg undef_array
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1331 const struct qreg
*output_colors
= (c
->output_color_index
!= -1 ?
1332 c
->outputs
+ c
->output_color_index
:
1334 struct qreg blend_src_color
[4];
1335 for (int i
= 0; i
< 4; i
++)
1336 blend_src_color
[i
] = output_colors
[i
];
1338 struct qreg blend_color
[4];
1339 vc4_blend(c
, blend_color
, linear_dst_color
, blend_src_color
);
1341 if (util_format_is_srgb(color_format
)) {
1342 for (int i
= 0; i
< 3; i
++)
1343 blend_color
[i
] = qir_srgb_encode(c
, blend_color
[i
]);
1346 /* Debug: Sometimes you're getting a black output and just want to see
1347 * if the FS is getting executed at all. Spam magenta into the color
1351 blend_color
[0] = qir_uniform_f(c
, 1.0);
1352 blend_color
[1] = qir_uniform_f(c
, 0.0);
1353 blend_color
[2] = qir_uniform_f(c
, 1.0);
1354 blend_color
[3] = qir_uniform_f(c
, 0.5);
1357 struct qreg swizzled_outputs
[4];
1358 for (int i
= 0; i
< 4; i
++) {
1359 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1363 struct qreg packed_color
= c
->undef
;
1364 for (int i
= 0; i
< 4; i
++) {
1365 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1367 if (packed_color
.file
== QFILE_NULL
) {
1368 packed_color
= qir_PACK_8888_F(c
, swizzled_outputs
[i
]);
1370 packed_color
= qir_PACK_8_F(c
,
1372 swizzled_outputs
[i
],
1377 if (packed_color
.file
== QFILE_NULL
)
1378 packed_color
= qir_uniform_ui(c
, 0);
1380 if (c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1381 packed_color
= vc4_logicop(c
, packed_color
, packed_dst_color
);
1384 /* If the bit isn't set in the color mask, then just return the
1385 * original dst color, instead.
1387 uint32_t colormask
= 0xffffffff;
1388 for (int i
= 0; i
< 4; i
++) {
1389 if (format_swiz
[i
] < 4 &&
1390 !(c
->fs_key
->blend
.colormask
& (1 << format_swiz
[i
]))) {
1391 colormask
&= ~(0xff << (i
* 8));
1394 if (colormask
!= 0xffffffff) {
1395 packed_color
= qir_OR(c
,
1396 qir_AND(c
, packed_color
,
1397 qir_uniform_ui(c
, colormask
)),
1398 qir_AND(c
, packed_dst_color
,
1399 qir_uniform_ui(c
, ~colormask
)));
1402 return packed_color
;
1406 emit_frag_end(struct vc4_compile
*c
)
1408 clip_distance_discard(c
);
1409 alpha_test_discard(c
);
1410 struct qreg color
= blend_pipeline(c
);
1412 if (c
->discard
.file
!= QFILE_NULL
)
1413 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1415 if (c
->fs_key
->stencil_enabled
) {
1416 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1417 if (c
->fs_key
->stencil_twoside
) {
1418 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1420 if (c
->fs_key
->stencil_full_writemasks
) {
1421 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1425 if (c
->fs_key
->depth_enabled
) {
1427 if (c
->output_position_index
!= -1) {
1428 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1429 qir_uniform_f(c
, 0xffffff)));
1433 qir_TLB_Z_WRITE(c
, z
);
1436 qir_TLB_COLOR_WRITE(c
, color
);
1440 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1444 for (int i
= 0; i
< 2; i
++) {
1446 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1448 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1450 c
->outputs
[c
->output_position_index
+ i
],
1455 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1459 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1461 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1462 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1464 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1465 c
->outputs
[c
->output_position_index
+ 2],
1472 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1474 qir_VPM_WRITE(c
, rcp_w
);
1478 emit_point_size_write(struct vc4_compile
*c
)
1480 struct qreg point_size
;
1482 if (c
->output_point_size_index
!= -1)
1483 point_size
= c
->outputs
[c
->output_point_size_index
+ 3];
1485 point_size
= qir_uniform_f(c
, 1.0);
1487 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1490 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1492 qir_VPM_WRITE(c
, point_size
);
1496 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1498 * The simulator insists that there be at least one vertex attribute, so
1499 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1500 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1501 * to consume it here.
1504 emit_stub_vpm_read(struct vc4_compile
*c
)
1509 c
->vattr_sizes
[0] = 4;
1510 struct qreg vpm
= { QFILE_VPM
, 0 };
1511 (void)qir_MOV(c
, vpm
);
1516 emit_ucp_clipdistance(struct vc4_compile
*c
)
1519 if (c
->output_clipvertex_index
!= -1)
1520 cv
= c
->output_clipvertex_index
;
1521 else if (c
->output_position_index
!= -1)
1522 cv
= c
->output_position_index
;
1526 for (int plane
= 0; plane
< PIPE_MAX_CLIP_PLANES
; plane
++) {
1527 if (!(c
->key
->ucp_enables
& (1 << plane
)))
1530 /* Pick the next outputs[] that hasn't been written to, since
1531 * there are no other program writes left to be processed at
1532 * this point. If something had been declared but not written
1533 * (like a w component), we'll just smash over the top of it.
1535 uint32_t output_index
= c
->num_outputs
++;
1536 add_output(c
, output_index
,
1537 TGSI_SEMANTIC_CLIPDIST
,
1542 struct qreg dist
= qir_uniform_f(c
, 0.0);
1543 for (int i
= 0; i
< 4; i
++) {
1544 struct qreg pos_chan
= c
->outputs
[cv
+ i
];
1546 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1548 dist
= qir_FADD(c
, dist
, qir_FMUL(c
, pos_chan
, ucp
));
1551 c
->outputs
[output_index
] = dist
;
1556 emit_vert_end(struct vc4_compile
*c
,
1557 struct vc4_varying_semantic
*fs_inputs
,
1558 uint32_t num_fs_inputs
)
1560 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1562 emit_stub_vpm_read(c
);
1563 emit_ucp_clipdistance(c
);
1565 emit_scaled_viewport_write(c
, rcp_w
);
1566 emit_zs_write(c
, rcp_w
);
1567 emit_rcp_wc_write(c
, rcp_w
);
1568 if (c
->vs_key
->per_vertex_point_size
)
1569 emit_point_size_write(c
);
1571 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1572 struct vc4_varying_semantic
*input
= &fs_inputs
[i
];
1575 for (j
= 0; j
< c
->num_outputs
; j
++) {
1576 struct vc4_varying_semantic
*output
=
1577 &c
->output_semantics
[j
];
1579 if (input
->semantic
== output
->semantic
&&
1580 input
->index
== output
->index
&&
1581 input
->swizzle
== output
->swizzle
) {
1582 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1586 /* Emit padding if we didn't find a declared VS output for
1589 if (j
== c
->num_outputs
)
1590 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1595 emit_coord_end(struct vc4_compile
*c
)
1597 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1599 emit_stub_vpm_read(c
);
1601 for (int i
= 0; i
< 4; i
++)
1602 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1604 emit_scaled_viewport_write(c
, rcp_w
);
1605 emit_zs_write(c
, rcp_w
);
1606 emit_rcp_wc_write(c
, rcp_w
);
1607 if (c
->vs_key
->per_vertex_point_size
)
1608 emit_point_size_write(c
);
1612 vc4_optimize_nir(struct nir_shader
*s
)
1619 nir_lower_vars_to_ssa(s
);
1620 nir_lower_alu_to_scalar(s
);
1622 progress
= nir_copy_prop(s
) || progress
;
1623 progress
= nir_opt_dce(s
) || progress
;
1624 progress
= nir_opt_cse(s
) || progress
;
1625 progress
= nir_opt_peephole_select(s
) || progress
;
1626 progress
= nir_opt_algebraic(s
) || progress
;
1627 progress
= nir_opt_constant_folding(s
) || progress
;
1632 driver_location_compare(const void *in_a
, const void *in_b
)
1634 const nir_variable
*const *a
= in_a
;
1635 const nir_variable
*const *b
= in_b
;
1637 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1641 ntq_setup_inputs(struct vc4_compile
*c
)
1643 unsigned num_entries
= 0;
1644 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1647 nir_variable
*vars
[num_entries
];
1650 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1653 /* Sort the variables so that we emit the input setup in
1654 * driver_location order. This is required for VPM reads, whose data
1655 * is fetched into the VPM in driver_location (TGSI register index)
1658 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1660 for (unsigned i
= 0; i
< num_entries
; i
++) {
1661 nir_variable
*var
= vars
[i
];
1662 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1663 /* XXX: map loc slots to semantics */
1664 unsigned semantic_name
= var
->data
.location
;
1665 unsigned semantic_index
= var
->data
.index
;
1666 unsigned loc
= var
->data
.driver_location
;
1668 assert(array_len
== 1);
1670 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1673 if (c
->stage
== QSTAGE_FRAG
) {
1674 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1675 emit_fragcoord_input(c
, loc
);
1676 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1677 c
->inputs
[loc
* 4 + 0] = qir_FRAG_REV_FLAG(c
);
1678 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1679 (c
->fs_key
->point_sprite_mask
&
1680 (1 << semantic_index
))) {
1681 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1682 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1684 emit_fragment_input(c
, loc
,
1689 emit_vertex_input(c
, loc
);
1695 ntq_setup_outputs(struct vc4_compile
*c
)
1697 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->outputs
) {
1698 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1699 /* XXX: map loc slots to semantics */
1700 unsigned semantic_name
= var
->data
.location
;
1701 unsigned semantic_index
= var
->data
.index
;
1702 unsigned loc
= var
->data
.driver_location
* 4;
1704 assert(array_len
== 1);
1707 /* NIR hack to pass through
1708 * TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS */
1709 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1710 semantic_index
== -1)
1713 for (int i
= 0; i
< 4; i
++) {
1721 switch (semantic_name
) {
1722 case TGSI_SEMANTIC_POSITION
:
1723 c
->output_position_index
= loc
;
1725 case TGSI_SEMANTIC_CLIPVERTEX
:
1726 c
->output_clipvertex_index
= loc
;
1728 case TGSI_SEMANTIC_COLOR
:
1729 c
->output_color_index
= loc
;
1731 case TGSI_SEMANTIC_PSIZE
:
1732 c
->output_point_size_index
= loc
;
1740 ntq_setup_uniforms(struct vc4_compile
*c
)
1742 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->uniforms
) {
1743 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1744 unsigned array_elem_size
= 4 * sizeof(float);
1746 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1747 array_len
* array_elem_size
);
1753 * Sets up the mapping from nir_register to struct qreg *.
1755 * Each nir_register gets a struct qreg per 32-bit component being stored.
1758 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1760 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1761 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1762 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1764 nir_reg
->num_components
);
1766 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1768 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1769 qregs
[i
] = qir_uniform_ui(c
, 0);
1774 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1776 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1777 instr
->def
.num_components
);
1778 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1779 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u
[i
]);
1781 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1785 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1787 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1788 struct qreg
*dest
= NULL
;
1790 if (info
->has_dest
) {
1791 dest
= ntq_get_dest(c
, instr
->dest
);
1794 switch (instr
->intrinsic
) {
1795 case nir_intrinsic_load_uniform
:
1796 for (int i
= 0; i
< instr
->num_components
; i
++) {
1797 dest
[i
] = qir_uniform(c
, QUNIFORM_UNIFORM
,
1798 instr
->const_index
[0] * 4 + i
);
1802 case nir_intrinsic_load_uniform_indirect
:
1803 for (int i
= 0; i
< instr
->num_components
; i
++) {
1804 dest
[i
] = indirect_uniform_load(c
,
1805 ntq_get_src(c
, instr
->src
[0], 0),
1806 (instr
->const_index
[0] *
1807 4 + i
) * sizeof(float));
1812 case nir_intrinsic_load_input
:
1813 assert(instr
->num_components
== 1);
1814 *dest
= c
->inputs
[instr
->const_index
[0]];
1818 case nir_intrinsic_store_output
:
1819 assert(instr
->num_components
== 1);
1820 c
->outputs
[instr
->const_index
[0]] =
1821 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1822 c
->num_outputs
= MAX2(c
->num_outputs
, instr
->const_index
[0] + 1);
1825 case nir_intrinsic_discard
:
1826 c
->discard
= qir_uniform_ui(c
, ~0);
1829 case nir_intrinsic_discard_if
:
1830 if (c
->discard
.file
== QFILE_NULL
)
1831 c
->discard
= qir_uniform_ui(c
, 0);
1832 c
->discard
= qir_OR(c
, c
->discard
,
1833 ntq_get_src(c
, instr
->src
[0], 0));
1837 fprintf(stderr
, "Unknown intrinsic: ");
1838 nir_print_instr(&instr
->instr
, stderr
);
1839 fprintf(stderr
, "\n");
1845 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1847 fprintf(stderr
, "general IF statements not handled.\n");
1851 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1853 switch (instr
->type
) {
1854 case nir_instr_type_alu
:
1855 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1858 case nir_instr_type_intrinsic
:
1859 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1862 case nir_instr_type_load_const
:
1863 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1866 case nir_instr_type_tex
:
1867 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1871 fprintf(stderr
, "Unknown NIR instr type: ");
1872 nir_print_instr(instr
, stderr
);
1873 fprintf(stderr
, "\n");
1879 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1881 nir_foreach_instr(block
, instr
) {
1882 ntq_emit_instr(c
, instr
);
1887 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1889 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1890 switch (node
->type
) {
1891 /* case nir_cf_node_loop: */
1892 case nir_cf_node_block
:
1893 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1896 case nir_cf_node_if
:
1897 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1907 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1909 ntq_setup_registers(c
, &impl
->registers
);
1910 ntq_emit_cf_list(c
, &impl
->body
);
1914 nir_to_qir(struct vc4_compile
*c
)
1916 ntq_setup_inputs(c
);
1917 ntq_setup_outputs(c
);
1918 ntq_setup_uniforms(c
);
1919 ntq_setup_registers(c
, &c
->s
->registers
);
1921 /* Find the main function and emit the body. */
1922 nir_foreach_overload(c
->s
, overload
) {
1923 assert(strcmp(overload
->function
->name
, "main") == 0);
1924 assert(overload
->impl
);
1925 ntq_emit_impl(c
, overload
->impl
);
1929 static const nir_shader_compiler_options nir_options
= {
1934 .lower_fsqrt
= true,
1935 .lower_negate
= true,
1939 count_nir_instrs_in_block(nir_block
*block
, void *state
)
1941 int *count
= (int *) state
;
1942 nir_foreach_instr(block
, instr
) {
1943 *count
= *count
+ 1;
1949 count_nir_instrs(nir_shader
*nir
)
1952 nir_foreach_overload(nir
, overload
) {
1953 if (!overload
->impl
)
1955 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
1960 static struct vc4_compile
*
1961 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
1962 struct vc4_key
*key
)
1964 struct vc4_compile
*c
= qir_compile_init();
1967 c
->shader_state
= &key
->shader_state
->base
;
1968 c
->program_id
= key
->shader_state
->program_id
;
1969 c
->variant_id
= key
->shader_state
->compiled_variant_count
++;
1974 c
->fs_key
= (struct vc4_fs_key
*)key
;
1975 if (c
->fs_key
->is_points
) {
1976 c
->point_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1977 c
->point_y
= emit_fragment_varying(c
, ~0, ~0, 0);
1978 } else if (c
->fs_key
->is_lines
) {
1979 c
->line_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1983 c
->vs_key
= (struct vc4_vs_key
*)key
;
1986 c
->vs_key
= (struct vc4_vs_key
*)key
;
1990 const struct tgsi_token
*tokens
= key
->shader_state
->base
.tokens
;
1991 if (c
->fs_key
&& c
->fs_key
->light_twoside
) {
1992 if (!key
->shader_state
->twoside_tokens
) {
1993 const struct tgsi_lowering_config lowering_config
= {
1994 .color_two_side
= true,
1996 struct tgsi_shader_info info
;
1997 key
->shader_state
->twoside_tokens
=
1998 tgsi_transform_lowering(&lowering_config
,
1999 key
->shader_state
->base
.tokens
,
2002 /* If no transformation occurred, then NULL is
2003 * returned and we just use our original tokens.
2005 if (!key
->shader_state
->twoside_tokens
) {
2006 key
->shader_state
->twoside_tokens
=
2007 key
->shader_state
->base
.tokens
;
2010 tokens
= key
->shader_state
->twoside_tokens
;
2013 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2014 fprintf(stderr
, "%s prog %d/%d TGSI:\n",
2015 qir_get_stage_name(c
->stage
),
2016 c
->program_id
, c
->variant_id
);
2017 tgsi_dump(tokens
, 0);
2020 c
->s
= tgsi_to_nir(tokens
, &nir_options
);
2021 nir_opt_global_to_local(c
->s
);
2022 nir_convert_to_ssa(c
->s
);
2023 vc4_nir_lower_io(c
);
2024 nir_lower_idiv(c
->s
);
2026 vc4_optimize_nir(c
->s
);
2028 nir_remove_dead_variables(c
->s
);
2030 nir_convert_from_ssa(c
->s
, false);
2032 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2033 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2034 qir_get_stage_name(c
->stage
),
2035 c
->program_id
, c
->variant_id
,
2036 count_nir_instrs(c
->s
));
2039 if (vc4_debug
& VC4_DEBUG_NIR
) {
2040 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2041 qir_get_stage_name(c
->stage
),
2042 c
->program_id
, c
->variant_id
);
2043 nir_print_shader(c
->s
, stderr
);
2054 vc4
->prog
.fs
->input_semantics
,
2055 vc4
->prog
.fs
->num_inputs
);
2062 if (vc4_debug
& VC4_DEBUG_QIR
) {
2063 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2064 qir_get_stage_name(c
->stage
),
2065 c
->program_id
, c
->variant_id
);
2070 qir_lower_uniforms(c
);
2072 if (vc4_debug
& VC4_DEBUG_QIR
) {
2073 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2074 qir_get_stage_name(c
->stage
),
2075 c
->program_id
, c
->variant_id
);
2078 qir_reorder_uniforms(c
);
2079 vc4_generate_code(vc4
, c
);
2081 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2082 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2083 qir_get_stage_name(c
->stage
),
2084 c
->program_id
, c
->variant_id
,
2086 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2087 qir_get_stage_name(c
->stage
),
2088 c
->program_id
, c
->variant_id
,
2098 vc4_shader_state_create(struct pipe_context
*pctx
,
2099 const struct pipe_shader_state
*cso
)
2101 struct vc4_context
*vc4
= vc4_context(pctx
);
2102 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2106 so
->base
.tokens
= tgsi_dup_tokens(cso
->tokens
);
2107 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2113 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2114 struct vc4_compile
*c
)
2116 int count
= c
->num_uniforms
;
2117 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2119 uinfo
->count
= count
;
2120 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2121 memcpy(uinfo
->data
, c
->uniform_data
,
2122 count
* sizeof(*uinfo
->data
));
2123 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2124 memcpy(uinfo
->contents
, c
->uniform_contents
,
2125 count
* sizeof(*uinfo
->contents
));
2126 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2128 vc4_set_shader_uniform_dirty_flags(shader
);
2131 static struct vc4_compiled_shader
*
2132 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2133 struct vc4_key
*key
)
2135 struct hash_table
*ht
;
2137 if (stage
== QSTAGE_FRAG
) {
2139 key_size
= sizeof(struct vc4_fs_key
);
2142 key_size
= sizeof(struct vc4_vs_key
);
2145 struct vc4_compiled_shader
*shader
;
2146 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2150 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2151 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2153 shader
->program_id
= vc4
->next_compiled_program_id
++;
2154 if (stage
== QSTAGE_FRAG
) {
2155 bool input_live
[c
->num_input_semantics
];
2157 memset(input_live
, 0, sizeof(input_live
));
2158 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
2159 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2160 if (inst
->src
[i
].file
== QFILE_VARY
)
2161 input_live
[inst
->src
[i
].index
] = true;
2165 shader
->input_semantics
= ralloc_array(shader
,
2166 struct vc4_varying_semantic
,
2167 c
->num_input_semantics
);
2169 for (int i
= 0; i
< c
->num_input_semantics
; i
++) {
2170 struct vc4_varying_semantic
*sem
= &c
->input_semantics
[i
];
2175 /* Skip non-VS-output inputs. */
2176 if (sem
->semantic
== (uint8_t)~0)
2179 if (sem
->semantic
== TGSI_SEMANTIC_COLOR
||
2180 sem
->semantic
== TGSI_SEMANTIC_BCOLOR
) {
2181 shader
->color_inputs
|= (1 << shader
->num_inputs
);
2184 shader
->input_semantics
[shader
->num_inputs
] = *sem
;
2185 shader
->num_inputs
++;
2188 shader
->num_inputs
= c
->num_inputs
;
2190 shader
->vattr_offsets
[0] = 0;
2191 for (int i
= 0; i
< 8; i
++) {
2192 shader
->vattr_offsets
[i
+ 1] =
2193 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2195 if (c
->vattr_sizes
[i
])
2196 shader
->vattrs_live
|= (1 << i
);
2200 copy_uniform_state_to_shader(shader
, c
);
2201 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2202 c
->qpu_inst_count
* sizeof(uint64_t));
2204 /* Copy the compiler UBO range state to the compiled shader, dropping
2205 * out arrays that were never referenced by an indirect load.
2207 * (Note that QIR dead code elimination of an array access still
2208 * leaves that array alive, though)
2210 if (c
->num_ubo_ranges
) {
2211 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2212 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2215 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2216 struct vc4_compiler_ubo_range
*range
=
2221 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2222 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2223 shader
->ubo_ranges
[j
].size
= range
->size
;
2224 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2228 if (shader
->ubo_size
) {
2229 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2230 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2231 qir_get_stage_name(c
->stage
),
2232 c
->program_id
, c
->variant_id
,
2233 shader
->ubo_size
/ 4);
2237 qir_compile_destroy(c
);
2239 struct vc4_key
*dup_key
;
2240 dup_key
= ralloc_size(shader
, key_size
);
2241 memcpy(dup_key
, key
, key_size
);
2242 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2248 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2249 struct vc4_texture_stateobj
*texstate
)
2251 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2252 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2253 struct pipe_sampler_state
*sampler_state
=
2254 texstate
->samplers
[i
];
2257 key
->tex
[i
].format
= sampler
->format
;
2258 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2259 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2260 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2261 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2262 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2263 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2264 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2265 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2269 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2273 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2275 struct vc4_fs_key local_key
;
2276 struct vc4_fs_key
*key
= &local_key
;
2278 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2280 VC4_DIRTY_FRAMEBUFFER
|
2282 VC4_DIRTY_RASTERIZER
|
2284 VC4_DIRTY_TEXSTATE
|
2285 VC4_DIRTY_UNCOMPILED_FS
))) {
2289 memset(key
, 0, sizeof(*key
));
2290 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2291 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2292 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2293 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2294 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2295 key
->blend
= vc4
->blend
->rt
[0];
2296 if (vc4
->blend
->logicop_enable
) {
2297 key
->logicop_func
= vc4
->blend
->logicop_func
;
2299 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2301 if (vc4
->framebuffer
.cbufs
[0])
2302 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2304 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2305 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2306 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2307 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2308 key
->stencil_enabled
);
2309 if (vc4
->zsa
->base
.alpha
.enabled
) {
2310 key
->alpha_test
= true;
2311 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2314 if (key
->is_points
) {
2315 key
->point_sprite_mask
=
2316 vc4
->rasterizer
->base
.sprite_coord_enable
;
2317 key
->point_coord_upper_left
=
2318 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2319 PIPE_SPRITE_COORD_UPPER_LEFT
);
2322 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2324 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2325 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2326 if (vc4
->prog
.fs
== old_fs
)
2329 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2330 if (vc4
->rasterizer
->base
.flatshade
&&
2331 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2332 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2337 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2339 struct vc4_vs_key local_key
;
2340 struct vc4_vs_key
*key
= &local_key
;
2342 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2343 VC4_DIRTY_RASTERIZER
|
2345 VC4_DIRTY_TEXSTATE
|
2346 VC4_DIRTY_VTXSTATE
|
2347 VC4_DIRTY_UNCOMPILED_VS
|
2348 VC4_DIRTY_COMPILED_FS
))) {
2352 memset(key
, 0, sizeof(*key
));
2353 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2354 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2355 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2357 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2358 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2360 key
->per_vertex_point_size
=
2361 (prim_mode
== PIPE_PRIM_POINTS
&&
2362 vc4
->rasterizer
->base
.point_size_per_vertex
);
2364 struct vc4_compiled_shader
*vs
=
2365 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2366 if (vs
!= vc4
->prog
.vs
) {
2368 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2371 key
->is_coord
= true;
2372 struct vc4_compiled_shader
*cs
=
2373 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2374 if (cs
!= vc4
->prog
.cs
) {
2376 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2381 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2383 vc4_update_compiled_fs(vc4
, prim_mode
);
2384 vc4_update_compiled_vs(vc4
, prim_mode
);
2388 fs_cache_hash(const void *key
)
2390 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2394 vs_cache_hash(const void *key
)
2396 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2400 fs_cache_compare(const void *key1
, const void *key2
)
2402 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2406 vs_cache_compare(const void *key1
, const void *key2
)
2408 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2412 delete_from_cache_if_matches(struct hash_table
*ht
,
2413 struct hash_entry
*entry
,
2414 struct vc4_uncompiled_shader
*so
)
2416 const struct vc4_key
*key
= entry
->key
;
2418 if (key
->shader_state
== so
) {
2419 struct vc4_compiled_shader
*shader
= entry
->data
;
2420 _mesa_hash_table_remove(ht
, entry
);
2421 vc4_bo_unreference(&shader
->bo
);
2422 ralloc_free(shader
);
2427 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2429 struct vc4_context
*vc4
= vc4_context(pctx
);
2430 struct vc4_uncompiled_shader
*so
= hwcso
;
2432 struct hash_entry
*entry
;
2433 hash_table_foreach(vc4
->fs_cache
, entry
)
2434 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2435 hash_table_foreach(vc4
->vs_cache
, entry
)
2436 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2438 if (so
->twoside_tokens
!= so
->base
.tokens
)
2439 free((void *)so
->twoside_tokens
);
2440 free((void *)so
->base
.tokens
);
2445 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2447 struct vc4_context
*vc4
= vc4_context(pctx
);
2448 vc4
->prog
.bind_fs
= hwcso
;
2449 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2453 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2455 struct vc4_context
*vc4
= vc4_context(pctx
);
2456 vc4
->prog
.bind_vs
= hwcso
;
2457 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2461 vc4_program_init(struct pipe_context
*pctx
)
2463 struct vc4_context
*vc4
= vc4_context(pctx
);
2465 pctx
->create_vs_state
= vc4_shader_state_create
;
2466 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2468 pctx
->create_fs_state
= vc4_shader_state_create
;
2469 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2471 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2472 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2474 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2476 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2481 vc4_program_fini(struct pipe_context
*pctx
)
2483 struct vc4_context
*vc4
= vc4_context(pctx
);
2485 struct hash_entry
*entry
;
2486 hash_table_foreach(vc4
->fs_cache
, entry
) {
2487 struct vc4_compiled_shader
*shader
= entry
->data
;
2488 vc4_bo_unreference(&shader
->bo
);
2489 ralloc_free(shader
);
2490 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2493 hash_table_foreach(vc4
->vs_cache
, entry
) {
2494 struct vc4_compiled_shader
*shader
= entry
->data
;
2495 vc4_bo_unreference(&shader
->bo
);
2496 ralloc_free(shader
);
2497 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);