blend.c revision 093dc9e548537e6c77e33064a584f849ad90dfa5
1/**
2 * \file blend.c
3 * Blending operations.
4 */
5
6/*
7 * Mesa 3-D graphics library
8 * Version:  6.5.1
9 *
10 * Copyright (C) 1999-2006  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 "context.h"
35#include "enums.h"
36#include "macros.h"
37#include "mtypes.h"
38
39
40
41/**
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
44 */
45static GLboolean
46legal_src_factor(const struct gl_context *ctx, GLenum factor)
47{
48   switch (factor) {
49   case GL_SRC_COLOR:
50   case GL_ONE_MINUS_SRC_COLOR:
51      return ctx->Extensions.NV_blend_square;
52   case GL_ZERO:
53   case GL_ONE:
54   case GL_DST_COLOR:
55   case GL_ONE_MINUS_DST_COLOR:
56   case GL_SRC_ALPHA:
57   case GL_ONE_MINUS_SRC_ALPHA:
58   case GL_DST_ALPHA:
59   case GL_ONE_MINUS_DST_ALPHA:
60   case GL_SRC_ALPHA_SATURATE:
61   case GL_CONSTANT_COLOR:
62   case GL_ONE_MINUS_CONSTANT_COLOR:
63   case GL_CONSTANT_ALPHA:
64   case GL_ONE_MINUS_CONSTANT_ALPHA:
65      return GL_TRUE;
66   default:
67      return GL_FALSE;
68   }
69}
70
71
72/**
73 * Check if given blend destination factor is legal.
74 * \return GL_TRUE if legal, GL_FALSE otherwise.
75 */
76static GLboolean
77legal_dst_factor(const struct gl_context *ctx, GLenum factor)
78{
79   switch (factor) {
80   case GL_DST_COLOR:
81   case GL_ONE_MINUS_DST_COLOR:
82      return ctx->Extensions.NV_blend_square;
83   case GL_ZERO:
84   case GL_ONE:
85   case GL_SRC_COLOR:
86   case GL_ONE_MINUS_SRC_COLOR:
87   case GL_SRC_ALPHA:
88   case GL_ONE_MINUS_SRC_ALPHA:
89   case GL_DST_ALPHA:
90   case GL_ONE_MINUS_DST_ALPHA:
91   case GL_CONSTANT_COLOR:
92   case GL_ONE_MINUS_CONSTANT_COLOR:
93   case GL_CONSTANT_ALPHA:
94   case GL_ONE_MINUS_CONSTANT_ALPHA:
95      return GL_TRUE;
96   default:
97      return GL_FALSE;
98   }
99}
100
101
102/**
103 * Check if src/dest RGB/A blend factors are legal.  If not generate
104 * a GL error.
105 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
106 */
107static GLboolean
108validate_blend_factors(struct gl_context *ctx, const char *func,
109                       GLenum sfactorRGB, GLenum dfactorRGB,
110                       GLenum sfactorA, GLenum dfactorA)
111{
112   if (!legal_src_factor(ctx, sfactorRGB)) {
113      _mesa_error(ctx, GL_INVALID_ENUM,
114                  "%s(sfactorRGB = %s)", func,
115                  _mesa_lookup_enum_by_nr(sfactorRGB));
116      return GL_FALSE;
117   }
118
119   if (!legal_dst_factor(ctx, dfactorRGB)) {
120      _mesa_error(ctx, GL_INVALID_ENUM,
121                  "%s(dfactorRGB = %s)", func,
122                  _mesa_lookup_enum_by_nr(dfactorRGB));
123      return GL_FALSE;
124   }
125
126   if (sfactorA != sfactorRGB && !legal_src_factor(ctx, sfactorA)) {
127      _mesa_error(ctx, GL_INVALID_ENUM,
128                  "%s(sfactorA = %s)", func,
129                  _mesa_lookup_enum_by_nr(sfactorA));
130      return GL_FALSE;
131   }
132
133   if (dfactorA != dfactorRGB && !legal_dst_factor(ctx, dfactorA)) {
134      _mesa_error(ctx, GL_INVALID_ENUM,
135                  "%s(dfactorA = %s)", func,
136                  _mesa_lookup_enum_by_nr(dfactorA));
137      return GL_FALSE;
138   }
139
140   return GL_TRUE;
141}
142
143
144/**
145 * Specify the blending operation.
146 *
147 * \param sfactor source factor operator.
148 * \param dfactor destination factor operator.
149 *
150 * \sa glBlendFunc, glBlendFuncSeparateEXT
151 */
152void GLAPIENTRY
153_mesa_BlendFunc( GLenum sfactor, GLenum dfactor )
154{
155   _mesa_BlendFuncSeparateEXT(sfactor, dfactor, sfactor, dfactor);
156}
157
158
159/**
160 * Set the separate blend source/dest factors for all draw buffers.
161 *
162 * \param sfactorRGB RGB source factor operator.
163 * \param dfactorRGB RGB destination factor operator.
164 * \param sfactorA alpha source factor operator.
165 * \param dfactorA alpha destination factor operator.
166 */
167void GLAPIENTRY
168_mesa_BlendFuncSeparateEXT( GLenum sfactorRGB, GLenum dfactorRGB,
169                            GLenum sfactorA, GLenum dfactorA )
170{
171   GLuint buf, numBuffers;
172   GLboolean changed;
173   GET_CURRENT_CONTEXT(ctx);
174   ASSERT_OUTSIDE_BEGIN_END(ctx);
175
176   if (MESA_VERBOSE & VERBOSE_API)
177      _mesa_debug(ctx, "glBlendFuncSeparate %s %s %s %s\n",
178                  _mesa_lookup_enum_by_nr(sfactorRGB),
179                  _mesa_lookup_enum_by_nr(dfactorRGB),
180                  _mesa_lookup_enum_by_nr(sfactorA),
181                  _mesa_lookup_enum_by_nr(dfactorA));
182
183   if (!validate_blend_factors(ctx, "glBlendFuncSeparate",
184                               sfactorRGB, dfactorRGB,
185                               sfactorA, dfactorA)) {
186      return;
187   }
188
189   numBuffers = ctx->Extensions.ARB_draw_buffers_blend
190      ? ctx->Const.MaxDrawBuffers : 1;
191
192   changed = GL_FALSE;
193   for (buf = 0; buf < numBuffers; buf++) {
194      if (ctx->Color.Blend[buf].SrcRGB != sfactorRGB ||
195          ctx->Color.Blend[buf].DstRGB != dfactorRGB ||
196          ctx->Color.Blend[buf].SrcA != sfactorA ||
197          ctx->Color.Blend[buf].DstA != dfactorA) {
198         changed = GL_TRUE;
199         break;
200      }
201   }
202   if (!changed)
203      return;
204
205   FLUSH_VERTICES(ctx, _NEW_COLOR);
206
207   for (buf = 0; buf < numBuffers; buf++) {
208      ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
209      ctx->Color.Blend[buf].DstRGB = dfactorRGB;
210      ctx->Color.Blend[buf].SrcA = sfactorA;
211      ctx->Color.Blend[buf].DstA = dfactorA;
212   }
213   ctx->Color._BlendFuncPerBuffer = GL_FALSE;
214
215   if (ctx->Driver.BlendFuncSeparate) {
216      ctx->Driver.BlendFuncSeparate(ctx, sfactorRGB, dfactorRGB,
217                                    sfactorA, dfactorA);
218   }
219}
220
221
222#if _HAVE_FULL_GL
223
224
225/**
226 * Set blend source/dest factors for one color buffer/target.
227 */
228void GLAPIENTRY
229_mesa_BlendFunci(GLuint buf, GLenum sfactor, GLenum dfactor)
230{
231   _mesa_BlendFuncSeparatei(buf, sfactor, dfactor, sfactor, dfactor);
232}
233
234
235/**
236 * Set separate blend source/dest factors for one color buffer/target.
237 */
238void GLAPIENTRY
239_mesa_BlendFuncSeparatei(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
240                         GLenum sfactorA, GLenum dfactorA)
241{
242   GET_CURRENT_CONTEXT(ctx);
243   ASSERT_OUTSIDE_BEGIN_END(ctx);
244
245   if (!ctx->Extensions.ARB_draw_buffers_blend) {
246      _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
247      return;
248   }
249
250   if (buf >= ctx->Const.MaxDrawBuffers) {
251      _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
252                  buf);
253      return;
254   }
255
256   if (!validate_blend_factors(ctx, "glBlendFuncSeparatei",
257                               sfactorRGB, dfactorRGB,
258                               sfactorA, dfactorA)) {
259      return;
260   }
261
262   if (ctx->Color.Blend[buf].SrcRGB == sfactorRGB &&
263       ctx->Color.Blend[buf].DstRGB == dfactorRGB &&
264       ctx->Color.Blend[buf].SrcA == sfactorA &&
265       ctx->Color.Blend[buf].DstA == dfactorA)
266      return; /* no change */
267
268   FLUSH_VERTICES(ctx, _NEW_COLOR);
269
270   ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
271   ctx->Color.Blend[buf].DstRGB = dfactorRGB;
272   ctx->Color.Blend[buf].SrcA = sfactorA;
273   ctx->Color.Blend[buf].DstA = dfactorA;
274   ctx->Color._BlendFuncPerBuffer = GL_TRUE;
275
276   if (ctx->Driver.BlendFuncSeparatei) {
277      ctx->Driver.BlendFuncSeparatei(ctx, buf, sfactorRGB, dfactorRGB,
278                                     sfactorA, dfactorA);
279   }
280}
281
282
283/**
284 * Check if given blend equation is legal.
285 * \return GL_TRUE if legal, GL_FALSE otherwise.
286 */
287static GLboolean
288legal_blend_equation(const struct gl_context *ctx,
289                     GLenum mode, GLboolean is_separate)
290{
291   switch (mode) {
292   case GL_FUNC_ADD:
293      return GL_TRUE;
294   case GL_MIN:
295   case GL_MAX:
296      return ctx->Extensions.EXT_blend_minmax;
297   case GL_LOGIC_OP:
298      /* glBlendEquationSeparate cannot take GL_LOGIC_OP as a parameter.
299       */
300      return ctx->Extensions.EXT_blend_logic_op && !is_separate;
301   case GL_FUNC_SUBTRACT:
302   case GL_FUNC_REVERSE_SUBTRACT:
303      return ctx->Extensions.EXT_blend_subtract;
304   default:
305      return GL_FALSE;
306   }
307}
308
309
310/* This is really an extension function! */
311void GLAPIENTRY
312_mesa_BlendEquation( GLenum mode )
313{
314   GLuint buf, numBuffers;
315   GLboolean changed;
316   GET_CURRENT_CONTEXT(ctx);
317   ASSERT_OUTSIDE_BEGIN_END(ctx);
318
319   if (MESA_VERBOSE & VERBOSE_API)
320      _mesa_debug(ctx, "glBlendEquation(%s)\n",
321                  _mesa_lookup_enum_by_nr(mode));
322
323   if (!legal_blend_equation(ctx, mode, GL_FALSE)) {
324      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
325      return;
326   }
327
328   numBuffers = ctx->Extensions.ARB_draw_buffers_blend
329      ? ctx->Const.MaxDrawBuffers : 1;
330
331   changed = GL_FALSE;
332   for (buf = 0; buf < numBuffers; buf++) {
333      if (ctx->Color.Blend[buf].EquationRGB != mode ||
334          ctx->Color.Blend[buf].EquationA != mode) {
335         changed = GL_TRUE;
336         break;
337      }
338   }
339   if (!changed)
340      return;
341
342   FLUSH_VERTICES(ctx, _NEW_COLOR);
343   for (buf = 0; buf < numBuffers; buf++) {
344      ctx->Color.Blend[buf].EquationRGB = mode;
345      ctx->Color.Blend[buf].EquationA = mode;
346   }
347   ctx->Color._BlendEquationPerBuffer = GL_FALSE;
348
349   if (ctx->Driver.BlendEquationSeparate)
350      (*ctx->Driver.BlendEquationSeparate)( ctx, mode, mode );
351}
352
353
354/**
355 * Set blend equation for one color buffer/target.
356 */
357void GLAPIENTRY
358_mesa_BlendEquationi(GLuint buf, GLenum mode)
359{
360   GET_CURRENT_CONTEXT(ctx);
361   ASSERT_OUTSIDE_BEGIN_END(ctx);
362
363   if (MESA_VERBOSE & VERBOSE_API)
364      _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
365                  buf, _mesa_lookup_enum_by_nr(mode));
366
367   if (buf >= ctx->Const.MaxDrawBuffers) {
368      _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
369                  buf);
370      return;
371   }
372
373   if (!legal_blend_equation(ctx, mode, GL_FALSE)) {
374      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
375      return;
376   }
377
378   if (ctx->Color.Blend[buf].EquationRGB == mode &&
379       ctx->Color.Blend[buf].EquationA == mode)
380      return;  /* no change */
381
382   FLUSH_VERTICES(ctx, _NEW_COLOR);
383   ctx->Color.Blend[buf].EquationRGB = mode;
384   ctx->Color.Blend[buf].EquationA = mode;
385   ctx->Color._BlendEquationPerBuffer = GL_TRUE;
386
387   if (ctx->Driver.BlendEquationSeparatei)
388      ctx->Driver.BlendEquationSeparatei(ctx, buf, mode, mode);
389}
390
391
392void GLAPIENTRY
393_mesa_BlendEquationSeparateEXT( GLenum modeRGB, GLenum modeA )
394{
395   GLuint buf, numBuffers;
396   GLboolean changed;
397   GET_CURRENT_CONTEXT(ctx);
398   ASSERT_OUTSIDE_BEGIN_END(ctx);
399
400   if (MESA_VERBOSE & VERBOSE_API)
401      _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
402                  _mesa_lookup_enum_by_nr(modeRGB),
403                  _mesa_lookup_enum_by_nr(modeA));
404
405   if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
406      _mesa_error(ctx, GL_INVALID_OPERATION,
407		  "glBlendEquationSeparateEXT not supported by driver");
408      return;
409   }
410
411   if (!legal_blend_equation(ctx, modeRGB, GL_TRUE)) {
412      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
413      return;
414   }
415
416   if (!legal_blend_equation(ctx, modeA, GL_TRUE)) {
417      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
418      return;
419   }
420
421   numBuffers = ctx->Extensions.ARB_draw_buffers_blend
422      ? ctx->Const.MaxDrawBuffers : 1;
423
424   changed = GL_FALSE;
425   for (buf = 0; buf < numBuffers; buf++) {
426      if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
427          ctx->Color.Blend[buf].EquationA != modeA) {
428         changed = GL_TRUE;
429         break;
430      }
431   }
432   if (!changed)
433      return;
434
435   FLUSH_VERTICES(ctx, _NEW_COLOR);
436   for (buf = 0; buf < numBuffers; buf++) {
437      ctx->Color.Blend[buf].EquationRGB = modeRGB;
438      ctx->Color.Blend[buf].EquationA = modeA;
439   }
440   ctx->Color._BlendEquationPerBuffer = GL_FALSE;
441
442   if (ctx->Driver.BlendEquationSeparate)
443      ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
444}
445
446
447/**
448 * Set separate blend equations for one color buffer/target.
449 */
450void GLAPIENTRY
451_mesa_BlendEquationSeparatei(GLuint buf, GLenum modeRGB, GLenum modeA)
452{
453   GET_CURRENT_CONTEXT(ctx);
454   ASSERT_OUTSIDE_BEGIN_END(ctx);
455
456   if (MESA_VERBOSE & VERBOSE_API)
457      _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
458                  _mesa_lookup_enum_by_nr(modeRGB),
459                  _mesa_lookup_enum_by_nr(modeA));
460
461   if (buf >= ctx->Const.MaxDrawBuffers) {
462      _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
463                  buf);
464      return;
465   }
466
467   if (!legal_blend_equation(ctx, modeRGB, GL_TRUE)) {
468      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
469      return;
470   }
471
472   if (!legal_blend_equation(ctx, modeA, GL_TRUE)) {
473      _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
474      return;
475   }
476
477   if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
478       ctx->Color.Blend[buf].EquationA == modeA)
479      return;  /* no change */
480
481   FLUSH_VERTICES(ctx, _NEW_COLOR);
482   ctx->Color.Blend[buf].EquationRGB = modeRGB;
483   ctx->Color.Blend[buf].EquationA = modeA;
484   ctx->Color._BlendEquationPerBuffer = GL_TRUE;
485
486   if (ctx->Driver.BlendEquationSeparatei)
487      ctx->Driver.BlendEquationSeparatei(ctx, buf, modeRGB, modeA);
488}
489
490
491
492#endif /* _HAVE_FULL_GL */
493
494
495/**
496 * Set the blending color.
497 *
498 * \param red red color component.
499 * \param green green color component.
500 * \param blue blue color component.
501 * \param alpha alpha color component.
502 *
503 * \sa glBlendColor().
504 *
505 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor.  On a
506 * change, flushes the vertices and notifies the driver via
507 * dd_function_table::BlendColor callback.
508 */
509void GLAPIENTRY
510_mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
511{
512   GLfloat tmp[4];
513   GET_CURRENT_CONTEXT(ctx);
514   ASSERT_OUTSIDE_BEGIN_END(ctx);
515
516   tmp[0] = red;
517   tmp[1] = green;
518   tmp[2] = blue;
519   tmp[3] = alpha;
520
521   if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
522      return;
523
524   FLUSH_VERTICES(ctx, _NEW_COLOR);
525   COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
526
527   ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
528   ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
529   ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
530   ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
531
532   if (ctx->Driver.BlendColor)
533      (*ctx->Driver.BlendColor)(ctx, ctx->Color.BlendColor);
534}
535
536
537/**
538 * Specify the alpha test function.
539 *
540 * \param func alpha comparison function.
541 * \param ref reference value.
542 *
543 * Verifies the parameters and updates gl_colorbuffer_attrib.
544 * On a change, flushes the vertices and notifies the driver via
545 * dd_function_table::AlphaFunc callback.
546 */
547void GLAPIENTRY
548_mesa_AlphaFunc( GLenum func, GLclampf ref )
549{
550   GET_CURRENT_CONTEXT(ctx);
551   ASSERT_OUTSIDE_BEGIN_END(ctx);
552
553   if (MESA_VERBOSE & VERBOSE_API)
554      _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
555                  _mesa_lookup_enum_by_nr(func), ref);
556
557   switch (func) {
558   case GL_NEVER:
559   case GL_LESS:
560   case GL_EQUAL:
561   case GL_LEQUAL:
562   case GL_GREATER:
563   case GL_NOTEQUAL:
564   case GL_GEQUAL:
565   case GL_ALWAYS:
566      if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
567         return; /* no change */
568
569      FLUSH_VERTICES(ctx, _NEW_COLOR);
570      ctx->Color.AlphaFunc = func;
571      ctx->Color.AlphaRefUnclamped = ref;
572      ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
573
574      if (ctx->Driver.AlphaFunc)
575         ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
576      return;
577
578   default:
579      _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
580      return;
581   }
582}
583
584
585/**
586 * Specify a logic pixel operation for color index rendering.
587 *
588 * \param opcode operation.
589 *
590 * Verifies that \p opcode is a valid enum and updates
591gl_colorbuffer_attrib::LogicOp.
592 * On a change, flushes the vertices and notifies the driver via the
593 * dd_function_table::LogicOpcode callback.
594 */
595void GLAPIENTRY
596_mesa_LogicOp( GLenum opcode )
597{
598   GET_CURRENT_CONTEXT(ctx);
599   ASSERT_OUTSIDE_BEGIN_END(ctx);
600
601   if (MESA_VERBOSE & VERBOSE_API)
602      _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_lookup_enum_by_nr(opcode));
603
604   switch (opcode) {
605      case GL_CLEAR:
606      case GL_SET:
607      case GL_COPY:
608      case GL_COPY_INVERTED:
609      case GL_NOOP:
610      case GL_INVERT:
611      case GL_AND:
612      case GL_NAND:
613      case GL_OR:
614      case GL_NOR:
615      case GL_XOR:
616      case GL_EQUIV:
617      case GL_AND_REVERSE:
618      case GL_AND_INVERTED:
619      case GL_OR_REVERSE:
620      case GL_OR_INVERTED:
621	 break;
622      default:
623         _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
624	 return;
625   }
626
627   if (ctx->Color.LogicOp == opcode)
628      return;
629
630   FLUSH_VERTICES(ctx, _NEW_COLOR);
631   ctx->Color.LogicOp = opcode;
632
633   if (ctx->Driver.LogicOpcode)
634      ctx->Driver.LogicOpcode( ctx, opcode );
635}
636
637#if _HAVE_FULL_GL
638void GLAPIENTRY
639_mesa_IndexMask( GLuint mask )
640{
641   GET_CURRENT_CONTEXT(ctx);
642   ASSERT_OUTSIDE_BEGIN_END(ctx);
643
644   if (ctx->Color.IndexMask == mask)
645      return;
646
647   FLUSH_VERTICES(ctx, _NEW_COLOR);
648   ctx->Color.IndexMask = mask;
649}
650#endif
651
652
653/**
654 * Enable or disable writing of frame buffer color components.
655 *
656 * \param red whether to mask writing of the red color component.
657 * \param green whether to mask writing of the green color component.
658 * \param blue whether to mask writing of the blue color component.
659 * \param alpha whether to mask writing of the alpha color component.
660 *
661 * \sa glColorMask().
662 *
663 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask.  On a
664 * change, flushes the vertices and notifies the driver via the
665 * dd_function_table::ColorMask callback.
666 */
667void GLAPIENTRY
668_mesa_ColorMask( GLboolean red, GLboolean green,
669                 GLboolean blue, GLboolean alpha )
670{
671   GET_CURRENT_CONTEXT(ctx);
672   GLubyte tmp[4];
673   GLuint i;
674   GLboolean flushed;
675   ASSERT_OUTSIDE_BEGIN_END(ctx);
676
677   if (MESA_VERBOSE & VERBOSE_API)
678      _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
679                  red, green, blue, alpha);
680
681   /* Shouldn't have any information about channel depth in core mesa
682    * -- should probably store these as the native booleans:
683    */
684   tmp[RCOMP] = red    ? 0xff : 0x0;
685   tmp[GCOMP] = green  ? 0xff : 0x0;
686   tmp[BCOMP] = blue   ? 0xff : 0x0;
687   tmp[ACOMP] = alpha  ? 0xff : 0x0;
688
689   flushed = GL_FALSE;
690   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
691      if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
692         if (!flushed) {
693            FLUSH_VERTICES(ctx, _NEW_COLOR);
694         }
695         flushed = GL_TRUE;
696         COPY_4UBV(ctx->Color.ColorMask[i], tmp);
697      }
698   }
699
700   if (ctx->Driver.ColorMask)
701      ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
702}
703
704
705/**
706 * For GL_EXT_draw_buffers2 and GL3
707 */
708void GLAPIENTRY
709_mesa_ColorMaskIndexed( GLuint buf, GLboolean red, GLboolean green,
710                        GLboolean blue, GLboolean alpha )
711{
712   GLubyte tmp[4];
713   GET_CURRENT_CONTEXT(ctx);
714   ASSERT_OUTSIDE_BEGIN_END(ctx);
715
716   if (MESA_VERBOSE & VERBOSE_API)
717      _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
718                  buf, red, green, blue, alpha);
719
720   if (buf >= ctx->Const.MaxDrawBuffers) {
721      _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
722      return;
723   }
724
725   /* Shouldn't have any information about channel depth in core mesa
726    * -- should probably store these as the native booleans:
727    */
728   tmp[RCOMP] = red    ? 0xff : 0x0;
729   tmp[GCOMP] = green  ? 0xff : 0x0;
730   tmp[BCOMP] = blue   ? 0xff : 0x0;
731   tmp[ACOMP] = alpha  ? 0xff : 0x0;
732
733   if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
734      return;
735
736   FLUSH_VERTICES(ctx, _NEW_COLOR);
737   COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
738
739   if (ctx->Driver.ColorMaskIndexed)
740      ctx->Driver.ColorMaskIndexed(ctx, buf, red, green, blue, alpha);
741}
742
743
744void GLAPIENTRY
745_mesa_ClampColorARB(GLenum target, GLenum clamp)
746{
747   GET_CURRENT_CONTEXT(ctx);
748
749   ASSERT_OUTSIDE_BEGIN_END(ctx);
750
751   if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
752      _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
753      return;
754   }
755
756   switch (target) {
757   case GL_CLAMP_VERTEX_COLOR_ARB:
758      FLUSH_VERTICES(ctx, _NEW_LIGHT);
759      ctx->Light.ClampVertexColor = clamp;
760      break;
761   case GL_CLAMP_FRAGMENT_COLOR_ARB:
762      FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
763      ctx->Color.ClampFragmentColor = clamp;
764      break;
765   case GL_CLAMP_READ_COLOR_ARB:
766      FLUSH_VERTICES(ctx, _NEW_COLOR);
767      ctx->Color.ClampReadColor = clamp;
768      break;
769   default:
770      _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(target)");
771      return;
772   }
773}
774
775
776
777
778/**********************************************************************/
779/** \name Initialization */
780/*@{*/
781
782/**
783 * Initialization of the context's Color attribute group.
784 *
785 * \param ctx GL context.
786 *
787 * Initializes the related fields in the context color attribute group,
788 * __struct gl_contextRec::Color.
789 */
790void _mesa_init_color( struct gl_context * ctx )
791{
792   GLuint i;
793
794   /* Color buffer group */
795   ctx->Color.IndexMask = ~0u;
796   memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
797   ctx->Color.ClearIndex = 0;
798   ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
799   ctx->Color.AlphaEnabled = GL_FALSE;
800   ctx->Color.AlphaFunc = GL_ALWAYS;
801   ctx->Color.AlphaRef = 0;
802   ctx->Color.BlendEnabled = 0x0;
803   for (i = 0; i < Elements(ctx->Color.Blend); i++) {
804      ctx->Color.Blend[i].SrcRGB = GL_ONE;
805      ctx->Color.Blend[i].DstRGB = GL_ZERO;
806      ctx->Color.Blend[i].SrcA = GL_ONE;
807      ctx->Color.Blend[i].DstA = GL_ZERO;
808      ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
809      ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
810   }
811   ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
812   ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
813   ctx->Color.IndexLogicOpEnabled = GL_FALSE;
814   ctx->Color.ColorLogicOpEnabled = GL_FALSE;
815   ctx->Color._LogicOpEnabled = GL_FALSE;
816   ctx->Color.LogicOp = GL_COPY;
817   ctx->Color.DitherFlag = GL_TRUE;
818
819   if (ctx->Visual.doubleBufferMode) {
820      ctx->Color.DrawBuffer[0] = GL_BACK;
821   }
822   else {
823      ctx->Color.DrawBuffer[0] = GL_FRONT;
824   }
825
826   ctx->Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
827   ctx->Color._ClampFragmentColor = GL_TRUE;
828   ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
829   ctx->Color._ClampReadColor = GL_TRUE;
830}
831
832/*@}*/
833