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