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/macros.h"
46 * Convert GLenum blend tokens to pipe tokens.
47 * Both blend factors and blend funcs are accepted.
50 translate_blend(GLenum blend
)
55 return PIPE_BLEND_ADD
;
56 case GL_FUNC_SUBTRACT
:
57 return PIPE_BLEND_SUBTRACT
;
58 case GL_FUNC_REVERSE_SUBTRACT
:
59 return PIPE_BLEND_REVERSE_SUBTRACT
;
61 return PIPE_BLEND_MIN
;
63 return PIPE_BLEND_MAX
;
67 return PIPE_BLENDFACTOR_ONE
;
69 return PIPE_BLENDFACTOR_SRC_COLOR
;
71 return PIPE_BLENDFACTOR_SRC_ALPHA
;
73 return PIPE_BLENDFACTOR_DST_ALPHA
;
75 return PIPE_BLENDFACTOR_DST_COLOR
;
76 case GL_SRC_ALPHA_SATURATE
:
77 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
;
78 case GL_CONSTANT_COLOR
:
79 return PIPE_BLENDFACTOR_CONST_COLOR
;
80 case GL_CONSTANT_ALPHA
:
81 return PIPE_BLENDFACTOR_CONST_ALPHA
;
83 return PIPE_BLENDFACTOR_SRC1_COLOR
;
85 return PIPE_BLENDFACTOR_SRC1_ALPHA
;
87 return PIPE_BLENDFACTOR_ZERO
;
88 case GL_ONE_MINUS_SRC_COLOR
:
89 return PIPE_BLENDFACTOR_INV_SRC_COLOR
;
90 case GL_ONE_MINUS_SRC_ALPHA
:
91 return PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
92 case GL_ONE_MINUS_DST_COLOR
:
93 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
94 case GL_ONE_MINUS_DST_ALPHA
:
95 return PIPE_BLENDFACTOR_INV_DST_ALPHA
;
96 case GL_ONE_MINUS_CONSTANT_COLOR
:
97 return PIPE_BLENDFACTOR_INV_CONST_COLOR
;
98 case GL_ONE_MINUS_CONSTANT_ALPHA
:
99 return PIPE_BLENDFACTOR_INV_CONST_ALPHA
;
100 case GL_ONE_MINUS_SRC1_COLOR
:
101 return PIPE_BLENDFACTOR_INV_SRC1_COLOR
;
102 case GL_ONE_MINUS_SRC1_ALPHA
:
103 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA
;
105 assert("invalid GL token in translate_blend()" == NULL
);
111 * Figure out if colormasks are different per rt.
114 colormask_per_rt(const struct gl_context
*ctx
)
116 /* a bit suboptimal have to compare lots of values */
118 for (i
= 1; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
119 if (memcmp(ctx
->Color
.ColorMask
[0], ctx
->Color
.ColorMask
[i
], 4)) {
127 * Figure out if blend enables/state are different per rt.
130 blend_per_rt(const struct gl_context
*ctx
)
132 if (ctx
->Color
.BlendEnabled
&&
133 (ctx
->Color
.BlendEnabled
!= ((1U << ctx
->Const
.MaxDrawBuffers
) - 1))) {
134 /* This can only happen if GL_EXT_draw_buffers2 is enabled */
137 if (ctx
->Color
._BlendFuncPerBuffer
|| ctx
->Color
._BlendEquationPerBuffer
) {
138 /* this can only happen if GL_ARB_draw_buffers_blend is enabled */
145 st_update_blend( struct st_context
*st
)
147 struct pipe_blend_state
*blend
= &st
->state
.blend
;
148 const struct gl_context
*ctx
= st
->ctx
;
149 unsigned num_state
= 1;
152 memset(blend
, 0, sizeof(*blend
));
154 if (blend_per_rt(ctx
) || colormask_per_rt(ctx
)) {
155 num_state
= ctx
->Const
.MaxDrawBuffers
;
156 blend
->independent_blend_enable
= 1;
158 if (ctx
->Color
.ColorLogicOpEnabled
) {
159 /* logicop enabled */
160 blend
->logicop_enable
= 1;
161 blend
->logicop_func
= ctx
->Color
._LogicOp
;
163 else if (ctx
->Color
.BlendEnabled
&& !ctx
->Color
._AdvancedBlendMode
) {
164 /* blending enabled */
165 for (i
= 0, j
= 0; i
< num_state
; i
++) {
167 blend
->rt
[i
].blend_enable
= (ctx
->Color
.BlendEnabled
>> i
) & 0x1;
169 if (ctx
->Extensions
.ARB_draw_buffers_blend
)
172 blend
->rt
[i
].rgb_func
=
173 translate_blend(ctx
->Color
.Blend
[j
].EquationRGB
);
175 if (ctx
->Color
.Blend
[i
].EquationRGB
== GL_MIN
||
176 ctx
->Color
.Blend
[i
].EquationRGB
== GL_MAX
) {
177 /* Min/max are special */
178 blend
->rt
[i
].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
179 blend
->rt
[i
].rgb_dst_factor
= PIPE_BLENDFACTOR_ONE
;
182 blend
->rt
[i
].rgb_src_factor
=
183 translate_blend(ctx
->Color
.Blend
[j
].SrcRGB
);
184 blend
->rt
[i
].rgb_dst_factor
=
185 translate_blend(ctx
->Color
.Blend
[j
].DstRGB
);
188 blend
->rt
[i
].alpha_func
=
189 translate_blend(ctx
->Color
.Blend
[j
].EquationA
);
191 if (ctx
->Color
.Blend
[i
].EquationA
== GL_MIN
||
192 ctx
->Color
.Blend
[i
].EquationA
== GL_MAX
) {
193 /* Min/max are special */
194 blend
->rt
[i
].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
195 blend
->rt
[i
].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
198 blend
->rt
[i
].alpha_src_factor
=
199 translate_blend(ctx
->Color
.Blend
[j
].SrcA
);
200 blend
->rt
[i
].alpha_dst_factor
=
201 translate_blend(ctx
->Color
.Blend
[j
].DstA
);
206 /* no blending / logicop */
209 /* Colormask - maybe reverse these bits? */
210 for (i
= 0; i
< num_state
; i
++) {
211 if (ctx
->Color
.ColorMask
[i
][0])
212 blend
->rt
[i
].colormask
|= PIPE_MASK_R
;
213 if (ctx
->Color
.ColorMask
[i
][1])
214 blend
->rt
[i
].colormask
|= PIPE_MASK_G
;
215 if (ctx
->Color
.ColorMask
[i
][2])
216 blend
->rt
[i
].colormask
|= PIPE_MASK_B
;
217 if (ctx
->Color
.ColorMask
[i
][3])
218 blend
->rt
[i
].colormask
|= PIPE_MASK_A
;
221 blend
->dither
= ctx
->Color
.DitherFlag
;
223 if (_mesa_is_multisample_enabled(ctx
) &&
224 !(ctx
->DrawBuffer
->_IntegerBuffers
& 0x1)) {
225 /* Unlike in gallium/d3d10 these operations are only performed
226 * if both msaa is enabled and we have a multisample buffer.
228 blend
->alpha_to_coverage
= ctx
->Multisample
.SampleAlphaToCoverage
;
229 blend
->alpha_to_one
= ctx
->Multisample
.SampleAlphaToOne
;
232 cso_set_blend(st
->cso_context
, blend
);
236 st_update_blend_color(struct st_context
*st
)
238 struct pipe_blend_color bc
;
240 COPY_4FV(bc
.color
, st
->ctx
->Color
.BlendColorUnclamped
);
241 cso_set_blend_color(st
->cso_context
, &bc
);