etnaviv: update Android build files
[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_screen.h"
31 #include "etnaviv_translate.h"
32 #include "hw/common.xml.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_memory.h"
35 #include "util/u_half.h"
36
37 void *
38 etna_blend_state_create(struct pipe_context *pctx,
39 const struct pipe_blend_state *so)
40 {
41 struct etna_context *ctx = etna_context(pctx);
42 const struct pipe_rt_blend_state *rt0 = &so->rt[0];
43 struct etna_blend_state *co = CALLOC_STRUCT(etna_blend_state);
44 bool alpha_enable, logicop_enable;
45
46 if (!co)
47 return NULL;
48
49 co->base = *so;
50
51 /* Enable blending if
52 * - blend enabled in blend state
53 * - NOT source factor is ONE and destination factor ZERO and eq is ADD for
54 * both rgb and alpha (which mean that blending is effectively disabled)
55 */
56 alpha_enable = rt0->blend_enable &&
57 !(rt0->rgb_src_factor == PIPE_BLENDFACTOR_ONE &&
58 rt0->rgb_dst_factor == PIPE_BLENDFACTOR_ZERO &&
59 rt0->rgb_func == PIPE_BLEND_ADD &&
60 rt0->alpha_src_factor == PIPE_BLENDFACTOR_ONE &&
61 rt0->alpha_dst_factor == PIPE_BLENDFACTOR_ZERO &&
62 rt0->alpha_func == PIPE_BLEND_ADD);
63
64 /* Enable separate alpha if
65 * - Blending enabled (see above)
66 * - NOT source/destination factor and eq is same for both rgb and alpha
67 * (which would effectively that mean alpha is not separate), and
68 */
69 bool separate_alpha = alpha_enable &&
70 !(rt0->rgb_src_factor == rt0->alpha_src_factor &&
71 rt0->rgb_dst_factor == rt0->alpha_dst_factor &&
72 rt0->rgb_func == rt0->alpha_func);
73
74 if (alpha_enable) {
75 co->PE_ALPHA_CONFIG =
76 VIVS_PE_ALPHA_CONFIG_BLEND_ENABLE_COLOR |
77 COND(separate_alpha, VIVS_PE_ALPHA_CONFIG_BLEND_SEPARATE_ALPHA) |
78 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_COLOR(translate_blend_factor(rt0->rgb_src_factor)) |
79 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_ALPHA(translate_blend_factor(rt0->alpha_src_factor)) |
80 VIVS_PE_ALPHA_CONFIG_DST_FUNC_COLOR(translate_blend_factor(rt0->rgb_dst_factor)) |
81 VIVS_PE_ALPHA_CONFIG_DST_FUNC_ALPHA(translate_blend_factor(rt0->alpha_dst_factor)) |
82 VIVS_PE_ALPHA_CONFIG_EQ_COLOR(translate_blend(rt0->rgb_func)) |
83 VIVS_PE_ALPHA_CONFIG_EQ_ALPHA(translate_blend(rt0->alpha_func));
84 } else {
85 co->PE_ALPHA_CONFIG = 0;
86 }
87
88 logicop_enable = so->logicop_enable &&
89 VIV_FEATURE(ctx->screen, chipMinorFeatures2, LOGIC_OP);
90
91 co->PE_LOGIC_OP =
92 VIVS_PE_LOGIC_OP_OP(logicop_enable ? so->logicop_func : LOGIC_OP_COPY) |
93 VIVS_PE_LOGIC_OP_DITHER_MODE(3) | /* TODO: related to dithering, sometimes 2 */
94 0x000E4000 /* ??? */;
95
96 co->fo_allowed = !alpha_enable && !logicop_enable;
97
98 /* independent_blend_enable not needed: only one rt supported */
99 /* XXX alpha_to_coverage / alpha_to_one? */
100 /* Set dither registers based on dither status. These registers set the
101 * dither pattern,
102 * for now, set the same values as the blob.
103 */
104 if (so->dither) {
105 co->PE_DITHER[0] = 0x6e4ca280;
106 co->PE_DITHER[1] = 0x5d7f91b3;
107 } else {
108 co->PE_DITHER[0] = 0xffffffff;
109 co->PE_DITHER[1] = 0xffffffff;
110 }
111
112 return co;
113 }
114
115 bool
116 etna_update_blend(struct etna_context *ctx)
117 {
118 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
119 struct pipe_blend_state *pblend = ctx->blend;
120 struct etna_blend_state *blend = etna_blend_state(pblend);
121 const struct pipe_rt_blend_state *rt0 = &pblend->rt[0];
122 const struct util_format_description *desc;
123 uint32_t colormask;
124
125 if (pfb->cbufs[0] &&
126 translate_pe_format_rb_swap(pfb->cbufs[0]->format)) {
127 colormask = rt0->colormask & (PIPE_MASK_A | PIPE_MASK_G);
128 if (rt0->colormask & PIPE_MASK_R)
129 colormask |= PIPE_MASK_B;
130 if (rt0->colormask & PIPE_MASK_B)
131 colormask |= PIPE_MASK_R;
132 } else {
133 colormask = rt0->colormask;
134 }
135
136 /* If the complete render target is written, set full_overwrite:
137 * - The color mask covers all channels of the render target
138 * - No blending or logicop is used
139 */
140 if (pfb->cbufs[0])
141 desc = util_format_description(pfb->cbufs[0]->format);
142 bool full_overwrite = !pfb->cbufs[0] || ((blend->fo_allowed &&
143 util_format_colormask_full(desc, colormask)));
144 blend->PE_COLOR_FORMAT =
145 VIVS_PE_COLOR_FORMAT_COMPONENTS(colormask) |
146 COND(full_overwrite, VIVS_PE_COLOR_FORMAT_OVERWRITE);
147
148 return true;
149 }
150
151 void
152 etna_set_blend_color(struct pipe_context *pctx, const struct pipe_blend_color *bc)
153 {
154 struct etna_context *ctx = etna_context(pctx);
155 struct compiled_blend_color *cs = &ctx->blend_color;
156
157 memcpy(cs->color, bc->color, sizeof(float) * 4);
158
159 ctx->dirty |= ETNA_DIRTY_BLEND_COLOR;
160 }
161
162 bool
163 etna_update_blend_color(struct etna_context *ctx)
164 {
165 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
166 struct compiled_blend_color *cs = &ctx->blend_color;
167 bool rb_swap = (pfb->cbufs[0] && translate_pe_format_rb_swap(pfb->cbufs[0]->format));
168
169 cs->PE_ALPHA_BLEND_COLOR =
170 VIVS_PE_ALPHA_BLEND_COLOR_R(etna_cfloat_to_uint8(cs->color[rb_swap ? 2 : 0])) |
171 VIVS_PE_ALPHA_BLEND_COLOR_G(etna_cfloat_to_uint8(cs->color[1])) |
172 VIVS_PE_ALPHA_BLEND_COLOR_B(etna_cfloat_to_uint8(cs->color[rb_swap ? 0 : 2])) |
173 VIVS_PE_ALPHA_BLEND_COLOR_A(etna_cfloat_to_uint8(cs->color[3]));
174
175 cs->PE_ALPHA_COLOR_EXT0 =
176 VIVS_PE_ALPHA_COLOR_EXT0_B(util_float_to_half(cs->color[rb_swap ? 2 : 0])) |
177 VIVS_PE_ALPHA_COLOR_EXT0_G(util_float_to_half(cs->color[1]));
178 cs->PE_ALPHA_COLOR_EXT1 =
179 VIVS_PE_ALPHA_COLOR_EXT1_R(util_float_to_half(cs->color[rb_swap ? 0 : 2])) |
180 VIVS_PE_ALPHA_COLOR_EXT1_A(util_float_to_half(cs->color[3]));
181
182 return true;
183 }