1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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 "framebuffer.h"
43 #include "main/blend.h"
44 #include "main/macros.h"
47 * Convert GLenum blend tokens to pipe tokens.
48 * Both blend factors and blend funcs are accepted.
51 translate_blend(GLenum blend
)
56 return PIPE_BLEND_ADD
;
57 case GL_FUNC_SUBTRACT
:
58 return PIPE_BLEND_SUBTRACT
;
59 case GL_FUNC_REVERSE_SUBTRACT
:
60 return PIPE_BLEND_REVERSE_SUBTRACT
;
62 return PIPE_BLEND_MIN
;
64 return PIPE_BLEND_MAX
;
68 return PIPE_BLENDFACTOR_ONE
;
70 return PIPE_BLENDFACTOR_SRC_COLOR
;
72 return PIPE_BLENDFACTOR_SRC_ALPHA
;
74 return PIPE_BLENDFACTOR_DST_ALPHA
;
76 return PIPE_BLENDFACTOR_DST_COLOR
;
77 case GL_SRC_ALPHA_SATURATE
:
78 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
;
79 case GL_CONSTANT_COLOR
:
80 return PIPE_BLENDFACTOR_CONST_COLOR
;
81 case GL_CONSTANT_ALPHA
:
82 return PIPE_BLENDFACTOR_CONST_ALPHA
;
84 return PIPE_BLENDFACTOR_SRC1_COLOR
;
86 return PIPE_BLENDFACTOR_SRC1_ALPHA
;
88 return PIPE_BLENDFACTOR_ZERO
;
89 case GL_ONE_MINUS_SRC_COLOR
:
90 return PIPE_BLENDFACTOR_INV_SRC_COLOR
;
91 case GL_ONE_MINUS_SRC_ALPHA
:
92 return PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
93 case GL_ONE_MINUS_DST_COLOR
:
94 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
95 case GL_ONE_MINUS_DST_ALPHA
:
96 return PIPE_BLENDFACTOR_INV_DST_ALPHA
;
97 case GL_ONE_MINUS_CONSTANT_COLOR
:
98 return PIPE_BLENDFACTOR_INV_CONST_COLOR
;
99 case GL_ONE_MINUS_CONSTANT_ALPHA
:
100 return PIPE_BLENDFACTOR_INV_CONST_ALPHA
;
101 case GL_ONE_MINUS_SRC1_COLOR
:
102 return PIPE_BLENDFACTOR_INV_SRC1_COLOR
;
103 case GL_ONE_MINUS_SRC1_ALPHA
:
104 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA
;
106 assert("invalid GL token in translate_blend()" == NULL
);
112 * Figure out if colormasks are different per rt.
115 colormask_per_rt(const struct gl_context
*ctx
, unsigned num_cb
)
117 GLbitfield full_mask
= _mesa_replicate_colormask(0xf, num_cb
);
118 GLbitfield repl_mask0
=
119 _mesa_replicate_colormask(GET_COLORMASK(ctx
->Color
.ColorMask
, 0),
122 return (ctx
->Color
.ColorMask
& full_mask
) != repl_mask0
;
126 * Figure out if blend enables/state are different per rt.
129 blend_per_rt(const struct gl_context
*ctx
, unsigned num_cb
)
131 GLbitfield cb_mask
= u_bit_consecutive(0, num_cb
);
132 GLbitfield blend_enabled
= ctx
->Color
.BlendEnabled
& cb_mask
;
134 if (blend_enabled
&& blend_enabled
!= cb_mask
) {
135 /* This can only happen if GL_EXT_draw_buffers2 is enabled */
138 if (ctx
->Color
._BlendFuncPerBuffer
|| ctx
->Color
._BlendEquationPerBuffer
) {
139 /* this can only happen if GL_ARB_draw_buffers_blend is enabled */
142 if (ctx
->DrawBuffer
->_IntegerBuffers
&&
143 (ctx
->DrawBuffer
->_IntegerBuffers
!= cb_mask
)) {
144 /* If there is a mix of integer/non-integer buffers then blending
145 * must be handled on a per buffer basis. */
152 st_update_blend( struct st_context
*st
)
154 struct pipe_blend_state
*blend
= &st
->state
.blend
;
155 const struct gl_context
*ctx
= st
->ctx
;
156 unsigned num_cb
= st
->state
.fb_num_cb
;
157 unsigned num_state
= 1;
160 memset(blend
, 0, sizeof(*blend
));
163 (blend_per_rt(ctx
, num_cb
) || colormask_per_rt(ctx
, num_cb
))) {
165 blend
->independent_blend_enable
= 1;
168 for (i
= 0; i
< num_state
; i
++)
169 blend
->rt
[i
].colormask
= GET_COLORMASK(ctx
->Color
.ColorMask
, i
);
171 if (ctx
->Color
.ColorLogicOpEnabled
) {
172 /* logicop enabled */
173 blend
->logicop_enable
= 1;
174 blend
->logicop_func
= ctx
->Color
._LogicOp
;
176 else if (ctx
->Color
.BlendEnabled
&& !ctx
->Color
._AdvancedBlendMode
) {
177 /* blending enabled */
178 for (i
= 0, j
= 0; i
< num_state
; i
++) {
179 if (!(ctx
->Color
.BlendEnabled
& (1 << i
)) ||
180 (ctx
->DrawBuffer
->_IntegerBuffers
& (1 << i
)) ||
181 !blend
->rt
[i
].colormask
)
184 if (ctx
->Extensions
.ARB_draw_buffers_blend
)
187 blend
->rt
[i
].blend_enable
= 1;
188 blend
->rt
[i
].rgb_func
=
189 translate_blend(ctx
->Color
.Blend
[j
].EquationRGB
);
191 if (ctx
->Color
.Blend
[i
].EquationRGB
== GL_MIN
||
192 ctx
->Color
.Blend
[i
].EquationRGB
== GL_MAX
) {
193 /* Min/max are special */
194 blend
->rt
[i
].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
195 blend
->rt
[i
].rgb_dst_factor
= PIPE_BLENDFACTOR_ONE
;
198 blend
->rt
[i
].rgb_src_factor
=
199 translate_blend(ctx
->Color
.Blend
[j
].SrcRGB
);
200 blend
->rt
[i
].rgb_dst_factor
=
201 translate_blend(ctx
->Color
.Blend
[j
].DstRGB
);
204 blend
->rt
[i
].alpha_func
=
205 translate_blend(ctx
->Color
.Blend
[j
].EquationA
);
207 if (ctx
->Color
.Blend
[i
].EquationA
== GL_MIN
||
208 ctx
->Color
.Blend
[i
].EquationA
== GL_MAX
) {
209 /* Min/max are special */
210 blend
->rt
[i
].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
211 blend
->rt
[i
].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
214 blend
->rt
[i
].alpha_src_factor
=
215 translate_blend(ctx
->Color
.Blend
[j
].SrcA
);
216 blend
->rt
[i
].alpha_dst_factor
=
217 translate_blend(ctx
->Color
.Blend
[j
].DstA
);
222 /* no blending / logicop */
225 blend
->dither
= ctx
->Color
.DitherFlag
;
227 if (_mesa_is_multisample_enabled(ctx
) &&
228 !(ctx
->DrawBuffer
->_IntegerBuffers
& 0x1)) {
229 /* Unlike in gallium/d3d10 these operations are only performed
230 * if both msaa is enabled and we have a multisample buffer.
232 blend
->alpha_to_coverage
= ctx
->Multisample
.SampleAlphaToCoverage
;
233 blend
->alpha_to_one
= ctx
->Multisample
.SampleAlphaToOne
;
236 cso_set_blend(st
->cso_context
, blend
);
240 st_update_blend_color(struct st_context
*st
)
242 struct pipe_blend_color bc
;
244 COPY_4FV(bc
.color
, st
->ctx
->Color
.BlendColorUnclamped
);
245 cso_set_blend_color(st
->cso_context
, &bc
);