Redo the cso cache to map driver data in a lot more pleasing way.
[mesa.git] / src / mesa / state_tracker / st_atom_blend.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34
35 #include "st_context.h"
36 #include "st_cache.h"
37 #include "st_atom.h"
38
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41
42
43 /**
44 * Convert GLenum blend tokens to pipe tokens.
45 * Both blend factors and blend funcs are accepted.
46 */
47 static GLuint
48 gl_blend_to_sp(GLenum blend)
49 {
50 switch (blend) {
51 /* blend functions */
52 case GL_FUNC_ADD:
53 return PIPE_BLEND_ADD;
54 case GL_FUNC_SUBTRACT:
55 return PIPE_BLEND_SUBTRACT;
56 case GL_FUNC_REVERSE_SUBTRACT:
57 return PIPE_BLEND_REVERSE_SUBTRACT;
58 case GL_MIN:
59 return PIPE_BLEND_MIN;
60 case GL_MAX:
61 return PIPE_BLEND_MAX;
62
63 /* blend factors */
64 case GL_ONE:
65 return PIPE_BLENDFACTOR_ONE;
66 case GL_SRC_COLOR:
67 return PIPE_BLENDFACTOR_SRC_COLOR;
68 case GL_SRC_ALPHA:
69 return PIPE_BLENDFACTOR_SRC_ALPHA;
70 case GL_DST_ALPHA:
71 return PIPE_BLENDFACTOR_DST_ALPHA;
72 case GL_DST_COLOR:
73 return PIPE_BLENDFACTOR_DST_COLOR;
74 case GL_SRC_ALPHA_SATURATE:
75 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
76 case GL_CONSTANT_COLOR:
77 return PIPE_BLENDFACTOR_CONST_COLOR;
78 case GL_CONSTANT_ALPHA:
79 return PIPE_BLENDFACTOR_CONST_ALPHA;
80 /*
81 return PIPE_BLENDFACTOR_SRC1_COLOR;
82 return PIPE_BLENDFACTOR_SRC1_ALPHA;
83 */
84 case GL_ZERO:
85 return PIPE_BLENDFACTOR_ZERO;
86 case GL_ONE_MINUS_SRC_COLOR:
87 return PIPE_BLENDFACTOR_INV_SRC_COLOR;
88 case GL_ONE_MINUS_SRC_ALPHA:
89 return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
90 case GL_ONE_MINUS_DST_COLOR:
91 return PIPE_BLENDFACTOR_INV_DST_ALPHA;
92 case GL_ONE_MINUS_DST_ALPHA:
93 return PIPE_BLENDFACTOR_INV_DST_COLOR;
94 case GL_ONE_MINUS_CONSTANT_COLOR:
95 return PIPE_BLENDFACTOR_INV_CONST_COLOR;
96 case GL_ONE_MINUS_CONSTANT_ALPHA:
97 return PIPE_BLENDFACTOR_INV_CONST_ALPHA;
98 /*
99 return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
100 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA;
101 */
102 default:
103 assert("invalid GL token in gl_blend_to_sp()" == NULL);
104 return 0;
105 }
106 }
107
108
109 /**
110 * Convert GLenum logicop tokens to pipe tokens.
111 */
112 static GLuint
113 gl_logicop_to_sp(GLenum logicop)
114 {
115 switch (logicop) {
116 case GL_CLEAR:
117 return PIPE_LOGICOP_CLEAR;
118 case GL_NOR:
119 return PIPE_LOGICOP_NOR;
120 case GL_AND_INVERTED:
121 return PIPE_LOGICOP_AND_INVERTED;
122 case GL_COPY_INVERTED:
123 return PIPE_LOGICOP_COPY_INVERTED;
124 case GL_AND_REVERSE:
125 return PIPE_LOGICOP_AND_REVERSE;
126 case GL_INVERT:
127 return PIPE_LOGICOP_INVERT;
128 case GL_XOR:
129 return PIPE_LOGICOP_XOR;
130 case GL_NAND:
131 return PIPE_LOGICOP_NAND;
132 case GL_AND:
133 return PIPE_LOGICOP_AND;
134 case GL_EQUIV:
135 return PIPE_LOGICOP_EQUIV;
136 case GL_NOOP:
137 return PIPE_LOGICOP_NOOP;
138 case GL_OR_INVERTED:
139 return PIPE_LOGICOP_OR_INVERTED;
140 case GL_COPY:
141 return PIPE_LOGICOP_COPY;
142 case GL_OR_REVERSE:
143 return PIPE_LOGICOP_OR_REVERSE;
144 case GL_OR:
145 return PIPE_LOGICOP_OR;
146 case GL_SET:
147 return PIPE_LOGICOP_SET;
148 default:
149 assert("invalid GL token in gl_logicop_to_sp()" == NULL);
150 return 0;
151 }
152 }
153
154
155 static void
156 update_blend( struct st_context *st )
157 {
158 struct pipe_blend_state blend;
159
160 memset(&blend, 0, sizeof(blend));
161
162 if (st->ctx->Color.ColorLogicOpEnabled ||
163 (st->ctx->Color.BlendEnabled &&
164 st->ctx->Color.BlendEquationRGB == GL_LOGIC_OP)) {
165 /* logicop enabled */
166 blend.logicop_enable = 1;
167 blend.logicop_func = gl_logicop_to_sp(st->ctx->Color.LogicOp);
168 }
169 else if (st->ctx->Color.BlendEnabled) {
170 /* blending enabled */
171 blend.blend_enable = 1;
172
173 blend.rgb_func = gl_blend_to_sp(st->ctx->Color.BlendEquationRGB);
174 if (st->ctx->Color.BlendEquationRGB == GL_MIN ||
175 st->ctx->Color.BlendEquationRGB == GL_MAX) {
176 /* Min/max are special */
177 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
178 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
179 }
180 else {
181 blend.rgb_src_factor = gl_blend_to_sp(st->ctx->Color.BlendSrcRGB);
182 blend.rgb_dst_factor = gl_blend_to_sp(st->ctx->Color.BlendDstRGB);
183 }
184
185 blend.alpha_func = gl_blend_to_sp(st->ctx->Color.BlendEquationA);
186 if (st->ctx->Color.BlendEquationA == GL_MIN ||
187 st->ctx->Color.BlendEquationA == GL_MAX) {
188 /* Min/max are special */
189 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
190 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
191 }
192 else {
193 blend.alpha_src_factor = gl_blend_to_sp(st->ctx->Color.BlendSrcA);
194 blend.alpha_dst_factor = gl_blend_to_sp(st->ctx->Color.BlendDstA);
195 }
196 }
197 else {
198 /* no blending / logicop */
199 }
200
201 /* Colormask - maybe reverse these bits? */
202 if (st->ctx->Color.ColorMask[0])
203 blend.colormask |= PIPE_MASK_R;
204 if (st->ctx->Color.ColorMask[1])
205 blend.colormask |= PIPE_MASK_G;
206 if (st->ctx->Color.ColorMask[2])
207 blend.colormask |= PIPE_MASK_B;
208 if (st->ctx->Color.ColorMask[3])
209 blend.colormask |= PIPE_MASK_A;
210
211 if (st->ctx->Color.DitherFlag)
212 blend.dither = 1;
213
214 const struct cso_blend *cso =
215 st_cached_blend_state(st, &blend);
216
217 if (st->state.blend != cso) {
218 /* state has changed */
219 st->state.blend = cso;
220 /* bind new state */
221 st->pipe->bind_blend_state(st->pipe, cso->data);
222 }
223
224 if (memcmp(st->ctx->Color.BlendColor, &st->state.blend_color, 4 * sizeof(GLfloat)) != 0) {
225 /* state has changed */
226 st->state.blend_color.color[0] = st->ctx->Color.BlendColor[0];
227 st->state.blend_color.color[1] = st->ctx->Color.BlendColor[1];
228 st->state.blend_color.color[2] = st->ctx->Color.BlendColor[2];
229 st->state.blend_color.color[3] = st->ctx->Color.BlendColor[3];
230 st->pipe->set_blend_color(st->pipe, (struct pipe_blend_color *) st->ctx->Color.BlendColor);
231 }
232 }
233
234
235 const struct st_tracked_state st_update_blend = {
236 .name = "st_update_blend",
237 .dirty = {
238 .mesa = (_NEW_COLOR), /* XXX _NEW_BLEND someday? */
239 .st = 0,
240 },
241 .update = update_blend
242 };
243
244
245
246
247