3acda3101c3f65333933b1544d9517cd70afcbd6
[mesa.git] / src / gallium / drivers / panfrost / pan_blending.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 *
23 */
24
25 #include <stdio.h>
26 #include "pan_blending.h"
27 #include "pan_context.h"
28 #include "gallium/auxiliary/util/u_blend.h"
29 #include "util/u_format.h"
30
31 /*
32 * Implements fixed-function blending on Midgard.
33 *
34 * Midgard splits blending into a fixed-function fast path and a programmable
35 * slow path. The fixed function blending architecture is based on "dominant"
36 * blend factors. Blending is encoded separately (but identically) between RGB
37 * and alpha functions.
38 *
39 * Essentially, for a given blending operation, there is a single dominant
40 * factor. The following dominant factors are possible:
41 *
42 * - zero
43 * - source color
44 * - destination color
45 * - source alpha
46 * - destination alpha
47 * - constant float
48 *
49 * Further, a dominant factor's arithmetic compliment could be used. For
50 * instance, to encode GL_ONE_MINUS_SOURCE_ALPHA, the dominant factor would be
51 * MALI_DOMINANT_SRC_ALPHA with the complement_dominant bit set.
52 *
53 * A single constant float can be passed to the fixed-function hardware,
54 * allowing CONSTANT_ALPHA support. Further, if all components of the constant
55 * glBlendColor are identical, CONSTANT_COLOR can be implemented with the
56 * constant float mode. If the components differ, programmable blending is
57 * required.
58 *
59 * The nondominant factor can be either:
60 *
61 * - the same as the dominant factor (MALI_BLEND_NON_MIRROR)
62 * - zero (MALI_BLEND_NON_ZERO)
63 *
64 * Exactly one of the blend operation's source or destination can be used as
65 * the dominant factor; this is selected by the
66 * MALI_BLEND_DOM_SOURCE/DESTINATION flag.
67 *
68 * By default, all blending follows the standard OpenGL addition equation:
69 *
70 * out = source_value * source_factor + destination_value * destination_factor
71 *
72 * By setting the negate_source or negate_dest bits, other blend functions can
73 * be created. For instance, for SUBTRACT mode, set the "negate destination"
74 * flag, and similarly for REVERSE_SUBTRACT with "negate source".
75 *
76 * Finally, there is a "clip modifier" controlling the final blending
77 * behaviour, allowing for the following modes:
78 *
79 * - normal
80 * - force source factor to one (MALI_BLEND_MODE_SOURCE_ONE)
81 * - force destination factor to one (MALI_BLEND_MODE_DEST_ONE)
82 *
83 * The clipping flags can be used to encode blend modes where the nondominant
84 * factor is ONE.
85 *
86 * As an example putting it all together, to encode the following blend state:
87 *
88 * glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
89 * glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
90 *
91 * We need the following configuration:
92 *
93 * - negate source (for REVERSE_SUBTRACT)
94 * - dominant factor "source alpha"
95 * - complement dominant
96 * - source dominant
97 * - force destination to ONE
98 *
99 * The following routines implement this fixed function blending encoding
100 */
101
102 /* Not all formats can be blended by fixed-function hardware */
103
104 bool
105 panfrost_can_fixed_blend(enum pipe_format format)
106 {
107 /* Fixed-function can handle sRGB */
108 format = util_format_linear(format);
109
110 /* Decompose the format */
111 const struct util_format_description *desc =
112 util_format_description(format);
113
114 /* Any 8-bit unorm is supported */
115 if (util_format_is_unorm8(desc))
116 return true;
117
118 /* Certain special formats are, too */
119 switch (format) {
120 case PIPE_FORMAT_B5G6R5_UNORM:
121 case PIPE_FORMAT_R10G10B10A2_UNORM:
122 case PIPE_FORMAT_B10G10R10A2_UNORM:
123 case PIPE_FORMAT_R10G10B10X2_UNORM:
124 case PIPE_FORMAT_B10G10R10X2_UNORM:
125 return true;
126 default:
127 return false;
128 }
129 }
130
131 /* Helper to find the uncomplemented Gallium blend factor corresponding to a
132 * complemented Gallium blend factor */
133
134 static int
135 complement_factor(int factor)
136 {
137 switch (factor) {
138 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
139 return PIPE_BLENDFACTOR_SRC_COLOR;
140
141 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
142 return PIPE_BLENDFACTOR_SRC_ALPHA;
143
144 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
145 return PIPE_BLENDFACTOR_DST_ALPHA;
146
147 case PIPE_BLENDFACTOR_INV_DST_COLOR:
148 return PIPE_BLENDFACTOR_DST_COLOR;
149
150 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
151 return PIPE_BLENDFACTOR_CONST_COLOR;
152
153 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
154 return PIPE_BLENDFACTOR_CONST_ALPHA;
155
156 default:
157 return -1;
158 }
159 }
160
161 /* Helper to strip the complement from any Gallium blend factor */
162
163 static int
164 uncomplement_factor(int factor)
165 {
166 int complement = complement_factor(factor);
167 return (complement == -1) ? factor : complement;
168 }
169
170
171 /* Attempt to find the dominant factor given a particular factor, complementing
172 * as necessary */
173
174 static bool
175 panfrost_make_dominant_factor(unsigned src_factor, enum mali_dominant_factor *factor)
176 {
177 switch (src_factor) {
178 case PIPE_BLENDFACTOR_SRC_COLOR:
179 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
180 *factor = MALI_DOMINANT_SRC_COLOR;
181 break;
182
183 case PIPE_BLENDFACTOR_SRC_ALPHA:
184 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
185 *factor = MALI_DOMINANT_SRC_ALPHA;
186 break;
187
188 case PIPE_BLENDFACTOR_DST_COLOR:
189 case PIPE_BLENDFACTOR_INV_DST_COLOR:
190 *factor = MALI_DOMINANT_DST_COLOR;
191 break;
192
193 case PIPE_BLENDFACTOR_DST_ALPHA:
194 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
195 *factor = MALI_DOMINANT_DST_ALPHA;
196 break;
197
198 case PIPE_BLENDFACTOR_ONE:
199 case PIPE_BLENDFACTOR_ZERO:
200 *factor = MALI_DOMINANT_ZERO;
201 break;
202
203 case PIPE_BLENDFACTOR_CONST_ALPHA:
204 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
205 case PIPE_BLENDFACTOR_CONST_COLOR:
206 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
207 *factor = MALI_DOMINANT_CONSTANT;
208 break;
209
210 default:
211 /* Fancy blend modes not supported */
212 return false;
213 }
214
215 return true;
216 }
217
218 /* Check if this is a special edge case blend factor, which may require the use
219 * of clip modifiers */
220
221 static bool
222 is_edge_blendfactor(unsigned factor)
223 {
224 return factor == PIPE_BLENDFACTOR_ONE || factor == PIPE_BLENDFACTOR_ZERO;
225 }
226
227 /* Perform the actual fixed function encoding. Encode the function with negate
228 * bits. Check for various cases to work out the dominant/nondominant split and
229 * accompanying flags. */
230
231 static bool
232 panfrost_make_fixed_blend_part(unsigned func, unsigned src_factor, unsigned dst_factor, unsigned *out)
233 {
234 struct mali_blend_mode part = { 0 };
235
236 /* Make sure that the blend function is representible */
237
238 switch (func) {
239 case PIPE_BLEND_ADD:
240 break;
241
242 /* TODO: Reenable subtraction modes when those fixed */
243 case PIPE_BLEND_SUBTRACT:
244 case PIPE_BLEND_REVERSE_SUBTRACT:
245 default:
246 return false;
247 }
248
249 part.clip_modifier = MALI_BLEND_MOD_NORMAL;
250
251 /* Decide which is dominant, source or destination. If one is an edge
252 * case, use the other as a factor. If they're the same, it doesn't
253 * matter; we just mirror. If they're different non-edge-cases, you
254 * need a blend shader (don't do that). */
255
256 if (is_edge_blendfactor(dst_factor)) {
257 part.dominant = MALI_BLEND_DOM_SOURCE;
258 part.nondominant_mode = MALI_BLEND_NON_ZERO;
259
260 if (dst_factor == PIPE_BLENDFACTOR_ONE)
261 part.clip_modifier = MALI_BLEND_MOD_DEST_ONE;
262 } else if (is_edge_blendfactor(src_factor)) {
263 part.dominant = MALI_BLEND_DOM_DESTINATION;
264 part.nondominant_mode = MALI_BLEND_NON_ZERO;
265
266 if (src_factor == PIPE_BLENDFACTOR_ONE)
267 part.clip_modifier = MALI_BLEND_MOD_SOURCE_ONE;
268 } else if (src_factor == dst_factor) {
269 /* XXX: Why? */
270 part.dominant = func == PIPE_BLEND_ADD ?
271 MALI_BLEND_DOM_DESTINATION : MALI_BLEND_DOM_SOURCE;
272
273 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
274 } else if (src_factor == complement_factor(dst_factor)) {
275 /* TODO: How does this work exactly? */
276 part.dominant = MALI_BLEND_DOM_SOURCE;
277 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
278 part.clip_modifier = MALI_BLEND_MOD_DEST_ONE;
279
280 /* The complement is handled by the clip modifier, don't set a
281 * complement flag */
282
283 dst_factor = src_factor;
284 } else if (dst_factor == complement_factor(src_factor)) {
285 part.dominant = MALI_BLEND_DOM_SOURCE;
286 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
287 part.clip_modifier = MALI_BLEND_MOD_SOURCE_ONE;
288
289 src_factor = dst_factor;
290 } else {
291 return false;
292 }
293
294 unsigned in_dominant_factor =
295 part.dominant == MALI_BLEND_DOM_SOURCE ? src_factor : dst_factor;
296
297 if (part.clip_modifier == MALI_BLEND_MOD_NORMAL && in_dominant_factor == PIPE_BLENDFACTOR_ONE) {
298 part.clip_modifier = part.dominant == MALI_BLEND_DOM_SOURCE ? MALI_BLEND_MOD_SOURCE_ONE : MALI_BLEND_MOD_DEST_ONE;
299 in_dominant_factor = PIPE_BLENDFACTOR_ZERO;
300 }
301
302 enum mali_dominant_factor dominant_factor;
303
304 if (!panfrost_make_dominant_factor(in_dominant_factor, &dominant_factor))
305 return false;
306
307 part.dominant_factor = dominant_factor;
308 part.complement_dominant = util_blend_factor_is_inverted(in_dominant_factor);
309
310 /* It's not clear what this does, but fixes some ADD blending tests.
311 * More research is needed XXX */
312
313 if ((part.clip_modifier == MALI_BLEND_MOD_SOURCE_ONE) && (part.dominant == MALI_BLEND_DOM_SOURCE))
314 part.negate_dest = true;
315
316 /* Write out mode */
317 memcpy(out, &part, sizeof(part));
318
319 return true;
320 }
321
322 /* We can upload a single constant for all of the factors. So, scan
323 * the factors for constants used to create a mask to check later. */
324
325 static unsigned
326 panfrost_constant_mask(unsigned *factors, unsigned num_factors)
327 {
328 unsigned mask = 0;
329
330 for (unsigned i = 0; i < num_factors; ++i) {
331 unsigned factor = uncomplement_factor(factors[i]);
332
333 if (factor == PIPE_BLENDFACTOR_CONST_COLOR)
334 mask |= 0b0111; /* RGB */
335 else if (factor == PIPE_BLENDFACTOR_CONST_ALPHA)
336 mask |= 0b1000; /* A */
337 }
338
339 return mask;
340 }
341
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
346 */
347
348 bool
349 panfrost_make_fixed_blend_mode(
350 const struct pipe_rt_blend_state *blend,
351 struct mali_blend_equation *out,
352 unsigned *constant_mask,
353 unsigned colormask)
354 {
355 /* Gallium and Mali represent colour masks identically. XXX: Static
356 * assert for future proof */
357
358 out->color_mask = colormask;
359
360 /* If no blending is enabled, default back on `replace` mode */
361
362 if (!blend->blend_enable) {
363 out->rgb_mode = 0x122;
364 out->alpha_mode = 0x122;
365 return true;
366 }
367
368 /* At draw-time, we'll need to analyze the blend constant, so
369 * precompute a mask for it -- even if we don't end up able to use
370 * fixed-function blending */
371
372 unsigned factors[] = {
373 blend->rgb_src_factor, blend->rgb_dst_factor,
374 blend->alpha_src_factor, blend->alpha_dst_factor,
375 };
376
377 *constant_mask = panfrost_constant_mask(factors, ARRAY_SIZE(factors));
378
379 /* Try to compile the actual fixed-function blend */
380
381 unsigned rgb_mode = 0;
382 unsigned alpha_mode = 0;
383
384 if (!panfrost_make_fixed_blend_part(
385 blend->rgb_func, blend->rgb_src_factor, blend->rgb_dst_factor,
386 &rgb_mode))
387 return false;
388
389 if (!panfrost_make_fixed_blend_part(
390 blend->alpha_func, blend->alpha_src_factor, blend->alpha_dst_factor,
391 &alpha_mode))
392 return false;
393
394 out->rgb_mode = rgb_mode;
395 out->alpha_mode = alpha_mode;
396
397 return true;
398 }