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