09acdf5c588a7ff3b92e92a9b4f66ff8d25a4e4f
[reactos.git] / reactos / dll / opengl / mesa / 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 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 */
76 static GLboolean
77 legal_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 */
107 static GLboolean
108 validate_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 */
152 void 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 */
167 void 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 */
228 void 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 */
238 void 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 */
287 static GLboolean
288 legal_blend_equation(const struct gl_context *ctx, GLenum mode)
289 {
290 switch (mode) {
291 case GL_FUNC_ADD:
292 case GL_FUNC_SUBTRACT:
293 case GL_FUNC_REVERSE_SUBTRACT:
294 return GL_TRUE;
295 case GL_MIN:
296 case GL_MAX:
297 return ctx->Extensions.EXT_blend_minmax;
298 default:
299 return GL_FALSE;
300 }
301 }
302
303
304 /* This is really an extension function! */
305 void GLAPIENTRY
306 _mesa_BlendEquation( GLenum mode )
307 {
308 GLuint buf, numBuffers;
309 GLboolean changed;
310 GET_CURRENT_CONTEXT(ctx);
311 ASSERT_OUTSIDE_BEGIN_END(ctx);
312
313 if (MESA_VERBOSE & VERBOSE_API)
314 _mesa_debug(ctx, "glBlendEquation(%s)\n",
315 _mesa_lookup_enum_by_nr(mode));
316
317 if (!legal_blend_equation(ctx, mode)) {
318 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
319 return;
320 }
321
322 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
323 ? ctx->Const.MaxDrawBuffers : 1;
324
325 changed = GL_FALSE;
326 for (buf = 0; buf < numBuffers; buf++) {
327 if (ctx->Color.Blend[buf].EquationRGB != mode ||
328 ctx->Color.Blend[buf].EquationA != mode) {
329 changed = GL_TRUE;
330 break;
331 }
332 }
333 if (!changed)
334 return;
335
336 FLUSH_VERTICES(ctx, _NEW_COLOR);
337 for (buf = 0; buf < numBuffers; buf++) {
338 ctx->Color.Blend[buf].EquationRGB = mode;
339 ctx->Color.Blend[buf].EquationA = mode;
340 }
341 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
342
343 if (ctx->Driver.BlendEquationSeparate)
344 (*ctx->Driver.BlendEquationSeparate)( ctx, mode, mode );
345 }
346
347
348 /**
349 * Set blend equation for one color buffer/target.
350 */
351 void GLAPIENTRY
352 _mesa_BlendEquationi(GLuint buf, GLenum mode)
353 {
354 GET_CURRENT_CONTEXT(ctx);
355 ASSERT_OUTSIDE_BEGIN_END(ctx);
356
357 if (MESA_VERBOSE & VERBOSE_API)
358 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
359 buf, _mesa_lookup_enum_by_nr(mode));
360
361 if (buf >= ctx->Const.MaxDrawBuffers) {
362 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
363 buf);
364 return;
365 }
366
367 if (!legal_blend_equation(ctx, mode)) {
368 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
369 return;
370 }
371
372 if (ctx->Color.Blend[buf].EquationRGB == mode &&
373 ctx->Color.Blend[buf].EquationA == mode)
374 return; /* no change */
375
376 FLUSH_VERTICES(ctx, _NEW_COLOR);
377 ctx->Color.Blend[buf].EquationRGB = mode;
378 ctx->Color.Blend[buf].EquationA = mode;
379 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
380
381 if (ctx->Driver.BlendEquationSeparatei)
382 ctx->Driver.BlendEquationSeparatei(ctx, buf, mode, mode);
383 }
384
385
386 void GLAPIENTRY
387 _mesa_BlendEquationSeparateEXT( GLenum modeRGB, GLenum modeA )
388 {
389 GLuint buf, numBuffers;
390 GLboolean changed;
391 GET_CURRENT_CONTEXT(ctx);
392 ASSERT_OUTSIDE_BEGIN_END(ctx);
393
394 if (MESA_VERBOSE & VERBOSE_API)
395 _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
396 _mesa_lookup_enum_by_nr(modeRGB),
397 _mesa_lookup_enum_by_nr(modeA));
398
399 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
400 _mesa_error(ctx, GL_INVALID_OPERATION,
401 "glBlendEquationSeparateEXT not supported by driver");
402 return;
403 }
404
405 if (!legal_blend_equation(ctx, modeRGB)) {
406 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
407 return;
408 }
409
410 if (!legal_blend_equation(ctx, modeA)) {
411 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
412 return;
413 }
414
415 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
416 ? ctx->Const.MaxDrawBuffers : 1;
417
418 changed = GL_FALSE;
419 for (buf = 0; buf < numBuffers; buf++) {
420 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
421 ctx->Color.Blend[buf].EquationA != modeA) {
422 changed = GL_TRUE;
423 break;
424 }
425 }
426 if (!changed)
427 return;
428
429 FLUSH_VERTICES(ctx, _NEW_COLOR);
430 for (buf = 0; buf < numBuffers; buf++) {
431 ctx->Color.Blend[buf].EquationRGB = modeRGB;
432 ctx->Color.Blend[buf].EquationA = modeA;
433 }
434 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
435
436 if (ctx->Driver.BlendEquationSeparate)
437 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
438 }
439
440
441 /**
442 * Set separate blend equations for one color buffer/target.
443 */
444 void GLAPIENTRY
445 _mesa_BlendEquationSeparatei(GLuint buf, GLenum modeRGB, GLenum modeA)
446 {
447 GET_CURRENT_CONTEXT(ctx);
448 ASSERT_OUTSIDE_BEGIN_END(ctx);
449
450 if (MESA_VERBOSE & VERBOSE_API)
451 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
452 _mesa_lookup_enum_by_nr(modeRGB),
453 _mesa_lookup_enum_by_nr(modeA));
454
455 if (buf >= ctx->Const.MaxDrawBuffers) {
456 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
457 buf);
458 return;
459 }
460
461 if (!legal_blend_equation(ctx, modeRGB)) {
462 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
463 return;
464 }
465
466 if (!legal_blend_equation(ctx, modeA)) {
467 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
468 return;
469 }
470
471 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
472 ctx->Color.Blend[buf].EquationA == modeA)
473 return; /* no change */
474
475 FLUSH_VERTICES(ctx, _NEW_COLOR);
476 ctx->Color.Blend[buf].EquationRGB = modeRGB;
477 ctx->Color.Blend[buf].EquationA = modeA;
478 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
479
480 if (ctx->Driver.BlendEquationSeparatei)
481 ctx->Driver.BlendEquationSeparatei(ctx, buf, modeRGB, modeA);
482 }
483
484
485
486 #endif /* _HAVE_FULL_GL */
487
488
489 /**
490 * Set the blending color.
491 *
492 * \param red red color component.
493 * \param green green color component.
494 * \param blue blue color component.
495 * \param alpha alpha color component.
496 *
497 * \sa glBlendColor().
498 *
499 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
500 * change, flushes the vertices and notifies the driver via
501 * dd_function_table::BlendColor callback.
502 */
503 void GLAPIENTRY
504 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
505 {
506 GLfloat tmp[4];
507 GET_CURRENT_CONTEXT(ctx);
508 ASSERT_OUTSIDE_BEGIN_END(ctx);
509
510 tmp[0] = red;
511 tmp[1] = green;
512 tmp[2] = blue;
513 tmp[3] = alpha;
514
515 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
516 return;
517
518 FLUSH_VERTICES(ctx, _NEW_COLOR);
519 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
520
521 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
522 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
523 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
524 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
525
526 if (ctx->Driver.BlendColor)
527 (*ctx->Driver.BlendColor)(ctx, ctx->Color.BlendColor);
528 }
529
530
531 /**
532 * Specify the alpha test function.
533 *
534 * \param func alpha comparison function.
535 * \param ref reference value.
536 *
537 * Verifies the parameters and updates gl_colorbuffer_attrib.
538 * On a change, flushes the vertices and notifies the driver via
539 * dd_function_table::AlphaFunc callback.
540 */
541 void GLAPIENTRY
542 _mesa_AlphaFunc( GLenum func, GLclampf ref )
543 {
544 GET_CURRENT_CONTEXT(ctx);
545 ASSERT_OUTSIDE_BEGIN_END(ctx);
546
547 if (MESA_VERBOSE & VERBOSE_API)
548 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
549 _mesa_lookup_enum_by_nr(func), ref);
550
551 switch (func) {
552 case GL_NEVER:
553 case GL_LESS:
554 case GL_EQUAL:
555 case GL_LEQUAL:
556 case GL_GREATER:
557 case GL_NOTEQUAL:
558 case GL_GEQUAL:
559 case GL_ALWAYS:
560 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
561 return; /* no change */
562
563 FLUSH_VERTICES(ctx, _NEW_COLOR);
564 ctx->Color.AlphaFunc = func;
565 ctx->Color.AlphaRefUnclamped = ref;
566 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
567
568 if (ctx->Driver.AlphaFunc)
569 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
570 return;
571
572 default:
573 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
574 return;
575 }
576 }
577
578
579 /**
580 * Specify a logic pixel operation for color index rendering.
581 *
582 * \param opcode operation.
583 *
584 * Verifies that \p opcode is a valid enum and updates
585 gl_colorbuffer_attrib::LogicOp.
586 * On a change, flushes the vertices and notifies the driver via the
587 * dd_function_table::LogicOpcode callback.
588 */
589 void GLAPIENTRY
590 _mesa_LogicOp( GLenum opcode )
591 {
592 GET_CURRENT_CONTEXT(ctx);
593 ASSERT_OUTSIDE_BEGIN_END(ctx);
594
595 if (MESA_VERBOSE & VERBOSE_API)
596 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_lookup_enum_by_nr(opcode));
597
598 switch (opcode) {
599 case GL_CLEAR:
600 case GL_SET:
601 case GL_COPY:
602 case GL_COPY_INVERTED:
603 case GL_NOOP:
604 case GL_INVERT:
605 case GL_AND:
606 case GL_NAND:
607 case GL_OR:
608 case GL_NOR:
609 case GL_XOR:
610 case GL_EQUIV:
611 case GL_AND_REVERSE:
612 case GL_AND_INVERTED:
613 case GL_OR_REVERSE:
614 case GL_OR_INVERTED:
615 break;
616 default:
617 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
618 return;
619 }
620
621 if (ctx->Color.LogicOp == opcode)
622 return;
623
624 FLUSH_VERTICES(ctx, _NEW_COLOR);
625 ctx->Color.LogicOp = opcode;
626
627 if (ctx->Driver.LogicOpcode)
628 ctx->Driver.LogicOpcode( ctx, opcode );
629 }
630
631 #if _HAVE_FULL_GL
632 void GLAPIENTRY
633 _mesa_IndexMask( GLuint mask )
634 {
635 GET_CURRENT_CONTEXT(ctx);
636 ASSERT_OUTSIDE_BEGIN_END(ctx);
637
638 if (ctx->Color.IndexMask == mask)
639 return;
640
641 FLUSH_VERTICES(ctx, _NEW_COLOR);
642 ctx->Color.IndexMask = mask;
643 }
644 #endif
645
646
647 /**
648 * Enable or disable writing of frame buffer color components.
649 *
650 * \param red whether to mask writing of the red color component.
651 * \param green whether to mask writing of the green color component.
652 * \param blue whether to mask writing of the blue color component.
653 * \param alpha whether to mask writing of the alpha color component.
654 *
655 * \sa glColorMask().
656 *
657 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
658 * change, flushes the vertices and notifies the driver via the
659 * dd_function_table::ColorMask callback.
660 */
661 void GLAPIENTRY
662 _mesa_ColorMask( GLboolean red, GLboolean green,
663 GLboolean blue, GLboolean alpha )
664 {
665 GET_CURRENT_CONTEXT(ctx);
666 GLubyte tmp[4];
667 GLuint i;
668 GLboolean flushed;
669 ASSERT_OUTSIDE_BEGIN_END(ctx);
670
671 if (MESA_VERBOSE & VERBOSE_API)
672 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
673 red, green, blue, alpha);
674
675 /* Shouldn't have any information about channel depth in core mesa
676 * -- should probably store these as the native booleans:
677 */
678 tmp[RCOMP] = red ? 0xff : 0x0;
679 tmp[GCOMP] = green ? 0xff : 0x0;
680 tmp[BCOMP] = blue ? 0xff : 0x0;
681 tmp[ACOMP] = alpha ? 0xff : 0x0;
682
683 flushed = GL_FALSE;
684 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
685 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
686 if (!flushed) {
687 FLUSH_VERTICES(ctx, _NEW_COLOR);
688 }
689 flushed = GL_TRUE;
690 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
691 }
692 }
693
694 if (ctx->Driver.ColorMask)
695 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
696 }
697
698
699 /**
700 * For GL_EXT_draw_buffers2 and GL3
701 */
702 void GLAPIENTRY
703 _mesa_ColorMaskIndexed( GLuint buf, GLboolean red, GLboolean green,
704 GLboolean blue, GLboolean alpha )
705 {
706 GLubyte tmp[4];
707 GET_CURRENT_CONTEXT(ctx);
708 ASSERT_OUTSIDE_BEGIN_END(ctx);
709
710 if (MESA_VERBOSE & VERBOSE_API)
711 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
712 buf, red, green, blue, alpha);
713
714 if (buf >= ctx->Const.MaxDrawBuffers) {
715 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
716 return;
717 }
718
719 /* Shouldn't have any information about channel depth in core mesa
720 * -- should probably store these as the native booleans:
721 */
722 tmp[RCOMP] = red ? 0xff : 0x0;
723 tmp[GCOMP] = green ? 0xff : 0x0;
724 tmp[BCOMP] = blue ? 0xff : 0x0;
725 tmp[ACOMP] = alpha ? 0xff : 0x0;
726
727 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
728 return;
729
730 FLUSH_VERTICES(ctx, _NEW_COLOR);
731 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
732
733 if (ctx->Driver.ColorMaskIndexed)
734 ctx->Driver.ColorMaskIndexed(ctx, buf, red, green, blue, alpha);
735 }
736
737
738 void GLAPIENTRY
739 _mesa_ClampColorARB(GLenum target, GLenum clamp)
740 {
741 GET_CURRENT_CONTEXT(ctx);
742
743 ASSERT_OUTSIDE_BEGIN_END(ctx);
744
745 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
746 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
747 return;
748 }
749
750 switch (target) {
751 case GL_CLAMP_VERTEX_COLOR_ARB:
752 FLUSH_VERTICES(ctx, _NEW_LIGHT);
753 ctx->Light.ClampVertexColor = clamp;
754 break;
755 case GL_CLAMP_FRAGMENT_COLOR_ARB:
756 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
757 ctx->Color.ClampFragmentColor = clamp;
758 break;
759 case GL_CLAMP_READ_COLOR_ARB:
760 FLUSH_VERTICES(ctx, _NEW_COLOR);
761 ctx->Color.ClampReadColor = clamp;
762 break;
763 default:
764 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(target)");
765 return;
766 }
767 }
768
769
770
771
772 /**********************************************************************/
773 /** \name Initialization */
774 /*@{*/
775
776 /**
777 * Initialization of the context's Color attribute group.
778 *
779 * \param ctx GL context.
780 *
781 * Initializes the related fields in the context color attribute group,
782 * __struct gl_contextRec::Color.
783 */
784 void _mesa_init_color( struct gl_context * ctx )
785 {
786 GLuint i;
787
788 /* Color buffer group */
789 ctx->Color.IndexMask = ~0u;
790 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
791 ctx->Color.ClearIndex = 0;
792 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
793 ctx->Color.AlphaEnabled = GL_FALSE;
794 ctx->Color.AlphaFunc = GL_ALWAYS;
795 ctx->Color.AlphaRef = 0;
796 ctx->Color.BlendEnabled = 0x0;
797 for (i = 0; i < Elements(ctx->Color.Blend); i++) {
798 ctx->Color.Blend[i].SrcRGB = GL_ONE;
799 ctx->Color.Blend[i].DstRGB = GL_ZERO;
800 ctx->Color.Blend[i].SrcA = GL_ONE;
801 ctx->Color.Blend[i].DstA = GL_ZERO;
802 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
803 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
804 }
805 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
806 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
807 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
808 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
809 ctx->Color.LogicOp = GL_COPY;
810 ctx->Color.DitherFlag = GL_TRUE;
811
812 if (ctx->Visual.doubleBufferMode) {
813 ctx->Color.DrawBuffer[0] = GL_BACK;
814 }
815 else {
816 ctx->Color.DrawBuffer[0] = GL_FRONT;
817 }
818
819 ctx->Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
820 ctx->Color._ClampFragmentColor = GL_TRUE;
821 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
822 ctx->Color._ClampReadColor = GL_TRUE;
823 }
824
825 /*@}*/