2 * © Copyright 2018 Alyssa Rosenzweig
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "pan_blending.h"
29 * Implements fixed-function blending on Midgard.
31 * Midgard splits blending into a fixed-function fast path and a programmable
32 * slow path. The fixed function blending architecture is based on "dominant"
33 * blend factors. Blending is encoded separately (but identically) between RGB
34 * and alpha functions.
36 * Essentially, for a given blending operation, there is a single dominant
37 * factor. The following dominant factors are possible:
46 * Further, a dominant factor's arithmetic compliment could be used. For
47 * instance, to encode GL_ONE_MINUS_SOURCE_ALPHA, the dominant factor would be
48 * MALI_DOMINANT_SRC_ALPHA with the complement_dominant bit set.
50 * A single constant float can be passed to the fixed-function hardware,
51 * allowing CONSTANT_ALPHA support. Further, if all components of the constant
52 * glBlendColor are identical, CONSTANT_COLOR can be implemented with the
53 * constant float mode. If the components differ, programmable blending is
56 * The nondominant factor can be either:
58 * - the same as the dominant factor (MALI_BLEND_NON_MIRROR)
59 * - zero (MALI_BLEND_NON_ZERO)
61 * Exactly one of the blend operation's source or destination can be used as
62 * the dominant factor; this is selected by the
63 * MALI_BLEND_DOM_SOURCE/DESTINATION flag.
65 * By default, all blending follows the standard OpenGL addition equation:
67 * out = source_value * source_factor + destination_value * destination_factor
69 * By setting the negate_source or negate_dest bits, other blend functions can
70 * be created. For instance, for SUBTRACT mode, set the "negate destination"
71 * flag, and similarly for REVERSE_SUBTRACT with "negate source".
73 * Finally, there is a "clip modifier" controlling the final blending
74 * behaviour, allowing for the following modes:
77 * - force source factor to one (MALI_BLEND_MODE_SOURCE_ONE)
78 * - force destination factor to one (MALI_BLEND_MODE_DEST_ONE)
80 * The clipping flags can be used to encode blend modes where the nondominant
83 * As an example putting it all together, to encode the following blend state:
85 * glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
86 * glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
88 * We need the following configuration:
90 * - negate source (for REVERSE_SUBTRACT)
91 * - dominant factor "source alpha"
92 * - compliment dominant
94 * - force destination to ONE
96 * The following routines implement this fixed function blending encoding
99 /* Helper to find the uncomplemented Gallium blend factor corresponding to a
100 * complemented Gallium blend factor */
103 complement_factor(int factor
)
106 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
107 return PIPE_BLENDFACTOR_SRC_COLOR
;
109 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
110 return PIPE_BLENDFACTOR_SRC_ALPHA
;
112 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
113 return PIPE_BLENDFACTOR_DST_ALPHA
;
115 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
116 return PIPE_BLENDFACTOR_DST_COLOR
;
118 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
119 return PIPE_BLENDFACTOR_CONST_COLOR
;
121 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
122 return PIPE_BLENDFACTOR_CONST_ALPHA
;
129 /* Helper to strip the complement from any Gallium blend factor */
132 uncomplement_factor(int factor
)
134 int complement
= complement_factor(factor
);
135 return (complement
== -1) ? factor
: complement
;
139 /* Attempt to find the dominant factor given a particular factor, complementing
143 panfrost_make_dominant_factor(unsigned src_factor
, enum mali_dominant_factor
*factor
, bool *invert
)
145 switch (src_factor
) {
146 case PIPE_BLENDFACTOR_SRC_COLOR
:
147 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
148 *factor
= MALI_DOMINANT_SRC_COLOR
;
151 case PIPE_BLENDFACTOR_SRC_ALPHA
:
152 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
153 *factor
= MALI_DOMINANT_SRC_ALPHA
;
156 case PIPE_BLENDFACTOR_DST_COLOR
:
157 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
158 *factor
= MALI_DOMINANT_DST_COLOR
;
161 case PIPE_BLENDFACTOR_DST_ALPHA
:
162 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
163 *factor
= MALI_DOMINANT_DST_ALPHA
;
166 case PIPE_BLENDFACTOR_ONE
:
167 case PIPE_BLENDFACTOR_ZERO
:
168 *factor
= MALI_DOMINANT_ZERO
;
171 case PIPE_BLENDFACTOR_CONST_ALPHA
:
172 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
173 case PIPE_BLENDFACTOR_CONST_COLOR
:
174 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
175 *factor
= MALI_DOMINANT_CONSTANT
;
179 /* Fancy blend modes not supported */
183 /* Set invert flags */
185 switch (src_factor
) {
186 case PIPE_BLENDFACTOR_ONE
:
187 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
188 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
189 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
190 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
191 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
192 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
193 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
194 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
204 /* Check if this is a special edge case blend factor, which may require the use
205 * of clip modifiers */
208 is_edge_blendfactor(unsigned factor
)
210 return factor
== PIPE_BLENDFACTOR_ONE
|| factor
== PIPE_BLENDFACTOR_ZERO
;
213 /* Perform the actual fixed function encoding. Encode the function with negate
214 * bits. Check for various cases to work out the dominant/nondominant split and
215 * accompanying flags. */
218 panfrost_make_fixed_blend_part(unsigned func
, unsigned src_factor
, unsigned dst_factor
, unsigned *out
)
220 struct mali_blend_mode part
= { 0 };
222 /* Make sure that the blend function is representible with negate flags */
224 if (func
== PIPE_BLEND_ADD
) {
225 /* Default, no modifiers needed */
226 } else if (func
== PIPE_BLEND_SUBTRACT
)
227 part
.negate_dest
= true;
228 else if (func
== PIPE_BLEND_REVERSE_SUBTRACT
)
229 part
.negate_source
= true;
233 part
.clip_modifier
= MALI_BLEND_MOD_NORMAL
;
235 /* Decide which is dominant, source or destination. If one is an edge
236 * case, use the other as a factor. If they're the same, it doesn't
237 * matter; we just mirror. If they're different non-edge-cases, you
238 * need a blend shader (don't do that). */
240 if (is_edge_blendfactor(dst_factor
)) {
241 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
242 part
.nondominant_mode
= MALI_BLEND_NON_ZERO
;
244 if (dst_factor
== PIPE_BLENDFACTOR_ONE
)
245 part
.clip_modifier
= MALI_BLEND_MOD_DEST_ONE
;
246 } else if (is_edge_blendfactor(src_factor
)) {
247 part
.dominant
= MALI_BLEND_DOM_DESTINATION
;
248 part
.nondominant_mode
= MALI_BLEND_NON_ZERO
;
250 if (src_factor
== PIPE_BLENDFACTOR_ONE
)
251 part
.clip_modifier
= MALI_BLEND_MOD_SOURCE_ONE
;
253 } else if (src_factor
== dst_factor
) {
254 part
.dominant
= MALI_BLEND_DOM_DESTINATION
; /* Ought to be an arbitrary choice, but we need to set destination for some reason? Align with the blob until we understand more */
255 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
256 } else if (src_factor
== complement_factor(dst_factor
)) {
257 /* TODO: How does this work exactly? */
258 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
259 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
260 part
.clip_modifier
= MALI_BLEND_MOD_DEST_ONE
;
261 } else if (dst_factor
== complement_factor(src_factor
)) {
262 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
263 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
264 part
.clip_modifier
= /*MALI_BLEND_MOD_SOURCE_ONE*/MALI_BLEND_MOD_DEST_ONE
; /* Which modifier should it be? */
266 printf("Failed to find dominant factor?\n");
270 unsigned in_dominant_factor
=
271 part
.dominant
== MALI_BLEND_DOM_SOURCE
? src_factor
: dst_factor
;
273 if (part
.clip_modifier
== MALI_BLEND_MOD_NORMAL
&& in_dominant_factor
== PIPE_BLENDFACTOR_ONE
) {
274 part
.clip_modifier
= part
.dominant
== MALI_BLEND_DOM_SOURCE
? MALI_BLEND_MOD_SOURCE_ONE
: MALI_BLEND_MOD_DEST_ONE
;
275 in_dominant_factor
= PIPE_BLENDFACTOR_ZERO
;
278 bool invert_dominant
= false;
279 enum mali_dominant_factor dominant_factor
;
281 if (!panfrost_make_dominant_factor(in_dominant_factor
, &dominant_factor
, &invert_dominant
))
284 part
.dominant_factor
= dominant_factor
;
285 part
.complement_dominant
= invert_dominant
;
288 memcpy(out
, &part
, sizeof(part
));
293 /* We can upload a single constant for all of the factors. So, scan the factors
294 * for constants used, and scan the constants for the constants used. If there
295 * is a single unique constant, output that. If there are multiple,
296 * fixed-function operation breaks down. */
299 panfrost_make_constant(unsigned *factors
, unsigned num_factors
, const struct pipe_blend_color
*blend_color
, void *out
)
301 /* Color components used */
302 bool cc
[4] = { false };
304 for (unsigned i
= 0; i
< num_factors
; ++i
) {
305 unsigned factor
= uncomplement_factor(factors
[i
]);
307 if (factor
== PIPE_BLENDFACTOR_CONST_COLOR
)
308 cc
[0] = cc
[1] = cc
[2] = true;
309 else if (factor
== PIPE_BLENDFACTOR_CONST_ALPHA
)
313 /* Find the actual constant associated with the components used*/
315 float constant
= 0.0;
316 bool has_constant
= false;
318 for (unsigned i
= 0; i
< 4; ++i
) {
319 /* If the component is unused, nothing to do */
320 if (!cc
[i
]) continue;
322 float value
= blend_color
->color
[i
];
324 /* Either there's a second constant, in which case we fail, or
325 * there's no constant / a first constant, in which case we use
328 if (has_constant
&& constant
!= value
) {
336 /* We have the constant -- success! */
338 memcpy(out
, &constant
, sizeof(float));
342 /* Create the descriptor for a fixed blend mode given the corresponding Gallium
343 * state, if possible. Return true and write out the blend descriptor into
344 * blend_equation. If it is not possible with the fixed function
345 * representating, return false to handle degenerate cases with a blend shader
348 static const struct pipe_rt_blend_state default_blend
= {
351 .rgb_func
= PIPE_BLEND_ADD
,
352 .rgb_src_factor
= PIPE_BLENDFACTOR_ONE
,
353 .rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
,
355 .alpha_func
= PIPE_BLEND_ADD
,
356 .alpha_src_factor
= PIPE_BLENDFACTOR_ONE
,
357 .alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
,
359 .colormask
= PIPE_MASK_RGBA
363 panfrost_make_fixed_blend_mode(const struct pipe_rt_blend_state
*blend
, struct mali_blend_equation
*out
, unsigned colormask
, const struct pipe_blend_color
*blend_color
)
365 /* If no blending is enabled, default back on `replace` mode */
367 if (!blend
->blend_enable
)
368 return panfrost_make_fixed_blend_mode(&default_blend
, out
, colormask
, blend_color
);
370 /* We have room only for a single float32 constant between the four
371 * components. If we need more, spill to the programmable pipeline. */
373 unsigned factors
[] = {
374 blend
->rgb_src_factor
, blend
->rgb_dst_factor
,
375 blend
->alpha_src_factor
, blend
->alpha_dst_factor
,
378 if (!panfrost_make_constant(factors
, ARRAY_SIZE(factors
), blend_color
, &out
->constant
))
381 unsigned rgb_mode
= 0;
382 unsigned alpha_mode
= 0;
384 if (!panfrost_make_fixed_blend_part(
385 blend
->rgb_func
, blend
->rgb_src_factor
, blend
->rgb_dst_factor
,
389 if (!panfrost_make_fixed_blend_part(
390 blend
->alpha_func
, blend
->alpha_src_factor
, blend
->alpha_dst_factor
,
394 out
->rgb_mode
= rgb_mode
;
395 out
->alpha_mode
= alpha_mode
;
397 /* Gallium and Mali represent colour masks identically. XXX: Static assert for future proof */
398 out
->color_mask
= colormask
;