mesa: replace gl_framebuffer::_IntegerColor wih _IntegerBuffers
[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 ctx->API != API_OPENGLES
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 (ctx->API != API_OPENGLES
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 ctx->API != API_OPENGLES
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 /**
195 * Return the number of per-buffer blend states to update in
196 * glBlendFunc, glBlendFuncSeparate, glBlendEquation, etc.
197 */
198 static inline unsigned
199 num_buffers(const struct gl_context *ctx)
200 {
201 return ctx->Extensions.ARB_draw_buffers_blend
202 ? ctx->Const.MaxDrawBuffers : 1;
203 }
204
205
206 /**
207 * Set the separate blend source/dest factors for all draw buffers.
208 *
209 * \param sfactorRGB RGB source factor operator.
210 * \param dfactorRGB RGB destination factor operator.
211 * \param sfactorA alpha source factor operator.
212 * \param dfactorA alpha destination factor operator.
213 */
214 void GLAPIENTRY
215 _mesa_BlendFuncSeparate( GLenum sfactorRGB, GLenum dfactorRGB,
216 GLenum sfactorA, GLenum dfactorA )
217 {
218 GET_CURRENT_CONTEXT(ctx);
219 const unsigned numBuffers = num_buffers(ctx);
220 unsigned buf;
221 bool changed = false;
222
223 if (MESA_VERBOSE & VERBOSE_API)
224 _mesa_debug(ctx, "glBlendFuncSeparate %s %s %s %s\n",
225 _mesa_enum_to_string(sfactorRGB),
226 _mesa_enum_to_string(dfactorRGB),
227 _mesa_enum_to_string(sfactorA),
228 _mesa_enum_to_string(dfactorA));
229
230 /* Check if we're really changing any state. If not, return early. */
231 if (ctx->Color._BlendFuncPerBuffer) {
232 /* Check all per-buffer states */
233 for (buf = 0; buf < numBuffers; buf++) {
234 if (ctx->Color.Blend[buf].SrcRGB != sfactorRGB ||
235 ctx->Color.Blend[buf].DstRGB != dfactorRGB ||
236 ctx->Color.Blend[buf].SrcA != sfactorA ||
237 ctx->Color.Blend[buf].DstA != dfactorA) {
238 changed = true;
239 break;
240 }
241 }
242 }
243 else {
244 /* only need to check 0th per-buffer state */
245 if (ctx->Color.Blend[0].SrcRGB != sfactorRGB ||
246 ctx->Color.Blend[0].DstRGB != dfactorRGB ||
247 ctx->Color.Blend[0].SrcA != sfactorA ||
248 ctx->Color.Blend[0].DstA != dfactorA) {
249 changed = true;
250 }
251 }
252
253 if (!changed)
254 return;
255
256 if (!validate_blend_factors(ctx, "glBlendFuncSeparate",
257 sfactorRGB, dfactorRGB,
258 sfactorA, dfactorA)) {
259 return;
260 }
261
262 FLUSH_VERTICES(ctx, _NEW_COLOR);
263
264 for (buf = 0; buf < numBuffers; buf++) {
265 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
266 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
267 ctx->Color.Blend[buf].SrcA = sfactorA;
268 ctx->Color.Blend[buf].DstA = dfactorA;
269 }
270
271 update_uses_dual_src(ctx, 0);
272 for (buf = 1; buf < numBuffers; buf++) {
273 ctx->Color.Blend[buf]._UsesDualSrc = ctx->Color.Blend[0]._UsesDualSrc;
274 }
275
276 ctx->Color._BlendFuncPerBuffer = GL_FALSE;
277
278 if (ctx->Driver.BlendFuncSeparate) {
279 ctx->Driver.BlendFuncSeparate(ctx, sfactorRGB, dfactorRGB,
280 sfactorA, dfactorA);
281 }
282 }
283
284
285 /**
286 * Set blend source/dest factors for one color buffer/target.
287 */
288 void GLAPIENTRY
289 _mesa_BlendFunciARB(GLuint buf, GLenum sfactor, GLenum dfactor)
290 {
291 _mesa_BlendFuncSeparateiARB(buf, sfactor, dfactor, sfactor, dfactor);
292 }
293
294
295 /**
296 * Set separate blend source/dest factors for one color buffer/target.
297 */
298 void GLAPIENTRY
299 _mesa_BlendFuncSeparateiARB(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
300 GLenum sfactorA, GLenum dfactorA)
301 {
302 GET_CURRENT_CONTEXT(ctx);
303
304 if (!ctx->Extensions.ARB_draw_buffers_blend) {
305 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
306 return;
307 }
308
309 if (buf >= ctx->Const.MaxDrawBuffers) {
310 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
311 buf);
312 return;
313 }
314
315 if (ctx->Color.Blend[buf].SrcRGB == sfactorRGB &&
316 ctx->Color.Blend[buf].DstRGB == dfactorRGB &&
317 ctx->Color.Blend[buf].SrcA == sfactorA &&
318 ctx->Color.Blend[buf].DstA == dfactorA)
319 return; /* no change */
320
321 if (!validate_blend_factors(ctx, "glBlendFuncSeparatei",
322 sfactorRGB, dfactorRGB,
323 sfactorA, dfactorA)) {
324 return;
325 }
326
327 FLUSH_VERTICES(ctx, _NEW_COLOR);
328
329 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
330 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
331 ctx->Color.Blend[buf].SrcA = sfactorA;
332 ctx->Color.Blend[buf].DstA = dfactorA;
333 update_uses_dual_src(ctx, buf);
334 ctx->Color._BlendFuncPerBuffer = GL_TRUE;
335 }
336
337
338 /**
339 * Return true if \p mode is a legal blending equation, excluding
340 * GL_KHR_blend_equation_advanced modes.
341 */
342 static bool
343 legal_simple_blend_equation(const struct gl_context *ctx, GLenum mode)
344 {
345 switch (mode) {
346 case GL_FUNC_ADD:
347 case GL_FUNC_SUBTRACT:
348 case GL_FUNC_REVERSE_SUBTRACT:
349 return GL_TRUE;
350 case GL_MIN:
351 case GL_MAX:
352 return ctx->Extensions.EXT_blend_minmax;
353 default:
354 return GL_FALSE;
355 }
356 }
357
358 static enum gl_advanced_blend_mode
359 advanced_blend_mode_from_gl_enum(GLenum mode)
360 {
361 switch (mode) {
362 case GL_MULTIPLY_KHR:
363 return BLEND_MULTIPLY;
364 case GL_SCREEN_KHR:
365 return BLEND_SCREEN;
366 case GL_OVERLAY_KHR:
367 return BLEND_OVERLAY;
368 case GL_DARKEN_KHR:
369 return BLEND_DARKEN;
370 case GL_LIGHTEN_KHR:
371 return BLEND_LIGHTEN;
372 case GL_COLORDODGE_KHR:
373 return BLEND_COLORDODGE;
374 case GL_COLORBURN_KHR:
375 return BLEND_COLORBURN;
376 case GL_HARDLIGHT_KHR:
377 return BLEND_HARDLIGHT;
378 case GL_SOFTLIGHT_KHR:
379 return BLEND_SOFTLIGHT;
380 case GL_DIFFERENCE_KHR:
381 return BLEND_DIFFERENCE;
382 case GL_EXCLUSION_KHR:
383 return BLEND_EXCLUSION;
384 case GL_HSL_HUE_KHR:
385 return BLEND_HSL_HUE;
386 case GL_HSL_SATURATION_KHR:
387 return BLEND_HSL_SATURATION;
388 case GL_HSL_COLOR_KHR:
389 return BLEND_HSL_COLOR;
390 case GL_HSL_LUMINOSITY_KHR:
391 return BLEND_HSL_LUMINOSITY;
392 default:
393 return BLEND_NONE;
394 }
395 }
396
397 /**
398 * If \p mode is one of the advanced blending equations defined by
399 * GL_KHR_blend_equation_advanced (and the extension is supported),
400 * return the corresponding BLEND_* enum. Otherwise, return BLEND_NONE
401 * (which can also be treated as false).
402 */
403 static enum gl_advanced_blend_mode
404 advanced_blend_mode(const struct gl_context *ctx, GLenum mode)
405 {
406 return _mesa_has_KHR_blend_equation_advanced(ctx) ?
407 advanced_blend_mode_from_gl_enum(mode) : BLEND_NONE;
408 }
409
410 /* This is really an extension function! */
411 void GLAPIENTRY
412 _mesa_BlendEquation( GLenum mode )
413 {
414 GET_CURRENT_CONTEXT(ctx);
415 const unsigned numBuffers = num_buffers(ctx);
416 unsigned buf;
417 bool changed = false;
418 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
419
420 if (MESA_VERBOSE & VERBOSE_API)
421 _mesa_debug(ctx, "glBlendEquation(%s)\n",
422 _mesa_enum_to_string(mode));
423
424 if (ctx->Color._BlendEquationPerBuffer) {
425 /* Check all per-buffer states */
426 for (buf = 0; buf < numBuffers; buf++) {
427 if (ctx->Color.Blend[buf].EquationRGB != mode ||
428 ctx->Color.Blend[buf].EquationA != mode) {
429 changed = true;
430 break;
431 }
432 }
433 }
434 else {
435 /* only need to check 0th per-buffer state */
436 if (ctx->Color.Blend[0].EquationRGB != mode ||
437 ctx->Color.Blend[0].EquationA != mode) {
438 changed = true;
439 }
440 }
441
442 if (!changed)
443 return;
444
445
446 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
447 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
448 return;
449 }
450
451 FLUSH_VERTICES(ctx, _NEW_COLOR);
452
453 for (buf = 0; buf < numBuffers; buf++) {
454 ctx->Color.Blend[buf].EquationRGB = mode;
455 ctx->Color.Blend[buf].EquationA = mode;
456 }
457 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
458 ctx->Color._AdvancedBlendMode = advanced_mode;
459
460 if (ctx->Driver.BlendEquationSeparate)
461 ctx->Driver.BlendEquationSeparate(ctx, mode, mode);
462 }
463
464
465 /**
466 * Set blend equation for one color buffer/target.
467 */
468 void GLAPIENTRY
469 _mesa_BlendEquationiARB(GLuint buf, GLenum mode)
470 {
471 GET_CURRENT_CONTEXT(ctx);
472 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
473
474 if (MESA_VERBOSE & VERBOSE_API)
475 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
476 buf, _mesa_enum_to_string(mode));
477
478 if (buf >= ctx->Const.MaxDrawBuffers) {
479 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationi(buffer=%u)",
480 buf);
481 return;
482 }
483
484 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
485 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
486 return;
487 }
488
489 if (ctx->Color.Blend[buf].EquationRGB == mode &&
490 ctx->Color.Blend[buf].EquationA == mode)
491 return; /* no change */
492
493 FLUSH_VERTICES(ctx, _NEW_COLOR);
494 ctx->Color.Blend[buf].EquationRGB = mode;
495 ctx->Color.Blend[buf].EquationA = mode;
496 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
497
498 if (buf == 0)
499 ctx->Color._AdvancedBlendMode = advanced_mode;
500 }
501
502
503 void GLAPIENTRY
504 _mesa_BlendEquationSeparate( GLenum modeRGB, GLenum modeA )
505 {
506 GET_CURRENT_CONTEXT(ctx);
507 const unsigned numBuffers = num_buffers(ctx);
508 unsigned buf;
509 bool changed = false;
510
511 if (MESA_VERBOSE & VERBOSE_API)
512 _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
513 _mesa_enum_to_string(modeRGB),
514 _mesa_enum_to_string(modeA));
515
516 if (ctx->Color._BlendEquationPerBuffer) {
517 /* Check all per-buffer states */
518 for (buf = 0; buf < numBuffers; buf++) {
519 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
520 ctx->Color.Blend[buf].EquationA != modeA) {
521 changed = true;
522 break;
523 }
524 }
525 }
526 else {
527 /* only need to check 0th per-buffer state */
528 if (ctx->Color.Blend[0].EquationRGB != modeRGB ||
529 ctx->Color.Blend[0].EquationA != modeA) {
530 changed = true;
531 }
532 }
533
534 if (!changed)
535 return;
536
537 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
538 _mesa_error(ctx, GL_INVALID_OPERATION,
539 "glBlendEquationSeparateEXT not supported by driver");
540 return;
541 }
542
543 /* Only allow simple blending equations.
544 * The GL_KHR_blend_equation_advanced spec says:
545 *
546 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
547 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
548 */
549 if (!legal_simple_blend_equation(ctx, modeRGB)) {
550 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
551 return;
552 }
553
554 if (!legal_simple_blend_equation(ctx, modeA)) {
555 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
556 return;
557 }
558
559 FLUSH_VERTICES(ctx, _NEW_COLOR);
560
561 for (buf = 0; buf < numBuffers; buf++) {
562 ctx->Color.Blend[buf].EquationRGB = modeRGB;
563 ctx->Color.Blend[buf].EquationA = modeA;
564 }
565 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
566 ctx->Color._AdvancedBlendMode = BLEND_NONE;
567
568 if (ctx->Driver.BlendEquationSeparate)
569 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
570 }
571
572
573 /**
574 * Set separate blend equations for one color buffer/target.
575 */
576 void GLAPIENTRY
577 _mesa_BlendEquationSeparateiARB(GLuint buf, GLenum modeRGB, GLenum modeA)
578 {
579 GET_CURRENT_CONTEXT(ctx);
580
581 if (MESA_VERBOSE & VERBOSE_API)
582 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
583 _mesa_enum_to_string(modeRGB),
584 _mesa_enum_to_string(modeA));
585
586 if (buf >= ctx->Const.MaxDrawBuffers) {
587 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
588 buf);
589 return;
590 }
591
592 /* Only allow simple blending equations.
593 * The GL_KHR_blend_equation_advanced spec says:
594 *
595 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
596 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
597 */
598 if (!legal_simple_blend_equation(ctx, modeRGB)) {
599 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
600 return;
601 }
602
603 if (!legal_simple_blend_equation(ctx, modeA)) {
604 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
605 return;
606 }
607
608 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
609 ctx->Color.Blend[buf].EquationA == modeA)
610 return; /* no change */
611
612 FLUSH_VERTICES(ctx, _NEW_COLOR);
613 ctx->Color.Blend[buf].EquationRGB = modeRGB;
614 ctx->Color.Blend[buf].EquationA = modeA;
615 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
616 ctx->Color._AdvancedBlendMode = BLEND_NONE;
617 }
618
619
620 /**
621 * Set the blending color.
622 *
623 * \param red red color component.
624 * \param green green color component.
625 * \param blue blue color component.
626 * \param alpha alpha color component.
627 *
628 * \sa glBlendColor().
629 *
630 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
631 * change, flushes the vertices and notifies the driver via
632 * dd_function_table::BlendColor callback.
633 */
634 void GLAPIENTRY
635 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
636 {
637 GLfloat tmp[4];
638 GET_CURRENT_CONTEXT(ctx);
639
640 tmp[0] = red;
641 tmp[1] = green;
642 tmp[2] = blue;
643 tmp[3] = alpha;
644
645 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
646 return;
647
648 FLUSH_VERTICES(ctx, _NEW_COLOR);
649 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
650
651 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
652 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
653 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
654 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
655
656 if (ctx->Driver.BlendColor)
657 ctx->Driver.BlendColor(ctx, ctx->Color.BlendColor);
658 }
659
660
661 /**
662 * Specify the alpha test function.
663 *
664 * \param func alpha comparison function.
665 * \param ref reference value.
666 *
667 * Verifies the parameters and updates gl_colorbuffer_attrib.
668 * On a change, flushes the vertices and notifies the driver via
669 * dd_function_table::AlphaFunc callback.
670 */
671 void GLAPIENTRY
672 _mesa_AlphaFunc( GLenum func, GLclampf ref )
673 {
674 GET_CURRENT_CONTEXT(ctx);
675
676 if (MESA_VERBOSE & VERBOSE_API)
677 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
678 _mesa_enum_to_string(func), ref);
679
680 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
681 return; /* no change */
682
683 switch (func) {
684 case GL_NEVER:
685 case GL_LESS:
686 case GL_EQUAL:
687 case GL_LEQUAL:
688 case GL_GREATER:
689 case GL_NOTEQUAL:
690 case GL_GEQUAL:
691 case GL_ALWAYS:
692 FLUSH_VERTICES(ctx, _NEW_COLOR);
693 ctx->Color.AlphaFunc = func;
694 ctx->Color.AlphaRefUnclamped = ref;
695 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
696
697 if (ctx->Driver.AlphaFunc)
698 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
699 return;
700
701 default:
702 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
703 return;
704 }
705 }
706
707
708 /**
709 * Specify a logic pixel operation for color index rendering.
710 *
711 * \param opcode operation.
712 *
713 * Verifies that \p opcode is a valid enum and updates
714 * gl_colorbuffer_attrib::LogicOp.
715 * On a change, flushes the vertices and notifies the driver via the
716 * dd_function_table::LogicOpcode callback.
717 */
718 void GLAPIENTRY
719 _mesa_LogicOp( GLenum opcode )
720 {
721 GET_CURRENT_CONTEXT(ctx);
722
723 if (MESA_VERBOSE & VERBOSE_API)
724 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode));
725
726 switch (opcode) {
727 case GL_CLEAR:
728 case GL_SET:
729 case GL_COPY:
730 case GL_COPY_INVERTED:
731 case GL_NOOP:
732 case GL_INVERT:
733 case GL_AND:
734 case GL_NAND:
735 case GL_OR:
736 case GL_NOR:
737 case GL_XOR:
738 case GL_EQUIV:
739 case GL_AND_REVERSE:
740 case GL_AND_INVERTED:
741 case GL_OR_REVERSE:
742 case GL_OR_INVERTED:
743 break;
744 default:
745 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
746 return;
747 }
748
749 if (ctx->Color.LogicOp == opcode)
750 return;
751
752 FLUSH_VERTICES(ctx, _NEW_COLOR);
753 ctx->Color.LogicOp = opcode;
754
755 if (ctx->Driver.LogicOpcode)
756 ctx->Driver.LogicOpcode( ctx, opcode );
757 }
758
759
760 void GLAPIENTRY
761 _mesa_IndexMask( GLuint mask )
762 {
763 GET_CURRENT_CONTEXT(ctx);
764
765 if (ctx->Color.IndexMask == mask)
766 return;
767
768 FLUSH_VERTICES(ctx, _NEW_COLOR);
769 ctx->Color.IndexMask = mask;
770 }
771
772
773 /**
774 * Enable or disable writing of frame buffer color components.
775 *
776 * \param red whether to mask writing of the red color component.
777 * \param green whether to mask writing of the green color component.
778 * \param blue whether to mask writing of the blue color component.
779 * \param alpha whether to mask writing of the alpha color component.
780 *
781 * \sa glColorMask().
782 *
783 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
784 * change, flushes the vertices and notifies the driver via the
785 * dd_function_table::ColorMask callback.
786 */
787 void GLAPIENTRY
788 _mesa_ColorMask( GLboolean red, GLboolean green,
789 GLboolean blue, GLboolean alpha )
790 {
791 GET_CURRENT_CONTEXT(ctx);
792 GLubyte tmp[4];
793 GLuint i;
794 GLboolean flushed;
795
796 if (MESA_VERBOSE & VERBOSE_API)
797 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
798 red, green, blue, alpha);
799
800 /* Shouldn't have any information about channel depth in core mesa
801 * -- should probably store these as the native booleans:
802 */
803 tmp[RCOMP] = red ? 0xff : 0x0;
804 tmp[GCOMP] = green ? 0xff : 0x0;
805 tmp[BCOMP] = blue ? 0xff : 0x0;
806 tmp[ACOMP] = alpha ? 0xff : 0x0;
807
808 flushed = GL_FALSE;
809 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
810 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
811 if (!flushed) {
812 FLUSH_VERTICES(ctx, _NEW_COLOR);
813 }
814 flushed = GL_TRUE;
815 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
816 }
817 }
818
819 if (ctx->Driver.ColorMask)
820 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
821 }
822
823
824 /**
825 * For GL_EXT_draw_buffers2 and GL3
826 */
827 void GLAPIENTRY
828 _mesa_ColorMaski( GLuint buf, GLboolean red, GLboolean green,
829 GLboolean blue, GLboolean alpha )
830 {
831 GLubyte tmp[4];
832 GET_CURRENT_CONTEXT(ctx);
833
834 if (MESA_VERBOSE & VERBOSE_API)
835 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
836 buf, red, green, blue, alpha);
837
838 if (buf >= ctx->Const.MaxDrawBuffers) {
839 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
840 return;
841 }
842
843 /* Shouldn't have any information about channel depth in core mesa
844 * -- should probably store these as the native booleans:
845 */
846 tmp[RCOMP] = red ? 0xff : 0x0;
847 tmp[GCOMP] = green ? 0xff : 0x0;
848 tmp[BCOMP] = blue ? 0xff : 0x0;
849 tmp[ACOMP] = alpha ? 0xff : 0x0;
850
851 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
852 return;
853
854 FLUSH_VERTICES(ctx, _NEW_COLOR);
855 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
856 }
857
858
859 void GLAPIENTRY
860 _mesa_ClampColor(GLenum target, GLenum clamp)
861 {
862 GET_CURRENT_CONTEXT(ctx);
863
864 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
865 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
866 return;
867 }
868
869 switch (target) {
870 case GL_CLAMP_VERTEX_COLOR_ARB:
871 if (ctx->API == API_OPENGL_CORE &&
872 !ctx->Extensions.ARB_color_buffer_float) {
873 goto invalid_enum;
874 }
875 FLUSH_VERTICES(ctx, _NEW_LIGHT);
876 ctx->Light.ClampVertexColor = clamp;
877 _mesa_update_clamp_vertex_color(ctx, ctx->DrawBuffer);
878 break;
879 case GL_CLAMP_FRAGMENT_COLOR_ARB:
880 if (ctx->API == API_OPENGL_CORE &&
881 !ctx->Extensions.ARB_color_buffer_float) {
882 goto invalid_enum;
883 }
884 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
885 ctx->Color.ClampFragmentColor = clamp;
886 _mesa_update_clamp_fragment_color(ctx, ctx->DrawBuffer);
887 break;
888 case GL_CLAMP_READ_COLOR_ARB:
889 ctx->Color.ClampReadColor = clamp;
890 break;
891 default:
892 goto invalid_enum;
893 }
894 return;
895
896 invalid_enum:
897 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColor(%s)",
898 _mesa_enum_to_string(target));
899 }
900
901 static GLboolean
902 get_clamp_color(const struct gl_framebuffer *fb, GLenum clamp)
903 {
904 if (clamp == GL_TRUE || clamp == GL_FALSE)
905 return clamp;
906
907 assert(clamp == GL_FIXED_ONLY);
908 if (!fb)
909 return GL_TRUE;
910
911 return fb->_AllColorBuffersFixedPoint;
912 }
913
914 GLboolean
915 _mesa_get_clamp_fragment_color(const struct gl_context *ctx,
916 const struct gl_framebuffer *drawFb)
917 {
918 return get_clamp_color(drawFb, ctx->Color.ClampFragmentColor);
919 }
920
921 GLboolean
922 _mesa_get_clamp_vertex_color(const struct gl_context *ctx,
923 const struct gl_framebuffer *drawFb)
924 {
925 return get_clamp_color(drawFb, ctx->Light.ClampVertexColor);
926 }
927
928 GLboolean
929 _mesa_get_clamp_read_color(const struct gl_context *ctx,
930 const struct gl_framebuffer *readFb)
931 {
932 return get_clamp_color(readFb, ctx->Color.ClampReadColor);
933 }
934
935 /**
936 * Update the ctx->Color._ClampFragmentColor field
937 */
938 void
939 _mesa_update_clamp_fragment_color(struct gl_context *ctx,
940 const struct gl_framebuffer *drawFb)
941 {
942 /* Don't clamp if:
943 * - there is no colorbuffer
944 * - all colorbuffers are unsigned normalized, so clamping has no effect
945 * - there is an integer colorbuffer
946 */
947 if (!drawFb || !drawFb->_HasSNormOrFloatColorBuffer ||
948 drawFb->_IntegerBuffers)
949 ctx->Color._ClampFragmentColor = GL_FALSE;
950 else
951 ctx->Color._ClampFragmentColor =
952 _mesa_get_clamp_fragment_color(ctx, drawFb);
953 }
954
955 /**
956 * Update the ctx->Color._ClampVertexColor field
957 */
958 void
959 _mesa_update_clamp_vertex_color(struct gl_context *ctx,
960 const struct gl_framebuffer *drawFb)
961 {
962 ctx->Light._ClampVertexColor =
963 _mesa_get_clamp_vertex_color(ctx, drawFb);
964 }
965
966 /**
967 * Returns an appropriate mesa_format for color rendering based on the
968 * GL_FRAMEBUFFER_SRGB state.
969 *
970 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
971 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
972 * overriding the format of the surface. This is a helper for doing the
973 * surface format override variant.
974 */
975 mesa_format
976 _mesa_get_render_format(const struct gl_context *ctx, mesa_format format)
977 {
978 if (ctx->Color.sRGBEnabled)
979 return format;
980 else
981 return _mesa_get_srgb_format_linear(format);
982 }
983
984 /**********************************************************************/
985 /** \name Initialization */
986 /*@{*/
987
988 /**
989 * Initialization of the context's Color attribute group.
990 *
991 * \param ctx GL context.
992 *
993 * Initializes the related fields in the context color attribute group,
994 * __struct gl_contextRec::Color.
995 */
996 void _mesa_init_color( struct gl_context * ctx )
997 {
998 GLuint i;
999
1000 /* Color buffer group */
1001 ctx->Color.IndexMask = ~0u;
1002 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
1003 ctx->Color.ClearIndex = 0;
1004 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
1005 ctx->Color.AlphaEnabled = GL_FALSE;
1006 ctx->Color.AlphaFunc = GL_ALWAYS;
1007 ctx->Color.AlphaRef = 0;
1008 ctx->Color.BlendEnabled = 0x0;
1009 for (i = 0; i < ARRAY_SIZE(ctx->Color.Blend); i++) {
1010 ctx->Color.Blend[i].SrcRGB = GL_ONE;
1011 ctx->Color.Blend[i].DstRGB = GL_ZERO;
1012 ctx->Color.Blend[i].SrcA = GL_ONE;
1013 ctx->Color.Blend[i].DstA = GL_ZERO;
1014 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
1015 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
1016 }
1017 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
1018 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
1019 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
1020 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
1021 ctx->Color.LogicOp = GL_COPY;
1022 ctx->Color.DitherFlag = GL_TRUE;
1023
1024 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1025 * the front or the back buffer depending on the config */
1026 if (ctx->Visual.doubleBufferMode || _mesa_is_gles(ctx)) {
1027 ctx->Color.DrawBuffer[0] = GL_BACK;
1028 }
1029 else {
1030 ctx->Color.DrawBuffer[0] = GL_FRONT;
1031 }
1032
1033 ctx->Color.ClampFragmentColor = ctx->API == API_OPENGL_COMPAT ?
1034 GL_FIXED_ONLY_ARB : GL_FALSE;
1035 ctx->Color._ClampFragmentColor = GL_FALSE;
1036 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
1037
1038 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1039 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1040 */
1041 ctx->Color.sRGBEnabled = _mesa_is_gles(ctx);
1042
1043 ctx->Color.BlendCoherent = true;
1044 }
1045
1046 /*@}*/