7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
43 * Check if given blend source factor is legal.
44 * \return GL_TRUE if legal, GL_FALSE otherwise.
47 legal_src_factor(const struct gl_context
*ctx
, GLenum factor
)
51 case GL_ONE_MINUS_SRC_COLOR
:
55 case GL_ONE_MINUS_DST_COLOR
:
57 case GL_ONE_MINUS_SRC_ALPHA
:
59 case GL_ONE_MINUS_DST_ALPHA
:
60 case GL_SRC_ALPHA_SATURATE
:
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
;
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
;
80 * Check if given blend destination factor is legal.
81 * \return GL_TRUE if legal, GL_FALSE otherwise.
84 legal_dst_factor(const struct gl_context
*ctx
, GLenum factor
)
88 case GL_ONE_MINUS_DST_COLOR
:
92 case GL_ONE_MINUS_SRC_COLOR
:
94 case GL_ONE_MINUS_SRC_ALPHA
:
96 case GL_ONE_MINUS_DST_ALPHA
:
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
);
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
;
120 * Check if src/dest RGB/A blend factors are legal. If not generate
122 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
125 validate_blend_factors(struct gl_context
*ctx
, const char *func
,
126 GLenum sfactorRGB
, GLenum dfactorRGB
,
127 GLenum sfactorA
, GLenum dfactorA
)
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
));
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
));
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
));
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
));
162 blend_factor_is_dual_src(GLenum factor
)
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
);
171 update_uses_dual_src(struct gl_context
*ctx
, int buf
)
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
));
182 * Return the number of per-buffer blend states to update in
183 * glBlendFunc, glBlendFuncSeparate, glBlendEquation, etc.
185 static inline unsigned
186 num_buffers(const struct gl_context
*ctx
)
188 return ctx
->Extensions
.ARB_draw_buffers_blend
189 ? ctx
->Const
.MaxDrawBuffers
: 1;
193 /* Returns true if there was no change */
195 skip_blend_state_update(const struct gl_context
*ctx
,
196 GLenum sfactorRGB
, GLenum dfactorRGB
,
197 GLenum sfactorA
, GLenum dfactorA
)
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
);
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
) {
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
) {
228 blend_func_separate(struct gl_context
*ctx
,
229 GLenum sfactorRGB
, GLenum dfactorRGB
,
230 GLenum sfactorA
, GLenum dfactorA
)
232 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlend
? 0 : _NEW_COLOR
);
233 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlend
;
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
;
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
;
248 ctx
->Color
._BlendFuncPerBuffer
= GL_FALSE
;
250 if (ctx
->Driver
.BlendFuncSeparate
) {
251 ctx
->Driver
.BlendFuncSeparate(ctx
, sfactorRGB
, dfactorRGB
,
258 * Specify the blending operation.
260 * \param sfactor source factor operator.
261 * \param dfactor destination factor operator.
263 * \sa glBlendFunc, glBlendFuncSeparateEXT
266 _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
268 GET_CURRENT_CONTEXT(ctx
);
270 if (skip_blend_state_update(ctx
, sfactor
, dfactor
, sfactor
, dfactor
))
273 if (!validate_blend_factors(ctx
, "glBlendFunc",
274 sfactor
, dfactor
, sfactor
, dfactor
)) {
278 blend_func_separate(ctx
, sfactor
, dfactor
, sfactor
, dfactor
);
283 _mesa_BlendFunc_no_error(GLenum sfactor
, GLenum dfactor
)
285 GET_CURRENT_CONTEXT(ctx
);
287 if (skip_blend_state_update(ctx
, sfactor
, dfactor
, sfactor
, dfactor
))
290 blend_func_separate(ctx
, sfactor
, dfactor
, sfactor
, dfactor
);
295 * Set the separate blend source/dest factors for all draw buffers.
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.
303 _mesa_BlendFuncSeparate( GLenum sfactorRGB
, GLenum dfactorRGB
,
304 GLenum sfactorA
, GLenum dfactorA
)
306 GET_CURRENT_CONTEXT(ctx
);
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
));
317 if (skip_blend_state_update(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
))
320 if (!validate_blend_factors(ctx
, "glBlendFuncSeparate",
321 sfactorRGB
, dfactorRGB
,
322 sfactorA
, dfactorA
)) {
326 blend_func_separate(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
);
331 _mesa_BlendFuncSeparate_no_error(GLenum sfactorRGB
, GLenum dfactorRGB
,
332 GLenum sfactorA
, GLenum dfactorA
)
334 GET_CURRENT_CONTEXT(ctx
);
336 if (skip_blend_state_update(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
))
339 blend_func_separate(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
);
344 _mesa_BlendFunciARB_no_error(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
346 _mesa_BlendFuncSeparateiARB_no_error(buf
, sfactor
, dfactor
, sfactor
,
352 * Set blend source/dest factors for one color buffer/target.
355 _mesa_BlendFunciARB(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
357 _mesa_BlendFuncSeparateiARB(buf
, sfactor
, dfactor
, sfactor
, dfactor
);
361 static ALWAYS_INLINE
void
362 blend_func_separatei(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
363 GLenum sfactorA
, GLenum dfactorA
, bool no_error
)
365 GET_CURRENT_CONTEXT(ctx
);
368 if (!ctx
->Extensions
.ARB_draw_buffers_blend
) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlendFunc[Separate]i()");
373 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
374 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendFuncSeparatei(buffer=%u)",
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 */
386 if (!no_error
&& !validate_blend_factors(ctx
, "glBlendFuncSeparatei",
387 sfactorRGB
, dfactorRGB
,
388 sfactorA
, dfactorA
)) {
392 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlend
? 0 : _NEW_COLOR
);
393 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlend
;
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
;
405 _mesa_BlendFuncSeparateiARB_no_error(GLuint buf
, GLenum sfactorRGB
,
406 GLenum dfactorRGB
, GLenum sfactorA
,
409 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
415 * Set separate blend source/dest factors for one color buffer/target.
418 _mesa_BlendFuncSeparateiARB(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
419 GLenum sfactorA
, GLenum dfactorA
)
421 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
427 * Return true if \p mode is a legal blending equation, excluding
428 * GL_KHR_blend_equation_advanced modes.
431 legal_simple_blend_equation(const struct gl_context
*ctx
, GLenum mode
)
435 case GL_FUNC_SUBTRACT
:
436 case GL_FUNC_REVERSE_SUBTRACT
:
440 return ctx
->Extensions
.EXT_blend_minmax
;
446 static enum gl_advanced_blend_mode
447 advanced_blend_mode_from_gl_enum(GLenum mode
)
450 case GL_MULTIPLY_KHR
:
451 return BLEND_MULTIPLY
;
455 return BLEND_OVERLAY
;
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
;
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
;
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).
491 static enum gl_advanced_blend_mode
492 advanced_blend_mode(const struct gl_context
*ctx
, GLenum mode
)
494 return _mesa_has_KHR_blend_equation_advanced(ctx
) ?
495 advanced_blend_mode_from_gl_enum(mode
) : BLEND_NONE
;
498 /* This is really an extension function! */
500 _mesa_BlendEquation( GLenum mode
)
502 GET_CURRENT_CONTEXT(ctx
);
503 const unsigned numBuffers
= num_buffers(ctx
);
505 bool changed
= false;
506 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
508 if (MESA_VERBOSE
& VERBOSE_API
)
509 _mesa_debug(ctx
, "glBlendEquation(%s)\n",
510 _mesa_enum_to_string(mode
));
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
) {
523 /* only need to check 0th per-buffer state */
524 if (ctx
->Color
.Blend
[0].EquationRGB
!= mode
||
525 ctx
->Color
.Blend
[0].EquationA
!= mode
) {
534 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
535 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
539 _mesa_flush_vertices_for_blend_adv(ctx
, ctx
->Color
.BlendEnabled
,
542 for (buf
= 0; buf
< numBuffers
; buf
++) {
543 ctx
->Color
.Blend
[buf
].EquationRGB
= mode
;
544 ctx
->Color
.Blend
[buf
].EquationA
= mode
;
546 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
547 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
549 if (ctx
->Driver
.BlendEquationSeparate
)
550 ctx
->Driver
.BlendEquationSeparate(ctx
, mode
, mode
);
555 * Set blend equation for one color buffer/target.
558 blend_equationi(struct gl_context
*ctx
, GLuint buf
, GLenum mode
,
559 enum gl_advanced_blend_mode advanced_mode
)
561 if (ctx
->Color
.Blend
[buf
].EquationRGB
== mode
&&
562 ctx
->Color
.Blend
[buf
].EquationA
== mode
)
563 return; /* no change */
565 _mesa_flush_vertices_for_blend_adv(ctx
, ctx
->Color
.BlendEnabled
,
567 ctx
->Color
.Blend
[buf
].EquationRGB
= mode
;
568 ctx
->Color
.Blend
[buf
].EquationA
= mode
;
569 ctx
->Color
._BlendEquationPerBuffer
= GL_TRUE
;
572 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
577 _mesa_BlendEquationiARB_no_error(GLuint buf
, GLenum mode
)
579 GET_CURRENT_CONTEXT(ctx
);
581 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
582 blend_equationi(ctx
, buf
, mode
, advanced_mode
);
587 _mesa_BlendEquationiARB(GLuint buf
, GLenum mode
)
589 GET_CURRENT_CONTEXT(ctx
);
590 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
592 if (MESA_VERBOSE
& VERBOSE_API
)
593 _mesa_debug(ctx
, "glBlendEquationi(%u, %s)\n",
594 buf
, _mesa_enum_to_string(mode
));
596 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
597 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationi(buffer=%u)",
602 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
603 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationi");
607 blend_equationi(ctx
, buf
, mode
, advanced_mode
);
612 blend_equation_separate(struct gl_context
*ctx
, GLenum modeRGB
, GLenum modeA
,
615 const unsigned numBuffers
= num_buffers(ctx
);
617 bool changed
= false;
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
) {
629 /* only need to check 0th per-buffer state */
630 if (ctx
->Color
.Blend
[0].EquationRGB
!= modeRGB
||
631 ctx
->Color
.Blend
[0].EquationA
!= modeA
) {
640 if ((modeRGB
!= modeA
) && !ctx
->Extensions
.EXT_blend_equation_separate
) {
641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
642 "glBlendEquationSeparateEXT not supported by driver");
646 /* Only allow simple blending equations.
647 * The GL_KHR_blend_equation_advanced spec says:
649 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
650 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
652 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
653 _mesa_error(ctx
, GL_INVALID_ENUM
,
654 "glBlendEquationSeparateEXT(modeRGB)");
658 if (!legal_simple_blend_equation(ctx
, modeA
)) {
659 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeA)");
664 _mesa_flush_vertices_for_blend_state(ctx
);
666 for (buf
= 0; buf
< numBuffers
; buf
++) {
667 ctx
->Color
.Blend
[buf
].EquationRGB
= modeRGB
;
668 ctx
->Color
.Blend
[buf
].EquationA
= modeA
;
670 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
671 ctx
->Color
._AdvancedBlendMode
= BLEND_NONE
;
673 if (ctx
->Driver
.BlendEquationSeparate
)
674 ctx
->Driver
.BlendEquationSeparate(ctx
, modeRGB
, modeA
);
679 _mesa_BlendEquationSeparate_no_error(GLenum modeRGB
, GLenum modeA
)
681 GET_CURRENT_CONTEXT(ctx
);
682 blend_equation_separate(ctx
, modeRGB
, modeA
, true);
687 _mesa_BlendEquationSeparate(GLenum modeRGB
, GLenum modeA
)
689 GET_CURRENT_CONTEXT(ctx
);
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
));
696 blend_equation_separate(ctx
, modeRGB
, modeA
, false);
700 static ALWAYS_INLINE
void
701 blend_equation_separatei(struct gl_context
*ctx
, GLuint buf
, GLenum modeRGB
,
702 GLenum modeA
, bool no_error
)
704 if (ctx
->Color
.Blend
[buf
].EquationRGB
== modeRGB
&&
705 ctx
->Color
.Blend
[buf
].EquationA
== modeA
)
706 return; /* no change */
709 /* Only allow simple blending equations.
710 * The GL_KHR_blend_equation_advanced spec says:
712 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
713 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
715 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
716 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeRGB)");
720 if (!legal_simple_blend_equation(ctx
, modeA
)) {
721 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeA)");
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
;
735 _mesa_BlendEquationSeparateiARB_no_error(GLuint buf
, GLenum modeRGB
,
738 GET_CURRENT_CONTEXT(ctx
);
739 blend_equation_separatei(ctx
, buf
, modeRGB
, modeA
, true);
744 * Set separate blend equations for one color buffer/target.
747 _mesa_BlendEquationSeparateiARB(GLuint buf
, GLenum modeRGB
, GLenum modeA
)
749 GET_CURRENT_CONTEXT(ctx
);
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
));
756 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
757 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationSeparatei(buffer=%u)",
762 blend_equation_separatei(ctx
, buf
, modeRGB
, modeA
, false);
767 * Set the blending color.
769 * \param red red color component.
770 * \param green green color component.
771 * \param blue blue color component.
772 * \param alpha alpha color component.
774 * \sa glBlendColor().
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.
781 _mesa_BlendColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
784 GET_CURRENT_CONTEXT(ctx
);
791 if (TEST_EQ_4V(tmp
, ctx
->Color
.BlendColorUnclamped
))
794 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlendColor
? 0 : _NEW_COLOR
);
795 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlendColor
;
796 COPY_4FV( ctx
->Color
.BlendColorUnclamped
, tmp
);
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
);
803 if (ctx
->Driver
.BlendColor
)
804 ctx
->Driver
.BlendColor(ctx
, ctx
->Color
.BlendColor
);
809 * Specify the alpha test function.
811 * \param func alpha comparison function.
812 * \param ref reference value.
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.
819 _mesa_AlphaFunc( GLenum func
, GLclampf ref
)
821 GET_CURRENT_CONTEXT(ctx
);
823 if (MESA_VERBOSE
& VERBOSE_API
)
824 _mesa_debug(ctx
, "glAlphaFunc(%s, %f)\n",
825 _mesa_enum_to_string(func
), ref
);
827 if (ctx
->Color
.AlphaFunc
== func
&& ctx
->Color
.AlphaRefUnclamped
== ref
)
828 return; /* no change */
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
);
845 if (ctx
->Driver
.AlphaFunc
)
846 ctx
->Driver
.AlphaFunc(ctx
, func
, ctx
->Color
.AlphaRef
);
850 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAlphaFunc(func)" );
855 static const enum gl_logicop_mode color_logicop_mapping
[16] = {
858 COLOR_LOGICOP_AND_REVERSE
,
860 COLOR_LOGICOP_AND_INVERTED
,
866 COLOR_LOGICOP_INVERT
,
867 COLOR_LOGICOP_OR_REVERSE
,
868 COLOR_LOGICOP_COPY_INVERTED
,
869 COLOR_LOGICOP_OR_INVERTED
,
874 static ALWAYS_INLINE
void
875 logic_op(struct gl_context
*ctx
, GLenum opcode
, bool no_error
)
877 if (ctx
->Color
.LogicOp
== opcode
)
885 case GL_COPY_INVERTED
:
895 case GL_AND_INVERTED
:
900 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLogicOp" );
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
);
911 if (ctx
->Driver
.LogicOpcode
)
912 ctx
->Driver
.LogicOpcode(ctx
, ctx
->Color
._LogicOp
);
917 * Specify a logic pixel operation for color index rendering.
919 * \param opcode operation.
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.
927 _mesa_LogicOp( GLenum opcode
)
929 GET_CURRENT_CONTEXT(ctx
);
931 if (MESA_VERBOSE
& VERBOSE_API
)
932 _mesa_debug(ctx
, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode
));
934 logic_op(ctx
, opcode
, false);
939 _mesa_LogicOp_no_error(GLenum opcode
)
941 GET_CURRENT_CONTEXT(ctx
);
942 logic_op(ctx
, opcode
, true);
947 _mesa_IndexMask( GLuint mask
)
949 GET_CURRENT_CONTEXT(ctx
);
951 if (ctx
->Color
.IndexMask
== mask
)
954 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewColorMask
? 0 : _NEW_COLOR
);
955 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewColorMask
;
956 ctx
->Color
.IndexMask
= mask
;
961 * Enable or disable writing of frame buffer color components.
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.
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.
975 _mesa_ColorMask( GLboolean red
, GLboolean green
,
976 GLboolean blue
, GLboolean alpha
)
978 GET_CURRENT_CONTEXT(ctx
);
980 if (MESA_VERBOSE
& VERBOSE_API
)
981 _mesa_debug(ctx
, "glColorMask(%d, %d, %d, %d)\n",
982 red
, green
, blue
, alpha
);
984 GLbitfield mask
= (!!red
) |
988 mask
= _mesa_replicate_colormask(mask
, ctx
->Const
.MaxDrawBuffers
);
990 if (ctx
->Color
.ColorMask
== mask
)
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
);
998 if (ctx
->Driver
.ColorMask
)
999 ctx
->Driver
.ColorMask( ctx
, red
, green
, blue
, alpha
);
1004 * For GL_EXT_draw_buffers2 and GL3
1007 _mesa_ColorMaski(GLuint buf
, GLboolean red
, GLboolean green
,
1008 GLboolean blue
, GLboolean alpha
)
1010 GET_CURRENT_CONTEXT(ctx
);
1012 if (MESA_VERBOSE
& VERBOSE_API
)
1013 _mesa_debug(ctx
, "glColorMaski %u %d %d %d %d\n",
1014 buf
, red
, green
, blue
, alpha
);
1016 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
1017 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorMaski(buf=%u)", buf
);
1021 GLbitfield mask
= (!!red
) |
1026 if (GET_COLORMASK(ctx
->Color
.ColorMask
, buf
) == mask
)
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
);
1038 _mesa_ClampColor(GLenum target
, GLenum clamp
)
1040 GET_CURRENT_CONTEXT(ctx
);
1042 /* Check for both the extension and the GL version, since the Intel driver
1043 * does not advertise the extension in core profiles.
1045 if (ctx
->Version
<= 30 && !ctx
->Extensions
.ARB_color_buffer_float
) {
1046 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClampColor()");
1050 if (clamp
!= GL_TRUE
&& clamp
!= GL_FALSE
&& clamp
!= GL_FIXED_ONLY_ARB
) {
1051 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColorARB(clamp)");
1056 case GL_CLAMP_VERTEX_COLOR_ARB
:
1057 if (ctx
->API
== API_OPENGL_CORE
)
1059 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
1060 ctx
->Light
.ClampVertexColor
= clamp
;
1061 _mesa_update_clamp_vertex_color(ctx
, ctx
->DrawBuffer
);
1063 case GL_CLAMP_FRAGMENT_COLOR_ARB
:
1064 if (ctx
->API
== API_OPENGL_CORE
)
1066 if (ctx
->Color
.ClampFragmentColor
!= clamp
) {
1067 ctx
->Color
.ClampFragmentColor
= clamp
;
1068 _mesa_update_clamp_fragment_color(ctx
, ctx
->DrawBuffer
);
1071 case GL_CLAMP_READ_COLOR_ARB
:
1072 ctx
->Color
.ClampReadColor
= clamp
;
1080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColor(%s)",
1081 _mesa_enum_to_string(target
));
1085 get_clamp_color(const struct gl_framebuffer
*fb
, GLenum clamp
)
1087 if (clamp
== GL_TRUE
|| clamp
== GL_FALSE
)
1090 assert(clamp
== GL_FIXED_ONLY
);
1094 return fb
->_AllColorBuffersFixedPoint
;
1098 _mesa_get_clamp_fragment_color(const struct gl_context
*ctx
,
1099 const struct gl_framebuffer
*drawFb
)
1101 return get_clamp_color(drawFb
, ctx
->Color
.ClampFragmentColor
);
1105 _mesa_get_clamp_vertex_color(const struct gl_context
*ctx
,
1106 const struct gl_framebuffer
*drawFb
)
1108 return get_clamp_color(drawFb
, ctx
->Light
.ClampVertexColor
);
1112 _mesa_get_clamp_read_color(const struct gl_context
*ctx
,
1113 const struct gl_framebuffer
*readFb
)
1115 return get_clamp_color(readFb
, ctx
->Color
.ClampReadColor
);
1119 * Update the ctx->Color._ClampFragmentColor field
1122 _mesa_update_clamp_fragment_color(struct gl_context
*ctx
,
1123 const struct gl_framebuffer
*drawFb
)
1128 * - there is no colorbuffer
1129 * - all colorbuffers are unsigned normalized, so clamping has no effect
1130 * - there is an integer colorbuffer
1132 if (!drawFb
|| !drawFb
->_HasSNormOrFloatColorBuffer
||
1133 drawFb
->_IntegerBuffers
)
1136 clamp
= _mesa_get_clamp_fragment_color(ctx
, drawFb
);
1138 if (ctx
->Color
._ClampFragmentColor
== clamp
)
1141 ctx
->NewState
|= _NEW_FRAG_CLAMP
; /* for state constants */
1142 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewFragClamp
;
1143 ctx
->Color
._ClampFragmentColor
= clamp
;
1147 * Update the ctx->Color._ClampVertexColor field
1150 _mesa_update_clamp_vertex_color(struct gl_context
*ctx
,
1151 const struct gl_framebuffer
*drawFb
)
1153 ctx
->Light
._ClampVertexColor
=
1154 _mesa_get_clamp_vertex_color(ctx
, drawFb
);
1158 * Returns an appropriate mesa_format for color rendering based on the
1159 * GL_FRAMEBUFFER_SRGB state.
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.
1167 _mesa_get_render_format(const struct gl_context
*ctx
, mesa_format format
)
1169 if (ctx
->Color
.sRGBEnabled
)
1172 return _mesa_get_srgb_format_linear(format
);
1175 /**********************************************************************/
1176 /** \name Initialization */
1180 * Initialization of the context's Color attribute group.
1182 * \param ctx GL context.
1184 * Initializes the related fields in the context color attribute group,
1185 * __struct gl_contextRec::Color.
1187 void _mesa_init_color( struct gl_context
* ctx
)
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
;
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
;
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
;
1222 ctx
->Color
.DrawBuffer
[0] = GL_FRONT
;
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
;
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.
1233 ctx
->Color
.sRGBEnabled
= _mesa_is_gles(ctx
);
1235 ctx
->Color
.BlendCoherent
= true;