2788d213fef6f306abde923d0d1fdb08abf53783
[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 case PIPE_FORMAT_B4G4R4A4_UNORM:
126 case PIPE_FORMAT_B4G4R4X4_UNORM:
127 case PIPE_FORMAT_A4R4_UNORM:
128 case PIPE_FORMAT_R4A4_UNORM:
129 case PIPE_FORMAT_A4B4G4R4_UNORM:
130 return true;
131 default:
132 return false;
133 }
134 }
135
136 /* Helper to find the uncomplemented Gallium blend factor corresponding to a
137 * complemented Gallium blend factor */
138
139 static int
140 complement_factor(int factor)
141 {
142 switch (factor) {
143 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
144 return PIPE_BLENDFACTOR_SRC_COLOR;
145
146 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
147 return PIPE_BLENDFACTOR_SRC_ALPHA;
148
149 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
150 return PIPE_BLENDFACTOR_DST_ALPHA;
151
152 case PIPE_BLENDFACTOR_INV_DST_COLOR:
153 return PIPE_BLENDFACTOR_DST_COLOR;
154
155 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
156 return PIPE_BLENDFACTOR_CONST_COLOR;
157
158 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
159 return PIPE_BLENDFACTOR_CONST_ALPHA;
160
161 default:
162 return -1;
163 }
164 }
165
166 /* Helper to strip the complement from any Gallium blend factor */
167
168 static int
169 uncomplement_factor(int factor)
170 {
171 int complement = complement_factor(factor);
172 return (complement == -1) ? factor : complement;
173 }
174
175
176 /* Attempt to find the dominant factor given a particular factor, complementing
177 * as necessary */
178
179 static bool
180 panfrost_make_dominant_factor(unsigned src_factor, enum mali_dominant_factor *factor)
181 {
182 switch (src_factor) {
183 case PIPE_BLENDFACTOR_SRC_COLOR:
184 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
185 *factor = MALI_DOMINANT_SRC_COLOR;
186 break;
187
188 case PIPE_BLENDFACTOR_SRC_ALPHA:
189 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
190 *factor = MALI_DOMINANT_SRC_ALPHA;
191 break;
192
193 case PIPE_BLENDFACTOR_DST_COLOR:
194 case PIPE_BLENDFACTOR_INV_DST_COLOR:
195 *factor = MALI_DOMINANT_DST_COLOR;
196 break;
197
198 case PIPE_BLENDFACTOR_DST_ALPHA:
199 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
200 *factor = MALI_DOMINANT_DST_ALPHA;
201 break;
202
203 case PIPE_BLENDFACTOR_ONE:
204 case PIPE_BLENDFACTOR_ZERO:
205 *factor = MALI_DOMINANT_ZERO;
206 break;
207
208 case PIPE_BLENDFACTOR_CONST_ALPHA:
209 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
210 case PIPE_BLENDFACTOR_CONST_COLOR:
211 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
212 *factor = MALI_DOMINANT_CONSTANT;
213 break;
214
215 default:
216 /* Fancy blend modes not supported */
217 return false;
218 }
219
220 return true;
221 }
222
223 /* Check if this is a special edge case blend factor, which may require the use
224 * of clip modifiers */
225
226 static bool
227 is_edge_blendfactor(unsigned factor)
228 {
229 return factor == PIPE_BLENDFACTOR_ONE || factor == PIPE_BLENDFACTOR_ZERO;
230 }
231
232 /* Perform the actual fixed function encoding. Encode the function with negate
233 * bits. Check for various cases to work out the dominant/nondominant split and
234 * accompanying flags. */
235
236 static bool
237 panfrost_make_fixed_blend_part(unsigned func, unsigned src_factor, unsigned dst_factor, unsigned *out)
238 {
239 struct mali_blend_mode part = { 0 };
240
241 /* Make sure that the blend function is representible */
242
243 switch (func) {
244 case PIPE_BLEND_ADD:
245 break;
246
247 /* TODO: Reenable subtraction modes when those fixed */
248 case PIPE_BLEND_SUBTRACT:
249 case PIPE_BLEND_REVERSE_SUBTRACT:
250 default:
251 return false;
252 }
253
254 part.clip_modifier = MALI_BLEND_MOD_NORMAL;
255
256 /* Decide which is dominant, source or destination. If one is an edge
257 * case, use the other as a factor. If they're the same, it doesn't
258 * matter; we just mirror. If they're different non-edge-cases, you
259 * need a blend shader (don't do that). */
260
261 if (is_edge_blendfactor(dst_factor)) {
262 part.dominant = MALI_BLEND_DOM_SOURCE;
263 part.nondominant_mode = MALI_BLEND_NON_ZERO;
264
265 if (dst_factor == PIPE_BLENDFACTOR_ONE)
266 part.clip_modifier = MALI_BLEND_MOD_DEST_ONE;
267 } else if (is_edge_blendfactor(src_factor)) {
268 part.dominant = MALI_BLEND_DOM_DESTINATION;
269 part.nondominant_mode = MALI_BLEND_NON_ZERO;
270
271 if (src_factor == PIPE_BLENDFACTOR_ONE)
272 part.clip_modifier = MALI_BLEND_MOD_SOURCE_ONE;
273 } else if (src_factor == dst_factor) {
274 /* XXX: Why? */
275 part.dominant = func == PIPE_BLEND_ADD ?
276 MALI_BLEND_DOM_DESTINATION : MALI_BLEND_DOM_SOURCE;
277
278 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
279 } else if (src_factor == complement_factor(dst_factor)) {
280 /* TODO: How does this work exactly? */
281 part.dominant = MALI_BLEND_DOM_SOURCE;
282 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
283 part.clip_modifier = MALI_BLEND_MOD_DEST_ONE;
284
285 /* The complement is handled by the clip modifier, don't set a
286 * complement flag */
287
288 dst_factor = src_factor;
289 } else if (dst_factor == complement_factor(src_factor)) {
290 part.dominant = MALI_BLEND_DOM_SOURCE;
291 part.nondominant_mode = MALI_BLEND_NON_MIRROR;
292 part.clip_modifier = MALI_BLEND_MOD_SOURCE_ONE;
293
294 src_factor = dst_factor;
295 } else {
296 return false;
297 }
298
299 unsigned in_dominant_factor =
300 part.dominant == MALI_BLEND_DOM_SOURCE ? src_factor : dst_factor;
301
302 if (part.clip_modifier == MALI_BLEND_MOD_NORMAL && in_dominant_factor == PIPE_BLENDFACTOR_ONE) {
303 part.clip_modifier = part.dominant == MALI_BLEND_DOM_SOURCE ? MALI_BLEND_MOD_SOURCE_ONE : MALI_BLEND_MOD_DEST_ONE;
304 in_dominant_factor = PIPE_BLENDFACTOR_ZERO;
305 }
306
307 enum mali_dominant_factor dominant_factor;
308
309 if (!panfrost_make_dominant_factor(in_dominant_factor, &dominant_factor))
310 return false;
311
312 part.dominant_factor = dominant_factor;
313 part.complement_dominant = util_blend_factor_is_inverted(in_dominant_factor);
314
315 /* It's not clear what this does, but fixes some ADD blending tests.
316 * More research is needed XXX */
317
318 if ((part.clip_modifier == MALI_BLEND_MOD_SOURCE_ONE) && (part.dominant == MALI_BLEND_DOM_SOURCE))
319 part.negate_dest = true;
320
321 /* Write out mode */
322 memcpy(out, &part, sizeof(part));
323
324 return true;
325 }
326
327 /* We can upload a single constant for all of the factors. So, scan
328 * the factors for constants used to create a mask to check later. */
329
330 static unsigned
331 panfrost_constant_mask(unsigned *factors, unsigned num_factors)
332 {
333 unsigned mask = 0;
334
335 for (unsigned i = 0; i < num_factors; ++i) {
336 unsigned factor = uncomplement_factor(factors[i]);
337
338 if (factor == PIPE_BLENDFACTOR_CONST_COLOR)
339 mask |= 0b0111; /* RGB */
340 else if (factor == PIPE_BLENDFACTOR_CONST_ALPHA)
341 mask |= 0b1000; /* A */
342 }
343
344 return mask;
345 }
346
347 /* Create the descriptor for a fixed blend mode given the corresponding Gallium
348 * state, if possible. Return true and write out the blend descriptor into
349 * blend_equation. If it is not possible with the fixed function
350 * representating, return false to handle degenerate cases with a blend shader
351 */
352
353 bool
354 panfrost_make_fixed_blend_mode(
355 const struct pipe_rt_blend_state *blend,
356 struct mali_blend_equation *out,
357 unsigned *constant_mask,
358 unsigned colormask)
359 {
360 /* Gallium and Mali represent colour masks identically. XXX: Static
361 * assert for future proof */
362
363 out->color_mask = colormask;
364
365 /* If no blending is enabled, default back on `replace` mode */
366
367 if (!blend->blend_enable) {
368 out->rgb_mode = 0x122;
369 out->alpha_mode = 0x122;
370 return true;
371 }
372
373 /* At draw-time, we'll need to analyze the blend constant, so
374 * precompute a mask for it -- even if we don't end up able to use
375 * fixed-function blending */
376
377 unsigned factors[] = {
378 blend->rgb_src_factor, blend->rgb_dst_factor,
379 blend->alpha_src_factor, blend->alpha_dst_factor,
380 };
381
382 *constant_mask = panfrost_constant_mask(factors, ARRAY_SIZE(factors));
383
384 /* Try to compile the actual fixed-function blend */
385
386 unsigned rgb_mode = 0;
387 unsigned alpha_mode = 0;
388
389 if (!panfrost_make_fixed_blend_part(
390 blend->rgb_func, blend->rgb_src_factor, blend->rgb_dst_factor,
391 &rgb_mode))
392 return false;
393
394 if (!panfrost_make_fixed_blend_part(
395 blend->alpha_func, blend->alpha_src_factor, blend->alpha_dst_factor,
396 &alpha_mode))
397 return false;
398
399 out->rgb_mode = rgb_mode;
400 out->alpha_mode = alpha_mode;
401
402 return true;
403 }