OSDN Git Service

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