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 "glsl/nir/nir.h"
37 #include "glsl/nir/nir_builder.h"
38 #include "nir/tgsi_to_nir.h"
39 #include "vc4_context.h"
42 #ifdef USE_VC4_SIMULATOR
43 #include "simpenrose/simpenrose.h"
47 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
50 resize_qreg_array(struct vc4_compile
*c
,
55 if (*size
>= decl_size
)
58 uint32_t old_size
= *size
;
59 *size
= MAX2(*size
* 2, decl_size
);
60 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
62 fprintf(stderr
, "Malloc failure\n");
66 for (uint32_t i
= old_size
; i
< *size
; i
++)
67 (*regs
)[i
] = c
->undef
;
71 indirect_uniform_load(struct vc4_compile
*c
, nir_intrinsic_instr
*intr
)
73 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
74 uint32_t offset
= intr
->const_index
[0];
75 struct vc4_compiler_ubo_range
*range
= NULL
;
77 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
78 range
= &c
->ubo_ranges
[i
];
79 if (offset
>= range
->src_offset
&&
80 offset
< range
->src_offset
+ range
->size
) {
84 /* The driver-location-based offset always has to be within a declared
90 range
->dst_offset
= c
->next_ubo_dst_offset
;
91 c
->next_ubo_dst_offset
+= range
->size
;
95 offset
-= range
->src_offset
;
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 c
->num_texture_samples
++;
110 return qir_TEX_RESULT(c
);
113 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
114 enum quniform_contents contents
)
116 nir_intrinsic_instr
*intr
=
117 nir_intrinsic_instr_create(b
->shader
,
118 nir_intrinsic_load_uniform
);
119 intr
->const_index
[0] = VC4_NIR_STATE_UNIFORM_OFFSET
+ contents
;
120 intr
->num_components
= 1;
121 nir_ssa_dest_init(&intr
->instr
, &intr
->dest
, 1, NULL
);
122 nir_builder_instr_insert(b
, &intr
->instr
);
123 return &intr
->dest
.ssa
;
127 ntq_init_ssa_def(struct vc4_compile
*c
, nir_ssa_def
*def
)
129 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
130 def
->num_components
);
131 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
136 ntq_get_dest(struct vc4_compile
*c
, nir_dest
*dest
)
139 struct qreg
*qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
140 for (int i
= 0; i
< dest
->ssa
.num_components
; i
++)
144 nir_register
*reg
= dest
->reg
.reg
;
145 assert(dest
->reg
.base_offset
== 0);
146 assert(reg
->num_array_elems
== 0);
147 struct hash_entry
*entry
=
148 _mesa_hash_table_search(c
->def_ht
, reg
);
154 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
156 struct hash_entry
*entry
;
158 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
159 assert(i
< src
.ssa
->num_components
);
161 nir_register
*reg
= src
.reg
.reg
;
162 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
163 assert(reg
->num_array_elems
== 0);
164 assert(src
.reg
.base_offset
== 0);
165 assert(i
< reg
->num_components
);
168 struct qreg
*qregs
= entry
->data
;
173 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
176 assert(util_is_power_of_two(instr
->dest
.write_mask
));
177 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
178 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
179 instr
->src
[src
].swizzle
[chan
]);
181 assert(!instr
->src
[src
].abs
);
182 assert(!instr
->src
[src
].negate
);
188 get_swizzled_channel(struct vc4_compile
*c
,
189 struct qreg
*srcs
, int swiz
)
193 case UTIL_FORMAT_SWIZZLE_NONE
:
194 fprintf(stderr
, "warning: unknown swizzle\n");
196 case UTIL_FORMAT_SWIZZLE_0
:
197 return qir_uniform_f(c
, 0.0);
198 case UTIL_FORMAT_SWIZZLE_1
:
199 return qir_uniform_f(c
, 1.0);
200 case UTIL_FORMAT_SWIZZLE_X
:
201 case UTIL_FORMAT_SWIZZLE_Y
:
202 case UTIL_FORMAT_SWIZZLE_Z
:
203 case UTIL_FORMAT_SWIZZLE_W
:
208 static inline struct qreg
209 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
212 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
213 qir_uniform_f(c
, 0.0));
217 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
219 struct qreg r
= qir_RCP(c
, x
);
221 /* Apply a Newton-Raphson step to improve the accuracy. */
222 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
223 qir_uniform_f(c
, 2.0),
230 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
232 struct qreg r
= qir_RSQ(c
, x
);
234 /* Apply a Newton-Raphson step to improve the accuracy. */
235 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
236 qir_uniform_f(c
, 1.5),
238 qir_uniform_f(c
, 0.5),
240 qir_FMUL(c
, r
, r
)))));
246 qir_srgb_decode(struct vc4_compile
*c
, struct qreg srgb
)
248 struct qreg low
= qir_FMUL(c
, srgb
, qir_uniform_f(c
, 1.0 / 12.92));
249 struct qreg high
= qir_POW(c
,
253 qir_uniform_f(c
, 0.055)),
254 qir_uniform_f(c
, 1.0 / 1.055)),
255 qir_uniform_f(c
, 2.4));
257 qir_SF(c
, qir_FSUB(c
, srgb
, qir_uniform_f(c
, 0.04045)));
258 return qir_SEL_X_Y_NS(c
, low
, high
);
262 qir_srgb_encode(struct vc4_compile
*c
, struct qreg linear
)
264 struct qreg low
= qir_FMUL(c
, linear
, qir_uniform_f(c
, 12.92));
265 struct qreg high
= qir_FSUB(c
,
267 qir_uniform_f(c
, 1.055),
270 qir_uniform_f(c
, 0.41666))),
271 qir_uniform_f(c
, 0.055));
273 qir_SF(c
, qir_FSUB(c
, linear
, qir_uniform_f(c
, 0.0031308)));
274 return qir_SEL_X_Y_NS(c
, low
, high
);
278 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
280 struct qreg src0_hi
= qir_SHR(c
, src0
,
281 qir_uniform_ui(c
, 24));
282 struct qreg src1_hi
= qir_SHR(c
, src1
,
283 qir_uniform_ui(c
, 24));
285 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
286 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
287 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
289 return qir_ADD(c
, lolo
, qir_SHL(c
,
290 qir_ADD(c
, hilo
, lohi
),
291 qir_uniform_ui(c
, 24)));
295 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
297 struct qreg s
, t
, r
, lod
, proj
, compare
;
298 bool is_txb
= false, is_txl
= false, has_proj
= false;
299 unsigned unit
= instr
->sampler_index
;
301 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
302 switch (instr
->src
[i
].src_type
) {
303 case nir_tex_src_coord
:
304 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
305 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
306 t
= qir_uniform_f(c
, 0.5);
308 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
309 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
310 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
312 case nir_tex_src_bias
:
313 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
316 case nir_tex_src_lod
:
317 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
320 case nir_tex_src_comparitor
:
321 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
323 case nir_tex_src_projector
:
324 proj
= qir_RCP(c
, ntq_get_src(c
, instr
->src
[i
].src
, 0));
325 s
= qir_FMUL(c
, s
, proj
);
326 t
= qir_FMUL(c
, t
, proj
);
330 unreachable("unknown texture source");
334 struct qreg texture_u
[] = {
335 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
336 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
337 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
338 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
340 uint32_t next_texture_u
= 0;
342 /* There is no native support for GL texture rectangle coordinates, so
343 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
346 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
348 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
350 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
353 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
354 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
355 unit
| (is_txl
<< 16));
358 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
359 struct qreg ma
= qir_FMAXABS(c
, qir_FMAXABS(c
, s
, t
), r
);
360 struct qreg rcp_ma
= qir_RCP(c
, ma
);
361 s
= qir_FMUL(c
, s
, rcp_ma
);
362 t
= qir_FMUL(c
, t
, rcp_ma
);
363 r
= qir_FMUL(c
, r
, rcp_ma
);
365 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
366 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
367 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
368 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
369 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
370 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
371 texture_u
[next_texture_u
++]);
374 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
378 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
382 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
384 if (is_txl
|| is_txb
)
385 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
387 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
389 c
->num_texture_samples
++;
390 struct qreg tex
= qir_TEX_RESULT(c
);
392 enum pipe_format format
= c
->key
->tex
[unit
].format
;
394 struct qreg unpacked
[4];
395 if (util_format_is_depth_or_stencil(format
)) {
396 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, tex
,
397 qir_uniform_ui(c
, 8)));
398 struct qreg normalized
= qir_FMUL(c
, depthf
,
399 qir_uniform_f(c
, 1.0f
/0xffffff));
401 struct qreg depth_output
;
403 struct qreg one
= qir_uniform_f(c
, 1.0f
);
404 if (c
->key
->tex
[unit
].compare_mode
) {
406 compare
= qir_FMUL(c
, compare
, proj
);
408 switch (c
->key
->tex
[unit
].compare_func
) {
409 case PIPE_FUNC_NEVER
:
410 depth_output
= qir_uniform_f(c
, 0.0f
);
412 case PIPE_FUNC_ALWAYS
:
415 case PIPE_FUNC_EQUAL
:
416 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
417 depth_output
= qir_SEL_X_0_ZS(c
, one
);
419 case PIPE_FUNC_NOTEQUAL
:
420 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
421 depth_output
= qir_SEL_X_0_ZC(c
, one
);
423 case PIPE_FUNC_GREATER
:
424 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
425 depth_output
= qir_SEL_X_0_NC(c
, one
);
427 case PIPE_FUNC_GEQUAL
:
428 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
429 depth_output
= qir_SEL_X_0_NS(c
, one
);
432 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
433 depth_output
= qir_SEL_X_0_NS(c
, one
);
435 case PIPE_FUNC_LEQUAL
:
436 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
437 depth_output
= qir_SEL_X_0_NC(c
, one
);
441 depth_output
= normalized
;
444 for (int i
= 0; i
< 4; i
++)
445 unpacked
[i
] = depth_output
;
447 for (int i
= 0; i
< 4; i
++)
448 unpacked
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
451 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
452 struct qreg texture_output
[4];
453 for (int i
= 0; i
< 4; i
++) {
454 texture_output
[i
] = get_swizzled_channel(c
, unpacked
,
458 if (util_format_is_srgb(format
)) {
459 for (int i
= 0; i
< 3; i
++)
460 texture_output
[i
] = qir_srgb_decode(c
,
464 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
);
465 for (int i
= 0; i
< 4; i
++) {
466 dest
[i
] = get_swizzled_channel(c
, texture_output
,
467 c
->key
->tex
[unit
].swizzle
[i
]);
472 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
476 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
478 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
479 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
481 return qir_SEL_X_Y_NS(c
,
482 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
487 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
491 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
493 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
495 /* This will be < 0 if we truncated and the truncation was of a value
496 * that was < 0 in the first place.
498 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
500 return qir_SEL_X_Y_NS(c
,
501 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
506 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
510 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
512 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
514 /* This will be < 0 if we truncated and the truncation was of a value
515 * that was > 0 in the first place.
517 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
519 return qir_SEL_X_Y_NS(c
,
520 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)),
525 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
529 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
530 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
531 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
532 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
535 struct qreg scaled_x
=
538 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
540 struct qreg x
= qir_FADD(c
,
541 ntq_ffract(c
, scaled_x
),
542 qir_uniform_f(c
, -0.5));
543 struct qreg x2
= qir_FMUL(c
, x
, x
);
544 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
545 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
546 x
= qir_FMUL(c
, x
, x2
);
551 qir_uniform_f(c
, coeff
[i
])));
557 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
561 pow(2.0 * M_PI
, 2) / (2 * 1),
562 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
563 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
564 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
565 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
568 struct qreg scaled_x
=
570 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
571 struct qreg x_frac
= qir_FADD(c
,
572 ntq_ffract(c
, scaled_x
),
573 qir_uniform_f(c
, -0.5));
575 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
576 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
577 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
578 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
580 x
= qir_FMUL(c
, x
, x2
);
582 struct qreg mul
= qir_FMUL(c
,
584 qir_uniform_f(c
, coeff
[i
]));
588 sum
= qir_FADD(c
, sum
, mul
);
594 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
597 return qir_SEL_X_Y_NC(c
,
598 qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0)),
599 qir_uniform_f(c
, -1.0));
603 get_channel_from_vpm(struct vc4_compile
*c
,
604 struct qreg
*vpm_reads
,
606 const struct util_format_description
*desc
)
608 const struct util_format_channel_description
*chan
=
609 &desc
->channel
[swiz
];
612 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
613 return get_swizzled_channel(c
, vpm_reads
, swiz
);
614 else if (chan
->size
== 32 &&
615 chan
->type
== UTIL_FORMAT_TYPE_FLOAT
) {
616 return get_swizzled_channel(c
, vpm_reads
, swiz
);
617 } else if (chan
->size
== 32 &&
618 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
619 if (chan
->normalized
) {
621 qir_ITOF(c
, vpm_reads
[swiz
]),
625 return qir_ITOF(c
, vpm_reads
[swiz
]);
627 } else if (chan
->size
== 8 &&
628 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
629 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
630 struct qreg vpm
= vpm_reads
[0];
631 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
632 temp
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
633 if (chan
->normalized
) {
634 return qir_FSUB(c
, qir_FMUL(c
,
635 qir_UNPACK_8_F(c
, temp
, swiz
),
636 qir_uniform_f(c
, 2.0)),
637 qir_uniform_f(c
, 1.0));
641 qir_UNPACK_8_I(c
, temp
,
643 qir_uniform_f(c
, -128.0));
646 if (chan
->normalized
) {
647 return qir_UNPACK_8_F(c
, vpm
, swiz
);
649 return qir_ITOF(c
, qir_UNPACK_8_I(c
, vpm
, swiz
));
652 } else if (chan
->size
== 16 &&
653 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
654 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
655 struct qreg vpm
= vpm_reads
[swiz
/ 2];
657 /* Note that UNPACK_16F eats a half float, not ints, so we use
658 * UNPACK_16_I for all of these.
660 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
661 temp
= qir_ITOF(c
, qir_UNPACK_16_I(c
, vpm
, swiz
% 2));
662 if (chan
->normalized
) {
663 return qir_FMUL(c
, temp
,
664 qir_uniform_f(c
, 1/32768.0f
));
669 /* UNPACK_16I sign-extends, so we have to emit ANDs. */
671 if (swiz
== 1 || swiz
== 3)
672 temp
= qir_UNPACK_16_I(c
, temp
, 1);
673 temp
= qir_AND(c
, temp
, qir_uniform_ui(c
, 0xffff));
674 temp
= qir_ITOF(c
, temp
);
676 if (chan
->normalized
) {
677 return qir_FMUL(c
, temp
,
678 qir_uniform_f(c
, 1 / 65535.0));
689 emit_vertex_input(struct vc4_compile
*c
, int attr
)
691 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
692 uint32_t attr_size
= util_format_get_blocksize(format
);
693 struct qreg vpm_reads
[4];
695 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
696 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
697 struct qreg vpm
= { QFILE_VPM
, attr
* 4 + i
};
698 vpm_reads
[i
] = qir_MOV(c
, vpm
);
702 bool format_warned
= false;
703 const struct util_format_description
*desc
=
704 util_format_description(format
);
706 for (int i
= 0; i
< 4; i
++) {
707 uint8_t swiz
= desc
->swizzle
[i
];
708 struct qreg result
= get_channel_from_vpm(c
, vpm_reads
,
711 if (result
.file
== QFILE_NULL
) {
712 if (!format_warned
) {
714 "vtx element %d unsupported type: %s\n",
715 attr
, util_format_name(format
));
716 format_warned
= true;
718 result
= qir_uniform_f(c
, 0.0);
720 c
->inputs
[attr
* 4 + i
] = result
;
725 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
727 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
728 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
729 c
->inputs
[attr
* 4 + 2] =
731 qir_ITOF(c
, qir_FRAG_Z(c
)),
732 qir_uniform_f(c
, 1.0 / 0xffffff));
733 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
737 emit_fragment_varying(struct vc4_compile
*c
, uint8_t semantic
,
738 uint8_t index
, uint8_t swizzle
)
740 uint32_t i
= c
->num_input_semantics
++;
746 if (c
->num_input_semantics
>= c
->input_semantics_array_size
) {
747 c
->input_semantics_array_size
=
748 MAX2(4, c
->input_semantics_array_size
* 2);
750 c
->input_semantics
= reralloc(c
, c
->input_semantics
,
751 struct vc4_varying_semantic
,
752 c
->input_semantics_array_size
);
755 c
->input_semantics
[i
].semantic
= semantic
;
756 c
->input_semantics
[i
].index
= index
;
757 c
->input_semantics
[i
].swizzle
= swizzle
;
759 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
763 emit_fragment_input(struct vc4_compile
*c
, int attr
,
764 unsigned semantic_name
, unsigned semantic_index
)
766 for (int i
= 0; i
< 4; i
++) {
767 c
->inputs
[attr
* 4 + i
] =
768 emit_fragment_varying(c
,
777 add_output(struct vc4_compile
*c
,
778 uint32_t decl_offset
,
779 uint8_t semantic_name
,
780 uint8_t semantic_index
,
781 uint8_t semantic_swizzle
)
783 uint32_t old_array_size
= c
->outputs_array_size
;
784 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
787 if (old_array_size
!= c
->outputs_array_size
) {
788 c
->output_semantics
= reralloc(c
,
790 struct vc4_varying_semantic
,
791 c
->outputs_array_size
);
794 c
->output_semantics
[decl_offset
].semantic
= semantic_name
;
795 c
->output_semantics
[decl_offset
].index
= semantic_index
;
796 c
->output_semantics
[decl_offset
].swizzle
= semantic_swizzle
;
800 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
802 unsigned array_id
= c
->num_uniform_ranges
++;
803 if (array_id
>= c
->ubo_ranges_array_size
) {
804 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
806 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
807 struct vc4_compiler_ubo_range
,
808 c
->ubo_ranges_array_size
);
811 c
->ubo_ranges
[array_id
].dst_offset
= 0;
812 c
->ubo_ranges
[array_id
].src_offset
= start
;
813 c
->ubo_ranges
[array_id
].size
= size
;
814 c
->ubo_ranges
[array_id
].used
= false;
818 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
820 /* Vectors are special in that they have non-scalarized writemasks,
821 * and just take the first swizzle channel for each argument in order
822 * into each writemask channel.
824 if (instr
->op
== nir_op_vec2
||
825 instr
->op
== nir_op_vec3
||
826 instr
->op
== nir_op_vec4
) {
828 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
829 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
830 instr
->src
[i
].swizzle
[0]);
831 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
832 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
837 /* General case: We can just grab the one used channel per src. */
838 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
839 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
840 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
843 /* Pick the channel to store the output in. */
844 assert(!instr
->dest
.saturate
);
845 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
846 assert(util_is_power_of_two(instr
->dest
.write_mask
));
847 dest
+= ffs(instr
->dest
.write_mask
) - 1;
852 *dest
= qir_MOV(c
, src
[0]);
855 *dest
= qir_FMUL(c
, src
[0], src
[1]);
858 *dest
= qir_FADD(c
, src
[0], src
[1]);
861 *dest
= qir_FSUB(c
, src
[0], src
[1]);
864 *dest
= qir_FMIN(c
, src
[0], src
[1]);
867 *dest
= qir_FMAX(c
, src
[0], src
[1]);
872 *dest
= qir_FTOI(c
, src
[0]);
876 *dest
= qir_ITOF(c
, src
[0]);
879 *dest
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
882 *dest
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
887 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
891 *dest
= qir_ADD(c
, src
[0], src
[1]);
894 *dest
= qir_SHR(c
, src
[0], src
[1]);
897 *dest
= qir_SUB(c
, src
[0], src
[1]);
900 *dest
= qir_ASR(c
, src
[0], src
[1]);
903 *dest
= qir_SHL(c
, src
[0], src
[1]);
906 *dest
= qir_MIN(c
, src
[0], src
[1]);
909 *dest
= qir_MAX(c
, src
[0], src
[1]);
912 *dest
= qir_AND(c
, src
[0], src
[1]);
915 *dest
= qir_OR(c
, src
[0], src
[1]);
918 *dest
= qir_XOR(c
, src
[0], src
[1]);
921 *dest
= qir_NOT(c
, src
[0]);
925 *dest
= ntq_umul(c
, src
[0], src
[1]);
929 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
930 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
933 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
934 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
937 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
938 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
941 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
942 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
945 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
946 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
949 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
950 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
953 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
954 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
957 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
958 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
961 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
962 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
965 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
966 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
969 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
970 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
973 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
974 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
979 *dest
= qir_SEL_X_Y_NS(c
, src
[1], src
[2]);
983 *dest
= qir_SEL_X_Y_ZC(c
, src
[1], src
[2]);
987 *dest
= ntq_rcp(c
, src
[0]);
990 *dest
= ntq_rsq(c
, src
[0]);
993 *dest
= qir_EXP2(c
, src
[0]);
996 *dest
= qir_LOG2(c
, src
[0]);
1000 *dest
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1003 *dest
= ntq_fceil(c
, src
[0]);
1006 *dest
= ntq_ffract(c
, src
[0]);
1009 *dest
= ntq_ffloor(c
, src
[0]);
1013 *dest
= ntq_fsin(c
, src
[0]);
1016 *dest
= ntq_fcos(c
, src
[0]);
1020 *dest
= ntq_fsign(c
, src
[0]);
1024 *dest
= qir_FMAXABS(c
, src
[0], src
[0]);
1027 *dest
= qir_MAX(c
, src
[0],
1028 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1032 fprintf(stderr
, "unknown NIR ALU inst: ");
1033 nir_print_instr(&instr
->instr
, stderr
);
1034 fprintf(stderr
, "\n");
1040 vc4_blend_channel(struct vc4_compile
*c
,
1048 case PIPE_BLENDFACTOR_ONE
:
1050 case PIPE_BLENDFACTOR_SRC_COLOR
:
1051 return qir_FMUL(c
, val
, src
[channel
]);
1052 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1053 return qir_FMUL(c
, val
, src
[3]);
1054 case PIPE_BLENDFACTOR_DST_ALPHA
:
1055 return qir_FMUL(c
, val
, dst
[3]);
1056 case PIPE_BLENDFACTOR_DST_COLOR
:
1057 return qir_FMUL(c
, val
, dst
[channel
]);
1058 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1065 qir_uniform_f(c
, 1.0),
1070 case PIPE_BLENDFACTOR_CONST_COLOR
:
1071 return qir_FMUL(c
, val
,
1072 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
,
1074 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1075 return qir_FMUL(c
, val
,
1076 qir_uniform(c
, QUNIFORM_BLEND_CONST_COLOR
, 3));
1077 case PIPE_BLENDFACTOR_ZERO
:
1078 return qir_uniform_f(c
, 0.0);
1079 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1080 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1082 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1083 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1085 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1086 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1088 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1089 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1091 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1092 return qir_FMUL(c
, val
,
1093 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1095 QUNIFORM_BLEND_CONST_COLOR
,
1097 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1098 return qir_FMUL(c
, val
,
1099 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1101 QUNIFORM_BLEND_CONST_COLOR
,
1105 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1106 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1107 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1108 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1110 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1116 vc4_blend_func(struct vc4_compile
*c
,
1117 struct qreg src
, struct qreg dst
,
1121 case PIPE_BLEND_ADD
:
1122 return qir_FADD(c
, src
, dst
);
1123 case PIPE_BLEND_SUBTRACT
:
1124 return qir_FSUB(c
, src
, dst
);
1125 case PIPE_BLEND_REVERSE_SUBTRACT
:
1126 return qir_FSUB(c
, dst
, src
);
1127 case PIPE_BLEND_MIN
:
1128 return qir_FMIN(c
, src
, dst
);
1129 case PIPE_BLEND_MAX
:
1130 return qir_FMAX(c
, src
, dst
);
1134 fprintf(stderr
, "Unknown blend func %d\n", func
);
1141 * Implements fixed function blending in shader code.
1143 * VC4 doesn't have any hardware support for blending. Instead, you read the
1144 * current contents of the destination from the tile buffer after having
1145 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1146 * math using your output color and that destination value, and update the
1147 * output color appropriately.
1150 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1151 struct qreg
*dst_color
, struct qreg
*src_color
)
1153 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1155 if (!blend
->blend_enable
) {
1156 for (int i
= 0; i
< 4; i
++)
1157 result
[i
] = src_color
[i
];
1161 for (int i
= 0; i
< 4; i
++)
1162 src_color
[i
] = qir_SAT(c
, src_color
[i
]);
1164 struct qreg src_blend
[4], dst_blend
[4];
1165 for (int i
= 0; i
< 3; i
++) {
1166 src_blend
[i
] = vc4_blend_channel(c
,
1167 dst_color
, src_color
,
1169 blend
->rgb_src_factor
, i
);
1170 dst_blend
[i
] = vc4_blend_channel(c
,
1171 dst_color
, src_color
,
1173 blend
->rgb_dst_factor
, i
);
1175 src_blend
[3] = vc4_blend_channel(c
,
1176 dst_color
, src_color
,
1178 blend
->alpha_src_factor
, 3);
1179 dst_blend
[3] = vc4_blend_channel(c
,
1180 dst_color
, src_color
,
1182 blend
->alpha_dst_factor
, 3);
1184 for (int i
= 0; i
< 3; i
++) {
1185 result
[i
] = vc4_blend_func(c
,
1186 src_blend
[i
], dst_blend
[i
],
1189 result
[3] = vc4_blend_func(c
,
1190 src_blend
[3], dst_blend
[3],
1195 clip_distance_discard(struct vc4_compile
*c
)
1197 for (int i
= 0; i
< PIPE_MAX_CLIP_PLANES
; i
++) {
1198 if (!(c
->key
->ucp_enables
& (1 << i
)))
1201 struct qreg dist
= emit_fragment_varying(c
,
1202 TGSI_SEMANTIC_CLIPDIST
,
1208 if (c
->discard
.file
== QFILE_NULL
)
1209 c
->discard
= qir_uniform_ui(c
, 0);
1211 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_ui(c
, ~0),
1217 alpha_test_discard(struct vc4_compile
*c
)
1219 struct qreg src_alpha
;
1220 struct qreg alpha_ref
= qir_uniform(c
, QUNIFORM_ALPHA_REF
, 0);
1222 if (!c
->fs_key
->alpha_test
)
1225 if (c
->output_color_index
!= -1)
1226 src_alpha
= c
->outputs
[c
->output_color_index
+ 3];
1228 src_alpha
= qir_uniform_f(c
, 1.0);
1230 if (c
->discard
.file
== QFILE_NULL
)
1231 c
->discard
= qir_uniform_ui(c
, 0);
1233 switch (c
->fs_key
->alpha_test_func
) {
1234 case PIPE_FUNC_NEVER
:
1235 c
->discard
= qir_uniform_ui(c
, ~0);
1237 case PIPE_FUNC_ALWAYS
:
1239 case PIPE_FUNC_EQUAL
:
1240 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1241 c
->discard
= qir_SEL_X_Y_ZS(c
, c
->discard
,
1242 qir_uniform_ui(c
, ~0));
1244 case PIPE_FUNC_NOTEQUAL
:
1245 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1246 c
->discard
= qir_SEL_X_Y_ZC(c
, c
->discard
,
1247 qir_uniform_ui(c
, ~0));
1249 case PIPE_FUNC_GREATER
:
1250 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1251 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1252 qir_uniform_ui(c
, ~0));
1254 case PIPE_FUNC_GEQUAL
:
1255 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1256 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1257 qir_uniform_ui(c
, ~0));
1259 case PIPE_FUNC_LESS
:
1260 qir_SF(c
, qir_FSUB(c
, src_alpha
, alpha_ref
));
1261 c
->discard
= qir_SEL_X_Y_NS(c
, c
->discard
,
1262 qir_uniform_ui(c
, ~0));
1264 case PIPE_FUNC_LEQUAL
:
1265 qir_SF(c
, qir_FSUB(c
, alpha_ref
, src_alpha
));
1266 c
->discard
= qir_SEL_X_Y_NC(c
, c
->discard
,
1267 qir_uniform_ui(c
, ~0));
1273 vc4_logicop(struct vc4_compile
*c
, struct qreg src
, struct qreg dst
)
1275 switch (c
->fs_key
->logicop_func
) {
1276 case PIPE_LOGICOP_CLEAR
:
1277 return qir_uniform_f(c
, 0.0);
1278 case PIPE_LOGICOP_NOR
:
1279 return qir_NOT(c
, qir_OR(c
, src
, dst
));
1280 case PIPE_LOGICOP_AND_INVERTED
:
1281 return qir_AND(c
, qir_NOT(c
, src
), dst
);
1282 case PIPE_LOGICOP_COPY_INVERTED
:
1283 return qir_NOT(c
, src
);
1284 case PIPE_LOGICOP_AND_REVERSE
:
1285 return qir_AND(c
, src
, qir_NOT(c
, dst
));
1286 case PIPE_LOGICOP_INVERT
:
1287 return qir_NOT(c
, dst
);
1288 case PIPE_LOGICOP_XOR
:
1289 return qir_XOR(c
, src
, dst
);
1290 case PIPE_LOGICOP_NAND
:
1291 return qir_NOT(c
, qir_AND(c
, src
, dst
));
1292 case PIPE_LOGICOP_AND
:
1293 return qir_AND(c
, src
, dst
);
1294 case PIPE_LOGICOP_EQUIV
:
1295 return qir_NOT(c
, qir_XOR(c
, src
, dst
));
1296 case PIPE_LOGICOP_NOOP
:
1298 case PIPE_LOGICOP_OR_INVERTED
:
1299 return qir_OR(c
, qir_NOT(c
, src
), dst
);
1300 case PIPE_LOGICOP_OR_REVERSE
:
1301 return qir_OR(c
, src
, qir_NOT(c
, dst
));
1302 case PIPE_LOGICOP_OR
:
1303 return qir_OR(c
, src
, dst
);
1304 case PIPE_LOGICOP_SET
:
1305 return qir_uniform_ui(c
, ~0);
1306 case PIPE_LOGICOP_COPY
:
1313 * Applies the GL blending pipeline and returns the packed (8888) output
1317 blend_pipeline(struct vc4_compile
*c
)
1319 enum pipe_format color_format
= c
->fs_key
->color_format
;
1320 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1321 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1322 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1323 struct qreg linear_dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1324 struct qreg packed_dst_color
= c
->undef
;
1326 if (c
->fs_key
->blend
.blend_enable
||
1327 c
->fs_key
->blend
.colormask
!= 0xf ||
1328 c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1329 packed_dst_color
= qir_TLB_COLOR_READ(c
);
1330 for (int i
= 0; i
< 4; i
++)
1331 tlb_read_color
[i
] = qir_UNPACK_8_F(c
,
1332 packed_dst_color
, i
);
1333 for (int i
= 0; i
< 4; i
++) {
1334 dst_color
[i
] = get_swizzled_channel(c
,
1337 if (util_format_is_srgb(color_format
) && i
!= 3) {
1338 linear_dst_color
[i
] =
1339 qir_srgb_decode(c
, dst_color
[i
]);
1341 linear_dst_color
[i
] = dst_color
[i
];
1346 struct qreg undef_array
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1347 const struct qreg
*output_colors
= (c
->output_color_index
!= -1 ?
1348 c
->outputs
+ c
->output_color_index
:
1350 struct qreg blend_src_color
[4];
1351 for (int i
= 0; i
< 4; i
++)
1352 blend_src_color
[i
] = output_colors
[i
];
1354 struct qreg blend_color
[4];
1355 vc4_blend(c
, blend_color
, linear_dst_color
, blend_src_color
);
1357 if (util_format_is_srgb(color_format
)) {
1358 for (int i
= 0; i
< 3; i
++)
1359 blend_color
[i
] = qir_srgb_encode(c
, blend_color
[i
]);
1362 /* Debug: Sometimes you're getting a black output and just want to see
1363 * if the FS is getting executed at all. Spam magenta into the color
1367 blend_color
[0] = qir_uniform_f(c
, 1.0);
1368 blend_color
[1] = qir_uniform_f(c
, 0.0);
1369 blend_color
[2] = qir_uniform_f(c
, 1.0);
1370 blend_color
[3] = qir_uniform_f(c
, 0.5);
1373 struct qreg swizzled_outputs
[4];
1374 for (int i
= 0; i
< 4; i
++) {
1375 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1379 struct qreg packed_color
= c
->undef
;
1380 for (int i
= 0; i
< 4; i
++) {
1381 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1383 if (packed_color
.file
== QFILE_NULL
) {
1384 packed_color
= qir_PACK_8888_F(c
, swizzled_outputs
[i
]);
1386 packed_color
= qir_PACK_8_F(c
,
1388 swizzled_outputs
[i
],
1393 if (packed_color
.file
== QFILE_NULL
)
1394 packed_color
= qir_uniform_ui(c
, 0);
1396 if (c
->fs_key
->logicop_func
!= PIPE_LOGICOP_COPY
) {
1397 packed_color
= vc4_logicop(c
, packed_color
, packed_dst_color
);
1400 /* If the bit isn't set in the color mask, then just return the
1401 * original dst color, instead.
1403 uint32_t colormask
= 0xffffffff;
1404 for (int i
= 0; i
< 4; i
++) {
1405 if (format_swiz
[i
] < 4 &&
1406 !(c
->fs_key
->blend
.colormask
& (1 << format_swiz
[i
]))) {
1407 colormask
&= ~(0xff << (i
* 8));
1410 if (colormask
!= 0xffffffff) {
1411 packed_color
= qir_OR(c
,
1412 qir_AND(c
, packed_color
,
1413 qir_uniform_ui(c
, colormask
)),
1414 qir_AND(c
, packed_dst_color
,
1415 qir_uniform_ui(c
, ~colormask
)));
1418 return packed_color
;
1422 emit_frag_end(struct vc4_compile
*c
)
1424 clip_distance_discard(c
);
1425 alpha_test_discard(c
);
1426 struct qreg color
= blend_pipeline(c
);
1428 if (c
->discard
.file
!= QFILE_NULL
)
1429 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1431 if (c
->fs_key
->stencil_enabled
) {
1432 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1433 if (c
->fs_key
->stencil_twoside
) {
1434 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1436 if (c
->fs_key
->stencil_full_writemasks
) {
1437 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1441 if (c
->fs_key
->depth_enabled
) {
1443 if (c
->output_position_index
!= -1) {
1444 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1445 qir_uniform_f(c
, 0xffffff)));
1449 qir_TLB_Z_WRITE(c
, z
);
1452 qir_TLB_COLOR_WRITE(c
, color
);
1456 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1460 for (int i
= 0; i
< 2; i
++) {
1462 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1464 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1466 c
->outputs
[c
->output_position_index
+ i
],
1471 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1475 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1477 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1478 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1480 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1481 c
->outputs
[c
->output_position_index
+ 2],
1488 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1490 qir_VPM_WRITE(c
, rcp_w
);
1494 emit_point_size_write(struct vc4_compile
*c
)
1496 struct qreg point_size
;
1498 if (c
->output_point_size_index
!= -1)
1499 point_size
= c
->outputs
[c
->output_point_size_index
+ 3];
1501 point_size
= qir_uniform_f(c
, 1.0);
1503 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1506 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1508 qir_VPM_WRITE(c
, point_size
);
1512 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1514 * The simulator insists that there be at least one vertex attribute, so
1515 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1516 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1517 * to consume it here.
1520 emit_stub_vpm_read(struct vc4_compile
*c
)
1525 c
->vattr_sizes
[0] = 4;
1526 struct qreg vpm
= { QFILE_VPM
, 0 };
1527 (void)qir_MOV(c
, vpm
);
1532 emit_ucp_clipdistance(struct vc4_compile
*c
)
1535 if (c
->output_clipvertex_index
!= -1)
1536 cv
= c
->output_clipvertex_index
;
1537 else if (c
->output_position_index
!= -1)
1538 cv
= c
->output_position_index
;
1542 for (int plane
= 0; plane
< PIPE_MAX_CLIP_PLANES
; plane
++) {
1543 if (!(c
->key
->ucp_enables
& (1 << plane
)))
1546 /* Pick the next outputs[] that hasn't been written to, since
1547 * there are no other program writes left to be processed at
1548 * this point. If something had been declared but not written
1549 * (like a w component), we'll just smash over the top of it.
1551 uint32_t output_index
= c
->num_outputs
++;
1552 add_output(c
, output_index
,
1553 TGSI_SEMANTIC_CLIPDIST
,
1558 struct qreg dist
= qir_uniform_f(c
, 0.0);
1559 for (int i
= 0; i
< 4; i
++) {
1560 struct qreg pos_chan
= c
->outputs
[cv
+ i
];
1562 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1564 dist
= qir_FADD(c
, dist
, qir_FMUL(c
, pos_chan
, ucp
));
1567 c
->outputs
[output_index
] = dist
;
1572 emit_vert_end(struct vc4_compile
*c
,
1573 struct vc4_varying_semantic
*fs_inputs
,
1574 uint32_t num_fs_inputs
)
1576 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1578 emit_stub_vpm_read(c
);
1579 emit_ucp_clipdistance(c
);
1581 emit_scaled_viewport_write(c
, rcp_w
);
1582 emit_zs_write(c
, rcp_w
);
1583 emit_rcp_wc_write(c
, rcp_w
);
1584 if (c
->vs_key
->per_vertex_point_size
)
1585 emit_point_size_write(c
);
1587 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1588 struct vc4_varying_semantic
*input
= &fs_inputs
[i
];
1591 for (j
= 0; j
< c
->num_outputs
; j
++) {
1592 struct vc4_varying_semantic
*output
=
1593 &c
->output_semantics
[j
];
1595 if (input
->semantic
== output
->semantic
&&
1596 input
->index
== output
->index
&&
1597 input
->swizzle
== output
->swizzle
) {
1598 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1602 /* Emit padding if we didn't find a declared VS output for
1605 if (j
== c
->num_outputs
)
1606 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1611 emit_coord_end(struct vc4_compile
*c
)
1613 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1615 emit_stub_vpm_read(c
);
1617 for (int i
= 0; i
< 4; i
++)
1618 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1620 emit_scaled_viewport_write(c
, rcp_w
);
1621 emit_zs_write(c
, rcp_w
);
1622 emit_rcp_wc_write(c
, rcp_w
);
1623 if (c
->vs_key
->per_vertex_point_size
)
1624 emit_point_size_write(c
);
1628 vc4_optimize_nir(struct nir_shader
*s
)
1635 nir_lower_vars_to_ssa(s
);
1636 nir_lower_alu_to_scalar(s
);
1638 progress
= nir_copy_prop(s
) || progress
;
1639 progress
= nir_opt_dce(s
) || progress
;
1640 progress
= nir_opt_cse(s
) || progress
;
1641 progress
= nir_opt_peephole_select(s
) || progress
;
1642 progress
= nir_opt_algebraic(s
) || progress
;
1643 progress
= nir_opt_constant_folding(s
) || progress
;
1644 progress
= nir_opt_undef(s
) || progress
;
1649 driver_location_compare(const void *in_a
, const void *in_b
)
1651 const nir_variable
*const *a
= in_a
;
1652 const nir_variable
*const *b
= in_b
;
1654 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1658 ntq_setup_inputs(struct vc4_compile
*c
)
1660 unsigned num_entries
= 0;
1661 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1664 nir_variable
*vars
[num_entries
];
1667 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1670 /* Sort the variables so that we emit the input setup in
1671 * driver_location order. This is required for VPM reads, whose data
1672 * is fetched into the VPM in driver_location (TGSI register index)
1675 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1677 for (unsigned i
= 0; i
< num_entries
; i
++) {
1678 nir_variable
*var
= vars
[i
];
1679 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1680 /* XXX: map loc slots to semantics */
1681 unsigned semantic_name
= var
->data
.location
;
1682 unsigned semantic_index
= var
->data
.index
;
1683 unsigned loc
= var
->data
.driver_location
;
1685 assert(array_len
== 1);
1687 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1690 if (c
->stage
== QSTAGE_FRAG
) {
1691 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1692 emit_fragcoord_input(c
, loc
);
1693 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1694 c
->inputs
[loc
* 4 + 0] = qir_FRAG_REV_FLAG(c
);
1695 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1696 (c
->fs_key
->point_sprite_mask
&
1697 (1 << semantic_index
))) {
1698 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1699 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1701 emit_fragment_input(c
, loc
,
1706 emit_vertex_input(c
, loc
);
1712 ntq_setup_outputs(struct vc4_compile
*c
)
1714 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->outputs
) {
1715 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1716 /* XXX: map loc slots to semantics */
1717 unsigned semantic_name
= var
->data
.location
;
1718 unsigned semantic_index
= var
->data
.index
;
1719 unsigned loc
= var
->data
.driver_location
* 4;
1721 assert(array_len
== 1);
1724 /* NIR hack to pass through
1725 * TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS */
1726 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1727 semantic_index
== -1)
1730 for (int i
= 0; i
< 4; i
++) {
1738 switch (semantic_name
) {
1739 case TGSI_SEMANTIC_POSITION
:
1740 c
->output_position_index
= loc
;
1742 case TGSI_SEMANTIC_CLIPVERTEX
:
1743 c
->output_clipvertex_index
= loc
;
1745 case TGSI_SEMANTIC_COLOR
:
1746 c
->output_color_index
= loc
;
1748 case TGSI_SEMANTIC_PSIZE
:
1749 c
->output_point_size_index
= loc
;
1757 ntq_setup_uniforms(struct vc4_compile
*c
)
1759 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->uniforms
) {
1760 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1761 unsigned array_elem_size
= 4 * sizeof(float);
1763 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1764 array_len
* array_elem_size
);
1770 * Sets up the mapping from nir_register to struct qreg *.
1772 * Each nir_register gets a struct qreg per 32-bit component being stored.
1775 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1777 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1778 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1779 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1781 nir_reg
->num_components
);
1783 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1785 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1786 qregs
[i
] = qir_uniform_ui(c
, 0);
1791 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1793 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1794 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1795 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u
[i
]);
1797 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1801 ntq_emit_ssa_undef(struct vc4_compile
*c
, nir_ssa_undef_instr
*instr
)
1803 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1805 /* QIR needs there to be *some* value, so pick 0 (same as for
1806 * ntq_setup_registers().
1808 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1809 qregs
[i
] = qir_uniform_ui(c
, 0);
1813 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1815 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1816 struct qreg
*dest
= NULL
;
1818 if (info
->has_dest
) {
1819 dest
= ntq_get_dest(c
, &instr
->dest
);
1822 switch (instr
->intrinsic
) {
1823 case nir_intrinsic_load_uniform
:
1824 assert(instr
->num_components
== 1);
1825 if (instr
->const_index
[0] < VC4_NIR_STATE_UNIFORM_OFFSET
) {
1826 *dest
= qir_uniform(c
, QUNIFORM_UNIFORM
,
1827 instr
->const_index
[0]);
1829 *dest
= qir_uniform(c
, instr
->const_index
[0] -
1830 VC4_NIR_STATE_UNIFORM_OFFSET
,
1835 case nir_intrinsic_load_uniform_indirect
:
1836 *dest
= indirect_uniform_load(c
, instr
);
1840 case nir_intrinsic_load_input
:
1841 assert(instr
->num_components
== 1);
1842 *dest
= c
->inputs
[instr
->const_index
[0]];
1846 case nir_intrinsic_store_output
:
1847 assert(instr
->num_components
== 1);
1848 c
->outputs
[instr
->const_index
[0]] =
1849 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1850 c
->num_outputs
= MAX2(c
->num_outputs
, instr
->const_index
[0] + 1);
1853 case nir_intrinsic_discard
:
1854 c
->discard
= qir_uniform_ui(c
, ~0);
1857 case nir_intrinsic_discard_if
:
1858 if (c
->discard
.file
== QFILE_NULL
)
1859 c
->discard
= qir_uniform_ui(c
, 0);
1860 c
->discard
= qir_OR(c
, c
->discard
,
1861 ntq_get_src(c
, instr
->src
[0], 0));
1865 fprintf(stderr
, "Unknown intrinsic: ");
1866 nir_print_instr(&instr
->instr
, stderr
);
1867 fprintf(stderr
, "\n");
1873 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1875 fprintf(stderr
, "general IF statements not handled.\n");
1879 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1881 switch (instr
->type
) {
1882 case nir_instr_type_alu
:
1883 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1886 case nir_instr_type_intrinsic
:
1887 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1890 case nir_instr_type_load_const
:
1891 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1894 case nir_instr_type_ssa_undef
:
1895 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
1898 case nir_instr_type_tex
:
1899 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1903 fprintf(stderr
, "Unknown NIR instr type: ");
1904 nir_print_instr(instr
, stderr
);
1905 fprintf(stderr
, "\n");
1911 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1913 nir_foreach_instr(block
, instr
) {
1914 ntq_emit_instr(c
, instr
);
1919 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1921 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1922 switch (node
->type
) {
1923 /* case nir_cf_node_loop: */
1924 case nir_cf_node_block
:
1925 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1928 case nir_cf_node_if
:
1929 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1939 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1941 ntq_setup_registers(c
, &impl
->registers
);
1942 ntq_emit_cf_list(c
, &impl
->body
);
1946 nir_to_qir(struct vc4_compile
*c
)
1948 ntq_setup_inputs(c
);
1949 ntq_setup_outputs(c
);
1950 ntq_setup_uniforms(c
);
1951 ntq_setup_registers(c
, &c
->s
->registers
);
1953 /* Find the main function and emit the body. */
1954 nir_foreach_overload(c
->s
, overload
) {
1955 assert(strcmp(overload
->function
->name
, "main") == 0);
1956 assert(overload
->impl
);
1957 ntq_emit_impl(c
, overload
->impl
);
1961 static const nir_shader_compiler_options nir_options
= {
1966 .lower_fsqrt
= true,
1967 .lower_negate
= true,
1971 count_nir_instrs_in_block(nir_block
*block
, void *state
)
1973 int *count
= (int *) state
;
1974 nir_foreach_instr(block
, instr
) {
1975 *count
= *count
+ 1;
1981 count_nir_instrs(nir_shader
*nir
)
1984 nir_foreach_overload(nir
, overload
) {
1985 if (!overload
->impl
)
1987 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
1992 static struct vc4_compile
*
1993 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
1994 struct vc4_key
*key
)
1996 struct vc4_compile
*c
= qir_compile_init();
1999 c
->shader_state
= &key
->shader_state
->base
;
2000 c
->program_id
= key
->shader_state
->program_id
;
2001 c
->variant_id
= key
->shader_state
->compiled_variant_count
++;
2006 c
->fs_key
= (struct vc4_fs_key
*)key
;
2007 if (c
->fs_key
->is_points
) {
2008 c
->point_x
= emit_fragment_varying(c
, ~0, ~0, 0);
2009 c
->point_y
= emit_fragment_varying(c
, ~0, ~0, 0);
2010 } else if (c
->fs_key
->is_lines
) {
2011 c
->line_x
= emit_fragment_varying(c
, ~0, ~0, 0);
2015 c
->vs_key
= (struct vc4_vs_key
*)key
;
2018 c
->vs_key
= (struct vc4_vs_key
*)key
;
2022 const struct tgsi_token
*tokens
= key
->shader_state
->base
.tokens
;
2023 if (c
->fs_key
&& c
->fs_key
->light_twoside
) {
2024 if (!key
->shader_state
->twoside_tokens
) {
2025 const struct tgsi_lowering_config lowering_config
= {
2026 .color_two_side
= true,
2028 struct tgsi_shader_info info
;
2029 key
->shader_state
->twoside_tokens
=
2030 tgsi_transform_lowering(&lowering_config
,
2031 key
->shader_state
->base
.tokens
,
2034 /* If no transformation occurred, then NULL is
2035 * returned and we just use our original tokens.
2037 if (!key
->shader_state
->twoside_tokens
) {
2038 key
->shader_state
->twoside_tokens
=
2039 key
->shader_state
->base
.tokens
;
2042 tokens
= key
->shader_state
->twoside_tokens
;
2045 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2046 fprintf(stderr
, "%s prog %d/%d TGSI:\n",
2047 qir_get_stage_name(c
->stage
),
2048 c
->program_id
, c
->variant_id
);
2049 tgsi_dump(tokens
, 0);
2052 c
->s
= tgsi_to_nir(tokens
, &nir_options
);
2053 nir_opt_global_to_local(c
->s
);
2054 nir_convert_to_ssa(c
->s
);
2055 vc4_nir_lower_io(c
);
2056 nir_lower_idiv(c
->s
);
2057 nir_lower_load_const_to_scalar(c
->s
);
2059 vc4_optimize_nir(c
->s
);
2061 nir_remove_dead_variables(c
->s
);
2063 nir_convert_from_ssa(c
->s
, true);
2065 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2066 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2067 qir_get_stage_name(c
->stage
),
2068 c
->program_id
, c
->variant_id
,
2069 count_nir_instrs(c
->s
));
2072 if (vc4_debug
& VC4_DEBUG_NIR
) {
2073 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2074 qir_get_stage_name(c
->stage
),
2075 c
->program_id
, c
->variant_id
);
2076 nir_print_shader(c
->s
, stderr
);
2087 vc4
->prog
.fs
->input_semantics
,
2088 vc4
->prog
.fs
->num_inputs
);
2095 if (vc4_debug
& VC4_DEBUG_QIR
) {
2096 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2097 qir_get_stage_name(c
->stage
),
2098 c
->program_id
, c
->variant_id
);
2103 qir_lower_uniforms(c
);
2105 if (vc4_debug
& VC4_DEBUG_QIR
) {
2106 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2107 qir_get_stage_name(c
->stage
),
2108 c
->program_id
, c
->variant_id
);
2111 qir_reorder_uniforms(c
);
2112 vc4_generate_code(vc4
, c
);
2114 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2115 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2116 qir_get_stage_name(c
->stage
),
2117 c
->program_id
, c
->variant_id
,
2119 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2120 qir_get_stage_name(c
->stage
),
2121 c
->program_id
, c
->variant_id
,
2131 vc4_shader_state_create(struct pipe_context
*pctx
,
2132 const struct pipe_shader_state
*cso
)
2134 struct vc4_context
*vc4
= vc4_context(pctx
);
2135 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2139 so
->base
.tokens
= tgsi_dup_tokens(cso
->tokens
);
2140 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2146 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2147 struct vc4_compile
*c
)
2149 int count
= c
->num_uniforms
;
2150 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2152 uinfo
->count
= count
;
2153 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2154 memcpy(uinfo
->data
, c
->uniform_data
,
2155 count
* sizeof(*uinfo
->data
));
2156 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2157 memcpy(uinfo
->contents
, c
->uniform_contents
,
2158 count
* sizeof(*uinfo
->contents
));
2159 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2161 vc4_set_shader_uniform_dirty_flags(shader
);
2164 static struct vc4_compiled_shader
*
2165 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2166 struct vc4_key
*key
)
2168 struct hash_table
*ht
;
2170 if (stage
== QSTAGE_FRAG
) {
2172 key_size
= sizeof(struct vc4_fs_key
);
2175 key_size
= sizeof(struct vc4_vs_key
);
2178 struct vc4_compiled_shader
*shader
;
2179 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2183 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2184 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2186 shader
->program_id
= vc4
->next_compiled_program_id
++;
2187 if (stage
== QSTAGE_FRAG
) {
2188 bool input_live
[c
->num_input_semantics
];
2190 memset(input_live
, 0, sizeof(input_live
));
2191 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
2192 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2193 if (inst
->src
[i
].file
== QFILE_VARY
)
2194 input_live
[inst
->src
[i
].index
] = true;
2198 shader
->input_semantics
= ralloc_array(shader
,
2199 struct vc4_varying_semantic
,
2200 c
->num_input_semantics
);
2202 for (int i
= 0; i
< c
->num_input_semantics
; i
++) {
2203 struct vc4_varying_semantic
*sem
= &c
->input_semantics
[i
];
2208 /* Skip non-VS-output inputs. */
2209 if (sem
->semantic
== (uint8_t)~0)
2212 if (sem
->semantic
== TGSI_SEMANTIC_COLOR
||
2213 sem
->semantic
== TGSI_SEMANTIC_BCOLOR
) {
2214 shader
->color_inputs
|= (1 << shader
->num_inputs
);
2217 shader
->input_semantics
[shader
->num_inputs
] = *sem
;
2218 shader
->num_inputs
++;
2221 shader
->num_inputs
= c
->num_inputs
;
2223 shader
->vattr_offsets
[0] = 0;
2224 for (int i
= 0; i
< 8; i
++) {
2225 shader
->vattr_offsets
[i
+ 1] =
2226 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2228 if (c
->vattr_sizes
[i
])
2229 shader
->vattrs_live
|= (1 << i
);
2233 copy_uniform_state_to_shader(shader
, c
);
2234 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2235 c
->qpu_inst_count
* sizeof(uint64_t));
2237 /* Copy the compiler UBO range state to the compiled shader, dropping
2238 * out arrays that were never referenced by an indirect load.
2240 * (Note that QIR dead code elimination of an array access still
2241 * leaves that array alive, though)
2243 if (c
->num_ubo_ranges
) {
2244 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2245 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2248 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2249 struct vc4_compiler_ubo_range
*range
=
2254 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2255 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2256 shader
->ubo_ranges
[j
].size
= range
->size
;
2257 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2261 if (shader
->ubo_size
) {
2262 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2263 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2264 qir_get_stage_name(c
->stage
),
2265 c
->program_id
, c
->variant_id
,
2266 shader
->ubo_size
/ 4);
2270 qir_compile_destroy(c
);
2272 struct vc4_key
*dup_key
;
2273 dup_key
= ralloc_size(shader
, key_size
);
2274 memcpy(dup_key
, key
, key_size
);
2275 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2281 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2282 struct vc4_texture_stateobj
*texstate
)
2284 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2285 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2286 struct pipe_sampler_state
*sampler_state
=
2287 texstate
->samplers
[i
];
2290 key
->tex
[i
].format
= sampler
->format
;
2291 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2292 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2293 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2294 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2295 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2296 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2297 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2298 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2302 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2306 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2308 struct vc4_fs_key local_key
;
2309 struct vc4_fs_key
*key
= &local_key
;
2311 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2313 VC4_DIRTY_FRAMEBUFFER
|
2315 VC4_DIRTY_RASTERIZER
|
2317 VC4_DIRTY_TEXSTATE
|
2318 VC4_DIRTY_UNCOMPILED_FS
))) {
2322 memset(key
, 0, sizeof(*key
));
2323 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2324 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2325 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2326 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2327 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2328 key
->blend
= vc4
->blend
->rt
[0];
2329 if (vc4
->blend
->logicop_enable
) {
2330 key
->logicop_func
= vc4
->blend
->logicop_func
;
2332 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2334 if (vc4
->framebuffer
.cbufs
[0])
2335 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2337 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2338 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2339 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2340 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2341 key
->stencil_enabled
);
2342 if (vc4
->zsa
->base
.alpha
.enabled
) {
2343 key
->alpha_test
= true;
2344 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2347 if (key
->is_points
) {
2348 key
->point_sprite_mask
=
2349 vc4
->rasterizer
->base
.sprite_coord_enable
;
2350 key
->point_coord_upper_left
=
2351 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2352 PIPE_SPRITE_COORD_UPPER_LEFT
);
2355 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2357 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2358 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2359 if (vc4
->prog
.fs
== old_fs
)
2362 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2363 if (vc4
->rasterizer
->base
.flatshade
&&
2364 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2365 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2370 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2372 struct vc4_vs_key local_key
;
2373 struct vc4_vs_key
*key
= &local_key
;
2375 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2376 VC4_DIRTY_RASTERIZER
|
2378 VC4_DIRTY_TEXSTATE
|
2379 VC4_DIRTY_VTXSTATE
|
2380 VC4_DIRTY_UNCOMPILED_VS
|
2381 VC4_DIRTY_COMPILED_FS
))) {
2385 memset(key
, 0, sizeof(*key
));
2386 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2387 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2388 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2390 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2391 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2393 key
->per_vertex_point_size
=
2394 (prim_mode
== PIPE_PRIM_POINTS
&&
2395 vc4
->rasterizer
->base
.point_size_per_vertex
);
2397 struct vc4_compiled_shader
*vs
=
2398 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2399 if (vs
!= vc4
->prog
.vs
) {
2401 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2404 key
->is_coord
= true;
2405 struct vc4_compiled_shader
*cs
=
2406 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2407 if (cs
!= vc4
->prog
.cs
) {
2409 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2414 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2416 vc4_update_compiled_fs(vc4
, prim_mode
);
2417 vc4_update_compiled_vs(vc4
, prim_mode
);
2421 fs_cache_hash(const void *key
)
2423 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2427 vs_cache_hash(const void *key
)
2429 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2433 fs_cache_compare(const void *key1
, const void *key2
)
2435 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2439 vs_cache_compare(const void *key1
, const void *key2
)
2441 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2445 delete_from_cache_if_matches(struct hash_table
*ht
,
2446 struct hash_entry
*entry
,
2447 struct vc4_uncompiled_shader
*so
)
2449 const struct vc4_key
*key
= entry
->key
;
2451 if (key
->shader_state
== so
) {
2452 struct vc4_compiled_shader
*shader
= entry
->data
;
2453 _mesa_hash_table_remove(ht
, entry
);
2454 vc4_bo_unreference(&shader
->bo
);
2455 ralloc_free(shader
);
2460 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2462 struct vc4_context
*vc4
= vc4_context(pctx
);
2463 struct vc4_uncompiled_shader
*so
= hwcso
;
2465 struct hash_entry
*entry
;
2466 hash_table_foreach(vc4
->fs_cache
, entry
)
2467 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2468 hash_table_foreach(vc4
->vs_cache
, entry
)
2469 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2471 if (so
->twoside_tokens
!= so
->base
.tokens
)
2472 free((void *)so
->twoside_tokens
);
2473 free((void *)so
->base
.tokens
);
2478 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2480 struct vc4_context
*vc4
= vc4_context(pctx
);
2481 vc4
->prog
.bind_fs
= hwcso
;
2482 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2486 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2488 struct vc4_context
*vc4
= vc4_context(pctx
);
2489 vc4
->prog
.bind_vs
= hwcso
;
2490 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2494 vc4_program_init(struct pipe_context
*pctx
)
2496 struct vc4_context
*vc4
= vc4_context(pctx
);
2498 pctx
->create_vs_state
= vc4_shader_state_create
;
2499 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2501 pctx
->create_fs_state
= vc4_shader_state_create
;
2502 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2504 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2505 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2507 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2509 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2514 vc4_program_fini(struct pipe_context
*pctx
)
2516 struct vc4_context
*vc4
= vc4_context(pctx
);
2518 struct hash_entry
*entry
;
2519 hash_table_foreach(vc4
->fs_cache
, entry
) {
2520 struct vc4_compiled_shader
*shader
= entry
->data
;
2521 vc4_bo_unreference(&shader
->bo
);
2522 ralloc_free(shader
);
2523 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2526 hash_table_foreach(vc4
->vs_cache
, entry
) {
2527 struct vc4_compiled_shader
*shader
= entry
->data
;
2528 vc4_bo_unreference(&shader
->bo
);
2529 ralloc_free(shader
);
2530 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);