etnaviv: fix blend color for RB swapped rendertargets
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_blend.c
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
34 void *
35 etna_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 co->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 = co->enable &&
63 !(rt0->rgb_src_factor == rt0->alpha_src_factor &&
64 rt0->rgb_dst_factor == rt0->alpha_dst_factor);
65
66 if (co->enable) {
67 co->PE_ALPHA_CONFIG =
68 VIVS_PE_ALPHA_CONFIG_BLEND_ENABLE_COLOR |
69 COND(separate_alpha, VIVS_PE_ALPHA_CONFIG_BLEND_SEPARATE_ALPHA) |
70 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_COLOR(translate_blend_factor(rt0->rgb_src_factor)) |
71 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_ALPHA(translate_blend_factor(rt0->alpha_src_factor)) |
72 VIVS_PE_ALPHA_CONFIG_DST_FUNC_COLOR(translate_blend_factor(rt0->rgb_dst_factor)) |
73 VIVS_PE_ALPHA_CONFIG_DST_FUNC_ALPHA(translate_blend_factor(rt0->alpha_dst_factor)) |
74 VIVS_PE_ALPHA_CONFIG_EQ_COLOR(translate_blend(rt0->rgb_func)) |
75 VIVS_PE_ALPHA_CONFIG_EQ_ALPHA(translate_blend(rt0->alpha_func));
76 } else {
77 co->PE_ALPHA_CONFIG = 0;
78 }
79
80 co->PE_LOGIC_OP =
81 VIVS_PE_LOGIC_OP_OP(so->logicop_enable ? so->logicop_func : LOGIC_OP_COPY) |
82 0x000E4000 /* ??? */;
83
84 /* independent_blend_enable not needed: only one rt supported */
85 /* XXX alpha_to_coverage / alpha_to_one? */
86 /* Set dither registers based on dither status. These registers set the
87 * dither pattern,
88 * for now, set the same values as the blob.
89 */
90 if (so->dither) {
91 co->PE_DITHER[0] = 0x6e4ca280;
92 co->PE_DITHER[1] = 0x5d7f91b3;
93 } else {
94 co->PE_DITHER[0] = 0xffffffff;
95 co->PE_DITHER[1] = 0xffffffff;
96 }
97
98 return co;
99 }
100
101 bool
102 etna_update_blend(struct etna_context *ctx)
103 {
104 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
105 struct pipe_blend_state *pblend = ctx->blend;
106 struct etna_blend_state *blend = etna_blend_state(pblend);
107 const struct pipe_rt_blend_state *rt0 = &pblend->rt[0];
108 uint32_t colormask;
109
110 if (pfb->cbufs[0] &&
111 translate_rs_format_rb_swap(pfb->cbufs[0]->texture->format)) {
112 colormask = rt0->colormask & (PIPE_MASK_A | PIPE_MASK_G);
113 if (rt0->colormask & PIPE_MASK_R)
114 colormask |= PIPE_MASK_B;
115 if (rt0->colormask & PIPE_MASK_B)
116 colormask |= PIPE_MASK_R;
117 } else {
118 colormask = rt0->colormask;
119 }
120
121 /* If the complete render target is written, set full_overwrite:
122 * - The color mask is 1111
123 * - No blending is used
124 */
125 bool full_overwrite = (rt0->colormask == 0xf) && !blend->enable;
126 blend->PE_COLOR_FORMAT =
127 VIVS_PE_COLOR_FORMAT_COMPONENTS(colormask) |
128 COND(full_overwrite, VIVS_PE_COLOR_FORMAT_OVERWRITE);
129
130 return true;
131 }
132
133 void
134 etna_set_blend_color(struct pipe_context *pctx, const struct pipe_blend_color *bc)
135 {
136 struct etna_context *ctx = etna_context(pctx);
137 struct compiled_blend_color *cs = &ctx->blend_color;
138
139 memcpy(cs->color, bc->color, sizeof(float) * 4);
140
141 ctx->dirty |= ETNA_DIRTY_BLEND_COLOR;
142 }
143
144 bool
145 etna_update_blend_color(struct etna_context *ctx)
146 {
147 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
148 struct compiled_blend_color *cs = &ctx->blend_color;
149
150 if (pfb->cbufs[0] &&
151 translate_rs_format_rb_swap(pfb->cbufs[0]->texture->format)) {
152 cs->PE_ALPHA_BLEND_COLOR =
153 VIVS_PE_ALPHA_BLEND_COLOR_R(etna_cfloat_to_uint8(cs->color[2])) |
154 VIVS_PE_ALPHA_BLEND_COLOR_G(etna_cfloat_to_uint8(cs->color[1])) |
155 VIVS_PE_ALPHA_BLEND_COLOR_B(etna_cfloat_to_uint8(cs->color[0])) |
156 VIVS_PE_ALPHA_BLEND_COLOR_A(etna_cfloat_to_uint8(cs->color[3]));
157 } else {
158 cs->PE_ALPHA_BLEND_COLOR =
159 VIVS_PE_ALPHA_BLEND_COLOR_R(etna_cfloat_to_uint8(cs->color[0])) |
160 VIVS_PE_ALPHA_BLEND_COLOR_G(etna_cfloat_to_uint8(cs->color[1])) |
161 VIVS_PE_ALPHA_BLEND_COLOR_B(etna_cfloat_to_uint8(cs->color[2])) |
162 VIVS_PE_ALPHA_BLEND_COLOR_A(etna_cfloat_to_uint8(cs->color[3]));
163 }
164
165 return true;
166 }