xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/a2xx/fd2_blend.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012-2013 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "pipe/p_state.h"
10 #include "util/u_blend.h"
11 #include "util/u_memory.h"
12 #include "util/u_string.h"
13 
14 #include "fd2_blend.h"
15 #include "fd2_context.h"
16 #include "fd2_util.h"
17 
18 static enum a2xx_rb_blend_opcode
blend_func(unsigned func)19 blend_func(unsigned func)
20 {
21    switch (func) {
22    case PIPE_BLEND_ADD:
23       return BLEND2_DST_PLUS_SRC;
24    case PIPE_BLEND_MIN:
25       return BLEND2_MIN_DST_SRC;
26    case PIPE_BLEND_MAX:
27       return BLEND2_MAX_DST_SRC;
28    case PIPE_BLEND_SUBTRACT:
29       return BLEND2_SRC_MINUS_DST;
30    case PIPE_BLEND_REVERSE_SUBTRACT:
31       return BLEND2_DST_MINUS_SRC;
32    default:
33       DBG("invalid blend func: %x", func);
34       return 0;
35    }
36 }
37 
38 void *
fd2_blend_state_create(struct pipe_context * pctx,const struct pipe_blend_state * cso)39 fd2_blend_state_create(struct pipe_context *pctx,
40                        const struct pipe_blend_state *cso)
41 {
42    const struct pipe_rt_blend_state *rt = &cso->rt[0];
43    struct fd2_blend_stateobj *so;
44    unsigned rop = PIPE_LOGICOP_COPY;
45 
46    if (cso->logicop_enable)
47       rop = cso->logicop_func; /* 1:1 mapping with hw */
48 
49    if (cso->independent_blend_enable) {
50       DBG("Unsupported! independent blend state");
51       return NULL;
52    }
53 
54    so = CALLOC_STRUCT(fd2_blend_stateobj);
55    if (!so)
56       return NULL;
57 
58    so->base = *cso;
59 
60    so->rb_colorcontrol = A2XX_RB_COLORCONTROL_ROP_CODE(rop);
61 
62    so->rb_blendcontrol =
63       A2XX_RB_BLEND_CONTROL_COLOR_SRCBLEND(
64          fd_blend_factor(rt->rgb_src_factor)) |
65       A2XX_RB_BLEND_CONTROL_COLOR_COMB_FCN(blend_func(rt->rgb_func)) |
66       A2XX_RB_BLEND_CONTROL_COLOR_DESTBLEND(
67          fd_blend_factor(rt->rgb_dst_factor));
68 
69    /* hardware doesn't support SRC_ALPHA_SATURATE for alpha, but it is
70     * equivalent to ONE */
71    unsigned alpha_src_factor = rt->alpha_src_factor;
72    if (alpha_src_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
73       alpha_src_factor = PIPE_BLENDFACTOR_ONE;
74 
75    so->rb_blendcontrol |=
76       A2XX_RB_BLEND_CONTROL_ALPHA_SRCBLEND(fd_blend_factor(alpha_src_factor)) |
77       A2XX_RB_BLEND_CONTROL_ALPHA_COMB_FCN(blend_func(rt->alpha_func)) |
78       A2XX_RB_BLEND_CONTROL_ALPHA_DESTBLEND(
79          fd_blend_factor(rt->alpha_dst_factor));
80 
81    if (rt->colormask & PIPE_MASK_R)
82       so->rb_colormask |= A2XX_RB_COLOR_MASK_WRITE_RED;
83    if (rt->colormask & PIPE_MASK_G)
84       so->rb_colormask |= A2XX_RB_COLOR_MASK_WRITE_GREEN;
85    if (rt->colormask & PIPE_MASK_B)
86       so->rb_colormask |= A2XX_RB_COLOR_MASK_WRITE_BLUE;
87    if (rt->colormask & PIPE_MASK_A)
88       so->rb_colormask |= A2XX_RB_COLOR_MASK_WRITE_ALPHA;
89 
90    if (!rt->blend_enable)
91       so->rb_colorcontrol |= A2XX_RB_COLORCONTROL_BLEND_DISABLE;
92 
93    if (cso->dither)
94       so->rb_colorcontrol |= A2XX_RB_COLORCONTROL_DITHER_MODE(DITHER_ALWAYS);
95 
96    return so;
97 }
98