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.
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
46 legal_src_factor(const struct gl_context
*ctx
, GLenum factor
)
50 case GL_ONE_MINUS_SRC_COLOR
:
54 case GL_ONE_MINUS_DST_COLOR
:
56 case GL_ONE_MINUS_SRC_ALPHA
:
58 case GL_ONE_MINUS_DST_ALPHA
:
59 case GL_SRC_ALPHA_SATURATE
:
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
;
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
;
79 * Check if given blend destination factor is legal.
80 * \return GL_TRUE if legal, GL_FALSE otherwise.
83 legal_dst_factor(const struct gl_context
*ctx
, GLenum factor
)
87 case GL_ONE_MINUS_DST_COLOR
:
91 case GL_ONE_MINUS_SRC_COLOR
:
93 case GL_ONE_MINUS_SRC_ALPHA
:
95 case GL_ONE_MINUS_DST_ALPHA
:
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
);
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
;
119 * Check if src/dest RGB/A blend factors are legal. If not generate
121 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
124 validate_blend_factors(struct gl_context
*ctx
, const char *func
,
125 GLenum sfactorRGB
, GLenum dfactorRGB
,
126 GLenum sfactorA
, GLenum dfactorA
)
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
));
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
));
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
));
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
));
161 blend_factor_is_dual_src(GLenum factor
)
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
);
170 update_uses_dual_src(struct gl_context
*ctx
, int buf
)
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
));
181 * Return the number of per-buffer blend states to update in
182 * glBlendFunc, glBlendFuncSeparate, glBlendEquation, etc.
184 static inline unsigned
185 num_buffers(const struct gl_context
*ctx
)
187 return ctx
->Extensions
.ARB_draw_buffers_blend
188 ? ctx
->Const
.MaxDrawBuffers
: 1;
192 /* Returns true if there was no change */
194 skip_blend_state_update(const struct gl_context
*ctx
,
195 GLenum sfactorRGB
, GLenum dfactorRGB
,
196 GLenum sfactorA
, GLenum dfactorA
)
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
);
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
) {
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
) {
227 blend_func_separate(struct gl_context
*ctx
,
228 GLenum sfactorRGB
, GLenum dfactorRGB
,
229 GLenum sfactorA
, GLenum dfactorA
)
231 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlend
? 0 : _NEW_COLOR
);
232 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlend
;
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
;
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
;
247 ctx
->Color
._BlendFuncPerBuffer
= GL_FALSE
;
249 if (ctx
->Driver
.BlendFuncSeparate
) {
250 ctx
->Driver
.BlendFuncSeparate(ctx
, sfactorRGB
, dfactorRGB
,
257 * Specify the blending operation.
259 * \param sfactor source factor operator.
260 * \param dfactor destination factor operator.
262 * \sa glBlendFunc, glBlendFuncSeparateEXT
265 _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
267 GET_CURRENT_CONTEXT(ctx
);
269 if (skip_blend_state_update(ctx
, sfactor
, dfactor
, sfactor
, dfactor
))
272 if (!validate_blend_factors(ctx
, "glBlendFunc",
273 sfactor
, dfactor
, sfactor
, dfactor
)) {
277 blend_func_separate(ctx
, sfactor
, dfactor
, sfactor
, dfactor
);
282 _mesa_BlendFunc_no_error(GLenum sfactor
, GLenum dfactor
)
284 GET_CURRENT_CONTEXT(ctx
);
286 if (skip_blend_state_update(ctx
, sfactor
, dfactor
, sfactor
, dfactor
))
289 blend_func_separate(ctx
, sfactor
, dfactor
, sfactor
, dfactor
);
294 * Set the separate blend source/dest factors for all draw buffers.
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.
302 _mesa_BlendFuncSeparate( GLenum sfactorRGB
, GLenum dfactorRGB
,
303 GLenum sfactorA
, GLenum dfactorA
)
305 GET_CURRENT_CONTEXT(ctx
);
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
));
316 if (skip_blend_state_update(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
))
319 if (!validate_blend_factors(ctx
, "glBlendFuncSeparate",
320 sfactorRGB
, dfactorRGB
,
321 sfactorA
, dfactorA
)) {
325 blend_func_separate(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
);
330 _mesa_BlendFuncSeparate_no_error(GLenum sfactorRGB
, GLenum dfactorRGB
,
331 GLenum sfactorA
, GLenum dfactorA
)
333 GET_CURRENT_CONTEXT(ctx
);
335 if (skip_blend_state_update(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
))
338 blend_func_separate(ctx
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
);
343 _mesa_BlendFunciARB_no_error(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
345 _mesa_BlendFuncSeparateiARB_no_error(buf
, sfactor
, dfactor
, sfactor
,
351 * Set blend source/dest factors for one color buffer/target.
354 _mesa_BlendFunciARB(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
356 _mesa_BlendFuncSeparateiARB(buf
, sfactor
, dfactor
, sfactor
, dfactor
);
360 static ALWAYS_INLINE
void
361 blend_func_separatei(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
362 GLenum sfactorA
, GLenum dfactorA
, bool no_error
)
364 GET_CURRENT_CONTEXT(ctx
);
367 if (!ctx
->Extensions
.ARB_draw_buffers_blend
) {
368 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlendFunc[Separate]i()");
372 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
373 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendFuncSeparatei(buffer=%u)",
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 */
385 if (!no_error
&& !validate_blend_factors(ctx
, "glBlendFuncSeparatei",
386 sfactorRGB
, dfactorRGB
,
387 sfactorA
, dfactorA
)) {
391 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlend
? 0 : _NEW_COLOR
);
392 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlend
;
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
;
404 _mesa_BlendFuncSeparateiARB_no_error(GLuint buf
, GLenum sfactorRGB
,
405 GLenum dfactorRGB
, GLenum sfactorA
,
408 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
414 * Set separate blend source/dest factors for one color buffer/target.
417 _mesa_BlendFuncSeparateiARB(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
418 GLenum sfactorA
, GLenum dfactorA
)
420 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
426 * Return true if \p mode is a legal blending equation, excluding
427 * GL_KHR_blend_equation_advanced modes.
430 legal_simple_blend_equation(const struct gl_context
*ctx
, GLenum mode
)
434 case GL_FUNC_SUBTRACT
:
435 case GL_FUNC_REVERSE_SUBTRACT
:
439 return ctx
->Extensions
.EXT_blend_minmax
;
445 static enum gl_advanced_blend_mode
446 advanced_blend_mode_from_gl_enum(GLenum mode
)
449 case GL_MULTIPLY_KHR
:
450 return BLEND_MULTIPLY
;
454 return BLEND_OVERLAY
;
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
;
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
;
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).
490 static enum gl_advanced_blend_mode
491 advanced_blend_mode(const struct gl_context
*ctx
, GLenum mode
)
493 return _mesa_has_KHR_blend_equation_advanced(ctx
) ?
494 advanced_blend_mode_from_gl_enum(mode
) : BLEND_NONE
;
497 /* This is really an extension function! */
499 _mesa_BlendEquation( GLenum mode
)
501 GET_CURRENT_CONTEXT(ctx
);
502 const unsigned numBuffers
= num_buffers(ctx
);
504 bool changed
= false;
505 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
507 if (MESA_VERBOSE
& VERBOSE_API
)
508 _mesa_debug(ctx
, "glBlendEquation(%s)\n",
509 _mesa_enum_to_string(mode
));
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
) {
522 /* only need to check 0th per-buffer state */
523 if (ctx
->Color
.Blend
[0].EquationRGB
!= mode
||
524 ctx
->Color
.Blend
[0].EquationA
!= mode
) {
533 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
534 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
538 _mesa_flush_vertices_for_blend_state(ctx
);
540 for (buf
= 0; buf
< numBuffers
; buf
++) {
541 ctx
->Color
.Blend
[buf
].EquationRGB
= mode
;
542 ctx
->Color
.Blend
[buf
].EquationA
= mode
;
544 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
545 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
547 if (ctx
->Driver
.BlendEquationSeparate
)
548 ctx
->Driver
.BlendEquationSeparate(ctx
, mode
, mode
);
553 * Set blend equation for one color buffer/target.
556 blend_equationi(struct gl_context
*ctx
, GLuint buf
, GLenum mode
,
557 enum gl_advanced_blend_mode advanced_mode
)
559 if (ctx
->Color
.Blend
[buf
].EquationRGB
== mode
&&
560 ctx
->Color
.Blend
[buf
].EquationA
== mode
)
561 return; /* no change */
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
;
569 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
573 _mesa_BlendEquationiARB(GLuint buf
, GLenum mode
)
575 GET_CURRENT_CONTEXT(ctx
);
576 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
578 if (MESA_VERBOSE
& VERBOSE_API
)
579 _mesa_debug(ctx
, "glBlendEquationi(%u, %s)\n",
580 buf
, _mesa_enum_to_string(mode
));
582 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
583 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationi(buffer=%u)",
588 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
589 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationi");
593 blend_equationi(ctx
, buf
, mode
, advanced_mode
);
598 _mesa_BlendEquationSeparate( GLenum modeRGB
, GLenum modeA
)
600 GET_CURRENT_CONTEXT(ctx
);
601 const unsigned numBuffers
= num_buffers(ctx
);
603 bool changed
= false;
605 if (MESA_VERBOSE
& VERBOSE_API
)
606 _mesa_debug(ctx
, "glBlendEquationSeparateEXT(%s %s)\n",
607 _mesa_enum_to_string(modeRGB
),
608 _mesa_enum_to_string(modeA
));
610 if (ctx
->Color
._BlendEquationPerBuffer
) {
611 /* Check all per-buffer states */
612 for (buf
= 0; buf
< numBuffers
; buf
++) {
613 if (ctx
->Color
.Blend
[buf
].EquationRGB
!= modeRGB
||
614 ctx
->Color
.Blend
[buf
].EquationA
!= modeA
) {
621 /* only need to check 0th per-buffer state */
622 if (ctx
->Color
.Blend
[0].EquationRGB
!= modeRGB
||
623 ctx
->Color
.Blend
[0].EquationA
!= modeA
) {
631 if ( (modeRGB
!= modeA
) && !ctx
->Extensions
.EXT_blend_equation_separate
) {
632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
633 "glBlendEquationSeparateEXT not supported by driver");
637 /* Only allow simple blending equations.
638 * The GL_KHR_blend_equation_advanced spec says:
640 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
641 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
643 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
644 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeRGB)");
648 if (!legal_simple_blend_equation(ctx
, modeA
)) {
649 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeA)");
653 _mesa_flush_vertices_for_blend_state(ctx
);
655 for (buf
= 0; buf
< numBuffers
; buf
++) {
656 ctx
->Color
.Blend
[buf
].EquationRGB
= modeRGB
;
657 ctx
->Color
.Blend
[buf
].EquationA
= modeA
;
659 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
660 ctx
->Color
._AdvancedBlendMode
= BLEND_NONE
;
662 if (ctx
->Driver
.BlendEquationSeparate
)
663 ctx
->Driver
.BlendEquationSeparate(ctx
, modeRGB
, modeA
);
668 blend_equation_separatei(struct gl_context
*ctx
, GLuint buf
, GLenum modeRGB
,
671 if (ctx
->Color
.Blend
[buf
].EquationRGB
== modeRGB
&&
672 ctx
->Color
.Blend
[buf
].EquationA
== modeA
)
673 return; /* no change */
675 _mesa_flush_vertices_for_blend_state(ctx
);
676 ctx
->Color
.Blend
[buf
].EquationRGB
= modeRGB
;
677 ctx
->Color
.Blend
[buf
].EquationA
= modeA
;
678 ctx
->Color
._BlendEquationPerBuffer
= GL_TRUE
;
679 ctx
->Color
._AdvancedBlendMode
= BLEND_NONE
;
684 _mesa_BlendEquationSeparateiARB_no_error(GLuint buf
, GLenum modeRGB
,
687 GET_CURRENT_CONTEXT(ctx
);
688 blend_equation_separatei(ctx
, buf
, modeRGB
, modeA
);
693 * Set separate blend equations for one color buffer/target.
696 _mesa_BlendEquationSeparateiARB(GLuint buf
, GLenum modeRGB
, GLenum modeA
)
698 GET_CURRENT_CONTEXT(ctx
);
700 if (MESA_VERBOSE
& VERBOSE_API
)
701 _mesa_debug(ctx
, "glBlendEquationSeparatei(%u, %s %s)\n", buf
,
702 _mesa_enum_to_string(modeRGB
),
703 _mesa_enum_to_string(modeA
));
705 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
706 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationSeparatei(buffer=%u)",
711 /* Only allow simple blending equations.
712 * The GL_KHR_blend_equation_advanced spec says:
714 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
715 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
717 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
718 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeRGB)");
722 if (!legal_simple_blend_equation(ctx
, modeA
)) {
723 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeA)");
727 blend_equation_separatei(ctx
, buf
, modeRGB
, modeA
);
732 * Set the blending color.
734 * \param red red color component.
735 * \param green green color component.
736 * \param blue blue color component.
737 * \param alpha alpha color component.
739 * \sa glBlendColor().
741 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
742 * change, flushes the vertices and notifies the driver via
743 * dd_function_table::BlendColor callback.
746 _mesa_BlendColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
749 GET_CURRENT_CONTEXT(ctx
);
756 if (TEST_EQ_4V(tmp
, ctx
->Color
.BlendColorUnclamped
))
759 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewBlendColor
? 0 : _NEW_COLOR
);
760 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewBlendColor
;
761 COPY_4FV( ctx
->Color
.BlendColorUnclamped
, tmp
);
763 ctx
->Color
.BlendColor
[0] = CLAMP(tmp
[0], 0.0F
, 1.0F
);
764 ctx
->Color
.BlendColor
[1] = CLAMP(tmp
[1], 0.0F
, 1.0F
);
765 ctx
->Color
.BlendColor
[2] = CLAMP(tmp
[2], 0.0F
, 1.0F
);
766 ctx
->Color
.BlendColor
[3] = CLAMP(tmp
[3], 0.0F
, 1.0F
);
768 if (ctx
->Driver
.BlendColor
)
769 ctx
->Driver
.BlendColor(ctx
, ctx
->Color
.BlendColor
);
774 * Specify the alpha test function.
776 * \param func alpha comparison function.
777 * \param ref reference value.
779 * Verifies the parameters and updates gl_colorbuffer_attrib.
780 * On a change, flushes the vertices and notifies the driver via
781 * dd_function_table::AlphaFunc callback.
784 _mesa_AlphaFunc( GLenum func
, GLclampf ref
)
786 GET_CURRENT_CONTEXT(ctx
);
788 if (MESA_VERBOSE
& VERBOSE_API
)
789 _mesa_debug(ctx
, "glAlphaFunc(%s, %f)\n",
790 _mesa_enum_to_string(func
), ref
);
792 if (ctx
->Color
.AlphaFunc
== func
&& ctx
->Color
.AlphaRefUnclamped
== ref
)
793 return; /* no change */
804 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewAlphaTest
? 0 : _NEW_COLOR
);
805 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAlphaTest
;
806 ctx
->Color
.AlphaFunc
= func
;
807 ctx
->Color
.AlphaRefUnclamped
= ref
;
808 ctx
->Color
.AlphaRef
= CLAMP(ref
, 0.0F
, 1.0F
);
810 if (ctx
->Driver
.AlphaFunc
)
811 ctx
->Driver
.AlphaFunc(ctx
, func
, ctx
->Color
.AlphaRef
);
815 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAlphaFunc(func)" );
822 logic_op(struct gl_context
*ctx
, GLenum opcode
)
824 if (ctx
->Color
.LogicOp
== opcode
)
827 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewLogicOp
? 0 : _NEW_COLOR
);
828 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewLogicOp
;
829 ctx
->Color
.LogicOp
= opcode
;
831 if (ctx
->Driver
.LogicOpcode
)
832 ctx
->Driver
.LogicOpcode(ctx
, opcode
);
837 * Specify a logic pixel operation for color index rendering.
839 * \param opcode operation.
841 * Verifies that \p opcode is a valid enum and updates
842 * gl_colorbuffer_attrib::LogicOp.
843 * On a change, flushes the vertices and notifies the driver via the
844 * dd_function_table::LogicOpcode callback.
847 _mesa_LogicOp( GLenum opcode
)
849 GET_CURRENT_CONTEXT(ctx
);
851 if (MESA_VERBOSE
& VERBOSE_API
)
852 _mesa_debug(ctx
, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode
));
858 case GL_COPY_INVERTED
:
868 case GL_AND_INVERTED
:
873 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLogicOp" );
877 logic_op(ctx
, opcode
);
882 _mesa_LogicOp_no_error(GLenum opcode
)
884 GET_CURRENT_CONTEXT(ctx
);
885 logic_op(ctx
, opcode
);
890 _mesa_IndexMask( GLuint mask
)
892 GET_CURRENT_CONTEXT(ctx
);
894 if (ctx
->Color
.IndexMask
== mask
)
897 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewColorMask
? 0 : _NEW_COLOR
);
898 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewColorMask
;
899 ctx
->Color
.IndexMask
= mask
;
904 * Enable or disable writing of frame buffer color components.
906 * \param red whether to mask writing of the red color component.
907 * \param green whether to mask writing of the green color component.
908 * \param blue whether to mask writing of the blue color component.
909 * \param alpha whether to mask writing of the alpha color component.
913 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
914 * change, flushes the vertices and notifies the driver via the
915 * dd_function_table::ColorMask callback.
918 _mesa_ColorMask( GLboolean red
, GLboolean green
,
919 GLboolean blue
, GLboolean alpha
)
921 GET_CURRENT_CONTEXT(ctx
);
926 if (MESA_VERBOSE
& VERBOSE_API
)
927 _mesa_debug(ctx
, "glColorMask(%d, %d, %d, %d)\n",
928 red
, green
, blue
, alpha
);
930 /* Shouldn't have any information about channel depth in core mesa
931 * -- should probably store these as the native booleans:
933 tmp
[RCOMP
] = red
? 0xff : 0x0;
934 tmp
[GCOMP
] = green
? 0xff : 0x0;
935 tmp
[BCOMP
] = blue
? 0xff : 0x0;
936 tmp
[ACOMP
] = alpha
? 0xff : 0x0;
939 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
940 if (!TEST_EQ_4V(tmp
, ctx
->Color
.ColorMask
[i
])) {
942 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewColorMask
? 0 : _NEW_COLOR
);
943 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewColorMask
;
946 COPY_4UBV(ctx
->Color
.ColorMask
[i
], tmp
);
950 if (ctx
->Driver
.ColorMask
)
951 ctx
->Driver
.ColorMask( ctx
, red
, green
, blue
, alpha
);
956 * For GL_EXT_draw_buffers2 and GL3
959 _mesa_ColorMaski( GLuint buf
, GLboolean red
, GLboolean green
,
960 GLboolean blue
, GLboolean alpha
)
963 GET_CURRENT_CONTEXT(ctx
);
965 if (MESA_VERBOSE
& VERBOSE_API
)
966 _mesa_debug(ctx
, "glColorMaskIndexed %u %d %d %d %d\n",
967 buf
, red
, green
, blue
, alpha
);
969 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
970 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorMaskIndexed(buf=%u)", buf
);
974 /* Shouldn't have any information about channel depth in core mesa
975 * -- should probably store these as the native booleans:
977 tmp
[RCOMP
] = red
? 0xff : 0x0;
978 tmp
[GCOMP
] = green
? 0xff : 0x0;
979 tmp
[BCOMP
] = blue
? 0xff : 0x0;
980 tmp
[ACOMP
] = alpha
? 0xff : 0x0;
982 if (TEST_EQ_4V(tmp
, ctx
->Color
.ColorMask
[buf
]))
985 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewColorMask
? 0 : _NEW_COLOR
);
986 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewColorMask
;
987 COPY_4UBV(ctx
->Color
.ColorMask
[buf
], tmp
);
992 _mesa_ClampColor(GLenum target
, GLenum clamp
)
994 GET_CURRENT_CONTEXT(ctx
);
996 /* Check for both the extension and the GL version, since the Intel driver
997 * does not advertise the extension in core profiles.
999 if (ctx
->Version
<= 30 && !ctx
->Extensions
.ARB_color_buffer_float
) {
1000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClampColor()");
1004 if (clamp
!= GL_TRUE
&& clamp
!= GL_FALSE
&& clamp
!= GL_FIXED_ONLY_ARB
) {
1005 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColorARB(clamp)");
1010 case GL_CLAMP_VERTEX_COLOR_ARB
:
1011 if (ctx
->API
== API_OPENGL_CORE
)
1013 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
1014 ctx
->Light
.ClampVertexColor
= clamp
;
1015 _mesa_update_clamp_vertex_color(ctx
, ctx
->DrawBuffer
);
1017 case GL_CLAMP_FRAGMENT_COLOR_ARB
:
1018 if (ctx
->API
== API_OPENGL_CORE
)
1020 FLUSH_VERTICES(ctx
, _NEW_FRAG_CLAMP
);
1021 ctx
->Color
.ClampFragmentColor
= clamp
;
1022 _mesa_update_clamp_fragment_color(ctx
, ctx
->DrawBuffer
);
1024 case GL_CLAMP_READ_COLOR_ARB
:
1025 ctx
->Color
.ClampReadColor
= clamp
;
1033 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColor(%s)",
1034 _mesa_enum_to_string(target
));
1038 get_clamp_color(const struct gl_framebuffer
*fb
, GLenum clamp
)
1040 if (clamp
== GL_TRUE
|| clamp
== GL_FALSE
)
1043 assert(clamp
== GL_FIXED_ONLY
);
1047 return fb
->_AllColorBuffersFixedPoint
;
1051 _mesa_get_clamp_fragment_color(const struct gl_context
*ctx
,
1052 const struct gl_framebuffer
*drawFb
)
1054 return get_clamp_color(drawFb
, ctx
->Color
.ClampFragmentColor
);
1058 _mesa_get_clamp_vertex_color(const struct gl_context
*ctx
,
1059 const struct gl_framebuffer
*drawFb
)
1061 return get_clamp_color(drawFb
, ctx
->Light
.ClampVertexColor
);
1065 _mesa_get_clamp_read_color(const struct gl_context
*ctx
,
1066 const struct gl_framebuffer
*readFb
)
1068 return get_clamp_color(readFb
, ctx
->Color
.ClampReadColor
);
1072 * Update the ctx->Color._ClampFragmentColor field
1075 _mesa_update_clamp_fragment_color(struct gl_context
*ctx
,
1076 const struct gl_framebuffer
*drawFb
)
1079 * - there is no colorbuffer
1080 * - all colorbuffers are unsigned normalized, so clamping has no effect
1081 * - there is an integer colorbuffer
1083 if (!drawFb
|| !drawFb
->_HasSNormOrFloatColorBuffer
||
1084 drawFb
->_IntegerBuffers
)
1085 ctx
->Color
._ClampFragmentColor
= GL_FALSE
;
1087 ctx
->Color
._ClampFragmentColor
=
1088 _mesa_get_clamp_fragment_color(ctx
, drawFb
);
1092 * Update the ctx->Color._ClampVertexColor field
1095 _mesa_update_clamp_vertex_color(struct gl_context
*ctx
,
1096 const struct gl_framebuffer
*drawFb
)
1098 ctx
->Light
._ClampVertexColor
=
1099 _mesa_get_clamp_vertex_color(ctx
, drawFb
);
1103 * Returns an appropriate mesa_format for color rendering based on the
1104 * GL_FRAMEBUFFER_SRGB state.
1106 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
1107 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
1108 * overriding the format of the surface. This is a helper for doing the
1109 * surface format override variant.
1112 _mesa_get_render_format(const struct gl_context
*ctx
, mesa_format format
)
1114 if (ctx
->Color
.sRGBEnabled
)
1117 return _mesa_get_srgb_format_linear(format
);
1120 /**********************************************************************/
1121 /** \name Initialization */
1125 * Initialization of the context's Color attribute group.
1127 * \param ctx GL context.
1129 * Initializes the related fields in the context color attribute group,
1130 * __struct gl_contextRec::Color.
1132 void _mesa_init_color( struct gl_context
* ctx
)
1136 /* Color buffer group */
1137 ctx
->Color
.IndexMask
= ~0u;
1138 memset(ctx
->Color
.ColorMask
, 0xff, sizeof(ctx
->Color
.ColorMask
));
1139 ctx
->Color
.ClearIndex
= 0;
1140 ASSIGN_4V( ctx
->Color
.ClearColor
.f
, 0, 0, 0, 0 );
1141 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
1142 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
1143 ctx
->Color
.AlphaRef
= 0;
1144 ctx
->Color
.BlendEnabled
= 0x0;
1145 for (i
= 0; i
< ARRAY_SIZE(ctx
->Color
.Blend
); i
++) {
1146 ctx
->Color
.Blend
[i
].SrcRGB
= GL_ONE
;
1147 ctx
->Color
.Blend
[i
].DstRGB
= GL_ZERO
;
1148 ctx
->Color
.Blend
[i
].SrcA
= GL_ONE
;
1149 ctx
->Color
.Blend
[i
].DstA
= GL_ZERO
;
1150 ctx
->Color
.Blend
[i
].EquationRGB
= GL_FUNC_ADD
;
1151 ctx
->Color
.Blend
[i
].EquationA
= GL_FUNC_ADD
;
1153 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
1154 ASSIGN_4V( ctx
->Color
.BlendColorUnclamped
, 0.0, 0.0, 0.0, 0.0 );
1155 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
1156 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
1157 ctx
->Color
.LogicOp
= GL_COPY
;
1158 ctx
->Color
.DitherFlag
= GL_TRUE
;
1160 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1161 * the front or the back buffer depending on the config */
1162 if (ctx
->Visual
.doubleBufferMode
|| _mesa_is_gles(ctx
)) {
1163 ctx
->Color
.DrawBuffer
[0] = GL_BACK
;
1166 ctx
->Color
.DrawBuffer
[0] = GL_FRONT
;
1169 ctx
->Color
.ClampFragmentColor
= ctx
->API
== API_OPENGL_COMPAT
?
1170 GL_FIXED_ONLY_ARB
: GL_FALSE
;
1171 ctx
->Color
._ClampFragmentColor
= GL_FALSE
;
1172 ctx
->Color
.ClampReadColor
= GL_FIXED_ONLY_ARB
;
1174 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1175 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1177 ctx
->Color
.sRGBEnabled
= _mesa_is_gles(ctx
);
1179 ctx
->Color
.BlendCoherent
= true;