Minor tweaks to help out at a driver level.
[mesa.git] / src / mesa / main / blend.c
1 /**
2 * \file blend.c
3 * Blending operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 4.1
9 *
10 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31
32 #include "glheader.h"
33 #include "blend.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "enums.h"
37 #include "macros.h"
38 #include "mtypes.h"
39
40
41 /**
42 * Specify the blending operation.
43 *
44 * \param sfactor source factor operator.
45 * \param dfactor destination factor operator.
46 *
47 * \sa glBlendFunc, glBlendFuncSeparateEXT
48 *
49 * Swizzles the inputs and calls \c glBlendFuncSeparateEXT. This is done
50 * using the \c CurrentDispatch table in the context, so this same function
51 * can be used while compiling display lists. Therefore, there is no need
52 * for the display list code to save and restore this function.
53 */
54 void GLAPIENTRY
55 _mesa_BlendFunc( GLenum sfactor, GLenum dfactor )
56 {
57 GET_CURRENT_CONTEXT(ctx);
58
59 (*ctx->CurrentDispatch->BlendFuncSeparateEXT)( sfactor, dfactor,
60 sfactor, dfactor );
61 }
62
63
64 /**
65 * Process GL_EXT_blend_func_separate().
66 *
67 * \param sfactorRGB RGB source factor operator.
68 * \param dfactorRGB RGB destination factor operator.
69 * \param sfactorA alpha source factor operator.
70 * \param dfactorA alpha destination factor operator.
71 *
72 * Verifies the parameters and updates gl_colorbuffer_attrib.
73 * On a change, flush the vertices and notify the driver via
74 * dd_function_table::BlendFuncSeparate.
75 */
76 void GLAPIENTRY
77 _mesa_BlendFuncSeparateEXT( GLenum sfactorRGB, GLenum dfactorRGB,
78 GLenum sfactorA, GLenum dfactorA )
79 {
80 GET_CURRENT_CONTEXT(ctx);
81 ASSERT_OUTSIDE_BEGIN_END(ctx);
82
83 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
84 _mesa_debug(ctx, "glBlendFuncSeparate %s %s %s %s\n",
85 _mesa_lookup_enum_by_nr(sfactorRGB),
86 _mesa_lookup_enum_by_nr(dfactorRGB),
87 _mesa_lookup_enum_by_nr(sfactorA),
88 _mesa_lookup_enum_by_nr(dfactorA));
89
90 switch (sfactorRGB) {
91 case GL_SRC_COLOR:
92 case GL_ONE_MINUS_SRC_COLOR:
93 if (!ctx->Extensions.NV_blend_square) {
94 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(sfactorRGB)");
95 return;
96 }
97 /* fall-through */
98 case GL_ZERO:
99 case GL_ONE:
100 case GL_DST_COLOR:
101 case GL_ONE_MINUS_DST_COLOR:
102 case GL_SRC_ALPHA:
103 case GL_ONE_MINUS_SRC_ALPHA:
104 case GL_DST_ALPHA:
105 case GL_ONE_MINUS_DST_ALPHA:
106 case GL_SRC_ALPHA_SATURATE:
107 case GL_CONSTANT_COLOR:
108 case GL_ONE_MINUS_CONSTANT_COLOR:
109 case GL_CONSTANT_ALPHA:
110 case GL_ONE_MINUS_CONSTANT_ALPHA:
111 break;
112 default:
113 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(sfactorRGB)");
114 return;
115 }
116
117 switch (dfactorRGB) {
118 case GL_DST_COLOR:
119 case GL_ONE_MINUS_DST_COLOR:
120 if (!ctx->Extensions.NV_blend_square) {
121 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(dfactorRGB)");
122 return;
123 }
124 /* fall-through */
125 case GL_ZERO:
126 case GL_ONE:
127 case GL_SRC_COLOR:
128 case GL_ONE_MINUS_SRC_COLOR:
129 case GL_SRC_ALPHA:
130 case GL_ONE_MINUS_SRC_ALPHA:
131 case GL_DST_ALPHA:
132 case GL_ONE_MINUS_DST_ALPHA:
133 case GL_CONSTANT_COLOR:
134 case GL_ONE_MINUS_CONSTANT_COLOR:
135 case GL_CONSTANT_ALPHA:
136 case GL_ONE_MINUS_CONSTANT_ALPHA:
137 break;
138 default:
139 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(dfactorRGB)");
140 return;
141 }
142
143 switch (sfactorA) {
144 case GL_SRC_COLOR:
145 case GL_ONE_MINUS_SRC_COLOR:
146 if (!ctx->Extensions.NV_blend_square) {
147 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(sfactorA)");
148 return;
149 }
150 /* fall-through */
151 case GL_ZERO:
152 case GL_ONE:
153 case GL_DST_COLOR:
154 case GL_ONE_MINUS_DST_COLOR:
155 case GL_SRC_ALPHA:
156 case GL_ONE_MINUS_SRC_ALPHA:
157 case GL_DST_ALPHA:
158 case GL_ONE_MINUS_DST_ALPHA:
159 case GL_SRC_ALPHA_SATURATE:
160 case GL_CONSTANT_COLOR:
161 case GL_ONE_MINUS_CONSTANT_COLOR:
162 case GL_CONSTANT_ALPHA:
163 case GL_ONE_MINUS_CONSTANT_ALPHA:
164 break;
165 default:
166 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(sfactorA)");
167 return;
168 }
169
170 switch (dfactorA) {
171 case GL_DST_COLOR:
172 case GL_ONE_MINUS_DST_COLOR:
173 if (!ctx->Extensions.NV_blend_square) {
174 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(dfactorA)");
175 return;
176 }
177 /* fall-through */
178 case GL_ZERO:
179 case GL_ONE:
180 case GL_SRC_COLOR:
181 case GL_ONE_MINUS_SRC_COLOR:
182 case GL_SRC_ALPHA:
183 case GL_ONE_MINUS_SRC_ALPHA:
184 case GL_DST_ALPHA:
185 case GL_ONE_MINUS_DST_ALPHA:
186 case GL_CONSTANT_COLOR:
187 case GL_ONE_MINUS_CONSTANT_COLOR:
188 case GL_CONSTANT_ALPHA:
189 case GL_ONE_MINUS_CONSTANT_ALPHA:
190 break;
191 default:
192 _mesa_error( ctx, GL_INVALID_ENUM, "glBlendFuncSeparate(dfactorA)" );
193 return;
194 }
195
196 if (ctx->Color.BlendSrcRGB == sfactorRGB &&
197 ctx->Color.BlendDstRGB == dfactorRGB &&
198 ctx->Color.BlendSrcA == sfactorA &&
199 ctx->Color.BlendDstA == dfactorA)
200 return;
201
202 FLUSH_VERTICES(ctx, _NEW_COLOR);
203
204 ctx->Color.BlendSrcRGB = sfactorRGB;
205 ctx->Color.BlendDstRGB = dfactorRGB;
206 ctx->Color.BlendSrcA = sfactorA;
207 ctx->Color.BlendDstA = dfactorA;
208
209 if (ctx->Driver.BlendFuncSeparate) {
210 (*ctx->Driver.BlendFuncSeparate)( ctx, sfactorRGB, dfactorRGB,
211 sfactorA, dfactorA );
212 }
213 }
214
215
216 #if _HAVE_FULL_GL
217
218 /* This is really an extension function! */
219 void GLAPIENTRY
220 _mesa_BlendEquation( GLenum mode )
221 {
222 GET_CURRENT_CONTEXT(ctx);
223 ASSERT_OUTSIDE_BEGIN_END(ctx);
224
225 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
226 _mesa_debug(ctx, "glBlendEquation %s\n",
227 _mesa_lookup_enum_by_nr(mode));
228
229 switch (mode) {
230 case GL_FUNC_ADD_EXT:
231 break;
232 case GL_MIN_EXT:
233 case GL_MAX_EXT:
234 if (!ctx->Extensions.EXT_blend_minmax &&
235 !ctx->Extensions.ARB_imaging) {
236 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
237 return;
238 }
239 break;
240 case GL_LOGIC_OP:
241 if (!ctx->Extensions.EXT_blend_logic_op) {
242 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
243 return;
244 }
245 break;
246 case GL_FUNC_SUBTRACT_EXT:
247 case GL_FUNC_REVERSE_SUBTRACT_EXT:
248 if (!ctx->Extensions.EXT_blend_subtract &&
249 !ctx->Extensions.ARB_imaging) {
250 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
251 return;
252 }
253 break;
254 default:
255 _mesa_error( ctx, GL_INVALID_ENUM, "glBlendEquation" );
256 return;
257 }
258
259 if (ctx->Color.BlendEquation == mode)
260 return;
261
262 FLUSH_VERTICES(ctx, _NEW_COLOR);
263 ctx->Color.BlendEquation = mode;
264
265 /* This is needed to support 1.1's RGB logic ops AND
266 * 1.0's blending logicops.
267 */
268 ctx->Color._LogicOpEnabled = (ctx->Color.ColorLogicOpEnabled ||
269 (ctx->Color.BlendEnabled &&
270 mode == GL_LOGIC_OP));
271
272 if (ctx->Driver.BlendEquation)
273 (*ctx->Driver.BlendEquation)( ctx, mode );
274 }
275
276 #endif
277
278
279 /**
280 * Set the blending color.
281 *
282 * \param red red color component.
283 * \param green green color component.
284 * \param blue blue color component.
285 * \param alpha alpha color component.
286 *
287 * \sa glBlendColor().
288 *
289 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
290 * change, flushes the vertices and notifies the driver via
291 * dd_function_table::BlendColor callback.
292 */
293 void GLAPIENTRY
294 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
295 {
296 GLfloat tmp[4];
297 GET_CURRENT_CONTEXT(ctx);
298 ASSERT_OUTSIDE_BEGIN_END(ctx);
299
300 tmp[0] = CLAMP( red, 0.0F, 1.0F );
301 tmp[1] = CLAMP( green, 0.0F, 1.0F );
302 tmp[2] = CLAMP( blue, 0.0F, 1.0F );
303 tmp[3] = CLAMP( alpha, 0.0F, 1.0F );
304
305 if (TEST_EQ_4V(tmp, ctx->Color.BlendColor))
306 return;
307
308 FLUSH_VERTICES(ctx, _NEW_COLOR);
309 COPY_4FV( ctx->Color.BlendColor, tmp );
310
311 if (ctx->Driver.BlendColor)
312 (*ctx->Driver.BlendColor)(ctx, tmp);
313 }
314
315
316 /**
317 * Specify the alpha test function.
318 *
319 * \param func alpha comparison function.
320 * \param ref reference value.
321 *
322 * Verifies the parameters and updates gl_colorbuffer_attrib.
323 * On a change, flushes the vertices and notifies the driver via
324 * dd_function_table::AlphaFunc callback.
325 */
326 void GLAPIENTRY
327 _mesa_AlphaFunc( GLenum func, GLclampf ref )
328 {
329 GET_CURRENT_CONTEXT(ctx);
330 ASSERT_OUTSIDE_BEGIN_END(ctx);
331
332 switch (func) {
333 case GL_NEVER:
334 case GL_LESS:
335 case GL_EQUAL:
336 case GL_LEQUAL:
337 case GL_GREATER:
338 case GL_NOTEQUAL:
339 case GL_GEQUAL:
340 case GL_ALWAYS:
341 ref = CLAMP(ref, 0.0F, 1.0F);
342
343 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRef == ref)
344 return; /* no change */
345
346 FLUSH_VERTICES(ctx, _NEW_COLOR);
347 ctx->Color.AlphaFunc = func;
348 ctx->Color.AlphaRef = ref;
349
350 if (ctx->Driver.AlphaFunc)
351 ctx->Driver.AlphaFunc(ctx, func, ref);
352 return;
353
354 default:
355 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
356 return;
357 }
358 }
359
360
361 /**
362 * Specify a logic pixel operation for color index rendering.
363 *
364 * \param opcode operation.
365 *
366 * Verifies that \p opcode is a valid enum and updates
367 gl_colorbuffer_attrib::LogicOp.
368 * On a change, flushes the vertices and notifies the driver via the
369 * dd_function_table::LogicOpcode callback.
370 */
371 void GLAPIENTRY
372 _mesa_LogicOp( GLenum opcode )
373 {
374 GET_CURRENT_CONTEXT(ctx);
375 ASSERT_OUTSIDE_BEGIN_END(ctx);
376
377 switch (opcode) {
378 case GL_CLEAR:
379 case GL_SET:
380 case GL_COPY:
381 case GL_COPY_INVERTED:
382 case GL_NOOP:
383 case GL_INVERT:
384 case GL_AND:
385 case GL_NAND:
386 case GL_OR:
387 case GL_NOR:
388 case GL_XOR:
389 case GL_EQUIV:
390 case GL_AND_REVERSE:
391 case GL_AND_INVERTED:
392 case GL_OR_REVERSE:
393 case GL_OR_INVERTED:
394 break;
395 default:
396 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
397 return;
398 }
399
400 if (ctx->Color.LogicOp == opcode)
401 return;
402
403 FLUSH_VERTICES(ctx, _NEW_COLOR);
404 ctx->Color.LogicOp = opcode;
405
406 if (ctx->Driver.LogicOpcode)
407 ctx->Driver.LogicOpcode( ctx, opcode );
408 }
409
410 #if _HAVE_FULL_GL
411 void GLAPIENTRY
412 _mesa_IndexMask( GLuint mask )
413 {
414 GET_CURRENT_CONTEXT(ctx);
415 ASSERT_OUTSIDE_BEGIN_END(ctx);
416
417 if (ctx->Color.IndexMask == mask)
418 return;
419
420 FLUSH_VERTICES(ctx, _NEW_COLOR);
421 ctx->Color.IndexMask = mask;
422
423 if (ctx->Driver.IndexMask)
424 ctx->Driver.IndexMask( ctx, mask );
425 }
426 #endif
427
428
429 /**
430 * Enable or disable writing of frame buffer color components.
431 *
432 * \param red whether to mask writing of the red color component.
433 * \param green whether to mask writing of the green color component.
434 * \param blue whether to mask writing of the blue color component.
435 * \param alpha whether to mask writing of the alpha color component.
436 *
437 * \sa glColorMask().
438 *
439 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
440 * change, flushes the vertices and notifies the driver via the
441 * dd_function_table::ColorMask callback.
442 */
443 void GLAPIENTRY
444 _mesa_ColorMask( GLboolean red, GLboolean green,
445 GLboolean blue, GLboolean alpha )
446 {
447 GET_CURRENT_CONTEXT(ctx);
448 GLubyte tmp[4];
449 ASSERT_OUTSIDE_BEGIN_END(ctx);
450
451 if (MESA_VERBOSE & VERBOSE_API)
452 _mesa_debug(ctx, "glColorMask %d %d %d %d\n", red, green, blue, alpha);
453
454 /* Shouldn't have any information about channel depth in core mesa
455 * -- should probably store these as the native booleans:
456 */
457 tmp[RCOMP] = red ? 0xff : 0x0;
458 tmp[GCOMP] = green ? 0xff : 0x0;
459 tmp[BCOMP] = blue ? 0xff : 0x0;
460 tmp[ACOMP] = alpha ? 0xff : 0x0;
461
462 if (TEST_EQ_4UBV(tmp, ctx->Color.ColorMask))
463 return;
464
465 FLUSH_VERTICES(ctx, _NEW_COLOR);
466 COPY_4UBV(ctx->Color.ColorMask, tmp);
467
468 if (ctx->Driver.ColorMask)
469 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
470 }
471
472 /**********************************************************************/
473 /** \name Initialization */
474 /*@{*/
475
476 /**
477 * Initialization of the context color data.
478 *
479 * \param ctx GL context.
480 *
481 * Initializes the related fields in the context color attribute group,
482 * __GLcontextRec::Color.
483 */
484 void _mesa_init_color( GLcontext * ctx )
485 {
486 /* Color buffer group */
487 ctx->Color.IndexMask = 0xffffffff;
488 ctx->Color.ColorMask[0] = 0xff;
489 ctx->Color.ColorMask[1] = 0xff;
490 ctx->Color.ColorMask[2] = 0xff;
491 ctx->Color.ColorMask[3] = 0xff;
492 ctx->Color.ClearIndex = 0;
493 ASSIGN_4V( ctx->Color.ClearColor, 0, 0, 0, 0 );
494 ctx->Color.DrawBuffer = GL_FRONT;
495 ctx->Color.AlphaEnabled = GL_FALSE;
496 ctx->Color.AlphaFunc = GL_ALWAYS;
497 ctx->Color.AlphaRef = 0;
498 ctx->Color.BlendEnabled = GL_FALSE;
499 ctx->Color.BlendSrcRGB = GL_ONE;
500 ctx->Color.BlendDstRGB = GL_ZERO;
501 ctx->Color.BlendSrcA = GL_ONE;
502 ctx->Color.BlendDstA = GL_ZERO;
503 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
504 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
505 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
506 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
507 ctx->Color._LogicOpEnabled = GL_FALSE;
508 ctx->Color.LogicOp = GL_COPY;
509 ctx->Color.DitherFlag = GL_TRUE;
510
511 if (ctx->Visual.doubleBufferMode) {
512 ctx->Color.DrawBuffer = GL_BACK;
513 ctx->Color._DrawDestMask = BACK_LEFT_BIT;
514 }
515 else {
516 ctx->Color.DrawBuffer = GL_FRONT;
517 ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
518 }
519 }
520
521 /*@}*/