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"
27 #include "pan_context.h"
28 #include "gallium/auxiliary/util/u_blend.h"
31 * Implements fixed-function blending on Midgard.
33 * Midgard splits blending into a fixed-function fast path and a programmable
34 * slow path. The fixed function blending architecture is based on "dominant"
35 * blend factors. Blending is encoded separately (but identically) between RGB
36 * and alpha functions.
38 * Essentially, for a given blending operation, there is a single dominant
39 * factor. The following dominant factors are possible:
48 * Further, a dominant factor's arithmetic compliment could be used. For
49 * instance, to encode GL_ONE_MINUS_SOURCE_ALPHA, the dominant factor would be
50 * MALI_DOMINANT_SRC_ALPHA with the complement_dominant bit set.
52 * A single constant float can be passed to the fixed-function hardware,
53 * allowing CONSTANT_ALPHA support. Further, if all components of the constant
54 * glBlendColor are identical, CONSTANT_COLOR can be implemented with the
55 * constant float mode. If the components differ, programmable blending is
58 * The nondominant factor can be either:
60 * - the same as the dominant factor (MALI_BLEND_NON_MIRROR)
61 * - zero (MALI_BLEND_NON_ZERO)
63 * Exactly one of the blend operation's source or destination can be used as
64 * the dominant factor; this is selected by the
65 * MALI_BLEND_DOM_SOURCE/DESTINATION flag.
67 * By default, all blending follows the standard OpenGL addition equation:
69 * out = source_value * source_factor + destination_value * destination_factor
71 * By setting the negate_source or negate_dest bits, other blend functions can
72 * be created. For instance, for SUBTRACT mode, set the "negate destination"
73 * flag, and similarly for REVERSE_SUBTRACT with "negate source".
75 * Finally, there is a "clip modifier" controlling the final blending
76 * behaviour, allowing for the following modes:
79 * - force source factor to one (MALI_BLEND_MODE_SOURCE_ONE)
80 * - force destination factor to one (MALI_BLEND_MODE_DEST_ONE)
82 * The clipping flags can be used to encode blend modes where the nondominant
85 * As an example putting it all together, to encode the following blend state:
87 * glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
88 * glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
90 * We need the following configuration:
92 * - negate source (for REVERSE_SUBTRACT)
93 * - dominant factor "source alpha"
94 * - complement dominant
96 * - force destination to ONE
98 * The following routines implement this fixed function blending encoding
101 /* Helper to find the uncomplemented Gallium blend factor corresponding to a
102 * complemented Gallium blend factor */
105 complement_factor(int factor
)
108 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
109 return PIPE_BLENDFACTOR_SRC_COLOR
;
111 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
112 return PIPE_BLENDFACTOR_SRC_ALPHA
;
114 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
115 return PIPE_BLENDFACTOR_DST_ALPHA
;
117 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
118 return PIPE_BLENDFACTOR_DST_COLOR
;
120 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
121 return PIPE_BLENDFACTOR_CONST_COLOR
;
123 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
124 return PIPE_BLENDFACTOR_CONST_ALPHA
;
131 /* Helper to strip the complement from any Gallium blend factor */
134 uncomplement_factor(int factor
)
136 int complement
= complement_factor(factor
);
137 return (complement
== -1) ? factor
: complement
;
141 /* Attempt to find the dominant factor given a particular factor, complementing
145 panfrost_make_dominant_factor(unsigned src_factor
, enum mali_dominant_factor
*factor
)
147 switch (src_factor
) {
148 case PIPE_BLENDFACTOR_SRC_COLOR
:
149 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
150 *factor
= MALI_DOMINANT_SRC_COLOR
;
153 case PIPE_BLENDFACTOR_SRC_ALPHA
:
154 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
155 *factor
= MALI_DOMINANT_SRC_ALPHA
;
158 case PIPE_BLENDFACTOR_DST_COLOR
:
159 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
160 *factor
= MALI_DOMINANT_DST_COLOR
;
163 case PIPE_BLENDFACTOR_DST_ALPHA
:
164 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
165 *factor
= MALI_DOMINANT_DST_ALPHA
;
168 case PIPE_BLENDFACTOR_ONE
:
169 case PIPE_BLENDFACTOR_ZERO
:
170 *factor
= MALI_DOMINANT_ZERO
;
173 case PIPE_BLENDFACTOR_CONST_ALPHA
:
174 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
175 case PIPE_BLENDFACTOR_CONST_COLOR
:
176 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
177 *factor
= MALI_DOMINANT_CONSTANT
;
181 /* Fancy blend modes not supported */
188 /* Check if this is a special edge case blend factor, which may require the use
189 * of clip modifiers */
192 is_edge_blendfactor(unsigned factor
)
194 return factor
== PIPE_BLENDFACTOR_ONE
|| factor
== PIPE_BLENDFACTOR_ZERO
;
197 /* Perform the actual fixed function encoding. Encode the function with negate
198 * bits. Check for various cases to work out the dominant/nondominant split and
199 * accompanying flags. */
202 panfrost_make_fixed_blend_part(unsigned func
, unsigned src_factor
, unsigned dst_factor
, unsigned *out
)
204 struct mali_blend_mode part
= { 0 };
206 /* Make sure that the blend function is representible */
212 /* TODO: Reenable subtraction modes when those fixed */
213 case PIPE_BLEND_SUBTRACT
:
214 case PIPE_BLEND_REVERSE_SUBTRACT
:
219 part
.clip_modifier
= MALI_BLEND_MOD_NORMAL
;
221 /* Decide which is dominant, source or destination. If one is an edge
222 * case, use the other as a factor. If they're the same, it doesn't
223 * matter; we just mirror. If they're different non-edge-cases, you
224 * need a blend shader (don't do that). */
226 if (is_edge_blendfactor(dst_factor
)) {
227 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
228 part
.nondominant_mode
= MALI_BLEND_NON_ZERO
;
230 if (dst_factor
== PIPE_BLENDFACTOR_ONE
)
231 part
.clip_modifier
= MALI_BLEND_MOD_DEST_ONE
;
232 } else if (is_edge_blendfactor(src_factor
)) {
233 part
.dominant
= MALI_BLEND_DOM_DESTINATION
;
234 part
.nondominant_mode
= MALI_BLEND_NON_ZERO
;
236 if (src_factor
== PIPE_BLENDFACTOR_ONE
)
237 part
.clip_modifier
= MALI_BLEND_MOD_SOURCE_ONE
;
238 } else if (src_factor
== dst_factor
) {
240 part
.dominant
= func
== PIPE_BLEND_ADD
?
241 MALI_BLEND_DOM_DESTINATION
: MALI_BLEND_DOM_SOURCE
;
243 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
244 } else if (src_factor
== complement_factor(dst_factor
)) {
245 /* TODO: How does this work exactly? */
246 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
247 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
248 part
.clip_modifier
= MALI_BLEND_MOD_DEST_ONE
;
250 /* The complement is handled by the clip modifier, don't set a
253 dst_factor
= src_factor
;
254 } else if (dst_factor
== complement_factor(src_factor
)) {
255 part
.dominant
= MALI_BLEND_DOM_SOURCE
;
256 part
.nondominant_mode
= MALI_BLEND_NON_MIRROR
;
257 part
.clip_modifier
= MALI_BLEND_MOD_SOURCE_ONE
;
259 src_factor
= dst_factor
;
264 unsigned in_dominant_factor
=
265 part
.dominant
== MALI_BLEND_DOM_SOURCE
? src_factor
: dst_factor
;
267 if (part
.clip_modifier
== MALI_BLEND_MOD_NORMAL
&& in_dominant_factor
== PIPE_BLENDFACTOR_ONE
) {
268 part
.clip_modifier
= part
.dominant
== MALI_BLEND_DOM_SOURCE
? MALI_BLEND_MOD_SOURCE_ONE
: MALI_BLEND_MOD_DEST_ONE
;
269 in_dominant_factor
= PIPE_BLENDFACTOR_ZERO
;
272 enum mali_dominant_factor dominant_factor
;
274 if (!panfrost_make_dominant_factor(in_dominant_factor
, &dominant_factor
))
277 part
.dominant_factor
= dominant_factor
;
278 part
.complement_dominant
= util_blend_factor_is_inverted(in_dominant_factor
);
280 /* It's not clear what this does, but fixes some ADD blending tests.
281 * More research is needed XXX */
283 if ((part
.clip_modifier
== MALI_BLEND_MOD_SOURCE_ONE
) && (part
.dominant
== MALI_BLEND_DOM_SOURCE
))
284 part
.negate_dest
= true;
287 memcpy(out
, &part
, sizeof(part
));
292 /* We can upload a single constant for all of the factors. So, scan the factors
293 * for constants used, and scan the constants for the constants used. If there
294 * is a single unique constant, output that. If there are multiple,
295 * fixed-function operation breaks down. */
298 panfrost_make_constant(unsigned *factors
, unsigned num_factors
, const struct pipe_blend_color
*blend_color
, void *out
)
300 /* Color components used */
301 bool cc
[4] = { false };
303 for (unsigned i
= 0; i
< num_factors
; ++i
) {
304 unsigned factor
= uncomplement_factor(factors
[i
]);
306 if (factor
== PIPE_BLENDFACTOR_CONST_COLOR
)
307 cc
[0] = cc
[1] = cc
[2] = true;
308 else if (factor
== PIPE_BLENDFACTOR_CONST_ALPHA
)
312 /* Find the actual constant associated with the components used*/
314 float constant
= 0.0;
315 bool has_constant
= false;
317 for (unsigned i
= 0; i
< 4; ++i
) {
318 /* If the component is unused, nothing to do */
319 if (!cc
[i
]) continue;
321 float value
= blend_color
->color
[i
];
323 /* Either there's a second constant, in which case we fail, or
324 * there's no constant / a first constant, in which case we use
327 if (has_constant
&& constant
!= value
) {
335 /* We have the constant -- success! */
337 memcpy(out
, &constant
, sizeof(float));
341 /* Create the descriptor for a fixed blend mode given the corresponding Gallium
342 * state, if possible. Return true and write out the blend descriptor into
343 * blend_equation. If it is not possible with the fixed function
344 * representating, return false to handle degenerate cases with a blend shader
348 panfrost_make_fixed_blend_mode(const struct pipe_rt_blend_state
*blend
, struct panfrost_blend_state
*so
, unsigned colormask
, const struct pipe_blend_color
*blend_color
)
350 struct mali_blend_equation
*out
= &so
->equation
;
352 /* Gallium and Mali represent colour masks identically. XXX: Static assert for future proof */
353 out
->color_mask
= colormask
;
355 /* If no blending is enabled, default back on `replace` mode */
357 if (!blend
->blend_enable
) {
358 out
->rgb_mode
= 0x122;
359 out
->alpha_mode
= 0x122;
363 /* We have room only for a single float32 constant between the four
364 * components. If we need more, spill to the programmable pipeline. */
366 unsigned factors
[] = {
367 blend
->rgb_src_factor
, blend
->rgb_dst_factor
,
368 blend
->alpha_src_factor
, blend
->alpha_dst_factor
,
371 if (!panfrost_make_constant(factors
, ARRAY_SIZE(factors
), blend_color
, &so
->constant
))
374 unsigned rgb_mode
= 0;
375 unsigned alpha_mode
= 0;
377 if (!panfrost_make_fixed_blend_part(
378 blend
->rgb_func
, blend
->rgb_src_factor
, blend
->rgb_dst_factor
,
382 if (!panfrost_make_fixed_blend_part(
383 blend
->alpha_func
, blend
->alpha_src_factor
, blend
->alpha_dst_factor
,
387 out
->rgb_mode
= rgb_mode
;
388 out
->alpha_mode
= alpha_mode
;