svga: refactor occlusion query code
[mesa.git] / src / gallium / drivers / freedreno / freedreno_blend.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32
33 #include "freedreno_blend.h"
34 #include "freedreno_context.h"
35 #include "freedreno_util.h"
36
37 static enum rb_blend_op
38 blend_factor(unsigned factor)
39 {
40 switch (factor) {
41 case PIPE_BLENDFACTOR_ONE:
42 return RB_BLEND_ONE;
43 case PIPE_BLENDFACTOR_SRC_COLOR:
44 return RB_BLEND_SRC_COLOR;
45 case PIPE_BLENDFACTOR_SRC_ALPHA:
46 return RB_BLEND_SRC_ALPHA;
47 case PIPE_BLENDFACTOR_DST_ALPHA:
48 return RB_BLEND_DST_ALPHA;
49 case PIPE_BLENDFACTOR_DST_COLOR:
50 return RB_BLEND_DST_COLOR;
51 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
52 return RB_BLEND_SRC_ALPHA_SATURATE;
53 case PIPE_BLENDFACTOR_CONST_COLOR:
54 return RB_BLEND_CONSTANT_COLOR;
55 case PIPE_BLENDFACTOR_CONST_ALPHA:
56 return RB_BLEND_CONSTANT_ALPHA;
57 case PIPE_BLENDFACTOR_ZERO:
58 case 0:
59 return RB_BLEND_ZERO;
60 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
61 return RB_BLEND_ONE_MINUS_SRC_COLOR;
62 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
63 return RB_BLEND_ONE_MINUS_SRC_ALPHA;
64 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
65 return RB_BLEND_ONE_MINUS_DST_ALPHA;
66 case PIPE_BLENDFACTOR_INV_DST_COLOR:
67 return RB_BLEND_ONE_MINUS_DST_COLOR;
68 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
69 return RB_BLEND_ONE_MINUS_CONSTANT_COLOR;
70 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
71 return RB_BLEND_ONE_MINUS_CONSTANT_ALPHA;
72 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
73 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
74 case PIPE_BLENDFACTOR_SRC1_COLOR:
75 case PIPE_BLENDFACTOR_SRC1_ALPHA:
76 /* I don't think these are supported */
77 default:
78 DBG("invalid blend factor: %x", factor);
79 return 0;
80 }
81 }
82
83 static enum rb_comb_func
84 blend_func(unsigned func)
85 {
86 switch (func) {
87 case PIPE_BLEND_ADD:
88 return COMB_DST_PLUS_SRC;
89 case PIPE_BLEND_MIN:
90 return COMB_MIN_DST_SRC;
91 case PIPE_BLEND_MAX:
92 return COMB_MAX_DST_SRC;
93 case PIPE_BLEND_SUBTRACT:
94 return COMB_SRC_MINUS_DST;
95 case PIPE_BLEND_REVERSE_SUBTRACT:
96 return COMB_DST_MINUS_SRC;
97 default:
98 DBG("invalid blend func: %x", func);
99 return 0;
100 }
101 }
102
103 static void *
104 fd_blend_state_create(struct pipe_context *pctx,
105 const struct pipe_blend_state *cso)
106 {
107 const struct pipe_rt_blend_state *rt = &cso->rt[0];
108 struct fd_blend_stateobj *so;
109
110 if (cso->logicop_enable) {
111 DBG("Unsupported! logicop");
112 return NULL;
113 }
114
115 if (cso->independent_blend_enable) {
116 DBG("Unsupported! independent blend state");
117 return NULL;
118 }
119
120 so = CALLOC_STRUCT(fd_blend_stateobj);
121 if (!so)
122 return NULL;
123
124 so->base = *cso;
125
126 so->rb_colorcontrol = RB_COLORCONTROL_ROP_CODE(12);
127
128 so->rb_blendcontrol =
129 RB_BLENDCONTROL_COLOR_SRCBLEND(blend_factor(rt->rgb_src_factor)) |
130 RB_BLENDCONTROL_COLOR_COMB_FCN(blend_func(rt->rgb_func)) |
131 RB_BLENDCONTROL_COLOR_DESTBLEND(blend_factor(rt->rgb_dst_factor)) |
132 RB_BLENDCONTROL_ALPHA_SRCBLEND(blend_factor(rt->alpha_src_factor)) |
133 RB_BLENDCONTROL_ALPHA_COMB_FCN(blend_func(rt->alpha_func)) |
134 RB_BLENDCONTROL_ALPHA_DESTBLEND(blend_factor(rt->alpha_dst_factor));
135
136 if (rt->colormask & PIPE_MASK_R)
137 so->rb_colormask |= RB_COLOR_MASK_WRITE_RED;
138 if (rt->colormask & PIPE_MASK_G)
139 so->rb_colormask |= RB_COLOR_MASK_WRITE_GREEN;
140 if (rt->colormask & PIPE_MASK_B)
141 so->rb_colormask |= RB_COLOR_MASK_WRITE_BLUE;
142 if (rt->colormask & PIPE_MASK_A)
143 so->rb_colormask |= RB_COLOR_MASK_WRITE_ALPHA;
144
145 if (!rt->blend_enable)
146 so->rb_colorcontrol |= RB_COLORCONTROL_BLEND_DISABLE;
147
148 if (cso->dither)
149 so->rb_colorcontrol |= RB_COLORCONTROL_DITHER_MODE(DITHER_ALWAYS);
150
151 return so;
152 }
153
154 static void
155 fd_blend_state_bind(struct pipe_context *pctx, void *hwcso)
156 {
157 struct fd_context *ctx = fd_context(pctx);
158 ctx->blend = hwcso;
159 ctx->dirty |= FD_DIRTY_BLEND;
160 }
161
162 static void
163 fd_blend_state_delete(struct pipe_context *pctx, void *hwcso)
164 {
165 FREE(hwcso);
166 }
167
168 void
169 fd_blend_init(struct pipe_context *pctx)
170 {
171 pctx->create_blend_state = fd_blend_state_create;
172 pctx->bind_blend_state = fd_blend_state_bind;
173 pctx->delete_blend_state = fd_blend_state_delete;
174 }
175