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