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 "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "util/half_float.h"
35
36 void *
etna_blend_state_create(struct pipe_context * pctx,const struct pipe_blend_state * so)37 etna_blend_state_create(struct pipe_context *pctx,
38 const struct pipe_blend_state *so)
39 {
40 struct etna_context *ctx = etna_context(pctx);
41 struct etna_blend_state *co = CALLOC_STRUCT(etna_blend_state);
42 bool logicop_enable;
43
44 /* pipe_blend_func happens to match the hardware. */
45 STATIC_ASSERT(PIPE_BLEND_ADD == BLEND_EQ_ADD);
46 STATIC_ASSERT(PIPE_BLEND_SUBTRACT == BLEND_EQ_SUBTRACT);
47 STATIC_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLEND_EQ_REVERSE_SUBTRACT);
48 STATIC_ASSERT(PIPE_BLEND_MIN == BLEND_EQ_MIN);
49 STATIC_ASSERT(PIPE_BLEND_MAX == BLEND_EQ_MAX);
50
51 if (!co)
52 return NULL;
53
54 co->base = *so;
55
56 logicop_enable = so->logicop_enable &&
57 VIV_FEATURE(ctx->screen, ETNA_FEATURE_LOGIC_OP);
58
59 for (unsigned int i = 0; i < so->max_rt + 1; i++) {
60 const struct pipe_rt_blend_state *rt;
61
62 if (so->independent_blend_enable)
63 rt = &so->rt[i];
64 else
65 rt = &so->rt[0];
66
67 /* Enable blending if
68 * - blend enabled in blend state
69 * - NOT source factor is ONE and destination factor ZERO and eq is ADD for
70 * both rgb and alpha (which mean that blending is effectively disabled)
71 */
72 co->rt[i].alpha_enable = rt->blend_enable &&
73 !(rt->rgb_src_factor == PIPE_BLENDFACTOR_ONE &&
74 rt->rgb_dst_factor == PIPE_BLENDFACTOR_ZERO &&
75 rt->rgb_func == PIPE_BLEND_ADD &&
76 rt->alpha_src_factor == PIPE_BLENDFACTOR_ONE &&
77 rt->alpha_dst_factor == PIPE_BLENDFACTOR_ZERO &&
78 rt->alpha_func == PIPE_BLEND_ADD);
79
80 /* Enable separate alpha if
81 * - Blending enabled (see above)
82 * - NOT source/destination factor and eq is same for both rgb and alpha
83 * (which would effectively that mean alpha is not separate), and
84 */
85 co->rt[i].separate_alpha = co->rt[i].alpha_enable &&
86 !(rt->rgb_src_factor == rt->alpha_src_factor &&
87 rt->rgb_dst_factor == rt->alpha_dst_factor &&
88 rt->rgb_func == rt->alpha_func);
89
90 co->rt[i].fo_allowed = !co->rt[i].alpha_enable && !logicop_enable;
91 }
92
93 co->PE_LOGIC_OP =
94 VIVS_PE_LOGIC_OP_OP(logicop_enable ? so->logicop_func : LOGIC_OP_COPY) |
95 VIVS_PE_LOGIC_OP_DITHER_MODE(3) | /* TODO: related to dithering, sometimes 2 */
96 0x000E4000 /* ??? */;
97
98 /* XXX alpha_to_coverage / alpha_to_one? */
99 /* Set dither registers based on dither status. These registers set the
100 * dither pattern,
101 * for now, set the same values as the blob.
102 */
103 if (so->dither &&
104 (!co->rt[0].alpha_enable ||
105 VIV_FEATURE(ctx->screen, ETNA_FEATURE_PE_DITHER_FIX))) {
106 co->PE_DITHER[0] = 0x6e4ca280;
107 co->PE_DITHER[1] = 0x5d7f91b3;
108 } else {
109 co->PE_DITHER[0] = 0xffffffff;
110 co->PE_DITHER[1] = 0xffffffff;
111 }
112
113 return co;
114 }
115
116 bool
etna_update_blend(struct etna_context * ctx)117 etna_update_blend(struct etna_context *ctx)
118 {
119 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
120 struct pipe_blend_state *pblend = ctx->blend;
121 struct etna_blend_state *blend = etna_blend_state(pblend);
122 unsigned current_rt = 0;
123
124 for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
125 if (!pfb->cbufs[i])
126 continue;
127
128 const struct pipe_rt_blend_state *rt;
129 uint32_t colormask;
130
131 if (pblend->independent_blend_enable)
132 rt = &pblend->rt[i];
133 else
134 rt = &pblend->rt[0];
135
136 if (translate_pe_format_rb_swap(pfb->cbufs[i]->format)) {
137 colormask = rt->colormask & (PIPE_MASK_A | PIPE_MASK_G);
138 if (rt->colormask & PIPE_MASK_R)
139 colormask |= PIPE_MASK_B;
140 if (rt->colormask & PIPE_MASK_B)
141 colormask |= PIPE_MASK_R;
142 } else {
143 colormask = rt->colormask;
144 }
145
146 /* If the complete render target is written, set full_overwrite:
147 * - The color mask covers all channels of the render target
148 * - No blending or logicop is used
149 */
150 const struct util_format_description *desc = util_format_description(pfb->cbufs[i]->format);
151 bool full_overwrite = (blend->rt[i].fo_allowed &&
152 util_format_colormask_full(desc, colormask));
153
154 if (current_rt == 0) {
155 blend->rt[0].PE_COLOR_FORMAT =
156 VIVS_PE_COLOR_FORMAT_COMPONENTS(colormask) |
157 COND(full_overwrite, VIVS_PE_COLOR_FORMAT_OVERWRITE);
158 } else {
159 blend->rt[current_rt].PE_HALTI5_COLORMASK =
160 VIVS_PE_HALTI5_RT_COLORMASK_COMPONENTS(colormask) |
161 COND(full_overwrite, VIVS_PE_HALTI5_RT_COLORMASK_OVERWRITE);
162 }
163
164 if (blend->rt[i].alpha_enable) {
165 blend->rt[current_rt].PE_ALPHA_CONFIG =
166 VIVS_PE_ALPHA_CONFIG_BLEND_ENABLE_COLOR |
167 COND(blend->rt[i].separate_alpha, VIVS_PE_ALPHA_CONFIG_BLEND_SEPARATE_ALPHA) |
168 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_COLOR(translate_blend_factor(rt->rgb_src_factor)) |
169 VIVS_PE_ALPHA_CONFIG_SRC_FUNC_ALPHA(translate_blend_factor(rt->alpha_src_factor)) |
170 VIVS_PE_ALPHA_CONFIG_DST_FUNC_COLOR(translate_blend_factor(rt->rgb_dst_factor)) |
171 VIVS_PE_ALPHA_CONFIG_DST_FUNC_ALPHA(translate_blend_factor(rt->alpha_dst_factor)) |
172 VIVS_PE_ALPHA_CONFIG_EQ_COLOR(rt->rgb_func) |
173 VIVS_PE_ALPHA_CONFIG_EQ_ALPHA(rt->alpha_func);
174 } else {
175 blend->rt[current_rt].PE_ALPHA_CONFIG = 0;
176 }
177
178 current_rt++;
179 }
180
181 if (current_rt == 0)
182 blend->rt[0].PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_OVERWRITE;
183
184 return true;
185 }
186
187 void
etna_set_blend_color(struct pipe_context * pctx,const struct pipe_blend_color * bc)188 etna_set_blend_color(struct pipe_context *pctx, const struct pipe_blend_color *bc)
189 {
190 struct etna_context *ctx = etna_context(pctx);
191 struct compiled_blend_color *cs = &ctx->blend_color;
192
193 memcpy(cs->color, bc->color, sizeof(float) * 4);
194
195 ctx->dirty |= ETNA_DIRTY_BLEND_COLOR;
196 }
197
198 bool
etna_update_blend_color(struct etna_context * ctx)199 etna_update_blend_color(struct etna_context *ctx)
200 {
201 struct pipe_framebuffer_state *fb = &ctx->framebuffer_s;
202 struct compiled_blend_color *cs = &ctx->blend_color;
203 unsigned rt = 0;
204
205 for (unsigned i = 0; i < fb->nr_cbufs; i++) {
206 if (!fb->cbufs[i])
207 continue;
208
209 bool rb_swap = translate_pe_format_rb_swap(fb->cbufs[i]->format);
210
211 if (rt == 0) {
212 cs->PE_ALPHA_BLEND_COLOR =
213 VIVS_PE_ALPHA_BLEND_COLOR_R(float_to_ubyte(cs->color[rb_swap ? 2 : 0])) |
214 VIVS_PE_ALPHA_BLEND_COLOR_G(float_to_ubyte(cs->color[1])) |
215 VIVS_PE_ALPHA_BLEND_COLOR_B(float_to_ubyte(cs->color[rb_swap ? 0 : 2])) |
216 VIVS_PE_ALPHA_BLEND_COLOR_A(float_to_ubyte(cs->color[3]));
217 }
218
219 cs->rt[rt].PE_ALPHA_COLOR_EXT0 =
220 VIVS_PE_ALPHA_COLOR_EXT0_B(_mesa_float_to_half(cs->color[rb_swap ? 2 : 0])) |
221 VIVS_PE_ALPHA_COLOR_EXT0_G(_mesa_float_to_half(cs->color[1]));
222 cs->rt[rt].PE_ALPHA_COLOR_EXT1 =
223 VIVS_PE_ALPHA_COLOR_EXT1_R(_mesa_float_to_half(cs->color[rb_swap ? 0 : 2])) |
224 VIVS_PE_ALPHA_COLOR_EXT1_A(_mesa_float_to_half(cs->color[3]));
225
226 rt++;
227 }
228
229 return true;
230 }
231