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