Added st_update_framebuffer struct/object.
[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_atom.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39
40
41 /**
42 * Convert GLenum blend tokens to pipe tokens.
43 * Both blend factors and blend funcs are accepted.
44 */
45 static GLuint
46 gl_blend_to_sp(GLenum blend)
47 {
48 switch (blend) {
49 /* blend functions */
50 case GL_FUNC_ADD:
51 return PIPE_BLEND_ADD;
52 case GL_FUNC_SUBTRACT:
53 return PIPE_BLEND_SUBTRACT;
54 case GL_FUNC_REVERSE_SUBTRACT:
55 return PIPE_BLEND_REVERSE_SUBTRACT;
56 case GL_MIN:
57 return PIPE_BLEND_MIN;
58 case GL_MAX:
59 return PIPE_BLEND_MAX;
60
61 /* blend factors */
62 case GL_ONE:
63 return PIPE_BLENDFACTOR_ONE;
64 case GL_SRC_COLOR:
65 return PIPE_BLENDFACTOR_SRC_COLOR;
66 case GL_SRC_ALPHA:
67 return PIPE_BLENDFACTOR_SRC_ALPHA;
68 case GL_DST_ALPHA:
69 return PIPE_BLENDFACTOR_DST_ALPHA;
70 case GL_DST_COLOR:
71 return PIPE_BLENDFACTOR_DST_COLOR;
72 case GL_SRC_ALPHA_SATURATE:
73 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
74 case GL_CONSTANT_COLOR:
75 return PIPE_BLENDFACTOR_CONST_COLOR;
76 case GL_CONSTANT_ALPHA:
77 return PIPE_BLENDFACTOR_CONST_ALPHA;
78 /*
79 return PIPE_BLENDFACTOR_SRC1_COLOR;
80 return PIPE_BLENDFACTOR_SRC1_ALPHA;
81 */
82 case GL_ZERO:
83 return PIPE_BLENDFACTOR_ZERO;
84 case GL_ONE_MINUS_SRC_COLOR:
85 return PIPE_BLENDFACTOR_INV_SRC_COLOR;
86 case GL_ONE_MINUS_SRC_ALPHA:
87 return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
88 case GL_ONE_MINUS_DST_COLOR:
89 return PIPE_BLENDFACTOR_INV_DST_ALPHA;
90 case GL_ONE_MINUS_DST_ALPHA:
91 return PIPE_BLENDFACTOR_INV_DST_COLOR;
92 case GL_ONE_MINUS_CONSTANT_COLOR:
93 return PIPE_BLENDFACTOR_INV_CONST_COLOR;
94 case GL_ONE_MINUS_CONSTANT_ALPHA:
95 return PIPE_BLENDFACTOR_INV_CONST_ALPHA;
96 /*
97 return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
98 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA;
99 */
100 default:
101 assert("invalid GL token in gl_blend_to_sp()" == NULL);
102 return 0;
103 }
104 }
105
106
107 /**
108 * Convert GLenum logicop tokens to pipe tokens.
109 */
110 static GLuint
111 gl_logicop_to_sp(GLenum logicop)
112 {
113 switch (logicop) {
114 case GL_CLEAR:
115 return PIPE_LOGICOP_CLEAR;
116 case GL_NOR:
117 return PIPE_LOGICOP_NOR;
118 case GL_AND_INVERTED:
119 return PIPE_LOGICOP_AND_INVERTED;
120 case GL_COPY_INVERTED:
121 return PIPE_LOGICOP_COPY_INVERTED;
122 case GL_AND_REVERSE:
123 return PIPE_LOGICOP_AND_REVERSE;
124 case GL_INVERT:
125 return PIPE_LOGICOP_INVERT;
126 case GL_XOR:
127 return PIPE_LOGICOP_XOR;
128 case GL_NAND:
129 return PIPE_LOGICOP_NAND;
130 case GL_AND:
131 return PIPE_LOGICOP_AND;
132 case GL_EQUIV:
133 return PIPE_LOGICOP_EQUIV;
134 case GL_NOOP:
135 return PIPE_LOGICOP_NOOP;
136 case GL_OR_INVERTED:
137 return PIPE_LOGICOP_OR_INVERTED;
138 case GL_COPY:
139 return PIPE_LOGICOP_COPY;
140 case GL_OR_REVERSE:
141 return PIPE_LOGICOP_OR_REVERSE;
142 case GL_OR:
143 return PIPE_LOGICOP_OR;
144 case GL_SET:
145 return PIPE_LOGICOP_SET;
146 default:
147 assert("invalid GL token in gl_logicop_to_sp()" == NULL);
148 return 0;
149 }
150 }
151
152
153 static void
154 update_blend( struct st_context *st )
155 {
156 struct pipe_blend_state blend;
157
158 memset(&blend, 0, sizeof(blend));
159
160 if (st->ctx->Color.ColorLogicOpEnabled ||
161 (st->ctx->Color.BlendEnabled &&
162 st->ctx->Color.BlendEquationRGB == GL_LOGIC_OP)) {
163 /* logicop enabled */
164 blend.logicop_enable = 1;
165 blend.logicop_func = gl_logicop_to_sp(st->ctx->Color.LogicOp);
166 }
167 else if (st->ctx->Color.BlendEnabled) {
168 /* blending enabled */
169 blend.blend_enable = 1;
170
171 blend.rgb_func = gl_blend_to_sp(st->ctx->Color.BlendEquationRGB);
172 blend.rgb_src_factor = gl_blend_to_sp(st->ctx->Color.BlendSrcRGB);
173 blend.rgb_dst_factor = gl_blend_to_sp(st->ctx->Color.BlendDstRGB);
174
175 blend.alpha_func = gl_blend_to_sp(st->ctx->Color.BlendEquationA);
176 blend.alpha_src_factor = gl_blend_to_sp(st->ctx->Color.BlendSrcA);
177 blend.alpha_dst_factor = gl_blend_to_sp(st->ctx->Color.BlendDstA);
178 }
179 else {
180 /* no blending / logicop */
181 }
182
183 if (memcmp(&blend, &st->state.blend, sizeof(blend)) != 0) {
184 /* state has changed */
185 st->state.blend = blend; /* struct copy */
186 st->pipe->set_blend_state(st->pipe, &blend); /* set new state */
187 }
188 }
189
190
191 const struct st_tracked_state st_update_blend = {
192 .dirty = {
193 .mesa = (_NEW_COLOR), /* XXX _NEW_BLEND someday? */
194 .st = 0,
195 },
196 .update = update_blend
197 };
198
199
200
201
202