1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "st_context.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "cso_cache/cso_context.h"
42 #include "main/macros.h"
45 * Convert GLenum blend tokens to pipe tokens.
46 * Both blend factors and blend funcs are accepted.
49 translate_blend(GLenum blend
)
54 return PIPE_BLEND_ADD
;
55 case GL_FUNC_SUBTRACT
:
56 return PIPE_BLEND_SUBTRACT
;
57 case GL_FUNC_REVERSE_SUBTRACT
:
58 return PIPE_BLEND_REVERSE_SUBTRACT
;
60 return PIPE_BLEND_MIN
;
62 return PIPE_BLEND_MAX
;
66 return PIPE_BLENDFACTOR_ONE
;
68 return PIPE_BLENDFACTOR_SRC_COLOR
;
70 return PIPE_BLENDFACTOR_SRC_ALPHA
;
72 return PIPE_BLENDFACTOR_DST_ALPHA
;
74 return PIPE_BLENDFACTOR_DST_COLOR
;
75 case GL_SRC_ALPHA_SATURATE
:
76 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
;
77 case GL_CONSTANT_COLOR
:
78 return PIPE_BLENDFACTOR_CONST_COLOR
;
79 case GL_CONSTANT_ALPHA
:
80 return PIPE_BLENDFACTOR_CONST_ALPHA
;
82 return PIPE_BLENDFACTOR_SRC1_COLOR;
83 return PIPE_BLENDFACTOR_SRC1_ALPHA;
86 return PIPE_BLENDFACTOR_ZERO
;
87 case GL_ONE_MINUS_SRC_COLOR
:
88 return PIPE_BLENDFACTOR_INV_SRC_COLOR
;
89 case GL_ONE_MINUS_SRC_ALPHA
:
90 return PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
91 case GL_ONE_MINUS_DST_COLOR
:
92 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
93 case GL_ONE_MINUS_DST_ALPHA
:
94 return PIPE_BLENDFACTOR_INV_DST_ALPHA
;
95 case GL_ONE_MINUS_CONSTANT_COLOR
:
96 return PIPE_BLENDFACTOR_INV_CONST_COLOR
;
97 case GL_ONE_MINUS_CONSTANT_ALPHA
:
98 return PIPE_BLENDFACTOR_INV_CONST_ALPHA
;
100 return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
101 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA;
104 assert("invalid GL token in translate_blend()" == NULL
);
111 * Convert GLenum logicop tokens to pipe tokens.
114 translate_logicop(GLenum logicop
)
118 return PIPE_LOGICOP_CLEAR
;
120 return PIPE_LOGICOP_NOR
;
121 case GL_AND_INVERTED
:
122 return PIPE_LOGICOP_AND_INVERTED
;
123 case GL_COPY_INVERTED
:
124 return PIPE_LOGICOP_COPY_INVERTED
;
126 return PIPE_LOGICOP_AND_REVERSE
;
128 return PIPE_LOGICOP_INVERT
;
130 return PIPE_LOGICOP_XOR
;
132 return PIPE_LOGICOP_NAND
;
134 return PIPE_LOGICOP_AND
;
136 return PIPE_LOGICOP_EQUIV
;
138 return PIPE_LOGICOP_NOOP
;
140 return PIPE_LOGICOP_OR_INVERTED
;
142 return PIPE_LOGICOP_COPY
;
144 return PIPE_LOGICOP_OR_REVERSE
;
146 return PIPE_LOGICOP_OR
;
148 return PIPE_LOGICOP_SET
;
150 assert("invalid GL token in translate_logicop()" == NULL
);
156 * Figure out if colormasks are different per rt.
159 colormask_per_rt(GLcontext
*ctx
)
161 /* a bit suboptimal have to compare lots of values */
163 for (i
= 1; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
164 if (memcmp(ctx
->Color
.ColorMask
[0], ctx
->Color
.ColorMask
[i
], 4)) {
172 * Figure out if blend enables are different per rt.
175 blend_per_rt(GLcontext
*ctx
)
177 if (ctx
->Color
.BlendEnabled
&&
178 (ctx
->Color
.BlendEnabled
!= ((1 << ctx
->Const
.MaxDrawBuffers
) - 1))) {
185 update_blend( struct st_context
*st
)
187 struct pipe_blend_state
*blend
= &st
->state
.blend
;
188 unsigned num_state
= 1;
191 memset(blend
, 0, sizeof(*blend
));
193 if (blend_per_rt(st
->ctx
) || colormask_per_rt(st
->ctx
)) {
194 num_state
= st
->ctx
->Const
.MaxDrawBuffers
;
195 blend
->independent_blend_enable
= 1;
197 /* Note it is impossible to correctly deal with EXT_blend_logic_op and
198 EXT_draw_buffers2/EXT_blend_equation_separate at the same time.
199 These combinations would require support for per-rt logicop enables
200 and separate alpha/rgb logicop/blend support respectively. Neither
201 possible in gallium nor most hardware. Assume these combinations
203 if (st
->ctx
->Color
.ColorLogicOpEnabled
||
204 (st
->ctx
->Color
.BlendEnabled
&&
205 st
->ctx
->Color
.BlendEquationRGB
== GL_LOGIC_OP
)) {
206 /* logicop enabled */
207 blend
->logicop_enable
= 1;
208 blend
->logicop_func
= translate_logicop(st
->ctx
->Color
.LogicOp
);
210 else if (st
->ctx
->Color
.BlendEnabled
) {
211 /* blending enabled */
212 for (i
= 0; i
< num_state
; i
++) {
214 blend
->rt
[i
].blend_enable
= (st
->ctx
->Color
.BlendEnabled
>> i
) & 0x1;
216 blend
->rt
[i
].rgb_func
= translate_blend(st
->ctx
->Color
.BlendEquationRGB
);
217 if (st
->ctx
->Color
.BlendEquationRGB
== GL_MIN
||
218 st
->ctx
->Color
.BlendEquationRGB
== GL_MAX
) {
219 /* Min/max are special */
220 blend
->rt
[i
].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
221 blend
->rt
[i
].rgb_dst_factor
= PIPE_BLENDFACTOR_ONE
;
224 blend
->rt
[i
].rgb_src_factor
= translate_blend(st
->ctx
->Color
.BlendSrcRGB
);
225 blend
->rt
[i
].rgb_dst_factor
= translate_blend(st
->ctx
->Color
.BlendDstRGB
);
228 blend
->rt
[i
].alpha_func
= translate_blend(st
->ctx
->Color
.BlendEquationA
);
229 if (st
->ctx
->Color
.BlendEquationA
== GL_MIN
||
230 st
->ctx
->Color
.BlendEquationA
== GL_MAX
) {
231 /* Min/max are special */
232 blend
->rt
[i
].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
233 blend
->rt
[i
].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
236 blend
->rt
[i
].alpha_src_factor
= translate_blend(st
->ctx
->Color
.BlendSrcA
);
237 blend
->rt
[i
].alpha_dst_factor
= translate_blend(st
->ctx
->Color
.BlendDstA
);
242 /* no blending / logicop */
245 /* Colormask - maybe reverse these bits? */
246 for (i
= 0; i
< num_state
; i
++) {
247 if (st
->ctx
->Color
.ColorMask
[i
][0])
248 blend
->rt
[i
].colormask
|= PIPE_MASK_R
;
249 if (st
->ctx
->Color
.ColorMask
[i
][1])
250 blend
->rt
[i
].colormask
|= PIPE_MASK_G
;
251 if (st
->ctx
->Color
.ColorMask
[i
][2])
252 blend
->rt
[i
].colormask
|= PIPE_MASK_B
;
253 if (st
->ctx
->Color
.ColorMask
[i
][3])
254 blend
->rt
[i
].colormask
|= PIPE_MASK_A
;
257 if (st
->ctx
->Color
.DitherFlag
)
260 cso_set_blend(st
->cso_context
, blend
);
263 struct pipe_blend_color bc
;
264 COPY_4FV(bc
.color
, st
->ctx
->Color
.BlendColor
);
265 cso_set_blend_color(st
->cso_context
, &bc
);
270 const struct st_tracked_state st_update_blend
= {
271 "st_update_blend", /* name */
273 (_NEW_COLOR
), /* XXX _NEW_BLEND someday? */ /* mesa */
276 update_blend
, /* update */