1/*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27#include "etnaviv_blend.h"
28
29#include "etnaviv_context.h"
30#include "etnaviv_translate.h"
31#include "pipe/p_defines.h"
32#include "util/u_memory.h"
33
34void *
35etna_blend_state_create(struct pipe_context *pctx,
36                        const struct pipe_blend_state *so)
37{
38   const struct pipe_rt_blend_state *rt0 = &so->rt[0];
39   struct etna_blend_state *co = CALLOC_STRUCT(etna_blend_state);
40
41   if (!co)
42      return NULL;
43
44   co->base = *so;
45
46   /* Enable blending if
47    * - blend enabled in blend state
48    * - NOT source factor is ONE and destination factor ZERO for both rgb and
49    *   alpha (which would mean that blending is effectively disabled)
50    */
51   bool enable = rt0->blend_enable &&
52                 !(rt0->rgb_src_factor == PIPE_BLENDFACTOR_ONE &&
53                   rt0->rgb_dst_factor == PIPE_BLENDFACTOR_ZERO &&
54                   rt0->alpha_src_factor == PIPE_BLENDFACTOR_ONE &&
55                   rt0->alpha_dst_factor == PIPE_BLENDFACTOR_ZERO);
56
57   /* Enable separate alpha if
58    * - Blending enabled (see above)
59    * - NOT source factor is equal to destination factor for both rgb abd
60    *   alpha (which would effectively that mean alpha is not separate)
61    */
62   bool separate_alpha = enable &&
63                         !(rt0->rgb_src_factor == rt0->alpha_src_factor &&
64                           rt0->rgb_dst_factor == rt0->alpha_dst_factor);
65
66   /* If the complete render target is written, set full_overwrite:
67    * - The color mask is 1111
68    * - No blending is used
69    */
70   bool full_overwrite = (rt0->colormask == 15) && !enable;
71
72   if (enable) {
73      co->PE_ALPHA_CONFIG =
74         VIVS_PE_ALPHA_CONFIG_BLEND_ENABLE_COLOR |
75         COND(separate_alpha, VIVS_PE_ALPHA_CONFIG_BLEND_SEPARATE_ALPHA) |
76         VIVS_PE_ALPHA_CONFIG_SRC_FUNC_COLOR(translate_blend_factor(rt0->rgb_src_factor)) |
77         VIVS_PE_ALPHA_CONFIG_SRC_FUNC_ALPHA(translate_blend_factor(rt0->alpha_src_factor)) |
78         VIVS_PE_ALPHA_CONFIG_DST_FUNC_COLOR(translate_blend_factor(rt0->rgb_dst_factor)) |
79         VIVS_PE_ALPHA_CONFIG_DST_FUNC_ALPHA(translate_blend_factor(rt0->alpha_dst_factor)) |
80         VIVS_PE_ALPHA_CONFIG_EQ_COLOR(translate_blend(rt0->rgb_func)) |
81         VIVS_PE_ALPHA_CONFIG_EQ_ALPHA(translate_blend(rt0->alpha_func));
82   } else {
83      co->PE_ALPHA_CONFIG = 0;
84   }
85
86   co->PE_COLOR_FORMAT =
87         VIVS_PE_COLOR_FORMAT_COMPONENTS(rt0->colormask) |
88         COND(full_overwrite, VIVS_PE_COLOR_FORMAT_OVERWRITE);
89
90   co->PE_LOGIC_OP =
91         VIVS_PE_LOGIC_OP_OP(so->logicop_enable ? so->logicop_func : LOGIC_OP_COPY) |
92         0x000E4000 /* ??? */;
93
94   /* independent_blend_enable not needed: only one rt supported */
95   /* XXX alpha_to_coverage / alpha_to_one? */
96   /* Set dither registers based on dither status. These registers set the
97    * dither pattern,
98    * for now, set the same values as the blob.
99    */
100   if (so->dither) {
101      co->PE_DITHER[0] = 0x6e4ca280;
102      co->PE_DITHER[1] = 0x5d7f91b3;
103   } else {
104      co->PE_DITHER[0] = 0xffffffff;
105      co->PE_DITHER[1] = 0xffffffff;
106   }
107
108   return co;
109}
110