Debian Codegraph @

src/panfrost/lib/pan_blend.c

raw
1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019-2021 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "pan_blend.h"
8 #include "util/blend.h"
9
10 #ifdef PAN_ARCH
11 #include "pan_texture.h"
12 #endif
13
14 #include "compiler/nir/nir.h"
15 #include "compiler/nir/nir_builder.h"
16 #include "compiler/nir/nir_conversion_builder.h"
17 #include "compiler/nir/nir_lower_blend.h"
18 #include "compiler/pan_compiler.h"
19 #include "compiler/pan_nir.h"
20 #include "util/format/u_format.h"
21
22 #ifndef PAN_ARCH
23
24 /* Fixed function blending */
25
26 static bool
27 factor_is_supported(enum pipe_blendfactor factor)
28 {
29 factor = util_blendfactor_without_invert(factor);
30
31 return factor != PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
32 factor != PIPE_BLENDFACTOR_SRC1_COLOR &&
33 factor != PIPE_BLENDFACTOR_SRC1_ALPHA;
34 }
35
36 /* The set of factors supported by the hardware for floats is significantly
37 * reduced.
38 */
39 static bool
40 factor_is_supported_for_float(enum pipe_blendfactor factor)
41 {
42 return factor == PIPE_BLENDFACTOR_ZERO ||
43 factor == PIPE_BLENDFACTOR_ONE ||
44 factor == PIPE_BLENDFACTOR_SRC_ALPHA ||
45 factor == PIPE_BLENDFACTOR_INV_SRC_ALPHA;
46 }
47
48 /* OpenGL allows encoding (src*dest + dest*src) which is incompatiblle with
49 * Midgard style blending since there are two multiplies. However, it may be
50 * factored as 2*src*dest = dest*(2*src), which can be encoded on Bifrost as 0
51 * + dest * (2*src) wih the new source_2 value of C. Detect this case. */
52
53 static bool
54 is_2srcdest(enum pipe_blend_func blend_func, enum pipe_blendfactor src_factor,
55 enum pipe_blendfactor dest_factor, bool is_alpha)
56 {
57 return (blend_func == PIPE_BLEND_ADD) &&
58 ((src_factor == PIPE_BLENDFACTOR_DST_COLOR) ||
59 ((src_factor == PIPE_BLENDFACTOR_DST_ALPHA) && is_alpha)) &&
60 ((dest_factor == PIPE_BLENDFACTOR_SRC_COLOR) ||
61 ((dest_factor == PIPE_BLENDFACTOR_SRC_ALPHA) && is_alpha));
62 }
63
64 static bool
65 can_fixed_function_equation(unsigned arch,
66 enum pipe_blend_func blend_func,
67 enum pipe_blendfactor src_factor,
68 enum pipe_blendfactor dest_factor,
69 bool is_alpha, bool is_float,
70 bool supports_2src)
71 {
72 /* We can only do add/subtract in hardware. No min/max. */
73 if (blend_func != PIPE_BLEND_ADD && blend_func != PIPE_BLEND_SUBTRACT &&
74 blend_func != PIPE_BLEND_REVERSE_SUBTRACT)
75 return false;
76
77 if (is_float) {
78 /* There are a couple special cases for add with zero */
79 if (blend_func == PIPE_BLEND_ADD &&
80 src_factor == PIPE_BLENDFACTOR_ZERO &&
81 (dest_factor == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
82 dest_factor == PIPE_BLENDFACTOR_DST_ALPHA))
83 return true;
84
85 return factor_is_supported_for_float(src_factor) &&
86 factor_is_supported_for_float(dest_factor);
87 } else {
88 if (is_2srcdest(blend_func, src_factor, dest_factor, is_alpha))
89 return supports_2src;
90
91 if (!factor_is_supported(src_factor) || !factor_is_supported(dest_factor))
92 return false;
93
94 /* Fixed function requires src/dest factors to match (up to invert) or be
95 * zero/one.
96 */
97 enum pipe_blendfactor src = util_blendfactor_without_invert(src_factor);
98 enum pipe_blendfactor dest = util_blendfactor_without_invert(dest_factor);
99
100 if (arch < 6 &&
101 (src == PIPE_BLENDFACTOR_DST_ALPHA))
102 return false;
103 return (src == dest) || (src == PIPE_BLENDFACTOR_ONE) ||
104 (dest == PIPE_BLENDFACTOR_ONE);
105 }
106 }
107
108 static unsigned
109 blend_factor_constant_mask(enum pipe_blendfactor factor)
110 {
111 factor = util_blendfactor_without_invert(factor);
112
113 if (factor == PIPE_BLENDFACTOR_CONST_COLOR)
114 return 0b0111; /* RGB */
115 else if (factor == PIPE_BLENDFACTOR_CONST_ALPHA)
116 return 0b1000; /* A */
117 else
118 return 0b0000; /* - */
119 }
120
121 unsigned
122 pan_blend_constant_mask(const struct pan_blend_equation eq)
123 {
124 return blend_factor_constant_mask(eq.rgb_src_factor) |
125 blend_factor_constant_mask(eq.rgb_dst_factor) |
126 blend_factor_constant_mask(eq.alpha_src_factor) |
127 blend_factor_constant_mask(eq.alpha_dst_factor);
128 }
129
130 static inline bool
131 is_min_max(enum pipe_blend_func func)
132 {
133 return func == PIPE_BLEND_MIN || func == PIPE_BLEND_MAX;
134 }
135
136 void
137 pan_blend_optimize_equation(struct pan_blend_equation *eq,
138 enum pipe_format format,
139 const float *constants)
140 {
141 unsigned comp_mask = 0xf;
142
143 if (!eq->blend_enable)
144 return;
145
146 /* Sanitize alpha blend factors because later optimizations rely on COLOR
147 * actually meaning color.
148 */
149 eq->alpha_src_factor = util_blendfactor_to_alpha(eq->alpha_src_factor);
150 eq->alpha_dst_factor = util_blendfactor_to_alpha(eq->alpha_dst_factor);
151
152 if (is_min_max(eq->rgb_func)) {
153 eq->rgb_src_factor = PIPE_BLENDFACTOR_ONE;
154 eq->rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
155 }
156
157 if (is_min_max(eq->alpha_func)) {
158 eq->alpha_src_factor = PIPE_BLENDFACTOR_ONE;
159 eq->alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
160 }
161
162 /* If we know the format, we can optimize a few things */
163 if (format != PIPE_FORMAT_NONE) {
164 const struct util_format_description *fmt =
165 util_format_description(format);
166 assert(eq->is_float == (fmt->channel[0].type == UTIL_FORMAT_TYPE_FLOAT));
167
168 comp_mask = util_format_colormask(fmt);
169
170 /* Check to see if any of the disabled channels actually matter. If
171 * not, smash the blend mask to 0xf. Otherwise, pan_blend_is_opaque()
172 * may return false unnecessarily.
173 */
174 if (!((~eq->color_mask) & comp_mask))
175 eq->color_mask = 0xf;
176
177 if (!(comp_mask & 0b1000)) {
178 eq->rgb_src_factor =
179 util_blend_dst_alpha_to_one(eq->rgb_src_factor);
180 eq->rgb_dst_factor =
181 util_blend_dst_alpha_to_one(eq->rgb_dst_factor);
182
183 eq->alpha_src_factor =
184 util_blend_dst_alpha_to_one(eq->alpha_src_factor);
185 eq->alpha_dst_factor =
186 util_blend_dst_alpha_to_one(eq->alpha_dst_factor);
187 }
188 }
189
190 /* If we know the blend constants, we can optimize more */
191 unsigned constant_mask = pan_blend_constant_mask(*eq);
192 if (constant_mask && constants != NULL) {
193 enum pipe_blendfactor const_alpha = PIPE_BLENDFACTOR_CONST_ALPHA;
194 if (constant_mask & 0b1000) {
195 if (constants[3] == 0.0f)
196 const_alpha = PIPE_BLENDFACTOR_ZERO;
197 else if (constants[3] == 1.0f)
198 const_alpha = PIPE_BLENDFACTOR_ONE;
199 }
200
201 /* Each color blend constant can only affect it's corresponding channel
202 * so we only care about components actually in our format.
203 */
204 const unsigned color_constant_mask = comp_mask & constant_mask & 0b0111;
205
206 enum pipe_blendfactor const_color = PIPE_BLENDFACTOR_CONST_COLOR;
207 if (color_constant_mask) {
208 bool all_zero = true, all_one = true;
209 u_foreach_bit(i, color_constant_mask) {
210 if (constants[i] != 0.0f)
211 all_zero = false;
212 if (constants[i] != 1.0f)
213 all_one = false;
214 }
215 assert(!all_zero || !all_one);
216 if (all_zero)
217 const_color = PIPE_BLENDFACTOR_ZERO;
218 if (all_one)
219 const_color = PIPE_BLENDFACTOR_ONE;
220 }
221
222 if (const_color != PIPE_BLENDFACTOR_CONST_COLOR ||
223 const_alpha != PIPE_BLENDFACTOR_CONST_ALPHA) {
224 #define REPLACE_CONST_FACTOR(factor) do { \
225 if (factor == PIPE_BLENDFACTOR_CONST_COLOR) \
226 factor = const_color; \
227 else if (factor == PIPE_BLENDFACTOR_CONST_ALPHA) \
228 factor = const_alpha; \
229 } while (false)
230
231 REPLACE_CONST_FACTOR(eq->rgb_src_factor);
232 REPLACE_CONST_FACTOR(eq->rgb_dst_factor);
233 REPLACE_CONST_FACTOR(eq->alpha_src_factor);
234 REPLACE_CONST_FACTOR(eq->alpha_dst_factor);
235
236 #undef REPLACE_CONST_FACTOR
237 }
238 }
239
240 if (pan_blend_is_opaque(*eq))
241 eq->blend_enable = false;
242 }
243
244 /* Only "homogenous" (scalar or vector with all components equal) constants are
245 * valid for fixed-function, so check for this condition */
246
247 bool
248 pan_blend_is_homogenous_constant(unsigned mask, const float *constants)
249 {
250 float constant = pan_blend_get_constant(mask, constants);
251
252 u_foreach_bit(i, mask) {
253 if (constants[i] != constant)
254 return false;
255 }
256
257 return true;
258 }
259
260 uint16_t
261 pan_pack_blend_constant(enum pipe_format format, float cons)
262 {
263 const struct util_format_description *format_desc =
264 util_format_description(format);
265
266 /* Mali doesn't support float blend constants */
267 assert(format_desc->channel[0].type != UTIL_FORMAT_TYPE_FLOAT);
268
269 /* On Bifrost, the blend constant is expressed with a UNORM of the
270 * size of the target format. The value is then shifted such that
271 * used bits are in the MSB.
272 */
273 unsigned chan_size = 0;
274 for (unsigned i = 0; i < format_desc->nr_channels; i++)
275 chan_size = MAX2(format_desc->channel[0].size, chan_size);
276
277 float factor = ((1 << chan_size) - 1) << (16 - chan_size);
278
279 return cons * factor;
280 }
281
282 /* Determines if an equation can run in fixed function */
283
284 bool
285 pan_blend_can_fixed_function(unsigned arch,
286 const struct pan_blend_equation equation,
287 bool supports_2src)
288 {
289 return !equation.blend_enable ||
290 (can_fixed_function_equation(arch,
291 equation.rgb_func, equation.rgb_src_factor,
292 equation.rgb_dst_factor, false /* is_alpha */,
293 equation.is_float, supports_2src) &&
294 can_fixed_function_equation(arch,
295 equation.alpha_func, equation.alpha_src_factor,
296 equation.alpha_dst_factor, true /* is_alpha */,
297 equation.is_float, supports_2src));
298 }
299
300 static enum mali_blend_operand_c
301 to_c_factor(enum pipe_blendfactor factor)
302 {
303 switch (util_blendfactor_without_invert(factor)) {
304 case PIPE_BLENDFACTOR_ONE:
305 /* Extra invert to flip back in caller */
306 return MALI_BLEND_OPERAND_C_ZERO;
307
308 case PIPE_BLENDFACTOR_SRC_ALPHA:
309 return MALI_BLEND_OPERAND_C_SRC_ALPHA;
310
311 case PIPE_BLENDFACTOR_DST_ALPHA:
312 return MALI_BLEND_OPERAND_C_DEST_ALPHA;
313
314 case PIPE_BLENDFACTOR_SRC_COLOR:
315 return MALI_BLEND_OPERAND_C_SRC;
316
317 case PIPE_BLENDFACTOR_DST_COLOR:
318 return MALI_BLEND_OPERAND_C_DEST;
319
320 case PIPE_BLENDFACTOR_CONST_COLOR:
321 case PIPE_BLENDFACTOR_CONST_ALPHA:
322 return MALI_BLEND_OPERAND_C_CONSTANT;
323
324 default:
325 UNREACHABLE("Unsupported blend factor");
326 }
327 }
328
329 static void
330 to_mali_function(enum pipe_blend_func blend_func,
331 enum pipe_blendfactor src_factor,
332 enum pipe_blendfactor dest_factor,
333 bool is_alpha, bool is_float,
334 struct MALI_BLEND_FUNCTION *function)
335 {
336 /* We handle ZERO/ONE specially since it's the hardware has 0 and can invert
337 * to 1 but Gallium has 0 as the uninverted version.
338 */
339 bool src_inverted =
340 util_blendfactor_is_inverted(src_factor) ^
341 (util_blendfactor_without_invert(src_factor) == PIPE_BLENDFACTOR_ONE);
342
343 bool dest_inverted =
344 util_blendfactor_is_inverted(dest_factor) ^
345 (util_blendfactor_without_invert(dest_factor) == PIPE_BLENDFACTOR_ONE);
346
347 if (src_factor == PIPE_BLENDFACTOR_ZERO) {
348 function->a = MALI_BLEND_OPERAND_A_ZERO;
349 function->b = MALI_BLEND_OPERAND_B_DEST;
350 if (blend_func == PIPE_BLEND_SUBTRACT)
351 function->negate_b = true;
352 function->invert_c = dest_inverted;
353 function->c = to_c_factor(dest_factor);
354 } else if (src_factor == PIPE_BLENDFACTOR_ONE) {
355 function->a = MALI_BLEND_OPERAND_A_SRC;
356 function->b = MALI_BLEND_OPERAND_B_DEST;
357 if (blend_func == PIPE_BLEND_SUBTRACT)
358 function->negate_b = true;
359 else if (blend_func == PIPE_BLEND_REVERSE_SUBTRACT)
360 function->negate_a = true;
361 function->invert_c = dest_inverted;
362 function->c = to_c_factor(dest_factor);
363 } else if (dest_factor == PIPE_BLENDFACTOR_ZERO) {
364 function->a = MALI_BLEND_OPERAND_A_ZERO;
365 function->b = MALI_BLEND_OPERAND_B_SRC;
366 if (blend_func == PIPE_BLEND_REVERSE_SUBTRACT)
367 function->negate_b = true;
368 function->invert_c = src_inverted;
369 function->c = to_c_factor(src_factor);
370 } else if (dest_factor == PIPE_BLENDFACTOR_ONE) {
371 function->a = MALI_BLEND_OPERAND_A_DEST;
372 function->b = MALI_BLEND_OPERAND_B_SRC;
373 if (blend_func == PIPE_BLEND_SUBTRACT)
374 function->negate_a = true;
375 else if (blend_func == PIPE_BLEND_REVERSE_SUBTRACT)
376 function->negate_b = true;
377 function->invert_c = src_inverted;
378 function->c = to_c_factor(src_factor);
379 } else if (src_factor == dest_factor) {
380 function->a = MALI_BLEND_OPERAND_A_ZERO;
381 function->invert_c = src_inverted;
382 function->c = to_c_factor(src_factor);
383
384 switch (blend_func) {
385 case PIPE_BLEND_ADD:
386 function->b = MALI_BLEND_OPERAND_B_SRC_PLUS_DEST;
387 break;
388 case PIPE_BLEND_REVERSE_SUBTRACT:
389 function->negate_b = true;
390 FALLTHROUGH;
391 case PIPE_BLEND_SUBTRACT:
392 function->b = MALI_BLEND_OPERAND_B_SRC_MINUS_DEST;
393 break;
394 default:
395 UNREACHABLE("Invalid blend function");
396 }
397 } else if (is_2srcdest(blend_func, src_factor, dest_factor, is_alpha)) {
398 /* src*dest + dest*src = 2*src*dest = 0 + dest*(2*src) */
399 function->a = MALI_BLEND_OPERAND_A_ZERO;
400 function->b = MALI_BLEND_OPERAND_B_DEST;
401 function->c = MALI_BLEND_OPERAND_C_SRC_X_2;
402 } else {
403 assert(util_blendfactor_without_invert(src_factor) ==
404 util_blendfactor_without_invert(dest_factor) &&
405 src_inverted != dest_inverted);
406
407 function->a = MALI_BLEND_OPERAND_A_DEST;
408 function->invert_c = src_inverted;
409 function->c = to_c_factor(src_factor);
410
411 switch (blend_func) {
412 case PIPE_BLEND_ADD:
413 function->b = MALI_BLEND_OPERAND_B_SRC_MINUS_DEST;
414 break;
415 case PIPE_BLEND_REVERSE_SUBTRACT:
416 function->b = MALI_BLEND_OPERAND_B_SRC_PLUS_DEST;
417 function->negate_b = true;
418 break;
419 case PIPE_BLEND_SUBTRACT:
420 function->b = MALI_BLEND_OPERAND_B_SRC_PLUS_DEST;
421 function->negate_a = true;
422 break;
423 default:
424 UNREACHABLE("Invalid blend function\n");
425 }
426 }
427 }
428
429 bool
430 pan_blend_is_opaque(const struct pan_blend_equation equation)
431 {
432 /* If a channel is masked out, we can't use opaque mode even if
433 * blending is disabled, since we need a tilebuffer read in there */
434 if (equation.color_mask != 0xF)
435 return false;
436
437 /* With nothing masked out, disabled bledning is opaque */
438 if (!equation.blend_enable)
439 return true;
440
441 /* NOTE (NaN/inf):
442 *
443 * Technically, we should reject this optimization for float blending
444 * because 0.0 * NaN/inf = NaN. However, Vulkan and OpenGL both allow us
445 * to drop inf/NaN pretty much at-will and the NIR blending we would fall
446 * back to will also drop 0.0 blend factors. One day we might want to do
447 * all this behind a driconf flag but today is not that day.
448 */
449
450 /* Also detect open-coded opaque blending */
451 return equation.rgb_src_factor == PIPE_BLENDFACTOR_ONE &&
452 equation.rgb_dst_factor == PIPE_BLENDFACTOR_ZERO &&
453 (equation.rgb_func == PIPE_BLEND_ADD ||
454 equation.rgb_func == PIPE_BLEND_SUBTRACT) &&
455 equation.alpha_src_factor == PIPE_BLENDFACTOR_ONE &&
456 equation.alpha_dst_factor == PIPE_BLENDFACTOR_ZERO &&
457 (equation.alpha_func == PIPE_BLEND_ADD ||
458 equation.alpha_func == PIPE_BLEND_SUBTRACT);
459 }
460
461 /* Check if a factor represents a constant value of val, assuming src_alpha is
462 * the given constant.
463 */
464
465 static inline bool
466 is_factor_01(enum pipe_blendfactor factor, unsigned val, unsigned srca)
467 {
468 assert(val == 0 || val == 1);
469 assert(srca == 0 || srca == 1);
470
471 switch (factor) {
472 case PIPE_BLENDFACTOR_ZERO:
473 return (val == 0);
474
475 case PIPE_BLENDFACTOR_ONE:
476 return (val == 1);
477
478 case PIPE_BLENDFACTOR_SRC_ALPHA:
479 return (val == srca);
480
481 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
482 return (val == (1 - srca));
483
484 default:
485 return false;
486 }
487 }
488
489 /* Returns if src alpha = 0 implies the blended colour equals the destination
490 * colour. Suppose source alpha = 0 and consider cases.
491 *
492 * Additive blending: Equivalent to D = S * f_s + D * f_d for all D and all S
493 * with S_a = 0, for each component. For the alpha component (if it unmasked),
494 * we have S_a = 0 so this reduces to D = D * f_d <===> f_d = 1. For RGB
495 * components (if unmasked), we need f_s = 0 and f_d = 1.
496 *
497 * Subtractive blending: Fails in general (D = S * f_S - D * f_D). We
498 * would need f_S = 0 and f_D = -1, which is not valid in the APIs.
499 *
500 * Reverse subtractive blending (D = D * f_D - S * f_S), we need f_D = 1
501 * and f_S = 0 up to masking. This is the same as additive blending.
502 *
503 * Min/max: Fails in general on the RGB components.
504 */
505
506 bool
507 pan_blend_alpha_zero_nop(const struct pan_blend_equation eq)
508 {
509 if (eq.rgb_func != PIPE_BLEND_ADD &&
510 eq.rgb_func != PIPE_BLEND_REVERSE_SUBTRACT)
511 return false;
512
513 if (eq.color_mask & 0x8) {
514 if (!is_factor_01(eq.alpha_dst_factor, 1, 0))
515 return false;
516 }
517
518 if (eq.color_mask & 0x7) {
519 if (!is_factor_01(eq.rgb_dst_factor, 1, 0))
520 return false;
521
522 if (!is_factor_01(eq.rgb_src_factor, 0, 0))
523 return false;
524 }
525
526 return true;
527 }
528
529 /* Returns if src alpha = 1 implies the blended colour equals the source
530 * colour. Suppose source alpha = 1 and consider cases.
531 *
532 * Additive blending: S = S * f_s + D * f_d. We need f_s = 1 and f_d = 0.
533 *
534 * Subtractive blending: S = S * f_s - D * f_d. Same as additive blending.
535 *
536 * Reverse subtractive blending: S = D * f_d - S * f_s. Fails in general since
537 * it would require f_s = -1, which is not valid in the APIs.
538 *
539 * Min/max: Fails in general on the RGB components.
540 *
541 * Note if any component is masked, we can't use a store.
542 */
543
544 bool
545 pan_blend_alpha_one_store(const struct pan_blend_equation eq)
546 {
547 if (eq.rgb_func != PIPE_BLEND_ADD && eq.rgb_func != PIPE_BLEND_SUBTRACT)
548 return false;
549
550 if (eq.color_mask != 0xf)
551 return false;
552
553 return is_factor_01(eq.rgb_src_factor, 1, 1) &&
554 is_factor_01(eq.alpha_src_factor, 1, 1) &&
555 is_factor_01(eq.rgb_dst_factor, 0, 1) &&
556 is_factor_01(eq.alpha_dst_factor, 0, 1);
557 }
558
559 static bool
560 is_dest_factor(enum pipe_blendfactor factor, bool alpha)
561 {
562 factor = util_blendfactor_without_invert(factor);
563
564 return factor == PIPE_BLENDFACTOR_DST_ALPHA ||
565 factor == PIPE_BLENDFACTOR_DST_COLOR ||
566 (factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE && !alpha);
567 }
568
569 /* Determines if a blend equation reads back the destination. This can occur by
570 * explicitly referencing the destination in the blend equation, or by using a
571 * partial writemask. */
572
573 bool
574 pan_blend_reads_dest(const struct pan_blend_equation equation)
575 {
576 if (equation.color_mask && equation.color_mask != 0xF)
577 return true;
578
579 if (!equation.blend_enable)
580 return false;
581
582 /* NOTE (NaN/inf):
583 *
584 * Technically, we should reject this optimization for float blending
585 * because 0.0 * NaN/inf = NaN. However, Vulkan and OpenGL both allow us
586 * to drop inf/NaN pretty much at-will and the NIR blending we would fall
587 * back to will also drop 0.0 blend factors. One day we might want to do
588 * all this behind a driconf flag but today is not that day.
589 */
590
591 /* Min/max blending ignores the factors so the destination always gets
592 * read verbatim.
593 */
594 if (is_min_max(equation.rgb_func) || is_min_max(equation.alpha_func))
595 return true;
596
597 return is_dest_factor(equation.rgb_src_factor, false) ||
598 is_dest_factor(equation.alpha_src_factor, true) ||
599 equation.rgb_dst_factor != PIPE_BLENDFACTOR_ZERO ||
600 equation.alpha_dst_factor != PIPE_BLENDFACTOR_ZERO;
601 }
602
603 /* Create the descriptor for a fixed blend mode given the corresponding API
604 * state. Assumes the equation can be represented as fixed-function. */
605
606 void
607 pan_blend_to_fixed_function_equation(const struct pan_blend_equation equation,
608 struct MALI_BLEND_EQUATION *out)
609 {
610 /* If no blending is enabled, default back on `replace` mode */
611 if (!equation.blend_enable) {
612 out->color_mask = equation.color_mask;
613 out->rgb.a = MALI_BLEND_OPERAND_A_SRC;
614 out->rgb.b = MALI_BLEND_OPERAND_B_SRC;
615 out->rgb.c = MALI_BLEND_OPERAND_C_ZERO;
616 out->alpha.a = MALI_BLEND_OPERAND_A_SRC;
617 out->alpha.b = MALI_BLEND_OPERAND_B_SRC;
618 out->alpha.c = MALI_BLEND_OPERAND_C_ZERO;
619 return;
620 }
621
622 /* Compile the fixed-function blend */
623 to_mali_function(equation.rgb_func, equation.rgb_src_factor,
624 equation.rgb_dst_factor, false /* is_alpha */,
625 equation.is_float, &out->rgb);
626 to_mali_function(equation.alpha_func, equation.alpha_src_factor,
627 equation.alpha_dst_factor, true /* is_alpha */,
628 equation.is_float, &out->alpha);
629
630 out->color_mask = equation.color_mask;
631 }
632
633 uint32_t
634 pan_pack_blend(const struct pan_blend_equation equation)
635 {
636 struct mali_blend_equation_packed out;
637
638 pan_pack(&out, BLEND_EQUATION, cfg) {
639 pan_blend_to_fixed_function_equation(equation, &cfg);
640 }
641
642 return out.opaque[0];
643 }
644
645 enum mali_register_file_format
646 pan_blend_type_from_nir(nir_alu_type nir_type)
647 {
648 switch (nir_type) {
649 case 0: /* Render target not in use */
650 return 0;
651 case nir_type_float16:
652 return MALI_REGISTER_FILE_FORMAT_F16;
653 case nir_type_float32:
654 return MALI_REGISTER_FILE_FORMAT_F32;
655 case nir_type_int32:
656 return MALI_REGISTER_FILE_FORMAT_I32;
657 case nir_type_uint32:
658 return MALI_REGISTER_FILE_FORMAT_U32;
659 case nir_type_int16:
660 return MALI_REGISTER_FILE_FORMAT_I16;
661 case nir_type_uint16:
662 return MALI_REGISTER_FILE_FORMAT_U16;
663 default:
664 UNREACHABLE("Unsupported blend shader type for NIR alu type");
665 return 0;
666 }
667 }
668
669 #else /* ifndef PAN_ARCH */
670
671 static const char *
672 logicop_str(enum pipe_logicop logicop)
673 {
674 switch (logicop) {
675 case PIPE_LOGICOP_CLEAR:
676 return "clear";
677 case PIPE_LOGICOP_NOR:
678 return "nor";
679 case PIPE_LOGICOP_AND_INVERTED:
680 return "and-inverted";
681 case PIPE_LOGICOP_COPY_INVERTED:
682 return "copy-inverted";
683 case PIPE_LOGICOP_AND_REVERSE:
684 return "and-reverse";
685 case PIPE_LOGICOP_INVERT:
686 return "invert";
687 case PIPE_LOGICOP_XOR:
688 return "xor";
689 case PIPE_LOGICOP_NAND:
690 return "nand";
691 case PIPE_LOGICOP_AND:
692 return "and";
693 case PIPE_LOGICOP_EQUIV:
694 return "equiv";
695 case PIPE_LOGICOP_NOOP:
696 return "noop";
697 case PIPE_LOGICOP_OR_INVERTED:
698 return "or-inverted";
699 case PIPE_LOGICOP_COPY:
700 return "copy";
701 case PIPE_LOGICOP_OR_REVERSE:
702 return "or-reverse";
703 case PIPE_LOGICOP_OR:
704 return "or";
705 case PIPE_LOGICOP_SET:
706 return "set";
707 default:
708 UNREACHABLE("Invalid logicop\n");
709 }
710 }
711
712 static void
713 get_equation_str(const struct pan_blend_rt_state *rt_state, char *str,
714 unsigned len)
715 {
716 const char *funcs[] = {
717 "add", "sub", "reverse_sub", "min", "max",
718 };
719 const char *factors[] = {
720 "", "one", "src_color", "src_alpha", "dst_alpha",
721 "dst_color", "src_alpha_sat", "const_color", "const_alpha", "src1_color",
722 "src1_alpha",
723 };
724 int ret;
725
726 if (!rt_state->equation.blend_enable) {
727 ret = snprintf(str, len, "replace(%s%s%s%s)",
728 (rt_state->equation.color_mask & 1) ? "R" : "",
729 (rt_state->equation.color_mask & 2) ? "G" : "",
730 (rt_state->equation.color_mask & 4) ? "B" : "",
731 (rt_state->equation.color_mask & 8) ? "A" : "");
732 assert(ret > 0);
733 return;
734 }
735
736 if (rt_state->equation.color_mask & 7) {
737 assert(rt_state->equation.rgb_func < ARRAY_SIZE(funcs));
738 ret = snprintf(
739 str, len, "%s%s%s(func=%s,src_factor=%s%s,dst_factor=%s%s)%s",
740 (rt_state->equation.color_mask & 1) ? "R" : "",
741 (rt_state->equation.color_mask & 2) ? "G" : "",
742 (rt_state->equation.color_mask & 4) ? "B" : "",
743 funcs[rt_state->equation.rgb_func],
744 util_blendfactor_is_inverted(rt_state->equation.rgb_src_factor) ? "-"
745 : "",
746 factors[util_blendfactor_without_invert(
747 rt_state->equation.rgb_src_factor)],
748 util_blendfactor_is_inverted(rt_state->equation.rgb_dst_factor) ? "-"
749 : "",
750 factors[util_blendfactor_without_invert(
751 rt_state->equation.rgb_dst_factor)],
752 rt_state->equation.color_mask & 8 ? ";" : "");
753 assert(ret > 0);
754 str += ret;
755 len -= ret;
756 }
757
758 if (rt_state->equation.color_mask & 8) {
759 assert(rt_state->equation.alpha_func < ARRAY_SIZE(funcs));
760 ret = snprintf(
761 str, len, "A(func=%s,src_factor=%s%s,dst_factor=%s%s)",
762 funcs[rt_state->equation.alpha_func],
763 util_blendfactor_is_inverted(rt_state->equation.alpha_src_factor) ? "-"
764 : "",
765 factors[util_blendfactor_without_invert(
766 rt_state->equation.alpha_src_factor)],
767 util_blendfactor_is_inverted(rt_state->equation.alpha_dst_factor) ? "-"
768 : "",
769 factors[util_blendfactor_without_invert(
770 rt_state->equation.alpha_dst_factor)]);
771 assert(ret > 0);
772 str += ret;
773 len -= ret;
774 }
775 }
776
777 enum pipe_format
778 GENX(pan_blend_shader_fmt)(enum pipe_format format)
779 {
780 switch (format) {
781 #if PAN_ARCH < 6
782 case PIPE_FORMAT_A16_FLOAT:
783 case PIPE_FORMAT_I16_FLOAT:
784 return PIPE_FORMAT_R16G16B16A16_FLOAT;
785 case PIPE_FORMAT_R10G10B10A2_UNORM:
786 case PIPE_FORMAT_R10G10B10X2_UNORM:
787 return PIPE_FORMAT_R8G8B8A8_UNORM;
788 #endif
789 case PIPE_FORMAT_A8_UNORM:
790 case PIPE_FORMAT_L8A8_UNORM:
791 return PIPE_FORMAT_R8G8B8A8_UNORM;
792 default:
793 return format;
794 }
795 }
796
797 nir_shader *
798 GENX(pan_blend_create_shader)(const struct pan_blend_state *state,
799 nir_alu_type src0_type, nir_alu_type src1_type,
800 unsigned rt)
801 {
802 const struct pan_blend_rt_state *rt_state = &state->rts[rt];
803 char equation_str[128] = {0};
804
805 get_equation_str(rt_state, equation_str, sizeof(equation_str));
806
807 const nir_shader_compiler_options *compiler_options =
808 pan_get_nir_shader_compiler_options(PAN_ARCH, false);
809 nir_builder builder = nir_builder_init_simple_shader(
810 MESA_SHADER_FRAGMENT, compiler_options,
811 "pan_blend(rt=%d,fmt=%s,nr_samples=%d,%s=%s)", rt,
812 util_format_name(rt_state->format), rt_state->nr_samples,
813 state->logicop_enable ? "logicop" : "equation",
814 state->logicop_enable ? logicop_str(state->logicop_func) : equation_str);
815 nir_builder *b = &builder;
816
817 const enum pipe_format format = GENX(pan_blend_shader_fmt)(rt_state->format);
818 const struct util_format_description *format_desc =
819 util_format_description(format);
820
821 /* Choose a type which is not going to lead to precision loss while
822 * blending. If we're not dual-source blending, src1_type will be
823 * nir_type_invalid which has a size of zero.
824 */
825 nir_alu_type dest_type = pan_unpacked_type_for_format(format_desc);
826 if (PAN_ARCH >= 6 && nir_alu_type_get_type_size(dest_type) == 8)
827 dest_type = nir_alu_type_get_base_type(dest_type) | 16;
828
829 const unsigned dest_bit_size = nir_alu_type_get_type_size(dest_type);
830 const nir_alu_type dest_base_type = nir_alu_type_get_base_type(dest_type);
831
832 /* Midgard doesn't always provide types at all but it's always float32 */
833 src0_type = src0_type ?: nir_type_float32;
834 src1_type = src1_type ?: nir_type_float32;
835
836 nir_def *src0 = nir_load_blend_input_pan(b,
837 4, nir_alu_type_get_type_size(src0_type),
838 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
839 .io_semantics.dual_source_blend_index = 0,
840 .io_semantics.num_slots = 1,
841 .dest_type = src0_type);
842
843 nir_def *src1 = nir_load_blend_input_pan(b,
844 4, nir_alu_type_get_type_size(src1_type),
845 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
846 .io_semantics.dual_source_blend_index = 1,
847 .io_semantics.num_slots = 1,
848 .dest_type = src1_type);
849
850 /* Make sure everyone is the same type. We assume the destination type
851 * here because TGSI sometimes gives us bogus types. When they're not
852 * bogus, shader types are required to match the format anyway.
853 *
854 * On Midgard, the blend shader is responsible for format conversion.
855 * As the OpenGL spec requires integer conversions to saturate, we must
856 * saturate ourselves here. On Bifrost and later, the conversion
857 * hardware handles this automatically.
858 */
859 bool should_saturate = PAN_ARCH <= 5 && dest_base_type != nir_type_float;
860 src0 = nir_convert_with_rounding(b, src0, dest_base_type, dest_type,
861 nir_rounding_mode_undef, should_saturate);
862 src1 = nir_convert_with_rounding(b, src1, dest_base_type, dest_type,
863 nir_rounding_mode_undef, should_saturate);
864
865 if (state->alpha_to_one && dest_base_type == nir_type_float) {
866 nir_def *one = nir_imm_floatN_t(b, 1.0, dest_bit_size);
867 src0 = nir_vector_insert_imm(b, src0, one, 3);
868 src1 = nir_vector_insert_imm(b, src1, one, 3);
869 }
870
871 #if PAN_ARCH >= 6
872 const uint64_t opaque_blend_desc =
873 GENX(pan_blend_get_internal_desc)(format, rt, dest_bit_size, false);
874 #else
875 const uint64_t opaque_blend_desc = 0;
876 #endif
877
878 nir_def *dest;
879 if (PAN_ARCH >= 6) {
880 nir_def *sample_id =
881 rt_state->nr_samples > 1 ? nir_load_sample_id(b) : nir_imm_int(b, 0);
882 dest = nir_load_tile_pan(b,
883 4, dest_bit_size,
884 pan_nir_tile_rt_sample(b, nir_imm_int(b, rt), sample_id),
885 pan_nir_tile_default_coverage(b),
886 nir_imm_int(b, opaque_blend_desc >> 32),
887 .dest_type = dest_type,
888 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
889 .io_semantics.num_slots = 1);
890 } else {
891 dest = nir_load_output(b,
892 4, dest_bit_size,
893 nir_imm_int(b, 0),
894 .dest_type = dest_type,
895 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
896 .io_semantics.num_slots = 1);
897 }
898
899 nir_def *color = src0;
900 if (state->logicop_enable) {
901 color = nir_color_logicop(b, src0, dest, state->logicop_func, format);
902 } else if (rt_state->equation.blend_enable) {
903 const nir_lower_blend_rt nir_rt = {
904 .format = format,
905 .rgb.func = rt_state->equation.rgb_func,
906 .rgb.src_factor = rt_state->equation.rgb_src_factor,
907 .rgb.dst_factor = rt_state->equation.rgb_dst_factor,
908 .alpha.func = rt_state->equation.alpha_func,
909 .alpha.src_factor = rt_state->equation.alpha_src_factor,
910 .alpha.dst_factor = rt_state->equation.alpha_dst_factor,
911 .colormask = rt_state->equation.color_mask,
912 };
913 color = nir_color_blend(b, src0, src1, dest, &nir_rt, false);
914 }
915
916 color = nir_color_mask(b, color, dest, rt_state->equation.color_mask);
917
918 /* Throw away any channels we don't need */
919 color = nir_color_mask(b, color, nir_undef(b, 4, dest_bit_size),
920 util_format_colormask(format_desc));
921
922 /* Only write the destination if it changed */
923 if (color != dest) {
924 if (PAN_ARCH >= 6) {
925 nir_blend_pan(b, nir_load_cumulative_coverage_pan(b),
926 nir_imm_int64(b, opaque_blend_desc),
927 color,
928 .src_type = dest_type,
929 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
930 .io_semantics.num_slots = 1);
931 } else {
932 nir_store_output(b, color, nir_imm_int(b, 0),
933 .src_type = dest_type,
934 .io_semantics.location = FRAG_RESULT_DATA0 + rt,
935 .io_semantics.num_slots = 1);
936 }
937 }
938
939 if (PAN_ARCH >= 6)
940 nir_blend_return_pan(b);
941
942 b->shader->info.io_lowered = true;
943
944 return builder.shader;
945 }
946
947 #if PAN_ARCH >= 6
948
949 #if PAN_ARCH < 9
950 static enum mali_register_file_format
951 get_register_format(nir_alu_type T)
952 {
953 switch (T) {
954 case nir_type_float16:
955 return MALI_REGISTER_FILE_FORMAT_F16;
956 case nir_type_float32:
957 return MALI_REGISTER_FILE_FORMAT_F32;
958 case nir_type_int8:
959 case nir_type_int16:
960 return MALI_REGISTER_FILE_FORMAT_I16;
961 case nir_type_int32:
962 return MALI_REGISTER_FILE_FORMAT_I32;
963 case nir_type_uint8:
964 case nir_type_uint16:
965 return MALI_REGISTER_FILE_FORMAT_U16;
966 case nir_type_uint32:
967 return MALI_REGISTER_FILE_FORMAT_U32;
968 default:
969 UNREACHABLE("Invalid format");
970 }
971 }
972 #endif
973
974 uint64_t
975 GENX(pan_blend_get_internal_desc)(enum pipe_format fmt, unsigned rt,
976 unsigned force_size, bool dithered)
977 {
978 const struct util_format_description *desc = util_format_description(fmt);
979 struct mali_internal_blend_packed res;
980
981 pan_pack(&res, INTERNAL_BLEND, cfg) {
982 cfg.mode = MALI_BLEND_MODE_OPAQUE;
983 cfg.fixed_function.num_comps = desc->nr_channels;
984 cfg.fixed_function.rt = rt;
985
986 #if PAN_ARCH < 9
987 nir_alu_type T = pan_unpacked_type_for_format(desc);
988
989 if (force_size)
990 T = nir_alu_type_get_base_type(T) | force_size;
991
992 cfg.fixed_function.conversion.register_format = get_register_format(T);
993 #endif
994
995 cfg.fixed_function.conversion.memory_format =
996 GENX(pan_dithered_format_from_pipe_format)(fmt, dithered);
997 }
998
999 return res.opaque[0] | ((uint64_t)res.opaque[1] << 32);
1000 }
1001
1002 #if PAN_ARCH < 9
1003 enum mali_register_file_format
1004 GENX(pan_fixup_blend_type)(nir_alu_type T_size, enum pipe_format format)
1005 {
1006 const struct util_format_description *desc = util_format_description(format);
1007 unsigned size = nir_alu_type_get_type_size(T_size);
1008 nir_alu_type T_format = pan_unpacked_type_for_format(desc);
1009 nir_alu_type T = nir_alu_type_get_base_type(T_format) | size;
1010
1011 return pan_blend_type_from_nir(T);
1012 }
1013 #endif
1014
1015 #endif
1016
1017 #endif /* ifndef PAN_ARCH */