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