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