• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3  * Copyright © 2018 Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Rob Clark <robclark@freedesktop.org>
26  */
27 
28 #define FD_BO_NO_HARDPIN 1
29 
30 #include "pipe/p_state.h"
31 #include "util/u_blend.h"
32 #include "util/u_dual_blend.h"
33 #include "util/u_memory.h"
34 #include "util/u_string.h"
35 
36 #include "fd6_blend.h"
37 #include "fd6_context.h"
38 #include "fd6_pack.h"
39 
40 // XXX move somewhere common.. same across a3xx/a4xx/a5xx..
41 static enum a3xx_rb_blend_opcode
blend_func(unsigned func)42 blend_func(unsigned func)
43 {
44    switch (func) {
45    case PIPE_BLEND_ADD:
46       return BLEND_DST_PLUS_SRC;
47    case PIPE_BLEND_MIN:
48       return BLEND_MIN_DST_SRC;
49    case PIPE_BLEND_MAX:
50       return BLEND_MAX_DST_SRC;
51    case PIPE_BLEND_SUBTRACT:
52       return BLEND_SRC_MINUS_DST;
53    case PIPE_BLEND_REVERSE_SUBTRACT:
54       return BLEND_DST_MINUS_SRC;
55    default:
56       DBG("invalid blend func: %x", func);
57       return (enum a3xx_rb_blend_opcode)0;
58    }
59 }
60 
61 struct fd6_blend_variant *
__fd6_setup_blend_variant(struct fd6_blend_stateobj * blend,unsigned sample_mask)62 __fd6_setup_blend_variant(struct fd6_blend_stateobj *blend,
63                           unsigned sample_mask)
64 {
65    const struct pipe_blend_state *cso = &blend->base;
66    struct fd6_blend_variant *so;
67    enum a3xx_rop_code rop = ROP_COPY;
68    bool reads_dest = false;
69    unsigned mrt_blend = 0;
70 
71    if (cso->logicop_enable) {
72       rop = (enum a3xx_rop_code)cso->logicop_func; /* maps 1:1 */
73       reads_dest = util_logicop_reads_dest((enum pipe_logicop)cso->logicop_func);
74    }
75 
76    so = (struct fd6_blend_variant *)rzalloc_size(blend, sizeof(*so));
77    if (!so)
78       return NULL;
79 
80    struct fd_ringbuffer *ring = fd_ringbuffer_new_object(
81       blend->ctx->pipe, ((A6XX_MAX_RENDER_TARGETS * 4) + 6) * 4);
82    so->stateobj = ring;
83 
84    for (unsigned i = 0; i <= cso->max_rt; i++) {
85       const struct pipe_rt_blend_state *rt;
86 
87       if (cso->independent_blend_enable)
88          rt = &cso->rt[i];
89       else
90          rt = &cso->rt[0];
91 
92       OUT_REG(ring,
93               A6XX_RB_MRT_BLEND_CONTROL(
94                  i, .rgb_src_factor = fd_blend_factor(rt->rgb_src_factor),
95                  .rgb_blend_opcode = blend_func(rt->rgb_func),
96                  .rgb_dest_factor = fd_blend_factor(rt->rgb_dst_factor),
97                  .alpha_src_factor = fd_blend_factor(rt->alpha_src_factor),
98                  .alpha_blend_opcode = blend_func(rt->alpha_func),
99                  .alpha_dest_factor = fd_blend_factor(rt->alpha_dst_factor), ));
100 
101       OUT_REG(ring,
102               A6XX_RB_MRT_CONTROL(
103                     i,
104                     .blend = rt->blend_enable,
105                     .blend2 = rt->blend_enable,
106                     .rop_enable = cso->logicop_enable,
107                     .rop_code = rop,
108                     .component_enable = rt->colormask,
109               )
110       );
111 
112       if (rt->blend_enable) {
113          mrt_blend |= (1 << i);
114       }
115 
116       if (reads_dest) {
117          mrt_blend |= (1 << i);
118       }
119    }
120 
121    OUT_REG(
122       ring,
123       A6XX_RB_DITHER_CNTL(
124             .dither_mode_mrt0 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
125             .dither_mode_mrt1 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
126             .dither_mode_mrt2 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
127             .dither_mode_mrt3 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
128             .dither_mode_mrt4 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
129             .dither_mode_mrt5 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
130             .dither_mode_mrt6 = cso->dither ? DITHER_ALWAYS : DITHER_DISABLE,
131             .dither_mode_mrt7 =
132                cso->dither ? DITHER_ALWAYS : DITHER_DISABLE, ));
133 
134    OUT_REG(ring,
135            A6XX_SP_BLEND_CNTL(
136                  .enable_blend = mrt_blend,
137                  .unk8 = true,
138                  .dual_color_in_enable = blend->use_dual_src_blend,
139                  .alpha_to_coverage = cso->alpha_to_coverage,
140            ),
141    );
142 
143    OUT_REG(ring,
144            A6XX_RB_BLEND_CNTL(
145                  .enable_blend = mrt_blend,
146                  .independent_blend = cso->independent_blend_enable,
147                  .dual_color_in_enable = blend->use_dual_src_blend,
148                  .alpha_to_coverage = cso->alpha_to_coverage,
149                  .alpha_to_one = cso->alpha_to_one,
150                  .sample_mask = sample_mask,
151            ),
152    );
153 
154    so->sample_mask = sample_mask;
155 
156    util_dynarray_append(&blend->variants, struct fd6_blend_variant *, so);
157 
158    return so;
159 }
160 
161 void *
fd6_blend_state_create(struct pipe_context * pctx,const struct pipe_blend_state * cso)162 fd6_blend_state_create(struct pipe_context *pctx,
163                        const struct pipe_blend_state *cso)
164 {
165    struct fd6_blend_stateobj *so;
166 
167    so = (struct fd6_blend_stateobj *)rzalloc_size(NULL, sizeof(*so));
168    if (!so)
169       return NULL;
170 
171    so->base = *cso;
172    so->ctx = fd_context(pctx);
173 
174    if (cso->logicop_enable) {
175       so->reads_dest |= util_logicop_reads_dest((enum pipe_logicop)cso->logicop_func);
176    }
177 
178    so->use_dual_src_blend =
179       cso->rt[0].blend_enable && util_blend_state_is_dual(cso, 0);
180 
181    STATIC_ASSERT((4 * PIPE_MAX_COLOR_BUFS) == (8 * sizeof(so->all_mrt_write_mask)));
182    so->all_mrt_write_mask = 0;
183 
184    for (unsigned i = 0; i <= cso->max_rt; i++) {
185       const struct pipe_rt_blend_state *rt =
186          &cso->rt[cso->independent_blend_enable ? i : 0];
187 
188       so->reads_dest |= rt->blend_enable;
189 
190       so->all_mrt_write_mask |= rt->colormask << (4 * i);
191    }
192 
193    util_dynarray_init(&so->variants, so);
194 
195    return so;
196 }
197 
198 void
fd6_blend_state_delete(struct pipe_context * pctx,void * hwcso)199 fd6_blend_state_delete(struct pipe_context *pctx, void *hwcso)
200 {
201    struct fd6_blend_stateobj *so = (struct fd6_blend_stateobj *)hwcso;
202 
203    util_dynarray_foreach (&so->variants, struct fd6_blend_variant *, vp) {
204       struct fd6_blend_variant *v = *vp;
205       fd_ringbuffer_del(v->stateobj);
206    }
207 
208    ralloc_free(so);
209 }
210