r300c: Unbreak after R4xx support was added to r300/compiler.
[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 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,
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! */
311 void 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 */
357 void 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
392 void 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 */
450 void
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 */
509 void 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] = CLAMP( red, 0.0F, 1.0F );
517 tmp[1] = CLAMP( green, 0.0F, 1.0F );
518 tmp[2] = CLAMP( blue, 0.0F, 1.0F );
519 tmp[3] = CLAMP( alpha, 0.0F, 1.0F );
520
521 if (TEST_EQ_4V(tmp, ctx->Color.BlendColor))
522 return;
523
524 FLUSH_VERTICES(ctx, _NEW_COLOR);
525 COPY_4FV( ctx->Color.BlendColor, tmp );
526
527 if (ctx->Driver.BlendColor)
528 (*ctx->Driver.BlendColor)(ctx, tmp);
529 }
530
531
532 /**
533 * Specify the alpha test function.
534 *
535 * \param func alpha comparison function.
536 * \param ref reference value.
537 *
538 * Verifies the parameters and updates gl_colorbuffer_attrib.
539 * On a change, flushes the vertices and notifies the driver via
540 * dd_function_table::AlphaFunc callback.
541 */
542 void GLAPIENTRY
543 _mesa_AlphaFunc( GLenum func, GLclampf ref )
544 {
545 GET_CURRENT_CONTEXT(ctx);
546 ASSERT_OUTSIDE_BEGIN_END(ctx);
547
548 switch (func) {
549 case GL_NEVER:
550 case GL_LESS:
551 case GL_EQUAL:
552 case GL_LEQUAL:
553 case GL_GREATER:
554 case GL_NOTEQUAL:
555 case GL_GEQUAL:
556 case GL_ALWAYS:
557 ref = CLAMP(ref, 0.0F, 1.0F);
558
559 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRef == ref)
560 return; /* no change */
561
562 FLUSH_VERTICES(ctx, _NEW_COLOR);
563 ctx->Color.AlphaFunc = func;
564 ctx->Color.AlphaRef = ref;
565
566 if (ctx->Driver.AlphaFunc)
567 ctx->Driver.AlphaFunc(ctx, func, ref);
568 return;
569
570 default:
571 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
572 return;
573 }
574 }
575
576
577 /**
578 * Specify a logic pixel operation for color index rendering.
579 *
580 * \param opcode operation.
581 *
582 * Verifies that \p opcode is a valid enum and updates
583 gl_colorbuffer_attrib::LogicOp.
584 * On a change, flushes the vertices and notifies the driver via the
585 * dd_function_table::LogicOpcode callback.
586 */
587 void GLAPIENTRY
588 _mesa_LogicOp( GLenum opcode )
589 {
590 GET_CURRENT_CONTEXT(ctx);
591 ASSERT_OUTSIDE_BEGIN_END(ctx);
592
593 switch (opcode) {
594 case GL_CLEAR:
595 case GL_SET:
596 case GL_COPY:
597 case GL_COPY_INVERTED:
598 case GL_NOOP:
599 case GL_INVERT:
600 case GL_AND:
601 case GL_NAND:
602 case GL_OR:
603 case GL_NOR:
604 case GL_XOR:
605 case GL_EQUIV:
606 case GL_AND_REVERSE:
607 case GL_AND_INVERTED:
608 case GL_OR_REVERSE:
609 case GL_OR_INVERTED:
610 break;
611 default:
612 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
613 return;
614 }
615
616 if (ctx->Color.LogicOp == opcode)
617 return;
618
619 FLUSH_VERTICES(ctx, _NEW_COLOR);
620 ctx->Color.LogicOp = opcode;
621
622 if (ctx->Driver.LogicOpcode)
623 ctx->Driver.LogicOpcode( ctx, opcode );
624 }
625
626 #if _HAVE_FULL_GL
627 void GLAPIENTRY
628 _mesa_IndexMask( GLuint mask )
629 {
630 GET_CURRENT_CONTEXT(ctx);
631 ASSERT_OUTSIDE_BEGIN_END(ctx);
632
633 if (ctx->Color.IndexMask == mask)
634 return;
635
636 FLUSH_VERTICES(ctx, _NEW_COLOR);
637 ctx->Color.IndexMask = mask;
638 }
639 #endif
640
641
642 /**
643 * Enable or disable writing of frame buffer color components.
644 *
645 * \param red whether to mask writing of the red color component.
646 * \param green whether to mask writing of the green color component.
647 * \param blue whether to mask writing of the blue color component.
648 * \param alpha whether to mask writing of the alpha color component.
649 *
650 * \sa glColorMask().
651 *
652 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
653 * change, flushes the vertices and notifies the driver via the
654 * dd_function_table::ColorMask callback.
655 */
656 void GLAPIENTRY
657 _mesa_ColorMask( GLboolean red, GLboolean green,
658 GLboolean blue, GLboolean alpha )
659 {
660 GET_CURRENT_CONTEXT(ctx);
661 GLubyte tmp[4];
662 GLuint i;
663 GLboolean flushed;
664 ASSERT_OUTSIDE_BEGIN_END(ctx);
665
666 if (MESA_VERBOSE & VERBOSE_API)
667 _mesa_debug(ctx, "glColorMask %d %d %d %d\n", red, green, blue, alpha);
668
669 /* Shouldn't have any information about channel depth in core mesa
670 * -- should probably store these as the native booleans:
671 */
672 tmp[RCOMP] = red ? 0xff : 0x0;
673 tmp[GCOMP] = green ? 0xff : 0x0;
674 tmp[BCOMP] = blue ? 0xff : 0x0;
675 tmp[ACOMP] = alpha ? 0xff : 0x0;
676
677 flushed = GL_FALSE;
678 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
679 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
680 if (!flushed) {
681 FLUSH_VERTICES(ctx, _NEW_COLOR);
682 }
683 flushed = GL_TRUE;
684 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
685 }
686 }
687
688 if (ctx->Driver.ColorMask)
689 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
690 }
691
692
693 /**
694 * For GL_EXT_draw_buffers2 and GL3
695 */
696 void GLAPIENTRY
697 _mesa_ColorMaskIndexed( GLuint buf, GLboolean red, GLboolean green,
698 GLboolean blue, GLboolean alpha )
699 {
700 GLubyte tmp[4];
701 GET_CURRENT_CONTEXT(ctx);
702 ASSERT_OUTSIDE_BEGIN_END(ctx);
703
704 if (MESA_VERBOSE & VERBOSE_API)
705 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
706 buf, red, green, blue, alpha);
707
708 if (buf >= ctx->Const.MaxDrawBuffers) {
709 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
710 return;
711 }
712
713 /* Shouldn't have any information about channel depth in core mesa
714 * -- should probably store these as the native booleans:
715 */
716 tmp[RCOMP] = red ? 0xff : 0x0;
717 tmp[GCOMP] = green ? 0xff : 0x0;
718 tmp[BCOMP] = blue ? 0xff : 0x0;
719 tmp[ACOMP] = alpha ? 0xff : 0x0;
720
721 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
722 return;
723
724 FLUSH_VERTICES(ctx, _NEW_COLOR);
725 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
726
727 if (ctx->Driver.ColorMaskIndexed)
728 ctx->Driver.ColorMaskIndexed(ctx, buf, red, green, blue, alpha);
729 }
730
731
732 extern void GLAPIENTRY
733 _mesa_ClampColorARB(GLenum target, GLenum clamp)
734 {
735 GET_CURRENT_CONTEXT(ctx);
736
737 ASSERT_OUTSIDE_BEGIN_END(ctx);
738
739 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
740 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
741 return;
742 }
743
744 switch (target) {
745 case GL_CLAMP_VERTEX_COLOR_ARB:
746 ctx->Light.ClampVertexColor = clamp;
747 break;
748 case GL_CLAMP_FRAGMENT_COLOR_ARB:
749 ctx->Color.ClampFragmentColor = clamp;
750 break;
751 case GL_CLAMP_READ_COLOR_ARB:
752 ctx->Color.ClampReadColor = clamp;
753 break;
754 default:
755 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(target)");
756 return;
757 }
758 }
759
760
761
762
763 /**********************************************************************/
764 /** \name Initialization */
765 /*@{*/
766
767 /**
768 * Initialization of the context's Color attribute group.
769 *
770 * \param ctx GL context.
771 *
772 * Initializes the related fields in the context color attribute group,
773 * __struct gl_contextRec::Color.
774 */
775 void _mesa_init_color( struct gl_context * ctx )
776 {
777 GLuint i;
778
779 /* Color buffer group */
780 ctx->Color.IndexMask = ~0u;
781 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
782 ctx->Color.ClearIndex = 0;
783 ASSIGN_4V( ctx->Color.ClearColor, 0, 0, 0, 0 );
784 ctx->Color.AlphaEnabled = GL_FALSE;
785 ctx->Color.AlphaFunc = GL_ALWAYS;
786 ctx->Color.AlphaRef = 0;
787 ctx->Color.BlendEnabled = 0x0;
788 for (i = 0; i < Elements(ctx->Color.Blend); i++) {
789 ctx->Color.Blend[i].SrcRGB = GL_ONE;
790 ctx->Color.Blend[i].DstRGB = GL_ZERO;
791 ctx->Color.Blend[i].SrcA = GL_ONE;
792 ctx->Color.Blend[i].DstA = GL_ZERO;
793 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
794 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
795 }
796 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
797 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
798 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
799 ctx->Color._LogicOpEnabled = GL_FALSE;
800 ctx->Color.LogicOp = GL_COPY;
801 ctx->Color.DitherFlag = GL_TRUE;
802
803 if (ctx->Visual.doubleBufferMode) {
804 ctx->Color.DrawBuffer[0] = GL_BACK;
805 }
806 else {
807 ctx->Color.DrawBuffer[0] = GL_FRONT;
808 }
809
810 ctx->Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
811 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
812 }
813
814 /*@}*/