mesa: remove #if _HAVE_FULL_GL checks
[mesa.git] / src / mesa / main / blend.c
1 /**
2 * \file blend.c
3 * Blending operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 6.5.1
9 *
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31
32 #include "glheader.h"
33 #include "blend.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "macros.h"
37 #include "mtypes.h"
38
39
40
41 /**
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
44 */
45 static GLboolean
46 legal_src_factor(const struct gl_context *ctx, GLenum factor)
47 {
48 switch (factor) {
49 case GL_SRC_COLOR:
50 case GL_ONE_MINUS_SRC_COLOR:
51 return ctx->Extensions.NV_blend_square;
52 case GL_ZERO:
53 case GL_ONE:
54 case GL_DST_COLOR:
55 case GL_ONE_MINUS_DST_COLOR:
56 case GL_SRC_ALPHA:
57 case GL_ONE_MINUS_SRC_ALPHA:
58 case GL_DST_ALPHA:
59 case GL_ONE_MINUS_DST_ALPHA:
60 case GL_SRC_ALPHA_SATURATE:
61 return GL_TRUE;
62 case GL_CONSTANT_COLOR:
63 case GL_ONE_MINUS_CONSTANT_COLOR:
64 case GL_CONSTANT_ALPHA:
65 case GL_ONE_MINUS_CONSTANT_ALPHA:
66 return _mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES2;
67 case GL_SRC1_COLOR:
68 case GL_SRC1_ALPHA:
69 case GL_ONE_MINUS_SRC1_COLOR:
70 case GL_ONE_MINUS_SRC1_ALPHA:
71 return _mesa_is_desktop_gl(ctx)
72 && ctx->Extensions.ARB_blend_func_extended;
73 default:
74 return GL_FALSE;
75 }
76 }
77
78
79 /**
80 * Check if given blend destination factor is legal.
81 * \return GL_TRUE if legal, GL_FALSE otherwise.
82 */
83 static GLboolean
84 legal_dst_factor(const struct gl_context *ctx, GLenum factor)
85 {
86 switch (factor) {
87 case GL_DST_COLOR:
88 case GL_ONE_MINUS_DST_COLOR:
89 return ctx->Extensions.NV_blend_square;
90 case GL_ZERO:
91 case GL_ONE:
92 case GL_SRC_COLOR:
93 case GL_ONE_MINUS_SRC_COLOR:
94 case GL_SRC_ALPHA:
95 case GL_ONE_MINUS_SRC_ALPHA:
96 case GL_DST_ALPHA:
97 case GL_ONE_MINUS_DST_ALPHA:
98 return GL_TRUE;
99 case GL_CONSTANT_COLOR:
100 case GL_ONE_MINUS_CONSTANT_COLOR:
101 case GL_CONSTANT_ALPHA:
102 case GL_ONE_MINUS_CONSTANT_ALPHA:
103 return _mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES2;
104 case GL_SRC_ALPHA_SATURATE:
105 return (_mesa_is_desktop_gl(ctx)
106 && ctx->Extensions.ARB_blend_func_extended)
107 || _mesa_is_gles3(ctx);
108 case GL_SRC1_COLOR:
109 case GL_SRC1_ALPHA:
110 case GL_ONE_MINUS_SRC1_COLOR:
111 case GL_ONE_MINUS_SRC1_ALPHA:
112 return _mesa_is_desktop_gl(ctx)
113 && ctx->Extensions.ARB_blend_func_extended;
114 default:
115 return GL_FALSE;
116 }
117 }
118
119
120 /**
121 * Check if src/dest RGB/A blend factors are legal. If not generate
122 * a GL error.
123 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
124 */
125 static GLboolean
126 validate_blend_factors(struct gl_context *ctx, const char *func,
127 GLenum sfactorRGB, GLenum dfactorRGB,
128 GLenum sfactorA, GLenum dfactorA)
129 {
130 if (!legal_src_factor(ctx, sfactorRGB)) {
131 _mesa_error(ctx, GL_INVALID_ENUM,
132 "%s(sfactorRGB = %s)", func,
133 _mesa_lookup_enum_by_nr(sfactorRGB));
134 return GL_FALSE;
135 }
136
137 if (!legal_dst_factor(ctx, dfactorRGB)) {
138 _mesa_error(ctx, GL_INVALID_ENUM,
139 "%s(dfactorRGB = %s)", func,
140 _mesa_lookup_enum_by_nr(dfactorRGB));
141 return GL_FALSE;
142 }
143
144 if (sfactorA != sfactorRGB && !legal_src_factor(ctx, sfactorA)) {
145 _mesa_error(ctx, GL_INVALID_ENUM,
146 "%s(sfactorA = %s)", func,
147 _mesa_lookup_enum_by_nr(sfactorA));
148 return GL_FALSE;
149 }
150
151 if (dfactorA != dfactorRGB && !legal_dst_factor(ctx, dfactorA)) {
152 _mesa_error(ctx, GL_INVALID_ENUM,
153 "%s(dfactorA = %s)", func,
154 _mesa_lookup_enum_by_nr(dfactorA));
155 return GL_FALSE;
156 }
157
158 return GL_TRUE;
159 }
160
161
162 /**
163 * Specify the blending operation.
164 *
165 * \param sfactor source factor operator.
166 * \param dfactor destination factor operator.
167 *
168 * \sa glBlendFunc, glBlendFuncSeparateEXT
169 */
170 void GLAPIENTRY
171 _mesa_BlendFunc( GLenum sfactor, GLenum dfactor )
172 {
173 _mesa_BlendFuncSeparateEXT(sfactor, dfactor, sfactor, dfactor);
174 }
175
176 static GLboolean
177 blend_factor_is_dual_src(GLenum factor)
178 {
179 return (factor == GL_SRC1_COLOR ||
180 factor == GL_SRC1_ALPHA ||
181 factor == GL_ONE_MINUS_SRC1_COLOR ||
182 factor == GL_ONE_MINUS_SRC1_ALPHA);
183 }
184
185 static void
186 update_uses_dual_src(struct gl_context *ctx, int buf)
187 {
188 ctx->Color.Blend[buf]._UsesDualSrc =
189 (blend_factor_is_dual_src(ctx->Color.Blend[buf].SrcRGB) ||
190 blend_factor_is_dual_src(ctx->Color.Blend[buf].DstRGB) ||
191 blend_factor_is_dual_src(ctx->Color.Blend[buf].SrcA) ||
192 blend_factor_is_dual_src(ctx->Color.Blend[buf].DstA));
193 }
194
195 /**
196 * Set the separate blend source/dest factors for all draw buffers.
197 *
198 * \param sfactorRGB RGB source factor operator.
199 * \param dfactorRGB RGB destination factor operator.
200 * \param sfactorA alpha source factor operator.
201 * \param dfactorA alpha destination factor operator.
202 */
203 void GLAPIENTRY
204 _mesa_BlendFuncSeparateEXT( GLenum sfactorRGB, GLenum dfactorRGB,
205 GLenum sfactorA, GLenum dfactorA )
206 {
207 GLuint buf, numBuffers;
208 GLboolean changed;
209 GET_CURRENT_CONTEXT(ctx);
210 ASSERT_OUTSIDE_BEGIN_END(ctx);
211
212 if (MESA_VERBOSE & VERBOSE_API)
213 _mesa_debug(ctx, "glBlendFuncSeparate %s %s %s %s\n",
214 _mesa_lookup_enum_by_nr(sfactorRGB),
215 _mesa_lookup_enum_by_nr(dfactorRGB),
216 _mesa_lookup_enum_by_nr(sfactorA),
217 _mesa_lookup_enum_by_nr(dfactorA));
218
219 if (!validate_blend_factors(ctx, "glBlendFuncSeparate",
220 sfactorRGB, dfactorRGB,
221 sfactorA, dfactorA)) {
222 return;
223 }
224
225 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
226 ? ctx->Const.MaxDrawBuffers : 1;
227
228 changed = GL_FALSE;
229 for (buf = 0; buf < numBuffers; buf++) {
230 if (ctx->Color.Blend[buf].SrcRGB != sfactorRGB ||
231 ctx->Color.Blend[buf].DstRGB != dfactorRGB ||
232 ctx->Color.Blend[buf].SrcA != sfactorA ||
233 ctx->Color.Blend[buf].DstA != dfactorA) {
234 changed = GL_TRUE;
235 break;
236 }
237 }
238 if (!changed)
239 return;
240
241 FLUSH_VERTICES(ctx, _NEW_COLOR);
242
243 for (buf = 0; buf < numBuffers; buf++) {
244 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
245 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
246 ctx->Color.Blend[buf].SrcA = sfactorA;
247 ctx->Color.Blend[buf].DstA = dfactorA;
248 update_uses_dual_src(ctx, buf);
249 }
250 ctx->Color._BlendFuncPerBuffer = GL_FALSE;
251
252 if (ctx->Driver.BlendFuncSeparate) {
253 ctx->Driver.BlendFuncSeparate(ctx, sfactorRGB, dfactorRGB,
254 sfactorA, dfactorA);
255 }
256 }
257
258
259 /**
260 * Set blend source/dest factors for one color buffer/target.
261 */
262 void GLAPIENTRY
263 _mesa_BlendFunci(GLuint buf, GLenum sfactor, GLenum dfactor)
264 {
265 _mesa_BlendFuncSeparatei(buf, sfactor, dfactor, sfactor, dfactor);
266 }
267
268
269 /**
270 * Set separate blend source/dest factors for one color buffer/target.
271 */
272 void GLAPIENTRY
273 _mesa_BlendFuncSeparatei(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
274 GLenum sfactorA, GLenum dfactorA)
275 {
276 GET_CURRENT_CONTEXT(ctx);
277 ASSERT_OUTSIDE_BEGIN_END(ctx);
278
279 if (!ctx->Extensions.ARB_draw_buffers_blend) {
280 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
281 return;
282 }
283
284 if (buf >= ctx->Const.MaxDrawBuffers) {
285 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
286 buf);
287 return;
288 }
289
290 if (!validate_blend_factors(ctx, "glBlendFuncSeparatei",
291 sfactorRGB, dfactorRGB,
292 sfactorA, dfactorA)) {
293 return;
294 }
295
296 if (ctx->Color.Blend[buf].SrcRGB == sfactorRGB &&
297 ctx->Color.Blend[buf].DstRGB == dfactorRGB &&
298 ctx->Color.Blend[buf].SrcA == sfactorA &&
299 ctx->Color.Blend[buf].DstA == dfactorA)
300 return; /* no change */
301
302 FLUSH_VERTICES(ctx, _NEW_COLOR);
303
304 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
305 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
306 ctx->Color.Blend[buf].SrcA = sfactorA;
307 ctx->Color.Blend[buf].DstA = dfactorA;
308 update_uses_dual_src(ctx, buf);
309 ctx->Color._BlendFuncPerBuffer = GL_TRUE;
310
311 if (ctx->Driver.BlendFuncSeparatei) {
312 ctx->Driver.BlendFuncSeparatei(ctx, buf, sfactorRGB, dfactorRGB,
313 sfactorA, dfactorA);
314 }
315 }
316
317
318 /**
319 * Check if given blend equation is legal.
320 * \return GL_TRUE if legal, GL_FALSE otherwise.
321 */
322 static GLboolean
323 legal_blend_equation(const struct gl_context *ctx, GLenum mode)
324 {
325 switch (mode) {
326 case GL_FUNC_ADD:
327 case GL_FUNC_SUBTRACT:
328 case GL_FUNC_REVERSE_SUBTRACT:
329 return GL_TRUE;
330 case GL_MIN:
331 case GL_MAX:
332 return ctx->Extensions.EXT_blend_minmax;
333 default:
334 return GL_FALSE;
335 }
336 }
337
338
339 /* This is really an extension function! */
340 void GLAPIENTRY
341 _mesa_BlendEquation( GLenum mode )
342 {
343 GLuint buf, numBuffers;
344 GLboolean changed;
345 GET_CURRENT_CONTEXT(ctx);
346 ASSERT_OUTSIDE_BEGIN_END(ctx);
347
348 if (MESA_VERBOSE & VERBOSE_API)
349 _mesa_debug(ctx, "glBlendEquation(%s)\n",
350 _mesa_lookup_enum_by_nr(mode));
351
352 if (!legal_blend_equation(ctx, mode)) {
353 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
354 return;
355 }
356
357 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
358 ? ctx->Const.MaxDrawBuffers : 1;
359
360 changed = GL_FALSE;
361 for (buf = 0; buf < numBuffers; buf++) {
362 if (ctx->Color.Blend[buf].EquationRGB != mode ||
363 ctx->Color.Blend[buf].EquationA != mode) {
364 changed = GL_TRUE;
365 break;
366 }
367 }
368 if (!changed)
369 return;
370
371 FLUSH_VERTICES(ctx, _NEW_COLOR);
372 for (buf = 0; buf < numBuffers; buf++) {
373 ctx->Color.Blend[buf].EquationRGB = mode;
374 ctx->Color.Blend[buf].EquationA = mode;
375 }
376 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
377
378 if (ctx->Driver.BlendEquationSeparate)
379 (*ctx->Driver.BlendEquationSeparate)( ctx, mode, mode );
380 }
381
382
383 /**
384 * Set blend equation for one color buffer/target.
385 */
386 void GLAPIENTRY
387 _mesa_BlendEquationi(GLuint buf, GLenum mode)
388 {
389 GET_CURRENT_CONTEXT(ctx);
390 ASSERT_OUTSIDE_BEGIN_END(ctx);
391
392 if (MESA_VERBOSE & VERBOSE_API)
393 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
394 buf, _mesa_lookup_enum_by_nr(mode));
395
396 if (buf >= ctx->Const.MaxDrawBuffers) {
397 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
398 buf);
399 return;
400 }
401
402 if (!legal_blend_equation(ctx, mode)) {
403 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
404 return;
405 }
406
407 if (ctx->Color.Blend[buf].EquationRGB == mode &&
408 ctx->Color.Blend[buf].EquationA == mode)
409 return; /* no change */
410
411 FLUSH_VERTICES(ctx, _NEW_COLOR);
412 ctx->Color.Blend[buf].EquationRGB = mode;
413 ctx->Color.Blend[buf].EquationA = mode;
414 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
415
416 if (ctx->Driver.BlendEquationSeparatei)
417 ctx->Driver.BlendEquationSeparatei(ctx, buf, mode, mode);
418 }
419
420
421 void GLAPIENTRY
422 _mesa_BlendEquationSeparateEXT( GLenum modeRGB, GLenum modeA )
423 {
424 GLuint buf, numBuffers;
425 GLboolean changed;
426 GET_CURRENT_CONTEXT(ctx);
427 ASSERT_OUTSIDE_BEGIN_END(ctx);
428
429 if (MESA_VERBOSE & VERBOSE_API)
430 _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
431 _mesa_lookup_enum_by_nr(modeRGB),
432 _mesa_lookup_enum_by_nr(modeA));
433
434 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
435 _mesa_error(ctx, GL_INVALID_OPERATION,
436 "glBlendEquationSeparateEXT not supported by driver");
437 return;
438 }
439
440 if (!legal_blend_equation(ctx, modeRGB)) {
441 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
442 return;
443 }
444
445 if (!legal_blend_equation(ctx, modeA)) {
446 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
447 return;
448 }
449
450 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
451 ? ctx->Const.MaxDrawBuffers : 1;
452
453 changed = GL_FALSE;
454 for (buf = 0; buf < numBuffers; buf++) {
455 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
456 ctx->Color.Blend[buf].EquationA != modeA) {
457 changed = GL_TRUE;
458 break;
459 }
460 }
461 if (!changed)
462 return;
463
464 FLUSH_VERTICES(ctx, _NEW_COLOR);
465 for (buf = 0; buf < numBuffers; buf++) {
466 ctx->Color.Blend[buf].EquationRGB = modeRGB;
467 ctx->Color.Blend[buf].EquationA = modeA;
468 }
469 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
470
471 if (ctx->Driver.BlendEquationSeparate)
472 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
473 }
474
475
476 /**
477 * Set separate blend equations for one color buffer/target.
478 */
479 void GLAPIENTRY
480 _mesa_BlendEquationSeparatei(GLuint buf, GLenum modeRGB, GLenum modeA)
481 {
482 GET_CURRENT_CONTEXT(ctx);
483 ASSERT_OUTSIDE_BEGIN_END(ctx);
484
485 if (MESA_VERBOSE & VERBOSE_API)
486 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
487 _mesa_lookup_enum_by_nr(modeRGB),
488 _mesa_lookup_enum_by_nr(modeA));
489
490 if (buf >= ctx->Const.MaxDrawBuffers) {
491 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
492 buf);
493 return;
494 }
495
496 if (!legal_blend_equation(ctx, modeRGB)) {
497 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
498 return;
499 }
500
501 if (!legal_blend_equation(ctx, modeA)) {
502 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
503 return;
504 }
505
506 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
507 ctx->Color.Blend[buf].EquationA == modeA)
508 return; /* no change */
509
510 FLUSH_VERTICES(ctx, _NEW_COLOR);
511 ctx->Color.Blend[buf].EquationRGB = modeRGB;
512 ctx->Color.Blend[buf].EquationA = modeA;
513 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
514
515 if (ctx->Driver.BlendEquationSeparatei)
516 ctx->Driver.BlendEquationSeparatei(ctx, buf, modeRGB, modeA);
517 }
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
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
676
677 /**
678 * Enable or disable writing of frame buffer color components.
679 *
680 * \param red whether to mask writing of the red color component.
681 * \param green whether to mask writing of the green color component.
682 * \param blue whether to mask writing of the blue color component.
683 * \param alpha whether to mask writing of the alpha color component.
684 *
685 * \sa glColorMask().
686 *
687 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
688 * change, flushes the vertices and notifies the driver via the
689 * dd_function_table::ColorMask callback.
690 */
691 void GLAPIENTRY
692 _mesa_ColorMask( GLboolean red, GLboolean green,
693 GLboolean blue, GLboolean alpha )
694 {
695 GET_CURRENT_CONTEXT(ctx);
696 GLubyte tmp[4];
697 GLuint i;
698 GLboolean flushed;
699 ASSERT_OUTSIDE_BEGIN_END(ctx);
700
701 if (MESA_VERBOSE & VERBOSE_API)
702 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
703 red, green, blue, alpha);
704
705 /* Shouldn't have any information about channel depth in core mesa
706 * -- should probably store these as the native booleans:
707 */
708 tmp[RCOMP] = red ? 0xff : 0x0;
709 tmp[GCOMP] = green ? 0xff : 0x0;
710 tmp[BCOMP] = blue ? 0xff : 0x0;
711 tmp[ACOMP] = alpha ? 0xff : 0x0;
712
713 flushed = GL_FALSE;
714 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
715 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
716 if (!flushed) {
717 FLUSH_VERTICES(ctx, _NEW_COLOR);
718 }
719 flushed = GL_TRUE;
720 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
721 }
722 }
723
724 if (ctx->Driver.ColorMask)
725 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
726 }
727
728
729 /**
730 * For GL_EXT_draw_buffers2 and GL3
731 */
732 void GLAPIENTRY
733 _mesa_ColorMaskIndexed( GLuint buf, GLboolean red, GLboolean green,
734 GLboolean blue, GLboolean alpha )
735 {
736 GLubyte tmp[4];
737 GET_CURRENT_CONTEXT(ctx);
738 ASSERT_OUTSIDE_BEGIN_END(ctx);
739
740 if (MESA_VERBOSE & VERBOSE_API)
741 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
742 buf, red, green, blue, alpha);
743
744 if (buf >= ctx->Const.MaxDrawBuffers) {
745 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
746 return;
747 }
748
749 /* Shouldn't have any information about channel depth in core mesa
750 * -- should probably store these as the native booleans:
751 */
752 tmp[RCOMP] = red ? 0xff : 0x0;
753 tmp[GCOMP] = green ? 0xff : 0x0;
754 tmp[BCOMP] = blue ? 0xff : 0x0;
755 tmp[ACOMP] = alpha ? 0xff : 0x0;
756
757 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
758 return;
759
760 FLUSH_VERTICES(ctx, _NEW_COLOR);
761 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
762
763 if (ctx->Driver.ColorMaskIndexed)
764 ctx->Driver.ColorMaskIndexed(ctx, buf, red, green, blue, alpha);
765 }
766
767
768 void GLAPIENTRY
769 _mesa_ClampColorARB(GLenum target, GLenum clamp)
770 {
771 GET_CURRENT_CONTEXT(ctx);
772
773 ASSERT_OUTSIDE_BEGIN_END(ctx);
774
775 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
776 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
777 return;
778 }
779
780 switch (target) {
781 case GL_CLAMP_VERTEX_COLOR_ARB:
782 FLUSH_VERTICES(ctx, _NEW_LIGHT);
783 ctx->Light.ClampVertexColor = clamp;
784 break;
785 case GL_CLAMP_FRAGMENT_COLOR_ARB:
786 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
787 ctx->Color.ClampFragmentColor = clamp;
788 break;
789 case GL_CLAMP_READ_COLOR_ARB:
790 FLUSH_VERTICES(ctx, _NEW_COLOR);
791 ctx->Color.ClampReadColor = clamp;
792 break;
793 default:
794 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(target)");
795 return;
796 }
797 }
798
799
800
801
802 /**********************************************************************/
803 /** \name Initialization */
804 /*@{*/
805
806 /**
807 * Initialization of the context's Color attribute group.
808 *
809 * \param ctx GL context.
810 *
811 * Initializes the related fields in the context color attribute group,
812 * __struct gl_contextRec::Color.
813 */
814 void _mesa_init_color( struct gl_context * ctx )
815 {
816 GLuint i;
817
818 /* Color buffer group */
819 ctx->Color.IndexMask = ~0u;
820 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
821 ctx->Color.ClearIndex = 0;
822 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
823 ctx->Color.AlphaEnabled = GL_FALSE;
824 ctx->Color.AlphaFunc = GL_ALWAYS;
825 ctx->Color.AlphaRef = 0;
826 ctx->Color.BlendEnabled = 0x0;
827 for (i = 0; i < Elements(ctx->Color.Blend); i++) {
828 ctx->Color.Blend[i].SrcRGB = GL_ONE;
829 ctx->Color.Blend[i].DstRGB = GL_ZERO;
830 ctx->Color.Blend[i].SrcA = GL_ONE;
831 ctx->Color.Blend[i].DstA = GL_ZERO;
832 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
833 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
834 }
835 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
836 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
837 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
838 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
839 ctx->Color.LogicOp = GL_COPY;
840 ctx->Color.DitherFlag = GL_TRUE;
841
842 if (ctx->Visual.doubleBufferMode) {
843 ctx->Color.DrawBuffer[0] = GL_BACK;
844 }
845 else {
846 ctx->Color.DrawBuffer[0] = GL_FRONT;
847 }
848
849 ctx->Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
850 ctx->Color._ClampFragmentColor = GL_TRUE;
851 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
852 ctx->Color._ClampReadColor = GL_TRUE;
853
854 if (ctx->API == API_OPENGLES2) {
855 /* GLES 3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled. */
856 ctx->Color.sRGBEnabled = GL_TRUE;
857 } else {
858 ctx->Color.sRGBEnabled = GL_FALSE;
859 }
860 }
861
862 /*@}*/